Coverage Report

Created: 2026-04-08 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
0
{
30
0
    if (ctx == NULL)
31
0
        return 1;
32
33
0
    if (ctx->cipher == NULL || ctx->cipher->prov == NULL)
34
0
        return 1;
35
36
0
    if (ctx->algctx != NULL) {
37
0
        if (ctx->cipher->freectx != NULL)
38
0
            ctx->cipher->freectx(ctx->algctx);
39
0
        ctx->algctx = NULL;
40
0
    }
41
0
    if (ctx->fetched_cipher != NULL)
42
0
        EVP_CIPHER_free(ctx->fetched_cipher);
43
0
    memset(ctx, 0, sizeof(*ctx));
44
0
    ctx->iv_len = -1;
45
46
0
    return 1;
47
0
}
48
49
EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
50
96
{
51
96
    EVP_CIPHER_CTX *ctx;
52
53
96
    ctx = OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
54
96
    if (ctx == NULL)
55
0
        return NULL;
56
57
96
    ctx->iv_len = -1;
58
96
    return ctx;
59
96
}
60
61
void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
62
0
{
63
0
    if (ctx == NULL)
64
0
        return;
65
0
    EVP_CIPHER_CTX_reset(ctx);
66
0
    OPENSSL_free(ctx);
67
0
}
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
656
{
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
656
    if (enc == -1) {
82
560
        enc = ctx->encrypt;
83
560
    } else {
84
96
        if (enc)
85
96
            enc = 1;
86
96
        ctx->encrypt = enc;
87
96
    }
88
89
656
    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
656
    if (cipher != NULL && ctx->cipher != NULL) {
96
0
        unsigned long flags = ctx->flags;
97
98
0
        EVP_CIPHER_CTX_reset(ctx);
99
        /* Restore encrypt and flags */
100
0
        ctx->encrypt = enc;
101
0
        ctx->flags = flags;
102
0
    }
103
104
656
    if (cipher == NULL)
105
560
        cipher = ctx->cipher;
106
107
656
    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
656
    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
656
    if (cipher != ctx->fetched_cipher) {
132
96
        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
96
        EVP_CIPHER_free(ctx->fetched_cipher);
137
        /* Coverity false positive, the reference counting is confusing it */
138
        /* coverity[use_after_free] */
139
96
        ctx->fetched_cipher = (EVP_CIPHER *)cipher;
140
96
    }
141
656
    ctx->cipher = cipher;
142
143
656
    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
656
    if (ctx->algctx == NULL) {
149
96
        ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
150
96
        if (ctx->algctx == NULL) {
151
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
152
0
            return 0;
153
0
        }
154
96
    }
155
156
656
    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
656
#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
656
    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
656
#endif
200
201
656
    if (is_pipeline)
202
0
        return 1;
203
204
656
    if (enc) {
205
656
        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
656
        return ctx->cipher->einit(ctx->algctx,
222
656
            key,
223
656
            key == NULL ? 0
224
656
                        : EVP_CIPHER_CTX_get_key_length(ctx),
225
656
            iv,
226
656
            iv == NULL ? 0
227
656
                       : EVP_CIPHER_CTX_get_iv_length(ctx),
228
656
            params);
229
656
    }
230
231
0
    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
0
    return ctx->cipher->dinit(ctx->algctx,
248
0
        key,
249
0
        key == NULL ? 0
250
0
                    : EVP_CIPHER_CTX_get_key_length(ctx),
251
0
        iv,
252
0
        iv == NULL ? 0
253
0
                   : EVP_CIPHER_CTX_get_iv_length(ctx),
254
0
        params);
255
0
}
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
0
{
397
0
    return evp_cipher_init_internal(ctx, cipher, key, iv, enc, 0, params);
398
0
}
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
656
{
412
656
    if (!ossl_assert(impl == NULL))
413
0
        return 0;
414
656
    return evp_cipher_init_internal(ctx, cipher, key, iv, enc, 0, NULL);
415
656
}
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
1.13k
{
480
1.13k
    if (ctx->encrypt)
481
1.13k
        return EVP_EncryptUpdate(ctx, out, outl, in, inl);
482
0
    else
483
0
        return EVP_DecryptUpdate(ctx, out, outl, in, inl);
484
1.13k
}
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
0
{
523
0
    if (ctx->encrypt)
524
0
        return EVP_EncryptFinal_ex(ctx, out, outl);
525
0
    else
526
0
        return EVP_DecryptFinal_ex(ctx, out, outl);
527
0
}
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
0
{
580
0
    if (!ossl_assert(impl == NULL))
581
0
        return 0;
582
0
    return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 1);
