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 "internal/cryptlib.h"
17 #include <openssl/evp.h>
18 #include <openssl/err.h>
19 #include <openssl/rand.h>
20 #include <openssl/engine.h>
21 #include <openssl/params.h>
22 #include <openssl/core_names.h>
23 #include "crypto/evp.h"
24 #include "internal/provider.h"
25 #include "evp_local.h"
27 int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
32 if (ctx->cipher == NULL || ctx->cipher->prov == NULL)
35 if (ctx->provctx != NULL) {
36 if (ctx->cipher->freectx != NULL)
37 ctx->cipher->freectx(ctx->provctx);
40 if (ctx->fetched_cipher != NULL)
41 EVP_CIPHER_free(ctx->fetched_cipher);
42 memset(ctx, 0, sizeof(*ctx));
46 /* Remove legacy code below when legacy support is removed. */
49 if (ctx->cipher != NULL) {
50 if (ctx->cipher->cleanup && !ctx->cipher->cleanup(ctx))
52 /* Cleanse cipher context data */
53 if (ctx->cipher_data && ctx->cipher->ctx_size)
54 OPENSSL_cleanse(ctx->cipher_data, ctx->cipher->ctx_size);
56 OPENSSL_free(ctx->cipher_data);
57 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
58 ENGINE_finish(ctx->engine);
60 memset(ctx, 0, sizeof(*ctx));
64 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
66 return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
69 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
73 EVP_CIPHER_CTX_reset(ctx);
77 static int evp_cipher_init_internal(EVP_CIPHER_CTX *ctx,
78 const EVP_CIPHER *cipher,
79 ENGINE *impl, const unsigned char *key,
80 const unsigned char *iv, int enc,
81 const OSSL_PARAM params[])
84 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
85 ENGINE *tmpimpl = NULL;
88 * enc == 1 means we are encrypting.
89 * enc == 0 means we are decrypting.
90 * enc == -1 means, use the previously initialised value for encrypt/decrypt
100 if (cipher == NULL && ctx->cipher == NULL) {
101 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
105 /* Code below to be removed when legacy support is dropped. */
107 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
109 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
110 * this context may already have an ENGINE! Try to avoid releasing the
111 * previous handle, re-querying for an ENGINE, and having a
112 * reinitialisation, when it may all be unnecessary.
114 if (ctx->engine && ctx->cipher
115 && (cipher == NULL || cipher->nid == ctx->cipher->nid))
118 if (cipher != NULL && impl == NULL) {
119 /* Ask if an ENGINE is reserved for this job */
120 tmpimpl = ENGINE_get_cipher_engine(cipher->nid);
125 * If there are engines involved then we should use legacy handling for now.
127 if (ctx->engine != NULL
128 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
132 if (ctx->cipher == ctx->fetched_cipher)
134 EVP_CIPHER_free(ctx->fetched_cipher);
135 ctx->fetched_cipher = NULL;
139 * Ensure a context left lying around from last time is cleared
142 if (cipher != NULL && ctx->cipher != NULL) {
143 OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size);
144 ctx->cipher_data = NULL;
148 /* Start of non-legacy code below */
150 /* Ensure a context left lying around from last time is cleared */
151 if (cipher != NULL && ctx->cipher != NULL) {
152 unsigned long flags = ctx->flags;
154 EVP_CIPHER_CTX_reset(ctx);
155 /* Restore encrypt and flags */
161 cipher = ctx->cipher;
163 if (cipher->prov == NULL) {
165 /* We only do explicit fetches inside the FIPS module */
166 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
169 EVP_CIPHER *provciph =
170 EVP_CIPHER_fetch(NULL,
171 cipher->nid == NID_undef ? "NULL"
172 : OBJ_nid2sn(cipher->nid),
175 if (provciph == NULL)
178 EVP_CIPHER_free(ctx->fetched_cipher);
179 ctx->fetched_cipher = provciph;
183 if (cipher->prov != NULL) {
184 if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) {
185 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
188 EVP_CIPHER_free(ctx->fetched_cipher);
189 ctx->fetched_cipher = (EVP_CIPHER *)cipher;
191 ctx->cipher = cipher;
192 if (ctx->provctx == NULL) {
193 ctx->provctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
194 if (ctx->provctx == NULL) {
195 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
200 if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
202 * If this ctx was already set up for no padding then we need to tell
203 * the new cipher about it.
205 if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
210 if (ctx->cipher->einit == NULL) {
211 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
215 return ctx->cipher->einit(ctx->provctx,
218 : EVP_CIPHER_CTX_key_length(ctx),
221 : EVP_CIPHER_CTX_iv_length(ctx),
225 if (ctx->cipher->dinit == NULL) {
226 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
230 return ctx->cipher->dinit(ctx->provctx,
233 : EVP_CIPHER_CTX_key_length(ctx),
236 : EVP_CIPHER_CTX_iv_length(ctx),
239 /* Code below to be removed when legacy support is dropped. */
242 if (cipher != NULL) {
244 * Ensure a context left lying around from last time is cleared (we
245 * previously attempted to avoid this if the same ENGINE and
246 * EVP_CIPHER could be used).
249 unsigned long flags = ctx->flags;
250 EVP_CIPHER_CTX_reset(ctx);
251 /* Restore encrypt and flags */
255 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
257 if (!ENGINE_init(impl)) {
258 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
265 /* There's an ENGINE for this job ... (apparently) */
266 const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
270 * One positive side-effect of US's export control history,
271 * is that we should at least be able to avoid using US
272 * misspellings of "initialisation"?
274 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
277 /* We'll use the ENGINE's private cipher definition */
280 * Store the ENGINE functional reference so we know 'cipher' came
281 * from an ENGINE and we need to release it when done.
289 ctx->cipher = cipher;
290 if (ctx->cipher->ctx_size) {
291 ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
292 if (ctx->cipher_data == NULL) {
294 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
298 ctx->cipher_data = NULL;
300 ctx->key_len = cipher->key_len;
301 /* Preserve wrap enable flag, zero everything else */
302 ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
303 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
304 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
306 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
311 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
314 if (ctx->cipher == NULL)
317 /* we assume block size is a power of 2 in *cryptUpdate */
318 OPENSSL_assert(ctx->cipher->block_size == 1
319 || ctx->cipher->block_size == 8
320 || ctx->cipher->block_size == 16);
322 if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
323 && EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) {
324 ERR_raise(ERR_LIB_EVP, EVP_R_WRAP_MODE_NOT_ALLOWED);
328 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_CUSTOM_IV)) {
329 switch (EVP_CIPHER_CTX_mode(ctx)) {
331 case EVP_CIPH_STREAM_CIPHER:
332 case EVP_CIPH_ECB_MODE:
335 case EVP_CIPH_CFB_MODE:
336 case EVP_CIPH_OFB_MODE:
341 case EVP_CIPH_CBC_MODE:
342 n = EVP_CIPHER_CTX_iv_length(ctx);
343 if (!ossl_assert(n >= 0 && n <= (int)sizeof(ctx->iv)))
346 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
347 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
350 case EVP_CIPH_CTR_MODE:
352 /* Don't reuse IV for CTR mode */
354 memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
362 if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
363 if (!ctx->cipher->init(ctx, key, iv, enc))
368 ctx->block_mask = ctx->cipher->block_size - 1;
372 int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
373 const unsigned char *key, const unsigned char *iv,
374 int enc, const OSSL_PARAM params[])
376 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, params);
379 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
380 const unsigned char *key, const unsigned char *iv, int enc)
383 EVP_CIPHER_CTX_reset(ctx);
384 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, NULL);
387 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
388 ENGINE *impl, const unsigned char *key,
389 const unsigned char *iv, int enc)
391 return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, NULL);
394 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
395 const unsigned char *in, int inl)
398 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
400 return EVP_DecryptUpdate(ctx, out, outl, in, inl);
403 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
406 return EVP_EncryptFinal_ex(ctx, out, outl);
408 return EVP_DecryptFinal_ex(ctx, out, outl);
411 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
414 return EVP_EncryptFinal(ctx, out, outl);
416 return EVP_DecryptFinal(ctx, out, outl);
419 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
420 const unsigned char *key, const unsigned char *iv)
422 return EVP_CipherInit(ctx, cipher, key, iv, 1);
425 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
426 ENGINE *impl, const unsigned char *key,
427 const unsigned char *iv)
429 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
432 int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
433 const unsigned char *key, const unsigned char *iv,
434 const OSSL_PARAM params[])
436 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params);
439 int EVP_DecryptInit(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, 0);
445 int EVP_DecryptInit_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, 0);
452 int EVP_DecryptInit_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, 0, params);
460 * According to the letter of standard difference between pointers
461 * is specified to be valid only within same object. This makes
462 * it formally challenging to determine if input and output buffers
463 * are not partially overlapping with standard pointer arithmetic.
468 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
470 * Then we have VMS that distinguishes itself by adhering to
471 * sizeof(size_t)==4 even in 64-bit builds, which means that
472 * difference between two pointers might be truncated to 32 bits.
473 * In the context one can even wonder how comparison for
474 * equality is implemented. To be on the safe side we adhere to
475 * PTRDIFF_T even for comparison for equality.
477 # define PTRDIFF_T uint64_t
479 # define PTRDIFF_T size_t
482 int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
484 PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
486 * Check for partially overlapping buffers. [Binary logical
487 * operations are used instead of boolean to minimize number
488 * of conditional branches.]
490 int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
491 (diff > (0 - (PTRDIFF_T)len)));
496 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
497 unsigned char *out, int *outl,
498 const unsigned char *in, int inl)
500 int i, j, bl, cmpl = inl;
502 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
503 cmpl = (cmpl + 7) / 8;
505 bl = ctx->cipher->block_size;
507 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
508 /* If block size > 1 then the cipher will have to do this check */
509 if (bl == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
510 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
514 i = ctx->cipher->do_cipher(ctx, out, in, inl);
526 if (ossl_is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
527 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
531 if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
532 if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
541 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
544 memcpy(&(ctx->buf[i]), in, inl);
552 * Once we've processed the first j bytes from in, the amount of
553 * data left that is a multiple of the block length is:
554 * (inl - j) & ~(bl - 1)
555 * We must ensure that this amount of data, plus the one block that
556 * we process from ctx->buf does not exceed INT_MAX
558 if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
559 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
562 memcpy(&(ctx->buf[i]), in, j);
565 if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
575 if (!ctx->cipher->do_cipher(ctx, out, in, inl))
581 memcpy(ctx->buf, &(in[inl]), i);
587 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
588 const unsigned char *in, int inl)
597 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
601 /* Prevent accidental use of decryption context when encrypting */
603 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
607 if (ctx->cipher == NULL) {
608 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
612 if (ctx->cipher->prov == NULL)
615 blocksize = ctx->cipher->block_size;
617 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
618 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
621 ret = ctx->cipher->cupdate(ctx->provctx, out, &soutl,
622 inl + (blocksize == 1 ? 0 : blocksize), in,
626 if (soutl > INT_MAX) {
627 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
635 /* Code below to be removed when legacy support is dropped. */
638 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
641 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
644 ret = EVP_EncryptFinal_ex(ctx, out, outl);
648 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
651 unsigned int i, b, bl;
658 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
662 /* Prevent accidental use of decryption context when encrypting */
664 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
668 if (ctx->cipher == NULL) {
669 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
672 if (ctx->cipher->prov == NULL)
675 blocksize = EVP_CIPHER_CTX_block_size(ctx);
677 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
678 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
682 ret = ctx->cipher->cfinal(ctx->provctx, out, &soutl,
683 blocksize == 1 ? 0 : blocksize);
686 if (soutl > INT_MAX) {
687 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
695 /* Code below to be removed when legacy support is dropped. */
698 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
699 ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
707 b = ctx->cipher->block_size;
708 OPENSSL_assert(b <= sizeof(ctx->buf));
714 if (ctx->flags & EVP_CIPH_NO_PADDING) {
716 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
724 for (i = bl; i < b; i++)
726 ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
734 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
735 const unsigned char *in, int inl)
737 int fix_len, cmpl = inl, ret;
745 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
749 /* Prevent accidental use of encryption context when decrypting */
751 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
755 if (ctx->cipher == NULL) {
756 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
759 if (ctx->cipher->prov == NULL)
762 blocksize = EVP_CIPHER_CTX_block_size(ctx);
764 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
765 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
768 ret = ctx->cipher->cupdate(ctx->provctx, out, &soutl,
769 inl + (blocksize == 1 ? 0 : blocksize), in,
773 if (soutl > INT_MAX) {
774 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
782 /* Code below to be removed when legacy support is dropped. */
785 b = ctx->cipher->block_size;
787 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
788 cmpl = (cmpl + 7) / 8;
790 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
791 if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
792 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
796 fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
810 if (ctx->flags & EVP_CIPH_NO_PADDING)
811 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
813 OPENSSL_assert(b <= sizeof(ctx->final));
815 if (ctx->final_used) {
816 /* see comment about PTRDIFF_T comparison above */
817 if (((PTRDIFF_T)out == (PTRDIFF_T)in)
818 || ossl_is_partially_overlapping(out, in, b)) {
819 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
823 * final_used is only ever set if buf_len is 0. Therefore the maximum
824 * length output we will ever see from evp_EncryptDecryptUpdate is
825 * the maximum multiple of the block length that is <= inl, or just:
827 * Since final_used has been set then the final output length is:
828 * (inl & ~(b - 1)) + b
829 * This must never exceed INT_MAX
831 if ((inl & ~(b - 1)) > INT_MAX - b) {
832 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
835 memcpy(out, ctx->final, b);
841 if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
845 * if we have 'decrypted' a multiple of block size, make sure we have a
846 * copy of this last block
848 if (b > 1 && !ctx->buf_len) {
851 memcpy(ctx->final, &out[*outl], b);
861 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
864 ret = EVP_DecryptFinal_ex(ctx, out, outl);
868 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
879 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
883 /* Prevent accidental use of encryption context when decrypting */
885 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
889 if (ctx->cipher == NULL) {
890 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
894 if (ctx->cipher->prov == NULL)
897 blocksize = EVP_CIPHER_CTX_block_size(ctx);
899 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
900 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
904 ret = ctx->cipher->cfinal(ctx->provctx, out, &soutl,
905 blocksize == 1 ? 0 : blocksize);
908 if (soutl > INT_MAX) {
909 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
917 /* Code below to be removed when legacy support is dropped. */
921 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
922 i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
930 b = ctx->cipher->block_size;
931 if (ctx->flags & EVP_CIPH_NO_PADDING) {
933 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
940 if (ctx->buf_len || !ctx->final_used) {
941 ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
944 OPENSSL_assert(b <= sizeof(ctx->final));
947 * The following assumes that the ciphertext has been authenticated.
948 * Otherwise it provides a padding oracle.
950 n = ctx->final[b - 1];
951 if (n == 0 || n > (int)b) {
952 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
955 for (i = 0; i < n; i++) {
956 if (ctx->final[--b] != n) {
957 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
961 n = ctx->cipher->block_size - n;
962 for (i = 0; i < n; i++)
963 out[i] = ctx->final[i];
970 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
972 if (c->cipher->prov != NULL) {
974 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
977 if (EVP_CIPHER_CTX_key_length(c) == keylen)
980 /* Check the cipher actually understands this parameter */
981 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
982 OSSL_CIPHER_PARAM_KEYLEN) == NULL)
985 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
986 ok = evp_do_ciph_ctx_setparams(c->cipher, c->provctx, params);
988 return ok > 0 ? 1 : 0;
991 /* Code below to be removed when legacy support is dropped. */
994 * Note there have never been any built-in ciphers that define this flag
995 * since it was first introduced.
997 if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
998 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
999 if (EVP_CIPHER_CTX_key_length(c) == keylen)
1001 if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
1002 c->key_len = keylen;
1005 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1009 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
1012 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1013 unsigned int pd = pad;
1016 ctx->flags &= ~EVP_CIPH_NO_PADDING;
1018 ctx->flags |= EVP_CIPH_NO_PADDING;
1020 if (ctx->cipher != NULL && ctx->cipher->prov == NULL)
1022 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
1023 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1028 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1030 int ret = EVP_CTRL_RET_UNSUPPORTED;
1034 OSSL_PARAM params[4] = {
1035 OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
1038 if (ctx == NULL || ctx->cipher == NULL) {
1039 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
1043 if (ctx->cipher->prov == NULL)
1047 case EVP_CTRL_SET_KEY_LENGTH:
1048 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
1050 case EVP_CTRL_RAND_KEY: /* Used by DES */
1053 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
1059 * EVP_CTRL_INIT is purely legacy, no provider counterpart.
1060 * As a matter of fact, this should be dead code, but some caller
1061 * might still do a direct control call with this command, so...
1062 * Legacy methods return 1 except for exceptional circumstances, so
1063 * we do the same here to not be disruptive.
1066 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
1069 case EVP_CTRL_AEAD_SET_IVLEN:
1072 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1074 case EVP_CTRL_AEAD_SET_IV_FIXED:
1075 params[0] = OSSL_PARAM_construct_octet_string(
1076 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
1078 case EVP_CTRL_GCM_IV_GEN:
1081 sz = 0; /* special case that uses the iv length */
1082 params[0] = OSSL_PARAM_construct_octet_string(
1083 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
1085 case EVP_CTRL_GCM_SET_IV_INV:
1088 params[0] = OSSL_PARAM_construct_octet_string(
1089 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
1091 case EVP_CTRL_GET_RC5_ROUNDS:
1092 set_params = 0; /* Fall thru */
1093 case EVP_CTRL_SET_RC5_ROUNDS:
1096 i = (unsigned int)arg;
1097 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
1099 case EVP_CTRL_SET_SPEED:
1102 i = (unsigned int)arg;
1103 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
1105 case EVP_CTRL_AEAD_GET_TAG:
1106 set_params = 0; /* Fall thru */
1107 case EVP_CTRL_AEAD_SET_TAG:
1108 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1111 case EVP_CTRL_AEAD_TLS1_AAD:
1112 /* This one does a set and a get - since it returns a size */
1114 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1116 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1120 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1121 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
1125 #ifndef OPENSSL_NO_RC2
1126 case EVP_CTRL_GET_RC2_KEY_BITS:
1127 set_params = 0; /* Fall thru */
1128 case EVP_CTRL_SET_RC2_KEY_BITS:
1129 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
1131 #endif /* OPENSSL_NO_RC2 */
1132 #if !defined(OPENSSL_NO_MULTIBLOCK)
1133 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
1134 params[0] = OSSL_PARAM_construct_size_t(
1135 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
1136 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1140 params[0] = OSSL_PARAM_construct_size_t(
1141 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
1142 params[1] = OSSL_PARAM_construct_end();
1143 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
1147 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
1148 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1149 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1151 if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
1154 params[0] = OSSL_PARAM_construct_octet_string(
1155 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void*)p->inp, p->len);
1156 params[1] = OSSL_PARAM_construct_uint(
1157 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1158 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1161 /* Retrieve the return values changed by the set */
1162 params[0] = OSSL_PARAM_construct_size_t(
1163 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
1164 params[1] = OSSL_PARAM_construct_uint(
1165 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1166 params[2] = OSSL_PARAM_construct_end();
1167 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
1172 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
1173 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1174 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1176 params[0] = OSSL_PARAM_construct_octet_string(
1177 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
1179 params[1] = OSSL_PARAM_construct_octet_string(
1180 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void*)p->inp,
1182 params[2] = OSSL_PARAM_construct_uint(
1183 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1184 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1187 params[0] = OSSL_PARAM_construct_size_t(
1188 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
1189 params[1] = OSSL_PARAM_construct_end();
1190 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
1195 #endif /* OPENSSL_NO_MULTIBLOCK */
1196 case EVP_CTRL_AEAD_SET_MAC_KEY:
1199 params[0] = OSSL_PARAM_construct_octet_string(
1200 OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
1205 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1207 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
1210 /* Code below to be removed when legacy support is dropped. */
1212 if (ctx->cipher->ctrl == NULL) {
1213 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
1217 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
1220 if (ret == EVP_CTRL_RET_UNSUPPORTED) {
1221 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
1227 int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1229 if (cipher != NULL && cipher->get_params != NULL)
1230 return cipher->get_params(params);
1234 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1236 if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL)
1237 return ctx->cipher->set_ctx_params(ctx->provctx, params);
1241 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1243 if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1244 return ctx->cipher->get_ctx_params(ctx->provctx, params);
1248 const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1250 if (cipher != NULL && cipher->gettable_params != NULL)
1251 return cipher->gettable_params(
1252 ossl_provider_ctx(EVP_CIPHER_provider(cipher)));
1256 const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
1260 if (cipher != NULL && cipher->settable_ctx_params != NULL) {
1261 alg = ossl_provider_ctx(EVP_CIPHER_provider(cipher));
1262 return cipher->settable_ctx_params(NULL, alg);
1267 const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
1271 if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
1272 alg = ossl_provider_ctx(EVP_CIPHER_provider(cipher));
1273 return cipher->gettable_ctx_params(NULL, alg);
1278 const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx)
1282 if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
1283 alg = ossl_provider_ctx(EVP_CIPHER_provider(cctx->cipher));
1284 return cctx->cipher->settable_ctx_params(cctx->provctx, alg);
1289 const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx)
1293 if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
1294 alg = ossl_provider_ctx(EVP_CIPHER_provider(cctx->cipher));
1295 return cctx->cipher->gettable_ctx_params(cctx->provctx, alg);
1301 static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx)
1303 const EVP_CIPHER *cipher = ctx->cipher;
1304 const OSSL_PROVIDER *prov;
1309 prov = EVP_CIPHER_provider(cipher);
1310 return ossl_provider_libctx(prov);
1314 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
1316 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1317 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1324 OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
1326 kl = EVP_CIPHER_CTX_key_length(ctx);
1327 if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl) <= 0)
1331 #endif /* FIPS_MODULE */
1334 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
1336 if ((in == NULL) || (in->cipher == NULL)) {
1337 ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
1341 if (in->cipher->prov == NULL)
1344 if (in->cipher->dupctx == NULL) {
1345 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1349 EVP_CIPHER_CTX_reset(out);
1352 out->provctx = NULL;
1354 if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
1355 out->fetched_cipher = NULL;
1359 out->provctx = in->cipher->dupctx(in->provctx);
1360 if (out->provctx == NULL) {
1361 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1367 /* Code below to be removed when legacy support is dropped. */
1370 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1371 /* Make sure it's safe to copy a cipher context using an ENGINE */
1372 if (in->engine && !ENGINE_init(in->engine)) {
1373 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
1378 EVP_CIPHER_CTX_reset(out);
1379 memcpy(out, in, sizeof(*out));
1381 if (in->cipher_data && in->cipher->ctx_size) {
1382 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
1383 if (out->cipher_data == NULL) {
1385 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1388 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
1391 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
1392 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
1394 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
1400 EVP_CIPHER *evp_cipher_new(void)
1402 EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1404 if (cipher != NULL) {
1405 cipher->lock = CRYPTO_THREAD_lock_new();
1406 if (cipher->lock == NULL) {
1407 OPENSSL_free(cipher);
1416 * FIPS module note: since internal fetches will be entirely
1417 * provider based, we know that none of its code depends on legacy
1418 * NIDs or any functionality that use them.
1421 /* After removal of legacy support get rid of the need for legacy NIDs */
1422 static void set_legacy_nid(const char *name, void *vlegacy_nid)
1425 int *legacy_nid = vlegacy_nid;
1427 * We use lowest level function to get the associated method, because
1428 * higher level functions such as EVP_get_cipherbyname() have changed
1429 * to look at providers too.
1431 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
1433 if (*legacy_nid == -1) /* We found a clash already */
1435 if (legacy_method == NULL)
1437 nid = EVP_CIPHER_nid(legacy_method);
1438 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
1446 static void *evp_cipher_from_algorithm(const int name_id,
1447 const OSSL_ALGORITHM *algodef,
1448 OSSL_PROVIDER *prov)
1450 const OSSL_DISPATCH *fns = algodef->implementation;
1451 EVP_CIPHER *cipher = NULL;
1452 int fnciphcnt = 0, fnctxcnt = 0;
1454 if ((cipher = evp_cipher_new()) == NULL) {
1455 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1460 cipher->nid = NID_undef;
1461 if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
1462 || cipher->nid == -1) {
1463 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1464 EVP_CIPHER_free(cipher);
1469 cipher->name_id = name_id;
1470 cipher->description = algodef->algorithm_description;
1472 for (; fns->function_id != 0; fns++) {
1473 switch (fns->function_id) {
1474 case OSSL_FUNC_CIPHER_NEWCTX:
1475 if (cipher->newctx != NULL)
1477 cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
1480 case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1481 if (cipher->einit != NULL)
1483 cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
1486 case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1487 if (cipher->dinit != NULL)
1489 cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
1492 case OSSL_FUNC_CIPHER_UPDATE:
1493 if (cipher->cupdate != NULL)
1495 cipher->cupdate = OSSL_FUNC_cipher_update(fns);
1498 case OSSL_FUNC_CIPHER_FINAL:
1499 if (cipher->cfinal != NULL)
1501 cipher->cfinal = OSSL_FUNC_cipher_final(fns);
1504 case OSSL_FUNC_CIPHER_CIPHER:
1505 if (cipher->ccipher != NULL)
1507 cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
1509 case OSSL_FUNC_CIPHER_FREECTX:
1510 if (cipher->freectx != NULL)
1512 cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
1515 case OSSL_FUNC_CIPHER_DUPCTX:
1516 if (cipher->dupctx != NULL)
1518 cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
1520 case OSSL_FUNC_CIPHER_GET_PARAMS:
1521 if (cipher->get_params != NULL)
1523 cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
1525 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1526 if (cipher->get_ctx_params != NULL)
1528 cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
1530 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1531 if (cipher->set_ctx_params != NULL)
1533 cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
1535 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1536 if (cipher->gettable_params != NULL)
1538 cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
1540 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1541 if (cipher->gettable_ctx_params != NULL)
1543 cipher->gettable_ctx_params =
1544 OSSL_FUNC_cipher_gettable_ctx_params(fns);
1546 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1547 if (cipher->settable_ctx_params != NULL)
1549 cipher->settable_ctx_params =
1550 OSSL_FUNC_cipher_settable_ctx_params(fns);
1554 if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1555 || (fnciphcnt == 0 && cipher->ccipher == NULL)
1558 * In order to be a consistent set of functions we must have at least
1559 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1560 * functions, or a single "cipher" function. In all cases we need both
1561 * the "newctx" and "freectx" functions.
1563 EVP_CIPHER_free(cipher);
1564 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1567 cipher->prov = prov;
1569 ossl_provider_up_ref(prov);
1571 if (!evp_cipher_cache_constants(cipher)) {
1572 EVP_CIPHER_free(cipher);
1573 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1580 static int evp_cipher_up_ref(void *cipher)
1582 return EVP_CIPHER_up_ref(cipher);
1585 static void evp_cipher_free(void *cipher)
1587 EVP_CIPHER_free(cipher);
1590 EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1591 const char *properties)
1593 EVP_CIPHER *cipher =
1594 evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1595 evp_cipher_from_algorithm, evp_cipher_up_ref,
1601 int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
1605 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
1609 void EVP_CIPHER_free(EVP_CIPHER *cipher)
1613 if (cipher == NULL || cipher->prov == NULL)
1616 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
1619 ossl_provider_free(cipher->prov);
1620 CRYPTO_THREAD_lock_free(cipher->lock);
1621 OPENSSL_free(cipher);
1624 void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
1625 void (*fn)(EVP_CIPHER *mac, void *arg),
1628 evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1629 (void (*)(void *, void *))fn, arg,
1630 evp_cipher_from_algorithm, evp_cipher_free);