Coverage Report

Created: 2026-04-01 06:39

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
816k
{
35
816k
    if (ctx == NULL)
36
0
        return 1;
37
38
816k
    if (ctx->cipher == NULL || ctx->cipher->prov == NULL)
39
18.3k
        goto legacy;
40
41
797k
    if (ctx->algctx != NULL) {
42
797k
        if (ctx->cipher->freectx != NULL)
43
797k
            ctx->cipher->freectx(ctx->algctx);
44
797k
        ctx->algctx = NULL;
45
797k
    }
46
797k
    if (ctx->fetched_cipher != NULL)
47
797k
        EVP_CIPHER_free(ctx->fetched_cipher);
48
797k
    memset(ctx, 0, sizeof(*ctx));
49
797k
    ctx->iv_len = -1;
50
51
797k
    return 1;
52
53
    /* Remove legacy code below when legacy support is removed. */
54
18.3k
legacy:
55
56
18.3k
    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
18.3k
    OPENSSL_free(ctx->cipher_data);
64
18.3k
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
65
18.3k
    ENGINE_finish(ctx->engine);
66
18.3k
#endif
67
18.3k
    memset(ctx, 0, sizeof(*ctx));
68
18.3k
    ctx->iv_len = -1;
69
18.3k
    return 1;
70
18.3k
}
71
72
EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
73
1.09M
{
74
1.09M
    EVP_CIPHER_CTX *ctx;
75
76
1.09M
    ctx = OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
77
1.09M
    if (ctx == NULL)
78
0
        return NULL;
79
80
1.09M
    ctx->iv_len = -1;
81
1.09M
    return ctx;
82
1.09M
}
83
84
void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
85
1.83M
{
86
1.83M
    if (ctx == NULL)
87
733k
        return;
88
1.10M
    EVP_CIPHER_CTX_reset(ctx);
89
1.10M
    OPENSSL_free(ctx);
90
1.10M
}
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.82M
{
98
3.82M
    int n;
99
3.82M
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
100
3.82M
    ENGINE *tmpimpl = NULL;
101
3.82M
#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.82M
    if (enc == -1) {
109
233k
        enc = ctx->encrypt;
110
3.58M
    } else {
111
3.58M
        if (enc)
112
2.84M
            enc = 1;
113
3.58M
        ctx->encrypt = enc;
114
3.58M
    }
115
116
3.82M
    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.82M
#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.82M
    if (ctx->engine && ctx->cipher
131
0
        && (cipher == NULL || cipher->nid == ctx->cipher->nid))
132
0
        goto skip_to_init;
133
134
3.82M
    if (cipher != NULL && impl == NULL) {
135
        /* Ask if an ENGINE is reserved for this job */
136
388k
        tmpimpl = ENGINE_get_cipher_engine(cipher->nid);
137
388k
    }
138
3.82M
#endif
139
140
    /*
141
     * If there are engines involved then we should use legacy handling for now.
142
     */
143
3.82M
    if (ctx->engine != NULL
144
3.82M
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
145
3.82M
        || tmpimpl != NULL
146
3.82M
#endif
147
3.82M
        || impl != NULL
148
3.82M
        || (cipher != NULL && cipher->origin == EVP_ORIG_METH)
149
3.82M
        || (cipher == NULL && ctx->cipher != NULL
150
3.43M
            && 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.82M
    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.82M
    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.82M
    if (cipher == NULL)
181
3.43M
        cipher = ctx->cipher;
182
183
3.82M
    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.82M
    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.82M
    if (cipher != ctx->fetched_cipher) {
208
388k
        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
388k
        EVP_CIPHER_free(ctx->fetched_cipher);
213
        /* Coverity false positive, the reference counting is confusing it */
214
        /* coverity[use_after_free] */
215
388k
        ctx->fetched_cipher = (EVP_CIPHER *)cipher;
216
388k
    }
217
3.82M
    ctx->cipher = cipher;
218
3.82M
    if (ctx->algctx == NULL) {
219
388k
        ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
220
388k
        if (ctx->algctx == NULL) {
221
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
222
0
            return 0;
223
0
        }
224
388k
    }
225
226
3.82M
    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.82M
#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.82M
    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.82M
#endif
270
271
3.82M
    if (enc) {
272
3.07M
        if (ctx->cipher->einit == NULL) {
273
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
274
0
            return 0;
275
0
        }
276
277
3.07M
        return ctx->cipher->einit(ctx->algctx,
278
3.07M
            key,
279
3.07M
            key == NULL ? 0
280
3.07M
                        : EVP_CIPHER_CTX_get_key_length(ctx),
281
3.07M
            iv,
282
3.07M
            iv == NULL ? 0
283
3.07M
                       : EVP_CIPHER_CTX_get_iv_length(ctx),
284
3.07M
            params);
285
3.07M
    }
286
287
743k
    if (ctx->cipher->dinit == NULL) {
288
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
289
0
        return 0;
290
0
    }
291
292
743k
    return ctx->cipher->dinit(ctx->algctx,
293
743k
        key,
294
743k
        key == NULL ? 0
295
743k
                    : EVP_CIPHER_CTX_get_key_length(ctx),
296
743k
        iv,
297
743k
        iv == NULL ? 0
298
743k
                   : EVP_CIPHER_CTX_get_iv_length(ctx),
299
743k
        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
59.1k
{
447
59.1k
    return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, params);
448
59.1k
}
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
6.71M
{
462
6.71M
    return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, NULL);
463
6.71M
}
464
465
int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
466
    const unsigned char *in, int inl)
467
14.6M
{
468
14.6M
    if (ctx->encrypt)
469
11.2M
        return EVP_EncryptUpdate(ctx, out, outl, in, inl);
470
3.35M
    else
471
3.35M
        return EVP_DecryptUpdate(ctx, out, outl, in, inl);
472
14.6M
}
473
474
int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
475
4.05M
{
476
4.05M
    if (ctx->encrypt)
477
2.73M
        return EVP_EncryptFinal_ex(ctx, out, outl);
478
1.32M
    else
479
1.32M
        return EVP_DecryptFinal_ex(ctx, out, outl);
480
4.05M
}
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
42.9k
{
500
42.9k
    return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
501
42.9k
}
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
59.1k
{
507
59.1k
    return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params);
508
59.1k
}
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.22k
{
520
1.22k
    return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
521
1.22k
}
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
9.28M
{
659
9.28M
    int ret;
660
9.28M
    size_t soutl, inl_ = (size_t)inl;
661
9.28M
    int blocksize;
662
663
9.28M
    if (inl < 0) {
664
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
665
0
        return 0;
666
0
    }
667
668
9.28M
    if (ossl_likely(outl != NULL)) {
669
9.28M
        *outl = 0;
670
9.28M
    } else {
671
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
672
0
        return 0;
673
0
    }
674
675
    /* Prevent accidental use of decryption context when encrypting */
676
9.28M
    if (ossl_unlikely(!ctx->encrypt)) {
677
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
678
0
        return 0;
679
0
    }
680
681
9.28M
    if (ossl_unlikely(ctx->cipher == NULL)) {
682
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
683
0
        return 0;
684
0
    }
685
686
9.28M
    if (ossl_unlikely(ctx->cipher->prov == NULL))
687
0
        goto legacy;
688
689
9.28M
    blocksize = ctx->cipher->block_size;
690
691
9.28M
    if (ossl_unlikely(ctx->cipher->cupdate == NULL || blocksize < 1)) {
692
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
693
0
        return 0;
694
0
    }
695
696
9.28M
    ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
697
9.28M
        inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
698
9.28M
        in, inl_);
