⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rijndaeltest-ref.c

📁 AES(The Advanced Encryption Standard)是美国国家标准与技术研究所用于加密电子数据的规范
💻 C
📖 第 1 页 / 共 2 页
字号:
		exit (EXIT_FAILURE);			}	if (NULL != (fp2 = fopen("table.256","r"))) {		rijndaelTKAT (fp, 128, 256,fp2);		fclose(fp2);	} else {		printf("Table Known Answer test expects file table.192\n");		fclose(fp);		exit (EXIT_FAILURE);			}		fprintf (fp,		"\n"		"==========");	fclose (fp);	/* prepare Intermediate Values Known Answer Tests: */	fp = fopen (ivFile, "w");	fprintf (fp,		"\n"		"=========================\n"		"\n"		"FILENAME:  \"%s\"\n"		"\n"		"Electronic Codebook (ECB) Mode\n"		"Intermediate Value Known Answer Tests\n"		"\n"		"Algorithm Name: Rijndael\n"		"Principal Submitter: %s\n",		ivFile,SUBMITTER);	fflush (fp);	rijndaelIVKAT (fp, 128, BITSPERBLOCK);	rijndaelIVKAT (fp, 192, BITSPERBLOCK);	rijndaelIVKAT (fp, 256, BITSPERBLOCK);	fprintf (fp,		"\n"		"==========");	fclose (fp);}static void rijndaelECB_MCT (FILE *fp, const char *initKey, int keyLength,	const char *initBlock, int blockLength, BYTE direction){	int i, j;	BYTE inBlock[4*MAXBC], outBlock[4*MAXBC], binKey[4*MAXKC];	BYTE keyMaterial[320];	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);	HexToBin (outBlock, initBlock, blockLength);	HexToBin (binKey, initKey, keyLength);	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);		keyInst.blockLen = blockLength;		makeKey(&keyInst, direction, keyLength, keyMaterial);		/* do encryption/decryption: */		blockPrint (fp, outBlock, blockLength, direction == DIR_ENCRYPT ? "PT" : "CT");		cipherInst.blockLen = blockLength;		cipherInit (&cipherInst, MODE_ECB, NULL);		if (direction == DIR_ENCRYPT) {			for (j = 0; j < 10000; j++) {				memcpy (inBlock, outBlock, blockLength/8);				blockEncrypt(&cipherInst, &keyInst, inBlock, blockLength, outBlock);			}		} else {			for (j = 0; j < 10000; j++) {				memcpy (inBlock, outBlock, blockLength/8);				blockDecrypt(&cipherInst, &keyInst, inBlock, blockLength, outBlock);			}		}		blockPrint (fp, outBlock, blockLength, 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, const char *initKey, int keyLength,	const char *initIV, const char *initBlock, int blockLength, BYTE direction){	int i, j, r, t;	BYTE inBlock[256/8], outBlock[256/8], binKey[256/8], cv[256/8];	BYTE keyMaterial[320];	BYTE iv[64+1];	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);	HexToBin (inBlock, initBlock, blockLength); /* this is either PT0 or CT0 */	HexToBin (cv, initIV, blockLength);	HexToBin (binKey, initKey, keyLength);	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);		keyInst.blockLen = blockLength;		r = makeKey(&keyInst, direction, keyLength, keyMaterial);		if (TRUE != r) {			fprintf(stderr,"makeKey error %d\n",r);			exit(-1);		}		/* do encryption/decryption: */		blockPrint (fp, cv, blockLength, "IV");		blockPrint (fp, inBlock, blockLength, direction == DIR_ENCRYPT ? "PT" : "CT");		if (direction == DIR_ENCRYPT) {			for (j = 0; j < 10000; j++) {				for(t = 0; t < blockLength/8; t++) {					sprintf(iv+2*t,"%02x",cv[t]);									}				cipherInst.blockLen = blockLength;				r = cipherInit (&cipherInst, MODE_CBC, iv);				if (TRUE != r) {					fprintf(stderr,"cipherInit error %d\n",r);					exit(-1);				}				r = blockEncrypt(&cipherInst, &keyInst, inBlock, blockLength, outBlock);				if (blockLength != r) {					fprintf(stderr,"blockEncrypt error %d\n",r);					exit(-1);				}				memcpy (inBlock, cv, blockLength/8);				memcpy (cv, outBlock, blockLength/8);			}		} else {			for (j = 0; j < 10000; j++) {				for(t = 0; t < blockLength/8; t++) {					sprintf(iv+2*t,"%02x",cv[t]);									}				cipherInst.blockLen = blockLength;				cipherInit (&cipherInst, MODE_CBC, iv);				blockDecrypt(&cipherInst, &keyInst, inBlock, blockLength, outBlock);				memcpy (cv, inBlock, blockLength/8);				memcpy (inBlock, outBlock, blockLength/8);			}		}		blockPrint (fp, outBlock, blockLength, 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,		"00000000000000000000000000000000", 128,		"00000000000000000000000000000000", BITSPERBLOCK, DIR_ENCRYPT);	rijndaelECB_MCT (fp,		"000000000000000000000000000000000000000000000000", 192,		"00000000000000000000000000000000", BITSPERBLOCK, DIR_ENCRYPT);	rijndaelECB_MCT (fp,		"0000000000000000000000000000000000000000000000000000000000000000", 256,		"00000000000000000000000000000000", BITSPERBLOCK, 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,		"00000000000000000000000000000000", 128,		"00000000000000000000000000000000", BITSPERBLOCK, DIR_DECRYPT);	rijndaelECB_MCT (fp,		"000000000000000000000000000000000000000000000000", 192,		"00000000000000000000000000000000", BITSPERBLOCK, DIR_DECRYPT);	rijndaelECB_MCT (fp,		"0000000000000000000000000000000000000000000000000000000000000000", 256,		"00000000000000000000000000000000", BITSPERBLOCK, 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,		"00000000000000000000000000000000", 128,		"00000000000000000000000000000000", "00000000000000000000000000000000", BITSPERBLOCK,		DIR_ENCRYPT);	rijndaelCBC_MCT (fp,		"000000000000000000000000000000000000000000000000", 192,		"00000000000000000000000000000000", "00000000000000000000000000000000", BITSPERBLOCK,		DIR_ENCRYPT);	rijndaelCBC_MCT (fp,		"0000000000000000000000000000000000000000000000000000000000000000", 256,		"00000000000000000000000000000000", "00000000000000000000000000000000", BITSPERBLOCK,		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,		"00000000000000000000000000000000", 128,		"00000000000000000000000000000000", "00000000000000000000000000000000", BITSPERBLOCK,		DIR_DECRYPT);	rijndaelCBC_MCT (fp,		"000000000000000000000000000000000000000000000000", 192,		"00000000000000000000000000000000", "00000000000000000000000000000000", BITSPERBLOCK,		DIR_DECRYPT);	rijndaelCBC_MCT (fp,		"0000000000000000000000000000000000000000000000000000000000000000", 256,		"00000000000000000000000000000000", "00000000000000000000000000000000", BITSPERBLOCK,		DIR_DECRYPT);	fprintf (fp,		"\n"		"===========");	fclose (fp);	} /* makeMCTs */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");		return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -