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