Coverage Report

Created: 2025-12-31 06:58

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