Coverage Report

Created: 2025-12-10 06:24

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
0
{
85
0
    return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_INDEX);
86
0
}
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
0
{
127
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
128
0
    rand_meth_lock = CRYPTO_THREAD_lock_new();
129
0
    if (rand_meth_lock == NULL)
130
0
        goto err;
131
0
#endif /* !OPENSSL_NO_DEPRECATED_3_0 */
132
133
0
    if (!ossl_rand_pool_init())
134
0
        goto err;
135
136
0
    rand_inited = 1;
137
0
    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
0
}
146
147
void ossl_rand_cleanup_int(void)
148
3
{
149
3
#ifndef OPENSSL_NO_DEPRECATED_3_0
150
3
    const RAND_METHOD *meth = default_RAND_meth;
151
152
3
    if (!rand_inited)
153
3
        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
0
{
188
0
    static const char salt[] = "polling";
189
190
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
191
0
    const RAND_METHOD *meth = RAND_get_rand_method();
192
0
    int ret = meth == RAND_OpenSSL();
193
194
0
    if (meth == NULL)
195
0
        return 0;
196
197
0
    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
0
#endif /* !OPENSSL_NO_DEPRECATED_3_0 */
222
223
0
    RAND_seed(salt, sizeof(salt));
224
0
    return 1;
225
0
}
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
0
{
250
0
    const RAND_METHOD *tmp_meth = NULL;
251
252
0
    if (!RUN_ONCE(&rand_init, do_rand_init))
253
0
        return NULL;
254
255
0
    if (rand_meth_lock == NULL)
256
0
        return NULL;
257
258
0
    if (!CRYPTO_THREAD_read_lock(rand_meth_lock))
259
0
        return NULL;
260
0
    tmp_meth = default_RAND_meth;
261
0
    CRYPTO_THREAD_unlock(rand_meth_lock);
262
0
    if (tmp_meth != NULL)
263
0
        return tmp_meth;
264
265
0
    if (!CRYPTO_THREAD_write_lock(rand_meth_lock))
266
0
        return NULL;
267
0
    if (default_RAND_meth == NULL)
268
0
        default_RAND_meth = &ossl_rand_meth;
269
0
    tmp_meth = default_RAND_meth;
270
0
    CRYPTO_THREAD_unlock(rand_meth_lock);
271
0
    return tmp_meth;
272
0
}
273
#endif /* OPENSSL_NO_DEPRECATED_3_0 */
274
275
void RAND_seed(const void *buf, int num)
276
0
{
277
0
    EVP_RAND_CTX *drbg;
278
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
279
0
    const RAND_METHOD *meth = RAND_get_rand_method();
280
281
0
    if (meth != NULL && meth->seed != NULL) {
282
0
        meth->seed(buf, num);
283
0
        return;
284
0
    }
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
0
{
328
0
    EVP_RAND_CTX *rand;
329
0
#ifndef OPENSSL_NO_DEPRECATED_3_0
330
0
    const RAND_METHOD *meth = RAND_get_rand_method();
331
332
0
    if (meth != NULL && meth != RAND_OpenSSL())
333
0
        return meth->status != NULL ? meth->status() : 0;
334
0
#endif
335
336
0
    if ((rand = RAND_get0_primary(NULL)) == NULL)
337
0
        return 0;
338
0
    return EVP_RAND_get_state(rand) == EVP_RAND_STATE_READY;
339
0
}
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
0
{
401
0
    RAND_GLOBAL *dgbl;
402
0
    EVP_RAND_CTX *rand;
403
0
#if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE)
404
0
    const RAND_METHOD *meth = RAND_get_rand_method();
405
406
0
    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
0
#endif
417
418
0
    dgbl = rand_get_global(ctx);
419
0
    if (dgbl == NULL)
420
0
        return 0;
421
0
#ifndef FIPS_MODULE
422
0
    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
0
#endif /* !FIPS_MODULE */
427
428
0
    rand = rand_get0_public(ctx, dgbl);
429
0
    if (rand != NULL)
430
0
        return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0);
431
432
0
    return 0;
433
0
}
434
435
int RAND_bytes(unsigned char *buf, int num)
436
0
{
437
0
    if (num < 0)
438
0
        return 0;
439
0
    return RAND_bytes_ex(NULL, buf, (size_t)num, 0);
440
0
}
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
9
{
448
9
    RAND_GLOBAL *dgbl = OPENSSL_zalloc(sizeof(*dgbl));
449
450
9
    if (dgbl == NULL)
451
0
        return NULL;
452
453
9
#ifndef FIPS_MODULE
454
    /*
455
     * We need to ensure that base libcrypto thread handling has been
456
     * initialised.
457
     */
458
9
    OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL);
459
460
    /* Prepopulate the random provider name */
461
9
    dgbl->random_provider_name = OPENSSL_strdup(random_provider_fips_name);
462
9
    if (dgbl->random_provider_name == NULL)
463
0
        goto err0;
464
9
#endif
465
466
9
    dgbl->lock = CRYPTO_THREAD_lock_new();
467
9
    if (dgbl->lock == NULL)
468
0
        goto err1;
469
470
9
    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
3
{
484
3
    RAND_GLOBAL *dgbl = vdgbl;
485
486
3
    if (dgbl == NULL)
487
0
        return;
488
489
3
    CRYPTO_THREAD_lock_free(dgbl->lock);
490
3
    EVP_RAND_CTX_free(dgbl->primary);
491
3
    EVP_RAND_CTX_free(dgbl->seed);
492
3
#ifndef FIPS_MODULE
493
3
    OPENSSL_free(dgbl->random_provider_name);
494
3
#endif /* !FIPS_MODULE */
495
3
    OPENSSL_free(dgbl->rng_name);
496
3
    OPENSSL_free(dgbl->rng_cipher);
497
3
    OPENSSL_free(dgbl->rng_digest);
498
3
    OPENSSL_free(dgbl->rng_propq);
499
3
    OPENSSL_free(dgbl->seed_name);
500
3
    OPENSSL_free(dgbl->seed_propq);
501
502
3
    OPENSSL_free(dgbl);
503
3
}
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
0
{
526
0
    EVP_RAND *rand;
527
0
    const char *propq;
528
0
    char *name;
529
0
    EVP_RAND_CTX *ctx = NULL;
530
0
#ifdef OPENSSL_NO_FIPS_JITTER
531
0
    RAND_GLOBAL *dgbl = rand_get_global(libctx);
532
533
0
    if (dgbl == NULL)
534
0
        return NULL;
535
0
    propq = dgbl->seed_propq;
536
0
    name = dgbl->seed_name != NULL ? dgbl->seed_name
537
0
                                   : OPENSSL_MSTR(OPENSSL_DEFAULT_SEED_SRC);
538
#else /* !OPENSSL_NO_FIPS_JITTER */
539
    name = "JITTER";
540
    propq = "";
541
#endif /* OPENSSL_NO_FIPS_JITTER */
542
543
0
    rand = EVP_RAND_fetch(libctx, name, propq);
544
0
    if (rand == NULL) {
545
0
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
546
0
        goto err;
547
0
    }
548
0
    ctx = EVP_RAND_CTX_new(rand, NULL);
549
0
    EVP_RAND_free(rand);
550
0
    if (ctx == NULL) {
551
0
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
552
0
        goto err;
553
0
    }
554
0
    if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) {
555
0
        ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
556
0
        goto err;
557
0
    }
558
0
    return ctx;
559
0
err:
560
0
    EVP_RAND_CTX_free(ctx);
561
0
    return NULL;
562
0
}
563
#endif /* !FIPS_MODULE || !OPENSSL_NO_FIPS_JITTER */
564
565
#ifndef FIPS_MODULE
566
EVP_RAND_CTX *ossl_rand_get0_seed_noncreating(OSSL_LIB_CTX *ctx)
567
0
{
568
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
569
0
    EVP_RAND_CTX *ret;
570
571
0
    if (dgbl == NULL)
572
0
        return NULL;
573
574
0
    if (!CRYPTO_THREAD_read_lock(dgbl->lock))
575
0
        return NULL;
576
0
    ret = dgbl->seed;
577
0
    CRYPTO_THREAD_unlock(dgbl->lock);
578
0
    return ret;
579
0
}
580
#endif /* !FIPS_MODULE */
581
582
static EVP_RAND_CTX *rand_new_drbg(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent,
583
    unsigned int reseed_interval,
584
    time_t reseed_time_interval)
