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