Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/providers/implementations/rands/drbg.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2011-2024 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include <string.h>
11
#include <openssl/crypto.h>
12
#include <openssl/err.h>
13
#include <openssl/rand.h>
14
#include <openssl/evp.h>
15
#include "crypto/rand.h"
16
#include <openssl/proverr.h>
17
#include "drbg_local.h"
18
#include "internal/thread_once.h"
19
#include "crypto/cryptlib.h"
20
#include "prov/seeding.h"
21
#include "crypto/rand_pool.h"
22
#include "prov/provider_ctx.h"
23
#include "prov/providercommon.h"
24
25
/*
26
 * Support framework for NIST SP 800-90A DRBG
27
 *
28
 * See manual page PROV_DRBG(7) for a general overview.
29
 *
30
 * The OpenSSL model is to have new and free functions, and that new
31
 * does all initialization.  That is not the NIST model, which has
32
 * instantiation and un-instantiate, and re-use within a new/free
33
 * lifecycle.  (No doubt this comes from the desire to support hardware
34
 * DRBG, where allocation of resources on something like an HSM is
35
 * a much bigger deal than just re-setting an allocated resource.)
36
 */
37
38
/* NIST SP 800-90A DRBG recommends the use of a personalization string. */
39
static const char ossl_pers_string[] = DRBG_DEFAULT_PERS_STRING;
40
41
static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch,
42
                                      int function);
43
44
static int rand_drbg_restart(PROV_DRBG *drbg);
45
46
int ossl_drbg_lock(void *vctx)
47
91.8k
{
48
91.8k
    PROV_DRBG *drbg = vctx;
49
50
91.8k
    if (drbg == NULL || drbg->lock == NULL)
51
45.8k
        return 1;
52
45.9k
    return CRYPTO_THREAD_write_lock(drbg->lock);
53
91.8k
}
54
55
void ossl_drbg_unlock(void *vctx)
56
91.8k
{
57
91.8k
    PROV_DRBG *drbg = vctx;
58
59
91.8k
    if (drbg != NULL && drbg->lock != NULL)
60
45.9k
        CRYPTO_THREAD_unlock(drbg->lock);
61
91.8k
}
62
63
static int ossl_drbg_lock_parent(PROV_DRBG *drbg)
64
81.7k
{
65
81.7k
    void *parent = drbg->parent;
66
67
81.7k
    if (parent != NULL
68
81.7k
            && drbg->parent_lock != NULL
69
81.7k
            && !drbg->parent_lock(parent)) {
70
0
        ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED);
71
0
        return 0;
72
0
    }
73
81.7k
    return 1;
74
81.7k
}
75
76
static void ossl_drbg_unlock_parent(PROV_DRBG *drbg)
77
81.7k
{
78
81.7k
    void *parent = drbg->parent;
79
80
81.7k
    if (parent != NULL && drbg->parent_unlock != NULL)
81
81.7k
        drbg->parent_unlock(parent);
82
81.7k
}
83
84
static int get_parent_strength(PROV_DRBG *drbg, unsigned int *str)
85
44
{
86
44
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
87
44
    void *parent = drbg->parent;
88
44
    int res;
89
90
44
    if (drbg->parent_get_ctx_params == NULL) {
91
0
        ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH);
92
0
        return 0;
93
0
    }
94
95
44
    *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, str);
96
44
    if (!ossl_drbg_lock_parent(drbg)) {
97
0
        ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOCK_PARENT);
98
0
        return 0;
99
0
    }
100
44
    res = drbg->parent_get_ctx_params(parent, params);
101
44
    ossl_drbg_unlock_parent(drbg);
102
44
    if (!res) {
103
0
        ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH);
104
0
        return 0;
105
0
    }
106
44
    return 1;
107
44
}
108
109
static unsigned int get_parent_reseed_count(PROV_DRBG *drbg)
110
81.6k
{
111
81.6k
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
112
81.6k
    void *parent = drbg->parent;
113
81.6k
    unsigned int r = 0;
114
115
81.6k
    *params = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_COUNTER, &r);
116
81.6k
    if (!ossl_drbg_lock_parent(drbg)) {
117
0
        ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_LOCK_PARENT);
118
0
        goto err;
119
0
    }
120
81.6k
    if (!drbg->parent_get_ctx_params(parent, params))
121
0
        r = 0;
122
81.6k
    ossl_drbg_unlock_parent(drbg);
123
81.6k
    return r;
124
125
0
 err:
126
0
    r = tsan_load(&drbg->reseed_counter) - 2;
127
0
    if (r == 0)
128
0
        r = UINT_MAX;
129
0
    return r;
