Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/rand/rand_lib.c

raw
1 /*
2 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
12
13 #include <openssl/err.h>
14 #include <openssl/opensslconf.h>
15 #include <openssl/core_names.h>
16 #include <openssl/provider.h>
17 #include "internal/cryptlib.h"
18 #include "internal/provider.h"
19 #include "internal/thread_once.h"
20 #include "internal/threads_common.h"
21 #include "crypto/rand.h"
22 #include "crypto/cryptlib.h"
23 #include "rand_local.h"
24 #include "crypto/context.h"
25 #include "internal/provider.h"
26
27 /* clang-format off */
28 #ifndef OPENSSL_DEFAULT_SEED_SRC
29 #define OPENSSL_DEFAULT_SEED_SRC SEED-SRC
30 #endif
31 /* clang-format on */
32
33 typedef struct rand_global_st {
34 /*
35 * The three shared DRBG instances
36 *
37 * There are three shared DRBG instances: <primary>, <public>, and
38 * <private>. The <public> and <private> DRBGs are secondary ones.
39 * These are used for non-secret (e.g. nonces) and secret
40 * (e.g. private keys) data respectively.
41 */
42 CRYPTO_RWLOCK *lock;
43
44 EVP_RAND_CTX *seed;
45
46 /*
47 * The <primary> DRBG
48 *
49 * Not used directly by the application, only for reseeding the two other
50 * DRBGs. It reseeds itself by pulling either randomness from os entropy
51 * sources or by consuming randomness which was added by RAND_add().
52 *
53 * The <primary> DRBG is a global instance which is accessed concurrently by
54 * all threads. The necessary locking is managed automatically by its child
55 * DRBG instances during reseeding.
56 */
57 EVP_RAND_CTX *primary;
58
59 /*
60 * The provider which we'll use to generate randomness.
61 */
62 #ifndef FIPS_MODULE
63 OSSL_PROVIDER *random_provider;
64 char *random_provider_name;
65 #endif /* !FIPS_MODULE */
66
67 /* Which RNG is being used by default and it's configuration settings */
68 char *rng_name;
69 char *rng_cipher;
70 char *rng_digest;
71 char *rng_propq;
72
73 /* Allow the randomness source to be changed */
74 char *seed_name;
75 char *seed_propq;
76 } RAND_GLOBAL;
77
78 static EVP_RAND_CTX *rand_get0_primary(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl);
79 static EVP_RAND_CTX *rand_get0_public(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl);
80 static EVP_RAND_CTX *rand_get0_private(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl);
81
82 static RAND_GLOBAL *rand_get_global(OSSL_LIB_CTX *libctx)
83 {
84 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_INDEX);
85 }
86
87 #ifndef FIPS_MODULE
88 #include <stdio.h>
89 #include <time.h>
90 #include <limits.h>
91 #include <openssl/conf.h>
92 #include <openssl/trace.h>
93 #include <openssl/engine.h>
94 #include "crypto/rand_pool.h"
95 #include "prov/seeding.h"
96 #include "internal/e_os.h"
97 #include "internal/property.h"
98
99 /*
100 * The default name for the random provider.
101 * This ensures that the FIPS provider will supply libcrypto's random byte
102 * requirements.
103 */
104 static const char random_provider_fips_name[] = "fips";
105
106 static int set_random_provider_name(RAND_GLOBAL *dgbl, const char *name)
107 {
108 if (dgbl->random_provider_name != NULL
109 && OPENSSL_strcasecmp(dgbl->random_provider_name, name) == 0)
110 return 1;
111
112 OPENSSL_free(dgbl->random_provider_name);
113 dgbl->random_provider_name = OPENSSL_strdup(name);
114 return dgbl->random_provider_name != NULL;
115 }
116
117 #ifndef OPENSSL_NO_ENGINE
118 /* non-NULL if default_RAND_meth is ENGINE-provided */
119 static ENGINE *funct_ref;
120 static CRYPTO_RWLOCK *rand_engine_lock;
121 #endif /* !OPENSSL_NO_ENGINE */
122 #ifndef OPENSSL_NO_DEPRECATED_3_0
123 static CRYPTO_RWLOCK *rand_meth_lock;
124 static const RAND_METHOD *default_RAND_meth;
125 #endif /* !OPENSSL_NO_DEPRECATED_3_0 */
126 static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT;
127
128 static int rand_inited = 0;
129
130 DEFINE_RUN_ONCE_STATIC(do_rand_init)
131 {
132 #ifndef OPENSSL_NO_ENGINE
133 rand_engine_lock = CRYPTO_THREAD_lock_new();
134 if (rand_engine_lock == NULL)
135 return 0;
136 #endif /* !OPENSSL_NO_ENGINE */
137
138 #ifndef OPENSSL_NO_DEPRECATED_3_0
139 rand_meth_lock = CRYPTO_THREAD_lock_new();
140 if (rand_meth_lock == NULL)
141 goto err;
142 #endif /* !OPENSSL_NO_DEPRECATED_3_0 */
143
144 if (!ossl_rand_pool_init())
145 goto err;
146
147 rand_inited = 1;
148 return 1;
149
150 err:
151 #ifndef OPENSSL_NO_DEPRECATED_3_0
152 CRYPTO_THREAD_lock_free(rand_meth_lock);
153 rand_meth_lock = NULL;
154 #endif /* !OPENSSL_NO_DEPRECATED_3_0 */
155 #ifndef OPENSSL_NO_ENGINE
156 CRYPTO_THREAD_lock_free(rand_engine_lock);
157 rand_engine_lock = NULL;
158 #endif /* !OPENSSL_NO_ENGINE */
159 return 0;
160 }
161
162 void ossl_rand_cleanup_int(void)
163 {
164 #ifndef OPENSSL_NO_DEPRECATED_3_0
165 const RAND_METHOD *meth = default_RAND_meth;
166
167 if (!rand_inited)
168 return;
169
170 if (meth != NULL && meth->cleanup != NULL)
171 meth->cleanup();
172 RAND_set_rand_method(NULL);
173 #endif /* !OPENSSL_NO_DEPRECATED_3_0 */
174 ossl_rand_pool_cleanup();
175 #ifndef OPENSSL_NO_ENGINE
176 CRYPTO_THREAD_lock_free(rand_engine_lock);
177 rand_engine_lock = NULL;
178 #endif /* !OPENSSL_NO_ENGINE */
179 #ifndef OPENSSL_NO_DEPRECATED_3_0
180 CRYPTO_THREAD_lock_free(rand_meth_lock);
181 rand_meth_lock = NULL;
182 #endif /* !OPENSSL_NO_DEPRECATED_3_0 */
183 ossl_release_default_drbg_ctx();
184 rand_inited = 0;
185 }
186
187 /*
188 * RAND_close_seed_files() ensures that any seed file descriptors are
189 * closed after use. This only applies to libcrypto/default provider,
190 * it does not apply to other providers.
191 */
192 void RAND_keep_random_devices_open(int keep)
193 {
194 if (RUN_ONCE(&rand_init, do_rand_init))
195 ossl_rand_pool_keep_random_devices_open(keep);
196 }
197
198 /*
199 * RAND_poll() reseeds the default RNG using random input
200 *
201 * The random input is obtained from polling various entropy
202 * sources which depend on the operating system and are
203 * configurable via the --with-rand-seed configure option.
204 */
205 int RAND_poll(void)
206 {
207 static const char salt[] = "polling";
208
209 #ifndef OPENSSL_NO_DEPRECATED_3_0
210 const RAND_METHOD *meth = RAND_get_rand_method();
211 int ret = meth == RAND_OpenSSL();
212
213 if (meth == NULL)
214 return 0;
215
216 if (!ret) {
217 /* fill random pool and seed the current legacy RNG */
218 RAND_POOL *pool = ossl_rand_pool_new(RAND_DRBG_STRENGTH, 1,
219 (RAND_DRBG_STRENGTH + 7) / 8,
220 RAND_POOL_MAX_LENGTH);
221
222 if (pool == NULL)
223 return 0;
224
225 if (ossl_pool_acquire_entropy(pool) == 0)
226 goto err;
227
228 if (meth->add == NULL
229 || meth->add(ossl_rand_pool_buffer(pool),
230 (int)ossl_rand_pool_length(pool),
231 (ossl_rand_pool_entropy(pool) / 8.0))
232 == 0)
233 goto err;
234
235 ret = 1;
236 err:
237 ossl_rand_pool_free(pool);
238 return ret;
239 }
240 #endif /* !OPENSSL_NO_DEPRECATED_3_0 */
241
242 RAND_seed(salt, sizeof(salt));
243 return 1;
244 }
245
246 #ifndef OPENSSL_NO_DEPRECATED_3_0
247 static int rand_set_rand_method_internal(const RAND_METHOD *meth,
248 ossl_unused ENGINE *e)
249 {
250 if (!RUN_ONCE(&rand_init, do_rand_init))
251 return 0;
252
253 if (!CRYPTO_THREAD_write_lock(rand_meth_lock))
254 return 0;
255 #ifndef OPENSSL_NO_ENGINE
256 ENGINE_finish(funct_ref);
257 funct_ref = e;
258 #endif
259 default_RAND_meth = meth;
260 CRYPTO_THREAD_unlock(rand_meth_lock);
261 return 1;
262 }
263
264 int RAND_set_rand_method(const RAND_METHOD *meth)
265 {
266 return rand_set_rand_method_internal(meth, NULL);
267 }
268
269 const RAND_METHOD *RAND_get_rand_method(void)
270 {
271 const RAND_METHOD *tmp_meth = NULL;
272
273 if (!RUN_ONCE(&rand_init, do_rand_init))
274 return NULL;
275
276 if (rand_meth_lock == NULL)
277 return NULL;
278
279 if (!CRYPTO_THREAD_read_lock(rand_meth_lock))
280 return NULL;
281 tmp_meth = default_RAND_meth;
282 CRYPTO_THREAD_unlock(rand_meth_lock);
283 if (tmp_meth != NULL)
284 return tmp_meth;
285
286 if (!CRYPTO_THREAD_write_lock(rand_meth_lock))
287 return NULL;
288 if (default_RAND_meth == NULL) {
289 #ifndef OPENSSL_NO_ENGINE
290 ENGINE *e;
291
292 /* If we have an engine that can do RAND, use it. */
293 if ((e = ENGINE_get_default_RAND()) != NULL
294 && (tmp_meth = ENGINE_get_RAND(e)) != NULL) {
295 funct_ref = e;
296 default_RAND_meth = tmp_meth;
297 } else {
298 ENGINE_finish(e);
299 default_RAND_meth = &ossl_rand_meth;
300 }
301 #else
302 default_RAND_meth = &ossl_rand_meth;
303 #endif
304 }
305 tmp_meth = default_RAND_meth;
306 CRYPTO_THREAD_unlock(rand_meth_lock);
307 return tmp_meth;
308 }
309
310 #if !defined(OPENSSL_NO_ENGINE)
311 int RAND_set_rand_engine(ENGINE *engine)
312 {
313 const RAND_METHOD *tmp_meth = NULL;
314
315 if (!RUN_ONCE(&rand_init, do_rand_init))
316 return 0;
317
318 if (engine != NULL) {
319 if (!ENGINE_init(engine))
320 return 0;
321 tmp_meth = ENGINE_get_RAND(engine);
322 if (tmp_meth == NULL) {
323 ENGINE_finish(engine);
324 return 0;
325 }
326 }
327 if (!CRYPTO_THREAD_write_lock(rand_engine_lock)) {
328 ENGINE_finish(engine);
329 return 0;
330 }
331
332 /* This function releases any prior ENGINE so call it first */
333 rand_set_rand_method_internal(tmp_meth, engine);
334 CRYPTO_THREAD_unlock(rand_engine_lock);
335 return 1;
336 }
337 #endif
338 #endif /* OPENSSL_NO_DEPRECATED_3_0 */
339
340 void RAND_seed(const void *buf, int num)
341 {
342 EVP_RAND_CTX *drbg;
343 #ifndef OPENSSL_NO_DEPRECATED_3_0
344 const RAND_METHOD *meth = RAND_get_rand_method();
345
346 if (meth != NULL && meth->seed != NULL) {
347 meth->seed(buf, num);
348 return;
349 }
350 #endif
351
352 drbg = RAND_get0_primary(NULL);
353 if (drbg != NULL && num > 0)
354 EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num);
355 }
356
357 void RAND_add(const void *buf, int num, double randomness)
358 {
359 EVP_RAND_CTX *drbg;
360 #ifndef OPENSSL_NO_DEPRECATED_3_0
361 const RAND_METHOD *meth = RAND_get_rand_method();
362
363 if (meth != NULL && meth->add != NULL) {
364 meth->add(buf, num, randomness);
365 return;
366 }
367 #endif
368 drbg = RAND_get0_primary(NULL);
369 if (drbg != NULL && num > 0)
370 #ifdef OPENSSL_RAND_SEED_NONE
371 /* Without an entropy source, we have to rely on the user */
372 EVP_RAND_reseed(drbg, 0, buf, num, NULL, 0);
373 #else
374 /* With an entropy source, we downgrade this to additional input */
375 EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num);
376 #endif
377 }
378
379 #if !defined(OPENSSL_NO_DEPRECATED_1_1_0)
380 int RAND_pseudo_bytes(unsigned char *buf, int num)
381 {
382 const RAND_METHOD *meth = RAND_get_rand_method();
383
384 if (meth != NULL && meth->pseudorand != NULL)
385 return meth->pseudorand(buf, num);
386 ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
387 return -1;
388 }
389 #endif
390
391 int RAND_status(void)
392 {
393 EVP_RAND_CTX *rand;
394 #ifndef OPENSSL_NO_DEPRECATED_3_0
395 const RAND_METHOD *meth = RAND_get_rand_method();
396
397 if (meth != NULL && meth != RAND_OpenSSL())
398 return meth->status != NULL ? meth->status() : 0;
399 #endif
400
401 if ((rand = RAND_get0_primary(NULL)) == NULL)
402 return 0;
403 return EVP_RAND_get_state(rand) == EVP_RAND_STATE_READY;
404 }
405 #else /* !FIPS_MODULE */
406
407 #ifndef OPENSSL_NO_DEPRECATED_3_0
408 const RAND_METHOD *RAND_get_rand_method(void)
409 {
410 return NULL;
411 }
412 #endif
413 #endif /* !FIPS_MODULE */
414
415 /*
416 * This function is not part of RAND_METHOD, so if we're not using
417 * the default method, then just call RAND_bytes(). Otherwise make
418 * sure we're instantiated and use the private DRBG.
419 */
420 int RAND_priv_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
421 unsigned int strength)
422 {
423 RAND_GLOBAL *dgbl;
424 EVP_RAND_CTX *rand;
425 #if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)
426 const RAND_METHOD *meth = RAND_get_rand_method();
427
428 if (meth != NULL && meth != RAND_OpenSSL()) {
429 if (num > INT_MAX) {
430 ERR_raise(ERR_LIB_RAND, RAND_R_ARGUMENT_OUT_OF_RANGE);
431 return -1;
432 }
433 if (meth->bytes != NULL)
434 return meth->bytes(buf, (int)num);
435 ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
436 return -1;
437 }
438 #endif
439
440 dgbl = rand_get_global(ctx);
441 if (dgbl == NULL)
442 return 0;
443 #ifndef FIPS_MODULE
444 if (dgbl->random_provider != NULL)
445 return ossl_provider_random_bytes(dgbl->random_provider,
446 OSSL_PROV_RANDOM_PRIVATE,
447 buf, num, strength);
448 #endif /* !FIPS_MODULE */
449 rand = rand_get0_private(ctx, dgbl);
450 if (rand != NULL)
451 return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);
452
453 return 0;
454 }
455
456 int RAND_priv_bytes(unsigned char *buf, int num)
457 {
458 if (num < 0)
459 return 0;
460 return RAND_priv_bytes_ex(NULL, buf, (size_t)num, 0);
461 }
462
463 int RAND_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
464 unsigned int strength)
465 {
466 RAND_GLOBAL *dgbl;
467 EVP_RAND_CTX *rand;
468 #if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)
469 const RAND_METHOD *meth = RAND_get_rand_method();
470
471 if (meth != NULL && meth != RAND_OpenSSL()) {
472 if (num > INT_MAX) {
473 ERR_raise(ERR_LIB_RAND, RAND_R_ARGUMENT_OUT_OF_RANGE);
474 return -1;
475 }
476 if (meth->bytes != NULL)
477 return meth->bytes(buf, (int)num);
478 ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
479 return -1;
480 }
481 #endif
482
483 dgbl = rand_get_global(ctx);
484 if (dgbl == NULL)
485 return 0;
486 #ifndef FIPS_MODULE
487 if (dgbl->random_provider != NULL)
488 return ossl_provider_random_bytes(dgbl->random_provider,
489 OSSL_PROV_RANDOM_PUBLIC,
490 buf, num, strength);
491 #endif /* !FIPS_MODULE */
492
493 rand = rand_get0_public(ctx, dgbl);
494 if (rand != NULL)
495 return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);
496
497 return 0;
498 }
499
500 int RAND_bytes(unsigned char *buf, int num)
501 {
502 if (num < 0)
503 return 0;
504 return RAND_bytes_ex(NULL, buf, (size_t)num, 0);
505 }
506
507 /*
508 * Initialize the OSSL_LIB_CTX global DRBGs on first use.
509 * Returns the allocated global data on success or NULL on failure.
510 */
511 void *ossl_rand_ctx_new(OSSL_LIB_CTX *libctx)
512 {
513 RAND_GLOBAL *dgbl = OPENSSL_zalloc(sizeof(*dgbl));
514
515 if (dgbl == NULL)
516 return NULL;
517
518 #ifndef FIPS_MODULE
519 /*
520 * We need to ensure that base libcrypto thread handling has been
521 * initialised.
522 */
523 OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL);
524
525 /* Prepopulate the random provider name */
526 dgbl->random_provider_name = OPENSSL_strdup(random_provider_fips_name);
527 if (dgbl->random_provider_name == NULL)
528 goto err0;
529 #endif
530
531 dgbl->lock = CRYPTO_THREAD_lock_new();
532 if (dgbl->lock == NULL)
533 goto err1;
534
535 return dgbl;
536
537 err1:
538 CRYPTO_THREAD_lock_free(dgbl->lock);
539 #ifndef FIPS_MODULE
540 err0:
541 OPENSSL_free(dgbl->random_provider_name);
542 #endif
543 OPENSSL_free(dgbl);
544 return NULL;
545 }
546
547 void ossl_rand_ctx_free(void *vdgbl)
548 {
549 RAND_GLOBAL *dgbl = vdgbl;
550
551 if (dgbl == NULL)
552 return;
553
554 CRYPTO_THREAD_lock_free(dgbl->lock);
555 EVP_RAND_CTX_free(dgbl->primary);
556 EVP_RAND_CTX_free(dgbl->seed);
557 #ifndef FIPS_MODULE
558 OPENSSL_free(dgbl->random_provider_name);
559 #endif /* !FIPS_MODULE */
560 OPENSSL_free(dgbl->rng_name);
561 OPENSSL_free(dgbl->rng_cipher);
562 OPENSSL_free(dgbl->rng_digest);
563 OPENSSL_free(dgbl->rng_propq);
564 OPENSSL_free(dgbl->seed_name);
565 OPENSSL_free(dgbl->seed_propq);
566
567 OPENSSL_free(dgbl);
568 }
569
570 static void rand_delete_thread_state(void *arg)
571 {
572 OSSL_LIB_CTX *ctx = arg;
573 RAND_GLOBAL *dgbl = rand_get_global(ctx);
574 EVP_RAND_CTX *rand;
575
576 if (dgbl == NULL)
577 return;
578
579 rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
580 CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, NULL);
581 EVP_RAND_CTX_free(rand);
582
583 rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
584 CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, NULL);
585 EVP_RAND_CTX_free(rand);
586 }
587
588 #if !defined(FIPS_MODULE) || !defined(OPENSSL_NO_FIPS_JITTER)
589 static EVP_RAND_CTX *rand_new_seed(OSSL_LIB_CTX *libctx)
590 {
591 EVP_RAND *rand;
592 const char *propq;
593 char *name;
594 EVP_RAND_CTX *ctx = NULL;
595 #ifdef OPENSSL_NO_FIPS_JITTER
596 RAND_GLOBAL *dgbl = rand_get_global(libctx);
597
598 if (dgbl == NULL)
599 return NULL;
600 propq = dgbl->seed_propq;
601 name = dgbl->seed_name != NULL ? dgbl->seed_name
602 : OPENSSL_MSTR(OPENSSL_DEFAULT_SEED_SRC);
603 #else /* !OPENSSL_NO_FIPS_JITTER */
604 name = "JITTER";
605 propq = "";
606 #endif /* OPENSSL_NO_FIPS_JITTER */
607
608 rand = EVP_RAND_fetch(libctx, name, propq);
609 if (rand == NULL) {
610 ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
611 goto err;
612 }
613 ctx = EVP_RAND_CTX_new(rand, NULL);
614 EVP_RAND_free(rand);
615 if (ctx == NULL) {
616 ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
617 goto err;
618 }
619 if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) {
620 ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
621 goto err;
622 }
623 return ctx;
624 err:
625 EVP_RAND_CTX_free(ctx);
626 return NULL;
627 }
628 #endif /* !FIPS_MODULE || !OPENSSL_NO_FIPS_JITTER */
629
630 #ifndef FIPS_MODULE
631 EVP_RAND_CTX *ossl_rand_get0_seed_noncreating(OSSL_LIB_CTX *ctx)
632 {
633 RAND_GLOBAL *dgbl = rand_get_global(ctx);
634 EVP_RAND_CTX *ret;
635
636 if (dgbl == NULL)
637 return NULL;
638
639 if (!CRYPTO_THREAD_read_lock(dgbl->lock))
640 return NULL;
641 ret = dgbl->seed;
642 CRYPTO_THREAD_unlock(dgbl->lock);
643 return ret;
644 }
645 #endif /* !FIPS_MODULE */
646
647 static EVP_RAND_CTX *rand_new_drbg(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent,
648 unsigned int reseed_interval,
649 time_t reseed_time_interval)
650 {
651 EVP_RAND *rand;
652 RAND_GLOBAL *dgbl = rand_get_global(libctx);
653 EVP_RAND_CTX *ctx;
654 OSSL_PARAM params[9], *p = params;
655 const OSSL_PARAM *settables;
656 const char *prov_name;
657 char *name, *cipher;
658 int use_df = 1;
659
660 if (dgbl == NULL)
661 return NULL;
662 name = dgbl->rng_name != NULL ? dgbl->rng_name : "CTR-DRBG";
663 rand = EVP_RAND_fetch(libctx, name, dgbl->rng_propq);
664 if (rand == NULL) {
665 ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
666 return NULL;
667 }
668 prov_name = ossl_provider_name(EVP_RAND_get0_provider(rand));
669 ctx = EVP_RAND_CTX_new(rand, parent);
670 EVP_RAND_free(rand);
671 if (ctx == NULL) {
672 ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
673 return NULL;
674 }
675
676 settables = EVP_RAND_CTX_settable_params(ctx);
677 if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_CIPHER)) {
678 cipher = dgbl->rng_cipher != NULL ? dgbl->rng_cipher : "AES-256-CTR";
679 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
680 cipher, 0);
681 }
682 if (dgbl->rng_digest != NULL
683 && OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_DIGEST))
684 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
685 dgbl->rng_digest, 0);
686 if (prov_name != NULL)
687 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_CORE_PROV_NAME,
688 (char *)prov_name, 0);
689 if (dgbl->rng_propq != NULL)
690 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_PROPERTIES,
691 dgbl->rng_propq, 0);
692 if (OSSL_PARAM_locate_const(settables, OSSL_ALG_PARAM_MAC))
693 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_MAC, "HMAC", 0);
694 if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_USE_DF))
695 *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &use_df);
696 *p++ = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS,
697 &reseed_interval);
698 *p++ = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL,
699 &reseed_time_interval);
700 *p = OSSL_PARAM_construct_end();
701 if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, params)) {
702 ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
703 EVP_RAND_CTX_free(ctx);
704 return NULL;
705 }
706 return ctx;
707 }
708
709 #if defined(FIPS_MODULE)
710 static EVP_RAND_CTX *rand_new_crngt(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent)
711 {
712 EVP_RAND *rand;
713 EVP_RAND_CTX *ctx;
714
715 rand = EVP_RAND_fetch(libctx, "CRNG-TEST", "-fips");
716 if (rand == NULL) {
717 ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
718 return NULL;
719 }
720 ctx = EVP_RAND_CTX_new(rand, parent);
721 EVP_RAND_free(rand);
722 if (ctx == NULL) {
723 ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
724 return NULL;
725 }
726
727 if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) {
728 ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
729 EVP_RAND_CTX_free(ctx);
730 return NULL;
731 }
732 return ctx;
733 }
734 #endif /* FIPS_MODULE */
735
736 /*
737 * Get the primary random generator.
738 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
739 *
740 */
741 static EVP_RAND_CTX *rand_get0_primary(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
742 {
743 EVP_RAND_CTX *ret, *seed, *newseed = NULL, *primary;
744
745 if (dgbl == NULL)
746 return NULL;
747
748 if (!CRYPTO_THREAD_read_lock(dgbl->lock))
749 return NULL;
750
751 ret = dgbl->primary;
752 seed = dgbl->seed;
753 CRYPTO_THREAD_unlock(dgbl->lock);
754
755 if (ret != NULL)
756 return ret;
757
758 #if !defined(FIPS_MODULE) || !defined(OPENSSL_NO_FIPS_JITTER)
759 /* Create a seed source for libcrypto or jitter enabled FIPS provider */
760 if (seed == NULL) {
761 ERR_set_mark();
762 seed = newseed = rand_new_seed(ctx);
763 ERR_pop_to_mark();
764 }
765 #endif /* !FIPS_MODULE || !OPENSSL_NO_FIPS_JITTER */
766
767 #if defined(FIPS_MODULE)
768 /* The FIPS provider has entropy health tests instead of the primary */
769 ret = rand_new_crngt(ctx, seed);
770 #else /* FIPS_MODULE */
771 ret = rand_new_drbg(ctx, seed, PRIMARY_RESEED_INTERVAL,
772 PRIMARY_RESEED_TIME_INTERVAL);
773 #endif /* FIPS_MODULE */
774
775 /*
776 * The primary DRBG may be shared between multiple threads so we must
777 * enable locking.
778 */
779 if (ret == NULL || !EVP_RAND_enable_locking(ret)) {
780 if (ret != NULL) {
781 ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_ENABLE_LOCKING);
782 EVP_RAND_CTX_free(ret);
783 }
784 if (newseed == NULL)
785 return NULL;
786 /* else carry on and store seed */
787 ret = NULL;
788 }
789
790 if (!CRYPTO_THREAD_write_lock(dgbl->lock))
791 return NULL;
792
793 primary = dgbl->primary;
794 if (primary != NULL) {
795 CRYPTO_THREAD_unlock(dgbl->lock);
796 EVP_RAND_CTX_free(ret);
797 EVP_RAND_CTX_free(newseed);
798 return primary;
799 }
800 if (newseed != NULL)
801 dgbl->seed = newseed;
802 dgbl->primary = ret;
803 CRYPTO_THREAD_unlock(dgbl->lock);
804
805 return ret;
806 }
807
808 /*
809 * Get the primary random generator.
810 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
811 *
812 */
813 EVP_RAND_CTX *RAND_get0_primary(OSSL_LIB_CTX *ctx)
814 {
815 RAND_GLOBAL *dgbl = rand_get_global(ctx);
816
817 return dgbl == NULL ? NULL : rand_get0_primary(ctx, dgbl);
818 }
819
820 static EVP_RAND_CTX *rand_get0_public(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
821 {
822 EVP_RAND_CTX *rand, *primary;
823 OSSL_LIB_CTX *origctx = ctx;
824
825 ctx = ossl_lib_ctx_get_concrete(ctx);
826
827 if (ctx == NULL)
828 return NULL;
829
830 if (dgbl == NULL)
831 return NULL;
832
833 rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
834 if (rand == NULL) {
835 primary = rand_get0_primary(origctx, dgbl);
836 if (primary == NULL)
837 return NULL;
838
839 /*
840 * If the private is also NULL then this is the first time we've
841 * used this thread.
842 */
843 if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx) == NULL
844 && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
845 return NULL;
846 rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
847 SECONDARY_RESEED_TIME_INTERVAL);
848 if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand)) {
849 EVP_RAND_CTX_free(rand);
850 rand = NULL;
851 }
852 }
853 return rand;
854 }
855
856 /*
857 * Get the public random generator.
858 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
859 */
860 EVP_RAND_CTX *RAND_get0_public(OSSL_LIB_CTX *ctx)
861 {
862 RAND_GLOBAL *dgbl = rand_get_global(ctx);
863
864 return dgbl == NULL ? NULL : rand_get0_public(ctx, dgbl);
865 }
866
867 static EVP_RAND_CTX *rand_get0_private(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
868 {
869 EVP_RAND_CTX *rand, *primary;
870 OSSL_LIB_CTX *origctx = ctx;
871
872 ctx = ossl_lib_ctx_get_concrete(ctx);
873 if (ctx == NULL)
874 return NULL;
875
876 rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
877 if (rand == NULL) {
878 primary = rand_get0_primary(origctx, dgbl);
879 if (primary == NULL)
880 return NULL;
881
882 /*
883 * If the public is also NULL then this is the first time we've
884 * used this thread.
885 */
886 if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx) == NULL
887 && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
888 return NULL;
889 rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
890 SECONDARY_RESEED_TIME_INTERVAL);
891 if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand)) {
892 EVP_RAND_CTX_free(rand);
893 rand = NULL;
894 }
895 }
896 return rand;
897 }
898
899 /*
900 * Get the private random generator.
901 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
902 */
903 EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx)
904 {
905 RAND_GLOBAL *dgbl = rand_get_global(ctx);
906
907 return dgbl == NULL ? NULL : rand_get0_private(ctx, dgbl);
908 }
909
910 #ifdef FIPS_MODULE
911 EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx)
912 {
913 RAND_GLOBAL *dgbl = rand_get_global(ctx);
914
915 if (dgbl == NULL)
916 return NULL;
917
918 return CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
919 }
920 #endif
921
922 int RAND_set0_public(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
923 {
924 RAND_GLOBAL *dgbl = rand_get_global(ctx);
925 EVP_RAND_CTX *old;
926 int r;
927
928 if (dgbl == NULL)
929 return 0;
930 old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
931 if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand)) > 0)
932 EVP_RAND_CTX_free(old);
933 return r;
934 }
935
936 int RAND_set0_private(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
937 {
938 RAND_GLOBAL *dgbl = rand_get_global(ctx);
939 EVP_RAND_CTX *old;
940 int r;
941
942 if (dgbl == NULL)
943 return 0;
944 old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
945 if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand)) > 0)
946 EVP_RAND_CTX_free(old);
947 return r;
948 }
949
950 #ifndef FIPS_MODULE
951 static int random_set_string(char **p, const char *s)
952 {
953 char *d = NULL;
954
955 if (s != NULL) {
956 d = OPENSSL_strdup(s);
957 if (d == NULL)
958 return 0;
959 }
960 OPENSSL_free(*p);
961 *p = d;
962 return 1;
963 }
964
965 /*
966 * Load the DRBG definitions from a configuration file.
967 */
968 static int random_conf_init(CONF_IMODULE *md, const CONF *cnf)
969 {
970 STACK_OF(CONF_VALUE) *elist;
971 CONF_VALUE *cval;
972 OSSL_LIB_CTX *libctx = NCONF_get0_libctx((CONF *)cnf);
973 RAND_GLOBAL *dgbl = rand_get_global(libctx);
974 int i, r = 1;
975
976 OSSL_TRACE1(CONF, "Loading random module: section %s\n",
977 CONF_imodule_get_value(md));
978
979 /* Value is a section containing RANDOM configuration */
980 elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
981 if (elist == NULL) {
982 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_RANDOM_SECTION_ERROR);
983 return 0;
984 }
985
986 if (dgbl == NULL)
987 return 0;
988
989 for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
990 cval = sk_CONF_VALUE_value(elist, i);
991 if (OPENSSL_strcasecmp(cval->name, "random") == 0) {
992 if (!random_set_string(&dgbl->rng_name, cval->value))
993 return 0;
994 } else if (OPENSSL_strcasecmp(cval->name, "cipher") == 0) {
995 if (!random_set_string(&dgbl->rng_cipher, cval->value))
996 return 0;
997 } else if (OPENSSL_strcasecmp(cval->name, "digest") == 0) {
998 if (!random_set_string(&dgbl->rng_digest, cval->value))
999 return 0;
1000 } else if (OPENSSL_strcasecmp(cval->name, "properties") == 0) {
1001 if (!random_set_string(&dgbl->rng_propq, cval->value))
1002 return 0;
1003 } else if (OPENSSL_strcasecmp(cval->name, "seed") == 0) {
1004 if (!random_set_string(&dgbl->seed_name, cval->value))
1005 return 0;
1006 } else if (OPENSSL_strcasecmp(cval->name, "seed_properties") == 0) {
1007 if (!random_set_string(&dgbl->seed_propq, cval->value))
1008 return 0;
1009 } else if (OPENSSL_strcasecmp(cval->name, "random_provider") == 0) {
1010 #ifndef FIPS_MODULE
1011 OSSL_PROVIDER *prov = ossl_provider_find(libctx, cval->value, 0);
1012
1013 if (prov != NULL) {
1014 if (!RAND_set1_random_provider(libctx, prov)) {
1015 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
1016 OSSL_PROVIDER_unload(prov);
1017 return 0;
1018 }
1019 /*
1020 * We need to release the reference from ossl_provider_find because
1021 * we don't want to keep a reference counted handle to the provider.
1022 *
1023 * The provider unload code checks for the random provider and,
1024 * if present, our reference will be NULLed when it is fully freed.
1025 * The provider load code, conversely, checks the provider name
1026 * and re-hooks our reference if required. This means that a load,
1027 * hook random provider, use, unload, reload, reuse sequence will
1028 * work as expected.
1029 */
1030 OSSL_PROVIDER_unload(prov);
1031 } else if (!set_random_provider_name(dgbl, cval->value))
1032 return 0;
1033 #endif
1034 } else {
1035 ERR_raise_data(ERR_LIB_CRYPTO,
1036 CRYPTO_R_UNKNOWN_NAME_IN_RANDOM_SECTION,
1037 "name=%s, value=%s", cval->name, cval->value);
1038 r = 0;
1039 }
1040 }
1041 return r;
1042 }
1043
1044 static void random_conf_deinit(CONF_IMODULE *md)
1045 {
1046 OSSL_TRACE(CONF, "Cleaned up random\n");
1047 }
1048
1049 void ossl_random_add_conf_module(void)
1050 {
1051 OSSL_TRACE(CONF, "Adding config module 'random'\n");
1052 CONF_module_add("random", random_conf_init, random_conf_deinit);
1053 }
1054
1055 int RAND_set_DRBG_type(OSSL_LIB_CTX *ctx, const char *drbg, const char *propq,
1056 const char *cipher, const char *digest)
1057 {
1058 RAND_GLOBAL *dgbl = rand_get_global(ctx);
1059
1060 if (dgbl == NULL)
1061 return 0;
1062 if (dgbl->primary != NULL) {
1063 ERR_raise(ERR_LIB_RAND, RAND_R_ALREADY_INSTANTIATED);
1064 return 0;
1065 }
1066 return random_set_string(&dgbl->rng_name, drbg)
1067 && random_set_string(&dgbl->rng_propq, propq)
1068 && random_set_string(&dgbl->rng_cipher, cipher)
1069 && random_set_string(&dgbl->rng_digest, digest);
1070 }
1071
1072 int RAND_set_seed_source_type(OSSL_LIB_CTX *ctx, const char *seed,
1073 const char *propq)
1074 {
1075 RAND_GLOBAL *dgbl = rand_get_global(ctx);
1076
1077 if (dgbl == NULL)
1078 return 0;
1079 if (dgbl->seed != NULL) {
1080 ERR_raise(ERR_LIB_RAND, RAND_R_ALREADY_INSTANTIATED);
1081 return 0;
1082 }
1083 return random_set_string(&dgbl->seed_name, seed)
1084 && random_set_string(&dgbl->seed_propq, propq);
1085 }
1086
1087 int RAND_set1_random_provider(OSSL_LIB_CTX *ctx, OSSL_PROVIDER *prov)
1088 {
1089 RAND_GLOBAL *dgbl = rand_get_global(ctx);
1090
1091 if (dgbl == NULL)
1092 return 0;
1093
1094 if (prov == NULL) {
1095 OPENSSL_free(dgbl->random_provider_name);
1096 dgbl->random_provider_name = NULL;
1097 dgbl->random_provider = NULL;
1098 return 1;
1099 }
1100
1101 if (dgbl->random_provider == prov)
1102 return 1;
1103
1104 if (!set_random_provider_name(dgbl, OSSL_PROVIDER_get0_name(prov)))
1105 return 0;
1106
1107 dgbl->random_provider = prov;
1108 return 1;
1109 }
1110
1111 /*
1112 * When a new provider is loaded, we need to check to see if it is the
1113 * designated randomness provider and register it if it is.
1114 */
1115 int ossl_rand_check_random_provider_on_load(OSSL_LIB_CTX *ctx,
1116 OSSL_PROVIDER *prov)
1117 {
1118 RAND_GLOBAL *dgbl = rand_get_global(ctx);
1119
1120 if (dgbl == NULL)
1121 return 0;
1122
1123 /* No random provider name specified, or one is installed already */
1124 if (dgbl->random_provider_name == NULL || dgbl->random_provider != NULL)
1125 return 1;
1126
1127 /* Does this provider match the name we're using? */
1128 if (strcmp(dgbl->random_provider_name, OSSL_PROVIDER_get0_name(prov)) != 0)
1129 return 1;
1130
1131 dgbl->random_provider = prov;
1132 return 1;
1133 }
1134
1135 /*
1136 * When a provider is being unloaded, if it is the randomness provider,
1137 * we need to deregister it.
1138 */
1139 int ossl_rand_check_random_provider_on_unload(OSSL_LIB_CTX *ctx,
1140 OSSL_PROVIDER *prov)
1141 {
1142 RAND_GLOBAL *dgbl = rand_get_global(ctx);
1143
1144 if (dgbl == NULL)
1145 return 0;
1146
1147 if (dgbl->random_provider == prov)
1148 dgbl->random_provider = NULL;
1149 return 1;
1150 }
1151
1152 #endif /* !FIPS_MODULE */