Coverage Report

Created: 2026-08-18 07:24

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
128
{
79
128
    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_INDEX);
80
128
}
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
16
{
121
16
#ifndef OPENSSL_NO_DEPRECATED_3_0
122
16
    rand_meth_lock = CRYPTO_THREAD_lock_new();
123
16
    if (rand_meth_lock == NULL)
124
0
        goto err;
125
16
#endif /* !OPENSSL_NO_DEPRECATED_3_0 */
126
127
16
    if (!ossl_rand_pool_init())
128
0
        goto err;
129
130
16
    rand_inited = 1;
131
16
    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
16
}
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
16
{
182
16
    static const char salt[] = "polling";
183
184
16
#ifndef OPENSSL_NO_DEPRECATED_3_0
185
16
    const RAND_METHOD *meth = RAND_get_rand_method();
186
16
    int ret = meth == RAND_OpenSSL();
187
188
16
    if (meth == NULL)
189
0
        return 0;
190
191
16
    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
16
#endif /* !OPENSSL_NO_DEPRECATED_3_0 */
216
217
16
    RAND_seed(salt, sizeof(salt));
218
16
    return 1;
219
16
}
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
128
{
242
128
    const RAND_METHOD *tmp_meth = NULL;
243
128
    int lock_failed;
244
245
128
    if (!RUN_ONCE(&rand_init, do_rand_init))
246
0
        goto end;
247
248
128
    if (CRYPTO_atomic_load_ptr((void **)&default_RAND_meth, (void **)&tmp_meth,
249
128
            rand_meth_lock)) {
250
128
        if (tmp_meth != NULL)
251
112
            return tmp_meth;
252
128
    } 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
16
    if (CRYPTO_atomic_cmp_exch_ptr((void **)&default_RAND_meth, (void **)&tmp_meth,
265
16
            (void *)&ossl_rand_meth, rand_meth_lock, &lock_failed)) {
266
16
        tmp_meth = &ossl_rand_meth;
267
16
    } else {
268
0
        if (lock_failed == 1)
269
0
            return NULL;
270
0
    }
271
16
end:
272
16
    return tmp_meth;
273
16
}
274
#endif /* OPENSSL_NO_DEPRECATED_3_0 */
275
276
void RAND_seed(const void *buf, int num)
277
32
{
278
32
    EVP_RAND_CTX *drbg;
279
32
#ifndef OPENSSL_NO_DEPRECATED_3_0
280
32
    const RAND_METHOD *meth = RAND_get_rand_method();
281
282
32
    if (meth != NULL && meth->seed != NULL) {
283
32
        meth->seed(buf, num);
284
32
        return;
285
32
    }
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
16
{
329
16
    EVP_RAND_CTX *rand;
330
16
#ifndef OPENSSL_NO_DEPRECATED_3_0
331
16
    const RAND_METHOD *meth = RAND_get_rand_method();
332
333
16
    if (meth != NULL && meth != RAND_OpenSSL())
334
0
        return meth->status != NULL ? meth->status() : 0;
335
16
#endif
336
337
16
    if ((rand = RAND_get0_primary(NULL)) == NULL)
338
0
        return 0;
339
16
    return EVP_RAND_get_state(rand) == EVP_RAND_STATE_READY;
340
16
}
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
32
{
402
32
    RAND_GLOBAL *dgbl;
403
32
    EVP_RAND_CTX *rand;
404
32
#if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)
405
32
    const RAND_METHOD *meth = RAND_get_rand_method();
406
407
32
    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
32
#endif
418
419
32
    dgbl = rand_get_global(ctx);
420
32
    if (dgbl == NULL)
421
0
        return 0;
422
32
#ifndef FIPS_MODULE
423
32
    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
32
#endif /* !FIPS_MODULE */
428
429
32
    rand = rand_get0_public(ctx, dgbl);
430
32
    if (rand != NULL)
431
32
        return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);
432
433
0
    return 0;
434
32
}
435
436
int RAND_bytes(unsigned char *buf, int num)
437
32
{
438
32
    if (num < 0)
439
0
        return 0;
440
32
    return RAND_bytes_ex(NULL, buf, (size_t)num, 0);
441
32
}
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
16
{
449
16
    RAND_GLOBAL *dgbl = OPENSSL_zalloc(sizeof(*dgbl));
450
451
16
    if (dgbl == NULL)
452
0
        return NULL;
453
454
16
#ifndef FIPS_MODULE
455
    /*
456
     * We need to ensure that base libcrypto thread handling has been
457
     * initialised.
458
     */
459
16
    OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL);
