Coverage Report

Created: 2026-07-16 06:59

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
0
{
56
0
    EVP_RAND *rand = (EVP_RAND *)vrand;
57
0
    int ref = 0;
58
59
0
    if (rand != NULL)
60
0
        return CRYPTO_UP_REF(&rand->refcnt, &ref);
61
0
    return 1;
62
0
}
63
64
static void evp_rand_free(void *vrand)
65
0
{
66
0
    EVP_RAND *rand = (EVP_RAND *)vrand;
67
0
    int ref = 0;
68
69
0
    if (rand == NULL)
70
0
        return;
71
0
    CRYPTO_DOWN_REF(&rand->refcnt, &ref);
72
0
    if (ref > 0)
73
0
        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
0
{
82
0
    EVP_RAND *rand = OPENSSL_zalloc(sizeof(*rand));
83
84
0
    if (rand == NULL)
85
0
        return NULL;
86
87
0
    if (!CRYPTO_NEW_REF(&rand->refcnt, 1)) {
88
0
        OPENSSL_free(rand);
89
0
        return NULL;
90
0
    }
91
0
    return rand;
92
0
}
93
94
/* Enable locking of the underlying DRBG/RAND if available */
95
int EVP_RAND_enable_locking(EVP_RAND_CTX *rand)
96
0
{
97
0
    if (rand->meth->enable_locking != NULL)
98
0
        return rand->meth->enable_locking(rand->algctx);
99
0
    ERR_raise(ERR_LIB_EVP, EVP_R_LOCKING_NOT_SUPPORTED);
100
0
    return 0;
101
0
}
102
103
/* Lock the underlying DRBG/RAND if available */
104
static int evp_rand_lock(EVP_RAND_CTX *rand)
105
0
{
106
0
    if (rand->meth->lock != NULL)
107
0
        return rand->meth->lock(rand->algctx);
108
0
    return 1;
109
0
}
110
111
/* Unlock the underlying DRBG/RAND if available */
112
static void evp_rand_unlock(EVP_RAND_CTX *rand)
113
0
{
114
0
    if (rand->meth->unlock != NULL)
115
0
        rand->meth->unlock(rand->algctx);
116
0
}
117
118
static void *evp_rand_from_algorithm(int name_id,
119
    const OSSL_ALGORITHM *algodef,
120
    OSSL_PROVIDER *prov, int no_store)
121
0
{
122
0
    const OSSL_DISPATCH *fns = algodef->implementation;
123
0
    EVP_RAND *rand = NULL;
124
0
    int fnrandcnt = 0, fnctxcnt = 0, fnlockcnt = 0, fnenablelockcnt = 0;
125
#ifdef FIPS_MODULE
126
    int fnzeroizecnt = 0;
127
#endif
128
129
0
    if ((rand = evp_rand_new()) == NULL) {
130
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
131
0
        return NULL;
132
0
    }
133
0
    rand->name_id = name_id;
134
0
    rand->no_store = no_store;
135
0
    if ((rand->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
136
0
        evp_rand_free(rand);
137
0
        return NULL;
138
0
    }
139
0
    rand->description = algodef->algorithm_description;
140
0
    rand->dispatch = fns;
141
0
    for (; fns->function_id != 0; fns++) {
142
0
        switch (fns->function_id) {
143
0
        case OSSL_FUNC_RAND_NEWCTX:
144
0
            if (rand->newctx != NULL)
145
0
                break;
146
0
            rand->newctx = OSSL_FUNC_rand_newctx(fns);
147
0
            fnctxcnt++;
148
0
            break;
149
0
        case OSSL_FUNC_RAND_FREECTX:
150
0
            if (rand->freectx != NULL)
151
0
                break;
152
0
            rand->freectx = OSSL_FUNC_rand_freectx(fns);
153
0
            fnctxcnt++;
154
0
            break;
155
0
        case OSSL_FUNC_RAND_INSTANTIATE:
156
0
            if (rand->instantiate != NULL)
157
0
                break;
158
0
            rand->instantiate = OSSL_FUNC_rand_instantiate(fns);
159
0
            fnrandcnt++;
160
0
            break;
161
0
        case OSSL_FUNC_RAND_UNINSTANTIATE:
162
0
            if (rand->uninstantiate != NULL)
163
0
                break;
164
0
            rand->uninstantiate = OSSL_FUNC_rand_uninstantiate(fns);
165
0
            fnrandcnt++;
166
0
            break;
167
0
        case OSSL_FUNC_RAND_GENERATE:
168
0
            if (rand->generate != NULL)
169
0
                break;
170
0
            rand->generate = OSSL_FUNC_rand_generate(fns);
171
0
            fnrandcnt++;
172
0
            break;
173
0
        case OSSL_FUNC_RAND_RESEED:
174
0
            if (rand->reseed != NULL)
175
0
                break;
176
0
            rand->reseed = OSSL_FUNC_rand_reseed(fns);
177
0
            break;
178
0
        case OSSL_FUNC_RAND_NONCE:
179
0
            if (rand->nonce != NULL)
180
0
                break;
181
0
            rand->nonce = OSSL_FUNC_rand_nonce(fns);
182
0
            break;
183
0
        case OSSL_FUNC_RAND_ENABLE_LOCKING:
184
0
            if (rand->enable_locking != NULL)
185
0
                break;
186
0
            rand->enable_locking = OSSL_FUNC_rand_enable_locking(fns);
187
0
            fnenablelockcnt++;
188
0
            break;
189
0
        case OSSL_FUNC_RAND_LOCK:
190
0
            if (rand->lock != NULL)
191
0
                break;
192
0
            rand->lock = OSSL_FUNC_rand_lock(fns);
193
0
            fnlockcnt++;
194
0
            break;
195
0
        case OSSL_FUNC_RAND_UNLOCK:
196
0
            if (rand->unlock != NULL)
197
0
                break;
198
0
            rand->unlock = OSSL_FUNC_rand_unlock(fns);
199
0
            fnlockcnt++;
200
0
            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
0
        case OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS:
207
0
            if (rand->gettable_ctx_params != NULL)
208
0
                break;
209
0
            rand->gettable_ctx_params = OSSL_FUNC_rand_gettable_ctx_params(fns);
210
0
            break;
211
0
        case OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS:
212
0
            if (rand->settable_ctx_params != NULL)
213
0
                break;
214
0
            rand->settable_ctx_params = OSSL_FUNC_rand_settable_ctx_params(fns);
215
0
            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
0
        case OSSL_FUNC_RAND_GET_CTX_PARAMS:
222
0
            if (rand->get_ctx_params != NULL)
223
0
                break;
224
0
            rand->get_ctx_params = OSSL_FUNC_rand_get_ctx_params(fns);
225
0
            fnctxcnt++;
226
0
            break;
227
0
        case OSSL_FUNC_RAND_SET_CTX_PARAMS:
228
0
            if (rand->set_ctx_params != NULL)
229
0
                break;
230
0
            rand->set_ctx_params = OSSL_FUNC_rand_set_ctx_params(fns);
231
0
            break;
232
0
        case OSSL_FUNC_RAND_VERIFY_ZEROIZATION:
233
0
            if (rand->verify_zeroization != NULL)
234
0
                break;
235
0
            rand->verify_zeroization = OSSL_FUNC_rand_verify_zeroization(fns);
236
#ifdef FIPS_MODULE
237
            fnzeroizecnt++;
238
#endif
239
0
            break;
240
0
        case OSSL_FUNC_RAND_GET_SEED:
241
0
            if (rand->get_seed != NULL)
242
0
                break;
243
0
            rand->get_seed = OSSL_FUNC_rand_get_seed(fns);
244
0
            break;
245
0
        case OSSL_FUNC_RAND_CLEAR_SEED:
246
0
            if (rand->clear_seed != NULL)
247
0
                break;
248
0
            rand->clear_seed = OSSL_FUNC_rand_clear_seed(fns);
249
0
            break;
250
0
        }
251
0
    }
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
0
    if (fnrandcnt != 3
262
0
        || fnctxcnt != 3
263
0
        || (fnenablelockcnt != 0 && fnenablelockcnt != 1)
264
0
        || (fnlockcnt != 0 && fnlockcnt != 2)
265
#ifdef FIPS_MODULE
266
        || fnzeroizecnt != 1
267
#endif
268
0
    ) {
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
0
    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
0
    rand->prov = prov;
280
281
0
    return rand;
282
0
}
283
284
EVP_RAND *EVP_RAND_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
285
    const char *properties)
286
0
{
287
0
    return evp_generic_fetch(libctx, OSSL_OP_RAND, algorithm, properties,
288
0
        evp_rand_from_algorithm, evp_rand_up_ref,
289
0
        evp_rand_free);
290
0
}
291
292
int EVP_RAND_up_ref(EVP_RAND *rand)
293
0
{
294
#ifdef OPENSSL_NO_CACHED_FETCH
295
    return evp_rand_up_ref(rand);
296
#else
297
0
    if (rand->no_store != 0)
298
0
        return evp_rand_up_ref(rand);
299
0
    return 1;
300
0
#endif
301
0
}
302
303
void EVP_RAND_free(EVP_RAND *rand)
304
0
{
305
#ifdef OPENSSL_NO_CACHED_FETCH
306
    evp_rand_free(rand);
307
#else
308
0
    if (rand != NULL && (rand->no_store != 0))
309
0
        evp_rand_free(rand);
310
0
#endif
311
0
}
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
0
{
335
0
    return rand->prov;
336
0
}
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
0
{
347
0
    int ref = 0;
348
349
0
    return CRYPTO_UP_REF(&ctx->refcnt, &ref);
350
0
}
351
352
EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, EVP_RAND_CTX *parent)
353
0
{
354
0
    EVP_RAND_CTX *ctx;
355
0
    void *parent_ctx = NULL;
356
0
    const OSSL_DISPATCH *parent_dispatch = NULL;
357
358
0
    if (rand == NULL) {
359
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
360
0
        return NULL;
361
0
    }
362
363
0
    ctx = OPENSSL_zalloc(sizeof(*ctx));
364
0
    if (ctx == NULL)
365
0
        return NULL;
366
0
    if (!CRYPTO_NEW_REF(&ctx->refcnt, 1)) {
367
0
        OPENSSL_free(ctx);
368
0
        return NULL;
369
0
    }
370
0
    if (parent != NULL) {
371
0
        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
0
        parent_ctx = parent->algctx;
378
0
        parent_dispatch = parent->meth->dispatch;
379
0
    }
380
0
    if ((ctx->algctx = rand->newctx(ossl_provider_ctx(rand->prov), parent_ctx,
381
0
             parent_dispatch))
382
0
            == NULL
383
0
        || !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
0
    ctx->meth = rand;
392
0
    ctx->parent = parent;
393
0
    return ctx;
394
0
}
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
0
{
424
0
    return ctx->meth->get_ctx_params(ctx->algctx, params);
425
0
}
426
427
int EVP_RAND_CTX_get_params(EVP_RAND_CTX *ctx, OSSL_PARAM params[])
428
0
{
429
0
    int res;
430
431
0
    if (!evp_rand_lock(ctx))
432
0
        return 0;
433
0
    res = evp_rand_get_ctx_params_locked(ctx, params);
434
0
    evp_rand_unlock(ctx);
435
0
    return res;
436
0
}
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
0
{
496
0
    void *provctx;
497
498
0
    if (ctx->meth->settable_ctx_params == NULL)
499
0
        return NULL;
500
0
    provctx = ossl_provider_ctx(EVP_RAND_get0_provider(ctx->meth));
501
0
    return ctx->meth->settable_ctx_params(ctx->algctx, provctx);
502
0
}
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
0
{
531
0
    return ctx->meth->instantiate(ctx->algctx, strength, prediction_resistance,
532
0
        pstr, pstr_len, params);
533
0
}
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
0
{
540
0
    int res;
541
542
0
    if (!evp_rand_lock(ctx))
543
0
        return 0;
544
0
    res = evp_rand_instantiate_locked(ctx, strength, prediction_resistance,
545
0
        pstr, pstr_len, params);
546
0
    evp_rand_unlock(ctx);
547
0
    return res;
548
0
}
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
0
{
572
0
    size_t chunk, max_request = 0;
573
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
574
575
0
    params[0] = OSSL_PARAM_construct_size_t(OSSL_RAND_PARAM_MAX_REQUEST,
576
0
        &max_request);
577
0
    if (!evp_rand_get_ctx_params_locked(ctx, params)
578
0
        || 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
0
    for (; outlen > 0; outlen -= chunk, out += chunk) {
583
0
        chunk = outlen > max_request ? max_request : outlen;
584
0
        if (!ctx->meth->generate(ctx->algctx, out, chunk, strength,
585
0
                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
0
        prediction_resistance = 0;
594
0
    }
595
0
    return 1;
596
0
}
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
0
{
602
0
    int res;
603
604
0
    if (!evp_rand_lock(ctx))
605
0
        return 0;
606
0
    res = evp_rand_generate_locked(ctx, out, outlen, strength,
607
0
        prediction_resistance, addin, addin_len);
608
0
    evp_rand_unlock(ctx);
609
0
    return res;
610
0
}
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
0
{
616
0
    if (ctx->meth->reseed != NULL)
617
0
        return ctx->meth->reseed(ctx->algctx, prediction_resistance,
618
0
            ent, ent_len, addin, addin_len);
619
0
    return 1;
620
0
}
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
0
{
626
0
    int res;
627
628
0
    if (!evp_rand_lock(ctx))
629
0
        return 0;
630
0
    res = evp_rand_reseed_locked(ctx, prediction_resistance,
631
0
        ent, ent_len, addin, addin_len);
632
0
    evp_rand_unlock(ctx);
633
0
    return res;
634
0
}
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
0
{
686
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
687
0
    int state;
688
689
0
    params[0] = OSSL_PARAM_construct_int(OSSL_RAND_PARAM_STATE, &state);
690
0
    if (!EVP_RAND_CTX_get_params(ctx, params))
691
0
        state = EVP_RAND_STATE_ERROR;
692
0
    return state;
693
0
}
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
}