67 BYTE keyMaterial[320];
73 printf(
"Executing Variable-Key KAT (key %d): ", keyLength);
76 fprintf(fp,
"\n" "==========\n" "\n" "KEYSIZE=%d\n" "\n", keyLength);
80 memset(keyMaterial, 0,
sizeof(keyMaterial));
81 memset(keyMaterial,
'0', keyLength / 4);
82 for (i = 0; i < keyLength; i++) {
83 keyMaterial[i / 4] = byteVal;
86 fprintf(stderr,
"makeKey error %d\n", r);
89 fprintf(fp,
"\nI=%d\n", i + 1);
90 fprintf(fp,
"KEY=%s\n", keyMaterial);
94 fprintf(stderr,
"cipherInit error %d\n", r);
97 r =
blockEncrypt(&cipherInst, &keyInst, block, 128, block);
99 fprintf(stderr,
"blockEncrypt error %d\n", r);
106 for (j = 0; j < 16; j++) {
107 assert(block[j] == 0);
110 keyMaterial[i / 4] = (
BYTE)
'0';
112 (byteVal ==
'8') ?
'4' :
113 (byteVal ==
'4') ?
'2' : (byteVal ==
'2') ?
'1' :
116 assert(byteVal == (
BYTE)
'8');
162 BYTE block[4 * 4], block2[4 * 4];
163 BYTE keyMaterial[320];
168 printf(
"Executing Tables KAT (key %d): ", keyLength);
171 fprintf(fp,
"\n" "==========\n" "\n" "KEYSIZE=%d\n" "\n", keyLength);
174 memset(keyMaterial, 0,
sizeof(keyMaterial));
176 for (i = 0; i < 64; i++) {
177 fprintf(fp,
"\nI=%d\n", i + 1);
178 for (j = 0; j < keyLength / 4; j++) {
179 fscanf(in,
"%c", &keyMaterial[j]);
183 fprintf(fp,
"KEY=%s\n", keyMaterial);
185 for (j = 0; j < 16; j++) {
186 fscanf(in,
"%02x", &s);
189 fscanf(in,
"%c", (
char *)&s);
190 fscanf(in,
"%c", (
char *)&s);
193 blockEncrypt(&cipherInst, &keyInst, block, 128, block2);
196 for (i = 64; i < 128; i++) {
197 fprintf(fp,
"\nI=%d\n", i + 1);
198 for (j = 0; j < keyLength / 4; j++) {
199 fscanf(in,
"%c", &keyMaterial[j]);
203 fprintf(fp,
"KEY=%s\n", keyMaterial);
205 for (j = 0; j < 16; j++) {
206 fscanf(in,
"%02x", &s);
209 fscanf(in,
"%c", (
char *)&s);
210 fscanf(in,
"%c", (
char *)&s);
212 blockDecrypt(&cipherInst, &keyInst, block, 128, block2);
225 BYTE pt[4 * 4], ct[4 * 4];
226 BYTE keyMaterial[320];
231 printf(
"Executing Intermediate value KAT (key %d): ", keyLength);
235 fprintf(fp,
"\n" "==========\n" "\n" "KEYSIZE=%d\n", keyLength);
237 memset(keyMaterial, 0,
sizeof(keyMaterial));
238 for (i = 0; i < keyLength / 8; i++) {
239 sprintf((
char *) &keyMaterial[2 * i],
"%02X", i);
241 fprintf(fp,
"KEY=%s\n", keyMaterial);
243 for (i = 0; i < 16; i++) {
247 fprintf(fp,
"\nIntermediate Ciphertext Values (Encryption)\n\n");
251 for (i = 1; i < keyInst.
Nr; i++) {
254 sprintf(format,
"CT%d", i);
260 fprintf(fp,
"\nIntermediate Ciphertext Values (Decryption)\n\n");
264 for (i = 1; i < keyInst.
Nr; i++) {
267 sprintf(format,
"PT%d", i);
407 BYTE inBlock[4 * 4], outBlock[4 * 4], binKey[4 *
MAXKC];
408 BYTE keyMaterial[320];
416 clock_t elapsed = -clock();
417 printf(
"Executing ECB MCT (%s, key %d): ",
418 direction ==
DIR_ENCRYPT ?
"ENCRYPT" :
"DECRYPT", keyLength);
423 "=========================\n" "\n" "KEYSIZE=%d\n", keyLength);
425 memset(outBlock, 0, 16);
426 memset(binKey, 0, keyLength / 8);
427 for (i = 0; i < 400; i++) {
429 while (width-- > 0) {
432 width = printf(
"%d", i);
435 fprintf(fp,
"\nI=%d\n", i);
437 for (j = 0; j < keyLength / 8; j++) {
438 sprintf((
char *) &keyMaterial[2 * j],
"%02X", binKey[j]);
440 keyMaterial[keyLength / 4] = 0;
441 fprintf(fp,
"KEY=%s\n", keyMaterial);
442 makeKey(&keyInst, direction, keyLength, (
char *) keyMaterial);
448 for (j = 0; j < 10000; j++) {
449 memcpy(inBlock, outBlock, 16);
454 for (j = 0; j < 10000; j++) {
455 memcpy(inBlock, outBlock, 16);
465 for (j = 0; j < 128 / 8; j++) {
466 binKey[j] ^= outBlock[j];
470 for (j = 0; j < 64 / 8; j++) {
471 binKey[j] ^= inBlock[j + 64 / 8];
473 for (j = 0; j < 128 / 8; j++) {
474 binKey[j + 64 / 8] ^= outBlock[j];
478 for (j = 0; j < 128 / 8; j++) {
479 binKey[j] ^= inBlock[j];
481 for (j = 0; j < 128 / 8; j++) {
482 binKey[j + 128 / 8] ^= outBlock[j];
489 while (width-- > 0) {
492 printf(
"%d done (%.1f s).\n", i, (
float)elapsed / CLOCKS_PER_SEC);
499 BYTE inBlock[256 / 8], outBlock[256 / 8], binKey[256 / 8], cv[256 / 8];
500 BYTE keyMaterial[320];
508 clock_t elapsed = -clock();
509 printf(
"Executing CBC MCT (%s, key %d): ",
510 direction ==
DIR_ENCRYPT ?
"ENCRYPT" :
"DECRYPT", keyLength);
513 fprintf(fp,
"\n" "==========\n" "\n" "KEYSIZE=%d\n", keyLength);
516 memset(inBlock, 0, 16);
517 memset(binKey, 0, keyLength / 8);
518 for (i = 0; i < 400; i++) {
520 while (width-- > 0) {
523 width = printf(
"%d", i);
526 fprintf(fp,
"\nI=%d\n", i);
528 for (j = 0; j < keyLength / 8; j++) {
529 sprintf((
char *) &keyMaterial[2 * j],
"%02X", binKey[j]);
531 keyMaterial[keyLength / 4] = 0;
532 fprintf(fp,
"KEY=%s\n", keyMaterial);
533 r =
makeKey(&keyInst, direction, keyLength, (
char *) keyMaterial);
535 fprintf(stderr,
"makeKey error %d\n", r);
540 fprintf(stderr,
"cipherInit error %d\n", r);
547 for (j = 0; j < 10000; j++) {
548 for (t = 0; t < 16; t++) {
555 "blockEncrypt error %d\n", r);
558 memcpy(inBlock, cv, 16);
559 memcpy(cv, outBlock, 16);
562 for (j = 0; j < 10000; j++) {
565 for (t = 0; t < 16; t++) {
566 outBlock[t] ^= cv[t];
568 memcpy(cv, inBlock, 16);
569 memcpy(inBlock, outBlock, 16);
577 for (j = 0; j < 128 / 8; j++) {
578 binKey[j] ^= outBlock[j];
582 for (j = 0; j < 64 / 8; j++) {
584 binKey[j] ^= inBlock[j + 64 / 8];
586 binKey[j] ^= cv[j + 64 / 8];
589 for (j = 0; j < 128 / 8; j++) {
590 binKey[j + 64 / 8] ^= outBlock[j];
594 for (j = 0; j < 128 / 8; j++) {
596 binKey[j] ^= inBlock[j];
601 for (j = 0; j < 128 / 8; j++) {
602 binKey[j + 128 / 8] ^= outBlock[j];
609 while (width-- > 0) {
612 printf(
"%d done (%.1f s).\n", i, (
float)elapsed / CLOCKS_PER_SEC);
int cipherUpdateRounds(cipherInstance *cipher, keyInstance *key, BYTE *input, int inputLen, BYTE *outBuffer, int rounds)
static void makeMCTs(const char *ecbEncryptionFile, const char *ecbDecryptionFile, const char *cbcEncryptionFile, const char *cbcDecryptionFile)