1 /* fips/rand/fips_drbg_lib.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 * ====================================================================
54 #define OPENSSL_FIPSAPI
57 #include <openssl/crypto.h>
58 #include <openssl/evp.h>
59 #include <openssl/aes.h>
60 #include <openssl/err.h>
61 #include <openssl/fips_rand.h>
62 #include "fips_rand_lcl.h"
64 /* Support framework for SP800-90 DRBGs */
66 int FIPS_drbg_init(DRBG_CTX *dctx, int type, unsigned int flags)
69 memset(dctx, 0, sizeof(DRBG_CTX));
70 dctx->status = DRBG_STATUS_UNINITIALISED;
74 dctx->entropy_blocklen = 0;
75 dctx->health_check_cnt = 0;
76 dctx->health_check_interval = DRBG_HEALTH_INTERVAL;
78 rv = fips_drbg_hash_init(dctx);
81 rv = fips_drbg_ctr_init(dctx);
86 FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_UNSUPPORTED_DRBG_TYPE);
88 FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_ERROR_INITIALISING_DRBG);
91 /* If not in test mode run selftests on DRBG of the same type */
93 if (!(dctx->flags & DRBG_FLAG_TEST))
96 if (!fips_drbg_kat(&tctx, type, flags | DRBG_FLAG_TEST))
98 FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_SELFTEST_FAILURE);
106 DRBG_CTX *FIPS_drbg_new(int type, unsigned int flags)
109 dctx = OPENSSL_malloc(sizeof(DRBG_CTX));
112 FIPSerr(FIPS_F_FIPS_DRBG_NEW, ERR_R_MALLOC_FAILURE);
118 if (FIPS_drbg_init(dctx, type, flags) <= 0)
127 void FIPS_drbg_free(DRBG_CTX *dctx)
129 if (dctx->uninstantiate)
130 dctx->uninstantiate(dctx);
131 OPENSSL_cleanse(&dctx->d, sizeof(dctx->d));
135 static size_t fips_get_entropy(DRBG_CTX *dctx, unsigned char **pout,
136 int entropy, size_t min_len, size_t max_len)
138 unsigned char *tout, *p;
139 size_t bl = dctx->entropy_blocklen, rv;
140 if (dctx->flags & DRBG_FLAG_TEST || !bl)
141 return dctx->get_entropy(dctx, pout, entropy, min_len, max_len);
142 rv = dctx->get_entropy(dctx, &tout, entropy + bl,
143 min_len + bl, max_len + bl);
145 if (rv < (min_len + bl) || (rv % bl))
147 /* Compare consecutive blocks for continuous PRNG test */
148 for (p = tout; p < tout + rv; p += bl)
150 if (!memcmp(p, p + bl, bl))
152 FIPSerr(FIPS_F_FIPS_GET_ENTROPY, FIPS_R_ENTROPY_SOURCE_STUCK);
162 static void fips_cleanup_entropy(DRBG_CTX *dctx,
163 unsigned char *out, size_t olen)
166 if (dctx->flags & DRBG_FLAG_TEST)
169 bl = dctx->entropy_blocklen;
170 /* Call cleanup with original arguments */
171 dctx->cleanup_entropy(dctx, out - bl, olen + bl);
175 int FIPS_drbg_instantiate(DRBG_CTX *dctx,
176 const unsigned char *pers, size_t perslen)
178 size_t entlen = 0, noncelen = 0;
179 unsigned char *nonce = NULL, *entropy = NULL;
182 /* Put here so error script picks them up */
183 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE,
184 FIPS_R_PERSONALISATION_STRING_TOO_LONG);
185 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_IN_ERROR_STATE);
186 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ALREADY_INSTANTIATED);
187 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_ENTROPY);
188 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_NONCE);
189 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_INSTANTIATE_ERROR);
194 if (perslen > dctx->max_pers)
196 r = FIPS_R_PERSONALISATION_STRING_TOO_LONG;
200 if (dctx->status != DRBG_STATUS_UNINITIALISED)
202 if (dctx->status == DRBG_STATUS_ERROR)
203 r = FIPS_R_IN_ERROR_STATE;
205 r = FIPS_R_ALREADY_INSTANTIATED;
209 dctx->status = DRBG_STATUS_ERROR;
211 entlen = fips_get_entropy(dctx, &entropy, dctx->strength,
212 dctx->min_entropy, dctx->max_entropy);
214 if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
216 r = FIPS_R_ERROR_RETRIEVING_ENTROPY;
220 if (dctx->max_nonce > 0)
222 noncelen = dctx->get_nonce(dctx, &nonce,
224 dctx->min_nonce, dctx->max_nonce);
226 if (noncelen < dctx->min_nonce || noncelen > dctx->max_nonce)
228 r = FIPS_R_ERROR_RETRIEVING_NONCE;
234 if (!dctx->instantiate(dctx,
239 r = FIPS_R_ERROR_INSTANTIATING_DRBG;
244 dctx->status = DRBG_STATUS_READY;
245 dctx->reseed_counter = 1;
249 if (entropy && dctx->cleanup_entropy)
250 fips_cleanup_entropy(dctx, entropy, entlen);
252 if (nonce && dctx->cleanup_nonce)
253 dctx->cleanup_nonce(dctx, nonce, noncelen);
255 if (dctx->status == DRBG_STATUS_READY)
258 if (r && !(dctx->flags & DRBG_FLAG_NOERR))
259 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, r);
265 int FIPS_drbg_reseed(DRBG_CTX *dctx,
266 const unsigned char *adin, size_t adinlen)
268 unsigned char *entropy = NULL;
273 FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_NOT_INSTANTIATED);
274 FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_ADDITIONAL_INPUT_TOO_LONG);
276 if (dctx->status != DRBG_STATUS_READY
277 && dctx->status != DRBG_STATUS_RESEED)
279 if (dctx->status == DRBG_STATUS_ERROR)
280 r = FIPS_R_IN_ERROR_STATE;
281 else if(dctx->status == DRBG_STATUS_UNINITIALISED)
282 r = FIPS_R_NOT_INSTANTIATED;
288 else if (adinlen > dctx->max_adin)
290 r = FIPS_R_ADDITIONAL_INPUT_TOO_LONG;
294 dctx->status = DRBG_STATUS_ERROR;
296 entlen = fips_get_entropy(dctx, &entropy, dctx->strength,
297 dctx->min_entropy, dctx->max_entropy);
299 if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
301 r = FIPS_R_ERROR_RETRIEVING_ENTROPY;
305 if (!dctx->reseed(dctx, entropy, entlen, adin, adinlen))
308 dctx->status = DRBG_STATUS_READY;
309 dctx->reseed_counter = 1;
312 if (entropy && dctx->cleanup_entropy)
313 fips_cleanup_entropy(dctx, entropy, entlen);
315 if (dctx->status == DRBG_STATUS_READY)
318 if (r && !(dctx->flags & DRBG_FLAG_NOERR))
319 FIPSerr(FIPS_F_FIPS_DRBG_RESEED, r);
324 static int fips_drbg_check(DRBG_CTX *dctx)
326 if (dctx->flags & DRBG_FLAG_TEST)
328 dctx->health_check_cnt++;
329 if (dctx->health_check_cnt >= dctx->health_check_interval)
332 if (!fips_drbg_kat(&tctx, dctx->type,
333 dctx->flags | DRBG_FLAG_TEST))
335 FIPSerr(FIPS_F_FIPS_DRBG_CHECK, FIPS_R_SELFTEST_FAILURE);
338 dctx->health_check_cnt = 0;
343 int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen,
344 int strength, int prediction_resistance,
345 const unsigned char *adin, size_t adinlen)
349 if (!fips_drbg_check(dctx))
352 if (dctx->status != DRBG_STATUS_READY
353 && dctx->status != DRBG_STATUS_RESEED)
355 if (dctx->status == DRBG_STATUS_ERROR)
356 r = FIPS_R_IN_ERROR_STATE;
357 else if(dctx->status == DRBG_STATUS_UNINITIALISED)
358 r = FIPS_R_NOT_INSTANTIATED;
362 if (outlen > dctx->max_request)
364 r = FIPS_R_REQUEST_TOO_LARGE_FOR_DRBG;
368 if (strength > dctx->strength)
370 r = FIPS_R_INSUFFICIENT_SECURITY_STRENGTH;
374 if (dctx->status == DRBG_STATUS_RESEED || prediction_resistance)
376 if (!FIPS_drbg_reseed(dctx, adin, adinlen))
378 r = FIPS_R_RESEED_ERROR;
385 if (!dctx->generate(dctx, out, outlen, adin, adinlen))
387 r = FIPS_R_GENERATE_ERROR;
388 dctx->status = DRBG_STATUS_ERROR;
391 if (dctx->reseed_counter >= dctx->reseed_interval)
392 dctx->status = DRBG_STATUS_RESEED;
394 dctx->reseed_counter++;
399 if (!(dctx->flags & DRBG_FLAG_NOERR))
400 FIPSerr(FIPS_F_FIPS_DRBG_GENERATE, r);
407 int FIPS_drbg_uninstantiate(DRBG_CTX *dctx)
410 if (!dctx->uninstantiate)
413 rv = dctx->uninstantiate(dctx);
414 /* Although we'd like to cleanse here we can't because we have to
415 * test the uninstantiate really zeroes the data.
417 memset(&dctx->d, 0, sizeof(dctx->d));
418 dctx->status = DRBG_STATUS_UNINITIALISED;
419 /* If method has problems uninstantiating, return error */
423 int FIPS_drbg_set_callbacks(DRBG_CTX *dctx,
424 size_t (*get_entropy)(DRBG_CTX *ctx, unsigned char **pout,
425 int entropy, size_t min_len, size_t max_len),
426 void (*cleanup_entropy)(DRBG_CTX *ctx, unsigned char *out, size_t olen),
427 size_t entropy_blocklen,
428 size_t (*get_nonce)(DRBG_CTX *ctx, unsigned char **pout,
429 int entropy, size_t min_len, size_t max_len),
430 void (*cleanup_nonce)(DRBG_CTX *ctx, unsigned char *out, size_t olen))
432 if (dctx->status != DRBG_STATUS_UNINITIALISED)
434 dctx->entropy_blocklen = entropy_blocklen;
435 dctx->get_entropy = get_entropy;
436 dctx->cleanup_entropy = cleanup_entropy;
437 dctx->get_nonce = get_nonce;
438 dctx->cleanup_nonce = cleanup_nonce;
442 int FIPS_drbg_set_rand_callbacks(DRBG_CTX *dctx,
443 size_t (*get_adin)(DRBG_CTX *ctx, unsigned char **pout),
444 void (*cleanup_adin)(DRBG_CTX *ctx, unsigned char *out, size_t olen),
445 int (*rand_seed_cb)(DRBG_CTX *ctx, const void *buf, int num),
446 int (*rand_add_cb)(DRBG_CTX *ctx,
447 const void *buf, int num, double entropy))
449 if (dctx->status != DRBG_STATUS_UNINITIALISED)
451 dctx->get_adin = get_adin;
452 dctx->cleanup_adin = cleanup_adin;
453 dctx->rand_seed_cb = rand_seed_cb;
454 dctx->rand_add_cb = rand_add_cb;
458 void *FIPS_drbg_get_app_data(DRBG_CTX *dctx)
460 return dctx->app_data;
463 void FIPS_drbg_set_app_data(DRBG_CTX *dctx, void *app_data)
465 dctx->app_data = app_data;
468 size_t FIPS_drbg_get_blocklength(DRBG_CTX *dctx)
470 return dctx->blocklength;
473 int FIPS_drbg_get_strength(DRBG_CTX *dctx)
475 return dctx->strength;
478 void FIPS_drbg_set_check_interval(DRBG_CTX *dctx, int interval)
480 dctx->health_check_interval = interval;
483 static int drbg_stick = 0;
485 void FIPS_drbg_stick(void)
490 /* Continuous DRBG utility function */
491 int fips_drbg_cprng_test(DRBG_CTX *dctx, const unsigned char *out)
493 /* No CPRNG in test mode */
494 if (dctx->flags & DRBG_FLAG_TEST)
496 /* Check block is valid: should never happen */
497 if (dctx->lb_valid == 0)
499 FIPSerr(FIPS_F_FIPS_DRBG_CPRNG_TEST, FIPS_R_INTERNAL_ERROR);
500 fips_set_selftest_fail();
504 memcpy(dctx->lb, out, dctx->blocklength);
505 /* Check against last block: fail if match */
506 if (!memcmp(dctx->lb, out, dctx->blocklength))
508 FIPSerr(FIPS_F_FIPS_DRBG_CPRNG_TEST, FIPS_R_DRBG_STUCK);
509 fips_set_selftest_fail();
512 /* Save last block for next comparison */
513 memcpy(dctx->lb, out, dctx->blocklength);