Updates from fips2 branch: close streams in test utilities, use cofactor ECDH
[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 dparse_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 = dparse_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 #ifdef FIPS_ALGVS
174 int fips_drbgvs_main(int argc,char **argv)
175 #else
176 int main(int argc,char **argv)
177 #endif
178         {
179         FILE *in = NULL, *out = NULL;
180         DRBG_CTX *dctx = NULL;
181         TEST_ENT t;
182         int r, nid = 0;
183         int pr = 0;
184         char buf[2048], lbuf[2048];
185         unsigned char randout[2048];
186         char *keyword = NULL, *value = NULL;
187
188         unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL;
189         long entlen, noncelen, perslen, adinlen;
190         int df = 0;
191
192         enum dtype { DRBG_NONE, DRBG_CTR, DRBG_HASH, DRBG_HMAC, DRBG_DUAL_EC }
193                 drbg_type = DRBG_NONE;
194
195         int randoutlen = 0;
196
197         int gen = 0;
198
199         fips_algtest_init();
200
201         if (argc == 3)
202                 {
203                 in = fopen(argv[1], "r");
204                 if (!in)
205                         {
206                         fprintf(stderr, "Error opening input file\n");
207                         exit(1);
208                         }
209                 out = fopen(argv[2], "w");
210                 if (!out)
211                         {
212                         fprintf(stderr, "Error opening output file\n");
213                         exit(1);
214                         }
215                 }
216         else if (argc == 1)
217                 {
218                 in = stdin;
219                 out = stdout;
220                 }
221         else
222                 {
223                 fprintf(stderr,"%s (infile outfile)\n",argv[0]);
224                 exit(1);
225                 }
226
227         while (fgets(buf, sizeof(buf), in) != NULL)
228                 {
229                 fputs(buf, out);
230                 if (drbg_type == DRBG_NONE)
231                         {
232                         if (strstr(buf, "CTR_DRBG"))
233                                 drbg_type = DRBG_CTR;
234                         else if (strstr(buf, "Hash_DRBG"))
235                                 drbg_type = DRBG_HASH;
236                         else if (strstr(buf, "HMAC_DRBG"))
237                                 drbg_type = DRBG_HMAC;
238                         else if (strstr(buf, "Dual_EC_DRBG"))
239                                 drbg_type = DRBG_DUAL_EC;
240                         else
241                                 continue;
242                         }
243                 if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5))
244                         {
245                         nid = dparse_md(buf);
246                         if (nid == NID_undef)
247                                 exit(1);
248                         if (drbg_type == DRBG_HMAC)
249                                 {
250                                 switch (nid)
251                                         {
252                                         case NID_sha1:
253                                         nid = NID_hmacWithSHA1;
254                                         break;
255
256                                         case NID_sha224:
257                                         nid = NID_hmacWithSHA224;
258                                         break;
259
260                                         case NID_sha256:
261                                         nid = NID_hmacWithSHA256;
262                                         break;
263
264                                         case NID_sha384:
265                                         nid = NID_hmacWithSHA384;
266                                         break;
267
268                                         case NID_sha512:
269                                         nid = NID_hmacWithSHA512;
270                                         break;
271
272                                         default:
273                                         exit(1);
274                                         }
275                                 }
276                         }
277                 if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5))
278                         {
279                         nid = parse_aes(buf, &df);
280                         if (nid == NID_undef)
281                                 exit(1);
282                         }
283                 if (strlen(buf) > 12 && !strncmp(buf, "[P-", 3))
284                         {
285                         nid = parse_ec(buf);
286                         if (nid == NID_undef)
287                                 exit(1);
288                         }
289                 if (!parse_line(&keyword, &value, lbuf, buf))
290                         continue;
291
292                 if (!strcmp(keyword, "[PredictionResistance"))
293                         {
294                         if (!strcmp(value, "True]"))
295                                 pr = 1;
296                         else if (!strcmp(value, "False]"))
297                                 pr = 0;
298                         else
299                                 exit(1);
300                         }
301
302                 if (!strcmp(keyword, "EntropyInput"))
303                         {
304                         ent = hex2bin_m(value, &entlen);
305                         t.ent = ent;
306                         t.entlen = entlen;
307                         }
308
309                 if (!strcmp(keyword, "Nonce"))
310                         {
311                         nonce = hex2bin_m(value, &noncelen);
312                         t.nonce = nonce;
313                         t.noncelen = noncelen;
314                         }
315
316                 if (!strcmp(keyword, "PersonalizationString"))
317                         {
318                         pers = hex2bin_m(value, &perslen);
319                         if (nid == 0)
320                                 {
321                                 fprintf(stderr, "DRBG type not recognised!\n");
322                                 exit (1);
323                                 }
324                         dctx = FIPS_drbg_new(nid, df | DRBG_FLAG_TEST);
325                         if (!dctx)
326                                 exit (1);
327                         FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0,
328                                                         test_nonce, 0);
329                         FIPS_drbg_set_app_data(dctx, &t);
330                         randoutlen = (int)FIPS_drbg_get_blocklength(dctx);
331                         r = FIPS_drbg_instantiate(dctx, pers, perslen);
332                         if (!r)
333                                 {
334                                 fprintf(stderr, "Error instantiating DRBG\n");
335                                 exit(1);
336                                 }
337                         OPENSSL_free(pers);
338                         OPENSSL_free(ent);
339                         OPENSSL_free(nonce);
340                         ent = nonce = pers = NULL;
341                         gen = 0;
342                         }
343
344                 if (!strcmp(keyword, "AdditionalInput"))
345                         {
346                         adin = hex2bin_m(value, &adinlen);
347                         if (pr)
348                                 continue;
349                         r = FIPS_drbg_generate(dctx, randout, randoutlen, 0,
350                                                                 adin, adinlen);
351                         if (!r)
352                                 {
353                                 fprintf(stderr, "Error generating DRBG bits\n");
354                                 exit(1);
355                                 }
356                         if (!r)
357                                 exit(1);
358                         OPENSSL_free(adin);
359                         adin = NULL;
360                         gen++;
361                         }
362
363                 if (pr)
364                         {
365                         if (!strcmp(keyword, "EntropyInputPR"))
366                                 {
367                                 ent = hex2bin_m(value, &entlen);
368                                 t.ent = ent;
369                                 t.entlen = entlen;
370                                 r = FIPS_drbg_generate(dctx,
371                                                         randout, randoutlen,
372                                                         1, adin, adinlen);
373                                 if (!r)
374                                         {
375                                         fprintf(stderr,
376                                                 "Error generating DRBG bits\n");
377                                         exit(1);
378                                         }
379                                 OPENSSL_free(adin);
380                                 OPENSSL_free(ent);
381                                 adin = ent = NULL;
382                                 gen++;
383                                 }
384                         }
385                 if (!strcmp(keyword, "EntropyInputReseed"))
386                         {
387                         ent = hex2bin_m(value, &entlen);
388                         t.ent = ent;
389                         t.entlen = entlen;
390                         }
391                 if (!strcmp(keyword, "AdditionalInputReseed"))
392                         {
393                         adin = hex2bin_m(value, &adinlen);
394                         FIPS_drbg_reseed(dctx, adin, adinlen);
395                         OPENSSL_free(ent);
396                         OPENSSL_free(adin);
397                         ent = adin = NULL;
398                         }
399                 if (gen == 2)
400                         {
401                         OutputValue("ReturnedBits", randout, randoutlen,
402                                                                         out, 0);
403                         FIPS_drbg_free(dctx);
404                         dctx = NULL;
405                         gen = 0;
406                         }
407
408                 }
409         if (in && in != stdin)
410                 fclose(in);
411         if (out && out != stdout)
412                 fclose(out);
413         return 0;
414         }
415
416 #endif