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 /* NIST SP 800-90A DRBG recommends the use of a personalization string. */
97 static const char ossl_pers_string
[] = "OpenSSL NIST SP 800-90A DRBG";
99 static CRYPTO_ONCE rand_drbg_init
= CRYPTO_ONCE_STATIC_INIT
;
101 static RAND_DRBG
*drbg_setup(RAND_DRBG
*parent
);
102 static void drbg_cleanup(RAND_DRBG
*drbg
);
105 * Set/initialize |drbg| to be of type |nid|, with optional |flags|.
107 * Returns 1 on success, 0 on failure.
109 int RAND_DRBG_set(RAND_DRBG
*drbg
, int nid
, unsigned int flags
)
113 drbg
->state
= DRBG_UNINITIALISED
;
119 RANDerr(RAND_F_RAND_DRBG_SET
, RAND_R_UNSUPPORTED_DRBG_TYPE
);
122 /* Uninitialized; that's okay. */
124 case NID_aes_128_ctr
:
125 case NID_aes_192_ctr
:
126 case NID_aes_256_ctr
:
127 ret
= drbg_ctr_init(drbg
);
132 RANDerr(RAND_F_RAND_DRBG_SET
, RAND_R_ERROR_INITIALISING_DRBG
);
137 * Allocate memory and initialize a new DRBG. The |parent|, if not
138 * NULL, will be used to auto-seed this RAND_DRBG as needed.
140 * Returns a pointer to the new DRBG instance on success, NULL on failure.
142 RAND_DRBG
*RAND_DRBG_new(int type
, unsigned int flags
, RAND_DRBG
*parent
)
144 RAND_DRBG
*drbg
= OPENSSL_zalloc(sizeof(*drbg
));
147 RANDerr(RAND_F_RAND_DRBG_NEW
, ERR_R_MALLOC_FAILURE
);
150 drbg
->fork_count
= rand_fork_count
;
151 drbg
->parent
= parent
;
152 if (RAND_DRBG_set(drbg
, type
, flags
) == 0)
155 if (!RAND_DRBG_set_callbacks(drbg
, rand_drbg_get_entropy
,
156 rand_drbg_cleanup_entropy
,
168 * Uninstantiate |drbg| and free all memory.
170 void RAND_DRBG_free(RAND_DRBG
*drbg
)
175 if (drbg
->meth
!= NULL
)
176 drbg
->meth
->uninstantiate(drbg
);
177 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG
, drbg
, &drbg
->ex_data
);
178 OPENSSL_clear_free(drbg
, sizeof(*drbg
));
182 * Instantiate |drbg|, after it has been initialized. Use |pers| and
183 * |perslen| as prediction-resistance input.
185 * Requires that drbg->lock is already locked for write, if non-null.
187 * Returns 1 on success, 0 on failure.
189 int RAND_DRBG_instantiate(RAND_DRBG
*drbg
,
190 const unsigned char *pers
, size_t perslen
)
192 unsigned char *nonce
= NULL
, *entropy
= NULL
;
193 size_t noncelen
= 0, entropylen
= 0;
195 if (perslen
> drbg
->max_perslen
) {
196 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
197 RAND_R_PERSONALISATION_STRING_TOO_LONG
);
201 if (drbg
->meth
== NULL
)
203 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
204 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED
);
208 if (drbg
->state
!= DRBG_UNINITIALISED
) {
209 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
210 drbg
->state
== DRBG_ERROR
? RAND_R_IN_ERROR_STATE
211 : RAND_R_ALREADY_INSTANTIATED
);
215 drbg
->state
= DRBG_ERROR
;
216 if (drbg
->get_entropy
!= NULL
)
217 entropylen
= drbg
->get_entropy(drbg
, &entropy
, drbg
->strength
,
218 drbg
->min_entropylen
, drbg
->max_entropylen
);
219 if (entropylen
< drbg
->min_entropylen
220 || entropylen
> drbg
->max_entropylen
) {
221 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
, RAND_R_ERROR_RETRIEVING_ENTROPY
);
225 if (drbg
->max_noncelen
> 0 && drbg
->get_nonce
!= NULL
) {
226 noncelen
= drbg
->get_nonce(drbg
, &nonce
, drbg
->strength
/ 2,
227 drbg
->min_noncelen
, drbg
->max_noncelen
);
228 if (noncelen
< drbg
->min_noncelen
|| noncelen
> drbg
->max_noncelen
) {
229 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
230 RAND_R_ERROR_RETRIEVING_NONCE
);
235 if (!drbg
->meth
->instantiate(drbg
, entropy
, entropylen
,
236 nonce
, noncelen
, pers
, perslen
)) {
237 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
, RAND_R_ERROR_INSTANTIATING_DRBG
);
241 drbg
->state
= DRBG_READY
;
242 drbg
->generate_counter
= 0;
243 drbg
->reseed_time
= time(NULL
);
244 if (drbg
->reseed_counter
> 0) {
245 if (drbg
->parent
== NULL
)
246 drbg
->reseed_counter
++;
248 drbg
->reseed_counter
= drbg
->parent
->reseed_counter
;
252 if (entropy
!= NULL
&& drbg
->cleanup_entropy
!= NULL
)
253 drbg
->cleanup_entropy(drbg
, entropy
, entropylen
);
254 if (nonce
!= NULL
&& drbg
->cleanup_nonce
!= NULL
)
255 drbg
->cleanup_nonce(drbg
, nonce
, noncelen
);
256 if (drbg
->pool
!= NULL
) {
257 if (drbg
->state
== DRBG_READY
) {
258 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
259 RAND_R_ERROR_ENTROPY_POOL_WAS_IGNORED
);
260 drbg
->state
= DRBG_ERROR
;
262 RAND_POOL_free(drbg
->pool
);
265 if (drbg
->state
== DRBG_READY
)
271 * Uninstantiate |drbg|. Must be instantiated before it can be used.
273 * Requires that drbg->lock is already locked for write, if non-null.
275 * Returns 1 on success, 0 on failure.
277 int RAND_DRBG_uninstantiate(RAND_DRBG
*drbg
)
279 if (drbg
->meth
== NULL
)
281 RANDerr(RAND_F_RAND_DRBG_UNINSTANTIATE
,
282 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED
);
286 /* Clear the entire drbg->ctr struct, then reset some important
287 * members of the drbg->ctr struct (e.g. keysize, df_ks) to their
290 drbg
->meth
->uninstantiate(drbg
);
291 return RAND_DRBG_set(drbg
, drbg
->nid
, drbg
->flags
);
295 * Reseed |drbg|, mixing in the specified data
297 * Requires that drbg->lock is already locked for write, if non-null.
299 * Returns 1 on success, 0 on failure.
301 int RAND_DRBG_reseed(RAND_DRBG
*drbg
,
302 const unsigned char *adin
, size_t adinlen
)
304 unsigned char *entropy
= NULL
;
305 size_t entropylen
= 0;
307 if (drbg
->state
== DRBG_ERROR
) {
308 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_IN_ERROR_STATE
);
311 if (drbg
->state
== DRBG_UNINITIALISED
) {
312 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_NOT_INSTANTIATED
);
318 else if (adinlen
> drbg
->max_adinlen
) {
319 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
323 drbg
->state
= DRBG_ERROR
;
324 if (drbg
->get_entropy
!= NULL
)
325 entropylen
= drbg
->get_entropy(drbg
, &entropy
, drbg
->strength
,
326 drbg
->min_entropylen
, drbg
->max_entropylen
);
327 if (entropylen
< drbg
->min_entropylen
328 || entropylen
> drbg
->max_entropylen
) {
329 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_ERROR_RETRIEVING_ENTROPY
);
333 if (!drbg
->meth
->reseed(drbg
, entropy
, entropylen
, adin
, adinlen
))
336 drbg
->state
= DRBG_READY
;
337 drbg
->generate_counter
= 0;
338 drbg
->reseed_time
= time(NULL
);
339 if (drbg
->reseed_counter
> 0) {
340 if (drbg
->parent
== NULL
)
341 drbg
->reseed_counter
++;
343 drbg
->reseed_counter
= drbg
->parent
->reseed_counter
;
347 if (entropy
!= NULL
&& drbg
->cleanup_entropy
!= NULL
)
348 drbg
->cleanup_entropy(drbg
, entropy
, entropylen
);
349 if (drbg
->state
== DRBG_READY
)
355 * Restart |drbg|, using the specified entropy or additional input
357 * Tries its best to get the drbg instantiated by all means,
358 * regardless of its current state.
360 * Optionally, a |buffer| of |len| random bytes can be passed,
361 * which is assumed to contain at least |entropy| bits of entropy.
363 * If |entropy| > 0, the buffer content is used as entropy input.
365 * If |entropy| == 0, the buffer content is used as additional input
367 * Returns 1 on success, 0 on failure.
369 * This function is used internally only.
371 int rand_drbg_restart(RAND_DRBG
*drbg
,
372 const unsigned char *buffer
, size_t len
, size_t entropy
)
375 const unsigned char *adin
= NULL
;
378 if (drbg
->pool
!= NULL
) {
379 RANDerr(RAND_F_RAND_DRBG_RESTART
, ERR_R_INTERNAL_ERROR
);
380 RAND_POOL_free(drbg
->pool
);
384 if (buffer
!= NULL
) {
386 if (drbg
->max_entropylen
< len
) {
387 RANDerr(RAND_F_RAND_DRBG_RESTART
,
388 RAND_R_ENTROPY_INPUT_TOO_LONG
);
392 if (entropy
> 8 * len
) {
393 RANDerr(RAND_F_RAND_DRBG_RESTART
, RAND_R_ENTROPY_OUT_OF_RANGE
);
397 /* will be picked up by the rand_drbg_get_entropy() callback */
398 drbg
->pool
= RAND_POOL_new(entropy
, len
, len
);
399 if (drbg
->pool
== NULL
)
402 RAND_POOL_add(drbg
->pool
, buffer
, len
, entropy
);
404 if (drbg
->max_adinlen
< len
) {
405 RANDerr(RAND_F_RAND_DRBG_RESTART
,
406 RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
414 /* repair error state */
415 if (drbg
->state
== DRBG_ERROR
)
416 RAND_DRBG_uninstantiate(drbg
);
418 /* repair uninitialized state */
419 if (drbg
->state
== DRBG_UNINITIALISED
) {
420 /* reinstantiate drbg */
421 RAND_DRBG_instantiate(drbg
,
422 (const unsigned char *) ossl_pers_string
,
423 sizeof(ossl_pers_string
) - 1);
424 /* already reseeded. prevent second reseeding below */
425 reseeded
= (drbg
->state
== DRBG_READY
);
428 /* refresh current state if entropy or additional input has been provided */
429 if (drbg
->state
== DRBG_READY
) {
432 * mix in additional input without reseeding
434 * Similar to RAND_DRBG_reseed(), but the provided additional
435 * data |adin| is mixed into the current state without pulling
436 * entropy from the trusted entropy source using get_entropy().
437 * This is not a reseeding in the strict sense of NIST SP 800-90A.
439 drbg
->meth
->reseed(drbg
, adin
, adinlen
, NULL
, 0);
440 } else if (reseeded
== 0) {
441 /* do a full reseeding if it has not been done yet above */
442 RAND_DRBG_reseed(drbg
, NULL
, 0);
446 /* check whether a given entropy pool was cleared properly during reseed */
447 if (drbg
->pool
!= NULL
) {
448 drbg
->state
= DRBG_ERROR
;
449 RANDerr(RAND_F_RAND_DRBG_RESTART
, ERR_R_INTERNAL_ERROR
);
450 RAND_POOL_free(drbg
->pool
);
455 return drbg
->state
== DRBG_READY
;
459 * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
460 * to or if |prediction_resistance| is set. Additional input can be
461 * sent in |adin| and |adinlen|.
463 * Requires that drbg->lock is already locked for write, if non-null.
465 * Returns 1 on success, 0 on failure.
468 int RAND_DRBG_generate(RAND_DRBG
*drbg
, unsigned char *out
, size_t outlen
,
469 int prediction_resistance
,
470 const unsigned char *adin
, size_t adinlen
)
472 int reseed_required
= 0;
474 if (drbg
->state
!= DRBG_READY
) {
475 /* try to recover from previous errors */
476 rand_drbg_restart(drbg
, NULL
, 0, 0);
478 if (drbg
->state
== DRBG_ERROR
) {
479 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_IN_ERROR_STATE
);
482 if (drbg
->state
== DRBG_UNINITIALISED
) {
483 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_NOT_INSTANTIATED
);
488 if (outlen
> drbg
->max_request
) {
489 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG
);
492 if (adinlen
> drbg
->max_adinlen
) {
493 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
497 if (drbg
->fork_count
!= rand_fork_count
) {
498 drbg
->fork_count
= rand_fork_count
;
502 if (drbg
->reseed_interval
> 0) {
503 if (drbg
->generate_counter
>= drbg
->reseed_interval
)
506 if (drbg
->reseed_time_interval
> 0) {
507 time_t now
= time(NULL
);
508 if (now
< drbg
->reseed_time
509 || now
- drbg
->reseed_time
>= drbg
->reseed_time_interval
)
512 if (drbg
->reseed_counter
> 0 && drbg
->parent
!= NULL
) {
513 if (drbg
->reseed_counter
!= drbg
->parent
->reseed_counter
)
517 if (reseed_required
|| prediction_resistance
) {
518 if (!RAND_DRBG_reseed(drbg
, adin
, adinlen
)) {
519 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_RESEED_ERROR
);
526 if (!drbg
->meth
->generate(drbg
, out
, outlen
, adin
, adinlen
)) {
527 drbg
->state
= DRBG_ERROR
;
528 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_GENERATE_ERROR
);
532 drbg
->generate_counter
++;
538 * Generates |outlen| random bytes and stores them in |out|. It will
539 * using the given |drbg| to generate the bytes.
541 * Requires that drbg->lock is already locked for write, if non-null.
543 * Returns 1 on success 0 on failure.
545 int RAND_DRBG_bytes(RAND_DRBG
*drbg
, unsigned char *out
, size_t outlen
)
547 unsigned char *additional
= NULL
;
548 size_t additional_len
;
552 additional_len
= rand_drbg_get_additional_data(&additional
, drbg
->max_adinlen
);
554 for ( ; outlen
> 0; outlen
-= chunk
, out
+= chunk
) {
556 if (chunk
> drbg
->max_request
)
557 chunk
= drbg
->max_request
;
558 ret
= RAND_DRBG_generate(drbg
, out
, chunk
, 0, additional
, additional_len
);
565 if (additional_len
!= 0)
566 OPENSSL_secure_clear_free(additional
, additional_len
);
572 * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
574 * In the following, the signature and the semantics of the
575 * get_entropy() and cleanup_entropy() callbacks are explained.
579 * size_t get_entropy(RAND_DRBG *ctx,
580 * unsigned char **pout,
582 * size_t min_len, size_t max_len);
584 * This is a request to allocate and fill a buffer of size
585 * |min_len| <= size <= |max_len| (in bytes) which contains
586 * at least |entropy| bits of randomness. The buffer's address is
587 * to be returned in |*pout| and the number of collected
588 * randomness bytes (which may be less than the allocated size
589 * of the buffer) as return value.
591 * If the callback fails to acquire at least |entropy| bits of
592 * randomness, it shall return a buffer length of 0.
596 * void cleanup_entropy(RAND_DRBG *ctx,
597 * unsigned char *out, size_t outlen);
599 * A request to clear and free the buffer allocated by get_entropy().
600 * The values |out| and |outlen| are expected to be the random buffer's
601 * address and length, as returned by the get_entropy() callback.
603 * GET_NONCE, CLEANUP_NONCE
605 * Signature and semantics of the get_nonce() and cleanup_nonce()
606 * callbacks are analogous to get_entropy() and cleanup_entropy().
607 * Currently, the nonce is used only for the known answer tests.
609 int RAND_DRBG_set_callbacks(RAND_DRBG
*drbg
,
610 RAND_DRBG_get_entropy_fn get_entropy
,
611 RAND_DRBG_cleanup_entropy_fn cleanup_entropy
,
612 RAND_DRBG_get_nonce_fn get_nonce
,
613 RAND_DRBG_cleanup_nonce_fn cleanup_nonce
)
615 if (drbg
->state
!= DRBG_UNINITIALISED
)
617 drbg
->get_entropy
= get_entropy
;
618 drbg
->cleanup_entropy
= cleanup_entropy
;
619 drbg
->get_nonce
= get_nonce
;
620 drbg
->cleanup_nonce
= cleanup_nonce
;
625 * Set the reseed interval.
627 * The drbg will reseed automatically whenever the number of generate
628 * requests exceeds the given reseed interval. If the reseed interval
629 * is 0, then this feature is disabled.
631 * Returns 1 on success, 0 on failure.
633 int RAND_DRBG_set_reseed_interval(RAND_DRBG
*drbg
, unsigned int interval
)
635 if (interval
> MAX_RESEED_INTERVAL
)
637 drbg
->reseed_interval
= interval
;
642 * Set the reseed time interval.
644 * The drbg will reseed automatically whenever the time elapsed since
645 * the last reseeding exceeds the given reseed time interval. For safety,
646 * a reseeding will also occur if the clock has been reset to a smaller
649 * Returns 1 on success, 0 on failure.
651 int RAND_DRBG_set_reseed_time_interval(RAND_DRBG
*drbg
, time_t interval
)
653 if (interval
> MAX_RESEED_TIME_INTERVAL
)
655 drbg
->reseed_time_interval
= interval
;
660 * Get and set the EXDATA
662 int RAND_DRBG_set_ex_data(RAND_DRBG
*drbg
, int idx
, void *arg
)
664 return CRYPTO_set_ex_data(&drbg
->ex_data
, idx
, arg
);
667 void *RAND_DRBG_get_ex_data(const RAND_DRBG
*drbg
, int idx
)
669 return CRYPTO_get_ex_data(&drbg
->ex_data
, idx
);
674 * The following functions provide a RAND_METHOD that works on the
675 * global DRBG. They lock.
679 * Allocates a new global DRBG on the secure heap (if enabled) and
680 * initializes it with default settings.
682 * Returns a pointer to the new DRBG instance on success, NULL on failure.
684 static RAND_DRBG
*drbg_setup(RAND_DRBG
*parent
)
688 drbg
= OPENSSL_secure_zalloc(sizeof(RAND_DRBG
));
692 drbg
->lock
= CRYPTO_THREAD_lock_new();
693 if (drbg
->lock
== NULL
) {
694 RANDerr(RAND_F_DRBG_SETUP
, RAND_R_FAILED_TO_CREATE_LOCK
);
698 if (RAND_DRBG_set(drbg
,
699 RAND_DRBG_NID
, RAND_DRBG_FLAG_CTR_USE_DF
) != 1)
701 if (RAND_DRBG_set_callbacks(drbg
, rand_drbg_get_entropy
,
702 rand_drbg_cleanup_entropy
, NULL
, NULL
) != 1)
705 if (parent
== NULL
) {
706 drbg
->reseed_interval
= MASTER_RESEED_INTERVAL
;
707 drbg
->reseed_time_interval
= MASTER_RESEED_TIME_INTERVAL
;
709 drbg
->parent
= parent
;
710 drbg
->reseed_interval
= SLAVE_RESEED_INTERVAL
;
711 drbg
->reseed_time_interval
= SLAVE_RESEED_TIME_INTERVAL
;
714 /* enable seed propagation */
715 drbg
->reseed_counter
= 1;
718 * Ignore instantiation error so support just-in-time instantiation.
720 * The state of the drbg will be checked in RAND_DRBG_generate() and
721 * an automatic recovery is attempted.
723 RAND_DRBG_instantiate(drbg
,
724 (const unsigned char *) ossl_pers_string
,
725 sizeof(ossl_pers_string
) - 1);
734 * Initialize the global DRBGs on first use.
735 * Returns 1 on success, 0 on failure.
737 DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init
)
740 * ensure that libcrypto is initialized, otherwise the
741 * DRBG locks are not cleaned up properly
743 if (!OPENSSL_init_crypto(0, NULL
))
746 drbg_master
= drbg_setup(NULL
);
747 drbg_public
= drbg_setup(drbg_master
);
748 drbg_private
= drbg_setup(drbg_master
);
750 if (drbg_master
== NULL
|| drbg_public
== NULL
|| drbg_private
== NULL
)
756 /* Cleans up the given global DRBG */
757 static void drbg_cleanup(RAND_DRBG
*drbg
)
760 RAND_DRBG_uninstantiate(drbg
);
761 CRYPTO_THREAD_lock_free(drbg
->lock
);
762 OPENSSL_secure_clear_free(drbg
, sizeof(RAND_DRBG
));
766 /* Clean up the global DRBGs before exit */
767 void rand_drbg_cleanup_int(void)
769 drbg_cleanup(drbg_private
);
770 drbg_cleanup(drbg_public
);
771 drbg_cleanup(drbg_master
);
773 drbg_private
= drbg_public
= drbg_master
= NULL
;
776 /* Implements the default OpenSSL RAND_bytes() method */
777 static int drbg_bytes(unsigned char *out
, int count
)
781 RAND_DRBG
*drbg
= RAND_DRBG_get0_public();
786 CRYPTO_THREAD_write_lock(drbg
->lock
);
787 for ( ; count
> 0; count
-= chunk
, out
+= chunk
) {
789 if (chunk
> drbg
->max_request
)
790 chunk
= drbg
->max_request
;
791 ret
= RAND_DRBG_generate(drbg
, out
, chunk
, 0, NULL
, 0);
798 CRYPTO_THREAD_unlock(drbg
->lock
);
802 /* Implements the default OpenSSL RAND_add() method */
803 static int drbg_add(const void *buf
, int num
, double randomness
)
806 RAND_DRBG
*drbg
= RAND_DRBG_get0_master();
811 if (num
< 0 || randomness
< 0.0)
814 if (randomness
> (double)drbg
->max_entropylen
) {
816 * The purpose of this check is to bound |randomness| by a
817 * relatively small value in order to prevent an integer
818 * overflow when multiplying by 8 in the rand_drbg_restart()
824 CRYPTO_THREAD_write_lock(drbg
->lock
);
825 ret
= rand_drbg_restart(drbg
, buf
,
826 (size_t)(unsigned int)num
,
827 (size_t)(8*randomness
));
828 CRYPTO_THREAD_unlock(drbg
->lock
);
833 /* Implements the default OpenSSL RAND_seed() method */
834 static int drbg_seed(const void *buf
, int num
)
836 return drbg_add(buf
, num
, num
);
839 /* Implements the default OpenSSL RAND_status() method */
840 static int drbg_status(void)
843 RAND_DRBG
*drbg
= RAND_DRBG_get0_master();
848 CRYPTO_THREAD_write_lock(drbg
->lock
);
849 ret
= drbg
->state
== DRBG_READY
? 1 : 0;
850 CRYPTO_THREAD_unlock(drbg
->lock
);
855 * Get the master DRBG.
856 * Returns pointer to the DRBG on success, NULL on failure.
859 RAND_DRBG
*RAND_DRBG_get0_master(void)
861 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
868 * Get the public DRBG.
869 * Returns pointer to the DRBG on success, NULL on failure.
871 RAND_DRBG
*RAND_DRBG_get0_public(void)
873 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
880 * Get the private DRBG.
881 * Returns pointer to the DRBG on success, NULL on failure.
883 RAND_DRBG
*RAND_DRBG_get0_private(void)
885 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
891 RAND_METHOD rand_meth
= {
900 RAND_METHOD
*RAND_OpenSSL(void)