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];
83 if (!data || !size || !out)
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)
115 unsigned char padsize = 0;
118 const unsigned char* prev = iv;
120 if (!data || !size || !out)
123 if (size % AES_BLOCKSIZE != 0)
127 while (written != size) {
128 dec.Decrypt(out, data + written);
129 for (
int i = 0; i != AES_BLOCKSIZE; i++)
131 prev = data + written;
132 written += AES_BLOCKSIZE;
140 fail = !padsize | (padsize > AES_BLOCKSIZE);
146 for (
int i = AES_BLOCKSIZE; i != 0; i--)
147 fail |= ((i > AES_BLOCKSIZE - padsize) & (*out-- != padsize));
151 return written * !fail;
155 : enc(key), pad(padIn)
162 return CBCEncrypt(
enc,
iv, data, size,
pad, out);
167 memset(
iv, 0,
sizeof(
iv));
171 : dec(key), pad(padIn)
179 return CBCDecrypt(
dec,
iv, data, size,
pad, out);
184 memset(
iv, 0,
sizeof(
iv));
188 : enc(key), pad(padIn)
195 memset(
iv, 0, AES_BLOCKSIZE);
200 return CBCEncrypt(
enc,
iv, data, size,
pad, out);
204 : dec(key), pad(padIn)
211 memset(
iv, 0, AES_BLOCKSIZE);
216 return CBCDecrypt(
dec,
iv, data, size,
pad, out);
unsigned char iv[AES_BLOCKSIZE]
AES128CBCDecrypt(const unsigned char key[AES128_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
int Decrypt(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)
unsigned char iv[AES_BLOCKSIZE]
int Encrypt(const unsigned char *data, int size, unsigned char *out) const
AES128Decrypt(const unsigned char key[16])
void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
AES128Encrypt(const unsigned char key[16])
AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
int Decrypt(const unsigned char *data, int size, unsigned char *out) const
unsigned char iv[AES_BLOCKSIZE]
AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn)
int Encrypt(const unsigned char *data, int size, unsigned char *out) const
unsigned char iv[AES_BLOCKSIZE]
AES256Decrypt(const unsigned char key[32])
void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
AES256Encrypt(const unsigned char key[32])
void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
void AES128_encrypt(const AES128_ctx *ctx, size_t blocks, unsigned char *cipher16, const unsigned char *plain16)
void AES256_encrypt(const AES256_ctx *ctx, size_t blocks, unsigned char *cipher16, const unsigned char *plain16)
void AES256_init(AES256_ctx *ctx, const unsigned char *key32)
void AES256_decrypt(const AES256_ctx *ctx, size_t blocks, unsigned char *plain16, const unsigned char *cipher16)
void AES128_decrypt(const AES128_ctx *ctx, size_t blocks, unsigned char *plain16, const unsigned char *cipher16)
void AES128_init(AES128_ctx *ctx, const unsigned char *key16)
void * memcpy(void *a, const void *b, size_t c)
#define T(expected, seed, data)