Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/providers/implementations/rands/drbg.c
Line
Count
Source
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
29.8k
{
48
29.8k
    PROV_DRBG *drbg = vctx;
49
50
29.8k
    if (drbg == NULL || drbg->lock == NULL)
51
14.9k
        return 1;
52
14.9k
    return CRYPTO_THREAD_write_lock(drbg->lock);
53
29.8k
}
54
55
void ossl_drbg_unlock(void *vctx)
56
29.8k
{
57
29.8k
    PROV_DRBG *drbg = vctx;
58
59
29.8k
    if (drbg != NULL && drbg->lock != NULL)
60
14.9k
        CRYPTO_THREAD_unlock(drbg->lock);
61
29.8k
}
62
63
static int ossl_drbg_lock_parent(PROV_DRBG *drbg)
64
115k
{
65
115k
    void *parent = drbg->parent;
66
67
115k
    if (parent != NULL
68
115k
        && drbg->parent_lock != NULL
69
115k
        && !drbg->parent_lock(parent)) {
70
0
        ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED);
71
0
        return 0;
72
0
    }
73
115k
    return 1;
74
115k
}
75
76
static void ossl_drbg_unlock_parent(PROV_DRBG *drbg)
77
115k
{
78
115k
    void *parent = drbg->parent;
79
80
115k
    if (parent != NULL && drbg->parent_unlock != NULL)
81
115k
        drbg->parent_unlock(parent);
82
115k
}
83
84
static int get_parent_strength(PROV_DRBG *drbg, unsigned int *str)
85
112
{
86
112
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
87
112
    void *parent = drbg->parent;
88
112
    int res;
89
90
112
    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
112
    *params = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, str);
96
112
    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
112
    res = drbg->parent_get_ctx_params(parent, params);
101
112
    ossl_drbg_unlock_parent(drbg);
102
112
    if (!res) {
103
0
        ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PARENT_STRENGTH);
104
0
        return 0;
105
0
    }
106
112
    return 1;
