Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl40/crypto/evp/evp_enc.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2026 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 <limits.h>
12
#include <assert.h>
13
#include <openssl/evp.h>
14
#include <openssl/err.h>
15
#include <openssl/rand.h>
16
#include <openssl/params.h>
17
#include <openssl/core_names.h>
18
#include "internal/cryptlib.h"
19
#include "internal/provider.h"
20
#include "internal/core.h"
21
#include "internal/common.h"
22
#include "internal/safe_math.h"
23
#include "crypto/evp.h"
24
#include "evp_local.h"
25
26
OSSL_SAFE_MATH_SIGNED(int, int)
27
28
int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
29
370k
{
30
370k
    if (ctx == NULL)
31
0
        return 1;
32
33
370k
    if (ctx->cipher == NULL || ctx->cipher->prov == NULL)
34
7.04k
        return 1;
35
36
363k
    if (ctx->algctx != NULL) {
37
363k
        if (ctx->cipher->freectx != NULL)
38
363k
            ctx->cipher->freectx(ctx->algctx);
39
363k
        ctx->algctx = NULL;
40
363k
    }
41
363k
    if (ctx->fetched_cipher != NULL)
42
363k
        EVP_CIPHER_free(ctx->fetched_cipher);
43
363k
    memset(ctx, 0, sizeof(*ctx));
44
363k
    ctx->iv_len = -1;
45
46
363k
    return 1;
47
370k
}
48
49
EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
50
970k
{
51
970k
    EVP_CIPHER_CTX *ctx;
52
53
970k
    ctx = OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
54
970k
    if (ctx == NULL)
55
0
        return NULL;
56
57
970k
    ctx->iv_len = -1;
58
970k
    return ctx;
59
970k
}
60
61
void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
62
1.77M
{
63
1.77M
    if (ctx == NULL)
64
804k
        return;
65
970k
    EVP_CIPHER_CTX_reset(ctx);
66
970k
    OPENSSL_free(ctx);
67
970k
}
68
69
static int evp_cipher_init_internal(EVP_CIPHER_CTX *ctx,
70
    const EVP_CIPHER *cipher,
71
    const unsigned char *key,
72
    const unsigned char *iv, int enc,
73
    uint8_t is_pipeline,
74
    const OSSL_PARAM params[])
75
1.70M
{
76
    /*
77
     * enc == 1 means we are encrypting.
78
     * enc == 0 means we are decrypting.
79
     * enc == -1 means, use the previously initialised value for encrypt/decrypt
80
     */
81
1.70M
    if (enc == -1) {
82
203k
        enc = ctx->encrypt;
83
1.49M
    } else {
84
1.49M
        if (enc)
85
1.16M
            enc = 1;
86
1.49M
        ctx->encrypt = enc;
87
1.49M
    }
88
89
1.70M
    if (cipher == NULL && ctx->cipher == NULL) {
90
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
91
0
        return 0;
92
0
    }
93
94
    /* Ensure a context left lying around from last time is cleared */
95
1.70M
    if (cipher != NULL && ctx->cipher != NULL) {
96
9
        unsigned long flags = ctx->flags;
97
98
9
        EVP_CIPHER_CTX_reset(ctx);
99
        /* Restore encrypt and flags */
100
9
        ctx->encrypt = enc;
101
9
        ctx->flags = flags;
102
9
    }
103
104
1.70M
    if (cipher == NULL)
105
1.33M
        cipher = ctx->cipher;
106
107
1.70M
    if (cipher->prov == NULL) {
108
#ifdef FIPS_MODULE
109
        /* We only do explicit fetches inside the FIPS module */
110
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
111
        return 0;
112
#else
113
0
        EVP_CIPHER *provciph = EVP_CIPHER_fetch(NULL,
114
0
            cipher->nid == NID_undef ? "NULL"
115
0
                                     : OBJ_nid2sn(cipher->nid),
116
0
            "");
117
118
0
        if (provciph == NULL)
119
0
            return 0;
120
0
        cipher = provciph;
121
0
        EVP_CIPHER_free(ctx->fetched_cipher);
122
0
        ctx->fetched_cipher = provciph;
123
0
#endif
124
0
    }
125
126
1.70M
    if (!ossl_assert(cipher->prov != NULL)) {
127
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
128
0
        return 0;
129
0
    }
130
131
1.70M
    if (cipher != ctx->fetched_cipher) {
132
363k
        if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) {
133
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
134
0
            return 0;
135
0
        }
136
363k
        EVP_CIPHER_free(ctx->fetched_cipher);
137
        /* Coverity false positive, the reference counting is confusing it */
138
        /* coverity[use_after_free] */
139
363k
        ctx->fetched_cipher = (EVP_CIPHER *)cipher;
140
363k
    }
141
1.70M
    ctx->cipher = cipher;
142
143
1.70M
    if (is_pipeline && !EVP_CIPHER_can_pipeline(cipher, enc)) {
144
0
        ERR_raise(ERR_LIB_EVP, EVP_R_PIPELINE_NOT_SUPPORTED);
145
0
        return 0;
146
0
    }
147
148
1.70M
    if (ctx->algctx == NULL) {
149
363k
        ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
150
363k
        if (ctx->algctx == NULL) {
151
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
152
0
            return 0;
153
0
        }
154
363k
    }
155
156
1.70M
    if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
157
        /*
158
         * If this ctx was already set up for no padding then we need to tell
159
         * the new cipher about it.
160
         */
161
0
        if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
162
0
            return 0;
163
0
    }
164
165
1.70M
#ifndef FIPS_MODULE
166
    /*
167
     * Fix for CVE-2023-5363
168
     * Passing in a size as part of the init call takes effect late
169
     * so, force such to occur before the initialisation.
170
     *
171
     * The FIPS provider's internal library context is used in a manner
172
     * such that this is not an issue.
173
     */
174
1.70M
    if (params != NULL) {
175
0
        OSSL_PARAM param_lens[3] = { OSSL_PARAM_END, OSSL_PARAM_END,
176
0
            OSSL_PARAM_END };
177
0
        OSSL_PARAM *q = param_lens;
178
0
        const OSSL_PARAM *p;
179
180
0
        p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
181
0
        if (p != NULL)
182
0
            memcpy(q++, p, sizeof(*q));
183
184
        /*
185
         * Note that OSSL_CIPHER_PARAM_AEAD_IVLEN is a synonym for
186
         * OSSL_CIPHER_PARAM_IVLEN so both are covered here.
187
         */
188
0
        p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
189
0
        if (p != NULL)
190
0
            memcpy(q++, p, sizeof(*q));
191
192
0
        if (q != param_lens) {
193
0
            if (!EVP_CIPHER_CTX_set_params(ctx, param_lens)) {
194
0
                ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
195
0
                return 0;
196
0
            }
197
0
        }
198
0
    }
199
1.70M
#endif
200
201
1.70M
    if (is_pipeline)
202
0
        return 1;
