Coverage Report

Created: 2024-11-21 07:03

/src/openssl/crypto/evp/evp_rand.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020-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 <stdio.h>
11
#include <stdlib.h>
12
#include <openssl/evp.h>
13
#include <openssl/rand.h>
14
#include <openssl/core.h>
15
#include <openssl/core_names.h>
16
#include <openssl/crypto.h>
17
#include "internal/cryptlib.h"
18
#include "internal/numbers.h"
19
#include "internal/provider.h"
20
#include "internal/core.h"
21
#include "crypto/evp.h"
22
#include "evp_local.h"
23
24
struct evp_rand_st {
25
    OSSL_PROVIDER *prov;
26
    int name_id;
27
    char *type_name;
28
    const char *description;
29
    CRYPTO_REF_COUNT refcnt;
30
31
    const OSSL_DISPATCH *dispatch;
32
    OSSL_FUNC_rand_newctx_fn *newctx;
33
    OSSL_FUNC_rand_freectx_fn *freectx;
34
    OSSL_FUNC_rand_instantiate_fn *instantiate;
35
    OSSL_FUNC_rand_uninstantiate_fn *uninstantiate;
36
    OSSL_FUNC_rand_generate_fn *generate;
37
    OSSL_FUNC_rand_reseed_fn *reseed;
38
    OSSL_FUNC_rand_nonce_fn *nonce;
39
    OSSL_FUNC_rand_enable_locking_fn *enable_locking;
40
    OSSL_FUNC_rand_lock_fn *lock;
41
    OSSL_FUNC_rand_unlock_fn *unlock;
42
    OSSL_FUNC_rand_gettable_params_fn *gettable_params;
43
    OSSL_FUNC_rand_gettable_ctx_params_fn *gettable_ctx_params;
44
    OSSL_FUNC_rand_settable_ctx_params_fn *settable_ctx_params;
45
    OSSL_FUNC_rand_get_params_fn *get_params;
46
    OSSL_FUNC_rand_get_ctx_params_fn *get_ctx_params;
47
    OSSL_FUNC_rand_set_ctx_params_fn *set_ctx_params;
48
    OSSL_FUNC_rand_verify_zeroization_fn *verify_zeroization;
49
    OSSL_FUNC_rand_get_seed_fn *get_seed;
50
    OSSL_FUNC_rand_clear_seed_fn *clear_seed;
51
} /* EVP_RAND */ ;
52
53
static int evp_rand_up_ref(void *vrand)
54
26
{
55
26
    EVP_RAND *rand = (EVP_RAND *)vrand;
56
26
    int ref = 0;
57
58
26
    if (rand != NULL)
59
26
        return CRYPTO_UP_REF(&rand->refcnt, &ref);
60
0
    return 1;
61
26
}
62
63
static void evp_rand_free(void *vrand)
64
36
{
65
36
    EVP_RAND *rand = (EVP_RAND *)vrand;
66
36
    int ref = 0;
67
68
36
    if (rand == NULL)
69
0
        return;
70
36
    CRYPTO_DOWN_REF(&rand->refcnt, &ref);
71
36
    if (ref > 0)
72
26
        return;
73
10
    OPENSSL_free(rand->type_name);
74
10
    ossl_provider_free(rand->prov);
75
10
    CRYPTO_FREE_REF(&rand->refcnt);
76
10
    OPENSSL_free(rand);
77
10
}
78
79
static void *evp_rand_new(void)
80
10
{
81
10
    EVP_RAND *rand = OPENSSL_zalloc(sizeof(*rand));
82
83
10
    if (rand == NULL)
84
0
        return NULL;
85
86
10
    if (!CRYPTO_NEW_REF(&rand->refcnt, 1)) {
87
0
        OPENSSL_free(rand);
88
0
        return NULL;
89
0
    }
90
10
    return rand;
91
10
}
92
93
/* Enable locking of the underlying DRBG/RAND if available */
94
int EVP_RAND_enable_locking(EVP_RAND_CTX *rand)
95
2
{
96
2
    if (rand->meth->enable_locking != NULL)
97
2
        return rand->meth->enable_locking(rand->algctx);
98
2
    ERR_raise(ERR_LIB_EVP, EVP_R_LOCKING_NOT_SUPPORTED);
99
0
    return 0;
100
2
}
101
102
/* Lock the underlying DRBG/RAND if available */
103
static int evp_rand_lock(EVP_RAND_CTX *rand)
104
125k
{
105
125k
    if (rand->meth->lock != NULL)
106
125k
        return rand->meth->lock(rand->algctx);
107
0
    return 1;
108
125k
}
109
110
/* Unlock the underlying DRBG/RAND if available */
111
static void evp_rand_unlock(EVP_RAND_CTX *rand)
112
125k
{
113
125k
    if (rand->meth->unlock != NULL)
114
125k
        rand->meth->unlock(rand->algctx);
115
125k
}
116
117
static void *evp_rand_from_algorithm(int name_id,
118
                                     const OSSL_ALGORITHM *algodef,
119
                                     OSSL_PROVIDER *prov)
