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