Coverage Report

Created: 2026-04-29 06:57

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