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"
19 * Support framework for NIST SP 800-90A DRBG, AES-CTR mode.
20 * The RAND_DRBG is OpenSSL's pointer to an instance of the DRBG.
22 * The OpenSSL model is to have new and free functions, and that new
23 * does all initialization. That is not the NIST model, which has
24 * instantiation and un-instantiate, and re-use within a new/free
25 * lifecycle. (No doubt this comes from the desire to support hardware
26 * DRBG, where allocation of resources on something like an HSM is
27 * a much bigger deal than just re-setting an allocated resource.)
31 * THE THREE SHARED DRBGs
33 * There are three shared DRBGs (master, public and private), which are
34 * accessed concurrently by all threads.
38 * Not used directly by the application, only for reseeding the two other
39 * DRBGs. It reseeds itself by pulling either randomness from os entropy
40 * sources or by consuming randomnes which was added by RAND_add()
42 static RAND_DRBG
*drbg_master
;
46 * Used by default for generating random bytes using RAND_bytes().
48 static RAND_DRBG
*drbg_public
;
52 * Used by default for generating private keys using RAND_priv_bytes()
54 static RAND_DRBG
*drbg_private
;
58 * In addition there are DRBGs, which are not shared, but used only by a
59 * single thread at every time, for example the DRBGs which are owned by
60 * an SSL context. All DRBGs are organized in a hierarchical fashion
61 * with the <master> DRBG as root.
63 * This gives the following overall picture:
65 * <os entropy sources>
67 * RAND_add() ==> <master> \
68 * / \ | shared DRBGs (with locking)
69 * <public> <private> /
71 * <ssl> owned by an SSL context
75 * Before satisfying a generate request, a DRBG reseeds itself automatically,
76 * if one of the following two conditions holds:
78 * - the number of generate requests since the last reseeding exceeds a
79 * certain threshold, the so called |reseed_interval|. This behaviour
80 * can be disabled by setting the |reseed_interval| to 0.
82 * - the time elapsed since the last reseeding exceeds a certain time
83 * interval, the so called |reseed_time_interval|. This behaviour
84 * can be disabled by setting the |reseed_time_interval| to 0.
88 * For the three shared DRBGs (and only for these) there is another way to
89 * reseed them manually by calling RAND_seed() (or RAND_add() with a positive
90 * |randomness| argument). This will immediately reseed the <master> DRBG.
91 * The <public> and <private> DRBG will detect this on their next generate
92 * call and reseed, pulling randomness from <master>.
96 * The three shared DRBGs are intended to be used concurrently, so they
97 * support locking by default. It is the callers responsibility to wrap
98 * calls to functions like RAND_DRBG_generate() which modify the DRBGs
99 * internal state with calls to RAND_DRBG_lock() and RAND_DRBG_unlock().
100 * The functions RAND_bytes() and RAND_priv_bytes() take the locks
101 * automatically, so using the RAND api is thread safe as before.
103 * All other DRBG instances don't have locking enabled by default, because
104 * they are intendended to be used by a single thread. If it is desired,
105 * locking can be enabled using RAND_DRBG_enable_locking(). However, instead
106 * of accessing a single DRBG instance concurrently from different threads,
107 * it is recommended to instantiate a separate DRBG instance per thread.
111 /* NIST SP 800-90A DRBG recommends the use of a personalization string. */
112 static const char ossl_pers_string
[] = "OpenSSL NIST SP 800-90A DRBG";
114 static CRYPTO_ONCE rand_drbg_init
= CRYPTO_ONCE_STATIC_INIT
;
116 static RAND_DRBG
*drbg_setup(RAND_DRBG
*parent
);
117 static void drbg_cleanup(RAND_DRBG
*drbg
);
120 * Set/initialize |drbg| to be of type |nid|, with optional |flags|.
122 * Returns 1 on success, 0 on failure.
124 int RAND_DRBG_set(RAND_DRBG
*drbg
, int nid
, unsigned int flags
)
128 drbg
->state
= DRBG_UNINITIALISED
;
134 RANDerr(RAND_F_RAND_DRBG_SET
, RAND_R_UNSUPPORTED_DRBG_TYPE
);
137 /* Uninitialized; that's okay. */
139 case NID_aes_128_ctr
:
140 case NID_aes_192_ctr
:
141 case NID_aes_256_ctr
:
142 ret
= drbg_ctr_init(drbg
);
147 RANDerr(RAND_F_RAND_DRBG_SET
, RAND_R_ERROR_INITIALISING_DRBG
);
152 * Allocate memory and initialize a new DRBG. The |parent|, if not
153 * NULL, will be used to auto-seed this RAND_DRBG as needed.
155 * Returns a pointer to the new DRBG instance on success, NULL on failure.
157 RAND_DRBG
*RAND_DRBG_new(int type
, unsigned int flags
, RAND_DRBG
*parent
)
159 RAND_DRBG
*drbg
= OPENSSL_zalloc(sizeof(*drbg
));
162 RANDerr(RAND_F_RAND_DRBG_NEW
, ERR_R_MALLOC_FAILURE
);
165 drbg
->fork_count
= rand_fork_count
;
166 drbg
->parent
= parent
;
167 if (RAND_DRBG_set(drbg
, type
, flags
) == 0)
170 if (!RAND_DRBG_set_callbacks(drbg
, rand_drbg_get_entropy
,
171 rand_drbg_cleanup_entropy
,
183 * Uninstantiate |drbg| and free all memory.
185 void RAND_DRBG_free(RAND_DRBG
*drbg
)
190 if (drbg
->meth
!= NULL
)
191 drbg
->meth
->uninstantiate(drbg
);
192 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG
, drbg
, &drbg
->ex_data
);
193 OPENSSL_clear_free(drbg
, sizeof(*drbg
));
197 * Instantiate |drbg|, after it has been initialized. Use |pers| and
198 * |perslen| as prediction-resistance input.
200 * Requires that drbg->lock is already locked for write, if non-null.
202 * Returns 1 on success, 0 on failure.
204 int RAND_DRBG_instantiate(RAND_DRBG
*drbg
,
205 const unsigned char *pers
, size_t perslen
)
207 unsigned char *nonce
= NULL
, *entropy
= NULL
;
208 size_t noncelen
= 0, entropylen
= 0;
210 if (perslen
> drbg
->max_perslen
) {
211 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
212 RAND_R_PERSONALISATION_STRING_TOO_LONG
);
216 if (drbg
->meth
== NULL
)
218 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
219 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED
);
223 if (drbg
->state
!= DRBG_UNINITIALISED
) {
224 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
225 drbg
->state
== DRBG_ERROR
? RAND_R_IN_ERROR_STATE
226 : RAND_R_ALREADY_INSTANTIATED
);
230 drbg
->state
= DRBG_ERROR
;
231 if (drbg
->get_entropy
!= NULL
)
232 entropylen
= drbg
->get_entropy(drbg
, &entropy
, drbg
->strength
,
233 drbg
->min_entropylen
, drbg
->max_entropylen
);
234 if (entropylen
< drbg
->min_entropylen
235 || entropylen
> drbg
->max_entropylen
) {
236 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
, RAND_R_ERROR_RETRIEVING_ENTROPY
);
240 if (drbg
->max_noncelen
> 0 && drbg
->get_nonce
!= NULL
) {
241 noncelen
= drbg
->get_nonce(drbg
, &nonce
, drbg
->strength
/ 2,
242 drbg
->min_noncelen
, drbg
->max_noncelen
);
243 if (noncelen
< drbg
->min_noncelen
|| noncelen
> drbg
->max_noncelen
) {
244 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
245 RAND_R_ERROR_RETRIEVING_NONCE
);
250 if (!drbg
->meth
->instantiate(drbg
, entropy
, entropylen
,
251 nonce
, noncelen
, pers
, perslen
)) {
252 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
, RAND_R_ERROR_INSTANTIATING_DRBG
);
256 drbg
->state
= DRBG_READY
;
257 drbg
->generate_counter
= 0;
258 drbg
->reseed_time
= time(NULL
);
259 if (drbg
->reseed_counter
> 0) {
260 if (drbg
->parent
== NULL
)
261 drbg
->reseed_counter
++;
263 drbg
->reseed_counter
= drbg
->parent
->reseed_counter
;
267 if (entropy
!= NULL
&& drbg
->cleanup_entropy
!= NULL
)
268 drbg
->cleanup_entropy(drbg
, entropy
, entropylen
);
269 if (nonce
!= NULL
&& drbg
->cleanup_nonce
!= NULL
)
270 drbg
->cleanup_nonce(drbg
, nonce
, noncelen
);
271 if (drbg
->pool
!= NULL
) {
272 if (drbg
->state
== DRBG_READY
) {
273 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
274 RAND_R_ERROR_ENTROPY_POOL_WAS_IGNORED
);
275 drbg
->state
= DRBG_ERROR
;
277 RAND_POOL_free(drbg
->pool
);
280 if (drbg
->state
== DRBG_READY
)
286 * Uninstantiate |drbg|. Must be instantiated before it can be used.
288 * Requires that drbg->lock is already locked for write, if non-null.
290 * Returns 1 on success, 0 on failure.
292 int RAND_DRBG_uninstantiate(RAND_DRBG
*drbg
)
294 if (drbg
->meth
== NULL
)
296 RANDerr(RAND_F_RAND_DRBG_UNINSTANTIATE
,
297 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED
);
301 /* Clear the entire drbg->ctr struct, then reset some important
302 * members of the drbg->ctr struct (e.g. keysize, df_ks) to their
305 drbg
->meth
->uninstantiate(drbg
);
306 return RAND_DRBG_set(drbg
, drbg
->nid
, drbg
->flags
);
310 * Reseed |drbg|, mixing in the specified data
312 * Requires that drbg->lock is already locked for write, if non-null.
314 * Returns 1 on success, 0 on failure.
316 int RAND_DRBG_reseed(RAND_DRBG
*drbg
,
317 const unsigned char *adin
, size_t adinlen
)
319 unsigned char *entropy
= NULL
;
320 size_t entropylen
= 0;
322 if (drbg
->state
== DRBG_ERROR
) {
323 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_IN_ERROR_STATE
);
326 if (drbg
->state
== DRBG_UNINITIALISED
) {
327 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_NOT_INSTANTIATED
);
333 else if (adinlen
> drbg
->max_adinlen
) {
334 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
338 drbg
->state
= DRBG_ERROR
;
339 if (drbg
->get_entropy
!= NULL
)
340 entropylen
= drbg
->get_entropy(drbg
, &entropy
, drbg
->strength
,
341 drbg
->min_entropylen
, drbg
->max_entropylen
);
342 if (entropylen
< drbg
->min_entropylen
343 || entropylen
> drbg
->max_entropylen
) {
344 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_ERROR_RETRIEVING_ENTROPY
);
348 if (!drbg
->meth
->reseed(drbg
, entropy
, entropylen
, adin
, adinlen
))
351 drbg
->state
= DRBG_READY
;
352 drbg
->generate_counter
= 0;
353 drbg
->reseed_time
= time(NULL
);
354 if (drbg
->reseed_counter
> 0) {
355 if (drbg
->parent
== NULL
)
356 drbg
->reseed_counter
++;
358 drbg
->reseed_counter
= drbg
->parent
->reseed_counter
;
362 if (entropy
!= NULL
&& drbg
->cleanup_entropy
!= NULL
)
363 drbg
->cleanup_entropy(drbg
, entropy
, entropylen
);
364 if (drbg
->state
== DRBG_READY
)
370 * Restart |drbg|, using the specified entropy or additional input
372 * Tries its best to get the drbg instantiated by all means,
373 * regardless of its current state.
375 * Optionally, a |buffer| of |len| random bytes can be passed,
376 * which is assumed to contain at least |entropy| bits of entropy.
378 * If |entropy| > 0, the buffer content is used as entropy input.
380 * If |entropy| == 0, the buffer content is used as additional input
382 * Returns 1 on success, 0 on failure.
384 * This function is used internally only.
386 int rand_drbg_restart(RAND_DRBG
*drbg
,
387 const unsigned char *buffer
, size_t len
, size_t entropy
)
390 const unsigned char *adin
= NULL
;
393 if (drbg
->pool
!= NULL
) {
394 RANDerr(RAND_F_RAND_DRBG_RESTART
, ERR_R_INTERNAL_ERROR
);
395 RAND_POOL_free(drbg
->pool
);
399 if (buffer
!= NULL
) {
401 if (drbg
->max_entropylen
< len
) {
402 RANDerr(RAND_F_RAND_DRBG_RESTART
,
403 RAND_R_ENTROPY_INPUT_TOO_LONG
);
407 if (entropy
> 8 * len
) {
408 RANDerr(RAND_F_RAND_DRBG_RESTART
, RAND_R_ENTROPY_OUT_OF_RANGE
);
412 /* will be picked up by the rand_drbg_get_entropy() callback */
413 drbg
->pool
= RAND_POOL_new(entropy
, len
, len
);
414 if (drbg
->pool
== NULL
)
417 RAND_POOL_add(drbg
->pool
, buffer
, len
, entropy
);
419 if (drbg
->max_adinlen
< len
) {
420 RANDerr(RAND_F_RAND_DRBG_RESTART
,
421 RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
429 /* repair error state */
430 if (drbg
->state
== DRBG_ERROR
)
431 RAND_DRBG_uninstantiate(drbg
);
433 /* repair uninitialized state */
434 if (drbg
->state
== DRBG_UNINITIALISED
) {
435 /* reinstantiate drbg */
436 RAND_DRBG_instantiate(drbg
,
437 (const unsigned char *) ossl_pers_string
,
438 sizeof(ossl_pers_string
) - 1);
439 /* already reseeded. prevent second reseeding below */
440 reseeded
= (drbg
->state
== DRBG_READY
);
443 /* refresh current state if entropy or additional input has been provided */
444 if (drbg
->state
== DRBG_READY
) {
447 * mix in additional input without reseeding
449 * Similar to RAND_DRBG_reseed(), but the provided additional
450 * data |adin| is mixed into the current state without pulling
451 * entropy from the trusted entropy source using get_entropy().
452 * This is not a reseeding in the strict sense of NIST SP 800-90A.
454 drbg
->meth
->reseed(drbg
, adin
, adinlen
, NULL
, 0);
455 } else if (reseeded
== 0) {
456 /* do a full reseeding if it has not been done yet above */
457 RAND_DRBG_reseed(drbg
, NULL
, 0);
461 /* check whether a given entropy pool was cleared properly during reseed */
462 if (drbg
->pool
!= NULL
) {
463 drbg
->state
= DRBG_ERROR
;
464 RANDerr(RAND_F_RAND_DRBG_RESTART
, ERR_R_INTERNAL_ERROR
);
465 RAND_POOL_free(drbg
->pool
);
470 return drbg
->state
== DRBG_READY
;
474 * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
475 * to or if |prediction_resistance| is set. Additional input can be
476 * sent in |adin| and |adinlen|.
478 * Requires that drbg->lock is already locked for write, if non-null.
480 * Returns 1 on success, 0 on failure.
483 int RAND_DRBG_generate(RAND_DRBG
*drbg
, unsigned char *out
, size_t outlen
,
484 int prediction_resistance
,
485 const unsigned char *adin
, size_t adinlen
)
487 int reseed_required
= 0;
489 if (drbg
->state
!= DRBG_READY
) {
490 /* try to recover from previous errors */
491 rand_drbg_restart(drbg
, NULL
, 0, 0);
493 if (drbg
->state
== DRBG_ERROR
) {
494 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_IN_ERROR_STATE
);
497 if (drbg
->state
== DRBG_UNINITIALISED
) {
498 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_NOT_INSTANTIATED
);
503 if (outlen
> drbg
->max_request
) {
504 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG
);
507 if (adinlen
> drbg
->max_adinlen
) {
508 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
512 if (drbg
->fork_count
!= rand_fork_count
) {
513 drbg
->fork_count
= rand_fork_count
;
517 if (drbg
->reseed_interval
> 0) {
518 if (drbg
->generate_counter
>= drbg
->reseed_interval
)
521 if (drbg
->reseed_time_interval
> 0) {
522 time_t now
= time(NULL
);
523 if (now
< drbg
->reseed_time
524 || now
- drbg
->reseed_time
>= drbg
->reseed_time_interval
)
527 if (drbg
->reseed_counter
> 0 && drbg
->parent
!= NULL
) {
528 if (drbg
->reseed_counter
!= drbg
->parent
->reseed_counter
)
532 if (reseed_required
|| prediction_resistance
) {
533 if (!RAND_DRBG_reseed(drbg
, adin
, adinlen
)) {
534 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_RESEED_ERROR
);
541 if (!drbg
->meth
->generate(drbg
, out
, outlen
, adin
, adinlen
)) {
542 drbg
->state
= DRBG_ERROR
;
543 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_GENERATE_ERROR
);
547 drbg
->generate_counter
++;
553 * Generates |outlen| random bytes and stores them in |out|. It will
554 * using the given |drbg| to generate the bytes.
556 * Requires that drbg->lock is already locked for write, if non-null.
558 * Returns 1 on success 0 on failure.
560 int RAND_DRBG_bytes(RAND_DRBG
*drbg
, unsigned char *out
, size_t outlen
)
562 unsigned char *additional
= NULL
;
563 size_t additional_len
;
567 additional_len
= rand_drbg_get_additional_data(&additional
, drbg
->max_adinlen
);
569 for ( ; outlen
> 0; outlen
-= chunk
, out
+= chunk
) {
571 if (chunk
> drbg
->max_request
)
572 chunk
= drbg
->max_request
;
573 ret
= RAND_DRBG_generate(drbg
, out
, chunk
, 0, additional
, additional_len
);
580 if (additional_len
!= 0)
581 OPENSSL_secure_clear_free(additional
, additional_len
);
587 * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
589 * In the following, the signature and the semantics of the
590 * get_entropy() and cleanup_entropy() callbacks are explained.
594 * size_t get_entropy(RAND_DRBG *ctx,
595 * unsigned char **pout,
597 * size_t min_len, size_t max_len);
599 * This is a request to allocate and fill a buffer of size
600 * |min_len| <= size <= |max_len| (in bytes) which contains
601 * at least |entropy| bits of randomness. The buffer's address is
602 * to be returned in |*pout| and the number of collected
603 * randomness bytes (which may be less than the allocated size
604 * of the buffer) as return value.
606 * If the callback fails to acquire at least |entropy| bits of
607 * randomness, it shall return a buffer length of 0.
611 * void cleanup_entropy(RAND_DRBG *ctx,
612 * unsigned char *out, size_t outlen);
614 * A request to clear and free the buffer allocated by get_entropy().
615 * The values |out| and |outlen| are expected to be the random buffer's
616 * address and length, as returned by the get_entropy() callback.
618 * GET_NONCE, CLEANUP_NONCE
620 * Signature and semantics of the get_nonce() and cleanup_nonce()
621 * callbacks are analogous to get_entropy() and cleanup_entropy().
622 * Currently, the nonce is used only for the known answer tests.
624 int RAND_DRBG_set_callbacks(RAND_DRBG
*drbg
,
625 RAND_DRBG_get_entropy_fn get_entropy
,
626 RAND_DRBG_cleanup_entropy_fn cleanup_entropy
,
627 RAND_DRBG_get_nonce_fn get_nonce
,
628 RAND_DRBG_cleanup_nonce_fn cleanup_nonce
)
630 if (drbg
->state
!= DRBG_UNINITIALISED
)
632 drbg
->get_entropy
= get_entropy
;
633 drbg
->cleanup_entropy
= cleanup_entropy
;
634 drbg
->get_nonce
= get_nonce
;
635 drbg
->cleanup_nonce
= cleanup_nonce
;
640 * Set the reseed interval.
642 * The drbg will reseed automatically whenever the number of generate
643 * requests exceeds the given reseed interval. If the reseed interval
644 * is 0, then this feature is disabled.
646 * Returns 1 on success, 0 on failure.
648 int RAND_DRBG_set_reseed_interval(RAND_DRBG
*drbg
, unsigned int interval
)
650 if (interval
> MAX_RESEED_INTERVAL
)
652 drbg
->reseed_interval
= interval
;
657 * Set the reseed time interval.
659 * The drbg will reseed automatically whenever the time elapsed since
660 * the last reseeding exceeds the given reseed time interval. For safety,
661 * a reseeding will also occur if the clock has been reset to a smaller
664 * Returns 1 on success, 0 on failure.
666 int RAND_DRBG_set_reseed_time_interval(RAND_DRBG
*drbg
, time_t interval
)
668 if (interval
> MAX_RESEED_TIME_INTERVAL
)
670 drbg
->reseed_time_interval
= interval
;
676 * Locks the given drbg. Locking a drbg which does not have locking
677 * enabled is considered a successful no-op.
679 * Returns 1 on success, 0 on failure.
681 int RAND_DRBG_lock(RAND_DRBG
*drbg
)
683 if (drbg
->lock
!= NULL
)
684 return CRYPTO_THREAD_write_lock(drbg
->lock
);
690 * Unlocks the given drbg. Unlocking a drbg which does not have locking
691 * enabled is considered a successful no-op.
693 * Returns 1 on success, 0 on failure.
695 int RAND_DRBG_unlock(RAND_DRBG
*drbg
)
697 if (drbg
->lock
!= NULL
)
698 return CRYPTO_THREAD_unlock(drbg
->lock
);
704 * Enables locking for the given drbg
706 * Locking can only be enabled if the random generator
707 * is in the uninitialized state.
709 * Returns 1 on success, 0 on failure.
711 int RAND_DRBG_enable_locking(RAND_DRBG
*drbg
)
713 if (drbg
->state
!= DRBG_UNINITIALISED
) {
714 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING
,
715 RAND_R_DRBG_ALREADY_INITIALIZED
);
719 if (drbg
->lock
== NULL
) {
720 if (drbg
->parent
!= NULL
&& drbg
->lock
== NULL
) {
721 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING
,
722 RAND_R_PARENT_LOCKING_NOT_ENABLED
);
726 drbg
->lock
= CRYPTO_THREAD_lock_new();
727 if (drbg
->lock
== NULL
) {
728 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING
,
729 RAND_R_FAILED_TO_CREATE_LOCK
);
738 * Get and set the EXDATA
740 int RAND_DRBG_set_ex_data(RAND_DRBG
*drbg
, int idx
, void *arg
)
742 return CRYPTO_set_ex_data(&drbg
->ex_data
, idx
, arg
);
745 void *RAND_DRBG_get_ex_data(const RAND_DRBG
*drbg
, int idx
)
747 return CRYPTO_get_ex_data(&drbg
->ex_data
, idx
);
752 * The following functions provide a RAND_METHOD that works on the
753 * global DRBG. They lock.
757 * Allocates a new global DRBG on the secure heap (if enabled) and
758 * initializes it with default settings.
760 * Returns a pointer to the new DRBG instance on success, NULL on failure.
762 static RAND_DRBG
*drbg_setup(RAND_DRBG
*parent
)
766 drbg
= OPENSSL_secure_zalloc(sizeof(RAND_DRBG
));
770 drbg
->lock
= CRYPTO_THREAD_lock_new();
771 if (drbg
->lock
== NULL
) {
772 RANDerr(RAND_F_DRBG_SETUP
, RAND_R_FAILED_TO_CREATE_LOCK
);
776 if (RAND_DRBG_set(drbg
,
777 RAND_DRBG_NID
, RAND_DRBG_FLAG_CTR_USE_DF
) != 1)
779 if (RAND_DRBG_set_callbacks(drbg
, rand_drbg_get_entropy
,
780 rand_drbg_cleanup_entropy
, NULL
, NULL
) != 1)
783 if (parent
== NULL
) {
784 drbg
->reseed_interval
= MASTER_RESEED_INTERVAL
;
785 drbg
->reseed_time_interval
= MASTER_RESEED_TIME_INTERVAL
;
787 drbg
->parent
= parent
;
788 drbg
->reseed_interval
= SLAVE_RESEED_INTERVAL
;
789 drbg
->reseed_time_interval
= SLAVE_RESEED_TIME_INTERVAL
;
792 /* enable seed propagation */
793 drbg
->reseed_counter
= 1;
796 * Ignore instantiation error so support just-in-time instantiation.
798 * The state of the drbg will be checked in RAND_DRBG_generate() and
799 * an automatic recovery is attempted.
801 RAND_DRBG_instantiate(drbg
,
802 (const unsigned char *) ossl_pers_string
,
803 sizeof(ossl_pers_string
) - 1);
812 * Initialize the global DRBGs on first use.
813 * Returns 1 on success, 0 on failure.
815 DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init
)
818 * ensure that libcrypto is initialized, otherwise the
819 * DRBG locks are not cleaned up properly
821 if (!OPENSSL_init_crypto(0, NULL
))
824 drbg_master
= drbg_setup(NULL
);
825 drbg_public
= drbg_setup(drbg_master
);
826 drbg_private
= drbg_setup(drbg_master
);
828 if (drbg_master
== NULL
|| drbg_public
== NULL
|| drbg_private
== NULL
)
834 /* Cleans up the given global DRBG */
835 static void drbg_cleanup(RAND_DRBG
*drbg
)
838 RAND_DRBG_uninstantiate(drbg
);
839 CRYPTO_THREAD_lock_free(drbg
->lock
);
840 OPENSSL_secure_clear_free(drbg
, sizeof(RAND_DRBG
));
844 /* Clean up the global DRBGs before exit */
845 void rand_drbg_cleanup_int(void)
847 drbg_cleanup(drbg_private
);
848 drbg_cleanup(drbg_public
);
849 drbg_cleanup(drbg_master
);
851 drbg_private
= drbg_public
= drbg_master
= NULL
;
854 /* Implements the default OpenSSL RAND_bytes() method */
855 static int drbg_bytes(unsigned char *out
, int count
)
858 RAND_DRBG
*drbg
= RAND_DRBG_get0_public();
863 RAND_DRBG_lock(drbg
);
864 ret
= RAND_DRBG_bytes(drbg
, out
, count
);
865 RAND_DRBG_unlock(drbg
);
870 /* Implements the default OpenSSL RAND_add() method */
871 static int drbg_add(const void *buf
, int num
, double randomness
)
874 RAND_DRBG
*drbg
= RAND_DRBG_get0_master();
879 if (num
< 0 || randomness
< 0.0)
882 if (randomness
> (double)drbg
->max_entropylen
) {
884 * The purpose of this check is to bound |randomness| by a
885 * relatively small value in order to prevent an integer
886 * overflow when multiplying by 8 in the rand_drbg_restart()
892 RAND_DRBG_lock(drbg
);
893 ret
= rand_drbg_restart(drbg
, buf
,
894 (size_t)(unsigned int)num
,
895 (size_t)(8*randomness
));
896 RAND_DRBG_unlock(drbg
);
901 /* Implements the default OpenSSL RAND_seed() method */
902 static int drbg_seed(const void *buf
, int num
)
904 return drbg_add(buf
, num
, num
);
907 /* Implements the default OpenSSL RAND_status() method */
908 static int drbg_status(void)
911 RAND_DRBG
*drbg
= RAND_DRBG_get0_master();
916 RAND_DRBG_lock(drbg
);
917 ret
= drbg
->state
== DRBG_READY
? 1 : 0;
918 RAND_DRBG_unlock(drbg
);
923 * Get the master DRBG.
924 * Returns pointer to the DRBG on success, NULL on failure.
927 RAND_DRBG
*RAND_DRBG_get0_master(void)
929 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
936 * Get the public DRBG.
937 * Returns pointer to the DRBG on success, NULL on failure.
939 RAND_DRBG
*RAND_DRBG_get0_public(void)
941 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
948 * Get the private DRBG.
949 * Returns pointer to the DRBG on success, NULL on failure.
951 RAND_DRBG
*RAND_DRBG_get0_private(void)
953 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
959 RAND_METHOD rand_meth
= {
968 RAND_METHOD
*RAND_OpenSSL(void)