Coverage Report

Created: 2026-09-12 06:55

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