2 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
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
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
16 #include <openssl/evp.h>
17 #include <openssl/err.h>
18 #include <openssl/rand.h>
20 # include <openssl/engine.h>
22 #include <openssl/params.h>
23 #include <openssl/core_names.h>
24 #include "internal/cryptlib.h"
25 #include "internal/provider.h"
26 #include "internal/core.h"
27 #include "internal/safe_math.h"
28 #include "crypto/evp.h"
29 #include "evp_local.h"
31 OSSL_SAFE_MATH_SIGNED(int, int)
33 int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX
*ctx
)
38 if (ctx
->cipher
== NULL
|| ctx
->cipher
->prov
== NULL
)
41 if (ctx
->algctx
!= NULL
) {
42 if (ctx
->cipher
->freectx
!= NULL
)
43 ctx
->cipher
->freectx(ctx
->algctx
);
46 if (ctx
->fetched_cipher
!= NULL
)
47 EVP_CIPHER_free(ctx
->fetched_cipher
);
48 memset(ctx
, 0, sizeof(*ctx
));
53 /* Remove legacy code below when legacy support is removed. */
56 if (ctx
->cipher
!= NULL
) {
57 if (ctx
->cipher
->cleanup
&& !ctx
->cipher
->cleanup(ctx
))
59 /* Cleanse cipher context data */
60 if (ctx
->cipher_data
&& ctx
->cipher
->ctx_size
)
61 OPENSSL_cleanse(ctx
->cipher_data
, ctx
->cipher
->ctx_size
);
63 OPENSSL_free(ctx
->cipher_data
);
64 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
65 ENGINE_finish(ctx
->engine
);
67 memset(ctx
, 0, sizeof(*ctx
));
72 EVP_CIPHER_CTX
*EVP_CIPHER_CTX_new(void)
76 ctx
= OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX
));
84 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX
*ctx
)
88 EVP_CIPHER_CTX_reset(ctx
);
92 static int evp_cipher_init_internal(EVP_CIPHER_CTX
*ctx
,
93 const EVP_CIPHER
*cipher
,
94 ENGINE
*impl
, const unsigned char *key
,
95 const unsigned char *iv
, int enc
,
96 const OSSL_PARAM params
[])
99 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
100 ENGINE
*tmpimpl
= NULL
;
104 * enc == 1 means we are encrypting.
105 * enc == 0 means we are decrypting.
106 * enc == -1 means, use the previously initialised value for encrypt/decrypt
116 if (cipher
== NULL
&& ctx
->cipher
== NULL
) {
117 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
121 /* Code below to be removed when legacy support is dropped. */
123 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
125 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
126 * this context may already have an ENGINE! Try to avoid releasing the
127 * previous handle, re-querying for an ENGINE, and having a
128 * reinitialisation, when it may all be unnecessary.
130 if (ctx
->engine
&& ctx
->cipher
131 && (cipher
== NULL
|| cipher
->nid
== ctx
->cipher
->nid
))
134 if (cipher
!= NULL
&& impl
== NULL
) {
135 /* Ask if an ENGINE is reserved for this job */
136 tmpimpl
= ENGINE_get_cipher_engine(cipher
->nid
);
141 * If there are engines involved then we should use legacy handling for now.
143 if (ctx
->engine
!= NULL
144 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
148 || (cipher
!= NULL
&& cipher
->origin
== EVP_ORIG_METH
)
149 || (cipher
== NULL
&& ctx
->cipher
!= NULL
150 && ctx
->cipher
->origin
== EVP_ORIG_METH
)) {
151 if (ctx
->cipher
== ctx
->fetched_cipher
)
153 EVP_CIPHER_free(ctx
->fetched_cipher
);
154 ctx
->fetched_cipher
= NULL
;
158 * Ensure a context left lying around from last time is cleared
161 if (cipher
!= NULL
&& ctx
->cipher
!= NULL
) {
162 if (ctx
->cipher
->cleanup
!= NULL
&& !ctx
->cipher
->cleanup(ctx
))
164 OPENSSL_clear_free(ctx
->cipher_data
, ctx
->cipher
->ctx_size
);
165 ctx
->cipher_data
= NULL
;
168 /* Start of non-legacy code below */
170 /* Ensure a context left lying around from last time is cleared */
171 if (cipher
!= NULL
&& ctx
->cipher
!= NULL
) {
172 unsigned long flags
= ctx
->flags
;
174 EVP_CIPHER_CTX_reset(ctx
);
175 /* Restore encrypt and flags */
181 cipher
= ctx
->cipher
;
183 if (cipher
->prov
== NULL
) {
185 /* We only do explicit fetches inside the FIPS module */
186 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
189 EVP_CIPHER
*provciph
=
190 EVP_CIPHER_fetch(NULL
,
191 cipher
->nid
== NID_undef
? "NULL"
192 : OBJ_nid2sn(cipher
->nid
),
195 if (provciph
== NULL
)
198 EVP_CIPHER_free(ctx
->fetched_cipher
);
199 ctx
->fetched_cipher
= provciph
;
203 if (!ossl_assert(cipher
->prov
!= NULL
)) {
204 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
208 if (cipher
!= ctx
->fetched_cipher
) {
209 if (!EVP_CIPHER_up_ref((EVP_CIPHER
*)cipher
)) {
210 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
213 EVP_CIPHER_free(ctx
->fetched_cipher
);
214 /* Coverity false positive, the reference counting is confusing it */
215 /* coverity[use_after_free] */
216 ctx
->fetched_cipher
= (EVP_CIPHER
*)cipher
;
218 ctx
->cipher
= cipher
;
219 if (ctx
->algctx
== NULL
) {
220 ctx
->algctx
= ctx
->cipher
->newctx(ossl_provider_ctx(cipher
->prov
));
221 if (ctx
->algctx
== NULL
) {
222 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
227 if ((ctx
->flags
& EVP_CIPH_NO_PADDING
) != 0) {
229 * If this ctx was already set up for no padding then we need to tell
230 * the new cipher about it.
232 if (!EVP_CIPHER_CTX_set_padding(ctx
, 0))
238 * Fix for CVE-2023-5363
239 * Passing in a size as part of the init call takes effect late
240 * so, force such to occur before the initialisation.
242 * The FIPS provider's internal library context is used in a manner
243 * such that this is not an issue.
245 if (params
!= NULL
) {
246 OSSL_PARAM param_lens
[3] = { OSSL_PARAM_END
, OSSL_PARAM_END
,
248 OSSL_PARAM
*q
= param_lens
;
251 p
= OSSL_PARAM_locate_const(params
, OSSL_CIPHER_PARAM_KEYLEN
);
253 memcpy(q
++, p
, sizeof(*q
));
256 * Note that OSSL_CIPHER_PARAM_AEAD_IVLEN is a synonym for
257 * OSSL_CIPHER_PARAM_IVLEN so both are covered here.
259 p
= OSSL_PARAM_locate_const(params
, OSSL_CIPHER_PARAM_IVLEN
);
261 memcpy(q
++, p
, sizeof(*q
));
263 if (q
!= param_lens
) {
264 if (!EVP_CIPHER_CTX_set_params(ctx
, param_lens
)) {
265 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_LENGTH
);
273 if (ctx
->cipher
->einit
== NULL
) {
274 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
278 return ctx
->cipher
->einit(ctx
->algctx
,
281 : EVP_CIPHER_CTX_get_key_length(ctx
),
284 : EVP_CIPHER_CTX_get_iv_length(ctx
),
288 if (ctx
->cipher
->dinit
== NULL
) {
289 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
293 return ctx
->cipher
->dinit(ctx
->algctx
,
296 : EVP_CIPHER_CTX_get_key_length(ctx
),
299 : EVP_CIPHER_CTX_get_iv_length(ctx
),
302 /* Code below to be removed when legacy support is dropped. */
305 if (cipher
!= NULL
) {
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).
312 unsigned long flags
= ctx
->flags
;
313 EVP_CIPHER_CTX_reset(ctx
);
314 /* Restore encrypt and flags */
318 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
320 if (!ENGINE_init(impl
)) {
321 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
328 /* There's an ENGINE for this job ... (apparently) */
329 const EVP_CIPHER
*c
= ENGINE_get_cipher(impl
, cipher
->nid
);
333 * One positive side-effect of US's export control history,
334 * is that we should at least be able to avoid using US
335 * misspellings of "initialisation"?
337 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
340 /* We'll use the ENGINE's private cipher definition */
343 * Store the ENGINE functional reference so we know 'cipher' came
344 * from an ENGINE and we need to release it when done.
352 ctx
->cipher
= cipher
;
353 if (ctx
->cipher
->ctx_size
) {
354 ctx
->cipher_data
= OPENSSL_zalloc(ctx
->cipher
->ctx_size
);
355 if (ctx
->cipher_data
== NULL
) {
360 ctx
->cipher_data
= NULL
;
362 ctx
->key_len
= cipher
->key_len
;
363 /* Preserve wrap enable flag, zero everything else */
364 ctx
->flags
&= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
;
365 if (ctx
->cipher
->flags
& EVP_CIPH_CTRL_INIT
) {
366 if (EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_INIT
, 0, NULL
) <= 0) {
368 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
373 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
376 if (ctx
->cipher
== NULL
)
379 /* we assume block size is a power of 2 in *cryptUpdate */
380 OPENSSL_assert(ctx
->cipher
->block_size
== 1
381 || ctx
->cipher
->block_size
== 8
382 || ctx
->cipher
->block_size
== 16);
384 if (!(ctx
->flags
& EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
)
385 && EVP_CIPHER_CTX_get_mode(ctx
) == EVP_CIPH_WRAP_MODE
) {
386 ERR_raise(ERR_LIB_EVP
, EVP_R_WRAP_MODE_NOT_ALLOWED
);
390 if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx
))
391 & EVP_CIPH_CUSTOM_IV
) == 0) {
392 switch (EVP_CIPHER_CTX_get_mode(ctx
)) {
394 case EVP_CIPH_STREAM_CIPHER
:
395 case EVP_CIPH_ECB_MODE
:
398 case EVP_CIPH_CFB_MODE
:
399 case EVP_CIPH_OFB_MODE
:
404 case EVP_CIPH_CBC_MODE
:
405 n
= EVP_CIPHER_CTX_get_iv_length(ctx
);
406 if (n
< 0 || n
> (int)sizeof(ctx
->iv
)) {
407 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_IV_LENGTH
);
411 memcpy(ctx
->oiv
, iv
, n
);
412 memcpy(ctx
->iv
, ctx
->oiv
, n
);
415 case EVP_CIPH_CTR_MODE
:
417 /* Don't reuse IV for CTR mode */
419 n
= EVP_CIPHER_CTX_get_iv_length(ctx
);
420 if (n
<= 0 || n
> (int)sizeof(ctx
->iv
)) {
421 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_IV_LENGTH
);
424 memcpy(ctx
->iv
, iv
, n
);
433 if (key
!= NULL
|| (ctx
->cipher
->flags
& EVP_CIPH_ALWAYS_CALL_INIT
)) {
434 if (!ctx
->cipher
->init(ctx
, key
, iv
, enc
))
439 ctx
->block_mask
= ctx
->cipher
->block_size
- 1;
443 int EVP_CipherInit_ex2(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
444 const unsigned char *key
, const unsigned char *iv
,
445 int enc
, const OSSL_PARAM params
[])
447 return evp_cipher_init_internal(ctx
, cipher
, NULL
, key
, iv
, enc
, params
);
450 int EVP_CipherInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
451 const unsigned char *key
, const unsigned char *iv
, int enc
)
454 EVP_CIPHER_CTX_reset(ctx
);
455 return evp_cipher_init_internal(ctx
, cipher
, NULL
, key
, iv
, enc
, NULL
);
458 int EVP_CipherInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
459 ENGINE
*impl
, const unsigned char *key
,
460 const unsigned char *iv
, int enc
)
462 return evp_cipher_init_internal(ctx
, cipher
, impl
, key
, iv
, enc
, NULL
);
465 int EVP_CipherUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
466 const unsigned char *in
, int inl
)
469 return EVP_EncryptUpdate(ctx
, out
, outl
, in
, inl
);
471 return EVP_DecryptUpdate(ctx
, out
, outl
, in
, inl
);
474 int EVP_CipherFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
477 return EVP_EncryptFinal_ex(ctx
, out
, outl
);
479 return EVP_DecryptFinal_ex(ctx
, out
, outl
);
482 int EVP_CipherFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
485 return EVP_EncryptFinal(ctx
, out
, outl
);
487 return EVP_DecryptFinal(ctx
, out
, outl
);
490 int EVP_EncryptInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
491 const unsigned char *key
, const unsigned char *iv
)
493 return EVP_CipherInit(ctx
, cipher
, key
, iv
, 1);
496 int EVP_EncryptInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
497 ENGINE
*impl
, const unsigned char *key
,
498 const unsigned char *iv
)
500 return EVP_CipherInit_ex(ctx
, cipher
, impl
, key
, iv
, 1);
503 int EVP_EncryptInit_ex2(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
504 const unsigned char *key
, const unsigned char *iv
,
505 const OSSL_PARAM params
[])
507 return EVP_CipherInit_ex2(ctx
, cipher
, key
, iv
, 1, params
);
510 int EVP_DecryptInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
511 const unsigned char *key
, const unsigned char *iv
)
513 return EVP_CipherInit(ctx
, cipher
, key
, iv
, 0);
516 int EVP_DecryptInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
517 ENGINE
*impl
, const unsigned char *key
,
518 const unsigned char *iv
)
520 return EVP_CipherInit_ex(ctx
, cipher
, impl
, key
, iv
, 0);
523 int EVP_DecryptInit_ex2(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
524 const unsigned char *key
, const unsigned char *iv
,
525 const OSSL_PARAM params
[])
527 return EVP_CipherInit_ex2(ctx
, cipher
, key
, iv
, 0, params
);
531 * According to the letter of standard difference between pointers
532 * is specified to be valid only within same object. This makes
533 * it formally challenging to determine if input and output buffers
534 * are not partially overlapping with standard pointer arithmetic.
539 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
541 * Then we have VMS that distinguishes itself by adhering to
542 * sizeof(size_t)==4 even in 64-bit builds, which means that
543 * difference between two pointers might be truncated to 32 bits.
544 * In the context one can even wonder how comparison for
545 * equality is implemented. To be on the safe side we adhere to
546 * PTRDIFF_T even for comparison for equality.
548 # define PTRDIFF_T uint64_t
550 # define PTRDIFF_T size_t
553 int ossl_is_partially_overlapping(const void *ptr1
, const void *ptr2
, int len
)
555 PTRDIFF_T diff
= (PTRDIFF_T
)ptr1
-(PTRDIFF_T
)ptr2
;
557 * Check for partially overlapping buffers. [Binary logical
558 * operations are used instead of boolean to minimize number
559 * of conditional branches.]
561 int overlapped
= (len
> 0) & (diff
!= 0) & ((diff
< (PTRDIFF_T
)len
) |
562 (diff
> (0 - (PTRDIFF_T
)len
)));
567 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX
*ctx
,
568 unsigned char *out
, int *outl
,
569 const unsigned char *in
, int inl
)
571 int i
, j
, bl
, cmpl
= inl
;
573 if (EVP_CIPHER_CTX_test_flags(ctx
, EVP_CIPH_FLAG_LENGTH_BITS
))
574 cmpl
= safe_div_round_up_int(cmpl
, 8, NULL
);
576 bl
= ctx
->cipher
->block_size
;
578 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
579 /* If block size > 1 then the cipher will have to do this check */
580 if (bl
== 1 && ossl_is_partially_overlapping(out
, in
, cmpl
)) {
581 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
585 i
= ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
597 if (ossl_is_partially_overlapping(out
+ ctx
->buf_len
, in
, cmpl
)) {
598 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
602 if (ctx
->buf_len
== 0 && (inl
& (ctx
->block_mask
)) == 0) {
603 if (ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
)) {
612 OPENSSL_assert(bl
<= (int)sizeof(ctx
->buf
));
615 memcpy(&(ctx
->buf
[i
]), in
, inl
);
623 * Once we've processed the first j bytes from in, the amount of
624 * data left that is a multiple of the block length is:
625 * (inl - j) & ~(bl - 1)
626 * We must ensure that this amount of data, plus the one block that
627 * we process from ctx->buf does not exceed INT_MAX
629 if (((inl
- j
) & ~(bl
- 1)) > INT_MAX
- bl
) {
630 ERR_raise(ERR_LIB_EVP
, EVP_R_OUTPUT_WOULD_OVERFLOW
);
633 memcpy(&(ctx
->buf
[i
]), in
, j
);
636 if (!ctx
->cipher
->do_cipher(ctx
, out
, ctx
->buf
, bl
))
646 if (!ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
))
652 memcpy(ctx
->buf
, &(in
[inl
]), i
);
658 int EVP_EncryptUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
659 const unsigned char *in
, int inl
)
662 size_t soutl
, inl_
= (size_t)inl
;
665 if (ossl_likely(outl
!= NULL
)) {
668 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
672 /* Prevent accidental use of decryption context when encrypting */
673 if (ossl_unlikely(!ctx
->encrypt
)) {
674 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
678 if (ossl_unlikely(ctx
->cipher
== NULL
)) {
679 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
683 if (ossl_unlikely(ctx
->cipher
->prov
== NULL
))
686 blocksize
= ctx
->cipher
->block_size
;
688 if (ossl_unlikely(ctx
->cipher
->cupdate
== NULL
|| blocksize
< 1)) {
689 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
693 ret
= ctx
->cipher
->cupdate(ctx
->algctx
, out
, &soutl
,
694 inl_
+ (size_t)(blocksize
== 1 ? 0 : blocksize
),
697 if (ossl_likely(ret
)) {
698 if (soutl
> INT_MAX
) {
699 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
707 /* Code below to be removed when legacy support is dropped. */
710 return evp_EncryptDecryptUpdate(ctx
, out
, outl
, in
, inl
);
713 int EVP_EncryptFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
716 ret
= EVP_EncryptFinal_ex(ctx
, out
, outl
);
720 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
723 unsigned int i
, b
, bl
;
730 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
734 /* Prevent accidental use of decryption context when encrypting */
736 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
740 if (ctx
->cipher
== NULL
) {
741 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
744 if (ctx
->cipher
->prov
== NULL
)
747 blocksize
= EVP_CIPHER_CTX_get_block_size(ctx
);
749 if (blocksize
< 1 || ctx
->cipher
->cfinal
== NULL
) {
750 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
754 ret
= ctx
->cipher
->cfinal(ctx
->algctx
, out
, &soutl
,
755 blocksize
== 1 ? 0 : blocksize
);
758 if (soutl
> INT_MAX
) {
759 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
767 /* Code below to be removed when legacy support is dropped. */
770 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
771 ret
= ctx
->cipher
->do_cipher(ctx
, out
, NULL
, 0);
779 b
= ctx
->cipher
->block_size
;
780 OPENSSL_assert(b
<= sizeof(ctx
->buf
));
786 if (ctx
->flags
& EVP_CIPH_NO_PADDING
) {
788 ERR_raise(ERR_LIB_EVP
, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH
);
796 for (i
= bl
; i
< b
; i
++)
798 ret
= ctx
->cipher
->do_cipher(ctx
, out
, ctx
->buf
, b
);
806 int EVP_DecryptUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
807 const unsigned char *in
, int inl
)
809 int fix_len
, cmpl
= inl
, ret
;
811 size_t soutl
, inl_
= (size_t)inl
;
814 if (ossl_likely(outl
!= NULL
)) {
817 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
821 /* Prevent accidental use of encryption context when decrypting */
822 if (ossl_unlikely(ctx
->encrypt
)) {
823 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
827 if (ossl_unlikely(ctx
->cipher
== NULL
)) {
828 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
831 if (ossl_unlikely(ctx
->cipher
->prov
== NULL
))
834 blocksize
= EVP_CIPHER_CTX_get_block_size(ctx
);
836 if (ossl_unlikely(ctx
->cipher
->cupdate
== NULL
|| blocksize
< 1)) {
837 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
840 ret
= ctx
->cipher
->cupdate(ctx
->algctx
, out
, &soutl
,
841 inl_
+ (size_t)(blocksize
== 1 ? 0 : blocksize
),
844 if (ossl_likely(ret
)) {
845 if (soutl
> INT_MAX
) {
846 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
854 /* Code below to be removed when legacy support is dropped. */
857 b
= ctx
->cipher
->block_size
;
859 if (EVP_CIPHER_CTX_test_flags(ctx
, EVP_CIPH_FLAG_LENGTH_BITS
))
860 cmpl
= safe_div_round_up_int(cmpl
, 8, NULL
);
862 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
863 if (b
== 1 && ossl_is_partially_overlapping(out
, in
, cmpl
)) {
864 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
868 fix_len
= ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
882 if (ctx
->flags
& EVP_CIPH_NO_PADDING
)
883 return evp_EncryptDecryptUpdate(ctx
, out
, outl
, in
, inl
);
885 OPENSSL_assert(b
<= sizeof(ctx
->final
));
887 if (ctx
->final_used
) {
888 /* see comment about PTRDIFF_T comparison above */
889 if (((PTRDIFF_T
)out
== (PTRDIFF_T
)in
)
890 || ossl_is_partially_overlapping(out
, in
, b
)) {
891 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
895 * final_used is only ever set if buf_len is 0. Therefore the maximum
896 * length output we will ever see from evp_EncryptDecryptUpdate is
897 * the maximum multiple of the block length that is <= inl, or just:
899 * Since final_used has been set then the final output length is:
900 * (inl & ~(b - 1)) + b
901 * This must never exceed INT_MAX
903 if ((inl
& ~(b
- 1)) > INT_MAX
- b
) {
904 ERR_raise(ERR_LIB_EVP
, EVP_R_OUTPUT_WOULD_OVERFLOW
);
907 memcpy(out
, ctx
->final
, b
);
913 if (!evp_EncryptDecryptUpdate(ctx
, out
, outl
, in
, inl
))
917 * if we have 'decrypted' a multiple of block size, make sure we have a
918 * copy of this last block
920 if (b
> 1 && !ctx
->buf_len
) {
923 memcpy(ctx
->final
, &out
[*outl
], b
);
933 int EVP_DecryptFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
936 ret
= EVP_DecryptFinal_ex(ctx
, out
, outl
);
940 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
951 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
955 /* Prevent accidental use of encryption context when decrypting */
957 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
961 if (ctx
->cipher
== NULL
) {
962 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
966 if (ctx
->cipher
->prov
== NULL
)
969 blocksize
= EVP_CIPHER_CTX_get_block_size(ctx
);
971 if (blocksize
< 1 || ctx
->cipher
->cfinal
== NULL
) {
972 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
976 ret
= ctx
->cipher
->cfinal(ctx
->algctx
, out
, &soutl
,
977 blocksize
== 1 ? 0 : blocksize
);
980 if (soutl
> INT_MAX
) {
981 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
989 /* Code below to be removed when legacy support is dropped. */
993 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
994 i
= ctx
->cipher
->do_cipher(ctx
, out
, NULL
, 0);
1002 b
= ctx
->cipher
->block_size
;
1003 if (ctx
->flags
& EVP_CIPH_NO_PADDING
) {
1005 ERR_raise(ERR_LIB_EVP
, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH
);
1012 if (ctx
->buf_len
|| !ctx
->final_used
) {
1013 ERR_raise(ERR_LIB_EVP
, EVP_R_WRONG_FINAL_BLOCK_LENGTH
);
1016 OPENSSL_assert(b
<= sizeof(ctx
->final
));
1019 * The following assumes that the ciphertext has been authenticated.
1020 * Otherwise it provides a padding oracle.
1022 n
= ctx
->final
[b
- 1];
1023 if (n
== 0 || n
> (int)b
) {
1024 ERR_raise(ERR_LIB_EVP
, EVP_R_BAD_DECRYPT
);
1027 for (i
= 0; i
< n
; i
++) {
1028 if (ctx
->final
[--b
] != n
) {
1029 ERR_raise(ERR_LIB_EVP
, EVP_R_BAD_DECRYPT
);
1033 n
= ctx
->cipher
->block_size
- n
;
1034 for (i
= 0; i
< n
; i
++)
1035 out
[i
] = ctx
->final
[i
];
1042 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX
*c
, int keylen
)
1044 if (c
->cipher
->prov
!= NULL
) {
1046 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
1049 if (EVP_CIPHER_CTX_get_key_length(c
) == keylen
)
1052 /* Check the cipher actually understands this parameter */
1053 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c
->cipher
),
1054 OSSL_CIPHER_PARAM_KEYLEN
) == NULL
) {
1055 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY_LENGTH
);
1059 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &len
);
1060 if (!OSSL_PARAM_set_int(params
, keylen
))
1062 ok
= evp_do_ciph_ctx_setparams(c
->cipher
, c
->algctx
, params
);
1065 c
->key_len
= keylen
;
1069 /* Code below to be removed when legacy support is dropped. */
1072 * Note there have never been any built-in ciphers that define this flag
1073 * since it was first introduced.
1075 if (c
->cipher
->flags
& EVP_CIPH_CUSTOM_KEY_LENGTH
)
1076 return EVP_CIPHER_CTX_ctrl(c
, EVP_CTRL_SET_KEY_LENGTH
, keylen
, NULL
);
1077 if (EVP_CIPHER_CTX_get_key_length(c
) == keylen
)
1079 if ((keylen
> 0) && (c
->cipher
->flags
& EVP_CIPH_VARIABLE_LENGTH
)) {
1080 c
->key_len
= keylen
;
1083 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY_LENGTH
);
1087 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX
*ctx
, int pad
)
1090 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
1091 unsigned int pd
= pad
;
1094 ctx
->flags
&= ~EVP_CIPH_NO_PADDING
;
1096 ctx
->flags
|= EVP_CIPH_NO_PADDING
;
1098 if (ctx
->cipher
!= NULL
&& ctx
->cipher
->prov
== NULL
)
1100 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING
, &pd
);
1101 ok
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->algctx
, params
);
1106 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
, void *ptr
)
1108 int ret
= EVP_CTRL_RET_UNSUPPORTED
;
1112 OSSL_PARAM params
[4] = {
1113 OSSL_PARAM_END
, OSSL_PARAM_END
, OSSL_PARAM_END
, OSSL_PARAM_END
1116 if (ctx
== NULL
|| ctx
->cipher
== NULL
) {
1117 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
1121 if (ctx
->cipher
->prov
== NULL
)
1125 case EVP_CTRL_SET_KEY_LENGTH
:
1128 if (ctx
->key_len
== arg
)
1129 /* Skip calling into provider if unchanged. */
1131 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &sz
);
1134 case EVP_CTRL_RAND_KEY
: /* Used by DES */
1137 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY
,
1143 * EVP_CTRL_INIT is purely legacy, no provider counterpart.
1144 * As a matter of fact, this should be dead code, but some caller
1145 * might still do a direct control call with this command, so...
1146 * Legacy methods return 1 except for exceptional circumstances, so
1147 * we do the same here to not be disruptive.
1150 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS
: /* Used by DASYNC */
1153 case EVP_CTRL_AEAD_SET_IVLEN
:
1156 if (ctx
->iv_len
== arg
)
1157 /* Skip calling into provider if unchanged. */
1159 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN
, &sz
);
1162 case EVP_CTRL_CCM_SET_L
:
1163 if (arg
< 2 || arg
> 8)
1166 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN
, &sz
);
1169 case EVP_CTRL_AEAD_SET_IV_FIXED
:
1170 params
[0] = OSSL_PARAM_construct_octet_string(
1171 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED
, ptr
, sz
);
1173 case EVP_CTRL_GCM_IV_GEN
:
1176 sz
= 0; /* special case that uses the iv length */
1177 params
[0] = OSSL_PARAM_construct_octet_string(
1178 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN
, ptr
, sz
);
1180 case EVP_CTRL_GCM_SET_IV_INV
:
1183 params
[0] = OSSL_PARAM_construct_octet_string(
1184 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV
, ptr
, sz
);
1186 case EVP_CTRL_GET_RC5_ROUNDS
:
1187 set_params
= 0; /* Fall thru */
1188 case EVP_CTRL_SET_RC5_ROUNDS
:
1191 i
= (unsigned int)arg
;
1192 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS
, &i
);
1194 case EVP_CTRL_SET_SPEED
:
1197 i
= (unsigned int)arg
;
1198 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED
, &i
);
1200 case EVP_CTRL_AEAD_GET_TAG
:
1201 set_params
= 0; /* Fall thru */
1202 case EVP_CTRL_AEAD_SET_TAG
:
1203 params
[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG
,
1206 case EVP_CTRL_AEAD_TLS1_AAD
:
1207 /* This one does a set and a get - since it returns a size */
1209 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD
,
1211 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->algctx
, params
);
1215 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD
, &sz
);
1216 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->algctx
, params
);
1220 #ifndef OPENSSL_NO_RC2
1221 case EVP_CTRL_GET_RC2_KEY_BITS
:
1222 set_params
= 0; /* Fall thru */
1223 case EVP_CTRL_SET_RC2_KEY_BITS
:
1224 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS
, &sz
);
1226 #endif /* OPENSSL_NO_RC2 */
1227 #if !defined(OPENSSL_NO_MULTIBLOCK)
1228 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE
:
1229 params
[0] = OSSL_PARAM_construct_size_t(
1230 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT
, &sz
);
1231 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->algctx
, params
);
1235 params
[0] = OSSL_PARAM_construct_size_t(
1236 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE
, &sz
);
1237 params
[1] = OSSL_PARAM_construct_end();
1238 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->algctx
, params
);
1242 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD
: {
1243 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*p
=
1244 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*)ptr
;
1246 if (arg
< (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
))
1249 params
[0] = OSSL_PARAM_construct_octet_string(
1250 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD
, (void*)p
->inp
, p
->len
);
1251 params
[1] = OSSL_PARAM_construct_uint(
1252 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE
, &p
->interleave
);
1253 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->algctx
, params
);
1256 /* Retrieve the return values changed by the set */
1257 params
[0] = OSSL_PARAM_construct_size_t(
1258 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN
, &sz
);
1259 params
[1] = OSSL_PARAM_construct_uint(
1260 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE
, &p
->interleave
);
1261 params
[2] = OSSL_PARAM_construct_end();
1262 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->algctx
, params
);
1267 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT
: {
1268 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*p
=
1269 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*)ptr
;
1271 params
[0] = OSSL_PARAM_construct_octet_string(
1272 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC
, p
->out
, p
->len
);
1274 params
[1] = OSSL_PARAM_construct_octet_string(
1275 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN
, (void*)p
->inp
,
1277 params
[2] = OSSL_PARAM_construct_uint(
1278 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE
, &p
->interleave
);
1279 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->algctx
, params
);
1282 params
[0] = OSSL_PARAM_construct_size_t(
1283 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN
, &sz
);
1284 params
[1] = OSSL_PARAM_construct_end();
1285 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->algctx
, params
);
1290 #endif /* OPENSSL_NO_MULTIBLOCK */
1291 case EVP_CTRL_AEAD_SET_MAC_KEY
:
1294 params
[0] = OSSL_PARAM_construct_octet_string(
1295 OSSL_CIPHER_PARAM_AEAD_MAC_KEY
, ptr
, sz
);
1300 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->algctx
, params
);
1302 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->algctx
, params
);
1305 /* Code below to be removed when legacy support is dropped. */
1307 if (ctx
->cipher
->ctrl
== NULL
) {
1308 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_NOT_IMPLEMENTED
);
1312 ret
= ctx
->cipher
->ctrl(ctx
, type
, arg
, ptr
);
1315 if (ret
== EVP_CTRL_RET_UNSUPPORTED
) {
1316 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED
);
1322 int EVP_CIPHER_get_params(EVP_CIPHER
*cipher
, OSSL_PARAM params
[])
1324 if (cipher
!= NULL
&& cipher
->get_params
!= NULL
)
1325 return cipher
->get_params(params
);
1329 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX
*ctx
, const OSSL_PARAM params
[])
1332 const OSSL_PARAM
*p
;
1334 if (ctx
->cipher
!= NULL
&& ctx
->cipher
->set_ctx_params
!= NULL
) {
1335 r
= ctx
->cipher
->set_ctx_params(ctx
->algctx
, params
);
1337 p
= OSSL_PARAM_locate_const(params
, OSSL_CIPHER_PARAM_KEYLEN
);
1338 if (p
!= NULL
&& !OSSL_PARAM_get_int(p
, &ctx
->key_len
)) {
1344 p
= OSSL_PARAM_locate_const(params
, OSSL_CIPHER_PARAM_IVLEN
);
1345 if (p
!= NULL
&& !OSSL_PARAM_get_int(p
, &ctx
->iv_len
)) {
1354 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX
*ctx
, OSSL_PARAM params
[])
1356 if (ctx
->cipher
!= NULL
&& ctx
->cipher
->get_ctx_params
!= NULL
)
1357 return ctx
->cipher
->get_ctx_params(ctx
->algctx
, params
);
1361 const OSSL_PARAM
*EVP_CIPHER_gettable_params(const EVP_CIPHER
*cipher
)
1363 if (cipher
!= NULL
&& cipher
->gettable_params
!= NULL
)
1364 return cipher
->gettable_params(
1365 ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher
)));
1369 const OSSL_PARAM
*EVP_CIPHER_settable_ctx_params(const EVP_CIPHER
*cipher
)
1373 if (cipher
!= NULL
&& cipher
->settable_ctx_params
!= NULL
) {
1374 provctx
= ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher
));
1375 return cipher
->settable_ctx_params(NULL
, provctx
);
1380 const OSSL_PARAM
*EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER
*cipher
)
1384 if (cipher
!= NULL
&& cipher
->gettable_ctx_params
!= NULL
) {
1385 provctx
= ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher
));
1386 return cipher
->gettable_ctx_params(NULL
, provctx
);
1391 const OSSL_PARAM
*EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX
*cctx
)
1395 if (cctx
!= NULL
&& cctx
->cipher
->settable_ctx_params
!= NULL
) {
1396 alg
= ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx
->cipher
));
1397 return cctx
->cipher
->settable_ctx_params(cctx
->algctx
, alg
);
1402 const OSSL_PARAM
*EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX
*cctx
)
1406 if (cctx
!= NULL
&& cctx
->cipher
->gettable_ctx_params
!= NULL
) {
1407 provctx
= ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx
->cipher
));
1408 return cctx
->cipher
->gettable_ctx_params(cctx
->algctx
, provctx
);
1414 static OSSL_LIB_CTX
*EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX
*ctx
)
1416 const EVP_CIPHER
*cipher
= ctx
->cipher
;
1417 const OSSL_PROVIDER
*prov
;
1422 prov
= EVP_CIPHER_get0_provider(cipher
);
1423 return ossl_provider_libctx(prov
);
1427 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX
*ctx
, unsigned char *key
)
1429 if (ctx
->cipher
->flags
& EVP_CIPH_RAND_KEY
)
1430 return EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_RAND_KEY
, 0, key
);
1437 OSSL_LIB_CTX
*libctx
= EVP_CIPHER_CTX_get_libctx(ctx
);
1439 kl
= EVP_CIPHER_CTX_get_key_length(ctx
);
1440 if (kl
<= 0 || RAND_priv_bytes_ex(libctx
, key
, kl
, 0) <= 0)
1444 #endif /* FIPS_MODULE */
1447 EVP_CIPHER_CTX
*EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX
*in
)
1449 EVP_CIPHER_CTX
*out
= EVP_CIPHER_CTX_new();
1451 if (out
!= NULL
&& !EVP_CIPHER_CTX_copy(out
, in
)) {
1452 EVP_CIPHER_CTX_free(out
);
1458 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX
*out
, const EVP_CIPHER_CTX
*in
)
1460 if ((in
== NULL
) || (in
->cipher
== NULL
)) {
1461 ERR_raise(ERR_LIB_EVP
, EVP_R_INPUT_NOT_INITIALIZED
);
1465 if (in
->cipher
->prov
== NULL
)
1468 if (in
->cipher
->dupctx
== NULL
) {
1469 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
1473 EVP_CIPHER_CTX_reset(out
);
1478 if (in
->fetched_cipher
!= NULL
&& !EVP_CIPHER_up_ref(in
->fetched_cipher
)) {
1479 out
->fetched_cipher
= NULL
;
1483 out
->algctx
= in
->cipher
->dupctx(in
->algctx
);
1484 if (out
->algctx
== NULL
) {
1485 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
1491 /* Code below to be removed when legacy support is dropped. */
1494 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1495 /* Make sure it's safe to copy a cipher context using an ENGINE */
1496 if (in
->engine
&& !ENGINE_init(in
->engine
)) {
1497 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
1502 EVP_CIPHER_CTX_reset(out
);
1503 memcpy(out
, in
, sizeof(*out
));
1505 if (in
->cipher_data
&& in
->cipher
->ctx_size
) {
1506 out
->cipher_data
= OPENSSL_malloc(in
->cipher
->ctx_size
);
1507 if (out
->cipher_data
== NULL
) {
1511 memcpy(out
->cipher_data
, in
->cipher_data
, in
->cipher
->ctx_size
);
1514 if (in
->cipher
->flags
& EVP_CIPH_CUSTOM_COPY
)
1515 if (!in
->cipher
->ctrl((EVP_CIPHER_CTX
*)in
, EVP_CTRL_COPY
, 0, out
)) {
1517 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
1523 EVP_CIPHER
*evp_cipher_new(void)
1525 EVP_CIPHER
*cipher
= OPENSSL_zalloc(sizeof(EVP_CIPHER
));
1527 if (cipher
!= NULL
&& !CRYPTO_NEW_REF(&cipher
->refcnt
, 1)) {
1528 OPENSSL_free(cipher
);
1535 * FIPS module note: since internal fetches will be entirely
1536 * provider based, we know that none of its code depends on legacy
1537 * NIDs or any functionality that use them.
1540 /* After removal of legacy support get rid of the need for legacy NIDs */
1541 static void set_legacy_nid(const char *name
, void *vlegacy_nid
)
1544 int *legacy_nid
= vlegacy_nid
;
1546 * We use lowest level function to get the associated method, because
1547 * higher level functions such as EVP_get_cipherbyname() have changed
1548 * to look at providers too.
1550 const void *legacy_method
= OBJ_NAME_get(name
, OBJ_NAME_TYPE_CIPHER_METH
);
1552 if (*legacy_nid
== -1) /* We found a clash already */
1554 if (legacy_method
== NULL
)
1556 nid
= EVP_CIPHER_get_nid(legacy_method
);
1557 if (*legacy_nid
!= NID_undef
&& *legacy_nid
!= nid
) {
1565 static void *evp_cipher_from_algorithm(const int name_id
,
1566 const OSSL_ALGORITHM
*algodef
,
1567 OSSL_PROVIDER
*prov
)
1569 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
1570 EVP_CIPHER
*cipher
= NULL
;
1571 int fnciphcnt
= 0, fnctxcnt
= 0;
1573 if ((cipher
= evp_cipher_new()) == NULL
) {
1574 ERR_raise(ERR_LIB_EVP
, ERR_R_EVP_LIB
);
1579 cipher
->nid
= NID_undef
;
1580 if (!evp_names_do_all(prov
, name_id
, set_legacy_nid
, &cipher
->nid
)
1581 || cipher
->nid
== -1) {
1582 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1583 EVP_CIPHER_free(cipher
);
1588 cipher
->name_id
= name_id
;
1589 if ((cipher
->type_name
= ossl_algorithm_get1_first_name(algodef
)) == NULL
) {
1590 EVP_CIPHER_free(cipher
);
1593 cipher
->description
= algodef
->algorithm_description
;
1595 for (; fns
->function_id
!= 0; fns
++) {
1596 switch (fns
->function_id
) {
1597 case OSSL_FUNC_CIPHER_NEWCTX
:
1598 if (cipher
->newctx
!= NULL
)
1600 cipher
->newctx
= OSSL_FUNC_cipher_newctx(fns
);
1603 case OSSL_FUNC_CIPHER_ENCRYPT_INIT
:
1604 if (cipher
->einit
!= NULL
)
1606 cipher
->einit
= OSSL_FUNC_cipher_encrypt_init(fns
);
1609 case OSSL_FUNC_CIPHER_DECRYPT_INIT
:
1610 if (cipher
->dinit
!= NULL
)
1612 cipher
->dinit
= OSSL_FUNC_cipher_decrypt_init(fns
);
1615 case OSSL_FUNC_CIPHER_UPDATE
:
1616 if (cipher
->cupdate
!= NULL
)
1618 cipher
->cupdate
= OSSL_FUNC_cipher_update(fns
);
1621 case OSSL_FUNC_CIPHER_FINAL
:
1622 if (cipher
->cfinal
!= NULL
)
1624 cipher
->cfinal
= OSSL_FUNC_cipher_final(fns
);
1627 case OSSL_FUNC_CIPHER_CIPHER
:
1628 if (cipher
->ccipher
!= NULL
)
1630 cipher
->ccipher
= OSSL_FUNC_cipher_cipher(fns
);
1632 case OSSL_FUNC_CIPHER_FREECTX
:
1633 if (cipher
->freectx
!= NULL
)
1635 cipher
->freectx
= OSSL_FUNC_cipher_freectx(fns
);
1638 case OSSL_FUNC_CIPHER_DUPCTX
:
1639 if (cipher
->dupctx
!= NULL
)
1641 cipher
->dupctx
= OSSL_FUNC_cipher_dupctx(fns
);
1643 case OSSL_FUNC_CIPHER_GET_PARAMS
:
1644 if (cipher
->get_params
!= NULL
)
1646 cipher
->get_params
= OSSL_FUNC_cipher_get_params(fns
);
1648 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS
:
1649 if (cipher
->get_ctx_params
!= NULL
)
1651 cipher
->get_ctx_params
= OSSL_FUNC_cipher_get_ctx_params(fns
);
1653 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS
:
1654 if (cipher
->set_ctx_params
!= NULL
)
1656 cipher
->set_ctx_params
= OSSL_FUNC_cipher_set_ctx_params(fns
);
1658 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS
:
1659 if (cipher
->gettable_params
!= NULL
)
1661 cipher
->gettable_params
= OSSL_FUNC_cipher_gettable_params(fns
);
1663 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS
:
1664 if (cipher
->gettable_ctx_params
!= NULL
)
1666 cipher
->gettable_ctx_params
=
1667 OSSL_FUNC_cipher_gettable_ctx_params(fns
);
1669 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS
:
1670 if (cipher
->settable_ctx_params
!= NULL
)
1672 cipher
->settable_ctx_params
=
1673 OSSL_FUNC_cipher_settable_ctx_params(fns
);
1677 if ((fnciphcnt
!= 0 && fnciphcnt
!= 3 && fnciphcnt
!= 4)
1678 || (fnciphcnt
== 0 && cipher
->ccipher
== NULL
)
1681 * In order to be a consistent set of functions we must have at least
1682 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1683 * functions, or a single "cipher" function. In all cases we need both
1684 * the "newctx" and "freectx" functions.
1686 EVP_CIPHER_free(cipher
);
1687 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
1690 cipher
->prov
= prov
;
1692 ossl_provider_up_ref(prov
);
1694 if (!evp_cipher_cache_constants(cipher
)) {
1695 EVP_CIPHER_free(cipher
);
1696 ERR_raise(ERR_LIB_EVP
, EVP_R_CACHE_CONSTANTS_FAILED
);
1703 static int evp_cipher_up_ref(void *cipher
)
1705 return EVP_CIPHER_up_ref(cipher
);
1708 static void evp_cipher_free(void *cipher
)
1710 EVP_CIPHER_free(cipher
);
1713 EVP_CIPHER
*EVP_CIPHER_fetch(OSSL_LIB_CTX
*ctx
, const char *algorithm
,
1714 const char *properties
)
1716 EVP_CIPHER
*cipher
=
1717 evp_generic_fetch(ctx
, OSSL_OP_CIPHER
, algorithm
, properties
,
1718 evp_cipher_from_algorithm
, evp_cipher_up_ref
,
1724 int EVP_CIPHER_up_ref(EVP_CIPHER
*cipher
)
1728 if (cipher
->origin
== EVP_ORIG_DYNAMIC
)
1729 CRYPTO_UP_REF(&cipher
->refcnt
, &ref
);
1733 void evp_cipher_free_int(EVP_CIPHER
*cipher
)
1735 OPENSSL_free(cipher
->type_name
);
1736 ossl_provider_free(cipher
->prov
);
1737 CRYPTO_FREE_REF(&cipher
->refcnt
);
1738 OPENSSL_free(cipher
);
1741 void EVP_CIPHER_free(EVP_CIPHER
*cipher
)
1745 if (cipher
== NULL
|| cipher
->origin
!= EVP_ORIG_DYNAMIC
)
1748 CRYPTO_DOWN_REF(&cipher
->refcnt
, &i
);
1751 evp_cipher_free_int(cipher
);
1754 void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX
*libctx
,
1755 void (*fn
)(EVP_CIPHER
*mac
, void *arg
),
1758 evp_generic_do_all(libctx
, OSSL_OP_CIPHER
,
1759 (void (*)(void *, void *))fn
, arg
,
1760 evp_cipher_from_algorithm
, evp_cipher_up_ref
,