Coverage Report

Created: 2025-06-13 06:58

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