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