23 memset(&
ctx, 0,
sizeof(
ctx));
38 memset(&
ctx, 0,
sizeof(
ctx));
53 memset(&
ctx, 0,
sizeof(
ctx));
68 memset(&
ctx, 0,
sizeof(
ctx));
78 static int CBCEncrypt(
const T& enc,
const unsigned char iv[AES_BLOCKSIZE],
const unsigned char* data,
int size,
bool pad,
unsigned char* out)
81 int padsize =
size % AES_BLOCKSIZE;
82 unsigned char mixed[AES_BLOCKSIZE];
84 if (!data || !
size || !out)
87 if (!pad && padsize != 0)
90 memcpy(mixed, iv, AES_BLOCKSIZE);
93 while (written + AES_BLOCKSIZE <=
size) {
94 for (
int i = 0; i != AES_BLOCKSIZE; i++)
96 enc.Encrypt(out + written, mixed);
97 memcpy(mixed, out + written, AES_BLOCKSIZE);
98 written += AES_BLOCKSIZE;
103 for (
int i = 0; i != padsize; i++)
105 for (
int i = padsize; i != AES_BLOCKSIZE; i++)
106 mixed[i] ^= AES_BLOCKSIZE - padsize;
107 enc.Encrypt(out + written, mixed);
108 written += AES_BLOCKSIZE;
113 template <
typename T>
114 static int CBCDecrypt(
const T& dec,
const unsigned char iv[AES_BLOCKSIZE],
const unsigned char* data,
int size,
bool pad,
unsigned char* out)
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;
139 unsigned char padsize = *--out;
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);
void AES256_init(AES256_ctx *ctx, const unsigned char *key32)
AES256Encrypt(const unsigned char key[32])
int Decrypt(const unsigned char *data, int size, unsigned char *out) const
void AES128_encrypt(const AES128_ctx *ctx, size_t blocks, unsigned char *cipher16, const unsigned char *plain16)
AES128Encrypt(const unsigned char key[16])
void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) 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)
int Decrypt(const unsigned char *data, int size, unsigned char *out) const
void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
void AES256_decrypt(const AES256_ctx *ctx, size_t blocks, unsigned char *plain16, const unsigned char *cipher16)
unsigned char iv[AES_BLOCKSIZE]
int Encrypt(const unsigned char *data, int size, unsigned char *out) const
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)
int Encrypt(const unsigned char *data, int size, unsigned char *out) const
void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
void * memcpy(void *a, const void *b, size_t c)
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)
AES256Decrypt(const unsigned char key[32])