2 * Copyright 1995-2020 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
15 #include "internal/cryptlib.h"
16 #include <openssl/evp.h>
17 #include <openssl/err.h>
18 #include <openssl/rand.h>
19 #include <openssl/engine.h>
20 #include <openssl/params.h>
21 #include <openssl/core_names.h>
22 #include "crypto/evp.h"
23 #include "internal/provider.h"
24 #include "evp_local.h"
26 int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX
*ctx
)
31 if (ctx
->cipher
== NULL
|| ctx
->cipher
->prov
== NULL
)
34 if (ctx
->provctx
!= NULL
) {
35 if (ctx
->cipher
->freectx
!= NULL
)
36 ctx
->cipher
->freectx(ctx
->provctx
);
39 if (ctx
->fetched_cipher
!= NULL
)
40 EVP_CIPHER_free(ctx
->fetched_cipher
);
41 memset(ctx
, 0, sizeof(*ctx
));
45 /* TODO(3.0): Remove legacy code below */
48 if (ctx
->cipher
!= NULL
) {
49 if (ctx
->cipher
->cleanup
&& !ctx
->cipher
->cleanup(ctx
))
51 /* Cleanse cipher context data */
52 if (ctx
->cipher_data
&& ctx
->cipher
->ctx_size
)
53 OPENSSL_cleanse(ctx
->cipher_data
, ctx
->cipher
->ctx_size
);
55 OPENSSL_free(ctx
->cipher_data
);
56 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
57 ENGINE_finish(ctx
->engine
);
59 memset(ctx
, 0, sizeof(*ctx
));
63 EVP_CIPHER_CTX
*EVP_CIPHER_CTX_new(void)
65 return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX
));
68 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX
*ctx
)
70 EVP_CIPHER_CTX_reset(ctx
);
74 int EVP_CipherInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
75 const unsigned char *key
, const unsigned char *iv
, int enc
)
78 EVP_CIPHER_CTX_reset(ctx
);
79 return EVP_CipherInit_ex(ctx
, cipher
, NULL
, key
, iv
, enc
);
82 int EVP_CipherInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
83 ENGINE
*impl
, const unsigned char *key
,
84 const unsigned char *iv
, int enc
)
86 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
87 ENGINE
*tmpimpl
= NULL
;
90 * enc == 1 means we are encrypting.
91 * enc == 0 means we are decrypting.
92 * enc == -1 means, use the previously initialised value for encrypt/decrypt
102 if (cipher
== NULL
&& ctx
->cipher
== NULL
) {
103 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
107 /* TODO(3.0): Legacy work around code below. Remove this */
109 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
111 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
112 * this context may already have an ENGINE! Try to avoid releasing the
113 * previous handle, re-querying for an ENGINE, and having a
114 * reinitialisation, when it may all be unnecessary.
116 if (ctx
->engine
&& ctx
->cipher
117 && (cipher
== NULL
|| cipher
->nid
== ctx
->cipher
->nid
))
120 if (cipher
!= NULL
&& impl
== NULL
) {
121 /* Ask if an ENGINE is reserved for this job */
122 tmpimpl
= ENGINE_get_cipher_engine(cipher
->nid
);
127 * If there are engines involved then we should use legacy handling for now.
129 if (ctx
->engine
!= NULL
130 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
134 if (ctx
->cipher
== ctx
->fetched_cipher
)
136 EVP_CIPHER_free(ctx
->fetched_cipher
);
137 ctx
->fetched_cipher
= NULL
;
141 * Ensure a context left lying around from last time is cleared
144 if (cipher
!= NULL
&& ctx
->cipher
!= NULL
) {
145 OPENSSL_clear_free(ctx
->cipher_data
, ctx
->cipher
->ctx_size
);
146 ctx
->cipher_data
= NULL
;
150 /* TODO(3.0): Start of non-legacy code below */
152 /* Ensure a context left lying around from last time is cleared */
153 if (cipher
!= NULL
&& ctx
->cipher
!= NULL
) {
154 unsigned long flags
= ctx
->flags
;
156 EVP_CIPHER_CTX_reset(ctx
);
157 /* Restore encrypt and flags */
163 cipher
= ctx
->cipher
;
165 if (cipher
->prov
== NULL
) {
167 /* We only do explicit fetches inside the FIPS module */
168 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
171 EVP_CIPHER
*provciph
=
172 EVP_CIPHER_fetch(NULL
,
173 cipher
->nid
== NID_undef
? "NULL"
174 : OBJ_nid2sn(cipher
->nid
),
177 if (provciph
== NULL
)
180 EVP_CIPHER_free(ctx
->fetched_cipher
);
181 ctx
->fetched_cipher
= provciph
;
185 ctx
->cipher
= cipher
;
186 if (ctx
->provctx
== NULL
) {
187 ctx
->provctx
= ctx
->cipher
->newctx(ossl_provider_ctx(cipher
->prov
));
188 if (ctx
->provctx
== NULL
) {
189 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
194 if ((ctx
->flags
& EVP_CIPH_NO_PADDING
) != 0) {
196 * If this ctx was already set up for no padding then we need to tell
197 * the new cipher about it.
199 if (!EVP_CIPHER_CTX_set_padding(ctx
, 0))
204 if (ctx
->cipher
->einit
== NULL
) {
205 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
209 return ctx
->cipher
->einit(ctx
->provctx
,
212 : EVP_CIPHER_CTX_key_length(ctx
),
215 : EVP_CIPHER_CTX_iv_length(ctx
));
218 if (ctx
->cipher
->dinit
== NULL
) {
219 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
223 return ctx
->cipher
->dinit(ctx
->provctx
,
226 : EVP_CIPHER_CTX_key_length(ctx
),
229 : EVP_CIPHER_CTX_iv_length(ctx
));
231 /* TODO(3.0): Remove legacy code below */
234 if (cipher
!= NULL
) {
236 * Ensure a context left lying around from last time is cleared (we
237 * previously attempted to avoid this if the same ENGINE and
238 * EVP_CIPHER could be used).
241 unsigned long flags
= ctx
->flags
;
242 EVP_CIPHER_CTX_reset(ctx
);
243 /* Restore encrypt and flags */
247 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
249 if (!ENGINE_init(impl
)) {
250 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
257 /* There's an ENGINE for this job ... (apparently) */
258 const EVP_CIPHER
*c
= ENGINE_get_cipher(impl
, cipher
->nid
);
262 * One positive side-effect of US's export control history,
263 * is that we should at least be able to avoid using US
264 * misspellings of "initialisation"?
266 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
269 /* We'll use the ENGINE's private cipher definition */
272 * Store the ENGINE functional reference so we know 'cipher' came
273 * from an ENGINE and we need to release it when done.
281 ctx
->cipher
= cipher
;
282 if (ctx
->cipher
->ctx_size
) {
283 ctx
->cipher_data
= OPENSSL_zalloc(ctx
->cipher
->ctx_size
);
284 if (ctx
->cipher_data
== NULL
) {
286 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
290 ctx
->cipher_data
= NULL
;
292 ctx
->key_len
= cipher
->key_len
;
293 /* Preserve wrap enable flag, zero everything else */
294 ctx
->flags
&= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
;
295 if (ctx
->cipher
->flags
& EVP_CIPH_CTRL_INIT
) {
296 if (!EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_INIT
, 0, NULL
)) {
298 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
303 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
306 if (ctx
->cipher
== NULL
)
309 /* we assume block size is a power of 2 in *cryptUpdate */
310 OPENSSL_assert(ctx
->cipher
->block_size
== 1
311 || ctx
->cipher
->block_size
== 8
312 || ctx
->cipher
->block_size
== 16);
314 if (!(ctx
->flags
& EVP_CIPHER_CTX_FLAG_WRAP_ALLOW
)
315 && EVP_CIPHER_CTX_mode(ctx
) == EVP_CIPH_WRAP_MODE
) {
316 ERR_raise(ERR_LIB_EVP
, EVP_R_WRAP_MODE_NOT_ALLOWED
);
320 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx
)) & EVP_CIPH_CUSTOM_IV
)) {
321 switch (EVP_CIPHER_CTX_mode(ctx
)) {
323 case EVP_CIPH_STREAM_CIPHER
:
324 case EVP_CIPH_ECB_MODE
:
327 case EVP_CIPH_CFB_MODE
:
328 case EVP_CIPH_OFB_MODE
:
333 case EVP_CIPH_CBC_MODE
:
335 OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx
) <=
336 (int)sizeof(ctx
->iv
));
338 memcpy(ctx
->oiv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
339 memcpy(ctx
->iv
, ctx
->oiv
, EVP_CIPHER_CTX_iv_length(ctx
));
342 case EVP_CIPH_CTR_MODE
:
344 /* Don't reuse IV for CTR mode */
346 memcpy(ctx
->iv
, iv
, EVP_CIPHER_CTX_iv_length(ctx
));
354 if (key
|| (ctx
->cipher
->flags
& EVP_CIPH_ALWAYS_CALL_INIT
)) {
355 if (!ctx
->cipher
->init(ctx
, key
, iv
, enc
))
360 ctx
->block_mask
= ctx
->cipher
->block_size
- 1;
364 int EVP_CipherUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
365 const unsigned char *in
, int inl
)
368 return EVP_EncryptUpdate(ctx
, out
, outl
, in
, inl
);
370 return EVP_DecryptUpdate(ctx
, out
, outl
, in
, inl
);
373 int EVP_CipherFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
376 return EVP_EncryptFinal_ex(ctx
, out
, outl
);
378 return EVP_DecryptFinal_ex(ctx
, out
, outl
);
381 int EVP_CipherFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
384 return EVP_EncryptFinal(ctx
, out
, outl
);
386 return EVP_DecryptFinal(ctx
, out
, outl
);
389 int EVP_EncryptInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
390 const unsigned char *key
, const unsigned char *iv
)
392 return EVP_CipherInit(ctx
, cipher
, key
, iv
, 1);
395 int EVP_EncryptInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
396 ENGINE
*impl
, const unsigned char *key
,
397 const unsigned char *iv
)
399 return EVP_CipherInit_ex(ctx
, cipher
, impl
, key
, iv
, 1);
402 int EVP_DecryptInit(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
403 const unsigned char *key
, const unsigned char *iv
)
405 return EVP_CipherInit(ctx
, cipher
, key
, iv
, 0);
408 int EVP_DecryptInit_ex(EVP_CIPHER_CTX
*ctx
, const EVP_CIPHER
*cipher
,
409 ENGINE
*impl
, const unsigned char *key
,
410 const unsigned char *iv
)
412 return EVP_CipherInit_ex(ctx
, cipher
, impl
, key
, iv
, 0);
416 * According to the letter of standard difference between pointers
417 * is specified to be valid only within same object. This makes
418 * it formally challenging to determine if input and output buffers
419 * are not partially overlapping with standard pointer arithmetic.
424 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
426 * Then we have VMS that distinguishes itself by adhering to
427 * sizeof(size_t)==4 even in 64-bit builds, which means that
428 * difference between two pointers might be truncated to 32 bits.
429 * In the context one can even wonder how comparison for
430 * equality is implemented. To be on the safe side we adhere to
431 * PTRDIFF_T even for comparison for equality.
433 # define PTRDIFF_T uint64_t
435 # define PTRDIFF_T size_t
438 int is_partially_overlapping(const void *ptr1
, const void *ptr2
, int len
)
440 PTRDIFF_T diff
= (PTRDIFF_T
)ptr1
-(PTRDIFF_T
)ptr2
;
442 * Check for partially overlapping buffers. [Binary logical
443 * operations are used instead of boolean to minimize number
444 * of conditional branches.]
446 int overlapped
= (len
> 0) & (diff
!= 0) & ((diff
< (PTRDIFF_T
)len
) |
447 (diff
> (0 - (PTRDIFF_T
)len
)));
452 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX
*ctx
,
453 unsigned char *out
, int *outl
,
454 const unsigned char *in
, int inl
)
456 int i
, j
, bl
, cmpl
= inl
;
458 if (EVP_CIPHER_CTX_test_flags(ctx
, EVP_CIPH_FLAG_LENGTH_BITS
))
459 cmpl
= (cmpl
+ 7) / 8;
461 bl
= ctx
->cipher
->block_size
;
463 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
464 /* If block size > 1 then the cipher will have to do this check */
465 if (bl
== 1 && is_partially_overlapping(out
, in
, cmpl
)) {
466 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
470 i
= ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
482 if (is_partially_overlapping(out
+ ctx
->buf_len
, in
, cmpl
)) {
483 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
487 if (ctx
->buf_len
== 0 && (inl
& (ctx
->block_mask
)) == 0) {
488 if (ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
)) {
497 OPENSSL_assert(bl
<= (int)sizeof(ctx
->buf
));
500 memcpy(&(ctx
->buf
[i
]), in
, inl
);
506 memcpy(&(ctx
->buf
[i
]), in
, j
);
509 if (!ctx
->cipher
->do_cipher(ctx
, out
, ctx
->buf
, bl
))
519 if (!ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
))
525 memcpy(ctx
->buf
, &(in
[inl
]), i
);
531 int EVP_EncryptUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
532 const unsigned char *in
, int inl
)
541 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
545 /* Prevent accidental use of decryption context when encrypting */
547 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
551 if (ctx
->cipher
== NULL
) {
552 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
556 if (ctx
->cipher
->prov
== NULL
)
559 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
561 if (ctx
->cipher
->cupdate
== NULL
|| blocksize
< 1) {
562 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
565 ret
= ctx
->cipher
->cupdate(ctx
->provctx
, out
, &soutl
,
566 inl
+ (blocksize
== 1 ? 0 : blocksize
), in
,
570 if (soutl
> INT_MAX
) {
571 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
579 /* TODO(3.0): Remove legacy code below */
582 return evp_EncryptDecryptUpdate(ctx
, out
, outl
, in
, inl
);
585 int EVP_EncryptFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
588 ret
= EVP_EncryptFinal_ex(ctx
, out
, outl
);
592 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
595 unsigned int i
, b
, bl
;
602 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
606 /* Prevent accidental use of decryption context when encrypting */
608 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
612 if (ctx
->cipher
== NULL
) {
613 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
616 if (ctx
->cipher
->prov
== NULL
)
619 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
621 if (blocksize
< 1 || ctx
->cipher
->cfinal
== NULL
) {
622 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
626 ret
= ctx
->cipher
->cfinal(ctx
->provctx
, out
, &soutl
,
627 blocksize
== 1 ? 0 : blocksize
);
630 if (soutl
> INT_MAX
) {
631 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
639 /* TODO(3.0): Remove legacy code below */
642 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
643 ret
= ctx
->cipher
->do_cipher(ctx
, out
, NULL
, 0);
651 b
= ctx
->cipher
->block_size
;
652 OPENSSL_assert(b
<= sizeof(ctx
->buf
));
658 if (ctx
->flags
& EVP_CIPH_NO_PADDING
) {
660 ERR_raise(ERR_LIB_EVP
, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH
);
668 for (i
= bl
; i
< b
; i
++)
670 ret
= ctx
->cipher
->do_cipher(ctx
, out
, ctx
->buf
, b
);
678 int EVP_DecryptUpdate(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
,
679 const unsigned char *in
, int inl
)
681 int fix_len
, cmpl
= inl
, ret
;
689 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
693 /* Prevent accidental use of encryption context when decrypting */
695 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
699 if (ctx
->cipher
== NULL
) {
700 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
703 if (ctx
->cipher
->prov
== NULL
)
706 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
708 if (ctx
->cipher
->cupdate
== NULL
|| blocksize
< 1) {
709 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
712 ret
= ctx
->cipher
->cupdate(ctx
->provctx
, out
, &soutl
,
713 inl
+ (blocksize
== 1 ? 0 : blocksize
), in
,
717 if (soutl
> INT_MAX
) {
718 ERR_raise(ERR_LIB_EVP
, EVP_R_UPDATE_ERROR
);
726 /* TODO(3.0): Remove legacy code below */
729 b
= ctx
->cipher
->block_size
;
731 if (EVP_CIPHER_CTX_test_flags(ctx
, EVP_CIPH_FLAG_LENGTH_BITS
))
732 cmpl
= (cmpl
+ 7) / 8;
734 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
735 if (b
== 1 && is_partially_overlapping(out
, in
, cmpl
)) {
736 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
740 fix_len
= ctx
->cipher
->do_cipher(ctx
, out
, in
, inl
);
754 if (ctx
->flags
& EVP_CIPH_NO_PADDING
)
755 return evp_EncryptDecryptUpdate(ctx
, out
, outl
, in
, inl
);
757 OPENSSL_assert(b
<= sizeof(ctx
->final
));
759 if (ctx
->final_used
) {
760 /* see comment about PTRDIFF_T comparison above */
761 if (((PTRDIFF_T
)out
== (PTRDIFF_T
)in
)
762 || is_partially_overlapping(out
, in
, b
)) {
763 ERR_raise(ERR_LIB_EVP
, EVP_R_PARTIALLY_OVERLAPPING
);
766 memcpy(out
, ctx
->final
, b
);
772 if (!evp_EncryptDecryptUpdate(ctx
, out
, outl
, in
, inl
))
776 * if we have 'decrypted' a multiple of block size, make sure we have a
777 * copy of this last block
779 if (b
> 1 && !ctx
->buf_len
) {
782 memcpy(ctx
->final
, &out
[*outl
], b
);
792 int EVP_DecryptFinal(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
795 ret
= EVP_DecryptFinal_ex(ctx
, out
, outl
);
799 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX
*ctx
, unsigned char *out
, int *outl
)
810 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
814 /* Prevent accidental use of encryption context when decrypting */
816 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_OPERATION
);
820 if (ctx
->cipher
== NULL
) {
821 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
825 if (ctx
->cipher
->prov
== NULL
)
828 blocksize
= EVP_CIPHER_CTX_block_size(ctx
);
830 if (blocksize
< 1 || ctx
->cipher
->cfinal
== NULL
) {
831 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
835 ret
= ctx
->cipher
->cfinal(ctx
->provctx
, out
, &soutl
,
836 blocksize
== 1 ? 0 : blocksize
);
839 if (soutl
> INT_MAX
) {
840 ERR_raise(ERR_LIB_EVP
, EVP_R_FINAL_ERROR
);
848 /* TODO(3.0): Remove legacy code below */
852 if (ctx
->cipher
->flags
& EVP_CIPH_FLAG_CUSTOM_CIPHER
) {
853 i
= ctx
->cipher
->do_cipher(ctx
, out
, NULL
, 0);
861 b
= ctx
->cipher
->block_size
;
862 if (ctx
->flags
& EVP_CIPH_NO_PADDING
) {
864 ERR_raise(ERR_LIB_EVP
, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH
);
871 if (ctx
->buf_len
|| !ctx
->final_used
) {
872 ERR_raise(ERR_LIB_EVP
, EVP_R_WRONG_FINAL_BLOCK_LENGTH
);
875 OPENSSL_assert(b
<= sizeof(ctx
->final
));
878 * The following assumes that the ciphertext has been authenticated.
879 * Otherwise it provides a padding oracle.
881 n
= ctx
->final
[b
- 1];
882 if (n
== 0 || n
> (int)b
) {
883 ERR_raise(ERR_LIB_EVP
, EVP_R_BAD_DECRYPT
);
886 for (i
= 0; i
< n
; i
++) {
887 if (ctx
->final
[--b
] != n
) {
888 ERR_raise(ERR_LIB_EVP
, EVP_R_BAD_DECRYPT
);
892 n
= ctx
->cipher
->block_size
- n
;
893 for (i
= 0; i
< n
; i
++)
894 out
[i
] = ctx
->final
[i
];
901 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX
*c
, int keylen
)
903 if (c
->cipher
->prov
!= NULL
) {
905 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
908 if (EVP_CIPHER_CTX_key_length(c
) == keylen
)
911 /* Check the cipher actually understands this parameter */
912 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c
->cipher
),
913 OSSL_CIPHER_PARAM_KEYLEN
) == NULL
)
916 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &len
);
917 ok
= evp_do_ciph_ctx_setparams(c
->cipher
, c
->provctx
, params
);
919 return ok
> 0 ? 1 : 0;
922 /* TODO(3.0) legacy code follows */
925 * Note there have never been any built-in ciphers that define this flag
926 * since it was first introduced.
928 if (c
->cipher
->flags
& EVP_CIPH_CUSTOM_KEY_LENGTH
)
929 return EVP_CIPHER_CTX_ctrl(c
, EVP_CTRL_SET_KEY_LENGTH
, keylen
, NULL
);
930 if (EVP_CIPHER_CTX_key_length(c
) == keylen
)
932 if ((keylen
> 0) && (c
->cipher
->flags
& EVP_CIPH_VARIABLE_LENGTH
)) {
936 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_KEY_LENGTH
);
940 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX
*ctx
, int pad
)
943 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
944 unsigned int pd
= pad
;
947 ctx
->flags
&= ~EVP_CIPH_NO_PADDING
;
949 ctx
->flags
|= EVP_CIPH_NO_PADDING
;
951 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING
, &pd
);
952 ok
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
957 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX
*ctx
, int type
, int arg
, void *ptr
)
959 int ret
= EVP_CTRL_RET_UNSUPPORTED
;
963 OSSL_PARAM params
[4] = {
964 OSSL_PARAM_END
, OSSL_PARAM_END
, OSSL_PARAM_END
, OSSL_PARAM_END
967 if (ctx
== NULL
|| ctx
->cipher
== NULL
) {
968 ERR_raise(ERR_LIB_EVP
, EVP_R_NO_CIPHER_SET
);
972 if (ctx
->cipher
->prov
== NULL
)
976 case EVP_CTRL_SET_KEY_LENGTH
:
977 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN
, &sz
);
979 case EVP_CTRL_RAND_KEY
: /* Used by DES */
982 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY
,
988 * TODO(3.0) EVP_CTRL_INIT is purely legacy, no provider counterpart
989 * As a matter of fact, this should be dead code, but some caller
990 * might still do a direct control call with this command, so...
991 * Legacy methods return 1 except for exceptional circumstances, so
992 * we do the same here to not be disruptive.
995 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS
: /* Used by DASYNC */
998 case EVP_CTRL_AEAD_SET_IVLEN
:
1001 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN
, &sz
);
1003 case EVP_CTRL_AEAD_SET_IV_FIXED
:
1004 params
[0] = OSSL_PARAM_construct_octet_string(
1005 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED
, ptr
, sz
);
1007 case EVP_CTRL_GCM_IV_GEN
:
1010 sz
= 0; /* special case that uses the iv length */
1011 params
[0] = OSSL_PARAM_construct_octet_string(
1012 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN
, ptr
, sz
);
1014 case EVP_CTRL_GCM_SET_IV_INV
:
1017 params
[0] = OSSL_PARAM_construct_octet_string(
1018 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV
, ptr
, sz
);
1020 case EVP_CTRL_GET_RC5_ROUNDS
:
1021 set_params
= 0; /* Fall thru */
1022 case EVP_CTRL_SET_RC5_ROUNDS
:
1025 i
= (unsigned int)arg
;
1026 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS
, &i
);
1028 case EVP_CTRL_SET_SPEED
:
1031 i
= (unsigned int)arg
;
1032 params
[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED
, &i
);
1034 case EVP_CTRL_AEAD_GET_TAG
:
1035 set_params
= 0; /* Fall thru */
1036 case EVP_CTRL_AEAD_SET_TAG
:
1037 params
[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG
,
1040 case EVP_CTRL_AEAD_TLS1_AAD
:
1041 /* This one does a set and a get - since it returns a size */
1043 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD
,
1045 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1049 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD
, &sz
);
1050 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1054 #ifndef OPENSSL_NO_RC2
1055 case EVP_CTRL_GET_RC2_KEY_BITS
:
1056 set_params
= 0; /* Fall thru */
1057 case EVP_CTRL_SET_RC2_KEY_BITS
:
1058 params
[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS
, &sz
);
1060 #endif /* OPENSSL_NO_RC2 */
1061 #if !defined(OPENSSL_NO_MULTIBLOCK)
1062 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE
:
1063 params
[0] = OSSL_PARAM_construct_size_t(
1064 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT
, &sz
);
1065 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1069 params
[0] = OSSL_PARAM_construct_size_t(
1070 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE
, &sz
);
1071 params
[1] = OSSL_PARAM_construct_end();
1072 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1076 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD
: {
1077 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*p
=
1078 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*)ptr
;
1080 if (arg
< (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
))
1083 params
[0] = OSSL_PARAM_construct_octet_string(
1084 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD
, (void*)p
->inp
, p
->len
);
1085 params
[1] = OSSL_PARAM_construct_uint(
1086 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE
, &p
->interleave
);
1087 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1090 /* Retrieve the return values changed by the set */
1091 params
[0] = OSSL_PARAM_construct_size_t(
1092 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN
, &sz
);
1093 params
[1] = OSSL_PARAM_construct_uint(
1094 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE
, &p
->interleave
);
1095 params
[2] = OSSL_PARAM_construct_end();
1096 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1101 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT
: {
1102 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*p
=
1103 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM
*)ptr
;
1105 params
[0] = OSSL_PARAM_construct_octet_string(
1106 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC
, p
->out
, p
->len
);
1108 params
[1] = OSSL_PARAM_construct_octet_string(
1109 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN
, (void*)p
->inp
,
1111 params
[2] = OSSL_PARAM_construct_uint(
1112 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE
, &p
->interleave
);
1113 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1116 params
[0] = OSSL_PARAM_construct_size_t(
1117 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN
, &sz
);
1118 params
[1] = OSSL_PARAM_construct_end();
1119 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1124 #endif /* OPENSSL_NO_MULTIBLOCK */
1125 case EVP_CTRL_AEAD_SET_MAC_KEY
:
1128 params
[0] = OSSL_PARAM_construct_octet_string(
1129 OSSL_CIPHER_PARAM_AEAD_MAC_KEY
, ptr
, sz
);
1134 ret
= evp_do_ciph_ctx_setparams(ctx
->cipher
, ctx
->provctx
, params
);
1136 ret
= evp_do_ciph_ctx_getparams(ctx
->cipher
, ctx
->provctx
, params
);
1139 /* TODO(3.0): Remove legacy code below */
1141 if (ctx
->cipher
->ctrl
== NULL
) {
1142 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_NOT_IMPLEMENTED
);
1146 ret
= ctx
->cipher
->ctrl(ctx
, type
, arg
, ptr
);
1149 if (ret
== EVP_CTRL_RET_UNSUPPORTED
) {
1150 ERR_raise(ERR_LIB_EVP
, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED
);
1156 int EVP_CIPHER_get_params(EVP_CIPHER
*cipher
, OSSL_PARAM params
[])
1158 if (cipher
!= NULL
&& cipher
->get_params
!= NULL
)
1159 return cipher
->get_params(params
);
1163 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX
*ctx
, const OSSL_PARAM params
[])
1165 if (ctx
->cipher
!= NULL
&& ctx
->cipher
->set_ctx_params
!= NULL
)
1166 return ctx
->cipher
->set_ctx_params(ctx
->provctx
, params
);
1170 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX
*ctx
, OSSL_PARAM params
[])
1172 if (ctx
->cipher
!= NULL
&& ctx
->cipher
->get_ctx_params
!= NULL
)
1173 return ctx
->cipher
->get_ctx_params(ctx
->provctx
, params
);
1177 const OSSL_PARAM
*EVP_CIPHER_gettable_params(const EVP_CIPHER
*cipher
)
1179 if (cipher
!= NULL
&& cipher
->gettable_params
!= NULL
)
1180 return cipher
->gettable_params(
1181 ossl_provider_ctx(EVP_CIPHER_provider(cipher
)));
1185 const OSSL_PARAM
*EVP_CIPHER_settable_ctx_params(const EVP_CIPHER
*cipher
)
1187 if (cipher
!= NULL
&& cipher
->settable_ctx_params
!= NULL
)
1188 return cipher
->settable_ctx_params(
1189 ossl_provider_ctx(EVP_CIPHER_provider(cipher
)));
1193 const OSSL_PARAM
*EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER
*cipher
)
1195 if (cipher
!= NULL
&& cipher
->gettable_ctx_params
!= NULL
)
1196 return cipher
->gettable_ctx_params(
1197 ossl_provider_ctx(EVP_CIPHER_provider(cipher
)));
1202 static OSSL_LIB_CTX
*EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX
*ctx
)
1204 const EVP_CIPHER
*cipher
= ctx
->cipher
;
1205 const OSSL_PROVIDER
*prov
;
1210 prov
= EVP_CIPHER_provider(cipher
);
1211 return ossl_provider_libctx(prov
);
1215 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX
*ctx
, unsigned char *key
)
1217 if (ctx
->cipher
->flags
& EVP_CIPH_RAND_KEY
)
1218 return EVP_CIPHER_CTX_ctrl(ctx
, EVP_CTRL_RAND_KEY
, 0, key
);
1225 OSSL_LIB_CTX
*libctx
= EVP_CIPHER_CTX_get_libctx(ctx
);
1227 kl
= EVP_CIPHER_CTX_key_length(ctx
);
1228 if (kl
<= 0 || RAND_priv_bytes_ex(libctx
, key
, kl
) <= 0)
1232 #endif /* FIPS_MODULE */
1235 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX
*out
, const EVP_CIPHER_CTX
*in
)
1237 if ((in
== NULL
) || (in
->cipher
== NULL
)) {
1238 ERR_raise(ERR_LIB_EVP
, EVP_R_INPUT_NOT_INITIALIZED
);
1242 if (in
->cipher
->prov
== NULL
)
1245 if (in
->cipher
->dupctx
== NULL
) {
1246 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
1250 EVP_CIPHER_CTX_reset(out
);
1253 out
->provctx
= NULL
;
1255 if (in
->fetched_cipher
!= NULL
&& !EVP_CIPHER_up_ref(in
->fetched_cipher
)) {
1256 out
->fetched_cipher
= NULL
;
1260 out
->provctx
= in
->cipher
->dupctx(in
->provctx
);
1261 if (out
->provctx
== NULL
) {
1262 ERR_raise(ERR_LIB_EVP
, EVP_R_NOT_ABLE_TO_COPY_CTX
);
1268 /* TODO(3.0): Remove legacy code below */
1271 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1272 /* Make sure it's safe to copy a cipher context using an ENGINE */
1273 if (in
->engine
&& !ENGINE_init(in
->engine
)) {
1274 ERR_raise(ERR_LIB_EVP
, ERR_R_ENGINE_LIB
);
1279 EVP_CIPHER_CTX_reset(out
);
1280 memcpy(out
, in
, sizeof(*out
));
1282 if (in
->cipher_data
&& in
->cipher
->ctx_size
) {
1283 out
->cipher_data
= OPENSSL_malloc(in
->cipher
->ctx_size
);
1284 if (out
->cipher_data
== NULL
) {
1286 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1289 memcpy(out
->cipher_data
, in
->cipher_data
, in
->cipher
->ctx_size
);
1292 if (in
->cipher
->flags
& EVP_CIPH_CUSTOM_COPY
)
1293 if (!in
->cipher
->ctrl((EVP_CIPHER_CTX
*)in
, EVP_CTRL_COPY
, 0, out
)) {
1295 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
1301 EVP_CIPHER
*evp_cipher_new(void)
1303 EVP_CIPHER
*cipher
= OPENSSL_zalloc(sizeof(EVP_CIPHER
));
1305 if (cipher
!= NULL
) {
1306 cipher
->lock
= CRYPTO_THREAD_lock_new();
1307 if (cipher
->lock
== NULL
) {
1308 OPENSSL_free(cipher
);
1317 * FIPS module note: since internal fetches will be entirely
1318 * provider based, we know that none of its code depends on legacy
1319 * NIDs or any functionality that use them.
1322 /* TODO(3.x) get rid of the need for legacy NIDs */
1323 static void set_legacy_nid(const char *name
, void *vlegacy_nid
)
1326 int *legacy_nid
= vlegacy_nid
;
1328 * We use lowest level function to get the associated method, because
1329 * higher level functions such as EVP_get_cipherbyname() have changed
1330 * to look at providers too.
1332 const void *legacy_method
= OBJ_NAME_get(name
, OBJ_NAME_TYPE_CIPHER_METH
);
1334 if (*legacy_nid
== -1) /* We found a clash already */
1336 if (legacy_method
== NULL
)
1338 nid
= EVP_CIPHER_nid(legacy_method
);
1339 if (*legacy_nid
!= NID_undef
&& *legacy_nid
!= nid
) {
1347 static void *evp_cipher_from_dispatch(const int name_id
,
1348 const OSSL_DISPATCH
*fns
,
1349 OSSL_PROVIDER
*prov
)
1351 EVP_CIPHER
*cipher
= NULL
;
1352 int fnciphcnt
= 0, fnctxcnt
= 0;
1354 if ((cipher
= evp_cipher_new()) == NULL
) {
1355 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
1360 /* TODO(3.x) get rid of the need for legacy NIDs */
1361 cipher
->nid
= NID_undef
;
1362 evp_names_do_all(prov
, name_id
, set_legacy_nid
, &cipher
->nid
);
1363 if (cipher
->nid
== -1) {
1364 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
1365 EVP_CIPHER_free(cipher
);
1370 cipher
->name_id
= name_id
;
1372 for (; fns
->function_id
!= 0; fns
++) {
1373 switch (fns
->function_id
) {
1374 case OSSL_FUNC_CIPHER_NEWCTX
:
1375 if (cipher
->newctx
!= NULL
)
1377 cipher
->newctx
= OSSL_FUNC_cipher_newctx(fns
);
1380 case OSSL_FUNC_CIPHER_ENCRYPT_INIT
:
1381 if (cipher
->einit
!= NULL
)
1383 cipher
->einit
= OSSL_FUNC_cipher_encrypt_init(fns
);
1386 case OSSL_FUNC_CIPHER_DECRYPT_INIT
:
1387 if (cipher
->dinit
!= NULL
)
1389 cipher
->dinit
= OSSL_FUNC_cipher_decrypt_init(fns
);
1392 case OSSL_FUNC_CIPHER_UPDATE
:
1393 if (cipher
->cupdate
!= NULL
)
1395 cipher
->cupdate
= OSSL_FUNC_cipher_update(fns
);
1398 case OSSL_FUNC_CIPHER_FINAL
:
1399 if (cipher
->cfinal
!= NULL
)
1401 cipher
->cfinal
= OSSL_FUNC_cipher_final(fns
);
1404 case OSSL_FUNC_CIPHER_CIPHER
:
1405 if (cipher
->ccipher
!= NULL
)
1407 cipher
->ccipher
= OSSL_FUNC_cipher_cipher(fns
);
1409 case OSSL_FUNC_CIPHER_FREECTX
:
1410 if (cipher
->freectx
!= NULL
)
1412 cipher
->freectx
= OSSL_FUNC_cipher_freectx(fns
);
1415 case OSSL_FUNC_CIPHER_DUPCTX
:
1416 if (cipher
->dupctx
!= NULL
)
1418 cipher
->dupctx
= OSSL_FUNC_cipher_dupctx(fns
);
1420 case OSSL_FUNC_CIPHER_GET_PARAMS
:
1421 if (cipher
->get_params
!= NULL
)
1423 cipher
->get_params
= OSSL_FUNC_cipher_get_params(fns
);
1425 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS
:
1426 if (cipher
->get_ctx_params
!= NULL
)
1428 cipher
->get_ctx_params
= OSSL_FUNC_cipher_get_ctx_params(fns
);
1430 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS
:
1431 if (cipher
->set_ctx_params
!= NULL
)
1433 cipher
->set_ctx_params
= OSSL_FUNC_cipher_set_ctx_params(fns
);
1435 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS
:
1436 if (cipher
->gettable_params
!= NULL
)
1438 cipher
->gettable_params
= OSSL_FUNC_cipher_gettable_params(fns
);
1440 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS
:
1441 if (cipher
->gettable_ctx_params
!= NULL
)
1443 cipher
->gettable_ctx_params
=
1444 OSSL_FUNC_cipher_gettable_ctx_params(fns
);
1446 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS
:
1447 if (cipher
->settable_ctx_params
!= NULL
)
1449 cipher
->settable_ctx_params
=
1450 OSSL_FUNC_cipher_settable_ctx_params(fns
);
1454 if ((fnciphcnt
!= 0 && fnciphcnt
!= 3 && fnciphcnt
!= 4)
1455 || (fnciphcnt
== 0 && cipher
->ccipher
== NULL
)
1458 * In order to be a consistent set of functions we must have at least
1459 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1460 * functions, or a single "cipher" function. In all cases we need both
1461 * the "newctx" and "freectx" functions.
1463 EVP_CIPHER_free(cipher
);
1464 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
1467 cipher
->prov
= prov
;
1469 ossl_provider_up_ref(prov
);
1474 static int evp_cipher_up_ref(void *cipher
)
1476 return EVP_CIPHER_up_ref(cipher
);
1479 static void evp_cipher_free(void *cipher
)
1481 EVP_CIPHER_free(cipher
);
1484 EVP_CIPHER
*EVP_CIPHER_fetch(OSSL_LIB_CTX
*ctx
, const char *algorithm
,
1485 const char *properties
)
1487 EVP_CIPHER
*cipher
=
1488 evp_generic_fetch(ctx
, OSSL_OP_CIPHER
, algorithm
, properties
,
1489 evp_cipher_from_dispatch
, evp_cipher_up_ref
,
1492 if (cipher
!= NULL
&& !evp_cipher_cache_constants(cipher
)) {
1493 EVP_CIPHER_free(cipher
);
1499 int EVP_CIPHER_up_ref(EVP_CIPHER
*cipher
)
1503 CRYPTO_UP_REF(&cipher
->refcnt
, &ref
, cipher
->lock
);
1507 void EVP_CIPHER_free(EVP_CIPHER
*cipher
)
1514 CRYPTO_DOWN_REF(&cipher
->refcnt
, &i
, cipher
->lock
);
1517 ossl_provider_free(cipher
->prov
);
1518 CRYPTO_THREAD_lock_free(cipher
->lock
);
1519 OPENSSL_free(cipher
);
1522 void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX
*libctx
,
1523 void (*fn
)(EVP_CIPHER
*mac
, void *arg
),
1526 evp_generic_do_all(libctx
, OSSL_OP_CIPHER
,
1527 (void (*)(void *, void *))fn
, arg
,
1528 evp_cipher_from_dispatch
, evp_cipher_free
);