2 * Copyright 2011-2020 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 "drbg_local.h"
17 #include "internal/thread_once.h"
18 #include "crypto/cryptlib.h"
19 #include "prov/seeding.h"
20 #include "prov/rand_pool.h"
21 #include "prov/provider_ctx.h"
22 #include "prov/providercommonerr.h"
25 * Support framework for NIST SP 800-90A DRBG
27 * See manual page PROV_DRBG(7) for a general overview.
29 * The OpenSSL model is to have new and free functions, and that new
30 * does all initialization. That is not the NIST model, which has
31 * instantiation and un-instantiate, and re-use within a new/free
32 * lifecycle. (No doubt this comes from the desire to support hardware
33 * DRBG, where allocation of resources on something like an HSM is
34 * a much bigger deal than just re-setting an allocated resource.)
37 /* NIST SP 800-90A DRBG recommends the use of a personalization string. */
38 static const char ossl_pers_string[] = DRBG_DEFAULT_PERS_STRING;
40 static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch,
43 static int rand_drbg_restart(PROV_DRBG *drbg);
45 int drbg_lock(void *vctx)
47 PROV_DRBG *drbg = vctx;
49 if (drbg == NULL || drbg->lock == NULL)
51 return CRYPTO_THREAD_write_lock(drbg->lock);
54 void drbg_unlock(void *vctx)
56 PROV_DRBG *drbg = vctx;
58 if (drbg != NULL && drbg->lock != NULL)
59 CRYPTO_THREAD_unlock(drbg->lock);
62 static int drbg_lock_parent(PROV_DRBG *drbg)
64 void *parent = drbg->parent;
67 && drbg->parent_lock != NULL
68 && !drbg->parent_lock(parent)) {
69 ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED);
75 static void drbg_unlock_parent(PROV_DRBG *drbg)
77 void *parent = drbg->parent;
79 if (parent != NULL && drbg->parent_unlock != NULL)
80 drbg->parent_unlock(parent);
83 static int get_parent_strength(PROV_DRBG *drbg, unsigned int *str)
85 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
86 void *parent = drbg->parent;
89 if (drbg->parent_get_ctx_params == NULL) {
90 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH);
94 *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, str);
95 if (!drbg_lock_parent(drbg)) {
96 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOCK_PARENT);
99 res = drbg->parent_get_ctx_params(parent, params);
100 drbg_unlock_parent(drbg);
102 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH);
108 static unsigned int get_parent_reseed_count(PROV_DRBG *drbg)
110 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
111 void *parent = drbg->parent;
114 *params = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_CTR, &r);
115 if (!drbg_lock_parent(drbg)) {
116 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOCK_PARENT);
119 if (!drbg->parent_get_ctx_params(parent, params)) {
120 drbg_unlock_parent(drbg);
121 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_RESEED_PROP_CTR);
124 drbg_unlock_parent(drbg);
128 r = tsan_load(&drbg->reseed_counter) - 2;
135 * Implements the get_entropy() callback (see RAND_DRBG_set_callbacks())
137 * If the DRBG has a parent, then the required amount of entropy input
138 * is fetched using the parent's RAND_DRBG_generate().
140 * Otherwise, the entropy is polled from the system entropy sources
141 * using prov_pool_acquire_entropy().
143 * If a random pool has been added to the DRBG using RAND_add(), then
144 * its entropy will be used up first.
146 static size_t prov_drbg_get_entropy(PROV_DRBG *drbg, unsigned char **pout,
147 int entropy, size_t min_len,
148 size_t max_len, int prediction_resistance)
151 size_t entropy_available = 0;
155 if (drbg->parent != NULL) {
156 if (!get_parent_strength(drbg, &p_str))
158 if (drbg->strength > p_str) {
160 * We currently don't support the algorithm from NIST SP 800-90C
161 * 10.1.2 to use a weaker DRBG as source
163 RANDerr(0, PROV_R_PARENT_STRENGTH_TOO_WEAK);
168 if (drbg->seed_pool != NULL) {
169 pool = drbg->seed_pool;
170 pool->entropy_requested = entropy;
172 pool = rand_pool_new(entropy, 1, min_len, max_len);
174 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
179 if (drbg->parent != NULL) {
180 size_t bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
181 unsigned char *buffer = rand_pool_add_begin(pool, bytes_needed);
183 if (buffer != NULL) {
187 * Get random data from parent. Include our address as additional input,
188 * in order to provide some additional distinction between different
189 * DRBG child instances.
190 * Our lock is already held, but we need to lock our parent before
191 * generating bits from it. (Note: taking the lock will be a no-op
192 * if locking if drbg->parent->lock == NULL.)
194 if (drbg->parent_generate == NULL)
196 drbg_lock_parent(drbg);
197 if (drbg->parent_generate(drbg->parent, buffer, bytes_needed,
198 drbg->strength, prediction_resistance,
199 (unsigned char *)&drbg,
201 bytes = bytes_needed;
202 drbg_unlock_parent(drbg);
203 drbg->parent_reseed_counter = get_parent_reseed_count(drbg);
205 rand_pool_add_end(pool, bytes, 8 * bytes);
206 entropy_available = rand_pool_entropy_available(pool);
209 /* Get entropy by polling system entropy sources. */
210 entropy_available = prov_pool_acquire_entropy(pool);
213 if (entropy_available > 0) {
214 ret = rand_pool_length(pool);
215 *pout = rand_pool_detach(pool);
219 if (drbg->seed_pool == NULL)
220 rand_pool_free(pool);
225 * Implements the cleanup_entropy() callback (see RAND_DRBG_set_callbacks())
228 static void prov_drbg_cleanup_entropy(PROV_DRBG *drbg,
229 unsigned char *out, size_t outlen)
231 OSSL_PARAM params[3], *p = params;
233 if (drbg->get_entropy_fn != NULL) {
234 if (drbg->cleanup_entropy_fn != NULL) {
235 *p++ = OSSL_PARAM_construct_size_t(OSSL_DRBG_PARAM_SIZE,
237 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_DRBG_PARAM_RANDOM_DATA,
239 *p = OSSL_PARAM_construct_end();
241 drbg->cleanup_entropy_fn(params, drbg->callback_arg);
243 } else if (drbg->seed_pool == NULL) {
244 OPENSSL_secure_clear_free(out, outlen);
248 static size_t get_entropy(PROV_DRBG *drbg, unsigned char **pout, int entropy,
249 size_t min_len, size_t max_len,
250 int prediction_resistance)
252 if (drbg->get_entropy_fn != NULL) {
253 OSSL_PARAM params[6], *p = params;
254 OSSL_PARAM out[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
256 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_ENTROPY_REQUIRED,
258 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_PREDICTION_RESISTANCE,
259 &prediction_resistance);
260 *p++ = OSSL_PARAM_construct_size_t(OSSL_DRBG_PARAM_MIN_LENGTH,
262 *p++ = OSSL_PARAM_construct_size_t(OSSL_DRBG_PARAM_MAX_LENGTH,
264 *p = OSSL_PARAM_construct_end();
265 *out = OSSL_PARAM_construct_octet_ptr(OSSL_DRBG_PARAM_RANDOM_DATA,
268 if (drbg->get_entropy_fn(params, out, drbg->callback_arg))
269 return out->return_size;
270 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_ENTROPY);
275 if (drbg->parent == NULL)
276 return prov_crngt_get_entropy(drbg, pout, entropy, min_len, max_len,
277 prediction_resistance);
280 return prov_drbg_get_entropy(drbg, pout, entropy, min_len, max_len,
281 prediction_resistance);
284 static void cleanup_entropy(PROV_DRBG *drbg, unsigned char *out, size_t outlen)
287 if (drbg->parent == NULL)
288 prov_crngt_cleanup_entropy(drbg, out, outlen);
291 prov_drbg_cleanup_entropy(drbg, out, outlen);
294 #ifndef PROV_RAND_GET_RANDOM_NONCE
295 typedef struct prov_drbg_nonce_global_st {
296 CRYPTO_RWLOCK *rand_nonce_lock;
297 int rand_nonce_count;
298 } PROV_DRBG_NONCE_GLOBAL;
301 * drbg_ossl_ctx_new() calls drgb_setup() which calls rand_drbg_get_nonce()
302 * which needs to get the rand_nonce_lock out of the OPENSSL_CTX...but since
303 * drbg_ossl_ctx_new() hasn't finished running yet we need the rand_nonce_lock
304 * to be in a different global data object. Otherwise we will go into an
305 * infinite recursion loop.
307 static void *prov_drbg_nonce_ossl_ctx_new(OPENSSL_CTX *libctx)
309 PROV_DRBG_NONCE_GLOBAL *dngbl = OPENSSL_zalloc(sizeof(*dngbl));
314 dngbl->rand_nonce_lock = CRYPTO_THREAD_lock_new();
315 if (dngbl->rand_nonce_lock == NULL) {
323 static void prov_drbg_nonce_ossl_ctx_free(void *vdngbl)
325 PROV_DRBG_NONCE_GLOBAL *dngbl = vdngbl;
330 CRYPTO_THREAD_lock_free(dngbl->rand_nonce_lock);
335 static const OPENSSL_CTX_METHOD drbg_nonce_ossl_ctx_method = {
336 prov_drbg_nonce_ossl_ctx_new,
337 prov_drbg_nonce_ossl_ctx_free,
340 /* Get a nonce from the operating system */
341 static size_t prov_drbg_get_nonce(PROV_DRBG *drbg,
342 unsigned char **pout,
343 int entropy, size_t min_len, size_t max_len)
347 unsigned char *buf = NULL;
348 OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(drbg->provctx);
349 PROV_DRBG_NONCE_GLOBAL *dngbl
350 = openssl_ctx_get_data(libctx, OPENSSL_CTX_DRBG_NONCE_INDEX,
351 &drbg_nonce_ossl_ctx_method);
352 OSSL_PARAM params[5], *p = params;
353 OSSL_PARAM out[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
362 if (drbg->get_nonce_fn != NULL) {
363 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_ENTROPY_REQUIRED,
365 *p++ = OSSL_PARAM_construct_size_t(OSSL_DRBG_PARAM_MIN_LENGTH,
367 *p++ = OSSL_PARAM_construct_size_t(OSSL_DRBG_PARAM_MAX_LENGTH,
369 *p = OSSL_PARAM_construct_end();
370 *out = OSSL_PARAM_construct_octet_ptr(OSSL_DRBG_PARAM_RANDOM_DATA,
373 if (drbg->get_nonce_fn(params, out, drbg->callback_arg))
374 return out->return_size;
375 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_NONCE);
378 if (drbg->parent != NULL) {
379 if (drbg->parent_nonce != NULL) {
380 n = drbg->parent_nonce(drbg->parent, NULL, 0, drbg->min_noncelen,
382 if (n > 0 && (buf = OPENSSL_malloc(n)) != NULL) {
383 ret = drbg->parent_nonce(drbg->parent, buf, 0,
395 /* Use the built in nonce source */
396 memset(&data, 0, sizeof(data));
397 pool = rand_pool_new(0, 0, min_len, max_len);
401 if (prov_pool_add_nonce_data(pool) == 0)
404 data.instance = drbg;
405 CRYPTO_atomic_add(&dngbl->rand_nonce_count, 1, &data.count,
406 dngbl->rand_nonce_lock);
408 if (rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0) == 0)
411 ret = rand_pool_length(pool);
412 *pout = rand_pool_detach(pool);
415 rand_pool_free(pool);
420 static void prov_drbg_clear_nonce(PROV_DRBG *drbg, unsigned char *nonce,
423 OSSL_PARAM params[3], *p = params;
425 if (drbg->get_nonce_fn != NULL) {
426 if (drbg->cleanup_nonce_fn != NULL) {
427 *p++ = OSSL_PARAM_construct_size_t(OSSL_DRBG_PARAM_SIZE,
429 *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_DRBG_PARAM_RANDOM_DATA,
431 *p = OSSL_PARAM_construct_end();
433 drbg->cleanup_nonce_fn(params, drbg->callback_arg);
436 OPENSSL_clear_free(nonce, noncelen);
440 # define prov_drbg_clear_nonce(drbg, nonce, len) \
441 OPENSSL_clear_free((nonce), (len))
442 #endif /* PROV_RAND_GET_RANDOM_NONCE */
445 * Instantiate |drbg|, after it has been initialized. Use |pers| and
446 * |perslen| as prediction-resistance input.
448 * Requires that drbg->lock is already locked for write, if non-null.
450 * Returns 1 on success, 0 on failure.
452 int PROV_DRBG_instantiate(PROV_DRBG *drbg, unsigned int strength,
453 int prediction_resistance,
454 const unsigned char *pers, size_t perslen)
456 unsigned char *nonce = NULL, *entropy = NULL;
457 size_t noncelen = 0, entropylen = 0;
458 size_t min_entropy, min_entropylen, max_entropylen;
460 if (strength > drbg->strength) {
461 PROVerr(0, PROV_R_INSUFFICIENT_DRBG_STRENGTH);
464 min_entropy = drbg->strength;
465 min_entropylen = drbg->min_entropylen;
466 max_entropylen = drbg->max_entropylen;
469 pers = (const unsigned char *)ossl_pers_string;
470 perslen = sizeof(ossl_pers_string);
472 if (perslen > drbg->max_perslen) {
473 PROVerr(0, PROV_R_PERSONALISATION_STRING_TOO_LONG);
477 if (drbg->state != EVP_RAND_STATE_UNINITIALISED) {
478 if (drbg->state == EVP_RAND_STATE_ERROR)
479 PROVerr(0, PROV_R_IN_ERROR_STATE);
481 PROVerr(0, PROV_R_ALREADY_INSTANTIATED);
485 drbg->state = EVP_RAND_STATE_ERROR;
487 if (drbg->min_noncelen > 0) {
488 if (drbg->parent_nonce != NULL) {
489 noncelen = drbg->parent_nonce(drbg->parent, NULL, drbg->strength,
493 PROVerr(0, PROV_R_ERROR_RETRIEVING_NONCE);
496 nonce = OPENSSL_malloc(noncelen);
498 PROVerr(0, PROV_R_ERROR_RETRIEVING_NONCE);
501 if (noncelen != drbg->parent_nonce(drbg->parent, nonce,
504 drbg->max_noncelen)) {
505 PROVerr(0, PROV_R_ERROR_RETRIEVING_NONCE);
508 #ifndef PROV_RAND_GET_RANDOM_NONCE
509 } else if (drbg->parent != NULL) {
512 * NIST SP800-90Ar1 section 9.1 says you can combine getting
513 * the entropy and nonce in 1 call by increasing the entropy
514 * with 50% and increasing the minimum length to accommodate
515 * the length of the nonce. We do this in case a nonce is
516 * required and there is no parental nonce capability.
518 min_entropy += drbg->strength / 2;
519 min_entropylen += drbg->min_noncelen;
520 max_entropylen += drbg->max_noncelen;
522 #ifndef PROV_RAND_GET_RANDOM_NONCE
523 else { /* parent == NULL */
524 noncelen = prov_drbg_get_nonce(drbg, &nonce, drbg->strength / 2,
527 if (noncelen < drbg->min_noncelen
528 || noncelen > drbg->max_noncelen) {
529 PROVerr(0, PROV_R_ERROR_RETRIEVING_NONCE);
536 drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter);
537 if (drbg->reseed_next_counter) {
538 drbg->reseed_next_counter++;
539 if (!drbg->reseed_next_counter)
540 drbg->reseed_next_counter = 1;
543 entropylen = get_entropy(drbg, &entropy, min_entropy,
544 min_entropylen, max_entropylen,
545 prediction_resistance);
546 if (entropylen < min_entropylen
547 || entropylen > max_entropylen) {
548 PROVerr(0, PROV_R_ERROR_RETRIEVING_ENTROPY);
552 if (!drbg->instantiate(drbg, entropy, entropylen, nonce, noncelen,
554 PROVerr(0, PROV_R_ERROR_INSTANTIATING_DRBG);
558 drbg->state = EVP_RAND_STATE_READY;
559 drbg->reseed_gen_counter = 1;
560 drbg->reseed_time = time(NULL);
561 tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter);
565 cleanup_entropy(drbg, entropy, entropylen);
566 prov_drbg_clear_nonce(drbg, nonce, noncelen);
567 if (drbg->state == EVP_RAND_STATE_READY)
573 * Uninstantiate |drbg|. Must be instantiated before it can be used.
575 * Requires that drbg->lock is already locked for write, if non-null.
577 * Returns 1 on success, 0 on failure.
579 int PROV_DRBG_uninstantiate(PROV_DRBG *drbg)
581 drbg->state = EVP_RAND_STATE_UNINITIALISED;
586 * Reseed |drbg|, mixing in the specified data
588 * Requires that drbg->lock is already locked for write, if non-null.
590 * Returns 1 on success, 0 on failure.
592 int PROV_DRBG_reseed(PROV_DRBG *drbg, int prediction_resistance,
593 const unsigned char *ent, size_t ent_len,
594 const unsigned char *adin, size_t adinlen)
596 unsigned char *entropy = NULL;
597 size_t entropylen = 0;
599 if (drbg->state != EVP_RAND_STATE_READY) {
600 /* try to recover from previous errors */
601 rand_drbg_restart(drbg);
603 if (drbg->state == EVP_RAND_STATE_ERROR) {
604 PROVerr(0, PROV_R_IN_ERROR_STATE);
607 if (drbg->state == EVP_RAND_STATE_UNINITIALISED) {
608 PROVerr(0, PROV_R_NOT_INSTANTIATED);
614 if (ent_len < drbg->min_entropylen) {
615 RANDerr(0, RAND_R_ENTROPY_OUT_OF_RANGE);
616 drbg->state = EVP_RAND_STATE_ERROR;
619 if (ent_len > drbg->max_entropylen) {
620 RANDerr(0, RAND_R_ENTROPY_INPUT_TOO_LONG);
621 drbg->state = EVP_RAND_STATE_ERROR;
628 } else if (adinlen > drbg->max_adinlen) {
629 PROVerr(0, PROV_R_ADDITIONAL_INPUT_TOO_LONG);
633 drbg->state = EVP_RAND_STATE_ERROR;
635 drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter);
636 if (drbg->reseed_next_counter) {
637 drbg->reseed_next_counter++;
638 if (!drbg->reseed_next_counter)
639 drbg->reseed_next_counter = 1;
645 * NIST SP-800-90A mandates that entropy *shall not* be provided
646 * by the consuming application. Instead the data is added as additional
649 * (NIST SP-800-90Ar1, Sections 9.1 and 9.2)
651 if (!drbg->reseed(drbg, NULL, 0, ent, ent_len)) {
652 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED);
656 if (!drbg->reseed(drbg, ent, ent_len, adin, adinlen)) {
657 ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED);
660 /* There isn't much point adding the same additional input twice */
666 /* Reseed using our sources in addition */
667 entropylen = get_entropy(drbg, &entropy, drbg->strength,
668 drbg->min_entropylen, drbg->max_entropylen,
669 prediction_resistance);
670 if (entropylen < drbg->min_entropylen
671 || entropylen > drbg->max_entropylen) {
672 PROVerr(0, PROV_R_ERROR_RETRIEVING_ENTROPY);
676 if (!drbg->reseed(drbg, entropy, entropylen, adin, adinlen))
679 drbg->state = EVP_RAND_STATE_READY;
680 drbg->reseed_gen_counter = 1;
681 drbg->reseed_time = time(NULL);
682 tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter);
683 if (drbg->parent != NULL)
684 drbg->parent_reseed_counter = get_parent_reseed_count(drbg);
687 cleanup_entropy(drbg, entropy, entropylen);
688 if (drbg->state == EVP_RAND_STATE_READY)
694 * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
695 * to or if |prediction_resistance| is set. Additional input can be
696 * sent in |adin| and |adinlen|.
698 * Requires that drbg->lock is already locked for write, if non-null.
700 * Returns 1 on success, 0 on failure.
703 int PROV_DRBG_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen,
704 unsigned int strength, int prediction_resistance,
705 const unsigned char *adin, size_t adinlen)
708 int reseed_required = 0;
710 if (drbg->state != EVP_RAND_STATE_READY) {
711 /* try to recover from previous errors */
712 rand_drbg_restart(drbg);
714 if (drbg->state == EVP_RAND_STATE_ERROR) {
715 PROVerr(0, PROV_R_IN_ERROR_STATE);
718 if (drbg->state == EVP_RAND_STATE_UNINITIALISED) {
719 PROVerr(0, PROV_R_NOT_INSTANTIATED);
723 if (strength > drbg->strength) {
724 PROVerr(0, PROV_R_INSUFFICIENT_DRBG_STRENGTH);
728 if (outlen > drbg->max_request) {
729 PROVerr(0, PROV_R_REQUEST_TOO_LARGE_FOR_DRBG);
732 if (adinlen > drbg->max_adinlen) {
733 PROVerr(0, PROV_R_ADDITIONAL_INPUT_TOO_LONG);
737 fork_id = openssl_get_fork_id();
739 if (drbg->fork_id != fork_id) {
740 drbg->fork_id = fork_id;
744 if (drbg->reseed_interval > 0) {
745 if (drbg->reseed_gen_counter > drbg->reseed_interval)
748 if (drbg->reseed_time_interval > 0) {
749 time_t now = time(NULL);
750 if (now < drbg->reseed_time
751 || now - drbg->reseed_time >= drbg->reseed_time_interval)
754 if (drbg->parent != NULL
755 && get_parent_reseed_count(drbg) != drbg->parent_reseed_counter)
758 if (reseed_required || prediction_resistance) {
759 if (!PROV_DRBG_reseed(drbg, prediction_resistance, NULL, 0,
761 PROVerr(0, PROV_R_RESEED_ERROR);
768 if (!drbg->generate(drbg, out, outlen, adin, adinlen)) {
769 drbg->state = EVP_RAND_STATE_ERROR;
770 PROVerr(0, PROV_R_GENERATE_ERROR);
774 drbg->reseed_gen_counter++;
780 * Restart |drbg|, using the specified entropy or additional input
782 * Tries its best to get the drbg instantiated by all means,
783 * regardless of its current state.
785 * Optionally, a |buffer| of |len| random bytes can be passed,
786 * which is assumed to contain at least |entropy| bits of entropy.
788 * If |entropy| > 0, the buffer content is used as entropy input.
790 * If |entropy| == 0, the buffer content is used as additional input
792 * Returns 1 on success, 0 on failure.
794 * This function is used internally only.
796 static int rand_drbg_restart(PROV_DRBG *drbg)
798 if (drbg->seed_pool != NULL) {
799 drbg->state = EVP_RAND_STATE_ERROR;
800 rand_pool_free(drbg->seed_pool);
801 drbg->seed_pool = NULL;
802 RANDerr(0, ERR_R_INTERNAL_ERROR);
806 /* repair error state */
807 if (drbg->state == EVP_RAND_STATE_ERROR)
808 drbg->uninstantiate(drbg);
810 /* repair uninitialized state */
811 if (drbg->state == EVP_RAND_STATE_UNINITIALISED)
812 /* reinstantiate drbg */
813 PROV_DRBG_instantiate(drbg, drbg->strength, 0, NULL, 0);
815 rand_pool_free(drbg->seed_pool);
816 drbg->seed_pool = NULL;
817 return drbg->state == EVP_RAND_STATE_READY;
820 /* Provider support from here down */
821 static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch,
824 if (dispatch != NULL)
825 while (dispatch->function_id != 0) {
826 if (dispatch->function_id == function)
833 int drbg_enable_locking(void *vctx)
835 PROV_DRBG *drbg = vctx;
837 if (drbg != NULL && drbg->lock == NULL) {
838 if (drbg->parent_enable_locking != NULL)
839 if (!drbg->parent_enable_locking(drbg->parent)) {
840 ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED);
843 drbg->lock = CRYPTO_THREAD_lock_new();
844 if (drbg->lock == NULL) {
845 ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_CREATE_LOCK);
853 * Allocate memory and initialize a new DRBG. The DRBG is allocated on
854 * the secure heap if |secure| is nonzero and the secure heap is enabled.
855 * The |parent|, if not NULL, will be used as random source for reseeding.
856 * This also requires the parent's provider context and the parent's lock.
858 * Returns a pointer to the new DRBG instance on success, NULL on failure.
860 PROV_DRBG *prov_rand_drbg_new
861 (void *provctx, void *parent, const OSSL_DISPATCH *p_dispatch,
862 int (*dnew)(PROV_DRBG *ctx),
863 int (*instantiate)(PROV_DRBG *drbg,
864 const unsigned char *entropy, size_t entropylen,
865 const unsigned char *nonce, size_t noncelen,
866 const unsigned char *pers, size_t perslen),
867 int (*uninstantiate)(PROV_DRBG *ctx),
868 int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,
869 const unsigned char *adin, size_t adin_len),
870 int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,
871 const unsigned char *adin, size_t adin_len))
873 PROV_DRBG *drbg = OPENSSL_zalloc(sizeof(*drbg));
875 const OSSL_DISPATCH *pfunc;
878 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
882 drbg->provctx = provctx;
883 drbg->instantiate = instantiate;
884 drbg->uninstantiate = uninstantiate;
885 drbg->reseed = reseed;
886 drbg->generate = generate;
887 drbg->fork_id = openssl_get_fork_id();
889 /* Extract parent's functions */
890 drbg->parent = parent;
891 if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_ENABLE_LOCKING)) != NULL)
892 drbg->parent_enable_locking = OSSL_FUNC_rand_enable_locking(pfunc);
893 if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_LOCK)) != NULL)
894 drbg->parent_lock = OSSL_FUNC_rand_lock(pfunc);
895 if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_UNLOCK)) != NULL)
896 drbg->parent_unlock = OSSL_FUNC_rand_unlock(pfunc);
897 if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_CTX_PARAMS)) != NULL)
898 drbg->parent_get_ctx_params = OSSL_FUNC_rand_get_ctx_params(pfunc);
899 if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GENERATE)) != NULL)
900 drbg->parent_generate = OSSL_FUNC_rand_generate(pfunc);
901 if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_NONCE)) != NULL)
902 drbg->parent_nonce = OSSL_FUNC_rand_nonce(pfunc);
904 /* Set some default maximums up */
905 drbg->max_entropylen = DRBG_MAX_LENGTH;
906 drbg->max_noncelen = DRBG_MAX_LENGTH;
907 drbg->max_perslen = DRBG_MAX_LENGTH;
908 drbg->max_adinlen = DRBG_MAX_LENGTH;
909 drbg->reseed_gen_counter = 1;
910 drbg->reseed_counter = 1;
911 drbg->reseed_interval = RESEED_INTERVAL;
912 drbg->reseed_time_interval = TIME_INTERVAL;
917 if (parent != NULL) {
918 if (!get_parent_strength(drbg, &p_str))
920 if (drbg->strength > p_str) {
922 * We currently don't support the algorithm from NIST SP 800-90C
923 * 10.1.2 to use a weaker DRBG as source
925 ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK);
932 prov_rand_drbg_free(drbg);
936 void prov_rand_drbg_free(PROV_DRBG *drbg)
941 rand_pool_free(drbg->adin_pool);
942 CRYPTO_THREAD_lock_free(drbg->lock);
946 int drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[])
950 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
951 if (p != NULL && !OSSL_PARAM_set_int(p, drbg->state))
954 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
955 if (p != NULL && !OSSL_PARAM_set_int(p, drbg->strength))
958 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_REQUEST);
959 if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_request))
962 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_ENTROPYLEN);
963 if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_entropylen))
966 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ENTROPYLEN);
967 if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_entropylen))
970 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_NONCELEN);
971 if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_noncelen))
974 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_NONCELEN);
975 if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_noncelen))
978 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_PERSLEN);
979 if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_perslen))
982 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ADINLEN);
983 if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_adinlen))
986 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
987 if (p != NULL && !OSSL_PARAM_set_uint(p, drbg->reseed_interval))
990 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME);
991 if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time))
994 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
995 if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time_interval))
998 p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_CTR);
1000 && !OSSL_PARAM_set_uint(p, tsan_load(&drbg->reseed_counter)))
1005 int drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[])
1007 const OSSL_PARAM *p;
1009 p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
1010 if (p != NULL && !OSSL_PARAM_get_uint(p, &drbg->reseed_interval))
1013 p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
1014 if (p != NULL && !OSSL_PARAM_get_time_t(p, &drbg->reseed_time_interval))
1019 int drbg_set_callbacks(void *vctx, OSSL_INOUT_CALLBACK *get_entropy_fn,
1020 OSSL_CALLBACK *cleanup_entropy_fn,
1021 OSSL_INOUT_CALLBACK *get_nonce_fn,
1022 OSSL_CALLBACK *cleanup_nonce_fn, void *arg)
1024 PROV_DRBG *drbg = vctx;
1026 if (drbg->state != EVP_RAND_STATE_UNINITIALISED
1027 || drbg->parent != NULL)
1030 drbg->get_entropy_fn = get_entropy_fn;
1031 drbg->cleanup_entropy_fn = cleanup_entropy_fn;
1032 drbg->get_nonce_fn = get_nonce_fn;
1033 drbg->cleanup_nonce_fn = cleanup_nonce_fn;
1034 drbg->callback_arg = arg;