120
10
{
121
10
    const OSSL_DISPATCH *fns = algodef->implementation;
122
10
    EVP_RAND *rand = NULL;
123
10
    int fnrandcnt = 0, fnctxcnt = 0, fnlockcnt = 0, fnenablelockcnt = 0;
124
#ifdef FIPS_MODULE
125
    int fnzeroizecnt = 0;
126
#endif
127
128
10
    if ((rand = evp_rand_new()) == NULL) {
129
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
130
0
        return NULL;
131
0
    }
132
10
    rand->name_id = name_id;
133
10
    if ((rand->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
134
0
        evp_rand_free(rand);
135
0
        return NULL;
136
0
    }
137
10
    rand->description = algodef->algorithm_description;
138
10
    rand->dispatch = fns;
139
166
    for (; fns->function_id != 0; fns++) {
140
156
        switch (fns->function_id) {
141
10
        case OSSL_FUNC_RAND_NEWCTX:
142
10
            if (rand->newctx != NULL)
143
0
                break;
144
10
            rand->newctx = OSSL_FUNC_rand_newctx(fns);
145
10
            fnctxcnt++;
146
10
            break;
147
10
        case OSSL_FUNC_RAND_FREECTX:
148
10
            if (rand->freectx != NULL)
149
0
                break;
150
10
            rand->freectx = OSSL_FUNC_rand_freectx(fns);
151
10
            fnctxcnt++;
152
10
            break;
153
10
        case OSSL_FUNC_RAND_INSTANTIATE:
154
10
            if (rand->instantiate != NULL)
155
0
                break;
156
10
            rand->instantiate = OSSL_FUNC_rand_instantiate(fns);
157
10
            fnrandcnt++;
158
10
            break;
159
10
        case OSSL_FUNC_RAND_UNINSTANTIATE:
160
10
             if (rand->uninstantiate != NULL)
161
0
                break;
162
10
            rand->uninstantiate = OSSL_FUNC_rand_uninstantiate(fns);
163
10
            fnrandcnt++;
164
10
            break;
165
10
        case OSSL_FUNC_RAND_GENERATE:
166
10
            if (rand->generate != NULL)
167
0
                break;
168
10
            rand->generate = OSSL_FUNC_rand_generate(fns);
169
10
            fnrandcnt++;
170
10
            break;
171
10
        case OSSL_FUNC_RAND_RESEED:
172
10
            if (rand->reseed != NULL)
173
0
                break;
174
10
            rand->reseed = OSSL_FUNC_rand_reseed(fns);
175
10
            break;
176
2
        case OSSL_FUNC_RAND_NONCE:
177
2
            if (rand->nonce != NULL)
178
0
                break;
179
2
            rand->nonce = OSSL_FUNC_rand_nonce(fns);
180
2
            break;
181
10
        case OSSL_FUNC_RAND_ENABLE_LOCKING:
182
10
            if (rand->enable_locking != NULL)
183
0
                break;
184
10
            rand->enable_locking = OSSL_FUNC_rand_enable_locking(fns);
185
10
            fnenablelockcnt++;
186
10
            break;
187
10
        case OSSL_FUNC_RAND_LOCK:
188
10
            if (rand->lock != NULL)
189
0
                break;
190
10
            rand->lock = OSSL_FUNC_rand_lock(fns);
191
10
            fnlockcnt++;
192
10
            break;
193
10
        case OSSL_FUNC_RAND_UNLOCK:
194
10
            if (rand->unlock != NULL)
195
0
                break;
196
10
            rand->unlock = OSSL_FUNC_rand_unlock(fns);
197
10
            fnlockcnt++;
198
10
            break;
199
0
        case OSSL_FUNC_RAND_GETTABLE_PARAMS:
200
0
            if (rand->gettable_params != NULL)
201
0
                break;
202
0
            rand->gettable_params =
203
0
                OSSL_FUNC_rand_gettable_params(fns);
204
0
            break;
205
10
        case OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS:
206
10
            if (rand->gettable_ctx_params != NULL)
207
0
                break;
208
10
            rand->gettable_ctx_params =
209
10
                OSSL_FUNC_rand_gettable_ctx_params(fns);
210
10
            break;
211
8
        case OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS:
212
8
            if (rand->settable_ctx_params != NULL)
213
0
                break;
214
8
            rand->settable_ctx_params =
215
8
                OSSL_FUNC_rand_settable_ctx_params(fns);
216
8
            break;
217
0
        case OSSL_FUNC_RAND_GET_PARAMS:
218
0
            if (rand->get_params != NULL)
219
0
                break;
220
0
            rand->get_params = OSSL_FUNC_rand_get_params(fns);
221
0
            break;
222
10
        case OSSL_FUNC_RAND_GET_CTX_PARAMS:
223
10
            if (rand->get_ctx_params != NULL)
224
0
                break;
225
10
            rand->get_ctx_params = OSSL_FUNC_rand_get_ctx_params(fns);
226
10
            fnctxcnt++;
227
10
            break;
228
8
        case OSSL_FUNC_RAND_SET_CTX_PARAMS:
229
8
            if (rand->set_ctx_params != NULL)
230
0
                break;
231
8
            rand->set_ctx_params = OSSL_FUNC_rand_set_ctx_params(fns);
232
8
            break;
233
10
        case OSSL_FUNC_RAND_VERIFY_ZEROIZATION:
234
10
            if (rand->verify_zeroization != NULL)
235
0
                break;
236
10
            rand->verify_zeroization = OSSL_FUNC_rand_verify_zeroization(fns);
237
#ifdef FIPS_MODULE
238
            fnzeroizecnt++;
239
#endif
240
10
            break;
241
10
        case OSSL_FUNC_RAND_GET_SEED:
242
10
            if (rand->get_seed != NULL)
243
0
                break;
244
10
            rand->get_seed = OSSL_FUNC_rand_get_seed(fns);
245
10
            break;
246
8
        case OSSL_FUNC_RAND_CLEAR_SEED:
247
8
            if (rand->clear_seed != NULL)
248
0
                break;
249
8
            rand->clear_seed = OSSL_FUNC_rand_clear_seed(fns);
250
8
            break;
251
156
        }
252
156
    }
253
    /*
254
     * In order to be a consistent set of functions we must have at least
255
     * a complete set of "rand" functions and a complete set of context
256
     * management functions.  In FIPS mode, we also require the zeroization
257
     * verification function.
258
     *
259
     * In addition, if locking can be enabled, we need a complete set of
260
     * locking functions.
261
     */
262
10
    if (fnrandcnt != 3
263
10
            || fnctxcnt != 3
264
10
            || (fnenablelockcnt != 0 && fnenablelockcnt != 1)
265
10
            || (fnlockcnt != 0 && fnlockcnt != 2)
266
#ifdef FIPS_MODULE
267
            || fnzeroizecnt != 1
268
#endif
269
10
       ) {
270
0
        evp_rand_free(rand);
271
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
272
0
        return NULL;
273
0
    }
274
275
10
    if (prov != NULL && !ossl_provider_up_ref(prov)) {
276
0
        evp_rand_free(rand);
277
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
278
0
        return NULL;
279
0
    }
280
10
    rand->prov = prov;
281
282
10
    return rand;
283
10
}
284
285
EVP_RAND *EVP_RAND_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
286
                         const char *properties)