460
461
    /* Prepopulate the random provider name */
462
16
    dgbl->random_provider_name = OPENSSL_strdup(random_provider_fips_name);
463
16
    if (dgbl->random_provider_name == NULL)
464
0
        goto err0;
465
16
#endif
466
467
16
    dgbl->lock = CRYPTO_THREAD_lock_new();
468
16
    if (dgbl->lock == NULL)
469
0
        goto err1;
470
471
16
    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
16
{
527
16
    EVP_RAND *rand;
528
16
    const char *propq;
529
16
    char *name;
530
16
    EVP_RAND_CTX *ctx = NULL;
531
16
    int fallback = 0;
532
16
#ifdef OPENSSL_NO_FIPS_JITTER
533
16
    RAND_GLOBAL *dgbl = rand_get_global(libctx);
534
535
16
    if (dgbl == NULL)
536
0
        return NULL;
537
16
    propq = dgbl->seed_propq;
538
16
    if (dgbl->seed_name != NULL) {
539
0
        name = dgbl->seed_name;
540
16
    } else {
541
16
        fallback = 1;
542
16
        name = OPENSSL_SEED_SRC_NAME;
543
16
    }
544
#else /* !OPENSSL_NO_FIPS_JITTER */
545
    name = OPENSSL_SEED_SRC_NAME;
546
    propq = "";
547
#endif /* OPENSSL_NO_FIPS_JITTER */
548
549
16
    ERR_set_mark();
550
16
    rand = EVP_RAND_fetch(libctx, name, propq);
551
16
    ERR_pop_to_mark();
552
16
    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
16
    ctx = EVP_RAND_CTX_new(rand, NULL);
558
16
    EVP_RAND_free(rand);
559
16
    if (ctx == NULL) {
560
0
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
561
0
        goto err;
562
0
    }
563
16
    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
16
    return ctx;
568
0
err:
569
0
    EVP_RAND_CTX_free(ctx);
570
0
    return NULL;
571
16
}
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
16
{
580
16
    EVP_RAND_CTX *ret, *seed;
581
582
16
    if (!CRYPTO_THREAD_read_lock(dgbl->lock))
583
0
        return NULL;
584
16
    ret = dgbl->seed;
585
16
    CRYPTO_THREAD_unlock(dgbl->lock);
586
16
    if (ret != NULL)
587
0
        return ret;
588
589
16
    seed = rand_new_seed(ctx);
590
16
    if (seed == NULL)
591
0
        return NULL;
592
593
16
    if (!CRYPTO_THREAD_write_lock(dgbl->lock)) {
594
0
        EVP_RAND_CTX_free(seed);
595
0
        return NULL;
596
0
    }
597
16
    if (dgbl->seed == NULL) {
598
16
        dgbl->seed = seed;
599
16
        seed = NULL;
600
16
    }
601
16
    ret = dgbl->seed;
602
16
    CRYPTO_THREAD_unlock(dgbl->lock);
603
    /* Free the instance that lost a creation race */
604
16
    EVP_RAND_CTX_free(seed);
605
16
    return ret;
606
16
}
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
32
{
630
32
    EVP_RAND *rand;
631
32
    RAND_GLOBAL *dgbl = rand_get_global(libctx);
632
32
    EVP_RAND_CTX *ctx;
633
32
    OSSL_PARAM params[9], *p = params;
634
32
    const OSSL_PARAM *settables;
635
32
    char *name, *cipher;
636
32
    int use_df = 1;
637
638
32
    if (dgbl == NULL)
639
0
        return NULL;
640
32
    name = dgbl->rng_name != NULL ? dgbl->rng_name : "CTR-DRBG";
641
32
    rand = EVP_RAND_fetch(libctx, name, dgbl->rng_propq);
642
32
    if (rand == NULL) {
643
0
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
644
0
        return NULL;
645
0
    }
646
32
    ctx = EVP_RAND_CTX_new(rand, parent);
647
32
    EVP_RAND_free(rand);
648
32
    if (ctx == NULL) {
649
0
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
650
0
        return NULL;
651
0
    }
652
653
32
    settables = EVP_RAND_CTX_settable_params(ctx);
654
32
    if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_CIPHER)) {
655
32
        cipher = dgbl->rng_cipher != NULL ? dgbl->rng_cipher : "AES-256-CTR";
656
32
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
657
32
            cipher, 0);
