/src/openssl/crypto/rand/drbg_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 | | #include <string.h> |
11 | | #include <openssl/crypto.h> |
12 | | #include <openssl/err.h> |
13 | | #include <openssl/rand.h> |
14 | | #include "rand_lcl.h" |
15 | | #include "internal/thread_once.h" |
16 | | #include "internal/rand_int.h" |
17 | | #include "internal/cryptlib_int.h" |
18 | | |
19 | | /* |
20 | | * Support framework for NIST SP 800-90A DRBG |
21 | | * |
22 | | * See manual page RAND_DRBG(7) for a general overview. |
23 | | * |
24 | | * The OpenSSL model is to have new and free functions, and that new |
25 | | * does all initialization. That is not the NIST model, which has |
26 | | * instantiation and un-instantiate, and re-use within a new/free |
27 | | * lifecycle. (No doubt this comes from the desire to support hardware |
28 | | * DRBG, where allocation of resources on something like an HSM is |
29 | | * a much bigger deal than just re-setting an allocated resource.) |
30 | | */ |
31 | | |
32 | | /* |
33 | | * The three shared DRBG instances |
34 | | * |
35 | | * There are three shared DRBG instances: <master>, <public>, and <private>. |
36 | | */ |
37 | | |
38 | | /* |
39 | | * The <master> DRBG |
40 | | * |
41 | | * Not used directly by the application, only for reseeding the two other |
42 | | * DRBGs. It reseeds itself by pulling either randomness from os entropy |
43 | | * sources or by consuming randomness which was added by RAND_add(). |
44 | | * |
45 | | * The <master> DRBG is a global instance which is accessed concurrently by |
46 | | * all threads. The necessary locking is managed automatically by its child |
47 | | * DRBG instances during reseeding. |
48 | | */ |
49 | | static RAND_DRBG *master_drbg; |
50 | | /* |
51 | | * The <public> DRBG |
52 | | * |
53 | | * Used by default for generating random bytes using RAND_bytes(). |
54 | | * |
55 | | * The <public> DRBG is thread-local, i.e., there is one instance per thread. |
56 | | */ |
57 | | static CRYPTO_THREAD_LOCAL public_drbg; |
58 | | /* |
59 | | * The <private> DRBG |
60 | | * |
61 | | * Used by default for generating private keys using RAND_priv_bytes() |
62 | | * |
63 | | * The <private> DRBG is thread-local, i.e., there is one instance per thread. |
64 | | */ |
65 | | static CRYPTO_THREAD_LOCAL private_drbg; |
66 | | |
67 | | |
68 | | |
69 | | /* NIST SP 800-90A DRBG recommends the use of a personalization string. */ |
70 | | static const char ossl_pers_string[] = "OpenSSL NIST SP 800-90A DRBG"; |
71 | | |
72 | | static CRYPTO_ONCE rand_drbg_init = CRYPTO_ONCE_STATIC_INIT; |
73 | | |
74 | | |
75 | | |
76 | | static int rand_drbg_type = RAND_DRBG_TYPE; |
77 | | static unsigned int rand_drbg_flags = RAND_DRBG_FLAGS; |
78 | | |
79 | | static unsigned int master_reseed_interval = MASTER_RESEED_INTERVAL; |
80 | | static unsigned int slave_reseed_interval = SLAVE_RESEED_INTERVAL; |
81 | | |
82 | | static time_t master_reseed_time_interval = MASTER_RESEED_TIME_INTERVAL; |
83 | | static time_t slave_reseed_time_interval = SLAVE_RESEED_TIME_INTERVAL; |
84 | | |
85 | | static RAND_DRBG *drbg_setup(RAND_DRBG *parent); |
86 | | |
87 | | static RAND_DRBG *rand_drbg_new(int secure, |
88 | | int type, |
89 | | unsigned int flags, |
90 | | RAND_DRBG *parent); |
91 | | |
92 | | /* |
93 | | * Set/initialize |drbg| to be of type |type|, with optional |flags|. |
94 | | * |
95 | | * If |type| and |flags| are zero, use the defaults |
96 | | * |
97 | | * Returns 1 on success, 0 on failure. |
98 | | */ |
99 | | int RAND_DRBG_set(RAND_DRBG *drbg, int type, unsigned int flags) |
100 | 0 | { |
101 | 0 | int ret = 1; |
102 | 0 |
|
103 | 0 | if (type == 0 && flags == 0) { |
104 | 0 | type = rand_drbg_type; |
105 | 0 | flags = rand_drbg_flags; |
106 | 0 | } |
107 | 0 |
|
108 | 0 | drbg->state = DRBG_UNINITIALISED; |
109 | 0 | drbg->flags = flags; |
110 | 0 | drbg->type = type; |
111 | 0 |
|
112 | 0 | switch (type) { |
113 | 0 | default: |
114 | 0 | RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_UNSUPPORTED_DRBG_TYPE); |
115 | 0 | return 0; |
116 | 0 | case 0: |
117 | 0 | /* Uninitialized; that's okay. */ |
118 | 0 | return 1; |
119 | 0 | case NID_aes_128_ctr: |
120 | 0 | case NID_aes_192_ctr: |
121 | 0 | case NID_aes_256_ctr: |
122 | 0 | ret = drbg_ctr_init(drbg); |
123 | 0 | break; |
124 | 0 | } |
125 | 0 |
|
126 | 0 | if (ret == 0) |
127 | 0 | RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_ERROR_INITIALISING_DRBG); |
128 | 0 | return ret; |
129 | 0 | } |
130 | | |
131 | | /* |
132 | | * Set/initialize default |type| and |flag| for new drbg instances. |
133 | | * |
134 | | * Returns 1 on success, 0 on failure. |
135 | | */ |
136 | | int RAND_DRBG_set_defaults(int type, unsigned int flags) |
137 | 0 | { |
138 | 0 | int ret = 1; |
139 | 0 |
|
140 | 0 | switch (type) { |
141 | 0 | default: |
142 | 0 | RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_TYPE); |
143 | 0 | return 0; |
144 | 0 | case NID_aes_128_ctr: |
145 | 0 | case NID_aes_192_ctr: |
146 | 0 | case NID_aes_256_ctr: |
147 | 0 | break; |
148 | 0 | } |
149 | 0 |
|
150 | 0 | if ((flags & ~RAND_DRBG_USED_FLAGS) != 0) { |
151 | 0 | RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_FLAGS); |
152 | 0 | return 0; |
153 | 0 | } |
154 | 0 |
|
155 | 0 | rand_drbg_type = type; |
156 | 0 | rand_drbg_flags = flags; |
157 | 0 |
|
158 | 0 | return ret; |
159 | 0 | } |
160 | | |
161 | | |
162 | | /* |
163 | | * Allocate memory and initialize a new DRBG. The DRBG is allocated on |
164 | | * the secure heap if |secure| is nonzero and the secure heap is enabled. |
165 | | * The |parent|, if not NULL, will be used as random source for reseeding. |
166 | | * |
167 | | * Returns a pointer to the new DRBG instance on success, NULL on failure. |
168 | | */ |
169 | | static RAND_DRBG *rand_drbg_new(int secure, |
170 | | int type, |
171 | | unsigned int flags, |
172 | | RAND_DRBG *parent) |
173 | 0 | { |
174 | 0 | RAND_DRBG *drbg = secure ? |
175 | 0 | OPENSSL_secure_zalloc(sizeof(*drbg)) : OPENSSL_zalloc(sizeof(*drbg)); |
176 | 0 |
|
177 | 0 | if (drbg == NULL) { |
178 | 0 | RANDerr(RAND_F_RAND_DRBG_NEW, ERR_R_MALLOC_FAILURE); |
179 | 0 | return NULL; |
180 | 0 | } |
181 | 0 |
|
182 | 0 | drbg->secure = secure && CRYPTO_secure_allocated(drbg); |
183 | 0 | drbg->fork_count = rand_fork_count; |
184 | 0 | drbg->parent = parent; |
185 | 0 |
|
186 | 0 | if (parent == NULL) { |
187 | 0 | drbg->get_entropy = rand_drbg_get_entropy; |
188 | 0 | drbg->cleanup_entropy = rand_drbg_cleanup_entropy; |
189 | 0 | #ifndef RAND_DRBG_GET_RANDOM_NONCE |
190 | 0 | drbg->get_nonce = rand_drbg_get_nonce; |
191 | 0 | drbg->cleanup_nonce = rand_drbg_cleanup_nonce; |
192 | 0 | #endif |
193 | 0 |
|
194 | 0 | drbg->reseed_interval = master_reseed_interval; |
195 | 0 | drbg->reseed_time_interval = master_reseed_time_interval; |
196 | 0 | } else { |
197 | 0 | drbg->get_entropy = rand_drbg_get_entropy; |
198 | 0 | drbg->cleanup_entropy = rand_drbg_cleanup_entropy; |
199 | 0 | /* |
200 | 0 | * Do not provide nonce callbacks, the child DRBGs will |
201 | 0 | * obtain their nonce using random bits from the parent. |
202 | 0 | */ |
203 | 0 |
|
204 | 0 | drbg->reseed_interval = slave_reseed_interval; |
205 | 0 | drbg->reseed_time_interval = slave_reseed_time_interval; |
206 | 0 | } |
207 | 0 |
|
208 | 0 | if (RAND_DRBG_set(drbg, type, flags) == 0) |
209 | 0 | goto err; |
210 | 0 | |
211 | 0 | if (parent != NULL) { |
212 | 0 | rand_drbg_lock(parent); |
213 | 0 | if (drbg->strength > parent->strength) { |
214 | 0 | /* |
215 | 0 | * We currently don't support the algorithm from NIST SP 800-90C |
216 | 0 | * 10.1.2 to use a weaker DRBG as source |
217 | 0 | */ |
218 | 0 | rand_drbg_unlock(parent); |
219 | 0 | RANDerr(RAND_F_RAND_DRBG_NEW, RAND_R_PARENT_STRENGTH_TOO_WEAK); |
220 | 0 | goto err; |
221 | 0 | } |
222 | 0 | rand_drbg_unlock(parent); |
223 | 0 | } |
224 | 0 |
|
225 | 0 | return drbg; |
226 | 0 | |
227 | 0 | err: |
228 | 0 | if (drbg->secure) |
229 | 0 | OPENSSL_secure_free(drbg); |
230 | 0 | else |
231 | 0 | OPENSSL_free(drbg); |
232 | 0 |
|
233 | 0 | return NULL; |
234 | 0 | } |
235 | | |
236 | | RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent) |
237 | 0 | { |
238 | 0 | return rand_drbg_new(0, type, flags, parent); |
239 | 0 | } |
240 | | |
241 | | RAND_DRBG *RAND_DRBG_secure_new(int type, unsigned int flags, RAND_DRBG *parent) |
242 | 0 | { |
243 | 0 | return rand_drbg_new(1, type, flags, parent); |
244 | 0 | } |
245 | | |
246 | | /* |
247 | | * Uninstantiate |drbg| and free all memory. |
248 | | */ |
249 | | void RAND_DRBG_free(RAND_DRBG *drbg) |
250 | 0 | { |
251 | 0 | if (drbg == NULL) |
252 | 0 | return; |
253 | 0 | |
254 | 0 | if (drbg->meth != NULL) |
255 | 0 | drbg->meth->uninstantiate(drbg); |
256 | 0 | CRYPTO_THREAD_lock_free(drbg->lock); |
257 | 0 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data); |
258 | 0 |
|
259 | 0 | if (drbg->secure) |
260 | 0 | OPENSSL_secure_clear_free(drbg, sizeof(*drbg)); |
261 | 0 | else |
262 | 0 | OPENSSL_clear_free(drbg, sizeof(*drbg)); |
263 | 0 | } |
264 | | |
265 | | /* |
266 | | * Instantiate |drbg|, after it has been initialized. Use |pers| and |
267 | | * |perslen| as prediction-resistance input. |
268 | | * |
269 | | * Requires that drbg->lock is already locked for write, if non-null. |
270 | | * |
271 | | * Returns 1 on success, 0 on failure. |
272 | | */ |
273 | | int RAND_DRBG_instantiate(RAND_DRBG *drbg, |
274 | | const unsigned char *pers, size_t perslen) |
275 | 0 | { |
276 | 0 | unsigned char *nonce = NULL, *entropy = NULL; |
277 | 0 | size_t noncelen = 0, entropylen = 0; |
278 | 0 | size_t min_entropy = drbg->strength; |
279 | 0 | size_t min_entropylen = drbg->min_entropylen; |
280 | 0 | size_t max_entropylen = drbg->max_entropylen; |
281 | 0 |
|
282 | 0 | if (perslen > drbg->max_perslen) { |
283 | 0 | RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, |
284 | 0 | RAND_R_PERSONALISATION_STRING_TOO_LONG); |
285 | 0 | goto end; |
286 | 0 | } |
287 | 0 |
|
288 | 0 | if (drbg->meth == NULL) { |
289 | 0 | RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, |
290 | 0 | RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED); |
291 | 0 | goto end; |
292 | 0 | } |
293 | 0 |
|
294 | 0 | if (drbg->state != DRBG_UNINITIALISED) { |
295 | 0 | RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, |
296 | 0 | drbg->state == DRBG_ERROR ? RAND_R_IN_ERROR_STATE |
297 | 0 | : RAND_R_ALREADY_INSTANTIATED); |
298 | 0 | goto end; |
299 | 0 | } |
300 | 0 |
|
301 | 0 | drbg->state = DRBG_ERROR; |
302 | 0 |
|
303 | 0 | /* |
304 | 0 | * NIST SP800-90Ar1 section 9.1 says you can combine getting the entropy |
305 | 0 | * and nonce in 1 call by increasing the entropy with 50% and increasing |
306 | 0 | * the minimum length to accomadate the length of the nonce. |
307 | 0 | * We do this in case a nonce is require and get_nonce is NULL. |
308 | 0 | */ |
309 | 0 | if (drbg->min_noncelen > 0 && drbg->get_nonce == NULL) { |
310 | 0 | min_entropy += drbg->strength / 2; |
311 | 0 | min_entropylen += drbg->min_noncelen; |
312 | 0 | max_entropylen += drbg->max_noncelen; |
313 | 0 | } |
314 | 0 |
|
315 | 0 | if (drbg->get_entropy != NULL) |
316 | 0 | entropylen = drbg->get_entropy(drbg, &entropy, min_entropy, |
317 | 0 | min_entropylen, max_entropylen, 0); |
318 | 0 | if (entropylen < min_entropylen |
319 | 0 | || entropylen > max_entropylen) { |
320 | 0 | RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_ENTROPY); |
321 | 0 | goto end; |
322 | 0 | } |
323 | 0 |
|
324 | 0 | if (drbg->min_noncelen > 0 && drbg->get_nonce != NULL) { |
325 | 0 | noncelen = drbg->get_nonce(drbg, &nonce, drbg->strength / 2, |
326 | 0 | drbg->min_noncelen, drbg->max_noncelen); |
327 | 0 | if (noncelen < drbg->min_noncelen || noncelen > drbg->max_noncelen) { |
328 | 0 | RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_NONCE); |
329 | 0 | goto end; |
330 | 0 | } |
331 | 0 | } |
332 | 0 |
|
333 | 0 | if (!drbg->meth->instantiate(drbg, entropy, entropylen, |
334 | 0 | nonce, noncelen, pers, perslen)) { |
335 | 0 | RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_INSTANTIATING_DRBG); |
336 | 0 | goto end; |
337 | 0 | } |
338 | 0 |
|
339 | 0 | drbg->state = DRBG_READY; |
340 | 0 | drbg->generate_counter = 0; |
341 | 0 | drbg->reseed_time = time(NULL); |
342 | 0 | if (drbg->reseed_counter > 0) { |
343 | 0 | if (drbg->parent == NULL) |
344 | 0 | drbg->reseed_counter++; |
345 | 0 | else |
346 | 0 | drbg->reseed_counter = drbg->parent->reseed_counter; |
347 | 0 | } |
348 | 0 |
|
349 | 0 | end: |
350 | 0 | if (entropy != NULL && drbg->cleanup_entropy != NULL) |
351 | 0 | drbg->cleanup_entropy(drbg, entropy, entropylen); |
352 | 0 | if (nonce != NULL && drbg->cleanup_nonce!= NULL ) |
353 | 0 | drbg->cleanup_nonce(drbg, nonce, noncelen); |
354 | 0 | if (drbg->pool != NULL) { |
355 | 0 | if (drbg->state == DRBG_READY) { |
356 | 0 | RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, |
357 | 0 | RAND_R_ERROR_ENTROPY_POOL_WAS_IGNORED); |
358 | 0 | drbg->state = DRBG_ERROR; |
359 | 0 | } |
360 | 0 | rand_pool_free(drbg->pool); |
361 | 0 | drbg->pool = NULL; |
362 | 0 | } |
363 | 0 | if (drbg->state == DRBG_READY) |
364 | 0 | return 1; |
365 | 0 | return 0; |
366 | 0 | } |
367 | | |
368 | | /* |
369 | | * Uninstantiate |drbg|. Must be instantiated before it can be used. |
370 | | * |
371 | | * Requires that drbg->lock is already locked for write, if non-null. |
372 | | * |
373 | | * Returns 1 on success, 0 on failure. |
374 | | */ |
375 | | int RAND_DRBG_uninstantiate(RAND_DRBG *drbg) |
376 | 0 | { |
377 | 0 | if (drbg->meth == NULL) { |
378 | 0 | RANDerr(RAND_F_RAND_DRBG_UNINSTANTIATE, |
379 | 0 | RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED); |
380 | 0 | return 0; |
381 | 0 | } |
382 | 0 |
|
383 | 0 | /* Clear the entire drbg->ctr struct, then reset some important |
384 | 0 | * members of the drbg->ctr struct (e.g. keysize, df_ks) to their |
385 | 0 | * initial values. |
386 | 0 | */ |
387 | 0 | drbg->meth->uninstantiate(drbg); |
388 | 0 | return RAND_DRBG_set(drbg, drbg->type, drbg->flags); |
389 | 0 | } |
390 | | |
391 | | /* |
392 | | * Reseed |drbg|, mixing in the specified data |
393 | | * |
394 | | * Requires that drbg->lock is already locked for write, if non-null. |
395 | | * |
396 | | * Returns 1 on success, 0 on failure. |
397 | | */ |
398 | | int RAND_DRBG_reseed(RAND_DRBG *drbg, |
399 | | const unsigned char *adin, size_t adinlen, |
400 | | int prediction_resistance) |
401 | 0 | { |
402 | 0 | unsigned char *entropy = NULL; |
403 | 0 | size_t entropylen = 0; |
404 | 0 |
|
405 | 0 | if (drbg->state == DRBG_ERROR) { |
406 | 0 | RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_IN_ERROR_STATE); |
407 | 0 | return 0; |
408 | 0 | } |
409 | 0 | if (drbg->state == DRBG_UNINITIALISED) { |
410 | 0 | RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_NOT_INSTANTIATED); |
411 | 0 | return 0; |
412 | 0 | } |
413 | 0 |
|
414 | 0 | if (adin == NULL) { |
415 | 0 | adinlen = 0; |
416 | 0 | } else if (adinlen > drbg->max_adinlen) { |
417 | 0 | RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ADDITIONAL_INPUT_TOO_LONG); |
418 | 0 | return 0; |
419 | 0 | } |
420 | 0 |
|
421 | 0 | drbg->state = DRBG_ERROR; |
422 | 0 | if (drbg->get_entropy != NULL) |
423 | 0 | entropylen = drbg->get_entropy(drbg, &entropy, drbg->strength, |
424 | 0 | drbg->min_entropylen, |
425 | 0 | drbg->max_entropylen, |
426 | 0 | prediction_resistance); |
427 | 0 | if (entropylen < drbg->min_entropylen |
428 | 0 | || entropylen > drbg->max_entropylen) { |
429 | 0 | RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ERROR_RETRIEVING_ENTROPY); |
430 | 0 | goto end; |
431 | 0 | } |
432 | 0 |
|
433 | 0 | if (!drbg->meth->reseed(drbg, entropy, entropylen, adin, adinlen)) |
434 | 0 | goto end; |
435 | 0 | |
436 | 0 | drbg->state = DRBG_READY; |
437 | 0 | drbg->generate_counter = 0; |
438 | 0 | drbg->reseed_time = time(NULL); |
439 | 0 | if (drbg->reseed_counter > 0) { |
440 | 0 | if (drbg->parent == NULL) |
441 | 0 | drbg->reseed_counter++; |
442 | 0 | else |
443 | 0 | drbg->reseed_counter = drbg->parent->reseed_counter; |
444 | 0 | } |
445 | 0 |
|
446 | 0 | end: |
447 | 0 | if (entropy != NULL && drbg->cleanup_entropy != NULL) |
448 | 0 | drbg->cleanup_entropy(drbg, entropy, entropylen); |
449 | 0 | if (drbg->state == DRBG_READY) |
450 | 0 | return 1; |
451 | 0 | return 0; |
452 | 0 | } |
453 | | |
454 | | /* |
455 | | * Restart |drbg|, using the specified entropy or additional input |
456 | | * |
457 | | * Tries its best to get the drbg instantiated by all means, |
458 | | * regardless of its current state. |
459 | | * |
460 | | * Optionally, a |buffer| of |len| random bytes can be passed, |
461 | | * which is assumed to contain at least |entropy| bits of entropy. |
462 | | * |
463 | | * If |entropy| > 0, the buffer content is used as entropy input. |
464 | | * |
465 | | * If |entropy| == 0, the buffer content is used as additional input |
466 | | * |
467 | | * Returns 1 on success, 0 on failure. |
468 | | * |
469 | | * This function is used internally only. |
470 | | */ |
471 | | int rand_drbg_restart(RAND_DRBG *drbg, |
472 | | const unsigned char *buffer, size_t len, size_t entropy) |
473 | 0 | { |
474 | 0 | int reseeded = 0; |
475 | 0 | const unsigned char *adin = NULL; |
476 | 0 | size_t adinlen = 0; |
477 | 0 |
|
478 | 0 | if (drbg->pool != NULL) { |
479 | 0 | RANDerr(RAND_F_RAND_DRBG_RESTART, ERR_R_INTERNAL_ERROR); |
480 | 0 | rand_pool_free(drbg->pool); |
481 | 0 | drbg->pool = NULL; |
482 | 0 | } |
483 | 0 |
|
484 | 0 | if (buffer != NULL) { |
485 | 0 | if (entropy > 0) { |
486 | 0 | if (drbg->max_entropylen < len) { |
487 | 0 | RANDerr(RAND_F_RAND_DRBG_RESTART, |
488 | 0 | RAND_R_ENTROPY_INPUT_TOO_LONG); |
489 | 0 | return 0; |
490 | 0 | } |
491 | 0 |
|
492 | 0 | if (entropy > 8 * len) { |
493 | 0 | RANDerr(RAND_F_RAND_DRBG_RESTART, RAND_R_ENTROPY_OUT_OF_RANGE); |
494 | 0 | return 0; |
495 | 0 | } |
496 | 0 |
|
497 | 0 | /* will be picked up by the rand_drbg_get_entropy() callback */ |
498 | 0 | drbg->pool = rand_pool_new(entropy, len, len); |
499 | 0 | if (drbg->pool == NULL) |
500 | 0 | return 0; |
501 | 0 | |
502 | 0 | rand_pool_add(drbg->pool, buffer, len, entropy); |
503 | 0 | } else { |
504 | 0 | if (drbg->max_adinlen < len) { |
505 | 0 | RANDerr(RAND_F_RAND_DRBG_RESTART, |
506 | 0 | RAND_R_ADDITIONAL_INPUT_TOO_LONG); |
507 | 0 | return 0; |
508 | 0 | } |
509 | 0 | adin = buffer; |
510 | 0 | adinlen = len; |
511 | 0 | } |
512 | 0 | } |
513 | 0 |
|
514 | 0 | /* repair error state */ |
515 | 0 | if (drbg->state == DRBG_ERROR) |
516 | 0 | RAND_DRBG_uninstantiate(drbg); |
517 | 0 |
|
518 | 0 | /* repair uninitialized state */ |
519 | 0 | if (drbg->state == DRBG_UNINITIALISED) { |
520 | 0 | /* reinstantiate drbg */ |
521 | 0 | RAND_DRBG_instantiate(drbg, |
522 | 0 | (const unsigned char *) ossl_pers_string, |
523 | 0 | sizeof(ossl_pers_string) - 1); |
524 | 0 | /* already reseeded. prevent second reseeding below */ |
525 | 0 | reseeded = (drbg->state == DRBG_READY); |
526 | 0 | } |
527 | 0 |
|
528 | 0 | /* refresh current state if entropy or additional input has been provided */ |
529 | 0 | if (drbg->state == DRBG_READY) { |
530 | 0 | if (adin != NULL) { |
531 | 0 | /* |
532 | 0 | * mix in additional input without reseeding |
533 | 0 | * |
534 | 0 | * Similar to RAND_DRBG_reseed(), but the provided additional |
535 | 0 | * data |adin| is mixed into the current state without pulling |
536 | 0 | * entropy from the trusted entropy source using get_entropy(). |
537 | 0 | * This is not a reseeding in the strict sense of NIST SP 800-90A. |
538 | 0 | */ |
539 | 0 | drbg->meth->reseed(drbg, adin, adinlen, NULL, 0); |
540 | 0 | } else if (reseeded == 0) { |
541 | 0 | /* do a full reseeding if it has not been done yet above */ |
542 | 0 | RAND_DRBG_reseed(drbg, NULL, 0, 0); |
543 | 0 | } |
544 | 0 | } |
545 | 0 |
|
546 | 0 | /* check whether a given entropy pool was cleared properly during reseed */ |
547 | 0 | if (drbg->pool != NULL) { |
548 | 0 | drbg->state = DRBG_ERROR; |
549 | 0 | RANDerr(RAND_F_RAND_DRBG_RESTART, ERR_R_INTERNAL_ERROR); |
550 | 0 | rand_pool_free(drbg->pool); |
551 | 0 | drbg->pool = NULL; |
552 | 0 | return 0; |
553 | 0 | } |
554 | 0 |
|
555 | 0 | return drbg->state == DRBG_READY; |
556 | 0 | } |
557 | | |
558 | | /* |
559 | | * Generate |outlen| bytes into the buffer at |out|. Reseed if we need |
560 | | * to or if |prediction_resistance| is set. Additional input can be |
561 | | * sent in |adin| and |adinlen|. |
562 | | * |
563 | | * Requires that drbg->lock is already locked for write, if non-null. |
564 | | * |
565 | | * Returns 1 on success, 0 on failure. |
566 | | * |
567 | | */ |
568 | | int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen, |
569 | | int prediction_resistance, |
570 | | const unsigned char *adin, size_t adinlen) |
571 | 0 | { |
572 | 0 | int reseed_required = 0; |
573 | 0 |
|
574 | 0 | if (drbg->state != DRBG_READY) { |
575 | 0 | /* try to recover from previous errors */ |
576 | 0 | rand_drbg_restart(drbg, NULL, 0, 0); |
577 | 0 |
|
578 | 0 | if (drbg->state == DRBG_ERROR) { |
579 | 0 | RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_IN_ERROR_STATE); |
580 | 0 | return 0; |
581 | 0 | } |
582 | 0 | if (drbg->state == DRBG_UNINITIALISED) { |
583 | 0 | RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_NOT_INSTANTIATED); |
584 | 0 | return 0; |
585 | 0 | } |
586 | 0 | } |
587 | 0 |
|
588 | 0 | if (outlen > drbg->max_request) { |
589 | 0 | RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG); |
590 | 0 | return 0; |
591 | 0 | } |
592 | 0 | if (adinlen > drbg->max_adinlen) { |
593 | 0 | RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_ADDITIONAL_INPUT_TOO_LONG); |
594 | 0 | return 0; |
595 | 0 | } |
596 | 0 |
|
597 | 0 | if (drbg->fork_count != rand_fork_count) { |
598 | 0 | drbg->fork_count = rand_fork_count; |
599 | 0 | reseed_required = 1; |
600 | 0 | } |
601 | 0 |
|
602 | 0 | if (drbg->reseed_interval > 0) { |
603 | 0 | if (drbg->generate_counter >= drbg->reseed_interval) |
604 | 0 | reseed_required = 1; |
605 | 0 | } |
606 | 0 | if (drbg->reseed_time_interval > 0) { |
607 | 0 | time_t now = time(NULL); |
608 | 0 | if (now < drbg->reseed_time |
609 | 0 | || now - drbg->reseed_time >= drbg->reseed_time_interval) |
610 | 0 | reseed_required = 1; |
611 | 0 | } |
612 | 0 | if (drbg->reseed_counter > 0 && drbg->parent != NULL) { |
613 | 0 | if (drbg->reseed_counter != drbg->parent->reseed_counter) |
614 | 0 | reseed_required = 1; |
615 | 0 | } |
616 | 0 |
|
617 | 0 | if (reseed_required || prediction_resistance) { |
618 | 0 | if (!RAND_DRBG_reseed(drbg, adin, adinlen, prediction_resistance)) { |
619 | 0 | RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_RESEED_ERROR); |
620 | 0 | return 0; |
621 | 0 | } |
622 | 0 | adin = NULL; |
623 | 0 | adinlen = 0; |
624 | 0 | } |
625 | 0 |
|
626 | 0 | if (!drbg->meth->generate(drbg, out, outlen, adin, adinlen)) { |
627 | 0 | drbg->state = DRBG_ERROR; |
628 | 0 | RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_GENERATE_ERROR); |
629 | 0 | return 0; |
630 | 0 | } |
631 | 0 |
|
632 | 0 | drbg->generate_counter++; |
633 | 0 |
|
634 | 0 | return 1; |
635 | 0 | } |
636 | | |
637 | | /* |
638 | | * Generates |outlen| random bytes and stores them in |out|. It will |
639 | | * using the given |drbg| to generate the bytes. |
640 | | * |
641 | | * Requires that drbg->lock is already locked for write, if non-null. |
642 | | * |
643 | | * Returns 1 on success 0 on failure. |
644 | | */ |
645 | | int RAND_DRBG_bytes(RAND_DRBG *drbg, unsigned char *out, size_t outlen) |
646 | 0 | { |
647 | 0 | unsigned char *additional = NULL; |
648 | 0 | size_t additional_len; |
649 | 0 | size_t chunk; |
650 | 0 | size_t ret; |
651 | 0 |
|
652 | 0 | additional_len = rand_drbg_get_additional_data(&additional, drbg->max_adinlen); |
653 | 0 |
|
654 | 0 | for ( ; outlen > 0; outlen -= chunk, out += chunk) { |
655 | 0 | chunk = outlen; |
656 | 0 | if (chunk > drbg->max_request) |
657 | 0 | chunk = drbg->max_request; |
658 | 0 | ret = RAND_DRBG_generate(drbg, out, chunk, 0, additional, additional_len); |
659 | 0 | if (!ret) |
660 | 0 | goto err; |
661 | 0 | } |
662 | 0 | ret = 1; |
663 | 0 |
|
664 | 0 | err: |
665 | 0 | if (additional_len != 0) |
666 | 0 | OPENSSL_secure_clear_free(additional, additional_len); |
667 | 0 |
|
668 | 0 | return ret; |
669 | 0 | } |
670 | | |
671 | | /* |
672 | | * Set the RAND_DRBG callbacks for obtaining entropy and nonce. |
673 | | * |
674 | | * Setting the callbacks is allowed only if the drbg has not been |
675 | | * initialized yet. Otherwise, the operation will fail. |
676 | | * |
677 | | * Returns 1 on success, 0 on failure. |
678 | | */ |
679 | | int RAND_DRBG_set_callbacks(RAND_DRBG *drbg, |
680 | | RAND_DRBG_get_entropy_fn get_entropy, |
681 | | RAND_DRBG_cleanup_entropy_fn cleanup_entropy, |
682 | | RAND_DRBG_get_nonce_fn get_nonce, |
683 | | RAND_DRBG_cleanup_nonce_fn cleanup_nonce) |
684 | 0 | { |
685 | 0 | if (drbg->state != DRBG_UNINITIALISED) |
686 | 0 | return 0; |
687 | 0 | drbg->get_entropy = get_entropy; |
688 | 0 | drbg->cleanup_entropy = cleanup_entropy; |
689 | 0 | drbg->get_nonce = get_nonce; |
690 | 0 | drbg->cleanup_nonce = cleanup_nonce; |
691 | 0 | return 1; |
692 | 0 | } |
693 | | |
694 | | /* |
695 | | * Set the reseed interval. |
696 | | * |
697 | | * The drbg will reseed automatically whenever the number of generate |
698 | | * requests exceeds the given reseed interval. If the reseed interval |
699 | | * is 0, then this feature is disabled. |
700 | | * |
701 | | * Returns 1 on success, 0 on failure. |
702 | | */ |
703 | | int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval) |
704 | 0 | { |
705 | 0 | if (interval > MAX_RESEED_INTERVAL) |
706 | 0 | return 0; |
707 | 0 | drbg->reseed_interval = interval; |
708 | 0 | return 1; |
709 | 0 | } |
710 | | |
711 | | /* |
712 | | * Set the reseed time interval. |
713 | | * |
714 | | * The drbg will reseed automatically whenever the time elapsed since |
715 | | * the last reseeding exceeds the given reseed time interval. For safety, |
716 | | * a reseeding will also occur if the clock has been reset to a smaller |
717 | | * value. |
718 | | * |
719 | | * Returns 1 on success, 0 on failure. |
720 | | */ |
721 | | int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval) |
722 | 0 | { |
723 | 0 | if (interval > MAX_RESEED_TIME_INTERVAL) |
724 | 0 | return 0; |
725 | 0 | drbg->reseed_time_interval = interval; |
726 | 0 | return 1; |
727 | 0 | } |
728 | | |
729 | | /* |
730 | | * Set the default values for reseed (time) intervals of new DRBG instances |
731 | | * |
732 | | * The default values can be set independently for master DRBG instances |
733 | | * (without a parent) and slave DRBG instances (with parent). |
734 | | * |
735 | | * Returns 1 on success, 0 on failure. |
736 | | */ |
737 | | |
738 | | int RAND_DRBG_set_reseed_defaults( |
739 | | unsigned int _master_reseed_interval, |
740 | | unsigned int _slave_reseed_interval, |
741 | | time_t _master_reseed_time_interval, |
742 | | time_t _slave_reseed_time_interval |
743 | | ) |
744 | 0 | { |
745 | 0 | if (_master_reseed_interval > MAX_RESEED_INTERVAL |
746 | 0 | || _slave_reseed_interval > MAX_RESEED_INTERVAL) |
747 | 0 | return 0; |
748 | 0 | |
749 | 0 | if (_master_reseed_time_interval > MAX_RESEED_TIME_INTERVAL |
750 | 0 | || _slave_reseed_time_interval > MAX_RESEED_TIME_INTERVAL) |
751 | 0 | return 0; |
752 | 0 | |
753 | 0 | master_reseed_interval = _master_reseed_interval; |
754 | 0 | slave_reseed_interval = _slave_reseed_interval; |
755 | 0 |
|
756 | 0 | master_reseed_time_interval = _master_reseed_time_interval; |
757 | 0 | slave_reseed_time_interval = _slave_reseed_time_interval; |
758 | 0 |
|
759 | 0 | return 1; |
760 | 0 | } |
761 | | |
762 | | /* |
763 | | * Locks the given drbg. Locking a drbg which does not have locking |
764 | | * enabled is considered a successful no-op. |
765 | | * |
766 | | * Returns 1 on success, 0 on failure. |
767 | | */ |
768 | | int rand_drbg_lock(RAND_DRBG *drbg) |
769 | 0 | { |
770 | 0 | if (drbg->lock != NULL) |
771 | 0 | return CRYPTO_THREAD_write_lock(drbg->lock); |
772 | 0 | |
773 | 0 | return 1; |
774 | 0 | } |
775 | | |
776 | | /* |
777 | | * Unlocks the given drbg. Unlocking a drbg which does not have locking |
778 | | * enabled is considered a successful no-op. |
779 | | * |
780 | | * Returns 1 on success, 0 on failure. |
781 | | */ |
782 | | int rand_drbg_unlock(RAND_DRBG *drbg) |
783 | 0 | { |
784 | 0 | if (drbg->lock != NULL) |
785 | 0 | return CRYPTO_THREAD_unlock(drbg->lock); |
786 | 0 | |
787 | 0 | return 1; |
788 | 0 | } |
789 | | |
790 | | /* |
791 | | * Enables locking for the given drbg |
792 | | * |
793 | | * Locking can only be enabled if the random generator |
794 | | * is in the uninitialized state. |
795 | | * |
796 | | * Returns 1 on success, 0 on failure. |
797 | | */ |
798 | | int rand_drbg_enable_locking(RAND_DRBG *drbg) |
799 | 0 | { |
800 | 0 | if (drbg->state != DRBG_UNINITIALISED) { |
801 | 0 | RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING, |
802 | 0 | RAND_R_DRBG_ALREADY_INITIALIZED); |
803 | 0 | return 0; |
804 | 0 | } |
805 | 0 |
|
806 | 0 | if (drbg->lock == NULL) { |
807 | 0 | if (drbg->parent != NULL && drbg->parent->lock == NULL) { |
808 | 0 | RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING, |
809 | 0 | RAND_R_PARENT_LOCKING_NOT_ENABLED); |
810 | 0 | return 0; |
811 | 0 | } |
812 | 0 |
|
813 | 0 | drbg->lock = CRYPTO_THREAD_lock_new(); |
814 | 0 | if (drbg->lock == NULL) { |
815 | 0 | RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING, |
816 | 0 | RAND_R_FAILED_TO_CREATE_LOCK); |
817 | 0 | return 0; |
818 | 0 | } |
819 | 0 | } |
820 | 0 |
|
821 | 0 | return 1; |
822 | 0 | } |
823 | | |
824 | | /* |
825 | | * Get and set the EXDATA |
826 | | */ |
827 | | int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg) |
828 | 0 | { |
829 | 0 | return CRYPTO_set_ex_data(&drbg->ex_data, idx, arg); |
830 | 0 | } |
831 | | |
832 | | void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx) |
833 | 0 | { |
834 | 0 | return CRYPTO_get_ex_data(&drbg->ex_data, idx); |
835 | 0 | } |
836 | | |
837 | | |
838 | | /* |
839 | | * The following functions provide a RAND_METHOD that works on the |
840 | | * global DRBG. They lock. |
841 | | */ |
842 | | |
843 | | /* |
844 | | * Allocates a new global DRBG on the secure heap (if enabled) and |
845 | | * initializes it with default settings. |
846 | | * |
847 | | * Returns a pointer to the new DRBG instance on success, NULL on failure. |
848 | | */ |
849 | | static RAND_DRBG *drbg_setup(RAND_DRBG *parent) |
850 | 0 | { |
851 | 0 | RAND_DRBG *drbg; |
852 | 0 |
|
853 | 0 | drbg = RAND_DRBG_secure_new(rand_drbg_type, rand_drbg_flags, parent); |
854 | 0 | if (drbg == NULL) |
855 | 0 | return NULL; |
856 | 0 | |
857 | 0 | /* Only the master DRBG needs to have a lock */ |
858 | 0 | if (parent == NULL && rand_drbg_enable_locking(drbg) == 0) |
859 | 0 | goto err; |
860 | 0 | |
861 | 0 | /* enable seed propagation */ |
862 | 0 | drbg->reseed_counter = 1; |
863 | 0 |
|
864 | 0 | /* |
865 | 0 | * Ignore instantiation error to support just-in-time instantiation. |
866 | 0 | * |
867 | 0 | * The state of the drbg will be checked in RAND_DRBG_generate() and |
868 | 0 | * an automatic recovery is attempted. |
869 | 0 | */ |
870 | 0 | (void)RAND_DRBG_instantiate(drbg, |
871 | 0 | (const unsigned char *) ossl_pers_string, |
872 | 0 | sizeof(ossl_pers_string) - 1); |
873 | 0 | return drbg; |
874 | 0 | |
875 | 0 | err: |
876 | 0 | RAND_DRBG_free(drbg); |
877 | 0 | return NULL; |
878 | 0 | } |
879 | | |
880 | | /* |
881 | | * Initialize the global DRBGs on first use. |
882 | | * Returns 1 on success, 0 on failure. |
883 | | */ |
884 | | DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init) |
885 | 0 | { |
886 | 0 | /* |
887 | 0 | * ensure that libcrypto is initialized, otherwise the |
888 | 0 | * DRBG locks are not cleaned up properly |
889 | 0 | */ |
890 | 0 | if (!OPENSSL_init_crypto(0, NULL)) |
891 | 0 | return 0; |
892 | 0 | |
893 | 0 | if (!CRYPTO_THREAD_init_local(&private_drbg, NULL)) |
894 | 0 | return 0; |
895 | 0 | |
896 | 0 | if (!CRYPTO_THREAD_init_local(&public_drbg, NULL)) |
897 | 0 | goto err1; |
898 | 0 | |
899 | 0 | master_drbg = drbg_setup(NULL); |
900 | 0 | if (master_drbg == NULL) |
901 | 0 | goto err2; |
902 | 0 | |
903 | 0 | return 1; |
904 | 0 | |
905 | 0 | err2: |
906 | 0 | CRYPTO_THREAD_cleanup_local(&public_drbg); |
907 | 0 | err1: |
908 | 0 | CRYPTO_THREAD_cleanup_local(&private_drbg); |
909 | 0 | return 0; |
910 | 0 | } |
911 | | |
912 | | /* Clean up the global DRBGs before exit */ |
913 | | void rand_drbg_cleanup_int(void) |
914 | 8 | { |
915 | 8 | if (master_drbg != NULL) { |
916 | 0 | RAND_DRBG_free(master_drbg); |
917 | 0 | master_drbg = NULL; |
918 | 0 |
|
919 | 0 | CRYPTO_THREAD_cleanup_local(&private_drbg); |
920 | 0 | CRYPTO_THREAD_cleanup_local(&public_drbg); |
921 | 0 | } |
922 | 8 | } |
923 | | |
924 | | void drbg_delete_thread_state(void) |
925 | 0 | { |
926 | 0 | RAND_DRBG *drbg; |
927 | 0 |
|
928 | 0 | drbg = CRYPTO_THREAD_get_local(&public_drbg); |
929 | 0 | CRYPTO_THREAD_set_local(&public_drbg, NULL); |
930 | 0 | RAND_DRBG_free(drbg); |
931 | 0 |
|
932 | 0 | drbg = CRYPTO_THREAD_get_local(&private_drbg); |
933 | 0 | CRYPTO_THREAD_set_local(&private_drbg, NULL); |
934 | 0 | RAND_DRBG_free(drbg); |
935 | 0 | } |
936 | | |
937 | | /* Implements the default OpenSSL RAND_bytes() method */ |
938 | | static int drbg_bytes(unsigned char *out, int count) |
939 | 0 | { |
940 | 0 | int ret; |
941 | 0 | RAND_DRBG *drbg = RAND_DRBG_get0_public(); |
942 | 0 |
|
943 | 0 | if (drbg == NULL) |
944 | 0 | return 0; |
945 | 0 | |
946 | 0 | ret = RAND_DRBG_bytes(drbg, out, count); |
947 | 0 |
|
948 | 0 | return ret; |
949 | 0 | } |
950 | | |
951 | | /* Implements the default OpenSSL RAND_add() method */ |
952 | | static int drbg_add(const void *buf, int num, double randomness) |
953 | 0 | { |
954 | 0 | int ret = 0; |
955 | 0 | RAND_DRBG *drbg = RAND_DRBG_get0_master(); |
956 | 0 |
|
957 | 0 | if (drbg == NULL) |
958 | 0 | return 0; |
959 | 0 | |
960 | 0 | if (num < 0 || randomness < 0.0) |
961 | 0 | return 0; |
962 | 0 | |
963 | 0 | if (randomness > (double)drbg->max_entropylen) { |
964 | 0 | /* |
965 | 0 | * The purpose of this check is to bound |randomness| by a |
966 | 0 | * relatively small value in order to prevent an integer |
967 | 0 | * overflow when multiplying by 8 in the rand_drbg_restart() |
968 | 0 | * call below. |
969 | 0 | */ |
970 | 0 | return 0; |
971 | 0 | } |
972 | 0 | |
973 | 0 | rand_drbg_lock(drbg); |
974 | 0 | ret = rand_drbg_restart(drbg, buf, |
975 | 0 | (size_t)(unsigned int)num, |
976 | 0 | (size_t)(8*randomness)); |
977 | 0 | rand_drbg_unlock(drbg); |
978 | 0 |
|
979 | 0 | return ret; |
980 | 0 | } |
981 | | |
982 | | /* Implements the default OpenSSL RAND_seed() method */ |
983 | | static int drbg_seed(const void *buf, int num) |
984 | 0 | { |
985 | 0 | return drbg_add(buf, num, num); |
986 | 0 | } |
987 | | |
988 | | /* Implements the default OpenSSL RAND_status() method */ |
989 | | static int drbg_status(void) |
990 | 0 | { |
991 | 0 | int ret; |
992 | 0 | RAND_DRBG *drbg = RAND_DRBG_get0_master(); |
993 | 0 |
|
994 | 0 | if (drbg == NULL) |
995 | 0 | return 0; |
996 | 0 | |
997 | 0 | rand_drbg_lock(drbg); |
998 | 0 | ret = drbg->state == DRBG_READY ? 1 : 0; |
999 | 0 | rand_drbg_unlock(drbg); |
1000 | 0 | return ret; |
1001 | 0 | } |
1002 | | |
1003 | | /* |
1004 | | * Get the master DRBG. |
1005 | | * Returns pointer to the DRBG on success, NULL on failure. |
1006 | | * |
1007 | | */ |
1008 | | RAND_DRBG *RAND_DRBG_get0_master(void) |
1009 | 0 | { |
1010 | 0 | if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init)) |
1011 | 0 | return NULL; |
1012 | 0 | |
1013 | 0 | return master_drbg; |
1014 | 0 | } |
1015 | | |
1016 | | /* |
1017 | | * Get the public DRBG. |
1018 | | * Returns pointer to the DRBG on success, NULL on failure. |
1019 | | */ |
1020 | | RAND_DRBG *RAND_DRBG_get0_public(void) |
1021 | 0 | { |
1022 | 0 | RAND_DRBG *drbg; |
1023 | 0 |
|
1024 | 0 | if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init)) |
1025 | 0 | return NULL; |
1026 | 0 | |
1027 | 0 | drbg = CRYPTO_THREAD_get_local(&public_drbg); |
1028 | 0 | if (drbg == NULL) { |
1029 | 0 | if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND)) |
1030 | 0 | return NULL; |
1031 | 0 | drbg = drbg_setup(master_drbg); |
1032 | 0 | CRYPTO_THREAD_set_local(&public_drbg, drbg); |
1033 | 0 | } |
1034 | 0 | return drbg; |
1035 | 0 | } |
1036 | | |
1037 | | /* |
1038 | | * Get the private DRBG. |
1039 | | * Returns pointer to the DRBG on success, NULL on failure. |
1040 | | */ |
1041 | | RAND_DRBG *RAND_DRBG_get0_private(void) |
1042 | 0 | { |
1043 | 0 | RAND_DRBG *drbg; |
1044 | 0 |
|
1045 | 0 | if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init)) |
1046 | 0 | return NULL; |
1047 | 0 | |
1048 | 0 | drbg = CRYPTO_THREAD_get_local(&private_drbg); |
1049 | 0 | if (drbg == NULL) { |
1050 | 0 | if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND)) |
1051 | 0 | return NULL; |
1052 | 0 | drbg = drbg_setup(master_drbg); |
1053 | 0 | CRYPTO_THREAD_set_local(&private_drbg, drbg); |
1054 | 0 | } |
1055 | 0 | return drbg; |
1056 | 0 | } |
1057 | | |
1058 | | RAND_METHOD rand_meth = { |
1059 | | drbg_seed, |
1060 | | drbg_bytes, |
1061 | | NULL, |
1062 | | drbg_add, |
1063 | | drbg_bytes, |
1064 | | drbg_status |
1065 | | }; |
1066 | | |
1067 | | RAND_METHOD *RAND_OpenSSL(void) |
1068 | 0 | { |
1069 | 0 | return &rand_meth; |
1070 | 0 | } |