203
204
1.70M
    if (enc) {
205
1.37M
        if (ctx->cipher->einit == NULL) {
206
            /*
207
             * We still should be able to set the IV using the new API
208
             * if the key is not specified and old API is not available
209
             */
210
0
            if (key == NULL && ctx->cipher->einit_skey != NULL) {
211
0
                return ctx->cipher->einit_skey(ctx->algctx, NULL,
212
0
                    iv,
213
0
                    iv == NULL ? 0
214
0
                               : EVP_CIPHER_CTX_get_iv_length(ctx),
215
0
                    params);
216
0
            }
217
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
218
0
            return 0;
219
0
        }
220
221
1.37M
        return ctx->cipher->einit(ctx->algctx,
222
1.37M
            key,
223
1.37M
            key == NULL ? 0
224
1.37M
                        : EVP_CIPHER_CTX_get_key_length(ctx),
225
1.37M
            iv,
226
1.37M
            iv == NULL ? 0
227
1.37M
                       : EVP_CIPHER_CTX_get_iv_length(ctx),
228
1.37M
            params);
229
1.37M
    }
230
231
329k
    if (ctx->cipher->dinit == NULL) {
232
        /*
233
         * We still should be able to set the IV using the new API
234
         * if the key is not specified and old API is not available
235
         */
236
0
        if (key == NULL && ctx->cipher->dinit_skey != NULL) {
237
0
            return ctx->cipher->dinit_skey(ctx->algctx, NULL,
238
0
                iv,
239
0
                iv == NULL ? 0
240
0
                           : EVP_CIPHER_CTX_get_iv_length(ctx),
241
0
                params);
242
0
        }
243
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
244
0
        return 0;
245
0
    }
246
247
329k
    return ctx->cipher->dinit(ctx->algctx,
248
329k
        key,
249
329k
        key == NULL ? 0
250
329k
                    : EVP_CIPHER_CTX_get_key_length(ctx),
251
329k
        iv,
252
329k
        iv == NULL ? 0
253
329k
                   : EVP_CIPHER_CTX_get_iv_length(ctx),
254
329k
        params);
255
329k
}
256
257
/*
258
 * This function is basically evp_cipher_init_internal without ENGINE support.
259
 * They should be combined when engines are not supported any longer.
260
 */
261
static int evp_cipher_init_skey_internal(EVP_CIPHER_CTX *ctx,
262
    const EVP_CIPHER *cipher,
263
    const EVP_SKEY *skey,
264
    const unsigned char *iv, size_t iv_len,
265
    int enc, const OSSL_PARAM params[])
266
0
{
267
0
    int ret;
268
269
    /*
270
     * enc == 1 means we are encrypting.
271
     * enc == 0 means we are decrypting.
272
     * enc == -1 means, use the previously initialised value for encrypt/decrypt
273
     */
274
0
    if (enc == -1)
275
0
        enc = ctx->encrypt;
276
0
    else
277
0
        ctx->encrypt = enc != 0;
278
279
0
    if (cipher == NULL && ctx->cipher == NULL) {
280
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
281
0
        return 0;
282
0
    }
283
284
    /* Ensure a context left lying around from last time is cleared */
285
0
    if (cipher != NULL && ctx->cipher != NULL) {
286
0
        unsigned long flags = ctx->flags;
287
288
0
        EVP_CIPHER_CTX_reset(ctx);
289
        /* Restore encrypt and flags */
290
0
        ctx->encrypt = enc;
291
0
        ctx->flags = flags;
292
0
    }
293
294
0
    if (cipher == NULL)
295
0
        cipher = ctx->cipher;
296
297
0
    if (cipher->prov == NULL) {
298
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
299
0
        return 0;
300
0
    }
301
302
0
    if (cipher != ctx->fetched_cipher) {
303
0
        if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) {
304
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
305
0
            return 0;
306
0
        }
307
0
        EVP_CIPHER_free(ctx->fetched_cipher);
308
        /* Coverity false positive, the reference counting is confusing it */
309
        /* coverity[use_after_free] */
310
0
        ctx->fetched_cipher = (EVP_CIPHER *)cipher;
311
0
    }
312
0
    ctx->cipher = cipher;
313
0
    if (ctx->algctx == NULL) {
314
0
        ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
315
0
        if (ctx->algctx == NULL) {
316
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
317
0
            return 0;
318
0
        }
319
0
    }
320
321
0
    if (skey != NULL && ctx->cipher->prov != skey->skeymgmt->prov) {
322
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
323
0
        return 0;
324
0
    }
325
326
0
    if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
327
        /*
328
         * If this ctx was already set up for no padding then we need to tell
329
         * the new cipher about it.
330
         */
331
0
        if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
332
0
            return 0;
333
0
    }
334
335
0
    if (iv == NULL)
336
0
        iv_len = 0;
337
338
    /* We have a data managed via key management, using the new callbacks */
339
0
    if (enc) {
340
0
        if (ctx->cipher->einit_skey == NULL) {
341
            /*
342
             *  When skey is NULL, it's a multiple-step init as the current API does.
343
             *  Otherwise we try to fallback for providers that do not support SKEYs.
344
             */
345
0
            const unsigned char *keydata = NULL;
346
0
            size_t keylen = 0;
347
348
0
            if (skey != NULL && !EVP_SKEY_get0_raw_key(skey, &keydata, &keylen)) {
349
0
                ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
350
0
                return 0;
351
0
            }
352
353
0
            ret = ctx->cipher->einit(ctx->algctx, keydata, keylen,
354
0
                iv, iv_len, params);
355
0
        } else {
356
0
            ret = ctx->cipher->einit_skey(ctx->algctx,
357
0
                skey == NULL ? NULL : skey->keydata,
358
0
                iv, iv_len, params);
359
0
        }
360
0
    } else {
361
0
        if (ctx->cipher->dinit_skey == NULL) {
362
            /*
363
             *  When skey is NULL, it's a multiple-step init as the current API does.
364
             *  Otherwise we try to fallback for providers that do not support SKEYs.
365
             */
366
0
            const unsigned char *keydata = NULL;
367
0
            size_t keylen = 0;
368
369
0
            if (skey != NULL && !EVP_SKEY_get0_raw_key(skey, &keydata, &keylen)) {
370
0
                ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
371
0
                return 0;
372
0
            }
373
374
0
            ret = ctx->cipher->dinit(ctx->algctx, keydata, keylen,
375
0
                iv, iv_len, params);
376
0
        } else {
377
0
            ret = ctx->cipher->dinit_skey(ctx->algctx,
378
0
                skey == NULL ? NULL : skey->keydata,
379
0
                iv, iv_len, params);
380
0
        }
381
0
    }
382
383
0
    return ret;
384
0
}
385
386
int EVP_CipherInit_SKEY(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
387
    EVP_SKEY *skey, const unsigned char *iv, size_t iv_len,
388
    int enc, const OSSL_PARAM params[])
389
0
{
390
0
    return evp_cipher_init_skey_internal(ctx, cipher, skey, iv, iv_len, enc, params);
391
0
}
392
393
int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
394
    const unsigned char *key, const unsigned char *iv,
