]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rand/drbg_lib.c
Add RAND_DRBG_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
933033b6
DMSP
101static RAND_DRBG *drbg_setup(const char *name, RAND_DRBG *parent);
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;
549 size_t ret;
550
551 additional_len = rand_drbg_get_additional_data(&additional, drbg->max_adinlen);
552 ret = RAND_DRBG_generate(drbg, out, outlen, 0, additional, additional_len);
553 if (additional_len != 0)
554 OPENSSL_secure_clear_free(additional, additional_len);
555
556 return ret;
557}
558
12fb8c3d 559/*
c16de9d8
DMSP
560 * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
561 *
562 * In the following, the signature and the semantics of the
563 * get_entropy() and cleanup_entropy() callbacks are explained.
564 *
565 * GET_ENTROPY
566 *
567 * size_t get_entropy(RAND_DRBG *ctx,
568 * unsigned char **pout,
569 * int entropy,
570 * size_t min_len, size_t max_len);
571 *
572 * This is a request to allocate and fill a buffer of size
573 * |min_len| <= size <= |max_len| (in bytes) which contains
574 * at least |entropy| bits of randomness. The buffer's address is
575 * to be returned in |*pout| and the number of collected
576 * randomness bytes (which may be less than the allocated size
577 * of the buffer) as return value.
578 *
579 * If the callback fails to acquire at least |entropy| bits of
580 * randomness, it shall return a buffer length of 0.
581 *
582 * CLEANUP_ENTROPY
583 *
584 * void cleanup_entropy(RAND_DRBG *ctx,
585 * unsigned char *out, size_t outlen);
586 *
587 * A request to clear and free the buffer allocated by get_entropy().
588 * The values |out| and |outlen| are expected to be the random buffer's
589 * address and length, as returned by the get_entropy() callback.
590 *
591 * GET_NONCE, CLEANUP_NONCE
592 *
593 * Signature and semantics of the get_nonce() and cleanup_nonce()
594 * callbacks are analogous to get_entropy() and cleanup_entropy().
595 * Currently, the nonce is used only for the known answer tests.
12fb8c3d 596 */
75e2c877 597int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
c16de9d8
DMSP
598 RAND_DRBG_get_entropy_fn get_entropy,
599 RAND_DRBG_cleanup_entropy_fn cleanup_entropy,
600 RAND_DRBG_get_nonce_fn get_nonce,
601 RAND_DRBG_cleanup_nonce_fn cleanup_nonce)
12fb8c3d 602{
75e2c877 603 if (drbg->state != DRBG_UNINITIALISED)
12fb8c3d 604 return 0;
c16de9d8
DMSP
605 drbg->get_entropy = get_entropy;
606 drbg->cleanup_entropy = cleanup_entropy;
607 drbg->get_nonce = get_nonce;
608 drbg->cleanup_nonce = cleanup_nonce;
12fb8c3d
RS
609 return 1;
610}
611
612/*
75e2c877 613 * Set the reseed interval.
a93ba405
DMSP
614 *
615 * The drbg will reseed automatically whenever the number of generate
616 * requests exceeds the given reseed interval. If the reseed interval
08a65d96 617 * is 0, then this feature is disabled.
a93ba405
DMSP
618 *
619 * Returns 1 on success, 0 on failure.
12fb8c3d 620 */
a93ba405 621int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval)
12fb8c3d 622{
a93ba405 623 if (interval > MAX_RESEED_INTERVAL)
4c75ee85 624 return 0;
75e2c877 625 drbg->reseed_interval = interval;
4c75ee85 626 return 1;
12fb8c3d
RS
627}
628
08a65d96
DMSP
629/*
630 * Set the reseed time interval.
631 *
632 * The drbg will reseed automatically whenever the time elapsed since
633 * the last reseeding exceeds the given reseed time interval. For safety,
634 * a reseeding will also occur if the clock has been reset to a smaller
635 * value.
636 *
637 * Returns 1 on success, 0 on failure.
638 */
639int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval)
640{
641 if (interval > MAX_RESEED_TIME_INTERVAL)
642 return 0;
643 drbg->reseed_time_interval = interval;
644 return 1;
645}
646
12fb8c3d
RS
647/*
648 * Get and set the EXDATA
649 */
75e2c877
RS
650int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg)
651{
652 return CRYPTO_set_ex_data(&drbg->ex_data, idx, arg);
653}
654
655void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx)
656{
657 return CRYPTO_get_ex_data(&drbg->ex_data, idx);
658}
659
660
661/*
662 * The following functions provide a RAND_METHOD that works on the
663 * global DRBG. They lock.
664 */
665
0b14a5b7 666/*
933033b6
DMSP
667 * Allocates a new global DRBG on the secure heap (if enabled) and
668 * initializes it with default settings.
a93ba405
DMSP
669 * A global lock for the DRBG is created with the given name.
670 *
671 * Returns a pointer to the new DRBG instance on success, NULL on failure.
0b14a5b7 672 */
933033b6 673static RAND_DRBG *drbg_setup(const char *name, RAND_DRBG *parent)
0b14a5b7 674{
933033b6 675 RAND_DRBG *drbg;
0b14a5b7 676
933033b6 677 if (name == NULL) {
a93ba405 678 RANDerr(RAND_F_DRBG_SETUP, ERR_R_INTERNAL_ERROR);
933033b6 679 return NULL;
a93ba405 680 }
c16de9d8 681
933033b6
DMSP
682 drbg = OPENSSL_secure_zalloc(sizeof(RAND_DRBG));
683 if (drbg == NULL)
684 return NULL;
685
a93ba405
DMSP
686 drbg->lock = CRYPTO_THREAD_glock_new(name);
687 if (drbg->lock == NULL) {
688 RANDerr(RAND_F_DRBG_SETUP, RAND_R_FAILED_TO_CREATE_LOCK);
933033b6 689 goto err;
c16de9d8
DMSP
690 }
691
933033b6
DMSP
692 if (RAND_DRBG_set(drbg,
693 RAND_DRBG_NID, RAND_DRBG_FLAG_CTR_USE_DF) != 1)
694 goto err;
695 if (RAND_DRBG_set_callbacks(drbg, rand_drbg_get_entropy,
696 rand_drbg_cleanup_entropy, NULL, NULL) != 1)
697 goto err;
a93ba405 698
08a65d96 699 if (parent == NULL) {
a93ba405 700 drbg->reseed_interval = MASTER_RESEED_INTERVAL;
08a65d96
DMSP
701 drbg->reseed_time_interval = MASTER_RESEED_TIME_INTERVAL;
702 } else {
a93ba405
DMSP
703 drbg->parent = parent;
704 drbg->reseed_interval = SLAVE_RESEED_INTERVAL;
08a65d96 705 drbg->reseed_time_interval = SLAVE_RESEED_TIME_INTERVAL;
a93ba405
DMSP
706 }
707
708 /* enable seed propagation */
709 drbg->reseed_counter = 1;
710
c16de9d8
DMSP
711 /*
712 * Ignore instantiation error so support just-in-time instantiation.
713 *
714 * The state of the drbg will be checked in RAND_DRBG_generate() and
715 * an automatic recovery is attempted.
716 */
717 RAND_DRBG_instantiate(drbg,
718 (const unsigned char *) ossl_pers_string,
719 sizeof(ossl_pers_string) - 1);
933033b6
DMSP
720 return drbg;
721
722err:
723 drbg_cleanup(drbg);
724 return NULL;
0b14a5b7
KR
725}
726
727/*
728 * Initialize the global DRBGs on first use.
729 * Returns 1 on success, 0 on failure.
730 */
c16de9d8 731DEFINE_RUN_ONCE_STATIC(do_rand_drbg_init)
0b14a5b7 732{
39571fca
DMSP
733 /*
734 * ensure that libcrypto is initialized, otherwise the
735 * DRBG locks are not cleaned up properly
736 */
737 if (!OPENSSL_init_crypto(0, NULL))
738 return 0;
739
933033b6
DMSP
740 drbg_master = drbg_setup("drbg_master", NULL);
741 drbg_public = drbg_setup("drbg_public", drbg_master);
742 drbg_private = drbg_setup("drbg_private", drbg_master);
0b14a5b7 743
933033b6
DMSP
744 if (drbg_master == NULL || drbg_public == NULL || drbg_private == NULL)
745 return 0;
0b14a5b7 746
933033b6 747 return 1;
0b14a5b7
KR
748}
749
c16de9d8
DMSP
750/* Cleans up the given global DRBG */
751static void drbg_cleanup(RAND_DRBG *drbg)
0b14a5b7 752{
933033b6
DMSP
753 if (drbg != NULL) {
754 RAND_DRBG_uninstantiate(drbg);
755 CRYPTO_THREAD_lock_free(drbg->lock);
756 OPENSSL_secure_clear_free(drbg, sizeof(RAND_DRBG));
757 }
0b14a5b7
KR
758}
759
760/* Clean up the global DRBGs before exit */
c16de9d8 761void rand_drbg_cleanup_int(void)
0b14a5b7 762{
933033b6
DMSP
763 drbg_cleanup(drbg_private);
764 drbg_cleanup(drbg_public);
765 drbg_cleanup(drbg_master);
766
767 drbg_private = drbg_public = drbg_master = NULL;
0b14a5b7
KR
768}
769
c16de9d8 770/* Implements the default OpenSSL RAND_bytes() method */
75e2c877
RS
771static int drbg_bytes(unsigned char *out, int count)
772{
773 int ret = 0;
774 size_t chunk;
a93ba405 775 RAND_DRBG *drbg = RAND_DRBG_get0_public();
75e2c877 776
0b14a5b7
KR
777 if (drbg == NULL)
778 return 0;
779
780 CRYPTO_THREAD_write_lock(drbg->lock);
781 if (drbg->state == DRBG_UNINITIALISED)
75e2c877
RS
782 goto err;
783
784 for ( ; count > 0; count -= chunk, out += chunk) {
785 chunk = count;
0b14a5b7
KR
786 if (chunk > drbg->max_request)
787 chunk = drbg->max_request;
788 ret = RAND_DRBG_generate(drbg, out, chunk, 0, NULL, 0);
75e2c877
RS
789 if (!ret)
790 goto err;
791 }
792 ret = 1;
793
794err:
0b14a5b7 795 CRYPTO_THREAD_unlock(drbg->lock);
75e2c877
RS
796 return ret;
797}
798
c16de9d8 799/* Implements the default OpenSSL RAND_add() method */
75e2c877
RS
800static int drbg_add(const void *buf, int num, double randomness)
801{
c16de9d8 802 int ret = 0;
a93ba405 803 RAND_DRBG *drbg = RAND_DRBG_get0_master();
75e2c877 804
c16de9d8
DMSP
805 if (drbg == NULL)
806 return 0;
75e2c877 807
c16de9d8
DMSP
808 if (num < 0 || randomness < 0.0)
809 return 0;
75e2c877 810
c16de9d8
DMSP
811 if (randomness > (double)drbg->max_entropylen) {
812 /*
813 * The purpose of this check is to bound |randomness| by a
814 * relatively small value in order to prevent an integer
815 * overflow when multiplying by 8 in the rand_drbg_restart()
816 * call below.
817 */
818 return 0;
75e2c877
RS
819 }
820
c16de9d8
DMSP
821 CRYPTO_THREAD_write_lock(drbg->lock);
822 ret = rand_drbg_restart(drbg, buf,
823 (size_t)(unsigned int)num,
824 (size_t)(8*randomness));
825 CRYPTO_THREAD_unlock(drbg->lock);
826
827 return ret;
75e2c877
RS
828}
829
c16de9d8 830/* Implements the default OpenSSL RAND_seed() method */
75e2c877
RS
831static int drbg_seed(const void *buf, int num)
832{
833 return drbg_add(buf, num, num);
834}
835
c16de9d8 836/* Implements the default OpenSSL RAND_status() method */
75e2c877 837static int drbg_status(void)
12fb8c3d 838{
75e2c877 839 int ret;
a93ba405 840 RAND_DRBG *drbg = RAND_DRBG_get0_master();
0b14a5b7
KR
841
842 if (drbg == NULL)
843 return 0;
75e2c877 844
0b14a5b7
KR
845 CRYPTO_THREAD_write_lock(drbg->lock);
846 ret = drbg->state == DRBG_READY ? 1 : 0;
847 CRYPTO_THREAD_unlock(drbg->lock);
75e2c877 848 return ret;
12fb8c3d
RS
849}
850
0b14a5b7 851/*
a93ba405
DMSP
852 * Get the master DRBG.
853 * Returns pointer to the DRBG on success, NULL on failure.
854 *
855 */
856RAND_DRBG *RAND_DRBG_get0_master(void)
857{
858 if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
859 return NULL;
860
933033b6 861 return drbg_master;
a93ba405
DMSP
862}
863
864/*
865 * Get the public DRBG.
0b14a5b7
KR
866 * Returns pointer to the DRBG on success, NULL on failure.
867 */
a93ba405 868RAND_DRBG *RAND_DRBG_get0_public(void)
0b14a5b7 869{
c16de9d8 870 if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
0b14a5b7
KR
871 return NULL;
872
933033b6 873 return drbg_public;
0b14a5b7
KR
874}
875
876/*
a93ba405 877 * Get the private DRBG.
0b14a5b7
KR
878 * Returns pointer to the DRBG on success, NULL on failure.
879 */
a93ba405 880RAND_DRBG *RAND_DRBG_get0_private(void)
0b14a5b7 881{
c16de9d8 882 if (!RUN_ONCE(&rand_drbg_init, do_rand_drbg_init))
0b14a5b7
KR
883 return NULL;
884
933033b6 885 return drbg_private;
0b14a5b7
KR
886}
887
75e2c877
RS
888RAND_METHOD rand_meth = {
889 drbg_seed,
890 drbg_bytes,
ddc6a5c8 891 NULL,
75e2c877
RS
892 drbg_add,
893 drbg_bytes,
894 drbg_status
895};
896
897RAND_METHOD *RAND_OpenSSL(void)
12fb8c3d 898{
75e2c877 899 return &rand_meth;
12fb8c3d 900}