Coverage Report

Created: 2025-06-13 06:57

/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
16
{
55
16
    EVP_RAND *rand = (EVP_RAND *)vrand;
56
16
    int ref = 0;
57
58
16
    if (rand != NULL)
59
16
        return CRYPTO_UP_REF(&rand->refcnt, &ref);
60
0
    return 1;
61
16
}
62
63
static void evp_rand_free(void *vrand)
64
22
{
65
22
    EVP_RAND *rand = (EVP_RAND *)vrand;
66
22
    int ref = 0;
67
68
22
    if (rand == NULL)
69
0
        return;
70
22
    CRYPTO_DOWN_REF(&rand->refcnt, &ref);
71
22
    if (ref > 0)
72
16
        return;
73
6
    OPENSSL_free(rand->type_name);
74
6
    ossl_provider_free(rand->prov);
75
6
    CRYPTO_FREE_REF(&rand->refcnt);
76
6
    OPENSSL_free(rand);
77
6
}
78
79
static void *evp_rand_new(void)
80
6
{
81
6
    EVP_RAND *rand = OPENSSL_zalloc(sizeof(*rand));
82
83
6
    if (rand == NULL)
84
0
        return NULL;
85
86
6
    if (!CRYPTO_NEW_REF(&rand->refcnt, 1)) {
87
0
        OPENSSL_free(rand);
88
0
        return NULL;
89
0
    }
90
6
    return rand;
91
6
}
92
93
/* Enable locking of the underlying DRBG/RAND if available */
94
int EVP_RAND_enable_locking(EVP_RAND_CTX *rand)
95
1
{
96
1
    if (rand->meth->enable_locking != NULL)
97
1
        return rand->meth->enable_locking(rand->algctx);
98
1
    ERR_raise(ERR_LIB_EVP, EVP_R_LOCKING_NOT_SUPPORTED);
99
0
    return 0;
100
1
}
101
102
/* Lock the underlying DRBG/RAND if available */
103
static int evp_rand_lock(EVP_RAND_CTX *rand)
104
14.5k
{
105
14.5k
    if (rand->meth->lock != NULL)
106
1
        return rand->meth->lock(rand->algctx);
107
14.5k
    return 1;
108
14.5k
}
109
110
/* Unlock the underlying DRBG/RAND if available */
111
static void evp_rand_unlock(EVP_RAND_CTX *rand)
112
14.5k
{
113
14.5k
    if (rand->meth->unlock != NULL)
114
1
        rand->meth->unlock(rand->algctx);
115
14.5k
}
116
117
static void *evp_rand_from_algorithm(int name_id,
118
                                     const OSSL_ALGORITHM *algodef,
119
                                     OSSL_PROVIDER *prov)