395
    int enc, const OSSL_PARAM params[])
396
50.9k
{
397
50.9k
    return evp_cipher_init_internal(ctx, cipher, key, iv, enc, 0, params);
398
50.9k
}
399
400
int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
401
    const unsigned char *key, const unsigned char *iv, int enc)
402
0
{
403
0
    if (cipher != NULL)
404
0
        EVP_CIPHER_CTX_reset(ctx);
405
0
    return evp_cipher_init_internal(ctx, cipher, key, iv, enc, 0, NULL);
406
0
}
407
408
int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
409
    ENGINE *impl, const unsigned char *key,
410
    const unsigned char *iv, int enc)
411
1.68M
{
412
1.68M
    if (!ossl_assert(impl == NULL))
413
0
        return 0;
414
1.68M
    return evp_cipher_init_internal(ctx, cipher, key, iv, enc, 0, NULL);
415
1.68M
}
416
417
int EVP_CipherPipelineEncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
418
    const unsigned char *key, size_t keylen,
419
    size_t numpipes,
420
    const unsigned char **iv, size_t ivlen)
421
0
{
422
0
    if (numpipes > EVP_MAX_PIPES) {
423
0
        ERR_raise(ERR_LIB_EVP, EVP_R_TOO_MANY_PIPES);
424
0
        return 0;
425
0
    }
426
427
0
    ctx->numpipes = numpipes;
428
429
0
    if (!evp_cipher_init_internal(ctx, cipher, NULL, NULL, 1, 1,
430
0
            NULL))
431
0
        return 0;
432
433
0
    if (ctx->cipher->p_einit == NULL) {
434
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
435
0
        return 0;
436
0
    }
437
438
0
    return ctx->cipher->p_einit(ctx->algctx,
439
0
        key,
440
0
        keylen,
441
0
        numpipes,
442
0
        iv,
443
0
        ivlen,
444
0
        NULL);
445
0
}
446
447
int EVP_CipherPipelineDecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
448
    const unsigned char *key, size_t keylen,
449
    size_t numpipes,
450
    const unsigned char **iv, size_t ivlen)
451
0
{
452
0
    if (numpipes > EVP_MAX_PIPES) {
453
0
        ERR_raise(ERR_LIB_EVP, EVP_R_TOO_MANY_PIPES);
454
0
        return 0;
455
0
    }
456
457
0
    ctx->numpipes = numpipes;
458
459
0
    if (!evp_cipher_init_internal(ctx, cipher, NULL, NULL, 0, 1,
460
0
            NULL))
461
0
        return 0;
462
463
0
    if (ctx->cipher->p_dinit == NULL) {
464
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
465
0
        return 0;
466
0
    }
467
468
0
    return ctx->cipher->p_dinit(ctx->algctx,
469
0
        key,
470
0
        keylen,
471
0
        numpipes,
472
0
        iv,
473
0
        ivlen,
474
0
        NULL);
475
0
}
476
477
int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
478
    const unsigned char *in, int inl)
479
7.65M
{
480
7.65M
    if (ctx->encrypt)
481
5.11M
        return EVP_EncryptUpdate(ctx, out, outl, in, inl);
482
2.54M
    else
483
2.54M
        return EVP_DecryptUpdate(ctx, out, outl, in, inl);
484
7.65M
}
485
486
int EVP_CipherPipelineUpdate(EVP_CIPHER_CTX *ctx,
487
    unsigned char **out, size_t *outl,
488
    const size_t *outsize,
489
    const unsigned char **in, const size_t *inl)
490
0
{
491
0
    size_t i;
492
493
0
    if (ossl_unlikely(outl == NULL || inl == NULL)) {
494
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
495
0
        return 0;
496
0
    }
497
498
0
    if (ossl_unlikely(ctx->cipher == NULL)) {
499
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
500
0
        return 0;
501
0
    }
502
503
0
    if (ossl_unlikely(ctx->cipher->prov == NULL)) {
504
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
505
0
        return 0;
506
0
    }
507
508
0
    if (ossl_unlikely(ctx->cipher->p_cupdate == NULL)) {
509
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
510
0
        return 0;
511
0
    }
512
513
0
    for (i = 0; i < ctx->numpipes; i++)
514
0
        outl[i] = 0;
515
516
0
    return ctx->cipher->p_cupdate(ctx->algctx, ctx->numpipes,
517
0
        out, outl, outsize,
518
0
        in, inl);
519
0
}
520
521
int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
522
2.19M
{
523
2.19M
    if (ctx->encrypt)
524
1.37M
        return EVP_EncryptFinal_ex(ctx, out, outl);
525
824k
    else
526
824k
        return EVP_DecryptFinal_ex(ctx, out, outl);
527
2.19M
}
528
529
int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
530
0
{
531
0
    if (ctx->encrypt)
532
0
        return EVP_EncryptFinal(ctx, out, outl);
533
0
    else
534
0
        return EVP_DecryptFinal(ctx, out, outl);
535
0
}
536
537
int EVP_CipherPipelineFinal(EVP_CIPHER_CTX *ctx,
538
    unsigned char **out, size_t *outl,
539
    const size_t *outsize)
540
0
{
541
0
    size_t i;
542
543
0
    if (ossl_unlikely(outl == NULL)) {
544
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
545
0
        return 0;
546
0
    }
547
548
0
    if (ossl_unlikely(ctx->cipher == NULL)) {
549
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
550
0
        return 0;
551
0
    }
552
553
0
    if (ossl_unlikely(ctx->cipher->prov == NULL)) {
554
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
555
0
        return 0;
556
0
    }
557
558
0
    if (ossl_unlikely(ctx->cipher->p_cfinal == NULL)) {
559
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
560
0
        return 0;
561
0
    }
562
563
0
    for (i = 0; i < ctx->numpipes; i++)
564
0
        outl[i] = 0;
565
566
0
    return ctx->cipher->p_cfinal(ctx->algctx, ctx->numpipes,
567
0
        out, outl, outsize);
568
0
}
569
570
int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
571
    const unsigned char *key, const unsigned char *iv)
572
0
{
573
0
    return EVP_CipherInit(ctx, cipher, key, iv, 1);
574
0
}
575
576
int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
577
    ENGINE *impl, const unsigned char *key,
578
    const unsigned char *iv)
579
34.1k
{
580
34.1k
    if (!ossl_assert(impl == NULL))
581
0
        return 0;
582
34.1k
    return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 1);
583
34.1k
}
584
585
int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
586
    const unsigned char *key, const unsigned char *iv,
587
    const OSSL_PARAM params[])
588
50.9k
{
589
50.9k
    return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params);
590
50.9k
}
591
592
int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
593
    const unsigned char *key, const unsigned char *iv)
594
0
{
595
0
    return EVP_CipherInit(ctx, cipher, key, iv, 0);
596
0
}
597
598
int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
599
    ENGINE *impl, const unsigned char *key,
600
    const unsigned char *iv)
