Coverage Report

Created: 2026-03-09 06:55

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-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 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
236
0
    if (!CRYPTO_THREAD_write_lock(rand_meth_lock))
237
0
        return 0;
238
0
    default_RAND_meth = meth;
239
0
    CRYPTO_THREAD_unlock(rand_meth_lock);
240
0
    return 1;
241
0
}
242
243
int RAND_set_rand_method(const RAND_METHOD *meth)
244
0
{
245
0
    return rand_set_rand_method_internal(meth, NULL);
246
0
}
247
248
const RAND_METHOD *RAND_get_rand_method(void)
249
128
{
250
128
    const RAND_METHOD *tmp_meth = NULL;
251
252
128
    if (!RUN_ONCE(&rand_init, do_rand_init))
253
0
        return NULL;
254
255
128
    if (rand_meth_lock == NULL)
256
0
        return NULL;
257
258
128
    if (!CRYPTO_THREAD_read_lock(rand_meth_lock))
259
0
        return NULL;
260
128
    tmp_meth = default_RAND_meth;
261
128
    CRYPTO_THREAD_unlock(rand_meth_lock);
262
128
    if (tmp_meth != NULL)
263
112
        return tmp_meth;
264
265
16
    if (!CRYPTO_THREAD_write_lock(rand_meth_lock))
266
0
        return NULL;
267
16
    if (default_RAND_meth == NULL)
268
16
        default_RAND_meth = &ossl_rand_meth;
269
16
    tmp_meth = default_RAND_meth;
270
16
    CRYPTO_THREAD_unlock(rand_meth_lock);
271
16
    return tmp_meth;
272
16
}
273
#endif /* OPENSSL_NO_DEPRECATED_3_0 */
274
275
void RAND_seed(const void *buf, int num)
276
32
{
277
32
    EVP_RAND_CTX *drbg;
278
32
#ifndef OPENSSL_NO_DEPRECATED_3_0
279
32
    const RAND_METHOD *meth = RAND_get_rand_method();
280
281
32
    if (meth != NULL && meth->seed != NULL) {
282
32
        meth->seed(buf, num);
283
32
        return;
284
32
    }
285
0
#endif
286
287
0
    drbg = RAND_get0_primary(NULL);
288
0
    if (drbg != NULL && num > 0)
289
0
        EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num);
290
0
}
291
292
void RAND_add(const void *buf, int num, double randomness)
293
0
{
294
0
    EVP_RAND_CTX *drbg;
295
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
296
0
    const RAND_METHOD *meth = RAND_get_rand_method();
297
298
0
    if (meth != NULL && meth->add != NULL) {
299
0
        meth->add(buf, num, randomness);
300
0
        return;
301
0
    }
302
0
#endif
303
0
    drbg = RAND_get0_primary(NULL);
304
0
    if (drbg != NULL && num > 0)
305
#ifdef OPENSSL_RAND_SEED_NONE
306
        /* Without an entropy source, we have to rely on the user */
307
        EVP_RAND_reseed(drbg, 0, buf, num, NULL, 0);
308
#else
309
        /* With an entropy source, we downgrade this to additional input */
310
0
        EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num);
311
0
#endif
312
0
}
313
314
#if !defined(OPENSSL_NO_DEPRECATED_1_1_0)
315
int RAND_pseudo_bytes(unsigned char *buf, int num)
316
0
{
317
0
    const RAND_METHOD *meth = RAND_get_rand_method();
318
319
0
    if (meth != NULL && meth->pseudorand != NULL)
320
0
        return meth->pseudorand(buf, num);
321
0
    ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
322
0
    return -1;
323
0
}
324
#endif
325
326
int RAND_status(void)
327
16
{
328
16
    EVP_RAND_CTX *rand;
329
16
#ifndef OPENSSL_NO_DEPRECATED_3_0
330
16
    const RAND_METHOD *meth = RAND_get_rand_method();
331
332
16
    if (meth != NULL && meth != RAND_OpenSSL())
333
0
        return meth->status != NULL ? meth->status() : 0;
334
16
#endif
335
336
16
    if ((rand = RAND_get0_primary(NULL)) == NULL)
337
0
        return 0;
338
16
    return EVP_RAND_get_state(rand) == EVP_RAND_STATE_READY;
339
16
}
340
#else /* !FIPS_MODULE */
341
342
#ifndef OPENSSL_NO_DEPRECATED_3_0
343
const RAND_METHOD *RAND_get_rand_method(void)
344
{
345
    return NULL;
346
}
347
#endif
348
#endif /* !FIPS_MODULE */
349
350
/*
351
 * This function is not part of RAND_METHOD, so if we're not using
352
 * the default method, then just call RAND_bytes().  Otherwise make
353
 * sure we're instantiated and use the private DRBG.
354
 */
