2011-01-26 00:56:19 +00:00
|
|
|
/*
|
|
|
|
* Crude test driver for processing the VST and MCT testvector files
|
|
|
|
* generated by the CMVP RNGVS product.
|
|
|
|
*
|
|
|
|
* Note the input files are assumed to have a _very_ specific format
|
|
|
|
* as described in the NIST document "The Random Number Generator
|
|
|
|
* Validation System (RNGVS)", May 25, 2004.
|
|
|
|
*
|
|
|
|
*/
|
2011-01-27 19:10:56 +00:00
|
|
|
|
|
|
|
#define OPENSSL_FIPSAPI
|
|
|
|
|
2011-01-26 00:56:19 +00:00
|
|
|
#include <openssl/opensslconf.h>
|
|
|
|
|
|
|
|
#ifndef OPENSSL_FIPS
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
printf("No FIPS RNG support\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/dsa.h>
|
|
|
|
#include <openssl/fips.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/fips_rand.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "fips_utl.h"
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
static void vst(FILE *in, FILE *out)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
unsigned char *key = NULL;
|
|
|
|
unsigned char *v = NULL;
|
|
|
|
unsigned char *dt = NULL;
|
|
|
|
unsigned char ret[16];
|
|
|
|
char buf[1024];
|
|
|
|
char lbuf[1024];
|
|
|
|
char *keyword, *value;
|
|
|
|
long i, keylen;
|
|
|
|
|
|
|
|
keylen = 0;
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
while(fgets(buf,sizeof buf,in) != NULL)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
2011-03-08 13:27:29 +00:00
|
|
|
fputs(buf,out);
|
2011-01-26 00:56:19 +00:00
|
|
|
if(!strncmp(buf,"[AES 128-Key]", 13))
|
|
|
|
keylen = 16;
|
|
|
|
else if(!strncmp(buf,"[AES 192-Key]", 13))
|
|
|
|
keylen = 24;
|
|
|
|
else if(!strncmp(buf,"[AES 256-Key]", 13))
|
|
|
|
keylen = 32;
|
|
|
|
if (!parse_line(&keyword, &value, lbuf, buf))
|
|
|
|
continue;
|
|
|
|
if(!strcmp(keyword,"Key"))
|
|
|
|
{
|
|
|
|
key=hex2bin_m(value,&i);
|
|
|
|
if (i != keylen)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Invalid key length, expecting %ld\n", keylen);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!strcmp(keyword,"DT"))
|
|
|
|
{
|
|
|
|
dt=hex2bin_m(value,&i);
|
|
|
|
if (i != 16)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Invalid DT length\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!strcmp(keyword,"V"))
|
|
|
|
{
|
|
|
|
v=hex2bin_m(value,&i);
|
|
|
|
if (i != 16)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Invalid V length\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key || !dt)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Missing key or DT\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-05 12:42:31 +00:00
|
|
|
FIPS_x931_set_key(key, keylen);
|
|
|
|
FIPS_x931_seed(v,16);
|
|
|
|
FIPS_x931_set_dt(dt);
|
|
|
|
if (FIPS_x931_bytes(ret,16) <= 0)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Error getting PRNG value\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
OutputValue("R", ret, 16, out, 0);
|
2011-01-26 00:56:19 +00:00
|
|
|
OPENSSL_free(key);
|
|
|
|
key = NULL;
|
|
|
|
OPENSSL_free(dt);
|
|
|
|
dt = NULL;
|
|
|
|
OPENSSL_free(v);
|
|
|
|
v = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
static void mct(FILE *in, FILE *out)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
unsigned char *key = NULL;
|
|
|
|
unsigned char *v = NULL;
|
|
|
|
unsigned char *dt = NULL;
|
|
|
|
unsigned char ret[16];
|
|
|
|
char buf[1024];
|
|
|
|
char lbuf[1024];
|
|
|
|
char *keyword, *value;
|
|
|
|
long i, keylen;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
keylen = 0;
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
while(fgets(buf,sizeof buf,in) != NULL)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
2011-03-08 13:27:29 +00:00
|
|
|
fputs(buf,out);
|
2011-01-26 00:56:19 +00:00
|
|
|
if(!strncmp(buf,"[AES 128-Key]", 13))
|
|
|
|
keylen = 16;
|
|
|
|
else if(!strncmp(buf,"[AES 192-Key]", 13))
|
|
|
|
keylen = 24;
|
|
|
|
else if(!strncmp(buf,"[AES 256-Key]", 13))
|
|
|
|
keylen = 32;
|
|
|
|
if (!parse_line(&keyword, &value, lbuf, buf))
|
|
|
|
continue;
|
|
|
|
if(!strcmp(keyword,"Key"))
|
|
|
|
{
|
|
|
|
key=hex2bin_m(value,&i);
|
|
|
|
if (i != keylen)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Invalid key length, expecting %ld\n", keylen);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!strcmp(keyword,"DT"))
|
|
|
|
{
|
|
|
|
dt=hex2bin_m(value,&i);
|
|
|
|
if (i != 16)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Invalid DT length\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(!strcmp(keyword,"V"))
|
|
|
|
{
|
|
|
|
v=hex2bin_m(value,&i);
|
|
|
|
if (i != 16)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Invalid V length\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key || !dt)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Missing key or DT\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-05 12:42:31 +00:00
|
|
|
FIPS_x931_set_key(key, keylen);
|
|
|
|
FIPS_x931_seed(v,16);
|
2011-01-26 00:56:19 +00:00
|
|
|
for (i = 0; i < 10000; i++)
|
|
|
|
{
|
2011-04-05 12:42:31 +00:00
|
|
|
FIPS_x931_set_dt(dt);
|
|
|
|
if (FIPS_x931_bytes(ret,16) <= 0)
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Error getting PRNG value\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Increment DT */
|
|
|
|
for (j = 15; j >= 0; j--)
|
|
|
|
{
|
|
|
|
dt[j]++;
|
|
|
|
if (dt[j])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
OutputValue("R", ret, 16, out, 0);
|
2011-01-26 00:56:19 +00:00
|
|
|
OPENSSL_free(key);
|
|
|
|
key = NULL;
|
|
|
|
OPENSSL_free(dt);
|
|
|
|
dt = NULL;
|
|
|
|
OPENSSL_free(v);
|
|
|
|
v = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc,char **argv)
|
|
|
|
{
|
2011-03-08 13:27:29 +00:00
|
|
|
FILE *in, *out;
|
|
|
|
if (argc == 4)
|
|
|
|
{
|
|
|
|
in = fopen(argv[2], "r");
|
|
|
|
if (!in)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error opening input file\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
out = fopen(argv[3], "w");
|
|
|
|
if (!out)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error opening output file\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (argc == 2)
|
|
|
|
{
|
|
|
|
in = stdin;
|
|
|
|
out = stdout;
|
|
|
|
}
|
|
|
|
else
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr,"%s [mct|vst]\n",argv[0]);
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-03-25 16:36:46 +00:00
|
|
|
fips_algtest_init();
|
2011-04-05 12:42:31 +00:00
|
|
|
FIPS_x931_reset();
|
|
|
|
if (!FIPS_x931_test_mode())
|
2011-01-26 00:56:19 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Error setting PRNG test mode\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(!strcmp(argv[1],"mct"))
|
2011-03-08 13:27:29 +00:00
|
|
|
mct(in, out);
|
2011-01-26 00:56:19 +00:00
|
|
|
else if(!strcmp(argv[1],"vst"))
|
2011-03-08 13:27:29 +00:00
|
|
|
vst(in, out);
|
2011-01-26 00:56:19 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr,"Don't know how to %s.\n",argv[1]);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-03-08 13:27:29 +00:00
|
|
|
if (argc == 4)
|
|
|
|
{
|
|
|
|
fclose(in);
|
|
|
|
fclose(out);
|
|
|
|
}
|
|
|
|
|
2011-01-26 00:56:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|