585
0
{
586
0
    EVP_RAND *rand;
587
0
    RAND_GLOBAL *dgbl = rand_get_global(libctx);
588
0
    EVP_RAND_CTX *ctx;
589
0
    OSSL_PARAM params[9], *p = params;
590
0
    const OSSL_PARAM *settables;
591
0
    const char *prov_name;
592
0
    char *name, *cipher;
593
0
    int use_df = 1;
594
595
0
    if (dgbl == NULL)
596
0
        return NULL;
597
0
    name = dgbl->rng_name != NULL ? dgbl->rng_name : "CTR-DRBG";
598
0
    rand = EVP_RAND_fetch(libctx, name, dgbl->rng_propq);
599
0
    if (rand == NULL) {
600
0
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
601
0
        return NULL;
602
0
    }
603
0
    prov_name = ossl_provider_name(EVP_RAND_get0_provider(rand));
604
0
    ctx = EVP_RAND_CTX_new(rand, parent);
605
0
    EVP_RAND_free(rand);
606
0
    if (ctx == NULL) {
607
0
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
608
0
        return NULL;
609
0
    }
610
611
0
    settables = EVP_RAND_CTX_settable_params(ctx);
612
0
    if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_CIPHER)) {
613
0
        cipher = dgbl->rng_cipher != NULL ? dgbl->rng_cipher : "AES-256-CTR";
614
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER,
615
0
            cipher, 0);