107
112
}
108
109
static unsigned int get_parent_reseed_count(PROV_DRBG *drbg)
110
114k
{
111
114k
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
112
114k
    void *parent = drbg->parent;
113
114k
    unsigned int r = 0;
114
115
114k
    *params = OSSL_PARAM_construct_uint(OSSL_DRBG_PARAM_RESEED_COUNTER, &r);
116
114k
    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
114k
    if (!drbg->parent_get_ctx_params(parent, params))
121
0
        r = 0;
122
114k
    ossl_drbg_unlock_parent(drbg);
123
114k
    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
114k
}
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
52
{
149
52
    PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
150
52
    size_t bytes_needed;
151
52
    unsigned char *buffer;
152
153
    /* Figure out how many bytes we need */
154
52
    bytes_needed = entropy >= 0 ? (entropy + 7) / 8 : 0;
155
52
    if (bytes_needed < min_len)
156
5
        bytes_needed = min_len;
157
52
    if (bytes_needed > max_len)
158
0
        bytes_needed = max_len;
159
160
    /* Allocate storage */
161
52
    buffer = OPENSSL_secure_malloc(bytes_needed);
162
52
    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
52
    if (!ossl_prov_drbg_generate(drbg, buffer, bytes_needed,
177
52
            drbg->strength, prediction_resistance,
178
52
            (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
52
    *pout = buffer;
184
52
    return bytes_needed;
185
52
}
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
52
{
191
52
    OPENSSL_secure_clear_free(out, outlen);
192
52
}
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
252
{
198
252
    size_t bytes;
199
252
    unsigned int p_str;
200
201
252
    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
240
        return ossl_prov_get_entropy(drbg->provctx, pout, entropy, min_len,
212
240
            max_len);
213
12
#endif
214
215
12
    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
12
    if (!get_parent_strength(drbg, &p_str))
220
0
        return 0;
221
12
    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
12
    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
12
    bytes = drbg->parent_get_seed(drbg->parent, pout, drbg->strength,
247
12
        min_len, max_len, prediction_resistance,
248
12
        (unsigned char *)&drbg, sizeof(drbg));
249
12
    ossl_drbg_unlock_parent(drbg);
250
12
    return bytes;
251
12
}
252
253
static void cleanup_entropy(PROV_DRBG *drbg, unsigned char *out, size_t outlen)
254
1.06k
{
255
1.06k
    if (drbg->parent == NULL) {
256
#ifdef FIPS_MODULE
257
        ossl_crngt_cleanup_entropy(drbg, out, outlen);
258
#else
259
996
        ossl_prov_cleanup_entropy(drbg->provctx, out, outlen);
260
996
#endif
261
996
    } else if (drbg->parent_clear_seed != NULL) {
262
69
        if (!ossl_drbg_lock_parent(drbg))
263
0
            return;
264
69
        drbg->parent_clear_seed(drbg->parent, out, outlen);
265
69
        ossl_drbg_unlock_parent(drbg);
266
69
    }
267
1.06k
}
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
521
{
370
521
    unsigned char *nonce = NULL, *entropy = NULL;
371
521
    size_t noncelen = 0, entropylen = 0;
372
521
    size_t min_entropy, min_entropylen, max_entropylen;
373
374
521
    if (strength > drbg->strength) {
375
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INSUFFICIENT_DRBG_STRENGTH);
376
0
        goto end;
377
0
    }
378
521
    min_entropy = drbg->strength;
379
521
    min_entropylen = drbg->min_entropylen;
380
521
    max_entropylen = drbg->max_entropylen;
381
382
521
    if (pers == NULL) {
383
521
        pers = (const unsigned char *)ossl_pers_string;
384
521
        perslen = sizeof(ossl_pers_string);
385
521
    }
386
521
    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
521
    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
521
    drbg->state = EVP_RAND_STATE_ERROR;
400
401
521
    if (drbg->min_noncelen > 0) {
402
519
        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, drbg->strength, drbg->min_noncelen, drbg->max_noncelen)) {
416
0
                ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
417
0
                goto end;
418
0
            }
419
0
#ifndef PROV_RAND_GET_RANDOM_NONCE
420
519
        } else if (drbg->parent != NULL) {
421
42
#endif
422
            /*
423
             * NIST SP800-90Ar1 section 9.1 says you can combine getting
424
             * the entropy and nonce in 1 call by increasing the entropy
425
             * with 50% and increasing the minimum length to accommodate
426
             * the length of the nonce. We do this in case a nonce is
427
             * required and there is no parental nonce capability.
428
             */
429
42
            min_entropy += drbg->strength / 2;
430
42
            min_entropylen += drbg->min_noncelen;
431
42
            max_entropylen += drbg->max_noncelen;
432
42
        }
433
477
#ifndef PROV_RAND_GET_RANDOM_NONCE
434
477
        else { /* parent == NULL */
435
477
            noncelen = prov_drbg_get_nonce(drbg, &nonce, drbg->min_noncelen,
436
477
                drbg->max_noncelen);
437
477
            if (noncelen < drbg->min_noncelen
438
477
                || noncelen > drbg->max_noncelen) {
439
0
                ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_NONCE);
440
0
                goto end;
441
0
            }
442
477
        }
443
519
#endif
444
519
    }
445
446
521
    drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter);
447
521
    if (drbg->reseed_next_counter) {
448
521
        drbg->reseed_next_counter++;
449
521
        if (!drbg->reseed_next_counter)
450
0
            drbg->reseed_next_counter = 1;
451
521
    }
452
453
521
    entropylen = get_entropy(drbg, &entropy, min_entropy,
454
521
        min_entropylen, max_entropylen,
455
521
        prediction_resistance);
456
521
    if (entropylen < min_entropylen
457
521
        || entropylen > max_entropylen) {
458
0
        ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_ENTROPY);
459
0
        goto end;
460
0
    }
461
462
521
    if (!drbg->instantiate(drbg, entropy, entropylen, nonce, noncelen,
463
521
            pers, perslen)) {
464
22
        cleanup_entropy(drbg, entropy, entropylen);
465
22
        ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_INSTANTIATING_DRBG);
