2 * Copyright 1995-2021 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 "internal/cryptlib.h"
17 #include <openssl/evp.h>
18 #include <openssl/err.h>
19 #include <openssl/rand.h>
20 #include <openssl/engine.h>
21 #include <openssl/params.h>
22 #include <openssl/core_names.h>
23 #include "crypto/evp.h"
24 #include "internal/provider.h"
25 #include "evp_local.h"
27 int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX
*ctx
)
32 if (ctx
->cipher
== NULL
|| ctx
->cipher
->prov
== NULL
)
35 if (ctx
->provctx
!= NULL
) {
36 if (ctx
->cipher
->freectx
!= NULL
)
37 ctx
->cipher
->freectx(ctx
->provctx
);
40 if (ctx
->fetched_cipher
!= NULL
)
41 EVP_CIPHER_free(ctx
->fetched_cipher
);
42 memset(ctx
, 0, sizeof(*ctx
));
46 /* Remove legacy code below when legacy support is removed. */
49 if (ctx
->cipher
!= NULL
) {
50 if (ctx
->cipher
->cleanup
&& !ctx
->cipher
->cleanup(ctx
))
52 /* Cleanse cipher context data */
53 if (ctx
->cipher_data
&& ctx
->cipher
->ctx_size
)
54 OPENSSL_cleanse(ctx
->cipher_data
, ctx
->cipher
->ctx_size
);
56 OPENSSL_free(ctx
->cipher_data
);
57 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
58 ENGINE_finish(ctx
->engine
);
60 memset(ctx
, 0, sizeof(*ctx
));
64 EVP_CIPHER_CTX
*EVP_CIPHER_CTX_new(void)
66 return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX
));
69 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX
*ctx
)
71 EVP_CIPHER_CTX_reset(ctx
);
75 static int evp_cipher_init_internal(EVP_CIPHER_CTX
*ctx
,
76 const EVP_CIPHER
*cipher
,
77 ENGINE
*impl
, const unsigned char *key
,
78 const unsigned char *iv
, int enc
,
79 const OSSL_PARAM params
[])
82 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
83 ENGINE
*tmpimpl
= NULL
;
86 * enc == 1 means we are encrypting.
87 * enc == 0 means we are decrypting.
88 * enc == -1 means, use the previously initialised value for encrypt/decrypt
98 if (cipher
== NULL
&& ctx
->cipher
== NULL
) {
99 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
103 /* Code below to be removed when legacy support is dropped. */
105 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
107 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
108 * this context may already have an ENGINE! Try to avoid releasing the
109 * previous handle, re-querying for an ENGINE, and having a
110 * reinitialisation, when it may all be unnecessary.
112 if (ctx
->engine
&& ctx
->cipher
113 && (cipher
== NULL
|| cipher
->nid
== ctx
->cipher
->nid
))
116 if (cipher
!= NULL
&& impl
== NULL
) {
117 /* Ask if an ENGINE is reserved for this job */
118 tmpimpl
= ENGINE_get_cipher_engine(cipher
->nid
);
123 * If there are engines involved then we should use legacy handling for now.
125 if (ctx
->engine
!= NULL
126 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
130 if (ctx
->cipher
== ctx
->fetched_cipher
)
132 EVP_CIPHER_free(ctx
->fetched_cipher
);
133 ctx
->fetched_cipher
= NULL
;
137 * Ensure a context left lying around from last time is cleared
140 if (cipher
!= NULL
&& ctx
->cipher
!= NULL
) {
141 OPENSSL_clear_free(ctx
->cipher_data
, ctx
->cipher
->ctx_size
);
142 ctx
->cipher_data
= NULL
;
146 /* Start of non-legacy code below */
148 /* Ensure a context left lying around from last time is cleared */
149 if (cipher
!= NULL
&& ctx
->cipher
!= NULL
) {
150 unsigned long flags
= ctx
->flags
;
152 EVP_CIPHER_CTX_reset(ctx
);
153 /* Restore encrypt and flags */
159 cipher
= ctx
->cipher
;
161 if (cipher
->prov
== NULL
) {
163 /* We only do explicit fetches inside the FIPS module */
164 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
167 EVP_CIPHER
*provciph
=
168 EVP_CIPHER_fetch(NULL
,
169 cipher
->nid
== NID_undef
? "NULL"
170 : OBJ_nid2sn(cipher
->nid
),
173 if (provciph
== NULL
)
176 EVP_CIPHER_free(ctx
->fetched_cipher
);
177 ctx
->fetched_cipher
= provciph
;
181 if (cipher
->prov
!= NULL
) {
182 if (!EVP_CIPHER_up_ref((EVP_CIPHER
*)cipher
)) {
183 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
186 EVP_CIPHER_free(ctx
->fetched_cipher
);
187 ctx
->fetched_cipher
= (EVP_CIPHER
*)cipher
;
189 ctx
->cipher
= cipher
;
190 if (ctx
->provctx
== NULL
) {
191 ctx
->provctx
= ctx
->cipher
->newctx(ossl_provider_ctx(cipher
->prov
));
192 if (ctx
->provctx
== NULL
) {
193 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
198 if ((ctx
->flags
& EVP_CIPH_NO_PADDING
) != 0) {
200 * If this ctx was already set up for no padding then we need to tell
201 * the new cipher about it.
203 if (!EVP_CIPHER_CTX_set_padding(ctx
, 0))
208 if (ctx
->cipher
->einit
== NULL
) {
209 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
213 return ctx
->cipher
->einit(ctx
->provctx
,
216 : EVP_CIPHER_CTX_key_length(ctx
),
219 : EVP_CIPHER_CTX_iv_length(ctx
),
223 if (ctx
->cipher
->dinit
== NULL
) {
224 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
228 return ctx
->cipher
->dinit(ctx
->provctx
,
231 : EVP_CIPHER_CTX_key_length(ctx
),
234 : EVP_CIPHER_CTX_iv_length(ctx
),
237 /* Code below to be removed when legacy support is dropped. */
240 if (cipher
!= NULL
) {
242 * Ensure a context left lying around from last time is cleared (we
243 * previously attempted to avoid this if the same ENGINE and
244 * EVP_CIPHER could be used).
247 unsigned long flags
= ctx
->flags
;
248 EVP_CIPHER_CTX_reset(ctx
);
249 /* Restore encrypt and flags */
253 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
255 if (!ENGINE_init(impl
)) {
256 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
263 /* There's an ENGINE for this job ... (apparently) */
264 const EVP_CIPHER
*c
= ENGINE_get_cipher(impl
, cipher
->nid
);
268 * One positive side-effect of US's export control history,
269 * is that we should at least be able to avoid using US
270 * misspellings of "initialisation"?
272 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
275 /* We'll use the ENGINE's private cipher definition */
278 * Store the ENGINE functional reference so we know 'cipher' came
279 * from an ENGINE and we need to release it when done.
287 ctx
->cipher
= cipher
;
288 if (ctx
->cipher
->ctx_size
) {
289 ctx
->cipher_data
= OPENSSL_zalloc(ctx
->cipher
->ctx_size
);
290 if (ctx
->cipher_data
== NULL
) {
292 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
296 ctx
->cipher_data
= NULL
;
298 ctx
->key_len
= cipher
->key_len
;
299 /* Preserve wrap enable flag, zero everything else */
300 ctx
->flags
&= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
;
301 if (ctx
->cipher
->flags
& EVP_CIPH_CTRL_INIT
) {
302 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_INIT
, 0, NULL
)) {
304 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
309 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
312 if (ctx
->cipher
== NULL
)
315 /* we assume block size is a power of 2 in *cryptUpdate */
316 OPENSSL_assert(ctx
->cipher
->block_size
== 1
317 || ctx
->cipher
->block_size
== 8
318 || ctx
->cipher
->block_size
== 16);
320 if (!(ctx
->flags
& EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
)
321 && EVP_CIPHER_CTX_mode(ctx
) == EVP_CIPH_WRAP_MODE
) {
322 ERR_raise(ERR_LIB_EVP
, EVP_R_WRAP_MODE_NOT_ALLOWED
);
326 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx
)) & EVP_CIPH_CUSTOM_IV
)) {
327 switch (EVP_CIPHER_CTX_mode(ctx
)) {
329 case EVP_CIPH_STREAM_CIPHER
:
330 case EVP_CIPH_ECB_MODE
:
333 case EVP_CIPH_CFB_MODE
:
334 case EVP_CIPH_OFB_MODE
:
339 case EVP_CIPH_CBC_MODE
:
340 n
= EVP_CIPHER_CTX_iv_length(ctx
);
341 if (!ossl_assert(n
>= 0 && n
<= (int)sizeof(ctx
->iv
)))
344 memcpy(ctx
->oiv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
345 memcpy(ctx
->iv
, ctx
->oiv
, EVP_CIPHER_CTX_iv_length(ctx
));
348 case EVP_CIPH_CTR_MODE
:
350 /* Don't reuse IV for CTR mode */
352 memcpy(ctx
->iv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
360 if (key
|| (ctx
->cipher
->flags
& EVP_CIPH_ALWAYS_CALL_INIT
)) {
361 if (!ctx
->cipher
->init(ctx
, key
, iv
, enc
))
366 ctx
->block_mask
= ctx
->cipher
->block_size
- 1;
370 int EVP_CipherInit_ex2(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
371 const unsigned char *key
, const unsigned char *iv
,
372 int enc
, const OSSL_PARAM params
[])
374 return evp_cipher_init_internal(ctx
, cipher
, NULL
, key
, iv
, enc
, params
);
377 int EVP_CipherInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
378 const unsigned char *key
, const unsigned char *iv
, int enc
)
381 EVP_CIPHER_CTX_reset(ctx
);
382 return evp_cipher_init_internal(ctx
, cipher
, NULL
, key
, iv
, enc
, NULL
);
385 int EVP_CipherInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
386 ENGINE
*impl
, const unsigned char *key
,
387 const unsigned char *iv
, int enc
)
389 return evp_cipher_init_internal(ctx
, cipher
, impl
, key
, iv
, enc
, NULL
);
392 int EVP_CipherUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
393 const unsigned char *in
, int inl
)
396 return EVP_EncryptUpdate(ctx
, out
, outl
, in
, inl
);
398 return EVP_DecryptUpdate(ctx
, out
, outl
, in
, inl
);
401 int EVP_CipherFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
404 return EVP_EncryptFinal_ex(ctx
, out
, outl
);
406 return EVP_DecryptFinal_ex(ctx
, out
, outl
);
409 int EVP_CipherFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
412 return EVP_EncryptFinal(ctx
, out
, outl
);
414 return EVP_DecryptFinal(ctx
, out
, outl
);
417 int EVP_EncryptInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
418 const unsigned char *key
, const unsigned char *iv
)
420 return EVP_CipherInit(ctx
, cipher
, key
, iv
, 1);
423 int EVP_EncryptInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
424 ENGINE
*impl
, const unsigned char *key
,
425 const unsigned char *iv
)
427 return EVP_CipherInit_ex(ctx
, cipher
, impl
, key
, iv
, 1);
430 int EVP_EncryptInit_ex2(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
431 const unsigned char *key
, const unsigned char *iv
,
432 const OSSL_PARAM params
[])
434 return EVP_CipherInit_ex2(ctx
, cipher
, key
, iv
, 1, params
);
437 int EVP_DecryptInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
438 const unsigned char *key
, const unsigned char *iv
)
440 return EVP_CipherInit(ctx
, cipher
, key
, iv
, 0);
443 int EVP_DecryptInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
444 ENGINE
*impl
, const unsigned char *key
,
445 const unsigned char *iv
)
447 return EVP_CipherInit_ex(ctx
, cipher
, impl
, key
, iv
, 0);
450 int EVP_DecryptInit_ex2(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
451 const unsigned char *key
, const unsigned char *iv
,
452 const OSSL_PARAM params
[])
454 return EVP_CipherInit_ex2(ctx
, cipher
, key
, iv
, 0, params
);
458 * According to the letter of standard difference between pointers
459 * is specified to be valid only within same object. This makes
460 * it formally challenging to determine if input and output buffers
461 * are not partially overlapping with standard pointer arithmetic.
466 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
468 * Then we have VMS that distinguishes itself by adhering to
469 * sizeof(size_t)==4 even in 64-bit builds, which means that
470 * difference between two pointers might be truncated to 32 bits.
471 * In the context one can even wonder how comparison for
472 * equality is implemented. To be on the safe side we adhere to
473 * PTRDIFF_T even for comparison for equality.
475 # define PTRDIFF_T uint64_t
477 # define PTRDIFF_T size_t
480 int ossl_is_partially_overlapping(const void *ptr1
, const void *ptr2
, int len
)
482 PTRDIFF_T diff
= (PTRDIFF_T
)ptr1
-(PTRDIFF_T
)ptr2
;
484 * Check for partially overlapping buffers. [Binary logical
485 * operations are used instead of boolean to minimize number
486 * of conditional branches.]
488 int overlapped
= (len
> 0) & (diff
!= 0) & ((diff
< (PTRDIFF_T
)len
) |
489 (diff
> (0 - (PTRDIFF_T
)len
)));
494 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX
*ctx
,
495 unsigned char *out
, int *outl
,
496 const unsigned char *in
, int inl
)
498 int i
, j
, bl
, cmpl
= inl
;
500 if (EVP_CIPHER_CTX_test_flags(ctx
, EVP_CIPH_FLAG_LENGTH_BITS
))
501 cmpl
= (cmpl
+ 7) / 8;
503 bl
= ctx
->cipher
->block_size
;
505 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
506 /* If block size > 1 then the cipher will have to do this check */
507 if (bl
== 1 && ossl_is_partially_overlapping(out
, in
, cmpl
)) {
508 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
512 i
= ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
524 if (ossl_is_partially_overlapping(out
+ ctx
->buf_len
, in
, cmpl
)) {
525 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
529 if (ctx
->buf_len
== 0 && (inl
& (ctx
->block_mask
)) == 0) {
530 if (ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
)) {
539 OPENSSL_assert(bl
<= (int)sizeof(ctx
->buf
));
542 memcpy(&(ctx
->buf
[i
]), in
, inl
);
550 * Once we've processed the first j bytes from in, the amount of
551 * data left that is a multiple of the block length is:
552 * (inl - j) & ~(bl - 1)
553 * We must ensure that this amount of data, plus the one block that
554 * we process from ctx->buf does not exceed INT_MAX
556 if (((inl
- j
) & ~(bl
- 1)) > INT_MAX
- bl
) {
557 ERR_raise(ERR_LIB_EVP
, EVP_R_OUTPUT_WOULD_OVERFLOW
);
560 memcpy(&(ctx
->buf
[i
]), in
, j
);
563 if (!ctx
->cipher
->do_cipher(ctx
, out
, ctx
->buf
, bl
))
573 if (!ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
))
579 memcpy(ctx
->buf
, &(in
[inl
]), i
);
585 int EVP_EncryptUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
586 const unsigned char *in
, int inl
)
595 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
599 /* Prevent accidental use of decryption context when encrypting */
601 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
605 if (ctx
->cipher
== NULL
) {
606 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
610 if (ctx
->cipher
->prov
== NULL
)
613 blocksize
= ctx
->cipher
->block_size
;
615 if (ctx
->cipher
->cupdate
== NULL
|| blocksize
< 1) {
616 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
619 ret
= ctx
->cipher
->cupdate(ctx
->provctx
, out
, &soutl
,
620 inl
+ (blocksize
== 1 ? 0 : blocksize
), in
,
624 if (soutl
> INT_MAX
) {
625 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
633 /* Code below to be removed when legacy support is dropped. */
636 return evp_EncryptDecryptUpdate(ctx
, out
, outl
, in
, inl
);
639 int EVP_EncryptFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
642 ret
= EVP_EncryptFinal_ex(ctx
, out
, outl
);
646 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
649 unsigned int i
, b
, bl
;
656 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
660 /* Prevent accidental use of decryption context when encrypting */
662 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
666 if (ctx
->cipher
== NULL
) {
667 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
670 if (ctx
->cipher
->prov
== NULL
)
673 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
675 if (blocksize
< 1 || ctx
->cipher
->cfinal
== NULL
) {
676 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
680 ret
= ctx
->cipher
->cfinal(ctx
->provctx
, out
, &soutl
,
681 blocksize
== 1 ? 0 : blocksize
);
684 if (soutl
> INT_MAX
) {
685 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
693 /* Code below to be removed when legacy support is dropped. */
696 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
697 ret
= ctx
->cipher
->do_cipher(ctx
, out
, NULL
, 0);
705 b
= ctx
->cipher
->block_size
;
706 OPENSSL_assert(b
<= sizeof(ctx
->buf
));
712 if (ctx
->flags
& EVP_CIPH_NO_PADDING
) {
714 ERR_raise(ERR_LIB_EVP
, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH
);
722 for (i
= bl
; i
< b
; i
++)
724 ret
= ctx
->cipher
->do_cipher(ctx
, out
, ctx
->buf
, b
);
732 int EVP_DecryptUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
733 const unsigned char *in
, int inl
)
735 int fix_len
, cmpl
= inl
, ret
;
743 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
747 /* Prevent accidental use of encryption context when decrypting */
749 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
753 if (ctx
->cipher
== NULL
) {
754 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
757 if (ctx
->cipher
->prov
== NULL
)
760 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
762 if (ctx
->cipher
->cupdate
== NULL
|| blocksize
< 1) {
763 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
766 ret
= ctx
->cipher
->cupdate(ctx
->provctx
, out
, &soutl
,
767 inl
+ (blocksize
== 1 ? 0 : blocksize
), in
,
771 if (soutl
> INT_MAX
) {
772 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
780 /* Code below to be removed when legacy support is dropped. */
783 b
= ctx
->cipher
->block_size
;
785 if (EVP_CIPHER_CTX_test_flags(ctx
, EVP_CIPH_FLAG_LENGTH_BITS
))
786 cmpl
= (cmpl
+ 7) / 8;
788 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
789 if (b
== 1 && ossl_is_partially_overlapping(out
, in
, cmpl
)) {
790 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
794 fix_len
= ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
808 if (ctx
->flags
& EVP_CIPH_NO_PADDING
)
809 return evp_EncryptDecryptUpdate(ctx
, out
, outl
, in
, inl
);
811 OPENSSL_assert(b
<= sizeof(ctx
->final
));
813 if (ctx
->final_used
) {
814 /* see comment about PTRDIFF_T comparison above */
815 if (((PTRDIFF_T
)out
== (PTRDIFF_T
)in
)
816 || ossl_is_partially_overlapping(out
, in
, b
)) {
817 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
821 * final_used is only ever set if buf_len is 0. Therefore the maximum
822 * length output we will ever see from evp_EncryptDecryptUpdate is
823 * the maximum multiple of the block length that is <= inl, or just:
825 * Since final_used has been set then the final output length is:
826 * (inl & ~(b - 1)) + b
827 * This must never exceed INT_MAX
829 if ((inl
& ~(b
- 1)) > INT_MAX
- b
) {
830 ERR_raise(ERR_LIB_EVP
, EVP_R_OUTPUT_WOULD_OVERFLOW
);
833 memcpy(out
, ctx
->final
, b
);
839 if (!evp_EncryptDecryptUpdate(ctx
, out
, outl
, in
, inl
))
843 * if we have 'decrypted' a multiple of block size, make sure we have a
844 * copy of this last block
846 if (b
> 1 && !ctx
->buf_len
) {
849 memcpy(ctx
->final
, &out
[*outl
], b
);
859 int EVP_DecryptFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
862 ret
= EVP_DecryptFinal_ex(ctx
, out
, outl
);
866 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
877 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
881 /* Prevent accidental use of encryption context when decrypting */
883 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
887 if (ctx
->cipher
== NULL
) {
888 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
892 if (ctx
->cipher
->prov
== NULL
)
895 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
897 if (blocksize
< 1 || ctx
->cipher
->cfinal
== NULL
) {
898 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
902 ret
= ctx
->cipher
->cfinal(ctx
->provctx
, out
, &soutl
,
903 blocksize
== 1 ? 0 : blocksize
);
906 if (soutl
> INT_MAX
) {
907 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
915 /* Code below to be removed when legacy support is dropped. */
919 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
920 i
= ctx
->cipher
->do_cipher(ctx
, out
, NULL
, 0);
928 b
= ctx
->cipher
->block_size
;
929 if (ctx
->flags
& EVP_CIPH_NO_PADDING
) {
931 ERR_raise(ERR_LIB_EVP
, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH
);
938 if (ctx
->buf_len
|| !ctx
->final_used
) {
939 ERR_raise(ERR_LIB_EVP
, EVP_R_WRONG_FINAL_BLOCK_LENGTH
);
942 OPENSSL_assert(b
<= sizeof(ctx
->final
));
945 * The following assumes that the ciphertext has been authenticated.
946 * Otherwise it provides a padding oracle.
948 n
= ctx
->final
[b
- 1];
949 if (n
== 0 || n
> (int)b
) {
950 ERR_raise(ERR_LIB_EVP
, EVP_R_BAD_DECRYPT
);
953 for (i
= 0; i
< n
; i
++) {
954 if (ctx
->final
[--b
] != n
) {
955 ERR_raise(ERR_LIB_EVP
, EVP_R_BAD_DECRYPT
);
959 n
= ctx
->cipher
->block_size
- n
;
960 for (i
= 0; i
< n
; i
++)
961 out
[i
] = ctx
->final
[i
];
968 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX
*c
, int keylen
)
970 if (c
->cipher
->prov
!= NULL
) {
972 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
975 if (EVP_CIPHER_CTX_key_length(c
) == keylen
)
978 /* Check the cipher actually understands this parameter */
979 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c
->cipher
),
980 OSSL_CIPHER_PARAM_KEYLEN
) == NULL
)
983 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &len
);
984 ok
= evp_do_ciph_ctx_setparams(c
->cipher
, c
->provctx
, params
);
986 return ok
> 0 ? 1 : 0;
989 /* Code below to be removed when legacy support is dropped. */
992 * Note there have never been any built-in ciphers that define this flag
993 * since it was first introduced.
995 if (c
->cipher
->flags
& EVP_CIPH_CUSTOM_KEY_LENGTH
)
996 return EVP_CIPHER_CTX_ctrl(c
, EVP_CTRL_SET_KEY_LENGTH
, keylen
, NULL
);
997 if (EVP_CIPHER_CTX_key_length(c
) == keylen
)
999 if ((keylen
> 0) && (c
->cipher
->flags
& EVP_CIPH_VARIABLE_LENGTH
)) {
1000 c
->key_len
= keylen
;
1003 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY_LENGTH
);
1007 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX
*ctx
, int pad
)
1010 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
1011 unsigned int pd
= pad
;
1014 ctx
->flags
&= ~EVP_CIPH_NO_PADDING
;
1016 ctx
->flags
|= EVP_CIPH_NO_PADDING
;
1018 if (ctx
->cipher
!= NULL
&& ctx
->cipher
->prov
== NULL
)
1020 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING
, &pd
);
1021 ok
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1026 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
, void *ptr
)
1028 int ret
= EVP_CTRL_RET_UNSUPPORTED
;
1032 OSSL_PARAM params
[4] = {
1033 OSSL_PARAM_END
, OSSL_PARAM_END
, OSSL_PARAM_END
, OSSL_PARAM_END
1036 if (ctx
== NULL
|| ctx
->cipher
== NULL
) {
1037 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
1041 if (ctx
->cipher
->prov
== NULL
)
1045 case EVP_CTRL_SET_KEY_LENGTH
:
1046 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &sz
);
1048 case EVP_CTRL_RAND_KEY
: /* Used by DES */
1051 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY
,
1057 * EVP_CTRL_INIT is purely legacy, no provider counterpart.
1058 * As a matter of fact, this should be dead code, but some caller
1059 * might still do a direct control call with this command, so...
1060 * Legacy methods return 1 except for exceptional circumstances, so
1061 * we do the same here to not be disruptive.
1064 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS
: /* Used by DASYNC */
1067 case EVP_CTRL_AEAD_SET_IVLEN
:
1070 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN
, &sz
);
1072 case EVP_CTRL_AEAD_SET_IV_FIXED
:
1073 params
[0] = OSSL_PARAM_construct_octet_string(
1074 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED
, ptr
, sz
);
1076 case EVP_CTRL_GCM_IV_GEN
:
1079 sz
= 0; /* special case that uses the iv length */
1080 params
[0] = OSSL_PARAM_construct_octet_string(
1081 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN
, ptr
, sz
);
1083 case EVP_CTRL_GCM_SET_IV_INV
:
1086 params
[0] = OSSL_PARAM_construct_octet_string(
1087 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV
, ptr
, sz
);
1089 case EVP_CTRL_GET_RC5_ROUNDS
:
1090 set_params
= 0; /* Fall thru */
1091 case EVP_CTRL_SET_RC5_ROUNDS
:
1094 i
= (unsigned int)arg
;
1095 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS
, &i
);
1097 case EVP_CTRL_SET_SPEED
:
1100 i
= (unsigned int)arg
;
1101 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED
, &i
);
1103 case EVP_CTRL_AEAD_GET_TAG
:
1104 set_params
= 0; /* Fall thru */
1105 case EVP_CTRL_AEAD_SET_TAG
:
1106 params
[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG
,
1109 case EVP_CTRL_AEAD_TLS1_AAD
:
1110 /* This one does a set and a get - since it returns a size */
1112 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD
,
1114 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1118 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD
, &sz
);
1119 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1123 #ifndef OPENSSL_NO_RC2
1124 case EVP_CTRL_GET_RC2_KEY_BITS
:
1125 set_params
= 0; /* Fall thru */
1126 case EVP_CTRL_SET_RC2_KEY_BITS
:
1127 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS
, &sz
);
1129 #endif /* OPENSSL_NO_RC2 */
1130 #if !defined(OPENSSL_NO_MULTIBLOCK)
1131 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE
:
1132 params
[0] = OSSL_PARAM_construct_size_t(
1133 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT
, &sz
);
1134 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1138 params
[0] = OSSL_PARAM_construct_size_t(
1139 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE
, &sz
);
1140 params
[1] = OSSL_PARAM_construct_end();
1141 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1145 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD
: {
1146 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*p
=
1147 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*)ptr
;
1149 if (arg
< (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
))
1152 params
[0] = OSSL_PARAM_construct_octet_string(
1153 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD
, (void*)p
->inp
, p
->len
);
1154 params
[1] = OSSL_PARAM_construct_uint(
1155 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE
, &p
->interleave
);
1156 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1159 /* Retrieve the return values changed by the set */
1160 params
[0] = OSSL_PARAM_construct_size_t(
1161 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN
, &sz
);
1162 params
[1] = OSSL_PARAM_construct_uint(
1163 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE
, &p
->interleave
);
1164 params
[2] = OSSL_PARAM_construct_end();
1165 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1170 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT
: {
1171 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*p
=
1172 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*)ptr
;
1174 params
[0] = OSSL_PARAM_construct_octet_string(
1175 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC
, p
->out
, p
->len
);
1177 params
[1] = OSSL_PARAM_construct_octet_string(
1178 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN
, (void*)p
->inp
,
1180 params
[2] = OSSL_PARAM_construct_uint(
1181 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE
, &p
->interleave
);
1182 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1185 params
[0] = OSSL_PARAM_construct_size_t(
1186 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN
, &sz
);
1187 params
[1] = OSSL_PARAM_construct_end();
1188 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1193 #endif /* OPENSSL_NO_MULTIBLOCK */
1194 case EVP_CTRL_AEAD_SET_MAC_KEY
:
1197 params
[0] = OSSL_PARAM_construct_octet_string(
1198 OSSL_CIPHER_PARAM_AEAD_MAC_KEY
, ptr
, sz
);
1203 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1205 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1208 /* Code below to be removed when legacy support is dropped. */
1210 if (ctx
->cipher
->ctrl
== NULL
) {
1211 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_NOT_IMPLEMENTED
);
1215 ret
= ctx
->cipher
->ctrl(ctx
, type
, arg
, ptr
);
1218 if (ret
== EVP_CTRL_RET_UNSUPPORTED
) {
1219 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED
);
1225 int EVP_CIPHER_get_params(EVP_CIPHER
*cipher
, OSSL_PARAM params
[])
1227 if (cipher
!= NULL
&& cipher
->get_params
!= NULL
)
1228 return cipher
->get_params(params
);
1232 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX
*ctx
, const OSSL_PARAM params
[])
1234 if (ctx
->cipher
!= NULL
&& ctx
->cipher
->set_ctx_params
!= NULL
)
1235 return ctx
->cipher
->set_ctx_params(ctx
->provctx
, params
);
1239 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX
*ctx
, OSSL_PARAM params
[])
1241 if (ctx
->cipher
!= NULL
&& ctx
->cipher
->get_ctx_params
!= NULL
)
1242 return ctx
->cipher
->get_ctx_params(ctx
->provctx
, params
);
1246 const OSSL_PARAM
*EVP_CIPHER_gettable_params(const EVP_CIPHER
*cipher
)
1248 if (cipher
!= NULL
&& cipher
->gettable_params
!= NULL
)
1249 return cipher
->gettable_params(
1250 ossl_provider_ctx(EVP_CIPHER_provider(cipher
)));
1254 const OSSL_PARAM
*EVP_CIPHER_settable_ctx_params(const EVP_CIPHER
*cipher
)
1258 if (cipher
!= NULL
&& cipher
->settable_ctx_params
!= NULL
) {
1259 alg
= ossl_provider_ctx(EVP_CIPHER_provider(cipher
));
1260 return cipher
->settable_ctx_params(NULL
, alg
);
1265 const OSSL_PARAM
*EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER
*cipher
)
1269 if (cipher
!= NULL
&& cipher
->gettable_ctx_params
!= NULL
) {
1270 alg
= ossl_provider_ctx(EVP_CIPHER_provider(cipher
));
1271 return cipher
->gettable_ctx_params(NULL
, alg
);
1276 const OSSL_PARAM
*EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX
*cctx
)
1280 if (cctx
!= NULL
&& cctx
->cipher
->settable_ctx_params
!= NULL
) {
1281 alg
= ossl_provider_ctx(EVP_CIPHER_provider(cctx
->cipher
));
1282 return cctx
->cipher
->settable_ctx_params(cctx
->provctx
, alg
);
1287 const OSSL_PARAM
*EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX
*cctx
)
1291 if (cctx
!= NULL
&& cctx
->cipher
->gettable_ctx_params
!= NULL
) {
1292 alg
= ossl_provider_ctx(EVP_CIPHER_provider(cctx
->cipher
));
1293 return cctx
->cipher
->gettable_ctx_params(cctx
->provctx
, alg
);
1299 static OSSL_LIB_CTX
*EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX
*ctx
)
1301 const EVP_CIPHER
*cipher
= ctx
->cipher
;
1302 const OSSL_PROVIDER
*prov
;
1307 prov
= EVP_CIPHER_provider(cipher
);
1308 return ossl_provider_libctx(prov
);
1312 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX
*ctx
, unsigned char *key
)
1314 if (ctx
->cipher
->flags
& EVP_CIPH_RAND_KEY
)
1315 return EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_RAND_KEY
, 0, key
);
1322 OSSL_LIB_CTX
*libctx
= EVP_CIPHER_CTX_get_libctx(ctx
);
1324 kl
= EVP_CIPHER_CTX_key_length(ctx
);
1325 if (kl
<= 0 || RAND_priv_bytes_ex(libctx
, key
, kl
) <= 0)
1329 #endif /* FIPS_MODULE */
1332 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX
*out
, const EVP_CIPHER_CTX
*in
)
1334 if ((in
== NULL
) || (in
->cipher
== NULL
)) {
1335 ERR_raise(ERR_LIB_EVP
, EVP_R_INPUT_NOT_INITIALIZED
);
1339 if (in
->cipher
->prov
== NULL
)
1342 if (in
->cipher
->dupctx
== NULL
) {
1343 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
1347 EVP_CIPHER_CTX_reset(out
);
1350 out
->provctx
= NULL
;
1352 if (in
->fetched_cipher
!= NULL
&& !EVP_CIPHER_up_ref(in
->fetched_cipher
)) {
1353 out
->fetched_cipher
= NULL
;
1357 out
->provctx
= in
->cipher
->dupctx(in
->provctx
);
1358 if (out
->provctx
== NULL
) {
1359 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
1365 /* Code below to be removed when legacy support is dropped. */
1368 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1369 /* Make sure it's safe to copy a cipher context using an ENGINE */
1370 if (in
->engine
&& !ENGINE_init(in
->engine
)) {
1371 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
1376 EVP_CIPHER_CTX_reset(out
);
1377 memcpy(out
, in
, sizeof(*out
));
1379 if (in
->cipher_data
&& in
->cipher
->ctx_size
) {
1380 out
->cipher_data
= OPENSSL_malloc(in
->cipher
->ctx_size
);
1381 if (out
->cipher_data
== NULL
) {
1383 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1386 memcpy(out
->cipher_data
, in
->cipher_data
, in
->cipher
->ctx_size
);
1389 if (in
->cipher
->flags
& EVP_CIPH_CUSTOM_COPY
)
1390 if (!in
->cipher
->ctrl((EVP_CIPHER_CTX
*)in
, EVP_CTRL_COPY
, 0, out
)) {
1392 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
1398 EVP_CIPHER
*evp_cipher_new(void)
1400 EVP_CIPHER
*cipher
= OPENSSL_zalloc(sizeof(EVP_CIPHER
));
1402 if (cipher
!= NULL
) {
1403 cipher
->lock
= CRYPTO_THREAD_lock_new();
1404 if (cipher
->lock
== NULL
) {
1405 OPENSSL_free(cipher
);
1414 * FIPS module note: since internal fetches will be entirely
1415 * provider based, we know that none of its code depends on legacy
1416 * NIDs or any functionality that use them.
1419 /* After removal of legacy support get rid of the need for legacy NIDs */
1420 static void set_legacy_nid(const char *name
, void *vlegacy_nid
)
1423 int *legacy_nid
= vlegacy_nid
;
1425 * We use lowest level function to get the associated method, because
1426 * higher level functions such as EVP_get_cipherbyname() have changed
1427 * to look at providers too.
1429 const void *legacy_method
= OBJ_NAME_get(name
, OBJ_NAME_TYPE_CIPHER_METH
);
1431 if (*legacy_nid
== -1) /* We found a clash already */
1433 if (legacy_method
== NULL
)
1435 nid
= EVP_CIPHER_nid(legacy_method
);
1436 if (*legacy_nid
!= NID_undef
&& *legacy_nid
!= nid
) {
1444 static void *evp_cipher_from_algorithm(const int name_id
,
1445 const OSSL_ALGORITHM
*algodef
,
1446 OSSL_PROVIDER
*prov
)
1448 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
1449 EVP_CIPHER
*cipher
= NULL
;
1450 int fnciphcnt
= 0, fnctxcnt
= 0;
1452 if ((cipher
= evp_cipher_new()) == NULL
) {
1453 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1458 cipher
->nid
= NID_undef
;
1459 if (!evp_names_do_all(prov
, name_id
, set_legacy_nid
, &cipher
->nid
)
1460 || cipher
->nid
== -1) {
1461 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1462 EVP_CIPHER_free(cipher
);
1467 cipher
->name_id
= name_id
;
1468 cipher
->description
= algodef
->algorithm_description
;
1470 for (; fns
->function_id
!= 0; fns
++) {
1471 switch (fns
->function_id
) {
1472 case OSSL_FUNC_CIPHER_NEWCTX
:
1473 if (cipher
->newctx
!= NULL
)
1475 cipher
->newctx
= OSSL_FUNC_cipher_newctx(fns
);
1478 case OSSL_FUNC_CIPHER_ENCRYPT_INIT
:
1479 if (cipher
->einit
!= NULL
)
1481 cipher
->einit
= OSSL_FUNC_cipher_encrypt_init(fns
);
1484 case OSSL_FUNC_CIPHER_DECRYPT_INIT
:
1485 if (cipher
->dinit
!= NULL
)
1487 cipher
->dinit
= OSSL_FUNC_cipher_decrypt_init(fns
);
1490 case OSSL_FUNC_CIPHER_UPDATE
:
1491 if (cipher
->cupdate
!= NULL
)
1493 cipher
->cupdate
= OSSL_FUNC_cipher_update(fns
);
1496 case OSSL_FUNC_CIPHER_FINAL
:
1497 if (cipher
->cfinal
!= NULL
)
1499 cipher
->cfinal
= OSSL_FUNC_cipher_final(fns
);
1502 case OSSL_FUNC_CIPHER_CIPHER
:
1503 if (cipher
->ccipher
!= NULL
)
1505 cipher
->ccipher
= OSSL_FUNC_cipher_cipher(fns
);
1507 case OSSL_FUNC_CIPHER_FREECTX
:
1508 if (cipher
->freectx
!= NULL
)
1510 cipher
->freectx
= OSSL_FUNC_cipher_freectx(fns
);
1513 case OSSL_FUNC_CIPHER_DUPCTX
:
1514 if (cipher
->dupctx
!= NULL
)
1516 cipher
->dupctx
= OSSL_FUNC_cipher_dupctx(fns
);
1518 case OSSL_FUNC_CIPHER_GET_PARAMS
:
1519 if (cipher
->get_params
!= NULL
)
1521 cipher
->get_params
= OSSL_FUNC_cipher_get_params(fns
);
1523 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS
:
1524 if (cipher
->get_ctx_params
!= NULL
)
1526 cipher
->get_ctx_params
= OSSL_FUNC_cipher_get_ctx_params(fns
);
1528 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS
:
1529 if (cipher
->set_ctx_params
!= NULL
)
1531 cipher
->set_ctx_params
= OSSL_FUNC_cipher_set_ctx_params(fns
);
1533 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS
:
1534 if (cipher
->gettable_params
!= NULL
)
1536 cipher
->gettable_params
= OSSL_FUNC_cipher_gettable_params(fns
);
1538 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS
:
1539 if (cipher
->gettable_ctx_params
!= NULL
)
1541 cipher
->gettable_ctx_params
=
1542 OSSL_FUNC_cipher_gettable_ctx_params(fns
);
1544 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS
:
1545 if (cipher
->settable_ctx_params
!= NULL
)
1547 cipher
->settable_ctx_params
=
1548 OSSL_FUNC_cipher_settable_ctx_params(fns
);
1552 if ((fnciphcnt
!= 0 && fnciphcnt
!= 3 && fnciphcnt
!= 4)
1553 || (fnciphcnt
== 0 && cipher
->ccipher
== NULL
)
1556 * In order to be a consistent set of functions we must have at least
1557 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1558 * functions, or a single "cipher" function. In all cases we need both
1559 * the "newctx" and "freectx" functions.
1561 EVP_CIPHER_free(cipher
);
1562 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
1565 cipher
->prov
= prov
;
1567 ossl_provider_up_ref(prov
);
1569 if (!evp_cipher_cache_constants(cipher
)) {
1570 EVP_CIPHER_free(cipher
);
1571 ERR_raise(ERR_LIB_EVP
, EVP_R_CACHE_CONSTANTS_FAILED
);
1578 static int evp_cipher_up_ref(void *cipher
)
1580 return EVP_CIPHER_up_ref(cipher
);
1583 static void evp_cipher_free(void *cipher
)
1585 EVP_CIPHER_free(cipher
);
1588 EVP_CIPHER
*EVP_CIPHER_fetch(OSSL_LIB_CTX
*ctx
, const char *algorithm
,
1589 const char *properties
)
1591 EVP_CIPHER
*cipher
=
1592 evp_generic_fetch(ctx
, OSSL_OP_CIPHER
, algorithm
, properties
,
1593 evp_cipher_from_algorithm
, evp_cipher_up_ref
,
1599 int EVP_CIPHER_up_ref(EVP_CIPHER
*cipher
)
1603 CRYPTO_UP_REF(&cipher
->refcnt
, &ref
, cipher
->lock
);
1607 void EVP_CIPHER_free(EVP_CIPHER
*cipher
)
1614 CRYPTO_DOWN_REF(&cipher
->refcnt
, &i
, cipher
->lock
);
1617 ossl_provider_free(cipher
->prov
);
1618 CRYPTO_THREAD_lock_free(cipher
->lock
);
1619 OPENSSL_free(cipher
);
1622 void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX
*libctx
,
1623 void (*fn
)(EVP_CIPHER
*mac
, void *arg
),
1626 evp_generic_do_all(libctx
, OSSL_OP_CIPHER
,
1627 (void (*)(void *, void *))fn
, arg
,
1628 evp_cipher_from_algorithm
, evp_cipher_free
);