Coverage Report

Created: 2025-06-13 06:58

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