583
0
}
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
0
{
589
0
    return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params);
590
0
}
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
0
{
602
0
    if (!ossl_assert(impl == NULL))
603
0
        return 0;
604
0
    return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 0);
605
0
}
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
1.13k
{
653
1.13k
    int ret;
654
1.13k
    size_t soutl, inl_ = (size_t)inl;
655
1.13k
    int blocksize;
656
657
1.13k
    if (inl < 0) {
658
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
659
0
        return 0;
660
0
    }
661
662
1.13k
    if (ossl_likely(outl != NULL)) {
663
1.13k
        *outl = 0;
664
1.13k
    } 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
1.13k
    if (ossl_unlikely(!ctx->encrypt)) {
671
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
672
0
        return 0;
673
0
    }
674
675
1.13k
    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
1.13k
    if (ossl_unlikely(ctx->cipher->prov == NULL))
681
0
        return 0;
682
683
1.13k
    blocksize = ctx->cipher->block_size;
684
685
1.13k
    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
1.13k
    ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
691
1.13k
        inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
692
1.13k
        in, inl_);
693
694
1.13k
    if (ossl_likely(ret)) {
695
1.13k
        if (ossl_unlikely(soutl > INT_MAX)) {
696
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
697
0
            return 0;
698
0
        }
699
1.13k
        *outl = (int)soutl;
700
1.13k
    }
701
702
1.13k
    return ret;
703
1.13k
}
704
705
int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
706
0
{
707
0
    int ret;
708
0
    ret = EVP_EncryptFinal_ex(ctx, out, outl);
709
0
    return ret;
710
0
}
711
712
int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
713
0
{
714
0
    int ret;
715
0
    size_t soutl;
716
0
    int blocksize;
717
718
0
    if (outl != NULL) {
719
0
        *outl = 0;
720
0
    } 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
0
    if (!ctx->encrypt) {
727
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
728
0
        return 0;
729
0
    }
730
731
0
    if (ctx->cipher == NULL) {
732
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
733
0
        return 0;
734
0
    }
735
0
    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
0
    blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
741
742
0
    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
0
    ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
748
0
        blocksize == 1 ? 0 : blocksize);
749
750
0
    if (ret) {
751
0
        if (soutl > INT_MAX) {
752
0
            ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
753
0
            return 0;
754
0
        }
755
0
        *outl = (int)soutl;
756
0
    }
757
758
0
    return ret;
759
0
}
760
761
int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
762
    const unsigned char *in, int inl)
763
0
{
764
0
    int ret;
765
0
    size_t soutl, inl_ = (size_t)inl;
766
0
    int blocksize;
767
768
0
    if (inl < 0) {
769
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
770
0
        return 0;
771
0
    }
772
773
0
    if (ossl_likely(outl != NULL)) {
774
0
        *outl = 0;
775
0
    } 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
0
    if (ossl_unlikely(ctx->encrypt)) {
782
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
783
0
        return 0;
784
0
    }
785
786
0
    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
0
    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
0
    blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
796
797
0
    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
0
    ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
802
0
        inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
803
0
        in, inl_);
804
805
0
    if (ossl_likely(ret)) {
806
0
        if (ossl_unlikely(soutl > INT_MAX)) {
807
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
808
0
            return 0;
809
0
        }
810
0
        *outl = (int)soutl;
811
0
    }
812
813
0
    return ret;
