Coverage Report

Created: 2025-12-31 06:58

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