54 key->direction = direction;
59 if ((keyLen == 128) || (keyLen == 192) || (keyLen == 256)) {
65 if (keyMaterial != NULL) {
66 strncpy(
key->keyMaterial, keyMaterial, keyLen / 4);
70 const char *keyMat =
key->keyMaterial;
71 for (
int i = 0; i <
key->keyLen / 8; i++) {
75 if ((t >=
'0') && (t <=
'9'))
77 else if ((t >=
'a') && (t <=
'f'))
78 v = (t -
'a' + 10) << 4;
79 else if ((t >=
'A') && (t <=
'F'))
80 v = (t -
'A' + 10) << 4;
85 if ((t >=
'0') && (t <=
'9'))
87 else if ((t >=
'a') && (t <=
'f'))
89 else if ((t >=
'A') && (t <=
'F'))
94 cipherKey[i] = (
u8) v;
118 if ((t >=
'0') && (t <=
'9'))
120 else if ((t >=
'a') && (t <=
'f'))
121 j = (t -
'a' + 10) << 4;
122 else if ((t >=
'A') && (t <=
'F'))
123 j = (t -
'A' + 10) << 4;
128 if ((t >=
'0') && (t <=
'9'))
130 else if ((t >=
'a') && (t <=
'f'))
132 else if ((t >=
'A') && (t <=
'F'))
137 cipher->
IV[i] = (
u8) j;
146 BYTE * input,
int inputLen,
BYTE * outBuffer)
148 int i, k, t, numBlocks;
154 if (input == NULL || inputLen <= 0) {
158 numBlocks = inputLen / 128;
160 switch (cipher->
mode) {
162 for (i = numBlocks; i > 0; i--) {
171 for (i = numBlocks; i > 0; i--) {
172 ((
u32 *)(
void *) block)[0] =
173 ((
u32 *)(
void *) input)[0] ^ ((
u32 *)(
void *) iv)[0];
174 ((
u32 *)(
void *) block)[1] =
175 ((
u32 *)(
void *) input)[1] ^ ((
u32 *)(
void *) iv)[1];
176 ((
u32 *)(
void *) block)[2] =
177 ((
u32 *)(
void *) input)[2] ^ ((
u32 *)(
void *) iv)[2];
178 ((
u32 *)(
void *) block)[3] =
179 ((
u32 *)(
void *) input)[3] ^ ((
u32 *)(
void *) iv)[3];
189 for (i = numBlocks; i > 0; i--) {
191memcpy(outBuffer, input, 16);
192 for (k = 0; k < 128; k++) {
195 (block[0] & 0x80U) >> (k & 7);
196 for (t = 0; t < 15; t++) {
197 iv[t] = (iv[t] << 1) | (iv[t + 1] >> 7);
201 ((outBuffer[k >> 3] >> (7 - (k & 7))) & 1);
212 return 128 * numBlocks;
225 BYTE * input,
int inputOctets,
BYTE * outBuffer)
227 int i, numBlocks, padLen;
233 if (input == NULL || inputOctets <= 0) {
237 numBlocks = inputOctets / 16;
239 switch (cipher->
mode) {
241 for (i = numBlocks; i > 0; i--) {
246 padLen = 16 - (inputOctets - 16 * numBlocks);
247 assert(padLen > 0 && padLen <= 16);
248 memcpy(block, input, 16 - padLen);
249 memset(block + 16 - padLen, padLen, padLen);
255 for (i = numBlocks; i > 0; i--) {
256 ((
u32 *)(
void *) block)[0] =
257 ((
u32 *)(
void *) input)[0] ^ ((
u32 *)(
void *) iv)[0];
258 ((
u32 *)(
void *) block)[1] =
259 ((
u32 *)(
void *) input)[1] ^ ((
u32 *)(
void *) iv)[1];
260 ((
u32 *)(
void *) block)[2] =
261 ((
u32 *)(
void *) input)[2] ^ ((
u32 *)(
void *) iv)[2];
262 ((
u32 *)(
void *) block)[3] =
263 ((
u32 *)(
void *) input)[3] ^ ((
u32 *)(
void *) iv)[3];
269 padLen = 16 - (inputOctets - 16 * numBlocks);
270 assert(padLen > 0 && padLen <= 16);
271 for (i = 0; i < 16 - padLen; i++) {
272 block[i] = input[i] ^ iv[i];
274 for (i = 16 - padLen; i < 16; i++) {
275 block[i] = (
BYTE) padLen ^ iv[i];
284 return 16 * (numBlocks + 1);
288 BYTE * input,
int inputLen,
BYTE * outBuffer)
290 int i, k, t, numBlocks;
293 if (cipher == NULL ||
298 if (input == NULL || inputLen <= 0) {
302 numBlocks = inputLen / 128;
304 switch (cipher->
mode) {
306 for (i = numBlocks; i > 0; i--) {
315 for (i = numBlocks; i > 0; i--) {
317 ((
u32 *)(
void *) block)[0] ^= ((
u32 *)(
void *) iv)[0];
318 ((
u32 *)(
void *) block)[1] ^= ((
u32 *)(
void *) iv)[1];
319 ((
u32 *)(
void *) block)[2] ^= ((
u32 *)(
void *) iv)[2];
320 ((
u32 *)(
void *) block)[3] ^= ((
u32 *)(
void *) iv)[3];
321 memcpy(cipher->
IV, input, 16);
322 memcpy(outBuffer, block, 16);
330 for (i = numBlocks; i > 0; i--) {
331 memcpy(outBuffer, input, 16);
333for (k = 0; k < 128; k++) {
335 for (t = 0; t < 15; t++) {
336 iv[t] = (iv[t] << 1) | (iv[t + 1] >> 7);
340 ((input[k >> 3] >> (7 - (k & 7))) & 1);
342 (block[0] & 0x80U) >> (k & 7);
353 return 128 * numBlocks;
357 BYTE * input,
int inputOctets,
BYTE * outBuffer)
359 int i, numBlocks, padLen;
365 if (input == NULL || inputOctets <= 0) {
368 if (inputOctets % 16 != 0) {
372 numBlocks = inputOctets / 16;
374 switch (cipher->
mode) {
377 for (i = numBlocks - 1; i > 0; i--) {
388 for (i = 16 - padLen; i < 16; i++) {
389 if (block[i] != padLen) {
393 memcpy(outBuffer, block, 16 - padLen);
398 for (i = numBlocks - 1; i > 0; i--) {
400 ((
u32 *)(
void *) block)[0] ^= ((
u32 *)(
void *) cipher->
IV)[0];
401 ((
u32 *)(
void *) block)[1] ^= ((
u32 *)(
void *) cipher->
IV)[1];
402 ((
u32 *)(
void *) block)[2] ^= ((
u32 *)(
void *) cipher->
IV)[2];
403 ((
u32 *)(
void *) block)[3] ^= ((
u32 *)(
void *) cipher->
IV)[3];
404 memcpy(cipher->
IV, input, 16);
405 memcpy(outBuffer, block, 16);
411 ((
u32 *)(
void *) block)[0] ^= ((
u32 *)(
void *) cipher->
IV)[0];
412 ((
u32 *)(
void *) block)[1] ^= ((
u32 *)(
void *) cipher->
IV)[1];
413 ((
u32 *)(
void *) block)[2] ^= ((
u32 *)(
void *) cipher->
IV)[2];
414 ((
u32 *)(
void *) block)[3] ^= ((
u32 *)(
void *) cipher->
IV)[3];
416 if (padLen <= 0 || padLen > 16) {
419 for (i = 16 - padLen; i < 16; i++) {
420 if (block[i] != padLen) {
424 memcpy(outBuffer, block, 16 - padLen);
431 return 16 * numBlocks - padLen;
445 BYTE * input,
int inputLen,
BYTE * outBuffer,
int rounds)
451 if (cipher == NULL ||
key == NULL) {
455 memcpy(block, input, 16);
457 switch (
key->direction) {
470 memcpy(outBuffer, block, 16);
int blockEncrypt(cipherInstance *cipher, keyInstance *key, BYTE *input, int inputLen, BYTE *outBuffer)
int cipherUpdateRounds(cipherInstance *cipher, keyInstance *key, BYTE *input, int inputLen, BYTE *outBuffer, int rounds)
int padEncrypt(cipherInstance *cipher, keyInstance *key, BYTE *input, int inputOctets, BYTE *outBuffer)
int blockDecrypt(cipherInstance *cipher, keyInstance *key, BYTE *input, int inputLen, BYTE *outBuffer)
int makeKey(keyInstance *key, BYTE direction, int keyLen, const char *keyMaterial)
int cipherInit(cipherInstance *cipher, BYTE mode, char *IV)
int padDecrypt(cipherInstance *cipher, keyInstance *key, BYTE *input, int inputOctets, BYTE *outBuffer)
#define BAD_CIPHER_INSTANCE
int rijndaelKeySetupDec(u32 rk[], const u8 cipherKey[], int keyBits)
void rijndaelEncryptRound(const u32 rk[], int Nr, u8 block[16], int rounds)
void rijndaelDecrypt(const u32 rk[], int Nr, const u8 ct[16], u8 pt[16])
int rijndaelKeySetupEnc(u32 rk[], const u8 cipherKey[], int keyBits)
void rijndaelEncrypt(const u32 rk[], int Nr, const u8 pt[16], u8 ct[16])
void rijndaelDecryptRound(const u32 rk[], int Nr, u8 block[16], int rounds)