2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include "internal/cryptlib.h"
13 #include <openssl/evp.h>
14 #include <openssl/err.h>
15 #include <openssl/rand.h>
16 #include <openssl/engine.h>
17 #include "internal/evp_int.h"
20 int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX
*c
)
24 if (c
->cipher
!= NULL
) {
25 if (c
->cipher
->cleanup
&& !c
->cipher
->cleanup(c
))
27 /* Cleanse cipher context data */
28 if (c
->cipher_data
&& c
->cipher
->ctx_size
)
29 OPENSSL_cleanse(c
->cipher_data
, c
->cipher
->ctx_size
);
31 OPENSSL_free(c
->cipher_data
);
32 #ifndef OPENSSL_NO_ENGINE
33 ENGINE_finish(c
->engine
);
35 memset(c
, 0, sizeof(*c
));
39 EVP_CIPHER_CTX
*EVP_CIPHER_CTX_new(void)
41 return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX
));
44 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX
*ctx
)
46 EVP_CIPHER_CTX_reset(ctx
);
50 int EVP_CipherInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
51 const unsigned char *key
, const unsigned char *iv
, int enc
)
53 EVP_CIPHER_CTX_reset(ctx
);
54 return EVP_CipherInit_ex(ctx
, cipher
, NULL
, key
, iv
, enc
);
57 int EVP_CipherInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
58 ENGINE
*impl
, const unsigned char *key
,
59 const unsigned char *iv
, int enc
)
68 #ifndef OPENSSL_NO_ENGINE
70 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
71 * this context may already have an ENGINE! Try to avoid releasing the
72 * previous handle, re-querying for an ENGINE, and having a
73 * reinitialisation, when it may all be unnecessary.
75 if (ctx
->engine
&& ctx
->cipher
76 && (cipher
== NULL
|| cipher
->nid
== ctx
->cipher
->nid
))
81 * Ensure a context left lying around from last time is cleared (the
82 * previous check attempted to avoid this if the same ENGINE and
83 * EVP_CIPHER could be used).
86 unsigned long flags
= ctx
->flags
;
87 EVP_CIPHER_CTX_reset(ctx
);
88 /* Restore encrypt and flags */
92 #ifndef OPENSSL_NO_ENGINE
94 if (!ENGINE_init(impl
)) {
95 EVPerr(EVP_F_EVP_CIPHERINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
99 /* Ask if an ENGINE is reserved for this job */
100 impl
= ENGINE_get_cipher_engine(cipher
->nid
);
102 /* There's an ENGINE for this job ... (apparently) */
103 const EVP_CIPHER
*c
= ENGINE_get_cipher(impl
, cipher
->nid
);
106 * One positive side-effect of US's export control history,
107 * is that we should at least be able to avoid using US
108 * misspellings of "initialisation"?
110 EVPerr(EVP_F_EVP_CIPHERINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
113 /* We'll use the ENGINE's private cipher definition */
116 * Store the ENGINE functional reference so we know 'cipher' came
117 * from an ENGINE and we need to release it when done.
124 ctx
->cipher
= cipher
;
125 if (ctx
->cipher
->ctx_size
) {
126 ctx
->cipher_data
= OPENSSL_zalloc(ctx
->cipher
->ctx_size
);
127 if (ctx
->cipher_data
== NULL
) {
128 EVPerr(EVP_F_EVP_CIPHERINIT_EX
, ERR_R_MALLOC_FAILURE
);
132 ctx
->cipher_data
= NULL
;
134 ctx
->key_len
= cipher
->key_len
;
135 /* Preserve wrap enable flag, zero everything else */
136 ctx
->flags
&= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
;
137 if (ctx
->cipher
->flags
& EVP_CIPH_CTRL_INIT
) {
138 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_INIT
, 0, NULL
)) {
139 EVPerr(EVP_F_EVP_CIPHERINIT_EX
, EVP_R_INITIALIZATION_ERROR
);
143 } else if (!ctx
->cipher
) {
144 EVPerr(EVP_F_EVP_CIPHERINIT_EX
, EVP_R_NO_CIPHER_SET
);
147 #ifndef OPENSSL_NO_ENGINE
150 /* we assume block size is a power of 2 in *cryptUpdate */
151 OPENSSL_assert(ctx
->cipher
->block_size
== 1
152 || ctx
->cipher
->block_size
== 8
153 || ctx
->cipher
->block_size
== 16);
155 if (!(ctx
->flags
& EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
)
156 && EVP_CIPHER_CTX_mode(ctx
) == EVP_CIPH_WRAP_MODE
) {
157 EVPerr(EVP_F_EVP_CIPHERINIT_EX
, EVP_R_WRAP_MODE_NOT_ALLOWED
);
161 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx
)) & EVP_CIPH_CUSTOM_IV
)) {
162 switch (EVP_CIPHER_CTX_mode(ctx
)) {
164 case EVP_CIPH_STREAM_CIPHER
:
165 case EVP_CIPH_ECB_MODE
:
168 case EVP_CIPH_CFB_MODE
:
169 case EVP_CIPH_OFB_MODE
:
174 case EVP_CIPH_CBC_MODE
:
176 OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx
) <=
177 (int)sizeof(ctx
->iv
));
179 memcpy(ctx
->oiv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
180 memcpy(ctx
->iv
, ctx
->oiv
, EVP_CIPHER_CTX_iv_length(ctx
));
183 case EVP_CIPH_CTR_MODE
:
185 /* Don't reuse IV for CTR mode */
187 memcpy(ctx
->iv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
195 if (key
|| (ctx
->cipher
->flags
& EVP_CIPH_ALWAYS_CALL_INIT
)) {
196 if (!ctx
->cipher
->init(ctx
, key
, iv
, enc
))
201 ctx
->block_mask
= ctx
->cipher
->block_size
- 1;
205 int EVP_CipherUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
206 const unsigned char *in
, int inl
)
209 return EVP_EncryptUpdate(ctx
, out
, outl
, in
, inl
);
211 return EVP_DecryptUpdate(ctx
, out
, outl
, in
, inl
);
214 int EVP_CipherFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
217 return EVP_EncryptFinal_ex(ctx
, out
, outl
);
219 return EVP_DecryptFinal_ex(ctx
, out
, outl
);
222 int EVP_CipherFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
225 return EVP_EncryptFinal(ctx
, out
, outl
);
227 return EVP_DecryptFinal(ctx
, out
, outl
);
230 int EVP_EncryptInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
231 const unsigned char *key
, const unsigned char *iv
)
233 return EVP_CipherInit(ctx
, cipher
, key
, iv
, 1);
236 int EVP_EncryptInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
237 ENGINE
*impl
, const unsigned char *key
,
238 const unsigned char *iv
)
240 return EVP_CipherInit_ex(ctx
, cipher
, impl
, key
, iv
, 1);
243 int EVP_DecryptInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
244 const unsigned char *key
, const unsigned char *iv
)
246 return EVP_CipherInit(ctx
, cipher
, key
, iv
, 0);
249 int EVP_DecryptInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
250 ENGINE
*impl
, const unsigned char *key
,
251 const unsigned char *iv
)
253 return EVP_CipherInit_ex(ctx
, cipher
, impl
, key
, iv
, 0);
257 * According to the letter of standard difference between pointers
258 * is specified to be valid only within same object. This makes
259 * it formally challenging to determine if input and output buffers
260 * are not partially overlapping with standard pointer arithmetic.
265 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
267 * Then we have VMS that distinguishes itself by adhering to
268 * sizeof(size_t)==4 even in 64-bit builds, which means that
269 * difference between two pointers might be truncated to 32 bits.
270 * In the context one can even wonder how comparison for
271 * equality is implemented. To be on the safe side we adhere to
272 * PTRDIFF_T even for comparison for equality.
274 # define PTRDIFF_T uint64_t
276 # define PTRDIFF_T size_t
279 int is_partially_overlapping(const void *ptr1
, const void *ptr2
, int len
)
281 PTRDIFF_T diff
= (PTRDIFF_T
)ptr1
-(PTRDIFF_T
)ptr2
;
283 * Check for partially overlapping buffers. [Binary logical
284 * operations are used instead of boolean to minimize number
285 * of conditional branches.]
287 int overlapped
= (len
> 0) & (diff
!= 0) & ((diff
< (PTRDIFF_T
)len
) |
288 (diff
> (0 - (PTRDIFF_T
)len
)));
293 int EVP_EncryptUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
294 const unsigned char *in
, int inl
)
298 bl
= ctx
->cipher
->block_size
;
300 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
301 /* If block size > 1 then the cipher will have to do this check */
302 if (bl
== 1 && is_partially_overlapping(out
, in
, inl
)) {
303 EVPerr(EVP_F_EVP_ENCRYPTUPDATE
, EVP_R_PARTIALLY_OVERLAPPING
);
307 i
= ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
319 if (is_partially_overlapping(out
+ ctx
->buf_len
, in
, inl
)) {
320 EVPerr(EVP_F_EVP_ENCRYPTUPDATE
, EVP_R_PARTIALLY_OVERLAPPING
);
324 if (ctx
->buf_len
== 0 && (inl
& (ctx
->block_mask
)) == 0) {
325 if (ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
)) {
334 OPENSSL_assert(bl
<= (int)sizeof(ctx
->buf
));
337 memcpy(&(ctx
->buf
[i
]), in
, inl
);
343 memcpy(&(ctx
->buf
[i
]), in
, j
);
346 if (!ctx
->cipher
->do_cipher(ctx
, out
, ctx
->buf
, bl
))
356 if (!ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
))
362 memcpy(ctx
->buf
, &(in
[inl
]), i
);
367 int EVP_EncryptFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
370 ret
= EVP_EncryptFinal_ex(ctx
, out
, outl
);
374 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
377 unsigned int i
, b
, bl
;
379 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
380 ret
= ctx
->cipher
->do_cipher(ctx
, out
, NULL
, 0);
388 b
= ctx
->cipher
->block_size
;
389 OPENSSL_assert(b
<= sizeof ctx
->buf
);
395 if (ctx
->flags
& EVP_CIPH_NO_PADDING
) {
397 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX
,
398 EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH
);
406 for (i
= bl
; i
< b
; i
++)
408 ret
= ctx
->cipher
->do_cipher(ctx
, out
, ctx
->buf
, b
);
416 int EVP_DecryptUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
417 const unsigned char *in
, int inl
)
422 b
= ctx
->cipher
->block_size
;
424 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
425 if (b
== 1 && is_partially_overlapping(out
, in
, inl
)) {
426 EVPerr(EVP_F_EVP_DECRYPTUPDATE
, EVP_R_PARTIALLY_OVERLAPPING
);
430 fix_len
= ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
444 if (ctx
->flags
& EVP_CIPH_NO_PADDING
)
445 return EVP_EncryptUpdate(ctx
, out
, outl
, in
, inl
);
447 OPENSSL_assert(b
<= sizeof ctx
->final
);
449 if (ctx
->final_used
) {
450 /* see comment about PTRDIFF_T comparison above */
451 if (((PTRDIFF_T
)out
== (PTRDIFF_T
)in
)
452 || is_partially_overlapping(out
, in
, b
)) {
453 EVPerr(EVP_F_EVP_DECRYPTUPDATE
, EVP_R_PARTIALLY_OVERLAPPING
);
456 memcpy(out
, ctx
->final
, b
);
462 if (!EVP_EncryptUpdate(ctx
, out
, outl
, in
, inl
))
466 * if we have 'decrypted' a multiple of block size, make sure we have a
467 * copy of this last block
469 if (b
> 1 && !ctx
->buf_len
) {
472 memcpy(ctx
->final
, &out
[*outl
], b
);
482 int EVP_DecryptFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
485 ret
= EVP_DecryptFinal_ex(ctx
, out
, outl
);
489 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
495 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
496 i
= ctx
->cipher
->do_cipher(ctx
, out
, NULL
, 0);
504 b
= ctx
->cipher
->block_size
;
505 if (ctx
->flags
& EVP_CIPH_NO_PADDING
) {
507 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX
,
508 EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH
);
515 if (ctx
->buf_len
|| !ctx
->final_used
) {
516 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX
, EVP_R_WRONG_FINAL_BLOCK_LENGTH
);
519 OPENSSL_assert(b
<= sizeof ctx
->final
);
522 * The following assumes that the ciphertext has been authenticated.
523 * Otherwise it provides a padding oracle.
525 n
= ctx
->final
[b
- 1];
526 if (n
== 0 || n
> (int)b
) {
527 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX
, EVP_R_BAD_DECRYPT
);
530 for (i
= 0; i
< n
; i
++) {
531 if (ctx
->final
[--b
] != n
) {
532 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX
, EVP_R_BAD_DECRYPT
);
536 n
= ctx
->cipher
->block_size
- n
;
537 for (i
= 0; i
< n
; i
++)
538 out
[i
] = ctx
->final
[i
];
545 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX
*c
, int keylen
)
547 if (c
->cipher
->flags
& EVP_CIPH_CUSTOM_KEY_LENGTH
)
548 return EVP_CIPHER_CTX_ctrl(c
, EVP_CTRL_SET_KEY_LENGTH
, keylen
, NULL
);
549 if (c
->key_len
== keylen
)
551 if ((keylen
> 0) && (c
->cipher
->flags
& EVP_CIPH_VARIABLE_LENGTH
)) {
555 EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH
, EVP_R_INVALID_KEY_LENGTH
);
559 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX
*ctx
, int pad
)
562 ctx
->flags
&= ~EVP_CIPH_NO_PADDING
;
564 ctx
->flags
|= EVP_CIPH_NO_PADDING
;
568 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
, void *ptr
)
572 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL
, EVP_R_NO_CIPHER_SET
);
576 if (!ctx
->cipher
->ctrl
) {
577 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL
, EVP_R_CTRL_NOT_IMPLEMENTED
);
581 ret
= ctx
->cipher
->ctrl(ctx
, type
, arg
, ptr
);
583 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL
,
584 EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED
);
590 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX
*ctx
, unsigned char *key
)
592 if (ctx
->cipher
->flags
& EVP_CIPH_RAND_KEY
)
593 return EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_RAND_KEY
, 0, key
);
594 if (RAND_bytes(key
, ctx
->key_len
) <= 0)
599 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX
*out
, const EVP_CIPHER_CTX
*in
)
601 if ((in
== NULL
) || (in
->cipher
== NULL
)) {
602 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY
, EVP_R_INPUT_NOT_INITIALIZED
);
605 #ifndef OPENSSL_NO_ENGINE
606 /* Make sure it's safe to copy a cipher context using an ENGINE */
607 if (in
->engine
&& !ENGINE_init(in
->engine
)) {
608 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY
, ERR_R_ENGINE_LIB
);
613 EVP_CIPHER_CTX_reset(out
);
614 memcpy(out
, in
, sizeof(*out
));
616 if (in
->cipher_data
&& in
->cipher
->ctx_size
) {
617 out
->cipher_data
= OPENSSL_malloc(in
->cipher
->ctx_size
);
618 if (out
->cipher_data
== NULL
) {
619 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY
, ERR_R_MALLOC_FAILURE
);
622 memcpy(out
->cipher_data
, in
->cipher_data
, in
->cipher
->ctx_size
);
625 if (in
->cipher
->flags
& EVP_CIPH_CUSTOM_COPY
)
626 return in
->cipher
->ctrl((EVP_CIPHER_CTX
*)in
, EVP_CTRL_COPY
, 0, out
);