466
22
        goto end;
467
22
    }
468
499
    cleanup_entropy(drbg, entropy, entropylen);
469
470
499
    drbg->state = EVP_RAND_STATE_READY;
471
499
    drbg->generate_counter = 1;
472
499
    drbg->reseed_time = time(NULL);
473
499
    tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter);
474
475
521
end:
476
521
    if (nonce != NULL)
477
477
        ossl_prov_cleanup_nonce(drbg->provctx, nonce, noncelen);
478
521
    if (drbg->state == EVP_RAND_STATE_READY)
479
499
        return 1;
480
22
    return 0;
481
521
}
482
483
/*
484
 * Uninstantiate |drbg|. Must be instantiated before it can be used.
485
 *
486
 * Requires that drbg->lock is already locked for write, if non-null.
487
 *
488
 * Returns 1 on success, 0 on failure.
489
 */
490
int ossl_prov_drbg_uninstantiate(PROV_DRBG *drbg)
491
0
{
492
0
    drbg->state = EVP_RAND_STATE_UNINITIALISED;
493
0
    return 1;
494
0
}
495
496
/*
497
 * Reseed |drbg|, mixing in the specified data
498
 *
499
 * Requires that drbg->lock is already locked for write, if non-null.
500
 *
501
 * Returns 1 on success, 0 on failure.
502
 */
503
int ossl_prov_drbg_reseed(PROV_DRBG *drbg, int prediction_resistance,
504
    const unsigned char *ent, size_t ent_len,
505
    const unsigned char *adin, size_t adinlen)
506
1
{
507
1
    unsigned char *entropy = NULL;
508
1
    size_t entropylen = 0;
509
510
1
    if (!ossl_prov_is_running())
511
0
        return 0;
512
513
1
    if (drbg->state != EVP_RAND_STATE_READY) {
514
        /* try to recover from previous errors */
515
0
        rand_drbg_restart(drbg);
516
517
0
        if (drbg->state == EVP_RAND_STATE_ERROR) {
518
0
            ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE);
519
0
            return 0;
520
0
        }
521
0
        if (drbg->state == EVP_RAND_STATE_UNINITIALISED) {
522
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_INSTANTIATED);
523
0
            return 0;
524
0
        }
525
0
    }
526
527
1
    if (ent != NULL) {
528
0
        if (ent_len < drbg->min_entropylen) {
529
0
            ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_OUT_OF_RANGE);
530
0
            drbg->state = EVP_RAND_STATE_ERROR;
531
0
            return 0;
532
0
        }
533
0
        if (ent_len > drbg->max_entropylen) {
534
0
            ERR_raise(ERR_LIB_RAND, RAND_R_ENTROPY_INPUT_TOO_LONG);
535
0
            drbg->state = EVP_RAND_STATE_ERROR;
536
0
            return 0;
537
0
        }
538
0
    }
539
540
1
    if (adin == NULL) {
541
1
        adinlen = 0;
542
1
    } else if (adinlen > drbg->max_adinlen) {
543
0
        ERR_raise(ERR_LIB_PROV, PROV_R_ADDITIONAL_INPUT_TOO_LONG);
544
0
        return 0;
545
0
    }
546
547
1
    drbg->state = EVP_RAND_STATE_ERROR;
548
549
1
    drbg->reseed_next_counter = tsan_load(&drbg->reseed_counter);
550
1
    if (drbg->reseed_next_counter) {
551
1
        drbg->reseed_next_counter++;
552
1
        if (!drbg->reseed_next_counter)
553
0
            drbg->reseed_next_counter = 1;
554
1
    }
555
556
1
    if (ent != NULL) {
557
#ifdef FIPS_MODULE
558
        /*
559
         * NIST SP-800-90A mandates that entropy *shall not* be provided
560
         * by the consuming application. Instead the data is added as additional
561
         * input.
562
         *
563
         * (NIST SP-800-90Ar1, Sections 9.1 and 9.2)
564
         */
565
        if (!drbg->reseed(drbg, NULL, 0, ent, ent_len)) {
566
            ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED);
567
            return 0;
568
        }
