]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rand/drbg_lib.c
RAND_DRBG: add a callback data for entropy and nonce callbacks
[thirdparty/openssl.git] / crypto / rand / drbg_lib.c
CommitLineData
12fb8c3d 1/*
3c7d0945 2 * Copyright 2011-2018 The OpenSSL Project Authors. All Rights Reserved.
12fb8c3d 3 *
0db63de9 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
12fb8c3d
RS
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>
706457b7 14#include "rand_local.h"
0b14a5b7 15#include "internal/thread_once.h"
25f2138b
DMSP
16#include "crypto/rand.h"
17#include "crypto/cryptlib.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
a73d990e 32
da0d114c
MC
33typedef struct drbg_global_st {
34 /*
35 * The three shared DRBG instances
36 *
37 * There are three shared DRBG instances: <master>, <public>, and <private>.
38 */
a93ba405 39
da0d114c
MC
40 /*
41 * The <master> DRBG
42 *
43 * Not used directly by the application, only for reseeding the two other
44 * DRBGs. It reseeds itself by pulling either randomness from os entropy
45 * sources or by consuming randomness which was added by RAND_add().
46 *
47 * The <master> DRBG is a global instance which is accessed concurrently by
48 * all threads. The necessary locking is managed automatically by its child
49 * DRBG instances during reseeding.
50 */
51 RAND_DRBG *master_drbg;
52 /*
53 * The <public> DRBG
54 *
55 * Used by default for generating random bytes using RAND_bytes().
56 *
57 * The <public> DRBG is thread-local, i.e., there is one instance per
58 * thread.
59 */
60 CRYPTO_THREAD_LOCAL public_drbg;
61 /*
62 * The <private> DRBG
63 *
64 * Used by default for generating private keys using RAND_priv_bytes()
65 *
66 * The <private> DRBG is thread-local, i.e., there is one instance per
67 * thread.
68 */
69 CRYPTO_THREAD_LOCAL private_drbg;
70} DRBG_GLOBAL;
a93ba405 71
a2f27fd7
MC
72typedef struct drbg_nonce_global_st {
73 CRYPTO_RWLOCK *rand_nonce_lock;
74 int rand_nonce_count;
75} DRBG_NONCE_GLOBAL;
76
a93ba405 77/* NIST SP 800-90A DRBG recommends the use of a personalization string. */
d69226a3 78static const char ossl_pers_string[] = DRBG_DEFAULT_PERS_STRING;
a93ba405 79
8bf36651
SL
80#define RAND_DRBG_TYPE_FLAGS ( \
81 RAND_DRBG_FLAG_MASTER | RAND_DRBG_FLAG_PUBLIC | RAND_DRBG_FLAG_PRIVATE )
82
83#define RAND_DRBG_TYPE_MASTER 0
84#define RAND_DRBG_TYPE_PUBLIC 1
85#define RAND_DRBG_TYPE_PRIVATE 2
86
87/* Defaults */
88static int rand_drbg_type[3] = {
89 RAND_DRBG_TYPE, /* Master */
90 RAND_DRBG_TYPE, /* Public */
91 RAND_DRBG_TYPE /* Private */
92};
93static unsigned int rand_drbg_flags[3] = {
94 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_MASTER, /* Master */
95 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PUBLIC, /* Public */
96 RAND_DRBG_FLAGS | RAND_DRBG_FLAG_PRIVATE /* Private */
97};
31393fd9 98
4917e911
DMSP
99static unsigned int master_reseed_interval = MASTER_RESEED_INTERVAL;
100static unsigned int slave_reseed_interval = SLAVE_RESEED_INTERVAL;
101
102static time_t master_reseed_time_interval = MASTER_RESEED_TIME_INTERVAL;
103static time_t slave_reseed_time_interval = SLAVE_RESEED_TIME_INTERVAL;
104
c402e943
DMSP
105/* A logical OR of all used DRBG flag bits (currently there is only one) */
106static const unsigned int rand_drbg_used_flags =
8bf36651
SL
107 RAND_DRBG_FLAG_CTR_NO_DF | RAND_DRBG_FLAG_HMAC | RAND_DRBG_TYPE_FLAGS;
108
c402e943 109
da0d114c 110static RAND_DRBG *drbg_setup(OPENSSL_CTX *ctx, RAND_DRBG *parent, int drbg_type);
4f9dabbf 111
da0d114c
MC
112static RAND_DRBG *rand_drbg_new(OPENSSL_CTX *ctx,
113 int secure,
4f9dabbf
DMSP
114 int type,
115 unsigned int flags,
116 RAND_DRBG *parent);
0b14a5b7 117
8bf36651
SL
118static int is_ctr(int type)
119{
120 switch (type) {
121 case NID_aes_128_ctr:
122 case NID_aes_192_ctr:
123 case NID_aes_256_ctr:
124 return 1;
125 default:
126 return 0;
127 }
128}
129
130static int is_digest(int type)
131{
132 switch (type) {
133 case NID_sha1:
134 case NID_sha224:
135 case NID_sha256:
136 case NID_sha384:
137 case NID_sha512:
138 case NID_sha512_224:
139 case NID_sha512_256:
140 case NID_sha3_224:
141 case NID_sha3_256:
142 case NID_sha3_384:
143 case NID_sha3_512:
144 return 1;
145 default:
146 return 0;
147 }
148}
149
a2f27fd7
MC
150/*
151 * Initialize the OPENSSL_CTX global DRBGs on first use.
152 * Returns the allocated global data on success or NULL on failure.
153 */
154static void *drbg_ossl_ctx_new(OPENSSL_CTX *libctx)
155{
156 DRBG_GLOBAL *dgbl = OPENSSL_zalloc(sizeof(*dgbl));
157
158 if (dgbl == NULL)
159 return NULL;
160
242f84d0
MC
161#ifndef FIPS_MODE
162 /*
163 * We need to ensure that base libcrypto thread handling has been
164 * initialised.
165 */
166 OPENSSL_init_crypto(0, NULL);
167#endif
168
a2f27fd7
MC
169 if (!CRYPTO_THREAD_init_local(&dgbl->private_drbg, NULL))
170 goto err1;
171
172 if (!CRYPTO_THREAD_init_local(&dgbl->public_drbg, NULL))
173 goto err2;
174
175 dgbl->master_drbg = drbg_setup(libctx, NULL, RAND_DRBG_TYPE_MASTER);
176 if (dgbl->master_drbg == NULL)
177 goto err3;
178
179 return dgbl;
180
181 err3:
182 CRYPTO_THREAD_cleanup_local(&dgbl->public_drbg);
183 err2:
184 CRYPTO_THREAD_cleanup_local(&dgbl->private_drbg);
185 err1:
186 OPENSSL_free(dgbl);
187 return NULL;
188}
189
190static void drbg_ossl_ctx_free(void *vdgbl)
191{
192 DRBG_GLOBAL *dgbl = vdgbl;
193
85d09e88
RL
194 if (dgbl == NULL)
195 return;
196
a2f27fd7
MC
197 RAND_DRBG_free(dgbl->master_drbg);
198 CRYPTO_THREAD_cleanup_local(&dgbl->private_drbg);
199 CRYPTO_THREAD_cleanup_local(&dgbl->public_drbg);
200
201 OPENSSL_free(dgbl);
202}
203
204static const OPENSSL_CTX_METHOD drbg_ossl_ctx_method = {
205 drbg_ossl_ctx_new,
206 drbg_ossl_ctx_free,
207};
208
209/*
210 * drbg_ossl_ctx_new() calls drgb_setup() which calls rand_drbg_get_nonce()
211 * which needs to get the rand_nonce_lock out of the OPENSSL_CTX...but since
212 * drbg_ossl_ctx_new() hasn't finished running yet we need the rand_nonce_lock
213 * to be in a different global data object. Otherwise we will go into an
214 * infinite recursion loop.
215 */
216static void *drbg_nonce_ossl_ctx_new(OPENSSL_CTX *libctx)
217{
218 DRBG_NONCE_GLOBAL *dngbl = OPENSSL_zalloc(sizeof(*dngbl));
219
220 if (dngbl == NULL)
221 return NULL;
222
223 dngbl->rand_nonce_lock = CRYPTO_THREAD_lock_new();
224 if (dngbl->rand_nonce_lock == NULL) {
225 OPENSSL_free(dngbl);
226 return NULL;
227 }
228
229 return dngbl;
230}
231
232static void drbg_nonce_ossl_ctx_free(void *vdngbl)
233{
234 DRBG_NONCE_GLOBAL *dngbl = vdngbl;
235
85d09e88
RL
236 if (dngbl == NULL)
237 return;
238
a2f27fd7
MC
239 CRYPTO_THREAD_lock_free(dngbl->rand_nonce_lock);
240
241 OPENSSL_free(dngbl);
242}
243
244static const OPENSSL_CTX_METHOD drbg_nonce_ossl_ctx_method = {
245 drbg_nonce_ossl_ctx_new,
246 drbg_nonce_ossl_ctx_free,
247};
248
249static DRBG_GLOBAL *drbg_get_global(OPENSSL_CTX *libctx)
250{
251 return openssl_ctx_get_data(libctx, OPENSSL_CTX_DRBG_INDEX,
252 &drbg_ossl_ctx_method);
253}
254
255/* Implements the get_nonce() callback (see RAND_DRBG_set_callbacks()) */
256size_t rand_drbg_get_nonce(RAND_DRBG *drbg,
257 unsigned char **pout,
258 int entropy, size_t min_len, size_t max_len)
259{
260 size_t ret = 0;
261 RAND_POOL *pool;
262 DRBG_NONCE_GLOBAL *dngbl
263 = openssl_ctx_get_data(drbg->libctx, OPENSSL_CTX_DRBG_NONCE_INDEX,
264 &drbg_nonce_ossl_ctx_method);
265 struct {
266 void *instance;
267 int count;
268 } data;
269
270 if (dngbl == NULL)
271 return 0;
272
273 memset(&data, 0, sizeof(data));
1372560f 274 pool = rand_pool_new(0, 0, min_len, max_len);
a2f27fd7
MC
275 if (pool == NULL)
276 return 0;
277
278 if (rand_pool_add_nonce_data(pool) == 0)
279 goto err;
280
281 data.instance = drbg;
282 CRYPTO_atomic_add(&dngbl->rand_nonce_count, 1, &data.count,
283 dngbl->rand_nonce_lock);
284
285 if (rand_pool_add(pool, (unsigned char *)&data, sizeof(data), 0) == 0)
286 goto err;
287
288 ret = rand_pool_length(pool);
289 *pout = rand_pool_detach(pool);
290
291 err:
292 rand_pool_free(pool);
293
294 return ret;
295}
296
297/*
298 * Implements the cleanup_nonce() callback (see RAND_DRBG_set_callbacks())
299 *
300 */
301void rand_drbg_cleanup_nonce(RAND_DRBG *drbg,
302 unsigned char *out, size_t outlen)
303{
1372560f 304 OPENSSL_clear_free(out, outlen);
a2f27fd7
MC
305}
306
30a9d5d1
DMSP
307/*
308 * Set the |drbg|'s callback data pointer for the entropy and nonce callbacks
309 *
310 * The ownership of the context data remains with the caller,
311 * i.e., it is the caller's responsibility to keep it available as long
312 * as it is need by the callbacks and free it after use.
313 *
314 * Setting the callback data is allowed only if the drbg has not been
315 * initialized yet. Otherwise, the operation will fail.
316 *
317 * Returns 1 on success, 0 on failure.
318 */
319int RAND_DRBG_set_callback_data(RAND_DRBG *drbg, void *data)
320{
321 if (drbg->state != DRBG_UNINITIALISED
322 || drbg->parent != NULL)
323 return 0;
324
325 drbg->callback_data = data;
326 return 1;
327}
328
329/* Retrieve the callback data pointer */
330void *RAND_DRBG_get_callback_data(RAND_DRBG *drbg)
331{
332 return drbg->callback_data;
333}
334
12fb8c3d 335/*
31393fd9
DMSP
336 * Set/initialize |drbg| to be of type |type|, with optional |flags|.
337 *
338 * If |type| and |flags| are zero, use the defaults
efb8128a
DMSP
339 *
340 * Returns 1 on success, 0 on failure.
12fb8c3d 341 */
31393fd9 342int RAND_DRBG_set(RAND_DRBG *drbg, int type, unsigned int flags)
12fb8c3d
RS
343{
344 int ret = 1;
345
31393fd9 346 if (type == 0 && flags == 0) {
8bf36651
SL
347 type = rand_drbg_type[RAND_DRBG_TYPE_MASTER];
348 flags = rand_drbg_flags[RAND_DRBG_TYPE_MASTER];
349 }
350
351 /* If set is called multiple times - clear the old one */
54f3e855 352 if (drbg->type != 0 && (type != drbg->type || flags != drbg->flags)) {
8bf36651 353 drbg->meth->uninstantiate(drbg);
54f3e855
BE
354 rand_pool_free(drbg->adin_pool);
355 drbg->adin_pool = NULL;
31393fd9
DMSP
356 }
357
75e2c877
RS
358 drbg->state = DRBG_UNINITIALISED;
359 drbg->flags = flags;
31393fd9 360 drbg->type = type;
12fb8c3d 361
8bf36651 362 if (type == 0) {
12fb8c3d 363 /* Uninitialized; that's okay. */
54f3e855 364 drbg->meth = NULL;
12fb8c3d 365 return 1;
8bf36651 366 } else if (is_ctr(type)) {
8212d505 367 ret = drbg_ctr_init(drbg);
8bf36651
SL
368 } else if (is_digest(type)) {
369 if (flags & RAND_DRBG_FLAG_HMAC)
370 ret = drbg_hmac_init(drbg);
371 else
372 ret = drbg_hash_init(drbg);
373 } else {
17209be8
BE
374 drbg->type = 0;
375 drbg->flags = 0;
376 drbg->meth = NULL;
8bf36651
SL
377 RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_UNSUPPORTED_DRBG_TYPE);
378 return 0;
12fb8c3d
RS
379 }
380
17209be8
BE
381 if (ret == 0) {
382 drbg->state = DRBG_ERROR;
12fb8c3d 383 RANDerr(RAND_F_RAND_DRBG_SET, RAND_R_ERROR_INITIALISING_DRBG);
17209be8 384 }
12fb8c3d
RS
385 return ret;
386}
387
31393fd9
DMSP
388/*
389 * Set/initialize default |type| and |flag| for new drbg instances.
390 *
391 * Returns 1 on success, 0 on failure.
392 */
393int RAND_DRBG_set_defaults(int type, unsigned int flags)
394{
8bf36651
SL
395 int all;
396 if (!(is_digest(type) || is_ctr(type))) {
31393fd9
DMSP
397 RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_TYPE);
398 return 0;
31393fd9
DMSP
399 }
400
c402e943 401 if ((flags & ~rand_drbg_used_flags) != 0) {
31393fd9
DMSP
402 RANDerr(RAND_F_RAND_DRBG_SET_DEFAULTS, RAND_R_UNSUPPORTED_DRBG_FLAGS);
403 return 0;
404 }
405
8bf36651
SL
406 all = ((flags & RAND_DRBG_TYPE_FLAGS) == 0);
407 if (all || (flags & RAND_DRBG_FLAG_MASTER) != 0) {
408 rand_drbg_type[RAND_DRBG_TYPE_MASTER] = type;
409 rand_drbg_flags[RAND_DRBG_TYPE_MASTER] = flags | RAND_DRBG_FLAG_MASTER;
410 }
411 if (all || (flags & RAND_DRBG_FLAG_PUBLIC) != 0) {
412 rand_drbg_type[RAND_DRBG_TYPE_PUBLIC] = type;
413 rand_drbg_flags[RAND_DRBG_TYPE_PUBLIC] = flags | RAND_DRBG_FLAG_PUBLIC;
414 }
415 if (all || (flags & RAND_DRBG_FLAG_PRIVATE) != 0) {
416 rand_drbg_type[RAND_DRBG_TYPE_PRIVATE] = type;
417 rand_drbg_flags[RAND_DRBG_TYPE_PRIVATE] = flags | RAND_DRBG_FLAG_PRIVATE;
418 }
419 return 1;
31393fd9
DMSP
420}
421
422
12fb8c3d 423/*
4f9dabbf
DMSP
424 * Allocate memory and initialize a new DRBG. The DRBG is allocated on
425 * the secure heap if |secure| is nonzero and the secure heap is enabled.
426 * The |parent|, if not NULL, will be used as random source for reseeding.
a93ba405
DMSP
427 *
428 * Returns a pointer to the new DRBG instance on success, NULL on failure.
12fb8c3d 429 */
da0d114c
MC
430static RAND_DRBG *rand_drbg_new(OPENSSL_CTX *ctx,
431 int secure,
4f9dabbf
DMSP
432 int type,
433 unsigned int flags,
434 RAND_DRBG *parent)
12fb8c3d 435{
b3d113ed
P
436 RAND_DRBG *drbg = secure ? OPENSSL_secure_zalloc(sizeof(*drbg))
437 : OPENSSL_zalloc(sizeof(*drbg));
12fb8c3d 438
9d951a78 439 if (drbg == NULL) {
12fb8c3d 440 RANDerr(RAND_F_RAND_DRBG_NEW, ERR_R_MALLOC_FAILURE);
f96ff4e9 441 return NULL;
12fb8c3d 442 }
4f9dabbf 443
da0d114c 444 drbg->libctx = ctx;
4f9dabbf 445 drbg->secure = secure && CRYPTO_secure_allocated(drbg);
84952925 446 drbg->fork_id = openssl_get_fork_id();
75e2c877 447 drbg->parent = parent;
4917e911
DMSP
448
449 if (parent == NULL) {
d69226a3
P
450#ifdef FIPS_MODE
451 drbg->get_entropy = rand_crngt_get_entropy;
452 drbg->cleanup_entropy = rand_crngt_cleanup_entropy;
453#else
5bc6bcf8
DMSP
454 drbg->get_entropy = rand_drbg_get_entropy;
455 drbg->cleanup_entropy = rand_drbg_cleanup_entropy;
d69226a3 456#endif
5bc6bcf8
DMSP
457#ifndef RAND_DRBG_GET_RANDOM_NONCE
458 drbg->get_nonce = rand_drbg_get_nonce;
459 drbg->cleanup_nonce = rand_drbg_cleanup_nonce;
460#endif
461
4917e911
DMSP
462 drbg->reseed_interval = master_reseed_interval;
463 drbg->reseed_time_interval = master_reseed_time_interval;
464 } else {
5bc6bcf8
DMSP
465 drbg->get_entropy = rand_drbg_get_entropy;
466 drbg->cleanup_entropy = rand_drbg_cleanup_entropy;
467 /*
468 * Do not provide nonce callbacks, the child DRBGs will
469 * obtain their nonce using random bits from the parent.
470 */
471
4917e911
DMSP
472 drbg->reseed_interval = slave_reseed_interval;
473 drbg->reseed_time_interval = slave_reseed_time_interval;
474 }
475
efb8128a 476 if (RAND_DRBG_set(drbg, type, flags) == 0)
75e2c877
RS
477 goto err;
478
7caf122e
KR
479 if (parent != NULL) {
480 rand_drbg_lock(parent);
481 if (drbg->strength > parent->strength) {
482 /*
483 * We currently don't support the algorithm from NIST SP 800-90C
484 * 10.1.2 to use a weaker DRBG as source
485 */
486 rand_drbg_unlock(parent);
487 RANDerr(RAND_F_RAND_DRBG_NEW, RAND_R_PARENT_STRENGTH_TOO_WEAK);
488 goto err;
489 }
490 rand_drbg_unlock(parent);
35503b7c
KR
491 }
492
75e2c877
RS
493 return drbg;
494
c2e33a05 495 err:
c5e0b3a6 496 RAND_DRBG_free(drbg);
4f9dabbf 497
75e2c877 498 return NULL;
12fb8c3d
RS
499}
500
da0d114c
MC
501RAND_DRBG *RAND_DRBG_new_ex(OPENSSL_CTX *ctx, int type, unsigned int flags,
502 RAND_DRBG *parent)
503{
504 return rand_drbg_new(ctx, 0, type, flags, parent);
505}
506
4f9dabbf
DMSP
507RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent)
508{
da0d114c 509 return RAND_DRBG_new_ex(NULL, type, flags, parent);
4f9dabbf
DMSP
510}
511
da0d114c
MC
512RAND_DRBG *RAND_DRBG_secure_new_ex(OPENSSL_CTX *ctx, int type,
513 unsigned int flags, RAND_DRBG *parent)
4f9dabbf 514{
da0d114c 515 return rand_drbg_new(ctx, 1, type, flags, parent);
4f9dabbf
DMSP
516}
517
da0d114c
MC
518RAND_DRBG *RAND_DRBG_secure_new(int type, unsigned int flags, RAND_DRBG *parent)
519{
520 return RAND_DRBG_secure_new_ex(NULL, type, flags, parent);
521}
12fb8c3d 522/*
75e2c877 523 * Uninstantiate |drbg| and free all memory.
12fb8c3d 524 */
75e2c877 525void RAND_DRBG_free(RAND_DRBG *drbg)
12fb8c3d 526{
c16de9d8 527 if (drbg == NULL)
12fb8c3d
RS
528 return;
529
8212d505
DMSP
530 if (drbg->meth != NULL)
531 drbg->meth->uninstantiate(drbg);
54f3e855 532 rand_pool_free(drbg->adin_pool);
4f9dabbf 533 CRYPTO_THREAD_lock_free(drbg->lock);
a3327784 534#ifndef FIPS_MODE
d318389e 535 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RAND_DRBG, drbg, &drbg->ex_data);
a3327784 536#endif
4f9dabbf
DMSP
537
538 if (drbg->secure)
539 OPENSSL_secure_clear_free(drbg, sizeof(*drbg));
540 else
541 OPENSSL_clear_free(drbg, sizeof(*drbg));
12fb8c3d
RS
542}
543
544/*
75e2c877 545 * Instantiate |drbg|, after it has been initialized. Use |pers| and
12fb8c3d 546 * |perslen| as prediction-resistance input.
2139145b
BK
547 *
548 * Requires that drbg->lock is already locked for write, if non-null.
efb8128a
DMSP
549 *
550 * Returns 1 on success, 0 on failure.
12fb8c3d 551 */
75e2c877 552int RAND_DRBG_instantiate(RAND_DRBG *drbg,
12fb8c3d
RS
553 const unsigned char *pers, size_t perslen)
554{
12fb8c3d 555 unsigned char *nonce = NULL, *entropy = NULL;
aa048aef 556 size_t noncelen = 0, entropylen = 0;
2a70d65b
KR
557 size_t min_entropy = drbg->strength;
558 size_t min_entropylen = drbg->min_entropylen;
559 size_t max_entropylen = drbg->max_entropylen;
12fb8c3d 560
aa048aef 561 if (perslen > drbg->max_perslen) {
75e2c877
RS
562 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
563 RAND_R_PERSONALISATION_STRING_TOO_LONG);
12fb8c3d
RS
564 goto end;
565 }
8212d505 566
272c0df8 567 if (drbg->meth == NULL) {
8212d505
DMSP
568 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE,
569 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED);
570 goto end;
571 }
572
75e2c877 573 if (drbg->state != DRBG_UNINITIALISED) {
51ba9ebd
RS
574 if (drbg->state == DRBG_ERROR)
575 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_IN_ERROR_STATE);
576 else
577 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ALREADY_INSTANTIATED);
12fb8c3d
RS
578 goto end;
579 }
580
75e2c877 581 drbg->state = DRBG_ERROR;
2a70d65b
KR
582
583 /*
584 * NIST SP800-90Ar1 section 9.1 says you can combine getting the entropy
585 * and nonce in 1 call by increasing the entropy with 50% and increasing
c2969ff6 586 * the minimum length to accommodate the length of the nonce.
2a70d65b
KR
587 * We do this in case a nonce is require and get_nonce is NULL.
588 */
589 if (drbg->min_noncelen > 0 && drbg->get_nonce == NULL) {
590 min_entropy += drbg->strength / 2;
591 min_entropylen += drbg->min_noncelen;
592 max_entropylen += drbg->max_noncelen;
593 }
594
a83dc59a
BE
595 drbg->reseed_next_counter = tsan_load(&drbg->reseed_prop_counter);
596 if (drbg->reseed_next_counter) {
597 drbg->reseed_next_counter++;
598 if(!drbg->reseed_next_counter)
599 drbg->reseed_next_counter = 1;
600 }
601
75e2c877 602 if (drbg->get_entropy != NULL)
2a70d65b
KR
603 entropylen = drbg->get_entropy(drbg, &entropy, min_entropy,
604 min_entropylen, max_entropylen, 0);
605 if (entropylen < min_entropylen
c2e33a05 606 || entropylen > max_entropylen) {
75e2c877 607 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_ENTROPY);
12fb8c3d
RS
608 goto end;
609 }
610
2a70d65b 611 if (drbg->min_noncelen > 0 && drbg->get_nonce != NULL) {
75e2c877 612 noncelen = drbg->get_nonce(drbg, &nonce, drbg->strength / 2,
aa048aef
DMSP
613 drbg->min_noncelen, drbg->max_noncelen);
614 if (noncelen < drbg->min_noncelen || noncelen > drbg->max_noncelen) {
2a70d65b 615 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_RETRIEVING_NONCE);
12fb8c3d
RS
616 goto end;
617 }
618 }
619
8212d505 620 if (!drbg->meth->instantiate(drbg, entropy, entropylen,
12fb8c3d 621 nonce, noncelen, pers, perslen)) {
75e2c877 622 RANDerr(RAND_F_RAND_DRBG_INSTANTIATE, RAND_R_ERROR_INSTANTIATING_DRBG);
12fb8c3d
RS
623 goto end;
624 }
625
75e2c877 626 drbg->state = DRBG_READY;
8bf36651 627 drbg->reseed_gen_counter = 1;
08a65d96 628 drbg->reseed_time = time(NULL);
a83dc59a 629 tsan_store(&drbg->reseed_prop_counter, drbg->reseed_next_counter);
12fb8c3d 630
c2e33a05 631 end:
75e2c877 632 if (entropy != NULL && drbg->cleanup_entropy != NULL)
6969a3f4 633 drbg->cleanup_entropy(drbg, entropy, entropylen);
c2e33a05 634 if (nonce != NULL && drbg->cleanup_nonce != NULL)
6969a3f4 635 drbg->cleanup_nonce(drbg, nonce, noncelen);
75e2c877 636 if (drbg->state == DRBG_READY)
12fb8c3d 637 return 1;
12fb8c3d
RS
638 return 0;
639}
640
641/*
75e2c877 642 * Uninstantiate |drbg|. Must be instantiated before it can be used.
2139145b
BK
643 *
644 * Requires that drbg->lock is already locked for write, if non-null.
efb8128a
DMSP
645 *
646 * Returns 1 on success, 0 on failure.
12fb8c3d 647 */
75e2c877 648int RAND_DRBG_uninstantiate(RAND_DRBG *drbg)
12fb8c3d 649{
8bf36651 650 int index = -1, type, flags;
272c0df8 651 if (drbg->meth == NULL) {
fb9c3ff5 652 drbg->state = DRBG_ERROR;
8212d505
DMSP
653 RANDerr(RAND_F_RAND_DRBG_UNINSTANTIATE,
654 RAND_R_NO_DRBG_IMPLEMENTATION_SELECTED);
655 return 0;
656 }
657
efb8128a
DMSP
658 /* Clear the entire drbg->ctr struct, then reset some important
659 * members of the drbg->ctr struct (e.g. keysize, df_ks) to their
660 * initial values.
661 */
8212d505 662 drbg->meth->uninstantiate(drbg);
8bf36651
SL
663
664 /* The reset uses the default values for type and flags */
665 if (drbg->flags & RAND_DRBG_FLAG_MASTER)
666 index = RAND_DRBG_TYPE_MASTER;
667 else if (drbg->flags & RAND_DRBG_FLAG_PRIVATE)
668 index = RAND_DRBG_TYPE_PRIVATE;
669 else if (drbg->flags & RAND_DRBG_FLAG_PUBLIC)
670 index = RAND_DRBG_TYPE_PUBLIC;
671
672 if (index != -1) {
673 flags = rand_drbg_flags[index];
674 type = rand_drbg_type[index];
675 } else {
676 flags = drbg->flags;
677 type = drbg->type;
678 }
679 return RAND_DRBG_set(drbg, type, flags);
12fb8c3d
RS
680}
681
682/*
c16de9d8 683 * Reseed |drbg|, mixing in the specified data
2139145b
BK
684 *
685 * Requires that drbg->lock is already locked for write, if non-null.
efb8128a
DMSP
686 *
687 * Returns 1 on success, 0 on failure.
12fb8c3d 688 */
75e2c877 689int RAND_DRBG_reseed(RAND_DRBG *drbg,
eb238134
KR
690 const unsigned char *adin, size_t adinlen,
691 int prediction_resistance)
12fb8c3d
RS
692{
693 unsigned char *entropy = NULL;
aa048aef 694 size_t entropylen = 0;
75e2c877
RS
695
696 if (drbg->state == DRBG_ERROR) {
697 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_IN_ERROR_STATE);
698 return 0;
699 }
700 if (drbg->state == DRBG_UNINITIALISED) {
701 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_NOT_INSTANTIATED);
702 return 0;
12fb8c3d
RS
703 }
704
272c0df8 705 if (adin == NULL) {
12fb8c3d 706 adinlen = 0;
272c0df8 707 } else if (adinlen > drbg->max_adinlen) {
75e2c877
RS
708 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
709 return 0;
12fb8c3d
RS
710 }
711
75e2c877 712 drbg->state = DRBG_ERROR;
a83dc59a
BE
713
714 drbg->reseed_next_counter = tsan_load(&drbg->reseed_prop_counter);
715 if (drbg->reseed_next_counter) {
716 drbg->reseed_next_counter++;
717 if(!drbg->reseed_next_counter)
718 drbg->reseed_next_counter = 1;
719 }
720
75e2c877 721 if (drbg->get_entropy != NULL)
aa048aef 722 entropylen = drbg->get_entropy(drbg, &entropy, drbg->strength,
eb238134
KR
723 drbg->min_entropylen,
724 drbg->max_entropylen,
725 prediction_resistance);
c16de9d8 726 if (entropylen < drbg->min_entropylen
c2e33a05 727 || entropylen > drbg->max_entropylen) {
75e2c877 728 RANDerr(RAND_F_RAND_DRBG_RESEED, RAND_R_ERROR_RETRIEVING_ENTROPY);
12fb8c3d
RS
729 goto end;
730 }
731
8212d505 732 if (!drbg->meth->reseed(drbg, entropy, entropylen, adin, adinlen))
12fb8c3d 733 goto end;
a93ba405 734
75e2c877 735 drbg->state = DRBG_READY;
8bf36651 736 drbg->reseed_gen_counter = 1;
08a65d96 737 drbg->reseed_time = time(NULL);
a83dc59a 738 tsan_store(&drbg->reseed_prop_counter, drbg->reseed_next_counter);
12fb8c3d 739
c2e33a05 740 end:
75e2c877 741 if (entropy != NULL && drbg->cleanup_entropy != NULL)
6969a3f4 742 drbg->cleanup_entropy(drbg, entropy, entropylen);
75e2c877 743 if (drbg->state == DRBG_READY)
12fb8c3d 744 return 1;
12fb8c3d
RS
745 return 0;
746}
747
c16de9d8
DMSP
748/*
749 * Restart |drbg|, using the specified entropy or additional input
750 *
751 * Tries its best to get the drbg instantiated by all means,
752 * regardless of its current state.
753 *
754 * Optionally, a |buffer| of |len| random bytes can be passed,
755 * which is assumed to contain at least |entropy| bits of entropy.
756 *
757 * If |entropy| > 0, the buffer content is used as entropy input.
758 *
759 * If |entropy| == 0, the buffer content is used as additional input
760 *
761 * Returns 1 on success, 0 on failure.
762 *
763 * This function is used internally only.
764 */
765int rand_drbg_restart(RAND_DRBG *drbg,
766 const unsigned char *buffer, size_t len, size_t entropy)
767{
768 int reseeded = 0;
769 const unsigned char *adin = NULL;
770 size_t adinlen = 0;
771
31f32abb 772 if (drbg->seed_pool != NULL) {
c16de9d8 773 RANDerr(RAND_F_RAND_DRBG_RESTART, ERR_R_INTERNAL_ERROR);
3064b551 774 drbg->state = DRBG_ERROR;
31f32abb
BE
775 rand_pool_free(drbg->seed_pool);
776 drbg->seed_pool = NULL;
3064b551 777 return 0;
c16de9d8
DMSP
778 }
779
780 if (buffer != NULL) {
781 if (entropy > 0) {
782 if (drbg->max_entropylen < len) {
783 RANDerr(RAND_F_RAND_DRBG_RESTART,
784 RAND_R_ENTROPY_INPUT_TOO_LONG);
3064b551 785 drbg->state = DRBG_ERROR;
c16de9d8
DMSP
786 return 0;
787 }
788
789 if (entropy > 8 * len) {
790 RANDerr(RAND_F_RAND_DRBG_RESTART, RAND_R_ENTROPY_OUT_OF_RANGE);
3064b551 791 drbg->state = DRBG_ERROR;
c16de9d8
DMSP
792 return 0;
793 }
794
795 /* will be picked up by the rand_drbg_get_entropy() callback */
31f32abb
BE
796 drbg->seed_pool = rand_pool_attach(buffer, len, entropy);
797 if (drbg->seed_pool == NULL)
c16de9d8 798 return 0;
c16de9d8
DMSP
799 } else {
800 if (drbg->max_adinlen < len) {
801 RANDerr(RAND_F_RAND_DRBG_RESTART,
802 RAND_R_ADDITIONAL_INPUT_TOO_LONG);
3064b551 803 drbg->state = DRBG_ERROR;
c16de9d8
DMSP
804 return 0;
805 }
806 adin = buffer;
807 adinlen = len;
808 }
809 }
810
811 /* repair error state */
efb8128a 812 if (drbg->state == DRBG_ERROR)
c16de9d8
DMSP
813 RAND_DRBG_uninstantiate(drbg);
814
815 /* repair uninitialized state */
816 if (drbg->state == DRBG_UNINITIALISED) {
a93ba405
DMSP
817 /* reinstantiate drbg */
818 RAND_DRBG_instantiate(drbg,
819 (const unsigned char *) ossl_pers_string,
820 sizeof(ossl_pers_string) - 1);
c16de9d8
DMSP
821 /* already reseeded. prevent second reseeding below */
822 reseeded = (drbg->state == DRBG_READY);
823 }
824
825 /* refresh current state if entropy or additional input has been provided */
826 if (drbg->state == DRBG_READY) {
827 if (adin != NULL) {
828 /*
829 * mix in additional input without reseeding
830 *
831 * Similar to RAND_DRBG_reseed(), but the provided additional
832 * data |adin| is mixed into the current state without pulling
833 * entropy from the trusted entropy source using get_entropy().
834 * This is not a reseeding in the strict sense of NIST SP 800-90A.
835 */
8212d505 836 drbg->meth->reseed(drbg, adin, adinlen, NULL, 0);
c16de9d8
DMSP
837 } else if (reseeded == 0) {
838 /* do a full reseeding if it has not been done yet above */
eb238134 839 RAND_DRBG_reseed(drbg, NULL, 0, 0);
c16de9d8
DMSP
840 }
841 }
842
31f32abb
BE
843 rand_pool_free(drbg->seed_pool);
844 drbg->seed_pool = NULL;
c16de9d8
DMSP
845
846 return drbg->state == DRBG_READY;
847}
848
12fb8c3d
RS
849/*
850 * Generate |outlen| bytes into the buffer at |out|. Reseed if we need
851 * to or if |prediction_resistance| is set. Additional input can be
852 * sent in |adin| and |adinlen|.
c16de9d8 853 *
2139145b
BK
854 * Requires that drbg->lock is already locked for write, if non-null.
855 *
c16de9d8
DMSP
856 * Returns 1 on success, 0 on failure.
857 *
12fb8c3d 858 */
75e2c877 859int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,
12fb8c3d
RS
860 int prediction_resistance,
861 const unsigned char *adin, size_t adinlen)
862{
84952925 863 int fork_id;
e0b625f9
DMSP
864 int reseed_required = 0;
865
c16de9d8
DMSP
866 if (drbg->state != DRBG_READY) {
867 /* try to recover from previous errors */
868 rand_drbg_restart(drbg, NULL, 0, 0);
869
870 if (drbg->state == DRBG_ERROR) {
871 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_IN_ERROR_STATE);
872 return 0;
873 }
874 if (drbg->state == DRBG_UNINITIALISED) {
875 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_NOT_INSTANTIATED);
876 return 0;
877 }
12fb8c3d 878 }
c16de9d8 879
75e2c877
RS
880 if (outlen > drbg->max_request) {
881 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_REQUEST_TOO_LARGE_FOR_DRBG);
882 return 0;
883 }
aa048aef 884 if (adinlen > drbg->max_adinlen) {
75e2c877
RS
885 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_ADDITIONAL_INPUT_TOO_LONG);
886 return 0;
12fb8c3d
RS
887 }
888
84952925
DMSP
889 fork_id = openssl_get_fork_id();
890
891 if (drbg->fork_id != fork_id) {
892 drbg->fork_id = fork_id;
e0b625f9 893 reseed_required = 1;
a35f607c
RS
894 }
895
a93ba405 896 if (drbg->reseed_interval > 0) {
8bf36651 897 if (drbg->reseed_gen_counter > drbg->reseed_interval)
a93ba405
DMSP
898 reseed_required = 1;
899 }
08a65d96
DMSP
900 if (drbg->reseed_time_interval > 0) {
901 time_t now = time(NULL);
902 if (now < drbg->reseed_time
903 || now - drbg->reseed_time >= drbg->reseed_time_interval)
904 reseed_required = 1;
905 }
a83dc59a
BE
906 if (drbg->parent != NULL) {
907 unsigned int reseed_counter = tsan_load(&drbg->reseed_prop_counter);
908 if (reseed_counter > 0
909 && tsan_load(&drbg->parent->reseed_prop_counter)
910 != reseed_counter)
a93ba405
DMSP
911 reseed_required = 1;
912 }
12fb8c3d 913
e0b625f9 914 if (reseed_required || prediction_resistance) {
eb238134 915 if (!RAND_DRBG_reseed(drbg, adin, adinlen, prediction_resistance)) {
75e2c877
RS
916 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_RESEED_ERROR);
917 return 0;
12fb8c3d
RS
918 }
919 adin = NULL;
920 adinlen = 0;
921 }
922
8212d505 923 if (!drbg->meth->generate(drbg, out, outlen, adin, adinlen)) {
75e2c877
RS
924 drbg->state = DRBG_ERROR;
925 RANDerr(RAND_F_RAND_DRBG_GENERATE, RAND_R_GENERATE_ERROR);
926 return 0;
12fb8c3d 927 }
75e2c877 928
8bf36651 929 drbg->reseed_gen_counter++;
e0b625f9 930
12fb8c3d 931 return 1;
12fb8c3d
RS
932}
933
20928ff6
KR
934/*
935 * Generates |outlen| random bytes and stores them in |out|. It will
936 * using the given |drbg| to generate the bytes.
937 *
938 * Requires that drbg->lock is already locked for write, if non-null.
939 *
940 * Returns 1 on success 0 on failure.
941 */
942int RAND_DRBG_bytes(RAND_DRBG *drbg, unsigned char *out, size_t outlen)
943{
944 unsigned char *additional = NULL;
945 size_t additional_len;
1648338b 946 size_t chunk;
54f3e855 947 size_t ret = 0;
20928ff6 948
54f3e855
BE
949 if (drbg->adin_pool == NULL) {
950 if (drbg->type == 0)
951 goto err;
1372560f 952 drbg->adin_pool = rand_pool_new(0, 0, 0, drbg->max_adinlen);
54f3e855
BE
953 if (drbg->adin_pool == NULL)
954 goto err;
955 }
956
957 additional_len = rand_drbg_get_additional_data(drbg->adin_pool,
958 &additional);
1648338b
DMSP
959
960 for ( ; outlen > 0; outlen -= chunk, out += chunk) {
961 chunk = outlen;
962 if (chunk > drbg->max_request)
963 chunk = drbg->max_request;
964 ret = RAND_DRBG_generate(drbg, out, chunk, 0, additional, additional_len);
965 if (!ret)
966 goto err;
967 }
968 ret = 1;
969
54f3e855
BE
970 err:
971 if (additional != NULL)
972 rand_drbg_cleanup_additional_data(drbg->adin_pool, additional);
20928ff6
KR
973
974 return ret;
975}
976
12fb8c3d 977/*
c16de9d8
DMSP
978 * Set the RAND_DRBG callbacks for obtaining entropy and nonce.
979 *
a73d990e
DMSP
980 * Setting the callbacks is allowed only if the drbg has not been
981 * initialized yet. Otherwise, the operation will fail.
c16de9d8 982 *
a73d990e 983 * Returns 1 on success, 0 on failure.
12fb8c3d 984 */
75e2c877 985int RAND_DRBG_set_callbacks(RAND_DRBG *drbg,
c16de9d8
DMSP
986 RAND_DRBG_get_entropy_fn get_entropy,
987 RAND_DRBG_cleanup_entropy_fn cleanup_entropy,
988 RAND_DRBG_get_nonce_fn get_nonce,
989 RAND_DRBG_cleanup_nonce_fn cleanup_nonce)
12fb8c3d 990{
a83dc59a
BE
991 if (drbg->state != DRBG_UNINITIALISED
992 || drbg->parent != NULL)
12fb8c3d 993 return 0;
c16de9d8
DMSP
994 drbg->get_entropy = get_entropy;
995 drbg->cleanup_entropy = cleanup_entropy;
996 drbg->get_nonce = get_nonce;
997 drbg->cleanup_nonce = cleanup_nonce;
12fb8c3d
RS
998 return 1;
999}
1000
1001/*
75e2c877 1002 * Set the reseed interval.
a93ba405
DMSP
1003 *
1004 * The drbg will reseed automatically whenever the number of generate
1005 * requests exceeds the given reseed interval. If the reseed interval
08a65d96 1006 * is 0, then this feature is disabled.
a93ba405
DMSP
1007 *
1008 * Returns 1 on success, 0 on failure.
12fb8c3d 1009 */
a93ba405 1010int RAND_DRBG_set_reseed_interval(RAND_DRBG *drbg, unsigned int interval)
12fb8c3d 1011{
a93ba405 1012 if (interval > MAX_RESEED_INTERVAL)
4c75ee85 1013 return 0;
75e2c877 1014 drbg->reseed_interval = interval;
4c75ee85 1015 return 1;
12fb8c3d
RS
1016}
1017
08a65d96
DMSP
1018/*
1019 * Set the reseed time interval.
1020 *
1021 * The drbg will reseed automatically whenever the time elapsed since
1022 * the last reseeding exceeds the given reseed time interval. For safety,
1023 * a reseeding will also occur if the clock has been reset to a smaller
1024 * value.
1025 *
1026 * Returns 1 on success, 0 on failure.
1027 */
1028int RAND_DRBG_set_reseed_time_interval(RAND_DRBG *drbg, time_t interval)
1029{
1030 if (interval > MAX_RESEED_TIME_INTERVAL)
1031 return 0;
1032 drbg->reseed_time_interval = interval;
1033 return 1;
1034}
1035
4917e911
DMSP
1036/*
1037 * Set the default values for reseed (time) intervals of new DRBG instances
1038 *
1039 * The default values can be set independently for master DRBG instances
1040 * (without a parent) and slave DRBG instances (with parent).
1041 *
1042 * Returns 1 on success, 0 on failure.
1043 */
1044
1045int RAND_DRBG_set_reseed_defaults(
1046 unsigned int _master_reseed_interval,
1047 unsigned int _slave_reseed_interval,
1048 time_t _master_reseed_time_interval,
1049 time_t _slave_reseed_time_interval
1050 )
1051{
1052 if (_master_reseed_interval > MAX_RESEED_INTERVAL
1053 || _slave_reseed_interval > MAX_RESEED_INTERVAL)
1054 return 0;
1055
1056 if (_master_reseed_time_interval > MAX_RESEED_TIME_INTERVAL
1057 || _slave_reseed_time_interval > MAX_RESEED_TIME_INTERVAL)
1058 return 0;
1059
1060 master_reseed_interval = _master_reseed_interval;
1061 slave_reseed_interval = _slave_reseed_interval;
1062
1063 master_reseed_time_interval = _master_reseed_time_interval;
1064 slave_reseed_time_interval = _slave_reseed_time_interval;
1065
1066 return 1;
1067}
3ce1c27b
DMSP
1068
1069/*
1070 * Locks the given drbg. Locking a drbg which does not have locking
1071 * enabled is considered a successful no-op.
1072 *
1073 * Returns 1 on success, 0 on failure.
1074 */
812b1537 1075int rand_drbg_lock(RAND_DRBG *drbg)
3ce1c27b
DMSP
1076{
1077 if (drbg->lock != NULL)
1078 return CRYPTO_THREAD_write_lock(drbg->lock);
1079
1080 return 1;
1081}
1082
1083/*
1084 * Unlocks the given drbg. Unlocking a drbg which does not have locking
1085 * enabled is considered a successful no-op.
1086 *
1087 * Returns 1 on success, 0 on failure.
1088 */
812b1537 1089int rand_drbg_unlock(RAND_DRBG *drbg)
3ce1c27b
DMSP
1090{
1091 if (drbg->lock != NULL)
1092 return CRYPTO_THREAD_unlock(drbg->lock);
1093
1094 return 1;
1095}
1096
1097/*
1098 * Enables locking for the given drbg
1099 *
1100 * Locking can only be enabled if the random generator
1101 * is in the uninitialized state.
1102 *
1103 * Returns 1 on success, 0 on failure.
1104 */
812b1537 1105int rand_drbg_enable_locking(RAND_DRBG *drbg)
3ce1c27b
DMSP
1106{
1107 if (drbg->state != DRBG_UNINITIALISED) {
1108 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
1109 RAND_R_DRBG_ALREADY_INITIALIZED);
1110 return 0;
1111 }
1112
1113 if (drbg->lock == NULL) {
4f9dabbf 1114 if (drbg->parent != NULL && drbg->parent->lock == NULL) {
3ce1c27b
DMSP
1115 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
1116 RAND_R_PARENT_LOCKING_NOT_ENABLED);
1117 return 0;
1118 }
1119
1120 drbg->lock = CRYPTO_THREAD_lock_new();
1121 if (drbg->lock == NULL) {
1122 RANDerr(RAND_F_RAND_DRBG_ENABLE_LOCKING,
1123 RAND_R_FAILED_TO_CREATE_LOCK);
1124 return 0;
1125 }
1126 }
1127
1128 return 1;
1129}
1130
a3327784 1131#ifndef FIPS_MODE
12fb8c3d
RS
1132/*
1133 * Get and set the EXDATA
1134 */
75e2c877
RS
1135int RAND_DRBG_set_ex_data(RAND_DRBG *drbg, int idx, void *arg)
1136{
1137 return CRYPTO_set_ex_data(&drbg->ex_data, idx, arg);
1138}
1139
1140void *RAND_DRBG_get_ex_data(const RAND_DRBG *drbg, int idx)
1141{
1142 return CRYPTO_get_ex_data(&drbg->ex_data, idx);
1143}
a3327784 1144#endif
75e2c877
RS
1145
1146/*
1147 * The following functions provide a RAND_METHOD that works on the
1148 * global DRBG. They lock.
1149 */
1150
0b14a5b7 1151/*
933033b6
DMSP
1152 * Allocates a new global DRBG on the secure heap (if enabled) and
1153 * initializes it with default settings.
a93ba405
DMSP
1154 *
1155 * Returns a pointer to the new DRBG instance on success, NULL on failure.
0b14a5b7 1156 */
da0d114c 1157static RAND_DRBG *drbg_setup(OPENSSL_CTX *ctx, RAND_DRBG *parent, int drbg_type)
0b14a5b7 1158{
933033b6 1159 RAND_DRBG *drbg;
0b14a5b7 1160
da0d114c
MC
1161 drbg = RAND_DRBG_secure_new_ex(ctx, rand_drbg_type[drbg_type],
1162 rand_drbg_flags[drbg_type], parent);
933033b6
DMSP
1163 if (drbg == NULL)
1164 return NULL;
1165
7caf122e
KR
1166 /* Only the master DRBG needs to have a lock */
1167 if (parent == NULL && rand_drbg_enable_locking(drbg) == 0)
933033b6 1168 goto err;
a93ba405 1169
a93ba405 1170 /* enable seed propagation */
a83dc59a 1171 tsan_store(&drbg->reseed_prop_counter, 1);
a93ba405 1172
c16de9d8 1173 /*
43687d68 1174 * Ignore instantiation error to support just-in-time instantiation.
c16de9d8
DMSP
1175 *
1176 * The state of the drbg will be checked in RAND_DRBG_generate() and
1177 * an automatic recovery is attempted.
1178 */
43687d68
DMSP
1179 (void)RAND_DRBG_instantiate(drbg,
1180 (const unsigned char *) ossl_pers_string,
1181 sizeof(ossl_pers_string) - 1);
933033b6
DMSP
1182 return drbg;
1183
1184err:
4f9dabbf 1185 RAND_DRBG_free(drbg);
933033b6 1186 return NULL;
0b14a5b7
KR
1187}
1188
da747958 1189static void drbg_delete_thread_state(void *arg)
7caf122e 1190{
da747958 1191 OPENSSL_CTX *ctx = arg;
da0d114c 1192 DRBG_GLOBAL *dgbl = drbg_get_global(ctx);
7caf122e
KR
1193 RAND_DRBG *drbg;
1194
da0d114c
MC
1195 if (dgbl == NULL)
1196 return;
1197 drbg = CRYPTO_THREAD_get_local(&dgbl->public_drbg);
1198 CRYPTO_THREAD_set_local(&dgbl->public_drbg, NULL);
7caf122e
KR
1199 RAND_DRBG_free(drbg);
1200
da0d114c
MC
1201 drbg = CRYPTO_THREAD_get_local(&dgbl->private_drbg);
1202 CRYPTO_THREAD_set_local(&dgbl->private_drbg, NULL);
7caf122e 1203 RAND_DRBG_free(drbg);
0b14a5b7
KR
1204}
1205
c16de9d8 1206/* Implements the default OpenSSL RAND_bytes() method */
75e2c877
RS
1207static int drbg_bytes(unsigned char *out, int count)
1208{
f61f62ea 1209 int ret;
a93ba405 1210 RAND_DRBG *drbg = RAND_DRBG_get0_public();
75e2c877 1211
0b14a5b7
KR
1212 if (drbg == NULL)
1213 return 0;
1214
f61f62ea 1215 ret = RAND_DRBG_bytes(drbg, out, count);
f61f62ea 1216
75e2c877
RS
1217 return ret;
1218}
1219
8817215d
DMSP
1220/*
1221 * Calculates the minimum length of a full entropy buffer
1222 * which is necessary to seed (i.e. instantiate) the DRBG
1223 * successfully.
8817215d 1224 */
1c615e4c 1225size_t rand_drbg_seedlen(RAND_DRBG *drbg)
8817215d
DMSP
1226{
1227 /*
1228 * If no os entropy source is available then RAND_seed(buffer, bufsize)
1229 * is expected to succeed if and only if the buffer length satisfies
1230 * the following requirements, which follow from the calculations
1231 * in RAND_DRBG_instantiate().
1232 */
1233 size_t min_entropy = drbg->strength;
1234 size_t min_entropylen = drbg->min_entropylen;
1235
1236 /*
1237 * Extra entropy for the random nonce in the absence of a
1238 * get_nonce callback, see comment in RAND_DRBG_instantiate().
1239 */
1240 if (drbg->min_noncelen > 0 && drbg->get_nonce == NULL) {
1241 min_entropy += drbg->strength / 2;
1242 min_entropylen += drbg->min_noncelen;
1243 }
1244
1245 /*
1246 * Convert entropy requirement from bits to bytes
1247 * (dividing by 8 without rounding upwards, because
1248 * all entropy requirements are divisible by 8).
1249 */
1250 min_entropy >>= 3;
1251
1252 /* Return a value that satisfies both requirements */
1253 return min_entropy > min_entropylen ? min_entropy : min_entropylen;
1254}
1255
c16de9d8 1256/* Implements the default OpenSSL RAND_add() method */
75e2c877
RS
1257static int drbg_add(const void *buf, int num, double randomness)
1258{
c16de9d8 1259 int ret = 0;
a93ba405 1260 RAND_DRBG *drbg = RAND_DRBG_get0_master();
8817215d 1261 size_t buflen;
59f90557 1262 size_t seedlen;
75e2c877 1263
c16de9d8
DMSP
1264 if (drbg == NULL)
1265 return 0;
75e2c877 1266
c16de9d8
DMSP
1267 if (num < 0 || randomness < 0.0)
1268 return 0;
75e2c877 1269
4011bab1 1270 rand_drbg_lock(drbg);
59f90557
DMSP
1271 seedlen = rand_drbg_seedlen(drbg);
1272
8817215d
DMSP
1273 buflen = (size_t)num;
1274
3a50a8a9
DMSP
1275#ifdef FIPS_MODE
1276 /*
1277 * NIST SP-800-90A mandates that entropy *shall not* be provided
1278 * by the consuming application. By setting the randomness to zero,
1279 * we ensure that the buffer contents will be added to the internal
1280 * state of the DRBG only as additional data.
1281 *
1282 * (NIST SP-800-90Ar1, Sections 9.1 and 9.2)
1283 */
1284 randomness = 0.0;
1285#endif
8817215d
DMSP
1286 if (buflen < seedlen || randomness < (double) seedlen) {
1287#if defined(OPENSSL_RAND_SEED_NONE)
1288 /*
1289 * If no os entropy source is available, a reseeding will fail
1290 * inevitably. So we use a trick to mix the buffer contents into
1291 * the DRBG state without forcing a reseeding: we generate a
1292 * dummy random byte, using the buffer content as additional data.
4011bab1 1293 * Note: This won't work with RAND_DRBG_FLAG_CTR_NO_DF.
8817215d
DMSP
1294 */
1295 unsigned char dummy[1];
1296
4011bab1
BE
1297 ret = RAND_DRBG_generate(drbg, dummy, sizeof(dummy), 0, buf, buflen);
1298 rand_drbg_unlock(drbg);
1299 return ret;
8817215d
DMSP
1300#else
1301 /*
3a50a8a9 1302 * If an os entropy source is available then we declare the buffer content
8817215d
DMSP
1303 * as additional data by setting randomness to zero and trigger a regular
1304 * reseeding.
1305 */
1306 randomness = 0.0;
1307#endif
1308 }
1309
8817215d 1310 if (randomness > (double)seedlen) {
c16de9d8
DMSP
1311 /*
1312 * The purpose of this check is to bound |randomness| by a
1313 * relatively small value in order to prevent an integer
1314 * overflow when multiplying by 8 in the rand_drbg_restart()
3064b551
DMSP
1315 * call below. Note that randomness is measured in bytes,
1316 * not bits, so this value corresponds to eight times the
1317 * security strength.
c16de9d8 1318 */
8817215d 1319 randomness = (double)seedlen;
75e2c877
RS
1320 }
1321
8817215d 1322 ret = rand_drbg_restart(drbg, buf, buflen, (size_t)(8 * randomness));
812b1537 1323 rand_drbg_unlock(drbg);
c16de9d8
DMSP
1324
1325 return ret;
75e2c877
RS
1326}
1327
c16de9d8 1328/* Implements the default OpenSSL RAND_seed() method */
75e2c877
RS
1329static int drbg_seed(const void *buf, int num)
1330{
1331 return drbg_add(buf, num, num);
1332}
1333
c16de9d8 1334/* Implements the default OpenSSL RAND_status() method */
75e2c877 1335static int drbg_status(void)
12fb8c3d 1336{
75e2c877 1337 int ret;
a93ba405 1338 RAND_DRBG *drbg = RAND_DRBG_get0_master();
0b14a5b7
KR
1339
1340 if (drbg == NULL)
1341 return 0;
75e2c877 1342
812b1537 1343 rand_drbg_lock(drbg);
0b14a5b7 1344 ret = drbg->state == DRBG_READY ? 1 : 0;
812b1537 1345 rand_drbg_unlock(drbg);
75e2c877 1346 return ret;
12fb8c3d
RS
1347}
1348
0b14a5b7 1349/*
a93ba405
DMSP
1350 * Get the master DRBG.
1351 * Returns pointer to the DRBG on success, NULL on failure.
1352 *
1353 */
da0d114c 1354RAND_DRBG *OPENSSL_CTX_get0_master_drbg(OPENSSL_CTX *ctx)
a93ba405 1355{
da0d114c
MC
1356 DRBG_GLOBAL *dgbl = drbg_get_global(ctx);
1357
1358 if (dgbl == NULL)
a93ba405
DMSP
1359 return NULL;
1360
da0d114c
MC
1361 return dgbl->master_drbg;
1362}
1363
1364RAND_DRBG *RAND_DRBG_get0_master(void)
1365{
1366 return OPENSSL_CTX_get0_master_drbg(NULL);
a93ba405
DMSP
1367}
1368
1369/*
1370 * Get the public DRBG.
0b14a5b7
KR
1371 * Returns pointer to the DRBG on success, NULL on failure.
1372 */
da0d114c 1373RAND_DRBG *OPENSSL_CTX_get0_public_drbg(OPENSSL_CTX *ctx)
0b14a5b7 1374{
da0d114c 1375 DRBG_GLOBAL *dgbl = drbg_get_global(ctx);
7caf122e
KR
1376 RAND_DRBG *drbg;
1377
da0d114c 1378 if (dgbl == NULL)
0b14a5b7
KR
1379 return NULL;
1380
da0d114c 1381 drbg = CRYPTO_THREAD_get_local(&dgbl->public_drbg);
7caf122e 1382 if (drbg == NULL) {
57ca171a 1383 ctx = openssl_ctx_get_concrete(ctx);
09a4cb9e
MC
1384 /*
1385 * If the private_drbg is also NULL then this is the first time we've
1386 * used this thread.
1387 */
1388 if (CRYPTO_THREAD_get_local(&dgbl->private_drbg) == NULL
1389 && !ossl_init_thread_start(NULL, ctx, drbg_delete_thread_state))
272c0df8 1390 return NULL;
da0d114c
MC
1391 drbg = drbg_setup(ctx, dgbl->master_drbg, RAND_DRBG_TYPE_PUBLIC);
1392 CRYPTO_THREAD_set_local(&dgbl->public_drbg, drbg);
7caf122e
KR
1393 }
1394 return drbg;
0b14a5b7
KR
1395}
1396
da0d114c
MC
1397RAND_DRBG *RAND_DRBG_get0_public(void)
1398{
1399 return OPENSSL_CTX_get0_public_drbg(NULL);
1400}
1401
0b14a5b7 1402/*
a93ba405 1403 * Get the private DRBG.
0b14a5b7
KR
1404 * Returns pointer to the DRBG on success, NULL on failure.
1405 */
da0d114c 1406RAND_DRBG *OPENSSL_CTX_get0_private_drbg(OPENSSL_CTX *ctx)
0b14a5b7 1407{
da0d114c 1408 DRBG_GLOBAL *dgbl = drbg_get_global(ctx);
7caf122e
KR
1409 RAND_DRBG *drbg;
1410
da0d114c 1411 if (dgbl == NULL)
0b14a5b7
KR
1412 return NULL;
1413
da0d114c 1414 drbg = CRYPTO_THREAD_get_local(&dgbl->private_drbg);
7caf122e 1415 if (drbg == NULL) {
57ca171a 1416 ctx = openssl_ctx_get_concrete(ctx);
09a4cb9e
MC
1417 /*
1418 * If the public_drbg is also NULL then this is the first time we've
1419 * used this thread.
1420 */
1421 if (CRYPTO_THREAD_get_local(&dgbl->public_drbg) == NULL
1422 && !ossl_init_thread_start(NULL, ctx, drbg_delete_thread_state))
272c0df8 1423 return NULL;
da0d114c
MC
1424 drbg = drbg_setup(ctx, dgbl->master_drbg, RAND_DRBG_TYPE_PRIVATE);
1425 CRYPTO_THREAD_set_local(&dgbl->private_drbg, drbg);
7caf122e
KR
1426 }
1427 return drbg;
0b14a5b7
KR
1428}
1429
da0d114c
MC
1430RAND_DRBG *RAND_DRBG_get0_private(void)
1431{
1432 return OPENSSL_CTX_get0_private_drbg(NULL);
1433}
1434
75e2c877
RS
1435RAND_METHOD rand_meth = {
1436 drbg_seed,
1437 drbg_bytes,
ddc6a5c8 1438 NULL,
75e2c877
RS
1439 drbg_add,
1440 drbg_bytes,
1441 drbg_status
1442};
1443
1444RAND_METHOD *RAND_OpenSSL(void)
12fb8c3d 1445{
a2f27fd7 1446#ifndef FIPS_MODE
75e2c877 1447 return &rand_meth;
a2f27fd7
MC
1448#else
1449 return NULL;
1450#endif
12fb8c3d 1451}