287
6
{
288
6
    return evp_generic_fetch(libctx, OSSL_OP_RAND, algorithm, properties,
289
6
                             evp_rand_from_algorithm, evp_rand_up_ref,
290
6
                             evp_rand_free);
291
6
}
292
293
int EVP_RAND_up_ref(EVP_RAND *rand)
294
6
{
295
6
    return evp_rand_up_ref(rand);
296
6
}
297
298
void EVP_RAND_free(EVP_RAND *rand)
299
12
{
300
12
    evp_rand_free(rand);
301
12
}
302
303
int evp_rand_get_number(const EVP_RAND *rand)
304
0
{
305
0
    return rand->name_id;
306
0
}
307
308
const char *EVP_RAND_get0_name(const EVP_RAND *rand)
309
0
{
310
0
    return rand->type_name;
311
0
}
312
313
const char *EVP_RAND_get0_description(const EVP_RAND *rand)
314
0
{
315
0
    return rand->description;
316
0
}
317
318
int EVP_RAND_is_a(const EVP_RAND *rand, const char *name)
319
0
{
320
0
    return rand != NULL && evp_is_a(rand->prov, rand->name_id, NULL, name);
321
0
}
322
323
const OSSL_PROVIDER *EVP_RAND_get0_provider(const EVP_RAND *rand)
324
4
{
325
4
    return rand->prov;
326
4
}
327
328
int EVP_RAND_get_params(EVP_RAND *rand, OSSL_PARAM params[])
329
0
{
330
0
    if (rand->get_params != NULL)
331
0
        return rand->get_params(params);
332
0
    return 1;
333
0
}
334
335
int EVP_RAND_CTX_up_ref(EVP_RAND_CTX *ctx)
336
4
{
337
4
    int ref = 0;
338
339
4
    return CRYPTO_UP_REF(&ctx->refcnt, &ref);
340
4
}
341
342
EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, EVP_RAND_CTX *parent)
343
6
{
344
6
    EVP_RAND_CTX *ctx;
345
6
    void *parent_ctx = NULL;
346
6
    const OSSL_DISPATCH *parent_dispatch = NULL;
347
348
6
    if (rand == NULL) {
349
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
350
0
        return NULL;
351
0
    }
352
353
6
    ctx = OPENSSL_zalloc(sizeof(*ctx));
354
6
    if (ctx == NULL)
355
0
        return NULL;
356
6
    if (!CRYPTO_NEW_REF(&ctx->refcnt, 1)) {
357
0
        OPENSSL_free(ctx);
358
0
        return NULL;
359
0
    }
360
6
    if (parent != NULL) {
361
4
        if (!EVP_RAND_CTX_up_ref(parent)) {
362
0
            ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
363
0
            CRYPTO_FREE_REF(&ctx->refcnt);
364
0
            OPENSSL_free(ctx);
365
0
            return NULL;
366
0
        }
367
4
        parent_ctx = parent->algctx;
368
4
        parent_dispatch = parent->meth->dispatch;
369
4
    }
370
6
    if ((ctx->algctx = rand->newctx(ossl_provider_ctx(rand->prov), parent_ctx,
371
6
                                    parent_dispatch)) == NULL
372
6
            || !EVP_RAND_up_ref(rand)) {
373
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
374
0
        rand->freectx(ctx->algctx);
375
0
        CRYPTO_FREE_REF(&ctx->refcnt);
376
0
        OPENSSL_free(ctx);
377
0
        EVP_RAND_CTX_free(parent);
378
0
        return NULL;
379
0
    }
380
6
    ctx->meth = rand;
381
6
    ctx->parent = parent;
382
6
    return ctx;
383
6
}
384
385
void EVP_RAND_CTX_free(EVP_RAND_CTX *ctx)
386
18
{
387
18
    int ref = 0;
388
18
    EVP_RAND_CTX *parent;
389
390
18
    if (ctx == NULL)
391
8
        return;
392
393
10
    CRYPTO_DOWN_REF(&ctx->refcnt, &ref);
394
10
    if (ref > 0)
395
4
        return;
396
6
    parent = ctx->parent;
397
6
    ctx->meth->freectx(ctx->algctx);
398
6
    ctx->algctx = NULL;
399
6
    EVP_RAND_free(ctx->meth);
400
6
    CRYPTO_FREE_REF(&ctx->refcnt);
401
6
    OPENSSL_free(ctx);
402
6
    EVP_RAND_CTX_free(parent);
403
6
}
404
405
EVP_RAND *EVP_RAND_CTX_get0_rand(EVP_RAND_CTX *ctx)
406
0
{
407
0
    return ctx->meth;
408
0
}
409
410
static int evp_rand_get_ctx_params_locked(EVP_RAND_CTX *ctx,
411
                                          OSSL_PARAM params[])
