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