Coverage Report

Created: 2026-09-12 06:55

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