Coverage Report

Created: 2025-08-28 07:07

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