355
int RAND_priv_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
356
    unsigned int strength)
357
0
{
358
0
    RAND_GLOBAL *dgbl;
359
0
    EVP_RAND_CTX *rand;
360
0
#if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)
361
0
    const RAND_METHOD *meth = RAND_get_rand_method();
362
363
0
    if (meth != NULL && meth != RAND_OpenSSL()) {
364
0
        if (num > INT_MAX) {
365
0
            ERR_raise(ERR_LIB_RAND, RAND_R_ARGUMENT_OUT_OF_RANGE);
366
0
            return -1;
367
0
        }
368
0
        if (meth->bytes != NULL)
369
0
            return meth->bytes(buf, (int)num);
370
0
        ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
371
0
        return -1;
372
0
    }
373
0
#endif
374
375
0
    dgbl = rand_get_global(ctx);
376
0
    if (dgbl == NULL)
377
0
        return 0;
378
0
#ifndef FIPS_MODULE
379
0
    if (dgbl->random_provider != NULL)
380
0
        return ossl_provider_random_bytes(dgbl->random_provider,
381
0
            OSSL_PROV_RANDOM_PRIVATE,
382
0
            buf, num, strength);
383
0
#endif /* !FIPS_MODULE */
384
0
    rand = rand_get0_private(ctx, dgbl);
385
0
    if (rand != NULL)
386
0
        return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);
387
388
0
    return 0;
389
0
}
390
391
int RAND_priv_bytes(unsigned char *buf, int num)
392
0
{
393
0
    if (num < 0)
394
0
        return 0;
395
0
    return RAND_priv_bytes_ex(NULL, buf, (size_t)num, 0);
396
0
}
397
398
int RAND_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num,
399
    unsigned int strength)
400
32
{
401
32
    RAND_GLOBAL *dgbl;
402
32
    EVP_RAND_CTX *rand;
403
32
#if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)
404
32
    const RAND_METHOD *meth = RAND_get_rand_method();
405
406
32
    if (meth != NULL && meth != RAND_OpenSSL()) {
407
0
        if (num > INT_MAX) {
408
0
            ERR_raise(ERR_LIB_RAND, RAND_R_ARGUMENT_OUT_OF_RANGE);
409
0
            return -1;
410
0
        }
411
0
        if (meth->bytes != NULL)
412
0
            return meth->bytes(buf, (int)num);
413
0
        ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED);
414
0
        return -1;
415
0
    }
416
32
#endif
417
418
32
    dgbl = rand_get_global(ctx);
419
32
    if (dgbl == NULL)
420
0
        return 0;
421
32
#ifndef FIPS_MODULE
422
32
    if (dgbl->random_provider != NULL)
423
0
        return ossl_provider_random_bytes(dgbl->random_provider,
424
0
            OSSL_PROV_RANDOM_PUBLIC,
425
0
            buf, num, strength);
426
32
#endif /* !FIPS_MODULE */
427
428
32
    rand = rand_get0_public(ctx, dgbl);
429
32
    if (rand != NULL)
430
32
        return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);
431
432
0
    return 0;
433
32
}
434
435
int RAND_bytes(unsigned char *buf, int num)
436
32
{
437
32
    if (num < 0)
438
0
        return 0;
439
32
    return RAND_bytes_ex(NULL, buf, (size_t)num, 0);
440
32
}
441
442
/*
443
 * Initialize the OSSL_LIB_CTX global DRBGs on first use.
444
 * Returns the allocated global data on success or NULL on failure.
445
 */
