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