699
700
9.28M
    if (ossl_likely(ret)) {
701
9.28M
        if (soutl > INT_MAX) {
702
3
            ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
703
3
            return 0;
704
3
        }
705
9.28M
        *outl = soutl;
706
9.28M
    }
707
708
9.28M
    return ret;
709
710
    /* Code below to be removed when legacy support is dropped. */
711
0
legacy:
712
713
0
    return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
714
9.28M
}
715
716
int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
717
84
{
718
84
    int ret;
719
84
    ret = EVP_EncryptFinal_ex(ctx, out, outl);
720
84
    return ret;
721
84
}
722
723
int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
724
2.09M
{
725
2.09M
    int n, ret;
726
2.09M
    unsigned int i, b, bl;
727
2.09M
    size_t soutl;
728
2.09M
    int blocksize;
729
730
2.09M
    if (outl != NULL) {
731
2.09M
        *outl = 0;
732
2.09M
    } else {
733
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
734
0
        return 0;
735
0
    }
736
737
    /* Prevent accidental use of decryption context when encrypting */
738
2.09M
    if (!ctx->encrypt) {
739
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
740
0
        return 0;
741
0
    }
742
743
2.09M
    if (ctx->cipher == NULL) {
744
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
745
0
        return 0;
746
0
    }
747
2.09M
    if (ctx->cipher->prov == NULL)
748
0
        goto legacy;
749
750
2.09M
    blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
751
752
2.09M
    if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
753
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
754
0
        return 0;
755
0
    }
756
757
2.09M
    ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
758
2.09M
        blocksize == 1 ? 0 : blocksize);
759
760
2.09M
    if (ret) {
761
2.09M
        if (soutl > INT_MAX) {
762
0
            ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
763
0
            return 0;
764
0
        }
765
2.09M
        *outl = soutl;
766
2.09M
    }
767
768
2.09M
    return ret;
769
770
    /* Code below to be removed when legacy support is dropped. */
771
0
legacy:
772
773
0
    if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
774
0
        ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
775
0
        if (ret < 0)
776
0
            return 0;
777
0
        else
778
0
            *outl = ret;
779
0
        return 1;
780
0
    }
781
782
0
    b = ctx->cipher->block_size;
783
0
    OPENSSL_assert(b <= sizeof(ctx->buf));
784
0
    if (b == 1) {
785
0
        *outl = 0;
786
0
        return 1;
787
0
    }
788
0
    bl = ctx->buf_len;
789
0
    if (ctx->flags & EVP_CIPH_NO_PADDING) {
790
0
        if (bl) {
791
0
            ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
792
0
            return 0;
793
0
        }
794
0
        *outl = 0;
795
0
        return 1;
796
0
    }
797
798
0
    n = b - bl;
799
0
    for (i = bl; i < b; i++)
800
0
        ctx->buf[i] = n;
801
0
    ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
802
803
0
    if (ret)
804
0
        *outl = b;
805
806
0
    return ret;
807
0
}
808
809
int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
810
    const unsigned char *in, int inl)
811
1.92M
{
812
1.92M
    int fix_len, cmpl = inl, ret;
813
1.92M
    unsigned int b;
814
1.92M
    size_t soutl, inl_ = (size_t)inl;
815
1.92M
    int blocksize;
816
817
1.92M
    if (inl < 0) {
818
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH);
819
0
        return 0;
820
0
    }
