a5992339080e96e5fd1d31d3121b00ed996460c7
[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 GCM 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         DRBG_CTX *dctx;
159         TEST_ENT t;
160         int r, nid;
161         int pr = 0;
162         char buf[2048], lbuf[2048];
163         unsigned char out[2048];
164         char *keyword = NULL, *value = NULL;
165
166         unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL;
167         long entlen, noncelen, perslen, adinlen;
168         int df;
169
170         int outlen = 0;
171
172         int gen = 0;
173
174         fips_set_error_print();
175         
176         while (fgets(buf, sizeof(buf), stdin) != NULL)
177                 {
178                 fputs(buf, stdout);
179                 if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5))
180                         {
181                         nid = parse_md(buf);
182                         if (nid == NID_undef)
183                                 exit(1);
184                         }
185                 if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5))
186                         {
187                         nid = parse_aes(buf, &df);
188                         if (nid == NID_undef)
189                                 exit(1);
190                         }
191                 if (!parse_line(&keyword, &value, lbuf, buf))
192                         continue;
193
194                 if (!strcmp(keyword, "[PredictionResistance"))
195                         {
196                         if (!strcmp(value, "True]"))
197                                 pr = 1;
198                         else if (!strcmp(value, "False]"))
199                                 pr = 0;
200                         else
201                                 exit(1);
202                         }
203
204                 if (!strcmp(keyword, "EntropyInput"))
205                         {
206                         ent = hex2bin_m(value, &entlen);
207                         t.ent = ent;
208                         t.entlen = entlen;
209                         }
210
211                 if (!strcmp(keyword, "Nonce"))
212                         {
213                         nonce = hex2bin_m(value, &noncelen);
214                         t.nonce = nonce;
215                         t.noncelen = noncelen;
216                         }
217
218                 if (!strcmp(keyword, "PersonalizationString"))
219                         {
220                         pers = hex2bin_m(value, &perslen);
221                         dctx = FIPS_drbg_new(nid, df);
222                         if (!dctx)
223                                 exit (1);
224                         FIPS_drbg_set_test_mode(dctx, test_entropy, test_nonce);
225                         FIPS_drbg_set_app_data(dctx, &t);
226                         outlen = (int)FIPS_drbg_get_blocklength(dctx);
227                         r = FIPS_drbg_instantiate(dctx, 0, pers, perslen);
228                         if (!r)
229                                 {
230                                 fprintf(stderr, "Error instantiating DRBG\n");
231                                 exit(1);
232                                 }
233                         OPENSSL_free(pers);
234                         OPENSSL_free(ent);
235                         OPENSSL_free(nonce);
236                         ent = nonce = pers = NULL;
237                         gen = 0;
238                         }
239
240                 if (!strcmp(keyword, "AdditionalInput"))
241                         {
242                         adin = hex2bin_m(value, &adinlen);
243                         if (pr)
244                                 continue;
245                         r = FIPS_drbg_generate(dctx, out, outlen, 0,
246                                                                 adin, adinlen);
247                         if (!r)
248                                 {
249                                 fprintf(stderr, "Error generating DRBG bits\n");
250                                 exit(1);
251                                 }
252                         if (!r)
253                                 exit(1);
254                         OPENSSL_free(adin);
255                         adin = NULL;
256                         gen++;
257                         }
258
259                 if (pr)
260                         {
261                         if (!strcmp(keyword, "EntropyInputPR"))
262                                 {
263                                 ent = hex2bin_m(value, &entlen);
264                                 t.ent = ent;
265                                 t.entlen = entlen;
266                                 r = FIPS_drbg_generate(dctx, out, outlen, 1,
267                                                         adin, adinlen);
268                                 if (!r)
269                                         {
270                                         fprintf(stderr,
271                                                 "Error generating DRBG bits\n");
272                                         exit(1);
273                                         }
274                                 OPENSSL_free(adin);
275                                 OPENSSL_free(ent);
276                                 adin = ent = NULL;
277                                 gen++;
278                                 }
279                         }
280                 if (!strcmp(keyword, "EntropyInputReseed"))
281                         {
282                         ent = hex2bin_m(value, &entlen);
283                         t.ent = ent;
284                         t.entlen = entlen;
285                         }
286                 if (!strcmp(keyword, "AdditionalInputReseed"))
287                         {
288                         adin = hex2bin_m(value, &adinlen);
289                         FIPS_drbg_reseed(dctx, adin, adinlen);
290                         OPENSSL_free(ent);
291                         OPENSSL_free(adin);
292                         ent = adin = NULL;
293                         }
294                 if (gen == 2)
295                         {
296                         OutputValue("ReturnedBits", out, outlen, stdout, 0);
297                         gen = 0;
298                         }
299
300                 }
301         return 0;
302         }
303
304 #endif