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