Coverage Report

Created: 2026-09-12 06:55

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