fips/fips_[canister|premain].c: make it work with VC6 and add sentinels
[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_x931_set_key(key, keylen);
94             FIPS_x931_seed(v,16);
95             FIPS_x931_set_dt(dt);
96             if (FIPS_x931_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_x931_set_key(key, keylen);
172             FIPS_x931_seed(v,16);
173             for (i = 0; i < 10000; i++)
174                 {
175                     FIPS_x931_set_dt(dt);
176                     if (FIPS_x931_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_algtest_init();
230     FIPS_x931_reset();
231     if (!FIPS_x931_test_mode())
232         {
233         fprintf(stderr, "Error setting PRNG test mode\n");
234         exit(1);
235         }
236     if(!strcmp(argv[1],"mct"))
237         mct(in, out);
238     else if(!strcmp(argv[1],"vst"))
239         vst(in, out);
240     else
241         {
242         fprintf(stderr,"Don't know how to %s.\n",argv[1]);
243         exit(1);
244         }
245
246     if (argc == 4)
247         {
248         fclose(in);
249         fclose(out);
250         }
251
252     return 0;
253     }
254 #endif