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"
18 static RAND_DRBG rand_drbg
; /* The default global DRBG. */
19 static RAND_DRBG priv_drbg
; /* The global private-key DRBG. */
21 /* NIST SP 800-90A DRBG recommends the use of a personalization string. */
22 static const char ossl_pers_string
[] = "OpenSSL NIST SP 800-90A DRBG";
25 * Support framework for NIST SP 800-90A DRBG, AES-CTR mode.
26 * The RAND_DRBG is OpenSSL's pointer to an instance of the DRBG.
28 * The OpenSSL model is to have new and free functions, and that new
29 * does all initialization. That is not the NIST model, which has
30 * instantiation and un-instantiate, and re-use within a new/free
31 * lifecycle. (No doubt this comes from the desire to support hardware
32 * DRBG, where allocation of resources on something like an HSM is
33 * a much bigger deal than just re-setting an allocated resource.)
36 static CRYPTO_ONCE rand_drbg_init
= CRYPTO_ONCE_STATIC_INIT
;
38 static int drbg_setup(RAND_DRBG
*drbg
, const char *name
);
41 * Set/initialize |drbg| to be of type |nid|, with optional |flags|.
42 * Return -2 if the type is not supported, 1 on success and -1 on
45 int RAND_DRBG_set(RAND_DRBG
*drbg
, int nid
, unsigned int flags
)
49 drbg
->state
= DRBG_UNINITIALISED
;
55 RANDerr(RAND_F_RAND_DRBG_SET
, RAND_R_UNSUPPORTED_DRBG_TYPE
);
58 /* Uninitialized; that's okay. */
68 RANDerr(RAND_F_RAND_DRBG_SET
, RAND_R_ERROR_INITIALISING_DRBG
);
73 * Allocate memory and initialize a new DRBG. The |parent|, if not
74 * NULL, will be used to auto-seed this RAND_DRBG as needed.
76 RAND_DRBG
*RAND_DRBG_new(int type
, unsigned int flags
, RAND_DRBG
*parent
)
78 RAND_DRBG
*drbg
= OPENSSL_zalloc(sizeof(*drbg
));
81 RANDerr(RAND_F_RAND_DRBG_NEW
, ERR_R_MALLOC_FAILURE
);
84 drbg
->fork_count
= rand_fork_count
;
85 drbg
->parent
= parent
;
86 if (RAND_DRBG_set(drbg
, type
, flags
) < 0)
90 if (!RAND_DRBG_set_callbacks(drbg
, rand_drbg_get_entropy
,
91 rand_drbg_cleanup_entropy
,
104 * Uninstantiate |drbg| and free all memory.
106 void RAND_DRBG_free(RAND_DRBG
*drbg
)
111 ctr_uninstantiate(drbg
);
112 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG
, drbg
, &drbg
->ex_data
);
113 OPENSSL_clear_free(drbg
, sizeof(*drbg
));
117 * Instantiate |drbg|, after it has been initialized. Use |pers| and
118 * |perslen| as prediction-resistance input.
120 int RAND_DRBG_instantiate(RAND_DRBG
*drbg
,
121 const unsigned char *pers
, size_t perslen
)
123 unsigned char *nonce
= NULL
, *entropy
= NULL
;
124 size_t noncelen
= 0, entropylen
= 0;
126 if (perslen
> drbg
->max_perslen
) {
127 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
128 RAND_R_PERSONALISATION_STRING_TOO_LONG
);
131 if (drbg
->state
!= DRBG_UNINITIALISED
) {
132 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
133 drbg
->state
== DRBG_ERROR
? RAND_R_IN_ERROR_STATE
134 : RAND_R_ALREADY_INSTANTIATED
);
138 drbg
->state
= DRBG_ERROR
;
139 if (drbg
->get_entropy
!= NULL
)
140 entropylen
= drbg
->get_entropy(drbg
, &entropy
, drbg
->strength
,
141 drbg
->min_entropylen
, drbg
->max_entropylen
);
142 if (entropylen
< drbg
->min_entropylen
143 || entropylen
> drbg
->max_entropylen
) {
144 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
, RAND_R_ERROR_RETRIEVING_ENTROPY
);
148 if (drbg
->max_noncelen
> 0 && drbg
->get_nonce
!= NULL
) {
149 noncelen
= drbg
->get_nonce(drbg
, &nonce
, drbg
->strength
/ 2,
150 drbg
->min_noncelen
, drbg
->max_noncelen
);
151 if (noncelen
< drbg
->min_noncelen
|| noncelen
> drbg
->max_noncelen
) {
152 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
153 RAND_R_ERROR_RETRIEVING_NONCE
);
158 if (!ctr_instantiate(drbg
, entropy
, entropylen
,
159 nonce
, noncelen
, pers
, perslen
)) {
160 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
, RAND_R_ERROR_INSTANTIATING_DRBG
);
164 drbg
->state
= DRBG_READY
;
165 drbg
->reseed_counter
= 1;
168 if (entropy
!= NULL
&& drbg
->cleanup_entropy
!= NULL
)
169 drbg
->cleanup_entropy(drbg
, entropy
, entropylen
);
170 if (nonce
!= NULL
&& drbg
->cleanup_nonce
!= NULL
)
171 drbg
->cleanup_nonce(drbg
, nonce
, noncelen
);
172 if (drbg
->pool
!= NULL
) {
173 if (drbg
->state
== DRBG_READY
) {
174 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE
,
175 RAND_R_ERROR_ENTROPY_POOL_WAS_IGNORED
);
176 drbg
->state
= DRBG_ERROR
;
178 RAND_POOL_free(drbg
->pool
);
181 if (drbg
->state
== DRBG_READY
)
187 * Uninstantiate |drbg|. Must be instantiated before it can be used.
189 int RAND_DRBG_uninstantiate(RAND_DRBG
*drbg
)
191 int ret
= ctr_uninstantiate(drbg
);
193 OPENSSL_cleanse(&drbg
->ctr
, sizeof(drbg
->ctr
));
194 drbg
->state
= DRBG_UNINITIALISED
;
199 * Reseed |drbg|, mixing in the specified data
201 int RAND_DRBG_reseed(RAND_DRBG
*drbg
,
202 const unsigned char *adin
, size_t adinlen
)
204 unsigned char *entropy
= NULL
;
205 size_t entropylen
= 0;
207 if (drbg
->state
== DRBG_ERROR
) {
208 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_IN_ERROR_STATE
);
211 if (drbg
->state
== DRBG_UNINITIALISED
) {
212 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_NOT_INSTANTIATED
);
218 else if (adinlen
> drbg
->max_adinlen
) {
219 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
223 drbg
->state
= DRBG_ERROR
;
224 if (drbg
->get_entropy
!= NULL
)
225 entropylen
= drbg
->get_entropy(drbg
, &entropy
, drbg
->strength
,
226 drbg
->min_entropylen
, drbg
->max_entropylen
);
227 if (entropylen
< drbg
->min_entropylen
228 || entropylen
> drbg
->max_entropylen
) {
229 RANDerr(RAND_F_RAND_DRBG_RESEED
, RAND_R_ERROR_RETRIEVING_ENTROPY
);
233 if (!ctr_reseed(drbg
, entropy
, entropylen
, adin
, adinlen
))
235 drbg
->state
= DRBG_READY
;
236 drbg
->reseed_counter
= 1;
239 if (entropy
!= NULL
&& drbg
->cleanup_entropy
!= NULL
)
240 drbg
->cleanup_entropy(drbg
, entropy
, entropylen
);
241 if (drbg
->state
== DRBG_READY
)
247 * Restart |drbg|, using the specified entropy or additional input
249 * Tries its best to get the drbg instantiated by all means,
250 * regardless of its current state.
252 * Optionally, a |buffer| of |len| random bytes can be passed,
253 * which is assumed to contain at least |entropy| bits of entropy.
255 * If |entropy| > 0, the buffer content is used as entropy input.
257 * If |entropy| == 0, the buffer content is used as additional input
259 * Returns 1 on success, 0 on failure.
261 * This function is used internally only.
263 int rand_drbg_restart(RAND_DRBG
*drbg
,
264 const unsigned char *buffer
, size_t len
, size_t entropy
)
267 const unsigned char *adin
= NULL
;
270 if (drbg
->pool
!= NULL
) {
271 RANDerr(RAND_F_RAND_DRBG_RESTART
, ERR_R_INTERNAL_ERROR
);
272 RAND_POOL_free(drbg
->pool
);
276 if (buffer
!= NULL
) {
278 if (drbg
->max_entropylen
< len
) {
279 RANDerr(RAND_F_RAND_DRBG_RESTART
,
280 RAND_R_ENTROPY_INPUT_TOO_LONG
);
284 if (entropy
> 8 * len
) {
285 RANDerr(RAND_F_RAND_DRBG_RESTART
, RAND_R_ENTROPY_OUT_OF_RANGE
);
289 /* will be picked up by the rand_drbg_get_entropy() callback */
290 drbg
->pool
= RAND_POOL_new(entropy
, len
, len
);
291 if (drbg
->pool
== NULL
)
294 RAND_POOL_add(drbg
->pool
, buffer
, len
, entropy
);
296 if (drbg
->max_adinlen
< len
) {
297 RANDerr(RAND_F_RAND_DRBG_RESTART
,
298 RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
306 /* repair error state */
307 if (drbg
->state
== DRBG_ERROR
)
308 RAND_DRBG_uninstantiate(drbg
);
310 /* repair uninitialized state */
311 if (drbg
->state
== DRBG_UNINITIALISED
) {
312 drbg_setup(drbg
, NULL
);
313 /* already reseeded. prevent second reseeding below */
314 reseeded
= (drbg
->state
== DRBG_READY
);
317 /* refresh current state if entropy or additional input has been provided */
318 if (drbg
->state
== DRBG_READY
) {
321 * mix in additional input without reseeding
323 * Similar to RAND_DRBG_reseed(), but the provided additional
324 * data |adin| is mixed into the current state without pulling
325 * entropy from the trusted entropy source using get_entropy().
326 * This is not a reseeding in the strict sense of NIST SP 800-90A.
328 ctr_reseed(drbg
, adin
, adinlen
, NULL
, 0);
329 } else if (reseeded
== 0) {
330 /* do a full reseeding if it has not been done yet above */
331 RAND_DRBG_reseed(drbg
, NULL
, 0);
335 /* check whether a given entropy pool was cleared properly during reseed */
336 if (drbg
->pool
!= NULL
) {
337 drbg
->state
= DRBG_ERROR
;
338 RANDerr(RAND_F_RAND_DRBG_RESTART
, ERR_R_INTERNAL_ERROR
);
339 RAND_POOL_free(drbg
->pool
);
344 return drbg
->state
== DRBG_READY
;
348 * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
349 * to or if |prediction_resistance| is set. Additional input can be
350 * sent in |adin| and |adinlen|.
352 * Returns 1 on success, 0 on failure.
355 int RAND_DRBG_generate(RAND_DRBG
*drbg
, unsigned char *out
, size_t outlen
,
356 int prediction_resistance
,
357 const unsigned char *adin
, size_t adinlen
)
359 if (drbg
->state
!= DRBG_READY
) {
360 /* try to recover from previous errors */
361 rand_drbg_restart(drbg
, NULL
, 0, 0);
363 if (drbg
->state
== DRBG_ERROR
) {
364 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_IN_ERROR_STATE
);
367 if (drbg
->state
== DRBG_UNINITIALISED
) {
368 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_NOT_INSTANTIATED
);
373 if (outlen
> drbg
->max_request
) {
374 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG
);
377 if (adinlen
> drbg
->max_adinlen
) {
378 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_ADDITIONAL_INPUT_TOO_LONG
);
382 if (drbg
->fork_count
!= rand_fork_count
) {
383 drbg
->fork_count
= rand_fork_count
;
384 drbg
->state
= DRBG_RESEED
;
387 if (drbg
->reseed_counter
>= drbg
->reseed_interval
)
388 drbg
->state
= DRBG_RESEED
;
390 if (drbg
->state
== DRBG_RESEED
|| prediction_resistance
) {
391 if (!RAND_DRBG_reseed(drbg
, adin
, adinlen
)) {
392 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_RESEED_ERROR
);
399 if (!ctr_generate(drbg
, out
, outlen
, adin
, adinlen
)) {
400 drbg
->state
= DRBG_ERROR
;
401 RANDerr(RAND_F_RAND_DRBG_GENERATE
, RAND_R_GENERATE_ERROR
);
405 if (drbg
->reseed_counter
>= drbg
->reseed_interval
)
406 drbg
->state
= DRBG_RESEED
;
408 drbg
->reseed_counter
++;
413 * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
415 * In the following, the signature and the semantics of the
416 * get_entropy() and cleanup_entropy() callbacks are explained.
420 * size_t get_entropy(RAND_DRBG *ctx,
421 * unsigned char **pout,
423 * size_t min_len, size_t max_len);
425 * This is a request to allocate and fill a buffer of size
426 * |min_len| <= size <= |max_len| (in bytes) which contains
427 * at least |entropy| bits of randomness. The buffer's address is
428 * to be returned in |*pout| and the number of collected
429 * randomness bytes (which may be less than the allocated size
430 * of the buffer) as return value.
432 * If the callback fails to acquire at least |entropy| bits of
433 * randomness, it shall return a buffer length of 0.
437 * void cleanup_entropy(RAND_DRBG *ctx,
438 * unsigned char *out, size_t outlen);
440 * A request to clear and free the buffer allocated by get_entropy().
441 * The values |out| and |outlen| are expected to be the random buffer's
442 * address and length, as returned by the get_entropy() callback.
444 * GET_NONCE, CLEANUP_NONCE
446 * Signature and semantics of the get_nonce() and cleanup_nonce()
447 * callbacks are analogous to get_entropy() and cleanup_entropy().
448 * Currently, the nonce is used only for the known answer tests.
450 int RAND_DRBG_set_callbacks(RAND_DRBG
*drbg
,
451 RAND_DRBG_get_entropy_fn get_entropy
,
452 RAND_DRBG_cleanup_entropy_fn cleanup_entropy
,
453 RAND_DRBG_get_nonce_fn get_nonce
,
454 RAND_DRBG_cleanup_nonce_fn cleanup_nonce
)
456 if (drbg
->state
!= DRBG_UNINITIALISED
)
458 drbg
->get_entropy
= get_entropy
;
459 drbg
->cleanup_entropy
= cleanup_entropy
;
460 drbg
->get_nonce
= get_nonce
;
461 drbg
->cleanup_nonce
= cleanup_nonce
;
466 * Set the reseed interval.
468 int RAND_DRBG_set_reseed_interval(RAND_DRBG
*drbg
, int interval
)
470 if (interval
< 0 || interval
> MAX_RESEED
)
472 drbg
->reseed_interval
= interval
;
477 * Get and set the EXDATA
479 int RAND_DRBG_set_ex_data(RAND_DRBG
*drbg
, int idx
, void *arg
)
481 return CRYPTO_set_ex_data(&drbg
->ex_data
, idx
, arg
);
484 void *RAND_DRBG_get_ex_data(const RAND_DRBG
*drbg
, int idx
)
486 return CRYPTO_get_ex_data(&drbg
->ex_data
, idx
);
491 * The following functions provide a RAND_METHOD that works on the
492 * global DRBG. They lock.
496 * Initializes the DRBG with default settings.
497 * For global DRBGs a global lock is created with the given name
498 * Returns 1 on success, 0 on failure
500 static int drbg_setup(RAND_DRBG
*drbg
, const char *name
)
505 if (drbg
->lock
!= NULL
) {
506 RANDerr(RAND_F_DRBG_SETUP
, ERR_R_INTERNAL_ERROR
);
510 drbg
->lock
= CRYPTO_THREAD_glock_new(name
);
511 if (drbg
->lock
== NULL
) {
512 RANDerr(RAND_F_DRBG_SETUP
, RAND_R_FAILED_TO_CREATE_LOCK
);
517 ret
&= RAND_DRBG_set(drbg
,
518 RAND_DRBG_NID
, RAND_DRBG_FLAG_CTR_USE_DF
) == 1;
519 ret
&= RAND_DRBG_set_callbacks(drbg
, rand_drbg_get_entropy
,
520 rand_drbg_cleanup_entropy
, NULL
, NULL
) == 1;
522 * Ignore instantiation error so support just-in-time instantiation.
524 * The state of the drbg will be checked in RAND_DRBG_generate() and
525 * an automatic recovery is attempted.
527 RAND_DRBG_instantiate(drbg
,
528 (const unsigned char *) ossl_pers_string
,
529 sizeof(ossl_pers_string
) - 1);
534 * Initialize the global DRBGs on first use.
535 * Returns 1 on success, 0 on failure.
537 DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init
)
541 ret
&= drbg_setup(&rand_drbg
, "rand_drbg");
542 ret
&= drbg_setup(&priv_drbg
, "priv_drbg");
547 /* Cleans up the given global DRBG */
548 static void drbg_cleanup(RAND_DRBG
*drbg
)
550 CRYPTO_THREAD_lock_free(drbg
->lock
);
551 RAND_DRBG_uninstantiate(drbg
);
554 /* Clean up the global DRBGs before exit */
555 void rand_drbg_cleanup_int(void)
557 drbg_cleanup(&rand_drbg
);
558 drbg_cleanup(&priv_drbg
);
561 /* Implements the default OpenSSL RAND_bytes() method */
562 static int drbg_bytes(unsigned char *out
, int count
)
566 RAND_DRBG
*drbg
= RAND_DRBG_get0_global();
571 CRYPTO_THREAD_write_lock(drbg
->lock
);
572 if (drbg
->state
== DRBG_UNINITIALISED
)
575 for ( ; count
> 0; count
-= chunk
, out
+= chunk
) {
577 if (chunk
> drbg
->max_request
)
578 chunk
= drbg
->max_request
;
579 ret
= RAND_DRBG_generate(drbg
, out
, chunk
, 0, NULL
, 0);
586 CRYPTO_THREAD_unlock(drbg
->lock
);
590 /* Implements the default OpenSSL RAND_add() method */
591 static int drbg_add(const void *buf
, int num
, double randomness
)
594 RAND_DRBG
*drbg
= RAND_DRBG_get0_global();
599 if (num
< 0 || randomness
< 0.0)
602 if (randomness
> (double)drbg
->max_entropylen
) {
604 * The purpose of this check is to bound |randomness| by a
605 * relatively small value in order to prevent an integer
606 * overflow when multiplying by 8 in the rand_drbg_restart()
612 CRYPTO_THREAD_write_lock(drbg
->lock
);
613 ret
= rand_drbg_restart(drbg
, buf
,
614 (size_t)(unsigned int)num
,
615 (size_t)(8*randomness
));
616 CRYPTO_THREAD_unlock(drbg
->lock
);
621 /* Implements the default OpenSSL RAND_seed() method */
622 static int drbg_seed(const void *buf
, int num
)
624 return drbg_add(buf
, num
, num
);
627 /* Implements the default OpenSSL RAND_status() method */
628 static int drbg_status(void)
631 RAND_DRBG
*drbg
= RAND_DRBG_get0_global();
636 CRYPTO_THREAD_write_lock(drbg
->lock
);
637 ret
= drbg
->state
== DRBG_READY
? 1 : 0;
638 CRYPTO_THREAD_unlock(drbg
->lock
);
643 * Get the global public DRBG.
644 * Returns pointer to the DRBG on success, NULL on failure.
646 RAND_DRBG
*RAND_DRBG_get0_global(void)
648 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
655 * Get the global private DRBG.
656 * Returns pointer to the DRBG on success, NULL on failure.
658 RAND_DRBG
*RAND_DRBG_get0_priv_global(void)
660 if (!RUN_ONCE(&rand_drbg_init
, do_rand_drbg_init
))
666 RAND_METHOD rand_meth
= {
675 RAND_METHOD
*RAND_OpenSSL(void)