616
0
    }
617
0
    if (dgbl->rng_digest != NULL
618
0
        && OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_DIGEST))
619
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST,
620
0
            dgbl->rng_digest, 0);
621
0
    if (prov_name != NULL)
622
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_CORE_PROV_NAME,
623
0
            (char *)prov_name, 0);
624
0
    if (dgbl->rng_propq != NULL)
625
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_PROPERTIES,
626
0
            dgbl->rng_propq, 0);
627
0
    if (OSSL_PARAM_locate_const(settables, OSSL_ALG_PARAM_MAC))
628
0
        *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_MAC, "HMAC", 0);
629
0
    if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_USE_DF))
630
0
        *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &use_df);
631
0
    *p++ = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS,
632
0
        &reseed_interval);
633
0
    *p++ = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL,
634
0
        &reseed_time_interval);
635
0
    *p = OSSL_PARAM_construct_end();
636
0
    if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, params)) {
637
0
        ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
638
0
        EVP_RAND_CTX_free(ctx);
639
0
        return NULL;
640
0
    }
641
0
    return ctx;
642
0
}
643
644
#if defined(FIPS_MODULE)
645
static EVP_RAND_CTX *rand_new_crngt(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent)
646
{
647
    EVP_RAND *rand;
648
    EVP_RAND_CTX *ctx;
649
650
    rand = EVP_RAND_fetch(libctx, "CRNG-TEST", "-fips");
651
    if (rand == NULL) {
652
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG);
653
        return NULL;
654
    }
655
    ctx = EVP_RAND_CTX_new(rand, parent);
656
    EVP_RAND_free(rand);
657
    if (ctx == NULL) {
658
        ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG);
659
        return NULL;
660
    }
661
662
    if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) {
663
        ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG);
664
        EVP_RAND_CTX_free(ctx);
