Coverage Report

Created: 2025-07-12 07:07

/src/irssi/subprojects/openssl-1.1.1l/crypto/evp/evp_enc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (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 "internal/cryptlib.h"
14
#include <openssl/evp.h>
15
#include <openssl/err.h>
16
#include <openssl/rand.h>
17
#include <openssl/rand_drbg.h>
18
#include <openssl/engine.h>
19
#include "crypto/evp.h"
20
#include "evp_local.h"
21
22
int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *c)
23
9
{
24
9
    if (c == NULL)
25
0
        return 1;
26
9
    if (c->cipher != NULL) {
27
9
        if (c->cipher->cleanup && !c->cipher->cleanup(c))
28
0
            return 0;
29
        /* Cleanse cipher context data */
30
9
        if (c->cipher_data && c->cipher->ctx_size)
31
9
            OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
32
9
    }
33
9
    OPENSSL_free(c->cipher_data);
34
9
#ifndef OPENSSL_NO_ENGINE
35
9
    ENGINE_finish(c->engine);
36
9
#endif
37
9
    memset(c, 0, sizeof(*c));
38
9
    return 1;
39
9
}
40
41
EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
42
9
{
43
9
    return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
44
9
}
45
46
void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
47
9
{
48
9
    EVP_CIPHER_CTX_reset(ctx);
49
9
    OPENSSL_free(ctx);
50
9
}
51
52
int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
53
                   const unsigned char *key, const unsigned char *iv, int enc)
54
0
{
55
0
    if (cipher != NULL)
56
0
        EVP_CIPHER_CTX_reset(ctx);
57
0
    return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
58
0
}
59
60
int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
61
                      ENGINE *impl, const unsigned char *key,
62
                      const unsigned char *iv, int enc)
63
2.83M
{
64
2.83M
    if (enc == -1)
65
2.83M
        enc = ctx->encrypt;
66
9
    else {
67
9
        if (enc)
68
9
            enc = 1;
69
9
        ctx->encrypt = enc;
70
9
    }
71
2.83M
#ifndef OPENSSL_NO_ENGINE
72
    /*
73
     * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
74
     * this context may already have an ENGINE! Try to avoid releasing the
75
     * previous handle, re-querying for an ENGINE, and having a
76
     * reinitialisation, when it may all be unnecessary.
77
     */
78
2.83M
    if (ctx->engine && ctx->cipher
79
2.83M
        && (cipher == NULL || cipher->nid == ctx->cipher->nid))
80
0
        goto skip_to_init;
81
2.83M
#endif
82
2.83M
    if (cipher) {
83
        /*
84
         * Ensure a context left lying around from last time is cleared (the
85
         * previous check attempted to avoid this if the same ENGINE and
86
         * EVP_CIPHER could be used).
87
         */
88
9
        if (ctx->cipher) {
89
0
            unsigned long flags = ctx->flags;
90
0
            EVP_CIPHER_CTX_reset(ctx);
91
            /* Restore encrypt and flags */
92
0
            ctx->encrypt = enc;
93
0
            ctx->flags = flags;
94
0
        }
95
9
#ifndef OPENSSL_NO_ENGINE
96
9
        if (impl) {
97
0
            if (!ENGINE_init(impl)) {
98
0
                EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
99
0
                return 0;
100
0
            }
101
0
        } else
102
            /* Ask if an ENGINE is reserved for this job */
103
9
            impl = ENGINE_get_cipher_engine(cipher->nid);
104
9
        if (impl) {
105
            /* There's an ENGINE for this job ... (apparently) */
106
0
            const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
107
0
            if (!c) {
108
                /*
109
                 * One positive side-effect of US's export control history,
110
                 * is that we should at least be able to avoid using US
111
                 * misspellings of "initialisation"?
112
                 */
113
0
                EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
114
0
                return 0;
115
0
            }
116
            /* We'll use the ENGINE's private cipher definition */
117
0
            cipher = c;
118
            /*
119
             * Store the ENGINE functional reference so we know 'cipher' came
120
             * from an ENGINE and we need to release it when done.
121
             */
122
0
            ctx->engine = impl;
123
0
        } else
124
9
            ctx->engine = NULL;
125
9
#endif
126
127
9
        ctx->cipher = cipher;
128
9
        if (ctx->cipher->ctx_size) {
129
9
            ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
130
9
            if (ctx->cipher_data == NULL) {
131
0
                ctx->cipher = NULL;
132
0
                EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
133
0
                return 0;
134
0
            }
135
9
        } else {
136
0
            ctx->cipher_data = NULL;
137
0
        }
138
9
        ctx->key_len = cipher->key_len;
139
        /* Preserve wrap enable flag, zero everything else */
140
9
        ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
141
9
        if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
142
0
            if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
143
0
                ctx->cipher = NULL;
144
0
                EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
145
0
                return 0;
146
0
            }
147
0
        }
