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
);
186 RANDerr(RAND_F_RAND_DRBG_SET
, RAND_R_UNSUPPORTED_DRBG_TYPE
);
191 drbg
->state
= DRBG_ERROR
;
192 RANDerr(RAND_F_RAND_DRBG_SET
, RAND_R_ERROR_INITIALISING_DRBG
);
198 * Set/initialize default |type| and |flag| for new drbg instances.
200 * Returns 1 on success, 0 on failure.
202 int RAND_DRBG_set_defaults(int type
, unsigned int flags
)
205 if (!(is_digest(type
) || is_ctr(type
))) {
206 RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS
, RAND_R_UNSUPPORTED_DRBG_TYPE
);
210 if ((flags
& ~rand_drbg_used_flags
) != 0) {
211 RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS
, RAND_R_UNSUPPORTED_DRBG_FLAGS
);
215 all
= ((flags
& RAND_DRBG_TYPE_FLAGS
) == 0);
216 if (all
|| (flags
& RAND_DRBG_FLAG_MASTER
) != 0) {
217 rand_drbg_type
[RAND_DRBG_TYPE_MASTER
] = type
;
218 rand_drbg_flags
[RAND_DRBG_TYPE_MASTER
] = flags
| RAND_DRBG_FLAG_MASTER
;
220 if (all
|| (flags
& RAND_DRBG_FLAG_PUBLIC
) != 0) {
221 rand_drbg_type
[RAND_DRBG_TYPE_PUBLIC
] = type
;
222 rand_drbg_flags
[RAND_DRBG_TYPE_PUBLIC
] = flags
| RAND_DRBG_FLAG_PUBLIC
;
224 if (all
|| (flags
& RAND_DRBG_FLAG_PRIVATE
) != 0) {
225 rand_drbg_type
[RAND_DRBG_TYPE_PRIVATE
] = type
;
226 rand_drbg_flags
[RAND_DRBG_TYPE_PRIVATE
] = flags
| RAND_DRBG_FLAG_PRIVATE
;
233 * Allocate memory and initialize a new DRBG. The DRBG is allocated on
234 * the secure heap if |secure| is nonzero and the secure heap is enabled.
235 * The |parent|, if not NULL, will be used as random source for reseeding.
237 * Returns a pointer to the new DRBG instance on success, NULL on failure.
239 static RAND_DRBG
*rand_drbg_new(int secure
,
244 RAND_DRBG
*drbg
= secure
?
245 OPENSSL_secure_zalloc(sizeof(*drbg
)) : OPENSSL_zalloc(sizeof(*drbg
));
248 RANDerr(RAND_F_RAND_DRBG_NEW
, ERR_R_MALLOC_FAILURE
);
252 drbg
->secure
= secure
&& CRYPTO_secure_allocated(drbg
);
253 drbg
->fork_count
= rand_fork_count
;
254 drbg
->parent
= parent
;
256 if (parent
== NULL
) {
257 drbg
->get_entropy
= rand_drbg_get_entropy
;
258 drbg
->cleanup_entropy
= rand_drbg_cleanup_entropy
;
259 #ifndef RAND_DRBG_GET_RANDOM_NONCE
260 drbg
->get_nonce
= rand_drbg_get_nonce
;
261 drbg
->cleanup_nonce
= rand_drbg_cleanup_nonce
;
264 drbg
->reseed_interval
= master_reseed_interval
;
265 drbg
->reseed_time_interval
= master_reseed_time_interval
;
267 drbg
->get_entropy
= rand_drbg_get_entropy
;
268 drbg
->cleanup_entropy
= rand_drbg_cleanup_entropy
;
270 * Do not provide nonce callbacks, the child DRBGs will
271 * obtain their nonce using random bits from the parent.
274 drbg
->reseed_interval
= slave_reseed_interval
;
275 drbg
->reseed_time_interval
= slave_reseed_time_interval
;
278 if (RAND_DRBG_set(drbg
, type
, flags
) == 0)
281 if (parent
!= NULL
) {
282 rand_drbg_lock(parent
);
283 if (drbg
->strength
> parent
->strength
) {
285 * We currently don't support the algorithm from NIST SP 800-90C
286 * 10.1.2 to use a weaker DRBG as source
288 rand_drbg_unlock(parent
);
289 RANDerr(RAND_F_RAND_DRBG_NEW
, RAND_R_PARENT_STRENGTH_TOO_WEAK
);
292 rand_drbg_unlock(parent
);
298 RAND_DRBG_free(drbg
);
303 RAND_DRBG
*RAND_DRBG_new(int type
, unsigned int flags
, RAND_DRBG
*parent
)
305 return rand_drbg_new(0, type
, flags
, parent
);
308 RAND_DRBG
*RAND_DRBG_secure_new(int type
, unsigned int flags
, RAND_DRBG
*parent
)
310 return rand_drbg_new(1, type
, flags
, parent
);
314 * Uninstantiate |drbg| and free all memory.
316 void RAND_DRBG_free(RAND_DRBG
*drbg
)
321 if (drbg
->meth
!= NULL
)
322 drbg
->meth
->uninstantiate(drbg
);
323 rand_pool_free(drbg
->adin_pool
);
324 CRYPTO_THREAD_lock_free(drbg
->lock
);
325 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG
, drbg
, &drbg
->ex_data
);
328 OPENSSL_secure_clear_free(drbg
, sizeof(*drbg
));
330 OPENSSL_clear_free(drbg
, sizeof(*drbg
));
334 * Instantiate |drbg|, after it has been initialized. Use |pers| and
335 * |perslen| as prediction-resistance input.
337 * Requires that drbg->lock is already locked for write, if non-null.
339 * Returns 1 on success, 0 on failure.
341 int RAND_DRBG_instantiate(RAND_DRBG
*drbg
,
342 const unsigned char *pers
, size_t perslen
)
344 unsigned char *nonce
= NULL
, *entropy
= NULL
;
345 size_t noncelen
= 0, entropylen
= 0;
346 size_t min_entropy
= drbg
->strength
;
347 size_t min_entropylen
= drbg
->min_entropylen
;
348 size_t max_entropylen
= drbg
->max_entropylen
;
350 if (perslen
> drbg
->max_perslen
) {
351 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
352 RAND_R_PERSONALISATION_STRING_TOO_LONG
);
356 if (drbg
->meth
== NULL
) {
357 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
358 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED
);
362 if (drbg
->state
!= DRBG_UNINITIALISED
) {
363 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
364 drbg
->state
== DRBG_ERROR
? RAND_R_IN_ERROR_STATE
365 : RAND_R_ALREADY_INSTANTIATED
);
369 drbg
->state
= DRBG_ERROR
;
372 * NIST SP800-90Ar1 section 9.1 says you can combine getting the entropy
373 * and nonce in 1 call by increasing the entropy with 50% and increasing
374 * the minimum length to accomadate the length of the nonce.
375 * We do this in case a nonce is require and get_nonce is NULL.
377 if (drbg
->min_noncelen
> 0 && drbg
->get_nonce
== NULL
) {
378 min_entropy
+= drbg
->strength
/ 2;
379 min_entropylen
+= drbg
->min_noncelen
;
380 max_entropylen
+= drbg
->max_noncelen
;
383 drbg
->reseed_next_counter
= tsan_load(&drbg
->reseed_prop_counter
);
384 if (drbg
->reseed_next_counter
) {
385 drbg
->reseed_next_counter
++;
386 if(!drbg
->reseed_next_counter
)
387 drbg
->reseed_next_counter
= 1;
390 if (drbg
->get_entropy
!= NULL
)
391 entropylen
= drbg
->get_entropy(drbg
, &entropy
, min_entropy
,
392 min_entropylen
, max_entropylen
, 0);
393 if (entropylen
< min_entropylen
394 || entropylen
> max_entropylen
) {
395 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
, RAND_R_ERROR_RETRIEVING_ENTROPY
);
399 if (drbg
->min_noncelen
> 0 && drbg
->get_nonce
!= NULL
) {
400 noncelen
= drbg
->get_nonce(drbg
, &nonce
, drbg
->strength
/ 2,
401 drbg
->min_noncelen
, drbg
->max_noncelen
);
402 if (noncelen
< drbg
->min_noncelen
|| noncelen
> drbg
->max_noncelen
) {
403 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
, RAND_R_ERROR_RETRIEVING_NONCE
);
408 if (!drbg
->meth
->instantiate(drbg
, entropy
, entropylen
,
409 nonce
, noncelen
, pers
, perslen
)) {
410 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
, RAND_R_ERROR_INSTANTIATING_DRBG
);
414 drbg
->state
= DRBG_READY
;
415 drbg
->reseed_gen_counter
= 1;
416 drbg
->reseed_time
= time(NULL
);
417 tsan_store(&drbg
->reseed_prop_counter
, drbg
->reseed_next_counter
);
420 if (entropy
!= NULL
&& drbg
->cleanup_entropy
!= NULL
)
421 drbg
->cleanup_entropy(drbg
, entropy
, entropylen
);
422 if (nonce
!= NULL
&& drbg
->cleanup_nonce
!= NULL
)
423 drbg
->cleanup_nonce(drbg
, nonce
, noncelen
);
424 if (drbg
->state
== DRBG_READY
)
430 * Uninstantiate |drbg|. Must be instantiated before it can be used.
432 * Requires that drbg->lock is already locked for write, if non-null.
434 * Returns 1 on success, 0 on failure.
436 int RAND_DRBG_uninstantiate(RAND_DRBG
*drbg
)
438 int index
= -1, type
, flags
;
439 if (drbg
->meth
== NULL
) {
440 drbg
->state
= DRBG_ERROR
;
441 RANDerr(RAND_F_RAND_DRBG_UNINSTANTIATE
,
442 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED
);
446 /* Clear the entire drbg->ctr struct, then reset some important
447 * members of the drbg->ctr struct (e.g. keysize, df_ks) to their
450 drbg
->meth
->uninstantiate(drbg
);
452 /* The reset uses the default values for type and flags */
453 if (drbg
->flags
& RAND_DRBG_FLAG_MASTER
)
454 index
= RAND_DRBG_TYPE_MASTER
;
455 else if (drbg
->flags
& RAND_DRBG_FLAG_PRIVATE
)
456 index
= RAND_DRBG_TYPE_PRIVATE
;
457 else if (drbg
->flags
& RAND_DRBG_FLAG_PUBLIC
)
458 index
= RAND_DRBG_TYPE_PUBLIC
;
461 flags
= rand_drbg_flags
[index
];
462 type
= rand_drbg_type
[index
];
467 return RAND_DRBG_set(drbg
, type
, flags
);
471 * Reseed |drbg|, mixing in the specified data
473 * Requires that drbg->lock is already locked for write, if non-null.
475 * Returns 1 on success, 0 on failure.
477 int RAND_DRBG_reseed(RAND_DRBG
*drbg
,
478 const unsigned char *adin
, size_t adinlen
,
479 int prediction_resistance
)
481 unsigned char *entropy
= NULL
;
482 size_t entropylen
= 0;
484 if (drbg
->state
== DRBG_ERROR
) {
485 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_IN_ERROR_STATE
);
488 if (drbg
->state
== DRBG_UNINITIALISED
) {
489 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_NOT_INSTANTIATED
);
495 } else if (adinlen
> drbg
->max_adinlen
) {
496 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
500 drbg
->state
= DRBG_ERROR
;
502 drbg
->reseed_next_counter
= tsan_load(&drbg
->reseed_prop_counter
);
503 if (drbg
->reseed_next_counter
) {
504 drbg
->reseed_next_counter
++;
505 if(!drbg
->reseed_next_counter
)
506 drbg
->reseed_next_counter
= 1;
509 if (drbg
->get_entropy
!= NULL
)
510 entropylen
= drbg
->get_entropy(drbg
, &entropy
, drbg
->strength
,
511 drbg
->min_entropylen
,
512 drbg
->max_entropylen
,
513 prediction_resistance
);
514 if (entropylen
< drbg
->min_entropylen
515 || entropylen
> drbg
->max_entropylen
) {
516 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_ERROR_RETRIEVING_ENTROPY
);
520 if (!drbg
->meth
->reseed(drbg
, entropy
, entropylen
, adin
, adinlen
))
523 drbg
->state
= DRBG_READY
;
524 drbg
->reseed_gen_counter
= 1;
525 drbg
->reseed_time
= time(NULL
);
526 tsan_store(&drbg
->reseed_prop_counter
, drbg
->reseed_next_counter
);
529 if (entropy
!= NULL
&& drbg
->cleanup_entropy
!= NULL
)
530 drbg
->cleanup_entropy(drbg
, entropy
, entropylen
);
531 if (drbg
->state
== DRBG_READY
)
537 * Restart |drbg|, using the specified entropy or additional input
539 * Tries its best to get the drbg instantiated by all means,
540 * regardless of its current state.
542 * Optionally, a |buffer| of |len| random bytes can be passed,
543 * which is assumed to contain at least |entropy| bits of entropy.
545 * If |entropy| > 0, the buffer content is used as entropy input.
547 * If |entropy| == 0, the buffer content is used as additional input
549 * Returns 1 on success, 0 on failure.
551 * This function is used internally only.
553 int rand_drbg_restart(RAND_DRBG
*drbg
,
554 const unsigned char *buffer
, size_t len
, size_t entropy
)
557 const unsigned char *adin
= NULL
;
560 if (drbg
->seed_pool
!= NULL
) {
561 RANDerr(RAND_F_RAND_DRBG_RESTART
, ERR_R_INTERNAL_ERROR
);
562 drbg
->state
= DRBG_ERROR
;
563 rand_pool_free(drbg
->seed_pool
);
564 drbg
->seed_pool
= NULL
;
568 if (buffer
!= NULL
) {
570 if (drbg
->max_entropylen
< len
) {
571 RANDerr(RAND_F_RAND_DRBG_RESTART
,
572 RAND_R_ENTROPY_INPUT_TOO_LONG
);
573 drbg
->state
= DRBG_ERROR
;
577 if (entropy
> 8 * len
) {
578 RANDerr(RAND_F_RAND_DRBG_RESTART
, RAND_R_ENTROPY_OUT_OF_RANGE
);
579 drbg
->state
= DRBG_ERROR
;
583 /* will be picked up by the rand_drbg_get_entropy() callback */
584 drbg
->seed_pool
= rand_pool_attach(buffer
, len
, entropy
);
585 if (drbg
->seed_pool
== NULL
)
588 if (drbg
->max_adinlen
< len
) {
589 RANDerr(RAND_F_RAND_DRBG_RESTART
,
590 RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
591 drbg
->state
= DRBG_ERROR
;
599 /* repair error state */
600 if (drbg
->state
== DRBG_ERROR
)
601 RAND_DRBG_uninstantiate(drbg
);
603 /* repair uninitialized state */
604 if (drbg
->state
== DRBG_UNINITIALISED
) {
605 /* reinstantiate drbg */
606 RAND_DRBG_instantiate(drbg
,
607 (const unsigned char *) ossl_pers_string
,
608 sizeof(ossl_pers_string
) - 1);
609 /* already reseeded. prevent second reseeding below */
610 reseeded
= (drbg
->state
== DRBG_READY
);
613 /* refresh current state if entropy or additional input has been provided */
614 if (drbg
->state
== DRBG_READY
) {
617 * mix in additional input without reseeding
619 * Similar to RAND_DRBG_reseed(), but the provided additional
620 * data |adin| is mixed into the current state without pulling
621 * entropy from the trusted entropy source using get_entropy().
622 * This is not a reseeding in the strict sense of NIST SP 800-90A.
624 drbg
->meth
->reseed(drbg
, adin
, adinlen
, NULL
, 0);
625 } else if (reseeded
== 0) {
626 /* do a full reseeding if it has not been done yet above */
627 RAND_DRBG_reseed(drbg
, NULL
, 0, 0);
631 rand_pool_free(drbg
->seed_pool
);
632 drbg
->seed_pool
= NULL
;
634 return drbg
->state
== DRBG_READY
;
638 * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
639 * to or if |prediction_resistance| is set. Additional input can be
640 * sent in |adin| and |adinlen|.
642 * Requires that drbg->lock is already locked for write, if non-null.
644 * Returns 1 on success, 0 on failure.
647 int RAND_DRBG_generate(RAND_DRBG
*drbg
, unsigned char *out
, size_t outlen
,
648 int prediction_resistance
,
649 const unsigned char *adin
, size_t adinlen
)
651 int reseed_required
= 0;
653 if (drbg
->state
!= DRBG_READY
) {
654 /* try to recover from previous errors */
655 rand_drbg_restart(drbg
, NULL
, 0, 0);
657 if (drbg
->state
== DRBG_ERROR
) {
658 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_IN_ERROR_STATE
);
661 if (drbg
->state
== DRBG_UNINITIALISED
) {
662 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_NOT_INSTANTIATED
);
667 if (outlen
> drbg
->max_request
) {
668 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG
);
671 if (adinlen
> drbg
->max_adinlen
) {
672 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
676 if (drbg
->fork_count
!= rand_fork_count
) {
677 drbg
->fork_count
= rand_fork_count
;
681 if (drbg
->reseed_interval
> 0) {
682 if (drbg
->reseed_gen_counter
> drbg
->reseed_interval
)
685 if (drbg
->reseed_time_interval
> 0) {
686 time_t now
= time(NULL
);
687 if (now
< drbg
->reseed_time
688 || now
- drbg
->reseed_time
>= drbg
->reseed_time_interval
)
691 if (drbg
->parent
!= NULL
) {
692 unsigned int reseed_counter
= tsan_load(&drbg
->reseed_prop_counter
);
693 if (reseed_counter
> 0
694 && tsan_load(&drbg
->parent
->reseed_prop_counter
)
699 if (reseed_required
|| prediction_resistance
) {
700 if (!RAND_DRBG_reseed(drbg
, adin
, adinlen
, prediction_resistance
)) {
701 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_RESEED_ERROR
);
708 if (!drbg
->meth
->generate(drbg
, out
, outlen
, adin
, adinlen
)) {
709 drbg
->state
= DRBG_ERROR
;
710 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_GENERATE_ERROR
);
714 drbg
->reseed_gen_counter
++;
720 * Generates |outlen| random bytes and stores them in |out|. It will
721 * using the given |drbg| to generate the bytes.
723 * Requires that drbg->lock is already locked for write, if non-null.
725 * Returns 1 on success 0 on failure.
727 int RAND_DRBG_bytes(RAND_DRBG
*drbg
, unsigned char *out
, size_t outlen
)
729 unsigned char *additional
= NULL
;
730 size_t additional_len
;
734 if (drbg
->adin_pool
== NULL
) {
737 drbg
->adin_pool
= rand_pool_new(0, 0, drbg
->max_adinlen
);
738 if (drbg
->adin_pool
== NULL
)
742 additional_len
= rand_drbg_get_additional_data(drbg
->adin_pool
,
745 for ( ; outlen
> 0; outlen
-= chunk
, out
+= chunk
) {
747 if (chunk
> drbg
->max_request
)
748 chunk
= drbg
->max_request
;
749 ret
= RAND_DRBG_generate(drbg
, out
, chunk
, 0, additional
, additional_len
);
756 if (additional
!= NULL
)
757 rand_drbg_cleanup_additional_data(drbg
->adin_pool
, additional
);
763 * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
765 * Setting the callbacks is allowed only if the drbg has not been
766 * initialized yet. Otherwise, the operation will fail.
768 * Returns 1 on success, 0 on failure.
770 int RAND_DRBG_set_callbacks(RAND_DRBG
*drbg
,
771 RAND_DRBG_get_entropy_fn get_entropy
,
772 RAND_DRBG_cleanup_entropy_fn cleanup_entropy
,
773 RAND_DRBG_get_nonce_fn get_nonce
,
774 RAND_DRBG_cleanup_nonce_fn cleanup_nonce
)
776 if (drbg
->state
!= DRBG_UNINITIALISED
777 || drbg
->parent
!= NULL
)
779 drbg
->get_entropy
= get_entropy
;
780 drbg
->cleanup_entropy
= cleanup_entropy
;
781 drbg
->get_nonce
= get_nonce
;
782 drbg
->cleanup_nonce
= cleanup_nonce
;
787 * Set the reseed interval.
789 * The drbg will reseed automatically whenever the number of generate
790 * requests exceeds the given reseed interval. If the reseed interval
791 * is 0, then this feature is disabled.
793 * Returns 1 on success, 0 on failure.
795 int RAND_DRBG_set_reseed_interval(RAND_DRBG
*drbg
, unsigned int interval
)
797 if (interval
> MAX_RESEED_INTERVAL
)
799 drbg
->reseed_interval
= interval
;
804 * Set the reseed time interval.
806 * The drbg will reseed automatically whenever the time elapsed since
807 * the last reseeding exceeds the given reseed time interval. For safety,
808 * a reseeding will also occur if the clock has been reset to a smaller
811 * Returns 1 on success, 0 on failure.
813 int RAND_DRBG_set_reseed_time_interval(RAND_DRBG
*drbg
, time_t interval
)
815 if (interval
> MAX_RESEED_TIME_INTERVAL
)
817 drbg
->reseed_time_interval
= interval
;
822 * Set the default values for reseed (time) intervals of new DRBG instances
824 * The default values can be set independently for master DRBG instances
825 * (without a parent) and slave DRBG instances (with parent).
827 * Returns 1 on success, 0 on failure.
830 int RAND_DRBG_set_reseed_defaults(
831 unsigned int _master_reseed_interval
,
832 unsigned int _slave_reseed_interval
,
833 time_t _master_reseed_time_interval
,
834 time_t _slave_reseed_time_interval
837 if (_master_reseed_interval
> MAX_RESEED_INTERVAL
838 || _slave_reseed_interval
> MAX_RESEED_INTERVAL
)
841 if (_master_reseed_time_interval
> MAX_RESEED_TIME_INTERVAL
842 || _slave_reseed_time_interval
> MAX_RESEED_TIME_INTERVAL
)
845 master_reseed_interval
= _master_reseed_interval
;
846 slave_reseed_interval
= _slave_reseed_interval
;
848 master_reseed_time_interval
= _master_reseed_time_interval
;
849 slave_reseed_time_interval
= _slave_reseed_time_interval
;
855 * Locks the given drbg. Locking a drbg which does not have locking
856 * enabled is considered a successful no-op.
858 * Returns 1 on success, 0 on failure.
860 int rand_drbg_lock(RAND_DRBG
*drbg
)
862 if (drbg
->lock
!= NULL
)
863 return CRYPTO_THREAD_write_lock(drbg
->lock
);
869 * Unlocks the given drbg. Unlocking a drbg which does not have locking
870 * enabled is considered a successful no-op.
872 * Returns 1 on success, 0 on failure.
874 int rand_drbg_unlock(RAND_DRBG
*drbg
)
876 if (drbg
->lock
!= NULL
)
877 return CRYPTO_THREAD_unlock(drbg
->lock
);
883 * Enables locking for the given drbg
885 * Locking can only be enabled if the random generator
886 * is in the uninitialized state.
888 * Returns 1 on success, 0 on failure.
890 int rand_drbg_enable_locking(RAND_DRBG
*drbg
)
892 if (drbg
->state
!= DRBG_UNINITIALISED
) {
893 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING
,
894 RAND_R_DRBG_ALREADY_INITIALIZED
);
898 if (drbg
->lock
== NULL
) {
899 if (drbg
->parent
!= NULL
&& drbg
->parent
->lock
== NULL
) {
900 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING
,
901 RAND_R_PARENT_LOCKING_NOT_ENABLED
);
905 drbg
->lock
= CRYPTO_THREAD_lock_new();
906 if (drbg
->lock
== NULL
) {
907 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING
,
908 RAND_R_FAILED_TO_CREATE_LOCK
);
917 * Get and set the EXDATA
919 int RAND_DRBG_set_ex_data(RAND_DRBG
*drbg
, int idx
, void *arg
)
921 return CRYPTO_set_ex_data(&drbg
->ex_data
, idx
, arg
);
924 void *RAND_DRBG_get_ex_data(const RAND_DRBG
*drbg
, int idx
)
926 return CRYPTO_get_ex_data(&drbg
->ex_data
, idx
);
931 * The following functions provide a RAND_METHOD that works on the
932 * global DRBG. They lock.
936 * Allocates a new global DRBG on the secure heap (if enabled) and
937 * initializes it with default settings.
939 * Returns a pointer to the new DRBG instance on success, NULL on failure.
941 static RAND_DRBG
*drbg_setup(RAND_DRBG
*parent
, int drbg_type
)
945 drbg
= RAND_DRBG_secure_new(rand_drbg_type
[drbg_type
],
946 rand_drbg_flags
[drbg_type
], parent
);
950 /* Only the master DRBG needs to have a lock */
951 if (parent
== NULL
&& rand_drbg_enable_locking(drbg
) == 0)
954 /* enable seed propagation */
955 tsan_store(&drbg
->reseed_prop_counter
, 1);
958 * Ignore instantiation error to support just-in-time instantiation.
960 * The state of the drbg will be checked in RAND_DRBG_generate() and
961 * an automatic recovery is attempted.
963 (void)RAND_DRBG_instantiate(drbg
,
964 (const unsigned char *) ossl_pers_string
,
965 sizeof(ossl_pers_string
) - 1);
969 RAND_DRBG_free(drbg
);
974 * Initialize the global DRBGs on first use.
975 * Returns 1 on success, 0 on failure.
977 DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init
)
980 * ensure that libcrypto is initialized, otherwise the
981 * DRBG locks are not cleaned up properly
983 if (!OPENSSL_init_crypto(0, NULL
))
986 if (!CRYPTO_THREAD_init_local(&private_drbg
, NULL
))
989 if (!CRYPTO_THREAD_init_local(&public_drbg
, NULL
))
992 master_drbg
= drbg_setup(NULL
, RAND_DRBG_TYPE_MASTER
);
993 if (master_drbg
== NULL
)
999 CRYPTO_THREAD_cleanup_local(&public_drbg
);
1001 CRYPTO_THREAD_cleanup_local(&private_drbg
);
1005 /* Clean up the global DRBGs before exit */
1006 void rand_drbg_cleanup_int(void)
1008 if (master_drbg
!= NULL
) {
1009 RAND_DRBG_free(master_drbg
);
1012 CRYPTO_THREAD_cleanup_local(&private_drbg
);
1013 CRYPTO_THREAD_cleanup_local(&public_drbg
);
1017 void drbg_delete_thread_state(void)
1021 drbg
= CRYPTO_THREAD_get_local(&public_drbg
);
1022 CRYPTO_THREAD_set_local(&public_drbg
, NULL
);
1023 RAND_DRBG_free(drbg
);
1025 drbg
= CRYPTO_THREAD_get_local(&private_drbg
);
1026 CRYPTO_THREAD_set_local(&private_drbg
, NULL
);
1027 RAND_DRBG_free(drbg
);
1030 /* Implements the default OpenSSL RAND_bytes() method */
1031 static int drbg_bytes(unsigned char *out
, int count
)
1034 RAND_DRBG
*drbg
= RAND_DRBG_get0_public();
1039 ret
= RAND_DRBG_bytes(drbg
, out
, count
);
1045 * Calculates the minimum length of a full entropy buffer
1046 * which is necessary to seed (i.e. instantiate) the DRBG
1049 * NOTE: There is a copy of this function in drbgtest.c.
1050 * If you change anything here, you need to update
1051 * the copy accordingly.
1053 static size_t rand_drbg_seedlen(RAND_DRBG
*drbg
)
1056 * If no os entropy source is available then RAND_seed(buffer, bufsize)
1057 * is expected to succeed if and only if the buffer length satisfies
1058 * the following requirements, which follow from the calculations
1059 * in RAND_DRBG_instantiate().
1061 size_t min_entropy
= drbg
->strength
;
1062 size_t min_entropylen
= drbg
->min_entropylen
;
1065 * Extra entropy for the random nonce in the absence of a
1066 * get_nonce callback, see comment in RAND_DRBG_instantiate().
1068 if (drbg
->min_noncelen
> 0 && drbg
->get_nonce
== NULL
) {
1069 min_entropy
+= drbg
->strength
/ 2;
1070 min_entropylen
+= drbg
->min_noncelen
;
1074 * Convert entropy requirement from bits to bytes
1075 * (dividing by 8 without rounding upwards, because
1076 * all entropy requirements are divisible by 8).
1080 /* Return a value that satisfies both requirements */
1081 return min_entropy
> min_entropylen
? min_entropy
: min_entropylen
;
1084 /* Implements the default OpenSSL RAND_add() method */
1085 static int drbg_add(const void *buf
, int num
, double randomness
)
1088 RAND_DRBG
*drbg
= RAND_DRBG_get0_master();
1095 if (num
< 0 || randomness
< 0.0)
1098 rand_drbg_lock(drbg
);
1099 seedlen
= rand_drbg_seedlen(drbg
);
1101 buflen
= (size_t)num
;
1103 if (buflen
< seedlen
|| randomness
< (double) seedlen
) {
1104 #if defined(OPENSSL_RAND_SEED_NONE)
1106 * If no os entropy source is available, a reseeding will fail
1107 * inevitably. So we use a trick to mix the buffer contents into
1108 * the DRBG state without forcing a reseeding: we generate a
1109 * dummy random byte, using the buffer content as additional data.
1110 * Note: This won't work with RAND_DRBG_FLAG_CTR_NO_DF.
1112 unsigned char dummy
[1];
1114 ret
= RAND_DRBG_generate(drbg
, dummy
, sizeof(dummy
), 0, buf
, buflen
);
1115 rand_drbg_unlock(drbg
);
1119 * If an os entropy source is avaible then we declare the buffer content
1120 * as additional data by setting randomness to zero and trigger a regular
1128 if (randomness
> (double)seedlen
) {
1130 * The purpose of this check is to bound |randomness| by a
1131 * relatively small value in order to prevent an integer
1132 * overflow when multiplying by 8 in the rand_drbg_restart()
1133 * call below. Note that randomness is measured in bytes,
1134 * not bits, so this value corresponds to eight times the
1135 * security strength.
1137 randomness
= (double)seedlen
;
1140 ret
= rand_drbg_restart(drbg
, buf
, buflen
, (size_t)(8 * randomness
));
1141 rand_drbg_unlock(drbg
);
1146 /* Implements the default OpenSSL RAND_seed() method */
1147 static int drbg_seed(const void *buf
, int num
)
1149 return drbg_add(buf
, num
, num
);
1152 /* Implements the default OpenSSL RAND_status() method */
1153 static int drbg_status(void)
1156 RAND_DRBG
*drbg
= RAND_DRBG_get0_master();
1161 rand_drbg_lock(drbg
);
1162 ret
= drbg
->state
== DRBG_READY
? 1 : 0;
1163 rand_drbg_unlock(drbg
);
1168 * Get the master DRBG.
1169 * Returns pointer to the DRBG on success, NULL on failure.
1172 RAND_DRBG
*RAND_DRBG_get0_master(void)
1174 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
1181 * Get the public DRBG.
1182 * Returns pointer to the DRBG on success, NULL on failure.
1184 RAND_DRBG
*RAND_DRBG_get0_public(void)
1188 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
1191 drbg
= CRYPTO_THREAD_get_local(&public_drbg
);
1193 if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND
))
1195 drbg
= drbg_setup(master_drbg
, RAND_DRBG_TYPE_PUBLIC
);
1196 CRYPTO_THREAD_set_local(&public_drbg
, drbg
);
1202 * Get the private DRBG.
1203 * Returns pointer to the DRBG on success, NULL on failure.
1205 RAND_DRBG
*RAND_DRBG_get0_private(void)
1209 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
1212 drbg
= CRYPTO_THREAD_get_local(&private_drbg
);
1214 if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND
))
1216 drbg
= drbg_setup(master_drbg
, RAND_DRBG_TYPE_PRIVATE
);
1217 CRYPTO_THREAD_set_local(&private_drbg
, drbg
);
1222 RAND_METHOD rand_meth
= {
1231 RAND_METHOD
*RAND_OpenSSL(void)