📄 aes_main.cpp
字号:
/* AES known answer tests and Monte Carlo tests.
This code assumes little endian (Intel)
The results are output to the following files:
cbc_d_m.txt - Monte Carlo cipher block chaining decryption
cbc_e_m.txt - Monte Carlo cipher block chaining encryption
ecb_d_m.txt - Monte Carlo electronic codebook mode decryption
ecb_e_m.txt - Monte Carlo electronic codebook mode encryption
ecb_vk.txt - variable key known answer test electrnoic codebook mode
ecb_vt.txt - variable text known answer test electrnoic codebook mode
*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "aes.h"
#define BLOCKSIZE 128
#define ALG_NAME "RC6 (TM)"
#define SUBMITTER "RSA Laboratories"
/* These macros always have hexadecimal letters in lower case */
#define HEX(x) (char) ( ((x) < 10) ? (x)+'0' : (x)+'a'-10 )
#define HEX_TO_DIGIT(x) (BYTE) ( ((x) >= '0' && (x) <= '9') ? (x)-'0' : (x)-'a'+10 )
void KAT1();
void KAT2();
void outputHeaderKAT1();
void outputHeaderKAT2();
void MCT1();
void MCT2();
void MCT3();
void MCT4();
void outputHeaderMCT1();
void outputHeaderMCT2();
void outputHeaderMCT3();
void outputHeaderMCT4();
int
main () {
FILE *fp;
char filename[100], fullname[100];
sprintf (filename, "ecb_vk.txt");
sprintf (fullname, "%s",filename);
fp = fopen (fullname,"w");
if (fp == NULL) {
printf ("error opening file %s\n", filename);
exit(1);
}
outputHeaderKAT1 (fp, filename);
printf ("KAT1\n"); fflush(stdout);
KAT1(fp, 16);
KAT1(fp, 24);
KAT1(fp, 32);
fprintf (fp, "==========\n");
fclose(fp);
sprintf (filename, "ecb_vt.txt");
sprintf (fullname, "%s",filename);
fp = fopen (fullname,"w");
if (fp == NULL) {
printf ("error opening file %s\n", filename);
exit(1);
}
outputHeaderKAT2 (fp, filename);
printf ("KAT2\n"); fflush(stdout);
KAT2(fp, 16);
KAT2(fp, 24);
KAT2(fp, 32);
fprintf (fp, "==========\n");
fclose(fp);
sprintf (filename, "ecb_e_m.txt");
sprintf (fullname, "%s",filename);
fp = fopen (fullname,"w");
if (fp == NULL) {
printf ("error opening file %s\n", filename);
exit(1);
}
outputHeaderMCT1 (fp, filename);
printf ("MCT1\n"); fflush(stdout);
MCT1(fp, 16);
MCT1(fp, 24);
MCT1(fp, 32);
fprintf (fp, "==========\n");
fclose(fp);
sprintf (filename, "ecb_d_m.txt");
sprintf (fullname, "%s",filename);
fp = fopen (fullname,"w");
if (fp == NULL) {
printf ("error opening file %s\n", filename);
exit(1);
}
outputHeaderMCT2 (fp, filename);
printf ("MCT2\n"); fflush(stdout);
MCT2(fp, 16);
MCT2(fp, 24);
MCT2(fp, 32);
fprintf (fp, "==========\n");
fclose(fp);
sprintf (filename, "cbc_e_m.txt");
sprintf (fullname, "%s",filename);
fp = fopen (fullname,"w");
if (fp == NULL) {
printf ("error opening file %s\n", filename);
exit(1);
}
outputHeaderMCT3 (fp, filename);
printf ("MCT3\n"); fflush(stdout);
MCT3(fp, 16);
MCT3(fp, 24);
MCT3(fp, 32);
fprintf (fp, "==========\n");
fclose(fp);
sprintf (filename, "cbc_d_m.txt");
sprintf (fullname, "%s",filename);
fp = fopen (fullname,"w");
if (fp == NULL) {
printf ("error opening file %s\n", filename);
exit(1);
}
outputHeaderMCT4 (fp, filename);
printf ("MCT4\n"); fflush(stdout);
MCT4(fp, 16);
MCT4(fp, 24);
MCT4(fp, 32);
fprintf (fp, "==========\n");
fclose(fp);
return 0;
}
void
outputBlock (FILE *fp, BYTE *block)
{
int k;
for (k=0; k < BLOCKSIZE/8; ++k)
fprintf (fp,"%.2x", block[k]);
}
void
outputKey (FILE *fp, char *K, int len)
{
register int i;
for (i=0; i < len; ++i)
fprintf (fp, "%c",K[i]);
}
void
outputHeaderKAT1 (FILE *fp, char *filename)
{
fprintf (fp,"=========================\n\n");
fprintf (fp,"FILENAME: \"%s\"\n\n", filename);
fprintf (fp,"Electronic Codebook (ECB) Mode\n");
fprintf (fp,"Variable Key Known Answer Tests\n\n");
fprintf (fp,"Algorithm Name: %s\n",ALG_NAME);
fprintf (fp,"Principal Submitter: %s\n\n", SUBMITTER);
}
void
outputHeaderKAT2 (FILE *fp, char *filename)
{
fprintf (fp,"=========================\n\n");
fprintf (fp,"FILENAME: \"%s\"\n\n", filename);
fprintf (fp,"Electronic Codebook (ECB) Mode\n");
fprintf (fp,"Variable Text Known Answer Tests\n\n");
fprintf (fp,"Algorithm Name: %s\n",ALG_NAME);
fprintf (fp,"Principal Submitter: %s\n\n", SUBMITTER);
}
void
outputHeaderMCT1 (FILE *fp, char *filename)
{
fprintf (fp,"=========================\n\n");
fprintf (fp,"FILENAME: \"%s\"\n\n", filename);
fprintf (fp,"Electronic Codebook (ECB) Mode - ENCRYPTION\n");
fprintf (fp,"Monte Carlo Test\n\n");
fprintf (fp,"Algorithm Name: %s\n",ALG_NAME);
fprintf (fp,"Principal Submitter: %s\n\n", SUBMITTER);
}
void
outputHeaderMCT2 (FILE *fp, char *filename)
{
fprintf (fp,"=========================\n\n");
fprintf (fp,"FILENAME: \"%s\"\n\n", filename);
fprintf (fp,"Electronic Codebook (ECB) Mode - DECRYPTION\n");
fprintf (fp,"Monte Carlo Test\n\n");
fprintf (fp,"Algorithm Name: %s\n",ALG_NAME);
fprintf (fp,"Principal Submitter: %s\n\n", SUBMITTER);
}
void
outputHeaderMCT3 (FILE *fp, char *filename)
{
fprintf (fp,"=========================\n\n");
fprintf (fp,"FILENAME: \"%s\"\n\n", filename);
fprintf (fp,"Cipher Block Chaining (CBC) Mode - ENCRYPTION\n");
fprintf (fp,"Monte Carlo Test\n\n");
fprintf (fp,"Algorithm Name: %s\n",ALG_NAME);
fprintf (fp,"Principal Submitter: %s\n\n", SUBMITTER);
}
void
outputHeaderMCT4 (FILE *fp, char *filename)
{
fprintf (fp,"=========================\n\n");
fprintf (fp,"FILENAME: \"%s\"\n\n", filename);
fprintf (fp,"Cipher Block Chaining (CBC) Mode - DECRYPTION\n");
fprintf (fp,"Monte Carlo Test\n\n");
fprintf (fp,"Algorithm Name: %s\n",ALG_NAME);
fprintf (fp,"Principal Submitter: %s\n\n", SUBMITTER);
}
/* Variable key known answer tests
(b = key bytes)
*/
void
KAT1 (FILE *fp, int b)
{
char *K;
keyInstance S;
int k, i;
BYTE pt[BLOCKSIZE/8], ct[BLOCKSIZE/8];
cipherInstance ci;
K = (char *) malloc (2*b);
/* set the plaintext to all 0's */
for (k=0; k<BLOCKSIZE/8; ++k)
pt[k] = 0;
/* set the key to all 0's */
memset (K, '0', 2*b);
fprintf (fp, "==========\n\n");
fprintf (fp, "KEYSIZE=%d\n\n", 8*b);
fprintf (fp, "PT=");
outputBlock (fp, pt);
fprintf (fp, "\n\n");
for (i=0; i < 8*b; ++i) {
K[i/4] = HEX (1 << (3 - (i&3)));
fprintf (fp, "I=%d\n",i+1);
fprintf (fp, "KEY=");
outputKey (fp, K, 2*b);
makeKey (&S, DIR_ENCRYPT, b*8, K);
cipherInit (&ci, MODE_ECB, "");
blockEncrypt (&ci, &S, pt, 128, ct);
fprintf (fp, "\nCT=");
outputBlock (fp, ct);
fprintf (fp, "\n\n");
K[i/4] = '0';
}
free (K);
}
/* Variable text known answer tests
(b = key bytes)
*/
void
KAT2 (FILE *fp, int b)
{
char *K;
keyInstance S;
int i, k;
BYTE pt[BLOCKSIZE/8], ct[BLOCKSIZE/8];
cipherInstance ci;
K = (char *) malloc (2*b);
/* set the plaintext to all 0's */
for (k=0; k<BLOCKSIZE/8; ++k)
pt[k] = 0;
/* set the key to all 0's */
memset (K, '0', 2*b);
fprintf (fp, "==========\n\n");
fprintf (fp, "KEYSIZE=%d\n\n", 8*b);
fprintf (fp, "KEY=");
outputKey (fp, K, 2*b);
fprintf (fp, "\n\n");
makeKey (&S, DIR_ENCRYPT, b*8, K);
cipherInit (&ci, MODE_ECB, "");
for (i=0; i < BLOCKSIZE; ++i) {
fprintf (fp, "I=%d\n",i+1);
pt[i/8] = (BYTE) (1 << (7 - (i&7)));
fprintf (fp, "PT=");
outputBlock (fp, pt);
blockEncrypt (&ci, &S, pt, 128, ct);
fprintf (fp, "\nCT=");
outputBlock (fp, ct);
fprintf (fp, "\n\n");
pt[i/8] = 0;
}
free (K);
}
/* this function is used for computing the new key in the Monte Carlo tests.
K is the current key (hexadecimal string)
b is half the number of bytes of K (which is the same as the number of
bytes of K after K is converted to a binary string)
secondToLast is the second to last plaintext or ciphertext from the
innermost loop of the Monte Carlo test (binary string)
last is the last plaintext or ciphertext from the innermost loop of
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -