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 "crypto/evp.h"
28 #include "evp_local.h"
30 int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
35 if (ctx->cipher == NULL || ctx->cipher->prov == NULL)
38 if (ctx->algctx != NULL) {
39 if (ctx->cipher->freectx != NULL)
40 ctx->cipher->freectx(ctx->algctx);
43 if (ctx->fetched_cipher != NULL)
44 EVP_CIPHER_free(ctx->fetched_cipher);
45 memset(ctx, 0, sizeof(*ctx));
50 /* Remove legacy code below when legacy support is removed. */
53 if (ctx->cipher != NULL) {
54 if (ctx->cipher->cleanup && !ctx->cipher->cleanup(ctx))
56 /* Cleanse cipher context data */
57 if (ctx->cipher_data && ctx->cipher->ctx_size)
58 OPENSSL_cleanse(ctx->cipher_data, ctx->cipher->ctx_size);
60 OPENSSL_free(ctx->cipher_data);
61 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
62 ENGINE_finish(ctx->engine);
64 memset(ctx, 0, sizeof(*ctx));
69 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
71 return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
74 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
78 EVP_CIPHER_CTX_reset(ctx);
82 static int evp_cipher_init_internal(EVP_CIPHER_CTX *ctx,
83 const EVP_CIPHER *cipher,
84 ENGINE *impl, const unsigned char *key,
85 const unsigned char *iv, int enc,
86 const OSSL_PARAM params[])
89 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
90 ENGINE *tmpimpl = NULL;
96 * enc == 1 means we are encrypting.
97 * enc == 0 means we are decrypting.
98 * enc == -1 means, use the previously initialised value for encrypt/decrypt
108 if (cipher == NULL && ctx->cipher == NULL) {
109 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
113 /* Code below to be removed when legacy support is dropped. */
115 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
117 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
118 * this context may already have an ENGINE! Try to avoid releasing the
119 * previous handle, re-querying for an ENGINE, and having a
120 * reinitialisation, when it may all be unnecessary.
122 if (ctx->engine && ctx->cipher
123 && (cipher == NULL || cipher->nid == ctx->cipher->nid))
126 if (cipher != NULL && impl == NULL) {
127 /* Ask if an ENGINE is reserved for this job */
128 tmpimpl = ENGINE_get_cipher_engine(cipher->nid);
133 * If there are engines involved then we should use legacy handling for now.
135 if (ctx->engine != NULL
136 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
140 if (ctx->cipher == ctx->fetched_cipher)
142 EVP_CIPHER_free(ctx->fetched_cipher);
143 ctx->fetched_cipher = NULL;
147 * Ensure a context left lying around from last time is cleared
150 if (cipher != NULL && ctx->cipher != NULL) {
151 OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size);
152 ctx->cipher_data = NULL;
156 /* Start of non-legacy code below */
158 /* Ensure a context left lying around from last time is cleared */
159 if (cipher != NULL && ctx->cipher != NULL) {
160 unsigned long flags = ctx->flags;
162 EVP_CIPHER_CTX_reset(ctx);
163 /* Restore encrypt and flags */
169 cipher = ctx->cipher;
171 if (cipher->prov == NULL) {
173 /* We only do explicit fetches inside the FIPS module */
174 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
177 EVP_CIPHER *provciph =
178 EVP_CIPHER_fetch(NULL,
179 cipher->nid == NID_undef ? "NULL"
180 : OBJ_nid2sn(cipher->nid),
183 if (provciph == NULL)
186 EVP_CIPHER_free(ctx->fetched_cipher);
187 ctx->fetched_cipher = provciph;
191 if (cipher->prov != NULL) {
192 if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) {
193 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
196 EVP_CIPHER_free(ctx->fetched_cipher);
197 ctx->fetched_cipher = (EVP_CIPHER *)cipher;
199 ctx->cipher = cipher;
200 if (ctx->algctx == NULL) {
201 ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
202 if (ctx->algctx == NULL) {
203 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
208 if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
210 * If this ctx was already set up for no padding then we need to tell
211 * the new cipher about it.
213 if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
218 if (ctx->cipher->einit == NULL) {
219 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
223 return ctx->cipher->einit(ctx->algctx,
226 : EVP_CIPHER_CTX_get_key_length(ctx),
229 : EVP_CIPHER_CTX_get_iv_length(ctx),
233 if (ctx->cipher->dinit == NULL) {
234 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
238 return ctx->cipher->dinit(ctx->algctx,
241 : EVP_CIPHER_CTX_get_key_length(ctx),
244 : EVP_CIPHER_CTX_get_iv_length(ctx),
247 /* Code below to be removed when legacy support is dropped. */
250 if (cipher != NULL) {
252 * Ensure a context left lying around from last time is cleared (we
253 * previously attempted to avoid this if the same ENGINE and
254 * EVP_CIPHER could be used).
257 unsigned long flags = ctx->flags;
258 EVP_CIPHER_CTX_reset(ctx);
259 /* Restore encrypt and flags */
263 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
265 if (!ENGINE_init(impl)) {
266 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
273 /* There's an ENGINE for this job ... (apparently) */
274 const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
278 * One positive side-effect of US's export control history,
279 * is that we should at least be able to avoid using US
280 * misspellings of "initialisation"?
282 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
285 /* We'll use the ENGINE's private cipher definition */
288 * Store the ENGINE functional reference so we know 'cipher' came
289 * from an ENGINE and we need to release it when done.
297 ctx->cipher = cipher;
298 if (ctx->cipher->ctx_size) {
299 ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
300 if (ctx->cipher_data == NULL) {
302 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
306 ctx->cipher_data = NULL;
308 ctx->key_len = cipher->key_len;
309 /* Preserve wrap enable flag, zero everything else */
310 ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
311 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
312 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL) <= 0) {
314 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
319 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
322 if (ctx->cipher == NULL)
325 /* we assume block size is a power of 2 in *cryptUpdate */
326 OPENSSL_assert(ctx->cipher->block_size == 1
327 || ctx->cipher->block_size == 8
328 || ctx->cipher->block_size == 16);
330 if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
331 && EVP_CIPHER_CTX_get_mode(ctx) == EVP_CIPH_WRAP_MODE) {
332 ERR_raise(ERR_LIB_EVP, EVP_R_WRAP_MODE_NOT_ALLOWED);
336 if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
337 & EVP_CIPH_CUSTOM_IV) == 0) {
338 switch (EVP_CIPHER_CTX_get_mode(ctx)) {
340 case EVP_CIPH_STREAM_CIPHER:
341 case EVP_CIPH_ECB_MODE:
344 case EVP_CIPH_CFB_MODE:
345 case EVP_CIPH_OFB_MODE:
350 case EVP_CIPH_CBC_MODE:
351 n = EVP_CIPHER_CTX_get_iv_length(ctx);
352 if (n < 0 || n > (int)sizeof(ctx->iv)) {
353 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
357 memcpy(ctx->oiv, iv, n);
358 memcpy(ctx->iv, ctx->oiv, n);
361 case EVP_CIPH_CTR_MODE:
363 /* Don't reuse IV for CTR mode */
365 n = EVP_CIPHER_CTX_get_iv_length(ctx);
366 if (n <= 0 || n > (int)sizeof(ctx->iv)) {
367 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
370 memcpy(ctx->iv, iv, n);
379 if (key != NULL || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
380 if (!ctx->cipher->init(ctx, key, iv, enc))
385 ctx->block_mask = ctx->cipher->block_size - 1;
389 int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
390 const unsigned char *key, const unsigned char *iv,
391 int enc, const OSSL_PARAM params[])
393 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, params);
396 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
397 const unsigned char *key, const unsigned char *iv, int enc)
400 EVP_CIPHER_CTX_reset(ctx);
401 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, NULL);
404 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
405 ENGINE *impl, const unsigned char *key,
406 const unsigned char *iv, int enc)
408 return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, NULL);
411 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
412 const unsigned char *in, int inl)
415 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
417 return EVP_DecryptUpdate(ctx, out, outl, in, inl);
420 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
423 return EVP_EncryptFinal_ex(ctx, out, outl);
425 return EVP_DecryptFinal_ex(ctx, out, outl);
428 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
431 return EVP_EncryptFinal(ctx, out, outl);
433 return EVP_DecryptFinal(ctx, out, outl);
436 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
437 const unsigned char *key, const unsigned char *iv)
439 return EVP_CipherInit(ctx, cipher, key, iv, 1);
442 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
443 ENGINE *impl, const unsigned char *key,
444 const unsigned char *iv)
446 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
449 int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
450 const unsigned char *key, const unsigned char *iv,
451 const OSSL_PARAM params[])
453 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params);
456 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
457 const unsigned char *key, const unsigned char *iv)
459 return EVP_CipherInit(ctx, cipher, key, iv, 0);
462 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
463 ENGINE *impl, const unsigned char *key,
464 const unsigned char *iv)
466 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
469 int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
470 const unsigned char *key, const unsigned char *iv,
471 const OSSL_PARAM params[])
473 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 0, params);
477 * According to the letter of standard difference between pointers
478 * is specified to be valid only within same object. This makes
479 * it formally challenging to determine if input and output buffers
480 * are not partially overlapping with standard pointer arithmetic.
485 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
487 * Then we have VMS that distinguishes itself by adhering to
488 * sizeof(size_t)==4 even in 64-bit builds, which means that
489 * difference between two pointers might be truncated to 32 bits.
490 * In the context one can even wonder how comparison for
491 * equality is implemented. To be on the safe side we adhere to
492 * PTRDIFF_T even for comparison for equality.
494 # define PTRDIFF_T uint64_t
496 # define PTRDIFF_T size_t
499 int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
501 PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
503 * Check for partially overlapping buffers. [Binary logical
504 * operations are used instead of boolean to minimize number
505 * of conditional branches.]
507 int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
508 (diff > (0 - (PTRDIFF_T)len)));
513 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
514 unsigned char *out, int *outl,
515 const unsigned char *in, int inl)
517 int i, j, bl, cmpl = inl;
519 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
520 cmpl = (cmpl + 7) / 8;
522 bl = ctx->cipher->block_size;
524 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
525 /* If block size > 1 then the cipher will have to do this check */
526 if (bl == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
527 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
531 i = ctx->cipher->do_cipher(ctx, out, in, inl);
543 if (ossl_is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
544 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
548 if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
549 if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
558 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
561 memcpy(&(ctx->buf[i]), in, inl);
569 * Once we've processed the first j bytes from in, the amount of
570 * data left that is a multiple of the block length is:
571 * (inl - j) & ~(bl - 1)
572 * We must ensure that this amount of data, plus the one block that
573 * we process from ctx->buf does not exceed INT_MAX
575 if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
576 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
579 memcpy(&(ctx->buf[i]), in, j);
582 if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
592 if (!ctx->cipher->do_cipher(ctx, out, in, inl))
598 memcpy(ctx->buf, &(in[inl]), i);
604 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
605 const unsigned char *in, int inl)
608 size_t soutl, inl_ = (size_t)inl;
614 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
618 /* Prevent accidental use of decryption context when encrypting */
620 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
624 if (ctx->cipher == NULL) {
625 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
629 if (ctx->cipher->prov == NULL)
632 blocksize = ctx->cipher->block_size;
634 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
635 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
639 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
640 inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
644 if (soutl > INT_MAX) {
645 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
653 /* Code below to be removed when legacy support is dropped. */
656 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
659 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
662 ret = EVP_EncryptFinal_ex(ctx, out, outl);
666 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
669 unsigned int i, b, bl;
676 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
680 /* Prevent accidental use of decryption context when encrypting */
682 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
686 if (ctx->cipher == NULL) {
687 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
690 if (ctx->cipher->prov == NULL)
693 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
695 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
696 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
700 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
701 blocksize == 1 ? 0 : blocksize);
704 if (soutl > INT_MAX) {
705 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
713 /* Code below to be removed when legacy support is dropped. */
716 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
717 ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
725 b = ctx->cipher->block_size;
726 OPENSSL_assert(b <= sizeof(ctx->buf));
732 if (ctx->flags & EVP_CIPH_NO_PADDING) {
734 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
742 for (i = bl; i < b; i++)
744 ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
752 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
753 const unsigned char *in, int inl)
755 int fix_len, cmpl = inl, ret;
757 size_t soutl, inl_ = (size_t)inl;
763 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
767 /* Prevent accidental use of encryption context when decrypting */
769 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
773 if (ctx->cipher == NULL) {
774 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
777 if (ctx->cipher->prov == NULL)
780 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
782 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
783 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
786 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
787 inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
791 if (soutl > INT_MAX) {
792 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
800 /* Code below to be removed when legacy support is dropped. */
803 b = ctx->cipher->block_size;
805 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
806 cmpl = (cmpl + 7) / 8;
808 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
809 if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
810 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
814 fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
828 if (ctx->flags & EVP_CIPH_NO_PADDING)
829 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
831 OPENSSL_assert(b <= sizeof(ctx->final));
833 if (ctx->final_used) {
834 /* see comment about PTRDIFF_T comparison above */
835 if (((PTRDIFF_T)out == (PTRDIFF_T)in)
836 || ossl_is_partially_overlapping(out, in, b)) {
837 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
841 * final_used is only ever set if buf_len is 0. Therefore the maximum
842 * length output we will ever see from evp_EncryptDecryptUpdate is
843 * the maximum multiple of the block length that is <= inl, or just:
845 * Since final_used has been set then the final output length is:
846 * (inl & ~(b - 1)) + b
847 * This must never exceed INT_MAX
849 if ((inl & ~(b - 1)) > INT_MAX - b) {
850 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
853 memcpy(out, ctx->final, b);
859 if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
863 * if we have 'decrypted' a multiple of block size, make sure we have a
864 * copy of this last block
866 if (b > 1 && !ctx->buf_len) {
869 memcpy(ctx->final, &out[*outl], b);
879 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
882 ret = EVP_DecryptFinal_ex(ctx, out, outl);
886 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
897 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
901 /* Prevent accidental use of encryption context when decrypting */
903 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
907 if (ctx->cipher == NULL) {
908 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
912 if (ctx->cipher->prov == NULL)
915 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
917 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
918 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
922 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
923 blocksize == 1 ? 0 : blocksize);
926 if (soutl > INT_MAX) {
927 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
935 /* Code below to be removed when legacy support is dropped. */
939 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
940 i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
948 b = ctx->cipher->block_size;
949 if (ctx->flags & EVP_CIPH_NO_PADDING) {
951 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
958 if (ctx->buf_len || !ctx->final_used) {
959 ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
962 OPENSSL_assert(b <= sizeof(ctx->final));
965 * The following assumes that the ciphertext has been authenticated.
966 * Otherwise it provides a padding oracle.
968 n = ctx->final[b - 1];
969 if (n == 0 || n > (int)b) {
970 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
973 for (i = 0; i < n; i++) {
974 if (ctx->final[--b] != n) {
975 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
979 n = ctx->cipher->block_size - n;
980 for (i = 0; i < n; i++)
981 out[i] = ctx->final[i];
988 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
990 if (c->cipher->prov != NULL) {
992 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
995 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
998 /* Check the cipher actually understands this parameter */
999 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
1000 OSSL_CIPHER_PARAM_KEYLEN) == NULL) {
1001 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1005 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
1006 ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params);
1008 return ok > 0 ? 1 : 0;
1011 /* Code below to be removed when legacy support is dropped. */
1014 * Note there have never been any built-in ciphers that define this flag
1015 * since it was first introduced.
1017 if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
1018 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
1019 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
1021 if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
1022 c->key_len = keylen;
1025 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1029 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
1032 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1033 unsigned int pd = pad;
1036 ctx->flags &= ~EVP_CIPH_NO_PADDING;
1038 ctx->flags |= EVP_CIPH_NO_PADDING;
1040 if (ctx->cipher != NULL && ctx->cipher->prov == NULL)
1042 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
1043 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1048 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1050 int ret = EVP_CTRL_RET_UNSUPPORTED;
1054 OSSL_PARAM params[4] = {
1055 OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
1058 if (ctx == NULL || ctx->cipher == NULL) {
1059 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
1063 if (ctx->cipher->prov == NULL)
1067 case EVP_CTRL_SET_KEY_LENGTH:
1068 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
1070 case EVP_CTRL_RAND_KEY: /* Used by DES */
1073 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
1079 * EVP_CTRL_INIT is purely legacy, no provider counterpart.
1080 * As a matter of fact, this should be dead code, but some caller
1081 * might still do a direct control call with this command, so...
1082 * Legacy methods return 1 except for exceptional circumstances, so
1083 * we do the same here to not be disruptive.
1086 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
1089 case EVP_CTRL_AEAD_SET_IVLEN:
1092 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1095 case EVP_CTRL_CCM_SET_L:
1096 if (arg < 2 || arg > 8)
1099 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1102 case EVP_CTRL_AEAD_SET_IV_FIXED:
1103 params[0] = OSSL_PARAM_construct_octet_string(
1104 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
1106 case EVP_CTRL_GCM_IV_GEN:
1109 sz = 0; /* special case that uses the iv length */
1110 params[0] = OSSL_PARAM_construct_octet_string(
1111 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
1113 case EVP_CTRL_GCM_SET_IV_INV:
1116 params[0] = OSSL_PARAM_construct_octet_string(
1117 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
1119 case EVP_CTRL_GET_RC5_ROUNDS:
1120 set_params = 0; /* Fall thru */
1121 case EVP_CTRL_SET_RC5_ROUNDS:
1124 i = (unsigned int)arg;
1125 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
1127 case EVP_CTRL_SET_SPEED:
1130 i = (unsigned int)arg;
1131 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
1133 case EVP_CTRL_AEAD_GET_TAG:
1134 set_params = 0; /* Fall thru */
1135 case EVP_CTRL_AEAD_SET_TAG:
1136 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1139 case EVP_CTRL_AEAD_TLS1_AAD:
1140 /* This one does a set and a get - since it returns a size */
1142 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1144 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1148 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1149 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1153 #ifndef OPENSSL_NO_RC2
1154 case EVP_CTRL_GET_RC2_KEY_BITS:
1155 set_params = 0; /* Fall thru */
1156 case EVP_CTRL_SET_RC2_KEY_BITS:
1157 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
1159 #endif /* OPENSSL_NO_RC2 */
1160 #if !defined(OPENSSL_NO_MULTIBLOCK)
1161 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
1162 params[0] = OSSL_PARAM_construct_size_t(
1163 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
1164 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1168 params[0] = OSSL_PARAM_construct_size_t(
1169 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
1170 params[1] = OSSL_PARAM_construct_end();
1171 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1175 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
1176 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1177 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1179 if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
1182 params[0] = OSSL_PARAM_construct_octet_string(
1183 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void*)p->inp, p->len);
1184 params[1] = OSSL_PARAM_construct_uint(
1185 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1186 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1189 /* Retrieve the return values changed by the set */
1190 params[0] = OSSL_PARAM_construct_size_t(
1191 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
1192 params[1] = OSSL_PARAM_construct_uint(
1193 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1194 params[2] = OSSL_PARAM_construct_end();
1195 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1200 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
1201 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1202 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1204 params[0] = OSSL_PARAM_construct_octet_string(
1205 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
1207 params[1] = OSSL_PARAM_construct_octet_string(
1208 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void*)p->inp,
1210 params[2] = 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 params[0] = OSSL_PARAM_construct_size_t(
1216 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
1217 params[1] = OSSL_PARAM_construct_end();
1218 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1223 #endif /* OPENSSL_NO_MULTIBLOCK */
1224 case EVP_CTRL_AEAD_SET_MAC_KEY:
1227 params[0] = OSSL_PARAM_construct_octet_string(
1228 OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
1233 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1235 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1238 /* Code below to be removed when legacy support is dropped. */
1240 if (ctx->cipher->ctrl == NULL) {
1241 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
1245 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
1248 if (ret == EVP_CTRL_RET_UNSUPPORTED) {
1249 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
1255 int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1257 if (cipher != NULL && cipher->get_params != NULL)
1258 return cipher->get_params(params);
1262 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1264 if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) {
1266 return ctx->cipher->set_ctx_params(ctx->algctx, params);
1271 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1273 if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1274 return ctx->cipher->get_ctx_params(ctx->algctx, params);
1278 const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1280 if (cipher != NULL && cipher->gettable_params != NULL)
1281 return cipher->gettable_params(
1282 ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)));
1286 const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
1290 if (cipher != NULL && cipher->settable_ctx_params != NULL) {
1291 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1292 return cipher->settable_ctx_params(NULL, provctx);
1297 const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
1301 if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
1302 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1303 return cipher->gettable_ctx_params(NULL, provctx);
1308 const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx)
1312 if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
1313 alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1314 return cctx->cipher->settable_ctx_params(cctx->algctx, alg);
1319 const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx)
1323 if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
1324 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1325 return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx);
1331 static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx)
1333 const EVP_CIPHER *cipher = ctx->cipher;
1334 const OSSL_PROVIDER *prov;
1339 prov = EVP_CIPHER_get0_provider(cipher);
1340 return ossl_provider_libctx(prov);
1344 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
1346 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1347 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1354 OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
1356 kl = EVP_CIPHER_CTX_get_key_length(ctx);
1357 if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0)
1361 #endif /* FIPS_MODULE */
1364 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
1366 if ((in == NULL) || (in->cipher == NULL)) {
1367 ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
1371 if (in->cipher->prov == NULL)
1374 if (in->cipher->dupctx == NULL) {
1375 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1379 EVP_CIPHER_CTX_reset(out);
1384 if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
1385 out->fetched_cipher = NULL;
1389 out->algctx = in->cipher->dupctx(in->algctx);
1390 if (out->algctx == NULL) {
1391 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1397 /* Code below to be removed when legacy support is dropped. */
1400 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1401 /* Make sure it's safe to copy a cipher context using an ENGINE */
1402 if (in->engine && !ENGINE_init(in->engine)) {
1403 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
1408 EVP_CIPHER_CTX_reset(out);
1409 memcpy(out, in, sizeof(*out));
1411 if (in->cipher_data && in->cipher->ctx_size) {
1412 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
1413 if (out->cipher_data == NULL) {
1415 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1418 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
1421 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
1422 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
1424 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
1430 EVP_CIPHER *evp_cipher_new(void)
1432 EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1434 if (cipher != NULL) {
1435 cipher->lock = CRYPTO_THREAD_lock_new();
1436 if (cipher->lock == NULL) {
1437 OPENSSL_free(cipher);
1446 * FIPS module note: since internal fetches will be entirely
1447 * provider based, we know that none of its code depends on legacy
1448 * NIDs or any functionality that use them.
1451 /* After removal of legacy support get rid of the need for legacy NIDs */
1452 static void set_legacy_nid(const char *name, void *vlegacy_nid)
1455 int *legacy_nid = vlegacy_nid;
1457 * We use lowest level function to get the associated method, because
1458 * higher level functions such as EVP_get_cipherbyname() have changed
1459 * to look at providers too.
1461 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
1463 if (*legacy_nid == -1) /* We found a clash already */
1465 if (legacy_method == NULL)
1467 nid = EVP_CIPHER_get_nid(legacy_method);
1468 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
1476 static void *evp_cipher_from_algorithm(const int name_id,
1477 const OSSL_ALGORITHM *algodef,
1478 OSSL_PROVIDER *prov)
1480 const OSSL_DISPATCH *fns = algodef->implementation;
1481 EVP_CIPHER *cipher = NULL;
1482 int fnciphcnt = 0, fnctxcnt = 0;
1484 if ((cipher = evp_cipher_new()) == NULL) {
1485 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1490 cipher->nid = NID_undef;
1491 if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
1492 || cipher->nid == -1) {
1493 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1494 EVP_CIPHER_free(cipher);
1499 cipher->name_id = name_id;
1500 if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
1501 EVP_CIPHER_free(cipher);
1504 cipher->description = algodef->algorithm_description;
1506 for (; fns->function_id != 0; fns++) {
1507 switch (fns->function_id) {
1508 case OSSL_FUNC_CIPHER_NEWCTX:
1509 if (cipher->newctx != NULL)
1511 cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
1514 case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1515 if (cipher->einit != NULL)
1517 cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
1520 case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1521 if (cipher->dinit != NULL)
1523 cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
1526 case OSSL_FUNC_CIPHER_UPDATE:
1527 if (cipher->cupdate != NULL)
1529 cipher->cupdate = OSSL_FUNC_cipher_update(fns);
1532 case OSSL_FUNC_CIPHER_FINAL:
1533 if (cipher->cfinal != NULL)
1535 cipher->cfinal = OSSL_FUNC_cipher_final(fns);
1538 case OSSL_FUNC_CIPHER_CIPHER:
1539 if (cipher->ccipher != NULL)
1541 cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
1543 case OSSL_FUNC_CIPHER_FREECTX:
1544 if (cipher->freectx != NULL)
1546 cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
1549 case OSSL_FUNC_CIPHER_DUPCTX:
1550 if (cipher->dupctx != NULL)
1552 cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
1554 case OSSL_FUNC_CIPHER_GET_PARAMS:
1555 if (cipher->get_params != NULL)
1557 cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
1559 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1560 if (cipher->get_ctx_params != NULL)
1562 cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
1564 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1565 if (cipher->set_ctx_params != NULL)
1567 cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
1569 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1570 if (cipher->gettable_params != NULL)
1572 cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
1574 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1575 if (cipher->gettable_ctx_params != NULL)
1577 cipher->gettable_ctx_params =
1578 OSSL_FUNC_cipher_gettable_ctx_params(fns);
1580 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1581 if (cipher->settable_ctx_params != NULL)
1583 cipher->settable_ctx_params =
1584 OSSL_FUNC_cipher_settable_ctx_params(fns);
1588 if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1589 || (fnciphcnt == 0 && cipher->ccipher == NULL)
1592 * In order to be a consistent set of functions we must have at least
1593 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1594 * functions, or a single "cipher" function. In all cases we need both
1595 * the "newctx" and "freectx" functions.
1597 EVP_CIPHER_free(cipher);
1598 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1601 cipher->prov = prov;
1603 ossl_provider_up_ref(prov);
1605 if (!evp_cipher_cache_constants(cipher)) {
1606 EVP_CIPHER_free(cipher);
1607 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1614 static int evp_cipher_up_ref(void *cipher)
1616 return EVP_CIPHER_up_ref(cipher);
1619 static void evp_cipher_free(void *cipher)
1621 EVP_CIPHER_free(cipher);
1624 EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1625 const char *properties)
1627 EVP_CIPHER *cipher =
1628 evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1629 evp_cipher_from_algorithm, evp_cipher_up_ref,
1635 int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
1639 if (cipher->origin == EVP_ORIG_DYNAMIC)
1640 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
1644 void evp_cipher_free_int(EVP_CIPHER *cipher)
1646 OPENSSL_free(cipher->type_name);
1647 ossl_provider_free(cipher->prov);
1648 CRYPTO_THREAD_lock_free(cipher->lock);
1649 OPENSSL_free(cipher);
1652 void EVP_CIPHER_free(EVP_CIPHER *cipher)
1656 if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC)
1659 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
1662 evp_cipher_free_int(cipher);
1665 void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
1666 void (*fn)(EVP_CIPHER *mac, void *arg),
1669 evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1670 (void (*)(void *, void *))fn, arg,
1671 evp_cipher_from_algorithm, evp_cipher_up_ref,