And so it begins...
[oweals/openssl.git] / fips-1.0 / 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 #include <openssl/opensslconf.h>
11
12 #ifndef OPENSSL_FIPS
13 #include <stdio.h>
14 int main()
15 {
16     printf("No FIPS RNG support\n");
17     return 0;
18 }
19 #else
20
21 #include <openssl/bn.h>
22 #include <openssl/dsa.h>
23 #include <openssl/fips.h>
24 #include <openssl/err.h>
25 #include <openssl/rand.h>
26 #include <openssl/fips_rand.h>
27 #include <openssl/x509v3.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #include "fips_utl.h"
32
33 void vst()
34     {
35     unsigned char *key = NULL;
36     unsigned char *v = NULL;
37     unsigned char *dt = NULL;
38     unsigned char ret[16];
39     char buf[1024];
40     char lbuf[1024];
41     char *keyword, *value;
42     long i, keylen;
43
44     keylen = 0;
45
46     while(fgets(buf,sizeof buf,stdin) != NULL)
47         {
48         fputs(buf,stdout);
49         if(!strncmp(buf,"[AES 128-Key]", 13))
50                 keylen = 16;
51         else if(!strncmp(buf,"[AES 192-Key]", 13))
52                 keylen = 24;
53         else if(!strncmp(buf,"[AES 256-Key]", 13))
54                 keylen = 32;
55         if (!parse_line(&keyword, &value, lbuf, buf))
56                 continue;
57         if(!strcmp(keyword,"Key"))
58             {
59             key=hex2bin_m(value,&i);
60             if (i != keylen)
61                 {
62                 fprintf(stderr, "Invalid key length, expecting %ld\n", keylen);
63                 return;
64                 }
65             }
66         else if(!strcmp(keyword,"DT"))
67             {
68             dt=hex2bin_m(value,&i);
69             if (i != 16)
70                 {
71                 fprintf(stderr, "Invalid DT length\n");
72                 return;
73                 }
74             }
75         else if(!strcmp(keyword,"V"))
76             {
77             v=hex2bin_m(value,&i);
78             if (i != 16)
79                 {
80                 fprintf(stderr, "Invalid V length\n");
81                 return;
82                 }
83
84             if (!key || !dt)
85                 {
86                 fprintf(stderr, "Missing key or DT\n");
87                 return;
88                 }
89
90             FIPS_rand_set_key(key, keylen);
91             FIPS_rand_seed(v,16);
92             FIPS_rand_set_dt(dt);
93             if (FIPS_rand_bytes(ret,16) <= 0)
94                 {
95                 fprintf(stderr, "Error getting PRNG value\n");
96                 return;
97                 }
98
99             pv("R",ret,16);
100             OPENSSL_free(key);
101             key = NULL;
102             OPENSSL_free(dt);
103             dt = NULL;
104             OPENSSL_free(v);
105             v = NULL;
106             }
107         }
108     }
109
110 void mct()
111     {
112     unsigned char *key = NULL;
113     unsigned char *v = NULL;
114     unsigned char *dt = NULL;
115     unsigned char ret[16];
116     char buf[1024];
117     char lbuf[1024];
118     char *keyword, *value;
119     long i, keylen;
120     int j;
121
122     keylen = 0;
123
124     while(fgets(buf,sizeof buf,stdin) != NULL)
125         {
126         fputs(buf,stdout);
127         if(!strncmp(buf,"[AES 128-Key]", 13))
128                 keylen = 16;
129         else if(!strncmp(buf,"[AES 192-Key]", 13))
130                 keylen = 24;
131         else if(!strncmp(buf,"[AES 256-Key]", 13))
132                 keylen = 32;
133         if (!parse_line(&keyword, &value, lbuf, buf))
134                 continue;
135         if(!strcmp(keyword,"Key"))
136             {
137             key=hex2bin_m(value,&i);
138             if (i != keylen)
139                 {
140                 fprintf(stderr, "Invalid key length, expecting %ld\n", keylen);
141                 return;
142                 }
143             }
144         else if(!strcmp(keyword,"DT"))
145             {
146             dt=hex2bin_m(value,&i);
147             if (i != 16)
148                 {
149                 fprintf(stderr, "Invalid DT length\n");
150                 return;
151                 }
152             }
153         else if(!strcmp(keyword,"V"))
154             {
155             v=hex2bin_m(value,&i);
156             if (i != 16)
157                 {
158                 fprintf(stderr, "Invalid V length\n");
159                 return;
160                 }
161
162             if (!key || !dt)
163                 {
164                 fprintf(stderr, "Missing key or DT\n");
165                 return;
166                 }
167
168             FIPS_rand_set_key(key, keylen);
169             FIPS_rand_seed(v,16);
170             for (i = 0; i < 10000; i++)
171                 {
172                     FIPS_rand_set_dt(dt);
173                     if (FIPS_rand_bytes(ret,16) <= 0)
174                         {
175                         fprintf(stderr, "Error getting PRNG value\n");
176                         return;
177                         }
178                     /* Increment DT */
179                     for (j = 15; j >= 0; j--)
180                         {
181                         dt[j]++;
182                         if (dt[j])
183                                 break;
184                         }
185                 }
186
187             pv("R",ret,16);
188             OPENSSL_free(key);
189             key = NULL;
190             OPENSSL_free(dt);
191             dt = NULL;
192             OPENSSL_free(v);
193             v = NULL;
194             }
195         }
196     }
197
198 int main(int argc,char **argv)
199     {
200     if(argc != 2)
201         {
202         fprintf(stderr,"%s [mct|vst]\n",argv[0]);
203         exit(1);
204         }
205     if(!FIPS_mode_set(1))
206         {
207         do_print_errors();
208         exit(1);
209         }
210     FIPS_rand_reset();
211     if (!FIPS_rand_test_mode())
212         {
213         fprintf(stderr, "Error setting PRNG test mode\n");
214         do_print_errors();
215         exit(1);
216         }
217     if(!strcmp(argv[1],"mct"))
218         mct();
219     else if(!strcmp(argv[1],"vst"))
220         vst();
221     else
222         {
223         fprintf(stderr,"Don't know how to %s.\n",argv[1]);
224         exit(1);
225         }
226
227     return 0;
228     }
229 #endif