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 GCM 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 *out,
139 int entropy, size_t min_len, size_t max_len)
141 TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
142 memcpy(out, t->ent, t->entlen);
146 static size_t test_nonce(DRBG_CTX *dctx, unsigned char *out,
147 int entropy, size_t min_len, size_t max_len)
149 TEST_ENT *t = FIPS_drbg_get_app_data(dctx);
150 memcpy(out, t->nonce, t->noncelen);
156 int main(int argc,char **argv)
162 char buf[2048], lbuf[2048];
163 unsigned char out[2048];
164 char *keyword = NULL, *value = NULL;
166 unsigned char *ent = NULL, *nonce = NULL, *pers = NULL, *adin = NULL;
167 long entlen, noncelen, perslen, adinlen;
174 fips_set_error_print();
176 while (fgets(buf, sizeof(buf), stdin) != NULL)
179 if (strlen(buf) > 4 && !strncmp(buf, "[SHA-", 5))
182 if (nid == NID_undef)
185 if (strlen(buf) > 12 && !strncmp(buf, "[AES-", 5))
187 nid = parse_aes(buf, &df);
188 if (nid == NID_undef)
191 if (!parse_line(&keyword, &value, lbuf, buf))
194 if (!strcmp(keyword, "[PredictionResistance"))
196 if (!strcmp(value, "True]"))
198 else if (!strcmp(value, "False]"))
204 if (!strcmp(keyword, "EntropyInput"))
206 ent = hex2bin_m(value, &entlen);
211 if (!strcmp(keyword, "Nonce"))
213 nonce = hex2bin_m(value, &noncelen);
215 t.noncelen = noncelen;
218 if (!strcmp(keyword, "PersonalizationString"))
220 pers = hex2bin_m(value, &perslen);
221 dctx = FIPS_drbg_new(nid, df);
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);
230 fprintf(stderr, "Error instantiating DRBG\n");
236 ent = nonce = pers = NULL;
240 if (!strcmp(keyword, "AdditionalInput"))
242 adin = hex2bin_m(value, &adinlen);
245 r = FIPS_drbg_generate(dctx, out, outlen, 0,
249 fprintf(stderr, "Error generating DRBG bits\n");
261 if (!strcmp(keyword, "EntropyInputPR"))
263 ent = hex2bin_m(value, &entlen);
266 r = FIPS_drbg_generate(dctx, out, outlen, 1,
271 "Error generating DRBG bits\n");
280 if (!strcmp(keyword, "EntropyInputReseed"))
282 ent = hex2bin_m(value, &entlen);
286 if (!strcmp(keyword, "AdditionalInputReseed"))
288 adin = hex2bin_m(value, &adinlen);
289 FIPS_drbg_reseed(dctx, adin, adinlen);
296 OutputValue("ReturnedBits", out, outlen, stdout, 0);