Coverage Report

Created: 2023-06-07 07:24

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