Coverage Report

Created: 2025-06-22 06:56

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