446
void *ossl_rand_ctx_new(OSSL_LIB_CTX *libctx)
447
16
{
448
16
    RAND_GLOBAL *dgbl = OPENSSL_zalloc(sizeof(*dgbl));
449
450
16
    if (dgbl == NULL)
451
0
        return NULL;
452
453
16
#ifndef FIPS_MODULE
454
    /*
455
     * We need to ensure that base libcrypto thread handling has been
456
     * initialised.
457
     */
458
16
    OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL);
459
460
    /* Prepopulate the random provider name */
461
16
    dgbl->random_provider_name = OPENSSL_strdup(random_provider_fips_name);
462
16
    if (dgbl->random_provider_name == NULL)
463
0
        goto err0;
464
16
#endif
465
466
16
    dgbl->lock = CRYPTO_THREAD_lock_new();
467
16
    if (dgbl->lock == NULL)
468
0
        goto err1;
469
470
16
    return dgbl;
471
472
0
err1:
473
0
    CRYPTO_THREAD_lock_free(dgbl->lock);
474
0
#ifndef FIPS_MODULE
475
0
err0:
476
0
    OPENSSL_free(dgbl->random_provider_name);
477
0
#endif
478
0
    OPENSSL_free(dgbl);
479
0
    return NULL;
480
0
}
481
482
void ossl_rand_ctx_free(void *vdgbl)
483
0
{
484
0
    RAND_GLOBAL *dgbl = vdgbl;
485
486
0
    if (dgbl == NULL)
487
0
        return;
488
489
0
    CRYPTO_THREAD_lock_free(dgbl->lock);
490
0
    EVP_RAND_CTX_free(dgbl->primary);
491
0
    EVP_RAND_CTX_free(dgbl->seed);
492
0
#ifndef FIPS_MODULE
493
0
    OPENSSL_free(dgbl->random_provider_name);
494
0
#endif /* !FIPS_MODULE */
495
0
    OPENSSL_free(dgbl->rng_name);
496
0
    OPENSSL_free(dgbl->rng_cipher);
497
0
    OPENSSL_free(dgbl->rng_digest);
498
0
    OPENSSL_free(dgbl->rng_propq);
499
0
    OPENSSL_free(dgbl->seed_name);
500
0
    OPENSSL_free(dgbl->seed_propq);
501
502
0
    OPENSSL_free(dgbl);
503
0
}
504
505
static void rand_delete_thread_state(void *arg)
506
0
{
507
0
    OSSL_LIB_CTX *ctx = arg;
508
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
509
0
    EVP_RAND_CTX *rand;
510
511
0
    if (dgbl == NULL)
512
0
        return;
513
514
0
    rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
515
0
    CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, NULL);
516
0
    EVP_RAND_CTX_free(rand);
517
518
0
    rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
519
0
    CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, NULL);
520
0
    EVP_RAND_CTX_free(rand);
521
0
}
522
523
#if !defined(FIPS_MODULE) || !defined(OPENSSL_NO_FIPS_JITTER)
524
static EVP_RAND_CTX *rand_new_seed(OSSL_LIB_CTX *libctx)
525
16
{
526
16
    EVP_RAND *rand;
527
16
    const char *propq;
528
16
    char *name;
529
16
    EVP_RAND_CTX *ctx = NULL;
530
16
    int fallback = 0;
531
16
#ifdef OPENSSL_NO_FIPS_JITTER
532
16
    RAND_GLOBAL *dgbl = rand_get_global(libctx);
533
534
16
    if (dgbl == NULL)
535
0
        return NULL;
536
16
    propq = dgbl->seed_propq;
537
16
    if (dgbl->seed_name != NULL) {
538
0
        name = dgbl->seed_name;
539
16
    } else {
540
16
        fallback = 1;
541
16
        name = OPENSSL_MSTR(OPENSSL_DEFAULT_SEED_SRC);
542
16
    }
543
#else /* !OPENSSL_NO_FIPS_JITTER */
544
    name = "JITTER";
545
    propq = "";
546
#endif /* OPENSSL_NO_FIPS_JITTER */
547
548
16
    ERR_set_mark();
549
16
    rand = EVP_RAND_fetch(libctx, name, propq);
550
16
    ERR_pop_to_mark();
551
16
    if (rand == NULL) {
552
0
        if (!fallback)
553
0
            ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
554
0
        goto err;
555
0
    }
556
16
    ctx = EVP_RAND_CTX_new(rand, NULL);
557
16
    EVP_RAND_free(rand);
558
16
    if (ctx == NULL) {
559
0
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
560
0
        goto err;
561
0
    }
562
16
    if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) {
563
0
        ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
564
0
        goto err;
565
0
    }
