Coverage Report

Created: 2025-07-11 06:57

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