Coverage Report

Created: 2023-06-08 06:41

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