412
125k
{
413
125k
    return ctx->meth->get_ctx_params(ctx->algctx, params);
414
125k
}
415
416
int EVP_RAND_CTX_get_params(EVP_RAND_CTX *ctx, OSSL_PARAM params[])
417
0
{
418
0
    int res;
419
420
0
    if (!evp_rand_lock(ctx))
421
0
        return 0;
422
0
    res = evp_rand_get_ctx_params_locked(ctx, params);
423
0
    evp_rand_unlock(ctx);
424
0
    return res;
425
0
}
426
427
static int evp_rand_set_ctx_params_locked(EVP_RAND_CTX *ctx,
428
                                          const OSSL_PARAM params[])
429
0
{
430
0
    if (ctx->meth->set_ctx_params != NULL)
431
0
        return ctx->meth->set_ctx_params(ctx->algctx, params);
432
0
    return 1;
433
0
}
434
435
int EVP_RAND_CTX_set_params(EVP_RAND_CTX *ctx, const OSSL_PARAM params[])
436
0
{
437
0
    int res;
438
439
0
    if (!evp_rand_lock(ctx))
440
0
        return 0;
441
0
    res = evp_rand_set_ctx_params_locked(ctx, params);
442
0
    evp_rand_unlock(ctx);
443
0
    return res;
444
0
}
445
446
const OSSL_PARAM *EVP_RAND_gettable_params(const EVP_RAND *rand)
447
0
{
448
0
    if (rand->gettable_params == NULL)
449
0
        return NULL;
450
0
    return rand->gettable_params(ossl_provider_ctx(EVP_RAND_get0_provider(rand)));
451
0
}
452
453
const OSSL_PARAM *EVP_RAND_gettable_ctx_params(const EVP_RAND *rand)
454
0
{
455
0
    void *provctx;
456
457
0
    if (rand->gettable_ctx_params == NULL)
458
0
        return NULL;
459
0
    provctx = ossl_provider_ctx(EVP_RAND_get0_provider(rand));
460
0
    return rand->gettable_ctx_params(NULL, provctx);
461
0
}
462
463
const OSSL_PARAM *EVP_RAND_settable_ctx_params(const EVP_RAND *rand)
464
0
{
465
0
    void *provctx;
466
467
0
    if (rand->settable_ctx_params == NULL)
468
0
        return NULL;
469
0
    provctx = ossl_provider_ctx(EVP_RAND_get0_provider(rand));
470
0
    return rand->settable_ctx_params(NULL, provctx);
471
0
}
472
473
const OSSL_PARAM *EVP_RAND_CTX_gettable_params(EVP_RAND_CTX *ctx)
474
0
{
475
0
    void *provctx;
476
477
0
    if (ctx->meth->gettable_ctx_params == NULL)
478
0
        return NULL;
479
0
    provctx = ossl_provider_ctx(EVP_RAND_get0_provider(ctx->meth));
480
0
    return ctx->meth->gettable_ctx_params(ctx->algctx, provctx);
481
0
}
482
483
const OSSL_PARAM *EVP_RAND_CTX_settable_params(EVP_RAND_CTX *ctx)
484
4
{
485
4
    void *provctx;
486
487
4
    if (ctx->meth->settable_ctx_params == NULL)
488
0
        return NULL;
489
4
    provctx = ossl_provider_ctx(EVP_RAND_get0_provider(ctx->meth));
490
4
    return ctx->meth->settable_ctx_params(ctx->algctx, provctx);
491
4
}
492
493
void EVP_RAND_do_all_provided(OSSL_LIB_CTX *libctx,
494
                              void (*fn)(EVP_RAND *rand, void *arg),
495
                              void *arg)
