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 (drbg->type != 0 && (type != drbg->type || flags != drbg->flags)) {
162 drbg->meth->uninstantiate(drbg);
163 rand_pool_free(drbg->adin_pool);
164 drbg->adin_pool = NULL;
167 drbg->state = DRBG_UNINITIALISED;
172 /* Uninitialized; that's okay. */
175 } else if (is_ctr(type)) {
176 ret = drbg_ctr_init(drbg);
177 } else if (is_digest(type)) {
178 if (flags & RAND_DRBG_FLAG_HMAC)
179 ret = drbg_hmac_init(drbg);
181 ret = drbg_hash_init(drbg);
183 RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_UNSUPPORTED_DRBG_TYPE);
188 RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_ERROR_INITIALISING_DRBG);
193 * Set/initialize default |type| and |flag| for new drbg instances.
195 * Returns 1 on success, 0 on failure.
197 int RAND_DRBG_set_defaults(int type, unsigned int flags)
200 if (!(is_digest(type) || is_ctr(type))) {
201 RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_TYPE);
205 if ((flags & ~rand_drbg_used_flags) != 0) {
206 RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_FLAGS);
210 all = ((flags & RAND_DRBG_TYPE_FLAGS) == 0);
211 if (all || (flags & RAND_DRBG_FLAG_MASTER) != 0) {
212 rand_drbg_type[RAND_DRBG_TYPE_MASTER] = type;
213 rand_drbg_flags[RAND_DRBG_TYPE_MASTER] = flags | RAND_DRBG_FLAG_MASTER;
215 if (all || (flags & RAND_DRBG_FLAG_PUBLIC) != 0) {
216 rand_drbg_type[RAND_DRBG_TYPE_PUBLIC] = type;
217 rand_drbg_flags[RAND_DRBG_TYPE_PUBLIC] = flags | RAND_DRBG_FLAG_PUBLIC;
219 if (all || (flags & RAND_DRBG_FLAG_PRIVATE) != 0) {
220 rand_drbg_type[RAND_DRBG_TYPE_PRIVATE] = type;
221 rand_drbg_flags[RAND_DRBG_TYPE_PRIVATE] = flags | RAND_DRBG_FLAG_PRIVATE;
228 * Allocate memory and initialize a new DRBG. The DRBG is allocated on
229 * the secure heap if |secure| is nonzero and the secure heap is enabled.
230 * The |parent|, if not NULL, will be used as random source for reseeding.
232 * Returns a pointer to the new DRBG instance on success, NULL on failure.
234 static RAND_DRBG *rand_drbg_new(int secure,
239 RAND_DRBG *drbg = secure ?
240 OPENSSL_secure_zalloc(sizeof(*drbg)) : OPENSSL_zalloc(sizeof(*drbg));
243 RANDerr(RAND_F_RAND_DRBG_NEW, ERR_R_MALLOC_FAILURE);
247 drbg->secure = secure && CRYPTO_secure_allocated(drbg);
248 drbg->fork_count = rand_fork_count;
249 drbg->parent = parent;
251 if (parent == NULL) {
252 drbg->get_entropy = rand_drbg_get_entropy;
253 drbg->cleanup_entropy = rand_drbg_cleanup_entropy;
254 #ifndef RAND_DRBG_GET_RANDOM_NONCE
255 drbg->get_nonce = rand_drbg_get_nonce;
256 drbg->cleanup_nonce = rand_drbg_cleanup_nonce;
259 drbg->reseed_interval = master_reseed_interval;
260 drbg->reseed_time_interval = master_reseed_time_interval;
262 drbg->get_entropy = rand_drbg_get_entropy;
263 drbg->cleanup_entropy = rand_drbg_cleanup_entropy;
265 * Do not provide nonce callbacks, the child DRBGs will
266 * obtain their nonce using random bits from the parent.
269 drbg->reseed_interval = slave_reseed_interval;
270 drbg->reseed_time_interval = slave_reseed_time_interval;
273 if (RAND_DRBG_set(drbg, type, flags) == 0)
276 if (parent != NULL) {
277 rand_drbg_lock(parent);
278 if (drbg->strength > parent->strength) {
280 * We currently don't support the algorithm from NIST SP 800-90C
281 * 10.1.2 to use a weaker DRBG as source
283 rand_drbg_unlock(parent);
284 RANDerr(RAND_F_RAND_DRBG_NEW, RAND_R_PARENT_STRENGTH_TOO_WEAK);
287 rand_drbg_unlock(parent);
294 OPENSSL_secure_free(drbg);
301 RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent)
303 return rand_drbg_new(0, type, flags, parent);
306 RAND_DRBG *RAND_DRBG_secure_new(int type, unsigned int flags, RAND_DRBG *parent)
308 return rand_drbg_new(1, type, flags, parent);
312 * Uninstantiate |drbg| and free all memory.
314 void RAND_DRBG_free(RAND_DRBG *drbg)
319 if (drbg->meth != NULL)
320 drbg->meth->uninstantiate(drbg);
321 rand_pool_free(drbg->adin_pool);
322 CRYPTO_THREAD_lock_free(drbg->lock);
323 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
326 OPENSSL_secure_clear_free(drbg, sizeof(*drbg));
328 OPENSSL_clear_free(drbg, sizeof(*drbg));
332 * Instantiate |drbg|, after it has been initialized. Use |pers| and
333 * |perslen| as prediction-resistance input.
335 * Requires that drbg->lock is already locked for write, if non-null.
337 * Returns 1 on success, 0 on failure.
339 int RAND_DRBG_instantiate(RAND_DRBG *drbg,
340 const unsigned char *pers, size_t perslen)
342 unsigned char *nonce = NULL, *entropy = NULL;
343 size_t noncelen = 0, entropylen = 0;
344 size_t min_entropy = drbg->strength;
345 size_t min_entropylen = drbg->min_entropylen;
346 size_t max_entropylen = drbg->max_entropylen;
348 if (perslen > drbg->max_perslen) {
349 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
350 RAND_R_PERSONALISATION_STRING_TOO_LONG);
354 if (drbg->meth == NULL) {
355 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
356 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED);
360 if (drbg->state != DRBG_UNINITIALISED) {
361 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
362 drbg->state == DRBG_ERROR ? RAND_R_IN_ERROR_STATE
363 : RAND_R_ALREADY_INSTANTIATED);
367 drbg->state = DRBG_ERROR;
370 * NIST SP800-90Ar1 section 9.1 says you can combine getting the entropy
371 * and nonce in 1 call by increasing the entropy with 50% and increasing
372 * the minimum length to accomadate the length of the nonce.
373 * We do this in case a nonce is require and get_nonce is NULL.
375 if (drbg->min_noncelen > 0 && drbg->get_nonce == NULL) {
376 min_entropy += drbg->strength / 2;
377 min_entropylen += drbg->min_noncelen;
378 max_entropylen += drbg->max_noncelen;
381 drbg->reseed_next_counter = tsan_load(&drbg->reseed_prop_counter);
382 if (drbg->reseed_next_counter) {
383 drbg->reseed_next_counter++;
384 if(!drbg->reseed_next_counter)
385 drbg->reseed_next_counter = 1;
388 if (drbg->get_entropy != NULL)
389 entropylen = drbg->get_entropy(drbg, &entropy, min_entropy,
390 min_entropylen, max_entropylen, 0);
391 if (entropylen < min_entropylen
392 || entropylen > max_entropylen) {
393 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_ENTROPY);
397 if (drbg->min_noncelen > 0 && drbg->get_nonce != NULL) {
398 noncelen = drbg->get_nonce(drbg, &nonce, drbg->strength / 2,
399 drbg->min_noncelen, drbg->max_noncelen);
400 if (noncelen < drbg->min_noncelen || noncelen > drbg->max_noncelen) {
401 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_NONCE);
406 if (!drbg->meth->instantiate(drbg, entropy, entropylen,
407 nonce, noncelen, pers, perslen)) {
408 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_INSTANTIATING_DRBG);
412 drbg->state = DRBG_READY;
413 drbg->reseed_gen_counter = 1;
414 drbg->reseed_time = time(NULL);
415 tsan_store(&drbg->reseed_prop_counter, drbg->reseed_next_counter);
418 if (entropy != NULL && drbg->cleanup_entropy != NULL)
419 drbg->cleanup_entropy(drbg, entropy, entropylen);
420 if (nonce != NULL && drbg->cleanup_nonce != NULL)
421 drbg->cleanup_nonce(drbg, nonce, noncelen);
422 if (drbg->state == DRBG_READY)
428 * Uninstantiate |drbg|. Must be instantiated before it can be used.
430 * Requires that drbg->lock is already locked for write, if non-null.
432 * Returns 1 on success, 0 on failure.
434 int RAND_DRBG_uninstantiate(RAND_DRBG *drbg)
436 int index = -1, type, flags;
437 if (drbg->meth == NULL) {
438 RANDerr(RAND_F_RAND_DRBG_UNINSTANTIATE,
439 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED);
443 /* Clear the entire drbg->ctr struct, then reset some important
444 * members of the drbg->ctr struct (e.g. keysize, df_ks) to their
447 drbg->meth->uninstantiate(drbg);
449 /* The reset uses the default values for type and flags */
450 if (drbg->flags & RAND_DRBG_FLAG_MASTER)
451 index = RAND_DRBG_TYPE_MASTER;
452 else if (drbg->flags & RAND_DRBG_FLAG_PRIVATE)
453 index = RAND_DRBG_TYPE_PRIVATE;
454 else if (drbg->flags & RAND_DRBG_FLAG_PUBLIC)
455 index = RAND_DRBG_TYPE_PUBLIC;
458 flags = rand_drbg_flags[index];
459 type = rand_drbg_type[index];
464 return RAND_DRBG_set(drbg, type, flags);
468 * Reseed |drbg|, mixing in the specified data
470 * Requires that drbg->lock is already locked for write, if non-null.
472 * Returns 1 on success, 0 on failure.
474 int RAND_DRBG_reseed(RAND_DRBG *drbg,
475 const unsigned char *adin, size_t adinlen,
476 int prediction_resistance)
478 unsigned char *entropy = NULL;
479 size_t entropylen = 0;
481 if (drbg->state == DRBG_ERROR) {
482 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_IN_ERROR_STATE);
485 if (drbg->state == DRBG_UNINITIALISED) {
486 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_NOT_INSTANTIATED);
492 } else if (adinlen > drbg->max_adinlen) {
493 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
497 drbg->state = DRBG_ERROR;
499 drbg->reseed_next_counter = tsan_load(&drbg->reseed_prop_counter);
500 if (drbg->reseed_next_counter) {
501 drbg->reseed_next_counter++;
502 if(!drbg->reseed_next_counter)
503 drbg->reseed_next_counter = 1;
506 if (drbg->get_entropy != NULL)
507 entropylen = drbg->get_entropy(drbg, &entropy, drbg->strength,
508 drbg->min_entropylen,
509 drbg->max_entropylen,
510 prediction_resistance);
511 if (entropylen < drbg->min_entropylen
512 || entropylen > drbg->max_entropylen) {
513 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ERROR_RETRIEVING_ENTROPY);
517 if (!drbg->meth->reseed(drbg, entropy, entropylen, adin, adinlen))
520 drbg->state = DRBG_READY;
521 drbg->reseed_gen_counter = 1;
522 drbg->reseed_time = time(NULL);
523 tsan_store(&drbg->reseed_prop_counter, drbg->reseed_next_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 drbg->state = DRBG_ERROR;
560 rand_pool_free(drbg->pool);
565 if (buffer != NULL) {
567 if (drbg->max_entropylen < len) {
568 RANDerr(RAND_F_RAND_DRBG_RESTART,
569 RAND_R_ENTROPY_INPUT_TOO_LONG);
570 drbg->state = DRBG_ERROR;
574 if (entropy > 8 * len) {
575 RANDerr(RAND_F_RAND_DRBG_RESTART, RAND_R_ENTROPY_OUT_OF_RANGE);
576 drbg->state = DRBG_ERROR;
580 /* will be picked up by the rand_drbg_get_entropy() callback */
581 drbg->pool = rand_pool_attach(buffer, len, entropy);
582 if (drbg->pool == NULL)
585 if (drbg->max_adinlen < len) {
586 RANDerr(RAND_F_RAND_DRBG_RESTART,
587 RAND_R_ADDITIONAL_INPUT_TOO_LONG);
588 drbg->state = DRBG_ERROR;
596 /* repair error state */
597 if (drbg->state == DRBG_ERROR)
598 RAND_DRBG_uninstantiate(drbg);
600 /* repair uninitialized state */
601 if (drbg->state == DRBG_UNINITIALISED) {
602 /* reinstantiate drbg */
603 RAND_DRBG_instantiate(drbg,
604 (const unsigned char *) ossl_pers_string,
605 sizeof(ossl_pers_string) - 1);
606 /* already reseeded. prevent second reseeding below */
607 reseeded = (drbg->state == DRBG_READY);
610 /* refresh current state if entropy or additional input has been provided */
611 if (drbg->state == DRBG_READY) {
614 * mix in additional input without reseeding
616 * Similar to RAND_DRBG_reseed(), but the provided additional
617 * data |adin| is mixed into the current state without pulling
618 * entropy from the trusted entropy source using get_entropy().
619 * This is not a reseeding in the strict sense of NIST SP 800-90A.
621 drbg->meth->reseed(drbg, adin, adinlen, NULL, 0);
622 } else if (reseeded == 0) {
623 /* do a full reseeding if it has not been done yet above */
624 RAND_DRBG_reseed(drbg, NULL, 0, 0);
628 rand_pool_free(drbg->pool);
631 return drbg->state == DRBG_READY;
635 * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
636 * to or if |prediction_resistance| is set. Additional input can be
637 * sent in |adin| and |adinlen|.
639 * Requires that drbg->lock is already locked for write, if non-null.
641 * Returns 1 on success, 0 on failure.
644 int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,
645 int prediction_resistance,
646 const unsigned char *adin, size_t adinlen)
648 int reseed_required = 0;
650 if (drbg->state != DRBG_READY) {
651 /* try to recover from previous errors */
652 rand_drbg_restart(drbg, NULL, 0, 0);
654 if (drbg->state == DRBG_ERROR) {
655 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_IN_ERROR_STATE);
658 if (drbg->state == DRBG_UNINITIALISED) {
659 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_NOT_INSTANTIATED);
664 if (outlen > drbg->max_request) {
665 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG);
668 if (adinlen > drbg->max_adinlen) {
669 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
673 if (drbg->fork_count != rand_fork_count) {
674 drbg->fork_count = rand_fork_count;
678 if (drbg->reseed_interval > 0) {
679 if (drbg->reseed_gen_counter > drbg->reseed_interval)
682 if (drbg->reseed_time_interval > 0) {
683 time_t now = time(NULL);
684 if (now < drbg->reseed_time
685 || now - drbg->reseed_time >= drbg->reseed_time_interval)
688 if (drbg->parent != NULL) {
689 unsigned int reseed_counter = tsan_load(&drbg->reseed_prop_counter);
690 if (reseed_counter > 0
691 && tsan_load(&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 if (drbg->adin_pool == NULL) {
734 drbg->adin_pool = rand_pool_new(0, 0, drbg->max_adinlen);
735 if (drbg->adin_pool == NULL)
739 additional_len = rand_drbg_get_additional_data(drbg->adin_pool,
742 for ( ; outlen > 0; outlen -= chunk, out += chunk) {
744 if (chunk > drbg->max_request)
745 chunk = drbg->max_request;
746 ret = RAND_DRBG_generate(drbg, out, chunk, 0, additional, additional_len);
753 if (additional != NULL)
754 rand_drbg_cleanup_additional_data(drbg->adin_pool, additional);
760 * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
762 * Setting the callbacks is allowed only if the drbg has not been
763 * initialized yet. Otherwise, the operation will fail.
765 * Returns 1 on success, 0 on failure.
767 int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
768 RAND_DRBG_get_entropy_fn get_entropy,
769 RAND_DRBG_cleanup_entropy_fn cleanup_entropy,
770 RAND_DRBG_get_nonce_fn get_nonce,
771 RAND_DRBG_cleanup_nonce_fn cleanup_nonce)
773 if (drbg->state != DRBG_UNINITIALISED
774 || drbg->parent != NULL)
776 drbg->get_entropy = get_entropy;
777 drbg->cleanup_entropy = cleanup_entropy;
778 drbg->get_nonce = get_nonce;
779 drbg->cleanup_nonce = cleanup_nonce;
784 * Set the reseed interval.
786 * The drbg will reseed automatically whenever the number of generate
787 * requests exceeds the given reseed interval. If the reseed interval
788 * is 0, then this feature is disabled.
790 * Returns 1 on success, 0 on failure.
792 int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval)
794 if (interval > MAX_RESEED_INTERVAL)
796 drbg->reseed_interval = interval;
801 * Set the reseed time interval.
803 * The drbg will reseed automatically whenever the time elapsed since
804 * the last reseeding exceeds the given reseed time interval. For safety,
805 * a reseeding will also occur if the clock has been reset to a smaller
808 * Returns 1 on success, 0 on failure.
810 int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval)
812 if (interval > MAX_RESEED_TIME_INTERVAL)
814 drbg->reseed_time_interval = interval;
819 * Set the default values for reseed (time) intervals of new DRBG instances
821 * The default values can be set independently for master DRBG instances
822 * (without a parent) and slave DRBG instances (with parent).
824 * Returns 1 on success, 0 on failure.
827 int RAND_DRBG_set_reseed_defaults(
828 unsigned int _master_reseed_interval,
829 unsigned int _slave_reseed_interval,
830 time_t _master_reseed_time_interval,
831 time_t _slave_reseed_time_interval
834 if (_master_reseed_interval > MAX_RESEED_INTERVAL
835 || _slave_reseed_interval > MAX_RESEED_INTERVAL)
838 if (_master_reseed_time_interval > MAX_RESEED_TIME_INTERVAL
839 || _slave_reseed_time_interval > MAX_RESEED_TIME_INTERVAL)
842 master_reseed_interval = _master_reseed_interval;
843 slave_reseed_interval = _slave_reseed_interval;
845 master_reseed_time_interval = _master_reseed_time_interval;
846 slave_reseed_time_interval = _slave_reseed_time_interval;
852 * Locks the given drbg. Locking a drbg which does not have locking
853 * enabled is considered a successful no-op.
855 * Returns 1 on success, 0 on failure.
857 int rand_drbg_lock(RAND_DRBG *drbg)
859 if (drbg->lock != NULL)
860 return CRYPTO_THREAD_write_lock(drbg->lock);
866 * Unlocks the given drbg. Unlocking a drbg which does not have locking
867 * enabled is considered a successful no-op.
869 * Returns 1 on success, 0 on failure.
871 int rand_drbg_unlock(RAND_DRBG *drbg)
873 if (drbg->lock != NULL)
874 return CRYPTO_THREAD_unlock(drbg->lock);
880 * Enables locking for the given drbg
882 * Locking can only be enabled if the random generator
883 * is in the uninitialized state.
885 * Returns 1 on success, 0 on failure.
887 int rand_drbg_enable_locking(RAND_DRBG *drbg)
889 if (drbg->state != DRBG_UNINITIALISED) {
890 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
891 RAND_R_DRBG_ALREADY_INITIALIZED);
895 if (drbg->lock == NULL) {
896 if (drbg->parent != NULL && drbg->parent->lock == NULL) {
897 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
898 RAND_R_PARENT_LOCKING_NOT_ENABLED);
902 drbg->lock = CRYPTO_THREAD_lock_new();
903 if (drbg->lock == NULL) {
904 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
905 RAND_R_FAILED_TO_CREATE_LOCK);
914 * Get and set the EXDATA
916 int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg)
918 return CRYPTO_set_ex_data(&drbg->ex_data, idx, arg);
921 void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx)
923 return CRYPTO_get_ex_data(&drbg->ex_data, idx);
928 * The following functions provide a RAND_METHOD that works on the
929 * global DRBG. They lock.
933 * Allocates a new global DRBG on the secure heap (if enabled) and
934 * initializes it with default settings.
936 * Returns a pointer to the new DRBG instance on success, NULL on failure.
938 static RAND_DRBG *drbg_setup(RAND_DRBG *parent, int drbg_type)
942 drbg = RAND_DRBG_secure_new(rand_drbg_type[drbg_type],
943 rand_drbg_flags[drbg_type], parent);
947 /* Only the master DRBG needs to have a lock */
948 if (parent == NULL && rand_drbg_enable_locking(drbg) == 0)
951 /* enable seed propagation */
952 tsan_store(&drbg->reseed_prop_counter, 1);
955 * Ignore instantiation error to support just-in-time instantiation.
957 * The state of the drbg will be checked in RAND_DRBG_generate() and
958 * an automatic recovery is attempted.
960 (void)RAND_DRBG_instantiate(drbg,
961 (const unsigned char *) ossl_pers_string,
962 sizeof(ossl_pers_string) - 1);
966 RAND_DRBG_free(drbg);
971 * Initialize the global DRBGs on first use.
972 * Returns 1 on success, 0 on failure.
974 DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init)
977 * ensure that libcrypto is initialized, otherwise the
978 * DRBG locks are not cleaned up properly
980 if (!OPENSSL_init_crypto(0, NULL))
983 if (!CRYPTO_THREAD_init_local(&private_drbg, NULL))
986 if (!CRYPTO_THREAD_init_local(&public_drbg, NULL))
989 master_drbg = drbg_setup(NULL, RAND_DRBG_TYPE_MASTER);
990 if (master_drbg == NULL)
996 CRYPTO_THREAD_cleanup_local(&public_drbg);
998 CRYPTO_THREAD_cleanup_local(&private_drbg);
1002 /* Clean up the global DRBGs before exit */
1003 void rand_drbg_cleanup_int(void)
1005 if (master_drbg != NULL) {
1006 RAND_DRBG_free(master_drbg);
1009 CRYPTO_THREAD_cleanup_local(&private_drbg);
1010 CRYPTO_THREAD_cleanup_local(&public_drbg);
1014 void drbg_delete_thread_state(void)
1018 drbg = CRYPTO_THREAD_get_local(&public_drbg);
1019 CRYPTO_THREAD_set_local(&public_drbg, NULL);
1020 RAND_DRBG_free(drbg);
1022 drbg = CRYPTO_THREAD_get_local(&private_drbg);
1023 CRYPTO_THREAD_set_local(&private_drbg, NULL);
1024 RAND_DRBG_free(drbg);
1027 /* Implements the default OpenSSL RAND_bytes() method */
1028 static int drbg_bytes(unsigned char *out, int count)
1031 RAND_DRBG *drbg = RAND_DRBG_get0_public();
1036 ret = RAND_DRBG_bytes(drbg, out, count);
1042 * Calculates the minimum length of a full entropy buffer
1043 * which is necessary to seed (i.e. instantiate) the DRBG
1046 * NOTE: There is a copy of this function in drbgtest.c.
1047 * If you change anything here, you need to update
1048 * the copy accordingly.
1050 static size_t rand_drbg_seedlen(RAND_DRBG *drbg)
1053 * If no os entropy source is available then RAND_seed(buffer, bufsize)
1054 * is expected to succeed if and only if the buffer length satisfies
1055 * the following requirements, which follow from the calculations
1056 * in RAND_DRBG_instantiate().
1058 size_t min_entropy = drbg->strength;
1059 size_t min_entropylen = drbg->min_entropylen;
1062 * Extra entropy for the random nonce in the absence of a
1063 * get_nonce callback, see comment in RAND_DRBG_instantiate().
1065 if (drbg->min_noncelen > 0 && drbg->get_nonce == NULL) {
1066 min_entropy += drbg->strength / 2;
1067 min_entropylen += drbg->min_noncelen;
1071 * Convert entropy requirement from bits to bytes
1072 * (dividing by 8 without rounding upwards, because
1073 * all entropy requirements are divisible by 8).
1077 /* Return a value that satisfies both requirements */
1078 return min_entropy > min_entropylen ? min_entropy : min_entropylen;
1081 /* Implements the default OpenSSL RAND_add() method */
1082 static int drbg_add(const void *buf, int num, double randomness)
1085 RAND_DRBG *drbg = RAND_DRBG_get0_master();
1092 if (num < 0 || randomness < 0.0)
1095 rand_drbg_lock(drbg);
1096 seedlen = rand_drbg_seedlen(drbg);
1098 buflen = (size_t)num;
1100 if (buflen < seedlen || randomness < (double) seedlen) {
1101 #if defined(OPENSSL_RAND_SEED_NONE)
1103 * If no os entropy source is available, a reseeding will fail
1104 * inevitably. So we use a trick to mix the buffer contents into
1105 * the DRBG state without forcing a reseeding: we generate a
1106 * dummy random byte, using the buffer content as additional data.
1107 * Note: This won't work with RAND_DRBG_FLAG_CTR_NO_DF.
1109 unsigned char dummy[1];
1111 ret = RAND_DRBG_generate(drbg, dummy, sizeof(dummy), 0, buf, buflen);
1112 rand_drbg_unlock(drbg);
1116 * If an os entropy source is avaible then we declare the buffer content
1117 * as additional data by setting randomness to zero and trigger a regular
1125 if (randomness > (double)seedlen) {
1127 * The purpose of this check is to bound |randomness| by a
1128 * relatively small value in order to prevent an integer
1129 * overflow when multiplying by 8 in the rand_drbg_restart()
1130 * call below. Note that randomness is measured in bytes,
1131 * not bits, so this value corresponds to eight times the
1132 * security strength.
1134 randomness = (double)seedlen;
1137 ret = rand_drbg_restart(drbg, buf, buflen, (size_t)(8 * randomness));
1138 rand_drbg_unlock(drbg);
1143 /* Implements the default OpenSSL RAND_seed() method */
1144 static int drbg_seed(const void *buf, int num)
1146 return drbg_add(buf, num, num);
1149 /* Implements the default OpenSSL RAND_status() method */
1150 static int drbg_status(void)
1153 RAND_DRBG *drbg = RAND_DRBG_get0_master();
1158 rand_drbg_lock(drbg);
1159 ret = drbg->state == DRBG_READY ? 1 : 0;
1160 rand_drbg_unlock(drbg);
1165 * Get the master DRBG.
1166 * Returns pointer to the DRBG on success, NULL on failure.
1169 RAND_DRBG *RAND_DRBG_get0_master(void)
1171 if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
1178 * Get the public DRBG.
1179 * Returns pointer to the DRBG on success, NULL on failure.
1181 RAND_DRBG *RAND_DRBG_get0_public(void)
1185 if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
1188 drbg = CRYPTO_THREAD_get_local(&public_drbg);
1190 if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND))
1192 drbg = drbg_setup(master_drbg, RAND_DRBG_TYPE_PUBLIC);
1193 CRYPTO_THREAD_set_local(&public_drbg, drbg);
1199 * Get the private DRBG.
1200 * Returns pointer to the DRBG on success, NULL on failure.
1202 RAND_DRBG *RAND_DRBG_get0_private(void)
1206 if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
1209 drbg = CRYPTO_THREAD_get_local(&private_drbg);
1211 if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND))
1213 drbg = drbg_setup(master_drbg, RAND_DRBG_TYPE_PRIVATE);
1214 CRYPTO_THREAD_set_local(&private_drbg, drbg);
1219 RAND_METHOD rand_meth = {
1228 RAND_METHOD *RAND_OpenSSL(void)