821
822
1.92M
    if (ossl_likely(outl != NULL)) {
823
1.92M
        *outl = 0;
824
1.92M
    } else {
825
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
826
0
        return 0;
827
0
    }
828
829
    /* Prevent accidental use of encryption context when decrypting */
830
1.92M
    if (ossl_unlikely(ctx->encrypt)) {
831
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
832
0
        return 0;
833
0
    }
834
835
1.92M
    if (ossl_unlikely(ctx->cipher == NULL)) {
836
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
837
0
        return 0;
838
0
    }
839
1.92M
    if (ossl_unlikely(ctx->cipher->prov == NULL))
840
0
        goto legacy;
841
842
1.92M
    blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
843
844
1.92M
    if (ossl_unlikely(ctx->cipher->cupdate == NULL || blocksize < 1)) {
845
0
        ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
846
0
        return 0;
847
0
    }
848
1.92M
    ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
849
1.92M
        inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
850
1.92M
        in, inl_);
851
852
1.92M
    if (ossl_likely(ret)) {
853
1.77M
        if (soutl > INT_MAX) {
854
0
            ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
855
0
            return 0;
856
0
        }
857
1.77M
        *outl = soutl;
858
1.77M
    }
859
860
1.92M
    return ret;
861
862
    /* Code below to be removed when legacy support is dropped. */
863
0
legacy:
864
865
0
    b = ctx->cipher->block_size;
866
867
0
    if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
868
0
        cmpl = safe_div_round_up_int(cmpl, 8, NULL);
869
870
0
    if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
871
0
        if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
872
0
            ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
873
0
            return 0;
874
0
        }
875
876
0
        fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
877
0
        if (fix_len < 0) {
878
0
            *outl = 0;
879
0
            return 0;
880
0
        } else
881
0
            *outl = fix_len;
882
0
        return 1;
883
0
    }
884
885
0
    if (inl <= 0) {
886
0
        *outl = 0;
887
0
        return inl == 0;
888
0
    }
889
890
0
    if (ctx->flags & EVP_CIPH_NO_PADDING)
891
0
        return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
892
893
0
    OPENSSL_assert(b <= sizeof(ctx->final));
894
895
0
    if (ctx->final_used) {
896
        /* see comment about PTRDIFF_T comparison above */
897
0
        if (((PTRDIFF_T)out == (PTRDIFF_T)in)
898
0
            || ossl_is_partially_overlapping(out, in, b)) {
899
0
            ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
900
0
            return 0;
901
0
        }
902
        /*
903
         * final_used is only ever set if buf_len is 0. Therefore the maximum
904
         * length output we will ever see from evp_EncryptDecryptUpdate is
905
         * the maximum multiple of the block length that is <= inl, or just:
906
         * inl & ~(b - 1)
907
         * Since final_used has been set then the final output length is:
908
         * (inl & ~(b - 1)) + b
909
         * This must never exceed INT_MAX
910
         */
911
0
        if ((inl & ~(b - 1)) > INT_MAX - b) {
912
0
            ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
913
0
            return 0;
914
0
        }
915
0
        memcpy(out, ctx->final, b);
916
0
        out += b;
917
0
        fix_len = 1;
918
0
    } else
919
0
        fix_len = 0;
920
921
0
    if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
922
0
        return 0;
923
924
    /*
925
     * if we have 'decrypted' a multiple of block size, make sure we have a
926
     * copy of this last block
927
     */
928
0
    if (b > 1 && !ctx->buf_len) {
929
0
        *outl -= b;
930
0
        ctx->final_used = 1;
931
0
        memcpy(ctx->final, &out[*outl], b);
932
0
    } else
933
0
        ctx->final_used = 0;
934
935
0
    if (fix_len)
936
0
        *outl += b;
937
938
0
    return 1;
939
0
}
940
941
int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
942
1.08k
{
943
1.08k
    int ret;
944
1.08k
    ret = EVP_DecryptFinal_ex(ctx, out, outl);
945
1.08k
    return ret;
946
1.08k
}
947
948
int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
949
382k
{
950
382k
    int i, n;
951
382k
    unsigned int b;
952
382k
    size_t soutl;
953
382k
    int ret;
954
382k
    int blocksize;
955
956
382k
    if (outl != NULL) {
957
382k
        *outl = 0;
958
382k
    } else {
959
0
        ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
960
0
        return 0;
961
0
    }
962
963
    /* Prevent accidental use of encryption context when decrypting */
964
382k
    if (ctx->encrypt) {
965
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
966
0
        return 0;
967
0
    }
968
969
382k
    if (ctx->cipher == NULL) {
970
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
971
0
        return 0;
972
0
    }
973
974
382k
    if (ctx->cipher->prov == NULL)
975
0
        goto legacy;
976
977
382k
    blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
978
979
382k
    if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
980
0
        ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
981
0
        return 0;
982
0
    }
983
984
382k
    ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
985
382k
        blocksize == 1 ? 0 : blocksize);
986
987
382k
    if (ret) {
988
1.35k
        if (soutl > INT_MAX) {
989
0
            ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
990
0
            return 0;
991
0
        }
992
1.35k
        *outl = soutl;
993
1.35k
    }
994
995
382k
    return ret;
996
997
    /* Code below to be removed when legacy support is dropped. */
998
0
legacy:
999
1000
0
    *outl = 0;
1001
0
    if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
1002
0
        i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
1003
0
        if (i < 0)
1004
0
            return 0;
1005
0
        else
1006
0
            *outl = i;
1007
0
        return 1;
1008
0
    }
