Coverage Report

Created: 2026-09-12 06:55

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