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;
76 static int rand_drbg_type = RAND_DRBG_TYPE;
77 static unsigned int rand_drbg_flags = RAND_DRBG_FLAGS;
79 static unsigned int master_reseed_interval = MASTER_RESEED_INTERVAL;
80 static unsigned int slave_reseed_interval = SLAVE_RESEED_INTERVAL;
82 static time_t master_reseed_time_interval = MASTER_RESEED_TIME_INTERVAL;
83 static time_t slave_reseed_time_interval = SLAVE_RESEED_TIME_INTERVAL;
85 static RAND_DRBG *drbg_setup(RAND_DRBG *parent);
87 static RAND_DRBG *rand_drbg_new(int secure,
93 * Set/initialize |drbg| to be of type |type|, with optional |flags|.
95 * If |type| and |flags| are zero, use the defaults
97 * Returns 1 on success, 0 on failure.
99 int RAND_DRBG_set(RAND_DRBG *drbg, int type, unsigned int flags)
103 if (type == 0 && flags == 0) {
104 type = rand_drbg_type;
105 flags = rand_drbg_flags;
108 drbg->state = DRBG_UNINITIALISED;
114 RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_UNSUPPORTED_DRBG_TYPE);
117 /* Uninitialized; that's okay. */
119 case NID_aes_128_ctr:
120 case NID_aes_192_ctr:
121 case NID_aes_256_ctr:
122 ret = drbg_ctr_init(drbg);
127 RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_ERROR_INITIALISING_DRBG);
132 * Set/initialize default |type| and |flag| for new drbg instances.
134 * Returns 1 on success, 0 on failure.
136 int RAND_DRBG_set_defaults(int type, unsigned int flags)
142 RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_TYPE);
144 case NID_aes_128_ctr:
145 case NID_aes_192_ctr:
146 case NID_aes_256_ctr:
150 if ((flags & ~RAND_DRBG_USED_FLAGS) != 0) {
151 RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_FLAGS);
155 rand_drbg_type = type;
156 rand_drbg_flags = flags;
163 * Allocate memory and initialize a new DRBG. The DRBG is allocated on
164 * the secure heap if |secure| is nonzero and the secure heap is enabled.
165 * The |parent|, if not NULL, will be used as random source for reseeding.
167 * Returns a pointer to the new DRBG instance on success, NULL on failure.
169 static RAND_DRBG *rand_drbg_new(int secure,
174 RAND_DRBG *drbg = secure ?
175 OPENSSL_secure_zalloc(sizeof(*drbg)) : OPENSSL_zalloc(sizeof(*drbg));
178 RANDerr(RAND_F_RAND_DRBG_NEW, ERR_R_MALLOC_FAILURE);
182 drbg->secure = secure && CRYPTO_secure_allocated(drbg);
183 drbg->fork_count = rand_fork_count;
184 drbg->parent = parent;
186 if (parent == NULL) {
187 drbg->reseed_interval = master_reseed_interval;
188 drbg->reseed_time_interval = master_reseed_time_interval;
190 drbg->reseed_interval = slave_reseed_interval;
191 drbg->reseed_time_interval = slave_reseed_time_interval;
194 if (RAND_DRBG_set(drbg, type, flags) == 0)
197 if (parent != NULL) {
198 rand_drbg_lock(parent);
199 if (drbg->strength > parent->strength) {
201 * We currently don't support the algorithm from NIST SP 800-90C
202 * 10.1.2 to use a weaker DRBG as source
204 rand_drbg_unlock(parent);
205 RANDerr(RAND_F_RAND_DRBG_NEW, RAND_R_PARENT_STRENGTH_TOO_WEAK);
208 rand_drbg_unlock(parent);
211 if (!RAND_DRBG_set_callbacks(drbg, rand_drbg_get_entropy,
212 rand_drbg_cleanup_entropy,
220 OPENSSL_secure_free(drbg);
227 RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent)
229 return rand_drbg_new(0, type, flags, parent);
232 RAND_DRBG *RAND_DRBG_secure_new(int type, unsigned int flags, RAND_DRBG *parent)
234 return rand_drbg_new(1, type, flags, parent);
238 * Uninstantiate |drbg| and free all memory.
240 void RAND_DRBG_free(RAND_DRBG *drbg)
245 if (drbg->meth != NULL)
246 drbg->meth->uninstantiate(drbg);
247 CRYPTO_THREAD_lock_free(drbg->lock);
248 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
251 OPENSSL_secure_clear_free(drbg, sizeof(*drbg));
253 OPENSSL_clear_free(drbg, sizeof(*drbg));
257 * Instantiate |drbg|, after it has been initialized. Use |pers| and
258 * |perslen| as prediction-resistance input.
260 * Requires that drbg->lock is already locked for write, if non-null.
262 * Returns 1 on success, 0 on failure.
264 int RAND_DRBG_instantiate(RAND_DRBG *drbg,
265 const unsigned char *pers, size_t perslen)
267 unsigned char *nonce = NULL, *entropy = NULL;
268 size_t noncelen = 0, entropylen = 0;
270 if (perslen > drbg->max_perslen) {
271 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
272 RAND_R_PERSONALISATION_STRING_TOO_LONG);
276 if (drbg->meth == NULL)
278 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
279 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED);
283 if (drbg->state != DRBG_UNINITIALISED) {
284 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
285 drbg->state == DRBG_ERROR ? RAND_R_IN_ERROR_STATE
286 : RAND_R_ALREADY_INSTANTIATED);
290 drbg->state = DRBG_ERROR;
291 if (drbg->get_entropy != NULL)
292 entropylen = drbg->get_entropy(drbg, &entropy, drbg->strength,
293 drbg->min_entropylen,
294 drbg->max_entropylen, 0);
295 if (entropylen < drbg->min_entropylen
296 || entropylen > drbg->max_entropylen) {
297 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_ENTROPY);
301 if (drbg->max_noncelen > 0 && drbg->get_nonce != NULL) {
302 noncelen = drbg->get_nonce(drbg, &nonce, drbg->strength / 2,
303 drbg->min_noncelen, drbg->max_noncelen);
304 if (noncelen < drbg->min_noncelen || noncelen > drbg->max_noncelen) {
305 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
306 RAND_R_ERROR_RETRIEVING_NONCE);
311 if (!drbg->meth->instantiate(drbg, entropy, entropylen,
312 nonce, noncelen, pers, perslen)) {
313 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_INSTANTIATING_DRBG);
317 drbg->state = DRBG_READY;
318 drbg->generate_counter = 0;
319 drbg->reseed_time = time(NULL);
320 if (drbg->reseed_counter > 0) {
321 if (drbg->parent == NULL)
322 drbg->reseed_counter++;
324 drbg->reseed_counter = drbg->parent->reseed_counter;
328 if (entropy != NULL && drbg->cleanup_entropy != NULL)
329 drbg->cleanup_entropy(drbg, entropy, entropylen);
330 if (nonce != NULL && drbg->cleanup_nonce!= NULL )
331 drbg->cleanup_nonce(drbg, nonce, noncelen);
332 if (drbg->pool != NULL) {
333 if (drbg->state == DRBG_READY) {
334 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
335 RAND_R_ERROR_ENTROPY_POOL_WAS_IGNORED);
336 drbg->state = DRBG_ERROR;
338 rand_pool_free(drbg->pool);
341 if (drbg->state == DRBG_READY)
347 * Uninstantiate |drbg|. Must be instantiated before it can be used.
349 * Requires that drbg->lock is already locked for write, if non-null.
351 * Returns 1 on success, 0 on failure.
353 int RAND_DRBG_uninstantiate(RAND_DRBG *drbg)
355 if (drbg->meth == NULL)
357 RANDerr(RAND_F_RAND_DRBG_UNINSTANTIATE,
358 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED);
362 /* Clear the entire drbg->ctr struct, then reset some important
363 * members of the drbg->ctr struct (e.g. keysize, df_ks) to their
366 drbg->meth->uninstantiate(drbg);
367 return RAND_DRBG_set(drbg, drbg->type, drbg->flags);
371 * Reseed |drbg|, mixing in the specified data
373 * Requires that drbg->lock is already locked for write, if non-null.
375 * Returns 1 on success, 0 on failure.
377 int RAND_DRBG_reseed(RAND_DRBG *drbg,
378 const unsigned char *adin, size_t adinlen,
379 int prediction_resistance)
381 unsigned char *entropy = NULL;
382 size_t entropylen = 0;
384 if (drbg->state == DRBG_ERROR) {
385 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_IN_ERROR_STATE);
388 if (drbg->state == DRBG_UNINITIALISED) {
389 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_NOT_INSTANTIATED);
395 else if (adinlen > drbg->max_adinlen) {
396 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
400 drbg->state = DRBG_ERROR;
401 if (drbg->get_entropy != NULL)
402 entropylen = drbg->get_entropy(drbg, &entropy, drbg->strength,
403 drbg->min_entropylen,
404 drbg->max_entropylen,
405 prediction_resistance);
406 if (entropylen < drbg->min_entropylen
407 || entropylen > drbg->max_entropylen) {
408 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ERROR_RETRIEVING_ENTROPY);
412 if (!drbg->meth->reseed(drbg, entropy, entropylen, adin, adinlen))
415 drbg->state = DRBG_READY;
416 drbg->generate_counter = 0;
417 drbg->reseed_time = time(NULL);
418 if (drbg->reseed_counter > 0) {
419 if (drbg->parent == NULL)
420 drbg->reseed_counter++;
422 drbg->reseed_counter = drbg->parent->reseed_counter;
426 if (entropy != NULL && drbg->cleanup_entropy != NULL)
427 drbg->cleanup_entropy(drbg, entropy, entropylen);
428 if (drbg->state == DRBG_READY)
434 * Restart |drbg|, using the specified entropy or additional input
436 * Tries its best to get the drbg instantiated by all means,
437 * regardless of its current state.
439 * Optionally, a |buffer| of |len| random bytes can be passed,
440 * which is assumed to contain at least |entropy| bits of entropy.
442 * If |entropy| > 0, the buffer content is used as entropy input.
444 * If |entropy| == 0, the buffer content is used as additional input
446 * Returns 1 on success, 0 on failure.
448 * This function is used internally only.
450 int rand_drbg_restart(RAND_DRBG *drbg,
451 const unsigned char *buffer, size_t len, size_t entropy)
454 const unsigned char *adin = NULL;
457 if (drbg->pool != NULL) {
458 RANDerr(RAND_F_RAND_DRBG_RESTART, ERR_R_INTERNAL_ERROR);
459 rand_pool_free(drbg->pool);
463 if (buffer != NULL) {
465 if (drbg->max_entropylen < len) {
466 RANDerr(RAND_F_RAND_DRBG_RESTART,
467 RAND_R_ENTROPY_INPUT_TOO_LONG);
471 if (entropy > 8 * len) {
472 RANDerr(RAND_F_RAND_DRBG_RESTART, RAND_R_ENTROPY_OUT_OF_RANGE);
476 /* will be picked up by the rand_drbg_get_entropy() callback */
477 drbg->pool = rand_pool_new(entropy, len, len);
478 if (drbg->pool == NULL)
481 rand_pool_add(drbg->pool, buffer, len, entropy);
483 if (drbg->max_adinlen < len) {
484 RANDerr(RAND_F_RAND_DRBG_RESTART,
485 RAND_R_ADDITIONAL_INPUT_TOO_LONG);
493 /* repair error state */
494 if (drbg->state == DRBG_ERROR)
495 RAND_DRBG_uninstantiate(drbg);
497 /* repair uninitialized state */
498 if (drbg->state == DRBG_UNINITIALISED) {
499 /* reinstantiate drbg */
500 RAND_DRBG_instantiate(drbg,
501 (const unsigned char *) ossl_pers_string,
502 sizeof(ossl_pers_string) - 1);
503 /* already reseeded. prevent second reseeding below */
504 reseeded = (drbg->state == DRBG_READY);
507 /* refresh current state if entropy or additional input has been provided */
508 if (drbg->state == DRBG_READY) {
511 * mix in additional input without reseeding
513 * Similar to RAND_DRBG_reseed(), but the provided additional
514 * data |adin| is mixed into the current state without pulling
515 * entropy from the trusted entropy source using get_entropy().
516 * This is not a reseeding in the strict sense of NIST SP 800-90A.
518 drbg->meth->reseed(drbg, adin, adinlen, NULL, 0);
519 } else if (reseeded == 0) {
520 /* do a full reseeding if it has not been done yet above */
521 RAND_DRBG_reseed(drbg, NULL, 0, 0);
525 /* check whether a given entropy pool was cleared properly during reseed */
526 if (drbg->pool != NULL) {
527 drbg->state = DRBG_ERROR;
528 RANDerr(RAND_F_RAND_DRBG_RESTART, ERR_R_INTERNAL_ERROR);
529 rand_pool_free(drbg->pool);
534 return drbg->state == DRBG_READY;
538 * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
539 * to or if |prediction_resistance| is set. Additional input can be
540 * sent in |adin| and |adinlen|.
542 * Requires that drbg->lock is already locked for write, if non-null.
544 * Returns 1 on success, 0 on failure.
547 int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,
548 int prediction_resistance,
549 const unsigned char *adin, size_t adinlen)
551 int reseed_required = 0;
553 if (drbg->state != DRBG_READY) {
554 /* try to recover from previous errors */
555 rand_drbg_restart(drbg, NULL, 0, 0);
557 if (drbg->state == DRBG_ERROR) {
558 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_IN_ERROR_STATE);
561 if (drbg->state == DRBG_UNINITIALISED) {
562 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_NOT_INSTANTIATED);
567 if (outlen > drbg->max_request) {
568 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG);
571 if (adinlen > drbg->max_adinlen) {
572 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
576 if (drbg->fork_count != rand_fork_count) {
577 drbg->fork_count = rand_fork_count;
581 if (drbg->reseed_interval > 0) {
582 if (drbg->generate_counter >= drbg->reseed_interval)
585 if (drbg->reseed_time_interval > 0) {
586 time_t now = time(NULL);
587 if (now < drbg->reseed_time
588 || now - drbg->reseed_time >= drbg->reseed_time_interval)
591 if (drbg->reseed_counter > 0 && drbg->parent != NULL) {
592 if (drbg->reseed_counter != drbg->parent->reseed_counter)
596 if (reseed_required || prediction_resistance) {
597 if (!RAND_DRBG_reseed(drbg, adin, adinlen, prediction_resistance)) {
598 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_RESEED_ERROR);
605 if (!drbg->meth->generate(drbg, out, outlen, adin, adinlen)) {
606 drbg->state = DRBG_ERROR;
607 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_GENERATE_ERROR);
611 drbg->generate_counter++;
617 * Generates |outlen| random bytes and stores them in |out|. It will
618 * using the given |drbg| to generate the bytes.
620 * Requires that drbg->lock is already locked for write, if non-null.
622 * Returns 1 on success 0 on failure.
624 int RAND_DRBG_bytes(RAND_DRBG *drbg, unsigned char *out, size_t outlen)
626 unsigned char *additional = NULL;
627 size_t additional_len;
631 additional_len = rand_drbg_get_additional_data(&additional, drbg->max_adinlen);
633 for ( ; outlen > 0; outlen -= chunk, out += chunk) {
635 if (chunk > drbg->max_request)
636 chunk = drbg->max_request;
637 ret = RAND_DRBG_generate(drbg, out, chunk, 0, additional, additional_len);
644 if (additional_len != 0)
645 OPENSSL_secure_clear_free(additional, additional_len);
651 * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
653 * Setting the callbacks is allowed only if the drbg has not been
654 * initialized yet. Otherwise, the operation will fail.
656 * Returns 1 on success, 0 on failure.
658 int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
659 RAND_DRBG_get_entropy_fn get_entropy,
660 RAND_DRBG_cleanup_entropy_fn cleanup_entropy,
661 RAND_DRBG_get_nonce_fn get_nonce,
662 RAND_DRBG_cleanup_nonce_fn cleanup_nonce)
664 if (drbg->state != DRBG_UNINITIALISED)
666 drbg->get_entropy = get_entropy;
667 drbg->cleanup_entropy = cleanup_entropy;
668 drbg->get_nonce = get_nonce;
669 drbg->cleanup_nonce = cleanup_nonce;
674 * Set the reseed interval.
676 * The drbg will reseed automatically whenever the number of generate
677 * requests exceeds the given reseed interval. If the reseed interval
678 * is 0, then this feature is disabled.
680 * Returns 1 on success, 0 on failure.
682 int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval)
684 if (interval > MAX_RESEED_INTERVAL)
686 drbg->reseed_interval = interval;
691 * Set the reseed time interval.
693 * The drbg will reseed automatically whenever the time elapsed since
694 * the last reseeding exceeds the given reseed time interval. For safety,
695 * a reseeding will also occur if the clock has been reset to a smaller
698 * Returns 1 on success, 0 on failure.
700 int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval)
702 if (interval > MAX_RESEED_TIME_INTERVAL)
704 drbg->reseed_time_interval = interval;
709 * Set the default values for reseed (time) intervals of new DRBG instances
711 * The default values can be set independently for master DRBG instances
712 * (without a parent) and slave DRBG instances (with parent).
714 * Returns 1 on success, 0 on failure.
717 int RAND_DRBG_set_reseed_defaults(
718 unsigned int _master_reseed_interval,
719 unsigned int _slave_reseed_interval,
720 time_t _master_reseed_time_interval,
721 time_t _slave_reseed_time_interval
724 if (_master_reseed_interval > MAX_RESEED_INTERVAL
725 || _slave_reseed_interval > MAX_RESEED_INTERVAL)
728 if (_master_reseed_time_interval > MAX_RESEED_TIME_INTERVAL
729 || _slave_reseed_time_interval > MAX_RESEED_TIME_INTERVAL)
732 master_reseed_interval = _master_reseed_interval;
733 slave_reseed_interval = _slave_reseed_interval;
735 master_reseed_time_interval = _master_reseed_time_interval;
736 slave_reseed_time_interval = _slave_reseed_time_interval;
742 * Locks the given drbg. Locking a drbg which does not have locking
743 * enabled is considered a successful no-op.
745 * Returns 1 on success, 0 on failure.
747 int rand_drbg_lock(RAND_DRBG *drbg)
749 if (drbg->lock != NULL)
750 return CRYPTO_THREAD_write_lock(drbg->lock);
756 * Unlocks the given drbg. Unlocking a drbg which does not have locking
757 * enabled is considered a successful no-op.
759 * Returns 1 on success, 0 on failure.
761 int rand_drbg_unlock(RAND_DRBG *drbg)
763 if (drbg->lock != NULL)
764 return CRYPTO_THREAD_unlock(drbg->lock);
770 * Enables locking for the given drbg
772 * Locking can only be enabled if the random generator
773 * is in the uninitialized state.
775 * Returns 1 on success, 0 on failure.
777 int rand_drbg_enable_locking(RAND_DRBG *drbg)
779 if (drbg->state != DRBG_UNINITIALISED) {
780 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
781 RAND_R_DRBG_ALREADY_INITIALIZED);
785 if (drbg->lock == NULL) {
786 if (drbg->parent != NULL && drbg->parent->lock == NULL) {
787 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
788 RAND_R_PARENT_LOCKING_NOT_ENABLED);
792 drbg->lock = CRYPTO_THREAD_lock_new();
793 if (drbg->lock == NULL) {
794 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
795 RAND_R_FAILED_TO_CREATE_LOCK);
804 * Get and set the EXDATA
806 int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg)
808 return CRYPTO_set_ex_data(&drbg->ex_data, idx, arg);
811 void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx)
813 return CRYPTO_get_ex_data(&drbg->ex_data, idx);
818 * The following functions provide a RAND_METHOD that works on the
819 * global DRBG. They lock.
823 * Allocates a new global DRBG on the secure heap (if enabled) and
824 * initializes it with default settings.
826 * Returns a pointer to the new DRBG instance on success, NULL on failure.
828 static RAND_DRBG *drbg_setup(RAND_DRBG *parent)
832 drbg = RAND_DRBG_secure_new(rand_drbg_type, rand_drbg_flags, parent);
836 /* Only the master DRBG needs to have a lock */
837 if (parent == NULL && rand_drbg_enable_locking(drbg) == 0)
840 /* enable seed propagation */
841 drbg->reseed_counter = 1;
844 * Ignore instantiation error so support just-in-time instantiation.
846 * The state of the drbg will be checked in RAND_DRBG_generate() and
847 * an automatic recovery is attempted.
849 RAND_DRBG_instantiate(drbg,
850 (const unsigned char *) ossl_pers_string,
851 sizeof(ossl_pers_string) - 1);
855 RAND_DRBG_free(drbg);
860 * Initialize the global DRBGs on first use.
861 * Returns 1 on success, 0 on failure.
863 DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init)
868 * ensure that libcrypto is initialized, otherwise the
869 * DRBG locks are not cleaned up properly
871 if (!OPENSSL_init_crypto(0, NULL))
874 ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND);
876 master_drbg = drbg_setup(NULL);
878 ret &= CRYPTO_THREAD_init_local(&private_drbg, NULL);
879 ret &= CRYPTO_THREAD_init_local(&public_drbg, NULL);
881 if (master_drbg == NULL || ret == 0)
887 /* Clean up the global DRBGs before exit */
888 void rand_drbg_cleanup_int(void)
890 RAND_DRBG_free(master_drbg);
893 CRYPTO_THREAD_cleanup_local(&private_drbg);
894 CRYPTO_THREAD_cleanup_local(&public_drbg);
897 void drbg_delete_thread_state()
901 drbg = CRYPTO_THREAD_get_local(&public_drbg);
902 RAND_DRBG_free(drbg);
904 drbg = CRYPTO_THREAD_get_local(&private_drbg);
905 RAND_DRBG_free(drbg);
908 /* Implements the default OpenSSL RAND_bytes() method */
909 static int drbg_bytes(unsigned char *out, int count)
912 RAND_DRBG *drbg = RAND_DRBG_get0_public();
917 ret = RAND_DRBG_bytes(drbg, out, count);
922 /* Implements the default OpenSSL RAND_add() method */
923 static int drbg_add(const void *buf, int num, double randomness)
926 RAND_DRBG *drbg = RAND_DRBG_get0_master();
931 if (num < 0 || randomness < 0.0)
934 if (randomness > (double)drbg->max_entropylen) {
936 * The purpose of this check is to bound |randomness| by a
937 * relatively small value in order to prevent an integer
938 * overflow when multiplying by 8 in the rand_drbg_restart()
944 rand_drbg_lock(drbg);
945 ret = rand_drbg_restart(drbg, buf,
946 (size_t)(unsigned int)num,
947 (size_t)(8*randomness));
948 rand_drbg_unlock(drbg);
953 /* Implements the default OpenSSL RAND_seed() method */
954 static int drbg_seed(const void *buf, int num)
956 return drbg_add(buf, num, num);
959 /* Implements the default OpenSSL RAND_status() method */
960 static int drbg_status(void)
963 RAND_DRBG *drbg = RAND_DRBG_get0_master();
968 rand_drbg_lock(drbg);
969 ret = drbg->state == DRBG_READY ? 1 : 0;
970 rand_drbg_unlock(drbg);
975 * Get the master DRBG.
976 * Returns pointer to the DRBG on success, NULL on failure.
979 RAND_DRBG *RAND_DRBG_get0_master(void)
981 if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
988 * Get the public DRBG.
989 * Returns pointer to the DRBG on success, NULL on failure.
991 RAND_DRBG *RAND_DRBG_get0_public(void)
995 if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
998 drbg = CRYPTO_THREAD_get_local(&public_drbg);
1000 ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND);
1001 drbg = drbg_setup(master_drbg);
1002 CRYPTO_THREAD_set_local(&public_drbg, drbg);
1008 * Get the private DRBG.
1009 * Returns pointer to the DRBG on success, NULL on failure.
1011 RAND_DRBG *RAND_DRBG_get0_private(void)
1015 if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
1018 drbg = CRYPTO_THREAD_get_local(&private_drbg);
1020 ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND);
1021 drbg = drbg_setup(master_drbg);
1022 CRYPTO_THREAD_set_local(&private_drbg, drbg);
1027 RAND_METHOD rand_meth = {
1036 RAND_METHOD *RAND_OpenSSL(void)