814
0
}
815
816
int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
817
0
{
818
0
    int ret;
819
0
    ret = EVP_DecryptFinal_ex(ctx, out, outl);
820
0
    return ret;
821
0
}
822
823
int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
824
0
{
825
0
    size_t soutl;
826
0
    int ret;
827
0
    int blocksize;
828
829
0
    if (outl != NULL) {
830
0
        *outl = 0;
831
0
    } 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
0
    if (ctx->encrypt) {
838
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
839
0
        return 0;
840
0
    }
841
842
0
    if (ctx->cipher == NULL) {
843
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
844
0
        return 0;
845
0
    }
846
847
0
    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
0
    blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
853
854
0
    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
0
    ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
860
0
        blocksize == 1 ? 0 : blocksize);
861
862
0
    if (ret) {
863
0
        if (soutl > INT_MAX) {
864
0
            ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
865
0
            return 0;
866
0
        }
867
0
        *outl = (int)soutl;
868
0
    }
869
870
0
    return ret;
871
0
}
872
873
int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
874
0
{
875
0
    int ok;
876
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
877
0
    size_t len;
878
879
0
    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
0
    if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
885
0
        return 1;
886
887
    /* Check the cipher actually understands this parameter */
888
0
    if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
889
0
            OSSL_CIPHER_PARAM_KEYLEN)
890
0
        == NULL) {
891
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
892
0
        return 0;
893
0
    }
894
895
0
    params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
896
0
    if (!OSSL_PARAM_set_int(params, keylen))
897
0
        return 0;
898
0
    ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params);
899
0
    if (ok <= 0)
900
0
        return 0;
901
0
    c->key_len = keylen;
902
0
    return 1;
903
0
}
904
905
int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
906
0
{
907
0
    int ok;
908
0
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
909
0
    unsigned int pd = pad;
910
911
0
    if (pad)
912
0
        ctx->flags &= ~EVP_CIPH_NO_PADDING;
913
0
    else
914
0
        ctx->flags |= EVP_CIPH_NO_PADDING;
915
916
0
    if (ctx->cipher != NULL && ctx->cipher->prov == NULL)
917
0
        return 1;
918
0
    params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
919
0
    ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
920
921
0
    return ok != 0;
922
0
}
923
924
int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
925
0
{
926
0
    int ret = EVP_CTRL_RET_UNSUPPORTED;
927
0
    int set_params = 1;
928
0
    size_t sz = arg;
929
0
    unsigned int i;
930
0
    OSSL_PARAM params[4] = {
931
0
        OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
932
0
    };
933
934
0
    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
0
    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
0
    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
0
    case EVP_CTRL_AEAD_SET_IVLEN:
973
0
        if (arg < 0)
974
0
            return 0;
975
0
        if (ctx->iv_len == arg)
976
            /* Skip calling into provider if unchanged. */
977
0
            return 1;
978
0
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
979
0
        ctx->iv_len = -1;
980
0
        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
0
    case EVP_CTRL_AEAD_SET_IV_FIXED:
989
0
        params[0] = OSSL_PARAM_construct_octet_string(
990
0
            OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
991
0
        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
0
    case EVP_CTRL_AEAD_GET_TAG:
1020
0
        set_params = 0; /* Fall thru */
1021
0
    case EVP_CTRL_AEAD_SET_TAG:
1022
0
        params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1023
0
            ptr, sz);
1024
0
        break;
1025
0
    case EVP_CTRL_AEAD_TLS1_AAD:
1026
        /* This one does a set and a get - since it returns a size */
1027
0
        params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1028
0
            ptr, sz);
1029
0
        ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1030
0
        if (ret <= 0)
1031
0
            goto end;
1032
0
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1033
0
        ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1034
0
        if (ret <= 0)
1035
0
            goto end;
1036
0
        if (sz > INT_MAX)
1037
0
            return 0;
1038
0
        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
0
    case EVP_CTRL_AEAD_SET_MAC_KEY:
1109
0
        if (arg < 0)
1110
0
            return -1;
1111
0
        params[0] = OSSL_PARAM_construct_octet_string(
1112
0
            OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
1113
0
        break;
1114
0
    }
1115
1116
0
    if (set_params)
1117
0
        ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1118
0
    else
1119
0
        ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1120
1121
0
end:
1122
0
    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
0
    return ret;
