786f5420b436c7e5cc0d4e5e0153a18e249a9c60
[openssl.git] / fips / rand / fips_rngvs.c
1 /*
2  * Crude test driver for processing the VST and MCT testvector files
3  * generated by the CMVP RNGVS product.
4  *
5  * Note the input files are assumed to have a _very_ specific format
6  * as described in the NIST document "The Random Number Generator
7  * Validation System (RNGVS)", May 25, 2004.
8  *
9  */
10
11 #define OPENSSL_FIPSAPI
12
13 #include <openssl/opensslconf.h>
14
15 #ifndef OPENSSL_FIPS
16 #include <stdio.h>
17
18 int main(int argc, char **argv)
19 {
20     printf("No FIPS RNG support\n");
21     return 0;
22 }
23 #else
24
25 #include <openssl/bn.h>
26 #include <openssl/dsa.h>
27 #include <openssl/fips.h>
28 #include <openssl/err.h>
29 #include <openssl/rand.h>
30 #include <openssl/fips_rand.h>
31 #include <string.h>
32 #include <ctype.h>
33
34 #include "fips_utl.h"
35
36 static void vst(FILE *in, FILE *out)
37     {
38     unsigned char *key = NULL;
39     unsigned char *v = NULL;
40     unsigned char *dt = NULL;
41     unsigned char ret[16];
42     char buf[1024];
43     char lbuf[1024];
44     char *keyword, *value;
45     long i, keylen;
46
47     keylen = 0;
48
49     while(fgets(buf,sizeof buf,in) != NULL)
50         {
51         fputs(buf,out);
52         if(!strncmp(buf,"[AES 128-Key]", 13))
53                 keylen = 16;
54         else if(!strncmp(buf,"[AES 192-Key]", 13))
55                 keylen = 24;
56         else if(!strncmp(buf,"[AES 256-Key]", 13))
57                 keylen = 32;
58         if (!parse_line(&keyword, &value, lbuf, buf))
59                 continue;
60         if(!strcmp(keyword,"Key"))
61             {
62             key=hex2bin_m(value,&i);
63             if (i != keylen)
64                 {
65                 fprintf(stderr, "Invalid key length, expecting %ld\n", keylen);
66                 return;
67                 }
68             }
69         else if(!strcmp(keyword,"DT"))
70             {
71             dt=hex2bin_m(value,&i);
72             if (i != 16)
73                 {
74                 fprintf(stderr, "Invalid DT length\n");
75                 return;
76                 }
77             }
78         else if(!strcmp(keyword,"V"))
79             {
80             v=hex2bin_m(value,&i);
81             if (i != 16)
82                 {
83                 fprintf(stderr, "Invalid V length\n");
84                 return;
85                 }
86
87             if (!key || !dt)
88                 {
89                 fprintf(stderr, "Missing key or DT\n");
90                 return;
91                 }
92
93             FIPS_rand_set_key(key, keylen);
94             FIPS_rand_seed(v,16);
95             FIPS_rand_set_dt(dt);
96             if (FIPS_rand_bytes(ret,16) <= 0)
97                 {
98                 fprintf(stderr, "Error getting PRNG value\n");
99                 return;
100                 }
101
102             OutputValue("R", ret, 16, out, 0);
103             OPENSSL_free(key);
104             key = NULL;
105             OPENSSL_free(dt);
106             dt = NULL;
107             OPENSSL_free(v);
108             v = NULL;
109             }
110         }
111     }
112
113 static void mct(FILE *in, FILE *out)
114     {
115     unsigned char *key = NULL;
116     unsigned char *v = NULL;
117     unsigned char *dt = NULL;
118     unsigned char ret[16];
119     char buf[1024];
120     char lbuf[1024];
121     char *keyword, *value;
122     long i, keylen;
123     int j;
124
125     keylen = 0;
126
127     while(fgets(buf,sizeof buf,in) != NULL)
128         {
129         fputs(buf,out);
130         if(!strncmp(buf,"[AES 128-Key]", 13))
131                 keylen = 16;
132         else if(!strncmp(buf,"[AES 192-Key]", 13))
133                 keylen = 24;
134         else if(!strncmp(buf,"[AES 256-Key]", 13))
135                 keylen = 32;
136         if (!parse_line(&keyword, &value, lbuf, buf))
137                 continue;
138         if(!strcmp(keyword,"Key"))
139             {
140             key=hex2bin_m(value,&i);
141             if (i != keylen)
142                 {
143                 fprintf(stderr, "Invalid key length, expecting %ld\n", keylen);
144                 return;
145                 }
146             }
147         else if(!strcmp(keyword,"DT"))
148             {
149             dt=hex2bin_m(value,&i);
150             if (i != 16)
151                 {
152                 fprintf(stderr, "Invalid DT length\n");
153                 return;
154                 }
155             }
156         else if(!strcmp(keyword,"V"))
157             {
158             v=hex2bin_m(value,&i);
159             if (i != 16)
160                 {
161                 fprintf(stderr, "Invalid V length\n");
162                 return;
163                 }
164
165             if (!key || !dt)
166                 {
167                 fprintf(stderr, "Missing key or DT\n");
168                 return;
169                 }
170
171             FIPS_rand_set_key(key, keylen);
172             FIPS_rand_seed(v,16);
173             for (i = 0; i < 10000; i++)
174                 {
175                     FIPS_rand_set_dt(dt);
176                     if (FIPS_rand_bytes(ret,16) <= 0)
177                         {
178                         fprintf(stderr, "Error getting PRNG value\n");
179                         return;
180                         }
181                     /* Increment DT */
182                     for (j = 15; j >= 0; j--)
183                         {
184                         dt[j]++;
185                         if (dt[j])
186                                 break;
187                         }
188                 }
189
190             OutputValue("R", ret, 16, out, 0);
191             OPENSSL_free(key);
192             key = NULL;
193             OPENSSL_free(dt);
194             dt = NULL;
195             OPENSSL_free(v);
196             v = NULL;
197             }
198         }
199     }
200
201 int main(int argc,char **argv)
202     {
203     FILE *in, *out;
204     if (argc == 4)
205         {
206         in = fopen(argv[2], "r");
207         if (!in)
208                 {
209                 fprintf(stderr, "Error opening input file\n");
210                 exit(1);
211                 }
212         out = fopen(argv[3], "w");
213         if (!out)
214                 {
215                 fprintf(stderr, "Error opening output file\n");
216                 exit(1);
217                 }
218         }
219     else if (argc == 2)
220         {
221         in = stdin;
222         out = stdout;
223         }
224     else
225         {
226         fprintf(stderr,"%s [mct|vst]\n",argv[0]);
227         exit(1);
228         }
229     fips_set_error_print();
230     if(!FIPS_mode_set(1))
231         exit(1);
232     FIPS_rand_reset();
233     if (!FIPS_rand_test_mode())
234         {
235         fprintf(stderr, "Error setting PRNG test mode\n");
236         exit(1);
237         }
238     if(!strcmp(argv[1],"mct"))
239         mct(in, out);
240     else if(!strcmp(argv[1],"vst"))
241         vst(in, out);
242     else
243         {
244         fprintf(stderr,"Don't know how to %s.\n",argv[1]);
245         exit(1);
246         }
247
248     if (argc == 4)
249         {
250         fclose(in);
251         fclose(out);
252         }
253
254     return 0;
255     }
256 #endif