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/err.h>
59 #include <openssl/fips_rand.h>
60 #include "fips_rand_lcl.h"
62 /* Support framework for SP800-90 DRBGs */
64 int FIPS_drbg_init(DRBG_CTX *dctx, int type, unsigned int flags)
67 memset(dctx, 0, sizeof(DRBG_CTX));
68 dctx->status = DRBG_STATUS_UNINITIALISED;
72 dctx->entropy_blocklen = 0;
73 dctx->health_check_cnt = 0;
74 dctx->health_check_interval = DRBG_HEALTH_INTERVAL;
76 rv = fips_drbg_hash_init(dctx);
79 rv = fips_drbg_ctr_init(dctx);
81 rv = fips_drbg_hmac_init(dctx);
83 rv = fips_drbg_ec_init(dctx);
88 FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_UNSUPPORTED_DRBG_TYPE);
90 FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_ERROR_INITIALISING_DRBG);
93 /* If not in test mode run selftests on DRBG of the same type */
95 if (!(dctx->flags & DRBG_FLAG_TEST))
98 if (!fips_drbg_kat(&tctx, type, flags | DRBG_FLAG_TEST))
100 FIPSerr(FIPS_F_FIPS_DRBG_INIT, FIPS_R_SELFTEST_FAILURE);
101 dctx->status = DRBG_STATUS_ERROR;
109 DRBG_CTX *FIPS_drbg_new(int type, unsigned int flags)
112 dctx = OPENSSL_malloc(sizeof(DRBG_CTX));
115 FIPSerr(FIPS_F_FIPS_DRBG_NEW, ERR_R_MALLOC_FAILURE);
121 memset(dctx, 0, sizeof(DRBG_CTX));
123 dctx->status = DRBG_STATUS_UNINITIALISED;
127 if (FIPS_drbg_init(dctx, type, flags) <= 0)
136 void FIPS_drbg_free(DRBG_CTX *dctx)
138 if (dctx->uninstantiate)
139 dctx->uninstantiate(dctx);
140 OPENSSL_cleanse(&dctx->d, sizeof(dctx->d));
144 static size_t fips_get_entropy(DRBG_CTX *dctx, unsigned char **pout,
145 int entropy, size_t min_len, size_t max_len)
147 unsigned char *tout, *p;
148 size_t bl = dctx->entropy_blocklen, rv;
149 if (dctx->flags & DRBG_FLAG_TEST || !bl)
150 return dctx->get_entropy(dctx, pout, entropy, min_len, max_len);
151 rv = dctx->get_entropy(dctx, &tout, entropy + bl,
152 min_len + bl, max_len + bl);
154 if (rv < (min_len + bl) || (rv % bl))
156 /* Compare consecutive blocks for continuous PRNG test */
157 for (p = tout; p < tout + rv - bl; p += bl)
159 if (!memcmp(p, p + bl, bl))
161 FIPSerr(FIPS_F_FIPS_GET_ENTROPY, FIPS_R_ENTROPY_SOURCE_STUCK);
171 static void fips_cleanup_entropy(DRBG_CTX *dctx,
172 unsigned char *out, size_t olen)
175 if (dctx->flags & DRBG_FLAG_TEST)
178 bl = dctx->entropy_blocklen;
179 /* Call cleanup with original arguments */
180 dctx->cleanup_entropy(dctx, out - bl, olen + bl);
184 int FIPS_drbg_instantiate(DRBG_CTX *dctx,
185 const unsigned char *pers, size_t perslen)
187 size_t entlen = 0, noncelen = 0;
188 unsigned char *nonce = NULL, *entropy = NULL;
191 /* Put here so error script picks them up */
192 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE,
193 FIPS_R_PERSONALISATION_STRING_TOO_LONG);
194 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_IN_ERROR_STATE);
195 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ALREADY_INSTANTIATED);
196 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_ENTROPY);
197 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_ERROR_RETRIEVING_NONCE);
198 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, FIPS_R_INSTANTIATE_ERROR);
203 if (perslen > dctx->max_pers)
205 r = FIPS_R_PERSONALISATION_STRING_TOO_LONG;
209 if (dctx->status != DRBG_STATUS_UNINITIALISED)
211 if (dctx->status == DRBG_STATUS_ERROR)
212 r = FIPS_R_IN_ERROR_STATE;
214 r = FIPS_R_ALREADY_INSTANTIATED;
218 dctx->status = DRBG_STATUS_ERROR;
220 entlen = fips_get_entropy(dctx, &entropy, dctx->strength,
221 dctx->min_entropy, dctx->max_entropy);
223 if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
225 r = FIPS_R_ERROR_RETRIEVING_ENTROPY;
229 if (dctx->max_nonce > 0)
231 noncelen = dctx->get_nonce(dctx, &nonce,
233 dctx->min_nonce, dctx->max_nonce);
235 if (noncelen < dctx->min_nonce || noncelen > dctx->max_nonce)
237 r = FIPS_R_ERROR_RETRIEVING_NONCE;
243 if (!dctx->instantiate(dctx,
248 r = FIPS_R_ERROR_INSTANTIATING_DRBG;
253 dctx->status = DRBG_STATUS_READY;
254 if (!(dctx->flags & DRBG_CUSTOM_RESEED))
255 dctx->reseed_counter = 1;
259 if (entropy && dctx->cleanup_entropy)
260 fips_cleanup_entropy(dctx, entropy, entlen);
262 if (nonce && dctx->cleanup_nonce)
263 dctx->cleanup_nonce(dctx, nonce, noncelen);
265 if (dctx->status == DRBG_STATUS_READY)
268 if (r && !(dctx->flags & DRBG_FLAG_NOERR))
269 FIPSerr(FIPS_F_FIPS_DRBG_INSTANTIATE, r);
275 int FIPS_drbg_reseed(DRBG_CTX *dctx,
276 const unsigned char *adin, size_t adinlen)
278 unsigned char *entropy = NULL;
283 FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_NOT_INSTANTIATED);
284 FIPSerr(FIPS_F_FIPS_DRBG_RESEED, FIPS_R_ADDITIONAL_INPUT_TOO_LONG);
286 if (dctx->status != DRBG_STATUS_READY
287 && dctx->status != DRBG_STATUS_RESEED)
289 if (dctx->status == DRBG_STATUS_ERROR)
290 r = FIPS_R_IN_ERROR_STATE;
291 else if(dctx->status == DRBG_STATUS_UNINITIALISED)
292 r = FIPS_R_NOT_INSTANTIATED;
298 else if (adinlen > dctx->max_adin)
300 r = FIPS_R_ADDITIONAL_INPUT_TOO_LONG;
304 dctx->status = DRBG_STATUS_ERROR;
306 entlen = fips_get_entropy(dctx, &entropy, dctx->strength,
307 dctx->min_entropy, dctx->max_entropy);
309 if (entlen < dctx->min_entropy || entlen > dctx->max_entropy)
311 r = FIPS_R_ERROR_RETRIEVING_ENTROPY;
315 if (!dctx->reseed(dctx, entropy, entlen, adin, adinlen))
318 dctx->status = DRBG_STATUS_READY;
319 if (!(dctx->flags & DRBG_CUSTOM_RESEED))
320 dctx->reseed_counter = 1;
323 if (entropy && dctx->cleanup_entropy)
324 fips_cleanup_entropy(dctx, entropy, entlen);
326 if (dctx->status == DRBG_STATUS_READY)
329 if (r && !(dctx->flags & DRBG_FLAG_NOERR))
330 FIPSerr(FIPS_F_FIPS_DRBG_RESEED, r);
335 static int fips_drbg_check(DRBG_CTX *dctx)
337 if (dctx->flags & DRBG_FLAG_TEST)
339 dctx->health_check_cnt++;
340 if (dctx->health_check_cnt >= dctx->health_check_interval)
343 if (!fips_drbg_kat(&tctx, dctx->type,
344 dctx->flags | DRBG_FLAG_TEST))
346 FIPSerr(FIPS_F_FIPS_DRBG_CHECK, FIPS_R_SELFTEST_FAILURE);
347 dctx->status = DRBG_STATUS_ERROR;
350 dctx->health_check_cnt = 0;
355 int FIPS_drbg_generate(DRBG_CTX *dctx, unsigned char *out, size_t outlen,
356 int prediction_resistance,
357 const unsigned char *adin, size_t adinlen)
361 if (!fips_drbg_check(dctx))
364 if (dctx->status != DRBG_STATUS_READY
365 && dctx->status != DRBG_STATUS_RESEED)
367 if (dctx->status == DRBG_STATUS_ERROR)
368 r = FIPS_R_IN_ERROR_STATE;
369 else if(dctx->status == DRBG_STATUS_UNINITIALISED)
370 r = FIPS_R_NOT_INSTANTIATED;
374 if (outlen > dctx->max_request)
376 r = FIPS_R_REQUEST_TOO_LARGE_FOR_DRBG;
380 if (adinlen > dctx->max_adin)
382 r = FIPS_R_ADDITIONAL_INPUT_TOO_LONG;
386 if (dctx->flags & DRBG_CUSTOM_RESEED)
387 dctx->generate(dctx, NULL, outlen, NULL, 0);
388 else if (dctx->reseed_counter >= dctx->reseed_interval)
389 dctx->status = DRBG_STATUS_RESEED;
391 if (dctx->status == DRBG_STATUS_RESEED || prediction_resistance)
393 if (!FIPS_drbg_reseed(dctx, adin, adinlen))
395 r = FIPS_R_RESEED_ERROR;
402 if (!dctx->generate(dctx, out, outlen, adin, adinlen))
404 r = FIPS_R_GENERATE_ERROR;
405 dctx->status = DRBG_STATUS_ERROR;
408 if (!(dctx->flags & DRBG_CUSTOM_RESEED))
410 if (dctx->reseed_counter >= dctx->reseed_interval)
411 dctx->status = DRBG_STATUS_RESEED;
413 dctx->reseed_counter++;
419 if (!(dctx->flags & DRBG_FLAG_NOERR))
420 FIPSerr(FIPS_F_FIPS_DRBG_GENERATE, r);
427 int FIPS_drbg_uninstantiate(DRBG_CTX *dctx)
430 if (!dctx->uninstantiate)
433 rv = dctx->uninstantiate(dctx);
434 /* Although we'd like to cleanse here we can't because we have to
435 * test the uninstantiate really zeroes the data.
437 memset(&dctx->d, 0, sizeof(dctx->d));
438 dctx->status = DRBG_STATUS_UNINITIALISED;
439 /* If method has problems uninstantiating, return error */
443 int FIPS_drbg_set_callbacks(DRBG_CTX *dctx,
444 size_t (*get_entropy)(DRBG_CTX *ctx, unsigned char **pout,
445 int entropy, size_t min_len, size_t max_len),
446 void (*cleanup_entropy)(DRBG_CTX *ctx, unsigned char *out, size_t olen),
447 size_t entropy_blocklen,
448 size_t (*get_nonce)(DRBG_CTX *ctx, unsigned char **pout,
449 int entropy, size_t min_len, size_t max_len),
450 void (*cleanup_nonce)(DRBG_CTX *ctx, unsigned char *out, size_t olen))
452 if (dctx->status != DRBG_STATUS_UNINITIALISED)
454 dctx->entropy_blocklen = entropy_blocklen;
455 dctx->get_entropy = get_entropy;
456 dctx->cleanup_entropy = cleanup_entropy;
457 dctx->get_nonce = get_nonce;
458 dctx->cleanup_nonce = cleanup_nonce;
462 int FIPS_drbg_set_rand_callbacks(DRBG_CTX *dctx,
463 size_t (*get_adin)(DRBG_CTX *ctx, unsigned char **pout),
464 void (*cleanup_adin)(DRBG_CTX *ctx, unsigned char *out, size_t olen),
465 int (*rand_seed_cb)(DRBG_CTX *ctx, const void *buf, int num),
466 int (*rand_add_cb)(DRBG_CTX *ctx,
467 const void *buf, int num, double entropy))
469 if (dctx->status != DRBG_STATUS_UNINITIALISED)
471 dctx->get_adin = get_adin;
472 dctx->cleanup_adin = cleanup_adin;
473 dctx->rand_seed_cb = rand_seed_cb;
474 dctx->rand_add_cb = rand_add_cb;
478 void *FIPS_drbg_get_app_data(DRBG_CTX *dctx)
480 return dctx->app_data;
483 void FIPS_drbg_set_app_data(DRBG_CTX *dctx, void *app_data)
485 dctx->app_data = app_data;
488 size_t FIPS_drbg_get_blocklength(DRBG_CTX *dctx)
490 return dctx->blocklength;
493 int FIPS_drbg_get_strength(DRBG_CTX *dctx)
495 return dctx->strength;
498 void FIPS_drbg_set_check_interval(DRBG_CTX *dctx, int interval)
500 dctx->health_check_interval = interval;
503 void FIPS_drbg_set_reseed_interval(DRBG_CTX *dctx, int interval)
505 dctx->reseed_interval = interval;
508 static int drbg_stick = 0;
510 void FIPS_drbg_stick(void)
515 /* Continuous DRBG utility function */
516 int fips_drbg_cprng_test(DRBG_CTX *dctx, const unsigned char *out)
518 /* No CPRNG in test mode */
519 if (dctx->flags & DRBG_FLAG_TEST)
521 /* Check block is valid: should never happen */
522 if (dctx->lb_valid == 0)
524 FIPSerr(FIPS_F_FIPS_DRBG_CPRNG_TEST, FIPS_R_INTERNAL_ERROR);
525 fips_set_selftest_fail();
529 memcpy(dctx->lb, out, dctx->blocklength);
530 /* Check against last block: fail if match */
531 if (!memcmp(dctx->lb, out, dctx->blocklength))
533 FIPSerr(FIPS_F_FIPS_DRBG_CPRNG_TEST, FIPS_R_DRBG_STUCK);
534 fips_set_selftest_fail();
537 /* Save last block for next comparison */
538 memcpy(dctx->lb, out, dctx->blocklength);