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 ctx->fetched_cipher = (EVP_CIPHER *)cipher;
207 ctx->cipher = cipher;
208 if (ctx->algctx == NULL) {
209 ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
210 if (ctx->algctx == NULL) {
211 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
216 if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
218 * If this ctx was already set up for no padding then we need to tell
219 * the new cipher about it.
221 if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
226 if (ctx->cipher->einit == NULL) {
227 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
231 return ctx->cipher->einit(ctx->algctx,
234 : EVP_CIPHER_CTX_get_key_length(ctx),
237 : EVP_CIPHER_CTX_get_iv_length(ctx),
241 if (ctx->cipher->dinit == NULL) {
242 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
246 return ctx->cipher->dinit(ctx->algctx,
249 : EVP_CIPHER_CTX_get_key_length(ctx),
252 : EVP_CIPHER_CTX_get_iv_length(ctx),
255 /* Code below to be removed when legacy support is dropped. */
258 if (cipher != NULL) {
260 * Ensure a context left lying around from last time is cleared (we
261 * previously attempted to avoid this if the same ENGINE and
262 * EVP_CIPHER could be used).
265 unsigned long flags = ctx->flags;
266 EVP_CIPHER_CTX_reset(ctx);
267 /* Restore encrypt and flags */
271 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
273 if (!ENGINE_init(impl)) {
274 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
281 /* There's an ENGINE for this job ... (apparently) */
282 const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
286 * One positive side-effect of US's export control history,
287 * is that we should at least be able to avoid using US
288 * misspellings of "initialisation"?
290 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
293 /* We'll use the ENGINE's private cipher definition */
296 * Store the ENGINE functional reference so we know 'cipher' came
297 * from an ENGINE and we need to release it when done.
305 ctx->cipher = cipher;
306 if (ctx->cipher->ctx_size) {
307 ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
308 if (ctx->cipher_data == NULL) {
310 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
314 ctx->cipher_data = NULL;
316 ctx->key_len = cipher->key_len;
317 /* Preserve wrap enable flag, zero everything else */
318 ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
319 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
320 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
322 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
327 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
330 if (ctx->cipher == NULL)
333 /* we assume block size is a power of 2 in *cryptUpdate */
334 OPENSSL_assert(ctx->cipher->block_size == 1
335 || ctx->cipher->block_size == 8
336 || ctx->cipher->block_size == 16);
338 if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
339 && EVP_CIPHER_CTX_get_mode(ctx) == EVP_CIPH_WRAP_MODE) {
340 ERR_raise(ERR_LIB_EVP, EVP_R_WRAP_MODE_NOT_ALLOWED);
344 if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
345 & EVP_CIPH_CUSTOM_IV) == 0) {
346 switch (EVP_CIPHER_CTX_get_mode(ctx)) {
348 case EVP_CIPH_STREAM_CIPHER:
349 case EVP_CIPH_ECB_MODE:
352 case EVP_CIPH_CFB_MODE:
353 case EVP_CIPH_OFB_MODE:
358 case EVP_CIPH_CBC_MODE:
359 n = EVP_CIPHER_CTX_get_iv_length(ctx);
360 if (n < 0 || n > (int)sizeof(ctx->iv)) {
361 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
365 memcpy(ctx->oiv, iv, n);
366 memcpy(ctx->iv, ctx->oiv, n);
369 case EVP_CIPH_CTR_MODE:
371 /* Don't reuse IV for CTR mode */
373 n = EVP_CIPHER_CTX_get_iv_length(ctx);
374 if (n <= 0 || n > (int)sizeof(ctx->iv)) {
375 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_IV_LENGTH);
378 memcpy(ctx->iv, iv, n);
387 if (key != NULL || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
388 if (!ctx->cipher->init(ctx, key, iv, enc))
393 ctx->block_mask = ctx->cipher->block_size - 1;
397 int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
398 const unsigned char *key, const unsigned char *iv,
399 int enc, const OSSL_PARAM params[])
401 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, params);
404 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
405 const unsigned char *key, const unsigned char *iv, int enc)
408 EVP_CIPHER_CTX_reset(ctx);
409 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, NULL);
412 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
413 ENGINE *impl, const unsigned char *key,
414 const unsigned char *iv, int enc)
416 return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, NULL);
419 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
420 const unsigned char *in, int inl)
423 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
425 return EVP_DecryptUpdate(ctx, out, outl, in, inl);
428 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
431 return EVP_EncryptFinal_ex(ctx, out, outl);
433 return EVP_DecryptFinal_ex(ctx, out, outl);
436 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
439 return EVP_EncryptFinal(ctx, out, outl);
441 return EVP_DecryptFinal(ctx, out, outl);
444 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
445 const unsigned char *key, const unsigned char *iv)
447 return EVP_CipherInit(ctx, cipher, key, iv, 1);
450 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
451 ENGINE *impl, const unsigned char *key,
452 const unsigned char *iv)
454 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
457 int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
458 const unsigned char *key, const unsigned char *iv,
459 const OSSL_PARAM params[])
461 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params);
464 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
465 const unsigned char *key, const unsigned char *iv)
467 return EVP_CipherInit(ctx, cipher, key, iv, 0);
470 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
471 ENGINE *impl, const unsigned char *key,
472 const unsigned char *iv)
474 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
477 int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
478 const unsigned char *key, const unsigned char *iv,
479 const OSSL_PARAM params[])
481 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 0, params);
485 * According to the letter of standard difference between pointers
486 * is specified to be valid only within same object. This makes
487 * it formally challenging to determine if input and output buffers
488 * are not partially overlapping with standard pointer arithmetic.
493 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
495 * Then we have VMS that distinguishes itself by adhering to
496 * sizeof(size_t)==4 even in 64-bit builds, which means that
497 * difference between two pointers might be truncated to 32 bits.
498 * In the context one can even wonder how comparison for
499 * equality is implemented. To be on the safe side we adhere to
500 * PTRDIFF_T even for comparison for equality.
502 # define PTRDIFF_T uint64_t
504 # define PTRDIFF_T size_t
507 int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
509 PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
511 * Check for partially overlapping buffers. [Binary logical
512 * operations are used instead of boolean to minimize number
513 * of conditional branches.]
515 int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
516 (diff > (0 - (PTRDIFF_T)len)));
521 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
522 unsigned char *out, int *outl,
523 const unsigned char *in, int inl)
525 int i, j, bl, cmpl = inl;
527 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
528 cmpl = safe_div_round_up_int(cmpl, 8, NULL);
530 bl = ctx->cipher->block_size;
532 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
533 /* If block size > 1 then the cipher will have to do this check */
534 if (bl == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
535 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
539 i = ctx->cipher->do_cipher(ctx, out, in, inl);
551 if (ossl_is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
552 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
556 if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
557 if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
566 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
569 memcpy(&(ctx->buf[i]), in, inl);
577 * Once we've processed the first j bytes from in, the amount of
578 * data left that is a multiple of the block length is:
579 * (inl - j) & ~(bl - 1)
580 * We must ensure that this amount of data, plus the one block that
581 * we process from ctx->buf does not exceed INT_MAX
583 if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
584 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
587 memcpy(&(ctx->buf[i]), in, j);
590 if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
600 if (!ctx->cipher->do_cipher(ctx, out, in, inl))
606 memcpy(ctx->buf, &(in[inl]), i);
612 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
613 const unsigned char *in, int inl)
616 size_t soutl, inl_ = (size_t)inl;
622 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
626 /* Prevent accidental use of decryption context when encrypting */
628 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
632 if (ctx->cipher == NULL) {
633 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
637 if (ctx->cipher->prov == NULL)
640 blocksize = ctx->cipher->block_size;
642 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
643 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
647 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
648 inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
652 if (soutl > INT_MAX) {
653 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
661 /* Code below to be removed when legacy support is dropped. */
664 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
667 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
670 ret = EVP_EncryptFinal_ex(ctx, out, outl);
674 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
677 unsigned int i, b, bl;
684 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
688 /* Prevent accidental use of decryption context when encrypting */
690 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
694 if (ctx->cipher == NULL) {
695 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
698 if (ctx->cipher->prov == NULL)
701 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
703 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
704 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
708 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
709 blocksize == 1 ? 0 : blocksize);
712 if (soutl > INT_MAX) {
713 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
721 /* Code below to be removed when legacy support is dropped. */
724 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
725 ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
733 b = ctx->cipher->block_size;
734 OPENSSL_assert(b <= sizeof(ctx->buf));
740 if (ctx->flags & EVP_CIPH_NO_PADDING) {
742 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
750 for (i = bl; i < b; i++)
752 ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
760 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
761 const unsigned char *in, int inl)
763 int fix_len, cmpl = inl, ret;
765 size_t soutl, inl_ = (size_t)inl;
771 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
775 /* Prevent accidental use of encryption context when decrypting */
777 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
781 if (ctx->cipher == NULL) {
782 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
785 if (ctx->cipher->prov == NULL)
788 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
790 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
791 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
794 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
795 inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
799 if (soutl > INT_MAX) {
800 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
808 /* Code below to be removed when legacy support is dropped. */
811 b = ctx->cipher->block_size;
813 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
814 cmpl = safe_div_round_up_int(cmpl, 8, NULL);
816 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
817 if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
818 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
822 fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
836 if (ctx->flags & EVP_CIPH_NO_PADDING)
837 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
839 OPENSSL_assert(b <= sizeof(ctx->final));
841 if (ctx->final_used) {
842 /* see comment about PTRDIFF_T comparison above */
843 if (((PTRDIFF_T)out == (PTRDIFF_T)in)
844 || ossl_is_partially_overlapping(out, in, b)) {
845 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
849 * final_used is only ever set if buf_len is 0. Therefore the maximum
850 * length output we will ever see from evp_EncryptDecryptUpdate is
851 * the maximum multiple of the block length that is <= inl, or just:
853 * Since final_used has been set then the final output length is:
854 * (inl & ~(b - 1)) + b
855 * This must never exceed INT_MAX
857 if ((inl & ~(b - 1)) > INT_MAX - b) {
858 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
861 memcpy(out, ctx->final, b);
867 if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
871 * if we have 'decrypted' a multiple of block size, make sure we have a
872 * copy of this last block
874 if (b > 1 && !ctx->buf_len) {
877 memcpy(ctx->final, &out[*outl], b);
887 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
890 ret = EVP_DecryptFinal_ex(ctx, out, outl);
894 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
905 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
909 /* Prevent accidental use of encryption context when decrypting */
911 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
915 if (ctx->cipher == NULL) {
916 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
920 if (ctx->cipher->prov == NULL)
923 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
925 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
926 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
930 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
931 blocksize == 1 ? 0 : blocksize);
934 if (soutl > INT_MAX) {
935 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
943 /* Code below to be removed when legacy support is dropped. */
947 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
948 i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
956 b = ctx->cipher->block_size;
957 if (ctx->flags & EVP_CIPH_NO_PADDING) {
959 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
966 if (ctx->buf_len || !ctx->final_used) {
967 ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
970 OPENSSL_assert(b <= sizeof(ctx->final));
973 * The following assumes that the ciphertext has been authenticated.
974 * Otherwise it provides a padding oracle.
976 n = ctx->final[b - 1];
977 if (n == 0 || n > (int)b) {
978 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
981 for (i = 0; i < n; i++) {
982 if (ctx->final[--b] != n) {
983 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
987 n = ctx->cipher->block_size - n;
988 for (i = 0; i < n; i++)
989 out[i] = ctx->final[i];
996 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
998 if (c->cipher->prov != NULL) {
1000 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1003 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
1006 /* Check the cipher actually understands this parameter */
1007 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
1008 OSSL_CIPHER_PARAM_KEYLEN) == NULL) {
1009 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1013 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
1014 if (!OSSL_PARAM_set_int(params, keylen))
1016 ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params);
1019 c->key_len = keylen;
1023 /* Code below to be removed when legacy support is dropped. */
1026 * Note there have never been any built-in ciphers that define this flag
1027 * since it was first introduced.
1029 if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
1030 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
1031 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
1033 if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
1034 c->key_len = keylen;
1037 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1041 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
1044 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1045 unsigned int pd = pad;
1048 ctx->flags &= ~EVP_CIPH_NO_PADDING;
1050 ctx->flags |= EVP_CIPH_NO_PADDING;
1052 if (ctx->cipher != NULL && ctx->cipher->prov == NULL)
1054 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
1055 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1060 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1062 int ret = EVP_CTRL_RET_UNSUPPORTED;
1066 OSSL_PARAM params[4] = {
1067 OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
1070 if (ctx == NULL || ctx->cipher == NULL) {
1071 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
1075 if (ctx->cipher->prov == NULL)
1079 case EVP_CTRL_SET_KEY_LENGTH:
1080 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
1083 case EVP_CTRL_RAND_KEY: /* Used by DES */
1086 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
1092 * EVP_CTRL_INIT is purely legacy, no provider counterpart.
1093 * As a matter of fact, this should be dead code, but some caller
1094 * might still do a direct control call with this command, so...
1095 * Legacy methods return 1 except for exceptional circumstances, so
1096 * we do the same here to not be disruptive.
1099 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
1102 case EVP_CTRL_AEAD_SET_IVLEN:
1105 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1108 case EVP_CTRL_CCM_SET_L:
1109 if (arg < 2 || arg > 8)
1112 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1115 case EVP_CTRL_AEAD_SET_IV_FIXED:
1116 params[0] = OSSL_PARAM_construct_octet_string(
1117 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
1119 case EVP_CTRL_GCM_IV_GEN:
1122 sz = 0; /* special case that uses the iv length */
1123 params[0] = OSSL_PARAM_construct_octet_string(
1124 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
1126 case EVP_CTRL_GCM_SET_IV_INV:
1129 params[0] = OSSL_PARAM_construct_octet_string(
1130 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
1132 case EVP_CTRL_GET_RC5_ROUNDS:
1133 set_params = 0; /* Fall thru */
1134 case EVP_CTRL_SET_RC5_ROUNDS:
1137 i = (unsigned int)arg;
1138 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
1140 case EVP_CTRL_SET_SPEED:
1143 i = (unsigned int)arg;
1144 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
1146 case EVP_CTRL_AEAD_GET_TAG:
1147 set_params = 0; /* Fall thru */
1148 case EVP_CTRL_AEAD_SET_TAG:
1149 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1152 case EVP_CTRL_AEAD_TLS1_AAD:
1153 /* This one does a set and a get - since it returns a size */
1155 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1157 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1161 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1162 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1166 #ifndef OPENSSL_NO_RC2
1167 case EVP_CTRL_GET_RC2_KEY_BITS:
1168 set_params = 0; /* Fall thru */
1169 case EVP_CTRL_SET_RC2_KEY_BITS:
1170 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
1172 #endif /* OPENSSL_NO_RC2 */
1173 #if !defined(OPENSSL_NO_MULTIBLOCK)
1174 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
1175 params[0] = OSSL_PARAM_construct_size_t(
1176 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
1177 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1181 params[0] = OSSL_PARAM_construct_size_t(
1182 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
1183 params[1] = OSSL_PARAM_construct_end();
1184 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1188 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
1189 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1190 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1192 if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
1195 params[0] = OSSL_PARAM_construct_octet_string(
1196 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void*)p->inp, p->len);
1197 params[1] = OSSL_PARAM_construct_uint(
1198 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1199 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1202 /* Retrieve the return values changed by the set */
1203 params[0] = OSSL_PARAM_construct_size_t(
1204 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
1205 params[1] = OSSL_PARAM_construct_uint(
1206 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1207 params[2] = OSSL_PARAM_construct_end();
1208 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1213 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
1214 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1215 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1217 params[0] = OSSL_PARAM_construct_octet_string(
1218 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
1220 params[1] = OSSL_PARAM_construct_octet_string(
1221 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void*)p->inp,
1223 params[2] = OSSL_PARAM_construct_uint(
1224 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1225 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1228 params[0] = OSSL_PARAM_construct_size_t(
1229 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
1230 params[1] = OSSL_PARAM_construct_end();
1231 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1236 #endif /* OPENSSL_NO_MULTIBLOCK */
1237 case EVP_CTRL_AEAD_SET_MAC_KEY:
1240 params[0] = OSSL_PARAM_construct_octet_string(
1241 OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
1246 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1248 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1251 /* Code below to be removed when legacy support is dropped. */
1253 if (ctx->cipher->ctrl == NULL) {
1254 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
1258 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
1261 if (ret == EVP_CTRL_RET_UNSUPPORTED) {
1262 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
1268 int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1270 if (cipher != NULL && cipher->get_params != NULL)
1271 return cipher->get_params(params);
1275 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1278 const OSSL_PARAM *p;
1280 if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) {
1281 r = ctx->cipher->set_ctx_params(ctx->algctx, params);
1283 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
1284 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->key_len)) {
1290 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
1291 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->iv_len)) {
1300 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1302 if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1303 return ctx->cipher->get_ctx_params(ctx->algctx, params);
1307 const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1309 if (cipher != NULL && cipher->gettable_params != NULL)
1310 return cipher->gettable_params(
1311 ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)));
1315 const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
1319 if (cipher != NULL && cipher->settable_ctx_params != NULL) {
1320 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1321 return cipher->settable_ctx_params(NULL, provctx);
1326 const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
1330 if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
1331 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1332 return cipher->gettable_ctx_params(NULL, provctx);
1337 const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx)
1341 if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
1342 alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1343 return cctx->cipher->settable_ctx_params(cctx->algctx, alg);
1348 const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx)
1352 if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
1353 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1354 return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx);
1360 static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx)
1362 const EVP_CIPHER *cipher = ctx->cipher;
1363 const OSSL_PROVIDER *prov;
1368 prov = EVP_CIPHER_get0_provider(cipher);
1369 return ossl_provider_libctx(prov);
1373 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
1375 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1376 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1383 OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
1385 kl = EVP_CIPHER_CTX_get_key_length(ctx);
1386 if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0)
1390 #endif /* FIPS_MODULE */
1393 EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in)
1395 EVP_CIPHER_CTX *out = EVP_CIPHER_CTX_new();
1397 if (out != NULL && !EVP_CIPHER_CTX_copy(out, in)) {
1398 EVP_CIPHER_CTX_free(out);
1404 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
1406 if ((in == NULL) || (in->cipher == NULL)) {
1407 ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
1411 if (in->cipher->prov == NULL)
1414 if (in->cipher->dupctx == NULL) {
1415 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1419 EVP_CIPHER_CTX_reset(out);
1424 if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
1425 out->fetched_cipher = NULL;
1429 out->algctx = in->cipher->dupctx(in->algctx);
1430 if (out->algctx == NULL) {
1431 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1437 /* Code below to be removed when legacy support is dropped. */
1440 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1441 /* Make sure it's safe to copy a cipher context using an ENGINE */
1442 if (in->engine && !ENGINE_init(in->engine)) {
1443 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
1448 EVP_CIPHER_CTX_reset(out);
1449 memcpy(out, in, sizeof(*out));
1451 if (in->cipher_data && in->cipher->ctx_size) {
1452 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
1453 if (out->cipher_data == NULL) {
1455 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1458 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
1461 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
1462 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
1464 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
1470 EVP_CIPHER *evp_cipher_new(void)
1472 EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1474 if (cipher != NULL) {
1475 cipher->lock = CRYPTO_THREAD_lock_new();
1476 if (cipher->lock == NULL) {
1477 OPENSSL_free(cipher);
1486 * FIPS module note: since internal fetches will be entirely
1487 * provider based, we know that none of its code depends on legacy
1488 * NIDs or any functionality that use them.
1491 /* After removal of legacy support get rid of the need for legacy NIDs */
1492 static void set_legacy_nid(const char *name, void *vlegacy_nid)
1495 int *legacy_nid = vlegacy_nid;
1497 * We use lowest level function to get the associated method, because
1498 * higher level functions such as EVP_get_cipherbyname() have changed
1499 * to look at providers too.
1501 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
1503 if (*legacy_nid == -1) /* We found a clash already */
1505 if (legacy_method == NULL)
1507 nid = EVP_CIPHER_get_nid(legacy_method);
1508 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
1516 static void *evp_cipher_from_algorithm(const int name_id,
1517 const OSSL_ALGORITHM *algodef,
1518 OSSL_PROVIDER *prov)
1520 const OSSL_DISPATCH *fns = algodef->implementation;
1521 EVP_CIPHER *cipher = NULL;
1522 int fnciphcnt = 0, fnctxcnt = 0;
1524 if ((cipher = evp_cipher_new()) == NULL) {
1525 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1530 cipher->nid = NID_undef;
1531 if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
1532 || cipher->nid == -1) {
1533 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1534 EVP_CIPHER_free(cipher);
1539 cipher->name_id = name_id;
1540 if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
1541 EVP_CIPHER_free(cipher);
1544 cipher->description = algodef->algorithm_description;
1546 for (; fns->function_id != 0; fns++) {
1547 switch (fns->function_id) {
1548 case OSSL_FUNC_CIPHER_NEWCTX:
1549 if (cipher->newctx != NULL)
1551 cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
1554 case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1555 if (cipher->einit != NULL)
1557 cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
1560 case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1561 if (cipher->dinit != NULL)
1563 cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
1566 case OSSL_FUNC_CIPHER_UPDATE:
1567 if (cipher->cupdate != NULL)
1569 cipher->cupdate = OSSL_FUNC_cipher_update(fns);
1572 case OSSL_FUNC_CIPHER_FINAL:
1573 if (cipher->cfinal != NULL)
1575 cipher->cfinal = OSSL_FUNC_cipher_final(fns);
1578 case OSSL_FUNC_CIPHER_CIPHER:
1579 if (cipher->ccipher != NULL)
1581 cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
1583 case OSSL_FUNC_CIPHER_FREECTX:
1584 if (cipher->freectx != NULL)
1586 cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
1589 case OSSL_FUNC_CIPHER_DUPCTX:
1590 if (cipher->dupctx != NULL)
1592 cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
1594 case OSSL_FUNC_CIPHER_GET_PARAMS:
1595 if (cipher->get_params != NULL)
1597 cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
1599 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1600 if (cipher->get_ctx_params != NULL)
1602 cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
1604 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1605 if (cipher->set_ctx_params != NULL)
1607 cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
1609 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1610 if (cipher->gettable_params != NULL)
1612 cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
1614 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1615 if (cipher->gettable_ctx_params != NULL)
1617 cipher->gettable_ctx_params =
1618 OSSL_FUNC_cipher_gettable_ctx_params(fns);
1620 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1621 if (cipher->settable_ctx_params != NULL)
1623 cipher->settable_ctx_params =
1624 OSSL_FUNC_cipher_settable_ctx_params(fns);
1628 if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1629 || (fnciphcnt == 0 && cipher->ccipher == NULL)
1632 * In order to be a consistent set of functions we must have at least
1633 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1634 * functions, or a single "cipher" function. In all cases we need both
1635 * the "newctx" and "freectx" functions.
1637 EVP_CIPHER_free(cipher);
1638 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1641 cipher->prov = prov;
1643 ossl_provider_up_ref(prov);
1645 if (!evp_cipher_cache_constants(cipher)) {
1646 EVP_CIPHER_free(cipher);
1647 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1654 static int evp_cipher_up_ref(void *cipher)
1656 return EVP_CIPHER_up_ref(cipher);
1659 static void evp_cipher_free(void *cipher)
1661 EVP_CIPHER_free(cipher);
1664 EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1665 const char *properties)
1667 EVP_CIPHER *cipher =
1668 evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1669 evp_cipher_from_algorithm, evp_cipher_up_ref,
1675 int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
1679 if (cipher->origin == EVP_ORIG_DYNAMIC)
1680 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
1684 void evp_cipher_free_int(EVP_CIPHER *cipher)
1686 OPENSSL_free(cipher->type_name);
1687 ossl_provider_free(cipher->prov);
1688 CRYPTO_THREAD_lock_free(cipher->lock);
1689 OPENSSL_free(cipher);
1692 void EVP_CIPHER_free(EVP_CIPHER *cipher)
1696 if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC)
1699 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
1702 evp_cipher_free_int(cipher);
1705 void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
1706 void (*fn)(EVP_CIPHER *mac, void *arg),
1709 evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1710 (void (*)(void *, void *))fn, arg,
1711 evp_cipher_from_algorithm, evp_cipher_up_ref,