665
        return NULL;
666
    }
667
    return ctx;
668
}
669
#endif /* FIPS_MODULE */
670
671
/*
672
 * Get the primary random generator.
673
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
674
 *
675
 */
676
static EVP_RAND_CTX *rand_get0_primary(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
677
0
{
678
0
    EVP_RAND_CTX *ret, *seed, *newseed = NULL, *primary;
679
680
0
    if (dgbl == NULL)
681
0
        return NULL;
682
683
0
    if (!CRYPTO_THREAD_read_lock(dgbl->lock))
684
0
        return NULL;
685
686
0
    ret = dgbl->primary;
687
0
    seed = dgbl->seed;
688
0
    CRYPTO_THREAD_unlock(dgbl->lock);
689
690
0
    if (ret != NULL)
691
0
        return ret;
692
693
0
#if !defined(FIPS_MODULE) || !defined(OPENSSL_NO_FIPS_JITTER)
694
    /* Create a seed source for libcrypto or jitter enabled FIPS provider */
695
0
    if (seed == NULL) {
696
0
        ERR_set_mark();
697
0
        seed = newseed = rand_new_seed(ctx);
698
0
        ERR_pop_to_mark();
699
0
    }
700
0
#endif /* !FIPS_MODULE || !OPENSSL_NO_FIPS_JITTER */
701
702
#if defined(FIPS_MODULE)
703
    /* The FIPS provider has entropy health tests instead of the primary */
704
    ret = rand_new_crngt(ctx, seed);
705
#else /* FIPS_MODULE */
706
0
    ret = rand_new_drbg(ctx, seed, PRIMARY_RESEED_INTERVAL,
707
0
        PRIMARY_RESEED_TIME_INTERVAL);
708
0
#endif /* FIPS_MODULE */
709
710
    /*
711
     * The primary DRBG may be shared between multiple threads so we must
712
     * enable locking.
713
     */
714
0
    if (ret == NULL || !EVP_RAND_enable_locking(ret)) {
715
0
        if (ret != NULL) {
716
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_ENABLE_LOCKING);
717
0
            EVP_RAND_CTX_free(ret);
718
0
        }
719
0
        if (newseed == NULL)
720
0
            return NULL;
721
        /* else carry on and store seed */
722
0
        ret = NULL;
723
0
    }
724
725
0
    if (!CRYPTO_THREAD_write_lock(dgbl->lock))
726
0
        return NULL;
727
728
0
    primary = dgbl->primary;
729
0
    if (primary != NULL) {
730
0
        CRYPTO_THREAD_unlock(dgbl->lock);
731
0
        EVP_RAND_CTX_free(ret);
732
0
        EVP_RAND_CTX_free(newseed);
733
0
        return primary;
734
0
    }
735
0
    if (newseed != NULL)
736
0
        dgbl->seed = newseed;
737
0
    dgbl->primary = ret;
738
0
    CRYPTO_THREAD_unlock(dgbl->lock);
739
740
0
    return ret;
741
0
}
742
743
/*
744
 * Get the primary random generator.
745
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
746
 *
747
 */
748
EVP_RAND_CTX *RAND_get0_primary(OSSL_LIB_CTX *ctx)
749
0
{
750
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
751
752
0
    return dgbl == NULL ? NULL : rand_get0_primary(ctx, dgbl);
753
0
}
754
755
static EVP_RAND_CTX *rand_get0_public(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
756
0
{
757
0
    EVP_RAND_CTX *rand, *primary;
758
0
    OSSL_LIB_CTX *origctx = ctx;
759
760
0
    ctx = ossl_lib_ctx_get_concrete(ctx);
761
762
0
    if (ctx == NULL)
763
0
        return NULL;
764
765
0
    if (dgbl == NULL)
766
0
        return NULL;
767
768
0
    rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
769
0
    if (rand == NULL) {
770
0
        primary = rand_get0_primary(origctx, dgbl);
771
0
        if (primary == NULL)
772
0
            return NULL;
773
774
        /*
775
         * If the private is also NULL then this is the first time we've
776
         * used this thread.
777
         */
778
0
        if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx) == NULL
779
0
            && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
780
0
            return NULL;
781
0
        rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
782
0
            SECONDARY_RESEED_TIME_INTERVAL);
783
0
        if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand)) {
784
0
            EVP_RAND_CTX_free(rand);
785
0
            rand = NULL;
786
0
        }
