2 * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/crypto.h>
12 #include <openssl/err.h>
13 #include <openssl/rand.h>
15 #include "internal/thread_once.h"
16 #include "internal/rand_int.h"
17 #include "internal/cryptlib_int.h"
20 * Support framework for NIST SP 800-90A DRBG
22 * See manual page RAND_DRBG(7) for a general overview.
24 * The OpenSSL model is to have new and free functions, and that new
25 * does all initialization. That is not the NIST model, which has
26 * instantiation and un-instantiate, and re-use within a new/free
27 * lifecycle. (No doubt this comes from the desire to support hardware
28 * DRBG, where allocation of resources on something like an HSM is
29 * a much bigger deal than just re-setting an allocated resource.)
33 * The three shared DRBG instances
35 * There are three shared DRBG instances: <master>, <public>, and <private>.
41 * Not used directly by the application, only for reseeding the two other
42 * DRBGs. It reseeds itself by pulling either randomness from os entropy
43 * sources or by consuming randomness which was added by RAND_add().
45 * The <master> DRBG is a global instance which is accessed concurrently by
46 * all threads. The necessary locking is managed automatically by its child
47 * DRBG instances during reseeding.
49 static RAND_DRBG *master_drbg;
53 * Used by default for generating random bytes using RAND_bytes().
55 * The <public> DRBG is thread-local, i.e., there is one instance per thread.
57 static CRYPTO_THREAD_LOCAL public_drbg;
61 * Used by default for generating private keys using RAND_priv_bytes()
63 * The <private> DRBG is thread-local, i.e., there is one instance per thread.
65 static CRYPTO_THREAD_LOCAL private_drbg;
69 /* NIST SP 800-90A DRBG recommends the use of a personalization string. */
70 static const char ossl_pers_string[] = "OpenSSL NIST SP 800-90A DRBG";
72 static CRYPTO_ONCE rand_drbg_init = CRYPTO_ONCE_STATIC_INIT;
75 #define RAND_DRBG_TYPE_FLAGS ( \
76 RAND_DRBG_FLAG_MASTER | RAND_DRBG_FLAG_PUBLIC | RAND_DRBG_FLAG_PRIVATE )
78 #define RAND_DRBG_TYPE_MASTER 0
79 #define RAND_DRBG_TYPE_PUBLIC 1
80 #define RAND_DRBG_TYPE_PRIVATE 2
83 static int rand_drbg_type[3] = {
84 RAND_DRBG_TYPE, /* Master */
85 RAND_DRBG_TYPE, /* Public */
86 RAND_DRBG_TYPE /* Private */
88 static unsigned int rand_drbg_flags[3] = {
89 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_MASTER, /* Master */
90 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC, /* Public */
91 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE /* Private */
94 static unsigned int master_reseed_interval = MASTER_RESEED_INTERVAL;
95 static unsigned int slave_reseed_interval = SLAVE_RESEED_INTERVAL;
97 static time_t master_reseed_time_interval = MASTER_RESEED_TIME_INTERVAL;
98 static time_t slave_reseed_time_interval = SLAVE_RESEED_TIME_INTERVAL;
100 /* A logical OR of all used DRBG flag bits (currently there is only one) */
101 static const unsigned int rand_drbg_used_flags =
102 RAND_DRBG_FLAG_CTR_NO_DF | RAND_DRBG_FLAG_HMAC | RAND_DRBG_TYPE_FLAGS;
105 static RAND_DRBG *drbg_setup(RAND_DRBG *parent, int drbg_type);
107 static RAND_DRBG *rand_drbg_new(int secure,
112 static int is_ctr(int type)
115 case NID_aes_128_ctr:
116 case NID_aes_192_ctr:
117 case NID_aes_256_ctr:
124 static int is_digest(int type)
145 * Set/initialize |drbg| to be of type |type|, with optional |flags|.
147 * If |type| and |flags| are zero, use the defaults
149 * Returns 1 on success, 0 on failure.
151 int RAND_DRBG_set(RAND_DRBG *drbg, int type, unsigned int flags)
155 if (type == 0 && flags == 0) {
156 type = rand_drbg_type[RAND_DRBG_TYPE_MASTER];
157 flags = rand_drbg_flags[RAND_DRBG_TYPE_MASTER];
160 /* If set is called multiple times - clear the old one */
161 if (type != drbg->type && drbg->type != 0 && drbg->meth != NULL) {
162 drbg->meth->uninstantiate(drbg);
165 drbg->state = DRBG_UNINITIALISED;
170 /* Uninitialized; that's okay. */
172 } else if (is_ctr(type)) {
173 ret = drbg_ctr_init(drbg);
174 } else if (is_digest(type)) {
175 if (flags & RAND_DRBG_FLAG_HMAC)
176 ret = drbg_hmac_init(drbg);
178 ret = drbg_hash_init(drbg);
180 RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_UNSUPPORTED_DRBG_TYPE);
185 RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_ERROR_INITIALISING_DRBG);
190 * Set/initialize default |type| and |flag| for new drbg instances.
192 * Returns 1 on success, 0 on failure.
194 int RAND_DRBG_set_defaults(int type, unsigned int flags)
197 if (!(is_digest(type) || is_ctr(type))) {
198 RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_TYPE);
202 if ((flags & ~rand_drbg_used_flags) != 0) {
203 RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_FLAGS);
207 all = ((flags & RAND_DRBG_TYPE_FLAGS) == 0);
208 if (all || (flags & RAND_DRBG_FLAG_MASTER) != 0) {
209 rand_drbg_type[RAND_DRBG_TYPE_MASTER] = type;
210 rand_drbg_flags[RAND_DRBG_TYPE_MASTER] = flags | RAND_DRBG_FLAG_MASTER;
212 if (all || (flags & RAND_DRBG_FLAG_PUBLIC) != 0) {
213 rand_drbg_type[RAND_DRBG_TYPE_PUBLIC] = type;
214 rand_drbg_flags[RAND_DRBG_TYPE_PUBLIC] = flags | RAND_DRBG_FLAG_PUBLIC;
216 if (all || (flags & RAND_DRBG_FLAG_PRIVATE) != 0) {
217 rand_drbg_type[RAND_DRBG_TYPE_PRIVATE] = type;
218 rand_drbg_flags[RAND_DRBG_TYPE_PRIVATE] = flags | RAND_DRBG_FLAG_PRIVATE;
225 * Allocate memory and initialize a new DRBG. The DRBG is allocated on
226 * the secure heap if |secure| is nonzero and the secure heap is enabled.
227 * The |parent|, if not NULL, will be used as random source for reseeding.
229 * Returns a pointer to the new DRBG instance on success, NULL on failure.
231 static RAND_DRBG *rand_drbg_new(int secure,
236 RAND_DRBG *drbg = secure ?
237 OPENSSL_secure_zalloc(sizeof(*drbg)) : OPENSSL_zalloc(sizeof(*drbg));
240 RANDerr(RAND_F_RAND_DRBG_NEW, ERR_R_MALLOC_FAILURE);
244 drbg->secure = secure && CRYPTO_secure_allocated(drbg);
245 drbg->fork_count = rand_fork_count;
246 drbg->parent = parent;
248 if (parent == NULL) {
249 drbg->get_entropy = rand_drbg_get_entropy;
250 drbg->cleanup_entropy = rand_drbg_cleanup_entropy;
251 #ifndef RAND_DRBG_GET_RANDOM_NONCE
252 drbg->get_nonce = rand_drbg_get_nonce;
253 drbg->cleanup_nonce = rand_drbg_cleanup_nonce;
256 drbg->reseed_interval = master_reseed_interval;
257 drbg->reseed_time_interval = master_reseed_time_interval;
259 drbg->get_entropy = rand_drbg_get_entropy;
260 drbg->cleanup_entropy = rand_drbg_cleanup_entropy;
262 * Do not provide nonce callbacks, the child DRBGs will
263 * obtain their nonce using random bits from the parent.
266 drbg->reseed_interval = slave_reseed_interval;
267 drbg->reseed_time_interval = slave_reseed_time_interval;
270 if (RAND_DRBG_set(drbg, type, flags) == 0)
273 if (parent != NULL) {
274 rand_drbg_lock(parent);
275 if (drbg->strength > parent->strength) {
277 * We currently don't support the algorithm from NIST SP 800-90C
278 * 10.1.2 to use a weaker DRBG as source
280 rand_drbg_unlock(parent);
281 RANDerr(RAND_F_RAND_DRBG_NEW, RAND_R_PARENT_STRENGTH_TOO_WEAK);
284 rand_drbg_unlock(parent);
291 OPENSSL_secure_free(drbg);
298 RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent)
300 return rand_drbg_new(0, type, flags, parent);
303 RAND_DRBG *RAND_DRBG_secure_new(int type, unsigned int flags, RAND_DRBG *parent)
305 return rand_drbg_new(1, type, flags, parent);
309 * Uninstantiate |drbg| and free all memory.
311 void RAND_DRBG_free(RAND_DRBG *drbg)
316 if (drbg->meth != NULL)
317 drbg->meth->uninstantiate(drbg);
318 CRYPTO_THREAD_lock_free(drbg->lock);
319 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
322 OPENSSL_secure_clear_free(drbg, sizeof(*drbg));
324 OPENSSL_clear_free(drbg, sizeof(*drbg));
328 * Instantiate |drbg|, after it has been initialized. Use |pers| and
329 * |perslen| as prediction-resistance input.
331 * Requires that drbg->lock is already locked for write, if non-null.
333 * Returns 1 on success, 0 on failure.
335 int RAND_DRBG_instantiate(RAND_DRBG *drbg,
336 const unsigned char *pers, size_t perslen)
338 unsigned char *nonce = NULL, *entropy = NULL;
339 size_t noncelen = 0, entropylen = 0;
340 size_t min_entropy = drbg->strength;
341 size_t min_entropylen = drbg->min_entropylen;
342 size_t max_entropylen = drbg->max_entropylen;
344 if (perslen > drbg->max_perslen) {
345 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
346 RAND_R_PERSONALISATION_STRING_TOO_LONG);
350 if (drbg->meth == NULL) {
351 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
352 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED);
356 if (drbg->state != DRBG_UNINITIALISED) {
357 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
358 drbg->state == DRBG_ERROR ? RAND_R_IN_ERROR_STATE
359 : RAND_R_ALREADY_INSTANTIATED);
363 drbg->state = DRBG_ERROR;
366 * NIST SP800-90Ar1 section 9.1 says you can combine getting the entropy
367 * and nonce in 1 call by increasing the entropy with 50% and increasing
368 * the minimum length to accomadate the length of the nonce.
369 * We do this in case a nonce is require and get_nonce is NULL.
371 if (drbg->min_noncelen > 0 && drbg->get_nonce == NULL) {
372 min_entropy += drbg->strength / 2;
373 min_entropylen += drbg->min_noncelen;
374 max_entropylen += drbg->max_noncelen;
377 if (drbg->get_entropy != NULL)
378 entropylen = drbg->get_entropy(drbg, &entropy, min_entropy,
379 min_entropylen, max_entropylen, 0);
380 if (entropylen < min_entropylen
381 || entropylen > max_entropylen) {
382 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_ENTROPY);
386 if (drbg->min_noncelen > 0 && drbg->get_nonce != NULL) {
387 noncelen = drbg->get_nonce(drbg, &nonce, drbg->strength / 2,
388 drbg->min_noncelen, drbg->max_noncelen);
389 if (noncelen < drbg->min_noncelen || noncelen > drbg->max_noncelen) {
390 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_NONCE);
395 if (!drbg->meth->instantiate(drbg, entropy, entropylen,
396 nonce, noncelen, pers, perslen)) {
397 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_INSTANTIATING_DRBG);
401 drbg->state = DRBG_READY;
402 drbg->reseed_gen_counter = 1;
403 drbg->reseed_time = time(NULL);
404 if (drbg->reseed_prop_counter > 0) {
405 if (drbg->parent == NULL)
406 drbg->reseed_prop_counter++;
408 drbg->reseed_prop_counter = drbg->parent->reseed_prop_counter;
412 if (entropy != NULL && drbg->cleanup_entropy != NULL)
413 drbg->cleanup_entropy(drbg, entropy, entropylen);
414 if (nonce != NULL && drbg->cleanup_nonce!= NULL )
415 drbg->cleanup_nonce(drbg, nonce, noncelen);
416 if (drbg->pool != NULL) {
417 if (drbg->state == DRBG_READY) {
418 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
419 RAND_R_ERROR_ENTROPY_POOL_WAS_IGNORED);
420 drbg->state = DRBG_ERROR;
422 rand_pool_free(drbg->pool);
425 if (drbg->state == DRBG_READY)
431 * Uninstantiate |drbg|. Must be instantiated before it can be used.
433 * Requires that drbg->lock is already locked for write, if non-null.
435 * Returns 1 on success, 0 on failure.
437 int RAND_DRBG_uninstantiate(RAND_DRBG *drbg)
439 int index = -1, type, flags;
440 if (drbg->meth == NULL) {
441 RANDerr(RAND_F_RAND_DRBG_UNINSTANTIATE,
442 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED);
446 /* Clear the entire drbg->ctr struct, then reset some important
447 * members of the drbg->ctr struct (e.g. keysize, df_ks) to their
450 drbg->meth->uninstantiate(drbg);
452 /* The reset uses the default values for type and flags */
453 if (drbg->flags & RAND_DRBG_FLAG_MASTER)
454 index = RAND_DRBG_TYPE_MASTER;
455 else if (drbg->flags & RAND_DRBG_FLAG_PRIVATE)
456 index = RAND_DRBG_TYPE_PRIVATE;
457 else if (drbg->flags & RAND_DRBG_FLAG_PUBLIC)
458 index = RAND_DRBG_TYPE_PUBLIC;
461 flags = rand_drbg_flags[index];
462 type = rand_drbg_type[index];
467 return RAND_DRBG_set(drbg, type, flags);
471 * Reseed |drbg|, mixing in the specified data
473 * Requires that drbg->lock is already locked for write, if non-null.
475 * Returns 1 on success, 0 on failure.
477 int RAND_DRBG_reseed(RAND_DRBG *drbg,
478 const unsigned char *adin, size_t adinlen,
479 int prediction_resistance)
481 unsigned char *entropy = NULL;
482 size_t entropylen = 0;
484 if (drbg->state == DRBG_ERROR) {
485 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_IN_ERROR_STATE);
488 if (drbg->state == DRBG_UNINITIALISED) {
489 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_NOT_INSTANTIATED);
495 } else if (adinlen > drbg->max_adinlen) {
496 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
500 drbg->state = DRBG_ERROR;
501 if (drbg->get_entropy != NULL)
502 entropylen = drbg->get_entropy(drbg, &entropy, drbg->strength,
503 drbg->min_entropylen,
504 drbg->max_entropylen,
505 prediction_resistance);
506 if (entropylen < drbg->min_entropylen
507 || entropylen > drbg->max_entropylen) {
508 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ERROR_RETRIEVING_ENTROPY);
512 if (!drbg->meth->reseed(drbg, entropy, entropylen, adin, adinlen))
515 drbg->state = DRBG_READY;
516 drbg->reseed_gen_counter = 1;
517 drbg->reseed_time = time(NULL);
518 if (drbg->reseed_prop_counter > 0) {
519 if (drbg->parent == NULL)
520 drbg->reseed_prop_counter++;
522 drbg->reseed_prop_counter = drbg->parent->reseed_prop_counter;
526 if (entropy != NULL && drbg->cleanup_entropy != NULL)
527 drbg->cleanup_entropy(drbg, entropy, entropylen);
528 if (drbg->state == DRBG_READY)
534 * Restart |drbg|, using the specified entropy or additional input
536 * Tries its best to get the drbg instantiated by all means,
537 * regardless of its current state.
539 * Optionally, a |buffer| of |len| random bytes can be passed,
540 * which is assumed to contain at least |entropy| bits of entropy.
542 * If |entropy| > 0, the buffer content is used as entropy input.
544 * If |entropy| == 0, the buffer content is used as additional input
546 * Returns 1 on success, 0 on failure.
548 * This function is used internally only.
550 int rand_drbg_restart(RAND_DRBG *drbg,
551 const unsigned char *buffer, size_t len, size_t entropy)
554 const unsigned char *adin = NULL;
557 if (drbg->pool != NULL) {
558 RANDerr(RAND_F_RAND_DRBG_RESTART, ERR_R_INTERNAL_ERROR);
559 rand_pool_free(drbg->pool);
563 if (buffer != NULL) {
565 if (drbg->max_entropylen < len) {
566 RANDerr(RAND_F_RAND_DRBG_RESTART,
567 RAND_R_ENTROPY_INPUT_TOO_LONG);
571 if (entropy > 8 * len) {
572 RANDerr(RAND_F_RAND_DRBG_RESTART, RAND_R_ENTROPY_OUT_OF_RANGE);
576 /* will be picked up by the rand_drbg_get_entropy() callback */
577 drbg->pool = rand_pool_new(entropy, len, len);
578 if (drbg->pool == NULL)
581 rand_pool_add(drbg->pool, buffer, len, entropy);
583 if (drbg->max_adinlen < len) {
584 RANDerr(RAND_F_RAND_DRBG_RESTART,
585 RAND_R_ADDITIONAL_INPUT_TOO_LONG);
593 /* repair error state */
594 if (drbg->state == DRBG_ERROR)
595 RAND_DRBG_uninstantiate(drbg);
597 /* repair uninitialized state */
598 if (drbg->state == DRBG_UNINITIALISED) {
599 /* reinstantiate drbg */
600 RAND_DRBG_instantiate(drbg,
601 (const unsigned char *) ossl_pers_string,
602 sizeof(ossl_pers_string) - 1);
603 /* already reseeded. prevent second reseeding below */
604 reseeded = (drbg->state == DRBG_READY);
607 /* refresh current state if entropy or additional input has been provided */
608 if (drbg->state == DRBG_READY) {
611 * mix in additional input without reseeding
613 * Similar to RAND_DRBG_reseed(), but the provided additional
614 * data |adin| is mixed into the current state without pulling
615 * entropy from the trusted entropy source using get_entropy().
616 * This is not a reseeding in the strict sense of NIST SP 800-90A.
618 drbg->meth->reseed(drbg, adin, adinlen, NULL, 0);
619 } else if (reseeded == 0) {
620 /* do a full reseeding if it has not been done yet above */
621 RAND_DRBG_reseed(drbg, NULL, 0, 0);
625 /* check whether a given entropy pool was cleared properly during reseed */
626 if (drbg->pool != NULL) {
627 drbg->state = DRBG_ERROR;
628 RANDerr(RAND_F_RAND_DRBG_RESTART, ERR_R_INTERNAL_ERROR);
629 rand_pool_free(drbg->pool);
634 return drbg->state == DRBG_READY;
638 * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
639 * to or if |prediction_resistance| is set. Additional input can be
640 * sent in |adin| and |adinlen|.
642 * Requires that drbg->lock is already locked for write, if non-null.
644 * Returns 1 on success, 0 on failure.
647 int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,
648 int prediction_resistance,
649 const unsigned char *adin, size_t adinlen)
651 int reseed_required = 0;
653 if (drbg->state != DRBG_READY) {
654 /* try to recover from previous errors */
655 rand_drbg_restart(drbg, NULL, 0, 0);
657 if (drbg->state == DRBG_ERROR) {
658 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_IN_ERROR_STATE);
661 if (drbg->state == DRBG_UNINITIALISED) {
662 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_NOT_INSTANTIATED);
667 if (outlen > drbg->max_request) {
668 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG);
671 if (adinlen > drbg->max_adinlen) {
672 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
676 if (drbg->fork_count != rand_fork_count) {
677 drbg->fork_count = rand_fork_count;
681 if (drbg->reseed_interval > 0) {
682 if (drbg->reseed_gen_counter > drbg->reseed_interval)
685 if (drbg->reseed_time_interval > 0) {
686 time_t now = time(NULL);
687 if (now < drbg->reseed_time
688 || now - drbg->reseed_time >= drbg->reseed_time_interval)
691 if (drbg->reseed_prop_counter > 0 && drbg->parent != NULL) {
692 if (drbg->reseed_prop_counter != drbg->parent->reseed_prop_counter)
696 if (reseed_required || prediction_resistance) {
697 if (!RAND_DRBG_reseed(drbg, adin, adinlen, prediction_resistance)) {
698 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_RESEED_ERROR);
705 if (!drbg->meth->generate(drbg, out, outlen, adin, adinlen)) {
706 drbg->state = DRBG_ERROR;
707 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_GENERATE_ERROR);
711 drbg->reseed_gen_counter++;
717 * Generates |outlen| random bytes and stores them in |out|. It will
718 * using the given |drbg| to generate the bytes.
720 * Requires that drbg->lock is already locked for write, if non-null.
722 * Returns 1 on success 0 on failure.
724 int RAND_DRBG_bytes(RAND_DRBG *drbg, unsigned char *out, size_t outlen)
726 unsigned char *additional = NULL;
727 size_t additional_len;
731 additional_len = rand_drbg_get_additional_data(&additional, drbg->max_adinlen);
733 for ( ; outlen > 0; outlen -= chunk, out += chunk) {
735 if (chunk > drbg->max_request)
736 chunk = drbg->max_request;
737 ret = RAND_DRBG_generate(drbg, out, chunk, 0, additional, additional_len);
744 if (additional_len != 0)
745 OPENSSL_secure_clear_free(additional, additional_len);
751 * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
753 * Setting the callbacks is allowed only if the drbg has not been
754 * initialized yet. Otherwise, the operation will fail.
756 * Returns 1 on success, 0 on failure.
758 int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
759 RAND_DRBG_get_entropy_fn get_entropy,
760 RAND_DRBG_cleanup_entropy_fn cleanup_entropy,
761 RAND_DRBG_get_nonce_fn get_nonce,
762 RAND_DRBG_cleanup_nonce_fn cleanup_nonce)
764 if (drbg->state != DRBG_UNINITIALISED)
766 drbg->get_entropy = get_entropy;
767 drbg->cleanup_entropy = cleanup_entropy;
768 drbg->get_nonce = get_nonce;
769 drbg->cleanup_nonce = cleanup_nonce;
774 * Set the reseed interval.
776 * The drbg will reseed automatically whenever the number of generate
777 * requests exceeds the given reseed interval. If the reseed interval
778 * is 0, then this feature is disabled.
780 * Returns 1 on success, 0 on failure.
782 int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval)
784 if (interval > MAX_RESEED_INTERVAL)
786 drbg->reseed_interval = interval;
791 * Set the reseed time interval.
793 * The drbg will reseed automatically whenever the time elapsed since
794 * the last reseeding exceeds the given reseed time interval. For safety,
795 * a reseeding will also occur if the clock has been reset to a smaller
798 * Returns 1 on success, 0 on failure.
800 int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval)
802 if (interval > MAX_RESEED_TIME_INTERVAL)
804 drbg->reseed_time_interval = interval;
809 * Set the default values for reseed (time) intervals of new DRBG instances
811 * The default values can be set independently for master DRBG instances
812 * (without a parent) and slave DRBG instances (with parent).
814 * Returns 1 on success, 0 on failure.
817 int RAND_DRBG_set_reseed_defaults(
818 unsigned int _master_reseed_interval,
819 unsigned int _slave_reseed_interval,
820 time_t _master_reseed_time_interval,
821 time_t _slave_reseed_time_interval
824 if (_master_reseed_interval > MAX_RESEED_INTERVAL
825 || _slave_reseed_interval > MAX_RESEED_INTERVAL)
828 if (_master_reseed_time_interval > MAX_RESEED_TIME_INTERVAL
829 || _slave_reseed_time_interval > MAX_RESEED_TIME_INTERVAL)
832 master_reseed_interval = _master_reseed_interval;
833 slave_reseed_interval = _slave_reseed_interval;
835 master_reseed_time_interval = _master_reseed_time_interval;
836 slave_reseed_time_interval = _slave_reseed_time_interval;
842 * Locks the given drbg. Locking a drbg which does not have locking
843 * enabled is considered a successful no-op.
845 * Returns 1 on success, 0 on failure.
847 int rand_drbg_lock(RAND_DRBG *drbg)
849 if (drbg->lock != NULL)
850 return CRYPTO_THREAD_write_lock(drbg->lock);
856 * Unlocks the given drbg. Unlocking a drbg which does not have locking
857 * enabled is considered a successful no-op.
859 * Returns 1 on success, 0 on failure.
861 int rand_drbg_unlock(RAND_DRBG *drbg)
863 if (drbg->lock != NULL)
864 return CRYPTO_THREAD_unlock(drbg->lock);
870 * Enables locking for the given drbg
872 * Locking can only be enabled if the random generator
873 * is in the uninitialized state.
875 * Returns 1 on success, 0 on failure.
877 int rand_drbg_enable_locking(RAND_DRBG *drbg)
879 if (drbg->state != DRBG_UNINITIALISED) {
880 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
881 RAND_R_DRBG_ALREADY_INITIALIZED);
885 if (drbg->lock == NULL) {
886 if (drbg->parent != NULL && drbg->parent->lock == NULL) {
887 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
888 RAND_R_PARENT_LOCKING_NOT_ENABLED);
892 drbg->lock = CRYPTO_THREAD_lock_new();
893 if (drbg->lock == NULL) {
894 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
895 RAND_R_FAILED_TO_CREATE_LOCK);
904 * Get and set the EXDATA
906 int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg)
908 return CRYPTO_set_ex_data(&drbg->ex_data, idx, arg);
911 void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx)
913 return CRYPTO_get_ex_data(&drbg->ex_data, idx);
918 * The following functions provide a RAND_METHOD that works on the
919 * global DRBG. They lock.
923 * Allocates a new global DRBG on the secure heap (if enabled) and
924 * initializes it with default settings.
926 * Returns a pointer to the new DRBG instance on success, NULL on failure.
928 static RAND_DRBG *drbg_setup(RAND_DRBG *parent, int drbg_type)
932 drbg = RAND_DRBG_secure_new(rand_drbg_type[drbg_type],
933 rand_drbg_flags[drbg_type], parent);
937 /* Only the master DRBG needs to have a lock */
938 if (parent == NULL && rand_drbg_enable_locking(drbg) == 0)
941 /* enable seed propagation */
942 drbg->reseed_prop_counter = 1;
945 * Ignore instantiation error to support just-in-time instantiation.
947 * The state of the drbg will be checked in RAND_DRBG_generate() and
948 * an automatic recovery is attempted.
950 (void)RAND_DRBG_instantiate(drbg,
951 (const unsigned char *) ossl_pers_string,
952 sizeof(ossl_pers_string) - 1);
956 RAND_DRBG_free(drbg);
961 * Initialize the global DRBGs on first use.
962 * Returns 1 on success, 0 on failure.
964 DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init)
967 * ensure that libcrypto is initialized, otherwise the
968 * DRBG locks are not cleaned up properly
970 if (!OPENSSL_init_crypto(0, NULL))
973 if (!CRYPTO_THREAD_init_local(&private_drbg, NULL))
976 if (!CRYPTO_THREAD_init_local(&public_drbg, NULL))
979 master_drbg = drbg_setup(NULL, RAND_DRBG_TYPE_MASTER);
980 if (master_drbg == NULL)
986 CRYPTO_THREAD_cleanup_local(&public_drbg);
988 CRYPTO_THREAD_cleanup_local(&private_drbg);
992 /* Clean up the global DRBGs before exit */
993 void rand_drbg_cleanup_int(void)
995 if (master_drbg != NULL) {
996 RAND_DRBG_free(master_drbg);
999 CRYPTO_THREAD_cleanup_local(&private_drbg);
1000 CRYPTO_THREAD_cleanup_local(&public_drbg);
1004 void drbg_delete_thread_state(void)
1008 drbg = CRYPTO_THREAD_get_local(&public_drbg);
1009 CRYPTO_THREAD_set_local(&public_drbg, NULL);
1010 RAND_DRBG_free(drbg);
1012 drbg = CRYPTO_THREAD_get_local(&private_drbg);
1013 CRYPTO_THREAD_set_local(&private_drbg, NULL);
1014 RAND_DRBG_free(drbg);
1017 /* Implements the default OpenSSL RAND_bytes() method */
1018 static int drbg_bytes(unsigned char *out, int count)
1021 RAND_DRBG *drbg = RAND_DRBG_get0_public();
1026 ret = RAND_DRBG_bytes(drbg, out, count);
1031 /* Implements the default OpenSSL RAND_add() method */
1032 static int drbg_add(const void *buf, int num, double randomness)
1035 RAND_DRBG *drbg = RAND_DRBG_get0_master();
1040 if (num < 0 || randomness < 0.0)
1043 if (randomness > (double)drbg->max_entropylen) {
1045 * The purpose of this check is to bound |randomness| by a
1046 * relatively small value in order to prevent an integer
1047 * overflow when multiplying by 8 in the rand_drbg_restart()
1053 rand_drbg_lock(drbg);
1054 ret = rand_drbg_restart(drbg, buf,
1055 (size_t)(unsigned int)num,
1056 (size_t)(8*randomness));
1057 rand_drbg_unlock(drbg);
1062 /* Implements the default OpenSSL RAND_seed() method */
1063 static int drbg_seed(const void *buf, int num)
1065 return drbg_add(buf, num, num);
1068 /* Implements the default OpenSSL RAND_status() method */
1069 static int drbg_status(void)
1072 RAND_DRBG *drbg = RAND_DRBG_get0_master();
1077 rand_drbg_lock(drbg);
1078 ret = drbg->state == DRBG_READY ? 1 : 0;
1079 rand_drbg_unlock(drbg);
1084 * Get the master DRBG.
1085 * Returns pointer to the DRBG on success, NULL on failure.
1088 RAND_DRBG *RAND_DRBG_get0_master(void)
1090 if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
1097 * Get the public DRBG.
1098 * Returns pointer to the DRBG on success, NULL on failure.
1100 RAND_DRBG *RAND_DRBG_get0_public(void)
1104 if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
1107 drbg = CRYPTO_THREAD_get_local(&public_drbg);
1109 if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND))
1111 drbg = drbg_setup(master_drbg, RAND_DRBG_TYPE_PUBLIC);
1112 CRYPTO_THREAD_set_local(&public_drbg, drbg);
1118 * Get the private DRBG.
1119 * Returns pointer to the DRBG on success, NULL on failure.
1121 RAND_DRBG *RAND_DRBG_get0_private(void)
1125 if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
1128 drbg = CRYPTO_THREAD_get_local(&private_drbg);
1130 if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND))
1132 drbg = drbg_setup(master_drbg, RAND_DRBG_TYPE_PRIVATE);
1133 CRYPTO_THREAD_set_local(&private_drbg, drbg);
1138 RAND_METHOD rand_meth = {
1147 RAND_METHOD *RAND_OpenSSL(void)