130
81.6k
}
131
132
/*
133
 * Implements the get_entropy() callback
134
 *
135
 * If the DRBG has a parent, then the required amount of entropy input
136
 * is fetched using the parent's ossl_prov_drbg_generate().
137
 *
138
 * Otherwise, the entropy is polled from the system entropy sources
139
 * using ossl_pool_acquire_entropy().
140
 *
141
 * If a random pool has been added to the DRBG using RAND_add(), then
142
 * its entropy will be used up first.
143
 */
144
size_t ossl_drbg_get_seed(void *vdrbg, unsigned char **pout,
145
                          int entropy, size_t min_len,
146
                          size_t max_len, int prediction_resistance,
147
                          const unsigned char *adin, size_t adin_len)
148
20
{
149
20
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
150
20
    size_t bytes_needed;
151
20
    unsigned char *buffer;
152
153
    /* Figure out how many bytes we need */
154
20
    bytes_needed = entropy >= 0 ? (entropy + 7) / 8 : 0;
155
20
    if (bytes_needed < min_len)
156
4
        bytes_needed = min_len;
157
20
    if (bytes_needed > max_len)
158
0
        bytes_needed = max_len;
159
160
    /* Allocate storage */
161
20
    buffer = OPENSSL_secure_malloc(bytes_needed);
162
20
    if (buffer == NULL) {
163
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
164
0
        return 0;
165
0
    }
166
167
    /*
168
     * Get random data.  Include our DRBG address as
169
     * additional input, in order to provide a distinction between
170
     * different DRBG child instances.
171
     *
172
     * Note: using the sizeof() operator on a pointer triggers
173
     *       a warning in some static code analyzers, but it's
174
     *       intentional and correct here.
175
     */
176
20
    if (!ossl_prov_drbg_generate(drbg, buffer, bytes_needed,
177
20
                                 drbg->strength, prediction_resistance,
178
20
                                 (unsigned char *)&drbg, sizeof(drbg))) {
179
0
        OPENSSL_secure_clear_free(buffer, bytes_needed);
180
0
        ERR_raise(ERR_LIB_PROV, PROV_R_GENERATE_ERROR);
181
0
        return 0;
182
0
    }
183
20
    *pout = buffer;
184
20
    return bytes_needed;
185
20
}
186
187
/* Implements the cleanup_entropy() callback */
188
void ossl_drbg_clear_seed(ossl_unused void *vdrbg,
189
                          unsigned char *out, size_t outlen)
190
20
{
191
20
    OPENSSL_secure_clear_free(out, outlen);
192
20
}
193
194
static size_t get_entropy(PROV_DRBG *drbg, unsigned char **pout, int entropy,
195
                          size_t min_len, size_t max_len,
196
                          int prediction_resistance)
197
9
{
198
9
    size_t bytes;
199
9
    unsigned int p_str;
200
201
9
    if (drbg->parent == NULL)
202
#ifdef FIPS_MODULE
203
        return ossl_crngt_get_entropy(drbg, pout, entropy, min_len, max_len,
204
                                      prediction_resistance);
205
#else
206
        /*
207
         * In normal use (i.e. OpenSSL's own uses), this is never called.
208
         * Outside of the FIPS provider, OpenSSL sets its DRBGs up so that
209
         * they always have a parent.  This remains purely for legacy reasons.
210
         */
211
0
        return ossl_prov_get_entropy(drbg->provctx, pout, entropy, min_len,
212
0
                                     max_len);
213
9
#endif
214
215
9
    if (drbg->parent_get_seed == NULL) {
216
0
        ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_CANNOT_SUPPLY_ENTROPY_SEED);
217
0
        return 0;
218
0
    }
219
9
    if (!get_parent_strength(drbg, &p_str))
220
0
        return 0;
221
9
    if (drbg->strength > p_str) {
222
        /*
223
         * We currently don't support the algorithm from NIST SP 800-90C
224
         * 10.1.2 to use a weaker DRBG as source
225
         */
226
0
        ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK);
227
0
        return 0;
228
0
    }
229
230
    /*
231
     * Our lock is already held, but we need to lock our parent before
232
     * generating bits from it.  Note: taking the lock will be a no-op
233
     * if locking is not required (while drbg->parent->lock == NULL).
234
     */
235
9
    if (!ossl_drbg_lock_parent(drbg))
236
0
        return 0;
237
    /*
238
     * Get random data from parent.  Include our DRBG address as
239
     * additional input, in order to provide a distinction between
240
     * different DRBG child instances.
241
     *
242
     * Note: using the sizeof() operator on a pointer triggers
243
     *       a warning in some static code analyzers, but it's
244
     *       intentional and correct here.
245
     */
246
9
    bytes = drbg->parent_get_seed(drbg->parent, pout, drbg->strength,
247
9
                                  min_len, max_len, prediction_resistance,
248
9
                                  (unsigned char *)&drbg, sizeof(drbg));
