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>
19 #include <openssl/engine.h>
20 #include <openssl/params.h>
21 #include <openssl/core_names.h>
22 #include "internal/cryptlib.h"
23 #include "internal/provider.h"
24 #include "internal/core.h"
25 #include "crypto/evp.h"
26 #include "evp_local.h"
28 int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
33 if (ctx->cipher == NULL || ctx->cipher->prov == NULL)
36 if (ctx->algctx != NULL) {
37 if (ctx->cipher->freectx != NULL)
38 ctx->cipher->freectx(ctx->algctx);
41 if (ctx->fetched_cipher != NULL)
42 EVP_CIPHER_free(ctx->fetched_cipher);
43 memset(ctx, 0, sizeof(*ctx));
47 /* Remove legacy code below when legacy support is removed. */
50 if (ctx->cipher != NULL) {
51 if (ctx->cipher->cleanup && !ctx->cipher->cleanup(ctx))
53 /* Cleanse cipher context data */
54 if (ctx->cipher_data && ctx->cipher->ctx_size)
55 OPENSSL_cleanse(ctx->cipher_data, ctx->cipher->ctx_size);
57 OPENSSL_free(ctx->cipher_data);
58 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
59 ENGINE_finish(ctx->engine);
61 memset(ctx, 0, sizeof(*ctx));
65 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
67 return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
70 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
74 EVP_CIPHER_CTX_reset(ctx);
78 static int evp_cipher_init_internal(EVP_CIPHER_CTX *ctx,
79 const EVP_CIPHER *cipher,
80 ENGINE *impl, const unsigned char *key,
81 const unsigned char *iv, int enc,
82 const OSSL_PARAM params[])
85 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
86 ENGINE *tmpimpl = NULL;
89 * enc == 1 means we are encrypting.
90 * enc == 0 means we are decrypting.
91 * enc == -1 means, use the previously initialised value for encrypt/decrypt
101 if (cipher == NULL && ctx->cipher == NULL) {
102 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
106 /* Code below to be removed when legacy support is dropped. */
108 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
110 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
111 * this context may already have an ENGINE! Try to avoid releasing the
112 * previous handle, re-querying for an ENGINE, and having a
113 * reinitialisation, when it may all be unnecessary.
115 if (ctx->engine && ctx->cipher
116 && (cipher == NULL || cipher->nid == ctx->cipher->nid))
119 if (cipher != NULL && impl == NULL) {
120 /* Ask if an ENGINE is reserved for this job */
121 tmpimpl = ENGINE_get_cipher_engine(cipher->nid);
126 * If there are engines involved then we should use legacy handling for now.
128 if (ctx->engine != NULL
129 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
133 if (ctx->cipher == ctx->fetched_cipher)
135 EVP_CIPHER_free(ctx->fetched_cipher);
136 ctx->fetched_cipher = NULL;
140 * Ensure a context left lying around from last time is cleared
143 if (cipher != NULL && ctx->cipher != NULL) {
144 OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size);
145 ctx->cipher_data = NULL;
149 /* Start of non-legacy code below */
151 /* Ensure a context left lying around from last time is cleared */
152 if (cipher != NULL && ctx->cipher != NULL) {
153 unsigned long flags = ctx->flags;
155 EVP_CIPHER_CTX_reset(ctx);
156 /* Restore encrypt and flags */
162 cipher = ctx->cipher;
164 if (cipher->prov == NULL) {
166 /* We only do explicit fetches inside the FIPS module */
167 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
170 EVP_CIPHER *provciph =
171 EVP_CIPHER_fetch(NULL,
172 cipher->nid == NID_undef ? "NULL"
173 : OBJ_nid2sn(cipher->nid),
176 if (provciph == NULL)
179 EVP_CIPHER_free(ctx->fetched_cipher);
180 ctx->fetched_cipher = provciph;
184 if (cipher->prov != NULL) {
185 if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) {
186 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
189 EVP_CIPHER_free(ctx->fetched_cipher);
190 ctx->fetched_cipher = (EVP_CIPHER *)cipher;
192 ctx->cipher = cipher;
193 if (ctx->algctx == NULL) {
194 ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
195 if (ctx->algctx == NULL) {
196 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
201 if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
203 * If this ctx was already set up for no padding then we need to tell
204 * the new cipher about it.
206 if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
211 if (ctx->cipher->einit == NULL) {
212 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
216 return ctx->cipher->einit(ctx->algctx,
219 : EVP_CIPHER_CTX_get_key_length(ctx),
222 : EVP_CIPHER_CTX_get_iv_length(ctx),
226 if (ctx->cipher->dinit == NULL) {
227 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
231 return ctx->cipher->dinit(ctx->algctx,
234 : EVP_CIPHER_CTX_get_key_length(ctx),
237 : EVP_CIPHER_CTX_get_iv_length(ctx),
240 /* Code below to be removed when legacy support is dropped. */
243 if (cipher != NULL) {
245 * Ensure a context left lying around from last time is cleared (we
246 * previously attempted to avoid this if the same ENGINE and
247 * EVP_CIPHER could be used).
250 unsigned long flags = ctx->flags;
251 EVP_CIPHER_CTX_reset(ctx);
252 /* Restore encrypt and flags */
256 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
258 if (!ENGINE_init(impl)) {
259 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
266 /* There's an ENGINE for this job ... (apparently) */
267 const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
271 * One positive side-effect of US's export control history,
272 * is that we should at least be able to avoid using US
273 * misspellings of "initialisation"?
275 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
278 /* We'll use the ENGINE's private cipher definition */
281 * Store the ENGINE functional reference so we know 'cipher' came
282 * from an ENGINE and we need to release it when done.
290 ctx->cipher = cipher;
291 if (ctx->cipher->ctx_size) {
292 ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
293 if (ctx->cipher_data == NULL) {
295 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
299 ctx->cipher_data = NULL;
301 ctx->key_len = cipher->key_len;
302 /* Preserve wrap enable flag, zero everything else */
303 ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
304 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
305 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
307 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
312 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
315 if (ctx->cipher == NULL)
318 /* we assume block size is a power of 2 in *cryptUpdate */
319 OPENSSL_assert(ctx->cipher->block_size == 1
320 || ctx->cipher->block_size == 8
321 || ctx->cipher->block_size == 16);
323 if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
324 && EVP_CIPHER_CTX_get_mode(ctx) == EVP_CIPH_WRAP_MODE) {
325 ERR_raise(ERR_LIB_EVP, EVP_R_WRAP_MODE_NOT_ALLOWED);
329 if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
330 & EVP_CIPH_CUSTOM_IV) == 0) {
331 switch (EVP_CIPHER_CTX_get_mode(ctx)) {
333 case EVP_CIPH_STREAM_CIPHER:
334 case EVP_CIPH_ECB_MODE:
337 case EVP_CIPH_CFB_MODE:
338 case EVP_CIPH_OFB_MODE:
343 case EVP_CIPH_CBC_MODE:
344 n = EVP_CIPHER_CTX_get_iv_length(ctx);
345 if (!ossl_assert(n >= 0 && n <= (int)sizeof(ctx->iv)))
348 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_get_iv_length(ctx));
349 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_get_iv_length(ctx));
352 case EVP_CIPH_CTR_MODE:
354 /* Don't reuse IV for CTR mode */
356 if ((n = EVP_CIPHER_CTX_get_iv_length(ctx)) <= 0)
358 memcpy(ctx->iv, iv, n);
367 if (key != NULL || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
368 if (!ctx->cipher->init(ctx, key, iv, enc))
373 ctx->block_mask = ctx->cipher->block_size - 1;
377 int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
378 const unsigned char *key, const unsigned char *iv,
379 int enc, const OSSL_PARAM params[])
381 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, params);
384 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
385 const unsigned char *key, const unsigned char *iv, int enc)
388 EVP_CIPHER_CTX_reset(ctx);
389 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, NULL);
392 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
393 ENGINE *impl, const unsigned char *key,
394 const unsigned char *iv, int enc)
396 return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, NULL);
399 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
400 const unsigned char *in, int inl)
403 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
405 return EVP_DecryptUpdate(ctx, out, outl, in, inl);
408 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
411 return EVP_EncryptFinal_ex(ctx, out, outl);
413 return EVP_DecryptFinal_ex(ctx, out, outl);
416 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
419 return EVP_EncryptFinal(ctx, out, outl);
421 return EVP_DecryptFinal(ctx, out, outl);
424 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
425 const unsigned char *key, const unsigned char *iv)
427 return EVP_CipherInit(ctx, cipher, key, iv, 1);
430 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
431 ENGINE *impl, const unsigned char *key,
432 const unsigned char *iv)
434 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
437 int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
438 const unsigned char *key, const unsigned char *iv,
439 const OSSL_PARAM params[])
441 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params);
444 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
445 const unsigned char *key, const unsigned char *iv)
447 return EVP_CipherInit(ctx, cipher, key, iv, 0);
450 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
451 ENGINE *impl, const unsigned char *key,
452 const unsigned char *iv)
454 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
457 int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
458 const unsigned char *key, const unsigned char *iv,
459 const OSSL_PARAM params[])
461 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 0, params);
465 * According to the letter of standard difference between pointers
466 * is specified to be valid only within same object. This makes
467 * it formally challenging to determine if input and output buffers
468 * are not partially overlapping with standard pointer arithmetic.
473 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
475 * Then we have VMS that distinguishes itself by adhering to
476 * sizeof(size_t)==4 even in 64-bit builds, which means that
477 * difference between two pointers might be truncated to 32 bits.
478 * In the context one can even wonder how comparison for
479 * equality is implemented. To be on the safe side we adhere to
480 * PTRDIFF_T even for comparison for equality.
482 # define PTRDIFF_T uint64_t
484 # define PTRDIFF_T size_t
487 int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
489 PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
491 * Check for partially overlapping buffers. [Binary logical
492 * operations are used instead of boolean to minimize number
493 * of conditional branches.]
495 int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
496 (diff > (0 - (PTRDIFF_T)len)));
501 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
502 unsigned char *out, int *outl,
503 const unsigned char *in, int inl)
505 int i, j, bl, cmpl = inl;
507 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
508 cmpl = (cmpl + 7) / 8;
510 bl = ctx->cipher->block_size;
512 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
513 /* If block size > 1 then the cipher will have to do this check */
514 if (bl == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
515 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
519 i = ctx->cipher->do_cipher(ctx, out, in, inl);
531 if (ossl_is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
532 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
536 if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
537 if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
546 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
549 memcpy(&(ctx->buf[i]), in, inl);
557 * Once we've processed the first j bytes from in, the amount of
558 * data left that is a multiple of the block length is:
559 * (inl - j) & ~(bl - 1)
560 * We must ensure that this amount of data, plus the one block that
561 * we process from ctx->buf does not exceed INT_MAX
563 if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
564 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
567 memcpy(&(ctx->buf[i]), in, j);
570 if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
580 if (!ctx->cipher->do_cipher(ctx, out, in, inl))
586 memcpy(ctx->buf, &(in[inl]), i);
592 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
593 const unsigned char *in, int inl)
602 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
606 /* Prevent accidental use of decryption context when encrypting */
608 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
612 if (ctx->cipher == NULL) {
613 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
617 if (ctx->cipher->prov == NULL)
620 blocksize = ctx->cipher->block_size;
622 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
623 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
626 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
627 inl + (blocksize == 1 ? 0 : blocksize), in,
631 if (soutl > INT_MAX) {
632 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
640 /* Code below to be removed when legacy support is dropped. */
643 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
646 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
649 ret = EVP_EncryptFinal_ex(ctx, out, outl);
653 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
656 unsigned int i, b, bl;
663 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
667 /* Prevent accidental use of decryption context when encrypting */
669 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
673 if (ctx->cipher == NULL) {
674 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
677 if (ctx->cipher->prov == NULL)
680 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
682 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
683 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
687 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
688 blocksize == 1 ? 0 : blocksize);
691 if (soutl > INT_MAX) {
692 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
700 /* Code below to be removed when legacy support is dropped. */
703 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
704 ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
712 b = ctx->cipher->block_size;
713 OPENSSL_assert(b <= sizeof(ctx->buf));
719 if (ctx->flags & EVP_CIPH_NO_PADDING) {
721 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
729 for (i = bl; i < b; i++)
731 ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
739 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
740 const unsigned char *in, int inl)
742 int fix_len, cmpl = inl, ret;
750 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
754 /* Prevent accidental use of encryption context when decrypting */
756 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
760 if (ctx->cipher == NULL) {
761 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
764 if (ctx->cipher->prov == NULL)
767 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
769 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
770 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
773 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
774 inl + (blocksize == 1 ? 0 : blocksize), in,
778 if (soutl > INT_MAX) {
779 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
787 /* Code below to be removed when legacy support is dropped. */
790 b = ctx->cipher->block_size;
792 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
793 cmpl = (cmpl + 7) / 8;
795 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
796 if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
797 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
801 fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
815 if (ctx->flags & EVP_CIPH_NO_PADDING)
816 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
818 OPENSSL_assert(b <= sizeof(ctx->final));
820 if (ctx->final_used) {
821 /* see comment about PTRDIFF_T comparison above */
822 if (((PTRDIFF_T)out == (PTRDIFF_T)in)
823 || ossl_is_partially_overlapping(out, in, b)) {
824 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
828 * final_used is only ever set if buf_len is 0. Therefore the maximum
829 * length output we will ever see from evp_EncryptDecryptUpdate is
830 * the maximum multiple of the block length that is <= inl, or just:
832 * Since final_used has been set then the final output length is:
833 * (inl & ~(b - 1)) + b
834 * This must never exceed INT_MAX
836 if ((inl & ~(b - 1)) > INT_MAX - b) {
837 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
840 memcpy(out, ctx->final, b);
846 if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
850 * if we have 'decrypted' a multiple of block size, make sure we have a
851 * copy of this last block
853 if (b > 1 && !ctx->buf_len) {
856 memcpy(ctx->final, &out[*outl], b);
866 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
869 ret = EVP_DecryptFinal_ex(ctx, out, outl);
873 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
884 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
888 /* Prevent accidental use of encryption context when decrypting */
890 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
894 if (ctx->cipher == NULL) {
895 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
899 if (ctx->cipher->prov == NULL)
902 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
904 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
905 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
909 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
910 blocksize == 1 ? 0 : blocksize);
913 if (soutl > INT_MAX) {
914 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
922 /* Code below to be removed when legacy support is dropped. */
926 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
927 i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
935 b = ctx->cipher->block_size;
936 if (ctx->flags & EVP_CIPH_NO_PADDING) {
938 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
945 if (ctx->buf_len || !ctx->final_used) {
946 ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
949 OPENSSL_assert(b <= sizeof(ctx->final));
952 * The following assumes that the ciphertext has been authenticated.
953 * Otherwise it provides a padding oracle.
955 n = ctx->final[b - 1];
956 if (n == 0 || n > (int)b) {
957 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
960 for (i = 0; i < n; i++) {
961 if (ctx->final[--b] != n) {
962 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
966 n = ctx->cipher->block_size - n;
967 for (i = 0; i < n; i++)
968 out[i] = ctx->final[i];
975 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
977 if (c->cipher->prov != NULL) {
979 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
982 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
985 /* Check the cipher actually understands this parameter */
986 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
987 OSSL_CIPHER_PARAM_KEYLEN) == NULL)
990 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
991 ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params);
993 return ok > 0 ? 1 : 0;
996 /* Code below to be removed when legacy support is dropped. */
999 * Note there have never been any built-in ciphers that define this flag
1000 * since it was first introduced.
1002 if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
1003 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
1004 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
1006 if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
1007 c->key_len = keylen;
1010 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1014 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
1017 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1018 unsigned int pd = pad;
1021 ctx->flags &= ~EVP_CIPH_NO_PADDING;
1023 ctx->flags |= EVP_CIPH_NO_PADDING;
1025 if (ctx->cipher != NULL && ctx->cipher->prov == NULL)
1027 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
1028 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1033 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1035 int ret = EVP_CTRL_RET_UNSUPPORTED;
1039 OSSL_PARAM params[4] = {
1040 OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
1043 if (ctx == NULL || ctx->cipher == NULL) {
1044 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
1048 if (ctx->cipher->prov == NULL)
1052 case EVP_CTRL_SET_KEY_LENGTH:
1053 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
1055 case EVP_CTRL_RAND_KEY: /* Used by DES */
1058 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
1064 * EVP_CTRL_INIT is purely legacy, no provider counterpart.
1065 * As a matter of fact, this should be dead code, but some caller
1066 * might still do a direct control call with this command, so...
1067 * Legacy methods return 1 except for exceptional circumstances, so
1068 * we do the same here to not be disruptive.
1071 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
1074 case EVP_CTRL_AEAD_SET_IVLEN:
1077 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1079 case EVP_CTRL_CCM_SET_L:
1080 if (arg < 2 || arg > 8)
1083 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1085 case EVP_CTRL_AEAD_SET_IV_FIXED:
1086 params[0] = OSSL_PARAM_construct_octet_string(
1087 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
1089 case EVP_CTRL_GCM_IV_GEN:
1092 sz = 0; /* special case that uses the iv length */
1093 params[0] = OSSL_PARAM_construct_octet_string(
1094 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
1096 case EVP_CTRL_GCM_SET_IV_INV:
1099 params[0] = OSSL_PARAM_construct_octet_string(
1100 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
1102 case EVP_CTRL_GET_RC5_ROUNDS:
1103 set_params = 0; /* Fall thru */
1104 case EVP_CTRL_SET_RC5_ROUNDS:
1107 i = (unsigned int)arg;
1108 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
1110 case EVP_CTRL_SET_SPEED:
1113 i = (unsigned int)arg;
1114 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
1116 case EVP_CTRL_AEAD_GET_TAG:
1117 set_params = 0; /* Fall thru */
1118 case EVP_CTRL_AEAD_SET_TAG:
1119 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1122 case EVP_CTRL_AEAD_TLS1_AAD:
1123 /* This one does a set and a get - since it returns a size */
1125 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1127 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1131 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1132 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1136 #ifndef OPENSSL_NO_RC2
1137 case EVP_CTRL_GET_RC2_KEY_BITS:
1138 set_params = 0; /* Fall thru */
1139 case EVP_CTRL_SET_RC2_KEY_BITS:
1140 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
1142 #endif /* OPENSSL_NO_RC2 */
1143 #if !defined(OPENSSL_NO_MULTIBLOCK)
1144 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
1145 params[0] = OSSL_PARAM_construct_size_t(
1146 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
1147 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1151 params[0] = OSSL_PARAM_construct_size_t(
1152 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
1153 params[1] = OSSL_PARAM_construct_end();
1154 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1158 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
1159 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1160 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1162 if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
1165 params[0] = OSSL_PARAM_construct_octet_string(
1166 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void*)p->inp, p->len);
1167 params[1] = OSSL_PARAM_construct_uint(
1168 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1169 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1172 /* Retrieve the return values changed by the set */
1173 params[0] = OSSL_PARAM_construct_size_t(
1174 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
1175 params[1] = OSSL_PARAM_construct_uint(
1176 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1177 params[2] = OSSL_PARAM_construct_end();
1178 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1183 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
1184 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1185 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1187 params[0] = OSSL_PARAM_construct_octet_string(
1188 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
1190 params[1] = OSSL_PARAM_construct_octet_string(
1191 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void*)p->inp,
1193 params[2] = OSSL_PARAM_construct_uint(
1194 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1195 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1198 params[0] = OSSL_PARAM_construct_size_t(
1199 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
1200 params[1] = OSSL_PARAM_construct_end();
1201 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1206 #endif /* OPENSSL_NO_MULTIBLOCK */
1207 case EVP_CTRL_AEAD_SET_MAC_KEY:
1210 params[0] = OSSL_PARAM_construct_octet_string(
1211 OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
1216 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1218 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1221 /* Code below to be removed when legacy support is dropped. */
1223 if (ctx->cipher->ctrl == NULL) {
1224 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
1228 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
1231 if (ret == EVP_CTRL_RET_UNSUPPORTED) {
1232 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
1238 int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1240 if (cipher != NULL && cipher->get_params != NULL)
1241 return cipher->get_params(params);
1245 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1247 if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL)
1248 return ctx->cipher->set_ctx_params(ctx->algctx, params);
1252 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1254 if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1255 return ctx->cipher->get_ctx_params(ctx->algctx, params);
1259 const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1261 if (cipher != NULL && cipher->gettable_params != NULL)
1262 return cipher->gettable_params(
1263 ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)));
1267 const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
1271 if (cipher != NULL && cipher->settable_ctx_params != NULL) {
1272 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1273 return cipher->settable_ctx_params(NULL, provctx);
1278 const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
1282 if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
1283 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1284 return cipher->gettable_ctx_params(NULL, provctx);
1289 const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx)
1293 if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
1294 alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1295 return cctx->cipher->settable_ctx_params(cctx->algctx, alg);
1300 const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx)
1304 if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
1305 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1306 return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx);
1312 static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx)
1314 const EVP_CIPHER *cipher = ctx->cipher;
1315 const OSSL_PROVIDER *prov;
1320 prov = EVP_CIPHER_get0_provider(cipher);
1321 return ossl_provider_libctx(prov);
1325 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
1327 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1328 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1335 OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
1337 kl = EVP_CIPHER_CTX_get_key_length(ctx);
1338 if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0)
1342 #endif /* FIPS_MODULE */
1345 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
1347 if ((in == NULL) || (in->cipher == NULL)) {
1348 ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
1352 if (in->cipher->prov == NULL)
1355 if (in->cipher->dupctx == NULL) {
1356 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1360 EVP_CIPHER_CTX_reset(out);
1365 if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
1366 out->fetched_cipher = NULL;
1370 out->algctx = in->cipher->dupctx(in->algctx);
1371 if (out->algctx == NULL) {
1372 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1378 /* Code below to be removed when legacy support is dropped. */
1381 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1382 /* Make sure it's safe to copy a cipher context using an ENGINE */
1383 if (in->engine && !ENGINE_init(in->engine)) {
1384 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
1389 EVP_CIPHER_CTX_reset(out);
1390 memcpy(out, in, sizeof(*out));
1392 if (in->cipher_data && in->cipher->ctx_size) {
1393 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
1394 if (out->cipher_data == NULL) {
1396 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1399 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
1402 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
1403 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
1405 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
1411 EVP_CIPHER *evp_cipher_new(void)
1413 EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1415 if (cipher != NULL) {
1416 cipher->lock = CRYPTO_THREAD_lock_new();
1417 if (cipher->lock == NULL) {
1418 OPENSSL_free(cipher);
1427 * FIPS module note: since internal fetches will be entirely
1428 * provider based, we know that none of its code depends on legacy
1429 * NIDs or any functionality that use them.
1432 /* After removal of legacy support get rid of the need for legacy NIDs */
1433 static void set_legacy_nid(const char *name, void *vlegacy_nid)
1436 int *legacy_nid = vlegacy_nid;
1438 * We use lowest level function to get the associated method, because
1439 * higher level functions such as EVP_get_cipherbyname() have changed
1440 * to look at providers too.
1442 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
1444 if (*legacy_nid == -1) /* We found a clash already */
1446 if (legacy_method == NULL)
1448 nid = EVP_CIPHER_get_nid(legacy_method);
1449 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
1457 static void *evp_cipher_from_algorithm(const int name_id,
1458 const OSSL_ALGORITHM *algodef,
1459 OSSL_PROVIDER *prov)
1461 const OSSL_DISPATCH *fns = algodef->implementation;
1462 EVP_CIPHER *cipher = NULL;
1463 int fnciphcnt = 0, fnctxcnt = 0;
1465 if ((cipher = evp_cipher_new()) == NULL) {
1466 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1471 cipher->nid = NID_undef;
1472 if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
1473 || cipher->nid == -1) {
1474 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1475 EVP_CIPHER_free(cipher);
1480 cipher->name_id = name_id;
1481 if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
1482 EVP_CIPHER_free(cipher);
1485 cipher->description = algodef->algorithm_description;
1487 for (; fns->function_id != 0; fns++) {
1488 switch (fns->function_id) {
1489 case OSSL_FUNC_CIPHER_NEWCTX:
1490 if (cipher->newctx != NULL)
1492 cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
1495 case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1496 if (cipher->einit != NULL)
1498 cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
1501 case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1502 if (cipher->dinit != NULL)
1504 cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
1507 case OSSL_FUNC_CIPHER_UPDATE:
1508 if (cipher->cupdate != NULL)
1510 cipher->cupdate = OSSL_FUNC_cipher_update(fns);
1513 case OSSL_FUNC_CIPHER_FINAL:
1514 if (cipher->cfinal != NULL)
1516 cipher->cfinal = OSSL_FUNC_cipher_final(fns);
1519 case OSSL_FUNC_CIPHER_CIPHER:
1520 if (cipher->ccipher != NULL)
1522 cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
1524 case OSSL_FUNC_CIPHER_FREECTX:
1525 if (cipher->freectx != NULL)
1527 cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
1530 case OSSL_FUNC_CIPHER_DUPCTX:
1531 if (cipher->dupctx != NULL)
1533 cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
1535 case OSSL_FUNC_CIPHER_GET_PARAMS:
1536 if (cipher->get_params != NULL)
1538 cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
1540 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1541 if (cipher->get_ctx_params != NULL)
1543 cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
1545 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1546 if (cipher->set_ctx_params != NULL)
1548 cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
1550 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1551 if (cipher->gettable_params != NULL)
1553 cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
1555 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1556 if (cipher->gettable_ctx_params != NULL)
1558 cipher->gettable_ctx_params =
1559 OSSL_FUNC_cipher_gettable_ctx_params(fns);
1561 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1562 if (cipher->settable_ctx_params != NULL)
1564 cipher->settable_ctx_params =
1565 OSSL_FUNC_cipher_settable_ctx_params(fns);
1569 if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1570 || (fnciphcnt == 0 && cipher->ccipher == NULL)
1573 * In order to be a consistent set of functions we must have at least
1574 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1575 * functions, or a single "cipher" function. In all cases we need both
1576 * the "newctx" and "freectx" functions.
1578 EVP_CIPHER_free(cipher);
1579 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1582 cipher->prov = prov;
1584 ossl_provider_up_ref(prov);
1586 if (!evp_cipher_cache_constants(cipher)) {
1587 EVP_CIPHER_free(cipher);
1588 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1595 static int evp_cipher_up_ref(void *cipher)
1597 return EVP_CIPHER_up_ref(cipher);
1600 static void evp_cipher_free(void *cipher)
1602 EVP_CIPHER_free(cipher);
1605 EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1606 const char *properties)
1608 EVP_CIPHER *cipher =
1609 evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1610 evp_cipher_from_algorithm, evp_cipher_up_ref,
1616 int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
1620 if (cipher->origin == EVP_ORIG_DYNAMIC)
1621 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
1625 void evp_cipher_free_int(EVP_CIPHER *cipher)
1627 OPENSSL_free(cipher->type_name);
1628 ossl_provider_free(cipher->prov);
1629 CRYPTO_THREAD_lock_free(cipher->lock);
1630 OPENSSL_free(cipher);
1633 void EVP_CIPHER_free(EVP_CIPHER *cipher)
1637 if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC)
1640 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
1643 evp_cipher_free_int(cipher);
1646 void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
1647 void (*fn)(EVP_CIPHER *mac, void *arg),
1650 evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1651 (void (*)(void *, void *))fn, arg,
1652 evp_cipher_from_algorithm, evp_cipher_free);