Coverage Report

Created: 2025-12-10 06:24

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