249
9
    ossl_drbg_unlock_parent(drbg);
250
9
    return bytes;
251
9
}
252
253
static void cleanup_entropy(PROV_DRBG *drbg, unsigned char *out, size_t outlen)
254
235
{
255
235
    if (drbg->parent == NULL) {
256
#ifdef FIPS_MODULE
257
        ossl_crngt_cleanup_entropy(drbg, out, outlen);
258
#else
259
208
        ossl_prov_cleanup_entropy(drbg->provctx, out, outlen);
260
208
#endif
261
208
    } else if (drbg->parent_clear_seed != NULL) {
262
27
        if (!ossl_drbg_lock_parent(drbg))
263
0
            return;
264
27
        drbg->parent_clear_seed(drbg->parent, out, outlen);
265
27
        ossl_drbg_unlock_parent(drbg);
266
27
    }
267
235
}
268
269
#ifndef PROV_RAND_GET_RANDOM_NONCE
270
typedef struct prov_drbg_nonce_global_st {
271
    CRYPTO_RWLOCK *rand_nonce_lock;
272
    int rand_nonce_count;
273
} PROV_DRBG_NONCE_GLOBAL;
274
275
/*
276
 * drbg_ossl_ctx_new() calls drgb_setup() which calls rand_drbg_get_nonce()
277
 * which needs to get the rand_nonce_lock out of the OSSL_LIB_CTX...but since
278
 * drbg_ossl_ctx_new() hasn't finished running yet we need the rand_nonce_lock
279
 * to be in a different global data object. Otherwise we will go into an
280
 * infinite recursion loop.
281
 */
282
static void *prov_drbg_nonce_ossl_ctx_new(OSSL_LIB_CTX *libctx)
283
0
{
284
0
    PROV_DRBG_NONCE_GLOBAL *dngbl = OPENSSL_zalloc(sizeof(*dngbl));
285
286
0
    if (dngbl == NULL)
287
0
        return NULL;
288
289
0
    dngbl->rand_nonce_lock = CRYPTO_THREAD_lock_new();
290
0
    if (dngbl->rand_nonce_lock == NULL) {
291
0
        OPENSSL_free(dngbl);
292
0
        return NULL;
293
0
    }
294
295
0
    return dngbl;
296
0
}
297
298
static void prov_drbg_nonce_ossl_ctx_free(void *vdngbl)
299
0
{
300
0
    PROV_DRBG_NONCE_GLOBAL *dngbl = vdngbl;
301
302
0
    if (dngbl == NULL)
303
0
        return;
304
305
0
    CRYPTO_THREAD_lock_free(dngbl->rand_nonce_lock);
306
307
0
    OPENSSL_free(dngbl);
308
0
}
309
310
static const OSSL_LIB_CTX_METHOD drbg_nonce_ossl_ctx_method = {
311
    OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
312
    prov_drbg_nonce_ossl_ctx_new,
313
    prov_drbg_nonce_ossl_ctx_free,
314
};
315
316
/* Get a nonce from the operating system */
317
static size_t prov_drbg_get_nonce(PROV_DRBG *drbg, unsigned char **pout,
318
                                  size_t min_len, size_t max_len)
319
{
320
    size_t ret = 0, n;
321
    unsigned char *buf = NULL;
322
    OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(drbg->provctx);
323
    PROV_DRBG_NONCE_GLOBAL *dngbl
324
        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DRBG_NONCE_INDEX,
325
                                &drbg_nonce_ossl_ctx_method);
326
    struct {
327
        void *drbg;
328
        int count;
329
    } data;
330
331
    if (dngbl == NULL)
332
        return 0;
333
334
    if (drbg->parent != NULL && drbg->parent_nonce != NULL) {
335
        n = drbg->parent_nonce(drbg->parent, NULL, 0, drbg->min_noncelen,
336
                               drbg->max_noncelen);
337
        if (n > 0 && (buf = OPENSSL_malloc(n)) != NULL) {
338
            ret = drbg->parent_nonce(drbg->parent, buf, 0,
339
                                     drbg->min_noncelen, drbg->max_noncelen);
340
            if (ret == n) {
341
                *pout = buf;
342
                return ret;
343
            }
344
            OPENSSL_free(buf);
345
        }
346
    }
347
348
    /* Use the built in nonce source plus some of our specifics */
349
    memset(&data, 0, sizeof(data));
350
    data.drbg = drbg;
351
    CRYPTO_atomic_add(&dngbl->rand_nonce_count, 1, &data.count,
352
                      dngbl->rand_nonce_lock);
353
    return ossl_prov_get_nonce(drbg->provctx, pout, min_len, max_len,
354
                               &data, sizeof(data));
