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));
50 /* Remove legacy code below when legacy support is removed. */
53 if (ctx->cipher != NULL) {
54 if (ctx->cipher->cleanup && !ctx->cipher->cleanup(ctx))
56 /* Cleanse cipher context data */
57 if (ctx->cipher_data && ctx->cipher->ctx_size)
58 OPENSSL_cleanse(ctx->cipher_data, ctx->cipher->ctx_size);
60 OPENSSL_free(ctx->cipher_data);
61 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
62 ENGINE_finish(ctx->engine);
64 memset(ctx, 0, sizeof(*ctx));
69 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
73 ctx = OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
81 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
85 EVP_CIPHER_CTX_reset(ctx);
89 static int evp_cipher_init_internal(EVP_CIPHER_CTX *ctx,
90 const EVP_CIPHER *cipher,
91 ENGINE *impl, const unsigned char *key,
92 const unsigned char *iv, int enc,
93 const OSSL_PARAM params[])
96 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
97 ENGINE *tmpimpl = NULL;
101 * enc == 1 means we are encrypting.
102 * enc == 0 means we are decrypting.
103 * enc == -1 means, use the previously initialised value for encrypt/decrypt
113 if (cipher == NULL && ctx->cipher == NULL) {
114 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
118 /* Code below to be removed when legacy support is dropped. */
120 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
122 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
123 * this context may already have an ENGINE! Try to avoid releasing the
124 * previous handle, re-querying for an ENGINE, and having a
125 * reinitialisation, when it may all be unnecessary.
127 if (ctx->engine && ctx->cipher
128 && (cipher == NULL || cipher->nid == ctx->cipher->nid))
131 if (cipher != NULL && impl == NULL) {
132 /* Ask if an ENGINE is reserved for this job */
133 tmpimpl = ENGINE_get_cipher_engine(cipher->nid);
138 * If there are engines involved then we should use legacy handling for now.
140 if (ctx->engine != NULL
141 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
145 if (ctx->cipher == ctx->fetched_cipher)
147 EVP_CIPHER_free(ctx->fetched_cipher);
148 ctx->fetched_cipher = NULL;
152 * Ensure a context left lying around from last time is cleared
155 if (cipher != NULL && ctx->cipher != NULL) {
156 OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size);
157 ctx->cipher_data = NULL;
161 /* Start of non-legacy code below */
163 /* Ensure a context left lying around from last time is cleared */
164 if (cipher != NULL && ctx->cipher != NULL) {
165 unsigned long flags = ctx->flags;
167 EVP_CIPHER_CTX_reset(ctx);
168 /* Restore encrypt and flags */
174 cipher = ctx->cipher;
176 if (cipher->prov == NULL) {
178 /* We only do explicit fetches inside the FIPS module */
179 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
182 EVP_CIPHER *provciph =
183 EVP_CIPHER_fetch(NULL,
184 cipher->nid == NID_undef ? "NULL"
185 : OBJ_nid2sn(cipher->nid),
188 if (provciph == NULL)
191 EVP_CIPHER_free(ctx->fetched_cipher);
192 ctx->fetched_cipher = provciph;
196 if (cipher->prov != NULL) {
197 if (!EVP_CIPHER_up_ref((EVP_CIPHER *)cipher)) {
198 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
201 EVP_CIPHER_free(ctx->fetched_cipher);
202 ctx->fetched_cipher = (EVP_CIPHER *)cipher;
204 ctx->cipher = cipher;
205 if (ctx->algctx == NULL) {
206 ctx->algctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
207 if (ctx->algctx == NULL) {
208 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
213 if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
215 * If this ctx was already set up for no padding then we need to tell
216 * the new cipher about it.
218 if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
223 if (ctx->cipher->einit == NULL) {
224 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
228 return ctx->cipher->einit(ctx->algctx,
231 : EVP_CIPHER_CTX_get_key_length(ctx),
234 : EVP_CIPHER_CTX_get_iv_length(ctx),
238 if (ctx->cipher->dinit == NULL) {
239 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
243 return ctx->cipher->dinit(ctx->algctx,
246 : EVP_CIPHER_CTX_get_key_length(ctx),
249 : EVP_CIPHER_CTX_get_iv_length(ctx),
252 /* Code below to be removed when legacy support is dropped. */
255 if (cipher != NULL) {
257 * Ensure a context left lying around from last time is cleared (we
258 * previously attempted to avoid this if the same ENGINE and
259 * EVP_CIPHER could be used).
262 unsigned long flags = ctx->flags;
263 EVP_CIPHER_CTX_reset(ctx);
264 /* Restore encrypt and flags */
268 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
270 if (!ENGINE_init(impl)) {
271 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
278 /* There's an ENGINE for this job ... (apparently) */
279 const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
283 * One positive side-effect of US's export control history,
284 * is that we should at least be able to avoid using US
285 * misspellings of "initialisation"?
287 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
290 /* We'll use the ENGINE's private cipher definition */
293 * Store the ENGINE functional reference so we know 'cipher' came
294 * from an ENGINE and we need to release it when done.
302 ctx->cipher = cipher;
303 if (ctx->cipher->ctx_size) {
304 ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
305 if (ctx->cipher_data == NULL) {
307 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
311 ctx->cipher_data = NULL;
313 ctx->key_len = cipher->key_len;
314 /* Preserve wrap enable flag, zero everything else */
315 ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
316 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
317 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
319 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
324 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
327 if (ctx->cipher == NULL)
330 /* we assume block size is a power of 2 in *cryptUpdate */
331 OPENSSL_assert(ctx->cipher->block_size == 1
332 || ctx->cipher->block_size == 8
333 || ctx->cipher->block_size == 16);
335 if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
336 && EVP_CIPHER_CTX_get_mode(ctx) == EVP_CIPH_WRAP_MODE) {
337 ERR_raise(ERR_LIB_EVP, EVP_R_WRAP_MODE_NOT_ALLOWED);
341 if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ctx))
342 & EVP_CIPH_CUSTOM_IV) == 0) {
343 switch (EVP_CIPHER_CTX_get_mode(ctx)) {
345 case EVP_CIPH_STREAM_CIPHER:
346 case EVP_CIPH_ECB_MODE:
349 case EVP_CIPH_CFB_MODE:
350 case EVP_CIPH_OFB_MODE:
355 case EVP_CIPH_CBC_MODE:
356 n = EVP_CIPHER_CTX_get_iv_length(ctx);
357 if (!ossl_assert(n >= 0 && n <= (int)sizeof(ctx->iv)))
360 memcpy(ctx->oiv, iv, n);
361 memcpy(ctx->iv, ctx->oiv, n);
364 case EVP_CIPH_CTR_MODE:
366 /* Don't reuse IV for CTR mode */
368 if ((n = EVP_CIPHER_CTX_get_iv_length(ctx)) <= 0)
370 memcpy(ctx->iv, iv, n);
379 if (key != NULL || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
380 if (!ctx->cipher->init(ctx, key, iv, enc))
385 ctx->block_mask = ctx->cipher->block_size - 1;
389 int EVP_CipherInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
390 const unsigned char *key, const unsigned char *iv,
391 int enc, const OSSL_PARAM params[])
393 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, params);
396 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
397 const unsigned char *key, const unsigned char *iv, int enc)
400 EVP_CIPHER_CTX_reset(ctx);
401 return evp_cipher_init_internal(ctx, cipher, NULL, key, iv, enc, NULL);
404 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
405 ENGINE *impl, const unsigned char *key,
406 const unsigned char *iv, int enc)
408 return evp_cipher_init_internal(ctx, cipher, impl, key, iv, enc, NULL);
411 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
412 const unsigned char *in, int inl)
415 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
417 return EVP_DecryptUpdate(ctx, out, outl, in, inl);
420 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
423 return EVP_EncryptFinal_ex(ctx, out, outl);
425 return EVP_DecryptFinal_ex(ctx, out, outl);
428 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
431 return EVP_EncryptFinal(ctx, out, outl);
433 return EVP_DecryptFinal(ctx, out, outl);
436 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
437 const unsigned char *key, const unsigned char *iv)
439 return EVP_CipherInit(ctx, cipher, key, iv, 1);
442 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
443 ENGINE *impl, const unsigned char *key,
444 const unsigned char *iv)
446 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
449 int EVP_EncryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
450 const unsigned char *key, const unsigned char *iv,
451 const OSSL_PARAM params[])
453 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 1, params);
456 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
457 const unsigned char *key, const unsigned char *iv)
459 return EVP_CipherInit(ctx, cipher, key, iv, 0);
462 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
463 ENGINE *impl, const unsigned char *key,
464 const unsigned char *iv)
466 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
469 int EVP_DecryptInit_ex2(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
470 const unsigned char *key, const unsigned char *iv,
471 const OSSL_PARAM params[])
473 return EVP_CipherInit_ex2(ctx, cipher, key, iv, 0, params);
477 * According to the letter of standard difference between pointers
478 * is specified to be valid only within same object. This makes
479 * it formally challenging to determine if input and output buffers
480 * are not partially overlapping with standard pointer arithmetic.
485 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
487 * Then we have VMS that distinguishes itself by adhering to
488 * sizeof(size_t)==4 even in 64-bit builds, which means that
489 * difference between two pointers might be truncated to 32 bits.
490 * In the context one can even wonder how comparison for
491 * equality is implemented. To be on the safe side we adhere to
492 * PTRDIFF_T even for comparison for equality.
494 # define PTRDIFF_T uint64_t
496 # define PTRDIFF_T size_t
499 int ossl_is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
501 PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
503 * Check for partially overlapping buffers. [Binary logical
504 * operations are used instead of boolean to minimize number
505 * of conditional branches.]
507 int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
508 (diff > (0 - (PTRDIFF_T)len)));
513 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
514 unsigned char *out, int *outl,
515 const unsigned char *in, int inl)
517 int i, j, bl, cmpl = inl;
519 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
520 cmpl = (cmpl + 7) / 8;
522 bl = ctx->cipher->block_size;
524 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
525 /* If block size > 1 then the cipher will have to do this check */
526 if (bl == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
527 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
531 i = ctx->cipher->do_cipher(ctx, out, in, inl);
543 if (ossl_is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
544 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
548 if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
549 if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
558 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
561 memcpy(&(ctx->buf[i]), in, inl);
569 * Once we've processed the first j bytes from in, the amount of
570 * data left that is a multiple of the block length is:
571 * (inl - j) & ~(bl - 1)
572 * We must ensure that this amount of data, plus the one block that
573 * we process from ctx->buf does not exceed INT_MAX
575 if (((inl - j) & ~(bl - 1)) > INT_MAX - bl) {
576 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
579 memcpy(&(ctx->buf[i]), in, j);
582 if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
592 if (!ctx->cipher->do_cipher(ctx, out, in, inl))
598 memcpy(ctx->buf, &(in[inl]), i);
604 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
605 const unsigned char *in, int inl)
614 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
618 /* Prevent accidental use of decryption context when encrypting */
620 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
624 if (ctx->cipher == NULL) {
625 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
629 if (ctx->cipher->prov == NULL)
632 blocksize = ctx->cipher->block_size;
634 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
635 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
638 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
639 inl + (blocksize == 1 ? 0 : blocksize), in,
643 if (soutl > INT_MAX) {
644 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
652 /* Code below to be removed when legacy support is dropped. */
655 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
658 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
661 ret = EVP_EncryptFinal_ex(ctx, out, outl);
665 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
668 unsigned int i, b, bl;
675 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
679 /* Prevent accidental use of decryption context when encrypting */
681 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
685 if (ctx->cipher == NULL) {
686 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
689 if (ctx->cipher->prov == NULL)
692 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
694 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
695 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
699 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
700 blocksize == 1 ? 0 : blocksize);
703 if (soutl > INT_MAX) {
704 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
712 /* Code below to be removed when legacy support is dropped. */
715 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
716 ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
724 b = ctx->cipher->block_size;
725 OPENSSL_assert(b <= sizeof(ctx->buf));
731 if (ctx->flags & EVP_CIPH_NO_PADDING) {
733 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
741 for (i = bl; i < b; i++)
743 ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
751 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
752 const unsigned char *in, int inl)
754 int fix_len, cmpl = inl, ret;
762 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
766 /* Prevent accidental use of encryption context when decrypting */
768 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
772 if (ctx->cipher == NULL) {
773 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
776 if (ctx->cipher->prov == NULL)
779 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
781 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
782 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
785 ret = ctx->cipher->cupdate(ctx->algctx, out, &soutl,
786 inl + (blocksize == 1 ? 0 : blocksize), in,
790 if (soutl > INT_MAX) {
791 ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR);
799 /* Code below to be removed when legacy support is dropped. */
802 b = ctx->cipher->block_size;
804 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
805 cmpl = (cmpl + 7) / 8;
807 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
808 if (b == 1 && ossl_is_partially_overlapping(out, in, cmpl)) {
809 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
813 fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
827 if (ctx->flags & EVP_CIPH_NO_PADDING)
828 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
830 OPENSSL_assert(b <= sizeof(ctx->final));
832 if (ctx->final_used) {
833 /* see comment about PTRDIFF_T comparison above */
834 if (((PTRDIFF_T)out == (PTRDIFF_T)in)
835 || ossl_is_partially_overlapping(out, in, b)) {
836 ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING);
840 * final_used is only ever set if buf_len is 0. Therefore the maximum
841 * length output we will ever see from evp_EncryptDecryptUpdate is
842 * the maximum multiple of the block length that is <= inl, or just:
844 * Since final_used has been set then the final output length is:
845 * (inl & ~(b - 1)) + b
846 * This must never exceed INT_MAX
848 if ((inl & ~(b - 1)) > INT_MAX - b) {
849 ERR_raise(ERR_LIB_EVP, EVP_R_OUTPUT_WOULD_OVERFLOW);
852 memcpy(out, ctx->final, b);
858 if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
862 * if we have 'decrypted' a multiple of block size, make sure we have a
863 * copy of this last block
865 if (b > 1 && !ctx->buf_len) {
868 memcpy(ctx->final, &out[*outl], b);
878 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
881 ret = EVP_DecryptFinal_ex(ctx, out, outl);
885 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
896 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
900 /* Prevent accidental use of encryption context when decrypting */
902 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
906 if (ctx->cipher == NULL) {
907 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
911 if (ctx->cipher->prov == NULL)
914 blocksize = EVP_CIPHER_CTX_get_block_size(ctx);
916 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
917 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
921 ret = ctx->cipher->cfinal(ctx->algctx, out, &soutl,
922 blocksize == 1 ? 0 : blocksize);
925 if (soutl > INT_MAX) {
926 ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR);
934 /* Code below to be removed when legacy support is dropped. */
938 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
939 i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
947 b = ctx->cipher->block_size;
948 if (ctx->flags & EVP_CIPH_NO_PADDING) {
950 ERR_raise(ERR_LIB_EVP, EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
957 if (ctx->buf_len || !ctx->final_used) {
958 ERR_raise(ERR_LIB_EVP, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
961 OPENSSL_assert(b <= sizeof(ctx->final));
964 * The following assumes that the ciphertext has been authenticated.
965 * Otherwise it provides a padding oracle.
967 n = ctx->final[b - 1];
968 if (n == 0 || n > (int)b) {
969 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
972 for (i = 0; i < n; i++) {
973 if (ctx->final[--b] != n) {
974 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_DECRYPT);
978 n = ctx->cipher->block_size - n;
979 for (i = 0; i < n; i++)
980 out[i] = ctx->final[i];
987 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
989 if (c->cipher->prov != NULL) {
991 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
994 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
997 /* Check the cipher actually understands this parameter */
998 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
999 OSSL_CIPHER_PARAM_KEYLEN) == NULL) {
1000 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1004 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
1005 if (!OSSL_PARAM_set_int(params, keylen))
1007 ok = evp_do_ciph_ctx_setparams(c->cipher, c->algctx, params);
1010 c->key_len = keylen;
1014 /* Code below to be removed when legacy support is dropped. */
1017 * Note there have never been any built-in ciphers that define this flag
1018 * since it was first introduced.
1020 if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
1021 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
1022 if (EVP_CIPHER_CTX_get_key_length(c) == keylen)
1024 if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
1025 c->key_len = keylen;
1028 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY_LENGTH);
1032 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
1035 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1036 unsigned int pd = pad;
1039 ctx->flags &= ~EVP_CIPH_NO_PADDING;
1041 ctx->flags |= EVP_CIPH_NO_PADDING;
1043 if (ctx->cipher != NULL && ctx->cipher->prov == NULL)
1045 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
1046 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1051 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1053 int ret = EVP_CTRL_RET_UNSUPPORTED;
1057 OSSL_PARAM params[4] = {
1058 OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
1061 if (ctx == NULL || ctx->cipher == NULL) {
1062 ERR_raise(ERR_LIB_EVP, EVP_R_NO_CIPHER_SET);
1066 if (ctx->cipher->prov == NULL)
1070 case EVP_CTRL_SET_KEY_LENGTH:
1071 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
1074 case EVP_CTRL_RAND_KEY: /* Used by DES */
1077 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
1083 * EVP_CTRL_INIT is purely legacy, no provider counterpart.
1084 * As a matter of fact, this should be dead code, but some caller
1085 * might still do a direct control call with this command, so...
1086 * Legacy methods return 1 except for exceptional circumstances, so
1087 * we do the same here to not be disruptive.
1090 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
1093 case EVP_CTRL_AEAD_SET_IVLEN:
1096 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1099 case EVP_CTRL_CCM_SET_L:
1100 if (arg < 2 || arg > 8)
1103 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1106 case EVP_CTRL_AEAD_SET_IV_FIXED:
1107 params[0] = OSSL_PARAM_construct_octet_string(
1108 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
1110 case EVP_CTRL_GCM_IV_GEN:
1113 sz = 0; /* special case that uses the iv length */
1114 params[0] = OSSL_PARAM_construct_octet_string(
1115 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
1117 case EVP_CTRL_GCM_SET_IV_INV:
1120 params[0] = OSSL_PARAM_construct_octet_string(
1121 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
1123 case EVP_CTRL_GET_RC5_ROUNDS:
1124 set_params = 0; /* Fall thru */
1125 case EVP_CTRL_SET_RC5_ROUNDS:
1128 i = (unsigned int)arg;
1129 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
1131 case EVP_CTRL_SET_SPEED:
1134 i = (unsigned int)arg;
1135 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
1137 case EVP_CTRL_AEAD_GET_TAG:
1138 set_params = 0; /* Fall thru */
1139 case EVP_CTRL_AEAD_SET_TAG:
1140 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1143 case EVP_CTRL_AEAD_TLS1_AAD:
1144 /* This one does a set and a get - since it returns a size */
1146 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1148 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1152 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1153 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1157 #ifndef OPENSSL_NO_RC2
1158 case EVP_CTRL_GET_RC2_KEY_BITS:
1159 set_params = 0; /* Fall thru */
1160 case EVP_CTRL_SET_RC2_KEY_BITS:
1161 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
1163 #endif /* OPENSSL_NO_RC2 */
1164 #if !defined(OPENSSL_NO_MULTIBLOCK)
1165 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
1166 params[0] = OSSL_PARAM_construct_size_t(
1167 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
1168 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1172 params[0] = OSSL_PARAM_construct_size_t(
1173 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
1174 params[1] = OSSL_PARAM_construct_end();
1175 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1179 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
1180 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1181 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1183 if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
1186 params[0] = OSSL_PARAM_construct_octet_string(
1187 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void*)p->inp, p->len);
1188 params[1] = OSSL_PARAM_construct_uint(
1189 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1190 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1193 /* Retrieve the return values changed by the set */
1194 params[0] = OSSL_PARAM_construct_size_t(
1195 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
1196 params[1] = OSSL_PARAM_construct_uint(
1197 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1198 params[2] = OSSL_PARAM_construct_end();
1199 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1204 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
1205 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1206 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1208 params[0] = OSSL_PARAM_construct_octet_string(
1209 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
1211 params[1] = OSSL_PARAM_construct_octet_string(
1212 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void*)p->inp,
1214 params[2] = OSSL_PARAM_construct_uint(
1215 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1216 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1219 params[0] = OSSL_PARAM_construct_size_t(
1220 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
1221 params[1] = OSSL_PARAM_construct_end();
1222 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1227 #endif /* OPENSSL_NO_MULTIBLOCK */
1228 case EVP_CTRL_AEAD_SET_MAC_KEY:
1231 params[0] = OSSL_PARAM_construct_octet_string(
1232 OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
1237 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->algctx, params);
1239 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->algctx, params);
1242 /* Code below to be removed when legacy support is dropped. */
1244 if (ctx->cipher->ctrl == NULL) {
1245 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_NOT_IMPLEMENTED);
1249 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
1252 if (ret == EVP_CTRL_RET_UNSUPPORTED) {
1253 ERR_raise(ERR_LIB_EVP, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
1259 int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1261 if (cipher != NULL && cipher->get_params != NULL)
1262 return cipher->get_params(params);
1266 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1269 const OSSL_PARAM *p;
1271 if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL) {
1272 r = ctx->cipher->set_ctx_params(ctx->algctx, params);
1274 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
1275 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->key_len)) {
1281 p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_IVLEN);
1282 if (p != NULL && !OSSL_PARAM_get_int(p, &ctx->iv_len)) {
1291 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1293 if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1294 return ctx->cipher->get_ctx_params(ctx->algctx, params);
1298 const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1300 if (cipher != NULL && cipher->gettable_params != NULL)
1301 return cipher->gettable_params(
1302 ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher)));
1306 const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
1310 if (cipher != NULL && cipher->settable_ctx_params != NULL) {
1311 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1312 return cipher->settable_ctx_params(NULL, provctx);
1317 const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
1321 if (cipher != NULL && cipher->gettable_ctx_params != NULL) {
1322 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cipher));
1323 return cipher->gettable_ctx_params(NULL, provctx);
1328 const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(EVP_CIPHER_CTX *cctx)
1332 if (cctx != NULL && cctx->cipher->settable_ctx_params != NULL) {
1333 alg = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1334 return cctx->cipher->settable_ctx_params(cctx->algctx, alg);
1339 const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(EVP_CIPHER_CTX *cctx)
1343 if (cctx != NULL && cctx->cipher->gettable_ctx_params != NULL) {
1344 provctx = ossl_provider_ctx(EVP_CIPHER_get0_provider(cctx->cipher));
1345 return cctx->cipher->gettable_ctx_params(cctx->algctx, provctx);
1351 static OSSL_LIB_CTX *EVP_CIPHER_CTX_get_libctx(EVP_CIPHER_CTX *ctx)
1353 const EVP_CIPHER *cipher = ctx->cipher;
1354 const OSSL_PROVIDER *prov;
1359 prov = EVP_CIPHER_get0_provider(cipher);
1360 return ossl_provider_libctx(prov);
1364 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
1366 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1367 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1374 OSSL_LIB_CTX *libctx = EVP_CIPHER_CTX_get_libctx(ctx);
1376 kl = EVP_CIPHER_CTX_get_key_length(ctx);
1377 if (kl <= 0 || RAND_priv_bytes_ex(libctx, key, kl, 0) <= 0)
1381 #endif /* FIPS_MODULE */
1384 EVP_CIPHER_CTX *EVP_CIPHER_CTX_dup(const EVP_CIPHER_CTX *in)
1386 EVP_CIPHER_CTX *out = EVP_CIPHER_CTX_new();
1388 if (out != NULL && !EVP_CIPHER_CTX_copy(out, in)) {
1389 EVP_CIPHER_CTX_free(out);
1395 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
1397 if ((in == NULL) || (in->cipher == NULL)) {
1398 ERR_raise(ERR_LIB_EVP, EVP_R_INPUT_NOT_INITIALIZED);
1402 if (in->cipher->prov == NULL)
1405 if (in->cipher->dupctx == NULL) {
1406 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1410 EVP_CIPHER_CTX_reset(out);
1415 if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
1416 out->fetched_cipher = NULL;
1420 out->algctx = in->cipher->dupctx(in->algctx);
1421 if (out->algctx == NULL) {
1422 ERR_raise(ERR_LIB_EVP, EVP_R_NOT_ABLE_TO_COPY_CTX);
1428 /* Code below to be removed when legacy support is dropped. */
1431 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1432 /* Make sure it's safe to copy a cipher context using an ENGINE */
1433 if (in->engine && !ENGINE_init(in->engine)) {
1434 ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
1439 EVP_CIPHER_CTX_reset(out);
1440 memcpy(out, in, sizeof(*out));
1442 if (in->cipher_data && in->cipher->ctx_size) {
1443 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
1444 if (out->cipher_data == NULL) {
1446 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1449 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
1452 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
1453 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
1455 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
1461 EVP_CIPHER *evp_cipher_new(void)
1463 EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1465 if (cipher != NULL) {
1466 cipher->lock = CRYPTO_THREAD_lock_new();
1467 if (cipher->lock == NULL) {
1468 OPENSSL_free(cipher);
1477 * FIPS module note: since internal fetches will be entirely
1478 * provider based, we know that none of its code depends on legacy
1479 * NIDs or any functionality that use them.
1482 /* After removal of legacy support get rid of the need for legacy NIDs */
1483 static void set_legacy_nid(const char *name, void *vlegacy_nid)
1486 int *legacy_nid = vlegacy_nid;
1488 * We use lowest level function to get the associated method, because
1489 * higher level functions such as EVP_get_cipherbyname() have changed
1490 * to look at providers too.
1492 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
1494 if (*legacy_nid == -1) /* We found a clash already */
1496 if (legacy_method == NULL)
1498 nid = EVP_CIPHER_get_nid(legacy_method);
1499 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
1507 static void *evp_cipher_from_algorithm(const int name_id,
1508 const OSSL_ALGORITHM *algodef,
1509 OSSL_PROVIDER *prov)
1511 const OSSL_DISPATCH *fns = algodef->implementation;
1512 EVP_CIPHER *cipher = NULL;
1513 int fnciphcnt = 0, fnctxcnt = 0;
1515 if ((cipher = evp_cipher_new()) == NULL) {
1516 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
1521 cipher->nid = NID_undef;
1522 if (!evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid)
1523 || cipher->nid == -1) {
1524 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1525 EVP_CIPHER_free(cipher);
1530 cipher->name_id = name_id;
1531 if ((cipher->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
1532 EVP_CIPHER_free(cipher);
1535 cipher->description = algodef->algorithm_description;
1537 for (; fns->function_id != 0; fns++) {
1538 switch (fns->function_id) {
1539 case OSSL_FUNC_CIPHER_NEWCTX:
1540 if (cipher->newctx != NULL)
1542 cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
1545 case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1546 if (cipher->einit != NULL)
1548 cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
1551 case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1552 if (cipher->dinit != NULL)
1554 cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
1557 case OSSL_FUNC_CIPHER_UPDATE:
1558 if (cipher->cupdate != NULL)
1560 cipher->cupdate = OSSL_FUNC_cipher_update(fns);
1563 case OSSL_FUNC_CIPHER_FINAL:
1564 if (cipher->cfinal != NULL)
1566 cipher->cfinal = OSSL_FUNC_cipher_final(fns);
1569 case OSSL_FUNC_CIPHER_CIPHER:
1570 if (cipher->ccipher != NULL)
1572 cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
1574 case OSSL_FUNC_CIPHER_FREECTX:
1575 if (cipher->freectx != NULL)
1577 cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
1580 case OSSL_FUNC_CIPHER_DUPCTX:
1581 if (cipher->dupctx != NULL)
1583 cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
1585 case OSSL_FUNC_CIPHER_GET_PARAMS:
1586 if (cipher->get_params != NULL)
1588 cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
1590 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1591 if (cipher->get_ctx_params != NULL)
1593 cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
1595 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1596 if (cipher->set_ctx_params != NULL)
1598 cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
1600 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1601 if (cipher->gettable_params != NULL)
1603 cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
1605 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1606 if (cipher->gettable_ctx_params != NULL)
1608 cipher->gettable_ctx_params =
1609 OSSL_FUNC_cipher_gettable_ctx_params(fns);
1611 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1612 if (cipher->settable_ctx_params != NULL)
1614 cipher->settable_ctx_params =
1615 OSSL_FUNC_cipher_settable_ctx_params(fns);
1619 if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1620 || (fnciphcnt == 0 && cipher->ccipher == NULL)
1623 * In order to be a consistent set of functions we must have at least
1624 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1625 * functions, or a single "cipher" function. In all cases we need both
1626 * the "newctx" and "freectx" functions.
1628 EVP_CIPHER_free(cipher);
1629 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1632 cipher->prov = prov;
1634 ossl_provider_up_ref(prov);
1636 if (!evp_cipher_cache_constants(cipher)) {
1637 EVP_CIPHER_free(cipher);
1638 ERR_raise(ERR_LIB_EVP, EVP_R_CACHE_CONSTANTS_FAILED);
1645 static int evp_cipher_up_ref(void *cipher)
1647 return EVP_CIPHER_up_ref(cipher);
1650 static void evp_cipher_free(void *cipher)
1652 EVP_CIPHER_free(cipher);
1655 EVP_CIPHER *EVP_CIPHER_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
1656 const char *properties)
1658 EVP_CIPHER *cipher =
1659 evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1660 evp_cipher_from_algorithm, evp_cipher_up_ref,
1666 int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
1670 if (cipher->origin == EVP_ORIG_DYNAMIC)
1671 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
1675 void evp_cipher_free_int(EVP_CIPHER *cipher)
1677 OPENSSL_free(cipher->type_name);
1678 ossl_provider_free(cipher->prov);
1679 CRYPTO_THREAD_lock_free(cipher->lock);
1680 OPENSSL_free(cipher);
1683 void EVP_CIPHER_free(EVP_CIPHER *cipher)
1687 if (cipher == NULL || cipher->origin != EVP_ORIG_DYNAMIC)
1690 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
1693 evp_cipher_free_int(cipher);
1696 void EVP_CIPHER_do_all_provided(OSSL_LIB_CTX *libctx,
1697 void (*fn)(EVP_CIPHER *mac, void *arg),
1700 evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1701 (void (*)(void *, void *))fn, arg,
1702 evp_cipher_from_algorithm, evp_cipher_up_ref,