2017-07-05 08:54:49 +00:00
|
|
|
#pragma once
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
namespace crypto
|
|
|
|
{
|
|
|
|
namespace aes
|
|
|
|
{
|
|
|
|
const size_t kAes128KeySize = 0x10;
|
|
|
|
const size_t kAesBlockSize = 0x10;
|
|
|
|
const size_t KAesCcmNonceSize = 0xc;
|
|
|
|
|
|
|
|
#pragma pack (push, 1)
|
|
|
|
struct sAes128Key
|
|
|
|
{
|
|
|
|
uint8_t key[kAes128KeySize];
|
|
|
|
|
|
|
|
void set(const uint8_t key[kAes128KeySize])
|
|
|
|
{
|
|
|
|
memcpy(this->key, key, kAes128KeySize);
|
|
|
|
}
|
|
|
|
|
2017-07-06 10:46:36 +00:00
|
|
|
bool compare(const sAes128Key& other) const
|
|
|
|
{
|
|
|
|
return memcmp(this->key, other.key, kAes128KeySize) == 0;
|
|
|
|
}
|
|
|
|
|
2017-07-05 08:54:49 +00:00
|
|
|
void operator=(const sAes128Key& other)
|
|
|
|
{
|
|
|
|
set(other.key);
|
|
|
|
}
|
2017-07-06 10:46:36 +00:00
|
|
|
bool operator==(const sAes128Key& other) const
|
|
|
|
{
|
|
|
|
return compare(other);
|
|
|
|
}
|
|
|
|
bool operator!=(const sAes128Key& other) const
|
|
|
|
{
|
|
|
|
return !compare(other);
|
|
|
|
}
|
|
|
|
|
2017-07-05 08:54:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct sAesXts128Key
|
|
|
|
{
|
2018-04-07 07:58:07 +00:00
|
|
|
uint8_t key[2][kAes128KeySize];
|
|
|
|
|
|
|
|
void set(const uint8_t key_0[kAes128KeySize], const uint8_t key_1[kAes128KeySize])
|
|
|
|
{
|
|
|
|
memcpy(this->key[0], key_0, kAes128KeySize);
|
|
|
|
memcpy(this->key[1], key_1, kAes128KeySize);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool compare(const sAesXts128Key& other) const
|
|
|
|
{
|
|
|
|
return memcmp(this->key[0], other.key[0], kAes128KeySize) == 0 && memcmp(this->key[1], other.key[1], kAes128KeySize) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator=(const sAesXts128Key& other)
|
|
|
|
{
|
|
|
|
set(other.key[0], other.key[1]);
|
|
|
|
}
|
|
|
|
bool operator==(const sAesXts128Key& other) const
|
|
|
|
{
|
|
|
|
return compare(other);
|
|
|
|
}
|
|
|
|
bool operator!=(const sAesXts128Key& other) const
|
|
|
|
{
|
|
|
|
return !compare(other);
|
|
|
|
}
|
2017-07-05 08:54:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct sAesIvCtr
|
|
|
|
{
|
|
|
|
uint8_t iv[kAesBlockSize];
|
|
|
|
|
|
|
|
void set(const uint8_t iv[kAes128KeySize])
|
|
|
|
{
|
|
|
|
memcpy(this->iv, iv, kAes128KeySize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator=(const sAesIvCtr& other)
|
|
|
|
{
|
|
|
|
set(other.iv);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#pragma pack (pop)
|
|
|
|
|
|
|
|
// aes-128
|
|
|
|
void AesEcbDecrypt(const uint8_t* in, uint64_t size, const uint8_t key[kAes128KeySize], uint8_t* out);
|
|
|
|
void AesEcbEncrypt(const uint8_t* in, uint64_t size, const uint8_t key[kAes128KeySize], uint8_t* out);
|
|
|
|
|
|
|
|
void AesCtr(const uint8_t* in, uint64_t size, const uint8_t key[kAes128KeySize], uint8_t ctr[kAesBlockSize], uint8_t* out);
|
|
|
|
void AesIncrementCounter(const uint8_t in[kAesBlockSize], size_t block_num, uint8_t out[kAesBlockSize]);
|
|
|
|
|
|
|
|
void AesCbcDecrypt(const uint8_t* in, uint64_t size, const uint8_t key[kAes128KeySize], uint8_t iv[kAesBlockSize], uint8_t* out);
|
|
|
|
void AesCbcEncrypt(const uint8_t* in, uint64_t size, const uint8_t key[kAes128KeySize], uint8_t iv[kAesBlockSize], uint8_t* out);
|
|
|
|
|
|
|
|
void AesXtsDecryptSector(const uint8_t* in, uint64_t sector_size, const uint8_t key1[kAes128KeySize], const uint8_t key2[kAes128KeySize], uint8_t tweak[kAesBlockSize], uint8_t* out);
|
|
|
|
void AesXtsEncryptSector(const uint8_t* in, uint64_t sector_size, const uint8_t key1[kAes128KeySize], const uint8_t key2[kAes128KeySize], uint8_t tweak[kAesBlockSize], uint8_t* out);
|
2017-07-21 10:26:52 +00:00
|
|
|
void AesXtsMakeTweak(uint8_t tweak[kAesBlockSize], size_t block_index);
|
2017-07-05 08:54:49 +00:00
|
|
|
|
|
|
|
void GaloisFunc(uint8_t x[kAesBlockSize]);
|
|
|
|
}
|
|
|
|
}
|