355
}
356
#endif /* PROV_RAND_GET_RANDOM_NONCE */
357
358
/*
359
 * Instantiate |drbg|, after it has been initialized.  Use |pers| and
360
 * |perslen| as prediction-resistance input.
361
 *
362
 * Requires that drbg->lock is already locked for write, if non-null.
363
 *
364
 * Returns 1 on success, 0 on failure.
365
 */
366
int ossl_prov_drbg_instantiate(PROV_DRBG *drbg, unsigned int strength,
367
                               int prediction_resistance,
368
                               const unsigned char *pers, size_t perslen)
369
117
{
370
117
    unsigned char *nonce = NULL, *entropy = NULL;
371
117
    size_t noncelen = 0, entropylen = 0;
372
117
    size_t min_entropy, min_entropylen, max_entropylen;
373
374
117
    if (strength > drbg->strength) {
375
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INSUFFICIENT_DRBG_STRENGTH);
376
0
        goto end;
377
0
    }
378
117
    min_entropy = drbg->strength;
379
117
    min_entropylen = drbg->min_entropylen;
380
117
    max_entropylen = drbg->max_entropylen;
381
382
117
    if (pers == NULL) {
383
117
        pers = (const unsigned char *)ossl_pers_string;
384
117
        perslen = sizeof(ossl_pers_string);
385
117
    }
386
117
    if (perslen > drbg->max_perslen) {
387
0
        ERR_raise(ERR_LIB_PROV, PROV_R_PERSONALISATION_STRING_TOO_LONG);
388
0
        goto end;
389
0
    }
390
391
117
    if (drbg->state != EVP_RAND_STATE_UNINITIALISED) {
392
0
        if (drbg->state == EVP_RAND_STATE_ERROR)
393
0
            ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE);
394
0
        else
395
0
            ERR_raise(ERR_LIB_PROV, PROV_R_ALREADY_INSTANTIATED);
396
0
        goto end;
397
0
    }
398
399
117
    drbg->state = EVP_RAND_STATE_ERROR;
400
401
117
    if (drbg->min_noncelen > 0) {
402
116
        if (drbg->parent_nonce != NULL) {
403
0
            noncelen = drbg->parent_nonce(drbg->parent, NULL, drbg->strength,
404
0
                                          drbg->min_noncelen,
405
0
                                          drbg->max_noncelen);
406
0
            if (noncelen == 0) {
407
0
                ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
408
0
                goto end;
409
0
            }
410
0
            nonce = OPENSSL_malloc(noncelen);
411
0
            if (nonce == NULL) {
412
0
                ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
413
0
                goto end;
414
0
            }
415
0
            if (noncelen != drbg->parent_nonce(drbg->parent, nonce,
416
0
                                               drbg->strength,
417
0
                                               drbg->min_noncelen,
418
0
                                               drbg->max_noncelen)) {
419
0
                ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
420
0
                goto end;
421
0
            }
422
0
#ifndef PROV_RAND_GET_RANDOM_NONCE
423
116
        } else if (drbg->parent != NULL) {
424
16
#endif
425
            /*
426
             * NIST SP800-90Ar1 section 9.1 says you can combine getting
427
             * the entropy and nonce in 1 call by increasing the entropy
428
             * with 50% and increasing the minimum length to accommodate
429
             * the length of the nonce. We do this in case a nonce is
430
             * required and there is no parental nonce capability.
431
             */
432
16
            min_entropy += drbg->strength / 2;
433
16
            min_entropylen += drbg->min_noncelen;
434
16
            max_entropylen += drbg->max_noncelen;
435
16
        }
436
100
#ifndef PROV_RAND_GET_RANDOM_NONCE
437
100
        else { /* parent == NULL */
438
100
            noncelen = prov_drbg_get_nonce(drbg, &nonce, drbg->min_noncelen, 
439
100
                                           drbg->max_noncelen);
440
100
            if (noncelen < drbg->min_noncelen
441
100
                    || noncelen > drbg->max_noncelen) {
442
0
                ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
443
0
                goto end;
444
0
            }
445
100
        }
446
116
#endif
447
116
    }
448
449
117
    drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter);
450
117
    if (drbg->reseed_next_counter) {
451
117
        drbg->reseed_next_counter++;
452
117
        if (!drbg->reseed_next_counter)
453
0
            drbg->reseed_next_counter = 1;
454
117
    }
455
456
117
    entropylen = get_entropy(drbg, &entropy, min_entropy,
457
117
                             min_entropylen, max_entropylen,
458
117
                             prediction_resistance);
459
117
    if (entropylen < min_entropylen
460
117
            || entropylen > max_entropylen) {
461
0
        ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_ENTROPY);
462
0
        goto end;
463
0
    }