569
#else
570
0
        if (!drbg->reseed(drbg, ent, ent_len, adin, adinlen)) {
571
0
            ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_RESEED);
572
0
            return 0;
573
0
        }
574
        /* There isn't much point adding the same additional input twice */
575
0
        adin = NULL;
576
0
        adinlen = 0;
577
0
#endif
578
0
    }
579
580
    /* Reseed using our sources in addition */
581
1
    entropylen = get_entropy(drbg, &entropy, drbg->strength,
582
1
        drbg->min_entropylen, drbg->max_entropylen,
583
1
        prediction_resistance);
584
1
    if (entropylen < drbg->min_entropylen
585
1
        || entropylen > drbg->max_entropylen) {
586
0
        ERR_raise(ERR_LIB_PROV, PROV_R_ERROR_RETRIEVING_ENTROPY);
587
0
        goto end;
588
0
    }
589
590
1
    if (!drbg->reseed(drbg, entropy, entropylen, adin, adinlen))
591
0
        goto end;
592
593
1
    drbg->state = EVP_RAND_STATE_READY;
594
1
    drbg->generate_counter = 1;
595
1
    drbg->reseed_time = time(NULL);
596
1
    tsan_store(&drbg->reseed_counter, drbg->reseed_next_counter);
597
1
    if (drbg->parent != NULL)
598
1
        drbg->parent_reseed_counter = get_parent_reseed_count(drbg);
599
600
1
end:
601
1
    cleanup_entropy(drbg, entropy, entropylen);
602
1
    if (drbg->state == EVP_RAND_STATE_READY)
603
1
        return 1;
604
0
    return 0;
605
1
}
606
607
/*
608
 * Generate |outlen| bytes into the buffer at |out|.  Reseed if we need
609
 * to or if |prediction_resistance| is set.  Additional input can be
610
 * sent in |adin| and |adinlen|.
611
 *
612
 * Requires that drbg->lock is already locked for write, if non-null.
613
 *
614
 * Returns 1 on success, 0 on failure.
615
 *
616
 */
617
int ossl_prov_drbg_generate(PROV_DRBG *drbg, unsigned char *out, size_t outlen,
618
    unsigned int strength, int prediction_resistance,
619
    const unsigned char *adin, size_t adinlen)