787
0
    }
788
0
    return rand;
789
0
}
790
791
/*
792
 * Get the public random generator.
793
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
794
 */
795
EVP_RAND_CTX *RAND_get0_public(OSSL_LIB_CTX *ctx)
796
0
{
797
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
798
799
0
    return dgbl == NULL ? NULL : rand_get0_public(ctx, dgbl);
800
0
}
801
802
static EVP_RAND_CTX *rand_get0_private(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl)
803
0
{
804
0
    EVP_RAND_CTX *rand, *primary;
805
0
    OSSL_LIB_CTX *origctx = ctx;
806
807
0
    ctx = ossl_lib_ctx_get_concrete(ctx);
808
0
    if (ctx == NULL)
809
0
        return NULL;
810
811
0
    rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
812
0
    if (rand == NULL) {
813
0
        primary = rand_get0_primary(origctx, dgbl);
814
0
        if (primary == NULL)
815
0
            return NULL;
816
817
        /*
818
         * If the public is also NULL then this is the first time we've
819
         * used this thread.
820
         */
821
0
        if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx) == NULL
822
0
            && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state))
823
0
            return NULL;
824
0
        rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL,
825
0
            SECONDARY_RESEED_TIME_INTERVAL);
826
0
        if (!CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand)) {
827
0
            EVP_RAND_CTX_free(rand);
828
0
            rand = NULL;
829
0
        }
830
0
    }
831
0
    return rand;
832
0
}
833
834
/*
835
 * Get the private random generator.
836
 * Returns pointer to its EVP_RAND_CTX on success, NULL on failure.
837
 */
838
EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx)
839
0
{
840
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
841
842
0
    return dgbl == NULL ? NULL : rand_get0_private(ctx, dgbl);
843
0
}
844
845
#ifdef FIPS_MODULE
846
EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx)
847
{
848
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
849
850
    if (dgbl == NULL)
851
        return NULL;
852
853
    return CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
854
}
855
#endif
856
857
int RAND_set0_public(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
858
0
{
859
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
860
0
    EVP_RAND_CTX *old;
861
0
    int r;
862
863
0
    if (dgbl == NULL)
864
0
        return 0;
865
0
    old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx);
866
0
    if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand)) > 0)
867
0
        EVP_RAND_CTX_free(old);
868
0
    return r;
869
0
}
870
871
int RAND_set0_private(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand)
872
0
{
873
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
874
0
    EVP_RAND_CTX *old;
875
0
    int r;
876
877
0
    if (dgbl == NULL)
878
0
        return 0;
879
0
    old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx);
880
0
    if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand)) > 0)
881
0
        EVP_RAND_CTX_free(old);
882
0
    return r;
883
0
}
884
885
#ifndef FIPS_MODULE
886
static int random_set_string(char **p, const char *s)
887
0
{
888
0
    char *d = NULL;
889
890
0
    if (s != NULL) {
891
0
        d = OPENSSL_strdup(s);
892
0
        if (d == NULL)
893
0
            return 0;
894
0
    }
895
0
    OPENSSL_free(*p);
896
0
    *p = d;
897
0
    return 1;
898
0
}
899
900
/*
901
 * Load the DRBG definitions from a configuration file.
902
 */