120
6
{
121
6
    const OSSL_DISPATCH *fns = algodef->implementation;
122
6
    EVP_RAND *rand = NULL;
123
6
    int fnrandcnt = 0, fnctxcnt = 0, fnlockcnt = 0, fnenablelockcnt = 0;
124
#ifdef FIPS_MODULE
125
    int fnzeroizecnt = 0;
126
#endif
127
128
6
    if ((rand = evp_rand_new()) == NULL) {
129
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
130
0
        return NULL;
131
0
    }
132
6
    rand->name_id = name_id;
133
6
    if ((rand->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
134
0
        evp_rand_free(rand);
135
0
        return NULL;
136
0
    }
137
6
    rand->description = algodef->algorithm_description;
138
6
    rand->dispatch = fns;
139
92
    for (; fns->function_id != 0; fns++) {
140
86
        switch (fns->function_id) {
141
6
        case OSSL_FUNC_RAND_NEWCTX:
142
6
            if (rand->newctx != NULL)
143
0
                break;
144
6
            rand->newctx = OSSL_FUNC_rand_newctx(fns);
145
6
            fnctxcnt++;
146
6
            break;
147
6
        case OSSL_FUNC_RAND_FREECTX:
148
6
            if (rand->freectx != NULL)
149
0
                break;
150
6
            rand->freectx = OSSL_FUNC_rand_freectx(fns);
151
6
            fnctxcnt++;
152
6
            break;
153
6
        case OSSL_FUNC_RAND_INSTANTIATE:
154
6
            if (rand->instantiate != NULL)
155
0
                break;
156
6
            rand->instantiate = OSSL_FUNC_rand_instantiate(fns);
157
6
            fnrandcnt++;
158
6
            break;
159
6
        case OSSL_FUNC_RAND_UNINSTANTIATE:
160
6
             if (rand->uninstantiate != NULL)
161
0
                break;
162
6
            rand->uninstantiate = OSSL_FUNC_rand_uninstantiate(fns);
163
6
            fnrandcnt++;
164
6
            break;
165
6
        case OSSL_FUNC_RAND_GENERATE:
166
6
            if (rand->generate != NULL)
167
0
                break;
168
6
            rand->generate = OSSL_FUNC_rand_generate(fns);
169
6
            fnrandcnt++;
170
6
            break;
171
5
        case OSSL_FUNC_RAND_RESEED:
172
5
            if (rand->reseed != NULL)
173
0
                break;
174
5
            rand->reseed = OSSL_FUNC_rand_reseed(fns);
175
5
            break;
176
1
        case OSSL_FUNC_RAND_NONCE:
177
1
            if (rand->nonce != NULL)
178
0
                break;
179
1
            rand->nonce = OSSL_FUNC_rand_nonce(fns);
180
1
            break;
181
6
        case OSSL_FUNC_RAND_ENABLE_LOCKING:
182
6
            if (rand->enable_locking != NULL)
183
0
                break;
184
6
            rand->enable_locking = OSSL_FUNC_rand_enable_locking(fns);
185
6
            fnenablelockcnt++;
186
6
            break;
187
5
        case OSSL_FUNC_RAND_LOCK:
188
5
            if (rand->lock != NULL)
189
0
                break;
190
5
            rand->lock = OSSL_FUNC_rand_lock(fns);
191
5
            fnlockcnt++;
192
5
            break;
193
5
        case OSSL_FUNC_RAND_UNLOCK:
194
5
            if (rand->unlock != NULL)
195
0
                break;
196
5
            rand->unlock = OSSL_FUNC_rand_unlock(fns);
197
5
            fnlockcnt++;
198
5
            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
6
        case OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS:
206
6
            if (rand->gettable_ctx_params != NULL)
207
0
                break;
208
6
            rand->gettable_ctx_params =
209
6
                OSSL_FUNC_rand_gettable_ctx_params(fns);
210
6
            break;
211
4
        case OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS:
212
4
            if (rand->settable_ctx_params != NULL)
213
0
                break;
214
4
            rand->settable_ctx_params =
215
4
                OSSL_FUNC_rand_settable_ctx_params(fns);
216
4
            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
6
        case OSSL_FUNC_RAND_GET_CTX_PARAMS:
223
6
            if (rand->get_ctx_params != NULL)
224
0
                break;
225
6
            rand->get_ctx_params = OSSL_FUNC_rand_get_ctx_params(fns);
226
6
            fnctxcnt++;
227
6
            break;
228
4
        case OSSL_FUNC_RAND_SET_CTX_PARAMS:
229
4
            if (rand->set_ctx_params != NULL)
230
0
                break;
231
4
            rand->set_ctx_params = OSSL_FUNC_rand_set_ctx_params(fns);
232
4
            break;
233
5
        case OSSL_FUNC_RAND_VERIFY_ZEROIZATION:
234
5
            if (rand->verify_zeroization != NULL)
235
0
                break;
236
5
            rand->verify_zeroization = OSSL_FUNC_rand_verify_zeroization(fns);
237
#ifdef FIPS_MODULE
238
            fnzeroizecnt++;
239
#endif
240
5
            break;
241
5
        case OSSL_FUNC_RAND_GET_SEED:
242
5
            if (rand->get_seed != NULL)
243
0
                break;
244
5
            rand->get_seed = OSSL_FUNC_rand_get_seed(fns);
245
5
            break;
246
4
        case OSSL_FUNC_RAND_CLEAR_SEED:
247
4
            if (rand->clear_seed != NULL)
248
0
                break;
249
4
            rand->clear_seed = OSSL_FUNC_rand_clear_seed(fns);
250
4
            break;
251
86
        }
252
86
    }
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
6
    if (fnrandcnt != 3
263
6
            || fnctxcnt != 3
264
6
            || (fnenablelockcnt != 0 && fnenablelockcnt != 1)
265
6
            || (fnlockcnt != 0 && fnlockcnt != 2)
266
#ifdef FIPS_MODULE
267
            || fnzeroizecnt != 1
268
#endif
269
6
       ) {
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
6
    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
6
    rand->prov = prov;
281
282
6
    return rand;
283
6
}
284
285
EVP_RAND *EVP_RAND_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
286
                         const char *properties)
287
4
{
288
4
    return evp_generic_fetch(libctx, OSSL_OP_RAND, algorithm, properties,
289
4
                             evp_rand_from_algorithm, evp_rand_up_ref,
290
4
                             evp_rand_free);
291
4
}
292
293
int EVP_RAND_up_ref(EVP_RAND *rand)
294
4
{
295
4
    return evp_rand_up_ref(rand);
296
4
}
297
298
void EVP_RAND_free(EVP_RAND *rand)
299
8
{
300
8
    evp_rand_free(rand);
301
8
}
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
3
{
325
3
    return rand->prov;
326
3
}
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
3
{
337
3
    int ref = 0;
338
339
3
    return CRYPTO_UP_REF(&ctx->refcnt, &ref);
340
3
}
341
342
EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, EVP_RAND_CTX *parent)
343
4
{
344
4
    EVP_RAND_CTX *ctx;
345
4
    void *parent_ctx = NULL;
346
4
    const OSSL_DISPATCH *parent_dispatch = NULL;
347
348
4
    if (rand == NULL) {
349
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
350
0
        return NULL;
351
0
    }
352
353
4
    ctx = OPENSSL_zalloc(sizeof(*ctx));
354
4
    if (ctx == NULL)
355
0
        return NULL;
356
4
    if (!CRYPTO_NEW_REF(&ctx->refcnt, 1)) {
357
0
        OPENSSL_free(ctx);
358
0
        return NULL;
359
0
    }
360
4
    if (parent != NULL) {
361
3
        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
3
        parent_ctx = parent->algctx;
368
3
        parent_dispatch = parent->meth->dispatch;
369
3
    }
370
4
    if ((ctx->algctx = rand->newctx(ossl_provider_ctx(rand->prov), parent_ctx,
371
4
                                    parent_dispatch)) == NULL
372
4
            || !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
4
    ctx->meth = rand;
381
4
    ctx->parent = parent;
382
4
    return ctx;
383
4
}
384
385
void EVP_RAND_CTX_free(EVP_RAND_CTX *ctx)
386
10
{
387
10
    int ref = 0;
388
10
    EVP_RAND_CTX *parent;
389
390
10
    if (ctx == NULL)
391
3
        return;
392
393
7
    CRYPTO_DOWN_REF(&ctx->refcnt, &ref);
394
7
    if (ref > 0)
395
3
        return;
396
4
    parent = ctx->parent;
397
4
    ctx->meth->freectx(ctx->algctx);
398
4
    ctx->algctx = NULL;
399
4
    EVP_RAND_free(ctx->meth);
400
4
    CRYPTO_FREE_REF(&ctx->refcnt);
401
4
    OPENSSL_free(ctx);
402
4
    EVP_RAND_CTX_free(parent);
403
4
}
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
14.5k
{
413
14.5k
    return ctx->meth->get_ctx_params(ctx->algctx, params);
414
14.5k
}
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
3
{
485
3
    void *provctx;
486
487
3
    if (ctx->meth->settable_ctx_params == NULL)
488
3
        return NULL;
489
0
    provctx = ossl_provider_ctx(EVP_RAND_get0_provider(ctx->meth));
490
0
    return ctx->meth->settable_ctx_params(ctx->algctx, provctx);
491
3
}
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
4
{
517
4
    return ctx->meth->instantiate(ctx->algctx, strength, prediction_resistance,
518
4
                                  pstr, pstr_len, params);
519
4
}
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
4
{
526
4
    int res;
527
528
4
    if (!evp_rand_lock(ctx))
529
0
        return 0;
530
4
    res = evp_rand_instantiate_locked(ctx, strength, prediction_resistance,
531
4
                                      pstr, pstr_len, params);
532
4
    evp_rand_unlock(ctx);
533
4
    return res;
534
4
}
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
14.5k
{
558
14.5k
    size_t chunk, max_request = 0;
559
14.5k
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
560
561
14.5k
    params[0] = OSSL_PARAM_construct_size_t(OSSL_RAND_PARAM_MAX_REQUEST,
562
14.5k
                                            &max_request);
563
14.5k
    if (!evp_rand_get_ctx_params_locked(ctx, params)
564
14.5k
            || 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
29.1k
    for (; outlen > 0; outlen -= chunk, out += chunk) {
569
14.5k
        chunk = outlen > max_request ? max_request : outlen;
570
14.5k
        if (!ctx->meth->generate(ctx->algctx, out, chunk, strength,
571
14.5k
                                 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
14.5k
        prediction_resistance = 0;
580
14.5k
    }
581
14.5k
    return 1;
582
14.5k
}
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
14.5k
{
588
14.5k
    int res;
589
590
14.5k
    if (!evp_rand_lock(ctx))
591
0
        return 0;
592
14.5k
    res = evp_rand_generate_locked(ctx, out, outlen, strength,
593
14.5k
                                   prediction_resistance, addin, addin_len);
594
14.5k
    evp_rand_unlock(ctx);
595
14.5k
    return res;
596
14.5k
}
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 ctx->meth->nonce(ctx->algctx, out, str, outlen, outlen) > 0;
651
0
    return evp_rand_generate_locked(ctx, out, outlen, str, 0, NULL, 0);
652
0
}
653
654
int EVP_RAND_nonce(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen)
655
0
{
656
0
    int res;
657
658
0
    if (ctx == NULL || out == NULL || outlen == 0) {
659
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
660
0
        return 0;
661
0
    }
662
663
0
    if (!evp_rand_lock(ctx))
664
0
        return 0;
665
0
    res = evp_rand_nonce_locked(ctx, out, outlen);
666
0
    evp_rand_unlock(ctx);
667
0
    return res;
668
0
}
669
670
int EVP_RAND_get_state(EVP_RAND_CTX *ctx)
671
0
{
672
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
673
0
    int state;
674
675
0
    params[0] = OSSL_PARAM_construct_int(OSSL_RAND_PARAM_STATE, &state);
676
0
    if (!EVP_RAND_CTX_get_params(ctx, params))
677
0
        state = EVP_RAND_STATE_ERROR;
678
0
    return state;
679
0
}
680
681
static int evp_rand_verify_zeroization_locked(EVP_RAND_CTX *ctx)
682
0
{
683
0
    if (ctx->meth->verify_zeroization != NULL)
684
0
        return ctx->meth->verify_zeroization(ctx->algctx);
685
0
    return 0;
686
0
}
687
688
int EVP_RAND_verify_zeroization(EVP_RAND_CTX *ctx)
689
0
{
690
0
    int res;
691
692
0
    if (!evp_rand_lock(ctx))
693
0
        return 0;
694
0
    res = evp_rand_verify_zeroization_locked(ctx);
695
0
    evp_rand_unlock(ctx);
696
0
    return res;
697
0
}
698
699
int evp_rand_can_seed(EVP_RAND_CTX *ctx)
700
0
{
701
0
    return ctx->meth->get_seed != NULL;
702
0
}
703
704
static size_t evp_rand_get_seed_locked(EVP_RAND_CTX *ctx,
705
                                       unsigned char **buffer,
706
                                       int entropy,
707
                                       size_t min_len, size_t max_len,
708
                                       int prediction_resistance,
709
                                       const unsigned char *adin,
710
                                       size_t adin_len)
711
0
{
712
0
    if (ctx->meth->get_seed != NULL)
713
0
        return ctx->meth->get_seed(ctx->algctx, buffer,
714
0
                                   entropy, min_len, max_len,
715
0
                                   prediction_resistance,
716
0
                                   adin, adin_len);
717
0
    return 0;
718
0
}
719
720
size_t evp_rand_get_seed(EVP_RAND_CTX *ctx,
721
                         unsigned char **buffer,
722
                         int entropy, size_t min_len, size_t max_len,
723
                         int prediction_resistance,
724
                         const unsigned char *adin, size_t adin_len)
725
0
{
726
0
    int res;
727
728
0
    if (!evp_rand_lock(ctx))
729
0
        return 0;
730
0
    res = evp_rand_get_seed_locked(ctx,
731
0
                                   buffer,
732
0
                                   entropy, min_len, max_len,
733
0
                                   prediction_resistance,
734
0
                                   adin, adin_len);
735
0
    evp_rand_unlock(ctx);
736
0
    return res;
737
0
}
738
739
static void evp_rand_clear_seed_locked(EVP_RAND_CTX *ctx,
740
                                       unsigned char *buffer, size_t b_len)
741
0
{
742
0
    if (ctx->meth->clear_seed != NULL)
743
0
        ctx->meth->clear_seed(ctx->algctx, buffer, b_len);
744
0
}
745
746
void evp_rand_clear_seed(EVP_RAND_CTX *ctx,
747
                         unsigned char *buffer, size_t b_len)
748
0
{
749
0
    if (!evp_rand_lock(ctx))
750
0
        return;
751
0
    evp_rand_clear_seed_locked(ctx, buffer, b_len);
752
0
    evp_rand_unlock(ctx);
753
0
}