601
523
{
602
523
    if (!ossl_assert(impl == NULL))
603
0
        return 0;
604
523
    return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 0);
605
523
}
606
607
int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
608
    const unsigned char *key, const unsigned char *iv,
609
    const OSSL_PARAM params[])
610
0
{
611
0
    return EVP_CipherInit_ex2(ctx, cipher, key, iv, 0, params);
612
0
}
613
614
/*
615
 * According to the letter of standard difference between pointers
616
 * is specified to be valid only within same object. This makes
617
 * it formally challenging to determine if input and output buffers
618
 * are not partially overlapping with standard pointer arithmetic.
619
 */
620
#ifdef PTRDIFF_T
621
#undef PTRDIFF_T
622
#endif
623
#if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE == 64
624
/*
625
 * Then we have VMS that distinguishes itself by adhering to
626
 * sizeof(size_t)==4 even in 64-bit builds, which means that
627
 * difference between two pointers might be truncated to 32 bits.
628
 * In the context one can even wonder how comparison for
629
 * equality is implemented. To be on the safe side we adhere to
630
 * PTRDIFF_T even for comparison for equality.
631
 */
632
#define PTRDIFF_T uint64_t
633
#else
634
0
#define PTRDIFF_T size_t
635
#endif
636
637
int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
638
0
{
639
0
    PTRDIFF_T diff = (PTRDIFF_T)ptr1 - (PTRDIFF_T)ptr2;
640
    /*
641
     * Check for partially overlapping buffers. [Binary logical
642
     * operations are used instead of boolean to minimize number
643
     * of conditional branches.]
644
     */
645
0
    int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) | (diff > (0 - (PTRDIFF_T)len)));
646
647
0
    return overlapped;
648
0
}
649
650
int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
651
    const unsigned char *in, int inl)
652
3.37M
{
653
3.37M
    int ret;
654
3.37M
    size_t soutl, inl_ = (size_t)inl;
655
3.37M
    int blocksize;
656
657
3.37M
    if (inl < 0) {
658
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
659
0
        return 0;
660
0
    }
661
662
3.37M
    if (ossl_likely(outl != NULL)) {
663
3.37M
        *outl = 0;
664
3.37M
    } else {
665
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
666
0
        return 0;
667
0
    }
668
669
    /* Prevent accidental use of decryption context when encrypting */
670
3.37M
    if (ossl_unlikely(!ctx->encrypt)) {
671
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
672
0
        return 0;
673
0
    }
674
675
3.37M
    if (ossl_unlikely(ctx->cipher == NULL)) {
676
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
677
0
        return 0;
678
0
    }
679
680
3.37M
    if (ossl_unlikely(ctx->cipher->prov == NULL))
681
0
        return 0;
682
683
3.37M
    blocksize = ctx->cipher->block_size;
684
685
3.37M
    if (ossl_unlikely(ctx->cipher->cupdate == NULL || blocksize < 1)) {
686
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
687
0
        return 0;
688
0
    }
689
690
3.37M
    ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
691
3.37M
        inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
692
3.37M
        in, inl_);
693
694
3.37M
    if (ossl_likely(ret)) {
695
3.37M
        if (ossl_unlikely(soutl > INT_MAX)) {
696
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
697
0
            return 0;
698
0
        }
699
3.37M
        *outl = (int)soutl;
700
3.37M
    }
701
702
3.37M
    return ret;
703
3.37M
}
704
705
int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
706
124
{
707
124
    int ret;
708
124
    ret = EVP_EncryptFinal_ex(ctx, out, outl);
709
124
    return ret;
710
124
}
711
712
int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
713
513k
{
714
513k
    int ret;
715
513k
    size_t soutl;
716
513k
    int blocksize;
717
718
513k
    if (outl != NULL) {
719
513k
        *outl = 0;
720
513k
    } else {
721
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
722
0
        return 0;
723
0
    }
724
725
    /* Prevent accidental use of decryption context when encrypting */
726
513k
    if (!ctx->encrypt) {
727
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
728
0
        return 0;
729
0
    }
730
731
513k
    if (ctx->cipher == NULL) {
732
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
733
0
        return 0;
734
0
    }
735
513k
    if (ctx->cipher->prov == NULL) {
736
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
737
0
        return 0;
738
0
    }
739
740
513k
    blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
741
742
513k
    if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
743
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
744
0
        return 0;
745
0
    }
746
747
513k
    ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
748
513k
        blocksize == 1 ? 0 : blocksize);
749
750
513k
    if (ret) {
751
513k
        if (soutl > INT_MAX) {
752
0
            ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
753
0
            return 0;
754
0
        }
755
513k
        *outl = (int)soutl;
756
513k
    }
757
758
513k
    return ret;
759
513k
}
760
761
int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
762
    const unsigned char *in, int inl)
763
812k
{
764
812k
    int ret;
765
812k
    size_t soutl, inl_ = (size_t)inl;
766
812k
    int blocksize;
767
768
812k
    if (inl < 0) {
769
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
770
0
        return 0;
771
0
    }
772
773
812k
    if (ossl_likely(outl != NULL)) {
774
812k
        *outl = 0;
775
812k
    } else {
776
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
777
0
        return 0;
778
0
    }
779
780
    /* Prevent accidental use of encryption context when decrypting */
781
812k
    if (ossl_unlikely(ctx->encrypt)) {
782
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
783
0
        return 0;
784
0
    }
785
786
812k
    if (ossl_unlikely(ctx->cipher == NULL)) {
787
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
788
0
        return 0;
789
0
    }
790
812k
    if (ossl_unlikely(ctx->cipher->prov == NULL)) {
791
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
792
0
        return 0;
793
0
    }
794
795
812k
    blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
796
797
812k
    if (ossl_unlikely(ctx->cipher->cupdate == NULL || blocksize < 1)) {
798
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
799
0
        return 0;
800
0
    }
801
812k
    ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
802
812k
        inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
803
812k
        in, inl_);
804
805
812k
    if (ossl_likely(ret)) {
806
699k
        if (ossl_unlikely(soutl > INT_MAX)) {
807
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
808
0
            return 0;
809
0
        }
810
699k
        *outl = (int)soutl;
811
699k
    }
812
813
812k
    return ret;
814
812k
}
815
816
int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
817
839
{
818
839
    int ret;
819
839
    ret = EVP_DecryptFinal_ex(ctx, out, outl);
820
839
    return ret;
821
839
}
822
823
int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
824
253k
{
825
253k
    size_t soutl;
826
253k
    int ret;
827
253k
    int blocksize;
828
829
253k
    if (outl != NULL) {
830
253k
        *outl = 0;
831
253k
    } else {
832
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
833
0
        return 0;
834
0
    }
835
836
    /* Prevent accidental use of encryption context when decrypting */
837
253k
    if (ctx->encrypt) {
838
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
839
0
        return 0;
840
0
    }
841
842
253k
    if (ctx->cipher == NULL) {
843
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
844
0
        return 0;
845
0
    }
846
847
253k
    if (ctx->cipher->prov == NULL) {
848
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
849
0
        return 0;
850
0
    }
851
852
253k
    blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
853
854
253k
    if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
855
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
856
0
        return 0;
857
0
    }
