Coverage Report

Created: 2025-12-31 06:58

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-2025 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
2.15M
{
84
2.15M
    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_INDEX);
85
2.15M
}
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
61
{
132
61
#ifndef OPENSSL_NO_ENGINE
133
61
    rand_engine_lock = CRYPTO_THREAD_lock_new();
134
61
    if (rand_engine_lock == NULL)
135
0
        return 0;
136
61
#endif /* !OPENSSL_NO_ENGINE */
137
138
61
#ifndef OPENSSL_NO_DEPRECATED_3_0
139
61
    rand_meth_lock = CRYPTO_THREAD_lock_new();
140
61
    if (rand_meth_lock == NULL)
141
0
        goto err;
142
61
#endif /* !OPENSSL_NO_DEPRECATED_3_0 */
143
144
61
    if (!ossl_rand_pool_init())
145
0
        goto err;
146
147
61
    rand_inited = 1;
148
61
    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
61
}
161
162
void ossl_rand_cleanup_int(void)
163
220
{
164
220
#ifndef OPENSSL_NO_DEPRECATED_3_0
165
220
    const RAND_METHOD *meth = default_RAND_meth;
166
167
220
    if (!rand_inited)
168
159
        return;
169
170
61
    if (meth != NULL && meth->cleanup != NULL)
171
0
        meth->cleanup();
172
61
    RAND_set_rand_method(NULL);
173
61
#endif /* !OPENSSL_NO_DEPRECATED_3_0 */
174
61
    ossl_rand_pool_cleanup();
175
61
#ifndef OPENSSL_NO_ENGINE
176
61
    CRYPTO_THREAD_lock_free(rand_engine_lock);
177
61
    rand_engine_lock = NULL;
178
61
#endif /* !OPENSSL_NO_ENGINE */
179
61
#ifndef OPENSSL_NO_DEPRECATED_3_0
180
61
    CRYPTO_THREAD_lock_free(rand_meth_lock);
181
61
    rand_meth_lock = NULL;
182
61
#endif /* !OPENSSL_NO_DEPRECATED_3_0 */
183
61
    ossl_release_default_drbg_ctx();
184
61
    rand_inited = 0;
185
61
}
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
61
{
250
61
    if (!RUN_ONCE(&rand_init, do_rand_init))
251
0
        return 0;
252
253
61
    if (!CRYPTO_THREAD_write_lock(rand_meth_lock))
254
0
        return 0;
255
61
#ifndef OPENSSL_NO_ENGINE
256
61
    ENGINE_finish(funct_ref);
257
61
    funct_ref = e;
258
61
#endif
259
61
    default_RAND_meth = meth;
260
61
    CRYPTO_THREAD_unlock(rand_meth_lock);
261
61
    return 1;
262
61
}
263
264
int RAND_set_rand_method(const RAND_METHOD *meth)
265
61
{
266
61
    return rand_set_rand_method_internal(meth, NULL);
267
61
}
268
269
const RAND_METHOD *RAND_get_rand_method(void)
270
1.15M
{
271
1.15M
    const RAND_METHOD *tmp_meth = NULL;
272
273
1.15M
    if (!RUN_ONCE(&rand_init, do_rand_init))
274
0
        return NULL;
275
276
1.15M
    if (rand_meth_lock == NULL)
277
0
        return NULL;
278
279
1.15M
    if (!CRYPTO_THREAD_read_lock(rand_meth_lock))
280
0
        return NULL;
281
1.15M
    tmp_meth = default_RAND_meth;
282
1.15M
    CRYPTO_THREAD_unlock(rand_meth_lock);
283
1.15M
    if (tmp_meth != NULL)
284
1.15M
        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
509k
{
423
509k
    RAND_GLOBAL *dgbl;
424
509k
    EVP_RAND_CTX *rand;
425
509k
#if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)
426
509k
    const RAND_METHOD *meth = RAND_get_rand_method();
427
428
509k
    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
509k
#endif
439
440
509k
    dgbl = rand_get_global(ctx);
441
509k
    if (dgbl == NULL)
442
0
        return 0;
443
509k
#ifndef FIPS_MODULE
444
509k
    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
509k
#endif /* !FIPS_MODULE */
449
509k
    rand = rand_get0_private(ctx, dgbl);
450
509k
    if (rand != NULL)
451
509k
        return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);
452
453
0
    return 0;
454
509k
}
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
266k
{
466
266k
    RAND_GLOBAL *dgbl;
467
266k
    EVP_RAND_CTX *rand;
468
266k
#if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)
469
266k
    const RAND_METHOD *meth = RAND_get_rand_method();
470
471
266k
    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
266k
#endif
482
483
266k
    dgbl = rand_get_global(ctx);
484
266k
    if (dgbl == NULL)
485
0
        return 0;
486
266k
#ifndef FIPS_MODULE
487
266k
    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
266k
#endif /* !FIPS_MODULE */
492
493
266k
    rand = rand_get0_public(ctx, dgbl);
494
266k
    if (rand != NULL)
495
266k
        return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);
