Coverage Report

Created: 2025-06-13 06:56

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