148
2.83M
    } else if (!ctx->cipher) {
149
0
        EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET);
150
0
        return 0;
151
0
    }
152
2.83M
#ifndef OPENSSL_NO_ENGINE
153
2.83M
 skip_to_init:
154
2.83M
#endif
155
    /* we assume block size is a power of 2 in *cryptUpdate */
156
2.83M
    OPENSSL_assert(ctx->cipher->block_size == 1
157
2.83M
                   || ctx->cipher->block_size == 8
158
2.83M
                   || ctx->cipher->block_size == 16);
159
160
2.83M
    if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
161
2.83M
        && EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) {
162
0
        EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_WRAP_MODE_NOT_ALLOWED);
163
0
        return 0;
164
0
    }
165
166
2.83M
    if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_CUSTOM_IV)) {
167
2.83M
        switch (EVP_CIPHER_CTX_mode(ctx)) {
168
169
0
        case EVP_CIPH_STREAM_CIPHER:
170
1.41M
        case EVP_CIPH_ECB_MODE:
171
1.41M
            break;
172
173
0
        case EVP_CIPH_CFB_MODE:
174
0
        case EVP_CIPH_OFB_MODE:
175
176
0
            ctx->num = 0;
177
            /* fall-through */
178
179
0
        case EVP_CIPH_CBC_MODE:
180
181
0
            OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
182
0
                           (int)sizeof(ctx->iv));
183
0
            if (iv)
184
0
                memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
185
0
            memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
186
0
            break;
187
188
1.41M
        case EVP_CIPH_CTR_MODE:
189
1.41M
            ctx->num = 0;
190
            /* Don't reuse IV for CTR mode */
191
1.41M
            if (iv)
192
472k
                memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
193
1.41M
            break;
194
195
0
        default:
196
0
            return 0;
197
2.83M
        }
198
2.83M
    }
199
200
2.83M
    if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
201
2.36M
        if (!ctx->cipher->init(ctx, key, iv, enc))
202
0
            return 0;
203
2.36M
    }
204
2.83M
    ctx->buf_len = 0;
205
2.83M
    ctx->final_used = 0;
206
2.83M
    ctx->block_mask = ctx->cipher->block_size - 1;
207
2.83M
    return 1;
208
2.83M
}
209
210
int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
211
                     const unsigned char *in, int inl)
212
4.72M
{
213
4.72M
    if (ctx->encrypt)
214
4.72M
        return EVP_EncryptUpdate(ctx, out, outl, in, inl);
215
0
    else
216
0
        return EVP_DecryptUpdate(ctx, out, outl, in, inl);
217
4.72M
}
218
219
int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
220
0
{
221
0
    if (ctx->encrypt)
222
0
        return EVP_EncryptFinal_ex(ctx, out, outl);
223
0
    else
224
0
        return EVP_DecryptFinal_ex(ctx, out, outl);
225
0
}
226
227
int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
228
0
{
229
0
    if (ctx->encrypt)
230
0
        return EVP_EncryptFinal(ctx, out, outl);
231
0
    else
232
0
        return EVP_DecryptFinal(ctx, out, outl);
233
0
}
234
235
int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
236
                    const unsigned char *key, const unsigned char *iv)
237
0
{
238
0
    return EVP_CipherInit(ctx, cipher, key, iv, 1);
239
0
}
240
241
int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
242
                       ENGINE *impl, const unsigned char *key,
243
                       const unsigned char *iv)
244
0
{
245
0
    return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
246
0
}
247
248
int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
249
                    const unsigned char *key, const unsigned char *iv)
250
0
{
251
0
    return EVP_CipherInit(ctx, cipher, key, iv, 0);
252
0
}
253
254
int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
255
                       ENGINE *impl, const unsigned char *key,
256
                       const unsigned char *iv)
257
0
{
258
0
    return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
259
0
}
260
261
/*
262
 * According to the letter of standard difference between pointers
263
 * is specified to be valid only within same object. This makes
264
 * it formally challenging to determine if input and output buffers
265
 * are not partially overlapping with standard pointer arithmetic.
266
 */