496
0
{
497
0
    evp_generic_do_all(libctx, OSSL_OP_RAND,
498
0
                       (void (*)(void *, void *))fn, arg,
499
0
                       evp_rand_from_algorithm, evp_rand_up_ref,
500
0
                       evp_rand_free);
501
0
}
502
503
int EVP_RAND_names_do_all(const EVP_RAND *rand,
504
                          void (*fn)(const char *name, void *data),
505
                          void *data)
506
0
{
507
0
    if (rand->prov != NULL)
508
0
        return evp_names_do_all(rand->prov, rand->name_id, fn, data);
509
510
0
    return 1;
511
0
}
512
513
static int evp_rand_instantiate_locked
514
    (EVP_RAND_CTX *ctx, unsigned int strength, int prediction_resistance,
515
     const unsigned char *pstr, size_t pstr_len, const OSSL_PARAM params[])
516
6
{
517
6
    return ctx->meth->instantiate(ctx->algctx, strength, prediction_resistance,
518
6
                                  pstr, pstr_len, params);
519
6
}
520
521
int EVP_RAND_instantiate(EVP_RAND_CTX *ctx, unsigned int strength,
522
                         int prediction_resistance,
523
                         const unsigned char *pstr, size_t pstr_len,
524
                         const OSSL_PARAM params[])
