2 * Copyright 2011-2017 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(const char *name
, 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 * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
540 * In the following, the signature and the semantics of the
541 * get_entropy() and cleanup_entropy() callbacks are explained.
545 * size_t get_entropy(RAND_DRBG *ctx,
546 * unsigned char **pout,
548 * size_t min_len, size_t max_len);
550 * This is a request to allocate and fill a buffer of size
551 * |min_len| <= size <= |max_len| (in bytes) which contains
552 * at least |entropy| bits of randomness. The buffer's address is
553 * to be returned in |*pout| and the number of collected
554 * randomness bytes (which may be less than the allocated size
555 * of the buffer) as return value.
557 * If the callback fails to acquire at least |entropy| bits of
558 * randomness, it shall return a buffer length of 0.
562 * void cleanup_entropy(RAND_DRBG *ctx,
563 * unsigned char *out, size_t outlen);
565 * A request to clear and free the buffer allocated by get_entropy().
566 * The values |out| and |outlen| are expected to be the random buffer's
567 * address and length, as returned by the get_entropy() callback.
569 * GET_NONCE, CLEANUP_NONCE
571 * Signature and semantics of the get_nonce() and cleanup_nonce()
572 * callbacks are analogous to get_entropy() and cleanup_entropy().
573 * Currently, the nonce is used only for the known answer tests.
575 int RAND_DRBG_set_callbacks(RAND_DRBG
*drbg
,
576 RAND_DRBG_get_entropy_fn get_entropy
,
577 RAND_DRBG_cleanup_entropy_fn cleanup_entropy
,
578 RAND_DRBG_get_nonce_fn get_nonce
,
579 RAND_DRBG_cleanup_nonce_fn cleanup_nonce
)
581 if (drbg
->state
!= DRBG_UNINITIALISED
)
583 drbg
->get_entropy
= get_entropy
;
584 drbg
->cleanup_entropy
= cleanup_entropy
;
585 drbg
->get_nonce
= get_nonce
;
586 drbg
->cleanup_nonce
= cleanup_nonce
;
591 * Set the reseed interval.
593 * The drbg will reseed automatically whenever the number of generate
594 * requests exceeds the given reseed interval. If the reseed interval
595 * is 0, then this feature is disabled.
597 * Returns 1 on success, 0 on failure.
599 int RAND_DRBG_set_reseed_interval(RAND_DRBG
*drbg
, unsigned int interval
)
601 if (interval
> MAX_RESEED_INTERVAL
)
603 drbg
->reseed_interval
= interval
;
608 * Set the reseed time interval.
610 * The drbg will reseed automatically whenever the time elapsed since
611 * the last reseeding exceeds the given reseed time interval. For safety,
612 * a reseeding will also occur if the clock has been reset to a smaller
615 * Returns 1 on success, 0 on failure.
617 int RAND_DRBG_set_reseed_time_interval(RAND_DRBG
*drbg
, time_t interval
)
619 if (interval
> MAX_RESEED_TIME_INTERVAL
)
621 drbg
->reseed_time_interval
= interval
;
626 * Get and set the EXDATA
628 int RAND_DRBG_set_ex_data(RAND_DRBG
*drbg
, int idx
, void *arg
)
630 return CRYPTO_set_ex_data(&drbg
->ex_data
, idx
, arg
);
633 void *RAND_DRBG_get_ex_data(const RAND_DRBG
*drbg
, int idx
)
635 return CRYPTO_get_ex_data(&drbg
->ex_data
, idx
);
640 * The following functions provide a RAND_METHOD that works on the
641 * global DRBG. They lock.
645 * Allocates a new global DRBG on the secure heap (if enabled) and
646 * initializes it with default settings.
647 * A global lock for the DRBG is created with the given name.
649 * Returns a pointer to the new DRBG instance on success, NULL on failure.
651 static RAND_DRBG
*drbg_setup(const char *name
, RAND_DRBG
*parent
)
656 RANDerr(RAND_F_DRBG_SETUP
, ERR_R_INTERNAL_ERROR
);
660 drbg
= OPENSSL_secure_zalloc(sizeof(RAND_DRBG
));
664 drbg
->lock
= CRYPTO_THREAD_glock_new(name
);
665 if (drbg
->lock
== NULL
) {
666 RANDerr(RAND_F_DRBG_SETUP
, RAND_R_FAILED_TO_CREATE_LOCK
);
670 if (RAND_DRBG_set(drbg
,
671 RAND_DRBG_NID
, RAND_DRBG_FLAG_CTR_USE_DF
) != 1)
673 if (RAND_DRBG_set_callbacks(drbg
, rand_drbg_get_entropy
,
674 rand_drbg_cleanup_entropy
, NULL
, NULL
) != 1)
677 if (parent
== NULL
) {
678 drbg
->reseed_interval
= MASTER_RESEED_INTERVAL
;
679 drbg
->reseed_time_interval
= MASTER_RESEED_TIME_INTERVAL
;
681 drbg
->parent
= parent
;
682 drbg
->reseed_interval
= SLAVE_RESEED_INTERVAL
;
683 drbg
->reseed_time_interval
= SLAVE_RESEED_TIME_INTERVAL
;
686 /* enable seed propagation */
687 drbg
->reseed_counter
= 1;
690 * Ignore instantiation error so support just-in-time instantiation.
692 * The state of the drbg will be checked in RAND_DRBG_generate() and
693 * an automatic recovery is attempted.
695 RAND_DRBG_instantiate(drbg
,
696 (const unsigned char *) ossl_pers_string
,
697 sizeof(ossl_pers_string
) - 1);
706 * Initialize the global DRBGs on first use.
707 * Returns 1 on success, 0 on failure.
709 DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init
)
711 drbg_master
= drbg_setup("drbg_master", NULL
);
712 drbg_public
= drbg_setup("drbg_public", drbg_master
);
713 drbg_private
= drbg_setup("drbg_private", drbg_master
);
715 if (drbg_master
== NULL
|| drbg_public
== NULL
|| drbg_private
== NULL
)
721 /* Cleans up the given global DRBG */
722 static void drbg_cleanup(RAND_DRBG
*drbg
)
725 RAND_DRBG_uninstantiate(drbg
);
726 CRYPTO_THREAD_lock_free(drbg
->lock
);
727 OPENSSL_secure_clear_free(drbg
, sizeof(RAND_DRBG
));
731 /* Clean up the global DRBGs before exit */
732 void rand_drbg_cleanup_int(void)
734 drbg_cleanup(drbg_private
);
735 drbg_cleanup(drbg_public
);
736 drbg_cleanup(drbg_master
);
738 drbg_private
= drbg_public
= drbg_master
= NULL
;
741 /* Implements the default OpenSSL RAND_bytes() method */
742 static int drbg_bytes(unsigned char *out
, int count
)
746 RAND_DRBG
*drbg
= RAND_DRBG_get0_public();
751 CRYPTO_THREAD_write_lock(drbg
->lock
);
752 if (drbg
->state
== DRBG_UNINITIALISED
)
755 for ( ; count
> 0; count
-= chunk
, out
+= chunk
) {
757 if (chunk
> drbg
->max_request
)
758 chunk
= drbg
->max_request
;
759 ret
= RAND_DRBG_generate(drbg
, out
, chunk
, 0, NULL
, 0);
766 CRYPTO_THREAD_unlock(drbg
->lock
);
770 /* Implements the default OpenSSL RAND_add() method */
771 static int drbg_add(const void *buf
, int num
, double randomness
)
774 RAND_DRBG
*drbg
= RAND_DRBG_get0_master();
779 if (num
< 0 || randomness
< 0.0)
782 if (randomness
> (double)drbg
->max_entropylen
) {
784 * The purpose of this check is to bound |randomness| by a
785 * relatively small value in order to prevent an integer
786 * overflow when multiplying by 8 in the rand_drbg_restart()
792 CRYPTO_THREAD_write_lock(drbg
->lock
);
793 ret
= rand_drbg_restart(drbg
, buf
,
794 (size_t)(unsigned int)num
,
795 (size_t)(8*randomness
));
796 CRYPTO_THREAD_unlock(drbg
->lock
);
801 /* Implements the default OpenSSL RAND_seed() method */
802 static int drbg_seed(const void *buf
, int num
)
804 return drbg_add(buf
, num
, num
);
807 /* Implements the default OpenSSL RAND_status() method */
808 static int drbg_status(void)
811 RAND_DRBG
*drbg
= RAND_DRBG_get0_master();
816 CRYPTO_THREAD_write_lock(drbg
->lock
);
817 ret
= drbg
->state
== DRBG_READY
? 1 : 0;
818 CRYPTO_THREAD_unlock(drbg
->lock
);
823 * Get the master DRBG.
824 * Returns pointer to the DRBG on success, NULL on failure.
827 RAND_DRBG
*RAND_DRBG_get0_master(void)
829 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
836 * Get the public DRBG.
837 * Returns pointer to the DRBG on success, NULL on failure.
839 RAND_DRBG
*RAND_DRBG_get0_public(void)
841 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
848 * Get the private DRBG.
849 * Returns pointer to the DRBG on success, NULL on failure.
851 RAND_DRBG
*RAND_DRBG_get0_private(void)
853 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
859 RAND_METHOD rand_meth
= {
868 RAND_METHOD
*RAND_OpenSSL(void)