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 dparse_md(char *str)
103 static int parse_ec(char *str)
105 int curve_nid, md_nid;
107 md = strchr(str, ' ');
110 if (!strncmp(str, "[P-256", 6))
111 curve_nid = NID_X9_62_prime256v1;
112 else if (!strncmp(str, "[P-384", 6))
113 curve_nid = NID_secp384r1;
114 else if (!strncmp(str, "[P-521", 6))
115 curve_nid = NID_secp521r1;
118 md_nid = dparse_md(md);
119 if (md_nid == NID_undef)
121 return (curve_nid << 16) | md_nid;
124 static int parse_aes(char *str, int *pdf)
127 if (!strncmp(str + 9, "no", 2))
130 *pdf = DRBG_FLAG_CTR_USE_DF;
132 switch(atoi(str + 5))
135 return NID_aes_128_ctr;
138 return NID_aes_192_ctr;
141 return NID_aes_256_ctr;
153 unsigned char *nonce;
157 static size_t test_entropy(DRBG_CTX *dctx, unsigned char **pout,
158 int entropy, size_t min_len, size_t max_len)
160 TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
161 *pout = (unsigned char *)t->ent;
165 static size_t test_nonce(DRBG_CTX *dctx, unsigned char **pout,
166 int entropy, size_t min_len, size_t max_len)
168 TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
169 *pout = (unsigned char *)t->nonce;
174 int fips_drbgvs_main(int argc,char **argv)
176 int main(int argc,char **argv)
180 DRBG_CTX *dctx = NULL;
184 char buf[2048], lbuf[2048];
185 unsigned char randout[2048];
186 char *keyword = NULL, *value = NULL;
188 unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL;
189 long entlen, noncelen, perslen, adinlen;
192 enum dtype { DRBG_NONE, DRBG_CTR, DRBG_HASH, DRBG_HMAC, DRBG_DUAL_EC }
193 drbg_type = DRBG_NONE;
203 in = fopen(argv[1], "r");
206 fprintf(stderr, "Error opening input file\n");
209 out = fopen(argv[2], "w");
212 fprintf(stderr, "Error opening output file\n");
223 fprintf(stderr,"%s (infile outfile)\n",argv[0]);
227 while (fgets(buf, sizeof(buf), in) != NULL)
230 if (drbg_type == DRBG_NONE)
232 if (strstr(buf, "CTR_DRBG"))
233 drbg_type = DRBG_CTR;
234 else if (strstr(buf, "Hash_DRBG"))
235 drbg_type = DRBG_HASH;
236 else if (strstr(buf, "HMAC_DRBG"))
237 drbg_type = DRBG_HMAC;
238 else if (strstr(buf, "Dual_EC_DRBG"))
239 drbg_type = DRBG_DUAL_EC;
243 if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5))
245 nid = dparse_md(buf);
246 if (nid == NID_undef)
248 if (drbg_type == DRBG_HMAC)
253 nid = NID_hmacWithSHA1;
257 nid = NID_hmacWithSHA224;
261 nid = NID_hmacWithSHA256;
265 nid = NID_hmacWithSHA384;
269 nid = NID_hmacWithSHA512;
277 if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5))
279 nid = parse_aes(buf, &df);
280 if (nid == NID_undef)
283 if (strlen(buf) > 12 && !strncmp(buf, "[P-", 3))
286 if (nid == NID_undef)
289 if (!parse_line(&keyword, &value, lbuf, buf))
292 if (!strcmp(keyword, "[PredictionResistance"))
294 if (!strcmp(value, "True]"))
296 else if (!strcmp(value, "False]"))
302 if (!strcmp(keyword, "EntropyInput"))
304 ent = hex2bin_m(value, &entlen);
309 if (!strcmp(keyword, "Nonce"))
311 nonce = hex2bin_m(value, &noncelen);
313 t.noncelen = noncelen;
316 if (!strcmp(keyword, "PersonalizationString"))
318 pers = hex2bin_m(value, &perslen);
321 fprintf(stderr, "DRBG type not recognised!\n");
324 dctx = FIPS_drbg_new(nid, df | DRBG_FLAG_TEST);
327 FIPS_drbg_set_callbacks(dctx, test_entropy, 0, 0,
329 FIPS_drbg_set_app_data(dctx, &t);
330 randoutlen = (int)FIPS_drbg_get_blocklength(dctx);
331 r = FIPS_drbg_instantiate(dctx, pers, perslen);
334 fprintf(stderr, "Error instantiating DRBG\n");
340 ent = nonce = pers = NULL;
344 if (!strcmp(keyword, "AdditionalInput"))
346 adin = hex2bin_m(value, &adinlen);
349 r = FIPS_drbg_generate(dctx, randout, randoutlen, 0,
353 fprintf(stderr, "Error generating DRBG bits\n");
365 if (!strcmp(keyword, "EntropyInputPR"))
367 ent = hex2bin_m(value, &entlen);
370 r = FIPS_drbg_generate(dctx,
376 "Error generating DRBG bits\n");
385 if (!strcmp(keyword, "EntropyInputReseed"))
387 ent = hex2bin_m(value, &entlen);
391 if (!strcmp(keyword, "AdditionalInputReseed"))
393 adin = hex2bin_m(value, &adinlen);
394 FIPS_drbg_reseed(dctx, adin, adinlen);
401 OutputValue("ReturnedBits", randout, randoutlen,
403 FIPS_drbg_free(dctx);