464
465
117
    if (!drbg->instantiate(drbg, entropy, entropylen, nonce, noncelen,
466
117
                           pers, perslen)) {
467
4
        cleanup_entropy(drbg, entropy, entropylen);
468
4
        ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_INSTANTIATING_DRBG);
469
4
        goto end;
470
4
    }
471
113
    cleanup_entropy(drbg, entropy, entropylen);
472
473
113
    drbg->state = EVP_RAND_STATE_READY;
474
113
    drbg->generate_counter = 1;
475
113
    drbg->reseed_time = time(NULL);
476
113
    tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter);
477
478
117
 end:
479
117
    if (nonce != NULL)
480
100
        ossl_prov_cleanup_nonce(drbg->provctx, nonce, noncelen);
481
117
    if (drbg->state == EVP_RAND_STATE_READY)
482
113
        return 1;
483
4
    return 0;
484
117
}
485
486
/*
487
 * Uninstantiate |drbg|. Must be instantiated before it can be used.
488
 *
489
 * Requires that drbg->lock is already locked for write, if non-null.
490
 *
491
 * Returns 1 on success, 0 on failure.
492
 */
493
int ossl_prov_drbg_uninstantiate(PROV_DRBG *drbg)
494
0
{
495
0
    drbg->state = EVP_RAND_STATE_UNINITIALISED;
496
0
    return 1;
497
0
}
498
499
/*
500
 * Reseed |drbg|, mixing in the specified data
501
 *
502
 * Requires that drbg->lock is already locked for write, if non-null.
503
 *
504
 * Returns 1 on success, 0 on failure.
505
 */
506
int ossl_prov_drbg_reseed(PROV_DRBG *drbg, int prediction_resistance,
507
                          const unsigned char *ent, size_t ent_len,
508
                          const unsigned char *adin, size_t adinlen)
509
2
{
510
2
    unsigned char *entropy = NULL;
511
2
    size_t entropylen = 0;
512
513
2
    if (!ossl_prov_is_running())
514
0
        return 0;
515
516
2
    if (drbg->state != EVP_RAND_STATE_READY) {
517
        /* try to recover from previous errors */
518
0
        rand_drbg_restart(drbg);
519
520
0
        if (drbg->state == EVP_RAND_STATE_ERROR) {
521
0
            ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE);
522
0
            return 0;
523
0
        }
524
0
        if (drbg->state == EVP_RAND_STATE_UNINITIALISED) {
525
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_INSTANTIATED);
526
0
            return 0;
527
0
        }
528
0
    }
529
530
2
    if (ent != NULL) {
531
0
        if (ent_len < drbg->min_entropylen) {
532
0
            ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_OUT_OF_RANGE);
533
0
            drbg->state = EVP_RAND_STATE_ERROR;
534
0
            return 0;
535
0
        }
536
0
        if (ent_len > drbg->max_entropylen) {
537
0
            ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_INPUT_TOO_LONG);
538
0
            drbg->state = EVP_RAND_STATE_ERROR;
539
0
            return 0;
540
0
        }
541
0
    }
542
543
2
    if (adin == NULL) {
544
2
        adinlen = 0;
545
2
    } else if (adinlen > drbg->max_adinlen) {
546
0
        ERR_raise(ERR_LIB_PROV, PROV_R_ADDITIONAL_INPUT_TOO_LONG);
547
0
        return 0;
548
0
    }
549
550
2
    drbg->state = EVP_RAND_STATE_ERROR;
551
552
2
    drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter);
553
2
    if (drbg->reseed_next_counter) {
554
2
        drbg->reseed_next_counter++;
555
2
        if (!drbg->reseed_next_counter)
556
0
            drbg->reseed_next_counter = 1;
557
2
    }
558
559
2
    if (ent != NULL) {
560
#ifdef FIPS_MODULE
561
        /*
562
         * NIST SP-800-90A mandates that entropy *shall not* be provided
563
         * by the consuming application. Instead the data is added as additional
564
         * input.
565
         *
566
         * (NIST SP-800-90Ar1, Sections 9.1 and 9.2)
567
         */
568
        if (!drbg->reseed(drbg, NULL, 0, ent, ent_len)) {
569
            ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED);
570
            return 0;
571
        }
572
#else
573
0
        if (!drbg->reseed(drbg, ent, ent_len, adin, adinlen)) {
574
0
            ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED);
575
0
            return 0;
576
0
        }
577
        /* There isn't much point adding the same additional input twice */
578
0
        adin = NULL;
579
0
        adinlen = 0;
580
0
#endif
581
0
    }
582
583
    /* Reseed using our sources in addition */
584
2
    entropylen = get_entropy(drbg, &entropy, drbg->strength,
585
2
                             drbg->min_entropylen, drbg->max_entropylen,
586
2
                             prediction_resistance);