620
14.9k
{
621
14.9k
    int fork_id;
622
14.9k
    int reseed_required = 0;
623
624
14.9k
    if (!ossl_prov_is_running())
625
0
        return 0;
626
627
14.9k
    if (drbg->state != EVP_RAND_STATE_READY) {
628
        /* try to recover from previous errors */
629
0
        rand_drbg_restart(drbg);
630
631
0
        if (drbg->state == EVP_RAND_STATE_ERROR) {
632
0
            ERR_raise(ERR_LIB_PROV, PROV_R_IN_ERROR_STATE);
633
0
            return 0;
634
0
        }
635
0
        if (drbg->state == EVP_RAND_STATE_UNINITIALISED) {
636
0
            ERR_raise(ERR_LIB_PROV, PROV_R_NOT_INSTANTIATED);
637
0
            return 0;
638
0
        }
639
0
    }
640
14.9k
    if (strength > drbg->strength) {
641
0
        ERR_raise(ERR_LIB_PROV, PROV_R_INSUFFICIENT_DRBG_STRENGTH);
642
0
        return 0;
643
0
    }
644
645
14.9k
    if (outlen > drbg->max_request) {
646
0
        ERR_raise(ERR_LIB_PROV, PROV_R_REQUEST_TOO_LARGE_FOR_DRBG);
647
0
        return 0;
648
0
    }
649
14.9k
    if (adinlen > drbg->max_adinlen) {
650
0
        ERR_raise(ERR_LIB_PROV, PROV_R_ADDITIONAL_INPUT_TOO_LONG);
651
0
        return 0;
652
0
    }
653
654
14.9k
    fork_id = openssl_get_fork_id();
655
656
14.9k
    if (drbg->fork_id != fork_id) {
657
0
        drbg->fork_id = fork_id;
658
0
        reseed_required = 1;
659
0
    }
660
661
14.9k
    if (drbg->reseed_interval > 0) {
662
14.9k
        if (drbg->generate_counter >= drbg->reseed_interval)
663
0
            reseed_required = 1;
664
14.9k
    }
665
14.9k
    if (drbg->reseed_time_interval > 0) {
666
14.9k
        time_t now = time(NULL);
667
14.9k
        if (now < drbg->reseed_time
668
14.9k
            || now - drbg->reseed_time >= drbg->reseed_time_interval)
669
0
            reseed_required = 1;
670
14.9k
    }
671
14.9k
    if (drbg->parent != NULL
672
14.9k
        && get_parent_reseed_count(drbg) != drbg->parent_reseed_counter)
673
1
        reseed_required = 1;
674
675
14.9k
    if (reseed_required || prediction_resistance) {
676
1
        if (!ossl_prov_drbg_reseed(drbg, prediction_resistance, NULL, 0,
677
1
                adin, adinlen)) {
678
0
            ERR_raise(ERR_LIB_PROV, PROV_R_RESEED_ERROR);
679
0
            return 0;
680
0
        }
681
1
        adin = NULL;
682
1
        adinlen = 0;
683
1
    }
684
685
14.9k
    if (!drbg->generate(drbg, out, outlen, adin, adinlen)) {
686
0
        drbg->state = EVP_RAND_STATE_ERROR;
687
0
        ERR_raise(ERR_LIB_PROV, PROV_R_GENERATE_ERROR);
688
0
        return 0;
689
0
    }
690
691
14.9k
    drbg->generate_counter++;
692
693
14.9k
    return 1;
694
14.9k
}
695
696
/*
697
 * Restart |drbg|, using the specified entropy or additional input
698
 *
699
 * Tries its best to get the drbg instantiated by all means,
700
 * regardless of its current state.
701
 *
702
 * Optionally, a |buffer| of |len| random bytes can be passed,
703
 * which is assumed to contain at least |entropy| bits of entropy.
704
 *
705
 * If |entropy| > 0, the buffer content is used as entropy input.
706
 *
707
 * If |entropy| == 0, the buffer content is used as additional input
708
 *
709
 * Returns 1 on success, 0 on failure.
710
 *
711
 * This function is used internally only.
712
 */
713
static int rand_drbg_restart(PROV_DRBG *drbg)
714
478
{
715
    /* repair error state */
716
478
    if (drbg->state == EVP_RAND_STATE_ERROR)
717
0
        drbg->uninstantiate(drbg);
718
719
    /* repair uninitialized state */
720
478
    if (drbg->state == EVP_RAND_STATE_UNINITIALISED)
721
        /* reinstantiate drbg */
722
478
        ossl_prov_drbg_instantiate(drbg, drbg->strength, 0, NULL, 0);
723
724
478
    return drbg->state == EVP_RAND_STATE_READY;
725
478
}
726
727
/* Provider support from here down */
728
static const OSSL_DISPATCH *find_call(const OSSL_DISPATCH *dispatch,
729
    int function)
730
5.74k
{
731
5.74k
    if (dispatch != NULL)
732
3.51k
        while (dispatch->function_id != 0) {
733
3.47k
            if (dispatch->function_id == function)
734
258
                return dispatch;
735
3.21k
            dispatch++;
736
3.21k
        }
737
5.48k
    return NULL;
738
5.74k
}
739
740
int ossl_drbg_enable_locking(void *vctx)
741
17
{
742
17
    PROV_DRBG *drbg = vctx;
743
744
17
    if (drbg != NULL && drbg->lock == NULL) {
745
17
        if (drbg->parent_enable_locking != NULL)
746
17
            if (!drbg->parent_enable_locking(drbg->parent)) {
747
0
                ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_LOCKING_NOT_ENABLED);
748
0
                return 0;
749
0
            }
750
17
        drbg->lock = CRYPTO_THREAD_lock_new();
751
17
        if (drbg->lock == NULL) {
752
0
            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_CREATE_LOCK);
753
0
            return 0;
754
0
        }