566
16
    return ctx;
567
0
err:
568
0
    EVP_RAND_CTX_free(ctx);
569
0
    return NULL;
570
16
}
571
#endif /* !FIPS_MODULE || !OPENSSL_NO_FIPS_JITTER */
572
573
#ifndef FIPS_MODULE
574
EVP_RAND_CTX *ossl_rand_get0_seed_noncreating(OSSL_LIB_CTX *ctx)
575
0
{
576
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
577
0
    EVP_RAND_CTX *ret;
578
579
0
    if (dgbl == NULL)
580
0
        return NULL;
581
582
0
    if (!CRYPTO_THREAD_read_lock(dgbl->lock))
583
0
        return NULL;
584
0
    ret = dgbl->seed;
585
0
    CRYPTO_THREAD_unlock(dgbl->lock);
586
0
    return ret;
587
0
}
588
#endif /* !FIPS_MODULE */
589
590
static EVP_RAND_CTX *rand_new_drbg(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent,
591
    unsigned int reseed_interval,
592
    time_t reseed_time_interval)
593
32
{
594
32
    EVP_RAND *rand;
595
32
    RAND_GLOBAL *dgbl = rand_get_global(libctx);
596
32
    EVP_RAND_CTX *ctx;
597
32
    OSSL_PARAM params[9], *p = params;
598
32
    const OSSL_PARAM *settables;
599
32
    const char *prov_name;
600
32
    char *name, *cipher;
601
32
    int use_df = 1;
602
603
32
    if (dgbl == NULL)
604
0
        return NULL;
605
32
    name = dgbl->rng_name != NULL ? dgbl->rng_name : "CTR-DRBG";
606
32
    rand = EVP_RAND_fetch(libctx, name, dgbl->rng_propq);
607
32
    if (rand == NULL) {
608
0
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
609
0
        return NULL;
610
0
    }
611
32
    prov_name = ossl_provider_name(EVP_RAND_get0_provider(rand));
612
32
    ctx = EVP_RAND_CTX_new(rand, parent);
613
32
    EVP_RAND_free(rand);
614
32
    if (ctx == NULL) {
615
0
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
616
0
        return NULL;
617
0
    }
618
619
32
    settables = EVP_RAND_CTX_settable_params(ctx);
620
32
    if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_CIPHER)) {
621
32
        cipher = dgbl->rng_cipher != NULL ? dgbl->rng_cipher : "AES-256-CTR";
622
32
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
623
32
            cipher, 0);
624
32
    }
625
32
    if (dgbl->rng_digest != NULL
626
0
        && OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_DIGEST))
627
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
628
0
            dgbl->rng_digest, 0);
629
32
    if (prov_name != NULL)
630
32
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_CORE_PROV_NAME,
631
32
            (char *)prov_name, 0);
632
32
    if (dgbl->rng_propq != NULL)
633
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_PROPERTIES,
634
0
            dgbl->rng_propq, 0);
635
32
    if (OSSL_PARAM_locate_const(settables, OSSL_ALG_PARAM_MAC))
636
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_MAC, "HMAC", 0);
637
32
    if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_USE_DF))
638
32
        *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &use_df);
639
32
    *p++ = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS,
640
32
        &reseed_interval);
641
32
    *p++ = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL,
642
32
        &reseed_time_interval);
643
32
    *p = OSSL_PARAM_construct_end();
644
32
    if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, params)) {
645
0
        ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
646
0
        EVP_RAND_CTX_free(ctx);
647
0
        return NULL;
648
0
    }