496
497
0
    return 0;
498
266k
}
499
500
int RAND_bytes(unsigned char *buf, int num)
501
765
{
502
765
    if (num < 0)
503
0
        return 0;
504
765
    return RAND_bytes_ex(NULL, buf, (size_t)num, 0);
505
765
}
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
157
{
513
157
    RAND_GLOBAL *dgbl = OPENSSL_zalloc(sizeof(*dgbl));
514
515
157
    if (dgbl == NULL)
516
0
        return NULL;
517
518
157
#ifndef FIPS_MODULE
519
    /*
520
     * We need to ensure that base libcrypto thread handling has been
521
     * initialised.
522
     */
523
157
    OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL);
524
525
    /* Prepopulate the random provider name */
526
157
    dgbl->random_provider_name = OPENSSL_strdup(random_provider_fips_name);
527
157
    if (dgbl->random_provider_name == NULL)
528
0
        goto err0;
529
157
#endif
530
531
157
    dgbl->lock = CRYPTO_THREAD_lock_new();
532
157
    if (dgbl->lock == NULL)
533
0
        goto err1;
534
535
157
    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
213
{
549
213
    RAND_GLOBAL *dgbl = vdgbl;
550
551
213
    if (dgbl == NULL)
552
6
        return;
553
554
207
    CRYPTO_THREAD_lock_free(dgbl->lock);
555
207
    EVP_RAND_CTX_free(dgbl->primary);
556
207
    EVP_RAND_CTX_free(dgbl->seed);
557
207
#ifndef FIPS_MODULE
558
207
    OPENSSL_free(dgbl->random_provider_name);
559
207
#endif /* !FIPS_MODULE */
560
207
    OPENSSL_free(dgbl->rng_name);
561
207
    OPENSSL_free(dgbl->rng_cipher);
562
207
    OPENSSL_free(dgbl->rng_digest);
563
207
    OPENSSL_free(dgbl->rng_propq);
564
207
    OPENSSL_free(dgbl->seed_name);
565
207
    OPENSSL_free(dgbl->seed_propq);
566
567
207
    OPENSSL_free(dgbl);
568
207
}
569
570
static void rand_delete_thread_state(void *arg)
571
61
{
572
61
    OSSL_LIB_CTX *ctx = arg;
573
61
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
574
61
    EVP_RAND_CTX *rand;
575
576
61
    if (dgbl == NULL)
577
0
        return;
578
579
61
    rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
580
61
    CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, NULL);
581
61
    EVP_RAND_CTX_free(rand);
582
583
61
    rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
584
61
    CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, NULL);
585
61
    EVP_RAND_CTX_free(rand);
