2 * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
16 #include <openssl/evp.h>
17 #include <openssl/err.h>
18 #include <openssl/rand.h>
20 # include <openssl/engine.h>
22 #include <openssl/params.h>
23 #include <openssl/core_names.h>
24 #include "internal/cryptlib.h"
25 #include "internal/provider.h"
26 #include "internal/core.h"
27 #include "internal/safe_math.h"
28 #include "crypto/evp.h"
29 #include "evp_local.h"
31 OSSL_SAFE_MATH_SIGNED(int, int)
33 int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
38 if (ctx->cipher == NULL || ctx->cipher->prov == NULL)
41 if (ctx->algctx != NULL) {
42 if (ctx->cipher->freectx != NULL)
43 ctx->cipher->freectx(ctx->algctx);
46 if (ctx->fetched_cipher != NULL)
47 EVP_CIPHER_free(ctx->fetched_cipher);
48 memset(ctx, 0, sizeof(*ctx));
53 /* Remove legacy code below when legacy support is removed. */
56 if (ctx->cipher != NULL) {
57 if (ctx->cipher->cleanup && !ctx->cipher->cleanup(ctx))
59 /* Cleanse cipher context data */
60 if (ctx->cipher_data && ctx->cipher->ctx_size)
61 OPENSSL_cleanse(ctx->cipher_data, ctx->cipher->ctx_size);
63 OPENSSL_free(ctx->cipher_data);
64 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
65 ENGINE_finish(ctx->engine);
67 memset(ctx, 0, sizeof(*ctx));
72 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
76 ctx = OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
84 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
88 EVP_CIPHER_CTX_reset(ctx);
92 static int evp_cipher_init_internal(EVP_CIPHER_CTX *ctx,
93 const EVP_CIPHER *cipher,
94 ENGINE *impl, const unsigned char *key,
95 const unsigned char *iv, int enc,
96 const OSSL_PARAM params[])
99 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
100 ENGINE *tmpimpl = NULL;
104 * enc == 1 means we are encrypting.
105 * enc == 0 means we are decrypting.
106 * enc == -1 means, use the previously initialised value for encrypt/decrypt
116 if (cipher == NULL && ctx->cipher == NULL) {
117 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
121 /* Code below to be removed when legacy support is dropped. */
123 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
125 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
126 * this context may already have an ENGINE! Try to avoid releasing the
127 * previous handle, re-querying for an ENGINE, and having a
128 * reinitialisation, when it may all be unnecessary.
130 if (ctx->engine && ctx->cipher
131 && (cipher == NULL || cipher->nid == ctx->cipher->nid))
134 if (cipher != NULL && impl == NULL) {
135 /* Ask if an ENGINE is reserved for this job */
136 tmpimpl = ENGINE_get_cipher_engine(cipher->nid);
141 * If there are engines involved then we should use legacy handling for now.
143 if (ctx->engine != NULL
144 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
148 || (cipher != NULL && cipher->origin == EVP_ORIG_METH)
149 || (cipher == NULL && ctx->cipher != NULL
150 && ctx->cipher->origin == EVP_ORIG_METH)) {
151 if (ctx->cipher == ctx->fetched_cipher)
153 EVP_CIPHER_free(ctx->fetched_cipher);
154 ctx->fetched_cipher = NULL;
158 * Ensure a context left lying around from last time is cleared
161 if (cipher != NULL && ctx->cipher != NULL) {
162 if (ctx->cipher->cleanup != NULL && !ctx->cipher->cleanup(ctx))
164 OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size);
165 ctx->cipher_data = NULL;
168 /* Start of non-legacy code below */
170 /* Ensure a context left lying around from last time is cleared */
171 if (cipher != NULL && ctx->cipher != NULL) {
172 unsigned long flags = ctx->flags;
174 EVP_CIPHER_CTX_reset(ctx);
175 /* Restore encrypt and flags */
181 cipher = ctx->cipher;
183 if (cipher->prov == NULL) {
185 /* We only do explicit fetches inside the FIPS module */
186 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
189 EVP_CIPHER *provciph =
190 EVP_CIPHER_fetch(NULL,
191 cipher->nid == NID_undef ? "NULL"
192 : OBJ_nid2sn(cipher->nid),
195 if (provciph == NULL)
198 EVP_CIPHER_free(ctx->fetched_cipher);
199 ctx->fetched_cipher = provciph;
203 if (cipher->prov != NULL) {
204 if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) {
205 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
208 EVP_CIPHER_free(ctx->fetched_cipher);
209 /* Coverity false positive, the reference counting is confusing it */
210 /* coverity[use_after_free] */
211 ctx->fetched_cipher = (EVP_CIPHER *)cipher;
213 ctx->cipher = cipher;
214 if (ctx->algctx == NULL) {
215 ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
216 if (ctx->algctx == NULL) {
217 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
222 if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
224 * If this ctx was already set up for no padding then we need to tell
225 * the new cipher about it.
227 if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
232 if (ctx->cipher->einit == NULL) {
233 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
237 return ctx->cipher->einit(ctx->algctx,
240 : EVP_CIPHER_CTX_get_key_length(ctx),
243 : EVP_CIPHER_CTX_get_iv_length(ctx),
247 if (ctx->cipher->dinit == NULL) {
248 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
252 return ctx->cipher->dinit(ctx->algctx,
255 : EVP_CIPHER_CTX_get_key_length(ctx),
258 : EVP_CIPHER_CTX_get_iv_length(ctx),
261 /* Code below to be removed when legacy support is dropped. */
264 if (cipher != NULL) {
266 * Ensure a context left lying around from last time is cleared (we
267 * previously attempted to avoid this if the same ENGINE and
268 * EVP_CIPHER could be used).
271 unsigned long flags = ctx->flags;
272 EVP_CIPHER_CTX_reset(ctx);
273 /* Restore encrypt and flags */
277 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
279 if (!ENGINE_init(impl)) {
280 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
287 /* There's an ENGINE for this job ... (apparently) */
288 const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
292 * One positive side-effect of US's export control history,
293 * is that we should at least be able to avoid using US
294 * misspellings of "initialisation"?
296 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
299 /* We'll use the ENGINE's private cipher definition */
302 * Store the ENGINE functional reference so we know 'cipher' came
303 * from an ENGINE and we need to release it when done.
311 ctx->cipher = cipher;
312 if (ctx->cipher->ctx_size) {
313 ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
314 if (ctx->cipher_data == NULL) {
319 ctx->cipher_data = NULL;
321 ctx->key_len = cipher->key_len;
322 /* Preserve wrap enable flag, zero everything else */
323 ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
324 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
325 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL) <= 0) {
327 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
332 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
335 if (ctx->cipher == NULL)
338 /* we assume block size is a power of 2 in *cryptUpdate */
339 OPENSSL_assert(ctx->cipher->block_size == 1
340 || ctx->cipher->block_size == 8
341 || ctx->cipher->block_size == 16);
343 if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
344 && EVP_CIPHER_CTX_get_mode(ctx) == EVP_CIPH_WRAP_MODE) {
345 ERR_raise(ERR_LIB_EVP, EVP_R_WRAP_MODE_NOT_ALLOWED);
349 if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
350 & EVP_CIPH_CUSTOM_IV) == 0) {
351 switch (EVP_CIPHER_CTX_get_mode(ctx)) {
353 case EVP_CIPH_STREAM_CIPHER:
354 case EVP_CIPH_ECB_MODE:
357 case EVP_CIPH_CFB_MODE:
358 case EVP_CIPH_OFB_MODE:
363 case EVP_CIPH_CBC_MODE:
364 n = EVP_CIPHER_CTX_get_iv_length(ctx);
365 if (n < 0 || n > (int)sizeof(ctx->iv)) {
366 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
370 memcpy(ctx->oiv, iv, n);
371 memcpy(ctx->iv, ctx->oiv, n);
374 case EVP_CIPH_CTR_MODE:
376 /* Don't reuse IV for CTR mode */
378 n = EVP_CIPHER_CTX_get_iv_length(ctx);
379 if (n <= 0 || n > (int)sizeof(ctx->iv)) {
380 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
383 memcpy(ctx->iv, iv, n);
392 if (key != NULL || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
393 if (!ctx->cipher->init(ctx, key, iv, enc))
398 ctx->block_mask = ctx->cipher->block_size - 1;
402 int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
403 const unsigned char *key, const unsigned char *iv,
404 int enc, const OSSL_PARAM params[])
406 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, params);
409 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
410 const unsigned char *key, const unsigned char *iv, int enc)
413 EVP_CIPHER_CTX_reset(ctx);
414 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, NULL);
417 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
418 ENGINE *impl, const unsigned char *key,
419 const unsigned char *iv, int enc)
421 return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, NULL);
424 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
425 const unsigned char *in, int inl)
428 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
430 return EVP_DecryptUpdate(ctx, out, outl, in, inl);
433 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
436 return EVP_EncryptFinal_ex(ctx, out, outl);
438 return EVP_DecryptFinal_ex(ctx, out, outl);
441 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
444 return EVP_EncryptFinal(ctx, out, outl);
446 return EVP_DecryptFinal(ctx, out, outl);
449 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
450 const unsigned char *key, const unsigned char *iv)
452 return EVP_CipherInit(ctx, cipher, key, iv, 1);
455 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
456 ENGINE *impl, const unsigned char *key,
457 const unsigned char *iv)
459 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
462 int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
463 const unsigned char *key, const unsigned char *iv,
464 const OSSL_PARAM params[])
466 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params);
469 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
470 const unsigned char *key, const unsigned char *iv)
472 return EVP_CipherInit(ctx, cipher, key, iv, 0);
475 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
476 ENGINE *impl, const unsigned char *key,
477 const unsigned char *iv)
479 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
482 int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
483 const unsigned char *key, const unsigned char *iv,
484 const OSSL_PARAM params[])
486 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 0, params);
490 * According to the letter of standard difference between pointers
491 * is specified to be valid only within same object. This makes
492 * it formally challenging to determine if input and output buffers
493 * are not partially overlapping with standard pointer arithmetic.
498 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
500 * Then we have VMS that distinguishes itself by adhering to
501 * sizeof(size_t)==4 even in 64-bit builds, which means that
502 * difference between two pointers might be truncated to 32 bits.
503 * In the context one can even wonder how comparison for
504 * equality is implemented. To be on the safe side we adhere to
505 * PTRDIFF_T even for comparison for equality.
507 # define PTRDIFF_T uint64_t
509 # define PTRDIFF_T size_t
512 int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
514 PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
516 * Check for partially overlapping buffers. [Binary logical
517 * operations are used instead of boolean to minimize number
518 * of conditional branches.]
520 int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
521 (diff > (0 - (PTRDIFF_T)len)));
526 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
527 unsigned char *out, int *outl,
528 const unsigned char *in, int inl)
530 int i, j, bl, cmpl = inl;
532 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
533 cmpl = safe_div_round_up_int(cmpl, 8, NULL);
535 bl = ctx->cipher->block_size;
537 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
538 /* If block size > 1 then the cipher will have to do this check */
539 if (bl == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
540 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
544 i = ctx->cipher->do_cipher(ctx, out, in, inl);
556 if (ossl_is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
557 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
561 if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
562 if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
571 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
574 memcpy(&(ctx->buf[i]), in, inl);
582 * Once we've processed the first j bytes from in, the amount of
583 * data left that is a multiple of the block length is:
584 * (inl - j) & ~(bl - 1)
585 * We must ensure that this amount of data, plus the one block that
586 * we process from ctx->buf does not exceed INT_MAX
588 if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
589 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
592 memcpy(&(ctx->buf[i]), in, j);
595 if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
605 if (!ctx->cipher->do_cipher(ctx, out, in, inl))
611 memcpy(ctx->buf, &(in[inl]), i);
617 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
618 const unsigned char *in, int inl)
621 size_t soutl, inl_ = (size_t)inl;
627 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
631 /* Prevent accidental use of decryption context when encrypting */
633 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
637 if (ctx->cipher == NULL) {
638 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
642 if (ctx->cipher->prov == NULL)
645 blocksize = ctx->cipher->block_size;
647 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
648 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
652 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
653 inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
657 if (soutl > INT_MAX) {
658 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
666 /* Code below to be removed when legacy support is dropped. */
669 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
672 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
675 ret = EVP_EncryptFinal_ex(ctx, out, outl);
679 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
682 unsigned int i, b, bl;
689 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
693 /* Prevent accidental use of decryption context when encrypting */
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_get_block_size(ctx);
708 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
709 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
713 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
714 blocksize == 1 ? 0 : blocksize);
717 if (soutl > INT_MAX) {
718 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
726 /* Code below to be removed when legacy support is dropped. */
729 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
730 ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
738 b = ctx->cipher->block_size;
739 OPENSSL_assert(b <= sizeof(ctx->buf));
745 if (ctx->flags & EVP_CIPH_NO_PADDING) {
747 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
755 for (i = bl; i < b; i++)
757 ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
765 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
766 const unsigned char *in, int inl)
768 int fix_len, cmpl = inl, ret;
770 size_t soutl, inl_ = (size_t)inl;
776 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
780 /* Prevent accidental use of encryption context when decrypting */
782 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
786 if (ctx->cipher == NULL) {
787 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
790 if (ctx->cipher->prov == NULL)
793 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
795 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
796 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
799 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
800 inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
804 if (soutl > INT_MAX) {
805 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
813 /* Code below to be removed when legacy support is dropped. */
816 b = ctx->cipher->block_size;
818 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
819 cmpl = safe_div_round_up_int(cmpl, 8, NULL);
821 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
822 if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
823 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
827 fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
841 if (ctx->flags & EVP_CIPH_NO_PADDING)
842 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
844 OPENSSL_assert(b <= sizeof(ctx->final));
846 if (ctx->final_used) {
847 /* see comment about PTRDIFF_T comparison above */
848 if (((PTRDIFF_T)out == (PTRDIFF_T)in)
849 || ossl_is_partially_overlapping(out, in, b)) {
850 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
854 * final_used is only ever set if buf_len is 0. Therefore the maximum
855 * length output we will ever see from evp_EncryptDecryptUpdate is
856 * the maximum multiple of the block length that is <= inl, or just:
858 * Since final_used has been set then the final output length is:
859 * (inl & ~(b - 1)) + b
860 * This must never exceed INT_MAX
862 if ((inl & ~(b - 1)) > INT_MAX - b) {
863 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
866 memcpy(out, ctx->final, b);
872 if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
876 * if we have 'decrypted' a multiple of block size, make sure we have a
877 * copy of this last block
879 if (b > 1 && !ctx->buf_len) {
882 memcpy(ctx->final, &out[*outl], b);
892 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
895 ret = EVP_DecryptFinal_ex(ctx, out, outl);
899 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
910 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
914 /* Prevent accidental use of encryption context when decrypting */
916 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
920 if (ctx->cipher == NULL) {
921 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
925 if (ctx->cipher->prov == NULL)
928 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
930 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
931 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
935 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
936 blocksize == 1 ? 0 : blocksize);
939 if (soutl > INT_MAX) {
940 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
948 /* Code below to be removed when legacy support is dropped. */
952 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
953 i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
961 b = ctx->cipher->block_size;
962 if (ctx->flags & EVP_CIPH_NO_PADDING) {
964 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
971 if (ctx->buf_len || !ctx->final_used) {
972 ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
975 OPENSSL_assert(b <= sizeof(ctx->final));
978 * The following assumes that the ciphertext has been authenticated.
979 * Otherwise it provides a padding oracle.
981 n = ctx->final[b - 1];
982 if (n == 0 || n > (int)b) {
983 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
986 for (i = 0; i < n; i++) {
987 if (ctx->final[--b] != n) {
988 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
992 n = ctx->cipher->block_size - n;
993 for (i = 0; i < n; i++)
994 out[i] = ctx->final[i];
1001 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
1003 if (c->cipher->prov != NULL) {
1005 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1008 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
1011 /* Check the cipher actually understands this parameter */
1012 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
1013 OSSL_CIPHER_PARAM_KEYLEN) == NULL) {
1014 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1018 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
1019 if (!OSSL_PARAM_set_int(params, keylen))
1021 ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params);
1024 c->key_len = keylen;
1028 /* Code below to be removed when legacy support is dropped. */
1031 * Note there have never been any built-in ciphers that define this flag
1032 * since it was first introduced.
1034 if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
1035 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
1036 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
1038 if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
1039 c->key_len = keylen;
1042 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1046 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
1049 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1050 unsigned int pd = pad;
1053 ctx->flags &= ~EVP_CIPH_NO_PADDING;
1055 ctx->flags |= EVP_CIPH_NO_PADDING;
1057 if (ctx->cipher != NULL && ctx->cipher->prov == NULL)
1059 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
1060 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1065 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1067 int ret = EVP_CTRL_RET_UNSUPPORTED;
1071 OSSL_PARAM params[4] = {
1072 OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
1075 if (ctx == NULL || ctx->cipher == NULL) {
1076 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
1080 if (ctx->cipher->prov == NULL)
1084 case EVP_CTRL_SET_KEY_LENGTH:
1087 if (ctx->key_len == arg)
1088 /* Skip calling into provider if unchanged. */
1090 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
1093 case EVP_CTRL_RAND_KEY: /* Used by DES */
1096 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
1102 * EVP_CTRL_INIT is purely legacy, no provider counterpart.
1103 * As a matter of fact, this should be dead code, but some caller
1104 * might still do a direct control call with this command, so...
1105 * Legacy methods return 1 except for exceptional circumstances, so
1106 * we do the same here to not be disruptive.
1109 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
1112 case EVP_CTRL_AEAD_SET_IVLEN:
1115 if (ctx->iv_len == arg)
1116 /* Skip calling into provider if unchanged. */
1118 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1121 case EVP_CTRL_CCM_SET_L:
1122 if (arg < 2 || arg > 8)
1125 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1128 case EVP_CTRL_AEAD_SET_IV_FIXED:
1129 params[0] = OSSL_PARAM_construct_octet_string(
1130 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
1132 case EVP_CTRL_GCM_IV_GEN:
1135 sz = 0; /* special case that uses the iv length */
1136 params[0] = OSSL_PARAM_construct_octet_string(
1137 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
1139 case EVP_CTRL_GCM_SET_IV_INV:
1142 params[0] = OSSL_PARAM_construct_octet_string(
1143 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
1145 case EVP_CTRL_GET_RC5_ROUNDS:
1146 set_params = 0; /* Fall thru */
1147 case EVP_CTRL_SET_RC5_ROUNDS:
1150 i = (unsigned int)arg;
1151 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
1153 case EVP_CTRL_SET_SPEED:
1156 i = (unsigned int)arg;
1157 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
1159 case EVP_CTRL_AEAD_GET_TAG:
1160 set_params = 0; /* Fall thru */
1161 case EVP_CTRL_AEAD_SET_TAG:
1162 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1165 case EVP_CTRL_AEAD_TLS1_AAD:
1166 /* This one does a set and a get - since it returns a size */
1168 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1170 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1174 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1175 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1179 #ifndef OPENSSL_NO_RC2
1180 case EVP_CTRL_GET_RC2_KEY_BITS:
1181 set_params = 0; /* Fall thru */
1182 case EVP_CTRL_SET_RC2_KEY_BITS:
1183 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
1185 #endif /* OPENSSL_NO_RC2 */
1186 #if !defined(OPENSSL_NO_MULTIBLOCK)
1187 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
1188 params[0] = OSSL_PARAM_construct_size_t(
1189 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
1190 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1194 params[0] = OSSL_PARAM_construct_size_t(
1195 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
1196 params[1] = OSSL_PARAM_construct_end();
1197 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1201 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
1202 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1203 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1205 if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
1208 params[0] = OSSL_PARAM_construct_octet_string(
1209 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void*)p->inp, p->len);
1210 params[1] = OSSL_PARAM_construct_uint(
1211 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1212 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1215 /* Retrieve the return values changed by the set */
1216 params[0] = OSSL_PARAM_construct_size_t(
1217 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
1218 params[1] = OSSL_PARAM_construct_uint(
1219 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1220 params[2] = OSSL_PARAM_construct_end();
1221 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1226 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
1227 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1228 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1230 params[0] = OSSL_PARAM_construct_octet_string(
1231 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
1233 params[1] = OSSL_PARAM_construct_octet_string(
1234 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void*)p->inp,
1236 params[2] = OSSL_PARAM_construct_uint(
1237 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1238 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1241 params[0] = OSSL_PARAM_construct_size_t(
1242 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
1243 params[1] = OSSL_PARAM_construct_end();
1244 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1249 #endif /* OPENSSL_NO_MULTIBLOCK */
1250 case EVP_CTRL_AEAD_SET_MAC_KEY:
1253 params[0] = OSSL_PARAM_construct_octet_string(
1254 OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
1259 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1261 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1264 /* Code below to be removed when legacy support is dropped. */
1266 if (ctx->cipher->ctrl == NULL) {
1267 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
1271 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
1274 if (ret == EVP_CTRL_RET_UNSUPPORTED) {
1275 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
1281 int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1283 if (cipher != NULL && cipher->get_params != NULL)
1284 return cipher->get_params(params);
1288 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1291 const OSSL_PARAM *p;
1293 if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) {
1294 r = ctx->cipher->set_ctx_params(ctx->algctx, params);
1296 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
1297 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->key_len)) {
1303 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
1304 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->iv_len)) {
1313 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1315 if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1316 return ctx->cipher->get_ctx_params(ctx->algctx, params);
1320 const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1322 if (cipher != NULL && cipher->gettable_params != NULL)
1323 return cipher->gettable_params(
1324 ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)));
1328 const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
1332 if (cipher != NULL && cipher->settable_ctx_params != NULL) {
1333 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1334 return cipher->settable_ctx_params(NULL, provctx);
1339 const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
1343 if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
1344 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1345 return cipher->gettable_ctx_params(NULL, provctx);
1350 const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx)
1354 if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
1355 alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1356 return cctx->cipher->settable_ctx_params(cctx->algctx, alg);
1361 const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx)
1365 if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
1366 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1367 return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx);
1373 static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx)
1375 const EVP_CIPHER *cipher = ctx->cipher;
1376 const OSSL_PROVIDER *prov;
1381 prov = EVP_CIPHER_get0_provider(cipher);
1382 return ossl_provider_libctx(prov);
1386 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
1388 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1389 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1396 OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
1398 kl = EVP_CIPHER_CTX_get_key_length(ctx);
1399 if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0)
1403 #endif /* FIPS_MODULE */
1406 EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in)
1408 EVP_CIPHER_CTX *out = EVP_CIPHER_CTX_new();
1410 if (out != NULL && !EVP_CIPHER_CTX_copy(out, in)) {
1411 EVP_CIPHER_CTX_free(out);
1417 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
1419 if ((in == NULL) || (in->cipher == NULL)) {
1420 ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
1424 if (in->cipher->prov == NULL)
1427 if (in->cipher->dupctx == NULL) {
1428 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1432 EVP_CIPHER_CTX_reset(out);
1437 if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
1438 out->fetched_cipher = NULL;
1442 out->algctx = in->cipher->dupctx(in->algctx);
1443 if (out->algctx == NULL) {
1444 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1450 /* Code below to be removed when legacy support is dropped. */
1453 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1454 /* Make sure it's safe to copy a cipher context using an ENGINE */
1455 if (in->engine && !ENGINE_init(in->engine)) {
1456 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
1461 EVP_CIPHER_CTX_reset(out);
1462 memcpy(out, in, sizeof(*out));
1464 if (in->cipher_data && in->cipher->ctx_size) {
1465 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
1466 if (out->cipher_data == NULL) {
1470 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
1473 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
1474 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
1476 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
1482 EVP_CIPHER *evp_cipher_new(void)
1484 EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1486 if (cipher != NULL) {
1487 cipher->lock = CRYPTO_THREAD_lock_new();
1488 if (cipher->lock == NULL) {
1489 OPENSSL_free(cipher);
1498 * FIPS module note: since internal fetches will be entirely
1499 * provider based, we know that none of its code depends on legacy
1500 * NIDs or any functionality that use them.
1503 /* After removal of legacy support get rid of the need for legacy NIDs */
1504 static void set_legacy_nid(const char *name, void *vlegacy_nid)
1507 int *legacy_nid = vlegacy_nid;
1509 * We use lowest level function to get the associated method, because
1510 * higher level functions such as EVP_get_cipherbyname() have changed
1511 * to look at providers too.
1513 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
1515 if (*legacy_nid == -1) /* We found a clash already */
1517 if (legacy_method == NULL)
1519 nid = EVP_CIPHER_get_nid(legacy_method);
1520 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
1528 static void *evp_cipher_from_algorithm(const int name_id,
1529 const OSSL_ALGORITHM *algodef,
1530 OSSL_PROVIDER *prov)
1532 const OSSL_DISPATCH *fns = algodef->implementation;
1533 EVP_CIPHER *cipher = NULL;
1534 int fnciphcnt = 0, fnctxcnt = 0;
1536 if ((cipher = evp_cipher_new()) == NULL) {
1537 ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
1542 cipher->nid = NID_undef;
1543 if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
1544 || cipher->nid == -1) {
1545 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1546 EVP_CIPHER_free(cipher);
1551 cipher->name_id = name_id;
1552 if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
1553 EVP_CIPHER_free(cipher);
1556 cipher->description = algodef->algorithm_description;
1558 for (; fns->function_id != 0; fns++) {
1559 switch (fns->function_id) {
1560 case OSSL_FUNC_CIPHER_NEWCTX:
1561 if (cipher->newctx != NULL)
1563 cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
1566 case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1567 if (cipher->einit != NULL)
1569 cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
1572 case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1573 if (cipher->dinit != NULL)
1575 cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
1578 case OSSL_FUNC_CIPHER_UPDATE:
1579 if (cipher->cupdate != NULL)
1581 cipher->cupdate = OSSL_FUNC_cipher_update(fns);
1584 case OSSL_FUNC_CIPHER_FINAL:
1585 if (cipher->cfinal != NULL)
1587 cipher->cfinal = OSSL_FUNC_cipher_final(fns);
1590 case OSSL_FUNC_CIPHER_CIPHER:
1591 if (cipher->ccipher != NULL)
1593 cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
1595 case OSSL_FUNC_CIPHER_FREECTX:
1596 if (cipher->freectx != NULL)
1598 cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
1601 case OSSL_FUNC_CIPHER_DUPCTX:
1602 if (cipher->dupctx != NULL)
1604 cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
1606 case OSSL_FUNC_CIPHER_GET_PARAMS:
1607 if (cipher->get_params != NULL)
1609 cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
1611 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1612 if (cipher->get_ctx_params != NULL)
1614 cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
1616 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1617 if (cipher->set_ctx_params != NULL)
1619 cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
1621 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1622 if (cipher->gettable_params != NULL)
1624 cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
1626 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1627 if (cipher->gettable_ctx_params != NULL)
1629 cipher->gettable_ctx_params =
1630 OSSL_FUNC_cipher_gettable_ctx_params(fns);
1632 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1633 if (cipher->settable_ctx_params != NULL)
1635 cipher->settable_ctx_params =
1636 OSSL_FUNC_cipher_settable_ctx_params(fns);
1640 if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1641 || (fnciphcnt == 0 && cipher->ccipher == NULL)
1644 * In order to be a consistent set of functions we must have at least
1645 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1646 * functions, or a single "cipher" function. In all cases we need both
1647 * the "newctx" and "freectx" functions.
1649 EVP_CIPHER_free(cipher);
1650 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1653 cipher->prov = prov;
1655 ossl_provider_up_ref(prov);
1657 if (!evp_cipher_cache_constants(cipher)) {
1658 EVP_CIPHER_free(cipher);
1659 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1666 static int evp_cipher_up_ref(void *cipher)
1668 return EVP_CIPHER_up_ref(cipher);
1671 static void evp_cipher_free(void *cipher)
1673 EVP_CIPHER_free(cipher);
1676 EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1677 const char *properties)
1679 EVP_CIPHER *cipher =
1680 evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1681 evp_cipher_from_algorithm, evp_cipher_up_ref,
1687 int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
1691 if (cipher->origin == EVP_ORIG_DYNAMIC)
1692 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
1696 void evp_cipher_free_int(EVP_CIPHER *cipher)
1698 OPENSSL_free(cipher->type_name);
1699 ossl_provider_free(cipher->prov);
1700 CRYPTO_THREAD_lock_free(cipher->lock);
1701 OPENSSL_free(cipher);
1704 void EVP_CIPHER_free(EVP_CIPHER *cipher)
1708 if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC)
1711 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
1714 evp_cipher_free_int(cipher);
1717 void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
1718 void (*fn)(EVP_CIPHER *mac, void *arg),
1721 evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1722 (void (*)(void *, void *))fn, arg,
1723 evp_cipher_from_algorithm, evp_cipher_up_ref,