649
32
    return ctx;
650
32
}
651
652
#if defined(FIPS_MODULE)
653
static EVP_RAND_CTX *rand_new_crngt(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent)
654
{
655
    EVP_RAND *rand;
656
    EVP_RAND_CTX *ctx;
657
658
    rand = EVP_RAND_fetch(libctx, "CRNG-TEST", "-fips");
659
    if (rand == NULL) {
660
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
661
        return NULL;
662
    }
663
    ctx = EVP_RAND_CTX_new(rand, parent);
664
    EVP_RAND_free(rand);
665
    if (ctx == NULL) {
666
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
667
        return NULL;
668
    }
669
670
    if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) {
671
        ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
672
        EVP_RAND_CTX_free(ctx);
673
        return NULL;
674
    }
675
    return ctx;
676
}
677
#endif /* FIPS_MODULE */
678
679
/*
680
 * Get the primary random generator.
681
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
682
 *
683
 */
684
static EVP_RAND_CTX *rand_get0_primary(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
685
64
{
686
64
    EVP_RAND_CTX *ret, *seed, *newseed = NULL, *primary;
687
688
64
    if (dgbl == NULL)
689
0
        return NULL;
690
691
64
    if (!CRYPTO_THREAD_read_lock(dgbl->lock))
692
0
        return NULL;
693
694
64
    ret = dgbl->primary;
695
64
    seed = dgbl->seed;
696
64
    CRYPTO_THREAD_unlock(dgbl->lock);
697
698
64
    if (ret != NULL)
699
48
        return ret;
700
701
16
#if !defined(FIPS_MODULE) || !defined(OPENSSL_NO_FIPS_JITTER)
702
    /* Create a seed source for libcrypto or jitter enabled FIPS provider */
703
16
    if (seed == NULL) {
704
16
        ERR_set_mark();
705
16
        seed = newseed = rand_new_seed(ctx);
706
16
        if (ERR_count_to_mark() > 0) {
707
0
            EVP_RAND_CTX_free(newseed);
708
0
            ERR_clear_last_mark();
709
0
            return NULL;
710
0
        }
711
16
        ERR_pop_to_mark();
712
16
    }
713
16
#endif /* !FIPS_MODULE || !OPENSSL_NO_FIPS_JITTER */
714
715
#if defined(FIPS_MODULE)
716
    /* The FIPS provider has entropy health tests instead of the primary */
717
    ret = rand_new_crngt(ctx, seed);
718
#else /* FIPS_MODULE */
719
16
    ret = rand_new_drbg(ctx, seed, PRIMARY_RESEED_INTERVAL,
720
16
        PRIMARY_RESEED_TIME_INTERVAL);
721
16
#endif /* FIPS_MODULE */
722
723
    /*
724
     * The primary DRBG may be shared between multiple threads so we must
725
     * enable locking.
726
     */
727
16
    if (ret == NULL || !EVP_RAND_enable_locking(ret)) {
728
0
        if (ret != NULL) {
729
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_ENABLE_LOCKING);
730
0
            EVP_RAND_CTX_free(ret);
731
0
        }
732
0
        if (newseed == NULL)
733
0
            return NULL;
734
        /* else carry on and store seed */
735
0
        ret = NULL;
736
0
    }
737
738
16
    if (!CRYPTO_THREAD_write_lock(dgbl->lock))
739
0
        return NULL;
740
741
16
    primary = dgbl->primary;
742
16
    if (primary != NULL) {
743
0
        CRYPTO_THREAD_unlock(dgbl->lock);
744
0
        EVP_RAND_CTX_free(ret);
745
0
        EVP_RAND_CTX_free(newseed);
746
0
        return primary;
747
0
    }
748
16
    if (newseed != NULL)
749
16
        dgbl->seed = newseed;
750
16
    dgbl->primary = ret;
751
16
    CRYPTO_THREAD_unlock(dgbl->lock);
752
753
16
    return ret;
754
16
}
755
756
/*
757
 * Get the primary random generator.
758
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
759
 *
760
 */
