2 * Copyright 2011-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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>
14 #include <openssl/evp.h>
15 #include "crypto/rand.h"
16 #include <openssl/proverr.h>
17 #include "drbg_local.h"
18 #include "internal/thread_once.h"
19 #include "crypto/cryptlib.h"
20 #include "prov/seeding.h"
21 #include "crypto/rand_pool.h"
22 #include "prov/provider_ctx.h"
23 #include "prov/providercommon.h"
24 #include "crypto/context.h"
27 * Support framework for NIST SP 800-90A DRBG
29 * See manual page PROV_DRBG(7) for a general overview.
31 * The OpenSSL model is to have new and free functions, and that new
32 * does all initialization. That is not the NIST model, which has
33 * instantiation and un-instantiate, and re-use within a new/free
34 * lifecycle. (No doubt this comes from the desire to support hardware
35 * DRBG, where allocation of resources on something like an HSM is
36 * a much bigger deal than just re-setting an allocated resource.)
39 /* NIST SP 800-90A DRBG recommends the use of a personalization string. */
40 static const char ossl_pers_string[] = DRBG_DEFAULT_PERS_STRING;
42 static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch,
45 static int rand_drbg_restart(PROV_DRBG *drbg);
47 int ossl_drbg_lock(void *vctx)
49 PROV_DRBG *drbg = vctx;
51 if (drbg == NULL || drbg->lock == NULL)
53 return CRYPTO_THREAD_write_lock(drbg->lock);
56 void ossl_drbg_unlock(void *vctx)
58 PROV_DRBG *drbg = vctx;
60 if (drbg != NULL && drbg->lock != NULL)
61 CRYPTO_THREAD_unlock(drbg->lock);
64 static int ossl_drbg_lock_parent(PROV_DRBG *drbg)
66 void *parent = drbg->parent;
69 && drbg->parent_lock != NULL
70 && !drbg->parent_lock(parent)) {
71 ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED);
77 static void ossl_drbg_unlock_parent(PROV_DRBG *drbg)
79 void *parent = drbg->parent;
81 if (parent != NULL && drbg->parent_unlock != NULL)
82 drbg->parent_unlock(parent);
85 static int get_parent_strength(PROV_DRBG *drbg, unsigned int *str)
87 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
88 void *parent = drbg->parent;
91 if (drbg->parent_get_ctx_params == NULL) {
92 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH);
96 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, str);
97 if (!ossl_drbg_lock_parent(drbg)) {
98 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOCK_PARENT);
101 res = drbg->parent_get_ctx_params(parent, params);
102 ossl_drbg_unlock_parent(drbg);
104 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH);
110 static unsigned int get_parent_reseed_count(PROV_DRBG *drbg)
112 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
113 void *parent = drbg->parent;
116 *params = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_COUNTER, &r);
117 if (!ossl_drbg_lock_parent(drbg)) {
118 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOCK_PARENT);
121 if (!drbg->parent_get_ctx_params(parent, params))
123 ossl_drbg_unlock_parent(drbg);
127 r = tsan_load(&drbg->reseed_counter) - 2;
134 * Implements the get_entropy() callback
136 * If the DRBG has a parent, then the required amount of entropy input
137 * is fetched using the parent's ossl_prov_drbg_generate().
139 * Otherwise, the entropy is polled from the system entropy sources
140 * using ossl_pool_acquire_entropy().
142 * If a random pool has been added to the DRBG using RAND_add(), then
143 * its entropy will be used up first.
145 size_t ossl_drbg_get_seed(void *vdrbg, unsigned char **pout,
146 int entropy, size_t min_len,
147 size_t max_len, int prediction_resistance,
148 const unsigned char *adin, size_t adin_len)
150 PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
152 unsigned char *buffer;
154 /* Figure out how many bytes we need */
155 bytes_needed = entropy >= 0 ? (entropy + 7) / 8 : 0;
156 if (bytes_needed < min_len)
157 bytes_needed = min_len;
158 if (bytes_needed > max_len)
159 bytes_needed = max_len;
161 /* Allocate storage */
162 buffer = OPENSSL_secure_malloc(bytes_needed);
163 if (buffer == NULL) {
164 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
169 * Get random data. Include our DRBG address as
170 * additional input, in order to provide a distinction between
171 * different DRBG child instances.
173 * Note: using the sizeof() operator on a pointer triggers
174 * a warning in some static code analyzers, but it's
175 * intentional and correct here.
177 if (!ossl_prov_drbg_generate(drbg, buffer, bytes_needed,
178 drbg->strength, prediction_resistance,
179 (unsigned char *)&drbg, sizeof(drbg))) {
180 OPENSSL_secure_clear_free(buffer, bytes_needed);
181 ERR_raise(ERR_LIB_PROV, PROV_R_GENERATE_ERROR);
188 /* Implements the cleanup_entropy() callback */
189 void ossl_drbg_clear_seed(ossl_unused void *vdrbg,
190 unsigned char *out, size_t outlen)
192 OPENSSL_secure_clear_free(out, outlen);
195 static size_t get_entropy(PROV_DRBG *drbg, unsigned char **pout, int entropy,
196 size_t min_len, size_t max_len,
197 int prediction_resistance)
202 if (drbg->parent == NULL)
204 return ossl_crngt_get_entropy(drbg, pout, entropy, min_len, max_len,
205 prediction_resistance);
207 return ossl_prov_get_entropy(drbg->provctx, pout, entropy, min_len,
211 if (drbg->parent_get_seed == NULL) {
212 ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_CANNOT_SUPPLY_ENTROPY_SEED);
215 if (!get_parent_strength(drbg, &p_str))
217 if (drbg->strength > p_str) {
219 * We currently don't support the algorithm from NIST SP 800-90C
220 * 10.1.2 to use a weaker DRBG as source
222 ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK);
227 * Our lock is already held, but we need to lock our parent before
228 * generating bits from it. Note: taking the lock will be a no-op
229 * if locking is not required (while drbg->parent->lock == NULL).
231 if (!ossl_drbg_lock_parent(drbg))
234 * Get random data from parent. Include our DRBG address as
235 * additional input, in order to provide a distinction between
236 * different DRBG child instances.
238 * Note: using the sizeof() operator on a pointer triggers
239 * a warning in some static code analyzers, but it's
240 * intentional and correct here.
242 bytes = drbg->parent_get_seed(drbg->parent, pout, drbg->strength,
243 min_len, max_len, prediction_resistance,
244 (unsigned char *)&drbg, sizeof(drbg));
245 ossl_drbg_unlock_parent(drbg);
249 static void cleanup_entropy(PROV_DRBG *drbg, unsigned char *out, size_t outlen)
251 if (drbg->parent == NULL) {
253 ossl_crngt_cleanup_entropy(drbg, out, outlen);
255 ossl_prov_cleanup_entropy(drbg->provctx, out, outlen);
257 } else if (drbg->parent_clear_seed != NULL) {
258 if (!ossl_drbg_lock_parent(drbg))
260 drbg->parent_clear_seed(drbg, out, outlen);
261 ossl_drbg_unlock_parent(drbg);
265 #ifndef PROV_RAND_GET_RANDOM_NONCE
266 typedef struct prov_drbg_nonce_global_st {
267 CRYPTO_RWLOCK *rand_nonce_lock;
268 int rand_nonce_count;
269 } PROV_DRBG_NONCE_GLOBAL;
272 * drbg_ossl_ctx_new() calls drgb_setup() which calls rand_drbg_get_nonce()
273 * which needs to get the rand_nonce_lock out of the OSSL_LIB_CTX...but since
274 * drbg_ossl_ctx_new() hasn't finished running yet we need the rand_nonce_lock
275 * to be in a different global data object. Otherwise we will go into an
276 * infinite recursion loop.
278 void *ossl_prov_drbg_nonce_ctx_new(OSSL_LIB_CTX *libctx)
280 PROV_DRBG_NONCE_GLOBAL *dngbl = OPENSSL_zalloc(sizeof(*dngbl));
285 dngbl->rand_nonce_lock = CRYPTO_THREAD_lock_new();
286 if (dngbl->rand_nonce_lock == NULL) {
294 void ossl_prov_drbg_nonce_ctx_free(void *vdngbl)
296 PROV_DRBG_NONCE_GLOBAL *dngbl = vdngbl;
301 CRYPTO_THREAD_lock_free(dngbl->rand_nonce_lock);
306 /* Get a nonce from the operating system */
307 static size_t prov_drbg_get_nonce(PROV_DRBG *drbg, unsigned char **pout,
308 size_t min_len, size_t max_len)
311 unsigned char *buf = NULL;
312 OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(drbg->provctx);
313 PROV_DRBG_NONCE_GLOBAL *dngbl
314 = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_NONCE_INDEX);
323 if (drbg->parent != NULL && drbg->parent_nonce != NULL) {
324 n = drbg->parent_nonce(drbg->parent, NULL, 0, drbg->min_noncelen,
326 if (n > 0 && (buf = OPENSSL_malloc(n)) != NULL) {
327 ret = drbg->parent_nonce(drbg->parent, buf, 0,
328 drbg->min_noncelen, drbg->max_noncelen);
337 /* Use the built in nonce source plus some of our specifics */
338 memset(&data, 0, sizeof(data));
340 CRYPTO_atomic_add(&dngbl->rand_nonce_count, 1, &data.count,
341 dngbl->rand_nonce_lock);
342 return ossl_prov_get_nonce(drbg->provctx, pout, min_len, max_len,
343 &data, sizeof(data));
345 #endif /* PROV_RAND_GET_RANDOM_NONCE */
348 * Instantiate |drbg|, after it has been initialized. Use |pers| and
349 * |perslen| as prediction-resistance input.
351 * Requires that drbg->lock is already locked for write, if non-null.
353 * Returns 1 on success, 0 on failure.
355 int ossl_prov_drbg_instantiate(PROV_DRBG *drbg, unsigned int strength,
356 int prediction_resistance,
357 const unsigned char *pers, size_t perslen)
359 unsigned char *nonce = NULL, *entropy = NULL;
360 size_t noncelen = 0, entropylen = 0;
361 size_t min_entropy, min_entropylen, max_entropylen;
363 if (strength > drbg->strength) {
364 ERR_raise(ERR_LIB_PROV, PROV_R_INSUFFICIENT_DRBG_STRENGTH);
367 min_entropy = drbg->strength;
368 min_entropylen = drbg->min_entropylen;
369 max_entropylen = drbg->max_entropylen;
372 pers = (const unsigned char *)ossl_pers_string;
373 perslen = sizeof(ossl_pers_string);
375 if (perslen > drbg->max_perslen) {
376 ERR_raise(ERR_LIB_PROV, PROV_R_PERSONALISATION_STRING_TOO_LONG);
380 if (drbg->state != EVP_RAND_STATE_UNINITIALISED) {
381 if (drbg->state == EVP_RAND_STATE_ERROR)
382 ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE);
384 ERR_raise(ERR_LIB_PROV, PROV_R_ALREADY_INSTANTIATED);
388 drbg->state = EVP_RAND_STATE_ERROR;
390 if (drbg->min_noncelen > 0) {
391 if (drbg->parent_nonce != NULL) {
392 noncelen = drbg->parent_nonce(drbg->parent, NULL, drbg->strength,
396 ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
399 nonce = OPENSSL_malloc(noncelen);
401 ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
404 if (noncelen != drbg->parent_nonce(drbg->parent, nonce,
407 drbg->max_noncelen)) {
408 ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
411 #ifndef PROV_RAND_GET_RANDOM_NONCE
412 } else if (drbg->parent != NULL) {
415 * NIST SP800-90Ar1 section 9.1 says you can combine getting
416 * the entropy and nonce in 1 call by increasing the entropy
417 * with 50% and increasing the minimum length to accommodate
418 * the length of the nonce. We do this in case a nonce is
419 * required and there is no parental nonce capability.
421 min_entropy += drbg->strength / 2;
422 min_entropylen += drbg->min_noncelen;
423 max_entropylen += drbg->max_noncelen;
425 #ifndef PROV_RAND_GET_RANDOM_NONCE
426 else { /* parent == NULL */
427 noncelen = prov_drbg_get_nonce(drbg, &nonce, drbg->min_noncelen,
429 if (noncelen < drbg->min_noncelen
430 || noncelen > drbg->max_noncelen) {
431 ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
438 drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter);
439 if (drbg->reseed_next_counter) {
440 drbg->reseed_next_counter++;
441 if (!drbg->reseed_next_counter)
442 drbg->reseed_next_counter = 1;
445 entropylen = get_entropy(drbg, &entropy, min_entropy,
446 min_entropylen, max_entropylen,
447 prediction_resistance);
448 if (entropylen < min_entropylen
449 || entropylen > max_entropylen) {
450 ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_ENTROPY);
454 if (!drbg->instantiate(drbg, entropy, entropylen, nonce, noncelen,
456 cleanup_entropy(drbg, entropy, entropylen);
457 ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_INSTANTIATING_DRBG);
460 cleanup_entropy(drbg, entropy, entropylen);
462 drbg->state = EVP_RAND_STATE_READY;
463 drbg->generate_counter = 1;
464 drbg->reseed_time = time(NULL);
465 tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter);
469 ossl_prov_cleanup_nonce(drbg->provctx, nonce, noncelen);
470 if (drbg->state == EVP_RAND_STATE_READY)
476 * Uninstantiate |drbg|. Must be instantiated before it can be used.
478 * Requires that drbg->lock is already locked for write, if non-null.
480 * Returns 1 on success, 0 on failure.
482 int ossl_prov_drbg_uninstantiate(PROV_DRBG *drbg)
484 drbg->state = EVP_RAND_STATE_UNINITIALISED;
489 * Reseed |drbg|, mixing in the specified data
491 * Requires that drbg->lock is already locked for write, if non-null.
493 * Returns 1 on success, 0 on failure.
495 int ossl_prov_drbg_reseed(PROV_DRBG *drbg, int prediction_resistance,
496 const unsigned char *ent, size_t ent_len,
497 const unsigned char *adin, size_t adinlen)
499 unsigned char *entropy = NULL;
500 size_t entropylen = 0;
502 if (!ossl_prov_is_running())
505 if (drbg->state != EVP_RAND_STATE_READY) {
506 /* try to recover from previous errors */
507 rand_drbg_restart(drbg);
509 if (drbg->state == EVP_RAND_STATE_ERROR) {
510 ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE);
513 if (drbg->state == EVP_RAND_STATE_UNINITIALISED) {
514 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_INSTANTIATED);
520 if (ent_len < drbg->min_entropylen) {
521 ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_OUT_OF_RANGE);
522 drbg->state = EVP_RAND_STATE_ERROR;
525 if (ent_len > drbg->max_entropylen) {
526 ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_INPUT_TOO_LONG);
527 drbg->state = EVP_RAND_STATE_ERROR;
534 } else if (adinlen > drbg->max_adinlen) {
535 ERR_raise(ERR_LIB_PROV, PROV_R_ADDITIONAL_INPUT_TOO_LONG);
539 drbg->state = EVP_RAND_STATE_ERROR;
541 drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter);
542 if (drbg->reseed_next_counter) {
543 drbg->reseed_next_counter++;
544 if (!drbg->reseed_next_counter)
545 drbg->reseed_next_counter = 1;
551 * NIST SP-800-90A mandates that entropy *shall not* be provided
552 * by the consuming application. Instead the data is added as additional
555 * (NIST SP-800-90Ar1, Sections 9.1 and 9.2)
557 if (!drbg->reseed(drbg, NULL, 0, ent, ent_len)) {
558 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED);
562 if (!drbg->reseed(drbg, ent, ent_len, adin, adinlen)) {
563 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED);
566 /* There isn't much point adding the same additional input twice */
572 /* Reseed using our sources in addition */
573 entropylen = get_entropy(drbg, &entropy, drbg->strength,
574 drbg->min_entropylen, drbg->max_entropylen,
575 prediction_resistance);
576 if (entropylen < drbg->min_entropylen
577 || entropylen > drbg->max_entropylen) {
578 ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_ENTROPY);
582 if (!drbg->reseed(drbg, entropy, entropylen, adin, adinlen))
585 drbg->state = EVP_RAND_STATE_READY;
586 drbg->generate_counter = 1;
587 drbg->reseed_time = time(NULL);
588 tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter);
589 if (drbg->parent != NULL)
590 drbg->parent_reseed_counter = get_parent_reseed_count(drbg);
593 cleanup_entropy(drbg, entropy, entropylen);
594 if (drbg->state == EVP_RAND_STATE_READY)
600 * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
601 * to or if |prediction_resistance| is set. Additional input can be
602 * sent in |adin| and |adinlen|.
604 * Requires that drbg->lock is already locked for write, if non-null.
606 * Returns 1 on success, 0 on failure.
609 int ossl_prov_drbg_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen,
610 unsigned int strength, int prediction_resistance,
611 const unsigned char *adin, size_t adinlen)
614 int reseed_required = 0;
616 if (!ossl_prov_is_running())
619 if (drbg->state != EVP_RAND_STATE_READY) {
620 /* try to recover from previous errors */
621 rand_drbg_restart(drbg);
623 if (drbg->state == EVP_RAND_STATE_ERROR) {
624 ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE);
627 if (drbg->state == EVP_RAND_STATE_UNINITIALISED) {
628 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_INSTANTIATED);
632 if (strength > drbg->strength) {
633 ERR_raise(ERR_LIB_PROV, PROV_R_INSUFFICIENT_DRBG_STRENGTH);
637 if (outlen > drbg->max_request) {
638 ERR_raise(ERR_LIB_PROV, PROV_R_REQUEST_TOO_LARGE_FOR_DRBG);
641 if (adinlen > drbg->max_adinlen) {
642 ERR_raise(ERR_LIB_PROV, PROV_R_ADDITIONAL_INPUT_TOO_LONG);
646 fork_id = openssl_get_fork_id();
648 if (drbg->fork_id != fork_id) {
649 drbg->fork_id = fork_id;
653 if (drbg->reseed_interval > 0) {
654 if (drbg->generate_counter >= drbg->reseed_interval)
657 if (drbg->reseed_time_interval > 0) {
658 time_t now = time(NULL);
659 if (now < drbg->reseed_time
660 || now - drbg->reseed_time >= drbg->reseed_time_interval)
663 if (drbg->parent != NULL
664 && get_parent_reseed_count(drbg) != drbg->parent_reseed_counter)
667 if (reseed_required || prediction_resistance) {
668 if (!ossl_prov_drbg_reseed(drbg, prediction_resistance, NULL, 0,
670 ERR_raise(ERR_LIB_PROV, PROV_R_RESEED_ERROR);
677 if (!drbg->generate(drbg, out, outlen, adin, adinlen)) {
678 drbg->state = EVP_RAND_STATE_ERROR;
679 ERR_raise(ERR_LIB_PROV, PROV_R_GENERATE_ERROR);
683 drbg->generate_counter++;
689 * Restart |drbg|, using the specified entropy or additional input
691 * Tries its best to get the drbg instantiated by all means,
692 * regardless of its current state.
694 * Optionally, a |buffer| of |len| random bytes can be passed,
695 * which is assumed to contain at least |entropy| bits of entropy.
697 * If |entropy| > 0, the buffer content is used as entropy input.
699 * If |entropy| == 0, the buffer content is used as additional input
701 * Returns 1 on success, 0 on failure.
703 * This function is used internally only.
705 static int rand_drbg_restart(PROV_DRBG *drbg)
707 /* repair error state */
708 if (drbg->state == EVP_RAND_STATE_ERROR)
709 drbg->uninstantiate(drbg);
711 /* repair uninitialized state */
712 if (drbg->state == EVP_RAND_STATE_UNINITIALISED)
713 /* reinstantiate drbg */
714 ossl_prov_drbg_instantiate(drbg, drbg->strength, 0, NULL, 0);
716 return drbg->state == EVP_RAND_STATE_READY;
719 /* Provider support from here down */
720 static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch,
723 if (dispatch != NULL)
724 while (dispatch->function_id != 0) {
725 if (dispatch->function_id == function)
732 int ossl_drbg_enable_locking(void *vctx)
734 PROV_DRBG *drbg = vctx;
736 if (drbg != NULL && drbg->lock == NULL) {
737 if (drbg->parent_enable_locking != NULL)
738 if (!drbg->parent_enable_locking(drbg->parent)) {
739 ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED);
742 drbg->lock = CRYPTO_THREAD_lock_new();
743 if (drbg->lock == NULL) {
744 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_CREATE_LOCK);
752 * Allocate memory and initialize a new DRBG. The DRBG is allocated on
753 * the secure heap if |secure| is nonzero and the secure heap is enabled.
754 * The |parent|, if not NULL, will be used as random source for reseeding.
755 * This also requires the parent's provider context and the parent's lock.
757 * Returns a pointer to the new DRBG instance on success, NULL on failure.
759 PROV_DRBG *ossl_rand_drbg_new
760 (void *provctx, void *parent, const OSSL_DISPATCH *p_dispatch,
761 int (*dnew)(PROV_DRBG *ctx),
762 int (*instantiate)(PROV_DRBG *drbg,
763 const unsigned char *entropy, size_t entropylen,
764 const unsigned char *nonce, size_t noncelen,
765 const unsigned char *pers, size_t perslen),
766 int (*uninstantiate)(PROV_DRBG *ctx),
767 int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,
768 const unsigned char *adin, size_t adin_len),
769 int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,
770 const unsigned char *adin, size_t adin_len))
774 const OSSL_DISPATCH *pfunc;
776 if (!ossl_prov_is_running())
779 drbg = OPENSSL_zalloc(sizeof(*drbg));
781 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
785 drbg->provctx = provctx;
786 drbg->instantiate = instantiate;
787 drbg->uninstantiate = uninstantiate;
788 drbg->reseed = reseed;
789 drbg->generate = generate;
790 drbg->fork_id = openssl_get_fork_id();
792 /* Extract parent's functions */
793 drbg->parent = parent;
794 if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_ENABLE_LOCKING)) != NULL)
795 drbg->parent_enable_locking = OSSL_FUNC_rand_enable_locking(pfunc);
796 if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_LOCK)) != NULL)
797 drbg->parent_lock = OSSL_FUNC_rand_lock(pfunc);
798 if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_UNLOCK)) != NULL)
799 drbg->parent_unlock = OSSL_FUNC_rand_unlock(pfunc);
800 if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_CTX_PARAMS)) != NULL)
801 drbg->parent_get_ctx_params = OSSL_FUNC_rand_get_ctx_params(pfunc);
802 if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_NONCE)) != NULL)
803 drbg->parent_nonce = OSSL_FUNC_rand_nonce(pfunc);
804 if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_SEED)) != NULL)
805 drbg->parent_get_seed = OSSL_FUNC_rand_get_seed(pfunc);
806 if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_CLEAR_SEED)) != NULL)
807 drbg->parent_clear_seed = OSSL_FUNC_rand_clear_seed(pfunc);
809 /* Set some default maximums up */
810 drbg->max_entropylen = DRBG_MAX_LENGTH;
811 drbg->max_noncelen = DRBG_MAX_LENGTH;
812 drbg->max_perslen = DRBG_MAX_LENGTH;
813 drbg->max_adinlen = DRBG_MAX_LENGTH;
814 drbg->generate_counter = 1;
815 drbg->reseed_counter = 1;
816 drbg->reseed_interval = RESEED_INTERVAL;
817 drbg->reseed_time_interval = TIME_INTERVAL;
822 if (parent != NULL) {
823 if (!get_parent_strength(drbg, &p_str))
825 if (drbg->strength > p_str) {
827 * We currently don't support the algorithm from NIST SP 800-90C
828 * 10.1.2 to use a weaker DRBG as source
830 ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK);
834 #ifdef TSAN_REQUIRES_LOCKING
835 if (!ossl_drbg_enable_locking(drbg))
841 ossl_rand_drbg_free(drbg);
845 void ossl_rand_drbg_free(PROV_DRBG *drbg)
850 CRYPTO_THREAD_lock_free(drbg->lock);
854 int ossl_drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[])
858 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
859 if (p != NULL && !OSSL_PARAM_set_int(p, drbg->state))
862 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
863 if (p != NULL && !OSSL_PARAM_set_int(p, drbg->strength))
866 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
867 if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_request))
870 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_ENTROPYLEN);
871 if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_entropylen))
874 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ENTROPYLEN);
875 if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_entropylen))
878 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_NONCELEN);
879 if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_noncelen))
882 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_NONCELEN);
883 if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_noncelen))
886 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_PERSLEN);
887 if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_perslen))
890 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ADINLEN);
891 if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_adinlen))
894 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
895 if (p != NULL && !OSSL_PARAM_set_uint(p, drbg->reseed_interval))
898 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME);
899 if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time))
902 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
903 if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time_interval))
906 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_COUNTER);
908 && !OSSL_PARAM_set_uint(p, tsan_load(&drbg->reseed_counter)))
913 int ossl_drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[])
920 p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
921 if (p != NULL && !OSSL_PARAM_get_uint(p, &drbg->reseed_interval))
924 p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
925 if (p != NULL && !OSSL_PARAM_get_time_t(p, &drbg->reseed_time_interval))