267
#ifdef PTRDIFF_T
268
# undef PTRDIFF_T
269
#endif
270
#if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
271
/*
272
 * Then we have VMS that distinguishes itself by adhering to
273
 * sizeof(size_t)==4 even in 64-bit builds, which means that
274
 * difference between two pointers might be truncated to 32 bits.
275
 * In the context one can even wonder how comparison for
276
 * equality is implemented. To be on the safe side we adhere to
277
 * PTRDIFF_T even for comparison for equality.
278
 */
279
# define PTRDIFF_T uint64_t
280
#else
281
4.72M
# define PTRDIFF_T size_t
282
#endif
283
284
int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
285
4.72M
{
286
4.72M
    PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
287
    /*
288
     * Check for partially overlapping buffers. [Binary logical
289
     * operations are used instead of boolean to minimize number
290
     * of conditional branches.]
291
     */
292
4.72M
    int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
293
4.72M
                                                (diff > (0 - (PTRDIFF_T)len)));
294
295
4.72M
    return overlapped;
296
4.72M
}
297
298
static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
299
                                    unsigned char *out, int *outl,
300
                                    const unsigned char *in, int inl)
301
4.72M
{
302
4.72M
    int i, j, bl, cmpl = inl;
303
304
4.72M
    if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
305
0
        cmpl = (cmpl + 7) / 8;
306
307
4.72M
    bl = ctx->cipher->block_size;
308
309
    /*
310
     * CCM mode needs to know about the case where inl == 0 && in == NULL - it
311
     * means the plaintext/ciphertext length is 0
312
     */
313
4.72M
    if (inl < 0
314
4.72M
            || (inl == 0
315
4.72M
                && EVP_CIPHER_mode(ctx->cipher) != EVP_CIPH_CCM_MODE)) {
316
0
        *outl = 0;
317
0
        return inl == 0;
318
0
    }
319
320
4.72M
    if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
321
        /* If block size > 1 then the cipher will have to do this check */
322
0
        if (bl == 1 && is_partially_overlapping(out, in, cmpl)) {
323
0
            EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
324
0
            return 0;
325
0
        }
326
327
0
        i = ctx->cipher->do_cipher(ctx, out, in, inl);
328
0
        if (i < 0)
329
0
            return 0;
330
0
        else
331
0
            *outl = i;
332
0
        return 1;
333
0
    }
334
335
4.72M
    if (is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
336
0
        EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
337
0
        return 0;
338
0
    }
339
340
4.72M
    if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
341
4.72M
        if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
342
4.72M
            *outl = inl;
343
4.72M
            return 1;
344
4.72M
        } else {
345
0
            *outl = 0;
346
0
            return 0;
347
0
        }
348
4.72M
    }
349
0
    i = ctx->buf_len;
350
0
    OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
351
0
    if (i != 0) {
352
0
        if (bl - i > inl) {
353
0
            memcpy(&(ctx->buf[i]), in, inl);
354
0
            ctx->buf_len += inl;
355
0
            *outl = 0;
356
0
            return 1;
357
0
        } else {
358
0
            j = bl - i;
359
360
            /*
361
             * Once we've processed the first j bytes from in, the amount of
362
             * data left that is a multiple of the block length is:
363
             * (inl - j) & ~(bl - 1)
364
             * We must ensure that this amount of data, plus the one block that
365
             * we process from ctx->buf does not exceed INT_MAX
366
             */
367
0
            if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
368
0
                EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE,
369
0
                       EVP_R_OUTPUT_WOULD_OVERFLOW);
370
0
                return 0;
371
0
            }
372
0
            memcpy(&(ctx->buf[i]), in, j);
373
0
            inl -= j;
374
0
            in += j;
375
0
            if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
376
0
                return 0;
377
0
            out += bl;
378
0
            *outl = bl;
379
0
        }
380
0
    } else
381
0
        *outl = 0;
382
0
    i = inl & (bl - 1);
383
0
    inl -= i;
384
0
    if (inl > 0) {
385
0
        if (!ctx->cipher->do_cipher(ctx, out, in, inl))
386
0
            return 0;
387
0
        *outl += inl;
388
0
    }
389
390
0
    if (i != 0)
391
0
        memcpy(ctx->buf, &(in[inl]), i);
392
0
    ctx->buf_len = i;
393
0
    return 1;
394
0
}
395
396
397
int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
398
                      const unsigned char *in, int inl)
399
4.72M
{
400
    /* Prevent accidental use of decryption context when encrypting */
401
4.72M
    if (!ctx->encrypt) {
402
0
        EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_INVALID_OPERATION);
403
0
        return 0;
404
0
    }
405
406
4.72M
    return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