658
32
    }
659
32
    if (dgbl->rng_digest != NULL
660
0
        && OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_DIGEST))
661
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
662
0
            dgbl->rng_digest, 0);
663
32
    if (dgbl->rng_propq != NULL)
664
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_PROPERTIES,
665
0
            dgbl->rng_propq, 0);
666
32
    if (OSSL_PARAM_locate_const(settables, OSSL_ALG_PARAM_MAC))
667
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_MAC, "HMAC", 0);
668
32
    if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_USE_DF))
669
32
        *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &use_df);
670
32
    *p++ = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS,
671
32
        &reseed_interval);
672
32
    *p++ = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL,
673
32
        &reseed_time_interval);
674
32
    *p = OSSL_PARAM_construct_end();
675
32
    if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, params)) {
676
0
        ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
677
0
        EVP_RAND_CTX_free(ctx);
678
0
        return NULL;
679
0
    }
680
32
    return ctx;
681
32
}
682
683
#if defined(FIPS_MODULE)
684
static EVP_RAND_CTX *rand_new_crngt(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent)
685
{
686
    EVP_RAND *rand;
687
    EVP_RAND_CTX *ctx;
688
689
    rand = EVP_RAND_fetch(libctx, "CRNG-TEST", "-fips");
690
    if (rand == NULL) {
691
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
692
        return NULL;
693
    }
694
    ctx = EVP_RAND_CTX_new(rand, parent);
695
    EVP_RAND_free(rand);
696
    if (ctx == NULL) {
697
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
698
        return NULL;
699
    }
700
701
    if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) {
702
        ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
703
        EVP_RAND_CTX_free(ctx);
704
        return NULL;
705
    }
706
    return ctx;
707
}
708
#endif /* FIPS_MODULE */
709
710
/*
711
 * Get the primary random generator.
712
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
713
 *
714
 */
715
static EVP_RAND_CTX *rand_get0_primary(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
716
64
{
717
64
    EVP_RAND_CTX *ret, *seed = NULL, *primary;
718
719
64
    if (dgbl == NULL)
720
0
        return NULL;
721
722
64
    if (!CRYPTO_THREAD_read_lock(dgbl->lock))
723
0
        return NULL;
724
725
64
    ret = dgbl->primary;
726
64
    CRYPTO_THREAD_unlock(dgbl->lock);
727
728
64
    if (ret != NULL)
729
48
        return ret;
730
731
16
#if !defined(FIPS_MODULE) || !defined(OPENSSL_NO_FIPS_JITTER)
732
    /* Create a seed source for libcrypto or jitter enabled FIPS provider */
733
16
    ERR_set_mark();
734
16
    seed = rand_get0_seed(ctx, dgbl);
735
16
    if (seed == NULL && ERR_count_to_mark() > 0) {
736
0
        ERR_clear_last_mark();
737
0
        return NULL;
738
0
    }
739
16
    ERR_pop_to_mark();
740
16
#endif /* !FIPS_MODULE || !OPENSSL_NO_FIPS_JITTER */
741
742
#if defined(FIPS_MODULE)
743
    /* The FIPS provider has entropy health tests instead of the primary */
744
    ret = rand_new_crngt(ctx, seed);
745
#else /* FIPS_MODULE */
746
16
    ret = rand_new_drbg(ctx, seed, PRIMARY_RESEED_INTERVAL,
747
16
        PRIMARY_RESEED_TIME_INTERVAL);
748
16
#endif /* FIPS_MODULE */
749
750
16
    if (ret == NULL)
751
0
        return NULL;
752
753
    /*
754
     * The primary DRBG may be shared between multiple threads so we must
755
     * enable locking.
756
     */
757
16
    if (!EVP_RAND_enable_locking(ret)) {
758
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_ENABLE_LOCKING);
759
0
        EVP_RAND_CTX_free(ret);
760
0
        return NULL;
761
0
    }
762
763
16
    if (!CRYPTO_THREAD_write_lock(dgbl->lock)) {
764
0
        EVP_RAND_CTX_free(ret);
765
0
        return NULL;
766
0
    }
767
768
16
    primary = dgbl->primary;
769
16
    if (primary != NULL) {
770
0
        CRYPTO_THREAD_unlock(dgbl->lock);
771
0
        EVP_RAND_CTX_free(ret);
772
0
        return primary;
773
0
    }
774
16
    dgbl->primary = ret;
775
16
    CRYPTO_THREAD_unlock(dgbl->lock);
776
777
16
    return ret;
778
16
}
779
780
/*
781
 * Get the primary random generator.
782
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
783
 *
784
 */
