Coverage Report

Created: 2026-08-12 06:14

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