Coverage Report

Created: 2025-12-10 06:24

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