586
61
}
587
588
#if !defined(FIPS_MODULE) || !defined(OPENSSL_NO_FIPS_JITTER)
589
static EVP_RAND_CTX *rand_new_seed(OSSL_LIB_CTX *libctx)
590
49
{
591
49
    EVP_RAND *rand;
592
49
    const char *propq;
593
49
    char *name;
594
49
    EVP_RAND_CTX *ctx = NULL;
595
49
#ifdef OPENSSL_NO_FIPS_JITTER
596
49
    RAND_GLOBAL *dgbl = rand_get_global(libctx);
597
598
49
    if (dgbl == NULL)
599
0
        return NULL;
600
49
    propq = dgbl->seed_propq;
601
49
    name = dgbl->seed_name != NULL ? dgbl->seed_name
602
49
                                   : 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
49
    rand = EVP_RAND_fetch(libctx, name, propq);
609
49
    if (rand == NULL) {
610
0
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
611
0
        goto err;
612
0
    }
613
49
    ctx = EVP_RAND_CTX_new(rand, NULL);
614
49
    EVP_RAND_free(rand);
615
49
    if (ctx == NULL) {
616
0
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
617
0
        goto err;
618
0
    }
619
49
    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
49
    return ctx;
624
0
err:
625
0
    EVP_RAND_CTX_free(ctx);
626
0
    return NULL;
627
49
}
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
2.46k
{
633
2.46k
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
634
2.46k
    EVP_RAND_CTX *ret;
635
636
2.46k
    if (dgbl == NULL)
637
0
        return NULL;
638
639
2.46k
    if (!CRYPTO_THREAD_read_lock(dgbl->lock))
640
0
        return NULL;
641
2.46k
    ret = dgbl->seed;
642
2.46k
    CRYPTO_THREAD_unlock(dgbl->lock);
643
2.46k
    return ret;
644
2.46k
}
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
128
{
651
128
    EVP_RAND *rand;
652
128
    RAND_GLOBAL *dgbl = rand_get_global(libctx);
653
128
    EVP_RAND_CTX *ctx;
654
128
    OSSL_PARAM params[9], *p = params;
655
128
    const OSSL_PARAM *settables;
656
128
    const char *prov_name;
657
128
    char *name, *cipher;
658
128
    int use_df = 1;
659
660
128
    if (dgbl == NULL)
661
0
        return NULL;
662
128
    name = dgbl->rng_name != NULL ? dgbl->rng_name : "CTR-DRBG";
663
128
    rand = EVP_RAND_fetch(libctx, name, dgbl->rng_propq);
664
128
    if (rand == NULL) {
665
0
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
666
0
        return NULL;
667
0
    }
668
128
    prov_name = ossl_provider_name(EVP_RAND_get0_provider(rand));
669
128
    ctx = EVP_RAND_CTX_new(rand, parent);
670
128
    EVP_RAND_free(rand);
671
128
    if (ctx == NULL) {
672
0
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
673
0
        return NULL;
674
0
    }
675
676
128
    settables = EVP_RAND_CTX_settable_params(ctx);
677
128
    if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_CIPHER)) {
678
35
        cipher = dgbl->rng_cipher != NULL ? dgbl->rng_cipher : "AES-256-CTR";
679
35
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
680
35
            cipher, 0);
681
35
    }
682
128
    if (dgbl->rng_digest != NULL
683
0
        && OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_DIGEST))
684
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
685
0
            dgbl->rng_digest, 0);
686
128
    if (prov_name != NULL)
687
128
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_CORE_PROV_NAME,
688
128
            (char *)prov_name, 0);
689
128
    if (dgbl->rng_propq != NULL)
690
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_PROPERTIES,
691
0
            dgbl->rng_propq, 0);
692
128
    if (OSSL_PARAM_locate_const(settables, OSSL_ALG_PARAM_MAC))
693
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_MAC, "HMAC", 0);
694
128
    if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_USE_DF))
695
35
        *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &use_df);
696
128
    *p++ = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS,
697
128
        &reseed_interval);
698
128
    *p++ = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL,
699
128
        &reseed_time_interval);
700
128
    *p = OSSL_PARAM_construct_end();
701
128
    if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, params)) {
702
0
        ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
703
0
        EVP_RAND_CTX_free(ctx);
704
0
        return NULL;
705
0
    }
706
128
    return ctx;
