1 /* fips/rand/fips_drbgvs.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
55 #define OPENSSL_FIPSAPI
56 #include <openssl/opensslconf.h>
61 int main(int argc, char **argv)
63 printf("No FIPS DRBG support\n");
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>
79 static int parse_md(char *str)
103 static int parse_aes(char *str, int *pdf)
106 if (!strncmp(str + 9, "no", 2))
109 *pdf = DRBG_FLAG_CTR_USE_DF;
111 switch(atoi(str + 5))
114 return NID_aes_128_ctr;
117 return NID_aes_192_ctr;
120 return NID_aes_256_ctr;
132 unsigned char *nonce;
136 static size_t test_entropy(DRBG_CTX *dctx, unsigned char **pout,
137 int entropy, size_t min_len, size_t max_len)
139 TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
140 *pout = (unsigned char *)t->ent;
144 static size_t test_nonce(DRBG_CTX *dctx, unsigned char **pout,
145 int entropy, size_t min_len, size_t max_len)
147 TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
148 *pout = (unsigned char *)t->nonce;
154 int main(int argc,char **argv)
157 DRBG_CTX *dctx = NULL;
161 char buf[2048], lbuf[2048];
162 unsigned char randout[2048];
163 char *keyword = NULL, *value = NULL;
165 unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL;
166 long entlen, noncelen, perslen, adinlen;
169 enum dtype { DRBG_NONE, DRBG_CTR, DRBG_HASH, DRBG_HMAC, DRBG_DUAL_EC }
170 drbg_type = DRBG_NONE;
180 in = fopen(argv[1], "r");
183 fprintf(stderr, "Error opening input file\n");
186 out = fopen(argv[2], "w");
189 fprintf(stderr, "Error opening output file\n");
200 fprintf(stderr,"%s (infile outfile)\n",argv[0]);
204 while (fgets(buf, sizeof(buf), in) != NULL)
207 if (drbg_type == DRBG_NONE)
209 if (strstr(buf, "CTR_DRBG"))
210 drbg_type = DRBG_CTR;
211 else if (strstr(buf, "Hash_DRBG"))
212 drbg_type = DRBG_HASH;
213 else if (strstr(buf, "HMAC_DRBG"))
214 drbg_type = DRBG_HMAC;
215 else if (strstr(buf, "Dual_EC_DRBG"))
216 drbg_type = DRBG_DUAL_EC;
220 if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5))
223 if (nid == NID_undef)
225 if (drbg_type == DRBG_HMAC)
230 nid = NID_hmacWithSHA1;
234 nid = NID_hmacWithSHA224;
238 nid = NID_hmacWithSHA256;
242 nid = NID_hmacWithSHA384;
246 nid = NID_hmacWithSHA512;
254 if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5))
256 nid = parse_aes(buf, &df);
257 if (nid == NID_undef)
260 if (!parse_line(&keyword, &value, lbuf, buf))
263 if (!strcmp(keyword, "[PredictionResistance"))
265 if (!strcmp(value, "True]"))
267 else if (!strcmp(value, "False]"))
273 if (!strcmp(keyword, "EntropyInput"))
275 ent = hex2bin_m(value, &entlen);
280 if (!strcmp(keyword, "Nonce"))
282 nonce = hex2bin_m(value, &noncelen);
284 t.noncelen = noncelen;
287 if (!strcmp(keyword, "PersonalizationString"))
289 pers = hex2bin_m(value, &perslen);
290 dctx = FIPS_drbg_new(nid, df | DRBG_FLAG_TEST);
293 FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0,
295 FIPS_drbg_set_app_data(dctx, &t);
296 randoutlen = (int)FIPS_drbg_get_blocklength(dctx);
297 r = FIPS_drbg_instantiate(dctx, pers, perslen);
300 fprintf(stderr, "Error instantiating DRBG\n");
306 ent = nonce = pers = NULL;
310 if (!strcmp(keyword, "AdditionalInput"))
312 adin = hex2bin_m(value, &adinlen);
315 r = FIPS_drbg_generate(dctx, randout, randoutlen, 0, 0,
319 fprintf(stderr, "Error generating DRBG bits\n");
331 if (!strcmp(keyword, "EntropyInputPR"))
333 ent = hex2bin_m(value, &entlen);
336 r = FIPS_drbg_generate(dctx,
338 0, 1, adin, adinlen);
342 "Error generating DRBG bits\n");
351 if (!strcmp(keyword, "EntropyInputReseed"))
353 ent = hex2bin_m(value, &entlen);
357 if (!strcmp(keyword, "AdditionalInputReseed"))
359 adin = hex2bin_m(value, &adinlen);
360 FIPS_drbg_reseed(dctx, adin, adinlen);
367 OutputValue("ReturnedBits", randout, randoutlen,
369 FIPS_drbg_free(dctx);