Coverage Report

Created: 2023-06-08 06:41

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