587
2
    if (entropylen < drbg->min_entropylen
588
2
            || entropylen > drbg->max_entropylen) {
589
0
        ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_ENTROPY);
590
0
        goto end;
591
0
    }
592
593
2
    if (!drbg->reseed(drbg, entropy, entropylen, adin, adinlen))
594
0
        goto end;
595
596
2
    drbg->state = EVP_RAND_STATE_READY;
597
2
    drbg->generate_counter = 1;
598
2
    drbg->reseed_time = time(NULL);
599
2
    tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter);
600
2
    if (drbg->parent != NULL)
601
2
        drbg->parent_reseed_counter = get_parent_reseed_count(drbg);
602
603
2
 end:
604
2
    cleanup_entropy(drbg, entropy, entropylen);
605
2
    if (drbg->state == EVP_RAND_STATE_READY)
606
2
        return 1;
607
0
    return 0;
608
2
}
609
610
/*
611
 * Generate |outlen| bytes into the buffer at |out|.  Reseed if we need
612
 * to or if |prediction_resistance| is set.  Additional input can be
613
 * sent in |adin| and |adinlen|.
614
 *
615
 * Requires that drbg->lock is already locked for write, if non-null.
616
 *
617
 * Returns 1 on success, 0 on failure.
618
 *
619
 */
620
int ossl_prov_drbg_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen,
621
                            unsigned int strength, int prediction_resistance,
622
                            const unsigned char *adin, size_t adinlen)
623
45.8k
{
624
45.8k
    int fork_id;
625
45.8k
    int reseed_required = 0;
626
627
45.8k
    if (!ossl_prov_is_running())
628
0
        return 0;
629
630
45.8k
    if (drbg->state != EVP_RAND_STATE_READY) {
631
        /* try to recover from previous errors */
632
0
        rand_drbg_restart(drbg);
633
634
0
        if (drbg->state == EVP_RAND_STATE_ERROR) {
635
0
            ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE);
636
0
            return 0;
637
0
        }
638
0
        if (drbg->state == EVP_RAND_STATE_UNINITIALISED) {
639
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_INSTANTIATED);
640
0
            return 0;
641
0
        }
642
0
    }
643
45.8k
    if (strength > drbg->strength) {
644
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INSUFFICIENT_DRBG_STRENGTH);
645
0
        return 0;
646
0
    }
647
648
45.8k
    if (outlen > drbg->max_request) {
649
0
        ERR_raise(ERR_LIB_PROV, PROV_R_REQUEST_TOO_LARGE_FOR_DRBG);
650
0
        return 0;
651
0
    }
652
45.8k
    if (adinlen > drbg->max_adinlen) {
653
0
        ERR_raise(ERR_LIB_PROV, PROV_R_ADDITIONAL_INPUT_TOO_LONG);
654
0
        return 0;
655
0
    }
656
657
45.8k
    fork_id = openssl_get_fork_id();
658
659
45.8k
    if (drbg->fork_id != fork_id) {
660
0
        drbg->fork_id = fork_id;
661
0
        reseed_required = 1;
662
0
    }
663
664
45.8k
    if (drbg->reseed_interval > 0) {
665
45.8k
        if (drbg->generate_counter >= drbg->reseed_interval)
666
0
            reseed_required = 1;
667
45.8k
    }
668
45.8k
    if (drbg->reseed_time_interval > 0) {
669
45.8k
        time_t now = time(NULL);
670
45.8k
        if (now < drbg->reseed_time
671
45.8k
            || now - drbg->reseed_time >= drbg->reseed_time_interval)
672
0
            reseed_required = 1;
673
45.8k
    }
674
45.8k
    if (drbg->parent != NULL
675
45.8k
            && get_parent_reseed_count(drbg) != drbg->parent_reseed_counter)
676
2
        reseed_required = 1;
677
678
45.8k
    if (reseed_required || prediction_resistance) {
679
2
        if (!ossl_prov_drbg_reseed(drbg, prediction_resistance, NULL, 0,
680
2
                                   adin, adinlen)) {
681
0
            ERR_raise(ERR_LIB_PROV, PROV_R_RESEED_ERROR);
682
0
            return 0;
683
0
        }
684
2
        adin = NULL;
685
2
        adinlen = 0;
686
2
    }
687
688
45.8k
    if (!drbg->generate(drbg, out, outlen, adin, adinlen)) {
689
0
        drbg->state = EVP_RAND_STATE_ERROR;
690
0
        ERR_raise(ERR_LIB_PROV, PROV_R_GENERATE_ERROR);
691
0
        return 0;
692
0
    }
693
694
45.8k
    drbg->generate_counter++;
695
696
45.8k
    return 1;
