Coverage Report

Created: 2025-11-16 06:37

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