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 "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));
49 /* Remove legacy code below when legacy support is removed. */
52 if (ctx->cipher != NULL) {
53 if (ctx->cipher->cleanup && !ctx->cipher->cleanup(ctx))
55 /* Cleanse cipher context data */
56 if (ctx->cipher_data && ctx->cipher->ctx_size)
57 OPENSSL_cleanse(ctx->cipher_data, ctx->cipher->ctx_size);
59 OPENSSL_free(ctx->cipher_data);
60 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
61 ENGINE_finish(ctx->engine);
63 memset(ctx, 0, sizeof(*ctx));
67 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
69 return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
72 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
76 EVP_CIPHER_CTX_reset(ctx);
80 static int evp_cipher_init_internal(EVP_CIPHER_CTX *ctx,
81 const EVP_CIPHER *cipher,
82 ENGINE *impl, const unsigned char *key,
83 const unsigned char *iv, int enc,
84 const OSSL_PARAM params[])
87 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
88 ENGINE *tmpimpl = NULL;
91 * enc == 1 means we are encrypting.
92 * enc == 0 means we are decrypting.
93 * enc == -1 means, use the previously initialised value for encrypt/decrypt
103 if (cipher == NULL && ctx->cipher == NULL) {
104 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
108 /* Code below to be removed when legacy support is dropped. */
110 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
112 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
113 * this context may already have an ENGINE! Try to avoid releasing the
114 * previous handle, re-querying for an ENGINE, and having a
115 * reinitialisation, when it may all be unnecessary.
117 if (ctx->engine && ctx->cipher
118 && (cipher == NULL || cipher->nid == ctx->cipher->nid))
121 if (cipher != NULL && impl == NULL) {
122 /* Ask if an ENGINE is reserved for this job */
123 tmpimpl = ENGINE_get_cipher_engine(cipher->nid);
128 * If there are engines involved then we should use legacy handling for now.
130 if (ctx->engine != NULL
131 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
135 if (ctx->cipher == ctx->fetched_cipher)
137 EVP_CIPHER_free(ctx->fetched_cipher);
138 ctx->fetched_cipher = NULL;
142 * Ensure a context left lying around from last time is cleared
145 if (cipher != NULL && ctx->cipher != NULL) {
146 OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size);
147 ctx->cipher_data = NULL;
151 /* Start of non-legacy code below */
153 /* Ensure a context left lying around from last time is cleared */
154 if (cipher != NULL && ctx->cipher != NULL) {
155 unsigned long flags = ctx->flags;
157 EVP_CIPHER_CTX_reset(ctx);
158 /* Restore encrypt and flags */
164 cipher = ctx->cipher;
166 if (cipher->prov == NULL) {
168 /* We only do explicit fetches inside the FIPS module */
169 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
172 EVP_CIPHER *provciph =
173 EVP_CIPHER_fetch(NULL,
174 cipher->nid == NID_undef ? "NULL"
175 : OBJ_nid2sn(cipher->nid),
178 if (provciph == NULL)
181 EVP_CIPHER_free(ctx->fetched_cipher);
182 ctx->fetched_cipher = provciph;
186 if (cipher->prov != NULL) {
187 if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) {
188 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
191 EVP_CIPHER_free(ctx->fetched_cipher);
192 ctx->fetched_cipher = (EVP_CIPHER *)cipher;
194 ctx->cipher = cipher;
195 if (ctx->algctx == NULL) {
196 ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
197 if (ctx->algctx == NULL) {
198 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
203 if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
205 * If this ctx was already set up for no padding then we need to tell
206 * the new cipher about it.
208 if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
213 if (ctx->cipher->einit == NULL) {
214 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
218 return ctx->cipher->einit(ctx->algctx,
221 : EVP_CIPHER_CTX_get_key_length(ctx),
224 : EVP_CIPHER_CTX_get_iv_length(ctx),
228 if (ctx->cipher->dinit == NULL) {
229 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
233 return ctx->cipher->dinit(ctx->algctx,
236 : EVP_CIPHER_CTX_get_key_length(ctx),
239 : EVP_CIPHER_CTX_get_iv_length(ctx),
242 /* Code below to be removed when legacy support is dropped. */
245 if (cipher != NULL) {
247 * Ensure a context left lying around from last time is cleared (we
248 * previously attempted to avoid this if the same ENGINE and
249 * EVP_CIPHER could be used).
252 unsigned long flags = ctx->flags;
253 EVP_CIPHER_CTX_reset(ctx);
254 /* Restore encrypt and flags */
258 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
260 if (!ENGINE_init(impl)) {
261 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
268 /* There's an ENGINE for this job ... (apparently) */
269 const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
273 * One positive side-effect of US's export control history,
274 * is that we should at least be able to avoid using US
275 * misspellings of "initialisation"?
277 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
280 /* We'll use the ENGINE's private cipher definition */
283 * Store the ENGINE functional reference so we know 'cipher' came
284 * from an ENGINE and we need to release it when done.
292 ctx->cipher = cipher;
293 if (ctx->cipher->ctx_size) {
294 ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
295 if (ctx->cipher_data == NULL) {
297 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
301 ctx->cipher_data = NULL;
303 ctx->key_len = cipher->key_len;
304 /* Preserve wrap enable flag, zero everything else */
305 ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
306 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
307 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
309 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
314 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
317 if (ctx->cipher == NULL)
320 /* we assume block size is a power of 2 in *cryptUpdate */
321 OPENSSL_assert(ctx->cipher->block_size == 1
322 || ctx->cipher->block_size == 8
323 || ctx->cipher->block_size == 16);
325 if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
326 && EVP_CIPHER_CTX_get_mode(ctx) == EVP_CIPH_WRAP_MODE) {
327 ERR_raise(ERR_LIB_EVP, EVP_R_WRAP_MODE_NOT_ALLOWED);
331 if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
332 & EVP_CIPH_CUSTOM_IV) == 0) {
333 switch (EVP_CIPHER_CTX_get_mode(ctx)) {
335 case EVP_CIPH_STREAM_CIPHER:
336 case EVP_CIPH_ECB_MODE:
339 case EVP_CIPH_CFB_MODE:
340 case EVP_CIPH_OFB_MODE:
345 case EVP_CIPH_CBC_MODE:
346 n = EVP_CIPHER_CTX_get_iv_length(ctx);
347 if (!ossl_assert(n >= 0 && n <= (int)sizeof(ctx->iv)))
350 memcpy(ctx->oiv, iv, n);
351 memcpy(ctx->iv, ctx->oiv, n);
354 case EVP_CIPH_CTR_MODE:
356 /* Don't reuse IV for CTR mode */
358 if ((n = EVP_CIPHER_CTX_get_iv_length(ctx)) <= 0)
360 memcpy(ctx->iv, iv, n);
369 if (key != NULL || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
370 if (!ctx->cipher->init(ctx, key, iv, enc))
375 ctx->block_mask = ctx->cipher->block_size - 1;
379 int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
380 const unsigned char *key, const unsigned char *iv,
381 int enc, const OSSL_PARAM params[])
383 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, params);
386 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
387 const unsigned char *key, const unsigned char *iv, int enc)
390 EVP_CIPHER_CTX_reset(ctx);
391 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, NULL);
394 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
395 ENGINE *impl, const unsigned char *key,
396 const unsigned char *iv, int enc)
398 return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, NULL);
401 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
402 const unsigned char *in, int inl)
405 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
407 return EVP_DecryptUpdate(ctx, out, outl, in, inl);
410 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
413 return EVP_EncryptFinal_ex(ctx, out, outl);
415 return EVP_DecryptFinal_ex(ctx, out, outl);
418 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
421 return EVP_EncryptFinal(ctx, out, outl);
423 return EVP_DecryptFinal(ctx, out, outl);
426 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
427 const unsigned char *key, const unsigned char *iv)
429 return EVP_CipherInit(ctx, cipher, key, iv, 1);
432 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
433 ENGINE *impl, const unsigned char *key,
434 const unsigned char *iv)
436 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
439 int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
440 const unsigned char *key, const unsigned char *iv,
441 const OSSL_PARAM params[])
443 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params);
446 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
447 const unsigned char *key, const unsigned char *iv)
449 return EVP_CipherInit(ctx, cipher, key, iv, 0);
452 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
453 ENGINE *impl, const unsigned char *key,
454 const unsigned char *iv)
456 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
459 int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
460 const unsigned char *key, const unsigned char *iv,
461 const OSSL_PARAM params[])
463 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 0, params);
467 * According to the letter of standard difference between pointers
468 * is specified to be valid only within same object. This makes
469 * it formally challenging to determine if input and output buffers
470 * are not partially overlapping with standard pointer arithmetic.
475 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
477 * Then we have VMS that distinguishes itself by adhering to
478 * sizeof(size_t)==4 even in 64-bit builds, which means that
479 * difference between two pointers might be truncated to 32 bits.
480 * In the context one can even wonder how comparison for
481 * equality is implemented. To be on the safe side we adhere to
482 * PTRDIFF_T even for comparison for equality.
484 # define PTRDIFF_T uint64_t
486 # define PTRDIFF_T size_t
489 int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
491 PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
493 * Check for partially overlapping buffers. [Binary logical
494 * operations are used instead of boolean to minimize number
495 * of conditional branches.]
497 int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
498 (diff > (0 - (PTRDIFF_T)len)));
503 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
504 unsigned char *out, int *outl,
505 const unsigned char *in, int inl)
507 int i, j, bl, cmpl = inl;
509 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
510 cmpl = (cmpl + 7) / 8;
512 bl = ctx->cipher->block_size;
514 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
515 /* If block size > 1 then the cipher will have to do this check */
516 if (bl == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
517 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
521 i = ctx->cipher->do_cipher(ctx, out, in, inl);
533 if (ossl_is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
534 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
538 if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
539 if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
548 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
551 memcpy(&(ctx->buf[i]), in, inl);
559 * Once we've processed the first j bytes from in, the amount of
560 * data left that is a multiple of the block length is:
561 * (inl - j) & ~(bl - 1)
562 * We must ensure that this amount of data, plus the one block that
563 * we process from ctx->buf does not exceed INT_MAX
565 if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
566 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
569 memcpy(&(ctx->buf[i]), in, j);
572 if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
582 if (!ctx->cipher->do_cipher(ctx, out, in, inl))
588 memcpy(ctx->buf, &(in[inl]), i);
594 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
595 const unsigned char *in, int inl)
604 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
608 /* Prevent accidental use of decryption context when encrypting */
610 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
614 if (ctx->cipher == NULL) {
615 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
619 if (ctx->cipher->prov == NULL)
622 blocksize = ctx->cipher->block_size;
624 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
625 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
628 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
629 inl + (blocksize == 1 ? 0 : blocksize), in,
633 if (soutl > INT_MAX) {
634 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
642 /* Code below to be removed when legacy support is dropped. */
645 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
648 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
651 ret = EVP_EncryptFinal_ex(ctx, out, outl);
655 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
658 unsigned int i, b, bl;
665 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
669 /* Prevent accidental use of decryption context when encrypting */
671 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
675 if (ctx->cipher == NULL) {
676 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
679 if (ctx->cipher->prov == NULL)
682 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
684 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
685 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
689 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
690 blocksize == 1 ? 0 : blocksize);
693 if (soutl > INT_MAX) {
694 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
702 /* Code below to be removed when legacy support is dropped. */
705 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
706 ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
714 b = ctx->cipher->block_size;
715 OPENSSL_assert(b <= sizeof(ctx->buf));
721 if (ctx->flags & EVP_CIPH_NO_PADDING) {
723 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
731 for (i = bl; i < b; i++)
733 ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
741 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
742 const unsigned char *in, int inl)
744 int fix_len, cmpl = inl, ret;
752 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
756 /* Prevent accidental use of encryption context when decrypting */
758 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
762 if (ctx->cipher == NULL) {
763 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
766 if (ctx->cipher->prov == NULL)
769 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
771 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
772 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
775 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
776 inl + (blocksize == 1 ? 0 : blocksize), in,
780 if (soutl > INT_MAX) {
781 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
789 /* Code below to be removed when legacy support is dropped. */
792 b = ctx->cipher->block_size;
794 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
795 cmpl = (cmpl + 7) / 8;
797 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
798 if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
799 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
803 fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
817 if (ctx->flags & EVP_CIPH_NO_PADDING)
818 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
820 OPENSSL_assert(b <= sizeof(ctx->final));
822 if (ctx->final_used) {
823 /* see comment about PTRDIFF_T comparison above */
824 if (((PTRDIFF_T)out == (PTRDIFF_T)in)
825 || ossl_is_partially_overlapping(out, in, b)) {
826 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
830 * final_used is only ever set if buf_len is 0. Therefore the maximum
831 * length output we will ever see from evp_EncryptDecryptUpdate is
832 * the maximum multiple of the block length that is <= inl, or just:
834 * Since final_used has been set then the final output length is:
835 * (inl & ~(b - 1)) + b
836 * This must never exceed INT_MAX
838 if ((inl & ~(b - 1)) > INT_MAX - b) {
839 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
842 memcpy(out, ctx->final, b);
848 if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
852 * if we have 'decrypted' a multiple of block size, make sure we have a
853 * copy of this last block
855 if (b > 1 && !ctx->buf_len) {
858 memcpy(ctx->final, &out[*outl], b);
868 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
871 ret = EVP_DecryptFinal_ex(ctx, out, outl);
875 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
886 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
890 /* Prevent accidental use of encryption context when decrypting */
892 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
896 if (ctx->cipher == NULL) {
897 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
901 if (ctx->cipher->prov == NULL)
904 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
906 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
907 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
911 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
912 blocksize == 1 ? 0 : blocksize);
915 if (soutl > INT_MAX) {
916 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
924 /* Code below to be removed when legacy support is dropped. */
928 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
929 i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
937 b = ctx->cipher->block_size;
938 if (ctx->flags & EVP_CIPH_NO_PADDING) {
940 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
947 if (ctx->buf_len || !ctx->final_used) {
948 ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
951 OPENSSL_assert(b <= sizeof(ctx->final));
954 * The following assumes that the ciphertext has been authenticated.
955 * Otherwise it provides a padding oracle.
957 n = ctx->final[b - 1];
958 if (n == 0 || n > (int)b) {
959 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
962 for (i = 0; i < n; i++) {
963 if (ctx->final[--b] != n) {
964 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
968 n = ctx->cipher->block_size - n;
969 for (i = 0; i < n; i++)
970 out[i] = ctx->final[i];
977 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
979 if (c->cipher->prov != NULL) {
981 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
984 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
987 /* Check the cipher actually understands this parameter */
988 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
989 OSSL_CIPHER_PARAM_KEYLEN) == NULL) {
990 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
994 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
995 ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params);
997 return ok > 0 ? 1 : 0;
1000 /* Code below to be removed when legacy support is dropped. */
1003 * Note there have never been any built-in ciphers that define this flag
1004 * since it was first introduced.
1006 if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
1007 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
1008 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
1010 if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
1011 c->key_len = keylen;
1014 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1018 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
1021 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1022 unsigned int pd = pad;
1025 ctx->flags &= ~EVP_CIPH_NO_PADDING;
1027 ctx->flags |= EVP_CIPH_NO_PADDING;
1029 if (ctx->cipher != NULL && ctx->cipher->prov == NULL)
1031 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
1032 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1037 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1039 int ret = EVP_CTRL_RET_UNSUPPORTED;
1043 OSSL_PARAM params[4] = {
1044 OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
1047 if (ctx == NULL || ctx->cipher == NULL) {
1048 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
1052 if (ctx->cipher->prov == NULL)
1056 case EVP_CTRL_SET_KEY_LENGTH:
1057 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
1059 case EVP_CTRL_RAND_KEY: /* Used by DES */
1062 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
1068 * EVP_CTRL_INIT is purely legacy, no provider counterpart.
1069 * As a matter of fact, this should be dead code, but some caller
1070 * might still do a direct control call with this command, so...
1071 * Legacy methods return 1 except for exceptional circumstances, so
1072 * we do the same here to not be disruptive.
1075 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
1078 case EVP_CTRL_AEAD_SET_IVLEN:
1081 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1083 case EVP_CTRL_CCM_SET_L:
1084 if (arg < 2 || arg > 8)
1087 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1089 case EVP_CTRL_AEAD_SET_IV_FIXED:
1090 params[0] = OSSL_PARAM_construct_octet_string(
1091 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
1093 case EVP_CTRL_GCM_IV_GEN:
1096 sz = 0; /* special case that uses the iv length */
1097 params[0] = OSSL_PARAM_construct_octet_string(
1098 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
1100 case EVP_CTRL_GCM_SET_IV_INV:
1103 params[0] = OSSL_PARAM_construct_octet_string(
1104 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
1106 case EVP_CTRL_GET_RC5_ROUNDS:
1107 set_params = 0; /* Fall thru */
1108 case EVP_CTRL_SET_RC5_ROUNDS:
1111 i = (unsigned int)arg;
1112 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
1114 case EVP_CTRL_SET_SPEED:
1117 i = (unsigned int)arg;
1118 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
1120 case EVP_CTRL_AEAD_GET_TAG:
1121 set_params = 0; /* Fall thru */
1122 case EVP_CTRL_AEAD_SET_TAG:
1123 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1126 case EVP_CTRL_AEAD_TLS1_AAD:
1127 /* This one does a set and a get - since it returns a size */
1129 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1131 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1135 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1136 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1140 #ifndef OPENSSL_NO_RC2
1141 case EVP_CTRL_GET_RC2_KEY_BITS:
1142 set_params = 0; /* Fall thru */
1143 case EVP_CTRL_SET_RC2_KEY_BITS:
1144 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
1146 #endif /* OPENSSL_NO_RC2 */
1147 #if !defined(OPENSSL_NO_MULTIBLOCK)
1148 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
1149 params[0] = OSSL_PARAM_construct_size_t(
1150 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
1151 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1155 params[0] = OSSL_PARAM_construct_size_t(
1156 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
1157 params[1] = OSSL_PARAM_construct_end();
1158 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1162 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
1163 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1164 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1166 if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
1169 params[0] = OSSL_PARAM_construct_octet_string(
1170 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void*)p->inp, p->len);
1171 params[1] = OSSL_PARAM_construct_uint(
1172 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1173 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1176 /* Retrieve the return values changed by the set */
1177 params[0] = OSSL_PARAM_construct_size_t(
1178 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
1179 params[1] = OSSL_PARAM_construct_uint(
1180 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1181 params[2] = OSSL_PARAM_construct_end();
1182 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1187 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
1188 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1189 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1191 params[0] = OSSL_PARAM_construct_octet_string(
1192 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
1194 params[1] = OSSL_PARAM_construct_octet_string(
1195 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void*)p->inp,
1197 params[2] = 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 params[0] = OSSL_PARAM_construct_size_t(
1203 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
1204 params[1] = OSSL_PARAM_construct_end();
1205 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1210 #endif /* OPENSSL_NO_MULTIBLOCK */
1211 case EVP_CTRL_AEAD_SET_MAC_KEY:
1214 params[0] = OSSL_PARAM_construct_octet_string(
1215 OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
1220 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1222 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1225 /* Code below to be removed when legacy support is dropped. */
1227 if (ctx->cipher->ctrl == NULL) {
1228 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
1232 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
1235 if (ret == EVP_CTRL_RET_UNSUPPORTED) {
1236 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
1242 int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1244 if (cipher != NULL && cipher->get_params != NULL)
1245 return cipher->get_params(params);
1249 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1251 if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL)
1252 return ctx->cipher->set_ctx_params(ctx->algctx, params);
1256 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1258 if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1259 return ctx->cipher->get_ctx_params(ctx->algctx, params);
1263 const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1265 if (cipher != NULL && cipher->gettable_params != NULL)
1266 return cipher->gettable_params(
1267 ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)));
1271 const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
1275 if (cipher != NULL && cipher->settable_ctx_params != NULL) {
1276 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1277 return cipher->settable_ctx_params(NULL, provctx);
1282 const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
1286 if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
1287 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1288 return cipher->gettable_ctx_params(NULL, provctx);
1293 const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx)
1297 if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
1298 alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1299 return cctx->cipher->settable_ctx_params(cctx->algctx, alg);
1304 const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx)
1308 if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
1309 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1310 return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx);
1316 static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx)
1318 const EVP_CIPHER *cipher = ctx->cipher;
1319 const OSSL_PROVIDER *prov;
1324 prov = EVP_CIPHER_get0_provider(cipher);
1325 return ossl_provider_libctx(prov);
1329 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
1331 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1332 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1339 OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
1341 kl = EVP_CIPHER_CTX_get_key_length(ctx);
1342 if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0)
1346 #endif /* FIPS_MODULE */
1349 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
1351 if ((in == NULL) || (in->cipher == NULL)) {
1352 ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
1356 if (in->cipher->prov == NULL)
1359 if (in->cipher->dupctx == NULL) {
1360 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1364 EVP_CIPHER_CTX_reset(out);
1369 if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
1370 out->fetched_cipher = NULL;
1374 out->algctx = in->cipher->dupctx(in->algctx);
1375 if (out->algctx == NULL) {
1376 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1382 /* Code below to be removed when legacy support is dropped. */
1385 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1386 /* Make sure it's safe to copy a cipher context using an ENGINE */
1387 if (in->engine && !ENGINE_init(in->engine)) {
1388 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
1393 EVP_CIPHER_CTX_reset(out);
1394 memcpy(out, in, sizeof(*out));
1396 if (in->cipher_data && in->cipher->ctx_size) {
1397 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
1398 if (out->cipher_data == NULL) {
1400 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1403 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
1406 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
1407 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
1409 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
1415 EVP_CIPHER *evp_cipher_new(void)
1417 EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1419 if (cipher != NULL) {
1420 cipher->lock = CRYPTO_THREAD_lock_new();
1421 if (cipher->lock == NULL) {
1422 OPENSSL_free(cipher);
1431 * FIPS module note: since internal fetches will be entirely
1432 * provider based, we know that none of its code depends on legacy
1433 * NIDs or any functionality that use them.
1436 /* After removal of legacy support get rid of the need for legacy NIDs */
1437 static void set_legacy_nid(const char *name, void *vlegacy_nid)
1440 int *legacy_nid = vlegacy_nid;
1442 * We use lowest level function to get the associated method, because
1443 * higher level functions such as EVP_get_cipherbyname() have changed
1444 * to look at providers too.
1446 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
1448 if (*legacy_nid == -1) /* We found a clash already */
1450 if (legacy_method == NULL)
1452 nid = EVP_CIPHER_get_nid(legacy_method);
1453 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
1461 static void *evp_cipher_from_algorithm(const int name_id,
1462 const OSSL_ALGORITHM *algodef,
1463 OSSL_PROVIDER *prov)
1465 const OSSL_DISPATCH *fns = algodef->implementation;
1466 EVP_CIPHER *cipher = NULL;
1467 int fnciphcnt = 0, fnctxcnt = 0;
1469 if ((cipher = evp_cipher_new()) == NULL) {
1470 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1475 cipher->nid = NID_undef;
1476 if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
1477 || cipher->nid == -1) {
1478 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1479 EVP_CIPHER_free(cipher);
1484 cipher->name_id = name_id;
1485 if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
1486 EVP_CIPHER_free(cipher);
1489 cipher->description = algodef->algorithm_description;
1491 for (; fns->function_id != 0; fns++) {
1492 switch (fns->function_id) {
1493 case OSSL_FUNC_CIPHER_NEWCTX:
1494 if (cipher->newctx != NULL)
1496 cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
1499 case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1500 if (cipher->einit != NULL)
1502 cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
1505 case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1506 if (cipher->dinit != NULL)
1508 cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
1511 case OSSL_FUNC_CIPHER_UPDATE:
1512 if (cipher->cupdate != NULL)
1514 cipher->cupdate = OSSL_FUNC_cipher_update(fns);
1517 case OSSL_FUNC_CIPHER_FINAL:
1518 if (cipher->cfinal != NULL)
1520 cipher->cfinal = OSSL_FUNC_cipher_final(fns);
1523 case OSSL_FUNC_CIPHER_CIPHER:
1524 if (cipher->ccipher != NULL)
1526 cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
1528 case OSSL_FUNC_CIPHER_FREECTX:
1529 if (cipher->freectx != NULL)
1531 cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
1534 case OSSL_FUNC_CIPHER_DUPCTX:
1535 if (cipher->dupctx != NULL)
1537 cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
1539 case OSSL_FUNC_CIPHER_GET_PARAMS:
1540 if (cipher->get_params != NULL)
1542 cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
1544 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1545 if (cipher->get_ctx_params != NULL)
1547 cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
1549 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1550 if (cipher->set_ctx_params != NULL)
1552 cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
1554 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1555 if (cipher->gettable_params != NULL)
1557 cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
1559 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1560 if (cipher->gettable_ctx_params != NULL)
1562 cipher->gettable_ctx_params =
1563 OSSL_FUNC_cipher_gettable_ctx_params(fns);
1565 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1566 if (cipher->settable_ctx_params != NULL)
1568 cipher->settable_ctx_params =
1569 OSSL_FUNC_cipher_settable_ctx_params(fns);
1573 if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1574 || (fnciphcnt == 0 && cipher->ccipher == NULL)
1577 * In order to be a consistent set of functions we must have at least
1578 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1579 * functions, or a single "cipher" function. In all cases we need both
1580 * the "newctx" and "freectx" functions.
1582 EVP_CIPHER_free(cipher);
1583 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1586 cipher->prov = prov;
1588 ossl_provider_up_ref(prov);
1590 if (!evp_cipher_cache_constants(cipher)) {
1591 EVP_CIPHER_free(cipher);
1592 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1599 static int evp_cipher_up_ref(void *cipher)
1601 return EVP_CIPHER_up_ref(cipher);
1604 static void evp_cipher_free(void *cipher)
1606 EVP_CIPHER_free(cipher);
1609 EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1610 const char *properties)
1612 EVP_CIPHER *cipher =
1613 evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1614 evp_cipher_from_algorithm, evp_cipher_up_ref,
1620 int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
1624 if (cipher->origin == EVP_ORIG_DYNAMIC)
1625 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
1629 void evp_cipher_free_int(EVP_CIPHER *cipher)
1631 OPENSSL_free(cipher->type_name);
1632 ossl_provider_free(cipher->prov);
1633 CRYPTO_THREAD_lock_free(cipher->lock);
1634 OPENSSL_free(cipher);
1637 void EVP_CIPHER_free(EVP_CIPHER *cipher)
1641 if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC)
1644 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
1647 evp_cipher_free_int(cipher);
1650 void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
1651 void (*fn)(EVP_CIPHER *mac, void *arg),
1654 evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1655 (void (*)(void *, void *))fn, arg,
1656 evp_cipher_from_algorithm, evp_cipher_up_ref,