Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/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.24k
{
55
2.24k
    EVP_RAND *rand = (EVP_RAND *)vrand;
56
2.24k
    int ref = 0;
57
58
2.24k
    if (rand != NULL)
59
2.24k
        return CRYPTO_UP_REF(&rand->refcnt, &ref);
60
0
    return 1;
61
2.24k
}
62
63
static void evp_rand_free(void *vrand)
64
2.46k
{
65
2.46k
    EVP_RAND *rand = (EVP_RAND *)vrand;
66
2.46k
    int ref = 0;
67
68
2.46k
    if (rand == NULL)
69
0
        return;
70
2.46k
    CRYPTO_DOWN_REF(&rand->refcnt, &ref);
71
2.46k
    if (ref > 0)
72
2.11k
        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
446
{
81
446
    EVP_RAND *rand = OPENSSL_zalloc(sizeof(*rand));
82
83
446
    if (rand == NULL)
84
0
        return NULL;
85
86
446
    if (!CRYPTO_NEW_REF(&rand->refcnt, 1)) {
87
0
        OPENSSL_free(rand);
88
0
        return NULL;
89
0
    }
90
446
    return rand;
91
446
}
92
93
/* Enable locking of the underlying DRBG/RAND if available */
94
int EVP_RAND_enable_locking(EVP_RAND_CTX *rand)
95
78
{
96
78
    if (rand->meth->enable_locking != NULL)
97
78
        return rand->meth->enable_locking(rand->algctx);
98
78
    ERR_raise(ERR_LIB_EVP, EVP_R_LOCKING_NOT_SUPPORTED);
99
0
    return 0;
100
78
}
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
117k
        return rand->meth->lock(rand->algctx);
107
2.03M
    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
117k
        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
446
{
121
446
    const OSSL_DISPATCH *fns = algodef->implementation;
122
446
    EVP_RAND *rand = NULL;
123
446
    int fnrandcnt = 0, fnctxcnt = 0, fnlockcnt = 0, fnenablelockcnt = 0;
124
#ifdef FIPS_MODULE
125
    int fnzeroizecnt = 0;
126
#endif
127
128
446
    if ((rand = evp_rand_new()) == NULL) {
129
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
130
0
        return NULL;
131
0
    }
132
446
    rand->name_id = name_id;
133
446
    if ((rand->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
134
0
        evp_rand_free(rand);
135
0
        return NULL;
136
0
    }
137
446
    rand->description = algodef->algorithm_description;
138
446
    rand->dispatch = fns;
139
6.97k
    for (; fns->function_id != 0; fns++) {
140
6.53k
        switch (fns->function_id) {
141
446
        case OSSL_FUNC_RAND_NEWCTX:
142
446
            if (rand->newctx != NULL)
143
0
                break;
144
446
            rand->newctx = OSSL_FUNC_rand_newctx(fns);
145
446
            fnctxcnt++;
146
446
            break;
147
446
        case OSSL_FUNC_RAND_FREECTX:
148
446
            if (rand->freectx != NULL)
149
0
                break;
150
446
            rand->freectx = OSSL_FUNC_rand_freectx(fns);
151
446
            fnctxcnt++;
152
446
            break;
153
446
        case OSSL_FUNC_RAND_INSTANTIATE:
154
446
            if (rand->instantiate != NULL)
155
0
                break;
156
446
            rand->instantiate = OSSL_FUNC_rand_instantiate(fns);
157
446
            fnrandcnt++;
158
446
            break;
159
446
        case OSSL_FUNC_RAND_UNINSTANTIATE:
160
446
            if (rand->uninstantiate != NULL)
161
0
                break;
162
446
            rand->uninstantiate = OSSL_FUNC_rand_uninstantiate(fns);
163
446
            fnrandcnt++;
164
446
            break;
165
446
        case OSSL_FUNC_RAND_GENERATE:
166
446
            if (rand->generate != NULL)
167
0
                break;
168
446
            rand->generate = OSSL_FUNC_rand_generate(fns);
169
446
            fnrandcnt++;
170
446
            break;
171
390
        case OSSL_FUNC_RAND_RESEED:
172
390
            if (rand->reseed != NULL)
173
0
                break;
174
390
            rand->reseed = OSSL_FUNC_rand_reseed(fns);
175
390
            break;
176
78
        case OSSL_FUNC_RAND_NONCE:
177
78
            if (rand->nonce != NULL)
178
0
                break;
179
78
            rand->nonce = OSSL_FUNC_rand_nonce(fns);
180
78
            break;
181
446
        case OSSL_FUNC_RAND_ENABLE_LOCKING:
182
446
            if (rand->enable_locking != NULL)
183
0
                break;
184
446
            rand->enable_locking = OSSL_FUNC_rand_enable_locking(fns);
185
446
            fnenablelockcnt++;
186
446
            break;
187
390
        case OSSL_FUNC_RAND_LOCK:
188
390
            if (rand->lock != NULL)
189
0
                break;
190
390
            rand->lock = OSSL_FUNC_rand_lock(fns);
191
390
            fnlockcnt++;
192
390
            break;
193
390
        case OSSL_FUNC_RAND_UNLOCK:
194
390
            if (rand->unlock != NULL)
195
0
                break;
196
390
            rand->unlock = OSSL_FUNC_rand_unlock(fns);
197
390
            fnlockcnt++;
198
390
            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
446
        case OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS:
205
446
            if (rand->gettable_ctx_params != NULL)
206
0
                break;
207
446
            rand->gettable_ctx_params = OSSL_FUNC_rand_gettable_ctx_params(fns);
208
446
            break;
209
312
        case OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS:
210
312
            if (rand->settable_ctx_params != NULL)
211
0
                break;
212
312
            rand->settable_ctx_params = OSSL_FUNC_rand_settable_ctx_params(fns);
213
312
            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
446
        case OSSL_FUNC_RAND_GET_CTX_PARAMS:
220
446
            if (rand->get_ctx_params != NULL)
221
0
                break;
222
446
            rand->get_ctx_params = OSSL_FUNC_rand_get_ctx_params(fns);
223
446
            fnctxcnt++;
224
446
            break;
225
312
        case OSSL_FUNC_RAND_SET_CTX_PARAMS:
226
312
            if (rand->set_ctx_params != NULL)
227
0
                break;
228
312
            rand->set_ctx_params = OSSL_FUNC_rand_set_ctx_params(fns);
229
312
            break;
230
390
        case OSSL_FUNC_RAND_VERIFY_ZEROIZATION:
231
390
            if (rand->verify_zeroization != NULL)
232
0
                break;
233
390
            rand->verify_zeroization = OSSL_FUNC_rand_verify_zeroization(fns);
234
#ifdef FIPS_MODULE
235
            fnzeroizecnt++;
236
#endif
237
390
            break;
238
390
        case OSSL_FUNC_RAND_GET_SEED:
239
390
            if (rand->get_seed != NULL)
240
0
                break;
241
390
            rand->get_seed = OSSL_FUNC_rand_get_seed(fns);
242
390
            break;
243
312
        case OSSL_FUNC_RAND_CLEAR_SEED:
244
312
            if (rand->clear_seed != NULL)
245
0
                break;
246
312
            rand->clear_seed = OSSL_FUNC_rand_clear_seed(fns);
247
312
            break;
248
6.53k
        }
249
6.53k
    }
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
446
    if (fnrandcnt != 3
260
446
        || fnctxcnt != 3
261
446
        || (fnenablelockcnt != 0 && fnenablelockcnt != 1)
262
446
        || (fnlockcnt != 0 && fnlockcnt != 2)
263
#ifdef FIPS_MODULE
264
        || fnzeroizecnt != 1
265
#endif
266
446
    ) {
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
446
    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
446
    rand->prov = prov;
278
279
446
    return rand;
280
446
}
281
282
EVP_RAND *EVP_RAND_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
283
    const char *properties)
284
279
{
285
279
    return evp_generic_fetch(libctx, OSSL_OP_RAND, algorithm, properties,
286
279
        evp_rand_from_algorithm, evp_rand_up_ref,
287
279
        evp_rand_free);
288
279
}
289
290
int EVP_RAND_up_ref(EVP_RAND *rand)
291
1.33k
{
292
1.33k
    return evp_rand_up_ref(rand);
293
1.33k
}
294
295
void EVP_RAND_free(EVP_RAND *rand)
296
1.50k
{
297
1.50k
    evp_rand_free(rand);
298
1.50k
}
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.19k
{
322
1.19k
    return rand->prov;
323
1.19k
}
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
186
{
334
186
    int ref = 0;
335
336
186
    return CRYPTO_UP_REF(&ctx->refcnt, &ref);
337
186
}
338
339
EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, EVP_RAND_CTX *parent)
340
1.27k
{
341
1.27k
    EVP_RAND_CTX *ctx;
342
1.27k
    void *parent_ctx = NULL;
343
1.27k
    const OSSL_DISPATCH *parent_dispatch = NULL;
344
345
1.27k
    if (rand == NULL) {
346
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
347
0
        return NULL;
348
0
    }
349
350
1.27k
    ctx = OPENSSL_zalloc(sizeof(*ctx));
351
1.27k
    if (ctx == NULL)
352
0
        return NULL;
353
1.27k
    if (!CRYPTO_NEW_REF(&ctx->refcnt, 1)) {
354
0
        OPENSSL_free(ctx);
355
0
        return NULL;
356
0
    }
357
1.27k
    if (parent != NULL) {
358
186
        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
186
        parent_ctx = parent->algctx;
365
186
        parent_dispatch = parent->meth->dispatch;
366
186
    }
367
1.27k
    if ((ctx->algctx = rand->newctx(ossl_provider_ctx(rand->prov), parent_ctx,
368
1.27k
             parent_dispatch))
369
1.27k
            == NULL
370
1.27k
        || !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.27k
    ctx->meth = rand;
379
1.27k
    ctx->parent = parent;
380
1.27k
    return ctx;
381
1.27k
}
382
383
void EVP_RAND_CTX_free(EVP_RAND_CTX *ctx)
384
2.77k
{
385
2.77k
    int ref = 0;
386
2.77k
    EVP_RAND_CTX *parent;
387
388
2.77k
    if (ctx == NULL)
389
1.39k
        return;
390
391
1.38k
    CRYPTO_DOWN_REF(&ctx->refcnt, &ref);
392
1.38k
    if (ref > 0)
393
156
        return;
394
1.22k
    parent = ctx->parent;
395
1.22k
    ctx->meth->freectx(ctx->algctx);
396
1.22k
    ctx->algctx = NULL;
397
1.22k
    EVP_RAND_free(ctx->meth);
398
1.22k
    CRYPTO_FREE_REF(&ctx->refcnt);
399
1.22k
    OPENSSL_free(ctx);
400
1.22k
    EVP_RAND_CTX_free(parent);
401
1.22k
}
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.14M
{
411
2.14M
    return ctx->meth->get_ctx_params(ctx->algctx, params);
412
2.14M
}
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.01k
{
428
1.01k
    if (ctx->meth->set_ctx_params != NULL)
429
1.00k
        return ctx->meth->set_ctx_params(ctx->algctx, params);
430
6
    return 1;
431
1.01k
}
432
433
int EVP_RAND_CTX_set_params(EVP_RAND_CTX *ctx, const OSSL_PARAM params[])
434
1.01k
{
435
1.01k
    int res;
436
437
1.01k
    if (!evp_rand_lock(ctx))
438
0
        return 0;
439
1.01k
    res = evp_rand_set_ctx_params_locked(ctx, params);
440
1.01k
    evp_rand_unlock(ctx);
441
1.01k
    return res;
442
1.01k
}
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.01k
{
463
1.01k
    void *provctx;
464
465
1.01k
    if (rand->settable_ctx_params == NULL)
466
6
        return NULL;
467
1.00k
    provctx = ossl_provider_ctx(EVP_RAND_get0_provider(rand));
468
1.00k
    return rand->settable_ctx_params(NULL, provctx);
469
1.01k
}
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
186
{
483
186
    void *provctx;
484
485
186
    if (ctx->meth->settable_ctx_params == NULL)
486
145
        return NULL;
487
41
    provctx = ossl_provider_ctx(EVP_RAND_get0_provider(ctx->meth));
488
41
    return ctx->meth->settable_ctx_params(ctx->algctx, provctx);
489
186
}
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
279
{
514
279
    return ctx->meth->instantiate(ctx->algctx, strength, prediction_resistance,
515
279
        pstr, pstr_len, params);
516
279
}
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
279
{
523
279
    int res;
524
525
279
    if (!evp_rand_lock(ctx))
526
0
        return 0;
527
279
    res = evp_rand_instantiate_locked(ctx, strength, prediction_resistance,
528
279
        pstr, pstr_len, params);
529
279
    evp_rand_unlock(ctx);
530
279
    return res;
531
279
}
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.14M
{
555
2.14M
    size_t chunk, max_request = 0;
556
2.14M
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
557
558
2.14M
    params[0] = OSSL_PARAM_construct_size_t(OSSL_RAND_PARAM_MAX_REQUEST,
559
2.14M
        &max_request);
560
2.14M
    if (!evp_rand_get_ctx_params_locked(ctx, params)
561
2.14M
        || max_request == 0) {
562
38
        ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_GET_MAXIMUM_REQUEST_SIZE);
563
38
        return 0;
564
38
    }
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
151
            ERR_raise(ERR_LIB_EVP, EVP_R_GENERATE_ERROR);
570
151
            return 0;
571
151
        }
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.14M
    return 1;
579
2.14M
}
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.14M
{
585
2.14M
    int res;
586
587
2.14M
    if (!evp_rand_lock(ctx))
588
0
        return 0;
589
2.14M
    res = evp_rand_generate_locked(ctx, out, outlen, strength,
590
2.14M
        prediction_resistance, addin, addin_len);
591
2.14M
    evp_rand_unlock(ctx);
592
2.14M
    return res;
593
2.14M
}
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
524
{
599
524
    if (ctx->meth->reseed != NULL)
600
524
        return ctx->meth->reseed(ctx->algctx, prediction_resistance,
601
524
            ent, ent_len, addin, addin_len);
602
0
    return 1;
603
524
}
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
524
{
609
524
    int res;
610
611
524
    if (!evp_rand_lock(ctx))
612
0
        return 0;
613
524
    res = evp_rand_reseed_locked(ctx, prediction_resistance,
614
524
        ent, ent_len, addin, addin_len);
615
524
    evp_rand_unlock(ctx);
616
524
    return res;
617
524
}
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
288
{
698
288
    return ctx->meth->get_seed != NULL;
699
288
}
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
144
{
709
144
    if (ctx->meth->get_seed != NULL)
710
144
        return ctx->meth->get_seed(ctx->algctx, buffer,
711
144
            entropy, min_len, max_len,
712
144
            prediction_resistance,
713
144
            adin, adin_len);
714
0
    return 0;
715
144
}
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
144
{
723
144
    int res;
724
725
144
    if (!evp_rand_lock(ctx))
726
0
        return 0;
727
144
    res = evp_rand_get_seed_locked(ctx,
728
144
        buffer,
729
144
        entropy, min_len, max_len,
730
144
        prediction_resistance,
731
144
        adin, adin_len);
732
144
    evp_rand_unlock(ctx);
733
144
    return res;
734
144
}
735
736
static void evp_rand_clear_seed_locked(EVP_RAND_CTX *ctx,
737
    unsigned char *buffer, size_t b_len)
738
144
{
739
144
    if (ctx->meth->clear_seed != NULL)
740
144
        ctx->meth->clear_seed(ctx->algctx, buffer, b_len);
741
144
}
742
743
void evp_rand_clear_seed(EVP_RAND_CTX *ctx,
744
    unsigned char *buffer, size_t b_len)
745
144
{
746
144
    if (!evp_rand_lock(ctx))
747
0
        return;
748
144
    evp_rand_clear_seed_locked(ctx, buffer, b_len);
749
144
    evp_rand_unlock(ctx);
750
144
}