407
4.72M
}
408
409
int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
410
0
{
411
0
    int ret;
412
0
    ret = EVP_EncryptFinal_ex(ctx, out, outl);
413
0
    return ret;
414
0
}
415
416
int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
417
0
{
418
0
    int n, ret;
419
0
    unsigned int i, b, bl;
420
421
    /* Prevent accidental use of decryption context when encrypting */
422
0
    if (!ctx->encrypt) {
423
0
        EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
424
0
        return 0;
425
0
    }
426
427
0
    if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
428
0
        ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
429
0
        if (ret < 0)
430
0
            return 0;
431
0
        else
432
0
            *outl = ret;
433
0
        return 1;
434
0
    }
435
436
0
    b = ctx->cipher->block_size;
437
0
    OPENSSL_assert(b <= sizeof(ctx->buf));
438
0
    if (b == 1) {
439
0
        *outl = 0;
440
0
        return 1;
441
0
    }
442
0
    bl = ctx->buf_len;
443
0
    if (ctx->flags & EVP_CIPH_NO_PADDING) {
444
0
        if (bl) {
445
0
            EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,
446
0
                   EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
447
0
            return 0;
448
0
        }
449
0
        *outl = 0;
450
0
        return 1;
451
0
    }
452
453
0
    n = b - bl;
454
0
    for (i = bl; i < b; i++)
455
0
        ctx->buf[i] = n;
456
0
    ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
457
458
0
    if (ret)
459
0
        *outl = b;
460
461
0
    return ret;
462
0
}
463
464
int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
465
                      const unsigned char *in, int inl)
466
0
{
467
0
    int fix_len, cmpl = inl;
468
0
    unsigned int b;
469
470
    /* Prevent accidental use of encryption context when decrypting */
471
0
    if (ctx->encrypt) {
472
0
        EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_INVALID_OPERATION);
473
0
        return 0;
474
0
    }
475
476
0
    b = ctx->cipher->block_size;
477
478
0
    if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
479
0
        cmpl = (cmpl + 7) / 8;
480
481
    /*
482
     * CCM mode needs to know about the case where inl == 0 - it means the
483
     * plaintext/ciphertext length is 0
484
     */
485
0
    if (inl < 0
486
0
            || (inl == 0
487
0
                && EVP_CIPHER_mode(ctx->cipher) != EVP_CIPH_CCM_MODE)) {
488
0
        *outl = 0;
489
0
        return inl == 0;
490
0
    }
491
492
0
    if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
493
0
        if (b == 1 && is_partially_overlapping(out, in, cmpl)) {
494
0
            EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
495
0
            return 0;
496
0
        }
497
498
0
        fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
499
0
        if (fix_len < 0) {
500
0
            *outl = 0;
501
0
            return 0;
502
0
        } else
503
0
            *outl = fix_len;
504
0
        return 1;
505
0
    }
506
507
0
    if (ctx->flags & EVP_CIPH_NO_PADDING)
508
0
        return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
509
510
0
    OPENSSL_assert(b <= sizeof(ctx->final));
511
512
0
    if (ctx->final_used) {
513
        /* see comment about PTRDIFF_T comparison above */
514
0
        if (((PTRDIFF_T)out == (PTRDIFF_T)in)
515
0
            || is_partially_overlapping(out, in, b)) {
516
0
            EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
517
0
            return 0;
518
0
        }
519
        /*
520
         * final_used is only ever set if buf_len is 0. Therefore the maximum
521
         * length output we will ever see from evp_EncryptDecryptUpdate is
522
         * the maximum multiple of the block length that is <= inl, or just:
523
         * inl & ~(b - 1)
524
         * Since final_used has been set then the final output length is:
525
         * (inl & ~(b - 1)) + b
526
         * This must never exceed INT_MAX
527
         */
528
0
        if ((inl & ~(b - 1)) > INT_MAX - b) {
529
0
            EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_OUTPUT_WOULD_OVERFLOW);
530
0
            return 0;
531
0
        }
532
0
        memcpy(out, ctx->final, b);
533
0
        out += b;
534
0
        fix_len = 1;
535
0
    } else
536
0
        fix_len = 0;
537
538
0
    if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
539
0
        return 0;
540
541
    /*
542
     * if we have 'decrypted' a multiple of block size, make sure we have a
543
     * copy of this last block
544
     */
545
0
    if (b > 1 && !ctx->buf_len) {
546
0
        *outl -= b;
547
0
        ctx->final_used = 1;
548
0
        memcpy(ctx->final, &out[*outl], b);
549
0
    } else
550
0
        ctx->final_used = 0;
551
552
0
    if (fix_len)
