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