755
17
    }
756
17
    return 1;
757
17
}
758
759
/*
760
 * Allocate memory and initialize a new DRBG. The DRBG is allocated on
761
 * the secure heap if |secure| is nonzero and the secure heap is enabled.
762
 * The |parent|, if not NULL, will be used as random source for reseeding.
763
 * This also requires the parent's provider context and the parent's lock.
764
 *
765
 * Returns a pointer to the new DRBG instance on success, NULL on failure.
766
 */
767
PROV_DRBG *ossl_rand_drbg_new(void *provctx, void *parent, const OSSL_DISPATCH *p_dispatch,
768
    int (*dnew)(PROV_DRBG *ctx),
769
    void (*dfree)(void *vctx),
770
    int (*instantiate)(PROV_DRBG *drbg,
771
        const unsigned char *entropy, size_t entropylen,
772
        const unsigned char *nonce, size_t noncelen,
773
        const unsigned char *pers, size_t perslen),
774
    int (*uninstantiate)(PROV_DRBG *ctx),
775
    int (*reseed)(PROV_DRBG *drbg, const unsigned char *ent, size_t ent_len,
776
        const unsigned char *adin, size_t adin_len),
777
    int (*generate)(PROV_DRBG *, unsigned char *out, size_t outlen,
778
        const unsigned char *adin, size_t adin_len))
779
820
{
780
820
    PROV_DRBG *drbg;
781
820
    unsigned int p_str;
782
820
    const OSSL_DISPATCH *pfunc;
783
784
820
    if (!ossl_prov_is_running())
785
0
        return NULL;
786
787
820
    drbg = OPENSSL_zalloc(sizeof(*drbg));
788
820
    if (drbg == NULL) {
789
0
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
790
0
        return NULL;
791
0
    }
792
793
820
    drbg->provctx = provctx;
794
820
    drbg->instantiate = instantiate;
795
820
    drbg->uninstantiate = uninstantiate;
796
820
    drbg->reseed = reseed;
797
820
    drbg->generate = generate;
798
820
    drbg->fork_id = openssl_get_fork_id();
799
800
    /* Extract parent's functions */
801
820
    drbg->parent = parent;
802
820
    if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_ENABLE_LOCKING)) != NULL)
803
43
        drbg->parent_enable_locking = OSSL_FUNC_rand_enable_locking(pfunc);
804
820
    if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_LOCK)) != NULL)
805
43
        drbg->parent_lock = OSSL_FUNC_rand_lock(pfunc);
806
820
    if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_UNLOCK)) != NULL)
807
43
        drbg->parent_unlock = OSSL_FUNC_rand_unlock(pfunc);
808
820
    if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_CTX_PARAMS)) != NULL)
809
43
        drbg->parent_get_ctx_params = OSSL_FUNC_rand_get_ctx_params(pfunc);
810
820
    if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_NONCE)) != NULL)
811
0
        drbg->parent_nonce = OSSL_FUNC_rand_nonce(pfunc);
812
820
    if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_GET_SEED)) != NULL)
813
43
        drbg->parent_get_seed = OSSL_FUNC_rand_get_seed(pfunc);
814
820
    if ((pfunc = find_call(p_dispatch, OSSL_FUNC_RAND_CLEAR_SEED)) != NULL)
815
43
        drbg->parent_clear_seed = OSSL_FUNC_rand_clear_seed(pfunc);
816
817
    /* Set some default maximums up */
818
820
    drbg->max_entropylen = DRBG_MAX_LENGTH;
819
820
    drbg->max_noncelen = DRBG_MAX_LENGTH;
820
820
    drbg->max_perslen = DRBG_MAX_LENGTH;
821
820
    drbg->max_adinlen = DRBG_MAX_LENGTH;
822
820
    drbg->generate_counter = 1;
823
820
    drbg->reseed_counter = 1;
824
820
    drbg->reseed_interval = RESEED_INTERVAL;
825
820
    drbg->reseed_time_interval = TIME_INTERVAL;
