]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rand/rand_lib.c
rand: trust user supplied entropy when configured without a random source
[thirdparty/openssl.git] / crypto / rand / rand_lib.c
CommitLineData
b1322259 1/*
fecb3aae 2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
dfeab068 3 *
0db63de9 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
dfeab068
RE
8 */
9
e4468e6d
P
10/* We need to use some engine deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
44d2482b 13#include <openssl/err.h>
98186eb4 14#include <openssl/opensslconf.h>
7d615e21 15#include <openssl/core_names.h>
dce7272d 16#include "internal/cryptlib.h"
87975cfa 17#include "internal/thread_once.h"
dce7272d
TM
18#include "crypto/rand.h"
19#include "crypto/cryptlib.h"
706457b7 20#include "rand_local.h"
927d0566 21#include "crypto/context.h"
dfeab068 22
f844f9eb 23#ifndef FIPS_MODULE
dce7272d
TM
24# include <stdio.h>
25# include <time.h>
26# include <limits.h>
27# include <openssl/conf.h>
28# include <openssl/trace.h>
29# include <openssl/engine.h>
03bede0c 30# include "crypto/rand_pool.h"
f000e828 31# include "prov/seeding.h"
d5f9166b 32# include "internal/e_os.h"
f000e828 33
a2f27fd7 34# ifndef OPENSSL_NO_ENGINE
cb78486d 35/* non-NULL if default_RAND_meth is ENGINE-provided */
da8fc25a
RS
36static ENGINE *funct_ref;
37static CRYPTO_RWLOCK *rand_engine_lock;
a2f27fd7 38# endif
786b13fa 39# ifndef OPENSSL_NO_DEPRECATED_3_0
da8fc25a
RS
40static CRYPTO_RWLOCK *rand_meth_lock;
41static const RAND_METHOD *default_RAND_meth;
786b13fa 42# endif
da8fc25a 43static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT;
c16de9d8 44
e2d227bb 45static int rand_inited = 0;
ddc6a5c8 46
da8fc25a 47DEFINE_RUN_ONCE_STATIC(do_rand_init)
87975cfa 48{
a2f27fd7 49# ifndef OPENSSL_NO_ENGINE
63ab5ea1 50 rand_engine_lock = CRYPTO_THREAD_lock_new();
0e5c1a66
BE
51 if (rand_engine_lock == NULL)
52 return 0;
a2f27fd7 53# endif
0e5c1a66 54
786b13fa 55# ifndef OPENSSL_NO_DEPRECATED_3_0
63ab5ea1 56 rand_meth_lock = CRYPTO_THREAD_lock_new();
0e5c1a66 57 if (rand_meth_lock == NULL)
a2f27fd7 58 goto err;
786b13fa 59# endif
5bc6bcf8 60
1335ca4b 61 if (!ossl_rand_pool_init())
a2f27fd7 62 goto err;
c7504aeb 63
e2d227bb 64 rand_inited = 1;
0e5c1a66
BE
65 return 1;
66
a2f27fd7 67 err:
786b13fa 68# ifndef OPENSSL_NO_DEPRECATED_3_0
0e5c1a66
BE
69 CRYPTO_THREAD_lock_free(rand_meth_lock);
70 rand_meth_lock = NULL;
786b13fa 71# endif
a2f27fd7 72# ifndef OPENSSL_NO_ENGINE
0e5c1a66
BE
73 CRYPTO_THREAD_lock_free(rand_engine_lock);
74 rand_engine_lock = NULL;
a2f27fd7 75# endif
0e5c1a66 76 return 0;
87975cfa 77}
dfeab068 78
1335ca4b 79void ossl_rand_cleanup_int(void)
da8fc25a 80{
786b13fa 81# ifndef OPENSSL_NO_DEPRECATED_3_0
da8fc25a
RS
82 const RAND_METHOD *meth = default_RAND_meth;
83
e2d227bb
BE
84 if (!rand_inited)
85 return;
bc420ebe 86
da8fc25a
RS
87 if (meth != NULL && meth->cleanup != NULL)
88 meth->cleanup();
89 RAND_set_rand_method(NULL);
786b13fa 90# endif
1335ca4b 91 ossl_rand_pool_cleanup();
a2f27fd7 92# ifndef OPENSSL_NO_ENGINE
da8fc25a 93 CRYPTO_THREAD_lock_free(rand_engine_lock);
0e5c1a66 94 rand_engine_lock = NULL;
a2f27fd7 95# endif
786b13fa 96# ifndef OPENSSL_NO_DEPRECATED_3_0
da8fc25a 97 CRYPTO_THREAD_lock_free(rand_meth_lock);
0e5c1a66 98 rand_meth_lock = NULL;
786b13fa 99# endif
a88e97fc 100 ossl_release_default_drbg_ctx();
e2d227bb 101 rand_inited = 0;
75e2c877
RS
102}
103
c7504aeb 104/*
c2969ff6 105 * RAND_close_seed_files() ensures that any seed file descriptors are
f000e828
P
106 * closed after use. This only applies to libcrypto/default provider,
107 * it does not apply to other providers.
c7504aeb
P
108 */
109void RAND_keep_random_devices_open(int keep)
110{
ac765685 111 if (RUN_ONCE(&rand_init, do_rand_init))
1335ca4b 112 ossl_rand_pool_keep_random_devices_open(keep);
c7504aeb
P
113}
114
75e2c877 115/*
c16de9d8
DMSP
116 * RAND_poll() reseeds the default RNG using random input
117 *
118 * The random input is obtained from polling various entropy
119 * sources which depend on the operating system and are
120 * configurable via the --with-rand-seed configure option.
121 */
122int RAND_poll(void)
123{
786b13fa 124# ifndef OPENSSL_NO_DEPRECATED_3_0
c16de9d8 125 const RAND_METHOD *meth = RAND_get_rand_method();
f000e828 126 int ret = meth == RAND_OpenSSL();
c16de9d8 127
0402c90f
DMSP
128 if (meth == NULL)
129 return 0;
130
f000e828 131 if (!ret) {
c16de9d8 132 /* fill random pool and seed the current legacy RNG */
1335ca4b
SL
133 RAND_POOL *pool = ossl_rand_pool_new(RAND_DRBG_STRENGTH, 1,
134 (RAND_DRBG_STRENGTH + 7) / 8,
135 RAND_POOL_MAX_LENGTH);
7d615e21 136
c16de9d8
DMSP
137 if (pool == NULL)
138 return 0;
f000e828 139
1dc188ba 140 if (ossl_pool_acquire_entropy(pool) == 0)
c16de9d8 141 goto err;
f000e828 142
c16de9d8 143 if (meth->add == NULL
1335ca4b
SL
144 || meth->add(ossl_rand_pool_buffer(pool),
145 ossl_rand_pool_length(pool),
146 (ossl_rand_pool_entropy(pool) / 8.0)) == 0)
c16de9d8
DMSP
147 goto err;
148
149 ret = 1;
a2f27fd7 150 err:
1335ca4b 151 ossl_rand_pool_free(pool);
c16de9d8 152 }
c16de9d8 153 return ret;
786b13fa
P
154# else
155 static const char salt[] = "polling";
156
157 RAND_seed(salt, sizeof(salt));
158 return 1;
159# endif
c16de9d8 160}
c16de9d8 161
786b13fa 162# ifndef OPENSSL_NO_DEPRECATED_3_0
8f4cddbc
P
163static int rand_set_rand_method_internal(const RAND_METHOD *meth,
164 ossl_unused ENGINE *e)
0f113f3e 165{
da8fc25a 166 if (!RUN_ONCE(&rand_init, do_rand_init))
87975cfa
RL
167 return 0;
168
cd3f8c1b
RS
169 if (!CRYPTO_THREAD_write_lock(rand_meth_lock))
170 return 0;
786b13fa 171# ifndef OPENSSL_NO_ENGINE
7c96dbcd 172 ENGINE_finish(funct_ref);
8f4cddbc 173 funct_ref = e;
786b13fa 174# endif
0f113f3e 175 default_RAND_meth = meth;
87975cfa 176 CRYPTO_THREAD_unlock(rand_meth_lock);
0f113f3e
MC
177 return 1;
178}
dfeab068 179
8f4cddbc
P
180int RAND_set_rand_method(const RAND_METHOD *meth)
181{
182 return rand_set_rand_method_internal(meth, NULL);
183}
184
a4a9d97a 185const RAND_METHOD *RAND_get_rand_method(void)
0f113f3e 186{
87975cfa
RL
187 const RAND_METHOD *tmp_meth = NULL;
188
da8fc25a 189 if (!RUN_ONCE(&rand_init, do_rand_init))
87975cfa
RL
190 return NULL;
191
cd3f8c1b
RS
192 if (!CRYPTO_THREAD_write_lock(rand_meth_lock))
193 return NULL;
da8fc25a 194 if (default_RAND_meth == NULL) {
786b13fa 195# ifndef OPENSSL_NO_ENGINE
da8fc25a
RS
196 ENGINE *e;
197
198 /* If we have an engine that can do RAND, use it. */
199 if ((e = ENGINE_get_default_RAND()) != NULL
200 && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {
0f113f3e 201 funct_ref = e;
da8fc25a
RS
202 default_RAND_meth = tmp_meth;
203 } else {
204 ENGINE_finish(e);
1335ca4b 205 default_RAND_meth = &ossl_rand_meth;
da8fc25a 206 }
786b13fa 207# else
1335ca4b 208 default_RAND_meth = &ossl_rand_meth;
786b13fa 209# endif
0f113f3e 210 }
87975cfa
RL
211 tmp_meth = default_RAND_meth;
212 CRYPTO_THREAD_unlock(rand_meth_lock);
213 return tmp_meth;
0f113f3e 214}
cb78486d 215
786b13fa 216# if !defined(OPENSSL_NO_ENGINE)
cb78486d 217int RAND_set_rand_engine(ENGINE *engine)
0f113f3e
MC
218{
219 const RAND_METHOD *tmp_meth = NULL;
87975cfa 220
da8fc25a 221 if (!RUN_ONCE(&rand_init, do_rand_init))
87975cfa
RL
222 return 0;
223
da8fc25a 224 if (engine != NULL) {
0f113f3e
MC
225 if (!ENGINE_init(engine))
226 return 0;
227 tmp_meth = ENGINE_get_RAND(engine);
7c96dbcd 228 if (tmp_meth == NULL) {
0f113f3e
MC
229 ENGINE_finish(engine);
230 return 0;
231 }
232 }
cd3f8c1b
RS
233 if (!CRYPTO_THREAD_write_lock(rand_engine_lock)) {
234 ENGINE_finish(engine);
235 return 0;
236 }
237
0f113f3e 238 /* This function releases any prior ENGINE so call it first */
8f4cddbc 239 rand_set_rand_method_internal(tmp_meth, engine);
87975cfa 240 CRYPTO_THREAD_unlock(rand_engine_lock);
0f113f3e
MC
241 return 1;
242}
786b13fa
P
243# endif
244# endif /* OPENSSL_NO_DEPRECATED_3_0 */
dfeab068 245
6343829a 246void RAND_seed(const void *buf, int num)
0f113f3e 247{
786b13fa
P
248 EVP_RAND_CTX *drbg;
249# ifndef OPENSSL_NO_DEPRECATED_3_0
0f113f3e 250 const RAND_METHOD *meth = RAND_get_rand_method();
da8fc25a 251
786b13fa 252 if (meth != NULL && meth->seed != NULL) {
0f113f3e 253 meth->seed(buf, num);
786b13fa
P
254 return;
255 }
256# endif
257
258 drbg = RAND_get0_primary(NULL);
259 if (drbg != NULL && num > 0)
260 EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num);
0f113f3e 261}
dfeab068 262
da8fc25a 263void RAND_add(const void *buf, int num, double randomness)
0f113f3e 264{
786b13fa
P
265 EVP_RAND_CTX *drbg;
266# ifndef OPENSSL_NO_DEPRECATED_3_0
0f113f3e 267 const RAND_METHOD *meth = RAND_get_rand_method();
da8fc25a 268
786b13fa 269 if (meth != NULL && meth->add != NULL) {
da8fc25a 270 meth->add(buf, num, randomness);
786b13fa
P
271 return;
272 }
273# endif
274 drbg = RAND_get0_primary(NULL);
275 if (drbg != NULL && num > 0)
56547da9
P
276# ifdef OPENSSL_RAND_SEED_NONE
277 /* Without an entropy source, we have to rely on the user */
278 EVP_RAND_reseed(drbg, 0, buf, num, NULL, 0);
279# else
280 /* With an entropy source, we downgrade this to additional input */
786b13fa 281 EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num);
56547da9 282# endif
0f113f3e 283}
eb952088 284
f000e828
P
285# if !defined(OPENSSL_NO_DEPRECATED_1_1_0)
286int RAND_pseudo_bytes(unsigned char *buf, int num)
287{
288 const RAND_METHOD *meth = RAND_get_rand_method();
289
290 if (meth != NULL && meth->pseudorand != NULL)
291 return meth->pseudorand(buf, num);
9311d0c4 292 ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
f000e828
P
293 return -1;
294}
295# endif
296
297int RAND_status(void)
298{
7d615e21 299 EVP_RAND_CTX *rand;
786b13fa 300# ifndef OPENSSL_NO_DEPRECATED_3_0
f000e828
P
301 const RAND_METHOD *meth = RAND_get_rand_method();
302
303 if (meth != NULL && meth != RAND_OpenSSL())
304 return meth->status != NULL ? meth->status() : 0;
786b13fa 305# endif
f000e828 306
7d615e21 307 if ((rand = RAND_get0_primary(NULL)) == NULL)
4516bf74 308 return 0;
ed576acd 309 return EVP_RAND_get_state(rand) == EVP_RAND_STATE_READY;
f000e828 310}
786b13fa 311# else /* !FIPS_MODULE */
f000e828 312
786b13fa 313# ifndef OPENSSL_NO_DEPRECATED_3_0
f000e828
P
314const RAND_METHOD *RAND_get_rand_method(void)
315{
316 return NULL;
317}
786b13fa 318# endif
f000e828
P
319#endif /* !FIPS_MODULE */
320
ddc6a5c8
RS
321/*
322 * This function is not part of RAND_METHOD, so if we're not using
323 * the default method, then just call RAND_bytes(). Otherwise make
324 * sure we're instantiated and use the private DRBG.
325 */
528685fe 326int RAND_priv_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
508258ca 327 unsigned int strength)
ddc6a5c8 328{
7d615e21 329 EVP_RAND_CTX *rand;
dce7272d 330#if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)
a2f27fd7 331 const RAND_METHOD *meth = RAND_get_rand_method();
ddc6a5c8 332
0402c90f
DMSP
333 if (meth != NULL && meth != RAND_OpenSSL()) {
334 if (meth->bytes != NULL)
335 return meth->bytes(buf, num);
9311d0c4 336 ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
0402c90f
DMSP
337 return -1;
338 }
786b13fa 339#endif
ddc6a5c8 340
7d615e21
P
341 rand = RAND_get0_private(ctx);
342 if (rand != NULL)
508258ca 343 return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);
ddc6a5c8 344
0402c90f 345 return 0;
ddc6a5c8
RS
346}
347
6694e51d 348int RAND_priv_bytes(unsigned char *buf, int num)
0f113f3e 349{
528685fe
P
350 if (num < 0)
351 return 0;
352 return RAND_priv_bytes_ex(NULL, buf, (size_t)num, 0);
6694e51d
MC
353}
354
528685fe 355int RAND_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
508258ca 356 unsigned int strength)
6694e51d 357{
7d615e21 358 EVP_RAND_CTX *rand;
dce7272d 359#if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)
0f113f3e 360 const RAND_METHOD *meth = RAND_get_rand_method();
da8fc25a 361
0402c90f 362 if (meth != NULL && meth != RAND_OpenSSL()) {
6694e51d
MC
363 if (meth->bytes != NULL)
364 return meth->bytes(buf, num);
9311d0c4 365 ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
6694e51d
MC
366 return -1;
367 }
786b13fa 368#endif
6694e51d 369
7d615e21
P
370 rand = RAND_get0_public(ctx);
371 if (rand != NULL)
508258ca 372 return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);
6694e51d 373
0402c90f 374 return 0;
6694e51d
MC
375}
376
377int RAND_bytes(unsigned char *buf, int num)
378{
528685fe
P
379 if (num < 0)
380 return 0;
381 return RAND_bytes_ex(NULL, buf, (size_t)num, 0);
0f113f3e 382}
7d615e21
P
383
384typedef struct rand_global_st {
385 /*
386 * The three shared DRBG instances
387 *
388 * There are three shared DRBG instances: <primary>, <public>, and
389 * <private>. The <public> and <private> DRBGs are secondary ones.
390 * These are used for non-secret (e.g. nonces) and secret
391 * (e.g. private keys) data respectively.
392 */
393 CRYPTO_RWLOCK *lock;
394
81aef6ba
P
395 EVP_RAND_CTX *seed;
396
7d615e21
P
397 /*
398 * The <primary> DRBG
399 *
400 * Not used directly by the application, only for reseeding the two other
401 * DRBGs. It reseeds itself by pulling either randomness from os entropy
402 * sources or by consuming randomness which was added by RAND_add().
403 *
404 * The <primary> DRBG is a global instance which is accessed concurrently by
405 * all threads. The necessary locking is managed automatically by its child
406 * DRBG instances during reseeding.
407 */
408 EVP_RAND_CTX *primary;
409
410 /*
411 * The <public> DRBG
412 *
413 * Used by default for generating random bytes using RAND_bytes().
414 *
415 * The <public> secondary DRBG is thread-local, i.e., there is one instance
416 * per thread.
417 */
418 CRYPTO_THREAD_LOCAL public;
419
420 /*
421 * The <private> DRBG
422 *
423 * Used by default for generating private keys using RAND_priv_bytes()
424 *
425 * The <private> secondary DRBG is thread-local, i.e., there is one
426 * instance per thread.
427 */
428 CRYPTO_THREAD_LOCAL private;
44d2482b
P
429
430 /* Which RNG is being used by default and it's configuration settings */
431 char *rng_name;
432 char *rng_cipher;
433 char *rng_digest;
434 char *rng_propq;
81aef6ba
P
435
436 /* Allow the randomness source to be changed */
437 char *seed_name;
438 char *seed_propq;
7d615e21
P
439} RAND_GLOBAL;
440
441/*
b4250010 442 * Initialize the OSSL_LIB_CTX global DRBGs on first use.
7d615e21
P
443 * Returns the allocated global data on success or NULL on failure.
444 */
927d0566 445void *ossl_rand_ctx_new(OSSL_LIB_CTX *libctx)
7d615e21
P
446{
447 RAND_GLOBAL *dgbl = OPENSSL_zalloc(sizeof(*dgbl));
448
449 if (dgbl == NULL)
450 return NULL;
451
452#ifndef FIPS_MODULE
453 /*
454 * We need to ensure that base libcrypto thread handling has been
455 * initialised.
456 */
12b4e582 457 OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL);
7d615e21
P
458#endif
459
460 dgbl->lock = CRYPTO_THREAD_lock_new();
461 if (dgbl->lock == NULL)
462 goto err1;
463
464 if (!CRYPTO_THREAD_init_local(&dgbl->private, NULL))
465 goto err1;
466
467 if (!CRYPTO_THREAD_init_local(&dgbl->public, NULL))
468 goto err2;
469
470 return dgbl;
471
472 err2:
473 CRYPTO_THREAD_cleanup_local(&dgbl->private);
474 err1:
475 CRYPTO_THREAD_lock_free(dgbl->lock);
476 OPENSSL_free(dgbl);
477 return NULL;
478}
479
927d0566 480void ossl_rand_ctx_free(void *vdgbl)
7d615e21
P
481{
482 RAND_GLOBAL *dgbl = vdgbl;
483
484 if (dgbl == NULL)
485 return;
486
487 CRYPTO_THREAD_lock_free(dgbl->lock);
7d615e21
P
488 CRYPTO_THREAD_cleanup_local(&dgbl->private);
489 CRYPTO_THREAD_cleanup_local(&dgbl->public);
81aef6ba
P
490 EVP_RAND_CTX_free(dgbl->primary);
491 EVP_RAND_CTX_free(dgbl->seed);
44d2482b
P
492 OPENSSL_free(dgbl->rng_name);
493 OPENSSL_free(dgbl->rng_cipher);
494 OPENSSL_free(dgbl->rng_digest);
495 OPENSSL_free(dgbl->rng_propq);
81aef6ba
P
496 OPENSSL_free(dgbl->seed_name);
497 OPENSSL_free(dgbl->seed_propq);
7d615e21
P
498
499 OPENSSL_free(dgbl);
500}
501
b4250010 502static RAND_GLOBAL *rand_get_global(OSSL_LIB_CTX *libctx)
7d615e21 503{
927d0566 504 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_INDEX);
7d615e21
P
505}
506
507static void rand_delete_thread_state(void *arg)
508{
b4250010 509 OSSL_LIB_CTX *ctx = arg;
7d615e21
P
510 RAND_GLOBAL *dgbl = rand_get_global(ctx);
511 EVP_RAND_CTX *rand;
512
513 if (dgbl == NULL)
514 return;
515
516 rand = CRYPTO_THREAD_get_local(&dgbl->public);
517 CRYPTO_THREAD_set_local(&dgbl->public, NULL);
518 EVP_RAND_CTX_free(rand);
519
520 rand = CRYPTO_THREAD_get_local(&dgbl->private);
521 CRYPTO_THREAD_set_local(&dgbl->private, NULL);
522 EVP_RAND_CTX_free(rand);
523}
524
81aef6ba
P
525#ifndef FIPS_MODULE
526static EVP_RAND_CTX *rand_new_seed(OSSL_LIB_CTX *libctx)
527{
528 EVP_RAND *rand;
529 RAND_GLOBAL *dgbl = rand_get_global(libctx);
530 EVP_RAND_CTX *ctx;
531 char *name;
532
09dca557
JJ
533 if (dgbl == NULL)
534 return NULL;
81aef6ba
P
535 name = dgbl->seed_name != NULL ? dgbl->seed_name : "SEED-SRC";
536 rand = EVP_RAND_fetch(libctx, name, dgbl->seed_propq);
537 if (rand == NULL) {
538 ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
539 return NULL;
540 }
541 ctx = EVP_RAND_CTX_new(rand, NULL);
542 EVP_RAND_free(rand);
543 if (ctx == NULL) {
544 ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
545 return NULL;
546 }
d5a936c5 547 if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) {
81aef6ba
P
548 ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
549 EVP_RAND_CTX_free(ctx);
550 return NULL;
551 }
552 return ctx;
553}
554#endif
555
b4250010 556static EVP_RAND_CTX *rand_new_drbg(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent,
7d615e21 557 unsigned int reseed_interval,
505d44c6 558 time_t reseed_time_interval, int use_df)
7d615e21 559{
44d2482b
P
560 EVP_RAND *rand;
561 RAND_GLOBAL *dgbl = rand_get_global(libctx);
7d615e21 562 EVP_RAND_CTX *ctx;
505d44c6
P
563 OSSL_PARAM params[8], *p = params;
564 const OSSL_PARAM *settables;
44d2482b
P
565 char *name, *cipher;
566
09dca557
JJ
567 if (dgbl == NULL)
568 return NULL;
44d2482b
P
569 name = dgbl->rng_name != NULL ? dgbl->rng_name : "CTR-DRBG";
570 rand = EVP_RAND_fetch(libctx, name, dgbl->rng_propq);
7d615e21 571 if (rand == NULL) {
9311d0c4 572 ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
7d615e21
P
573 return NULL;
574 }
575 ctx = EVP_RAND_CTX_new(rand, parent);
576 EVP_RAND_free(rand);
577 if (ctx == NULL) {
9311d0c4 578 ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
7d615e21
P
579 return NULL;
580 }
581
505d44c6
P
582 settables = EVP_RAND_CTX_settable_params(ctx);
583 if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_CIPHER)) {
584 cipher = dgbl->rng_cipher != NULL ? dgbl->rng_cipher : "AES-256-CTR";
585 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
586 cipher, 0);
587 }
588 if (dgbl->rng_digest != NULL
589 && OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_DIGEST))
44d2482b
P
590 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
591 dgbl->rng_digest, 0);
592 if (dgbl->rng_propq != NULL)
593 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_PROPERTIES,
594 dgbl->rng_propq, 0);
505d44c6
P
595 if (OSSL_PARAM_locate_const(settables, OSSL_ALG_PARAM_MAC))
596 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_MAC, "HMAC", 0);
597 if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_USE_DF))
598 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &use_df);
7d615e21
P
599 *p++ = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS,
600 &reseed_interval);
601 *p++ = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL,
602 &reseed_time_interval);
603 *p = OSSL_PARAM_construct_end();
d5a936c5 604 if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, params)) {
9311d0c4 605 ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
4516bf74
P
606 EVP_RAND_CTX_free(ctx);
607 return NULL;
7d615e21
P
608 }
609 return ctx;
610}
611
612/*
613 * Get the primary random generator.
614 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
615 *
616 */
b4250010 617EVP_RAND_CTX *RAND_get0_primary(OSSL_LIB_CTX *ctx)
7d615e21
P
618{
619 RAND_GLOBAL *dgbl = rand_get_global(ctx);
cd4e6a35 620 EVP_RAND_CTX *ret;
7d615e21
P
621
622 if (dgbl == NULL)
623 return NULL;
624
cd4e6a35
MC
625 if (!CRYPTO_THREAD_read_lock(dgbl->lock))
626 return NULL;
627
628 ret = dgbl->primary;
629 CRYPTO_THREAD_unlock(dgbl->lock);
630
631 if (ret != NULL)
632 return ret;
633
634 if (!CRYPTO_THREAD_write_lock(dgbl->lock))
635 return NULL;
636
637 ret = dgbl->primary;
638 if (ret != NULL) {
639 CRYPTO_THREAD_unlock(dgbl->lock);
640 return ret;
641 }
642
81aef6ba 643#ifndef FIPS_MODULE
cd4e6a35
MC
644 if (dgbl->seed == NULL) {
645 ERR_set_mark();
646 dgbl->seed = rand_new_seed(ctx);
647 ERR_pop_to_mark();
648 }
81aef6ba 649#endif
cd4e6a35
MC
650
651 ret = dgbl->primary = rand_new_drbg(ctx, dgbl->seed,
652 PRIMARY_RESEED_INTERVAL,
505d44c6 653 PRIMARY_RESEED_TIME_INTERVAL, 1);
cd4e6a35
MC
654 /*
655 * The primary DRBG may be shared between multiple threads so we must
656 * enable locking.
657 */
658 if (ret != NULL && !EVP_RAND_enable_locking(ret)) {
659 ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_ENABLE_LOCKING);
660 EVP_RAND_CTX_free(ret);
661 ret = dgbl->primary = NULL;
7d615e21 662 }
cd4e6a35
MC
663 CRYPTO_THREAD_unlock(dgbl->lock);
664
665 return ret;
7d615e21
P
666}
667
668/*
669 * Get the public random generator.
670 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
671 */
b4250010 672EVP_RAND_CTX *RAND_get0_public(OSSL_LIB_CTX *ctx)
7d615e21
P
673{
674 RAND_GLOBAL *dgbl = rand_get_global(ctx);
675 EVP_RAND_CTX *rand, *primary;
676
677 if (dgbl == NULL)
678 return NULL;
679
680 rand = CRYPTO_THREAD_get_local(&dgbl->public);
681 if (rand == NULL) {
682 primary = RAND_get0_primary(ctx);
683 if (primary == NULL)
684 return NULL;
685
b4250010 686 ctx = ossl_lib_ctx_get_concrete(ctx);
7d615e21
P
687 /*
688 * If the private is also NULL then this is the first time we've
689 * used this thread.
690 */
691 if (CRYPTO_THREAD_get_local(&dgbl->private) == NULL
692 && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
693 return NULL;
694 rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
505d44c6 695 SECONDARY_RESEED_TIME_INTERVAL, 0);
7d615e21
P
696 CRYPTO_THREAD_set_local(&dgbl->public, rand);
697 }
698 return rand;
699}
700
701/*
702 * Get the private random generator.
703 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
704 */
b4250010 705EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx)
7d615e21
P
706{
707 RAND_GLOBAL *dgbl = rand_get_global(ctx);
708 EVP_RAND_CTX *rand, *primary;
709
710 if (dgbl == NULL)
711 return NULL;
712
713 rand = CRYPTO_THREAD_get_local(&dgbl->private);
714 if (rand == NULL) {
715 primary = RAND_get0_primary(ctx);
716 if (primary == NULL)
717 return NULL;
718
b4250010 719 ctx = ossl_lib_ctx_get_concrete(ctx);
7d615e21
P
720 /*
721 * If the public is also NULL then this is the first time we've
722 * used this thread.
723 */
724 if (CRYPTO_THREAD_get_local(&dgbl->public) == NULL
725 && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
726 return NULL;
727 rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
505d44c6 728 SECONDARY_RESEED_TIME_INTERVAL, 0);
7d615e21
P
729 CRYPTO_THREAD_set_local(&dgbl->private, rand);
730 }
731 return rand;
732}
44d2482b 733
7c8187d4
P
734int RAND_set0_public(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
735{
736 RAND_GLOBAL *dgbl = rand_get_global(ctx);
737 EVP_RAND_CTX *old;
738 int r;
739
740 if (dgbl == NULL)
741 return 0;
742 old = CRYPTO_THREAD_get_local(&dgbl->public);
743 if ((r = CRYPTO_THREAD_set_local(&dgbl->public, rand)) > 0)
744 EVP_RAND_CTX_free(old);
745 return r;
746}
747
748int RAND_set0_private(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
749{
750 RAND_GLOBAL *dgbl = rand_get_global(ctx);
751 EVP_RAND_CTX *old;
752 int r;
753
754 if (dgbl == NULL)
755 return 0;
756 old = CRYPTO_THREAD_get_local(&dgbl->private);
757 if ((r = CRYPTO_THREAD_set_local(&dgbl->private, rand)) > 0)
758 EVP_RAND_CTX_free(old);
759 return r;
760}
761
44d2482b
P
762#ifndef FIPS_MODULE
763static int random_set_string(char **p, const char *s)
764{
786b13fa 765 char *d = NULL;
44d2482b 766
786b13fa
P
767 if (s != NULL) {
768 d = OPENSSL_strdup(s);
e077455e 769 if (d == NULL)
786b13fa 770 return 0;
44d2482b
P
771 }
772 OPENSSL_free(*p);
773 *p = d;
774 return 1;
775}
776
777/*
778 * Load the DRBG definitions from a configuration file.
779 */
780static int random_conf_init(CONF_IMODULE *md, const CONF *cnf)
781{
782 STACK_OF(CONF_VALUE) *elist;
783 CONF_VALUE *cval;
6b750b89 784 RAND_GLOBAL *dgbl = rand_get_global(NCONF_get0_libctx((CONF *)cnf));
44d2482b
P
785 int i, r = 1;
786
787 OSSL_TRACE1(CONF, "Loading random module: section %s\n",
788 CONF_imodule_get_value(md));
789
790 /* Value is a section containing RANDOM configuration */
791 elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
792 if (elist == NULL) {
9311d0c4 793 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_RANDOM_SECTION_ERROR);
44d2482b
P
794 return 0;
795 }
796
09dca557
JJ
797 if (dgbl == NULL)
798 return 0;
799
44d2482b
P
800 for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
801 cval = sk_CONF_VALUE_value(elist, i);
fba140c7 802 if (OPENSSL_strcasecmp(cval->name, "random") == 0) {
44d2482b
P
803 if (!random_set_string(&dgbl->rng_name, cval->value))
804 return 0;
fba140c7 805 } else if (OPENSSL_strcasecmp(cval->name, "cipher") == 0) {
44d2482b
P
806 if (!random_set_string(&dgbl->rng_cipher, cval->value))
807 return 0;
fba140c7 808 } else if (OPENSSL_strcasecmp(cval->name, "digest") == 0) {
44d2482b
P
809 if (!random_set_string(&dgbl->rng_digest, cval->value))
810 return 0;
fba140c7 811 } else if (OPENSSL_strcasecmp(cval->name, "properties") == 0) {
44d2482b
P
812 if (!random_set_string(&dgbl->rng_propq, cval->value))
813 return 0;
fba140c7 814 } else if (OPENSSL_strcasecmp(cval->name, "seed") == 0) {
81aef6ba
P
815 if (!random_set_string(&dgbl->seed_name, cval->value))
816 return 0;
fba140c7 817 } else if (OPENSSL_strcasecmp(cval->name, "seed_properties") == 0) {
81aef6ba
P
818 if (!random_set_string(&dgbl->seed_propq, cval->value))
819 return 0;
44d2482b 820 } else {
a150f8e1
RL
821 ERR_raise_data(ERR_LIB_CRYPTO,
822 CRYPTO_R_UNKNOWN_NAME_IN_RANDOM_SECTION,
823 "name=%s, value=%s", cval->name, cval->value);
44d2482b
P
824 r = 0;
825 }
826 }
827 return r;
828}
829
830
831static void random_conf_deinit(CONF_IMODULE *md)
832{
833 OSSL_TRACE(CONF, "Cleaned up random\n");
834}
835
836void ossl_random_add_conf_module(void)
837{
838 OSSL_TRACE(CONF, "Adding config module 'random'\n");
839 CONF_module_add("random", random_conf_init, random_conf_deinit);
840}
786b13fa
P
841
842int RAND_set_DRBG_type(OSSL_LIB_CTX *ctx, const char *drbg, const char *propq,
843 const char *cipher, const char *digest)
844{
845 RAND_GLOBAL *dgbl = rand_get_global(ctx);
846
847 if (dgbl == NULL)
848 return 0;
849 if (dgbl->primary != NULL) {
850 ERR_raise(ERR_LIB_CRYPTO, RAND_R_ALREADY_INSTANTIATED);
851 return 0;
852 }
853 return random_set_string(&dgbl->rng_name, drbg)
854 && random_set_string(&dgbl->rng_propq, propq)
855 && random_set_string(&dgbl->rng_cipher, cipher)
856 && random_set_string(&dgbl->rng_digest, digest);
857}
858
859int RAND_set_seed_source_type(OSSL_LIB_CTX *ctx, const char *seed,
860 const char *propq)
861{
862 RAND_GLOBAL *dgbl = rand_get_global(ctx);
863
864 if (dgbl == NULL)
865 return 0;
866 if (dgbl->primary != NULL) {
867 ERR_raise(ERR_LIB_CRYPTO, RAND_R_ALREADY_INSTANTIATED);
868 return 0;
869 }
870 return random_set_string(&dgbl->seed_name, seed)
871 && random_set_string(&dgbl->seed_propq, propq);
872}
873
44d2482b 874#endif