553
0
        *outl += b;
554
555
0
    return 1;
556
0
}
557
558
int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
559
0
{
560
0
    int ret;
561
0
    ret = EVP_DecryptFinal_ex(ctx, out, outl);
562
0
    return ret;
563
0
}
564
565
int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
566
0
{
567
0
    int i, n;
568
0
    unsigned int b;
569
570
    /* Prevent accidental use of encryption context when decrypting */
571
0
    if (ctx->encrypt) {
572
0
        EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
573
0
        return 0;
574
0
    }
575
576
0
    *outl = 0;
577
578
0
    if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
579
0
        i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
580
0
        if (i < 0)
581
0
            return 0;
582
0
        else
583
0
            *outl = i;
584
0
        return 1;
585
0
    }
586
587
0
    b = ctx->cipher->block_size;
588
0
    if (ctx->flags & EVP_CIPH_NO_PADDING) {
589
0
        if (ctx->buf_len) {
590
0
            EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,
591
0
                   EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
592
0
            return 0;
593
0
        }
594
0
        *outl = 0;
595
0
        return 1;
596
0
    }
597
0
    if (b > 1) {
598
0
        if (ctx->buf_len || !ctx->final_used) {
599
0
            EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
600
0
            return 0;
601
0
        }
602
0
        OPENSSL_assert(b <= sizeof(ctx->final));
603
604
        /*
605
         * The following assumes that the ciphertext has been authenticated.
606
         * Otherwise it provides a padding oracle.
607
         */
608
0
        n = ctx->final[b - 1];
609
0
        if (n == 0 || n > (int)b) {
610
0
            EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
611
0
            return 0;
612
0
        }
613
0
        for (i = 0; i < n; i++) {
614
0
            if (ctx->final[--b] != n) {
615
0
                EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
616
0
                return 0;
617
0
            }
618
0
        }
619
0
        n = ctx->cipher->block_size - n;
620
0
        for (i = 0; i < n; i++)
621
0
            out[i] = ctx->final[i];
622
0
        *outl = n;
623
0
    } else
624
0
        *outl = 0;
625
0
    return 1;
626
0
}
627
628
int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
629
0
{
630
0
    if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
631
0
        return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
632
0
    if (c->key_len == keylen)
633
0
        return 1;
634
0
    if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
635
0
        c->key_len = keylen;
636
0
        return 1;
637
0
    }
638
0
    EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH);
639
0
    return 0;
640
0
}
641
642
int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
643
0
{
644
0
    if (pad)
645
0
        ctx->flags &= ~EVP_CIPH_NO_PADDING;
646
0
    else
647
0
        ctx->flags |= EVP_CIPH_NO_PADDING;
648
0
    return 1;
649
0
}
650
651
int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
652
0
{
653
0
    int ret;
654
655
0
    if (!ctx->cipher) {
656
0
        EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
657
0
        return 0;
658
0
    }
659
660
0
    if (!ctx->cipher->ctrl) {
661
0
        EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
662
0
        return 0;
663
0
    }
664
665
0
    ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
666
0
    if (ret == -1) {
667
0
        EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL,
668
0
               EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
669
0
        return 0;
670
0
    }
671
0
    return ret;
672
0
}
673
674
int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
675
0
{
676
0
    if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
677
0
        return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
678
0
    if (RAND_priv_bytes(key, ctx->key_len) <= 0)
679
0
        return 0;
680
0
    return 1;
681
0
}
682
683
int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
684
0
{
685
0
    if ((in == NULL) || (in->cipher == NULL)) {
686
0
        EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED);
687
0
        return 0;
688
0
    }
689
0
#ifndef OPENSSL_NO_ENGINE
690
    /* Make sure it's safe to copy a cipher context using an ENGINE */
691
0
    if (in->engine && !ENGINE_init(in->engine)) {
692
0
        EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB);
693
0
        return 0;
694
0
    }
695
0
#endif
696
697
0
    EVP_CIPHER_CTX_reset(out);
698
0
    memcpy(out, in, sizeof(*out));
699
700
0
    if (in->cipher_data && in->cipher->ctx_size) {
701
0
        out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
702
0
        if (out->cipher_data == NULL) {
703
0
            out->cipher = NULL;
704
0
            EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE);
705
0
            return 0;
706
0
        }
707
0
        memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
708
0
    }
709
710
0
    if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
711
0
        if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
712
0
            out->cipher = NULL;
713
0
            EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INITIALIZATION_ERROR);
714
0
            return 0;
715
0
        }
716
0
    return 1;
717
0
}