707
128
}
708
709
#if defined(FIPS_MODULE)
710
static EVP_RAND_CTX *rand_new_crngt(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent)
711
{
712
    EVP_RAND *rand;
713
    EVP_RAND_CTX *ctx;
714
715
    rand = EVP_RAND_fetch(libctx, "CRNG-TEST", "-fips");
716
    if (rand == NULL) {
717
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
718
        return NULL;
719
    }
720
    ctx = EVP_RAND_CTX_new(rand, parent);
721
    EVP_RAND_free(rand);
722
    if (ctx == NULL) {
723
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
724
        return NULL;
725
    }
726
727
    if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) {
728
        ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
729
        EVP_RAND_CTX_free(ctx);
730
        return NULL;
731
    }
732
    return ctx;
733
}
734
#endif /* FIPS_MODULE */
735
736
/*
737
 * Get the primary random generator.
738
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
739
 *
740
 */
741
static EVP_RAND_CTX *rand_get0_primary(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
742
79
{
743
79
    EVP_RAND_CTX *ret, *seed, *newseed = NULL, *primary;
744
745
79
    if (dgbl == NULL)
746
0
        return NULL;
747
748
79
    if (!CRYPTO_THREAD_read_lock(dgbl->lock))
749
0
        return NULL;
750
751
79
    ret = dgbl->primary;
752
79
    seed = dgbl->seed;
753
79
    CRYPTO_THREAD_unlock(dgbl->lock);
754
755
79
    if (ret != NULL)
756
30
        return ret;
757
758
49
#if !defined(FIPS_MODULE) || !defined(OPENSSL_NO_FIPS_JITTER)
759
    /* Create a seed source for libcrypto or jitter enabled FIPS provider */
760
49
    if (seed == NULL) {
761
49
        ERR_set_mark();
762
49
        seed = newseed = rand_new_seed(ctx);
763
49
        ERR_pop_to_mark();
764
49
    }
765
49
#endif /* !FIPS_MODULE || !OPENSSL_NO_FIPS_JITTER */
766
767
#if defined(FIPS_MODULE)
768
    /* The FIPS provider has entropy health tests instead of the primary */
769
    ret = rand_new_crngt(ctx, seed);
770
#else /* FIPS_MODULE */
771
49
    ret = rand_new_drbg(ctx, seed, PRIMARY_RESEED_INTERVAL,
772
49
        PRIMARY_RESEED_TIME_INTERVAL);
773
49
#endif /* FIPS_MODULE */
774
775
    /*
776
     * The primary DRBG may be shared between multiple threads so we must
777
     * enable locking.
778
     */
779
49
    if (ret == NULL || !EVP_RAND_enable_locking(ret)) {
780
0
        if (ret != NULL) {
781
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_ENABLE_LOCKING);
782
0
            EVP_RAND_CTX_free(ret);
783
0
        }
784
0
        if (newseed == NULL)
785
0
            return NULL;
786
        /* else carry on and store seed */
787
0
        ret = NULL;
788
0
    }
789
790
49
    if (!CRYPTO_THREAD_write_lock(dgbl->lock))
791
0
        return NULL;
792
793
49
    primary = dgbl->primary;
794
49
    if (primary != NULL) {
795
0
        CRYPTO_THREAD_unlock(dgbl->lock);
796
0
        EVP_RAND_CTX_free(ret);
797
0
        EVP_RAND_CTX_free(newseed);
798
0
        return primary;
799
0
    }
800
49
    if (newseed != NULL)
801
49
        dgbl->seed = newseed;
802
49
    dgbl->primary = ret;
803
49
    CRYPTO_THREAD_unlock(dgbl->lock);
804
805
49
    return ret;
806
49
}
807
808
/*
809
 * Get the primary random generator.
810
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
811
 *
812
 */