697
45.8k
}
698
699
/*
700
 * Restart |drbg|, using the specified entropy or additional input
701
 *
702
 * Tries its best to get the drbg instantiated by all means,
703
 * regardless of its current state.
704
 *
705
 * Optionally, a |buffer| of |len| random bytes can be passed,
706
 * which is assumed to contain at least |entropy| bits of entropy.
707
 *
708
 * If |entropy| > 0, the buffer content is used as entropy input.
709
 *
710
 * If |entropy| == 0, the buffer content is used as additional input
711
 *
712
 * Returns 1 on success, 0 on failure.
713
 *
714
 * This function is used internally only.
715
 */
716
static int rand_drbg_restart(PROV_DRBG *drbg)
717
100
{
718
    /* repair error state */
719
100
    if (drbg->state == EVP_RAND_STATE_ERROR)
720
0
        drbg->uninstantiate(drbg);
721
722
    /* repair uninitialized state */
723
100
    if (drbg->state == EVP_RAND_STATE_UNINITIALISED)
724
        /* reinstantiate drbg */
725
100
        ossl_prov_drbg_instantiate(drbg, drbg->strength, 0, NULL, 0);
726
727
100
    return drbg->state == EVP_RAND_STATE_READY;
728
100
}
729
730
/* Provider support from here down */
731
static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch,
732
                                      int function)
733
1.19k
{
734
1.19k
    if (dispatch != NULL)
735
1.38k
        while (dispatch->function_id != 0) {
736
1.37k
            if (dispatch->function_id == function)
737
102
                return dispatch;
738
1.27k
            dispatch++;
739
1.27k
        }
740
1.08k
    return NULL;
741
1.19k
}
742
743
int ossl_drbg_enable_locking(void *vctx)
744
7
{
745
7
    PROV_DRBG *drbg = vctx;
746
747
7
    if (drbg != NULL && drbg->lock == NULL) {
748
7
        if (drbg->parent_enable_locking != NULL)
749
7
            if (!drbg->parent_enable_locking(drbg->parent)) {
750
0
                ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED);
751
0
                return 0;
752
0
            }
753
7
        drbg->lock = CRYPTO_THREAD_lock_new();
754
7
        if (drbg->lock == NULL) {
755
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_CREATE_LOCK);
756
0
            return 0;
757
0
        }
758
7
    }
759
7
    return 1;
760
7
}
761
762
/*
763
 * Allocate memory and initialize a new DRBG. The DRBG is allocated on
764
 * the secure heap if |secure| is nonzero and the secure heap is enabled.
765
 * The |parent|, if not NULL, will be used as random source for reseeding.
766
 * This also requires the parent's provider context and the parent's lock.
767
 *
768
 * Returns a pointer to the new DRBG instance on success, NULL on failure.
769
 */
770
PROV_DRBG *ossl_rand_drbg_new
771
    (void *provctx, void *parent, const OSSL_DISPATCH *p_dispatch,
772
     int (*dnew)(PROV_DRBG *ctx),
773
     void (*dfree)(void *vctx),
774
     int (*instantiate)(PROV_DRBG *drbg,
775
                        const unsigned char *entropy, size_t entropylen,
776
                        const unsigned char *nonce, size_t noncelen,
777
                        const unsigned char *pers, size_t perslen),
778
     int (*uninstantiate)(PROV_DRBG *ctx),
779
     int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,
780
                   const unsigned char *adin, size_t adin_len),
781
     int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,
782
                     const unsigned char *adin, size_t adin_len))
783
170
{
784
170
    PROV_DRBG *drbg;
785
170
    unsigned int p_str;
786
170
    const OSSL_DISPATCH *pfunc;
787
788
170
    if (!ossl_prov_is_running())
789
0
        return NULL;
790
791
170
    drbg = OPENSSL_zalloc(sizeof(*drbg));
792
170
    if (drbg == NULL) {
793
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
794
0
        return NULL;
795
0
    }
796
797
170
    drbg->provctx = provctx;
798
170
    drbg->instantiate = instantiate;
799
170
    drbg->uninstantiate = uninstantiate;
800
170
    drbg->reseed = reseed;
801
170
    drbg->generate = generate;
802
170
    drbg->fork_id = openssl_get_fork_id();
803
804
    /* Extract parent's functions */
805
170
    drbg->parent = parent;
806
170
    if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_ENABLE_LOCKING)) != NULL)
807
17
        drbg->parent_enable_locking = OSSL_FUNC_rand_enable_locking(pfunc);
808
170
    if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_LOCK)) != NULL)
809
17
        drbg->parent_lock = OSSL_FUNC_rand_lock(pfunc);
810
170
    if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_UNLOCK)) != NULL)
