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 if (ctx->cipher == ctx->fetched_cipher)
150 EVP_CIPHER_free(ctx->fetched_cipher);
151 ctx->fetched_cipher = NULL;
155 * Ensure a context left lying around from last time is cleared
158 if (cipher != NULL && ctx->cipher != NULL) {
159 OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size);
160 ctx->cipher_data = NULL;
164 /* Start of non-legacy code below */
166 /* Ensure a context left lying around from last time is cleared */
167 if (cipher != NULL && ctx->cipher != NULL) {
168 unsigned long flags = ctx->flags;
170 EVP_CIPHER_CTX_reset(ctx);
171 /* Restore encrypt and flags */
177 cipher = ctx->cipher;
179 if (cipher->prov == NULL) {
181 /* We only do explicit fetches inside the FIPS module */
182 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
185 EVP_CIPHER *provciph =
186 EVP_CIPHER_fetch(NULL,
187 cipher->nid == NID_undef ? "NULL"
188 : OBJ_nid2sn(cipher->nid),
191 if (provciph == NULL)
194 EVP_CIPHER_free(ctx->fetched_cipher);
195 ctx->fetched_cipher = provciph;
199 if (cipher->prov != NULL) {
200 if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) {
201 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
204 EVP_CIPHER_free(ctx->fetched_cipher);
205 /* Coverity false positive, the reference counting is confusing it */
206 /* coverity[use_after_free] */
207 ctx->fetched_cipher = (EVP_CIPHER *)cipher;
209 ctx->cipher = cipher;
210 if (ctx->algctx == NULL) {
211 ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
212 if (ctx->algctx == NULL) {
213 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
218 if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
220 * If this ctx was already set up for no padding then we need to tell
221 * the new cipher about it.
223 if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
228 if (ctx->cipher->einit == NULL) {
229 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
233 return ctx->cipher->einit(ctx->algctx,
236 : EVP_CIPHER_CTX_get_key_length(ctx),
239 : EVP_CIPHER_CTX_get_iv_length(ctx),
243 if (ctx->cipher->dinit == NULL) {
244 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
248 return ctx->cipher->dinit(ctx->algctx,
251 : EVP_CIPHER_CTX_get_key_length(ctx),
254 : EVP_CIPHER_CTX_get_iv_length(ctx),
257 /* Code below to be removed when legacy support is dropped. */
260 if (cipher != NULL) {
262 * Ensure a context left lying around from last time is cleared (we
263 * previously attempted to avoid this if the same ENGINE and
264 * EVP_CIPHER could be used).
267 unsigned long flags = ctx->flags;
268 EVP_CIPHER_CTX_reset(ctx);
269 /* Restore encrypt and flags */
273 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
275 if (!ENGINE_init(impl)) {
276 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
283 /* There's an ENGINE for this job ... (apparently) */
284 const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
288 * One positive side-effect of US's export control history,
289 * is that we should at least be able to avoid using US
290 * misspellings of "initialisation"?
292 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
295 /* We'll use the ENGINE's private cipher definition */
298 * Store the ENGINE functional reference so we know 'cipher' came
299 * from an ENGINE and we need to release it when done.
307 ctx->cipher = cipher;
308 if (ctx->cipher->ctx_size) {
309 ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
310 if (ctx->cipher_data == NULL) {
312 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
316 ctx->cipher_data = NULL;
318 ctx->key_len = cipher->key_len;
319 /* Preserve wrap enable flag, zero everything else */
320 ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
321 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
322 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL) <= 0) {
324 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
329 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
332 if (ctx->cipher == NULL)
335 /* we assume block size is a power of 2 in *cryptUpdate */
336 OPENSSL_assert(ctx->cipher->block_size == 1
337 || ctx->cipher->block_size == 8
338 || ctx->cipher->block_size == 16);
340 if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
341 && EVP_CIPHER_CTX_get_mode(ctx) == EVP_CIPH_WRAP_MODE) {
342 ERR_raise(ERR_LIB_EVP, EVP_R_WRAP_MODE_NOT_ALLOWED);
346 if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
347 & EVP_CIPH_CUSTOM_IV) == 0) {
348 switch (EVP_CIPHER_CTX_get_mode(ctx)) {
350 case EVP_CIPH_STREAM_CIPHER:
351 case EVP_CIPH_ECB_MODE:
354 case EVP_CIPH_CFB_MODE:
355 case EVP_CIPH_OFB_MODE:
360 case EVP_CIPH_CBC_MODE:
361 n = EVP_CIPHER_CTX_get_iv_length(ctx);
362 if (n < 0 || n > (int)sizeof(ctx->iv)) {
363 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
367 memcpy(ctx->oiv, iv, n);
368 memcpy(ctx->iv, ctx->oiv, n);
371 case EVP_CIPH_CTR_MODE:
373 /* Don't reuse IV for CTR mode */
375 n = EVP_CIPHER_CTX_get_iv_length(ctx);
376 if (n <= 0 || n > (int)sizeof(ctx->iv)) {
377 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
380 memcpy(ctx->iv, iv, n);
389 if (key != NULL || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
390 if (!ctx->cipher->init(ctx, key, iv, enc))
395 ctx->block_mask = ctx->cipher->block_size - 1;
399 int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
400 const unsigned char *key, const unsigned char *iv,
401 int enc, const OSSL_PARAM params[])
403 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, params);
406 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
407 const unsigned char *key, const unsigned char *iv, int enc)
410 EVP_CIPHER_CTX_reset(ctx);
411 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, NULL);
414 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
415 ENGINE *impl, const unsigned char *key,
416 const unsigned char *iv, int enc)
418 return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, NULL);
421 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
422 const unsigned char *in, int inl)
425 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
427 return EVP_DecryptUpdate(ctx, out, outl, in, inl);
430 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
433 return EVP_EncryptFinal_ex(ctx, out, outl);
435 return EVP_DecryptFinal_ex(ctx, out, outl);
438 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
441 return EVP_EncryptFinal(ctx, out, outl);
443 return EVP_DecryptFinal(ctx, out, outl);
446 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
447 const unsigned char *key, const unsigned char *iv)
449 return EVP_CipherInit(ctx, cipher, key, iv, 1);
452 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
453 ENGINE *impl, const unsigned char *key,
454 const unsigned char *iv)
456 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
459 int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
460 const unsigned char *key, const unsigned char *iv,
461 const OSSL_PARAM params[])
463 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params);
466 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
467 const unsigned char *key, const unsigned char *iv)
469 return EVP_CipherInit(ctx, cipher, key, iv, 0);
472 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
473 ENGINE *impl, const unsigned char *key,
474 const unsigned char *iv)
476 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
479 int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
480 const unsigned char *key, const unsigned char *iv,
481 const OSSL_PARAM params[])
483 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 0, params);
487 * According to the letter of standard difference between pointers
488 * is specified to be valid only within same object. This makes
489 * it formally challenging to determine if input and output buffers
490 * are not partially overlapping with standard pointer arithmetic.
495 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
497 * Then we have VMS that distinguishes itself by adhering to
498 * sizeof(size_t)==4 even in 64-bit builds, which means that
499 * difference between two pointers might be truncated to 32 bits.
500 * In the context one can even wonder how comparison for
501 * equality is implemented. To be on the safe side we adhere to
502 * PTRDIFF_T even for comparison for equality.
504 # define PTRDIFF_T uint64_t
506 # define PTRDIFF_T size_t
509 int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
511 PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
513 * Check for partially overlapping buffers. [Binary logical
514 * operations are used instead of boolean to minimize number
515 * of conditional branches.]
517 int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
518 (diff > (0 - (PTRDIFF_T)len)));
523 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
524 unsigned char *out, int *outl,
525 const unsigned char *in, int inl)
527 int i, j, bl, cmpl = inl;
529 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
530 cmpl = safe_div_round_up_int(cmpl, 8, NULL);
532 bl = ctx->cipher->block_size;
534 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
535 /* If block size > 1 then the cipher will have to do this check */
536 if (bl == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
537 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
541 i = ctx->cipher->do_cipher(ctx, out, in, inl);
553 if (ossl_is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
554 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
558 if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
559 if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
568 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
571 memcpy(&(ctx->buf[i]), in, inl);
579 * Once we've processed the first j bytes from in, the amount of
580 * data left that is a multiple of the block length is:
581 * (inl - j) & ~(bl - 1)
582 * We must ensure that this amount of data, plus the one block that
583 * we process from ctx->buf does not exceed INT_MAX
585 if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
586 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
589 memcpy(&(ctx->buf[i]), in, j);
592 if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
602 if (!ctx->cipher->do_cipher(ctx, out, in, inl))
608 memcpy(ctx->buf, &(in[inl]), i);
614 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
615 const unsigned char *in, int inl)
618 size_t soutl, inl_ = (size_t)inl;
624 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
628 /* Prevent accidental use of decryption context when encrypting */
630 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
634 if (ctx->cipher == NULL) {
635 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
639 if (ctx->cipher->prov == NULL)
642 blocksize = ctx->cipher->block_size;
644 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
645 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
649 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
650 inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
654 if (soutl > INT_MAX) {
655 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
663 /* Code below to be removed when legacy support is dropped. */
666 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
669 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
672 ret = EVP_EncryptFinal_ex(ctx, out, outl);
676 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
679 unsigned int i, b, bl;
686 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
690 /* Prevent accidental use of decryption context when encrypting */
692 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
696 if (ctx->cipher == NULL) {
697 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
700 if (ctx->cipher->prov == NULL)
703 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
705 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
706 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
710 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
711 blocksize == 1 ? 0 : blocksize);
714 if (soutl > INT_MAX) {
715 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
723 /* Code below to be removed when legacy support is dropped. */
726 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
727 ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
735 b = ctx->cipher->block_size;
736 OPENSSL_assert(b <= sizeof(ctx->buf));
742 if (ctx->flags & EVP_CIPH_NO_PADDING) {
744 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
752 for (i = bl; i < b; i++)
754 ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
762 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
763 const unsigned char *in, int inl)
765 int fix_len, cmpl = inl, ret;
767 size_t soutl, inl_ = (size_t)inl;
773 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
777 /* Prevent accidental use of encryption context when decrypting */
779 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
783 if (ctx->cipher == NULL) {
784 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
787 if (ctx->cipher->prov == NULL)
790 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
792 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
793 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
796 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
797 inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
801 if (soutl > INT_MAX) {
802 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
810 /* Code below to be removed when legacy support is dropped. */
813 b = ctx->cipher->block_size;
815 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
816 cmpl = safe_div_round_up_int(cmpl, 8, NULL);
818 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
819 if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
820 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
824 fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
838 if (ctx->flags & EVP_CIPH_NO_PADDING)
839 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
841 OPENSSL_assert(b <= sizeof(ctx->final));
843 if (ctx->final_used) {
844 /* see comment about PTRDIFF_T comparison above */
845 if (((PTRDIFF_T)out == (PTRDIFF_T)in)
846 || ossl_is_partially_overlapping(out, in, b)) {
847 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
851 * final_used is only ever set if buf_len is 0. Therefore the maximum
852 * length output we will ever see from evp_EncryptDecryptUpdate is
853 * the maximum multiple of the block length that is <= inl, or just:
855 * Since final_used has been set then the final output length is:
856 * (inl & ~(b - 1)) + b
857 * This must never exceed INT_MAX
859 if ((inl & ~(b - 1)) > INT_MAX - b) {
860 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
863 memcpy(out, ctx->final, b);
869 if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
873 * if we have 'decrypted' a multiple of block size, make sure we have a
874 * copy of this last block
876 if (b > 1 && !ctx->buf_len) {
879 memcpy(ctx->final, &out[*outl], b);
889 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
892 ret = EVP_DecryptFinal_ex(ctx, out, outl);
896 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
907 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
911 /* Prevent accidental use of encryption context when decrypting */
913 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
917 if (ctx->cipher == NULL) {
918 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
922 if (ctx->cipher->prov == NULL)
925 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
927 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
928 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
932 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
933 blocksize == 1 ? 0 : blocksize);
936 if (soutl > INT_MAX) {
937 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
945 /* Code below to be removed when legacy support is dropped. */
949 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
950 i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
958 b = ctx->cipher->block_size;
959 if (ctx->flags & EVP_CIPH_NO_PADDING) {
961 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
968 if (ctx->buf_len || !ctx->final_used) {
969 ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
972 OPENSSL_assert(b <= sizeof(ctx->final));
975 * The following assumes that the ciphertext has been authenticated.
976 * Otherwise it provides a padding oracle.
978 n = ctx->final[b - 1];
979 if (n == 0 || n > (int)b) {
980 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
983 for (i = 0; i < n; i++) {
984 if (ctx->final[--b] != n) {
985 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
989 n = ctx->cipher->block_size - n;
990 for (i = 0; i < n; i++)
991 out[i] = ctx->final[i];
998 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
1000 if (c->cipher->prov != NULL) {
1002 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1005 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
1008 /* Check the cipher actually understands this parameter */
1009 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
1010 OSSL_CIPHER_PARAM_KEYLEN) == NULL) {
1011 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1015 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
1016 if (!OSSL_PARAM_set_int(params, keylen))
1018 ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params);
1021 c->key_len = keylen;
1025 /* Code below to be removed when legacy support is dropped. */
1028 * Note there have never been any built-in ciphers that define this flag
1029 * since it was first introduced.
1031 if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
1032 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
1033 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
1035 if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
1036 c->key_len = keylen;
1039 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1043 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
1046 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1047 unsigned int pd = pad;
1050 ctx->flags &= ~EVP_CIPH_NO_PADDING;
1052 ctx->flags |= EVP_CIPH_NO_PADDING;
1054 if (ctx->cipher != NULL && ctx->cipher->prov == NULL)
1056 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
1057 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1062 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1064 int ret = EVP_CTRL_RET_UNSUPPORTED;
1068 OSSL_PARAM params[4] = {
1069 OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
1072 if (ctx == NULL || ctx->cipher == NULL) {
1073 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
1077 if (ctx->cipher->prov == NULL)
1081 case EVP_CTRL_SET_KEY_LENGTH:
1082 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
1085 case EVP_CTRL_RAND_KEY: /* Used by DES */
1088 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
1094 * EVP_CTRL_INIT is purely legacy, no provider counterpart.
1095 * As a matter of fact, this should be dead code, but some caller
1096 * might still do a direct control call with this command, so...
1097 * Legacy methods return 1 except for exceptional circumstances, so
1098 * we do the same here to not be disruptive.
1101 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
1104 case EVP_CTRL_AEAD_SET_IVLEN:
1107 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1110 case EVP_CTRL_CCM_SET_L:
1111 if (arg < 2 || arg > 8)
1114 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1117 case EVP_CTRL_AEAD_SET_IV_FIXED:
1118 params[0] = OSSL_PARAM_construct_octet_string(
1119 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
1121 case EVP_CTRL_GCM_IV_GEN:
1124 sz = 0; /* special case that uses the iv length */
1125 params[0] = OSSL_PARAM_construct_octet_string(
1126 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
1128 case EVP_CTRL_GCM_SET_IV_INV:
1131 params[0] = OSSL_PARAM_construct_octet_string(
1132 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
1134 case EVP_CTRL_GET_RC5_ROUNDS:
1135 set_params = 0; /* Fall thru */
1136 case EVP_CTRL_SET_RC5_ROUNDS:
1139 i = (unsigned int)arg;
1140 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
1142 case EVP_CTRL_SET_SPEED:
1145 i = (unsigned int)arg;
1146 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
1148 case EVP_CTRL_AEAD_GET_TAG:
1149 set_params = 0; /* Fall thru */
1150 case EVP_CTRL_AEAD_SET_TAG:
1151 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1154 case EVP_CTRL_AEAD_TLS1_AAD:
1155 /* This one does a set and a get - since it returns a size */
1157 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1159 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1163 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1164 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1168 #ifndef OPENSSL_NO_RC2
1169 case EVP_CTRL_GET_RC2_KEY_BITS:
1170 set_params = 0; /* Fall thru */
1171 case EVP_CTRL_SET_RC2_KEY_BITS:
1172 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
1174 #endif /* OPENSSL_NO_RC2 */
1175 #if !defined(OPENSSL_NO_MULTIBLOCK)
1176 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
1177 params[0] = OSSL_PARAM_construct_size_t(
1178 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
1179 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1183 params[0] = OSSL_PARAM_construct_size_t(
1184 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
1185 params[1] = OSSL_PARAM_construct_end();
1186 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1190 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
1191 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1192 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1194 if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
1197 params[0] = OSSL_PARAM_construct_octet_string(
1198 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void*)p->inp, p->len);
1199 params[1] = OSSL_PARAM_construct_uint(
1200 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1201 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1204 /* Retrieve the return values changed by the set */
1205 params[0] = OSSL_PARAM_construct_size_t(
1206 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
1207 params[1] = OSSL_PARAM_construct_uint(
1208 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1209 params[2] = OSSL_PARAM_construct_end();
1210 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1215 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
1216 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1217 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1219 params[0] = OSSL_PARAM_construct_octet_string(
1220 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
1222 params[1] = OSSL_PARAM_construct_octet_string(
1223 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void*)p->inp,
1225 params[2] = OSSL_PARAM_construct_uint(
1226 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1227 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1230 params[0] = OSSL_PARAM_construct_size_t(
1231 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
1232 params[1] = OSSL_PARAM_construct_end();
1233 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1238 #endif /* OPENSSL_NO_MULTIBLOCK */
1239 case EVP_CTRL_AEAD_SET_MAC_KEY:
1242 params[0] = OSSL_PARAM_construct_octet_string(
1243 OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
1248 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1250 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1253 /* Code below to be removed when legacy support is dropped. */
1255 if (ctx->cipher->ctrl == NULL) {
1256 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
1260 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
1263 if (ret == EVP_CTRL_RET_UNSUPPORTED) {
1264 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
1270 int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1272 if (cipher != NULL && cipher->get_params != NULL)
1273 return cipher->get_params(params);
1277 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1280 const OSSL_PARAM *p;
1282 if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) {
1283 r = ctx->cipher->set_ctx_params(ctx->algctx, params);
1285 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
1286 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->key_len)) {
1292 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
1293 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->iv_len)) {
1302 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1304 if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1305 return ctx->cipher->get_ctx_params(ctx->algctx, params);
1309 const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1311 if (cipher != NULL && cipher->gettable_params != NULL)
1312 return cipher->gettable_params(
1313 ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)));
1317 const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
1321 if (cipher != NULL && cipher->settable_ctx_params != NULL) {
1322 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1323 return cipher->settable_ctx_params(NULL, provctx);
1328 const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
1332 if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
1333 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1334 return cipher->gettable_ctx_params(NULL, provctx);
1339 const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx)
1343 if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
1344 alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1345 return cctx->cipher->settable_ctx_params(cctx->algctx, alg);
1350 const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx)
1354 if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
1355 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1356 return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx);
1362 static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx)
1364 const EVP_CIPHER *cipher = ctx->cipher;
1365 const OSSL_PROVIDER *prov;
1370 prov = EVP_CIPHER_get0_provider(cipher);
1371 return ossl_provider_libctx(prov);
1375 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
1377 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1378 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1385 OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
1387 kl = EVP_CIPHER_CTX_get_key_length(ctx);
1388 if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0)
1392 #endif /* FIPS_MODULE */
1395 EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in)
1397 EVP_CIPHER_CTX *out = EVP_CIPHER_CTX_new();
1399 if (out != NULL && !EVP_CIPHER_CTX_copy(out, in)) {
1400 EVP_CIPHER_CTX_free(out);
1406 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
1408 if ((in == NULL) || (in->cipher == NULL)) {
1409 ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
1413 if (in->cipher->prov == NULL)
1416 if (in->cipher->dupctx == NULL) {
1417 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1421 EVP_CIPHER_CTX_reset(out);
1426 if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
1427 out->fetched_cipher = NULL;
1431 out->algctx = in->cipher->dupctx(in->algctx);
1432 if (out->algctx == NULL) {
1433 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1439 /* Code below to be removed when legacy support is dropped. */
1442 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1443 /* Make sure it's safe to copy a cipher context using an ENGINE */
1444 if (in->engine && !ENGINE_init(in->engine)) {
1445 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
1450 EVP_CIPHER_CTX_reset(out);
1451 memcpy(out, in, sizeof(*out));
1453 if (in->cipher_data && in->cipher->ctx_size) {
1454 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
1455 if (out->cipher_data == NULL) {
1457 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1460 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
1463 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
1464 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
1466 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
1472 EVP_CIPHER *evp_cipher_new(void)
1474 EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1476 if (cipher != NULL) {
1477 cipher->lock = CRYPTO_THREAD_lock_new();
1478 if (cipher->lock == NULL) {
1479 OPENSSL_free(cipher);
1488 * FIPS module note: since internal fetches will be entirely
1489 * provider based, we know that none of its code depends on legacy
1490 * NIDs or any functionality that use them.
1493 /* After removal of legacy support get rid of the need for legacy NIDs */
1494 static void set_legacy_nid(const char *name, void *vlegacy_nid)
1497 int *legacy_nid = vlegacy_nid;
1499 * We use lowest level function to get the associated method, because
1500 * higher level functions such as EVP_get_cipherbyname() have changed
1501 * to look at providers too.
1503 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
1505 if (*legacy_nid == -1) /* We found a clash already */
1507 if (legacy_method == NULL)
1509 nid = EVP_CIPHER_get_nid(legacy_method);
1510 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
1518 static void *evp_cipher_from_algorithm(const int name_id,
1519 const OSSL_ALGORITHM *algodef,
1520 OSSL_PROVIDER *prov)
1522 const OSSL_DISPATCH *fns = algodef->implementation;
1523 EVP_CIPHER *cipher = NULL;
1524 int fnciphcnt = 0, fnctxcnt = 0;
1526 if ((cipher = evp_cipher_new()) == NULL) {
1527 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1532 cipher->nid = NID_undef;
1533 if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
1534 || cipher->nid == -1) {
1535 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1536 EVP_CIPHER_free(cipher);
1541 cipher->name_id = name_id;
1542 if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
1543 EVP_CIPHER_free(cipher);
1546 cipher->description = algodef->algorithm_description;
1548 for (; fns->function_id != 0; fns++) {
1549 switch (fns->function_id) {
1550 case OSSL_FUNC_CIPHER_NEWCTX:
1551 if (cipher->newctx != NULL)
1553 cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
1556 case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1557 if (cipher->einit != NULL)
1559 cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
1562 case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1563 if (cipher->dinit != NULL)
1565 cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
1568 case OSSL_FUNC_CIPHER_UPDATE:
1569 if (cipher->cupdate != NULL)
1571 cipher->cupdate = OSSL_FUNC_cipher_update(fns);
1574 case OSSL_FUNC_CIPHER_FINAL:
1575 if (cipher->cfinal != NULL)
1577 cipher->cfinal = OSSL_FUNC_cipher_final(fns);
1580 case OSSL_FUNC_CIPHER_CIPHER:
1581 if (cipher->ccipher != NULL)
1583 cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
1585 case OSSL_FUNC_CIPHER_FREECTX:
1586 if (cipher->freectx != NULL)
1588 cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
1591 case OSSL_FUNC_CIPHER_DUPCTX:
1592 if (cipher->dupctx != NULL)
1594 cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
1596 case OSSL_FUNC_CIPHER_GET_PARAMS:
1597 if (cipher->get_params != NULL)
1599 cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
1601 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1602 if (cipher->get_ctx_params != NULL)
1604 cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
1606 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1607 if (cipher->set_ctx_params != NULL)
1609 cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
1611 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1612 if (cipher->gettable_params != NULL)
1614 cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
1616 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1617 if (cipher->gettable_ctx_params != NULL)
1619 cipher->gettable_ctx_params =
1620 OSSL_FUNC_cipher_gettable_ctx_params(fns);
1622 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1623 if (cipher->settable_ctx_params != NULL)
1625 cipher->settable_ctx_params =
1626 OSSL_FUNC_cipher_settable_ctx_params(fns);
1630 if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1631 || (fnciphcnt == 0 && cipher->ccipher == NULL)
1634 * In order to be a consistent set of functions we must have at least
1635 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1636 * functions, or a single "cipher" function. In all cases we need both
1637 * the "newctx" and "freectx" functions.
1639 EVP_CIPHER_free(cipher);
1640 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1643 cipher->prov = prov;
1645 ossl_provider_up_ref(prov);
1647 if (!evp_cipher_cache_constants(cipher)) {
1648 EVP_CIPHER_free(cipher);
1649 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1656 static int evp_cipher_up_ref(void *cipher)
1658 return EVP_CIPHER_up_ref(cipher);
1661 static void evp_cipher_free(void *cipher)
1663 EVP_CIPHER_free(cipher);
1666 EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1667 const char *properties)
1669 EVP_CIPHER *cipher =
1670 evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1671 evp_cipher_from_algorithm, evp_cipher_up_ref,
1677 int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
1681 if (cipher->origin == EVP_ORIG_DYNAMIC)
1682 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
1686 void evp_cipher_free_int(EVP_CIPHER *cipher)
1688 OPENSSL_free(cipher->type_name);
1689 ossl_provider_free(cipher->prov);
1690 CRYPTO_THREAD_lock_free(cipher->lock);
1691 OPENSSL_free(cipher);
1694 void EVP_CIPHER_free(EVP_CIPHER *cipher)
1698 if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC)
1701 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
1704 evp_cipher_free_int(cipher);
1707 void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
1708 void (*fn)(EVP_CIPHER *mac, void *arg),
1711 evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1712 (void (*)(void *, void *))fn, arg,
1713 evp_cipher_from_algorithm, evp_cipher_up_ref,