20 memset(&
ctx, 0,
sizeof(
ctx));
35 memset(&
ctx, 0,
sizeof(
ctx));
45static int CBCEncrypt(
const T& enc,
const unsigned char iv[AES_BLOCKSIZE],
const unsigned char* data,
int size,
bool pad,
unsigned char* out)
48 int padsize = size % AES_BLOCKSIZE;
49 unsigned char mixed[AES_BLOCKSIZE];
51 if (!data || !size || !out)
54 if (!pad && padsize != 0)
57 memcpy(mixed, iv, AES_BLOCKSIZE);
60 while (written + AES_BLOCKSIZE <= size) {
61 for (
int i = 0; i != AES_BLOCKSIZE; i++)
63 enc.Encrypt(out + written, mixed);
64 memcpy(mixed, out + written, AES_BLOCKSIZE);
65 written += AES_BLOCKSIZE;
70 for (
int i = 0; i != padsize; i++)
72 for (
int i = padsize; i != AES_BLOCKSIZE; i++)
73 mixed[i] ^= AES_BLOCKSIZE - padsize;
74 enc.Encrypt(out + written, mixed);
75 written += AES_BLOCKSIZE;
81static int CBCDecrypt(
const T& dec,
const unsigned char iv[AES_BLOCKSIZE],
const unsigned char* data,
int size,
bool pad,
unsigned char* out)
85 const unsigned char* prev = iv;
87 if (!data || !size || !out)
90 if (size % AES_BLOCKSIZE != 0)
94 while (written != size) {
95 dec.Decrypt(out, data + written);
96 for (
int i = 0; i != AES_BLOCKSIZE; i++)
98 prev = data + written;
99 written += AES_BLOCKSIZE;
106 unsigned char padsize = *--out;
107 fail = !padsize | (padsize > AES_BLOCKSIZE);
113 for (
int i = AES_BLOCKSIZE; i != 0; i--)
114 fail |= ((i > AES_BLOCKSIZE - padsize) & (*out-- != padsize));
118 return written * !fail;
124 memcpy(
iv, ivIn, AES_BLOCKSIZE);
134 memset(
iv, 0,
sizeof(
iv));
140 memcpy(
iv, ivIn, AES_BLOCKSIZE);
151 memset(
iv, 0,
sizeof(
iv));
static int CBCEncrypt(const T &enc, const unsigned char iv[AES_BLOCKSIZE], const unsigned char *data, int size, bool pad, unsigned char *out)
static int CBCDecrypt(const T &dec, const unsigned char iv[AES_BLOCKSIZE], const unsigned char *data, int size, bool pad, unsigned char *out)
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 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)
#define T(expected, seed, data)