]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
0c679f55 | 2 | * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. |
dfeab068 | 3 | * |
0db63de9 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
dfeab068 RE |
8 | */ |
9 | ||
e4468e6d P |
10 | /* We need to use some engine deprecated APIs */ |
11 | #define OPENSSL_SUPPRESS_DEPRECATED | |
12 | ||
44d2482b | 13 | #include <openssl/err.h> |
98186eb4 | 14 | #include <openssl/opensslconf.h> |
7d615e21 | 15 | #include <openssl/core_names.h> |
4636a395 | 16 | #include <openssl/provider.h> |
dce7272d | 17 | #include "internal/cryptlib.h" |
4636a395 | 18 | #include "internal/provider.h" |
87975cfa | 19 | #include "internal/thread_once.h" |
ce990ce8 | 20 | #include "internal/threads_common.h" |
dce7272d TM |
21 | #include "crypto/rand.h" |
22 | #include "crypto/cryptlib.h" | |
706457b7 | 23 | #include "rand_local.h" |
927d0566 | 24 | #include "crypto/context.h" |
c9a2ce61 | 25 | #include "internal/provider.h" |
dfeab068 | 26 | |
1e7ff7be | 27 | #ifndef OPENSSL_DEFAULT_SEED_SRC |
20bf3fe2 | 28 | # define OPENSSL_DEFAULT_SEED_SRC SEED-SRC |
1e7ff7be DJL |
29 | #endif |
30 | ||
4636a395 P |
31 | typedef struct rand_global_st { |
32 | /* | |
33 | * The three shared DRBG instances | |
34 | * | |
35 | * There are three shared DRBG instances: <primary>, <public>, and | |
36 | * <private>. The <public> and <private> DRBGs are secondary ones. | |
37 | * These are used for non-secret (e.g. nonces) and secret | |
38 | * (e.g. private keys) data respectively. | |
39 | */ | |
40 | CRYPTO_RWLOCK *lock; | |
41 | ||
42 | EVP_RAND_CTX *seed; | |
43 | ||
44 | /* | |
45 | * The <primary> DRBG | |
46 | * | |
47 | * Not used directly by the application, only for reseeding the two other | |
48 | * DRBGs. It reseeds itself by pulling either randomness from os entropy | |
49 | * sources or by consuming randomness which was added by RAND_add(). | |
50 | * | |
51 | * The <primary> DRBG is a global instance which is accessed concurrently by | |
52 | * all threads. The necessary locking is managed automatically by its child | |
53 | * DRBG instances during reseeding. | |
54 | */ | |
55 | EVP_RAND_CTX *primary; | |
56 | ||
57 | /* | |
58 | * The provider which we'll use to generate randomness. | |
59 | */ | |
60 | #ifndef FIPS_MODULE | |
61 | OSSL_PROVIDER *random_provider; | |
b1cca259 | 62 | char *random_provider_name; |
4636a395 P |
63 | #endif /* !FIPS_MODULE */ |
64 | ||
4636a395 P |
65 | /* Which RNG is being used by default and it's configuration settings */ |
66 | char *rng_name; | |
67 | char *rng_cipher; | |
68 | char *rng_digest; | |
69 | char *rng_propq; | |
70 | ||
71 | /* Allow the randomness source to be changed */ | |
72 | char *seed_name; | |
73 | char *seed_propq; | |
74 | } RAND_GLOBAL; | |
75 | ||
b1cca259 | 76 | static EVP_RAND_CTX *rand_get0_primary(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl); |
4636a395 P |
77 | static EVP_RAND_CTX *rand_get0_public(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl); |
78 | static EVP_RAND_CTX *rand_get0_private(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl); | |
79 | ||
80 | static RAND_GLOBAL *rand_get_global(OSSL_LIB_CTX *libctx) | |
81 | { | |
82 | return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_INDEX); | |
83 | } | |
84 | ||
f844f9eb | 85 | #ifndef FIPS_MODULE |
dce7272d TM |
86 | # include <stdio.h> |
87 | # include <time.h> | |
88 | # include <limits.h> | |
89 | # include <openssl/conf.h> | |
90 | # include <openssl/trace.h> | |
91 | # include <openssl/engine.h> | |
03bede0c | 92 | # include "crypto/rand_pool.h" |
f000e828 | 93 | # include "prov/seeding.h" |
d5f9166b | 94 | # include "internal/e_os.h" |
4cde7585 | 95 | # include "internal/property.h" |
f000e828 | 96 | |
b1cca259 P |
97 | /* |
98 | * The default name for the random provider. | |
99 | * This ensures that the FIPS provider will supply libcrypto's random byte | |
100 | * requirements. | |
101 | */ | |
102 | static const char random_provider_fips_name[] = "fips"; | |
103 | ||
104 | static int set_random_provider_name(RAND_GLOBAL *dgbl, const char *name) | |
105 | { | |
106 | if (dgbl->random_provider_name != NULL | |
107 | && OPENSSL_strcasecmp(dgbl->random_provider_name, name) == 0) | |
108 | return 1; | |
109 | ||
110 | OPENSSL_free(dgbl->random_provider_name); | |
7fa51041 | 111 | dgbl->random_provider_name = OPENSSL_strdup(name); |
b1cca259 P |
112 | return dgbl->random_provider_name != NULL; |
113 | } | |
114 | ||
a2f27fd7 | 115 | # ifndef OPENSSL_NO_ENGINE |
cb78486d | 116 | /* non-NULL if default_RAND_meth is ENGINE-provided */ |
da8fc25a RS |
117 | static ENGINE *funct_ref; |
118 | static CRYPTO_RWLOCK *rand_engine_lock; | |
4636a395 | 119 | # endif /* !OPENSSL_NO_ENGINE */ |
786b13fa | 120 | # ifndef OPENSSL_NO_DEPRECATED_3_0 |
da8fc25a RS |
121 | static CRYPTO_RWLOCK *rand_meth_lock; |
122 | static const RAND_METHOD *default_RAND_meth; | |
4636a395 | 123 | # endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
da8fc25a | 124 | static CRYPTO_ONCE rand_init = CRYPTO_ONCE_STATIC_INIT; |
c16de9d8 | 125 | |
e2d227bb | 126 | static int rand_inited = 0; |
ddc6a5c8 | 127 | |
da8fc25a | 128 | DEFINE_RUN_ONCE_STATIC(do_rand_init) |
87975cfa | 129 | { |
a2f27fd7 | 130 | # ifndef OPENSSL_NO_ENGINE |
63ab5ea1 | 131 | rand_engine_lock = CRYPTO_THREAD_lock_new(); |
0e5c1a66 BE |
132 | if (rand_engine_lock == NULL) |
133 | return 0; | |
4636a395 | 134 | # endif /* !OPENSSL_NO_ENGINE */ |
0e5c1a66 | 135 | |
786b13fa | 136 | # ifndef OPENSSL_NO_DEPRECATED_3_0 |
63ab5ea1 | 137 | rand_meth_lock = CRYPTO_THREAD_lock_new(); |
0e5c1a66 | 138 | if (rand_meth_lock == NULL) |
a2f27fd7 | 139 | goto err; |
4636a395 | 140 | # endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
5bc6bcf8 | 141 | |
1335ca4b | 142 | if (!ossl_rand_pool_init()) |
a2f27fd7 | 143 | goto err; |
c7504aeb | 144 | |
e2d227bb | 145 | rand_inited = 1; |
0e5c1a66 BE |
146 | return 1; |
147 | ||
a2f27fd7 | 148 | err: |
786b13fa | 149 | # ifndef OPENSSL_NO_DEPRECATED_3_0 |
0e5c1a66 BE |
150 | CRYPTO_THREAD_lock_free(rand_meth_lock); |
151 | rand_meth_lock = NULL; | |
4636a395 | 152 | # endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
a2f27fd7 | 153 | # ifndef OPENSSL_NO_ENGINE |
0e5c1a66 BE |
154 | CRYPTO_THREAD_lock_free(rand_engine_lock); |
155 | rand_engine_lock = NULL; | |
4636a395 | 156 | # endif /* !OPENSSL_NO_ENGINE */ |
0e5c1a66 | 157 | return 0; |
87975cfa | 158 | } |
dfeab068 | 159 | |
1335ca4b | 160 | void ossl_rand_cleanup_int(void) |
da8fc25a | 161 | { |
786b13fa | 162 | # ifndef OPENSSL_NO_DEPRECATED_3_0 |
da8fc25a RS |
163 | const RAND_METHOD *meth = default_RAND_meth; |
164 | ||
e2d227bb BE |
165 | if (!rand_inited) |
166 | return; | |
bc420ebe | 167 | |
da8fc25a RS |
168 | if (meth != NULL && meth->cleanup != NULL) |
169 | meth->cleanup(); | |
170 | RAND_set_rand_method(NULL); | |
4636a395 | 171 | # endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
1335ca4b | 172 | ossl_rand_pool_cleanup(); |
a2f27fd7 | 173 | # ifndef OPENSSL_NO_ENGINE |
da8fc25a | 174 | CRYPTO_THREAD_lock_free(rand_engine_lock); |
0e5c1a66 | 175 | rand_engine_lock = NULL; |
4636a395 | 176 | # endif /* !OPENSSL_NO_ENGINE */ |
786b13fa | 177 | # ifndef OPENSSL_NO_DEPRECATED_3_0 |
da8fc25a | 178 | CRYPTO_THREAD_lock_free(rand_meth_lock); |
0e5c1a66 | 179 | rand_meth_lock = NULL; |
4636a395 | 180 | # endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
a88e97fc | 181 | ossl_release_default_drbg_ctx(); |
e2d227bb | 182 | rand_inited = 0; |
75e2c877 RS |
183 | } |
184 | ||
c7504aeb | 185 | /* |
c2969ff6 | 186 | * RAND_close_seed_files() ensures that any seed file descriptors are |
f000e828 P |
187 | * closed after use. This only applies to libcrypto/default provider, |
188 | * it does not apply to other providers. | |
c7504aeb P |
189 | */ |
190 | void RAND_keep_random_devices_open(int keep) | |
191 | { | |
ac765685 | 192 | if (RUN_ONCE(&rand_init, do_rand_init)) |
1335ca4b | 193 | ossl_rand_pool_keep_random_devices_open(keep); |
c7504aeb P |
194 | } |
195 | ||
75e2c877 | 196 | /* |
c16de9d8 DMSP |
197 | * RAND_poll() reseeds the default RNG using random input |
198 | * | |
199 | * The random input is obtained from polling various entropy | |
200 | * sources which depend on the operating system and are | |
201 | * configurable via the --with-rand-seed configure option. | |
202 | */ | |
203 | int RAND_poll(void) | |
204 | { | |
cc343d04 MK |
205 | static const char salt[] = "polling"; |
206 | ||
786b13fa | 207 | # ifndef OPENSSL_NO_DEPRECATED_3_0 |
c16de9d8 | 208 | const RAND_METHOD *meth = RAND_get_rand_method(); |
f000e828 | 209 | int ret = meth == RAND_OpenSSL(); |
c16de9d8 | 210 | |
0402c90f DMSP |
211 | if (meth == NULL) |
212 | return 0; | |
213 | ||
f000e828 | 214 | if (!ret) { |
c16de9d8 | 215 | /* fill random pool and seed the current legacy RNG */ |
1335ca4b SL |
216 | RAND_POOL *pool = ossl_rand_pool_new(RAND_DRBG_STRENGTH, 1, |
217 | (RAND_DRBG_STRENGTH + 7) / 8, | |
218 | RAND_POOL_MAX_LENGTH); | |
7d615e21 | 219 | |
c16de9d8 DMSP |
220 | if (pool == NULL) |
221 | return 0; | |
f000e828 | 222 | |
1dc188ba | 223 | if (ossl_pool_acquire_entropy(pool) == 0) |
c16de9d8 | 224 | goto err; |
f000e828 | 225 | |
c16de9d8 | 226 | if (meth->add == NULL |
1335ca4b | 227 | || meth->add(ossl_rand_pool_buffer(pool), |
bb86c43f | 228 | (int)ossl_rand_pool_length(pool), |
1335ca4b | 229 | (ossl_rand_pool_entropy(pool) / 8.0)) == 0) |
c16de9d8 DMSP |
230 | goto err; |
231 | ||
232 | ret = 1; | |
a2f27fd7 | 233 | err: |
1335ca4b | 234 | ossl_rand_pool_free(pool); |
cc343d04 | 235 | return ret; |
c16de9d8 | 236 | } |
4636a395 | 237 | # endif /* !OPENSSL_NO_DEPRECATED_3_0 */ |
786b13fa P |
238 | |
239 | RAND_seed(salt, sizeof(salt)); | |
240 | return 1; | |
c16de9d8 | 241 | } |
c16de9d8 | 242 | |
786b13fa | 243 | # ifndef OPENSSL_NO_DEPRECATED_3_0 |
8f4cddbc P |
244 | static int rand_set_rand_method_internal(const RAND_METHOD *meth, |
245 | ossl_unused ENGINE *e) | |
0f113f3e | 246 | { |
da8fc25a | 247 | if (!RUN_ONCE(&rand_init, do_rand_init)) |
87975cfa RL |
248 | return 0; |
249 | ||
cd3f8c1b RS |
250 | if (!CRYPTO_THREAD_write_lock(rand_meth_lock)) |
251 | return 0; | |
786b13fa | 252 | # ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 253 | ENGINE_finish(funct_ref); |
8f4cddbc | 254 | funct_ref = e; |
786b13fa | 255 | # endif |
0f113f3e | 256 | default_RAND_meth = meth; |
87975cfa | 257 | CRYPTO_THREAD_unlock(rand_meth_lock); |
0f113f3e MC |
258 | return 1; |
259 | } | |
dfeab068 | 260 | |
8f4cddbc P |
261 | int RAND_set_rand_method(const RAND_METHOD *meth) |
262 | { | |
263 | return rand_set_rand_method_internal(meth, NULL); | |
264 | } | |
265 | ||
a4a9d97a | 266 | const RAND_METHOD *RAND_get_rand_method(void) |
0f113f3e | 267 | { |
87975cfa RL |
268 | const RAND_METHOD *tmp_meth = NULL; |
269 | ||
da8fc25a | 270 | if (!RUN_ONCE(&rand_init, do_rand_init)) |
87975cfa RL |
271 | return NULL; |
272 | ||
4eb3eea7 JZ |
273 | if (rand_meth_lock == NULL) |
274 | return NULL; | |
275 | ||
7f2c22c1 MC |
276 | if (!CRYPTO_THREAD_read_lock(rand_meth_lock)) |
277 | return NULL; | |
278 | tmp_meth = default_RAND_meth; | |
279 | CRYPTO_THREAD_unlock(rand_meth_lock); | |
280 | if (tmp_meth != NULL) | |
281 | return tmp_meth; | |
282 | ||
cd3f8c1b RS |
283 | if (!CRYPTO_THREAD_write_lock(rand_meth_lock)) |
284 | return NULL; | |
da8fc25a | 285 | if (default_RAND_meth == NULL) { |
786b13fa | 286 | # ifndef OPENSSL_NO_ENGINE |
da8fc25a RS |
287 | ENGINE *e; |
288 | ||
289 | /* If we have an engine that can do RAND, use it. */ | |
290 | if ((e = ENGINE_get_default_RAND()) != NULL | |
291 | && (tmp_meth = ENGINE_get_RAND(e)) != NULL) { | |
0f113f3e | 292 | funct_ref = e; |
da8fc25a RS |
293 | default_RAND_meth = tmp_meth; |
294 | } else { | |
295 | ENGINE_finish(e); | |
1335ca4b | 296 | default_RAND_meth = &ossl_rand_meth; |
da8fc25a | 297 | } |
786b13fa | 298 | # else |
1335ca4b | 299 | default_RAND_meth = &ossl_rand_meth; |
786b13fa | 300 | # endif |
0f113f3e | 301 | } |
87975cfa RL |
302 | tmp_meth = default_RAND_meth; |
303 | CRYPTO_THREAD_unlock(rand_meth_lock); | |
304 | return tmp_meth; | |
0f113f3e | 305 | } |
cb78486d | 306 | |
786b13fa | 307 | # if !defined(OPENSSL_NO_ENGINE) |
cb78486d | 308 | int RAND_set_rand_engine(ENGINE *engine) |
0f113f3e MC |
309 | { |
310 | const RAND_METHOD *tmp_meth = NULL; | |
87975cfa | 311 | |
da8fc25a | 312 | if (!RUN_ONCE(&rand_init, do_rand_init)) |
87975cfa RL |
313 | return 0; |
314 | ||
da8fc25a | 315 | if (engine != NULL) { |
0f113f3e MC |
316 | if (!ENGINE_init(engine)) |
317 | return 0; | |
318 | tmp_meth = ENGINE_get_RAND(engine); | |
7c96dbcd | 319 | if (tmp_meth == NULL) { |
0f113f3e MC |
320 | ENGINE_finish(engine); |
321 | return 0; | |
322 | } | |
323 | } | |
cd3f8c1b RS |
324 | if (!CRYPTO_THREAD_write_lock(rand_engine_lock)) { |
325 | ENGINE_finish(engine); | |
326 | return 0; | |
327 | } | |
328 | ||
0f113f3e | 329 | /* This function releases any prior ENGINE so call it first */ |
8f4cddbc | 330 | rand_set_rand_method_internal(tmp_meth, engine); |
87975cfa | 331 | CRYPTO_THREAD_unlock(rand_engine_lock); |
0f113f3e MC |
332 | return 1; |
333 | } | |
786b13fa P |
334 | # endif |
335 | # endif /* OPENSSL_NO_DEPRECATED_3_0 */ | |
dfeab068 | 336 | |
6343829a | 337 | void RAND_seed(const void *buf, int num) |
0f113f3e | 338 | { |
786b13fa P |
339 | EVP_RAND_CTX *drbg; |
340 | # ifndef OPENSSL_NO_DEPRECATED_3_0 | |
0f113f3e | 341 | const RAND_METHOD *meth = RAND_get_rand_method(); |
da8fc25a | 342 | |
786b13fa | 343 | if (meth != NULL && meth->seed != NULL) { |
0f113f3e | 344 | meth->seed(buf, num); |
786b13fa P |
345 | return; |
346 | } | |
347 | # endif | |
348 | ||
349 | drbg = RAND_get0_primary(NULL); | |
350 | if (drbg != NULL && num > 0) | |
351 | EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num); | |
0f113f3e | 352 | } |
dfeab068 | 353 | |
da8fc25a | 354 | void RAND_add(const void *buf, int num, double randomness) |
0f113f3e | 355 | { |
786b13fa P |
356 | EVP_RAND_CTX *drbg; |
357 | # ifndef OPENSSL_NO_DEPRECATED_3_0 | |
0f113f3e | 358 | const RAND_METHOD *meth = RAND_get_rand_method(); |
da8fc25a | 359 | |
786b13fa | 360 | if (meth != NULL && meth->add != NULL) { |
da8fc25a | 361 | meth->add(buf, num, randomness); |
786b13fa P |
362 | return; |
363 | } | |
364 | # endif | |
365 | drbg = RAND_get0_primary(NULL); | |
366 | if (drbg != NULL && num > 0) | |
56547da9 P |
367 | # ifdef OPENSSL_RAND_SEED_NONE |
368 | /* Without an entropy source, we have to rely on the user */ | |
369 | EVP_RAND_reseed(drbg, 0, buf, num, NULL, 0); | |
370 | # else | |
371 | /* With an entropy source, we downgrade this to additional input */ | |
786b13fa | 372 | EVP_RAND_reseed(drbg, 0, NULL, 0, buf, num); |
56547da9 | 373 | # endif |
0f113f3e | 374 | } |
eb952088 | 375 | |
f000e828 P |
376 | # if !defined(OPENSSL_NO_DEPRECATED_1_1_0) |
377 | int RAND_pseudo_bytes(unsigned char *buf, int num) | |
378 | { | |
379 | const RAND_METHOD *meth = RAND_get_rand_method(); | |
380 | ||
381 | if (meth != NULL && meth->pseudorand != NULL) | |
382 | return meth->pseudorand(buf, num); | |
9311d0c4 | 383 | ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED); |
f000e828 P |
384 | return -1; |
385 | } | |
386 | # endif | |
387 | ||
388 | int RAND_status(void) | |
389 | { | |
7d615e21 | 390 | EVP_RAND_CTX *rand; |
786b13fa | 391 | # ifndef OPENSSL_NO_DEPRECATED_3_0 |
f000e828 P |
392 | const RAND_METHOD *meth = RAND_get_rand_method(); |
393 | ||
394 | if (meth != NULL && meth != RAND_OpenSSL()) | |
395 | return meth->status != NULL ? meth->status() : 0; | |
786b13fa | 396 | # endif |
f000e828 | 397 | |
7d615e21 | 398 | if ((rand = RAND_get0_primary(NULL)) == NULL) |
4516bf74 | 399 | return 0; |
ed576acd | 400 | return EVP_RAND_get_state(rand) == EVP_RAND_STATE_READY; |
f000e828 | 401 | } |
786b13fa | 402 | # else /* !FIPS_MODULE */ |
f000e828 | 403 | |
786b13fa | 404 | # ifndef OPENSSL_NO_DEPRECATED_3_0 |
f000e828 P |
405 | const RAND_METHOD *RAND_get_rand_method(void) |
406 | { | |
407 | return NULL; | |
408 | } | |
786b13fa | 409 | # endif |
f000e828 P |
410 | #endif /* !FIPS_MODULE */ |
411 | ||
ddc6a5c8 RS |
412 | /* |
413 | * This function is not part of RAND_METHOD, so if we're not using | |
414 | * the default method, then just call RAND_bytes(). Otherwise make | |
415 | * sure we're instantiated and use the private DRBG. | |
416 | */ | |
528685fe | 417 | int RAND_priv_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num, |
508258ca | 418 | unsigned int strength) |
ddc6a5c8 | 419 | { |
4636a395 | 420 | RAND_GLOBAL *dgbl; |
7d615e21 | 421 | EVP_RAND_CTX *rand; |
dce7272d | 422 | #if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE) |
a2f27fd7 | 423 | const RAND_METHOD *meth = RAND_get_rand_method(); |
ddc6a5c8 | 424 | |
0402c90f | 425 | if (meth != NULL && meth != RAND_OpenSSL()) { |
bb86c43f TM |
426 | if (num > INT_MAX) { |
427 | ERR_raise(ERR_LIB_RAND, RAND_R_ARGUMENT_OUT_OF_RANGE); | |
428 | return -1; | |
429 | } | |
0402c90f | 430 | if (meth->bytes != NULL) |
bb86c43f | 431 | return meth->bytes(buf, (int)num); |
9311d0c4 | 432 | ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED); |
0402c90f DMSP |
433 | return -1; |
434 | } | |
786b13fa | 435 | #endif |
ddc6a5c8 | 436 | |
4636a395 P |
437 | dgbl = rand_get_global(ctx); |
438 | if (dgbl == NULL) | |
439 | return 0; | |
440 | #ifndef FIPS_MODULE | |
441 | if (dgbl->random_provider != NULL) | |
b1cca259 P |
442 | return ossl_provider_random_bytes(dgbl->random_provider, |
443 | OSSL_PROV_RANDOM_PRIVATE, | |
444 | buf, num, strength); | |
4636a395 P |
445 | #endif /* !FIPS_MODULE */ |
446 | rand = rand_get0_private(ctx, dgbl); | |
7d615e21 | 447 | if (rand != NULL) |
508258ca | 448 | return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0); |
ddc6a5c8 | 449 | |
0402c90f | 450 | return 0; |
ddc6a5c8 RS |
451 | } |
452 | ||
6694e51d | 453 | int RAND_priv_bytes(unsigned char *buf, int num) |
0f113f3e | 454 | { |
4cde7585 P |
455 | if (num < 0) |
456 | return 0; | |
528685fe | 457 | return RAND_priv_bytes_ex(NULL, buf, (size_t)num, 0); |
6694e51d MC |
458 | } |
459 | ||
528685fe | 460 | int RAND_bytes_ex(OSSL_LIB_CTX *ctx, unsigned char *buf, size_t num, |
508258ca | 461 | unsigned int strength) |
6694e51d | 462 | { |
4636a395 | 463 | RAND_GLOBAL *dgbl; |
7d615e21 | 464 | EVP_RAND_CTX *rand; |
dce7272d | 465 | #if !defined(OPENSSL_NO_DEPRECATED_3_0) && !defined(FIPS_MODULE) |
0f113f3e | 466 | const RAND_METHOD *meth = RAND_get_rand_method(); |
da8fc25a | 467 | |
0402c90f | 468 | if (meth != NULL && meth != RAND_OpenSSL()) { |
bb86c43f TM |
469 | if (num > INT_MAX) { |
470 | ERR_raise(ERR_LIB_RAND, RAND_R_ARGUMENT_OUT_OF_RANGE); | |
471 | return -1; | |
472 | } | |
6694e51d | 473 | if (meth->bytes != NULL) |
bb86c43f | 474 | return meth->bytes(buf, (int)num); |
9311d0c4 | 475 | ERR_raise(ERR_LIB_RAND, RAND_R_FUNC_NOT_IMPLEMENTED); |
6694e51d MC |
476 | return -1; |
477 | } | |
786b13fa | 478 | #endif |
6694e51d | 479 | |
4636a395 P |
480 | dgbl = rand_get_global(ctx); |
481 | if (dgbl == NULL) | |
482 | return 0; | |
483 | #ifndef FIPS_MODULE | |
484 | if (dgbl->random_provider != NULL) | |
b1cca259 P |
485 | return ossl_provider_random_bytes(dgbl->random_provider, |
486 | OSSL_PROV_RANDOM_PUBLIC, | |
487 | buf, num, strength); | |
4636a395 P |
488 | #endif /* !FIPS_MODULE */ |
489 | ||
490 | rand = rand_get0_public(ctx, dgbl); | |
7d615e21 | 491 | if (rand != NULL) |
508258ca | 492 | return EVP_RAND_generate(rand, buf, num, strength, 0, NULL, 0); |
6694e51d | 493 | |
0402c90f | 494 | return 0; |
6694e51d MC |
495 | } |
496 | ||
497 | int RAND_bytes(unsigned char *buf, int num) | |
498 | { | |
4cde7585 P |
499 | if (num < 0) |
500 | return 0; | |
528685fe | 501 | return RAND_bytes_ex(NULL, buf, (size_t)num, 0); |
0f113f3e | 502 | } |
7d615e21 | 503 | |
7d615e21 | 504 | /* |
b4250010 | 505 | * Initialize the OSSL_LIB_CTX global DRBGs on first use. |
7d615e21 P |
506 | * Returns the allocated global data on success or NULL on failure. |
507 | */ | |
927d0566 | 508 | void *ossl_rand_ctx_new(OSSL_LIB_CTX *libctx) |
7d615e21 P |
509 | { |
510 | RAND_GLOBAL *dgbl = OPENSSL_zalloc(sizeof(*dgbl)); | |
511 | ||
512 | if (dgbl == NULL) | |
513 | return NULL; | |
514 | ||
515 | #ifndef FIPS_MODULE | |
516 | /* | |
517 | * We need to ensure that base libcrypto thread handling has been | |
518 | * initialised. | |
519 | */ | |
b1cca259 P |
520 | OPENSSL_init_crypto(OPENSSL_INIT_BASE_ONLY, NULL); |
521 | ||
522 | /* Prepopulate the random provider name */ | |
dfd177b7 | 523 | dgbl->random_provider_name = OPENSSL_strdup(random_provider_fips_name); |
b1cca259 P |
524 | if (dgbl->random_provider_name == NULL) |
525 | goto err0; | |
7d615e21 P |
526 | #endif |
527 | ||
528 | dgbl->lock = CRYPTO_THREAD_lock_new(); | |
529 | if (dgbl->lock == NULL) | |
530 | goto err1; | |
531 | ||
7d615e21 P |
532 | return dgbl; |
533 | ||
7d615e21 P |
534 | err1: |
535 | CRYPTO_THREAD_lock_free(dgbl->lock); | |
b1cca259 P |
536 | #ifndef FIPS_MODULE |
537 | err0: | |
538 | OPENSSL_free(dgbl->random_provider_name); | |
539 | #endif | |
7d615e21 P |
540 | OPENSSL_free(dgbl); |
541 | return NULL; | |
542 | } | |
543 | ||
927d0566 | 544 | void ossl_rand_ctx_free(void *vdgbl) |
7d615e21 P |
545 | { |
546 | RAND_GLOBAL *dgbl = vdgbl; | |
547 | ||
548 | if (dgbl == NULL) | |
549 | return; | |
550 | ||
551 | CRYPTO_THREAD_lock_free(dgbl->lock); | |
81aef6ba P |
552 | EVP_RAND_CTX_free(dgbl->primary); |
553 | EVP_RAND_CTX_free(dgbl->seed); | |
4636a395 | 554 | #ifndef FIPS_MODULE |
b1cca259 | 555 | OPENSSL_free(dgbl->random_provider_name); |
4636a395 | 556 | #endif /* !FIPS_MODULE */ |
44d2482b P |
557 | OPENSSL_free(dgbl->rng_name); |
558 | OPENSSL_free(dgbl->rng_cipher); | |
559 | OPENSSL_free(dgbl->rng_digest); | |
560 | OPENSSL_free(dgbl->rng_propq); | |
81aef6ba P |
561 | OPENSSL_free(dgbl->seed_name); |
562 | OPENSSL_free(dgbl->seed_propq); | |
7d615e21 P |
563 | |
564 | OPENSSL_free(dgbl); | |
565 | } | |
566 | ||
7d615e21 P |
567 | static void rand_delete_thread_state(void *arg) |
568 | { | |
b4250010 | 569 | OSSL_LIB_CTX *ctx = arg; |
7d615e21 P |
570 | RAND_GLOBAL *dgbl = rand_get_global(ctx); |
571 | EVP_RAND_CTX *rand; | |
572 | ||
573 | if (dgbl == NULL) | |
574 | return; | |
575 | ||
ce990ce8 NH |
576 | rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx); |
577 | CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, NULL); | |
7d615e21 P |
578 | EVP_RAND_CTX_free(rand); |
579 | ||
ce990ce8 NH |
580 | rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx); |
581 | CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, NULL); | |
7d615e21 P |
582 | EVP_RAND_CTX_free(rand); |
583 | } | |
584 | ||
3a01d5d6 | 585 | #if !defined(FIPS_MODULE) || !defined(OPENSSL_NO_FIPS_JITTER) |
81aef6ba P |
586 | static EVP_RAND_CTX *rand_new_seed(OSSL_LIB_CTX *libctx) |
587 | { | |
588 | EVP_RAND *rand; | |
15410839 | 589 | const char *propq; |
3a01d5d6 P |
590 | char *name; |
591 | EVP_RAND_CTX *ctx = NULL; | |
592 | # ifdef OPENSSL_NO_FIPS_JITTER | |
593 | RAND_GLOBAL *dgbl = rand_get_global(libctx); | |
81aef6ba | 594 | |
09dca557 JJ |
595 | if (dgbl == NULL) |
596 | return NULL; | |
15410839 | 597 | propq = dgbl->seed_propq; |
21f92ecf P |
598 | name = dgbl->seed_name != NULL ? dgbl->seed_name |
599 | : OPENSSL_MSTR(OPENSSL_DEFAULT_SEED_SRC); | |
3a01d5d6 P |
600 | # else /* !OPENSSL_NO_FIPS_JITTER */ |
601 | name = "JITTER"; | |
21f92ecf | 602 | propq = ""; |
3a01d5d6 | 603 | # endif /* OPENSSL_NO_FIPS_JITTER */ |
4cde7585 P |
604 | |
605 | rand = EVP_RAND_fetch(libctx, name, propq); | |
81aef6ba P |
606 | if (rand == NULL) { |
607 | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG); | |
4cde7585 | 608 | goto err; |
81aef6ba P |
609 | } |
610 | ctx = EVP_RAND_CTX_new(rand, NULL); | |
611 | EVP_RAND_free(rand); | |
612 | if (ctx == NULL) { | |
613 | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG); | |
4cde7585 | 614 | goto err; |
81aef6ba | 615 | } |
d5a936c5 | 616 | if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) { |
81aef6ba | 617 | ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG); |
4cde7585 | 618 | goto err; |
81aef6ba P |
619 | } |
620 | return ctx; | |
4cde7585 P |
621 | err: |
622 | EVP_RAND_CTX_free(ctx); | |
4cde7585 P |
623 | return NULL; |
624 | } | |
3a01d5d6 | 625 | #endif /* !FIPS_MODULE || !OPENSSL_NO_FIPS_JITTER */ |
4cde7585 | 626 | |
3a01d5d6 | 627 | #ifndef FIPS_MODULE |
4cde7585 P |
628 | EVP_RAND_CTX *ossl_rand_get0_seed_noncreating(OSSL_LIB_CTX *ctx) |
629 | { | |
630 | RAND_GLOBAL *dgbl = rand_get_global(ctx); | |
631 | EVP_RAND_CTX *ret; | |
632 | ||
633 | if (dgbl == NULL) | |
634 | return NULL; | |
635 | ||
636 | if (!CRYPTO_THREAD_read_lock(dgbl->lock)) | |
637 | return NULL; | |
638 | ret = dgbl->seed; | |
639 | CRYPTO_THREAD_unlock(dgbl->lock); | |
640 | return ret; | |
81aef6ba | 641 | } |
3a01d5d6 | 642 | #endif /* !FIPS_MODULE */ |
81aef6ba | 643 | |
b4250010 | 644 | static EVP_RAND_CTX *rand_new_drbg(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent, |
7d615e21 | 645 | unsigned int reseed_interval, |
260ecea0 | 646 | time_t reseed_time_interval) |
7d615e21 | 647 | { |
44d2482b P |
648 | EVP_RAND *rand; |
649 | RAND_GLOBAL *dgbl = rand_get_global(libctx); | |
7d615e21 | 650 | EVP_RAND_CTX *ctx; |
c9a2ce61 | 651 | OSSL_PARAM params[9], *p = params; |
505d44c6 | 652 | const OSSL_PARAM *settables; |
c9a2ce61 | 653 | const char *prov_name; |
44d2482b | 654 | char *name, *cipher; |
260ecea0 | 655 | int use_df = 1; |
44d2482b | 656 | |
09dca557 JJ |
657 | if (dgbl == NULL) |
658 | return NULL; | |
44d2482b P |
659 | name = dgbl->rng_name != NULL ? dgbl->rng_name : "CTR-DRBG"; |
660 | rand = EVP_RAND_fetch(libctx, name, dgbl->rng_propq); | |
7d615e21 | 661 | if (rand == NULL) { |
9311d0c4 | 662 | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG); |
7d615e21 P |
663 | return NULL; |
664 | } | |
c9a2ce61 | 665 | prov_name = ossl_provider_name(EVP_RAND_get0_provider(rand)); |
7d615e21 P |
666 | ctx = EVP_RAND_CTX_new(rand, parent); |
667 | EVP_RAND_free(rand); | |
668 | if (ctx == NULL) { | |
9311d0c4 | 669 | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG); |
7d615e21 P |
670 | return NULL; |
671 | } | |
672 | ||
505d44c6 P |
673 | settables = EVP_RAND_CTX_settable_params(ctx); |
674 | if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_CIPHER)) { | |
675 | cipher = dgbl->rng_cipher != NULL ? dgbl->rng_cipher : "AES-256-CTR"; | |
676 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_CIPHER, | |
677 | cipher, 0); | |
678 | } | |
679 | if (dgbl->rng_digest != NULL | |
680 | && OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_DIGEST)) | |
44d2482b P |
681 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_DIGEST, |
682 | dgbl->rng_digest, 0); | |
c9a2ce61 SS |
683 | if (prov_name != NULL) |
684 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PROV_PARAM_CORE_PROV_NAME, | |
685 | (char *)prov_name, 0); | |
44d2482b P |
686 | if (dgbl->rng_propq != NULL) |
687 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, | |
688 | dgbl->rng_propq, 0); | |
505d44c6 P |
689 | if (OSSL_PARAM_locate_const(settables, OSSL_ALG_PARAM_MAC)) |
690 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_MAC, "HMAC", 0); | |
691 | if (OSSL_PARAM_locate_const(settables, OSSL_DRBG_PARAM_USE_DF)) | |
692 | *p++ = OSSL_PARAM_construct_int(OSSL_DRBG_PARAM_USE_DF, &use_df); | |
7d615e21 P |
693 | *p++ = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_REQUESTS, |
694 | &reseed_interval); | |
695 | *p++ = OSSL_PARAM_construct_time_t(OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL, | |
696 | &reseed_time_interval); | |
697 | *p = OSSL_PARAM_construct_end(); | |
d5a936c5 | 698 | if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, params)) { |
9311d0c4 | 699 | ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG); |
4516bf74 P |
700 | EVP_RAND_CTX_free(ctx); |
701 | return NULL; | |
7d615e21 P |
702 | } |
703 | return ctx; | |
704 | } | |
705 | ||
3a01d5d6 | 706 | #if defined(FIPS_MODULE) |
6f20c680 P |
707 | static EVP_RAND_CTX *rand_new_crngt(OSSL_LIB_CTX *libctx, EVP_RAND_CTX *parent) |
708 | { | |
709 | EVP_RAND *rand; | |
710 | EVP_RAND_CTX *ctx; | |
711 | ||
3a01d5d6 | 712 | rand = EVP_RAND_fetch(libctx, "CRNG-TEST", "-fips"); |
6f20c680 P |
713 | if (rand == NULL) { |
714 | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_FETCH_DRBG); | |
715 | return NULL; | |
716 | } | |
717 | ctx = EVP_RAND_CTX_new(rand, parent); | |
718 | EVP_RAND_free(rand); | |
719 | if (ctx == NULL) { | |
720 | ERR_raise(ERR_LIB_RAND, RAND_R_UNABLE_TO_CREATE_DRBG); | |
721 | return NULL; | |
722 | } | |
723 | ||
724 | if (!EVP_RAND_instantiate(ctx, 0, 0, NULL, 0, NULL)) { | |
725 | ERR_raise(ERR_LIB_RAND, RAND_R_ERROR_INSTANTIATING_DRBG); | |
726 | EVP_RAND_CTX_free(ctx); | |
727 | return NULL; | |
728 | } | |
729 | return ctx; | |
730 | } | |
3a01d5d6 | 731 | #endif /* FIPS_MODULE */ |
6f20c680 | 732 | |
7d615e21 P |
733 | /* |
734 | * Get the primary random generator. | |
735 | * Returns pointer to its EVP_RAND_CTX on success, NULL on failure. | |
736 | * | |
737 | */ | |
b1cca259 | 738 | static EVP_RAND_CTX *rand_get0_primary(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl) |
7d615e21 | 739 | { |
01ea0804 | 740 | EVP_RAND_CTX *ret, *seed, *newseed = NULL, *primary; |
7d615e21 P |
741 | |
742 | if (dgbl == NULL) | |
743 | return NULL; | |
744 | ||
cd4e6a35 MC |
745 | if (!CRYPTO_THREAD_read_lock(dgbl->lock)) |
746 | return NULL; | |
747 | ||
748 | ret = dgbl->primary; | |
01ea0804 | 749 | seed = dgbl->seed; |
cd4e6a35 MC |
750 | CRYPTO_THREAD_unlock(dgbl->lock); |
751 | ||
752 | if (ret != NULL) | |
753 | return ret; | |
754 | ||
3a01d5d6 P |
755 | #if !defined(FIPS_MODULE) || !defined(OPENSSL_NO_FIPS_JITTER) |
756 | /* Create a seed source for libcrypto or jitter enabled FIPS provider */ | |
01ea0804 | 757 | if (seed == NULL) { |
cd4e6a35 | 758 | ERR_set_mark(); |
01ea0804 | 759 | seed = newseed = rand_new_seed(ctx); |
cd4e6a35 MC |
760 | ERR_pop_to_mark(); |
761 | } | |
3a01d5d6 P |
762 | #endif /* !FIPS_MODULE || !OPENSSL_NO_FIPS_JITTER */ |
763 | ||
764 | #if defined(FIPS_MODULE) | |
765 | /* The FIPS provider has entropy health tests instead of the primary */ | |
01ea0804 | 766 | ret = rand_new_crngt(ctx, seed); |
3a01d5d6 | 767 | #else /* FIPS_MODULE */ |
01ea0804 | 768 | ret = rand_new_drbg(ctx, seed, PRIMARY_RESEED_INTERVAL, |
260ecea0 | 769 | PRIMARY_RESEED_TIME_INTERVAL); |
3a01d5d6 | 770 | #endif /* FIPS_MODULE */ |
cd4e6a35 | 771 | |
cd4e6a35 | 772 | /* |
6f20c680 P |
773 | * The primary DRBG may be shared between multiple threads so we must |
774 | * enable locking. | |
775 | */ | |
01ea0804 MC |
776 | if (ret == NULL || !EVP_RAND_enable_locking(ret)) { |
777 | if (ret != NULL) { | |
778 | ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_ENABLE_LOCKING); | |
779 | EVP_RAND_CTX_free(ret); | |
780 | } | |
781 | if (newseed == NULL) | |
782 | return NULL; | |
783 | /* else carry on and store seed */ | |
784 | ret = NULL; | |
785 | } | |
786 | ||
787 | if (!CRYPTO_THREAD_write_lock(dgbl->lock)) | |
788 | return NULL; | |
789 | ||
790 | primary = dgbl->primary; | |
791 | if (primary != NULL) { | |
792 | CRYPTO_THREAD_unlock(dgbl->lock); | |
cd4e6a35 | 793 | EVP_RAND_CTX_free(ret); |
01ea0804 MC |
794 | EVP_RAND_CTX_free(newseed); |
795 | return primary; | |
7d615e21 | 796 | } |
01ea0804 MC |
797 | if (newseed != NULL) |
798 | dgbl->seed = newseed; | |
799 | dgbl->primary = ret; | |
cd4e6a35 MC |
800 | CRYPTO_THREAD_unlock(dgbl->lock); |
801 | ||
802 | return ret; | |
7d615e21 P |
803 | } |
804 | ||
b1cca259 P |
805 | /* |
806 | * Get the primary random generator. | |
807 | * Returns pointer to its EVP_RAND_CTX on success, NULL on failure. | |
808 | * | |
809 | */ | |
810 | EVP_RAND_CTX *RAND_get0_primary(OSSL_LIB_CTX *ctx) | |
811 | { | |
812 | RAND_GLOBAL *dgbl = rand_get_global(ctx); | |
813 | ||
814 | return dgbl == NULL ? NULL : rand_get0_primary(ctx, dgbl); | |
815 | } | |
816 | ||
4636a395 | 817 | static EVP_RAND_CTX *rand_get0_public(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl) |
7d615e21 | 818 | { |
7d615e21 | 819 | EVP_RAND_CTX *rand, *primary; |
ce990ce8 NH |
820 | OSSL_LIB_CTX *origctx = ctx; |
821 | ||
822 | ctx = ossl_lib_ctx_get_concrete(ctx); | |
823 | ||
824 | if (ctx == NULL) | |
825 | return NULL; | |
7d615e21 P |
826 | |
827 | if (dgbl == NULL) | |
828 | return NULL; | |
829 | ||
ce990ce8 | 830 | rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx); |
7d615e21 | 831 | if (rand == NULL) { |
ce990ce8 | 832 | primary = rand_get0_primary(origctx, dgbl); |
7d615e21 P |
833 | if (primary == NULL) |
834 | return NULL; | |
835 | ||
7d615e21 P |
836 | /* |
837 | * If the private is also NULL then this is the first time we've | |
838 | * used this thread. | |
839 | */ | |
ce990ce8 | 840 | if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx) == NULL |
7d615e21 P |
841 | && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state)) |
842 | return NULL; | |
843 | rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL, | |
260ecea0 | 844 | SECONDARY_RESEED_TIME_INTERVAL); |
ce990ce8 | 845 | CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand); |
7d615e21 P |
846 | } |
847 | return rand; | |
848 | } | |
849 | ||
850 | /* | |
4636a395 | 851 | * Get the public random generator. |
7d615e21 P |
852 | * Returns pointer to its EVP_RAND_CTX on success, NULL on failure. |
853 | */ | |
4636a395 | 854 | EVP_RAND_CTX *RAND_get0_public(OSSL_LIB_CTX *ctx) |
7d615e21 P |
855 | { |
856 | RAND_GLOBAL *dgbl = rand_get_global(ctx); | |
7d615e21 | 857 | |
4636a395 P |
858 | return dgbl == NULL ? NULL : rand_get0_public(ctx, dgbl); |
859 | } | |
860 | ||
861 | static EVP_RAND_CTX *rand_get0_private(OSSL_LIB_CTX *ctx, RAND_GLOBAL *dgbl) | |
862 | { | |
863 | EVP_RAND_CTX *rand, *primary; | |
ce990ce8 NH |
864 | OSSL_LIB_CTX *origctx = ctx; |
865 | ||
866 | ctx = ossl_lib_ctx_get_concrete(ctx); | |
867 | if (ctx == NULL) | |
868 | return NULL; | |
7d615e21 | 869 | |
ce990ce8 | 870 | rand = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx); |
7d615e21 | 871 | if (rand == NULL) { |
ce990ce8 | 872 | primary = rand_get0_primary(origctx, dgbl); |
7d615e21 P |
873 | if (primary == NULL) |
874 | return NULL; | |
875 | ||
7d615e21 P |
876 | /* |
877 | * If the public is also NULL then this is the first time we've | |
878 | * used this thread. | |
879 | */ | |
ce990ce8 | 880 | if (CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx) == NULL |
7d615e21 P |
881 | && !ossl_init_thread_start(NULL, ctx, rand_delete_thread_state)) |
882 | return NULL; | |
883 | rand = rand_new_drbg(ctx, primary, SECONDARY_RESEED_INTERVAL, | |
260ecea0 | 884 | SECONDARY_RESEED_TIME_INTERVAL); |
ce990ce8 | 885 | CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand); |
7d615e21 P |
886 | } |
887 | return rand; | |
888 | } | |
44d2482b | 889 | |
4636a395 P |
890 | /* |
891 | * Get the private random generator. | |
892 | * Returns pointer to its EVP_RAND_CTX on success, NULL on failure. | |
893 | */ | |
894 | EVP_RAND_CTX *RAND_get0_private(OSSL_LIB_CTX *ctx) | |
895 | { | |
896 | RAND_GLOBAL *dgbl = rand_get_global(ctx); | |
897 | ||
898 | return dgbl == NULL ? NULL : rand_get0_private(ctx, dgbl); | |
899 | } | |
900 | ||
fffa78c2 P |
901 | #ifdef FIPS_MODULE |
902 | EVP_RAND_CTX *ossl_rand_get0_private_noncreating(OSSL_LIB_CTX *ctx) | |
903 | { | |
904 | RAND_GLOBAL *dgbl = rand_get_global(ctx); | |
905 | ||
906 | if (dgbl == NULL) | |
907 | return NULL; | |
908 | ||
ce990ce8 | 909 | return CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx); |
fffa78c2 P |
910 | } |
911 | #endif | |
912 | ||
7c8187d4 P |
913 | int RAND_set0_public(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand) |
914 | { | |
915 | RAND_GLOBAL *dgbl = rand_get_global(ctx); | |
916 | EVP_RAND_CTX *old; | |
917 | int r; | |
918 | ||
919 | if (dgbl == NULL) | |
920 | return 0; | |
ce990ce8 NH |
921 | old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx); |
922 | if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PUB_KEY, ctx, rand)) > 0) | |
7c8187d4 P |
923 | EVP_RAND_CTX_free(old); |
924 | return r; | |
925 | } | |
926 | ||
927 | int RAND_set0_private(OSSL_LIB_CTX *ctx, EVP_RAND_CTX *rand) | |
928 | { | |
929 | RAND_GLOBAL *dgbl = rand_get_global(ctx); | |
930 | EVP_RAND_CTX *old; | |
931 | int r; | |
932 | ||
933 | if (dgbl == NULL) | |
934 | return 0; | |
ce990ce8 NH |
935 | old = CRYPTO_THREAD_get_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx); |
936 | if ((r = CRYPTO_THREAD_set_local_ex(CRYPTO_THREAD_LOCAL_DRBG_PRIV_KEY, ctx, rand)) > 0) | |
7c8187d4 P |
937 | EVP_RAND_CTX_free(old); |
938 | return r; | |
939 | } | |
940 | ||
44d2482b P |
941 | #ifndef FIPS_MODULE |
942 | static int random_set_string(char **p, const char *s) | |
943 | { | |
786b13fa | 944 | char *d = NULL; |
44d2482b | 945 | |
786b13fa P |
946 | if (s != NULL) { |
947 | d = OPENSSL_strdup(s); | |
e077455e | 948 | if (d == NULL) |
786b13fa | 949 | return 0; |
44d2482b P |
950 | } |
951 | OPENSSL_free(*p); | |
952 | *p = d; | |
953 | return 1; | |
954 | } | |
955 | ||
956 | /* | |
957 | * Load the DRBG definitions from a configuration file. | |
958 | */ | |
959 | static int random_conf_init(CONF_IMODULE *md, const CONF *cnf) | |
960 | { | |
961 | STACK_OF(CONF_VALUE) *elist; | |
962 | CONF_VALUE *cval; | |
b1cca259 P |
963 | OSSL_LIB_CTX *libctx = NCONF_get0_libctx((CONF *)cnf); |
964 | RAND_GLOBAL *dgbl = rand_get_global(libctx); | |
44d2482b P |
965 | int i, r = 1; |
966 | ||
967 | OSSL_TRACE1(CONF, "Loading random module: section %s\n", | |
968 | CONF_imodule_get_value(md)); | |
969 | ||
970 | /* Value is a section containing RANDOM configuration */ | |
971 | elist = NCONF_get_section(cnf, CONF_imodule_get_value(md)); | |
972 | if (elist == NULL) { | |
9311d0c4 | 973 | ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_RANDOM_SECTION_ERROR); |
44d2482b P |
974 | return 0; |
975 | } | |
976 | ||
09dca557 JJ |
977 | if (dgbl == NULL) |
978 | return 0; | |
979 | ||
44d2482b P |
980 | for (i = 0; i < sk_CONF_VALUE_num(elist); i++) { |
981 | cval = sk_CONF_VALUE_value(elist, i); | |
fba140c7 | 982 | if (OPENSSL_strcasecmp(cval->name, "random") == 0) { |
44d2482b P |
983 | if (!random_set_string(&dgbl->rng_name, cval->value)) |
984 | return 0; | |
fba140c7 | 985 | } else if (OPENSSL_strcasecmp(cval->name, "cipher") == 0) { |
44d2482b P |
986 | if (!random_set_string(&dgbl->rng_cipher, cval->value)) |
987 | return 0; | |
fba140c7 | 988 | } else if (OPENSSL_strcasecmp(cval->name, "digest") == 0) { |
44d2482b P |
989 | if (!random_set_string(&dgbl->rng_digest, cval->value)) |
990 | return 0; | |
fba140c7 | 991 | } else if (OPENSSL_strcasecmp(cval->name, "properties") == 0) { |
44d2482b P |
992 | if (!random_set_string(&dgbl->rng_propq, cval->value)) |
993 | return 0; | |
fba140c7 | 994 | } else if (OPENSSL_strcasecmp(cval->name, "seed") == 0) { |
81aef6ba P |
995 | if (!random_set_string(&dgbl->seed_name, cval->value)) |
996 | return 0; | |
fba140c7 | 997 | } else if (OPENSSL_strcasecmp(cval->name, "seed_properties") == 0) { |
81aef6ba P |
998 | if (!random_set_string(&dgbl->seed_propq, cval->value)) |
999 | return 0; | |
b1cca259 P |
1000 | } else if (OPENSSL_strcasecmp(cval->name, "random_provider") == 0) { |
1001 | # ifndef FIPS_MODULE | |
1002 | OSSL_PROVIDER *prov = ossl_provider_find(libctx, cval->value, 0); | |
1003 | ||
1004 | if (prov != NULL) { | |
1005 | if (!RAND_set1_random_provider(libctx, prov)) { | |
1006 | ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); | |
1007 | OSSL_PROVIDER_unload(prov); | |
1008 | return 0; | |
1009 | } | |
1010 | /* | |
1011 | * We need to release the reference from ossl_provider_find because | |
1012 | * we don't want to keep a reference counted handle to the provider. | |
1013 | * | |
1014 | * The provider unload code checks for the random provider and, | |
1015 | * if present, our reference will be NULLed when it is fully freed. | |
1016 | * The provider load code, conversely, checks the provider name | |
1017 | * and re-hooks our reference if required. This means that a load, | |
1018 | * hook random provider, use, unload, reload, reuse sequence will | |
1019 | * work as expected. | |
1020 | */ | |
1021 | OSSL_PROVIDER_unload(prov); | |
1022 | } else if (!set_random_provider_name(dgbl, cval->value)) | |
1023 | return 0; | |
1024 | # endif | |
44d2482b | 1025 | } else { |
a150f8e1 RL |
1026 | ERR_raise_data(ERR_LIB_CRYPTO, |
1027 | CRYPTO_R_UNKNOWN_NAME_IN_RANDOM_SECTION, | |
1028 | "name=%s, value=%s", cval->name, cval->value); | |
44d2482b P |
1029 | r = 0; |
1030 | } | |
1031 | } | |
1032 | return r; | |
1033 | } | |
1034 | ||
1035 | ||
1036 | static void random_conf_deinit(CONF_IMODULE *md) | |
1037 | { | |
1038 | OSSL_TRACE(CONF, "Cleaned up random\n"); | |
1039 | } | |
1040 | ||
1041 | void ossl_random_add_conf_module(void) | |
1042 | { | |
1043 | OSSL_TRACE(CONF, "Adding config module 'random'\n"); | |
1044 | CONF_module_add("random", random_conf_init, random_conf_deinit); | |
1045 | } | |
786b13fa P |
1046 | |
1047 | int RAND_set_DRBG_type(OSSL_LIB_CTX *ctx, const char *drbg, const char *propq, | |
1048 | const char *cipher, const char *digest) | |
1049 | { | |
1050 | RAND_GLOBAL *dgbl = rand_get_global(ctx); | |
1051 | ||
1052 | if (dgbl == NULL) | |
1053 | return 0; | |
1054 | if (dgbl->primary != NULL) { | |
1055 | ERR_raise(ERR_LIB_CRYPTO, RAND_R_ALREADY_INSTANTIATED); | |
1056 | return 0; | |
1057 | } | |
1058 | return random_set_string(&dgbl->rng_name, drbg) | |
1059 | && random_set_string(&dgbl->rng_propq, propq) | |
1060 | && random_set_string(&dgbl->rng_cipher, cipher) | |
1061 | && random_set_string(&dgbl->rng_digest, digest); | |
1062 | } | |
1063 | ||
1064 | int RAND_set_seed_source_type(OSSL_LIB_CTX *ctx, const char *seed, | |
1065 | const char *propq) | |
1066 | { | |
1067 | RAND_GLOBAL *dgbl = rand_get_global(ctx); | |
1068 | ||
1069 | if (dgbl == NULL) | |
1070 | return 0; | |
4cde7585 | 1071 | if (dgbl->seed != NULL) { |
786b13fa P |
1072 | ERR_raise(ERR_LIB_CRYPTO, RAND_R_ALREADY_INSTANTIATED); |
1073 | return 0; | |
1074 | } | |
1075 | return random_set_string(&dgbl->seed_name, seed) | |
1076 | && random_set_string(&dgbl->seed_propq, propq); | |
1077 | } | |
1078 | ||
b1cca259 | 1079 | int RAND_set1_random_provider(OSSL_LIB_CTX *ctx, OSSL_PROVIDER *prov) |
4636a395 P |
1080 | { |
1081 | RAND_GLOBAL *dgbl = rand_get_global(ctx); | |
4636a395 P |
1082 | |
1083 | if (dgbl == NULL) | |
1084 | return 0; | |
b1cca259 P |
1085 | |
1086 | if (prov == NULL) { | |
1087 | OPENSSL_free(dgbl->random_provider_name); | |
1088 | dgbl->random_provider_name = NULL; | |
1089 | dgbl->random_provider = NULL; | |
1090 | return 1; | |
1091 | } | |
1092 | ||
1093 | if (dgbl->random_provider == prov) | |
1094 | return 1; | |
1095 | ||
1096 | if (!set_random_provider_name(dgbl, OSSL_PROVIDER_get0_name(prov))) | |
1097 | return 0; | |
1098 | ||
1099 | dgbl->random_provider = prov; | |
4636a395 P |
1100 | return 1; |
1101 | } | |
b1cca259 P |
1102 | |
1103 | /* | |
1104 | * When a new provider is loaded, we need to check to see if it is the | |
1105 | * designated randomness provider and register it if it is. | |
1106 | */ | |
1107 | int ossl_rand_check_random_provider_on_load(OSSL_LIB_CTX *ctx, | |
1108 | OSSL_PROVIDER *prov) | |
1109 | { | |
1110 | RAND_GLOBAL *dgbl = rand_get_global(ctx); | |
1111 | ||
1112 | if (dgbl == NULL) | |
1113 | return 0; | |
1114 | ||
1115 | /* No random provider name specified, or one is installed already */ | |
1116 | if (dgbl->random_provider_name == NULL || dgbl->random_provider != NULL) | |
1117 | return 1; | |
1118 | ||
1119 | /* Does this provider match the name we're using? */ | |
1120 | if (strcmp(dgbl->random_provider_name, OSSL_PROVIDER_get0_name(prov)) != 0) | |
1121 | return 1; | |
1122 | ||
1123 | dgbl->random_provider = prov; | |
1124 | return 1; | |
1125 | } | |
1126 | ||
1127 | /* | |
1128 | * When a provider is being unloaded, if it is the randomness provider, | |
1129 | * we need to deregister it. | |
1130 | */ | |
1131 | int ossl_rand_check_random_provider_on_unload(OSSL_LIB_CTX *ctx, | |
1132 | OSSL_PROVIDER *prov) | |
1133 | { | |
1134 | RAND_GLOBAL *dgbl = rand_get_global(ctx); | |
1135 | ||
1136 | if (dgbl == NULL) | |
1137 | return 0; | |
1138 | ||
1139 | if (dgbl->random_provider == prov) | |
1140 | dgbl->random_provider = NULL; | |
1141 | return 1; | |
1142 | } | |
1143 | ||
4636a395 | 1144 | #endif /* !FIPS_MODULE */ |