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