903
static int random_conf_init(CONF_IMODULE *md, const CONF *cnf)
904
0
{
905
0
    STACK_OF(CONF_VALUE) *elist;
906
0
    CONF_VALUE *cval;
907
0
    OSSL_LIB_CTX *libctx = NCONF_get0_libctx((CONF *)cnf);
908
0
    RAND_GLOBAL *dgbl = rand_get_global(libctx);
909
0
    int i, r = 1;
910
911
0
    OSSL_TRACE1(CONF, "Loading random module: section %s\n",
912
0
        CONF_imodule_get_value(md));
913
914
    /* Value is a section containing RANDOM configuration */
915
0
    elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
916
0
    if (elist == NULL) {
917
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_RANDOM_SECTION_ERROR);
918
0
        return 0;
919
0
    }
920
921
0
    if (dgbl == NULL)
922
0
        return 0;
923
924
0
    for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
925
0
        cval = sk_CONF_VALUE_value(elist, i);
926
0
        if (OPENSSL_strcasecmp(cval->name, "random") == 0) {
927
0
            if (!random_set_string(&dgbl->rng_name, cval->value))
928
0
                return 0;
929
0
        } else if (OPENSSL_strcasecmp(cval->name, "cipher") == 0) {
930
0
            if (!random_set_string(&dgbl->rng_cipher, cval->value))
931
0
                return 0;
932
0
        } else if (OPENSSL_strcasecmp(cval->name, "digest") == 0) {
933
0
            if (!random_set_string(&dgbl->rng_digest, cval->value))
934
0
                return 0;
935
0
        } else if (OPENSSL_strcasecmp(cval->name, "properties") == 0) {
936
0
            if (!random_set_string(&dgbl->rng_propq, cval->value))
937
0
                return 0;
938
0
        } else if (OPENSSL_strcasecmp(cval->name, "seed") == 0) {
939
0
            if (!random_set_string(&dgbl->seed_name, cval->value))
940
0
                return 0;
941
0
        } else if (OPENSSL_strcasecmp(cval->name, "seed_properties") == 0) {
942
0
            if (!random_set_string(&dgbl->seed_propq, cval->value))
943
0
                return 0;
944
0
        } else if (OPENSSL_strcasecmp(cval->name, "random_provider") == 0) {
945
0
#ifndef FIPS_MODULE
946
0
            OSSL_PROVIDER *prov = ossl_provider_find(libctx, cval->value, 0);
947
948
0
            if (prov != NULL) {
949
0
                if (!RAND_set1_random_provider(libctx, prov)) {
950
0
                    ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
951
0
                    OSSL_PROVIDER_unload(prov);
952
0
                    return 0;
953
0
                }
954
                /*
955
                 * We need to release the reference from ossl_provider_find because
956
                 * we don't want to keep a reference counted handle to the provider.
957
                 *
958
                 * The provider unload code checks for the random provider and,
959
                 * if present, our reference will be NULLed when it is fully freed.
960
                 * The provider load code, conversely, checks the provider name
961
                 * and re-hooks our reference if required.  This means that a load,
962
                 * hook random provider, use, unload, reload, reuse sequence will
963
                 * work as expected.
964
                 */
965
0
                OSSL_PROVIDER_unload(prov);
966
0
            } else if (!set_random_provider_name(dgbl, cval->value))
967
0
                return 0;
968
0
#endif
969
0
        } else {
970
0
            ERR_raise_data(ERR_LIB_CRYPTO,
971
0
                CRYPTO_R_UNKNOWN_NAME_IN_RANDOM_SECTION,
972
0
                "name=%s, value=%s", cval->name, cval->value);
973
0
            r = 0;
974
0
        }
975
0
    }
976
0
    return r;
977
0
}
978
979
static void random_conf_deinit(CONF_IMODULE *md)
980
0
{
981
0
    OSSL_TRACE(CONF, "Cleaned up random\n");
982
0
}
983
984
void ossl_random_add_conf_module(void)
985
0
{
986
0
    OSSL_TRACE(CONF, "Adding config module 'random'\n");
987
0
    CONF_module_add("random", random_conf_init, random_conf_deinit);
988
0
}
989
990
int RAND_set_DRBG_type(OSSL_LIB_CTX *ctx, const char *drbg, const char *propq,
991
    const char *cipher, const char *digest)
