2 * Copyright 1995-2021 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 (!ossl_assert(n >= 0 && n <= (int)sizeof(ctx->iv)))
363 memcpy(ctx->oiv, iv, n);
364 memcpy(ctx->iv, ctx->oiv, n);
367 case EVP_CIPH_CTR_MODE:
369 /* Don't reuse IV for CTR mode */
371 if ((n = EVP_CIPHER_CTX_get_iv_length(ctx)) <= 0)
373 memcpy(ctx->iv, iv, n);
382 if (key != NULL || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
383 if (!ctx->cipher->init(ctx, key, iv, enc))
388 ctx->block_mask = ctx->cipher->block_size - 1;
392 int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
393 const unsigned char *key, const unsigned char *iv,
394 int enc, const OSSL_PARAM params[])
396 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, params);
399 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
400 const unsigned char *key, const unsigned char *iv, int enc)
403 EVP_CIPHER_CTX_reset(ctx);
404 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, NULL);
407 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
408 ENGINE *impl, const unsigned char *key,
409 const unsigned char *iv, int enc)
411 return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, NULL);
414 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
415 const unsigned char *in, int inl)
418 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
420 return EVP_DecryptUpdate(ctx, out, outl, in, inl);
423 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
426 return EVP_EncryptFinal_ex(ctx, out, outl);
428 return EVP_DecryptFinal_ex(ctx, out, outl);
431 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
434 return EVP_EncryptFinal(ctx, out, outl);
436 return EVP_DecryptFinal(ctx, out, outl);
439 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
440 const unsigned char *key, const unsigned char *iv)
442 return EVP_CipherInit(ctx, cipher, key, iv, 1);
445 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
446 ENGINE *impl, const unsigned char *key,
447 const unsigned char *iv)
449 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
452 int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
453 const unsigned char *key, const unsigned char *iv,
454 const OSSL_PARAM params[])
456 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params);
459 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
460 const unsigned char *key, const unsigned char *iv)
462 return EVP_CipherInit(ctx, cipher, key, iv, 0);
465 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
466 ENGINE *impl, const unsigned char *key,
467 const unsigned char *iv)
469 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
472 int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
473 const unsigned char *key, const unsigned char *iv,
474 const OSSL_PARAM params[])
476 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 0, params);
480 * According to the letter of standard difference between pointers
481 * is specified to be valid only within same object. This makes
482 * it formally challenging to determine if input and output buffers
483 * are not partially overlapping with standard pointer arithmetic.
488 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
490 * Then we have VMS that distinguishes itself by adhering to
491 * sizeof(size_t)==4 even in 64-bit builds, which means that
492 * difference between two pointers might be truncated to 32 bits.
493 * In the context one can even wonder how comparison for
494 * equality is implemented. To be on the safe side we adhere to
495 * PTRDIFF_T even for comparison for equality.
497 # define PTRDIFF_T uint64_t
499 # define PTRDIFF_T size_t
502 int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
504 PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
506 * Check for partially overlapping buffers. [Binary logical
507 * operations are used instead of boolean to minimize number
508 * of conditional branches.]
510 int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
511 (diff > (0 - (PTRDIFF_T)len)));
516 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
517 unsigned char *out, int *outl,
518 const unsigned char *in, int inl)
520 int i, j, bl, cmpl = inl;
522 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
523 cmpl = safe_div_round_up_int(cmpl, 8, NULL);
525 bl = ctx->cipher->block_size;
527 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
528 /* If block size > 1 then the cipher will have to do this check */
529 if (bl == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
530 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
534 i = ctx->cipher->do_cipher(ctx, out, in, inl);
546 if (ossl_is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
547 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
551 if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
552 if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
561 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
564 memcpy(&(ctx->buf[i]), in, inl);
572 * Once we've processed the first j bytes from in, the amount of
573 * data left that is a multiple of the block length is:
574 * (inl - j) & ~(bl - 1)
575 * We must ensure that this amount of data, plus the one block that
576 * we process from ctx->buf does not exceed INT_MAX
578 if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
579 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
582 memcpy(&(ctx->buf[i]), in, j);
585 if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
595 if (!ctx->cipher->do_cipher(ctx, out, in, inl))
601 memcpy(ctx->buf, &(in[inl]), i);
607 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
608 const unsigned char *in, int inl)
611 size_t soutl, inl_ = (size_t)inl;
617 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
621 /* Prevent accidental use of decryption context when encrypting */
623 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
627 if (ctx->cipher == NULL) {
628 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
632 if (ctx->cipher->prov == NULL)
635 blocksize = ctx->cipher->block_size;
637 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
638 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
642 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
643 inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
647 if (soutl > INT_MAX) {
648 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
656 /* Code below to be removed when legacy support is dropped. */
659 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
662 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
665 ret = EVP_EncryptFinal_ex(ctx, out, outl);
669 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
672 unsigned int i, b, bl;
679 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
683 /* Prevent accidental use of decryption context when encrypting */
685 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
689 if (ctx->cipher == NULL) {
690 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
693 if (ctx->cipher->prov == NULL)
696 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
698 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
699 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
703 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
704 blocksize == 1 ? 0 : blocksize);
707 if (soutl > INT_MAX) {
708 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
716 /* Code below to be removed when legacy support is dropped. */
719 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
720 ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
728 b = ctx->cipher->block_size;
729 OPENSSL_assert(b <= sizeof(ctx->buf));
735 if (ctx->flags & EVP_CIPH_NO_PADDING) {
737 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
745 for (i = bl; i < b; i++)
747 ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
755 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
756 const unsigned char *in, int inl)
758 int fix_len, cmpl = inl, ret;
760 size_t soutl, inl_ = (size_t)inl;
766 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
770 /* Prevent accidental use of encryption context when decrypting */
772 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
776 if (ctx->cipher == NULL) {
777 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
780 if (ctx->cipher->prov == NULL)
783 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
785 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
786 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
789 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
790 inl_ + (size_t)(blocksize == 1 ? 0 : blocksize),
794 if (soutl > INT_MAX) {
795 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
803 /* Code below to be removed when legacy support is dropped. */
806 b = ctx->cipher->block_size;
808 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
809 cmpl = safe_div_round_up_int(cmpl, 8, NULL);
811 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
812 if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
813 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
817 fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
831 if (ctx->flags & EVP_CIPH_NO_PADDING)
832 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
834 OPENSSL_assert(b <= sizeof(ctx->final));
836 if (ctx->final_used) {
837 /* see comment about PTRDIFF_T comparison above */
838 if (((PTRDIFF_T)out == (PTRDIFF_T)in)
839 || ossl_is_partially_overlapping(out, in, b)) {
840 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
844 * final_used is only ever set if buf_len is 0. Therefore the maximum
845 * length output we will ever see from evp_EncryptDecryptUpdate is
846 * the maximum multiple of the block length that is <= inl, or just:
848 * Since final_used has been set then the final output length is:
849 * (inl & ~(b - 1)) + b
850 * This must never exceed INT_MAX
852 if ((inl & ~(b - 1)) > INT_MAX - b) {
853 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
856 memcpy(out, ctx->final, b);
862 if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
866 * if we have 'decrypted' a multiple of block size, make sure we have a
867 * copy of this last block
869 if (b > 1 && !ctx->buf_len) {
872 memcpy(ctx->final, &out[*outl], b);
882 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
885 ret = EVP_DecryptFinal_ex(ctx, out, outl);
889 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
900 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
904 /* Prevent accidental use of encryption context when decrypting */
906 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
910 if (ctx->cipher == NULL) {
911 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
915 if (ctx->cipher->prov == NULL)
918 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
920 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
921 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
925 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
926 blocksize == 1 ? 0 : blocksize);
929 if (soutl > INT_MAX) {
930 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
938 /* Code below to be removed when legacy support is dropped. */
942 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
943 i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
951 b = ctx->cipher->block_size;
952 if (ctx->flags & EVP_CIPH_NO_PADDING) {
954 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
961 if (ctx->buf_len || !ctx->final_used) {
962 ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
965 OPENSSL_assert(b <= sizeof(ctx->final));
968 * The following assumes that the ciphertext has been authenticated.
969 * Otherwise it provides a padding oracle.
971 n = ctx->final[b - 1];
972 if (n == 0 || n > (int)b) {
973 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
976 for (i = 0; i < n; i++) {
977 if (ctx->final[--b] != n) {
978 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
982 n = ctx->cipher->block_size - n;
983 for (i = 0; i < n; i++)
984 out[i] = ctx->final[i];
991 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
993 if (c->cipher->prov != NULL) {
995 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
998 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
1001 /* Check the cipher actually understands this parameter */
1002 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
1003 OSSL_CIPHER_PARAM_KEYLEN) == NULL) {
1004 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1008 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
1009 if (!OSSL_PARAM_set_int(params, keylen))
1011 ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params);
1014 c->key_len = keylen;
1018 /* Code below to be removed when legacy support is dropped. */
1021 * Note there have never been any built-in ciphers that define this flag
1022 * since it was first introduced.
1024 if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
1025 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
1026 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
1028 if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
1029 c->key_len = keylen;
1032 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1036 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
1039 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1040 unsigned int pd = pad;
1043 ctx->flags &= ~EVP_CIPH_NO_PADDING;
1045 ctx->flags |= EVP_CIPH_NO_PADDING;
1047 if (ctx->cipher != NULL && ctx->cipher->prov == NULL)
1049 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
1050 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1055 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1057 int ret = EVP_CTRL_RET_UNSUPPORTED;
1061 OSSL_PARAM params[4] = {
1062 OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
1065 if (ctx == NULL || ctx->cipher == NULL) {
1066 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
1070 if (ctx->cipher->prov == NULL)
1074 case EVP_CTRL_SET_KEY_LENGTH:
1075 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
1078 case EVP_CTRL_RAND_KEY: /* Used by DES */
1081 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
1087 * EVP_CTRL_INIT is purely legacy, no provider counterpart.
1088 * As a matter of fact, this should be dead code, but some caller
1089 * might still do a direct control call with this command, so...
1090 * Legacy methods return 1 except for exceptional circumstances, so
1091 * we do the same here to not be disruptive.
1094 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
1097 case EVP_CTRL_AEAD_SET_IVLEN:
1100 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1103 case EVP_CTRL_CCM_SET_L:
1104 if (arg < 2 || arg > 8)
1107 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1110 case EVP_CTRL_AEAD_SET_IV_FIXED:
1111 params[0] = OSSL_PARAM_construct_octet_string(
1112 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
1114 case EVP_CTRL_GCM_IV_GEN:
1117 sz = 0; /* special case that uses the iv length */
1118 params[0] = OSSL_PARAM_construct_octet_string(
1119 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
1121 case EVP_CTRL_GCM_SET_IV_INV:
1124 params[0] = OSSL_PARAM_construct_octet_string(
1125 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
1127 case EVP_CTRL_GET_RC5_ROUNDS:
1128 set_params = 0; /* Fall thru */
1129 case EVP_CTRL_SET_RC5_ROUNDS:
1132 i = (unsigned int)arg;
1133 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
1135 case EVP_CTRL_SET_SPEED:
1138 i = (unsigned int)arg;
1139 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
1141 case EVP_CTRL_AEAD_GET_TAG:
1142 set_params = 0; /* Fall thru */
1143 case EVP_CTRL_AEAD_SET_TAG:
1144 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1147 case EVP_CTRL_AEAD_TLS1_AAD:
1148 /* This one does a set and a get - since it returns a size */
1150 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1152 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1156 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1157 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1161 #ifndef OPENSSL_NO_RC2
1162 case EVP_CTRL_GET_RC2_KEY_BITS:
1163 set_params = 0; /* Fall thru */
1164 case EVP_CTRL_SET_RC2_KEY_BITS:
1165 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
1167 #endif /* OPENSSL_NO_RC2 */
1168 #if !defined(OPENSSL_NO_MULTIBLOCK)
1169 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
1170 params[0] = OSSL_PARAM_construct_size_t(
1171 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
1172 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1176 params[0] = OSSL_PARAM_construct_size_t(
1177 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
1178 params[1] = OSSL_PARAM_construct_end();
1179 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1183 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
1184 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1185 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1187 if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
1190 params[0] = OSSL_PARAM_construct_octet_string(
1191 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void*)p->inp, p->len);
1192 params[1] = OSSL_PARAM_construct_uint(
1193 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1194 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1197 /* Retrieve the return values changed by the set */
1198 params[0] = OSSL_PARAM_construct_size_t(
1199 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
1200 params[1] = OSSL_PARAM_construct_uint(
1201 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1202 params[2] = OSSL_PARAM_construct_end();
1203 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1208 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
1209 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1210 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1212 params[0] = OSSL_PARAM_construct_octet_string(
1213 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
1215 params[1] = OSSL_PARAM_construct_octet_string(
1216 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void*)p->inp,
1218 params[2] = OSSL_PARAM_construct_uint(
1219 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1220 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1223 params[0] = OSSL_PARAM_construct_size_t(
1224 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
1225 params[1] = OSSL_PARAM_construct_end();
1226 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1231 #endif /* OPENSSL_NO_MULTIBLOCK */
1232 case EVP_CTRL_AEAD_SET_MAC_KEY:
1235 params[0] = OSSL_PARAM_construct_octet_string(
1236 OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
1241 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1243 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1246 /* Code below to be removed when legacy support is dropped. */
1248 if (ctx->cipher->ctrl == NULL) {
1249 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
1253 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
1256 if (ret == EVP_CTRL_RET_UNSUPPORTED) {
1257 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
1263 int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1265 if (cipher != NULL && cipher->get_params != NULL)
1266 return cipher->get_params(params);
1270 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1273 const OSSL_PARAM *p;
1275 if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) {
1276 r = ctx->cipher->set_ctx_params(ctx->algctx, params);
1278 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
1279 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->key_len)) {
1285 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
1286 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->iv_len)) {
1295 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1297 if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1298 return ctx->cipher->get_ctx_params(ctx->algctx, params);
1302 const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1304 if (cipher != NULL && cipher->gettable_params != NULL)
1305 return cipher->gettable_params(
1306 ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)));
1310 const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
1314 if (cipher != NULL && cipher->settable_ctx_params != NULL) {
1315 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1316 return cipher->settable_ctx_params(NULL, provctx);
1321 const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
1325 if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
1326 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1327 return cipher->gettable_ctx_params(NULL, provctx);
1332 const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx)
1336 if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
1337 alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1338 return cctx->cipher->settable_ctx_params(cctx->algctx, alg);
1343 const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx)
1347 if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
1348 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1349 return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx);
1355 static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx)
1357 const EVP_CIPHER *cipher = ctx->cipher;
1358 const OSSL_PROVIDER *prov;
1363 prov = EVP_CIPHER_get0_provider(cipher);
1364 return ossl_provider_libctx(prov);
1368 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
1370 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1371 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1378 OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
1380 kl = EVP_CIPHER_CTX_get_key_length(ctx);
1381 if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0)
1385 #endif /* FIPS_MODULE */
1388 EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in)
1390 EVP_CIPHER_CTX *out = EVP_CIPHER_CTX_new();
1392 if (out != NULL && !EVP_CIPHER_CTX_copy(out, in)) {
1393 EVP_CIPHER_CTX_free(out);
1399 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
1401 if ((in == NULL) || (in->cipher == NULL)) {
1402 ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
1406 if (in->cipher->prov == NULL)
1409 if (in->cipher->dupctx == NULL) {
1410 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1414 EVP_CIPHER_CTX_reset(out);
1419 if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
1420 out->fetched_cipher = NULL;
1424 out->algctx = in->cipher->dupctx(in->algctx);
1425 if (out->algctx == NULL) {
1426 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1432 /* Code below to be removed when legacy support is dropped. */
1435 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1436 /* Make sure it's safe to copy a cipher context using an ENGINE */
1437 if (in->engine && !ENGINE_init(in->engine)) {
1438 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
1443 EVP_CIPHER_CTX_reset(out);
1444 memcpy(out, in, sizeof(*out));
1446 if (in->cipher_data && in->cipher->ctx_size) {
1447 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
1448 if (out->cipher_data == NULL) {
1450 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1453 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
1456 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
1457 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
1459 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
1465 EVP_CIPHER *evp_cipher_new(void)
1467 EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1469 if (cipher != NULL) {
1470 cipher->lock = CRYPTO_THREAD_lock_new();
1471 if (cipher->lock == NULL) {
1472 OPENSSL_free(cipher);
1481 * FIPS module note: since internal fetches will be entirely
1482 * provider based, we know that none of its code depends on legacy
1483 * NIDs or any functionality that use them.
1486 /* After removal of legacy support get rid of the need for legacy NIDs */
1487 static void set_legacy_nid(const char *name, void *vlegacy_nid)
1490 int *legacy_nid = vlegacy_nid;
1492 * We use lowest level function to get the associated method, because
1493 * higher level functions such as EVP_get_cipherbyname() have changed
1494 * to look at providers too.
1496 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
1498 if (*legacy_nid == -1) /* We found a clash already */
1500 if (legacy_method == NULL)
1502 nid = EVP_CIPHER_get_nid(legacy_method);
1503 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
1511 static void *evp_cipher_from_algorithm(const int name_id,
1512 const OSSL_ALGORITHM *algodef,
1513 OSSL_PROVIDER *prov)
1515 const OSSL_DISPATCH *fns = algodef->implementation;
1516 EVP_CIPHER *cipher = NULL;
1517 int fnciphcnt = 0, fnctxcnt = 0;
1519 if ((cipher = evp_cipher_new()) == NULL) {
1520 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1525 cipher->nid = NID_undef;
1526 if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
1527 || cipher->nid == -1) {
1528 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1529 EVP_CIPHER_free(cipher);
1534 cipher->name_id = name_id;
1535 if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
1536 EVP_CIPHER_free(cipher);
1539 cipher->description = algodef->algorithm_description;
1541 for (; fns->function_id != 0; fns++) {
1542 switch (fns->function_id) {
1543 case OSSL_FUNC_CIPHER_NEWCTX:
1544 if (cipher->newctx != NULL)
1546 cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
1549 case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1550 if (cipher->einit != NULL)
1552 cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
1555 case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1556 if (cipher->dinit != NULL)
1558 cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
1561 case OSSL_FUNC_CIPHER_UPDATE:
1562 if (cipher->cupdate != NULL)
1564 cipher->cupdate = OSSL_FUNC_cipher_update(fns);
1567 case OSSL_FUNC_CIPHER_FINAL:
1568 if (cipher->cfinal != NULL)
1570 cipher->cfinal = OSSL_FUNC_cipher_final(fns);
1573 case OSSL_FUNC_CIPHER_CIPHER:
1574 if (cipher->ccipher != NULL)
1576 cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
1578 case OSSL_FUNC_CIPHER_FREECTX:
1579 if (cipher->freectx != NULL)
1581 cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
1584 case OSSL_FUNC_CIPHER_DUPCTX:
1585 if (cipher->dupctx != NULL)
1587 cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
1589 case OSSL_FUNC_CIPHER_GET_PARAMS:
1590 if (cipher->get_params != NULL)
1592 cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
1594 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1595 if (cipher->get_ctx_params != NULL)
1597 cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
1599 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1600 if (cipher->set_ctx_params != NULL)
1602 cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
1604 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1605 if (cipher->gettable_params != NULL)
1607 cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
1609 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1610 if (cipher->gettable_ctx_params != NULL)
1612 cipher->gettable_ctx_params =
1613 OSSL_FUNC_cipher_gettable_ctx_params(fns);
1615 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1616 if (cipher->settable_ctx_params != NULL)
1618 cipher->settable_ctx_params =
1619 OSSL_FUNC_cipher_settable_ctx_params(fns);
1623 if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1624 || (fnciphcnt == 0 && cipher->ccipher == NULL)
1627 * In order to be a consistent set of functions we must have at least
1628 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1629 * functions, or a single "cipher" function. In all cases we need both
1630 * the "newctx" and "freectx" functions.
1632 EVP_CIPHER_free(cipher);
1633 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1636 cipher->prov = prov;
1638 ossl_provider_up_ref(prov);
1640 if (!evp_cipher_cache_constants(cipher)) {
1641 EVP_CIPHER_free(cipher);
1642 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1649 static int evp_cipher_up_ref(void *cipher)
1651 return EVP_CIPHER_up_ref(cipher);
1654 static void evp_cipher_free(void *cipher)
1656 EVP_CIPHER_free(cipher);
1659 EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1660 const char *properties)
1662 EVP_CIPHER *cipher =
1663 evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1664 evp_cipher_from_algorithm, evp_cipher_up_ref,
1670 int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
1674 if (cipher->origin == EVP_ORIG_DYNAMIC)
1675 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
1679 void evp_cipher_free_int(EVP_CIPHER *cipher)
1681 OPENSSL_free(cipher->type_name);
1682 ossl_provider_free(cipher->prov);
1683 CRYPTO_THREAD_lock_free(cipher->lock);
1684 OPENSSL_free(cipher);
1687 void EVP_CIPHER_free(EVP_CIPHER *cipher)
1691 if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC)
1694 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
1697 evp_cipher_free_int(cipher);
1700 void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
1701 void (*fn)(EVP_CIPHER *mac, void *arg),
1704 evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1705 (void (*)(void *, void *))fn, arg,
1706 evp_cipher_from_algorithm, evp_cipher_up_ref,