858
859
253k
    ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
860
253k
        blocksize == 1 ? 0 : blocksize);
861
862
253k
    if (ret) {
863
1.16k
        if (soutl > INT_MAX) {
864
0
            ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
865
0
            return 0;
866
0
        }
867
1.16k
        *outl = (int)soutl;
868
1.16k
    }
869
870
253k
    return ret;
871
253k
}
872
873
int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
874
220
{
875
220
    int ok;
876
220
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
877
220
    size_t len;
878
879
220
    if (c->cipher->prov == NULL) {
880
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CIPHER_PARAMETER_ERROR);
881
0
        return 0;
882
0
    }
883
884
220
    if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
885
179
        return 1;
886
887
    /* Check the cipher actually understands this parameter */
888
41
    if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
889
41
            OSSL_CIPHER_PARAM_KEYLEN)
890
41
        == NULL) {
891
38
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
892
38
        return 0;
893
38
    }
894
895
3
    params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
896
3
    if (!OSSL_PARAM_set_int(params, keylen))
897
0
        return 0;
898
3
    ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params);
899
3
    if (ok <= 0)
900
3
        return 0;
901
0
    c->key_len = keylen;
902
0
    return 1;
903
3
}
904
905
int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
906
1.39k
{
907
1.39k
    int ok;
908
1.39k
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
909
1.39k
    unsigned int pd = pad;
910
911
1.39k
    if (pad)
912
0
        ctx->flags &= ~EVP_CIPH_NO_PADDING;
913
1.39k
    else
914
1.39k
        ctx->flags |= EVP_CIPH_NO_PADDING;
915
916
1.39k
    if (ctx->cipher != NULL && ctx->cipher->prov == NULL)
917
0
        return 1;
918
1.39k
    params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
919
1.39k
    ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
920
921
1.39k
    return ok != 0;
922
1.39k
}
923
924
int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
925
225k
{
926
225k
    int ret = EVP_CTRL_RET_UNSUPPORTED;
927
225k
    int set_params = 1;
928
225k
    size_t sz = arg;
929
225k
    unsigned int i;
930
225k
    OSSL_PARAM params[4] = {
931
225k
        OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
932
225k
    };
933
934
225k
    if (ctx == NULL || ctx->cipher == NULL) {
935
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
936
0
        return 0;
937
0
    }
938
939
225k
    if (ctx->cipher->prov == NULL) {
940
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
941
0
        return 0;
942
0
    }
943
944
225k
    switch (type) {
945
0
    case EVP_CTRL_SET_KEY_LENGTH:
946
0
        if (arg < 0)
947
0
            return 0;
948
0
        if (ctx->key_len == arg)
949
            /* Skip calling into provider if unchanged. */
950
0
            return 1;
951
0
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
952
0
        ctx->key_len = -1;
953
0
        break;
954
0
    case EVP_CTRL_RAND_KEY: /* Used by DES */
955
0
        set_params = 0;
956
0
        params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
957
0
            ptr, sz);
958
0
        break;
959
960
0
    case EVP_CTRL_INIT:
961
        /*
962
         * EVP_CTRL_INIT is purely legacy, no provider counterpart.
963
         * As a matter of fact, this should be dead code, but some caller
964
         * might still do a direct control call with this command, so...
965
         * Legacy methods return 1 except for exceptional circumstances, so
966
         * we do the same here to not be disruptive.
967
         */
968
0
        return 1;
969
0
    case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
970
0
    default:
971
0
        goto end;
972
1.46k
    case EVP_CTRL_AEAD_SET_IVLEN:
973
1.46k
        if (arg < 0)
974
0
            return 0;
975
1.46k
        if (ctx->iv_len == arg)
976
            /* Skip calling into provider if unchanged. */
977
0
            return 1;
978
1.46k
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
979
1.46k
        ctx->iv_len = -1;
980
1.46k
        break;
981
0
    case EVP_CTRL_CCM_SET_L:
982
0
        if (arg < 2 || arg > 8)
983
0
            return 0;
984
0
        sz = 15 - arg;
985
0
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
986
0
        ctx->iv_len = -1;
987
0
        break;
988
238
    case EVP_CTRL_AEAD_SET_IV_FIXED:
989
238
        params[0] = OSSL_PARAM_construct_octet_string(
990
238
            OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
991
238
        break;
992
0
    case EVP_CTRL_GCM_IV_GEN:
993
0
        set_params = 0;
994
0
        if (arg < 0)
995
0
            sz = 0; /* special case that uses the iv length */
996
0
        params[0] = OSSL_PARAM_construct_octet_string(
997
0
            OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
998
0
        break;
999
0
    case EVP_CTRL_GCM_SET_IV_INV:
1000
0
        if (arg < 0)
1001
0
            return 0;
1002
0
        params[0] = OSSL_PARAM_construct_octet_string(
1003
0
            OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
1004
0
        break;
1005
0
    case EVP_CTRL_GET_RC5_ROUNDS:
1006
0
        set_params = 0; /* Fall thru */
1007
0
    case EVP_CTRL_SET_RC5_ROUNDS:
1008
0
        if (arg < 0)
1009
0
            return 0;
1010
0
        i = (unsigned int)arg;
1011
0
        params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
1012
0
        break;
1013
0
    case EVP_CTRL_SET_SPEED:
1014
0
        if (arg < 0)
1015
0
            return 0;
1016
0
        i = (unsigned int)arg;
1017
0
        params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
1018
0
        break;
1019
147k
    case EVP_CTRL_AEAD_GET_TAG:
1020
147k
        set_params = 0; /* Fall thru */
1021
222k
    case EVP_CTRL_AEAD_SET_TAG:
1022
222k
        params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1023
222k
            ptr, sz);
1024
222k
        break;
1025
1.27k
    case EVP_CTRL_AEAD_TLS1_AAD:
1026
        /* This one does a set and a get - since it returns a size */
1027
1.27k
        params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1028
1.27k
            ptr, sz);
1029
1.27k
        ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1030
1.27k
        if (ret <= 0)
1031
16
            goto end;
1032
1.25k
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1033
1.25k
        ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1034
1.25k
        if (ret <= 0)
1035
0
            goto end;
1036
1.25k
        if (sz > INT_MAX)
1037
0
            return 0;
1038
1.25k
        return (int)sz;
1039
0
#ifndef OPENSSL_NO_RC2
1040
0
    case EVP_CTRL_GET_RC2_KEY_BITS:
1041
0
        set_params = 0; /* Fall thru */
1042
0
    case EVP_CTRL_SET_RC2_KEY_BITS:
1043
0
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
1044
0
        break;
1045
0
#endif /* OPENSSL_NO_RC2 */
1046
0
#if !defined(OPENSSL_NO_MULTIBLOCK)
1047
0
    case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
1048
0
        params[0] = OSSL_PARAM_construct_size_t(
1049
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
1050
0
        ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1051
0
        if (ret <= 0)
1052
0
            return 0;
1053
1054
0
        params[0] = OSSL_PARAM_construct_size_t(
1055
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
1056
0
        params[1] = OSSL_PARAM_construct_end();
1057
0
        ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1058
0
        if (ret <= 0 || sz > INT_MAX)
1059
0
            return 0;
1060
0
        return (int)sz;
1061
0
    case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
1062
0
        EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p = (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1063
1064
0
        if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
1065
0
            return 0;
1066
1067
0
        params[0] = OSSL_PARAM_construct_octet_string(
1068
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void *)p->inp, p->len);
1069
0
        params[1] = OSSL_PARAM_construct_uint(
1070
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1071
0
        ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1072
0
        if (ret <= 0)
1073
0
            return ret;
1074
        /* Retrieve the return values changed by the set */
1075
0
        params[0] = OSSL_PARAM_construct_size_t(
1076
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
1077
0
        params[1] = OSSL_PARAM_construct_uint(
1078
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1079
0
        params[2] = OSSL_PARAM_construct_end();
1080
0
        ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1081
0
        if (ret <= 0 || sz > INT_MAX)
1082
0
            return 0;
1083
0
        return (int)sz;
1084
0
    }
1085
0
    case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
1086
0
        EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p = (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1087
1088
0
        params[0] = OSSL_PARAM_construct_octet_string(
1089
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
1090
1091
0
        params[1] = OSSL_PARAM_construct_octet_string(
1092
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void *)p->inp,
1093
0
            p->len);
1094
0
        params[2] = OSSL_PARAM_construct_uint(
1095
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1096
0
        ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1097
0
        if (ret <= 0)
1098
0
            return ret;
1099
0
        params[0] = OSSL_PARAM_construct_size_t(
1100
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
1101
0
        params[1] = OSSL_PARAM_construct_end();
1102
0
        ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1103
0
        if (ret <= 0 || sz > INT_MAX)
1104
0
            return 0;
1105
0
        return (int)sz;
1106
0
    }
1107
0
#endif /* OPENSSL_NO_MULTIBLOCK */
1108
908
    case EVP_CTRL_AEAD_SET_MAC_KEY:
1109
908
        if (arg < 0)
1110
0
            return -1;
1111
908
        params[0] = OSSL_PARAM_construct_octet_string(
1112
908
            OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
1113
908
        break;
1114
225k
    }
1115
1116
224k
    if (set_params)
1117
77.1k
        ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1118
147k
    else
1119
147k
        ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1120
1121
224k
end:
1122
224k
    if (ret == EVP_CTRL_RET_UNSUPPORTED) {
1123
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
1124
0
        return 0;
1125
0
    }
1126
224k
    return ret;
1127
224k
}
1128
1129
int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1130
2.01M
{
1131
2.01M
    if (cipher != NULL && cipher->get_params != NULL)
1132
2.01M
        return cipher->get_params(params);
1133
0
    return 0;
1134
2.01M
}
1135
1136
int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1137
29.9k
{
1138
29.9k
    int r = 0;
1139
29.9k
    const OSSL_PARAM *p;
1140
1141
29.9k
    if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) {
1142
29.8k
        r = ctx->cipher->set_ctx_params(ctx->algctx, params);
1143
29.8k
        if (r > 0) {
1144
29.6k
            p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
1145
29.6k
            if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->key_len)) {
1146
0
                r = 0;
1147
0
                ctx->key_len = -1;
1148
0
            }
1149
29.6k
        }
1150
29.8k
        if (r > 0) {
1151
29.6k
            p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
1152
29.6k
            if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->iv_len)) {
1153
0
                r = 0;
1154
0
                ctx->iv_len = -1;
1155
0
            }
1156
29.6k
        }
1157
29.8k
    }
1158
29.9k
    return r;
1159
29.9k
}
1160
1161
int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1162
627k
{
1163
627k
    if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1164
627k
        return ctx->cipher->get_ctx_params(ctx->algctx, params);
1165
0
    return 0;
1166
627k
}
1167
1168
const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1169
0
{
1170
0
    if (cipher != NULL && cipher->gettable_params != NULL)
1171
0
        return cipher->gettable_params(
1172
0
            ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)));
1173
0
    return NULL;
1174
0
}
1175
1176
const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
1177
625
{
1178
625
    void *provctx;
1179
1180
625
    if (cipher != NULL && cipher->settable_ctx_params != NULL) {
1181
625
        provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1182
625
        return cipher->settable_ctx_params(NULL, provctx);
1183
625
    }
1184
0
    return NULL;
1185
625
}
1186
1187
const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
1188
9.10k
{
1189
9.10k
    void *provctx;
1190
1191
9.10k
    if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
1192
9.10k
        provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1193
9.10k
        return cipher->gettable_ctx_params(NULL, provctx);
1194
9.10k
    }