811
17
        drbg->parent_unlock = OSSL_FUNC_rand_unlock(pfunc);
812
170
    if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_CTX_PARAMS)) != NULL)
813
17
        drbg->parent_get_ctx_params = OSSL_FUNC_rand_get_ctx_params(pfunc);
814
170
    if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_NONCE)) != NULL)
815
0
        drbg->parent_nonce = OSSL_FUNC_rand_nonce(pfunc);
816
170
    if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_SEED)) != NULL)
817
17
        drbg->parent_get_seed = OSSL_FUNC_rand_get_seed(pfunc);
818
170
    if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_CLEAR_SEED)) != NULL)
819
17
        drbg->parent_clear_seed = OSSL_FUNC_rand_clear_seed(pfunc);
820
821
    /* Set some default maximums up */
822
170
    drbg->max_entropylen = DRBG_MAX_LENGTH;
823
170
    drbg->max_noncelen = DRBG_MAX_LENGTH;
824
170
    drbg->max_perslen = DRBG_MAX_LENGTH;
825
170
    drbg->max_adinlen = DRBG_MAX_LENGTH;
826
170
    drbg->generate_counter = 1;
827
170
    drbg->reseed_counter = 1;
828
170
    drbg->reseed_interval = RESEED_INTERVAL;
829
170
    drbg->reseed_time_interval = TIME_INTERVAL;
830
831
170
    if (!dnew(drbg))
832
0
        goto err;
833
834
170
    if (parent != NULL) {
835
17
        if (!get_parent_strength(drbg, &p_str))
836
0
            goto err;
837
17
        if (drbg->strength > p_str) {
838
            /*
839
             * We currently don't support the algorithm from NIST SP 800-90C
840
             * 10.1.2 to use a weaker DRBG as source
841
             */
842
0
            ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK);
843
0
            goto err;
844
0
        }
845
17
    }
846
#ifdef TSAN_REQUIRES_LOCKING
847
    if (!ossl_drbg_enable_locking(drbg))
848
        goto err;
849
#endif
850
170
    return drbg;
851
852
0
 err:
853
0
    dfree(drbg);
854
0
    return NULL;
855
170
}
856
857
void ossl_rand_drbg_free(PROV_DRBG *drbg)
858
170
{
859
170
    if (drbg == NULL)
860
0
        return;
861
862
170
    CRYPTO_THREAD_lock_free(drbg->lock);
863
170
    OPENSSL_free(drbg);
864
170
}
865
866
int ossl_drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[])
867
91.7k
{
868
91.7k
    OSSL_PARAM *p;
869
870
91.7k
    p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
871
91.7k
    if (p != NULL && !OSSL_PARAM_set_int(p, drbg->state))
872
0
        return 0;
873
874
91.7k
    p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
875
91.7k
    if (p != NULL && !OSSL_PARAM_set_int(p, drbg->strength))
876
0
        return 0;
877
878
91.7k
    p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
879
91.7k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_request))
880
0
        return 0;
881
882
91.7k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_ENTROPYLEN);
883
91.7k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_entropylen))
884
0
        return 0;
885
886
91.7k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ENTROPYLEN);
887
91.7k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_entropylen))
888
0
        return 0;
889
890
91.7k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_NONCELEN);
891
91.7k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_noncelen))
892
0
        return 0;
893
894
91.7k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_NONCELEN);
895
91.7k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_noncelen))
896
0
        return 0;
897
898
91.7k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_PERSLEN);
899
91.7k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_perslen))
900
0
        return 0;
901
902
91.7k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ADINLEN);
903
91.7k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_adinlen))
904
0
        return 0;
905
906
91.7k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
907
91.7k
    if (p != NULL && !OSSL_PARAM_set_uint(p, drbg->reseed_interval))
908
0
        return 0;
909
910
91.7k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME);
911
91.7k
    if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time))
912
0
        return 0;
913
914
91.7k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
915
91.7k
    if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time_interval))
916
0
        return 0;
917
918
91.7k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_COUNTER);
919
91.7k
    if (p != NULL
920
91.7k
            && !OSSL_PARAM_set_uint(p, tsan_load(&drbg->reseed_counter)))
921
0
        return 0;
922
91.7k
    return 1;
923
91.7k
}
924
925
int ossl_drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[])
926
6
{
927
6
    const OSSL_PARAM *p;
928
929
6
    if (params == NULL)
930
0
        return 1;
931
932
6
    p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
933
6
    if (p != NULL && !OSSL_PARAM_get_uint(p, &drbg->reseed_interval))
934
0
        return 0;
935
936
6
    p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
937
6
    if (p != NULL && !OSSL_PARAM_get_time_t(p, &drbg->reseed_time_interval))
938
0
        return 0;
939
6
    return 1;
940
6
}