Coverage Report

Created: 2026-06-18 06:34

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