761
EVP_RAND_CTX *RAND_get0_primary(OSSL_LIB_CTX *ctx)
762
48
{
763
48
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
764
765
48
    return dgbl == NULL ? NULL : rand_get0_primary(ctx, dgbl);
766
48
}
767
768
static EVP_RAND_CTX *rand_get0_public(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
769
32
{
770
32
    EVP_RAND_CTX *rand, *primary;
771
32
    OSSL_LIB_CTX *origctx = ctx;
772
773
32
    ctx = ossl_lib_ctx_get_concrete(ctx);
774
775
32
    if (ctx == NULL)
776
0
        return NULL;
777
778
32
    if (dgbl == NULL)
779
0
        return NULL;
780
781
32
    rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
782
32
    if (rand == NULL) {
783
16
        primary = rand_get0_primary(origctx, dgbl);
784
16
        if (primary == NULL)
785
0
            return NULL;
786
787
        /*
788
         * If the private is also NULL then this is the first time we've
789
         * used this thread.
790
         */
791
16
        if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx) == NULL
792
16
            && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
793
0
            return NULL;
794
16
        rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
795
16
            SECONDARY_RESEED_TIME_INTERVAL);
796
16
        if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand)) {
797
0
            EVP_RAND_CTX_free(rand);
798
0
            rand = NULL;
799
0
        }
800
16
    }
801
32
    return rand;
802
32
}
803
804
/*
805
 * Get the public random generator.
806
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
807
 */
808
EVP_RAND_CTX *RAND_get0_public(OSSL_LIB_CTX *ctx)
809
0
{
810
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
811
812
0
    return dgbl == NULL ? NULL : rand_get0_public(ctx, dgbl);
813
0
}
814
815
static EVP_RAND_CTX *rand_get0_private(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
816
0
{
817
0
    EVP_RAND_CTX *rand, *primary;
818
0
    OSSL_LIB_CTX *origctx = ctx;
819
820
0
    ctx = ossl_lib_ctx_get_concrete(ctx);
821
0
    if (ctx == NULL)
822
0
        return NULL;
823
824
0
    rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
825
0
    if (rand == NULL) {
826
0
        primary = rand_get0_primary(origctx, dgbl);
827
0
        if (primary == NULL)
828
0
            return NULL;
829
830
        /*
831
         * If the public is also NULL then this is the first time we've
832
         * used this thread.
833
         */
834
0
        if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx) == NULL
835
0
            && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
836
0
            return NULL;
837
0
        rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
838
0
            SECONDARY_RESEED_TIME_INTERVAL);
839
0
        if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand)) {
840
0
            EVP_RAND_CTX_free(rand);
841
0
            rand = NULL;
842
0
        }
843
0
    }
844
0
    return rand;
845
0
}
846
847
/*
848
 * Get the private random generator.
849
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
850
 */
851
EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx)
852
0
{
853
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
854
855
0
    return dgbl == NULL ? NULL : rand_get0_private(ctx, dgbl);
856
0
}
857
858
#ifdef FIPS_MODULE
859
EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx)
860
{
861
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
862
863
    if (dgbl == NULL)
864
        return NULL;
865
866
    return CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
867
}
868
#endif
869
870
int RAND_set0_public(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
871
0
{
872
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
873
0
    EVP_RAND_CTX *old;
874
0
    int r;
875
876
0
    if (dgbl == NULL)
877
0
        return 0;
878
0
    old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
879
0
    if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand)) > 0)
880
0
        EVP_RAND_CTX_free(old);
881
0
    return r;
882
0
}
883
884
int RAND_set0_private(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
885
0
{
886
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
887
0
    EVP_RAND_CTX *old;
888
0
    int r;
889
890
0
    if (dgbl == NULL)
891
0
        return 0;
892
0
    old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
893
0
    if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand)) > 0)
894
0
        EVP_RAND_CTX_free(old);
895
0
    return r;
896
0
}
897
898
#ifndef FIPS_MODULE
899
static int random_set_string(char **p, const char *s)
900
0
{
901
0
    char *d = NULL;
902
903
0
    if (s != NULL) {
904
0
        d = OPENSSL_strdup(s);
905
0
        if (d == NULL)
906
0
            return 0;
907
0
    }
908
0
    OPENSSL_free(*p);
909
0
    *p = d;
910
0
    return 1;
911
0
}
912
913
/*
914
 * Load the DRBG definitions from a configuration file.
915
 */