813
EVP_RAND_CTX *RAND_get0_primary(OSSL_LIB_CTX *ctx)
814
0
{
815
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
816
817
0
    return dgbl == NULL ? NULL : rand_get0_primary(ctx, dgbl);
818
0
}
819
820
static EVP_RAND_CTX *rand_get0_public(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
821
266k
{
822
266k
    EVP_RAND_CTX *rand, *primary;
823
266k
    OSSL_LIB_CTX *origctx = ctx;
824
825
266k
    ctx = ossl_lib_ctx_get_concrete(ctx);
826
827
266k
    if (ctx == NULL)
828
0
        return NULL;
829
830
266k
    if (dgbl == NULL)
831
0
        return NULL;
832
833
266k
    rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
834
266k
    if (rand == NULL) {
835
23
        primary = rand_get0_primary(origctx, dgbl);
836
23
        if (primary == NULL)
837
0
            return NULL;
838
839
        /*
840
         * If the private is also NULL then this is the first time we've
841
         * used this thread.
842
         */
843
23
        if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx) == NULL
844
19
            && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
845
0
            return NULL;
846
23
        rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
847
23
            SECONDARY_RESEED_TIME_INTERVAL);
848
23
        if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand)) {
849
0
            EVP_RAND_CTX_free(rand);
850
0
            rand = NULL;
851
0
        }
852
23
    }
853
266k
    return rand;
854
266k
}
855
856
/*
857
 * Get the public random generator.
858
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
859
 */
860
EVP_RAND_CTX *RAND_get0_public(OSSL_LIB_CTX *ctx)
861
{
862
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
863
864
    return dgbl == NULL ? NULL : rand_get0_public(ctx, dgbl);
865
}
866
867
static EVP_RAND_CTX *rand_get0_private(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
868
509k
{
869
509k
    EVP_RAND_CTX *rand, *primary;
870
509k
    OSSL_LIB_CTX *origctx = ctx;
871
872
509k
    ctx = ossl_lib_ctx_get_concrete(ctx);
873
509k
    if (ctx == NULL)
874
0
        return NULL;
875
876
509k
    rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
877
509k
    if (rand == NULL) {
878
30
        primary = rand_get0_primary(origctx, dgbl);
879
30
        if (primary == NULL)
880
0
            return NULL;
881
882
        /*
883
         * If the public is also NULL then this is the first time we've
884
         * used this thread.
885
         */
886
30
        if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx) == NULL
887
14
            && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
888
0
            return NULL;
889
30
        rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
890
30
            SECONDARY_RESEED_TIME_INTERVAL);
891
30
        if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand)) {
892
0
            EVP_RAND_CTX_free(rand);
893
0
            rand = NULL;
894
0
        }
895
30
    }
896
509k
    return rand;
897
509k
}
898
899
/*
900
 * Get the private random generator.
901
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
902
 */
903
EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx)
904
{
905
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
906
907
    return dgbl == NULL ? NULL : rand_get0_private(ctx, dgbl);
908
}
909
910
#ifdef FIPS_MODULE
911
EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx)
912
{
913
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
914
915
    if (dgbl == NULL)
916
        return NULL;
917
918
    return CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
919
}
920
#endif
921
922
int RAND_set0_public(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
923
0
{
924
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
925
0
    EVP_RAND_CTX *old;
926
0
    int r;
927
928
0
    if (dgbl == NULL)
929
0
        return 0;
930
0
    old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
931
0
    if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand)) > 0)
932
0
        EVP_RAND_CTX_free(old);
933
0
    return r;
934
0
}
935
936
int RAND_set0_private(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
937
0
{
938
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
939
0
    EVP_RAND_CTX *old;
940
0
    int r;
941
942
0
    if (dgbl == NULL)
943
0
        return 0;
944
0
    old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
945
0
    if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand)) > 0)
946
0
        EVP_RAND_CTX_free(old);
947
0
    return r;
948
0
}
949
950
#ifndef FIPS_MODULE
951
static int random_set_string(char **p, const char *s)
952
544
{
953
544
    char *d = NULL;
954
955
544
    if (s != NULL) {
956
136
        d = OPENSSL_strdup(s);
957
136
        if (d == NULL)
958
0
            return 0;
959
136
    }
960
544
    OPENSSL_free(*p);
961
544
    *p = d;
962
544
    return 1;
963
544
}
964
965
/*
966
 * Load the DRBG definitions from a configuration file.
967
 */