1009
1010
0
    b = ctx->cipher->block_size;
1011
0
    if (ctx->flags & EVP_CIPH_NO_PADDING) {
1012
0
        if (ctx->buf_len) {
1013
0
            ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
1014
0
            return 0;
1015
0
        }
1016
0
        *outl = 0;
1017
0
        return 1;
1018
0
    }
1019
0
    if (b > 1) {
1020
0
        if (ctx->buf_len || !ctx->final_used) {
1021
0
            ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
1022
0
            return 0;
1023
0
        }
1024
0
        OPENSSL_assert(b <= sizeof(ctx->final));
1025
1026
        /*
1027
         * The following assumes that the ciphertext has been authenticated.
1028
         * Otherwise it provides a padding oracle.
1029
         */
1030
0
        n = ctx->final[b - 1];
1031
0
        if (n == 0 || n > (int)b) {
1032
0
            ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
1033
0
            return 0;
1034
0
        }
1035
0
        for (i = 0; i < n; i++) {
1036
0
            if (ctx->final[--b] != n) {
1037
0
                ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
1038
0
                return 0;
1039
0
            }
1040
0
        }
1041
0
        n = ctx->cipher->block_size - n;
1042
0
        for (i = 0; i < n; i++)
1043
0
            out[i] = ctx->final[i];
1044
0
        *outl = n;
1045
0
    } else
1046
0
        *outl = 0;
1047
0
    return 1;
1048
0
}
1049
1050
int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
1051
419
{
1052
419
    if (c->cipher->prov != NULL) {
1053
419
        int ok;
1054
419
        OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1055
419
        size_t len;
1056
1057
419
        if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
1058
247
            return 1;
1059
1060
        /* Check the cipher actually understands this parameter */
1061
172
        if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
1062
172
                OSSL_CIPHER_PARAM_KEYLEN)
1063
172
            == NULL) {
1064
121
            ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1065
121
            return 0;
1066
121
        }
1067
1068
51
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
1069
51
        if (!OSSL_PARAM_set_int(params, keylen))
1070
0
            return 0;
1071
51
        ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params);
1072
51
        if (ok <= 0)
1073
51
            return 0;
1074
0
        c->key_len = keylen;
1075
0
        return 1;
1076
51
    }
1077
1078
    /* Code below to be removed when legacy support is dropped. */
1079
1080
    /*
1081
     * Note there have never been any built-in ciphers that define this flag
1082
     * since it was first introduced.
1083
     */
1084
0
    if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
1085
0
        return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
1086
0
    if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
1087
0
        return 1;
1088
0
    if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
1089
0
        c->key_len = keylen;
1090
0
        return 1;
1091
0
    }
1092
0
    ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1093
0
    return 0;