785
EVP_RAND_CTX *RAND_get0_primary(OSSL_LIB_CTX *ctx)
786
48
{
787
48
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
788
789
48
    return dgbl == NULL ? NULL : rand_get0_primary(ctx, dgbl);
790
48
}
791
792
static EVP_RAND_CTX *rand_get0_public(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
793
32
{
794
32
    EVP_RAND_CTX *rand, *primary;
795
32
    OSSL_LIB_CTX *origctx = ctx;
796
797
32
    ctx = ossl_lib_ctx_get_concrete(ctx);
798
799
32
    if (ctx == NULL)
800
0
        return NULL;
801
802
32
    if (dgbl == NULL)
803
0
        return NULL;
804
805
32
    rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
806
32
    if (rand == NULL) {
807
16
        primary = rand_get0_primary(origctx, dgbl);
808
16
        if (primary == NULL)
809
0
            return NULL;
810
811
        /*
812
         * If the private is also NULL then this is the first time we've
813
         * used this thread.
814
         */
815
16
        if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx) == NULL
816
16
            && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
817
0
            return NULL;
818
16
        rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
819
16
            SECONDARY_RESEED_TIME_INTERVAL);
820
16
        if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand)) {
821
0
            EVP_RAND_CTX_free(rand);
822
0
            rand = NULL;
823
0
        }
824
16
    }
825
32
    return rand;
826
32
}
827
828
/*
829
 * Get the public random generator.
830
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
831
 */
832
EVP_RAND_CTX *RAND_get0_public(OSSL_LIB_CTX *ctx)
833
0
{
834
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
835
836
0
    return dgbl == NULL ? NULL : rand_get0_public(ctx, dgbl);
837
0
}
838
839
static EVP_RAND_CTX *rand_get0_private(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
840
0
{
841
0
    EVP_RAND_CTX *rand, *primary;
842
0
    OSSL_LIB_CTX *origctx = ctx;
843
844
0
    ctx = ossl_lib_ctx_get_concrete(ctx);
845
0
    if (ctx == NULL)
846
0
        return NULL;
847
848
0
    rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
849
0
    if (rand == NULL) {
850
0
        primary = rand_get0_primary(origctx, dgbl);
851
0
        if (primary == NULL)
852
0
            return NULL;
853
854
        /*
855
         * If the public is also NULL then this is the first time we've
856
         * used this thread.
857
         */
858
0
        if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx) == NULL
859
0
            && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
860
0
            return NULL;
861
0
        rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
862
0
            SECONDARY_RESEED_TIME_INTERVAL);
863
0
        if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand)) {
864
0
            EVP_RAND_CTX_free(rand);
865
0
            rand = NULL;
866
0
        }
867
0
    }
868
0
    return rand;
869
0
}
870
871
/*
872
 * Get the private random generator.
873
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
874
 */
875
EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx)
876
0
{
877
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
878
879
0
    return dgbl == NULL ? NULL : rand_get0_private(ctx, dgbl);
880
0
}
881
882
#ifdef FIPS_MODULE
883
EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx)
884
{
885
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
886
887
    if (dgbl == NULL)
888
        return NULL;
889
890
    return CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
891
}
892
#endif
893
894
int RAND_set0_public(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
895
0
{
896
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
897
0
    EVP_RAND_CTX *old;
898
0
    int r;
899
900
0
    if (dgbl == NULL)
901
0
        return 0;
902
0
    old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
903
0
    if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand)) > 0)
904
0
        EVP_RAND_CTX_free(old);
905
0
    return r;
906
0
}
907
908
int RAND_set0_private(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
909
0
{
910
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
911
0
    EVP_RAND_CTX *old;
912
0
    int r;
913
914
0
    if (dgbl == NULL)
915
0
        return 0;
916
0
    old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
917
0
    if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand)) > 0)