916
static int random_conf_init(CONF_IMODULE *md, const CONF *cnf)
917
0
{
918
0
    STACK_OF(CONF_VALUE) *elist;
919
0
    CONF_VALUE *cval;
920
0
    OSSL_LIB_CTX *libctx = NCONF_get0_libctx((CONF *)cnf);
921
0
    RAND_GLOBAL *dgbl = rand_get_global(libctx);
922
0
    int i, r = 1;
923
924
0
    OSSL_TRACE1(CONF, "Loading random module: section %s\n",
925
0
        CONF_imodule_get_value(md));
926
927
    /* Value is a section containing RANDOM configuration */
928
0
    elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
929
0
    if (elist == NULL) {
930
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_RANDOM_SECTION_ERROR);
931
0
        return 0;
932
0
    }
933
934
0
    if (dgbl == NULL)
935
0
        return 0;
936
937
0
    for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
938
0
        cval = sk_CONF_VALUE_value(elist, i);
939
0
        if (OPENSSL_strcasecmp(cval->name, "random") == 0) {
940
0
            if (!random_set_string(&dgbl->rng_name, cval->value))
941
0
                return 0;
942
0
        } else if (OPENSSL_strcasecmp(cval->name, "cipher") == 0) {
943
0
            if (!random_set_string(&dgbl->rng_cipher, cval->value))
944
0
                return 0;
945
0
        } else if (OPENSSL_strcasecmp(cval->name, "digest") == 0) {
946
0
            if (!random_set_string(&dgbl->rng_digest, cval->value))
947
0
                return 0;
948
0
        } else if (OPENSSL_strcasecmp(cval->name, "properties") == 0) {
949
0
            if (!random_set_string(&dgbl->rng_propq, cval->value))
950
0
                return 0;
951
0
        } else if (OPENSSL_strcasecmp(cval->name, "seed") == 0) {
952
0
            if (!random_set_string(&dgbl->seed_name, cval->value))
953
0
                return 0;
954
0
        } else if (OPENSSL_strcasecmp(cval->name, "seed_properties") == 0) {
955
0
            if (!random_set_string(&dgbl->seed_propq, cval->value))
956
0
                return 0;
957
0
        } else if (OPENSSL_strcasecmp(cval->name, "random_provider") == 0) {
958
0
#ifndef FIPS_MODULE
959
0
            OSSL_PROVIDER *prov = ossl_provider_find(libctx, cval->value, 0);
960
961
0
            if (prov != NULL) {
962
0
                if (!RAND_set1_random_provider(libctx, prov)) {
963
0
                    ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
964
0
                    OSSL_PROVIDER_unload(prov);
965
0
                    return 0;
966
0
                }
967
                /*
968
                 * We need to release the reference from ossl_provider_find because
969
                 * we don't want to keep a reference counted handle to the provider.
970
                 *
971
                 * The provider unload code checks for the random provider and,
972
                 * if present, our reference will be NULLed when it is fully freed.
973
                 * The provider load code, conversely, checks the provider name
974
                 * and re-hooks our reference if required.  This means that a load,
975
                 * hook random provider, use, unload, reload, reuse sequence will
976
                 * work as expected.
977
                 */
978
0
                OSSL_PROVIDER_unload(prov);
979
0
            } else if (!set_random_provider_name(dgbl, cval->value))
980
0
                return 0;
981
0
#endif
982
0
        } else {
983
0
            ERR_raise_data(ERR_LIB_CRYPTO,
984
0
                CRYPTO_R_UNKNOWN_NAME_IN_RANDOM_SECTION,
985
0
                "name=%s, value=%s", cval->name, cval->value);
986
0
            r = 0;
987
0
        }
988
0
    }
989
0
    return r;