1094
0
}
1095
1096
int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
1097
1.29k
{
1098
1.29k
    int ok;
1099
1.29k
    OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1100
1.29k
    unsigned int pd = pad;
1101
1102
1.29k
    if (pad)
1103
0
        ctx->flags &= ~EVP_CIPH_NO_PADDING;
1104
1.29k
    else
1105
1.29k
        ctx->flags |= EVP_CIPH_NO_PADDING;
1106
1107
1.29k
    if (ctx->cipher != NULL && ctx->cipher->prov == NULL)
1108
0
        return 1;
1109
1.29k
    params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
1110
1.29k
    ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1111
1112
1.29k
    return ok != 0;
1113
1.29k
}
1114
1115
int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1116
2.40M
{
1117
2.40M
    int ret = EVP_CTRL_RET_UNSUPPORTED;
1118
2.40M
    int set_params = 1;
1119
2.40M
    size_t sz = arg;
1120
2.40M
    unsigned int i;
1121
2.40M
    OSSL_PARAM params[4] = {
1122
2.40M
        OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
1123
2.40M
    };
1124
1125
2.40M
    if (ctx == NULL || ctx->cipher == NULL) {
1126
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
1127
0
        return 0;
1128
0
    }
1129
1130
2.40M
    if (ctx->cipher->prov == NULL)
1131
0
        goto legacy;
1132
1133
2.40M
    switch (type) {
1134
0
    case EVP_CTRL_SET_KEY_LENGTH:
1135
0
        if (arg < 0)
1136
0
            return 0;
1137
0
        if (ctx->key_len == arg)
1138
            /* Skip calling into provider if unchanged. */
1139
0
            return 1;
1140
0
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
1141
0
        ctx->key_len = -1;
1142
0
        break;
1143
0
    case EVP_CTRL_RAND_KEY: /* Used by DES */
1144
0
        set_params = 0;
1145
0
        params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
1146
0
            ptr, sz);
1147
0
        break;
1148
1149
0
    case EVP_CTRL_INIT:
1150
        /*
1151
         * EVP_CTRL_INIT is purely legacy, no provider counterpart.
1152
         * As a matter of fact, this should be dead code, but some caller
1153
         * might still do a direct control call with this command, so...
1154
         * Legacy methods return 1 except for exceptional circumstances, so
1155
         * we do the same here to not be disruptive.
1156
         */
1157
0
        return 1;
1158
0
    case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
1159
0
    default:
1160
0
        goto end;
1161
5.71k
    case EVP_CTRL_AEAD_SET_IVLEN:
1162
5.71k
        if (arg < 0)
1163
0
            return 0;
1164
5.71k
        if (ctx->iv_len == arg)
1165
            /* Skip calling into provider if unchanged. */
1166
0
            return 1;
1167
5.71k
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1168
5.71k
        ctx->iv_len = -1;
1169
5.71k
        break;
1170
0
    case EVP_CTRL_CCM_SET_L:
1171
0
        if (arg < 2 || arg > 8)
1172
0
            return 0;
1173
0
        sz = 15 - arg;
1174
0
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1175
0
        ctx->iv_len = -1;
1176
0
        break;
1177
4.30k
    case EVP_CTRL_AEAD_SET_IV_FIXED:
1178
4.30k
        params[0] = OSSL_PARAM_construct_octet_string(
1179
4.30k
            OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
1180
4.30k
        break;
1181
0
    case EVP_CTRL_GCM_IV_GEN:
1182
0
        set_params = 0;
1183
0
        if (arg < 0)
1184
0
            sz = 0; /* special case that uses the iv length */
1185
0
        params[0] = OSSL_PARAM_construct_octet_string(
1186
0
            OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
1187
0
        break;
1188
0
    case EVP_CTRL_GCM_SET_IV_INV:
1189
0
        if (arg < 0)
1190
0
            return 0;
1191
0
        params[0] = OSSL_PARAM_construct_octet_string(
1192
0
            OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
1193
0
        break;
1194
0
    case EVP_CTRL_GET_RC5_ROUNDS:
1195
0
        set_params = 0; /* Fall thru */
1196
0
    case EVP_CTRL_SET_RC5_ROUNDS:
1197
0
        if (arg < 0)
1198
0
            return 0;
1199
0
        i = (unsigned int)arg;
1200
0
        params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
1201
0
        break;
1202
0
    case EVP_CTRL_SET_SPEED:
1203
0
        if (arg < 0)
1204
0
            return 0;
1205
0
        i = (unsigned int)arg;
1206
0
        params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
1207
0
        break;
1208
1.55M
    case EVP_CTRL_AEAD_GET_TAG:
1209
1.55M
        set_params = 0; /* Fall thru */
1210
2.26M
    case EVP_CTRL_AEAD_SET_TAG:
1211
2.26M
        params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1212
2.26M
            ptr, sz);
1213
2.26M
        break;
1214
121k
    case EVP_CTRL_AEAD_TLS1_AAD:
1215
        /* This one does a set and a get - since it returns a size */
1216
121k
        params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1217
121k
            ptr, sz);
1218
121k
        ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1219
121k
        if (ret <= 0)
1220
182
            goto end;
1221
121k
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1222
121k
        ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1223
121k
        if (ret <= 0)
1224
0
            goto end;
1225
121k
        return sz;
1226
0
#ifndef OPENSSL_NO_RC2
1227
0
    case EVP_CTRL_GET_RC2_KEY_BITS:
1228
0
        set_params = 0; /* Fall thru */
1229
0
    case EVP_CTRL_SET_RC2_KEY_BITS:
1230
0
        params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
1231
0
        break;
1232
0
#endif /* OPENSSL_NO_RC2 */
1233
0
#if !defined(OPENSSL_NO_MULTIBLOCK)
1234
0
    case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
1235
0
        params[0] = OSSL_PARAM_construct_size_t(
1236
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
1237
0
        ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1238
0
        if (ret <= 0)
1239
0
            return 0;
1240
1241
0
        params[0] = OSSL_PARAM_construct_size_t(
1242
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
1243
0
        params[1] = OSSL_PARAM_construct_end();
1244
0
        ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1245
0
        if (ret <= 0)
1246
0
            return 0;
1247
0
        return sz;
1248
0
    case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
1249
0
        EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p = (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1250
1251
0
        if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
1252
0
            return 0;
1253
1254
0
        params[0] = OSSL_PARAM_construct_octet_string(
1255
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void *)p->inp, p->len);
1256
0
        params[1] = OSSL_PARAM_construct_uint(
1257
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1258
0
        ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1259
0
        if (ret <= 0)
1260
0
            return ret;
1261
        /* Retrieve the return values changed by the set */
1262
0
        params[0] = OSSL_PARAM_construct_size_t(
1263
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
1264
0
        params[1] = OSSL_PARAM_construct_uint(
1265
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1266
0
        params[2] = OSSL_PARAM_construct_end();
1267
0
        ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1268
0
        if (ret <= 0)
1269
0
            return 0;
1270
0
        return sz;
1271
0
    }
1272
0
    case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
1273
0
        EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p = (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1274
1275
0
        params[0] = OSSL_PARAM_construct_octet_string(
1276
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
1277
1278
0
        params[1] = OSSL_PARAM_construct_octet_string(
1279
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void *)p->inp,
1280
0
            p->len);
1281
0
        params[2] = OSSL_PARAM_construct_uint(
1282
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1283
0
        ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1284
0
        if (ret <= 0)
1285
0
            return ret;
1286
0
        params[0] = OSSL_PARAM_construct_size_t(
1287
0
            OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
1288
0
        params[1] = OSSL_PARAM_construct_end();
1289
0
        ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1290
0
        if (ret <= 0)
1291
0
            return 0;
1292
0
        return sz;
1293
0
    }
1294
0
#endif /* OPENSSL_NO_MULTIBLOCK */
1295
2.34k
    case EVP_CTRL_AEAD_SET_MAC_KEY:
1296
2.34k
        if (arg < 0)
1297
0
            return -1;
1298
2.34k
        params[0] = OSSL_PARAM_construct_octet_string(
1299
2.34k
            OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
1300
2.34k
        break;
1301
2.40M
    }
1302
1303
2.27M
    if (set_params)
1304
725k
        ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1305
1.55M
    else
1306
1.55M
        ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1307
2.27M
    goto end;
1308
1309
    /* Code below to be removed when legacy support is dropped. */
1310
0
legacy:
1311
0
    if (ctx->cipher->ctrl == NULL) {
1312
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
1313
0
        return 0;
1314
0
    }
1315
1316
0
    ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
1317
1318
2.27M
end:
1319
2.27M
    if (ret == EVP_CTRL_RET_UNSUPPORTED) {
1320
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
1321
0
        return 0;
1322
0
    }
1323
2.27M
    return ret;
1324
2.27M
}
1325
1326
int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1327
1.78M
{
1328
1.78M
    if (cipher != NULL && cipher->get_params != NULL)
1329
1.78M
        return cipher->get_params(params);
1330
0
    return 0;
1331
1.78M
}
1332
1333
int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1334
29.9k
{
1335
29.9k
    int r = 0;
1336
29.9k
    const OSSL_PARAM *p;
1337
1338
29.9k
    if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) {
1339
29.8k
        r = ctx->cipher->set_ctx_params(ctx->algctx, params);
1340
29.8k
        if (r > 0) {
1341
29.7k
            p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
1342
29.7k
            if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->key_len)) {
1343
0
                r = 0;
1344
0
                ctx->key_len = -1;
1345
0
            }
1346
29.7k
        }
1347
29.8k
        if (r > 0) {
1348
29.7k
            p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
1349
29.7k
            if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->iv_len)) {
1350
0
                r = 0;
1351
0
                ctx->iv_len = -1;
1352
0
            }
1353
29.7k
        }
1354
29.8k
    }
1355
29.9k
    return r;
1356
29.9k
}
1357
1358
int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1359
507k
{
1360
507k
    if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1361
507k
        return ctx->cipher->get_ctx_params(ctx->algctx, params);
1362
0
    return 0;
1363
507k
}
1364
1365
const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1366
0
{
1367
0
    if (cipher != NULL && cipher->gettable_params != NULL)
1368
0
        return cipher->gettable_params(
1369
0
            ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)));
1370
0
    return NULL;
1371
0
}
1372
1373
const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
1374
546
{
1375
546
    void *provctx;
1376
1377
546
    if (cipher != NULL && cipher->settable_ctx_params != NULL) {
1378
546
        provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1379
546
        return cipher->settable_ctx_params(NULL, provctx);
1380
546
    }
1381
0
    return NULL;
1382
546
}
1383
1384
const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
1385
8.56k
{
1386
8.56k
    void *provctx;
1387
1388
8.56k
    if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
1389
8.56k
        provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1390
8.56k
        return cipher->gettable_ctx_params(NULL, provctx);
1391
8.56k
    }
