22 memset(&
ctx, 0,
sizeof(
ctx));
37 memset(&
ctx, 0,
sizeof(
ctx));
52 memset(&
ctx, 0,
sizeof(
ctx));
67 memset(&
ctx, 0,
sizeof(
ctx));
77 static int CBCEncrypt(
const T& enc,
const unsigned char iv[AES_BLOCKSIZE],
const unsigned char*
data,
int size,
bool pad,
unsigned char* out)
80 int padsize =
size % AES_BLOCKSIZE;
81 unsigned char mixed[AES_BLOCKSIZE];
86 if (!pad && padsize != 0)
89 memcpy(mixed, iv, AES_BLOCKSIZE);
92 while (written + AES_BLOCKSIZE <=
size) {
93 for (
int i = 0; i != AES_BLOCKSIZE; i++)
95 enc.Encrypt(out + written, mixed);
96 memcpy(mixed, out + written, AES_BLOCKSIZE);
97 written += AES_BLOCKSIZE;
102 for (
int i = 0; i != padsize; i++)
104 for (
int i = padsize; i != AES_BLOCKSIZE; i++)
105 mixed[i] ^= AES_BLOCKSIZE - padsize;
106 enc.Encrypt(out + written, mixed);
107 written += AES_BLOCKSIZE;
112 template <
typename T>
113 static int CBCDecrypt(
const T& dec,
const unsigned char iv[AES_BLOCKSIZE],
const unsigned char*
data,
int size,
bool pad,
unsigned char* out)
117 const unsigned char* prev = iv;
122 if (
size % AES_BLOCKSIZE != 0)
126 while (written !=
size) {
127 dec.Decrypt(out,
data + written);
128 for (
int i = 0; i != AES_BLOCKSIZE; i++)
130 prev =
data + written;
131 written += AES_BLOCKSIZE;
138 unsigned char padsize = *--out;
139 fail = !padsize | (padsize > AES_BLOCKSIZE);
145 for (
int i = AES_BLOCKSIZE; i != 0; i--)
146 fail |= ((i > AES_BLOCKSIZE - padsize) & (*out-- != padsize));
150 return written * !fail;
154 : enc(key), pad(padIn)
161 return CBCEncrypt(
enc,
iv, data, size,
pad, out);
166 memset(
iv, 0,
sizeof(
iv));
170 : dec(key),
pad(padIn)
178 return CBCDecrypt(
dec,
iv, data, size,
pad, out);
183 memset(
iv, 0,
sizeof(
iv));
187 : enc(key),
pad(padIn)
194 memset(
iv, 0, AES_BLOCKSIZE);
199 return CBCEncrypt(
enc,
iv, data, size,
pad, out);
203 : dec(key),
pad(padIn)
210 memset(
iv, 0, AES_BLOCKSIZE);
215 return CBCDecrypt(
dec,
iv, data, size,
pad, out);
void AES256_init(AES256_ctx *ctx, const unsigned char *key32)
void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
AES256Encrypt(const unsigned char key[32])
void AES128_encrypt(const AES128_ctx *ctx, size_t blocks, unsigned char *cipher16, const unsigned char *plain16)
int Decrypt(const unsigned char *data, int size, unsigned char *out) const
AES128Encrypt(const unsigned char key[16])
int Encrypt(const unsigned char *data, int size, unsigned char *out) const
AES128CBCEncrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
void AES256_encrypt(const AES256_ctx *ctx, size_t blocks, unsigned char *cipher16, const unsigned char *plain16)
unsigned char iv[AES_BLOCKSIZE]
void AES128_decrypt(const AES128_ctx *ctx, size_t blocks, unsigned char *plain16, const unsigned char *cipher16)
void AES256_decrypt(const AES256_ctx *ctx, size_t blocks, unsigned char *plain16, const unsigned char *cipher16)
unsigned char iv[AES_BLOCKSIZE]
AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
void AES128_init(AES128_ctx *ctx, const unsigned char *key16)
AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
int Decrypt(const unsigned char *data, int size, unsigned char *out) const
uint8_t const size_t const size
void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
void * memcpy(void *a, const void *b, size_t c)
int Encrypt(const unsigned char *data, int size, unsigned char *out) const
AES128Decrypt(const unsigned char key[16])
unsigned char iv[AES_BLOCKSIZE]
unsigned char iv[AES_BLOCKSIZE]
AES128CBCDecrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
AES256Decrypt(const unsigned char key[32])