Allow setting of get_entropy and get_nonce callbacks outside test mode.
[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_aes(char *str, int *pdf)
104         {
105
106         if (!strncmp(str + 9, "no", 2))
107                 *pdf = 0;
108         else
109                 *pdf = DRBG_FLAG_CTR_USE_DF;
110
111         switch(atoi(str + 5))
112                 {
113                 case 128:
114                 return NID_aes_128_ctr;
115
116                 case 192:
117                 return NID_aes_192_ctr;
118
119                 case 256:
120                 return NID_aes_256_ctr;
121
122                 default:
123                 return NID_undef;
124
125                 }
126
127         return NID_undef;
128         }
129
130 typedef struct 
131         {
132         unsigned char *ent;
133         size_t entlen;
134         unsigned char *nonce;
135         size_t noncelen;
136         } TEST_ENT;
137
138 static size_t test_entropy(DRBG_CTX *dctx, unsigned char *out,
139                                 int entropy, size_t min_len, size_t max_len)
140         {
141         TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
142         memcpy(out, t->ent, t->entlen);
143         return t->entlen;
144         }
145
146 static size_t test_nonce(DRBG_CTX *dctx, unsigned char *out,
147                                 int entropy, size_t min_len, size_t max_len)
148         {
149         TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
150         memcpy(out, t->nonce, t->noncelen);
151         return t->noncelen;
152         }
153
154
155
156 int main(int argc,char **argv)
157         {
158         FILE *in, *out;
159         DRBG_CTX *dctx = NULL;
160         TEST_ENT t;
161         int r, nid = 0;
162         int pr = 0;
163         char buf[2048], lbuf[2048];
164         unsigned char randout[2048];
165         char *keyword = NULL, *value = NULL;
166
167         unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL;
168         long entlen, noncelen, perslen, adinlen;
169         int df = 0;
170
171         int randoutlen = 0;
172
173         int gen = 0;
174
175         fips_set_error_print();
176
177         if (argc == 3)
178                 {
179                 in = fopen(argv[1], "r");
180                 if (!in)
181                         {
182                         fprintf(stderr, "Error opening input file\n");
183                         exit(1);
184                         }
185                 out = fopen(argv[2], "w");
186                 if (!out)
187                         {
188                         fprintf(stderr, "Error opening output file\n");
189                         exit(1);
190                         }
191                 }
192         else if (argc == 1)
193                 {
194                 in = stdin;
195                 out = stdout;
196                 }
197         else
198                 {
199                 fprintf(stderr,"%s (infile outfile)\n",argv[0]);
200                 exit(1);
201                 }
202
203         while (fgets(buf, sizeof(buf), in) != NULL)
204                 {
205                 fputs(buf, out);
206                 if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5))
207                         {
208                         nid = parse_md(buf);
209                         if (nid == NID_undef)
210                                 exit(1);
211                         }
212                 if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5))
213                         {
214                         nid = parse_aes(buf, &df);
215                         if (nid == NID_undef)
216                                 exit(1);
217                         }
218                 if (!parse_line(&keyword, &value, lbuf, buf))
219                         continue;
220
221                 if (!strcmp(keyword, "[PredictionResistance"))
222                         {
223                         if (!strcmp(value, "True]"))
224                                 pr = 1;
225                         else if (!strcmp(value, "False]"))
226                                 pr = 0;
227                         else
228                                 exit(1);
229                         }
230
231                 if (!strcmp(keyword, "EntropyInput"))
232                         {
233                         ent = hex2bin_m(value, &entlen);
234                         t.ent = ent;
235                         t.entlen = entlen;
236                         }
237
238                 if (!strcmp(keyword, "Nonce"))
239                         {
240                         nonce = hex2bin_m(value, &noncelen);
241                         t.nonce = nonce;
242                         t.noncelen = noncelen;
243                         }
244
245                 if (!strcmp(keyword, "PersonalizationString"))
246                         {
247                         pers = hex2bin_m(value, &perslen);
248                         dctx = FIPS_drbg_new(nid, df | DRBG_FLAG_TEST);
249                         if (!dctx)
250                                 exit (1);
251                         FIPS_drbg_set_callbacks(dctx, test_entropy, test_nonce);
252                         FIPS_drbg_set_app_data(dctx, &t);
253                         randoutlen = (int)FIPS_drbg_get_blocklength(dctx);
254                         r = FIPS_drbg_instantiate(dctx, pers, perslen);
255                         if (!r)
256                                 {
257                                 fprintf(stderr, "Error instantiating DRBG\n");
258                                 exit(1);
259                                 }
260                         OPENSSL_free(pers);
261                         OPENSSL_free(ent);
262                         OPENSSL_free(nonce);
263                         ent = nonce = pers = NULL;
264                         gen = 0;
265                         }
266
267                 if (!strcmp(keyword, "AdditionalInput"))
268                         {
269                         adin = hex2bin_m(value, &adinlen);
270                         if (pr)
271                                 continue;
272                         r = FIPS_drbg_generate(dctx, randout, randoutlen, 0, 0,
273                                                                 adin, adinlen);
274                         if (!r)
275                                 {
276                                 fprintf(stderr, "Error generating DRBG bits\n");
277                                 exit(1);
278                                 }
279                         if (!r)
280                                 exit(1);
281                         OPENSSL_free(adin);
282                         adin = NULL;
283                         gen++;
284                         }
285
286                 if (pr)
287                         {
288                         if (!strcmp(keyword, "EntropyInputPR"))
289                                 {
290                                 ent = hex2bin_m(value, &entlen);
291                                 t.ent = ent;
292                                 t.entlen = entlen;
293                                 r = FIPS_drbg_generate(dctx,
294                                                         randout, randoutlen,
295                                                         0, 1, adin, adinlen);
296                                 if (!r)
297                                         {
298                                         fprintf(stderr,
299                                                 "Error generating DRBG bits\n");
300                                         exit(1);
301                                         }
302                                 OPENSSL_free(adin);
303                                 OPENSSL_free(ent);
304                                 adin = ent = NULL;
305                                 gen++;
306                                 }
307                         }
308                 if (!strcmp(keyword, "EntropyInputReseed"))
309                         {
310                         ent = hex2bin_m(value, &entlen);
311                         t.ent = ent;
312                         t.entlen = entlen;
313                         }
314                 if (!strcmp(keyword, "AdditionalInputReseed"))
315                         {
316                         adin = hex2bin_m(value, &adinlen);
317                         FIPS_drbg_reseed(dctx, adin, adinlen);
318                         OPENSSL_free(ent);
319                         OPENSSL_free(adin);
320                         ent = adin = NULL;
321                         }
322                 if (gen == 2)
323                         {
324                         OutputValue("ReturnedBits", randout, randoutlen,
325                                                                         out, 0);
326                         FIPS_drbg_free(dctx);
327                         dctx = NULL;
328                         gen = 0;
329                         }
330
331                 }
332         return 0;
333         }
334
335 #endif