1127
0
}
1128
1129
int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1130
0
{
1131
0
    if (cipher != NULL && cipher->get_params != NULL)
1132
0
        return cipher->get_params(params);
1133
0
    return 0;
1134
0
}
1135
1136
int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1137
0
{
1138
0
    int r = 0;
1139
0
    const OSSL_PARAM *p;
1140
1141
0
    if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) {
1142
0
        r = ctx->cipher->set_ctx_params(ctx->algctx, params);
1143
0
        if (r > 0) {
1144
0
            p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
1145
0
            if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->key_len)) {
1146
0
                r = 0;
1147
0
                ctx->key_len = -1;
1148
0
            }
1149
0
        }
1150
0
        if (r > 0) {
1151
0
            p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
1152
0
            if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->iv_len)) {
1153
0
                r = 0;
1154
0
                ctx->iv_len = -1;
1155
0
            }
1156
0
        }
1157
0
    }
1158
0
    return r;
1159
0
}
1160
1161
int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1162
0
{
1163
0
    if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1164
0
        return ctx->cipher->get_ctx_params(ctx->algctx, params);
1165
0
    return 0;
1166
0
}
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
0
{
1178
0
    void *provctx;
1179
1180
0
    if (cipher != NULL && cipher->settable_ctx_params != NULL) {
1181
0
        provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1182
0
        return cipher->settable_ctx_params(NULL, provctx);
1183
0
    }
1184
0
    return NULL;
1185
0
}
1186
1187
const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
1188
2.08k
{
1189
2.08k
    void *provctx;
1190
1191
2.08k
    if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
1192
2.08k
        provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1193
2.08k
        return cipher->gettable_ctx_params(NULL, provctx);
1194
2.08k
    }
1195
0
    return NULL;
1196
2.08k
}
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
0
{
1267
0
    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
0
    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
0
    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
0
    EVP_CIPHER_CTX_reset(out);
1283
1284
0
    *out = *in;
1285
0
    out->algctx = NULL;
1286
1287
0
    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
0
    out->algctx = in->cipher->dupctx(in->algctx);
1293
0
    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
0
    return 1;
1299
0
}
1300
1301
EVP_CIPHER *evp_cipher_new(void)
1302
2.08k
{
1303
2.08k
    EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1304
1305
2.08k
    if (cipher != NULL && !CRYPTO_NEW_REF(&cipher->refcnt, 1)) {
1306
0
        OPENSSL_free(cipher);
1307
0
        return NULL;
1308
0
    }
1309
2.08k
    return cipher;
1310
2.08k
}
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
3.88k
{
1321
3.88k
    int nid;
1322
3.88k
    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
3.88k
    const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
1329
1330
3.88k
    if (*legacy_nid == -1) /* We found a clash already */
1331
0
        return;
1332
3.88k
    if (legacy_method == NULL)
1333
1.74k
        return;
1334
2.14k
    nid = EVP_CIPHER_get_nid(legacy_method);
1335
2.14k
    if (*legacy_nid != NID_undef && *legacy_nid != nid) {
1336
0
        *legacy_nid = -1;
1337
0
        return;
1338
0
    }
1339
2.14k
    *legacy_nid = nid;
1340
2.14k
}
1341
#endif
1342
1343
static void *evp_cipher_from_algorithm(const int name_id,
1344
    const OSSL_ALGORITHM *algodef,
1345
    OSSL_PROVIDER *prov)
1346
2.08k
{
1347
2.08k
    const OSSL_DISPATCH *fns = algodef->implementation;
1348
2.08k
    EVP_CIPHER *cipher = NULL;
1349
2.08k
    int fnciphcnt = 0, encinit = 0, decinit = 0, fnpipecnt = 0, fnctxcnt = 0;
1350
1351
2.08k
    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
2.08k
#ifndef FIPS_MODULE
1357
2.08k
    cipher->nid = NID_undef;
1358
2.08k
    if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
1359
2.08k
        || cipher->nid == -1) {
1360
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1361
0
        goto err;
1362
0
    }
1363
2.08k
#endif
1364
1365
2.08k
    cipher->name_id = name_id;
1366
2.08k
    if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
1367
0
        goto err;
1368
1369
2.08k
    cipher->description = algodef->algorithm_description;