525
6
{
526
6
    int res;
527
528
6
    if (!evp_rand_lock(ctx))
529
0
        return 0;
530
6
    res = evp_rand_instantiate_locked(ctx, strength, prediction_resistance,
531
6
                                      pstr, pstr_len, params);
532
6
    evp_rand_unlock(ctx);
533
6
    return res;
534
6
}
535
536
static int evp_rand_uninstantiate_locked(EVP_RAND_CTX *ctx)
537
0
{
538
0
    return ctx->meth->uninstantiate(ctx->algctx);
539
0
}
540
541
int EVP_RAND_uninstantiate(EVP_RAND_CTX *ctx)
542
0
{
543
0
    int res;
544
545
0
    if (!evp_rand_lock(ctx))
546
0
        return 0;
547
0
    res = evp_rand_uninstantiate_locked(ctx);
548
0
    evp_rand_unlock(ctx);
549
0
    return res;
550
0
}
551
552
static int evp_rand_generate_locked(EVP_RAND_CTX *ctx, unsigned char *out,
553
                                    size_t outlen, unsigned int strength,
554
                                    int prediction_resistance,
555
                                    const unsigned char *addin,
556
                                    size_t addin_len)
557
125k
{
558
125k
    size_t chunk, max_request = 0;
559
125k
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
560
561
125k
    params[0] = OSSL_PARAM_construct_size_t(OSSL_RAND_PARAM_MAX_REQUEST,
562
125k
                                            &max_request);
563
125k
    if (!evp_rand_get_ctx_params_locked(ctx, params)
564
125k
            || max_request == 0) {
565
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_GET_MAXIMUM_REQUEST_SIZE);
566
0
        return 0;
567
0
    }
