4d84884e361bf2ef6a1770fa2d4bfa7567069faf
[openssl.git] / fips / rand / fips_drbgvs.c
1 /* fips/rand/fips_drbgvs.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  */
53
54
55 #define OPENSSL_FIPSAPI
56 #include <openssl/opensslconf.h>
57
58 #ifndef OPENSSL_FIPS
59 #include <stdio.h>
60
61 int main(int argc, char **argv)
62 {
63     printf("No FIPS DRBG support\n");
64     return(0);
65 }
66 #else
67
68 #include <openssl/bn.h>
69 #include <openssl/dsa.h>
70 #include <openssl/fips.h>
71 #include <openssl/fips_rand.h>
72 #include <openssl/err.h>
73 #include <openssl/evp.h>
74 #include <string.h>
75 #include <ctype.h>
76
77 #include "fips_utl.h"
78
79 static int parse_md(char *str)
80         {
81         switch(atoi(str + 5))
82                 {
83                 case 1:
84                 return NID_sha1;
85
86                 case 224:
87                 return NID_sha224;
88
89                 case 256:
90                 return NID_sha256;
91
92                 case 384:
93                 return NID_sha384;
94
95                 case 512:
96                 return NID_sha512;
97
98                 }
99
100         return NID_undef;
101         }
102
103 static int parse_ec(char *str)
104         {
105         int curve_nid, md_nid;
106         char *md;
107         md = strchr(str, ' ');
108         if (!md)
109                 return NID_undef;
110         if (!strncmp(str, "[P-256", 6))
111                 curve_nid = NID_X9_62_prime256v1;
112         else if (!strncmp(str, "[P-384", 6))
113                 curve_nid = NID_secp384r1;
114         else if (!strncmp(str, "[P-521", 6))
115                 curve_nid = NID_secp521r1;
116         else
117                 return NID_undef;
118         md_nid = parse_md(md);
119         if (md_nid == NID_undef)
120                 return NID_undef;
121         return (curve_nid << 16) | md_nid;
122         }
123
124 static int parse_aes(char *str, int *pdf)
125         {
126
127         if (!strncmp(str + 9, "no", 2))
128                 *pdf = 0;
129         else
130                 *pdf = DRBG_FLAG_CTR_USE_DF;
131
132         switch(atoi(str + 5))
133                 {
134                 case 128:
135                 return NID_aes_128_ctr;
136
137                 case 192:
138                 return NID_aes_192_ctr;
139
140                 case 256:
141                 return NID_aes_256_ctr;
142
143                 default:
144                 return NID_undef;
145
146                 }
147         }
148
149 typedef struct 
150         {
151         unsigned char *ent;
152         size_t entlen;
153         unsigned char *nonce;
154         size_t noncelen;
155         } TEST_ENT;
156
157 static size_t test_entropy(DRBG_CTX *dctx, unsigned char **pout,
158                                 int entropy, size_t min_len, size_t max_len)
159         {
160         TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
161         *pout = (unsigned char *)t->ent;
162         return t->entlen;
163         }
164
165 static size_t test_nonce(DRBG_CTX *dctx, unsigned char **pout,
166                                 int entropy, size_t min_len, size_t max_len)
167         {
168         TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
169         *pout = (unsigned char *)t->nonce;
170         return t->noncelen;
171         }
172
173
174
175 int main(int argc,char **argv)
176         {
177         FILE *in, *out;
178         DRBG_CTX *dctx = NULL;
179         TEST_ENT t;
180         int r, nid = 0;
181         int pr = 0;
182         char buf[2048], lbuf[2048];
183         unsigned char randout[2048];
184         char *keyword = NULL, *value = NULL;
185
186         unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL;
187         long entlen, noncelen, perslen, adinlen;
188         int df = 0;
189
190         enum dtype { DRBG_NONE, DRBG_CTR, DRBG_HASH, DRBG_HMAC, DRBG_DUAL_EC }
191                 drbg_type = DRBG_NONE;
192
193         int randoutlen = 0;
194
195         int gen = 0;
196
197         fips_algtest_init();
198
199         if (argc == 3)
200                 {
201                 in = fopen(argv[1], "r");
202                 if (!in)
203                         {
204                         fprintf(stderr, "Error opening input file\n");
205                         exit(1);
206                         }
207                 out = fopen(argv[2], "w");
208                 if (!out)
209                         {
210                         fprintf(stderr, "Error opening output file\n");
211                         exit(1);
212                         }
213                 }
214         else if (argc == 1)
215                 {
216                 in = stdin;
217                 out = stdout;
218                 }
219         else
220                 {
221                 fprintf(stderr,"%s (infile outfile)\n",argv[0]);
222                 exit(1);
223                 }
224
225         while (fgets(buf, sizeof(buf), in) != NULL)
226                 {
227                 fputs(buf, out);
228                 if (drbg_type == DRBG_NONE)
229                         {
230                         if (strstr(buf, "CTR_DRBG"))
231                                 drbg_type = DRBG_CTR;
232                         else if (strstr(buf, "Hash_DRBG"))
233                                 drbg_type = DRBG_HASH;
234                         else if (strstr(buf, "HMAC_DRBG"))
235                                 drbg_type = DRBG_HMAC;
236                         else if (strstr(buf, "Dual_EC_DRBG"))
237                                 drbg_type = DRBG_DUAL_EC;
238                         else
239                                 continue;
240                         }
241                 if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5))
242                         {
243                         nid = parse_md(buf);
244                         if (nid == NID_undef)
245                                 exit(1);
246                         if (drbg_type == DRBG_HMAC)
247                                 {
248                                 switch (nid)
249                                         {
250                                         case NID_sha1:
251                                         nid = NID_hmacWithSHA1;
252                                         break;
253
254                                         case NID_sha224:
255                                         nid = NID_hmacWithSHA224;
256                                         break;
257
258                                         case NID_sha256:
259                                         nid = NID_hmacWithSHA256;
260                                         break;
261
262                                         case NID_sha384:
263                                         nid = NID_hmacWithSHA384;
264                                         break;
265
266                                         case NID_sha512:
267                                         nid = NID_hmacWithSHA512;
268                                         break;
269
270                                         default:
271                                         exit(1);
272                                         }
273                                 }
274                         }
275                 if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5))
276                         {
277                         nid = parse_aes(buf, &df);
278                         if (nid == NID_undef)
279                                 exit(1);
280                         }
281                 if (strlen(buf) > 12 && !strncmp(buf, "[P-", 3))
282                         {
283                         nid = parse_ec(buf);
284                         if (nid == NID_undef)
285                                 exit(1);
286                         }
287                 if (!parse_line(&keyword, &value, lbuf, buf))
288                         continue;
289
290                 if (!strcmp(keyword, "[PredictionResistance"))
291                         {
292                         if (!strcmp(value, "True]"))
293                                 pr = 1;
294                         else if (!strcmp(value, "False]"))
295                                 pr = 0;
296                         else
297                                 exit(1);
298                         }
299
300                 if (!strcmp(keyword, "EntropyInput"))
301                         {
302                         ent = hex2bin_m(value, &entlen);
303                         t.ent = ent;
304                         t.entlen = entlen;
305                         }
306
307                 if (!strcmp(keyword, "Nonce"))
308                         {
309                         nonce = hex2bin_m(value, &noncelen);
310                         t.nonce = nonce;
311                         t.noncelen = noncelen;
312                         }
313
314                 if (!strcmp(keyword, "PersonalizationString"))
315                         {
316                         pers = hex2bin_m(value, &perslen);
317                         if (nid == 0)
318                                 {
319                                 fprintf(stderr, "DRBG type not recognised!\n");
320                                 exit (1);
321                                 }
322                         dctx = FIPS_drbg_new(nid, df | DRBG_FLAG_TEST);
323                         if (!dctx)
324                                 exit (1);
325                         FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0,
326                                                         test_nonce, 0);
327                         FIPS_drbg_set_app_data(dctx, &t);
328                         randoutlen = (int)FIPS_drbg_get_blocklength(dctx);
329                         r = FIPS_drbg_instantiate(dctx, pers, perslen);
330                         if (!r)
331                                 {
332                                 fprintf(stderr, "Error instantiating DRBG\n");
333                                 exit(1);
334                                 }
335                         OPENSSL_free(pers);
336                         OPENSSL_free(ent);
337                         OPENSSL_free(nonce);
338                         ent = nonce = pers = NULL;
339                         gen = 0;
340                         }
341
342                 if (!strcmp(keyword, "AdditionalInput"))
343                         {
344                         adin = hex2bin_m(value, &adinlen);
345                         if (pr)
346                                 continue;
347                         r = FIPS_drbg_generate(dctx, randout, randoutlen, 0, 0,
348                                                                 adin, adinlen);
349                         if (!r)
350                                 {
351                                 fprintf(stderr, "Error generating DRBG bits\n");
352                                 exit(1);
353                                 }
354                         if (!r)
355                                 exit(1);
356                         OPENSSL_free(adin);
357                         adin = NULL;
358                         gen++;
359                         }
360
361                 if (pr)
362                         {
363                         if (!strcmp(keyword, "EntropyInputPR"))
364                                 {
365                                 ent = hex2bin_m(value, &entlen);
366                                 t.ent = ent;
367                                 t.entlen = entlen;
368                                 r = FIPS_drbg_generate(dctx,
369                                                         randout, randoutlen,
370                                                         0, 1, adin, adinlen);
371                                 if (!r)
372                                         {
373                                         fprintf(stderr,
374                                                 "Error generating DRBG bits\n");
375                                         exit(1);
376                                         }
377                                 OPENSSL_free(adin);
378                                 OPENSSL_free(ent);
379                                 adin = ent = NULL;
380                                 gen++;
381                                 }
382                         }
383                 if (!strcmp(keyword, "EntropyInputReseed"))
384                         {
385                         ent = hex2bin_m(value, &entlen);
386                         t.ent = ent;
387                         t.entlen = entlen;
388                         }
389                 if (!strcmp(keyword, "AdditionalInputReseed"))
390                         {
391                         adin = hex2bin_m(value, &adinlen);
392                         FIPS_drbg_reseed(dctx, adin, adinlen);
393                         OPENSSL_free(ent);
394                         OPENSSL_free(adin);
395                         ent = adin = NULL;
396                         }
397                 if (gen == 2)
398                         {
399                         OutputValue("ReturnedBits", randout, randoutlen,
400                                                                         out, 0);
401                         FIPS_drbg_free(dctx);
402                         dctx = NULL;
403                         gen = 0;
404                         }
405
406                 }
407         return 0;
408         }
409
410 #endif