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