990
0
}
991
992
static void random_conf_deinit(CONF_IMODULE *md)
993
0
{
994
0
    OSSL_TRACE(CONF, "Cleaned up random\n");
995
0
}
996
997
void ossl_random_add_conf_module(void)
998
0
{
999
0
    OSSL_TRACE(CONF, "Adding config module 'random'\n");
1000
0
    CONF_module_add("random", random_conf_init, random_conf_deinit);
1001
0
}
1002
1003
int RAND_set_DRBG_type(OSSL_LIB_CTX *ctx, const char *drbg, const char *propq,
1004
    const char *cipher, const char *digest)
1005
0
{
1006
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1007
1008
0
    if (dgbl == NULL)
1009
0
        return 0;
1010
0
    if (dgbl->primary != NULL) {
1011
0
        ERR_raise(ERR_LIB_RAND, RAND_R_ALREADY_INSTANTIATED);
1012
0
        return 0;
1013
0
    }
1014
0
    return random_set_string(&dgbl->rng_name, drbg)
1015
0
        && random_set_string(&dgbl->rng_propq, propq)
1016
0
        && random_set_string(&dgbl->rng_cipher, cipher)
1017
0
        && random_set_string(&dgbl->rng_digest, digest);
1018
0
}
1019
1020
int RAND_set_seed_source_type(OSSL_LIB_CTX *ctx, const char *seed,
1021
    const char *propq)
1022
0
{
1023
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1024
1025
0
    if (dgbl == NULL)
1026
0
        return 0;
1027
0
    if (dgbl->seed != NULL) {
1028
0
        ERR_raise(ERR_LIB_RAND, RAND_R_ALREADY_INSTANTIATED);
1029
0
        return 0;
1030
0
    }
1031
0
    return random_set_string(&dgbl->seed_name, seed)
1032
0
        && random_set_string(&dgbl->seed_propq, propq);
1033
0
}
1034
1035
int RAND_set1_random_provider(OSSL_LIB_CTX *ctx, OSSL_PROVIDER *prov)
1036
0
{
1037
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1038
1039
0
    if (dgbl == NULL)
1040
0
        return 0;
1041
1042
0
    if (prov == NULL) {
1043
0
        OPENSSL_free(dgbl->random_provider_name);
1044
0
        dgbl->random_provider_name = NULL;
1045
0
        dgbl->random_provider = NULL;
1046
0
        return 1;
1047
0
    }
1048
1049
0
    if (dgbl->random_provider == prov)
1050
0
        return 1;
1051
1052
0
    if (!set_random_provider_name(dgbl, OSSL_PROVIDER_get0_name(prov)))
1053
0
        return 0;
1054
1055
0
    dgbl->random_provider = prov;
1056
0
    return 1;
1057
0
}
1058
1059
/*
1060
 * When a new provider is loaded, we need to check to see if it is the
1061
 * designated randomness provider and register it if it is.
1062
 */
1063
int ossl_rand_check_random_provider_on_load(OSSL_LIB_CTX *ctx,
1064
    OSSL_PROVIDER *prov)
1065
0
{
1066
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1067
1068
0
    if (dgbl == NULL)
1069
0
        return 0;
1070
1071
    /* No random provider name specified, or one is installed already */
1072
0
    if (dgbl->random_provider_name == NULL || dgbl->random_provider != NULL)
1073
0
        return 1;
1074
1075
    /* Does this provider match the name we're using? */
1076
0
    if (strcmp(dgbl->random_provider_name, OSSL_PROVIDER_get0_name(prov)) != 0)
1077
0
        return 1;
1078
1079
0
    dgbl->random_provider = prov;
1080
0
    return 1;
1081
0
}
1082
1083
/*
1084
 * When a provider is being unloaded, if it is the randomness provider,
1085
 * we need to deregister it.
1086
 */
1087
int ossl_rand_check_random_provider_on_unload(OSSL_LIB_CTX *ctx,
1088
    OSSL_PROVIDER *prov)
1089
0
{
1090
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1091
1092
0
    if (dgbl == NULL)
1093
0
        return 0;
1094
1095
0
    if (dgbl->random_provider == prov)
1096
0
        dgbl->random_provider = NULL;
1097
0
    return 1;
1098
0
}
1099
1100
#endif /* !FIPS_MODULE */