968
static int random_conf_init(CONF_IMODULE *md, const CONF *cnf)
969
0
{
970
0
    STACK_OF(CONF_VALUE) *elist;
971
0
    CONF_VALUE *cval;
972
0
    OSSL_LIB_CTX *libctx = NCONF_get0_libctx((CONF *)cnf);
973
0
    RAND_GLOBAL *dgbl = rand_get_global(libctx);
974
0
    int i, r = 1;
975
976
0
    OSSL_TRACE1(CONF, "Loading random module: section %s\n",
977
0
        CONF_imodule_get_value(md));
978
979
    /* Value is a section containing RANDOM configuration */
980
0
    elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
981
0
    if (elist == NULL) {
982
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_RANDOM_SECTION_ERROR);
983
0
        return 0;
984
0
    }
985
986
0
    if (dgbl == NULL)
987
0
        return 0;
988
989
0
    for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
990
0
        cval = sk_CONF_VALUE_value(elist, i);
991
0
        if (OPENSSL_strcasecmp(cval->name, "random") == 0) {
992
0
            if (!random_set_string(&dgbl->rng_name, cval->value))
993
0
                return 0;
994
0
        } else if (OPENSSL_strcasecmp(cval->name, "cipher") == 0) {
995
0
            if (!random_set_string(&dgbl->rng_cipher, cval->value))
996
0
                return 0;
997
0
        } else if (OPENSSL_strcasecmp(cval->name, "digest") == 0) {
998
0
            if (!random_set_string(&dgbl->rng_digest, cval->value))
999
0
                return 0;
1000
0
        } else if (OPENSSL_strcasecmp(cval->name, "properties") == 0) {
1001
0
            if (!random_set_string(&dgbl->rng_propq, cval->value))
1002
0
                return 0;
1003
0
        } else if (OPENSSL_strcasecmp(cval->name, "seed") == 0) {
1004
0
            if (!random_set_string(&dgbl->seed_name, cval->value))
1005
0
                return 0;
1006
0
        } else if (OPENSSL_strcasecmp(cval->name, "seed_properties") == 0) {
1007
0
            if (!random_set_string(&dgbl->seed_propq, cval->value))
1008
0
                return 0;
1009
0
        } else if (OPENSSL_strcasecmp(cval->name, "random_provider") == 0) {
1010
0
#ifndef FIPS_MODULE
1011
0
            OSSL_PROVIDER *prov = ossl_provider_find(libctx, cval->value, 0);
1012
1013
0
            if (prov != NULL) {
1014
0
                if (!RAND_set1_random_provider(libctx, prov)) {
1015
0
                    ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
1016
0
                    OSSL_PROVIDER_unload(prov);
1017
0
                    return 0;
1018
0
                }
1019
                /*
1020
                 * We need to release the reference from ossl_provider_find because
1021
                 * we don't want to keep a reference counted handle to the provider.
1022
                 *
1023
                 * The provider unload code checks for the random provider and,
1024
                 * if present, our reference will be NULLed when it is fully freed.
1025
                 * The provider load code, conversely, checks the provider name
1026
                 * and re-hooks our reference if required.  This means that a load,
1027
                 * hook random provider, use, unload, reload, reuse sequence will
1028
                 * work as expected.
1029
                 */
1030
0
                OSSL_PROVIDER_unload(prov);
1031
0
            } else if (!set_random_provider_name(dgbl, cval->value))
1032
0
                return 0;
1033
0
#endif
1034
0
        } else {
1035
0
            ERR_raise_data(ERR_LIB_CRYPTO,
1036
0
                CRYPTO_R_UNKNOWN_NAME_IN_RANDOM_SECTION,
1037
0
                "name=%s, value=%s", cval->name, cval->value);
1038
0
            r = 0;
1039
0
        }
1040
0
    }
1041
0
    return r;
