📄 rijndaeltest-fst.c
字号:
keyInstance keyInst; cipherInstance cipherInst;#ifdef TRACE_KAT_MCT int width = 0; clock_t elapsed = -clock(); printf ("Executing ECB MCT (%s, key %d): ", direction == DIR_ENCRYPT ? "ENCRYPT" : "DECRYPT", keyLength); fflush (stdout);#endif /* ?TRACE_KAT_MCT */ fprintf (fp, "\n" "=========================\n" "\n" "KEYSIZE=%d\n", keyLength); fflush (fp); memset (outBlock, 0, 16); memset (binKey, 0, keyLength/8); for (i = 0; i < 400; i++) {#ifdef TRACE_KAT_MCT while (width-- > 0) putchar ('\b'); width = printf ("%d", i); fflush (stdout); #endif /* ?TRACE_KAT_MCT */ fprintf (fp, "\nI=%d\n", i); /* prepare key: */ for (j = 0; j < keyLength/8; j++) { sprintf (&keyMaterial[2*j], "%02X", binKey[j]); } keyMaterial[keyLength/4] = 0; fprintf (fp, "KEY=%s\n", keyMaterial); makeKey(&keyInst, direction, keyLength, keyMaterial); /* do encryption/decryption: */ blockPrint (fp, outBlock, direction == DIR_ENCRYPT ? "PT" : "CT"); cipherInit (&cipherInst, MODE_ECB, NULL); if (direction == DIR_ENCRYPT) { for (j = 0; j < 10000; j++) { memcpy (inBlock, outBlock, 16); blockEncrypt(&cipherInst, &keyInst, inBlock, 128, outBlock); } } else { for (j = 0; j < 10000; j++) { memcpy (inBlock, outBlock, 16); blockDecrypt(&cipherInst, &keyInst, inBlock, 128, outBlock); } } blockPrint (fp, outBlock, direction == DIR_ENCRYPT ? "CT" : "PT"); /* prepare new key: */ switch (keyLength) { case 128: for (j = 0; j < 128/8; j++) { binKey[j] ^= outBlock[j]; } break; case 192: for (j = 0; j < 64/8; j++) { binKey[j] ^= inBlock[j + 64/8]; } for (j = 0; j < 128/8; j++) { binKey[j + 64/8] ^= outBlock[j]; } break; case 256: for (j = 0; j < 128/8; j++) { binKey[j] ^= inBlock[j]; } for (j = 0; j < 128/8; j++) { binKey[j + 128/8] ^= outBlock[j]; } break; } }#ifdef TRACE_KAT_MCT elapsed += clock(); printf (" done (%.1f s).\n", (float)elapsed/CLOCKS_PER_SEC);#endif /* ?TRACE_KAT_MCT */} /* rijndaelECB_MCT */static void rijndaelCBC_MCT (FILE *fp, int keyLength, BYTE direction){ int i, j, r, t; BYTE inBlock[256/8], outBlock[256/8], binKey[256/8], cv[256/8]; BYTE keyMaterial[320]; keyInstance keyInst; cipherInstance cipherInst;#ifdef TRACE_KAT_MCT int width = 0; clock_t elapsed = -clock(); printf ("Executing CBC MCT (%s, key %d): ", direction == DIR_ENCRYPT ? "ENCRYPT" : "DECRYPT", keyLength); fflush (stdout);#endif /* ?TRACE_KAT_MCT */ fprintf (fp, "\n" "==========\n" "\n" "KEYSIZE=%d\n", keyLength); fflush (fp); memset(cv,0,16); memset(inBlock,0,16); memset(binKey,0,keyLength/8); for (i = 0; i < 400; i++) {#ifdef TRACE_KAT_MCT while (width-- > 0) putchar ('\b'); width = printf ("%d", i); fflush (stdout); #endif /* ?TRACE_KAT_MCT */ fprintf (fp, "\nI=%d\n", i); /* prepare key: */ for (j = 0; j < keyLength/8; j++) { sprintf (&keyMaterial[2*j], "%02X", binKey[j]); } keyMaterial[keyLength/4] = 0; fprintf (fp, "KEY=%s\n", keyMaterial); r = makeKey(&keyInst, direction, keyLength, keyMaterial); if (TRUE != r) { fprintf(stderr,"makeKey error %d\n",r); exit(-1); } r = cipherInit (&cipherInst, MODE_ECB, NULL); if (TRUE != r) { fprintf(stderr,"cipherInit error %d\n",r); exit(-1); } /* do encryption/decryption: */ blockPrint (fp, cv, "IV"); blockPrint (fp, inBlock, direction == DIR_ENCRYPT ? "PT" : "CT"); if (direction == DIR_ENCRYPT) { for (j = 0; j < 10000; j++) { for(t = 0; t < 16; t++) inBlock[t] ^= cv[t]; r = blockEncrypt(&cipherInst, &keyInst, inBlock, 128, outBlock); if (128 != r) { fprintf(stderr,"blockEncrypt error %d\n",r); exit(-1); } memcpy (inBlock, cv, 16); memcpy (cv, outBlock, 16); } } else { for (j = 0; j < 10000; j++) { blockDecrypt(&cipherInst, &keyInst, inBlock, 128, outBlock); for(t = 0; t < 16; t++) outBlock[t] ^= cv[t]; memcpy (cv, inBlock, 16); memcpy (inBlock, outBlock, 16); } } blockPrint (fp, outBlock, direction == DIR_ENCRYPT ? "CT" : "PT"); /* prepare new key: */ switch (keyLength) { case 128: for (j = 0; j < 128/8; j++) { binKey[j] ^= outBlock[j]; } break; case 192: for (j = 0; j < 64/8; j++) { if (direction == DIR_ENCRYPT) binKey[j] ^= inBlock[j + 64/8]; else binKey[j] ^= cv[j + 64/8]; } for (j = 0; j < 128/8; j++) { binKey[j + 64/8] ^= outBlock[j]; } break; case 256: for (j = 0; j < 128/8; j++) { if (direction == DIR_ENCRYPT) binKey[j] ^= inBlock[j]; else binKey[j] ^= cv[j]; } for (j = 0; j < 128/8; j++) { binKey[j + 128/8] ^= outBlock[j]; } break; } }#ifdef TRACE_KAT_MCT elapsed += clock(); printf (" done (%.1f s).\n", (float)elapsed/CLOCKS_PER_SEC);#endif /* ?TRACE_KAT_MCT */} /* rijndaelCBC_MCT */static void makeMCTs (const char *ecbEncryptionFile, const char *ecbDecryptionFile, const char *cbcEncryptionFile, const char *cbcDecryptionFile){ FILE *fp; /* prepare ECB Encryption Monte Carlo Tests: */ fp = fopen (ecbEncryptionFile, "w"); fprintf (fp, "\n" "=========================\n" "\n" "FILENAME: \"%s\"\n" "\n" "Electronic Codebook (ECB) Mode - ENCRYPTION\n" "Monte Carlo Test\n" "\n" "Algorithm Name: Rijndael\n" "Principal Submitter: %s\n", ecbEncryptionFile,SUBMITTER); fflush (fp); rijndaelECB_MCT (fp, 128, DIR_ENCRYPT); rijndaelECB_MCT (fp, 192, DIR_ENCRYPT); rijndaelECB_MCT (fp, 256, DIR_ENCRYPT); fprintf (fp, "\n" "==========="); fclose (fp); /* prepare ECB Decryption Monte Carlo Tests: */ fp = fopen (ecbDecryptionFile, "w"); fprintf (fp, "\n" "=========================\n" "\n" "FILENAME: \"%s\"\n" "\n" "Electronic Codebook (ECB) Mode - DECRYPTION\n" "Monte Carlo Test\n" "\n" "Algorithm Name: Rijndael\n" "Principal Submitter: %s\n", ecbDecryptionFile,SUBMITTER); fflush (fp); rijndaelECB_MCT (fp, 128, DIR_DECRYPT); rijndaelECB_MCT (fp, 192, DIR_DECRYPT); rijndaelECB_MCT (fp, 256, DIR_DECRYPT); fprintf (fp, "\n" "==========="); fclose (fp); /* prepare CBC Encryption Monte Carlo Tests: */ fp = fopen (cbcEncryptionFile, "w"); fprintf (fp, "\n" "=========================\n" "\n" "FILENAME: \"%s\"\n" "\n" "Cipher Block Chaining (CBC) Mode - ENCRYPTION\n" "Monte Carlo Test\n" "\n" "Algorithm Name: Rijndael\n" "Principal Submitter: %s\n", cbcEncryptionFile,SUBMITTER); fflush (fp); rijndaelCBC_MCT (fp, 128, DIR_ENCRYPT); rijndaelCBC_MCT (fp, 192, DIR_ENCRYPT); rijndaelCBC_MCT (fp, 256, DIR_ENCRYPT); fprintf (fp, "\n" "==========="); fclose (fp); /* prepare CBC Decryption Monte Carlo Tests: */ fp = fopen (cbcDecryptionFile, "w"); fprintf (fp, "\n" "=========================\n" "\n" "FILENAME: \"%s\"\n" "\n" "Cipher Block Chaining (CBC) Mode - DECRYPTION\n" "Monte Carlo Test\n" "\n" "Algorithm Name: Rijndael\n" "Principal Submitter: %s\n", cbcDecryptionFile,SUBMITTER); fflush (fp); rijndaelCBC_MCT (fp, 128, DIR_DECRYPT); rijndaelCBC_MCT (fp, 192, DIR_DECRYPT); rijndaelCBC_MCT (fp, 256, DIR_DECRYPT); fprintf (fp, "\n" "==========="); fclose (fp);} /* makeMCTs */void rand_init(void){ srand(time(NULL));}word32 rand_word32(void){ return ((word32)(rand() >> 7) << 24) | ((word32)(rand() >> 7) << 16) | ((word32)(rand() >> 7) << 8) | (word32)(rand() >> 7);}static void rijndaelSpeed (FILE *fp, int keyLength, BYTE mode, int iterations){ int i, rounds; BYTE block[4*4]; BYTE keyMaterial[320]; char IV[2*4*4+1] = "A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5A5"; keyInstance keyInst; cipherInstance cipherInst; clock_t elapsed; double sec; printf ("Measuring speed (key %d): \n", keyLength); fflush (stdout); rounds = iterations; rand_init(); for (i=3; i>=0; i--) ((word32*)block)[i] = rand_word32(); blockPrint (fp, block, "plaintext"); memset (keyMaterial, 0, sizeof (keyMaterial)); memset (keyMaterial, '0', keyLength/4); for (i = 0; i < keyLength/4; i++) keyMaterial[i] = '0'+ i; printf ("Measuring key scheduling speed..."); elapsed = -clock (); for (i = rounds; i > 0; i--) { makeKey(&keyInst, DIR_ENCRYPT, keyLength, keyMaterial); } elapsed += clock (); sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0; printf (" %.2f sec, %.1f Mbit/sec.\n", sec, (double)keyLength*iterations/1E6/sec); if (mode == MODE_ECB) { cipherInit (&cipherInst, mode, NULL); } else { cipherInit (&cipherInst, mode, IV); } printf ("Measuring encryption speed..."); elapsed = -clock (); for (i = rounds; i > 0; i--) { blockEncrypt(&cipherInst, &keyInst, block, 128, block); } elapsed += clock (); sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0; printf (" %.2f sec, %.1f Mbit/sec.\n", sec, (double)128*iterations/1E6/sec); blockPrint (fp, block, "encrypted text"); if (mode == MODE_ECB) { cipherInit (&cipherInst, mode, NULL); } else { cipherInit (&cipherInst, mode, IV); } if (mode != MODE_CFB1) { printf ("Measuring key scheduling speed..."); elapsed = -clock (); for (i = rounds; i > 0; i--) { makeKey(&keyInst, DIR_DECRYPT, keyLength, keyMaterial); } elapsed += clock (); sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0; printf (" %.2f sec, %.1f Mbit/sec.\n", sec, (double)keyLength*iterations/1E6/sec); } printf ("Measuring decryption speed..."); elapsed = -clock (); for (i = rounds; i > 0; i--) { blockDecrypt(&cipherInst, &keyInst, block, 128, block); } elapsed += clock (); sec = elapsed ? (double) elapsed / CLOCKS_PER_SEC : 1.0; printf (" %.2f sec, %.1f Mbit/sec.\n", sec, (double)128*iterations/1E6/sec); blockPrint (fp, block, "decrypted text"); printf ("done.\n");} /* rijndaelSpeed */int main (void){ makeKATs ("ecb_vk.txt", "ecb_vt.txt", "ecb_tbl.txt", "ecb_iv.txt"); makeMCTs ("ecb_e_m.txt", "ecb_d_m.txt", "cbc_e_m.txt", "cbc_d_m.txt"); printf("ECB:\n"); rijndaelSpeed (stdout, 128, MODE_ECB, 1000000); rijndaelSpeed (stdout, 192, MODE_ECB, 1000000); rijndaelSpeed (stdout, 256, MODE_ECB, 1000000); printf("CFB1:\n"); rijndaelSpeed (stdout, 128, MODE_CFB1, 10000); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -