]>
Commit | Line | Data |
---|---|---|
12fb8c3d | 1 | /* |
3c7d0945 | 2 | * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved. |
12fb8c3d RS |
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" | |
0b14a5b7 KR |
15 | #include "internal/thread_once.h" |
16 | #include "internal/rand_int.h" | |
7caf122e | 17 | #include "internal/cryptlib_int.h" |
12fb8c3d RS |
18 | |
19 | /* | |
a73d990e DMSP |
20 | * Support framework for NIST SP 800-90A DRBG |
21 | * | |
22 | * See manual page RAND_DRBG(7) for a general overview. | |
75e2c877 | 23 | * |
12fb8c3d RS |
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.) | |
12fb8c3d RS |
30 | */ |
31 | ||
a93ba405 | 32 | /* |
a73d990e | 33 | * The three shared DRBG instances |
a93ba405 | 34 | * |
a73d990e DMSP |
35 | * There are three shared DRBG instances: <master>, <public>, and <private>. |
36 | */ | |
37 | ||
38 | /* | |
39 | * The <master> DRBG | |
a93ba405 DMSP |
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 | |
a73d990e | 43 | * sources or by consuming randomness which was added by RAND_add(). |
a93ba405 | 44 | * |
a73d990e DMSP |
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 | |
a93ba405 | 52 | * |
a73d990e | 53 | * Used by default for generating random bytes using RAND_bytes(). |
3ce1c27b | 54 | * |
a73d990e DMSP |
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 | |
3ce1c27b | 60 | * |
a73d990e | 61 | * Used by default for generating private keys using RAND_priv_bytes() |
812b1537 | 62 | * |
a73d990e | 63 | * The <private> DRBG is thread-local, i.e., there is one instance per thread. |
a93ba405 | 64 | */ |
a73d990e DMSP |
65 | static CRYPTO_THREAD_LOCAL private_drbg; |
66 | ||
a93ba405 DMSP |
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 | ||
c16de9d8 DMSP |
72 | static CRYPTO_ONCE rand_drbg_init = CRYPTO_ONCE_STATIC_INIT; |
73 | ||
31393fd9 | 74 | |
8bf36651 SL |
75 | #define RAND_DRBG_TYPE_FLAGS ( \ |
76 | RAND_DRBG_FLAG_MASTER | RAND_DRBG_FLAG_PUBLIC | RAND_DRBG_FLAG_PRIVATE ) | |
77 | ||
78 | #define RAND_DRBG_TYPE_MASTER 0 | |
79 | #define RAND_DRBG_TYPE_PUBLIC 1 | |
80 | #define RAND_DRBG_TYPE_PRIVATE 2 | |
81 | ||
82 | /* Defaults */ | |
83 | static int rand_drbg_type[3] = { | |
84 | RAND_DRBG_TYPE, /* Master */ | |
85 | RAND_DRBG_TYPE, /* Public */ | |
86 | RAND_DRBG_TYPE /* Private */ | |
87 | }; | |
88 | static unsigned int rand_drbg_flags[3] = { | |
89 | RAND_DRBG_FLAGS | RAND_DRBG_FLAG_MASTER, /* Master */ | |
90 | RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC, /* Public */ | |
91 | RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE /* Private */ | |
92 | }; | |
31393fd9 | 93 | |
4917e911 DMSP |
94 | static unsigned int master_reseed_interval = MASTER_RESEED_INTERVAL; |
95 | static unsigned int slave_reseed_interval = SLAVE_RESEED_INTERVAL; | |
96 | ||
97 | static time_t master_reseed_time_interval = MASTER_RESEED_TIME_INTERVAL; | |
98 | static time_t slave_reseed_time_interval = SLAVE_RESEED_TIME_INTERVAL; | |
99 | ||
c402e943 DMSP |
100 | /* A logical OR of all used DRBG flag bits (currently there is only one) */ |
101 | static const unsigned int rand_drbg_used_flags = | |
8bf36651 SL |
102 | RAND_DRBG_FLAG_CTR_NO_DF | RAND_DRBG_FLAG_HMAC | RAND_DRBG_TYPE_FLAGS; |
103 | ||
c402e943 | 104 | |
8bf36651 | 105 | static RAND_DRBG *drbg_setup(RAND_DRBG *parent, int drbg_type); |
4f9dabbf DMSP |
106 | |
107 | static RAND_DRBG *rand_drbg_new(int secure, | |
108 | int type, | |
109 | unsigned int flags, | |
110 | RAND_DRBG *parent); | |
0b14a5b7 | 111 | |
8bf36651 SL |
112 | static int is_ctr(int type) |
113 | { | |
114 | switch (type) { | |
115 | case NID_aes_128_ctr: | |
116 | case NID_aes_192_ctr: | |
117 | case NID_aes_256_ctr: | |
118 | return 1; | |
119 | default: | |
120 | return 0; | |
121 | } | |
122 | } | |
123 | ||
124 | static int is_digest(int type) | |
125 | { | |
126 | switch (type) { | |
127 | case NID_sha1: | |
128 | case NID_sha224: | |
129 | case NID_sha256: | |
130 | case NID_sha384: | |
131 | case NID_sha512: | |
132 | case NID_sha512_224: | |
133 | case NID_sha512_256: | |
134 | case NID_sha3_224: | |
135 | case NID_sha3_256: | |
136 | case NID_sha3_384: | |
137 | case NID_sha3_512: | |
138 | return 1; | |
139 | default: | |
140 | return 0; | |
141 | } | |
142 | } | |
143 | ||
12fb8c3d | 144 | /* |
31393fd9 DMSP |
145 | * Set/initialize |drbg| to be of type |type|, with optional |flags|. |
146 | * | |
147 | * If |type| and |flags| are zero, use the defaults | |
efb8128a DMSP |
148 | * |
149 | * Returns 1 on success, 0 on failure. | |
12fb8c3d | 150 | */ |
31393fd9 | 151 | int RAND_DRBG_set(RAND_DRBG *drbg, int type, unsigned int flags) |
12fb8c3d RS |
152 | { |
153 | int ret = 1; | |
154 | ||
31393fd9 | 155 | if (type == 0 && flags == 0) { |
8bf36651 SL |
156 | type = rand_drbg_type[RAND_DRBG_TYPE_MASTER]; |
157 | flags = rand_drbg_flags[RAND_DRBG_TYPE_MASTER]; | |
158 | } | |
159 | ||
160 | /* If set is called multiple times - clear the old one */ | |
161 | if (type != drbg->type && drbg->type != 0 && drbg->meth != NULL) { | |
162 | drbg->meth->uninstantiate(drbg); | |
31393fd9 DMSP |
163 | } |
164 | ||
75e2c877 RS |
165 | drbg->state = DRBG_UNINITIALISED; |
166 | drbg->flags = flags; | |
31393fd9 | 167 | drbg->type = type; |
12fb8c3d | 168 | |
8bf36651 | 169 | if (type == 0) { |
12fb8c3d RS |
170 | /* Uninitialized; that's okay. */ |
171 | return 1; | |
8bf36651 | 172 | } else if (is_ctr(type)) { |
8212d505 | 173 | ret = drbg_ctr_init(drbg); |
8bf36651 SL |
174 | } else if (is_digest(type)) { |
175 | if (flags & RAND_DRBG_FLAG_HMAC) | |
176 | ret = drbg_hmac_init(drbg); | |
177 | else | |
178 | ret = drbg_hash_init(drbg); | |
179 | } else { | |
180 | RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_UNSUPPORTED_DRBG_TYPE); | |
181 | return 0; | |
12fb8c3d RS |
182 | } |
183 | ||
efb8128a | 184 | if (ret == 0) |
12fb8c3d RS |
185 | RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_ERROR_INITIALISING_DRBG); |
186 | return ret; | |
187 | } | |
188 | ||
31393fd9 DMSP |
189 | /* |
190 | * Set/initialize default |type| and |flag| for new drbg instances. | |
191 | * | |
192 | * Returns 1 on success, 0 on failure. | |
193 | */ | |
194 | int RAND_DRBG_set_defaults(int type, unsigned int flags) | |
195 | { | |
8bf36651 SL |
196 | int all; |
197 | if (!(is_digest(type) || is_ctr(type))) { | |
31393fd9 DMSP |
198 | RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_TYPE); |
199 | return 0; | |
31393fd9 DMSP |
200 | } |
201 | ||
c402e943 | 202 | if ((flags & ~rand_drbg_used_flags) != 0) { |
31393fd9 DMSP |
203 | RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_FLAGS); |
204 | return 0; | |
205 | } | |
206 | ||
8bf36651 SL |
207 | all = ((flags & RAND_DRBG_TYPE_FLAGS) == 0); |
208 | if (all || (flags & RAND_DRBG_FLAG_MASTER) != 0) { | |
209 | rand_drbg_type[RAND_DRBG_TYPE_MASTER] = type; | |
210 | rand_drbg_flags[RAND_DRBG_TYPE_MASTER] = flags | RAND_DRBG_FLAG_MASTER; | |
211 | } | |
212 | if (all || (flags & RAND_DRBG_FLAG_PUBLIC) != 0) { | |
213 | rand_drbg_type[RAND_DRBG_TYPE_PUBLIC] = type; | |
214 | rand_drbg_flags[RAND_DRBG_TYPE_PUBLIC] = flags | RAND_DRBG_FLAG_PUBLIC; | |
215 | } | |
216 | if (all || (flags & RAND_DRBG_FLAG_PRIVATE) != 0) { | |
217 | rand_drbg_type[RAND_DRBG_TYPE_PRIVATE] = type; | |
218 | rand_drbg_flags[RAND_DRBG_TYPE_PRIVATE] = flags | RAND_DRBG_FLAG_PRIVATE; | |
219 | } | |
220 | return 1; | |
31393fd9 DMSP |
221 | } |
222 | ||
223 | ||
12fb8c3d | 224 | /* |
4f9dabbf DMSP |
225 | * Allocate memory and initialize a new DRBG. The DRBG is allocated on |
226 | * the secure heap if |secure| is nonzero and the secure heap is enabled. | |
227 | * The |parent|, if not NULL, will be used as random source for reseeding. | |
a93ba405 DMSP |
228 | * |
229 | * Returns a pointer to the new DRBG instance on success, NULL on failure. | |
12fb8c3d | 230 | */ |
4f9dabbf DMSP |
231 | static RAND_DRBG *rand_drbg_new(int secure, |
232 | int type, | |
233 | unsigned int flags, | |
234 | RAND_DRBG *parent) | |
12fb8c3d | 235 | { |
4f9dabbf DMSP |
236 | RAND_DRBG *drbg = secure ? |
237 | OPENSSL_secure_zalloc(sizeof(*drbg)) : OPENSSL_zalloc(sizeof(*drbg)); | |
12fb8c3d | 238 | |
9d951a78 | 239 | if (drbg == NULL) { |
12fb8c3d | 240 | RANDerr(RAND_F_RAND_DRBG_NEW, ERR_R_MALLOC_FAILURE); |
f96ff4e9 | 241 | return NULL; |
12fb8c3d | 242 | } |
4f9dabbf DMSP |
243 | |
244 | drbg->secure = secure && CRYPTO_secure_allocated(drbg); | |
a35f607c | 245 | drbg->fork_count = rand_fork_count; |
75e2c877 | 246 | drbg->parent = parent; |
4917e911 DMSP |
247 | |
248 | if (parent == NULL) { | |
5bc6bcf8 DMSP |
249 | drbg->get_entropy = rand_drbg_get_entropy; |
250 | drbg->cleanup_entropy = rand_drbg_cleanup_entropy; | |
251 | #ifndef RAND_DRBG_GET_RANDOM_NONCE | |
252 | drbg->get_nonce = rand_drbg_get_nonce; | |
253 | drbg->cleanup_nonce = rand_drbg_cleanup_nonce; | |
254 | #endif | |
255 | ||
4917e911 DMSP |
256 | drbg->reseed_interval = master_reseed_interval; |
257 | drbg->reseed_time_interval = master_reseed_time_interval; | |
258 | } else { | |
5bc6bcf8 DMSP |
259 | drbg->get_entropy = rand_drbg_get_entropy; |
260 | drbg->cleanup_entropy = rand_drbg_cleanup_entropy; | |
261 | /* | |
262 | * Do not provide nonce callbacks, the child DRBGs will | |
263 | * obtain their nonce using random bits from the parent. | |
264 | */ | |
265 | ||
4917e911 DMSP |
266 | drbg->reseed_interval = slave_reseed_interval; |
267 | drbg->reseed_time_interval = slave_reseed_time_interval; | |
268 | } | |
269 | ||
efb8128a | 270 | if (RAND_DRBG_set(drbg, type, flags) == 0) |
75e2c877 RS |
271 | goto err; |
272 | ||
7caf122e KR |
273 | if (parent != NULL) { |
274 | rand_drbg_lock(parent); | |
275 | if (drbg->strength > parent->strength) { | |
276 | /* | |
277 | * We currently don't support the algorithm from NIST SP 800-90C | |
278 | * 10.1.2 to use a weaker DRBG as source | |
279 | */ | |
280 | rand_drbg_unlock(parent); | |
281 | RANDerr(RAND_F_RAND_DRBG_NEW, RAND_R_PARENT_STRENGTH_TOO_WEAK); | |
282 | goto err; | |
283 | } | |
284 | rand_drbg_unlock(parent); | |
35503b7c KR |
285 | } |
286 | ||
75e2c877 RS |
287 | return drbg; |
288 | ||
c2e33a05 | 289 | err: |
4f9dabbf DMSP |
290 | if (drbg->secure) |
291 | OPENSSL_secure_free(drbg); | |
292 | else | |
293 | OPENSSL_free(drbg); | |
294 | ||
75e2c877 | 295 | return NULL; |
12fb8c3d RS |
296 | } |
297 | ||
4f9dabbf DMSP |
298 | RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent) |
299 | { | |
300 | return rand_drbg_new(0, type, flags, parent); | |
301 | } | |
302 | ||
303 | RAND_DRBG *RAND_DRBG_secure_new(int type, unsigned int flags, RAND_DRBG *parent) | |
304 | { | |
305 | return rand_drbg_new(1, type, flags, parent); | |
306 | } | |
307 | ||
12fb8c3d | 308 | /* |
75e2c877 | 309 | * Uninstantiate |drbg| and free all memory. |
12fb8c3d | 310 | */ |
75e2c877 | 311 | void RAND_DRBG_free(RAND_DRBG *drbg) |
12fb8c3d | 312 | { |
c16de9d8 | 313 | if (drbg == NULL) |
12fb8c3d RS |
314 | return; |
315 | ||
8212d505 DMSP |
316 | if (drbg->meth != NULL) |
317 | drbg->meth->uninstantiate(drbg); | |
4f9dabbf | 318 | CRYPTO_THREAD_lock_free(drbg->lock); |
75e2c877 | 319 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data); |
4f9dabbf DMSP |
320 | |
321 | if (drbg->secure) | |
322 | OPENSSL_secure_clear_free(drbg, sizeof(*drbg)); | |
323 | else | |
324 | OPENSSL_clear_free(drbg, sizeof(*drbg)); | |
12fb8c3d RS |
325 | } |
326 | ||
327 | /* | |
75e2c877 | 328 | * Instantiate |drbg|, after it has been initialized. Use |pers| and |
12fb8c3d | 329 | * |perslen| as prediction-resistance input. |
2139145b BK |
330 | * |
331 | * Requires that drbg->lock is already locked for write, if non-null. | |
efb8128a DMSP |
332 | * |
333 | * Returns 1 on success, 0 on failure. | |
12fb8c3d | 334 | */ |
75e2c877 | 335 | int RAND_DRBG_instantiate(RAND_DRBG *drbg, |
12fb8c3d RS |
336 | const unsigned char *pers, size_t perslen) |
337 | { | |
12fb8c3d | 338 | unsigned char *nonce = NULL, *entropy = NULL; |
aa048aef | 339 | size_t noncelen = 0, entropylen = 0; |
2a70d65b KR |
340 | size_t min_entropy = drbg->strength; |
341 | size_t min_entropylen = drbg->min_entropylen; | |
342 | size_t max_entropylen = drbg->max_entropylen; | |
12fb8c3d | 343 | |
aa048aef | 344 | if (perslen > drbg->max_perslen) { |
75e2c877 RS |
345 | RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, |
346 | RAND_R_PERSONALISATION_STRING_TOO_LONG); | |
12fb8c3d RS |
347 | goto end; |
348 | } | |
8212d505 | 349 | |
272c0df8 | 350 | if (drbg->meth == NULL) { |
8212d505 DMSP |
351 | RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, |
352 | RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED); | |
353 | goto end; | |
354 | } | |
355 | ||
75e2c877 RS |
356 | if (drbg->state != DRBG_UNINITIALISED) { |
357 | RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, | |
358 | drbg->state == DRBG_ERROR ? RAND_R_IN_ERROR_STATE | |
359 | : RAND_R_ALREADY_INSTANTIATED); | |
12fb8c3d RS |
360 | goto end; |
361 | } | |
362 | ||
75e2c877 | 363 | drbg->state = DRBG_ERROR; |
2a70d65b KR |
364 | |
365 | /* | |
366 | * NIST SP800-90Ar1 section 9.1 says you can combine getting the entropy | |
367 | * and nonce in 1 call by increasing the entropy with 50% and increasing | |
368 | * the minimum length to accomadate the length of the nonce. | |
369 | * We do this in case a nonce is require and get_nonce is NULL. | |
370 | */ | |
371 | if (drbg->min_noncelen > 0 && drbg->get_nonce == NULL) { | |
372 | min_entropy += drbg->strength / 2; | |
373 | min_entropylen += drbg->min_noncelen; | |
374 | max_entropylen += drbg->max_noncelen; | |
375 | } | |
376 | ||
a83dc59a BE |
377 | drbg->reseed_next_counter = tsan_load(&drbg->reseed_prop_counter); |
378 | if (drbg->reseed_next_counter) { | |
379 | drbg->reseed_next_counter++; | |
380 | if(!drbg->reseed_next_counter) | |
381 | drbg->reseed_next_counter = 1; | |
382 | } | |
383 | ||
75e2c877 | 384 | if (drbg->get_entropy != NULL) |
2a70d65b KR |
385 | entropylen = drbg->get_entropy(drbg, &entropy, min_entropy, |
386 | min_entropylen, max_entropylen, 0); | |
387 | if (entropylen < min_entropylen | |
c2e33a05 | 388 | || entropylen > max_entropylen) { |
75e2c877 | 389 | RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_ENTROPY); |
12fb8c3d RS |
390 | goto end; |
391 | } | |
392 | ||
2a70d65b | 393 | if (drbg->min_noncelen > 0 && drbg->get_nonce != NULL) { |
75e2c877 | 394 | noncelen = drbg->get_nonce(drbg, &nonce, drbg->strength / 2, |
aa048aef DMSP |
395 | drbg->min_noncelen, drbg->max_noncelen); |
396 | if (noncelen < drbg->min_noncelen || noncelen > drbg->max_noncelen) { | |
2a70d65b | 397 | RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_NONCE); |
12fb8c3d RS |
398 | goto end; |
399 | } | |
400 | } | |
401 | ||
8212d505 | 402 | if (!drbg->meth->instantiate(drbg, entropy, entropylen, |
12fb8c3d | 403 | nonce, noncelen, pers, perslen)) { |
75e2c877 | 404 | RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_INSTANTIATING_DRBG); |
12fb8c3d RS |
405 | goto end; |
406 | } | |
407 | ||
75e2c877 | 408 | drbg->state = DRBG_READY; |
8bf36651 | 409 | drbg->reseed_gen_counter = 1; |
08a65d96 | 410 | drbg->reseed_time = time(NULL); |
a83dc59a | 411 | tsan_store(&drbg->reseed_prop_counter, drbg->reseed_next_counter); |
12fb8c3d | 412 | |
c2e33a05 | 413 | end: |
75e2c877 | 414 | if (entropy != NULL && drbg->cleanup_entropy != NULL) |
6969a3f4 | 415 | drbg->cleanup_entropy(drbg, entropy, entropylen); |
c2e33a05 | 416 | if (nonce != NULL && drbg->cleanup_nonce != NULL) |
6969a3f4 | 417 | drbg->cleanup_nonce(drbg, nonce, noncelen); |
75e2c877 | 418 | if (drbg->state == DRBG_READY) |
12fb8c3d | 419 | return 1; |
12fb8c3d RS |
420 | return 0; |
421 | } | |
422 | ||
423 | /* | |
75e2c877 | 424 | * Uninstantiate |drbg|. Must be instantiated before it can be used. |
2139145b BK |
425 | * |
426 | * Requires that drbg->lock is already locked for write, if non-null. | |
efb8128a DMSP |
427 | * |
428 | * Returns 1 on success, 0 on failure. | |
12fb8c3d | 429 | */ |
75e2c877 | 430 | int RAND_DRBG_uninstantiate(RAND_DRBG *drbg) |
12fb8c3d | 431 | { |
8bf36651 | 432 | int index = -1, type, flags; |
272c0df8 | 433 | if (drbg->meth == NULL) { |
8212d505 DMSP |
434 | RANDerr(RAND_F_RAND_DRBG_UNINSTANTIATE, |
435 | RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED); | |
436 | return 0; | |
437 | } | |
438 | ||
efb8128a DMSP |
439 | /* Clear the entire drbg->ctr struct, then reset some important |
440 | * members of the drbg->ctr struct (e.g. keysize, df_ks) to their | |
441 | * initial values. | |
442 | */ | |
8212d505 | 443 | drbg->meth->uninstantiate(drbg); |
8bf36651 SL |
444 | |
445 | /* The reset uses the default values for type and flags */ | |
446 | if (drbg->flags & RAND_DRBG_FLAG_MASTER) | |
447 | index = RAND_DRBG_TYPE_MASTER; | |
448 | else if (drbg->flags & RAND_DRBG_FLAG_PRIVATE) | |
449 | index = RAND_DRBG_TYPE_PRIVATE; | |
450 | else if (drbg->flags & RAND_DRBG_FLAG_PUBLIC) | |
451 | index = RAND_DRBG_TYPE_PUBLIC; | |
452 | ||
453 | if (index != -1) { | |
454 | flags = rand_drbg_flags[index]; | |
455 | type = rand_drbg_type[index]; | |
456 | } else { | |
457 | flags = drbg->flags; | |
458 | type = drbg->type; | |
459 | } | |
460 | return RAND_DRBG_set(drbg, type, flags); | |
12fb8c3d RS |
461 | } |
462 | ||
463 | /* | |
c16de9d8 | 464 | * Reseed |drbg|, mixing in the specified data |
2139145b BK |
465 | * |
466 | * Requires that drbg->lock is already locked for write, if non-null. | |
efb8128a DMSP |
467 | * |
468 | * Returns 1 on success, 0 on failure. | |
12fb8c3d | 469 | */ |
75e2c877 | 470 | int RAND_DRBG_reseed(RAND_DRBG *drbg, |
eb238134 KR |
471 | const unsigned char *adin, size_t adinlen, |
472 | int prediction_resistance) | |
12fb8c3d RS |
473 | { |
474 | unsigned char *entropy = NULL; | |
aa048aef | 475 | size_t entropylen = 0; |
75e2c877 RS |
476 | |
477 | if (drbg->state == DRBG_ERROR) { | |
478 | RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_IN_ERROR_STATE); | |
479 | return 0; | |
480 | } | |
481 | if (drbg->state == DRBG_UNINITIALISED) { | |
482 | RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_NOT_INSTANTIATED); | |
483 | return 0; | |
12fb8c3d RS |
484 | } |
485 | ||
272c0df8 | 486 | if (adin == NULL) { |
12fb8c3d | 487 | adinlen = 0; |
272c0df8 | 488 | } else if (adinlen > drbg->max_adinlen) { |
75e2c877 RS |
489 | RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ADDITIONAL_INPUT_TOO_LONG); |
490 | return 0; | |
12fb8c3d RS |
491 | } |
492 | ||
75e2c877 | 493 | drbg->state = DRBG_ERROR; |
a83dc59a BE |
494 | |
495 | drbg->reseed_next_counter = tsan_load(&drbg->reseed_prop_counter); | |
496 | if (drbg->reseed_next_counter) { | |
497 | drbg->reseed_next_counter++; | |
498 | if(!drbg->reseed_next_counter) | |
499 | drbg->reseed_next_counter = 1; | |
500 | } | |
501 | ||
75e2c877 | 502 | if (drbg->get_entropy != NULL) |
aa048aef | 503 | entropylen = drbg->get_entropy(drbg, &entropy, drbg->strength, |
eb238134 KR |
504 | drbg->min_entropylen, |
505 | drbg->max_entropylen, | |
506 | prediction_resistance); | |
c16de9d8 | 507 | if (entropylen < drbg->min_entropylen |
c2e33a05 | 508 | || entropylen > drbg->max_entropylen) { |
75e2c877 | 509 | RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ERROR_RETRIEVING_ENTROPY); |
12fb8c3d RS |
510 | goto end; |
511 | } | |
512 | ||
8212d505 | 513 | if (!drbg->meth->reseed(drbg, entropy, entropylen, adin, adinlen)) |
12fb8c3d | 514 | goto end; |
a93ba405 | 515 | |
75e2c877 | 516 | drbg->state = DRBG_READY; |
8bf36651 | 517 | drbg->reseed_gen_counter = 1; |
08a65d96 | 518 | drbg->reseed_time = time(NULL); |
a83dc59a | 519 | tsan_store(&drbg->reseed_prop_counter, drbg->reseed_next_counter); |
12fb8c3d | 520 | |
c2e33a05 | 521 | end: |
75e2c877 | 522 | if (entropy != NULL && drbg->cleanup_entropy != NULL) |
6969a3f4 | 523 | drbg->cleanup_entropy(drbg, entropy, entropylen); |
75e2c877 | 524 | if (drbg->state == DRBG_READY) |
12fb8c3d | 525 | return 1; |
12fb8c3d RS |
526 | return 0; |
527 | } | |
528 | ||
c16de9d8 DMSP |
529 | /* |
530 | * Restart |drbg|, using the specified entropy or additional input | |
531 | * | |
532 | * Tries its best to get the drbg instantiated by all means, | |
533 | * regardless of its current state. | |
534 | * | |
535 | * Optionally, a |buffer| of |len| random bytes can be passed, | |
536 | * which is assumed to contain at least |entropy| bits of entropy. | |
537 | * | |
538 | * If |entropy| > 0, the buffer content is used as entropy input. | |
539 | * | |
540 | * If |entropy| == 0, the buffer content is used as additional input | |
541 | * | |
542 | * Returns 1 on success, 0 on failure. | |
543 | * | |
544 | * This function is used internally only. | |
545 | */ | |
546 | int rand_drbg_restart(RAND_DRBG *drbg, | |
547 | const unsigned char *buffer, size_t len, size_t entropy) | |
548 | { | |
549 | int reseeded = 0; | |
550 | const unsigned char *adin = NULL; | |
551 | size_t adinlen = 0; | |
552 | ||
553 | if (drbg->pool != NULL) { | |
554 | RANDerr(RAND_F_RAND_DRBG_RESTART, ERR_R_INTERNAL_ERROR); | |
3064b551 | 555 | drbg->state = DRBG_ERROR; |
6decf943 | 556 | rand_pool_free(drbg->pool); |
c16de9d8 | 557 | drbg->pool = NULL; |
3064b551 | 558 | return 0; |
c16de9d8 DMSP |
559 | } |
560 | ||
561 | if (buffer != NULL) { | |
562 | if (entropy > 0) { | |
563 | if (drbg->max_entropylen < len) { | |
564 | RANDerr(RAND_F_RAND_DRBG_RESTART, | |
565 | RAND_R_ENTROPY_INPUT_TOO_LONG); | |
3064b551 | 566 | drbg->state = DRBG_ERROR; |
c16de9d8 DMSP |
567 | return 0; |
568 | } | |
569 | ||
570 | if (entropy > 8 * len) { | |
571 | RANDerr(RAND_F_RAND_DRBG_RESTART, RAND_R_ENTROPY_OUT_OF_RANGE); | |
3064b551 | 572 | drbg->state = DRBG_ERROR; |
c16de9d8 DMSP |
573 | return 0; |
574 | } | |
575 | ||
576 | /* will be picked up by the rand_drbg_get_entropy() callback */ | |
3064b551 | 577 | drbg->pool = rand_pool_attach(buffer, len, entropy); |
c16de9d8 DMSP |
578 | if (drbg->pool == NULL) |
579 | return 0; | |
c16de9d8 DMSP |
580 | } else { |
581 | if (drbg->max_adinlen < len) { | |
582 | RANDerr(RAND_F_RAND_DRBG_RESTART, | |
583 | RAND_R_ADDITIONAL_INPUT_TOO_LONG); | |
3064b551 | 584 | drbg->state = DRBG_ERROR; |
c16de9d8 DMSP |
585 | return 0; |
586 | } | |
587 | adin = buffer; | |
588 | adinlen = len; | |
589 | } | |
590 | } | |
591 | ||
592 | /* repair error state */ | |
efb8128a | 593 | if (drbg->state == DRBG_ERROR) |
c16de9d8 DMSP |
594 | RAND_DRBG_uninstantiate(drbg); |
595 | ||
596 | /* repair uninitialized state */ | |
597 | if (drbg->state == DRBG_UNINITIALISED) { | |
a93ba405 DMSP |
598 | /* reinstantiate drbg */ |
599 | RAND_DRBG_instantiate(drbg, | |
600 | (const unsigned char *) ossl_pers_string, | |
601 | sizeof(ossl_pers_string) - 1); | |
c16de9d8 DMSP |
602 | /* already reseeded. prevent second reseeding below */ |
603 | reseeded = (drbg->state == DRBG_READY); | |
604 | } | |
605 | ||
606 | /* refresh current state if entropy or additional input has been provided */ | |
607 | if (drbg->state == DRBG_READY) { | |
608 | if (adin != NULL) { | |
609 | /* | |
610 | * mix in additional input without reseeding | |
611 | * | |
612 | * Similar to RAND_DRBG_reseed(), but the provided additional | |
613 | * data |adin| is mixed into the current state without pulling | |
614 | * entropy from the trusted entropy source using get_entropy(). | |
615 | * This is not a reseeding in the strict sense of NIST SP 800-90A. | |
616 | */ | |
8212d505 | 617 | drbg->meth->reseed(drbg, adin, adinlen, NULL, 0); |
c16de9d8 DMSP |
618 | } else if (reseeded == 0) { |
619 | /* do a full reseeding if it has not been done yet above */ | |
eb238134 | 620 | RAND_DRBG_reseed(drbg, NULL, 0, 0); |
c16de9d8 DMSP |
621 | } |
622 | } | |
623 | ||
f9e43929 BE |
624 | rand_pool_free(drbg->pool); |
625 | drbg->pool = NULL; | |
c16de9d8 DMSP |
626 | |
627 | return drbg->state == DRBG_READY; | |
628 | } | |
629 | ||
12fb8c3d RS |
630 | /* |
631 | * Generate |outlen| bytes into the buffer at |out|. Reseed if we need | |
632 | * to or if |prediction_resistance| is set. Additional input can be | |
633 | * sent in |adin| and |adinlen|. | |
c16de9d8 | 634 | * |
2139145b BK |
635 | * Requires that drbg->lock is already locked for write, if non-null. |
636 | * | |
c16de9d8 DMSP |
637 | * Returns 1 on success, 0 on failure. |
638 | * | |
12fb8c3d | 639 | */ |
75e2c877 | 640 | int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen, |
12fb8c3d RS |
641 | int prediction_resistance, |
642 | const unsigned char *adin, size_t adinlen) | |
643 | { | |
e0b625f9 DMSP |
644 | int reseed_required = 0; |
645 | ||
c16de9d8 DMSP |
646 | if (drbg->state != DRBG_READY) { |
647 | /* try to recover from previous errors */ | |
648 | rand_drbg_restart(drbg, NULL, 0, 0); | |
649 | ||
650 | if (drbg->state == DRBG_ERROR) { | |
651 | RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_IN_ERROR_STATE); | |
652 | return 0; | |
653 | } | |
654 | if (drbg->state == DRBG_UNINITIALISED) { | |
655 | RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_NOT_INSTANTIATED); | |
656 | return 0; | |
657 | } | |
12fb8c3d | 658 | } |
c16de9d8 | 659 | |
75e2c877 RS |
660 | if (outlen > drbg->max_request) { |
661 | RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG); | |
662 | return 0; | |
663 | } | |
aa048aef | 664 | if (adinlen > drbg->max_adinlen) { |
75e2c877 RS |
665 | RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_ADDITIONAL_INPUT_TOO_LONG); |
666 | return 0; | |
12fb8c3d RS |
667 | } |
668 | ||
a35f607c RS |
669 | if (drbg->fork_count != rand_fork_count) { |
670 | drbg->fork_count = rand_fork_count; | |
e0b625f9 | 671 | reseed_required = 1; |
a35f607c RS |
672 | } |
673 | ||
a93ba405 | 674 | if (drbg->reseed_interval > 0) { |
8bf36651 | 675 | if (drbg->reseed_gen_counter > drbg->reseed_interval) |
a93ba405 DMSP |
676 | reseed_required = 1; |
677 | } | |
08a65d96 DMSP |
678 | if (drbg->reseed_time_interval > 0) { |
679 | time_t now = time(NULL); | |
680 | if (now < drbg->reseed_time | |
681 | || now - drbg->reseed_time >= drbg->reseed_time_interval) | |
682 | reseed_required = 1; | |
683 | } | |
a83dc59a BE |
684 | if (drbg->parent != NULL) { |
685 | unsigned int reseed_counter = tsan_load(&drbg->reseed_prop_counter); | |
686 | if (reseed_counter > 0 | |
687 | && tsan_load(&drbg->parent->reseed_prop_counter) | |
688 | != reseed_counter) | |
a93ba405 DMSP |
689 | reseed_required = 1; |
690 | } | |
12fb8c3d | 691 | |
e0b625f9 | 692 | if (reseed_required || prediction_resistance) { |
eb238134 | 693 | if (!RAND_DRBG_reseed(drbg, adin, adinlen, prediction_resistance)) { |
75e2c877 RS |
694 | RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_RESEED_ERROR); |
695 | return 0; | |
12fb8c3d RS |
696 | } |
697 | adin = NULL; | |
698 | adinlen = 0; | |
699 | } | |
700 | ||
8212d505 | 701 | if (!drbg->meth->generate(drbg, out, outlen, adin, adinlen)) { |
75e2c877 RS |
702 | drbg->state = DRBG_ERROR; |
703 | RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_GENERATE_ERROR); | |
704 | return 0; | |
12fb8c3d | 705 | } |
75e2c877 | 706 | |
8bf36651 | 707 | drbg->reseed_gen_counter++; |
e0b625f9 | 708 | |
12fb8c3d | 709 | return 1; |
12fb8c3d RS |
710 | } |
711 | ||
20928ff6 KR |
712 | /* |
713 | * Generates |outlen| random bytes and stores them in |out|. It will | |
714 | * using the given |drbg| to generate the bytes. | |
715 | * | |
716 | * Requires that drbg->lock is already locked for write, if non-null. | |
717 | * | |
718 | * Returns 1 on success 0 on failure. | |
719 | */ | |
720 | int RAND_DRBG_bytes(RAND_DRBG *drbg, unsigned char *out, size_t outlen) | |
721 | { | |
722 | unsigned char *additional = NULL; | |
723 | size_t additional_len; | |
1648338b | 724 | size_t chunk; |
20928ff6 KR |
725 | size_t ret; |
726 | ||
727 | additional_len = rand_drbg_get_additional_data(&additional, drbg->max_adinlen); | |
1648338b DMSP |
728 | |
729 | for ( ; outlen > 0; outlen -= chunk, out += chunk) { | |
730 | chunk = outlen; | |
731 | if (chunk > drbg->max_request) | |
732 | chunk = drbg->max_request; | |
733 | ret = RAND_DRBG_generate(drbg, out, chunk, 0, additional, additional_len); | |
734 | if (!ret) | |
735 | goto err; | |
736 | } | |
737 | ret = 1; | |
738 | ||
739 | err: | |
20928ff6 KR |
740 | if (additional_len != 0) |
741 | OPENSSL_secure_clear_free(additional, additional_len); | |
742 | ||
743 | return ret; | |
744 | } | |
745 | ||
12fb8c3d | 746 | /* |
c16de9d8 DMSP |
747 | * Set the RAND_DRBG callbacks for obtaining entropy and nonce. |
748 | * | |
a73d990e DMSP |
749 | * Setting the callbacks is allowed only if the drbg has not been |
750 | * initialized yet. Otherwise, the operation will fail. | |
c16de9d8 | 751 | * |
a73d990e | 752 | * Returns 1 on success, 0 on failure. |
12fb8c3d | 753 | */ |
75e2c877 | 754 | int RAND_DRBG_set_callbacks(RAND_DRBG *drbg, |
c16de9d8 DMSP |
755 | RAND_DRBG_get_entropy_fn get_entropy, |
756 | RAND_DRBG_cleanup_entropy_fn cleanup_entropy, | |
757 | RAND_DRBG_get_nonce_fn get_nonce, | |
758 | RAND_DRBG_cleanup_nonce_fn cleanup_nonce) | |
12fb8c3d | 759 | { |
a83dc59a BE |
760 | if (drbg->state != DRBG_UNINITIALISED |
761 | || drbg->parent != NULL) | |
12fb8c3d | 762 | return 0; |
c16de9d8 DMSP |
763 | drbg->get_entropy = get_entropy; |
764 | drbg->cleanup_entropy = cleanup_entropy; | |
765 | drbg->get_nonce = get_nonce; | |
766 | drbg->cleanup_nonce = cleanup_nonce; | |
12fb8c3d RS |
767 | return 1; |
768 | } | |
769 | ||
770 | /* | |
75e2c877 | 771 | * Set the reseed interval. |
a93ba405 DMSP |
772 | * |
773 | * The drbg will reseed automatically whenever the number of generate | |
774 | * requests exceeds the given reseed interval. If the reseed interval | |
08a65d96 | 775 | * is 0, then this feature is disabled. |
a93ba405 DMSP |
776 | * |
777 | * Returns 1 on success, 0 on failure. | |
12fb8c3d | 778 | */ |
a93ba405 | 779 | int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval) |
12fb8c3d | 780 | { |
a93ba405 | 781 | if (interval > MAX_RESEED_INTERVAL) |
4c75ee85 | 782 | return 0; |
75e2c877 | 783 | drbg->reseed_interval = interval; |
4c75ee85 | 784 | return 1; |
12fb8c3d RS |
785 | } |
786 | ||
08a65d96 DMSP |
787 | /* |
788 | * Set the reseed time interval. | |
789 | * | |
790 | * The drbg will reseed automatically whenever the time elapsed since | |
791 | * the last reseeding exceeds the given reseed time interval. For safety, | |
792 | * a reseeding will also occur if the clock has been reset to a smaller | |
793 | * value. | |
794 | * | |
795 | * Returns 1 on success, 0 on failure. | |
796 | */ | |
797 | int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval) | |
798 | { | |
799 | if (interval > MAX_RESEED_TIME_INTERVAL) | |
800 | return 0; | |
801 | drbg->reseed_time_interval = interval; | |
802 | return 1; | |
803 | } | |
804 | ||
4917e911 DMSP |
805 | /* |
806 | * Set the default values for reseed (time) intervals of new DRBG instances | |
807 | * | |
808 | * The default values can be set independently for master DRBG instances | |
809 | * (without a parent) and slave DRBG instances (with parent). | |
810 | * | |
811 | * Returns 1 on success, 0 on failure. | |
812 | */ | |
813 | ||
814 | int RAND_DRBG_set_reseed_defaults( | |
815 | unsigned int _master_reseed_interval, | |
816 | unsigned int _slave_reseed_interval, | |
817 | time_t _master_reseed_time_interval, | |
818 | time_t _slave_reseed_time_interval | |
819 | ) | |
820 | { | |
821 | if (_master_reseed_interval > MAX_RESEED_INTERVAL | |
822 | || _slave_reseed_interval > MAX_RESEED_INTERVAL) | |
823 | return 0; | |
824 | ||
825 | if (_master_reseed_time_interval > MAX_RESEED_TIME_INTERVAL | |
826 | || _slave_reseed_time_interval > MAX_RESEED_TIME_INTERVAL) | |
827 | return 0; | |
828 | ||
829 | master_reseed_interval = _master_reseed_interval; | |
830 | slave_reseed_interval = _slave_reseed_interval; | |
831 | ||
832 | master_reseed_time_interval = _master_reseed_time_interval; | |
833 | slave_reseed_time_interval = _slave_reseed_time_interval; | |
834 | ||
835 | return 1; | |
836 | } | |
3ce1c27b DMSP |
837 | |
838 | /* | |
839 | * Locks the given drbg. Locking a drbg which does not have locking | |
840 | * enabled is considered a successful no-op. | |
841 | * | |
842 | * Returns 1 on success, 0 on failure. | |
843 | */ | |
812b1537 | 844 | int rand_drbg_lock(RAND_DRBG *drbg) |
3ce1c27b DMSP |
845 | { |
846 | if (drbg->lock != NULL) | |
847 | return CRYPTO_THREAD_write_lock(drbg->lock); | |
848 | ||
849 | return 1; | |
850 | } | |
851 | ||
852 | /* | |
853 | * Unlocks the given drbg. Unlocking a drbg which does not have locking | |
854 | * enabled is considered a successful no-op. | |
855 | * | |
856 | * Returns 1 on success, 0 on failure. | |
857 | */ | |
812b1537 | 858 | int rand_drbg_unlock(RAND_DRBG *drbg) |
3ce1c27b DMSP |
859 | { |
860 | if (drbg->lock != NULL) | |
861 | return CRYPTO_THREAD_unlock(drbg->lock); | |
862 | ||
863 | return 1; | |
864 | } | |
865 | ||
866 | /* | |
867 | * Enables locking for the given drbg | |
868 | * | |
869 | * Locking can only be enabled if the random generator | |
870 | * is in the uninitialized state. | |
871 | * | |
872 | * Returns 1 on success, 0 on failure. | |
873 | */ | |
812b1537 | 874 | int rand_drbg_enable_locking(RAND_DRBG *drbg) |
3ce1c27b DMSP |
875 | { |
876 | if (drbg->state != DRBG_UNINITIALISED) { | |
877 | RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING, | |
878 | RAND_R_DRBG_ALREADY_INITIALIZED); | |
879 | return 0; | |
880 | } | |
881 | ||
882 | if (drbg->lock == NULL) { | |
4f9dabbf | 883 | if (drbg->parent != NULL && drbg->parent->lock == NULL) { |
3ce1c27b DMSP |
884 | RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING, |
885 | RAND_R_PARENT_LOCKING_NOT_ENABLED); | |
886 | return 0; | |
887 | } | |
888 | ||
889 | drbg->lock = CRYPTO_THREAD_lock_new(); | |
890 | if (drbg->lock == NULL) { | |
891 | RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING, | |
892 | RAND_R_FAILED_TO_CREATE_LOCK); | |
893 | return 0; | |
894 | } | |
895 | } | |
896 | ||
897 | return 1; | |
898 | } | |
899 | ||
12fb8c3d RS |
900 | /* |
901 | * Get and set the EXDATA | |
902 | */ | |
75e2c877 RS |
903 | int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg) |
904 | { | |
905 | return CRYPTO_set_ex_data(&drbg->ex_data, idx, arg); | |
906 | } | |
907 | ||
908 | void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx) | |
909 | { | |
910 | return CRYPTO_get_ex_data(&drbg->ex_data, idx); | |
911 | } | |
912 | ||
913 | ||
914 | /* | |
915 | * The following functions provide a RAND_METHOD that works on the | |
916 | * global DRBG. They lock. | |
917 | */ | |
918 | ||
0b14a5b7 | 919 | /* |
933033b6 DMSP |
920 | * Allocates a new global DRBG on the secure heap (if enabled) and |
921 | * initializes it with default settings. | |
a93ba405 DMSP |
922 | * |
923 | * Returns a pointer to the new DRBG instance on success, NULL on failure. | |
0b14a5b7 | 924 | */ |
8bf36651 | 925 | static RAND_DRBG *drbg_setup(RAND_DRBG *parent, int drbg_type) |
0b14a5b7 | 926 | { |
933033b6 | 927 | RAND_DRBG *drbg; |
0b14a5b7 | 928 | |
8bf36651 SL |
929 | drbg = RAND_DRBG_secure_new(rand_drbg_type[drbg_type], |
930 | rand_drbg_flags[drbg_type], parent); | |
933033b6 DMSP |
931 | if (drbg == NULL) |
932 | return NULL; | |
933 | ||
7caf122e KR |
934 | /* Only the master DRBG needs to have a lock */ |
935 | if (parent == NULL && rand_drbg_enable_locking(drbg) == 0) | |
933033b6 | 936 | goto err; |
a93ba405 | 937 | |
a93ba405 | 938 | /* enable seed propagation */ |
a83dc59a | 939 | tsan_store(&drbg->reseed_prop_counter, 1); |
a93ba405 | 940 | |
c16de9d8 | 941 | /* |
43687d68 | 942 | * Ignore instantiation error to support just-in-time instantiation. |
c16de9d8 DMSP |
943 | * |
944 | * The state of the drbg will be checked in RAND_DRBG_generate() and | |
945 | * an automatic recovery is attempted. | |
946 | */ | |
43687d68 DMSP |
947 | (void)RAND_DRBG_instantiate(drbg, |
948 | (const unsigned char *) ossl_pers_string, | |
949 | sizeof(ossl_pers_string) - 1); | |
933033b6 DMSP |
950 | return drbg; |
951 | ||
952 | err: | |
4f9dabbf | 953 | RAND_DRBG_free(drbg); |
933033b6 | 954 | return NULL; |
0b14a5b7 KR |
955 | } |
956 | ||
957 | /* | |
958 | * Initialize the global DRBGs on first use. | |
959 | * Returns 1 on success, 0 on failure. | |
960 | */ | |
c16de9d8 | 961 | DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init) |
0b14a5b7 | 962 | { |
39571fca DMSP |
963 | /* |
964 | * ensure that libcrypto is initialized, otherwise the | |
965 | * DRBG locks are not cleaned up properly | |
966 | */ | |
967 | if (!OPENSSL_init_crypto(0, NULL)) | |
968 | return 0; | |
969 | ||
272c0df8 BE |
970 | if (!CRYPTO_THREAD_init_local(&private_drbg, NULL)) |
971 | return 0; | |
0b14a5b7 | 972 | |
272c0df8 BE |
973 | if (!CRYPTO_THREAD_init_local(&public_drbg, NULL)) |
974 | goto err1; | |
7caf122e | 975 | |
8bf36651 | 976 | master_drbg = drbg_setup(NULL, RAND_DRBG_TYPE_MASTER); |
272c0df8 BE |
977 | if (master_drbg == NULL) |
978 | goto err2; | |
0b14a5b7 | 979 | |
933033b6 | 980 | return 1; |
272c0df8 BE |
981 | |
982 | err2: | |
983 | CRYPTO_THREAD_cleanup_local(&public_drbg); | |
984 | err1: | |
985 | CRYPTO_THREAD_cleanup_local(&private_drbg); | |
986 | return 0; | |
0b14a5b7 KR |
987 | } |
988 | ||
0b14a5b7 | 989 | /* Clean up the global DRBGs before exit */ |
c16de9d8 | 990 | void rand_drbg_cleanup_int(void) |
0b14a5b7 | 991 | { |
bf7ae750 BE |
992 | if (master_drbg != NULL) { |
993 | RAND_DRBG_free(master_drbg); | |
994 | master_drbg = NULL; | |
933033b6 | 995 | |
bf7ae750 BE |
996 | CRYPTO_THREAD_cleanup_local(&private_drbg); |
997 | CRYPTO_THREAD_cleanup_local(&public_drbg); | |
998 | } | |
7caf122e KR |
999 | } |
1000 | ||
3cb7c5cf | 1001 | void drbg_delete_thread_state(void) |
7caf122e KR |
1002 | { |
1003 | RAND_DRBG *drbg; | |
1004 | ||
a73d990e | 1005 | drbg = CRYPTO_THREAD_get_local(&public_drbg); |
272c0df8 | 1006 | CRYPTO_THREAD_set_local(&public_drbg, NULL); |
7caf122e KR |
1007 | RAND_DRBG_free(drbg); |
1008 | ||
a73d990e | 1009 | drbg = CRYPTO_THREAD_get_local(&private_drbg); |
272c0df8 | 1010 | CRYPTO_THREAD_set_local(&private_drbg, NULL); |
7caf122e | 1011 | RAND_DRBG_free(drbg); |
0b14a5b7 KR |
1012 | } |
1013 | ||
c16de9d8 | 1014 | /* Implements the default OpenSSL RAND_bytes() method */ |
75e2c877 RS |
1015 | static int drbg_bytes(unsigned char *out, int count) |
1016 | { | |
f61f62ea | 1017 | int ret; |
a93ba405 | 1018 | RAND_DRBG *drbg = RAND_DRBG_get0_public(); |
75e2c877 | 1019 | |
0b14a5b7 KR |
1020 | if (drbg == NULL) |
1021 | return 0; | |
1022 | ||
f61f62ea | 1023 | ret = RAND_DRBG_bytes(drbg, out, count); |
f61f62ea | 1024 | |
75e2c877 RS |
1025 | return ret; |
1026 | } | |
1027 | ||
8817215d DMSP |
1028 | /* |
1029 | * Calculates the minimum length of a full entropy buffer | |
1030 | * which is necessary to seed (i.e. instantiate) the DRBG | |
1031 | * successfully. | |
1032 | * | |
1033 | * NOTE: There is a copy of this function in drbgtest.c. | |
1034 | * If you change anything here, you need to update | |
1035 | * the copy accordingly. | |
1036 | */ | |
1037 | static size_t rand_drbg_seedlen(RAND_DRBG *drbg) | |
1038 | { | |
1039 | /* | |
1040 | * If no os entropy source is available then RAND_seed(buffer, bufsize) | |
1041 | * is expected to succeed if and only if the buffer length satisfies | |
1042 | * the following requirements, which follow from the calculations | |
1043 | * in RAND_DRBG_instantiate(). | |
1044 | */ | |
1045 | size_t min_entropy = drbg->strength; | |
1046 | size_t min_entropylen = drbg->min_entropylen; | |
1047 | ||
1048 | /* | |
1049 | * Extra entropy for the random nonce in the absence of a | |
1050 | * get_nonce callback, see comment in RAND_DRBG_instantiate(). | |
1051 | */ | |
1052 | if (drbg->min_noncelen > 0 && drbg->get_nonce == NULL) { | |
1053 | min_entropy += drbg->strength / 2; | |
1054 | min_entropylen += drbg->min_noncelen; | |
1055 | } | |
1056 | ||
1057 | /* | |
1058 | * Convert entropy requirement from bits to bytes | |
1059 | * (dividing by 8 without rounding upwards, because | |
1060 | * all entropy requirements are divisible by 8). | |
1061 | */ | |
1062 | min_entropy >>= 3; | |
1063 | ||
1064 | /* Return a value that satisfies both requirements */ | |
1065 | return min_entropy > min_entropylen ? min_entropy : min_entropylen; | |
1066 | } | |
1067 | ||
c16de9d8 | 1068 | /* Implements the default OpenSSL RAND_add() method */ |
75e2c877 RS |
1069 | static int drbg_add(const void *buf, int num, double randomness) |
1070 | { | |
c16de9d8 | 1071 | int ret = 0; |
a93ba405 | 1072 | RAND_DRBG *drbg = RAND_DRBG_get0_master(); |
8817215d | 1073 | size_t buflen; |
59f90557 | 1074 | size_t seedlen; |
75e2c877 | 1075 | |
c16de9d8 DMSP |
1076 | if (drbg == NULL) |
1077 | return 0; | |
75e2c877 | 1078 | |
c16de9d8 DMSP |
1079 | if (num < 0 || randomness < 0.0) |
1080 | return 0; | |
75e2c877 | 1081 | |
4011bab1 | 1082 | rand_drbg_lock(drbg); |
59f90557 DMSP |
1083 | seedlen = rand_drbg_seedlen(drbg); |
1084 | ||
8817215d DMSP |
1085 | buflen = (size_t)num; |
1086 | ||
1087 | if (buflen < seedlen || randomness < (double) seedlen) { | |
1088 | #if defined(OPENSSL_RAND_SEED_NONE) | |
1089 | /* | |
1090 | * If no os entropy source is available, a reseeding will fail | |
1091 | * inevitably. So we use a trick to mix the buffer contents into | |
1092 | * the DRBG state without forcing a reseeding: we generate a | |
1093 | * dummy random byte, using the buffer content as additional data. | |
4011bab1 | 1094 | * Note: This won't work with RAND_DRBG_FLAG_CTR_NO_DF. |
8817215d DMSP |
1095 | */ |
1096 | unsigned char dummy[1]; | |
1097 | ||
4011bab1 BE |
1098 | ret = RAND_DRBG_generate(drbg, dummy, sizeof(dummy), 0, buf, buflen); |
1099 | rand_drbg_unlock(drbg); | |
1100 | return ret; | |
8817215d DMSP |
1101 | #else |
1102 | /* | |
1103 | * If an os entropy source is avaible then we declare the buffer content | |
1104 | * as additional data by setting randomness to zero and trigger a regular | |
1105 | * reseeding. | |
1106 | */ | |
1107 | randomness = 0.0; | |
1108 | #endif | |
1109 | } | |
1110 | ||
1111 | ||
1112 | if (randomness > (double)seedlen) { | |
c16de9d8 DMSP |
1113 | /* |
1114 | * The purpose of this check is to bound |randomness| by a | |
1115 | * relatively small value in order to prevent an integer | |
1116 | * overflow when multiplying by 8 in the rand_drbg_restart() | |
3064b551 DMSP |
1117 | * call below. Note that randomness is measured in bytes, |
1118 | * not bits, so this value corresponds to eight times the | |
1119 | * security strength. | |
c16de9d8 | 1120 | */ |
8817215d | 1121 | randomness = (double)seedlen; |
75e2c877 RS |
1122 | } |
1123 | ||
8817215d | 1124 | ret = rand_drbg_restart(drbg, buf, buflen, (size_t)(8 * randomness)); |
812b1537 | 1125 | rand_drbg_unlock(drbg); |
c16de9d8 DMSP |
1126 | |
1127 | return ret; | |
75e2c877 RS |
1128 | } |
1129 | ||
c16de9d8 | 1130 | /* Implements the default OpenSSL RAND_seed() method */ |
75e2c877 RS |
1131 | static int drbg_seed(const void *buf, int num) |
1132 | { | |
1133 | return drbg_add(buf, num, num); | |
1134 | } | |
1135 | ||
c16de9d8 | 1136 | /* Implements the default OpenSSL RAND_status() method */ |
75e2c877 | 1137 | static int drbg_status(void) |
12fb8c3d | 1138 | { |
75e2c877 | 1139 | int ret; |
a93ba405 | 1140 | RAND_DRBG *drbg = RAND_DRBG_get0_master(); |
0b14a5b7 KR |
1141 | |
1142 | if (drbg == NULL) | |
1143 | return 0; | |
75e2c877 | 1144 | |
812b1537 | 1145 | rand_drbg_lock(drbg); |
0b14a5b7 | 1146 | ret = drbg->state == DRBG_READY ? 1 : 0; |
812b1537 | 1147 | rand_drbg_unlock(drbg); |
75e2c877 | 1148 | return ret; |
12fb8c3d RS |
1149 | } |
1150 | ||
0b14a5b7 | 1151 | /* |
a93ba405 DMSP |
1152 | * Get the master DRBG. |
1153 | * Returns pointer to the DRBG on success, NULL on failure. | |
1154 | * | |
1155 | */ | |
1156 | RAND_DRBG *RAND_DRBG_get0_master(void) | |
1157 | { | |
1158 | if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init)) | |
1159 | return NULL; | |
1160 | ||
a73d990e | 1161 | return master_drbg; |
a93ba405 DMSP |
1162 | } |
1163 | ||
1164 | /* | |
1165 | * Get the public DRBG. | |
0b14a5b7 KR |
1166 | * Returns pointer to the DRBG on success, NULL on failure. |
1167 | */ | |
a93ba405 | 1168 | RAND_DRBG *RAND_DRBG_get0_public(void) |
0b14a5b7 | 1169 | { |
7caf122e KR |
1170 | RAND_DRBG *drbg; |
1171 | ||
c16de9d8 | 1172 | if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init)) |
0b14a5b7 KR |
1173 | return NULL; |
1174 | ||
a73d990e | 1175 | drbg = CRYPTO_THREAD_get_local(&public_drbg); |
7caf122e | 1176 | if (drbg == NULL) { |
272c0df8 BE |
1177 | if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND)) |
1178 | return NULL; | |
8bf36651 | 1179 | drbg = drbg_setup(master_drbg, RAND_DRBG_TYPE_PUBLIC); |
a73d990e | 1180 | CRYPTO_THREAD_set_local(&public_drbg, drbg); |
7caf122e KR |
1181 | } |
1182 | return drbg; | |
0b14a5b7 KR |
1183 | } |
1184 | ||
1185 | /* | |
a93ba405 | 1186 | * Get the private DRBG. |
0b14a5b7 KR |
1187 | * Returns pointer to the DRBG on success, NULL on failure. |
1188 | */ | |
a93ba405 | 1189 | RAND_DRBG *RAND_DRBG_get0_private(void) |
0b14a5b7 | 1190 | { |
7caf122e KR |
1191 | RAND_DRBG *drbg; |
1192 | ||
c16de9d8 | 1193 | if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init)) |
0b14a5b7 KR |
1194 | return NULL; |
1195 | ||
a73d990e | 1196 | drbg = CRYPTO_THREAD_get_local(&private_drbg); |
7caf122e | 1197 | if (drbg == NULL) { |
272c0df8 BE |
1198 | if (!ossl_init_thread_start(OPENSSL_INIT_THREAD_RAND)) |
1199 | return NULL; | |
8bf36651 | 1200 | drbg = drbg_setup(master_drbg, RAND_DRBG_TYPE_PRIVATE); |
a73d990e | 1201 | CRYPTO_THREAD_set_local(&private_drbg, drbg); |
7caf122e KR |
1202 | } |
1203 | return drbg; | |
0b14a5b7 KR |
1204 | } |
1205 | ||
75e2c877 RS |
1206 | RAND_METHOD rand_meth = { |
1207 | drbg_seed, | |
1208 | drbg_bytes, | |
ddc6a5c8 | 1209 | NULL, |
75e2c877 RS |
1210 | drbg_add, |
1211 | drbg_bytes, | |
1212 | drbg_status | |
1213 | }; | |
1214 | ||
1215 | RAND_METHOD *RAND_OpenSSL(void) | |
12fb8c3d | 1216 | { |
75e2c877 | 1217 | return &rand_meth; |
12fb8c3d | 1218 | } |