1392
0
    return NULL;
1393
8.56k
}
1394
1395
const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx)
1396
0
{
1397
0
    void *alg;
1398
1399
0
    if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
1400
0
        alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1401
0
        return cctx->cipher->settable_ctx_params(cctx->algctx, alg);
1402
0
    }
1403
0
    return NULL;
1404
0
}
1405
1406
const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx)
1407
0
{
1408
0
    void *provctx;
1409
1410
0
    if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
1411
0
        provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1412
0
        return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx);
1413
0
    }
1414
0
    return NULL;
1415
0
}
1416
1417
#ifndef FIPS_MODULE
1418
static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx)
1419
0
{
1420
0
    const EVP_CIPHER *cipher = ctx->cipher;
1421
0
    const OSSL_PROVIDER *prov;
1422
1423
0
    if (cipher == NULL)
1424
0
        return NULL;
1425
1426
0
    prov = EVP_CIPHER_get0_provider(cipher);
1427
0
    return ossl_provider_libctx(prov);
1428
0
}
1429
#endif
1430
1431
int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
1432
0
{
1433
0
    if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1434
0
        return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1435
1436
#ifdef FIPS_MODULE
1437
    return 0;
1438
#else
1439
0
    {
1440
0
        int kl;
1441
0
        OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
1442
1443
0
        kl = EVP_CIPHER_CTX_get_key_length(ctx);
1444
0
        if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0)
1445
0
            return 0;
1446
0
        return 1;
1447
0
    }
1448
0
#endif /* FIPS_MODULE */
1449
0
}
1450
1451
EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in)
1452
0
{
1453
0
    EVP_CIPHER_CTX *out = EVP_CIPHER_CTX_new();
1454
1455
0
    if (out != NULL && !EVP_CIPHER_CTX_copy(out, in)) {
1456
0
        EVP_CIPHER_CTX_free(out);
1457
0
        out = NULL;
1458
0
    }
1459
0
    return out;
1460
0
}
1461
1462
int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
1463
258
{
1464
258
    if ((in == NULL) || (in->cipher == NULL)) {
1465
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
1466
0
        return 0;
1467
0
    }
1468
1469
258
    if (in->cipher->prov == NULL)
1470
0
        goto legacy;
1471
1472
258
    if (in->cipher->dupctx == NULL) {
1473
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1474
0
        return 0;
1475
0
    }
1476
1477
258
    EVP_CIPHER_CTX_reset(out);
1478
1479
258
    *out = *in;
1480
258
    out->algctx = NULL;
1481
1482
258
    if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
1483
0
        out->fetched_cipher = NULL;
1484
0
        return 0;
1485
0
    }
1486
1487
258
    out->algctx = in->cipher->dupctx(in->algctx);
1488
258
    if (out->algctx == NULL) {
1489
0
        ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1490
0
        return 0;
1491
0
    }
