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;
134 unsigned char *nonce;
138 static size_t test_entropy(DRBG_CTX *dctx, unsigned char **pout,
139 int entropy, size_t min_len, size_t max_len)
141 TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
142 *pout = (unsigned char *)t->ent;
146 static size_t test_nonce(DRBG_CTX *dctx, unsigned char **pout,
147 int entropy, size_t min_len, size_t max_len)
149 TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
150 *pout = (unsigned char *)t->nonce;
156 int main(int argc,char **argv)
159 DRBG_CTX *dctx = NULL;
163 char buf[2048], lbuf[2048];
164 unsigned char randout[2048];
165 char *keyword = NULL, *value = NULL;
167 unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL;
168 long entlen, noncelen, perslen, adinlen;
179 in = fopen(argv[1], "r");
182 fprintf(stderr, "Error opening input file\n");
185 out = fopen(argv[2], "w");
188 fprintf(stderr, "Error opening output file\n");
199 fprintf(stderr,"%s (infile outfile)\n",argv[0]);
203 while (fgets(buf, sizeof(buf), in) != NULL)
206 if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5))
209 if (nid == NID_undef)
212 if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5))
214 nid = parse_aes(buf, &df);
215 if (nid == NID_undef)
218 if (!parse_line(&keyword, &value, lbuf, buf))
221 if (!strcmp(keyword, "[PredictionResistance"))
223 if (!strcmp(value, "True]"))
225 else if (!strcmp(value, "False]"))
231 if (!strcmp(keyword, "EntropyInput"))
233 ent = hex2bin_m(value, &entlen);
238 if (!strcmp(keyword, "Nonce"))
240 nonce = hex2bin_m(value, &noncelen);
242 t.noncelen = noncelen;
245 if (!strcmp(keyword, "PersonalizationString"))
247 pers = hex2bin_m(value, &perslen);
248 dctx = FIPS_drbg_new(nid, df | DRBG_FLAG_TEST);
251 FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 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);
258 fprintf(stderr, "Error instantiating DRBG\n");
264 ent = nonce = pers = NULL;
268 if (!strcmp(keyword, "AdditionalInput"))
270 adin = hex2bin_m(value, &adinlen);
273 r = FIPS_drbg_generate(dctx, randout, randoutlen, 0, 0,
277 fprintf(stderr, "Error generating DRBG bits\n");
289 if (!strcmp(keyword, "EntropyInputPR"))
291 ent = hex2bin_m(value, &entlen);
294 r = FIPS_drbg_generate(dctx,
296 0, 1, adin, adinlen);
300 "Error generating DRBG bits\n");
309 if (!strcmp(keyword, "EntropyInputReseed"))
311 ent = hex2bin_m(value, &entlen);
315 if (!strcmp(keyword, "AdditionalInputReseed"))
317 adin = hex2bin_m(value, &adinlen);
318 FIPS_drbg_reseed(dctx, adin, adinlen);
325 OutputValue("ReturnedBits", randout, randoutlen,
327 FIPS_drbg_free(dctx);