1195
0
    return NULL;
1196
9.10k
}
1197
1198
const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx)
1199
0
{
1200
0
    void *alg;
1201
1202
0
    if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
1203
0
        alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1204
0
        return cctx->cipher->settable_ctx_params(cctx->algctx, alg);
1205
0
    }
1206
0
    return NULL;
1207
0
}
1208
1209
const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx)
1210
0
{
1211
0
    void *provctx;
1212
1213
0
    if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
1214
0
        provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1215
0
        return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx);
1216
0
    }
1217
0
    return NULL;
1218
0
}
1219
1220
#ifndef FIPS_MODULE
1221
static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx)
1222
0
{
1223
0
    const EVP_CIPHER *cipher = ctx->cipher;
1224
0
    const OSSL_PROVIDER *prov;
1225
1226
0
    if (cipher == NULL)
1227
0
        return NULL;
1228
1229
0
    prov = EVP_CIPHER_get0_provider(cipher);
1230
0
    return ossl_provider_libctx(prov);
1231
0
}
1232
#endif
1233
1234
int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
1235
0
{
1236
0
    if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1237
0
        return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1238
1239
#ifdef FIPS_MODULE
1240
    return 0;
1241
#else
1242
0
    {
1243
0
        int kl;
1244
0
        OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
1245
1246
0
        kl = EVP_CIPHER_CTX_get_key_length(ctx);
1247
0
        if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0)
1248
0
            return 0;
1249
0
        return 1;
1250
0
    }
1251
0
#endif /* FIPS_MODULE */
1252
0
}
1253
1254
EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in)
1255
0
{
1256
0
    EVP_CIPHER_CTX *out = EVP_CIPHER_CTX_new();
1257
1258
0
    if (out != NULL && !EVP_CIPHER_CTX_copy(out, in)) {
1259
0
        EVP_CIPHER_CTX_free(out);
1260
0
        out = NULL;
1261
0
    }
1262
0
    return out;
1263
0
}
1264
1265
int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
1266
276
{
1267
276
    if ((in == NULL) || (in->cipher == NULL)) {
1268
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
1269
0
        return 0;
1270
0
    }
1271
1272
276
    if (in->cipher->prov == NULL) {
1273
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
1274
0
        return 0;
1275
0
    }
1276
1277
276
    if (in->cipher->dupctx == NULL) {
1278
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1279
0
        return 0;
1280
0
    }
1281
1282
276
    EVP_CIPHER_CTX_reset(out);
1283
1284
276
    *out = *in;
1285
276
    out->algctx = NULL;
1286
1287
276
    if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
1288
0
        out->fetched_cipher = NULL;
1289
0
        return 0;
1290
0
    }