918
0
        EVP_RAND_CTX_free(old);
919
0
    return r;
920
0
}
921
922
#ifndef FIPS_MODULE
923
static int random_set_string(char **p, const char *s)
924
0
{
925
0
    char *d = NULL;
926
927
0
    if (s != NULL) {
928
0
        d = OPENSSL_strdup(s);
929
0
        if (d == NULL)
930
0
            return 0;
931
0
    }
932
0
    OPENSSL_free(*p);
933
0
    *p = d;
934
0
    return 1;
935
0
}
936
937
/*
938
 * Load the DRBG definitions from a configuration file.
939
 */
940
static int random_conf_init(CONF_IMODULE *md, const CONF *cnf)
941
0
{
942
0
    STACK_OF(CONF_VALUE) *elist;
943
0
    CONF_VALUE *cval;
944
0
    OSSL_LIB_CTX *libctx = NCONF_get0_libctx((CONF *)cnf);
945
0
    RAND_GLOBAL *dgbl = rand_get_global(libctx);
946
0
    int i, r = 1;
947
948
0
    OSSL_TRACE1(CONF, "Loading random module: section %s\n",
949
0
        CONF_imodule_get_value(md));
950
951
    /* Value is a section containing RANDOM configuration */
952
0
    elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
953
0
    if (elist == NULL) {
954
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_RANDOM_SECTION_ERROR);
955
0
        return 0;
956
0
    }
957
958
0
    if (dgbl == NULL)
959
0
        return 0;
960
961
0
    for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
962
0
        cval = sk_CONF_VALUE_value(elist, i);
963
0
        if (OPENSSL_strcasecmp(cval->name, "random") == 0) {
964
0
            if (!random_set_string(&dgbl->rng_name, cval->value))
965
0
                return 0;
966
0
        } else if (OPENSSL_strcasecmp(cval->name, "cipher") == 0) {
967
0
            if (!random_set_string(&dgbl->rng_cipher, cval->value))
968
0
                return 0;
969
0
        } else if (OPENSSL_strcasecmp(cval->name, "digest") == 0) {
970
0
            if (!random_set_string(&dgbl->rng_digest, cval->value))
971
0
                return 0;
972
0
        } else if (OPENSSL_strcasecmp(cval->name, "properties") == 0) {
973
0
            if (!random_set_string(&dgbl->rng_propq, cval->value))
974
0
                return 0;
975
0
        } else if (OPENSSL_strcasecmp(cval->name, "seed") == 0) {
976
0
            if (!random_set_string(&dgbl->seed_name, cval->value))
977
0
                return 0;
978
0
        } else if (OPENSSL_strcasecmp(cval->name, "seed_properties") == 0) {
979
0
            if (!random_set_string(&dgbl->seed_propq, cval->value))
980
0
                return 0;
981
0
        } else if (OPENSSL_strcasecmp(cval->name, "random_provider") == 0) {
982
0
#ifndef FIPS_MODULE
983
0
            OSSL_PROVIDER *prov = ossl_provider_find(libctx, cval->value, 0);
984
985
0
            if (prov != NULL) {
986
0
                if (!RAND_set1_random_provider(libctx, prov)) {
987
0
                    ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
988
0
                    OSSL_PROVIDER_unload(prov);
989
0
                    return 0;
990
0
                }
991
                /*
992
                 * We need to release the reference from ossl_provider_find because
993
                 * we don't want to keep a reference counted handle to the provider.
994
                 *
995
                 * The provider unload code checks for the random provider and,
996
                 * if present, our reference will be NULLed when it is fully freed.
997
                 * The provider load code, conversely, checks the provider name
998
                 * and re-hooks our reference if required.  This means that a load,
999
                 * hook random provider, use, unload, reload, reuse sequence will
1000
                 * work as expected.
1001
                 */
1002
0
                OSSL_PROVIDER_unload(prov);
1003
0
            } else if (!set_random_provider_name(dgbl, cval->value))
1004
0
                return 0;
1005
0
#endif
1006
0
        } else {
1007
0
            ERR_raise_data(ERR_LIB_CRYPTO,
1008
0
                CRYPTO_R_UNKNOWN_NAME_IN_RANDOM_SECTION,
1009
0
                "name=%s, value=%s", cval->name, cval->value);
1010
0
            r = 0;
1011
0
        }
1012
0
    }
