Coverage Report

Created: 2025-06-13 06:58

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