1291
1292
276
    out->algctx = in->cipher->dupctx(in->algctx);
1293
276
    if (out->algctx == NULL) {
1294
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1295
0
        return 0;
1296
0
    }
1297
1298
276
    return 1;
1299
276
}
1300
1301
EVP_CIPHER *evp_cipher_new(void)
1302
9.10k
{
1303
9.10k
    EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1304
1305
9.10k
    if (cipher != NULL && !CRYPTO_NEW_REF(&cipher->refcnt, 1)) {
1306
0
        OPENSSL_free(cipher);
1307
0
        return NULL;
1308
0
    }
1309
9.10k
    return cipher;
1310
9.10k
}
1311
1312
/*
1313
 * FIPS module note: since internal fetches will be entirely
1314
 * provider based, we know that none of its code depends on legacy
1315
 * NIDs or any functionality that use them.
1316
 */
1317
#ifndef FIPS_MODULE
1318
/* After removal of legacy support get rid of the need for legacy NIDs */
1319
static void set_legacy_nid(const char *name, void *vlegacy_nid)
1320
17.0k
{
1321
17.0k
    int nid;
1322
17.0k
    int *legacy_nid = vlegacy_nid;
1323
    /*
1324
     * We use lowest level function to get the associated method, because
1325
     * higher level functions such as EVP_get_cipherbyname() have changed
1326
     * to look at providers too.
1327
     */
1328
17.0k
    const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
1329
1330
17.0k
    if (*legacy_nid == -1) /* We found a clash already */
1331
0
        return;
1332
17.0k
    if (legacy_method == NULL)
1333
7.63k
        return;
1334
9.38k
    nid = EVP_CIPHER_get_nid(legacy_method);
1335
9.38k
    if (*legacy_nid != NID_undef && *legacy_nid != nid) {
1336
0
        *legacy_nid = -1;
1337
0
        return;
1338
0
    }
1339
9.38k
    *legacy_nid = nid;
1340
9.38k
}
1341
#endif
1342
1343
static void *evp_cipher_from_algorithm(const int name_id,
1344
    const OSSL_ALGORITHM *algodef,
1345
    OSSL_PROVIDER *prov)
1346
5.46k
{
1347
5.46k
    const OSSL_DISPATCH *fns = algodef->implementation;
1348
5.46k
    EVP_CIPHER *cipher = NULL;
1349
5.46k
    int fnciphcnt = 0, encinit = 0, decinit = 0, fnpipecnt = 0, fnctxcnt = 0;
1350
1351
5.46k
    if ((cipher = evp_cipher_new()) == NULL) {
1352
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
1353
0
        return NULL;
1354
0
    }
1355
1356
5.46k
#ifndef FIPS_MODULE
1357
5.46k
    cipher->nid = NID_undef;
1358
5.46k
    if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
1359
5.46k
        || cipher->nid == -1) {
1360
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1361
0
        goto err;
1362
0
    }
1363
5.46k
#endif
1364
1365
5.46k
    cipher->name_id = name_id;
1366
5.46k
    if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
1367
0
        goto err;
1368
1369
5.46k
    cipher->description = algodef->algorithm_description;
1370
1371
87.0k
    for (; fns->function_id != 0; fns++) {
1372
81.6k
        switch (fns->function_id) {
1373
5.46k
        case OSSL_FUNC_CIPHER_NEWCTX:
1374
5.46k
            if (cipher->newctx != NULL)
1375
0
                break;
1376
5.46k
            cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
1377
5.46k
            fnctxcnt++;
1378
5.46k
            break;
1379
5.46k
        case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1380
5.46k
            if (cipher->einit != NULL)
1381
0
                break;
1382
5.46k
            cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
1383
5.46k
            encinit = 1;
1384
5.46k
            break;
1385
5.46k
        case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1386
5.46k
            if (cipher->dinit != NULL)
1387
0
                break;
1388
5.46k
            cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
1389
5.46k
            decinit = 1;
1390
5.46k
            break;
1391
2.85k
        case OSSL_FUNC_CIPHER_ENCRYPT_SKEY_INIT:
1392
2.85k
            if (cipher->einit_skey != NULL)
1393
0
                break;
1394
2.85k
            cipher->einit_skey = OSSL_FUNC_cipher_encrypt_skey_init(fns);
1395
2.85k
            encinit = 1;
1396
2.85k
            break;
1397
2.85k
        case OSSL_FUNC_CIPHER_DECRYPT_SKEY_INIT:
1398
2.85k
            if (cipher->dinit_skey != NULL)
1399
0
                break;
1400
2.85k
            cipher->dinit_skey = OSSL_FUNC_cipher_decrypt_skey_init(fns);
1401
2.85k
            decinit = 1;
1402
2.85k
            break;
1403
5.46k
        case OSSL_FUNC_CIPHER_UPDATE:
1404
5.46k
            if (cipher->cupdate != NULL)
1405
0
                break;
1406
5.46k
            cipher->cupdate = OSSL_FUNC_cipher_update(fns);
1407
5.46k
            fnciphcnt++;
1408
5.46k
            break;
1409
5.46k
        case OSSL_FUNC_CIPHER_FINAL:
1410
5.46k
            if (cipher->cfinal != NULL)
1411
0
                break;
1412
5.46k
            cipher->cfinal = OSSL_FUNC_cipher_final(fns);
1413
5.46k
            fnciphcnt++;
1414
5.46k
            break;
1415
4.95k
        case OSSL_FUNC_CIPHER_CIPHER:
1416
4.95k
            if (cipher->ccipher != NULL)
1417
0
                break;
1418
4.95k
            cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
1419
4.95k
            break;
1420
0
        case OSSL_FUNC_CIPHER_PIPELINE_ENCRYPT_INIT:
1421
0
            if (cipher->p_einit != NULL)
1422
0
                break;
1423
0
            cipher->p_einit = OSSL_FUNC_cipher_pipeline_encrypt_init(fns);
1424
0
            fnpipecnt++;
1425
0
            break;
1426
0
        case OSSL_FUNC_CIPHER_PIPELINE_DECRYPT_INIT:
1427
0
            if (cipher->p_dinit != NULL)
1428
0
                break;
1429
0
            cipher->p_dinit = OSSL_FUNC_cipher_pipeline_decrypt_init(fns);
1430
0
            fnpipecnt++;
1431
0
            break;
1432
0
        case OSSL_FUNC_CIPHER_PIPELINE_UPDATE:
1433
0
            if (cipher->p_cupdate != NULL)
1434
0
                break;
1435
0
            cipher->p_cupdate = OSSL_FUNC_cipher_pipeline_update(fns);
1436
0
            fnpipecnt++;
1437
0
            break;
1438
0
        case OSSL_FUNC_CIPHER_PIPELINE_FINAL:
1439
0
            if (cipher->p_cfinal != NULL)
1440
0
                break;
1441
0
            cipher->p_cfinal = OSSL_FUNC_cipher_pipeline_final(fns);
1442
0
            fnpipecnt++;
1443
0
            break;
1444
5.46k
        case OSSL_FUNC_CIPHER_FREECTX:
1445
5.46k
            if (cipher->freectx != NULL)
1446
0
                break;
1447
5.46k
            cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
1448
5.46k
            fnctxcnt++;
1449
5.46k
            break;
1450
5.41k
        case OSSL_FUNC_CIPHER_DUPCTX:
1451
5.41k
            if (cipher->dupctx != NULL)
1452
0
                break;
1453
5.41k
            cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
1454
5.41k
            break;
1455
5.46k
        case OSSL_FUNC_CIPHER_GET_PARAMS:
1456
5.46k
            if (cipher->get_params != NULL)
1457
0
                break;
1458
5.46k
            cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
1459
5.46k
            break;
1460
5.46k
        case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1461
5.46k
            if (cipher->get_ctx_params != NULL)
1462
0
                break;
1463
5.46k
            cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
1464
5.46k
            break;
1465
5.46k
        case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1466
5.46k
            if (cipher->set_ctx_params != NULL)
1467
0
                break;
1468
5.46k
            cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
1469
5.46k
            break;
1470
5.46k
        case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1471
5.46k
            if (cipher->gettable_params != NULL)
1472
0
                break;
1473
5.46k
            cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
1474
5.46k
            break;
1475
5.46k
        case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1476
5.46k
            if (cipher->gettable_ctx_params != NULL)
1477
0
                break;
1478
5.46k
            cipher->gettable_ctx_params = OSSL_FUNC_cipher_gettable_ctx_params(fns);
1479
5.46k
            break;
1480
5.46k
        case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1481
5.46k
            if (cipher->settable_ctx_params != NULL)
1482
0
                break;
1483
5.46k
            cipher->settable_ctx_params = OSSL_FUNC_cipher_settable_ctx_params(fns);
1484
5.46k
            break;
1485
81.6k
        }
1486
81.6k
    }
