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