Coverage Report

Created: 2026-07-19 06:36

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