1487
5.46k
    fnciphcnt += encinit + decinit;
1488
5.46k
    if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1489
5.46k
        || (fnciphcnt == 0 && cipher->ccipher == NULL && fnpipecnt == 0)
1490
5.46k
        || (fnpipecnt != 0 && (fnpipecnt < 3 || cipher->p_cupdate == NULL || cipher->p_cfinal == NULL))
1491
5.46k
        || fnctxcnt != 2) {
1492
        /*
1493
         * In order to be a consistent set of functions we must have at least
1494
         * a complete set of "encrypt" functions, or a complete set of "decrypt"
1495
         * functions, or a single "cipher" function. In all cases we need both
1496
         * the "newctx" and "freectx" functions.
1497
         */
1498
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1499
0
        goto err;
1500
0
    }
1501
5.46k
    if (prov != NULL && !ossl_provider_up_ref(prov))
1502
0
        goto err;
1503
1504
5.46k
    cipher->prov = prov;
1505
1506
5.46k
    if (!evp_cipher_cache_constants(cipher)) {
1507
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1508
0
        goto err;
1509
0
    }
1510
1511
5.46k
    return cipher;
1512
1513
0
err:
1514
0
    EVP_CIPHER_free(cipher);
1515
0
    return NULL;
1516
5.46k
}
1517
1518
static int evp_cipher_up_ref(void *cipher)
1519
2.22M
{
1520
2.22M
    return EVP_CIPHER_up_ref(cipher);
1521
2.22M
}
1522
1523
static void evp_cipher_free(void *cipher)
1524
11.7k
{
1525
11.7k
    EVP_CIPHER_free(cipher);
1526
11.7k
}
1527
1528
EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1529
    const char *properties)
1530
4.19M
{
1531
4.19M
    EVP_CIPHER *cipher = evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1532
4.19M
        evp_cipher_from_algorithm, evp_cipher_up_ref,
1533
4.19M
        evp_cipher_free);
1534
1535
4.19M
    return cipher;
1536
4.19M
}
1537
1538
EVP_CIPHER *evp_cipher_fetch_from_prov(OSSL_PROVIDER *prov,
1539
    const char *algorithm,
1540
    const char *properties)
1541
0
{
1542
0
    return evp_generic_fetch_from_prov(prov, OSSL_OP_CIPHER,
1543
0
        algorithm, properties,
1544
0
        evp_cipher_from_algorithm,
1545
0
        evp_cipher_up_ref,
1546
0
        evp_cipher_free);
1547
0
}
1548
1549
int EVP_CIPHER_can_pipeline(const EVP_CIPHER *cipher, int enc)
1550
0
{
1551
0
    if (((enc && cipher->p_einit != NULL) || (!enc && cipher->p_dinit != NULL))
1552
0
        && cipher->p_cupdate != NULL && cipher->p_cfinal != NULL)
1553
0
        return 1;
1554
1555
0
    return 0;
1556
0
}
1557
1558
int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
1559
2.95M
{
1560
2.95M
    int ref = 0;
1561
1562
2.95M
    if (cipher->origin == EVP_ORIG_DYNAMIC)
1563
2.95M
        CRYPTO_UP_REF(&cipher->refcnt, &ref);
1564
2.95M
    return 1;
1565
2.95M
}
1566
1567
void evp_cipher_free_int(EVP_CIPHER *cipher)
1568
4.42k
{
1569
4.42k
    OPENSSL_free(cipher->type_name);
1570
4.42k
    ossl_provider_free(cipher->prov);
1571
4.42k
    CRYPTO_FREE_REF(&cipher->refcnt);
1572
4.42k
    OPENSSL_free(cipher);
1573
4.42k
}
1574
1575
void EVP_CIPHER_free(EVP_CIPHER *cipher)
1576
3.97M
{
1577
3.97M
    int i;
1578
1579
3.97M
    if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC)
1580
1.02M
        return;
1581
1582
2.95M
    CRYPTO_DOWN_REF(&cipher->refcnt, &i);
1583
2.95M
    if (i > 0)
1584
2.94M
        return;
1585
4.42k
    evp_cipher_free_int(cipher);
1586
4.42k
}
1587
1588
void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
1589
    void (*fn)(EVP_CIPHER *mac, void *arg),
1590
    void *arg)
1591
11
{
1592
11
    evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1593
11
        (void (*)(void *, void *))fn, arg,
1594
11
        evp_cipher_from_algorithm, evp_cipher_up_ref,
1595
11
        evp_cipher_free);
1596
11
}