1042
0
}
1043
1044
static void random_conf_deinit(CONF_IMODULE *md)
1045
0
{
1046
0
    OSSL_TRACE(CONF, "Cleaned up random\n");
1047
0
}
1048
1049
void ossl_random_add_conf_module(void)
1050
0
{
1051
0
    OSSL_TRACE(CONF, "Adding config module 'random'\n");
1052
0
    CONF_module_add("random", random_conf_init, random_conf_deinit);
1053
0
}
1054
1055
int RAND_set_DRBG_type(OSSL_LIB_CTX *ctx, const char *drbg, const char *propq,
1056
    const char *cipher, const char *digest)
1057
136
{
1058
136
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1059
1060
136
    if (dgbl == NULL)
1061
0
        return 0;
1062
136
    if (dgbl->primary != NULL) {
1063
0
        ERR_raise(ERR_LIB_RAND, RAND_R_ALREADY_INSTANTIATED);
1064
0
        return 0;
1065
0
    }
1066
136
    return random_set_string(&dgbl->rng_name, drbg)
1067
136
        && random_set_string(&dgbl->rng_propq, propq)
1068
136
        && random_set_string(&dgbl->rng_cipher, cipher)
1069
136
        && random_set_string(&dgbl->rng_digest, digest);
1070
136
}
1071
1072
int RAND_set_seed_source_type(OSSL_LIB_CTX *ctx, const char *seed,
1073
    const char *propq)
1074
0
{
1075
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1076
1077
0
    if (dgbl == NULL)
1078
0
        return 0;
1079
0
    if (dgbl->seed != NULL) {
1080
0
        ERR_raise(ERR_LIB_RAND, RAND_R_ALREADY_INSTANTIATED);
1081
0
        return 0;
1082
0
    }
1083
0
    return random_set_string(&dgbl->seed_name, seed)
1084
0
        && random_set_string(&dgbl->seed_propq, propq);
1085
0
}
1086
1087
int RAND_set1_random_provider(OSSL_LIB_CTX *ctx, 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
0
    if (prov == NULL) {
1095
0
        OPENSSL_free(dgbl->random_provider_name);
1096
0
        dgbl->random_provider_name = NULL;
1097
0
        dgbl->random_provider = NULL;
1098
0
        return 1;
1099
0
    }
1100
1101
0
    if (dgbl->random_provider == prov)
1102
0
        return 1;
1103
1104
0
    if (!set_random_provider_name(dgbl, OSSL_PROVIDER_get0_name(prov)))
1105
0
        return 0;
1106
1107
0
    dgbl->random_provider = prov;
1108
0
    return 1;
1109
0
}
1110
1111
/*
1112
 * When a new provider is loaded, we need to check to see if it is the
1113
 * designated randomness provider and register it if it is.
1114
 */
1115
int ossl_rand_check_random_provider_on_load(OSSL_LIB_CTX *ctx,
1116
    OSSL_PROVIDER *prov)
1117
0
{
1118
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1119
1120
0
    if (dgbl == NULL)
1121
0
        return 0;
1122
1123
    /* No random provider name specified, or one is installed already */
1124
0
    if (dgbl->random_provider_name == NULL || dgbl->random_provider != NULL)
1125
0
        return 1;
1126
1127
    /* Does this provider match the name we're using? */
1128
0
    if (strcmp(dgbl->random_provider_name, OSSL_PROVIDER_get0_name(prov)) != 0)
1129
0
        return 1;
1130
1131
0
    dgbl->random_provider = prov;
1132
0
    return 1;
1133
0
}
1134
1135
/*
1136
 * When a provider is being unloaded, if it is the randomness provider,
1137
 * we need to deregister it.
1138
 */
1139
int ossl_rand_check_random_provider_on_unload(OSSL_LIB_CTX *ctx,
1140
    OSSL_PROVIDER *prov)
1141
0
{
1142
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1143
1144
0
    if (dgbl == NULL)
1145
0
        return 0;
1146
1147
0
    if (dgbl->random_provider == prov)
1148
0
        dgbl->random_provider = NULL;
1149
0
    return 1;
1150
0
}
1151
1152
#endif /* !FIPS_MODULE */