1370
1371
33.1k
    for (; fns->function_id != 0; fns++) {
1372
31.0k
        switch (fns->function_id) {
1373
2.08k
        case OSSL_FUNC_CIPHER_NEWCTX:
1374
2.08k
            if (cipher->newctx != NULL)
1375
0
                break;
1376
2.08k
            cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
1377
2.08k
            fnctxcnt++;
1378
2.08k
            break;
1379
2.08k
        case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1380
2.08k
            if (cipher->einit != NULL)
1381
0
                break;
1382
2.08k
            cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
1383
2.08k
            encinit = 1;
1384
2.08k
            break;
1385
2.08k
        case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1386
2.08k
            if (cipher->dinit != NULL)
1387
0
                break;
1388
2.08k
            cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
1389
2.08k
            decinit = 1;
1390
2.08k
            break;
1391
1.08k
        case OSSL_FUNC_CIPHER_ENCRYPT_SKEY_INIT:
1392
1.08k
            if (cipher->einit_skey != NULL)
1393
0
                break;
1394
1.08k
            cipher->einit_skey = OSSL_FUNC_cipher_encrypt_skey_init(fns);
1395
1.08k
            encinit = 1;
1396
1.08k
            break;
1397
1.08k
        case OSSL_FUNC_CIPHER_DECRYPT_SKEY_INIT:
1398
1.08k
            if (cipher->dinit_skey != NULL)
1399
0
                break;
1400
1.08k
            cipher->dinit_skey = OSSL_FUNC_cipher_decrypt_skey_init(fns);
1401
1.08k
            decinit = 1;
1402
1.08k
            break;
1403
2.08k
        case OSSL_FUNC_CIPHER_UPDATE:
1404
2.08k
            if (cipher->cupdate != NULL)
1405
0
                break;
1406
2.08k
            cipher->cupdate = OSSL_FUNC_cipher_update(fns);
1407
2.08k
            fnciphcnt++;
1408
2.08k
            break;
1409
2.08k
        case OSSL_FUNC_CIPHER_FINAL:
1410
2.08k
            if (cipher->cfinal != NULL)
1411
0
                break;
1412
2.08k
            cipher->cfinal = OSSL_FUNC_cipher_final(fns);
1413
2.08k
            fnciphcnt++;
1414
2.08k
            break;
1415
1.88k
        case OSSL_FUNC_CIPHER_CIPHER:
1416
1.88k
            if (cipher->ccipher != NULL)
1417
0
                break;
1418
1.88k
            cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
1419
1.88k
            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
2.08k
        case OSSL_FUNC_CIPHER_FREECTX:
1445
2.08k
            if (cipher->freectx != NULL)
1446
0
                break;
1447
2.08k
            cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
1448
2.08k
            fnctxcnt++;
1449
2.08k
            break;
1450
2.06k
        case OSSL_FUNC_CIPHER_DUPCTX:
1451
2.06k
            if (cipher->dupctx != NULL)
1452
0
                break;
1453
2.06k
            cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
1454
2.06k
            break;
1455
2.08k
        case OSSL_FUNC_CIPHER_GET_PARAMS:
1456
2.08k
            if (cipher->get_params != NULL)
1457
0
                break;
1458
2.08k
            cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
1459
2.08k
            break;
1460
2.08k
        case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1461
2.08k
            if (cipher->get_ctx_params != NULL)
1462
0
                break;
1463
2.08k
            cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
1464
2.08k
            break;
1465
2.08k
        case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1466
2.08k
            if (cipher->set_ctx_params != NULL)
1467
0
                break;
1468
2.08k
            cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
1469
2.08k
            break;
1470
2.08k
        case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1471
2.08k
            if (cipher->gettable_params != NULL)
1472
0
                break;
1473
2.08k
            cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
1474
2.08k
            break;
1475
2.08k
        case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1476
2.08k
            if (cipher->gettable_ctx_params != NULL)
1477
0
                break;
1478
2.08k
            cipher->gettable_ctx_params = OSSL_FUNC_cipher_gettable_ctx_params(fns);
1479
2.08k
            break;
1480
2.08k
        case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1481
2.08k
            if (cipher->settable_ctx_params != NULL)
1482
0
                break;
1483
2.08k
            cipher->settable_ctx_params = OSSL_FUNC_cipher_settable_ctx_params(fns);
1484
2.08k
            break;
1485
31.0k
        }
1486
31.0k
    }