1492
1493
258
    return 1;
1494
1495
    /* Code below to be removed when legacy support is dropped. */
1496
0
legacy:
1497
1498
0
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1499
    /* Make sure it's safe to copy a cipher context using an ENGINE */
1500
0
    if (in->engine && !ENGINE_init(in->engine)) {
1501
0
        ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
1502
0
        return 0;
1503
0
    }
1504
0
#endif
1505
1506
0
    EVP_CIPHER_CTX_reset(out);
1507
0
    memcpy(out, in, sizeof(*out));
1508
1509
0
    if (in->cipher_data && in->cipher->ctx_size) {
1510
0
        out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
1511
0
        if (out->cipher_data == NULL) {
1512
0
            out->cipher = NULL;
1513
0
            return 0;
1514
0
        }
1515
0
        memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
1516
0
    }
1517
1518
0
    if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
1519
0
        if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
1520
0
            out->cipher = NULL;
1521
0
            ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
1522
0
            return 0;
1523
0
        }
1524
0
    return 1;
1525
0
}
1526
1527
EVP_CIPHER *evp_cipher_new(void)
1528
8.19k
{
1529
8.19k
    EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1530
1531
8.19k
    if (cipher != NULL && !CRYPTO_NEW_REF(&cipher->refcnt, 1)) {
1532
0
        OPENSSL_free(cipher);
1533
0
        return NULL;
1534
0
    }
1535
8.19k
    return cipher;
1536
8.19k
}
1537
1538
/*
1539
 * FIPS module note: since internal fetches will be entirely
1540
 * provider based, we know that none of its code depends on legacy
1541
 * NIDs or any functionality that use them.
1542
 */
1543
#ifndef FIPS_MODULE
1544
/* After removal of legacy support get rid of the need for legacy NIDs */
1545
static void set_legacy_nid(const char *name, void *vlegacy_nid)
1546
16.0k
{
1547
16.0k
    int nid;
1548
16.0k
    int *legacy_nid = vlegacy_nid;
1549
    /*
1550
     * We use lowest level function to get the associated method, because
1551
     * higher level functions such as EVP_get_cipherbyname() have changed
1552
     * to look at providers too.
1553
     */
1554
16.0k
    const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
1555
1556
16.0k
    if (*legacy_nid == -1) /* We found a clash already */
1557
0
        return;
1558
16.0k
    if (legacy_method == NULL)
1559
7.17k
        return;
1560
8.83k
    nid = EVP_CIPHER_get_nid(legacy_method);
1561
8.83k
    if (*legacy_nid != NID_undef && *legacy_nid != nid) {
1562
0
        *legacy_nid = -1;
1563
0
        return;
1564
0
    }
1565
8.83k
    *legacy_nid = nid;
1566
8.83k
}
1567
#endif
1568
1569
static void *evp_cipher_from_algorithm(const int name_id,
1570
    const OSSL_ALGORITHM *algodef,
1571
    OSSL_PROVIDER *prov)
1572
2.84k
{
1573
2.84k
    const OSSL_DISPATCH *fns = algodef->implementation;
1574
2.84k
    EVP_CIPHER *cipher = NULL;
1575
2.84k
    int fnciphcnt = 0, fnctxcnt = 0;
1576
1577
2.84k
    if ((cipher = evp_cipher_new()) == NULL) {
1578
0
        ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
1579
0
        return NULL;
1580
0
    }
1581
1582
2.84k
#ifndef FIPS_MODULE
1583
2.84k
    cipher->nid = NID_undef;
1584
2.84k
    if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
1585
2.84k
        || cipher->nid == -1) {
1586
0
        ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1587
0
        EVP_CIPHER_free(cipher);
1588
0
        return NULL;
1589
0
    }
1590
2.84k
#endif
1591
1592
2.84k
    cipher->name_id = name_id;
1593
2.84k
    if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
1594
0
        EVP_CIPHER_free(cipher);
1595
0
        return NULL;
1596
0
    }
1597
2.84k
    cipher->description = algodef->algorithm_description;
