2 * Copyright 2011-2018 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>
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 typedef struct drbg_global_st
{
35 * The three shared DRBG instances
37 * There are three shared DRBG instances: <master>, <public>, and <private>.
43 * Not used directly by the application, only for reseeding the two other
44 * DRBGs. It reseeds itself by pulling either randomness from os entropy
45 * sources or by consuming randomness which was added by RAND_add().
47 * The <master> DRBG is a global instance which is accessed concurrently by
48 * all threads. The necessary locking is managed automatically by its child
49 * DRBG instances during reseeding.
51 RAND_DRBG
*master_drbg
;
55 * Used by default for generating random bytes using RAND_bytes().
57 * The <public> DRBG is thread-local, i.e., there is one instance per
60 CRYPTO_THREAD_LOCAL public_drbg
;
64 * Used by default for generating private keys using RAND_priv_bytes()
66 * The <private> DRBG is thread-local, i.e., there is one instance per
69 CRYPTO_THREAD_LOCAL private_drbg
;
72 /* NIST SP 800-90A DRBG recommends the use of a personalization string. */
73 static const char ossl_pers_string
[] = DRBG_DEFAULT_PERS_STRING
;
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(OPENSSL_CTX
*ctx
, RAND_DRBG
*parent
, int drbg_type
);
107 static RAND_DRBG
*rand_drbg_new(OPENSSL_CTX
*ctx
,
113 static int is_ctr(int type
)
116 case NID_aes_128_ctr
:
117 case NID_aes_192_ctr
:
118 case NID_aes_256_ctr
:
125 static int is_digest(int type
)
146 * Set/initialize |drbg| to be of type |type|, with optional |flags|.
148 * If |type| and |flags| are zero, use the defaults
150 * Returns 1 on success, 0 on failure.
152 int RAND_DRBG_set(RAND_DRBG
*drbg
, int type
, unsigned int flags
)
156 if (type
== 0 && flags
== 0) {
157 type
= rand_drbg_type
[RAND_DRBG_TYPE_MASTER
];
158 flags
= rand_drbg_flags
[RAND_DRBG_TYPE_MASTER
];
161 /* If set is called multiple times - clear the old one */
162 if (drbg
->type
!= 0 && (type
!= drbg
->type
|| flags
!= drbg
->flags
)) {
163 drbg
->meth
->uninstantiate(drbg
);
164 rand_pool_free(drbg
->adin_pool
);
165 drbg
->adin_pool
= NULL
;
168 drbg
->state
= DRBG_UNINITIALISED
;
173 /* Uninitialized; that's okay. */
176 } else if (is_ctr(type
)) {
177 ret
= drbg_ctr_init(drbg
);
178 } else if (is_digest(type
)) {
179 if (flags
& RAND_DRBG_FLAG_HMAC
)
180 ret
= drbg_hmac_init(drbg
);
182 ret
= drbg_hash_init(drbg
);
187 RANDerr(RAND_F_RAND_DRBG_SET
, RAND_R_UNSUPPORTED_DRBG_TYPE
);
192 drbg
->state
= DRBG_ERROR
;
193 RANDerr(RAND_F_RAND_DRBG_SET
, RAND_R_ERROR_INITIALISING_DRBG
);
199 * Set/initialize default |type| and |flag| for new drbg instances.
201 * Returns 1 on success, 0 on failure.
203 int RAND_DRBG_set_defaults(int type
, unsigned int flags
)
206 if (!(is_digest(type
) || is_ctr(type
))) {
207 RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS
, RAND_R_UNSUPPORTED_DRBG_TYPE
);
211 if ((flags
& ~rand_drbg_used_flags
) != 0) {
212 RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS
, RAND_R_UNSUPPORTED_DRBG_FLAGS
);
216 all
= ((flags
& RAND_DRBG_TYPE_FLAGS
) == 0);
217 if (all
|| (flags
& RAND_DRBG_FLAG_MASTER
) != 0) {
218 rand_drbg_type
[RAND_DRBG_TYPE_MASTER
] = type
;
219 rand_drbg_flags
[RAND_DRBG_TYPE_MASTER
] = flags
| RAND_DRBG_FLAG_MASTER
;
221 if (all
|| (flags
& RAND_DRBG_FLAG_PUBLIC
) != 0) {
222 rand_drbg_type
[RAND_DRBG_TYPE_PUBLIC
] = type
;
223 rand_drbg_flags
[RAND_DRBG_TYPE_PUBLIC
] = flags
| RAND_DRBG_FLAG_PUBLIC
;
225 if (all
|| (flags
& RAND_DRBG_FLAG_PRIVATE
) != 0) {
226 rand_drbg_type
[RAND_DRBG_TYPE_PRIVATE
] = type
;
227 rand_drbg_flags
[RAND_DRBG_TYPE_PRIVATE
] = flags
| RAND_DRBG_FLAG_PRIVATE
;
234 * Allocate memory and initialize a new DRBG. The DRBG is allocated on
235 * the secure heap if |secure| is nonzero and the secure heap is enabled.
236 * The |parent|, if not NULL, will be used as random source for reseeding.
238 * Returns a pointer to the new DRBG instance on success, NULL on failure.
240 static RAND_DRBG
*rand_drbg_new(OPENSSL_CTX
*ctx
,
246 RAND_DRBG
*drbg
= secure
? OPENSSL_secure_zalloc(sizeof(*drbg
))
247 : OPENSSL_zalloc(sizeof(*drbg
));
250 RANDerr(RAND_F_RAND_DRBG_NEW
, ERR_R_MALLOC_FAILURE
);
255 drbg
->secure
= secure
&& CRYPTO_secure_allocated(drbg
);
256 drbg
->fork_count
= rand_fork_count
;
257 drbg
->parent
= parent
;
259 if (parent
== NULL
) {
261 drbg
->get_entropy
= rand_crngt_get_entropy
;
262 drbg
->cleanup_entropy
= rand_crngt_cleanup_entropy
;
264 drbg
->get_entropy
= rand_drbg_get_entropy
;
265 drbg
->cleanup_entropy
= rand_drbg_cleanup_entropy
;
267 #ifndef RAND_DRBG_GET_RANDOM_NONCE
268 drbg
->get_nonce
= rand_drbg_get_nonce
;
269 drbg
->cleanup_nonce
= rand_drbg_cleanup_nonce
;
272 drbg
->reseed_interval
= master_reseed_interval
;
273 drbg
->reseed_time_interval
= master_reseed_time_interval
;
275 drbg
->get_entropy
= rand_drbg_get_entropy
;
276 drbg
->cleanup_entropy
= rand_drbg_cleanup_entropy
;
278 * Do not provide nonce callbacks, the child DRBGs will
279 * obtain their nonce using random bits from the parent.
282 drbg
->reseed_interval
= slave_reseed_interval
;
283 drbg
->reseed_time_interval
= slave_reseed_time_interval
;
286 if (RAND_DRBG_set(drbg
, type
, flags
) == 0)
289 if (parent
!= NULL
) {
290 rand_drbg_lock(parent
);
291 if (drbg
->strength
> parent
->strength
) {
293 * We currently don't support the algorithm from NIST SP 800-90C
294 * 10.1.2 to use a weaker DRBG as source
296 rand_drbg_unlock(parent
);
297 RANDerr(RAND_F_RAND_DRBG_NEW
, RAND_R_PARENT_STRENGTH_TOO_WEAK
);
300 rand_drbg_unlock(parent
);
306 RAND_DRBG_free(drbg
);
311 RAND_DRBG
*RAND_DRBG_new_ex(OPENSSL_CTX
*ctx
, int type
, unsigned int flags
,
314 return rand_drbg_new(ctx
, 0, type
, flags
, parent
);
317 RAND_DRBG
*RAND_DRBG_new(int type
, unsigned int flags
, RAND_DRBG
*parent
)
319 return RAND_DRBG_new_ex(NULL
, type
, flags
, parent
);
322 RAND_DRBG
*RAND_DRBG_secure_new_ex(OPENSSL_CTX
*ctx
, int type
,
323 unsigned int flags
, RAND_DRBG
*parent
)
325 return rand_drbg_new(ctx
, 1, type
, flags
, parent
);
328 RAND_DRBG
*RAND_DRBG_secure_new(int type
, unsigned int flags
, RAND_DRBG
*parent
)
330 return RAND_DRBG_secure_new_ex(NULL
, type
, flags
, parent
);
333 * Uninstantiate |drbg| and free all memory.
335 void RAND_DRBG_free(RAND_DRBG
*drbg
)
340 if (drbg
->meth
!= NULL
)
341 drbg
->meth
->uninstantiate(drbg
);
342 rand_pool_free(drbg
->adin_pool
);
343 CRYPTO_THREAD_lock_free(drbg
->lock
);
344 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG
, drbg
, &drbg
->ex_data
);
347 OPENSSL_secure_clear_free(drbg
, sizeof(*drbg
));
349 OPENSSL_clear_free(drbg
, sizeof(*drbg
));
353 * Instantiate |drbg|, after it has been initialized. Use |pers| and
354 * |perslen| as prediction-resistance input.
356 * Requires that drbg->lock is already locked for write, if non-null.
358 * Returns 1 on success, 0 on failure.
360 int RAND_DRBG_instantiate(RAND_DRBG
*drbg
,
361 const unsigned char *pers
, size_t perslen
)
363 unsigned char *nonce
= NULL
, *entropy
= NULL
;
364 size_t noncelen
= 0, entropylen
= 0;
365 size_t min_entropy
= drbg
->strength
;
366 size_t min_entropylen
= drbg
->min_entropylen
;
367 size_t max_entropylen
= drbg
->max_entropylen
;
369 if (perslen
> drbg
->max_perslen
) {
370 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
371 RAND_R_PERSONALISATION_STRING_TOO_LONG
);
375 if (drbg
->meth
== NULL
) {
376 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
377 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED
);
381 if (drbg
->state
!= DRBG_UNINITIALISED
) {
382 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
383 drbg
->state
== DRBG_ERROR
? RAND_R_IN_ERROR_STATE
384 : RAND_R_ALREADY_INSTANTIATED
);
388 drbg
->state
= DRBG_ERROR
;
391 * NIST SP800-90Ar1 section 9.1 says you can combine getting the entropy
392 * and nonce in 1 call by increasing the entropy with 50% and increasing
393 * the minimum length to accomadate the length of the nonce.
394 * We do this in case a nonce is require and get_nonce is NULL.
396 if (drbg
->min_noncelen
> 0 && drbg
->get_nonce
== NULL
) {
397 min_entropy
+= drbg
->strength
/ 2;
398 min_entropylen
+= drbg
->min_noncelen
;
399 max_entropylen
+= drbg
->max_noncelen
;
402 drbg
->reseed_next_counter
= tsan_load(&drbg
->reseed_prop_counter
);
403 if (drbg
->reseed_next_counter
) {
404 drbg
->reseed_next_counter
++;
405 if(!drbg
->reseed_next_counter
)
406 drbg
->reseed_next_counter
= 1;
409 if (drbg
->get_entropy
!= NULL
)
410 entropylen
= drbg
->get_entropy(drbg
, &entropy
, min_entropy
,
411 min_entropylen
, max_entropylen
, 0);
412 if (entropylen
< min_entropylen
413 || entropylen
> max_entropylen
) {
414 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
, RAND_R_ERROR_RETRIEVING_ENTROPY
);
418 if (drbg
->min_noncelen
> 0 && drbg
->get_nonce
!= NULL
) {
419 noncelen
= drbg
->get_nonce(drbg
, &nonce
, drbg
->strength
/ 2,
420 drbg
->min_noncelen
, drbg
->max_noncelen
);
421 if (noncelen
< drbg
->min_noncelen
|| noncelen
> drbg
->max_noncelen
) {
422 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
, RAND_R_ERROR_RETRIEVING_NONCE
);
427 if (!drbg
->meth
->instantiate(drbg
, entropy
, entropylen
,
428 nonce
, noncelen
, pers
, perslen
)) {
429 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
, RAND_R_ERROR_INSTANTIATING_DRBG
);
433 drbg
->state
= DRBG_READY
;
434 drbg
->reseed_gen_counter
= 1;
435 drbg
->reseed_time
= time(NULL
);
436 tsan_store(&drbg
->reseed_prop_counter
, drbg
->reseed_next_counter
);
439 if (entropy
!= NULL
&& drbg
->cleanup_entropy
!= NULL
)
440 drbg
->cleanup_entropy(drbg
, entropy
, entropylen
);
441 if (nonce
!= NULL
&& drbg
->cleanup_nonce
!= NULL
)
442 drbg
->cleanup_nonce(drbg
, nonce
, noncelen
);
443 if (drbg
->state
== DRBG_READY
)
449 * Uninstantiate |drbg|. Must be instantiated before it can be used.
451 * Requires that drbg->lock is already locked for write, if non-null.
453 * Returns 1 on success, 0 on failure.
455 int RAND_DRBG_uninstantiate(RAND_DRBG
*drbg
)
457 int index
= -1, type
, flags
;
458 if (drbg
->meth
== NULL
) {
459 drbg
->state
= DRBG_ERROR
;
460 RANDerr(RAND_F_RAND_DRBG_UNINSTANTIATE
,
461 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED
);
465 /* Clear the entire drbg->ctr struct, then reset some important
466 * members of the drbg->ctr struct (e.g. keysize, df_ks) to their
469 drbg
->meth
->uninstantiate(drbg
);
471 /* The reset uses the default values for type and flags */
472 if (drbg
->flags
& RAND_DRBG_FLAG_MASTER
)
473 index
= RAND_DRBG_TYPE_MASTER
;
474 else if (drbg
->flags
& RAND_DRBG_FLAG_PRIVATE
)
475 index
= RAND_DRBG_TYPE_PRIVATE
;
476 else if (drbg
->flags
& RAND_DRBG_FLAG_PUBLIC
)
477 index
= RAND_DRBG_TYPE_PUBLIC
;
480 flags
= rand_drbg_flags
[index
];
481 type
= rand_drbg_type
[index
];
486 return RAND_DRBG_set(drbg
, type
, flags
);
490 * Reseed |drbg|, mixing in the specified data
492 * Requires that drbg->lock is already locked for write, if non-null.
494 * Returns 1 on success, 0 on failure.
496 int RAND_DRBG_reseed(RAND_DRBG
*drbg
,
497 const unsigned char *adin
, size_t adinlen
,
498 int prediction_resistance
)
500 unsigned char *entropy
= NULL
;
501 size_t entropylen
= 0;
503 if (drbg
->state
== DRBG_ERROR
) {
504 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_IN_ERROR_STATE
);
507 if (drbg
->state
== DRBG_UNINITIALISED
) {
508 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_NOT_INSTANTIATED
);
514 } else if (adinlen
> drbg
->max_adinlen
) {
515 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
519 drbg
->state
= DRBG_ERROR
;
521 drbg
->reseed_next_counter
= tsan_load(&drbg
->reseed_prop_counter
);
522 if (drbg
->reseed_next_counter
) {
523 drbg
->reseed_next_counter
++;
524 if(!drbg
->reseed_next_counter
)
525 drbg
->reseed_next_counter
= 1;
528 if (drbg
->get_entropy
!= NULL
)
529 entropylen
= drbg
->get_entropy(drbg
, &entropy
, drbg
->strength
,
530 drbg
->min_entropylen
,
531 drbg
->max_entropylen
,
532 prediction_resistance
);
533 if (entropylen
< drbg
->min_entropylen
534 || entropylen
> drbg
->max_entropylen
) {
535 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_ERROR_RETRIEVING_ENTROPY
);
539 if (!drbg
->meth
->reseed(drbg
, entropy
, entropylen
, adin
, adinlen
))
542 drbg
->state
= DRBG_READY
;
543 drbg
->reseed_gen_counter
= 1;
544 drbg
->reseed_time
= time(NULL
);
545 tsan_store(&drbg
->reseed_prop_counter
, drbg
->reseed_next_counter
);
548 if (entropy
!= NULL
&& drbg
->cleanup_entropy
!= NULL
)
549 drbg
->cleanup_entropy(drbg
, entropy
, entropylen
);
550 if (drbg
->state
== DRBG_READY
)
556 * Restart |drbg|, using the specified entropy or additional input
558 * Tries its best to get the drbg instantiated by all means,
559 * regardless of its current state.
561 * Optionally, a |buffer| of |len| random bytes can be passed,
562 * which is assumed to contain at least |entropy| bits of entropy.
564 * If |entropy| > 0, the buffer content is used as entropy input.
566 * If |entropy| == 0, the buffer content is used as additional input
568 * Returns 1 on success, 0 on failure.
570 * This function is used internally only.
572 int rand_drbg_restart(RAND_DRBG
*drbg
,
573 const unsigned char *buffer
, size_t len
, size_t entropy
)
576 const unsigned char *adin
= NULL
;
579 if (drbg
->seed_pool
!= NULL
) {
580 RANDerr(RAND_F_RAND_DRBG_RESTART
, ERR_R_INTERNAL_ERROR
);
581 drbg
->state
= DRBG_ERROR
;
582 rand_pool_free(drbg
->seed_pool
);
583 drbg
->seed_pool
= NULL
;
587 if (buffer
!= NULL
) {
589 if (drbg
->max_entropylen
< len
) {
590 RANDerr(RAND_F_RAND_DRBG_RESTART
,
591 RAND_R_ENTROPY_INPUT_TOO_LONG
);
592 drbg
->state
= DRBG_ERROR
;
596 if (entropy
> 8 * len
) {
597 RANDerr(RAND_F_RAND_DRBG_RESTART
, RAND_R_ENTROPY_OUT_OF_RANGE
);
598 drbg
->state
= DRBG_ERROR
;
602 /* will be picked up by the rand_drbg_get_entropy() callback */
603 drbg
->seed_pool
= rand_pool_attach(buffer
, len
, entropy
);
604 if (drbg
->seed_pool
== NULL
)
607 if (drbg
->max_adinlen
< len
) {
608 RANDerr(RAND_F_RAND_DRBG_RESTART
,
609 RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
610 drbg
->state
= DRBG_ERROR
;
618 /* repair error state */
619 if (drbg
->state
== DRBG_ERROR
)
620 RAND_DRBG_uninstantiate(drbg
);
622 /* repair uninitialized state */
623 if (drbg
->state
== DRBG_UNINITIALISED
) {
624 /* reinstantiate drbg */
625 RAND_DRBG_instantiate(drbg
,
626 (const unsigned char *) ossl_pers_string
,
627 sizeof(ossl_pers_string
) - 1);
628 /* already reseeded. prevent second reseeding below */
629 reseeded
= (drbg
->state
== DRBG_READY
);
632 /* refresh current state if entropy or additional input has been provided */
633 if (drbg
->state
== DRBG_READY
) {
636 * mix in additional input without reseeding
638 * Similar to RAND_DRBG_reseed(), but the provided additional
639 * data |adin| is mixed into the current state without pulling
640 * entropy from the trusted entropy source using get_entropy().
641 * This is not a reseeding in the strict sense of NIST SP 800-90A.
643 drbg
->meth
->reseed(drbg
, adin
, adinlen
, NULL
, 0);
644 } else if (reseeded
== 0) {
645 /* do a full reseeding if it has not been done yet above */
646 RAND_DRBG_reseed(drbg
, NULL
, 0, 0);
650 rand_pool_free(drbg
->seed_pool
);
651 drbg
->seed_pool
= NULL
;
653 return drbg
->state
== DRBG_READY
;
657 * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
658 * to or if |prediction_resistance| is set. Additional input can be
659 * sent in |adin| and |adinlen|.
661 * Requires that drbg->lock is already locked for write, if non-null.
663 * Returns 1 on success, 0 on failure.
666 int RAND_DRBG_generate(RAND_DRBG
*drbg
, unsigned char *out
, size_t outlen
,
667 int prediction_resistance
,
668 const unsigned char *adin
, size_t adinlen
)
670 int reseed_required
= 0;
672 if (drbg
->state
!= DRBG_READY
) {
673 /* try to recover from previous errors */
674 rand_drbg_restart(drbg
, NULL
, 0, 0);
676 if (drbg
->state
== DRBG_ERROR
) {
677 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_IN_ERROR_STATE
);
680 if (drbg
->state
== DRBG_UNINITIALISED
) {
681 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_NOT_INSTANTIATED
);
686 if (outlen
> drbg
->max_request
) {
687 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG
);
690 if (adinlen
> drbg
->max_adinlen
) {
691 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
695 if (drbg
->fork_count
!= rand_fork_count
) {
696 drbg
->fork_count
= rand_fork_count
;
700 if (drbg
->reseed_interval
> 0) {
701 if (drbg
->reseed_gen_counter
> drbg
->reseed_interval
)
704 if (drbg
->reseed_time_interval
> 0) {
705 time_t now
= time(NULL
);
706 if (now
< drbg
->reseed_time
707 || now
- drbg
->reseed_time
>= drbg
->reseed_time_interval
)
710 if (drbg
->parent
!= NULL
) {
711 unsigned int reseed_counter
= tsan_load(&drbg
->reseed_prop_counter
);
712 if (reseed_counter
> 0
713 && tsan_load(&drbg
->parent
->reseed_prop_counter
)
718 if (reseed_required
|| prediction_resistance
) {
719 if (!RAND_DRBG_reseed(drbg
, adin
, adinlen
, prediction_resistance
)) {
720 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_RESEED_ERROR
);
727 if (!drbg
->meth
->generate(drbg
, out
, outlen
, adin
, adinlen
)) {
728 drbg
->state
= DRBG_ERROR
;
729 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_GENERATE_ERROR
);
733 drbg
->reseed_gen_counter
++;
739 * Generates |outlen| random bytes and stores them in |out|. It will
740 * using the given |drbg| to generate the bytes.
742 * Requires that drbg->lock is already locked for write, if non-null.
744 * Returns 1 on success 0 on failure.
746 int RAND_DRBG_bytes(RAND_DRBG
*drbg
, unsigned char *out
, size_t outlen
)
748 unsigned char *additional
= NULL
;
749 size_t additional_len
;
753 if (drbg
->adin_pool
== NULL
) {
756 drbg
->adin_pool
= rand_pool_new(0, 0, drbg
->max_adinlen
);
757 if (drbg
->adin_pool
== NULL
)
761 additional_len
= rand_drbg_get_additional_data(drbg
->adin_pool
,
764 for ( ; outlen
> 0; outlen
-= chunk
, out
+= chunk
) {
766 if (chunk
> drbg
->max_request
)
767 chunk
= drbg
->max_request
;
768 ret
= RAND_DRBG_generate(drbg
, out
, chunk
, 0, additional
, additional_len
);
775 if (additional
!= NULL
)
776 rand_drbg_cleanup_additional_data(drbg
->adin_pool
, additional
);
782 * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
784 * Setting the callbacks is allowed only if the drbg has not been
785 * initialized yet. Otherwise, the operation will fail.
787 * Returns 1 on success, 0 on failure.
789 int RAND_DRBG_set_callbacks(RAND_DRBG
*drbg
,
790 RAND_DRBG_get_entropy_fn get_entropy
,
791 RAND_DRBG_cleanup_entropy_fn cleanup_entropy
,
792 RAND_DRBG_get_nonce_fn get_nonce
,
793 RAND_DRBG_cleanup_nonce_fn cleanup_nonce
)
795 if (drbg
->state
!= DRBG_UNINITIALISED
796 || drbg
->parent
!= NULL
)
798 drbg
->get_entropy
= get_entropy
;
799 drbg
->cleanup_entropy
= cleanup_entropy
;
800 drbg
->get_nonce
= get_nonce
;
801 drbg
->cleanup_nonce
= cleanup_nonce
;
806 * Set the reseed interval.
808 * The drbg will reseed automatically whenever the number of generate
809 * requests exceeds the given reseed interval. If the reseed interval
810 * is 0, then this feature is disabled.
812 * Returns 1 on success, 0 on failure.
814 int RAND_DRBG_set_reseed_interval(RAND_DRBG
*drbg
, unsigned int interval
)
816 if (interval
> MAX_RESEED_INTERVAL
)
818 drbg
->reseed_interval
= interval
;
823 * Set the reseed time interval.
825 * The drbg will reseed automatically whenever the time elapsed since
826 * the last reseeding exceeds the given reseed time interval. For safety,
827 * a reseeding will also occur if the clock has been reset to a smaller
830 * Returns 1 on success, 0 on failure.
832 int RAND_DRBG_set_reseed_time_interval(RAND_DRBG
*drbg
, time_t interval
)
834 if (interval
> MAX_RESEED_TIME_INTERVAL
)
836 drbg
->reseed_time_interval
= interval
;
841 * Set the default values for reseed (time) intervals of new DRBG instances
843 * The default values can be set independently for master DRBG instances
844 * (without a parent) and slave DRBG instances (with parent).
846 * Returns 1 on success, 0 on failure.
849 int RAND_DRBG_set_reseed_defaults(
850 unsigned int _master_reseed_interval
,
851 unsigned int _slave_reseed_interval
,
852 time_t _master_reseed_time_interval
,
853 time_t _slave_reseed_time_interval
856 if (_master_reseed_interval
> MAX_RESEED_INTERVAL
857 || _slave_reseed_interval
> MAX_RESEED_INTERVAL
)
860 if (_master_reseed_time_interval
> MAX_RESEED_TIME_INTERVAL
861 || _slave_reseed_time_interval
> MAX_RESEED_TIME_INTERVAL
)
864 master_reseed_interval
= _master_reseed_interval
;
865 slave_reseed_interval
= _slave_reseed_interval
;
867 master_reseed_time_interval
= _master_reseed_time_interval
;
868 slave_reseed_time_interval
= _slave_reseed_time_interval
;
874 * Locks the given drbg. Locking a drbg which does not have locking
875 * enabled is considered a successful no-op.
877 * Returns 1 on success, 0 on failure.
879 int rand_drbg_lock(RAND_DRBG
*drbg
)
881 if (drbg
->lock
!= NULL
)
882 return CRYPTO_THREAD_write_lock(drbg
->lock
);
888 * Unlocks the given drbg. Unlocking a drbg which does not have locking
889 * enabled is considered a successful no-op.
891 * Returns 1 on success, 0 on failure.
893 int rand_drbg_unlock(RAND_DRBG
*drbg
)
895 if (drbg
->lock
!= NULL
)
896 return CRYPTO_THREAD_unlock(drbg
->lock
);
902 * Enables locking for the given drbg
904 * Locking can only be enabled if the random generator
905 * is in the uninitialized state.
907 * Returns 1 on success, 0 on failure.
909 int rand_drbg_enable_locking(RAND_DRBG
*drbg
)
911 if (drbg
->state
!= DRBG_UNINITIALISED
) {
912 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING
,
913 RAND_R_DRBG_ALREADY_INITIALIZED
);
917 if (drbg
->lock
== NULL
) {
918 if (drbg
->parent
!= NULL
&& drbg
->parent
->lock
== NULL
) {
919 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING
,
920 RAND_R_PARENT_LOCKING_NOT_ENABLED
);
924 drbg
->lock
= CRYPTO_THREAD_lock_new();
925 if (drbg
->lock
== NULL
) {
926 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING
,
927 RAND_R_FAILED_TO_CREATE_LOCK
);
936 * Get and set the EXDATA
938 int RAND_DRBG_set_ex_data(RAND_DRBG
*drbg
, int idx
, void *arg
)
940 return CRYPTO_set_ex_data(&drbg
->ex_data
, idx
, arg
);
943 void *RAND_DRBG_get_ex_data(const RAND_DRBG
*drbg
, int idx
)
945 return CRYPTO_get_ex_data(&drbg
->ex_data
, idx
);
950 * The following functions provide a RAND_METHOD that works on the
951 * global DRBG. They lock.
955 * Allocates a new global DRBG on the secure heap (if enabled) and
956 * initializes it with default settings.
958 * Returns a pointer to the new DRBG instance on success, NULL on failure.
960 static RAND_DRBG
*drbg_setup(OPENSSL_CTX
*ctx
, RAND_DRBG
*parent
, int drbg_type
)
964 drbg
= RAND_DRBG_secure_new_ex(ctx
, rand_drbg_type
[drbg_type
],
965 rand_drbg_flags
[drbg_type
], parent
);
969 /* Only the master DRBG needs to have a lock */
970 if (parent
== NULL
&& rand_drbg_enable_locking(drbg
) == 0)
973 /* enable seed propagation */
974 tsan_store(&drbg
->reseed_prop_counter
, 1);
977 * Ignore instantiation error to support just-in-time instantiation.
979 * The state of the drbg will be checked in RAND_DRBG_generate() and
980 * an automatic recovery is attempted.
982 (void)RAND_DRBG_instantiate(drbg
,
983 (const unsigned char *) ossl_pers_string
,
984 sizeof(ossl_pers_string
) - 1);
988 RAND_DRBG_free(drbg
);
993 * Initialize the OPENSSL_CTX global DRBGs on first use.
994 * Returns the allocated global data on success or NULL on failure.
996 static void *drbg_ossl_ctx_new(OPENSSL_CTX
*libctx
)
998 DRBG_GLOBAL
*dgbl
= OPENSSL_zalloc(sizeof(*dgbl
));
1003 if (!CRYPTO_THREAD_init_local(&dgbl
->private_drbg
, NULL
))
1006 if (!CRYPTO_THREAD_init_local(&dgbl
->public_drbg
, NULL
))
1009 dgbl
->master_drbg
= drbg_setup(libctx
, NULL
, RAND_DRBG_TYPE_MASTER
);
1010 if (dgbl
->master_drbg
== NULL
)
1016 CRYPTO_THREAD_cleanup_local(&dgbl
->public_drbg
);
1018 CRYPTO_THREAD_cleanup_local(&dgbl
->private_drbg
);
1024 static void drbg_ossl_ctx_free(void *vdgbl
)
1026 DRBG_GLOBAL
*dgbl
= vdgbl
;
1028 RAND_DRBG_free(dgbl
->master_drbg
);
1029 CRYPTO_THREAD_cleanup_local(&dgbl
->private_drbg
);
1030 CRYPTO_THREAD_cleanup_local(&dgbl
->public_drbg
);
1035 static const OPENSSL_CTX_METHOD drbg_ossl_ctx_method
= {
1040 static DRBG_GLOBAL
*drbg_get_global(OPENSSL_CTX
*libctx
)
1042 return openssl_ctx_get_data(libctx
, OPENSSL_CTX_DRBG_INDEX
,
1043 &drbg_ossl_ctx_method
);
1046 void drbg_delete_thread_state(void)
1048 DRBG_GLOBAL
*dgbl
= drbg_get_global(ctx
);
1053 drbg
= CRYPTO_THREAD_get_local(&dgbl
->public_drbg
);
1054 CRYPTO_THREAD_set_local(&dgbl
->public_drbg
, NULL
);
1055 RAND_DRBG_free(drbg
);
1057 drbg
= CRYPTO_THREAD_get_local(&dgbl
->private_drbg
);
1058 CRYPTO_THREAD_set_local(&dgbl
->private_drbg
, NULL
);
1059 RAND_DRBG_free(drbg
);
1062 /* Implements the default OpenSSL RAND_bytes() method */
1063 static int drbg_bytes(unsigned char *out
, int count
)
1066 RAND_DRBG
*drbg
= RAND_DRBG_get0_public();
1071 ret
= RAND_DRBG_bytes(drbg
, out
, count
);
1077 * Calculates the minimum length of a full entropy buffer
1078 * which is necessary to seed (i.e. instantiate) the DRBG
1081 size_t rand_drbg_seedlen(RAND_DRBG
*drbg
)
1084 * If no os entropy source is available then RAND_seed(buffer, bufsize)
1085 * is expected to succeed if and only if the buffer length satisfies
1086 * the following requirements, which follow from the calculations
1087 * in RAND_DRBG_instantiate().
1089 size_t min_entropy
= drbg
->strength
;
1090 size_t min_entropylen
= drbg
->min_entropylen
;
1093 * Extra entropy for the random nonce in the absence of a
1094 * get_nonce callback, see comment in RAND_DRBG_instantiate().
1096 if (drbg
->min_noncelen
> 0 && drbg
->get_nonce
== NULL
) {
1097 min_entropy
+= drbg
->strength
/ 2;
1098 min_entropylen
+= drbg
->min_noncelen
;
1102 * Convert entropy requirement from bits to bytes
1103 * (dividing by 8 without rounding upwards, because
1104 * all entropy requirements are divisible by 8).
1108 /* Return a value that satisfies both requirements */
1109 return min_entropy
> min_entropylen
? min_entropy
: min_entropylen
;
1112 /* Implements the default OpenSSL RAND_add() method */
1113 static int drbg_add(const void *buf
, int num
, double randomness
)
1116 RAND_DRBG
*drbg
= RAND_DRBG_get0_master();
1123 if (num
< 0 || randomness
< 0.0)
1126 rand_drbg_lock(drbg
);
1127 seedlen
= rand_drbg_seedlen(drbg
);
1129 buflen
= (size_t)num
;
1133 * NIST SP-800-90A mandates that entropy *shall not* be provided
1134 * by the consuming application. By setting the randomness to zero,
1135 * we ensure that the buffer contents will be added to the internal
1136 * state of the DRBG only as additional data.
1138 * (NIST SP-800-90Ar1, Sections 9.1 and 9.2)
1142 if (buflen
< seedlen
|| randomness
< (double) seedlen
) {
1143 #if defined(OPENSSL_RAND_SEED_NONE)
1145 * If no os entropy source is available, a reseeding will fail
1146 * inevitably. So we use a trick to mix the buffer contents into
1147 * the DRBG state without forcing a reseeding: we generate a
1148 * dummy random byte, using the buffer content as additional data.
1149 * Note: This won't work with RAND_DRBG_FLAG_CTR_NO_DF.
1151 unsigned char dummy
[1];
1153 ret
= RAND_DRBG_generate(drbg
, dummy
, sizeof(dummy
), 0, buf
, buflen
);
1154 rand_drbg_unlock(drbg
);
1158 * If an os entropy source is available then we declare the buffer content
1159 * as additional data by setting randomness to zero and trigger a regular
1166 if (randomness
> (double)seedlen
) {
1168 * The purpose of this check is to bound |randomness| by a
1169 * relatively small value in order to prevent an integer
1170 * overflow when multiplying by 8 in the rand_drbg_restart()
1171 * call below. Note that randomness is measured in bytes,
1172 * not bits, so this value corresponds to eight times the
1173 * security strength.
1175 randomness
= (double)seedlen
;
1178 ret
= rand_drbg_restart(drbg
, buf
, buflen
, (size_t)(8 * randomness
));
1179 rand_drbg_unlock(drbg
);
1184 /* Implements the default OpenSSL RAND_seed() method */
1185 static int drbg_seed(const void *buf
, int num
)
1187 return drbg_add(buf
, num
, num
);
1190 /* Implements the default OpenSSL RAND_status() method */
1191 static int drbg_status(void)
1194 RAND_DRBG
*drbg
= RAND_DRBG_get0_master();
1199 rand_drbg_lock(drbg
);
1200 ret
= drbg
->state
== DRBG_READY
? 1 : 0;
1201 rand_drbg_unlock(drbg
);
1206 * Get the master DRBG.
1207 * Returns pointer to the DRBG on success, NULL on failure.
1210 RAND_DRBG
*OPENSSL_CTX_get0_master_drbg(OPENSSL_CTX
*ctx
)
1212 DRBG_GLOBAL
*dgbl
= drbg_get_global(ctx
);
1217 return dgbl
->master_drbg
;
1220 RAND_DRBG
*RAND_DRBG_get0_master(void)
1222 return OPENSSL_CTX_get0_master_drbg(NULL
);
1226 * Get the public DRBG.
1227 * Returns pointer to the DRBG on success, NULL on failure.
1229 RAND_DRBG
*OPENSSL_CTX_get0_public_drbg(OPENSSL_CTX
*ctx
)
1231 DRBG_GLOBAL
*dgbl
= drbg_get_global(ctx
);
1237 drbg
= CRYPTO_THREAD_get_local(&dgbl
->public_drbg
);
1239 if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND
))
1241 drbg
= drbg_setup(ctx
, dgbl
->master_drbg
, RAND_DRBG_TYPE_PUBLIC
);
1242 CRYPTO_THREAD_set_local(&dgbl
->public_drbg
, drbg
);
1247 RAND_DRBG
*RAND_DRBG_get0_public(void)
1249 return OPENSSL_CTX_get0_public_drbg(NULL
);
1253 * Get the private DRBG.
1254 * Returns pointer to the DRBG on success, NULL on failure.
1256 RAND_DRBG
*OPENSSL_CTX_get0_private_drbg(OPENSSL_CTX
*ctx
)
1258 DRBG_GLOBAL
*dgbl
= drbg_get_global(ctx
);
1264 drbg
= CRYPTO_THREAD_get_local(&dgbl
->private_drbg
);
1266 if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND
))
1268 drbg
= drbg_setup(ctx
, dgbl
->master_drbg
, RAND_DRBG_TYPE_PRIVATE
);
1269 CRYPTO_THREAD_set_local(&dgbl
->private_drbg
, drbg
);
1274 RAND_DRBG
*RAND_DRBG_get0_private(void)
1276 return OPENSSL_CTX_get0_private_drbg(NULL
);
1279 RAND_METHOD rand_meth
= {
1288 RAND_METHOD
*RAND_OpenSSL(void)