1013
0
    return r;
1014
0
}
1015
1016
static void random_conf_deinit(CONF_IMODULE *md)
1017
0
{
1018
0
    OSSL_TRACE(CONF, "Cleaned up random\n");
1019
0
}
1020
1021
void ossl_random_add_conf_module(void)
1022
0
{
1023
0
    OSSL_TRACE(CONF, "Adding config module 'random'\n");
1024
0
    CONF_module_add("random", random_conf_init, random_conf_deinit);
1025
0
}
1026
1027
int RAND_set_DRBG_type(OSSL_LIB_CTX *ctx, const char *drbg, const char *propq,
1028
    const char *cipher, const char *digest)
1029
0
{
1030
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1031
1032
0
    if (dgbl == NULL)
1033
0
        return 0;
1034
0
    if (dgbl->primary != NULL) {
1035
0
        ERR_raise(ERR_LIB_RAND, RAND_R_ALREADY_INSTANTIATED);
1036
0
        return 0;
1037
0
    }
1038
0
    return random_set_string(&dgbl->rng_name, drbg)
1039
0
        && random_set_string(&dgbl->rng_propq, propq)
1040
0
        && random_set_string(&dgbl->rng_cipher, cipher)
1041
0
        && random_set_string(&dgbl->rng_digest, digest);
1042
0
}
1043
1044
int RAND_set_seed_source_type(OSSL_LIB_CTX *ctx, const char *seed,
1045
    const char *propq)
1046
0
{
1047
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1048
1049
0
    if (dgbl == NULL)
1050
0
        return 0;
1051
0
    if (dgbl->seed != NULL) {
1052
0
        ERR_raise(ERR_LIB_RAND, RAND_R_ALREADY_INSTANTIATED);
1053
0
        return 0;
1054
0
    }
1055
0
    return random_set_string(&dgbl->seed_name, seed)
1056
0
        && random_set_string(&dgbl->seed_propq, propq);
1057
0
}
1058
1059
int RAND_set1_random_provider(OSSL_LIB_CTX *ctx, OSSL_PROVIDER *prov)
1060
0
{
1061
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1062
1063
0
    if (dgbl == NULL)
1064
0
        return 0;
1065
1066
0
    if (prov == NULL) {
1067
0
        OPENSSL_free(dgbl->random_provider_name);
1068
0
        dgbl->random_provider_name = NULL;
1069
0
        dgbl->random_provider = NULL;
1070
0
        return 1;
1071
0
    }
1072
1073
0
    if (dgbl->random_provider == prov)
1074
0
        return 1;
1075
1076
0
    if (!set_random_provider_name(dgbl, OSSL_PROVIDER_get0_name(prov)))
1077
0
        return 0;
1078
1079
0
    dgbl->random_provider = prov;
1080
0
    return 1;
1081
0
}
1082
1083
/*
1084
 * When a new provider is loaded, we need to check to see if it is the
1085
 * designated randomness provider and register it if it is.
1086
 */
1087
int ossl_rand_check_random_provider_on_load(OSSL_LIB_CTX *ctx,
1088
    OSSL_PROVIDER *prov)
1089
0
{
1090
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1091
1092
0
    if (dgbl == NULL)
1093
0
        return 0;
1094
1095
    /* No random provider name specified, or one is installed already */
1096
0
    if (dgbl->random_provider_name == NULL || dgbl->random_provider != NULL)
1097
0
        return 1;
1098
1099
    /* Does this provider match the name we're using? */
1100
0
    if (strcmp(dgbl->random_provider_name, OSSL_PROVIDER_get0_name(prov)) != 0)
1101
0
        return 1;
1102
1103
0
    dgbl->random_provider = prov;
1104
0
    return 1;
1105
0
}
1106
1107
/*
1108
 * When a provider is being unloaded, if it is the randomness provider,
1109
 * we need to deregister it.
1110
 */
1111
int ossl_rand_check_random_provider_on_unload(OSSL_LIB_CTX *ctx,
1112
    OSSL_PROVIDER *prov)
1113
0
{
1114
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1115
1116
0
    if (dgbl == NULL)
1117
0
        return 0;
1118
1119
0
    if (dgbl->random_provider == prov)
1120
0
        dgbl->random_provider = NULL;
1121
0
    return 1;
1122
0
}
1123
1124
#endif /* !FIPS_MODULE */