568
251k
    for (; outlen > 0; outlen -= chunk, out += chunk) {
569
125k
        chunk = outlen > max_request ? max_request : outlen;
570
125k
        if (!ctx->meth->generate(ctx->algctx, out, chunk, strength,
571
125k
                                 prediction_resistance, addin, addin_len)) {
572
0
            ERR_raise(ERR_LIB_EVP, EVP_R_GENERATE_ERROR);
573
0
            return 0;
574
0
        }
575
        /*
576
         * Prediction resistance is only relevant the first time around,
577
         * subsequently, the DRBG has already been properly reseeded.
578
         */
579
125k
        prediction_resistance = 0;
580
125k
    }
581
125k
    return 1;
582
125k
}
583
584
int EVP_RAND_generate(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen,
585
                      unsigned int strength, int prediction_resistance,
586
                      const unsigned char *addin, size_t addin_len)
587
125k
{
588
125k
    int res;
589
590
125k
    if (!evp_rand_lock(ctx))
591
0
        return 0;
592
125k
    res = evp_rand_generate_locked(ctx, out, outlen, strength,
593
125k
                                   prediction_resistance, addin, addin_len);
594
125k
    evp_rand_unlock(ctx);
595
125k
    return res;
596
125k
}
597
598
static int evp_rand_reseed_locked(EVP_RAND_CTX *ctx, int prediction_resistance,
599
                                  const unsigned char *ent, size_t ent_len,
600
                                  const unsigned char *addin, size_t addin_len)
601
0
{
602
0
    if (ctx->meth->reseed != NULL)
603
0
        return ctx->meth->reseed(ctx->algctx, prediction_resistance,
604
0
                                 ent, ent_len, addin, addin_len);
605
0
    return 1;
606
0
}
607
608
int EVP_RAND_reseed(EVP_RAND_CTX *ctx, int prediction_resistance,
609
                    const unsigned char *ent, size_t ent_len,
610
                    const unsigned char *addin, size_t addin_len)
