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 /* TODO(3.0): Remove legacy code below */
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 int EVP_CipherInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
76 const unsigned char *key
, const unsigned char *iv
, int enc
)
79 EVP_CIPHER_CTX_reset(ctx
);
80 return EVP_CipherInit_ex(ctx
, cipher
, NULL
, key
, iv
, enc
);
83 int EVP_CipherInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
84 ENGINE
*impl
, const unsigned char *key
,
85 const unsigned char *iv
, int enc
)
87 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
88 ENGINE
*tmpimpl
= NULL
;
91 * enc == 1 means we are encrypting.
92 * enc == 0 means we are decrypting.
93 * enc == -1 means, use the previously initialised value for encrypt/decrypt
103 if (cipher
== NULL
&& ctx
->cipher
== NULL
) {
104 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
108 /* TODO(3.0): Legacy work around code below. Remove this */
110 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
112 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
113 * this context may already have an ENGINE! Try to avoid releasing the
114 * previous handle, re-querying for an ENGINE, and having a
115 * reinitialisation, when it may all be unnecessary.
117 if (ctx
->engine
&& ctx
->cipher
118 && (cipher
== NULL
|| cipher
->nid
== ctx
->cipher
->nid
))
121 if (cipher
!= NULL
&& impl
== NULL
) {
122 /* Ask if an ENGINE is reserved for this job */
123 tmpimpl
= ENGINE_get_cipher_engine(cipher
->nid
);
128 * If there are engines involved then we should use legacy handling for now.
130 if (ctx
->engine
!= NULL
131 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
135 if (ctx
->cipher
== ctx
->fetched_cipher
)
137 EVP_CIPHER_free(ctx
->fetched_cipher
);
138 ctx
->fetched_cipher
= NULL
;
142 * Ensure a context left lying around from last time is cleared
145 if (cipher
!= NULL
&& ctx
->cipher
!= NULL
) {
146 OPENSSL_clear_free(ctx
->cipher_data
, ctx
->cipher
->ctx_size
);
147 ctx
->cipher_data
= NULL
;
151 /* TODO(3.0): Start of non-legacy code below */
153 /* Ensure a context left lying around from last time is cleared */
154 if (cipher
!= NULL
&& ctx
->cipher
!= NULL
) {
155 unsigned long flags
= ctx
->flags
;
157 EVP_CIPHER_CTX_reset(ctx
);
158 /* Restore encrypt and flags */
164 cipher
= ctx
->cipher
;
166 if (cipher
->prov
== NULL
) {
168 /* We only do explicit fetches inside the FIPS module */
169 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
172 EVP_CIPHER
*provciph
=
173 EVP_CIPHER_fetch(NULL
,
174 cipher
->nid
== NID_undef
? "NULL"
175 : OBJ_nid2sn(cipher
->nid
),
178 if (provciph
== NULL
)
181 EVP_CIPHER_free(ctx
->fetched_cipher
);
182 ctx
->fetched_cipher
= provciph
;
186 if (cipher
->prov
!= NULL
) {
187 if (!EVP_CIPHER_up_ref((EVP_CIPHER
*)cipher
)) {
188 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
191 EVP_CIPHER_free(ctx
->fetched_cipher
);
192 ctx
->fetched_cipher
= (EVP_CIPHER
*)cipher
;
194 ctx
->cipher
= cipher
;
195 if (ctx
->provctx
== NULL
) {
196 ctx
->provctx
= ctx
->cipher
->newctx(ossl_provider_ctx(cipher
->prov
));
197 if (ctx
->provctx
== NULL
) {
198 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
203 if ((ctx
->flags
& EVP_CIPH_NO_PADDING
) != 0) {
205 * If this ctx was already set up for no padding then we need to tell
206 * the new cipher about it.
208 if (!EVP_CIPHER_CTX_set_padding(ctx
, 0))
213 if (ctx
->cipher
->einit
== NULL
) {
214 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
218 return ctx
->cipher
->einit(ctx
->provctx
,
221 : EVP_CIPHER_CTX_key_length(ctx
),
224 : EVP_CIPHER_CTX_iv_length(ctx
));
227 if (ctx
->cipher
->dinit
== NULL
) {
228 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
232 return ctx
->cipher
->dinit(ctx
->provctx
,
235 : EVP_CIPHER_CTX_key_length(ctx
),
238 : EVP_CIPHER_CTX_iv_length(ctx
));
240 /* TODO(3.0): Remove legacy code below */
243 if (cipher
!= NULL
) {
245 * Ensure a context left lying around from last time is cleared (we
246 * previously attempted to avoid this if the same ENGINE and
247 * EVP_CIPHER could be used).
250 unsigned long flags
= ctx
->flags
;
251 EVP_CIPHER_CTX_reset(ctx
);
252 /* Restore encrypt and flags */
256 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
258 if (!ENGINE_init(impl
)) {
259 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
266 /* There's an ENGINE for this job ... (apparently) */
267 const EVP_CIPHER
*c
= ENGINE_get_cipher(impl
, cipher
->nid
);
271 * One positive side-effect of US's export control history,
272 * is that we should at least be able to avoid using US
273 * misspellings of "initialisation"?
275 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
278 /* We'll use the ENGINE's private cipher definition */
281 * Store the ENGINE functional reference so we know 'cipher' came
282 * from an ENGINE and we need to release it when done.
290 ctx
->cipher
= cipher
;
291 if (ctx
->cipher
->ctx_size
) {
292 ctx
->cipher_data
= OPENSSL_zalloc(ctx
->cipher
->ctx_size
);
293 if (ctx
->cipher_data
== NULL
) {
295 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
299 ctx
->cipher_data
= NULL
;
301 ctx
->key_len
= cipher
->key_len
;
302 /* Preserve wrap enable flag, zero everything else */
303 ctx
->flags
&= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
;
304 if (ctx
->cipher
->flags
& EVP_CIPH_CTRL_INIT
) {
305 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_INIT
, 0, NULL
)) {
307 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
312 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
315 if (ctx
->cipher
== NULL
)
318 /* we assume block size is a power of 2 in *cryptUpdate */
319 OPENSSL_assert(ctx
->cipher
->block_size
== 1
320 || ctx
->cipher
->block_size
== 8
321 || ctx
->cipher
->block_size
== 16);
323 if (!(ctx
->flags
& EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
)
324 && EVP_CIPHER_CTX_mode(ctx
) == EVP_CIPH_WRAP_MODE
) {
325 ERR_raise(ERR_LIB_EVP
, EVP_R_WRAP_MODE_NOT_ALLOWED
);
329 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx
)) & EVP_CIPH_CUSTOM_IV
)) {
330 switch (EVP_CIPHER_CTX_mode(ctx
)) {
332 case EVP_CIPH_STREAM_CIPHER
:
333 case EVP_CIPH_ECB_MODE
:
336 case EVP_CIPH_CFB_MODE
:
337 case EVP_CIPH_OFB_MODE
:
342 case EVP_CIPH_CBC_MODE
:
344 OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx
) <=
345 (int)sizeof(ctx
->iv
));
347 memcpy(ctx
->oiv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
348 memcpy(ctx
->iv
, ctx
->oiv
, EVP_CIPHER_CTX_iv_length(ctx
));
351 case EVP_CIPH_CTR_MODE
:
353 /* Don't reuse IV for CTR mode */
355 memcpy(ctx
->iv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
363 if (key
|| (ctx
->cipher
->flags
& EVP_CIPH_ALWAYS_CALL_INIT
)) {
364 if (!ctx
->cipher
->init(ctx
, key
, iv
, enc
))
369 ctx
->block_mask
= ctx
->cipher
->block_size
- 1;
373 int EVP_CipherUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
374 const unsigned char *in
, int inl
)
377 return EVP_EncryptUpdate(ctx
, out
, outl
, in
, inl
);
379 return EVP_DecryptUpdate(ctx
, out
, outl
, in
, inl
);
382 int EVP_CipherFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
385 return EVP_EncryptFinal_ex(ctx
, out
, outl
);
387 return EVP_DecryptFinal_ex(ctx
, out
, outl
);
390 int EVP_CipherFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
393 return EVP_EncryptFinal(ctx
, out
, outl
);
395 return EVP_DecryptFinal(ctx
, out
, outl
);
398 int EVP_EncryptInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
399 const unsigned char *key
, const unsigned char *iv
)
401 return EVP_CipherInit(ctx
, cipher
, key
, iv
, 1);
404 int EVP_EncryptInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
405 ENGINE
*impl
, const unsigned char *key
,
406 const unsigned char *iv
)
408 return EVP_CipherInit_ex(ctx
, cipher
, impl
, key
, iv
, 1);
411 int EVP_DecryptInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
412 const unsigned char *key
, const unsigned char *iv
)
414 return EVP_CipherInit(ctx
, cipher
, key
, iv
, 0);
417 int EVP_DecryptInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
418 ENGINE
*impl
, const unsigned char *key
,
419 const unsigned char *iv
)
421 return EVP_CipherInit_ex(ctx
, cipher
, impl
, key
, iv
, 0);
425 * According to the letter of standard difference between pointers
426 * is specified to be valid only within same object. This makes
427 * it formally challenging to determine if input and output buffers
428 * are not partially overlapping with standard pointer arithmetic.
433 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
435 * Then we have VMS that distinguishes itself by adhering to
436 * sizeof(size_t)==4 even in 64-bit builds, which means that
437 * difference between two pointers might be truncated to 32 bits.
438 * In the context one can even wonder how comparison for
439 * equality is implemented. To be on the safe side we adhere to
440 * PTRDIFF_T even for comparison for equality.
442 # define PTRDIFF_T uint64_t
444 # define PTRDIFF_T size_t
447 int is_partially_overlapping(const void *ptr1
, const void *ptr2
, int len
)
449 PTRDIFF_T diff
= (PTRDIFF_T
)ptr1
-(PTRDIFF_T
)ptr2
;
451 * Check for partially overlapping buffers. [Binary logical
452 * operations are used instead of boolean to minimize number
453 * of conditional branches.]
455 int overlapped
= (len
> 0) & (diff
!= 0) & ((diff
< (PTRDIFF_T
)len
) |
456 (diff
> (0 - (PTRDIFF_T
)len
)));
461 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX
*ctx
,
462 unsigned char *out
, int *outl
,
463 const unsigned char *in
, int inl
)
465 int i
, j
, bl
, cmpl
= inl
;
467 if (EVP_CIPHER_CTX_test_flags(ctx
, EVP_CIPH_FLAG_LENGTH_BITS
))
468 cmpl
= (cmpl
+ 7) / 8;
470 bl
= ctx
->cipher
->block_size
;
472 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
473 /* If block size > 1 then the cipher will have to do this check */
474 if (bl
== 1 && is_partially_overlapping(out
, in
, cmpl
)) {
475 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
479 i
= ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
491 if (is_partially_overlapping(out
+ ctx
->buf_len
, in
, cmpl
)) {
492 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
496 if (ctx
->buf_len
== 0 && (inl
& (ctx
->block_mask
)) == 0) {
497 if (ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
)) {
506 OPENSSL_assert(bl
<= (int)sizeof(ctx
->buf
));
509 memcpy(&(ctx
->buf
[i
]), in
, inl
);
517 * Once we've processed the first j bytes from in, the amount of
518 * data left that is a multiple of the block length is:
519 * (inl - j) & ~(bl - 1)
520 * We must ensure that this amount of data, plus the one block that
521 * we process from ctx->buf does not exceed INT_MAX
523 if (((inl
- j
) & ~(bl
- 1)) > INT_MAX
- bl
) {
524 ERR_raise(ERR_LIB_EVP
, EVP_R_OUTPUT_WOULD_OVERFLOW
);
527 memcpy(&(ctx
->buf
[i
]), in
, j
);
530 if (!ctx
->cipher
->do_cipher(ctx
, out
, ctx
->buf
, bl
))
540 if (!ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
))
546 memcpy(ctx
->buf
, &(in
[inl
]), i
);
552 int EVP_EncryptUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
553 const unsigned char *in
, int inl
)
562 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
566 /* Prevent accidental use of decryption context when encrypting */
568 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
572 if (ctx
->cipher
== NULL
) {
573 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
577 if (ctx
->cipher
->prov
== NULL
)
580 blocksize
= ctx
->cipher
->block_size
;
582 if (ctx
->cipher
->cupdate
== NULL
|| blocksize
< 1) {
583 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
586 ret
= ctx
->cipher
->cupdate(ctx
->provctx
, out
, &soutl
,
587 inl
+ (blocksize
== 1 ? 0 : blocksize
), in
,
591 if (soutl
> INT_MAX
) {
592 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
600 /* TODO(3.0): Remove legacy code below */
603 return evp_EncryptDecryptUpdate(ctx
, out
, outl
, in
, inl
);
606 int EVP_EncryptFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
609 ret
= EVP_EncryptFinal_ex(ctx
, out
, outl
);
613 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
616 unsigned int i
, b
, bl
;
623 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
627 /* Prevent accidental use of decryption context when encrypting */
629 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
633 if (ctx
->cipher
== NULL
) {
634 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
637 if (ctx
->cipher
->prov
== NULL
)
640 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
642 if (blocksize
< 1 || ctx
->cipher
->cfinal
== NULL
) {
643 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
647 ret
= ctx
->cipher
->cfinal(ctx
->provctx
, out
, &soutl
,
648 blocksize
== 1 ? 0 : blocksize
);
651 if (soutl
> INT_MAX
) {
652 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
660 /* TODO(3.0): Remove legacy code below */
663 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
664 ret
= ctx
->cipher
->do_cipher(ctx
, out
, NULL
, 0);
672 b
= ctx
->cipher
->block_size
;
673 OPENSSL_assert(b
<= sizeof(ctx
->buf
));
679 if (ctx
->flags
& EVP_CIPH_NO_PADDING
) {
681 ERR_raise(ERR_LIB_EVP
, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH
);
689 for (i
= bl
; i
< b
; i
++)
691 ret
= ctx
->cipher
->do_cipher(ctx
, out
, ctx
->buf
, b
);
699 int EVP_DecryptUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
700 const unsigned char *in
, int inl
)
702 int fix_len
, cmpl
= inl
, ret
;
710 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
714 /* Prevent accidental use of encryption context when decrypting */
716 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
720 if (ctx
->cipher
== NULL
) {
721 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
724 if (ctx
->cipher
->prov
== NULL
)
727 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
729 if (ctx
->cipher
->cupdate
== NULL
|| blocksize
< 1) {
730 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
733 ret
= ctx
->cipher
->cupdate(ctx
->provctx
, out
, &soutl
,
734 inl
+ (blocksize
== 1 ? 0 : blocksize
), in
,
738 if (soutl
> INT_MAX
) {
739 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
747 /* TODO(3.0): Remove legacy code below */
750 b
= ctx
->cipher
->block_size
;
752 if (EVP_CIPHER_CTX_test_flags(ctx
, EVP_CIPH_FLAG_LENGTH_BITS
))
753 cmpl
= (cmpl
+ 7) / 8;
755 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
756 if (b
== 1 && is_partially_overlapping(out
, in
, cmpl
)) {
757 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
761 fix_len
= ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
775 if (ctx
->flags
& EVP_CIPH_NO_PADDING
)
776 return evp_EncryptDecryptUpdate(ctx
, out
, outl
, in
, inl
);
778 OPENSSL_assert(b
<= sizeof(ctx
->final
));
780 if (ctx
->final_used
) {
781 /* see comment about PTRDIFF_T comparison above */
782 if (((PTRDIFF_T
)out
== (PTRDIFF_T
)in
)
783 || is_partially_overlapping(out
, in
, b
)) {
784 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
788 * final_used is only ever set if buf_len is 0. Therefore the maximum
789 * length output we will ever see from evp_EncryptDecryptUpdate is
790 * the maximum multiple of the block length that is <= inl, or just:
792 * Since final_used has been set then the final output length is:
793 * (inl & ~(b - 1)) + b
794 * This must never exceed INT_MAX
796 if ((inl
& ~(b
- 1)) > INT_MAX
- b
) {
797 ERR_raise(ERR_LIB_EVP
, EVP_R_OUTPUT_WOULD_OVERFLOW
);
800 memcpy(out
, ctx
->final
, b
);
806 if (!evp_EncryptDecryptUpdate(ctx
, out
, outl
, in
, inl
))
810 * if we have 'decrypted' a multiple of block size, make sure we have a
811 * copy of this last block
813 if (b
> 1 && !ctx
->buf_len
) {
816 memcpy(ctx
->final
, &out
[*outl
], b
);
826 int EVP_DecryptFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
829 ret
= EVP_DecryptFinal_ex(ctx
, out
, outl
);
833 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
844 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
848 /* Prevent accidental use of encryption context when decrypting */
850 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
854 if (ctx
->cipher
== NULL
) {
855 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
859 if (ctx
->cipher
->prov
== NULL
)
862 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
864 if (blocksize
< 1 || ctx
->cipher
->cfinal
== NULL
) {
865 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
869 ret
= ctx
->cipher
->cfinal(ctx
->provctx
, out
, &soutl
,
870 blocksize
== 1 ? 0 : blocksize
);
873 if (soutl
> INT_MAX
) {
874 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
882 /* TODO(3.0): Remove legacy code below */
886 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
887 i
= ctx
->cipher
->do_cipher(ctx
, out
, NULL
, 0);
895 b
= ctx
->cipher
->block_size
;
896 if (ctx
->flags
& EVP_CIPH_NO_PADDING
) {
898 ERR_raise(ERR_LIB_EVP
, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH
);
905 if (ctx
->buf_len
|| !ctx
->final_used
) {
906 ERR_raise(ERR_LIB_EVP
, EVP_R_WRONG_FINAL_BLOCK_LENGTH
);
909 OPENSSL_assert(b
<= sizeof(ctx
->final
));
912 * The following assumes that the ciphertext has been authenticated.
913 * Otherwise it provides a padding oracle.
915 n
= ctx
->final
[b
- 1];
916 if (n
== 0 || n
> (int)b
) {
917 ERR_raise(ERR_LIB_EVP
, EVP_R_BAD_DECRYPT
);
920 for (i
= 0; i
< n
; i
++) {
921 if (ctx
->final
[--b
] != n
) {
922 ERR_raise(ERR_LIB_EVP
, EVP_R_BAD_DECRYPT
);
926 n
= ctx
->cipher
->block_size
- n
;
927 for (i
= 0; i
< n
; i
++)
928 out
[i
] = ctx
->final
[i
];
935 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX
*c
, int keylen
)
937 if (c
->cipher
->prov
!= NULL
) {
939 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
942 if (EVP_CIPHER_CTX_key_length(c
) == keylen
)
945 /* Check the cipher actually understands this parameter */
946 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c
->cipher
),
947 OSSL_CIPHER_PARAM_KEYLEN
) == NULL
)
950 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &len
);
951 ok
= evp_do_ciph_ctx_setparams(c
->cipher
, c
->provctx
, params
);
953 return ok
> 0 ? 1 : 0;
956 /* TODO(3.0) legacy code follows */
959 * Note there have never been any built-in ciphers that define this flag
960 * since it was first introduced.
962 if (c
->cipher
->flags
& EVP_CIPH_CUSTOM_KEY_LENGTH
)
963 return EVP_CIPHER_CTX_ctrl(c
, EVP_CTRL_SET_KEY_LENGTH
, keylen
, NULL
);
964 if (EVP_CIPHER_CTX_key_length(c
) == keylen
)
966 if ((keylen
> 0) && (c
->cipher
->flags
& EVP_CIPH_VARIABLE_LENGTH
)) {
970 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY_LENGTH
);
974 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX
*ctx
, int pad
)
977 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
978 unsigned int pd
= pad
;
981 ctx
->flags
&= ~EVP_CIPH_NO_PADDING
;
983 ctx
->flags
|= EVP_CIPH_NO_PADDING
;
985 if (ctx
->cipher
!= NULL
&& ctx
->cipher
->prov
== NULL
)
987 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING
, &pd
);
988 ok
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
993 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
, void *ptr
)
995 int ret
= EVP_CTRL_RET_UNSUPPORTED
;
999 OSSL_PARAM params
[4] = {
1000 OSSL_PARAM_END
, OSSL_PARAM_END
, OSSL_PARAM_END
, OSSL_PARAM_END
1003 if (ctx
== NULL
|| ctx
->cipher
== NULL
) {
1004 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
1008 if (ctx
->cipher
->prov
== NULL
)
1012 case EVP_CTRL_SET_KEY_LENGTH
:
1013 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &sz
);
1015 case EVP_CTRL_RAND_KEY
: /* Used by DES */
1018 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY
,
1024 * TODO(3.0) EVP_CTRL_INIT is purely legacy, no provider counterpart
1025 * As a matter of fact, this should be dead code, but some caller
1026 * might still do a direct control call with this command, so...
1027 * Legacy methods return 1 except for exceptional circumstances, so
1028 * we do the same here to not be disruptive.
1031 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS
: /* Used by DASYNC */
1034 case EVP_CTRL_AEAD_SET_IVLEN
:
1037 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN
, &sz
);
1039 case EVP_CTRL_AEAD_SET_IV_FIXED
:
1040 params
[0] = OSSL_PARAM_construct_octet_string(
1041 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED
, ptr
, sz
);
1043 case EVP_CTRL_GCM_IV_GEN
:
1046 sz
= 0; /* special case that uses the iv length */
1047 params
[0] = OSSL_PARAM_construct_octet_string(
1048 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN
, ptr
, sz
);
1050 case EVP_CTRL_GCM_SET_IV_INV
:
1053 params
[0] = OSSL_PARAM_construct_octet_string(
1054 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV
, ptr
, sz
);
1056 case EVP_CTRL_GET_RC5_ROUNDS
:
1057 set_params
= 0; /* Fall thru */
1058 case EVP_CTRL_SET_RC5_ROUNDS
:
1061 i
= (unsigned int)arg
;
1062 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS
, &i
);
1064 case EVP_CTRL_SET_SPEED
:
1067 i
= (unsigned int)arg
;
1068 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED
, &i
);
1070 case EVP_CTRL_AEAD_GET_TAG
:
1071 set_params
= 0; /* Fall thru */
1072 case EVP_CTRL_AEAD_SET_TAG
:
1073 params
[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG
,
1076 case EVP_CTRL_AEAD_TLS1_AAD
:
1077 /* This one does a set and a get - since it returns a size */
1079 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD
,
1081 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1085 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD
, &sz
);
1086 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1090 #ifndef OPENSSL_NO_RC2
1091 case EVP_CTRL_GET_RC2_KEY_BITS
:
1092 set_params
= 0; /* Fall thru */
1093 case EVP_CTRL_SET_RC2_KEY_BITS
:
1094 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS
, &sz
);
1096 #endif /* OPENSSL_NO_RC2 */
1097 #if !defined(OPENSSL_NO_MULTIBLOCK)
1098 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE
:
1099 params
[0] = OSSL_PARAM_construct_size_t(
1100 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT
, &sz
);
1101 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1105 params
[0] = OSSL_PARAM_construct_size_t(
1106 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE
, &sz
);
1107 params
[1] = OSSL_PARAM_construct_end();
1108 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1112 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD
: {
1113 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*p
=
1114 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*)ptr
;
1116 if (arg
< (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
))
1119 params
[0] = OSSL_PARAM_construct_octet_string(
1120 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD
, (void*)p
->inp
, p
->len
);
1121 params
[1] = OSSL_PARAM_construct_uint(
1122 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE
, &p
->interleave
);
1123 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1126 /* Retrieve the return values changed by the set */
1127 params
[0] = OSSL_PARAM_construct_size_t(
1128 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN
, &sz
);
1129 params
[1] = OSSL_PARAM_construct_uint(
1130 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE
, &p
->interleave
);
1131 params
[2] = OSSL_PARAM_construct_end();
1132 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1137 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT
: {
1138 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*p
=
1139 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*)ptr
;
1141 params
[0] = OSSL_PARAM_construct_octet_string(
1142 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC
, p
->out
, p
->len
);
1144 params
[1] = OSSL_PARAM_construct_octet_string(
1145 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN
, (void*)p
->inp
,
1147 params
[2] = OSSL_PARAM_construct_uint(
1148 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE
, &p
->interleave
);
1149 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1152 params
[0] = OSSL_PARAM_construct_size_t(
1153 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN
, &sz
);
1154 params
[1] = OSSL_PARAM_construct_end();
1155 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1160 #endif /* OPENSSL_NO_MULTIBLOCK */
1161 case EVP_CTRL_AEAD_SET_MAC_KEY
:
1164 params
[0] = OSSL_PARAM_construct_octet_string(
1165 OSSL_CIPHER_PARAM_AEAD_MAC_KEY
, ptr
, sz
);
1170 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1172 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1175 /* TODO(3.0): Remove legacy code below */
1177 if (ctx
->cipher
->ctrl
== NULL
) {
1178 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_NOT_IMPLEMENTED
);
1182 ret
= ctx
->cipher
->ctrl(ctx
, type
, arg
, ptr
);
1185 if (ret
== EVP_CTRL_RET_UNSUPPORTED
) {
1186 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED
);
1192 int EVP_CIPHER_get_params(EVP_CIPHER
*cipher
, OSSL_PARAM params
[])
1194 if (cipher
!= NULL
&& cipher
->get_params
!= NULL
)
1195 return cipher
->get_params(params
);
1199 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX
*ctx
, const OSSL_PARAM params
[])
1201 if (ctx
->cipher
!= NULL
&& ctx
->cipher
->set_ctx_params
!= NULL
)
1202 return ctx
->cipher
->set_ctx_params(ctx
->provctx
, params
);
1206 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX
*ctx
, OSSL_PARAM params
[])
1208 if (ctx
->cipher
!= NULL
&& ctx
->cipher
->get_ctx_params
!= NULL
)
1209 return ctx
->cipher
->get_ctx_params(ctx
->provctx
, params
);
1213 const OSSL_PARAM
*EVP_CIPHER_gettable_params(const EVP_CIPHER
*cipher
)
1215 if (cipher
!= NULL
&& cipher
->gettable_params
!= NULL
)
1216 return cipher
->gettable_params(
1217 ossl_provider_ctx(EVP_CIPHER_provider(cipher
)));
1221 const OSSL_PARAM
*EVP_CIPHER_settable_ctx_params(const EVP_CIPHER
*cipher
)
1223 if (cipher
!= NULL
&& cipher
->settable_ctx_params
!= NULL
)
1224 return cipher
->settable_ctx_params(
1225 ossl_provider_ctx(EVP_CIPHER_provider(cipher
)));
1229 const OSSL_PARAM
*EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER
*cipher
)
1231 if (cipher
!= NULL
&& cipher
->gettable_ctx_params
!= NULL
)
1232 return cipher
->gettable_ctx_params(
1233 ossl_provider_ctx(EVP_CIPHER_provider(cipher
)));
1238 static OSSL_LIB_CTX
*EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX
*ctx
)
1240 const EVP_CIPHER
*cipher
= ctx
->cipher
;
1241 const OSSL_PROVIDER
*prov
;
1246 prov
= EVP_CIPHER_provider(cipher
);
1247 return ossl_provider_libctx(prov
);
1251 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX
*ctx
, unsigned char *key
)
1253 if (ctx
->cipher
->flags
& EVP_CIPH_RAND_KEY
)
1254 return EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_RAND_KEY
, 0, key
);
1261 OSSL_LIB_CTX
*libctx
= EVP_CIPHER_CTX_get_libctx(ctx
);
1263 kl
= EVP_CIPHER_CTX_key_length(ctx
);
1264 if (kl
<= 0 || RAND_priv_bytes_ex(libctx
, key
, kl
) <= 0)
1268 #endif /* FIPS_MODULE */
1271 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX
*out
, const EVP_CIPHER_CTX
*in
)
1273 if ((in
== NULL
) || (in
->cipher
== NULL
)) {
1274 ERR_raise(ERR_LIB_EVP
, EVP_R_INPUT_NOT_INITIALIZED
);
1278 if (in
->cipher
->prov
== NULL
)
1281 if (in
->cipher
->dupctx
== NULL
) {
1282 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
1286 EVP_CIPHER_CTX_reset(out
);
1289 out
->provctx
= NULL
;
1291 if (in
->fetched_cipher
!= NULL
&& !EVP_CIPHER_up_ref(in
->fetched_cipher
)) {
1292 out
->fetched_cipher
= NULL
;
1296 out
->provctx
= in
->cipher
->dupctx(in
->provctx
);
1297 if (out
->provctx
== NULL
) {
1298 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
1304 /* TODO(3.0): Remove legacy code below */
1307 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1308 /* Make sure it's safe to copy a cipher context using an ENGINE */
1309 if (in
->engine
&& !ENGINE_init(in
->engine
)) {
1310 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
1315 EVP_CIPHER_CTX_reset(out
);
1316 memcpy(out
, in
, sizeof(*out
));
1318 if (in
->cipher_data
&& in
->cipher
->ctx_size
) {
1319 out
->cipher_data
= OPENSSL_malloc(in
->cipher
->ctx_size
);
1320 if (out
->cipher_data
== NULL
) {
1322 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1325 memcpy(out
->cipher_data
, in
->cipher_data
, in
->cipher
->ctx_size
);
1328 if (in
->cipher
->flags
& EVP_CIPH_CUSTOM_COPY
)
1329 if (!in
->cipher
->ctrl((EVP_CIPHER_CTX
*)in
, EVP_CTRL_COPY
, 0, out
)) {
1331 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
1337 EVP_CIPHER
*evp_cipher_new(void)
1339 EVP_CIPHER
*cipher
= OPENSSL_zalloc(sizeof(EVP_CIPHER
));
1341 if (cipher
!= NULL
) {
1342 cipher
->lock
= CRYPTO_THREAD_lock_new();
1343 if (cipher
->lock
== NULL
) {
1344 OPENSSL_free(cipher
);
1353 * FIPS module note: since internal fetches will be entirely
1354 * provider based, we know that none of its code depends on legacy
1355 * NIDs or any functionality that use them.
1358 /* TODO(3.x) get rid of the need for legacy NIDs */
1359 static void set_legacy_nid(const char *name
, void *vlegacy_nid
)
1362 int *legacy_nid
= vlegacy_nid
;
1364 * We use lowest level function to get the associated method, because
1365 * higher level functions such as EVP_get_cipherbyname() have changed
1366 * to look at providers too.
1368 const void *legacy_method
= OBJ_NAME_get(name
, OBJ_NAME_TYPE_CIPHER_METH
);
1370 if (*legacy_nid
== -1) /* We found a clash already */
1372 if (legacy_method
== NULL
)
1374 nid
= EVP_CIPHER_nid(legacy_method
);
1375 if (*legacy_nid
!= NID_undef
&& *legacy_nid
!= nid
) {
1383 static void *evp_cipher_from_dispatch(const int name_id
,
1384 const OSSL_DISPATCH
*fns
,
1385 OSSL_PROVIDER
*prov
)
1387 EVP_CIPHER
*cipher
= NULL
;
1388 int fnciphcnt
= 0, fnctxcnt
= 0;
1390 if ((cipher
= evp_cipher_new()) == NULL
) {
1391 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1396 /* TODO(3.x) get rid of the need for legacy NIDs */
1397 cipher
->nid
= NID_undef
;
1398 if (!evp_names_do_all(prov
, name_id
, set_legacy_nid
, &cipher
->nid
)
1399 || cipher
->nid
== -1) {
1400 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1401 EVP_CIPHER_free(cipher
);
1406 cipher
->name_id
= name_id
;
1408 for (; fns
->function_id
!= 0; fns
++) {
1409 switch (fns
->function_id
) {
1410 case OSSL_FUNC_CIPHER_NEWCTX
:
1411 if (cipher
->newctx
!= NULL
)
1413 cipher
->newctx
= OSSL_FUNC_cipher_newctx(fns
);
1416 case OSSL_FUNC_CIPHER_ENCRYPT_INIT
:
1417 if (cipher
->einit
!= NULL
)
1419 cipher
->einit
= OSSL_FUNC_cipher_encrypt_init(fns
);
1422 case OSSL_FUNC_CIPHER_DECRYPT_INIT
:
1423 if (cipher
->dinit
!= NULL
)
1425 cipher
->dinit
= OSSL_FUNC_cipher_decrypt_init(fns
);
1428 case OSSL_FUNC_CIPHER_UPDATE
:
1429 if (cipher
->cupdate
!= NULL
)
1431 cipher
->cupdate
= OSSL_FUNC_cipher_update(fns
);
1434 case OSSL_FUNC_CIPHER_FINAL
:
1435 if (cipher
->cfinal
!= NULL
)
1437 cipher
->cfinal
= OSSL_FUNC_cipher_final(fns
);
1440 case OSSL_FUNC_CIPHER_CIPHER
:
1441 if (cipher
->ccipher
!= NULL
)
1443 cipher
->ccipher
= OSSL_FUNC_cipher_cipher(fns
);
1445 case OSSL_FUNC_CIPHER_FREECTX
:
1446 if (cipher
->freectx
!= NULL
)
1448 cipher
->freectx
= OSSL_FUNC_cipher_freectx(fns
);
1451 case OSSL_FUNC_CIPHER_DUPCTX
:
1452 if (cipher
->dupctx
!= NULL
)
1454 cipher
->dupctx
= OSSL_FUNC_cipher_dupctx(fns
);
1456 case OSSL_FUNC_CIPHER_GET_PARAMS
:
1457 if (cipher
->get_params
!= NULL
)
1459 cipher
->get_params
= OSSL_FUNC_cipher_get_params(fns
);
1461 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS
:
1462 if (cipher
->get_ctx_params
!= NULL
)
1464 cipher
->get_ctx_params
= OSSL_FUNC_cipher_get_ctx_params(fns
);
1466 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS
:
1467 if (cipher
->set_ctx_params
!= NULL
)
1469 cipher
->set_ctx_params
= OSSL_FUNC_cipher_set_ctx_params(fns
);
1471 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS
:
1472 if (cipher
->gettable_params
!= NULL
)
1474 cipher
->gettable_params
= OSSL_FUNC_cipher_gettable_params(fns
);
1476 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS
:
1477 if (cipher
->gettable_ctx_params
!= NULL
)
1479 cipher
->gettable_ctx_params
=
1480 OSSL_FUNC_cipher_gettable_ctx_params(fns
);
1482 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS
:
1483 if (cipher
->settable_ctx_params
!= NULL
)
1485 cipher
->settable_ctx_params
=
1486 OSSL_FUNC_cipher_settable_ctx_params(fns
);
1490 if ((fnciphcnt
!= 0 && fnciphcnt
!= 3 && fnciphcnt
!= 4)
1491 || (fnciphcnt
== 0 && cipher
->ccipher
== NULL
)
1494 * In order to be a consistent set of functions we must have at least
1495 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1496 * functions, or a single "cipher" function. In all cases we need both
1497 * the "newctx" and "freectx" functions.
1499 EVP_CIPHER_free(cipher
);
1500 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
1503 cipher
->prov
= prov
;
1505 ossl_provider_up_ref(prov
);
1507 if (!evp_cipher_cache_constants(cipher
)) {
1508 EVP_CIPHER_free(cipher
);
1509 ERR_raise(ERR_LIB_EVP
, EVP_R_CACHE_CONSTANTS_FAILED
);
1516 static int evp_cipher_up_ref(void *cipher
)
1518 return EVP_CIPHER_up_ref(cipher
);
1521 static void evp_cipher_free(void *cipher
)
1523 EVP_CIPHER_free(cipher
);
1526 EVP_CIPHER
*EVP_CIPHER_fetch(OSSL_LIB_CTX
*ctx
, const char *algorithm
,
1527 const char *properties
)
1529 EVP_CIPHER
*cipher
=
1530 evp_generic_fetch(ctx
, OSSL_OP_CIPHER
, algorithm
, properties
,
1531 evp_cipher_from_dispatch
, evp_cipher_up_ref
,
1537 int EVP_CIPHER_up_ref(EVP_CIPHER
*cipher
)
1541 CRYPTO_UP_REF(&cipher
->refcnt
, &ref
, cipher
->lock
);
1545 void EVP_CIPHER_free(EVP_CIPHER
*cipher
)
1552 CRYPTO_DOWN_REF(&cipher
->refcnt
, &i
, cipher
->lock
);
1555 ossl_provider_free(cipher
->prov
);
1556 CRYPTO_THREAD_lock_free(cipher
->lock
);
1557 OPENSSL_free(cipher
);
1560 void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX
*libctx
,
1561 void (*fn
)(EVP_CIPHER
*mac
, void *arg
),
1564 evp_generic_do_all(libctx
, OSSL_OP_CIPHER
,
1565 (void (*)(void *, void *))fn
, arg
,
1566 evp_cipher_from_dispatch
, evp_cipher_free
);