Coverage Report

Created: 2026-02-14 07:20

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