2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
12 #include "internal/cryptlib.h"
13 #include <openssl/opensslconf.h>
14 #include "crypto/rand.h"
15 #include <openssl/engine.h>
16 #include "internal/thread_once.h"
17 #include "rand_local.h"
21 # ifndef OPENSSL_NO_ENGINE
22 /* non-NULL if default_RAND_meth is ENGINE-provided */
23 static ENGINE *funct_ref;
24 static CRYPTO_RWLOCK *rand_engine_lock;
26 static CRYPTO_RWLOCK *rand_meth_lock;
27 static const RAND_METHOD *default_RAND_meth;
28 static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT;
30 static int rand_inited = 0;
31 #endif /* FIPS_MODULE */
33 #ifdef OPENSSL_RAND_SEED_RDTSC
35 * IMPORTANT NOTE: It is not currently possible to use this code
36 * because we are not sure about the amount of randomness it provides.
37 * Some SP900 tests have been run, but there is internal skepticism.
38 * So for now this code is not used.
40 # error "RDTSC enabled? Should not be possible!"
43 * Acquire entropy from high-speed clock
45 * Since we get some randomness from the low-order bits of the
46 * high-speed clock, it can help.
48 * Returns the total entropy count, if it exceeds the requested
49 * entropy count. Otherwise, returns an entropy count of 0.
51 size_t rand_acquire_entropy_from_tsc(RAND_POOL *pool)
56 if ((OPENSSL_ia32cap_P[0] & (1 << 4)) != 0) {
57 for (i = 0; i < TSC_READ_COUNT; i++) {
58 c = (unsigned char)(OPENSSL_rdtsc() & 0xFF);
59 rand_pool_add(pool, &c, 1, 4);
62 return rand_pool_entropy_available(pool);
66 #ifdef OPENSSL_RAND_SEED_RDCPU
67 size_t OPENSSL_ia32_rdseed_bytes(unsigned char *buf, size_t len);
68 size_t OPENSSL_ia32_rdrand_bytes(unsigned char *buf, size_t len);
71 * Acquire entropy using Intel-specific cpu instructions
73 * Uses the RDSEED instruction if available, otherwise uses
74 * RDRAND if available.
76 * For the differences between RDSEED and RDRAND, and why RDSEED
77 * is the preferred choice, see https://goo.gl/oK3KcN
79 * Returns the total entropy count, if it exceeds the requested
80 * entropy count. Otherwise, returns an entropy count of 0.
82 size_t rand_acquire_entropy_from_cpu(RAND_POOL *pool)
85 unsigned char *buffer;
87 bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
88 if (bytes_needed > 0) {
89 buffer = rand_pool_add_begin(pool, bytes_needed);
92 /* Whichever comes first, use RDSEED, RDRAND or nothing */
93 if ((OPENSSL_ia32cap_P[2] & (1 << 18)) != 0) {
94 if (OPENSSL_ia32_rdseed_bytes(buffer, bytes_needed)
96 rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
98 } else if ((OPENSSL_ia32cap_P[1] & (1 << (62 - 32))) != 0) {
99 if (OPENSSL_ia32_rdrand_bytes(buffer, bytes_needed)
101 rand_pool_add_end(pool, bytes_needed, 8 * bytes_needed);
104 rand_pool_add_end(pool, 0, 0);
109 return rand_pool_entropy_available(pool);
115 * Implements the get_entropy() callback (see RAND_DRBG_set_callbacks())
117 * If the DRBG has a parent, then the required amount of entropy input
118 * is fetched using the parent's RAND_DRBG_generate().
120 * Otherwise, the entropy is polled from the system entropy sources
121 * using rand_pool_acquire_entropy().
123 * If a random pool has been added to the DRBG using RAND_add(), then
124 * its entropy will be used up first.
126 size_t rand_drbg_get_entropy(RAND_DRBG *drbg,
127 unsigned char **pout,
128 int entropy, size_t min_len, size_t max_len,
129 int prediction_resistance)
132 size_t entropy_available = 0;
135 if (drbg->parent != NULL && drbg->strength > drbg->parent->strength) {
137 * We currently don't support the algorithm from NIST SP 800-90C
138 * 10.1.2 to use a weaker DRBG as source
140 RANDerr(RAND_F_RAND_DRBG_GET_ENTROPY, RAND_R_PARENT_STRENGTH_TOO_WEAK);
144 if (drbg->seed_pool != NULL) {
145 pool = drbg->seed_pool;
146 pool->entropy_requested = entropy;
148 pool = rand_pool_new(entropy, drbg->secure, min_len, max_len);
153 if (drbg->parent != NULL) {
154 size_t bytes_needed = rand_pool_bytes_needed(pool, 1 /*entropy_factor*/);
155 unsigned char *buffer = rand_pool_add_begin(pool, bytes_needed);
157 if (buffer != NULL) {
161 * Get random data from parent. Include our address as additional input,
162 * in order to provide some additional distinction between different
163 * DRBG child instances.
164 * Our lock is already held, but we need to lock our parent before
165 * generating bits from it. (Note: taking the lock will be a no-op
166 * if locking if drbg->parent->lock == NULL.)
168 rand_drbg_lock(drbg->parent);
169 if (RAND_DRBG_generate(drbg->parent,
170 buffer, bytes_needed,
171 prediction_resistance,
172 (unsigned char *)&drbg, sizeof(drbg)) != 0)
173 bytes = bytes_needed;
174 drbg->reseed_next_counter
175 = tsan_load(&drbg->parent->reseed_prop_counter);
176 rand_drbg_unlock(drbg->parent);
178 rand_pool_add_end(pool, bytes, 8 * bytes);
179 entropy_available = rand_pool_entropy_available(pool);
183 /* Get entropy by polling system entropy sources. */
184 entropy_available = rand_pool_acquire_entropy(pool);
187 if (entropy_available > 0) {
188 ret = rand_pool_length(pool);
189 *pout = rand_pool_detach(pool);
192 if (drbg->seed_pool == NULL)
193 rand_pool_free(pool);
198 * Implements the cleanup_entropy() callback (see RAND_DRBG_set_callbacks())
201 void rand_drbg_cleanup_entropy(RAND_DRBG *drbg,
202 unsigned char *out, size_t outlen)
204 if (drbg->seed_pool == NULL) {
206 OPENSSL_secure_clear_free(out, outlen);
208 OPENSSL_clear_free(out, outlen);
213 * Generate additional data that can be used for the drbg. The data does
214 * not need to contain entropy, but it's useful if it contains at least
215 * some bits that are unpredictable.
217 * Returns 0 on failure.
219 * On success it allocates a buffer at |*pout| and returns the length of
220 * the data. The buffer should get freed using OPENSSL_secure_clear_free().
222 size_t rand_drbg_get_additional_data(RAND_POOL *pool, unsigned char **pout)
226 if (rand_pool_add_additional_data(pool) == 0)
229 ret = rand_pool_length(pool);
230 *pout = rand_pool_detach(pool);
236 void rand_drbg_cleanup_additional_data(RAND_POOL *pool, unsigned char *out)
238 rand_pool_reattach(pool, out);
243 DEFINE_RUN_ONCE_STATIC(do_rand_init)
245 # ifndef OPENSSL_NO_ENGINE
246 rand_engine_lock = CRYPTO_THREAD_lock_new();
247 if (rand_engine_lock == NULL)
251 rand_meth_lock = CRYPTO_THREAD_lock_new();
252 if (rand_meth_lock == NULL)
255 if (!rand_pool_init())
262 CRYPTO_THREAD_lock_free(rand_meth_lock);
263 rand_meth_lock = NULL;
264 # ifndef OPENSSL_NO_ENGINE
265 CRYPTO_THREAD_lock_free(rand_engine_lock);
266 rand_engine_lock = NULL;
271 void rand_cleanup_int(void)
273 const RAND_METHOD *meth = default_RAND_meth;
278 if (meth != NULL && meth->cleanup != NULL)
280 RAND_set_rand_method(NULL);
282 # ifndef OPENSSL_NO_ENGINE
283 CRYPTO_THREAD_lock_free(rand_engine_lock);
284 rand_engine_lock = NULL;
286 CRYPTO_THREAD_lock_free(rand_meth_lock);
287 rand_meth_lock = NULL;
291 /* TODO(3.0): Do we need to handle this somehow in the FIPS module? */
293 * RAND_close_seed_files() ensures that any seed file descriptors are
296 void RAND_keep_random_devices_open(int keep)
298 if (RUN_ONCE(&rand_init, do_rand_init))
299 rand_pool_keep_random_devices_open(keep);
303 * RAND_poll() reseeds the default RNG using random input
305 * The random input is obtained from polling various entropy
306 * sources which depend on the operating system and are
307 * configurable via the --with-rand-seed configure option.
313 const RAND_METHOD *meth = RAND_get_rand_method();
318 if (meth == RAND_OpenSSL()) {
319 /* fill random pool and seed the master DRBG */
320 RAND_DRBG *drbg = RAND_DRBG_get0_master();
326 ret = rand_drbg_restart(drbg, NULL, 0, 0);
332 RAND_POOL *pool = NULL;
334 /* fill random pool and seed the current legacy RNG */
335 pool = rand_pool_new(RAND_DRBG_STRENGTH, 1,
336 (RAND_DRBG_STRENGTH + 7) / 8,
337 RAND_POOL_MAX_LENGTH);
341 if (rand_pool_acquire_entropy(pool) == 0)
344 if (meth->add == NULL
345 || meth->add(rand_pool_buffer(pool),
346 rand_pool_length(pool),
347 (rand_pool_entropy(pool) / 8.0)) == 0)
353 rand_pool_free(pool);
359 int RAND_set_rand_method(const RAND_METHOD *meth)
361 if (!RUN_ONCE(&rand_init, do_rand_init))
364 CRYPTO_THREAD_write_lock(rand_meth_lock);
365 # ifndef OPENSSL_NO_ENGINE
366 ENGINE_finish(funct_ref);
369 default_RAND_meth = meth;
370 CRYPTO_THREAD_unlock(rand_meth_lock);
373 #endif /* FIPS_MODULE */
375 const RAND_METHOD *RAND_get_rand_method(void)
380 const RAND_METHOD *tmp_meth = NULL;
382 if (!RUN_ONCE(&rand_init, do_rand_init))
385 CRYPTO_THREAD_write_lock(rand_meth_lock);
386 if (default_RAND_meth == NULL) {
387 # ifndef OPENSSL_NO_ENGINE
390 /* If we have an engine that can do RAND, use it. */
391 if ((e = ENGINE_get_default_RAND()) != NULL
392 && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {
394 default_RAND_meth = tmp_meth;
397 default_RAND_meth = &rand_meth;
400 default_RAND_meth = &rand_meth;
403 tmp_meth = default_RAND_meth;
404 CRYPTO_THREAD_unlock(rand_meth_lock);
409 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
410 int RAND_set_rand_engine(ENGINE *engine)
412 const RAND_METHOD *tmp_meth = NULL;
414 if (!RUN_ONCE(&rand_init, do_rand_init))
417 if (engine != NULL) {
418 if (!ENGINE_init(engine))
420 tmp_meth = ENGINE_get_RAND(engine);
421 if (tmp_meth == NULL) {
422 ENGINE_finish(engine);
426 CRYPTO_THREAD_write_lock(rand_engine_lock);
427 /* This function releases any prior ENGINE so call it first */
428 RAND_set_rand_method(tmp_meth);
430 CRYPTO_THREAD_unlock(rand_engine_lock);
435 void RAND_seed(const void *buf, int num)
437 const RAND_METHOD *meth = RAND_get_rand_method();
439 if (meth != NULL && meth->seed != NULL)
440 meth->seed(buf, num);
443 void RAND_add(const void *buf, int num, double randomness)
445 const RAND_METHOD *meth = RAND_get_rand_method();
447 if (meth != NULL && meth->add != NULL)
448 meth->add(buf, num, randomness);
452 * This function is not part of RAND_METHOD, so if we're not using
453 * the default method, then just call RAND_bytes(). Otherwise make
454 * sure we're instantiated and use the private DRBG.
456 int RAND_priv_bytes_ex(OPENSSL_CTX *ctx, unsigned char *buf, int num)
459 const RAND_METHOD *meth = RAND_get_rand_method();
461 if (meth != NULL && meth != RAND_OpenSSL()) {
462 if (meth->bytes != NULL)
463 return meth->bytes(buf, num);
464 RANDerr(RAND_F_RAND_PRIV_BYTES_EX, RAND_R_FUNC_NOT_IMPLEMENTED);
468 drbg = OPENSSL_CTX_get0_private_drbg(ctx);
470 return RAND_DRBG_bytes(drbg, buf, num);
475 int RAND_priv_bytes(unsigned char *buf, int num)
477 return RAND_priv_bytes_ex(NULL, buf, num);
480 int RAND_bytes_ex(OPENSSL_CTX *ctx, unsigned char *buf, int num)
483 const RAND_METHOD *meth = RAND_get_rand_method();
485 if (meth != NULL && meth != RAND_OpenSSL()) {
486 if (meth->bytes != NULL)
487 return meth->bytes(buf, num);
488 RANDerr(RAND_F_RAND_BYTES_EX, RAND_R_FUNC_NOT_IMPLEMENTED);
492 drbg = OPENSSL_CTX_get0_public_drbg(ctx);
494 return RAND_DRBG_bytes(drbg, buf, num);
499 int RAND_bytes(unsigned char *buf, int num)
501 return RAND_bytes_ex(NULL, buf, num);
504 #if !defined(OPENSSL_NO_DEPRECATED_1_1_0) && !defined(FIPS_MODULE)
505 int RAND_pseudo_bytes(unsigned char *buf, int num)
507 const RAND_METHOD *meth = RAND_get_rand_method();
509 if (meth != NULL && meth->pseudorand != NULL)
510 return meth->pseudorand(buf, num);
511 RANDerr(RAND_F_RAND_PSEUDO_BYTES, RAND_R_FUNC_NOT_IMPLEMENTED);
516 int RAND_status(void)
518 const RAND_METHOD *meth = RAND_get_rand_method();
520 if (meth != NULL && meth->status != NULL)
521 return meth->status();