Reformat.

This commit is contained in:
Ben Laurie 2003-07-29 14:06:02 +00:00
parent c473d53898
commit 0b8c5413a4

View File

@ -551,289 +551,289 @@ int do_mct(char *amode,
----------------------------*/ ----------------------------*/
int proc_file(char *rqfile) int proc_file(char *rqfile)
{ {
char afn[256], rfn[256]; char afn[256], rfn[256];
FILE *afp = NULL, *rfp = NULL; FILE *afp = NULL, *rfp = NULL;
char ibuf[2048]; char ibuf[2048];
int ilen, len, ret = 0; int ilen, len, ret = 0;
char algo[8] = ""; char algo[8] = "";
char amode[8] = ""; char amode[8] = "";
char atest[8] = ""; char atest[8] = "";
int akeysz = 0; int akeysz = 0;
unsigned char iVec[20], aKey[40]; unsigned char iVec[20], aKey[40];
int dir = -1, err = 0, step = 0; int dir = -1, err = 0, step = 0;
char plaintext[2048]; char plaintext[2048];
unsigned char ciphertext[2048]; unsigned char ciphertext[2048];
char *rp; char *rp;
EVP_CIPHER_CTX ctx; EVP_CIPHER_CTX ctx;
if (!rqfile || !(*rqfile)) if (!rqfile || !(*rqfile))
{
printf("No req file\n");
return -1;
}
strcpy(afn, rqfile);
if ((afp = fopen(afn, "r")) == NULL)
{
printf("Cannot open file: %s, %s\n",
afn, strerror(errno));
return -1;
}
strcpy(rfn,afn);
rp=strstr(rfn,"req/");
assert(rp);
memcpy(rp,"rsp",3);
rp = strstr(rfn, ".req");
memcpy(rp, ".rsp", 4);
if ((rfp = fopen(rfn, "w")) == NULL)
{
printf("Cannot open file: %s, %s\n",
rfn, strerror(errno));
fclose(afp);
afp = NULL;
return -1;
}
while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL)
{
ilen = strlen(ibuf);
// printf("step=%d ibuf=%s",step,ibuf);
switch (step)
{ {
case 0: /* read preamble */ printf("No req file\n");
if (ibuf[0] == '\n') return -1;
{ /* end of preamble */ }
if ((*algo == '\0') || strcpy(afn, rqfile);
(*amode == '\0') ||
(akeysz == 0)) if ((afp = fopen(afn, "r")) == NULL)
{ {
printf("Missing Algorithm, Mode or KeySize (%s/%s/%d)\n", printf("Cannot open file: %s, %s\n",
algo,amode,akeysz); afn, strerror(errno));
err = 1; return -1;
} }
else strcpy(rfn,afn);
{ rp=strstr(rfn,"req/");
fputs(ibuf, rfp); assert(rp);
++ step; memcpy(rp,"rsp",3);
} rp = strstr(rfn, ".req");
} memcpy(rp, ".rsp", 4);
else if (ibuf[0] != '#') if ((rfp = fopen(rfn, "w")) == NULL)
{
printf("Cannot open file: %s, %s\n",
rfn, strerror(errno));
fclose(afp);
afp = NULL;
return -1;
}
while (!err && (fgets(ibuf, sizeof(ibuf), afp)) != NULL)
{
ilen = strlen(ibuf);
// printf("step=%d ibuf=%s",step,ibuf);
switch (step)
{ {
printf("Invalid preamble item: %s\n", ibuf); case 0: /* read preamble */
err = 1; if (ibuf[0] == '\n')
} { /* end of preamble */
else if ((*algo == '\0') ||
{ /* process preamble */ (*amode == '\0') ||
char *xp, *pp = ibuf+2; (akeysz == 0))
int n; {
if (akeysz) printf("Missing Algorithm, Mode or KeySize (%s/%s/%d)\n",
{ /* insert current time & date */ algo,amode,akeysz);
time_t rtim = time(0); err = 1;
fprintf(rfp, "# %s", ctime(&rtim)); }
else
{
fputs(ibuf, rfp);
++ step;
}
} }
else else if (ibuf[0] != '#')
{ {
fputs(ibuf, rfp); printf("Invalid preamble item: %s\n", ibuf);
if (strncmp(pp, "AESVS ", 6) == 0) err = 1;
{ }
strcpy(algo, "AES"); else
/* get test type */ { /* process preamble */
pp += 6; char *xp, *pp = ibuf+2;
xp = strchr(pp, ' '); int n;
n = xp-pp; if (akeysz)
strncpy(atest, pp, n); { /* insert current time & date */
atest[n] = '\0'; time_t rtim = time(0);
/* get mode */ fprintf(rfp, "# %s", ctime(&rtim));
xp = strrchr(pp, ' '); /* get mode" */
n = strlen(xp+1)-1;
strncpy(amode, xp+1, n);
amode[n] = '\0';
/* amode[3] = '\0'; */
printf("Test = %s, Mode = %s\n", atest, amode);
} }
else if (strncasecmp(pp, "Key Length : ", 13) == 0) else
{ {
akeysz = atoi(pp+13); fputs(ibuf, rfp);
printf("Key size = %d\n", akeysz); if (strncmp(pp, "AESVS ", 6) == 0)
{
strcpy(algo, "AES");
/* get test type */
pp += 6;
xp = strchr(pp, ' ');
n = xp-pp;
strncpy(atest, pp, n);
atest[n] = '\0';
/* get mode */
xp = strrchr(pp, ' '); /* get mode" */
n = strlen(xp+1)-1;
strncpy(amode, xp+1, n);
amode[n] = '\0';
/* amode[3] = '\0'; */
printf("Test = %s, Mode = %s\n", atest, amode);
}
else if (strncasecmp(pp, "Key Length : ", 13) == 0)
{
akeysz = atoi(pp+13);
printf("Key size = %d\n", akeysz);
}
} }
} }
} break;
break;
case 1: /* [ENCRYPT] | [DECRYPT] */ case 1: /* [ENCRYPT] | [DECRYPT] */
if (ibuf[0] == '[') if (ibuf[0] == '[')
{
fputs(ibuf, rfp);
++step;
if (strncasecmp(ibuf, "[ENCRYPT]", 9) == 0)
dir = 1;
else if (strncasecmp(ibuf, "[DECRYPT]", 9) == 0)
dir = 0;
else
{ {
printf("Invalid keyword: %s\n", ibuf); fputs(ibuf, rfp);
err = 1; ++step;
if (strncasecmp(ibuf, "[ENCRYPT]", 9) == 0)
dir = 1;
else if (strncasecmp(ibuf, "[DECRYPT]", 9) == 0)
dir = 0;
else
{
printf("Invalid keyword: %s\n", ibuf);
err = 1;
}
break;
} }
break; else if (dir == -1)
} {
else if (dir == -1) err = 1;
{ printf("Missing ENCRYPT/DECRYPT keyword\n");
err = 1; break;
printf("Missing ENCRYPT/DECRYPT keyword\n"); }
break; else
} step = 2;
else
step = 2;
case 2: /* KEY = xxxx */ case 2: /* KEY = xxxx */
fputs(ibuf, rfp); fputs(ibuf, rfp);
if(*ibuf == '\n') if(*ibuf == '\n')
break; break;
if(!strncasecmp(ibuf,"COUNT = ",8)) if(!strncasecmp(ibuf,"COUNT = ",8))
break; break;
if (strncasecmp(ibuf, "KEY = ", 6) != 0) if (strncasecmp(ibuf, "KEY = ", 6) != 0)
{
printf("Missing KEY\n");
err = 1;
}
else
{
len = hex2bin((char*)ibuf+6, strlen(ibuf+6)-1, aKey);
if (len < 0)
{ {
printf("Invalid KEY\n"); printf("Missing KEY\n");
err =1; err = 1;
break;
} }
PrintValue("KEY", aKey, len); else
if (strcmp(amode, "ECB") == 0)
{ {
memset(iVec, 0, sizeof(iVec)); len = hex2bin((char*)ibuf+6, strlen(ibuf+6)-1, aKey);
step = (dir)? 4: 5; /* no ivec for ECB */ if (len < 0)
{
printf("Invalid KEY\n");
err =1;
break;
}
PrintValue("KEY", aKey, len);
if (strcmp(amode, "ECB") == 0)
{
memset(iVec, 0, sizeof(iVec));
step = (dir)? 4: 5; /* no ivec for ECB */
}
else
++step;
} }
else break;
++step;
}
break;
case 3: /* IV = xxxx */ case 3: /* IV = xxxx */
fputs(ibuf, rfp); fputs(ibuf, rfp);
if (strncasecmp(ibuf, "IV = ", 5) != 0) if (strncasecmp(ibuf, "IV = ", 5) != 0)
{
printf("Missing IV\n");
err = 1;
}
else
{
len = hex2bin((char*)ibuf+5, strlen(ibuf+5)-1, iVec);
if (len < 0)
{ {
printf("Invalid IV\n"); printf("Missing IV\n");
err =1; err = 1;
break;
} }
PrintValue("IV", iVec, len); else
step = (dir)? 4: 5; {
} len = hex2bin((char*)ibuf+5, strlen(ibuf+5)-1, iVec);
break; if (len < 0)
{
printf("Invalid IV\n");
err =1;
break;
}
PrintValue("IV", iVec, len);
step = (dir)? 4: 5;
}
break;
case 4: /* PLAINTEXT = xxxx */ case 4: /* PLAINTEXT = xxxx */
fputs(ibuf, rfp); fputs(ibuf, rfp);
if (strncasecmp(ibuf, "PLAINTEXT = ", 12) != 0) if (strncasecmp(ibuf, "PLAINTEXT = ", 12) != 0)
{
printf("Missing PLAINTEXT\n");
err = 1;
}
else
{
int nn = strlen(ibuf+12);
len = hex2bin((char*)ibuf+12, nn-1,
(unsigned char*)plaintext);
if (len < 0)
{ {
printf("Invalid PLAINTEXT: %s", ibuf+12); printf("Missing PLAINTEXT\n");
err =1; err = 1;
break;
} }
if (len >= sizeof(plaintext)) else
{ {
printf("Buffer overflow\n"); int nn = strlen(ibuf+12);
len = hex2bin((char*)ibuf+12, nn-1,
(unsigned char*)plaintext);
if (len < 0)
{
printf("Invalid PLAINTEXT: %s", ibuf+12);
err =1;
break;
}
if (len >= sizeof(plaintext))
{
printf("Buffer overflow\n");
}
PrintValue("PLAINTEXT", (unsigned char*)plaintext, len);
if (strcmp(atest, "MCT") == 0) /* Monte Carlo Test */
{
if(do_mct(amode, akeysz, aKey, iVec,
dir, (unsigned char*)plaintext, len,
rfp) < 0)
exit(1);
}
else
{
ret = AESTest(&ctx, amode, akeysz, aKey, iVec,
dir, /* 0 = decrypt, 1 = encrypt */
plaintext, ciphertext, len);
OutputValue("CIPHERTEXT",ciphertext,len,rfp,0);
}
step = 6;
} }
PrintValue("PLAINTEXT", (unsigned char*)plaintext, len); break;
if (strcmp(atest, "MCT") == 0) /* Monte Carlo Test */
{
if(do_mct(amode, akeysz, aKey, iVec,
dir, (unsigned char*)plaintext, len,
rfp) < 0)
exit(1);
}
else
{
ret = AESTest(&ctx, amode, akeysz, aKey, iVec,
dir, /* 0 = decrypt, 1 = encrypt */
plaintext, ciphertext, len);
OutputValue("CIPHERTEXT",ciphertext,len,rfp,0);
}
step = 6;
}
break;
case 5: /* CIPHERTEXT = xxxx */ case 5: /* CIPHERTEXT = xxxx */
fputs(ibuf, rfp); fputs(ibuf, rfp);
if (strncasecmp(ibuf, "CIPHERTEXT = ", 13) != 0) if (strncasecmp(ibuf, "CIPHERTEXT = ", 13) != 0)
{
printf("Missing KEY\n");
err = 1;
}
else
{
len = hex2bin((char*)ibuf+13, strlen(ibuf+13)-1, ciphertext);
if (len < 0)
{ {
printf("Invalid CIPHERTEXT\n"); printf("Missing KEY\n");
err =1; err = 1;
break;
} }
else
PrintValue("CIPHERTEXT", ciphertext, len);
if (strcmp(atest, "MCT") == 0) /* Monte Carlo Test */
{ {
do_mct(amode, akeysz, aKey, iVec, len = hex2bin((char*)ibuf+13, strlen(ibuf+13)-1, ciphertext);
dir, ciphertext, len, rfp); if (len < 0)
{
printf("Invalid CIPHERTEXT\n");
err =1;
break;
}
PrintValue("CIPHERTEXT", ciphertext, len);
if (strcmp(atest, "MCT") == 0) /* Monte Carlo Test */
{
do_mct(amode, akeysz, aKey, iVec,
dir, ciphertext, len, rfp);
}
else
{
ret = AESTest(&ctx, amode, akeysz, aKey, iVec,
dir, /* 0 = decrypt, 1 = encrypt */
plaintext, ciphertext, len);
OutputValue("PLAINTEXT",(unsigned char *)plaintext,len,rfp,
0);
}
step = 6;
} }
else break;
{
ret = AESTest(&ctx, amode, akeysz, aKey, iVec,
dir, /* 0 = decrypt, 1 = encrypt */
plaintext, ciphertext, len);
OutputValue("PLAINTEXT",(unsigned char *)plaintext,len,rfp,
0);
}
step = 6;
}
break;
case 6: case 6:
if (ibuf[0] != '\n') if (ibuf[0] != '\n')
{ {
err = 1; err = 1;
printf("Missing terminator\n"); printf("Missing terminator\n");
}
else if (strcmp(atest, "MCT") != 0)
{ /* MCT already added terminating nl */
fputs(ibuf, rfp);
}
step = 1;
break;
} }
else if (strcmp(atest, "MCT") != 0)
{ /* MCT already added terminating nl */
fputs(ibuf, rfp);
}
step = 1;
break;
} }
if (rfp)
fclose(rfp);
if (afp)
fclose(afp);
return err;
} }
if (rfp)
fclose(rfp);
if (afp)
fclose(afp);
return err;
}
/*-------------------------------------------------- /*--------------------------------------------------
Processes either a single file or Processes either a single file or
@ -845,77 +845,69 @@ int proc_file(char *rqfile)
The default is: -d req.txt The default is: -d req.txt
--------------------------------------------------*/ --------------------------------------------------*/
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char *rqlist = "req.txt"; char *rqlist = "req.txt";
FILE *fp = NULL; FILE *fp = NULL;
char fn[250] = "", rfn[256] = ""; char fn[250] = "", rfn[256] = "";
int f_opt = 0, d_opt = 1; int f_opt = 0, d_opt = 1;
/*
unsigned char x[1];
sb(x,1,1);
sb(x,3,1);
printf("%02x %d %d %d\n",x[0],gb(x,0),gb(x,1),gb(x,2));
exit(0);
*/
#ifdef FIPS #ifdef FIPS
FIPS_mode_set(1); FIPS_mode_set(1);
#endif #endif
ERR_load_crypto_strings(); ERR_load_crypto_strings();
if (argc > 1) if (argc > 1)
{
if (strcasecmp(argv[1], "-d") == 0)
{ {
d_opt = 1; if (strcasecmp(argv[1], "-d") == 0)
}
else if (strcasecmp(argv[1], "-f") == 0)
{
f_opt = 1;
d_opt = 0;
}
else
{
printf("Invalid parameter: %s\n", argv[1]);
return 0;
}
if (argc < 3)
{
printf("Missing parameter\n");
return 0;
}
if (d_opt)
rqlist = argv[2];
else
strcpy(fn, argv[2]);
}
if (d_opt)
{ /* list of files (directory) */
if (!(fp = fopen(rqlist, "r")))
{
printf("Cannot open req list file\n");
return -1;
}
while (fgets(fn, sizeof(fn), fp))
{
strtok(fn, "\r\n");
strcpy(rfn, fn);
printf("Processing: %s\n", rfn);
if (proc_file(rfn))
{ {
printf(">>> Processing failed for: %s <<<\n", rfn); d_opt = 1;
exit(1); }
else if (strcasecmp(argv[1], "-f") == 0)
{
f_opt = 1;
d_opt = 0;
}
else
{
printf("Invalid parameter: %s\n", argv[1]);
return 0;
}
if (argc < 3)
{
printf("Missing parameter\n");
return 0;
}
if (d_opt)
rqlist = argv[2];
else
strcpy(fn, argv[2]);
}
if (d_opt)
{ /* list of files (directory) */
if (!(fp = fopen(rqlist, "r")))
{
printf("Cannot open req list file\n");
return -1;
}
while (fgets(fn, sizeof(fn), fp))
{
strtok(fn, "\r\n");
strcpy(rfn, fn);
printf("Processing: %s\n", rfn);
if (proc_file(rfn))
{
printf(">>> Processing failed for: %s <<<\n", rfn);
exit(1);
}
}
fclose(fp);
}
else /* single file */
{
printf("Processing: %s\n", fn);
if (proc_file(fn))
{
printf(">>> Processing failed for: %s <<<\n", fn);
} }
} }
fclose(fp); return 0;
} }
else /* single file */
{
printf("Processing: %s\n", fn);
if (proc_file(fn))
{
printf(">>> Processing failed for: %s <<<\n", fn);
}
}
return 0;
}