1487
2.08k
    fnciphcnt += encinit + decinit;
1488
2.08k
    if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1489
2.08k
        || (fnciphcnt == 0 && cipher->ccipher == NULL && fnpipecnt == 0)
1490
2.08k
        || (fnpipecnt != 0 && (fnpipecnt < 3 || cipher->p_cupdate == NULL || cipher->p_cfinal == NULL))
1491
2.08k
        || fnctxcnt != 2
1492
2.08k
        || cipher->get_params == NULL) {
1493
        /*
1494
         * In order to be a consistent set of functions we must have at least
1495
         * a complete set of "encrypt" functions, or a complete set of "decrypt"
1496
         * functions, or a single "cipher" function.
1497
         */
1498
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1499
0
        goto err;
1500
0
    }
1501
2.08k
    if (prov != NULL && !ossl_provider_up_ref(prov))
1502
0
        goto err;
1503
1504
2.08k
    cipher->prov = prov;
1505
1506
2.08k
    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
2.08k
    return cipher;
1512
1513
0
err:
1514
0
    EVP_CIPHER_free(cipher);
1515
0
    return NULL;
1516
2.08k
}
1517
1518
static int evp_cipher_up_ref(void *cipher)
1519
2.20k
{
1520
2.20k
    return EVP_CIPHER_up_ref(cipher);
1521
2.20k
}
1522
1523
static void evp_cipher_free(void *cipher)
1524
2.08k
{
1525
2.08k
    EVP_CIPHER_free(cipher);
1526
2.08k
}
1527
1528
EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1529
    const char *properties)
1530
0
{
1531
0
    EVP_CIPHER *cipher = evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1532
0
        evp_cipher_from_algorithm, evp_cipher_up_ref,
1533
0
        evp_cipher_free);
1534
1535
0
    return cipher;
1536
0
}
1537
1538
EVP_CIPHER *evp_cipher_fetch_from_prov(OSSL_PROVIDER *prov,
1539
    const char *algorithm,
1540
    const char *properties)
1541
64
{
1542
64
    return evp_generic_fetch_from_prov(prov, OSSL_OP_CIPHER,
1543
64
        algorithm, properties,
1544
64
        evp_cipher_from_algorithm,
1545
64
        evp_cipher_up_ref,
1546
64
        evp_cipher_free);
1547
64
}
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.30k
{
1560
2.30k
    int ref = 0;
1561
1562
2.30k
    if (cipher->origin == EVP_ORIG_DYNAMIC)
1563
2.30k
        CRYPTO_UP_REF(&cipher->refcnt, &ref);
1564
2.30k
    return 1;
1565
2.30k
}
1566
1567
void evp_cipher_free_int(EVP_CIPHER *cipher)
1568
0
{
1569
0
    OPENSSL_free(cipher->type_name);
1570
0
    ossl_provider_free(cipher->prov);
1571
0
    CRYPTO_FREE_REF(&cipher->refcnt);
1572
0
    OPENSSL_free(cipher);
1573
0
}
1574
1575
void EVP_CIPHER_free(EVP_CIPHER *cipher)
1576
2.24k
{
1577
2.24k
    int i;
1578
1579
2.24k
    if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC)
1580
160
        return;
1581
1582
2.08k
    CRYPTO_DOWN_REF(&cipher->refcnt, &i);
1583
2.08k
    if (i > 0)
1584
2.08k
        return;
1585
0
    evp_cipher_free_int(cipher);
1586
0
}
1587
1588
void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
1589
    void (*fn)(EVP_CIPHER *mac, void *arg),
1590
    void *arg)
1591
0
{
1592
0
    evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1593
0
        (void (*)(void *, void *))fn, arg,
1594
0
        evp_cipher_from_algorithm, evp_cipher_up_ref,
1595
0
        evp_cipher_free);
1596
0
}