826
827
820
    if (!dnew(drbg))
828
0
        goto err;
829
830
820
    if (parent != NULL) {
831
43
        if (!get_parent_strength(drbg, &p_str))
832
0
            goto err;
833
43
        if (drbg->strength > p_str) {
834
            /*
835
             * We currently don't support the algorithm from NIST SP 800-90C
836
             * 10.1.2 to use a weaker DRBG as source
837
             */
838
0
            ERR_raise(ERR_LIB_PROV, PROV_R_PARENT_STRENGTH_TOO_WEAK);
839
0
            goto err;
840
0
        }
841
43
    }
842
#ifdef TSAN_REQUIRES_LOCKING
843
    if (!ossl_drbg_enable_locking(drbg))
844
        goto err;
845
#endif
846
820
    return drbg;
847
848
0
err:
849
0
    dfree(drbg);
850
0
    return NULL;
851
820
}
852
853
void ossl_rand_drbg_free(PROV_DRBG *drbg)
854
806
{
855
806
    if (drbg == NULL)
856
0
        return;
857
858
806
    CRYPTO_THREAD_lock_free(drbg->lock);
859
806
    OPENSSL_free(drbg);
860
806
}
861
862
int ossl_drbg_get_ctx_params(PROV_DRBG *drbg, OSSL_PARAM params[])
863
29.8k
{
864
29.8k
    OSSL_PARAM *p;
865
866
29.8k
    p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
867
29.8k
    if (p != NULL && !OSSL_PARAM_set_int(p, drbg->state))
868
0
        return 0;
869
870
29.8k
    p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
871
29.8k
    if (p != NULL && !OSSL_PARAM_set_int(p, drbg->strength))
872
0
        return 0;
873
874
29.8k
    p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
875
29.8k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_request))
876
0
        return 0;
877
878
29.8k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_ENTROPYLEN);
879
29.8k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_entropylen))
880
0
        return 0;
881
882
29.8k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ENTROPYLEN);
883
29.8k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_entropylen))
884
0
        return 0;
885
886
29.8k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MIN_NONCELEN);
887
29.8k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->min_noncelen))
888
0
        return 0;
889
890
29.8k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_NONCELEN);
891
29.8k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_noncelen))
892
0
        return 0;
893
894
29.8k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_PERSLEN);
895
29.8k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_perslen))
896
0
        return 0;
897
898
29.8k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAX_ADINLEN);
899
29.8k
    if (p != NULL && !OSSL_PARAM_set_size_t(p, drbg->max_adinlen))
900
0
        return 0;
901
902
29.8k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
903
29.8k
    if (p != NULL && !OSSL_PARAM_set_uint(p, drbg->reseed_interval))
904
0
        return 0;
905
906
29.8k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME);
907
29.8k
    if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time))
908
0
        return 0;
909
910
29.8k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
911
29.8k
    if (p != NULL && !OSSL_PARAM_set_time_t(p, drbg->reseed_time_interval))
912
0
        return 0;
913
914
29.8k
    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_RESEED_COUNTER);
915
29.8k
    if (p != NULL
916
14.9k
        && !OSSL_PARAM_set_uint(p, tsan_load(&drbg->reseed_counter)))
917
0
        return 0;
918
29.8k
    return 1;
919
29.8k
}
920
921
int ossl_drbg_set_ctx_params(PROV_DRBG *drbg, const OSSL_PARAM params[])
922
123
{
923
123
    const OSSL_PARAM *p;
924
925
123
    if (params == NULL)
926
0
        return 1;
927
928
123
    p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_REQUESTS);
929
123
    if (p != NULL && !OSSL_PARAM_get_uint(p, &drbg->reseed_interval))
930
0
        return 0;
931
932
123
    p = OSSL_PARAM_locate_const(params, OSSL_DRBG_PARAM_RESEED_TIME_INTERVAL);
933
123
    if (p != NULL && !OSSL_PARAM_get_time_t(p, &drbg->reseed_time_interval))
934
0
        return 0;
935
123
    return 1;
936
123
}