1598
1599
42.3k
    for (; fns->function_id != 0; fns++) {
1600
39.5k
        switch (fns->function_id) {
1601
2.84k
        case OSSL_FUNC_CIPHER_NEWCTX:
1602
2.84k
            if (cipher->newctx != NULL)
1603
0
                break;
1604
2.84k
            cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
1605
2.84k
            fnctxcnt++;
1606
2.84k
            break;
1607
2.84k
        case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1608
2.84k
            if (cipher->einit != NULL)
1609
0
                break;
1610
2.84k
            cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
1611
2.84k
            fnciphcnt++;
1612
2.84k
            break;
1613
2.84k
        case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1614
2.84k
            if (cipher->dinit != NULL)
1615
0
                break;
1616
2.84k
            cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
1617
2.84k
            fnciphcnt++;
1618
2.84k
            break;
1619
2.84k
        case OSSL_FUNC_CIPHER_UPDATE:
1620
2.84k
            if (cipher->cupdate != NULL)
1621
0
                break;
1622
2.84k
            cipher->cupdate = OSSL_FUNC_cipher_update(fns);
1623
2.84k
            fnciphcnt++;
1624
2.84k
            break;
1625
2.84k
        case OSSL_FUNC_CIPHER_FINAL:
1626
2.84k
            if (cipher->cfinal != NULL)
1627
0
                break;
1628
2.84k
            cipher->cfinal = OSSL_FUNC_cipher_final(fns);
1629
2.84k
            fnciphcnt++;
1630
2.84k
            break;
1631
2.57k
        case OSSL_FUNC_CIPHER_CIPHER:
1632
2.57k
            if (cipher->ccipher != NULL)
1633
0
                break;
1634
2.57k
            cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
1635
2.57k
            break;
1636
2.84k
        case OSSL_FUNC_CIPHER_FREECTX:
1637
2.84k
            if (cipher->freectx != NULL)
1638
0
                break;
1639
2.84k
            cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
1640
2.84k
            fnctxcnt++;
1641
2.84k
            break;
1642
2.82k
        case OSSL_FUNC_CIPHER_DUPCTX:
1643
2.82k
            if (cipher->dupctx != NULL)
1644
0
                break;
1645
2.82k
            cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
1646
2.82k
            break;
1647
2.84k
        case OSSL_FUNC_CIPHER_GET_PARAMS:
1648
2.84k
            if (cipher->get_params != NULL)
1649
0
                break;
1650
2.84k
            cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
1651
2.84k
            break;
1652
2.84k
        case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1653
2.84k
            if (cipher->get_ctx_params != NULL)
1654
0
                break;
1655
2.84k
            cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
1656
2.84k
            break;
1657
2.84k
        case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1658
2.84k
            if (cipher->set_ctx_params != NULL)
1659
0
                break;
1660
2.84k
            cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
1661
2.84k
            break;
1662
2.84k
        case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1663
2.84k
            if (cipher->gettable_params != NULL)
1664
0
                break;
1665
2.84k
            cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
1666
2.84k
            break;
1667
2.84k
        case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1668
2.84k
            if (cipher->gettable_ctx_params != NULL)
1669
0
                break;
1670
2.84k
            cipher->gettable_ctx_params = OSSL_FUNC_cipher_gettable_ctx_params(fns);
1671
2.84k
            break;
1672
2.84k
        case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1673
2.84k
            if (cipher->settable_ctx_params != NULL)
1674
0
                break;
1675
2.84k
            cipher->settable_ctx_params = OSSL_FUNC_cipher_settable_ctx_params(fns);
1676
2.84k
            break;
1677
39.5k
        }
1678
39.5k
    }
1679
2.84k
    if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1680
2.84k
        || (fnciphcnt == 0 && cipher->ccipher == NULL)
1681
2.84k
        || fnctxcnt != 2) {
1682
        /*
1683
         * In order to be a consistent set of functions we must have at least
1684
         * a complete set of "encrypt" functions, or a complete set of "decrypt"
1685
         * functions, or a single "cipher" function. In all cases we need both
1686
         * the "newctx" and "freectx" functions.
1687
         */
1688
0
        EVP_CIPHER_free(cipher);
1689
0
        ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1690
0
        return NULL;
1691
0
    }
1692
2.84k
    cipher->prov = prov;
1693
2.84k
    if (prov != NULL)
1694
2.84k
        ossl_provider_up_ref(prov);
1695
1696
2.84k
    if (!evp_cipher_cache_constants(cipher)) {
1697
0
        EVP_CIPHER_free(cipher);
1698
0
        ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1699
0
        cipher = NULL;
1700
0
    }
1701
1702
2.84k
    return cipher;
1703
2.84k
}
1704
1705
static int evp_cipher_up_ref(void *cipher)
1706
3.45M
{
1707
3.45M
    return EVP_CIPHER_up_ref(cipher);
1708
3.45M
}
1709
1710
static void evp_cipher_free(void *cipher)
1711
15.0k
{
1712
15.0k
    EVP_CIPHER_free(cipher);
1713
15.0k
}
1714
1715
EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1716
    const char *properties)
1717
4.94M
{
1718
4.94M
    EVP_CIPHER *cipher = evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1719
4.94M
        evp_cipher_from_algorithm, evp_cipher_up_ref,
1720
4.94M
        evp_cipher_free);
1721
1722
4.94M
    return cipher;
1723
4.94M
}
1724
1725
int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
1726
4.58M
{
1727
4.58M
    int ref = 0;
1728
1729
4.58M
    if (cipher->origin == EVP_ORIG_DYNAMIC)
1730
4.58M
        CRYPTO_UP_REF(&cipher->refcnt, &ref);
1731
4.58M
    return 1;
1732
4.58M
}
1733
1734
void evp_cipher_free_int(EVP_CIPHER *cipher)
1735
5.83k
{
1736
5.83k
    OPENSSL_free(cipher->type_name);
1737
5.83k
    ossl_provider_free(cipher->prov);
1738
5.83k
    CRYPTO_FREE_REF(&cipher->refcnt);
1739
5.83k
    OPENSSL_free(cipher);
1740
5.83k
}
1741
1742
void EVP_CIPHER_free(EVP_CIPHER *cipher)
1743
6.25M
{
1744
6.25M
    int i;
1745
1746
6.25M
    if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC)
1747
1.65M
        return;
1748
1749
4.59M
    CRYPTO_DOWN_REF(&cipher->refcnt, &i);
1750
4.59M
    if (i > 0)
1751
4.58M
        return;
1752
5.83k
    evp_cipher_free_int(cipher);
1753
5.83k
}
1754
1755
void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
1756
    void (*fn)(EVP_CIPHER *mac, void *arg),
1757
    void *arg)
1758
8
{
1759
8
    evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1760
8
        (void (*)(void *, void *))fn, arg,
1761
8
        evp_cipher_from_algorithm, evp_cipher_up_ref,
1762
8
        evp_cipher_free);
1763
8
}