992
0
{
993
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
994
995
0
    if (dgbl == NULL)
996
0
        return 0;
997
0
    if (dgbl->primary != NULL) {
998
0
        ERR_raise(ERR_LIB_RAND, RAND_R_ALREADY_INSTANTIATED);
999
0
        return 0;
1000
0
    }
1001
0
    return random_set_string(&dgbl->rng_name, drbg)
1002
0
        && random_set_string(&dgbl->rng_propq, propq)
1003
0
        && random_set_string(&dgbl->rng_cipher, cipher)
1004
0
        && random_set_string(&dgbl->rng_digest, digest);
1005
0
}
1006
1007
int RAND_set_seed_source_type(OSSL_LIB_CTX *ctx, const char *seed,
1008
    const char *propq)
1009
0
{
1010
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1011
1012
0
    if (dgbl == NULL)
1013
0
        return 0;
1014
0
    if (dgbl->seed != NULL) {
1015
0
        ERR_raise(ERR_LIB_RAND, RAND_R_ALREADY_INSTANTIATED);
1016
0
        return 0;
1017
0
    }
1018
0
    return random_set_string(&dgbl->seed_name, seed)
1019
0
        && random_set_string(&dgbl->seed_propq, propq);
1020
0
}
1021
1022
int RAND_set1_random_provider(OSSL_LIB_CTX *ctx, OSSL_PROVIDER *prov)
1023
0
{
1024
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1025
1026
0
    if (dgbl == NULL)
1027
0
        return 0;
1028
1029
0
    if (prov == NULL) {
1030
0
        OPENSSL_free(dgbl->random_provider_name);
1031
0
        dgbl->random_provider_name = NULL;
1032
0
        dgbl->random_provider = NULL;
1033
0
        return 1;
1034
0
    }
1035
1036
0
    if (dgbl->random_provider == prov)
1037
0
        return 1;
1038
1039
0
    if (!set_random_provider_name(dgbl, OSSL_PROVIDER_get0_name(prov)))
1040
0
        return 0;
1041
1042
0
    dgbl->random_provider = prov;
1043
0
    return 1;
1044
0
}
1045
1046
/*
1047
 * When a new provider is loaded, we need to check to see if it is the
1048
 * designated randomness provider and register it if it is.
1049
 */
1050
int ossl_rand_check_random_provider_on_load(OSSL_LIB_CTX *ctx,
1051
    OSSL_PROVIDER *prov)
1052
0
{
1053
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1054
1055
0
    if (dgbl == NULL)
1056
0
        return 0;
1057
1058
    /* No random provider name specified, or one is installed already */
1059
0
    if (dgbl->random_provider_name == NULL || dgbl->random_provider != NULL)
1060
0
        return 1;
1061
1062
    /* Does this provider match the name we're using? */
1063
0
    if (strcmp(dgbl->random_provider_name, OSSL_PROVIDER_get0_name(prov)) != 0)
1064
0
        return 1;
1065
1066
0
    dgbl->random_provider = prov;
1067
0
    return 1;
1068
0
}
1069
1070
/*
1071
 * When a provider is being unloaded, if it is the randomness provider,
1072
 * we need to deregister it.
1073
 */
1074
int ossl_rand_check_random_provider_on_unload(OSSL_LIB_CTX *ctx,
1075
    OSSL_PROVIDER *prov)
1076
0
{
1077
0
    RAND_GLOBAL *dgbl = rand_get_global(ctx);
1078
1079
0
    if (dgbl == NULL)
1080
0
        return 0;
1081
1082
0
    if (dgbl->random_provider == prov)
1083
0
        dgbl->random_provider = NULL;
1084
0
    return 1;
1085
0
}
1086
1087
#endif /* !FIPS_MODULE */