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