611
0
{
612
0
    int res;
613
614
0
    if (!evp_rand_lock(ctx))
615
0
        return 0;
616
0
    res = evp_rand_reseed_locked(ctx, prediction_resistance,
617
0
                                 ent, ent_len, addin, addin_len);
618
0
    evp_rand_unlock(ctx);
619
0
    return res;
620
0
}
621
622
static unsigned int evp_rand_strength_locked(EVP_RAND_CTX *ctx)
623
0
{
624
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
625
0
    unsigned int strength = 0;
626
627
0
    params[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
628
0
    if (!evp_rand_get_ctx_params_locked(ctx, params))
629
0
        return 0;
630
0
    return strength;
631
0
}
632
633
unsigned int EVP_RAND_get_strength(EVP_RAND_CTX *ctx)
634
0
{
635
0
    unsigned int res;
636
637
0
    if (!evp_rand_lock(ctx))
638
0
        return 0;
639
0
    res = evp_rand_strength_locked(ctx);
640
0
    evp_rand_unlock(ctx);
641
0
    return res;
642
0
}
643
644
static int evp_rand_nonce_locked(EVP_RAND_CTX *ctx, unsigned char *out,
645
                                 size_t outlen)
646
0
{
647
0
    unsigned int str = evp_rand_strength_locked(ctx);
648
649
0
    if (ctx->meth->nonce == NULL)
650
0
        return 0;
651
0
    if (ctx->meth->nonce(ctx->algctx, out, str, outlen, outlen))
652
0
        return 1;
653
0
    return evp_rand_generate_locked(ctx, out, outlen, str, 0, NULL, 0);
654
0
}
655
656
int EVP_RAND_nonce(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen)
657
0
{
658
0
    int res;
659
660
0
    if (!evp_rand_lock(ctx))
661
0
        return 0;
662
0
    res = evp_rand_nonce_locked(ctx, out, outlen);
663
0
    evp_rand_unlock(ctx);
664
0
    return res;
665
0
}
666
667
int EVP_RAND_get_state(EVP_RAND_CTX *ctx)
668
0
{
669
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
670
0
    int state;
671
672
0
    params[0] = OSSL_PARAM_construct_int(OSSL_RAND_PARAM_STATE, &state);
673
0
    if (!EVP_RAND_CTX_get_params(ctx, params))
674
0
        state = EVP_RAND_STATE_ERROR;
675
0
    return state;
676
0
}
677
678
static int evp_rand_verify_zeroization_locked(EVP_RAND_CTX *ctx)
679
0
{
680
0
    if (ctx->meth->verify_zeroization != NULL)
681
0
        return ctx->meth->verify_zeroization(ctx->algctx);
682
0
    return 0;
683
0
}
684
685
int EVP_RAND_verify_zeroization(EVP_RAND_CTX *ctx)
686
0
{
687
0
    int res;
688
689
0
    if (!evp_rand_lock(ctx))
690
0
        return 0;
691
0
    res = evp_rand_verify_zeroization_locked(ctx);
692
0
    evp_rand_unlock(ctx);
693
0
    return res;
694
0
}
695
696
int evp_rand_can_seed(EVP_RAND_CTX *ctx)
697
0
{
698
0
    return ctx->meth->get_seed != NULL;
699
0
}
700
701
static size_t evp_rand_get_seed_locked(EVP_RAND_CTX *ctx,
702
                                       unsigned char **buffer,
703
                                       int entropy,
704
                                       size_t min_len, size_t max_len,
705
                                       int prediction_resistance,
706
                                       const unsigned char *adin,
707
                                       size_t adin_len)
708
0
{
709
0
    if (ctx->meth->get_seed != NULL)
710
0
        return ctx->meth->get_seed(ctx->algctx, buffer,
711
0
                                   entropy, min_len, max_len,
712
0
                                   prediction_resistance,
713
0
                                   adin, adin_len);
714
0
    return 0;
715
0
}
716
717
size_t evp_rand_get_seed(EVP_RAND_CTX *ctx,
718
                         unsigned char **buffer,
719
                         int entropy, size_t min_len, size_t max_len,
720
                         int prediction_resistance,
721
                         const unsigned char *adin, size_t adin_len)
722
0
{
723
0
    int res;
724
725
0
    if (!evp_rand_lock(ctx))
726
0
        return 0;
727
0
    res = evp_rand_get_seed_locked(ctx,
728
0
                                   buffer,
729
0
                                   entropy, min_len, max_len,
730
0
                                   prediction_resistance,
731
0
                                   adin, adin_len);
732
0
    evp_rand_unlock(ctx);
733
0
    return res;
734
0
}
735
736
static void evp_rand_clear_seed_locked(EVP_RAND_CTX *ctx,
737
                                       unsigned char *buffer, size_t b_len)
738
0
{
739
0
    if (ctx->meth->clear_seed != NULL)
740
0
        ctx->meth->clear_seed(ctx->algctx, buffer, b_len);
741
0
}
742
743
void evp_rand_clear_seed(EVP_RAND_CTX *ctx,
744
                         unsigned char *buffer, size_t b_len)
745
0
{
746
0
    if (!evp_rand_lock(ctx))
747
0
        return;
748
0
    evp_rand_clear_seed_locked(ctx, buffer, b_len);
749
0
    evp_rand_unlock(ctx);
750
0
}