70a1168224e83259342e16b9562cbf4a5a8d6942
[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 **pout,
139                                 int entropy, size_t min_len, size_t max_len)
140         {
141         TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
142         *pout = (unsigned char *)t->ent;
143         return t->entlen;
144         }
145
146 static size_t test_nonce(DRBG_CTX *dctx, unsigned char **pout,
147                                 int entropy, size_t min_len, size_t max_len)
148         {
149         TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
150         *pout = (unsigned char *)t->nonce;
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_algtest_init();
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, 0, 0,
252                                                         test_nonce, 0);
253                         FIPS_drbg_set_app_data(dctx, &t);
254                         randoutlen = (int)FIPS_drbg_get_blocklength(dctx);
255                         r = FIPS_drbg_instantiate(dctx, pers, perslen);
256                         if (!r)
257                                 {
258                                 fprintf(stderr, "Error instantiating DRBG\n");
259                                 exit(1);
260                                 }
261                         OPENSSL_free(pers);
262                         OPENSSL_free(ent);
263                         OPENSSL_free(nonce);
264                         ent = nonce = pers = NULL;
265                         gen = 0;
266                         }
267
268                 if (!strcmp(keyword, "AdditionalInput"))
269                         {
270                         adin = hex2bin_m(value, &adinlen);
271                         if (pr)
272                                 continue;
273                         r = FIPS_drbg_generate(dctx, randout, randoutlen, 0, 0,
274                                                                 adin, adinlen);
275                         if (!r)
276                                 {
277                                 fprintf(stderr, "Error generating DRBG bits\n");
278                                 exit(1);
279                                 }
280                         if (!r)
281                                 exit(1);
282                         OPENSSL_free(adin);
283                         adin = NULL;
284                         gen++;
285                         }
286
287                 if (pr)
288                         {
289                         if (!strcmp(keyword, "EntropyInputPR"))
290                                 {
291                                 ent = hex2bin_m(value, &entlen);
292                                 t.ent = ent;
293                                 t.entlen = entlen;
294                                 r = FIPS_drbg_generate(dctx,
295                                                         randout, randoutlen,
296                                                         0, 1, adin, adinlen);
297                                 if (!r)
298                                         {
299                                         fprintf(stderr,
300                                                 "Error generating DRBG bits\n");
301                                         exit(1);
302                                         }
303                                 OPENSSL_free(adin);
304                                 OPENSSL_free(ent);
305                                 adin = ent = NULL;
306                                 gen++;
307                                 }
308                         }
309                 if (!strcmp(keyword, "EntropyInputReseed"))
310                         {
311                         ent = hex2bin_m(value, &entlen);
312                         t.ent = ent;
313                         t.entlen = entlen;
314                         }
315                 if (!strcmp(keyword, "AdditionalInputReseed"))
316                         {
317                         adin = hex2bin_m(value, &adinlen);
318                         FIPS_drbg_reseed(dctx, adin, adinlen);
319                         OPENSSL_free(ent);
320                         OPENSSL_free(adin);
321                         ent = adin = NULL;
322                         }
323                 if (gen == 2)
324                         {
325                         OutputValue("ReturnedBits", randout, randoutlen,
326                                                                         out, 0);
327                         FIPS_drbg_free(dctx);
328                         dctx = NULL;
329                         gen = 0;
330                         }
331
332                 }
333         return 0;
334         }
335
336 #endif