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
= 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 ctr_uninstantiate(drbg
);
176 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG
, drbg
, &drbg
->ex_data
);
177 OPENSSL_clear_free(drbg
, sizeof(*drbg
));
181 * Instantiate |drbg|, after it has been initialized. Use |pers| and
182 * |perslen| as prediction-resistance input.
184 * Requires that drbg->lock is already locked for write, if non-null.
186 * Returns 1 on success, 0 on failure.
188 int RAND_DRBG_instantiate(RAND_DRBG
*drbg
,
189 const unsigned char *pers
, size_t perslen
)
191 unsigned char *nonce
= NULL
, *entropy
= NULL
;
192 size_t noncelen
= 0, entropylen
= 0;
194 if (perslen
> drbg
->max_perslen
) {
195 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
196 RAND_R_PERSONALISATION_STRING_TOO_LONG
);
199 if (drbg
->state
!= DRBG_UNINITIALISED
) {
200 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
201 drbg
->state
== DRBG_ERROR
? RAND_R_IN_ERROR_STATE
202 : RAND_R_ALREADY_INSTANTIATED
);
206 drbg
->state
= DRBG_ERROR
;
207 if (drbg
->get_entropy
!= NULL
)
208 entropylen
= drbg
->get_entropy(drbg
, &entropy
, drbg
->strength
,
209 drbg
->min_entropylen
, drbg
->max_entropylen
);
210 if (entropylen
< drbg
->min_entropylen
211 || entropylen
> drbg
->max_entropylen
) {
212 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
, RAND_R_ERROR_RETRIEVING_ENTROPY
);
216 if (drbg
->max_noncelen
> 0 && drbg
->get_nonce
!= NULL
) {
217 noncelen
= drbg
->get_nonce(drbg
, &nonce
, drbg
->strength
/ 2,
218 drbg
->min_noncelen
, drbg
->max_noncelen
);
219 if (noncelen
< drbg
->min_noncelen
|| noncelen
> drbg
->max_noncelen
) {
220 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
221 RAND_R_ERROR_RETRIEVING_NONCE
);
226 if (!ctr_instantiate(drbg
, entropy
, entropylen
,
227 nonce
, noncelen
, pers
, perslen
)) {
228 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
, RAND_R_ERROR_INSTANTIATING_DRBG
);
232 drbg
->state
= DRBG_READY
;
233 drbg
->generate_counter
= 0;
234 drbg
->reseed_time
= time(NULL
);
235 if (drbg
->reseed_counter
> 0) {
236 if (drbg
->parent
== NULL
)
237 drbg
->reseed_counter
++;
239 drbg
->reseed_counter
= drbg
->parent
->reseed_counter
;
243 if (entropy
!= NULL
&& drbg
->cleanup_entropy
!= NULL
)
244 drbg
->cleanup_entropy(drbg
, entropy
, entropylen
);
245 if (nonce
!= NULL
&& drbg
->cleanup_nonce
!= NULL
)
246 drbg
->cleanup_nonce(drbg
, nonce
, noncelen
);
247 if (drbg
->pool
!= NULL
) {
248 if (drbg
->state
== DRBG_READY
) {
249 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
250 RAND_R_ERROR_ENTROPY_POOL_WAS_IGNORED
);
251 drbg
->state
= DRBG_ERROR
;
253 RAND_POOL_free(drbg
->pool
);
256 if (drbg
->state
== DRBG_READY
)
262 * Uninstantiate |drbg|. Must be instantiated before it can be used.
264 * Requires that drbg->lock is already locked for write, if non-null.
266 * Returns 1 on success, 0 on failure.
268 int RAND_DRBG_uninstantiate(RAND_DRBG
*drbg
)
270 /* Clear the entire drbg->ctr struct, then reset some important
271 * members of the drbg->ctr struct (e.g. keysize, df_ks) to their
274 ctr_uninstantiate(drbg
);
275 return RAND_DRBG_set(drbg
, drbg
->nid
, drbg
->flags
);
279 * Reseed |drbg|, mixing in the specified data
281 * Requires that drbg->lock is already locked for write, if non-null.
283 * Returns 1 on success, 0 on failure.
285 int RAND_DRBG_reseed(RAND_DRBG
*drbg
,
286 const unsigned char *adin
, size_t adinlen
)
288 unsigned char *entropy
= NULL
;
289 size_t entropylen
= 0;
291 if (drbg
->state
== DRBG_ERROR
) {
292 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_IN_ERROR_STATE
);
295 if (drbg
->state
== DRBG_UNINITIALISED
) {
296 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_NOT_INSTANTIATED
);
302 else if (adinlen
> drbg
->max_adinlen
) {
303 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
307 drbg
->state
= DRBG_ERROR
;
308 if (drbg
->get_entropy
!= NULL
)
309 entropylen
= drbg
->get_entropy(drbg
, &entropy
, drbg
->strength
,
310 drbg
->min_entropylen
, drbg
->max_entropylen
);
311 if (entropylen
< drbg
->min_entropylen
312 || entropylen
> drbg
->max_entropylen
) {
313 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_ERROR_RETRIEVING_ENTROPY
);
317 if (!ctr_reseed(drbg
, entropy
, entropylen
, adin
, adinlen
))
320 drbg
->state
= DRBG_READY
;
321 drbg
->generate_counter
= 0;
322 drbg
->reseed_time
= time(NULL
);
323 if (drbg
->reseed_counter
> 0) {
324 if (drbg
->parent
== NULL
)
325 drbg
->reseed_counter
++;
327 drbg
->reseed_counter
= drbg
->parent
->reseed_counter
;
331 if (entropy
!= NULL
&& drbg
->cleanup_entropy
!= NULL
)
332 drbg
->cleanup_entropy(drbg
, entropy
, entropylen
);
333 if (drbg
->state
== DRBG_READY
)
339 * Restart |drbg|, using the specified entropy or additional input
341 * Tries its best to get the drbg instantiated by all means,
342 * regardless of its current state.
344 * Optionally, a |buffer| of |len| random bytes can be passed,
345 * which is assumed to contain at least |entropy| bits of entropy.
347 * If |entropy| > 0, the buffer content is used as entropy input.
349 * If |entropy| == 0, the buffer content is used as additional input
351 * Returns 1 on success, 0 on failure.
353 * This function is used internally only.
355 int rand_drbg_restart(RAND_DRBG
*drbg
,
356 const unsigned char *buffer
, size_t len
, size_t entropy
)
359 const unsigned char *adin
= NULL
;
362 if (drbg
->pool
!= NULL
) {
363 RANDerr(RAND_F_RAND_DRBG_RESTART
, ERR_R_INTERNAL_ERROR
);
364 RAND_POOL_free(drbg
->pool
);
368 if (buffer
!= NULL
) {
370 if (drbg
->max_entropylen
< len
) {
371 RANDerr(RAND_F_RAND_DRBG_RESTART
,
372 RAND_R_ENTROPY_INPUT_TOO_LONG
);
376 if (entropy
> 8 * len
) {
377 RANDerr(RAND_F_RAND_DRBG_RESTART
, RAND_R_ENTROPY_OUT_OF_RANGE
);
381 /* will be picked up by the rand_drbg_get_entropy() callback */
382 drbg
->pool
= RAND_POOL_new(entropy
, len
, len
);
383 if (drbg
->pool
== NULL
)
386 RAND_POOL_add(drbg
->pool
, buffer
, len
, entropy
);
388 if (drbg
->max_adinlen
< len
) {
389 RANDerr(RAND_F_RAND_DRBG_RESTART
,
390 RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
398 /* repair error state */
399 if (drbg
->state
== DRBG_ERROR
)
400 RAND_DRBG_uninstantiate(drbg
);
402 /* repair uninitialized state */
403 if (drbg
->state
== DRBG_UNINITIALISED
) {
404 /* reinstantiate drbg */
405 RAND_DRBG_instantiate(drbg
,
406 (const unsigned char *) ossl_pers_string
,
407 sizeof(ossl_pers_string
) - 1);
408 /* already reseeded. prevent second reseeding below */
409 reseeded
= (drbg
->state
== DRBG_READY
);
412 /* refresh current state if entropy or additional input has been provided */
413 if (drbg
->state
== DRBG_READY
) {
416 * mix in additional input without reseeding
418 * Similar to RAND_DRBG_reseed(), but the provided additional
419 * data |adin| is mixed into the current state without pulling
420 * entropy from the trusted entropy source using get_entropy().
421 * This is not a reseeding in the strict sense of NIST SP 800-90A.
423 ctr_reseed(drbg
, adin
, adinlen
, NULL
, 0);
424 } else if (reseeded
== 0) {
425 /* do a full reseeding if it has not been done yet above */
426 RAND_DRBG_reseed(drbg
, NULL
, 0);
430 /* check whether a given entropy pool was cleared properly during reseed */
431 if (drbg
->pool
!= NULL
) {
432 drbg
->state
= DRBG_ERROR
;
433 RANDerr(RAND_F_RAND_DRBG_RESTART
, ERR_R_INTERNAL_ERROR
);
434 RAND_POOL_free(drbg
->pool
);
439 return drbg
->state
== DRBG_READY
;
443 * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
444 * to or if |prediction_resistance| is set. Additional input can be
445 * sent in |adin| and |adinlen|.
447 * Requires that drbg->lock is already locked for write, if non-null.
449 * Returns 1 on success, 0 on failure.
452 int RAND_DRBG_generate(RAND_DRBG
*drbg
, unsigned char *out
, size_t outlen
,
453 int prediction_resistance
,
454 const unsigned char *adin
, size_t adinlen
)
456 int reseed_required
= 0;
458 if (drbg
->state
!= DRBG_READY
) {
459 /* try to recover from previous errors */
460 rand_drbg_restart(drbg
, NULL
, 0, 0);
462 if (drbg
->state
== DRBG_ERROR
) {
463 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_IN_ERROR_STATE
);
466 if (drbg
->state
== DRBG_UNINITIALISED
) {
467 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_NOT_INSTANTIATED
);
472 if (outlen
> drbg
->max_request
) {
473 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG
);
476 if (adinlen
> drbg
->max_adinlen
) {
477 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
481 if (drbg
->fork_count
!= rand_fork_count
) {
482 drbg
->fork_count
= rand_fork_count
;
486 if (drbg
->reseed_interval
> 0) {
487 if (drbg
->generate_counter
>= drbg
->reseed_interval
)
490 if (drbg
->reseed_time_interval
> 0) {
491 time_t now
= time(NULL
);
492 if (now
< drbg
->reseed_time
493 || now
- drbg
->reseed_time
>= drbg
->reseed_time_interval
)
496 if (drbg
->reseed_counter
> 0 && drbg
->parent
!= NULL
) {
497 if (drbg
->reseed_counter
!= drbg
->parent
->reseed_counter
)
501 if (reseed_required
|| prediction_resistance
) {
502 if (!RAND_DRBG_reseed(drbg
, adin
, adinlen
)) {
503 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_RESEED_ERROR
);
510 if (!ctr_generate(drbg
, out
, outlen
, adin
, adinlen
)) {
511 drbg
->state
= DRBG_ERROR
;
512 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_GENERATE_ERROR
);
516 drbg
->generate_counter
++;
522 * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
524 * In the following, the signature and the semantics of the
525 * get_entropy() and cleanup_entropy() callbacks are explained.
529 * size_t get_entropy(RAND_DRBG *ctx,
530 * unsigned char **pout,
532 * size_t min_len, size_t max_len);
534 * This is a request to allocate and fill a buffer of size
535 * |min_len| <= size <= |max_len| (in bytes) which contains
536 * at least |entropy| bits of randomness. The buffer's address is
537 * to be returned in |*pout| and the number of collected
538 * randomness bytes (which may be less than the allocated size
539 * of the buffer) as return value.
541 * If the callback fails to acquire at least |entropy| bits of
542 * randomness, it shall return a buffer length of 0.
546 * void cleanup_entropy(RAND_DRBG *ctx,
547 * unsigned char *out, size_t outlen);
549 * A request to clear and free the buffer allocated by get_entropy().
550 * The values |out| and |outlen| are expected to be the random buffer's
551 * address and length, as returned by the get_entropy() callback.
553 * GET_NONCE, CLEANUP_NONCE
555 * Signature and semantics of the get_nonce() and cleanup_nonce()
556 * callbacks are analogous to get_entropy() and cleanup_entropy().
557 * Currently, the nonce is used only for the known answer tests.
559 int RAND_DRBG_set_callbacks(RAND_DRBG
*drbg
,
560 RAND_DRBG_get_entropy_fn get_entropy
,
561 RAND_DRBG_cleanup_entropy_fn cleanup_entropy
,
562 RAND_DRBG_get_nonce_fn get_nonce
,
563 RAND_DRBG_cleanup_nonce_fn cleanup_nonce
)
565 if (drbg
->state
!= DRBG_UNINITIALISED
)
567 drbg
->get_entropy
= get_entropy
;
568 drbg
->cleanup_entropy
= cleanup_entropy
;
569 drbg
->get_nonce
= get_nonce
;
570 drbg
->cleanup_nonce
= cleanup_nonce
;
575 * Set the reseed interval.
577 * The drbg will reseed automatically whenever the number of generate
578 * requests exceeds the given reseed interval. If the reseed interval
579 * is 0, then this feature is disabled.
581 * Returns 1 on success, 0 on failure.
583 int RAND_DRBG_set_reseed_interval(RAND_DRBG
*drbg
, unsigned int interval
)
585 if (interval
> MAX_RESEED_INTERVAL
)
587 drbg
->reseed_interval
= interval
;
592 * Set the reseed time interval.
594 * The drbg will reseed automatically whenever the time elapsed since
595 * the last reseeding exceeds the given reseed time interval. For safety,
596 * a reseeding will also occur if the clock has been reset to a smaller
599 * Returns 1 on success, 0 on failure.
601 int RAND_DRBG_set_reseed_time_interval(RAND_DRBG
*drbg
, time_t interval
)
603 if (interval
> MAX_RESEED_TIME_INTERVAL
)
605 drbg
->reseed_time_interval
= interval
;
610 * Get and set the EXDATA
612 int RAND_DRBG_set_ex_data(RAND_DRBG
*drbg
, int idx
, void *arg
)
614 return CRYPTO_set_ex_data(&drbg
->ex_data
, idx
, arg
);
617 void *RAND_DRBG_get_ex_data(const RAND_DRBG
*drbg
, int idx
)
619 return CRYPTO_get_ex_data(&drbg
->ex_data
, idx
);
624 * The following functions provide a RAND_METHOD that works on the
625 * global DRBG. They lock.
629 * Allocates a new global DRBG on the secure heap (if enabled) and
630 * initializes it with default settings.
631 * A global lock for the DRBG is created with the given name.
633 * Returns a pointer to the new DRBG instance on success, NULL on failure.
635 static RAND_DRBG
*drbg_setup(const char *name
, RAND_DRBG
*parent
)
640 RANDerr(RAND_F_DRBG_SETUP
, ERR_R_INTERNAL_ERROR
);
644 drbg
= OPENSSL_secure_zalloc(sizeof(RAND_DRBG
));
648 drbg
->lock
= CRYPTO_THREAD_glock_new(name
);
649 if (drbg
->lock
== NULL
) {
650 RANDerr(RAND_F_DRBG_SETUP
, RAND_R_FAILED_TO_CREATE_LOCK
);
654 if (RAND_DRBG_set(drbg
,
655 RAND_DRBG_NID
, RAND_DRBG_FLAG_CTR_USE_DF
) != 1)
657 if (RAND_DRBG_set_callbacks(drbg
, rand_drbg_get_entropy
,
658 rand_drbg_cleanup_entropy
, NULL
, NULL
) != 1)
661 if (parent
== NULL
) {
662 drbg
->reseed_interval
= MASTER_RESEED_INTERVAL
;
663 drbg
->reseed_time_interval
= MASTER_RESEED_TIME_INTERVAL
;
665 drbg
->parent
= parent
;
666 drbg
->reseed_interval
= SLAVE_RESEED_INTERVAL
;
667 drbg
->reseed_time_interval
= SLAVE_RESEED_TIME_INTERVAL
;
670 /* enable seed propagation */
671 drbg
->reseed_counter
= 1;
674 * Ignore instantiation error so support just-in-time instantiation.
676 * The state of the drbg will be checked in RAND_DRBG_generate() and
677 * an automatic recovery is attempted.
679 RAND_DRBG_instantiate(drbg
,
680 (const unsigned char *) ossl_pers_string
,
681 sizeof(ossl_pers_string
) - 1);
690 * Initialize the global DRBGs on first use.
691 * Returns 1 on success, 0 on failure.
693 DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init
)
695 drbg_master
= drbg_setup("drbg_master", NULL
);
696 drbg_public
= drbg_setup("drbg_public", drbg_master
);
697 drbg_private
= drbg_setup("drbg_private", drbg_master
);
699 if (drbg_master
== NULL
|| drbg_public
== NULL
|| drbg_private
== NULL
)
705 /* Cleans up the given global DRBG */
706 static void drbg_cleanup(RAND_DRBG
*drbg
)
709 RAND_DRBG_uninstantiate(drbg
);
710 CRYPTO_THREAD_lock_free(drbg
->lock
);
711 OPENSSL_secure_clear_free(drbg
, sizeof(RAND_DRBG
));
715 /* Clean up the global DRBGs before exit */
716 void rand_drbg_cleanup_int(void)
718 drbg_cleanup(drbg_private
);
719 drbg_cleanup(drbg_public
);
720 drbg_cleanup(drbg_master
);
722 drbg_private
= drbg_public
= drbg_master
= NULL
;
725 /* Implements the default OpenSSL RAND_bytes() method */
726 static int drbg_bytes(unsigned char *out
, int count
)
730 RAND_DRBG
*drbg
= RAND_DRBG_get0_public();
735 CRYPTO_THREAD_write_lock(drbg
->lock
);
736 if (drbg
->state
== DRBG_UNINITIALISED
)
739 for ( ; count
> 0; count
-= chunk
, out
+= chunk
) {
741 if (chunk
> drbg
->max_request
)
742 chunk
= drbg
->max_request
;
743 ret
= RAND_DRBG_generate(drbg
, out
, chunk
, 0, NULL
, 0);
750 CRYPTO_THREAD_unlock(drbg
->lock
);
754 /* Implements the default OpenSSL RAND_add() method */
755 static int drbg_add(const void *buf
, int num
, double randomness
)
758 RAND_DRBG
*drbg
= RAND_DRBG_get0_master();
763 if (num
< 0 || randomness
< 0.0)
766 if (randomness
> (double)drbg
->max_entropylen
) {
768 * The purpose of this check is to bound |randomness| by a
769 * relatively small value in order to prevent an integer
770 * overflow when multiplying by 8 in the rand_drbg_restart()
776 CRYPTO_THREAD_write_lock(drbg
->lock
);
777 ret
= rand_drbg_restart(drbg
, buf
,
778 (size_t)(unsigned int)num
,
779 (size_t)(8*randomness
));
780 CRYPTO_THREAD_unlock(drbg
->lock
);
785 /* Implements the default OpenSSL RAND_seed() method */
786 static int drbg_seed(const void *buf
, int num
)
788 return drbg_add(buf
, num
, num
);
791 /* Implements the default OpenSSL RAND_status() method */
792 static int drbg_status(void)
795 RAND_DRBG
*drbg
= RAND_DRBG_get0_master();
800 CRYPTO_THREAD_write_lock(drbg
->lock
);
801 ret
= drbg
->state
== DRBG_READY
? 1 : 0;
802 CRYPTO_THREAD_unlock(drbg
->lock
);
807 * Get the master DRBG.
808 * Returns pointer to the DRBG on success, NULL on failure.
811 RAND_DRBG
*RAND_DRBG_get0_master(void)
813 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
820 * Get the public DRBG.
821 * Returns pointer to the DRBG on success, NULL on failure.
823 RAND_DRBG
*RAND_DRBG_get0_public(void)
825 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
832 * Get the private DRBG.
833 * Returns pointer to the DRBG on success, NULL on failure.
835 RAND_DRBG
*RAND_DRBG_get0_private(void)
837 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
843 RAND_METHOD rand_meth
= {
852 RAND_METHOD
*RAND_OpenSSL(void)