2 * Copyright 1995-2020 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
15 #include "internal/cryptlib.h"
16 #include <openssl/evp.h>
17 #include <openssl/err.h>
18 #include <openssl/rand.h>
19 #include <openssl/rand_drbg.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 /* TODO(3.0): Remove legacy code below */
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)
71 EVP_CIPHER_CTX_reset(ctx);
75 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
76 const unsigned char *key, const unsigned char *iv, int enc)
79 EVP_CIPHER_CTX_reset(ctx);
80 return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
83 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
84 ENGINE *impl, const unsigned char *key,
85 const unsigned char *iv, int enc)
87 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
88 ENGINE *tmpimpl = NULL;
91 * enc == 1 means we are encrypting.
92 * enc == 0 means we are decrypting.
93 * enc == -1 means, use the previously initialised value for encrypt/decrypt
103 if (cipher == NULL && ctx->cipher == NULL) {
104 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET);
108 /* TODO(3.0): Legacy work around code below. Remove this */
110 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
112 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
113 * this context may already have an ENGINE! Try to avoid releasing the
114 * previous handle, re-querying for an ENGINE, and having a
115 * reinitialisation, when it may all be unnecessary.
117 if (ctx->engine && ctx->cipher
118 && (cipher == NULL || cipher->nid == ctx->cipher->nid))
121 if (cipher != NULL && impl == NULL) {
122 /* Ask if an ENGINE is reserved for this job */
123 tmpimpl = ENGINE_get_cipher_engine(cipher->nid);
128 * If there are engines involved then we should use legacy handling for now.
130 if (ctx->engine != NULL
131 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
135 if (ctx->cipher == ctx->fetched_cipher)
137 EVP_CIPHER_free(ctx->fetched_cipher);
138 ctx->fetched_cipher = NULL;
142 * Ensure a context left lying around from last time is cleared
145 if (cipher != NULL && ctx->cipher != NULL) {
146 OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size);
147 ctx->cipher_data = NULL;
151 /* TODO(3.0): Start of non-legacy code below */
153 /* Ensure a context left lying around from last time is cleared */
154 if (cipher != NULL && ctx->cipher != NULL) {
155 unsigned long flags = ctx->flags;
157 EVP_CIPHER_CTX_reset(ctx);
158 /* Restore encrypt and flags */
164 cipher = ctx->cipher;
166 if (cipher->prov == NULL) {
168 /* We only do explicit fetches inside the FIPS module */
169 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
172 EVP_CIPHER *provciph =
173 EVP_CIPHER_fetch(NULL,
174 cipher->nid == NID_undef ? "NULL"
175 : OBJ_nid2sn(cipher->nid),
178 if (provciph == NULL) {
179 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
183 EVP_CIPHER_free(ctx->fetched_cipher);
184 ctx->fetched_cipher = provciph;
188 ctx->cipher = cipher;
189 if (ctx->provctx == NULL) {
190 ctx->provctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
191 if (ctx->provctx == NULL) {
192 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
197 if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
199 * If this ctx was already set up for no padding then we need to tell
200 * the new cipher about it.
202 if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
207 if (ctx->cipher->einit == NULL) {
208 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
212 return ctx->cipher->einit(ctx->provctx,
215 : EVP_CIPHER_CTX_key_length(ctx),
218 : EVP_CIPHER_CTX_iv_length(ctx));
221 if (ctx->cipher->dinit == NULL) {
222 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
226 return ctx->cipher->dinit(ctx->provctx,
229 : EVP_CIPHER_CTX_key_length(ctx),
232 : EVP_CIPHER_CTX_iv_length(ctx));
234 /* TODO(3.0): Remove legacy code below */
237 if (cipher != NULL) {
239 * Ensure a context left lying around from last time is cleared (we
240 * previously attempted to avoid this if the same ENGINE and
241 * EVP_CIPHER could be used).
244 unsigned long flags = ctx->flags;
245 EVP_CIPHER_CTX_reset(ctx);
246 /* Restore encrypt and flags */
250 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
252 if (!ENGINE_init(impl)) {
253 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
260 /* There's an ENGINE for this job ... (apparently) */
261 const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
265 * One positive side-effect of US's export control history,
266 * is that we should at least be able to avoid using US
267 * misspellings of "initialisation"?
269 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
272 /* We'll use the ENGINE's private cipher definition */
275 * Store the ENGINE functional reference so we know 'cipher' came
276 * from an ENGINE and we need to release it when done.
284 ctx->cipher = cipher;
285 if (ctx->cipher->ctx_size) {
286 ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
287 if (ctx->cipher_data == NULL) {
289 EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
293 ctx->cipher_data = NULL;
295 ctx->key_len = cipher->key_len;
296 /* Preserve wrap enable flag, zero everything else */
297 ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
298 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
299 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
301 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
306 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
309 if (ctx->cipher == NULL)
312 /* we assume block size is a power of 2 in *cryptUpdate */
313 OPENSSL_assert(ctx->cipher->block_size == 1
314 || ctx->cipher->block_size == 8
315 || ctx->cipher->block_size == 16);
317 if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
318 && EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) {
319 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_WRAP_MODE_NOT_ALLOWED);
323 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_CUSTOM_IV)) {
324 switch (EVP_CIPHER_CTX_mode(ctx)) {
326 case EVP_CIPH_STREAM_CIPHER:
327 case EVP_CIPH_ECB_MODE:
330 case EVP_CIPH_CFB_MODE:
331 case EVP_CIPH_OFB_MODE:
336 case EVP_CIPH_CBC_MODE:
338 OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
339 (int)sizeof(ctx->iv));
341 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
342 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
345 case EVP_CIPH_CTR_MODE:
347 /* Don't reuse IV for CTR mode */
349 memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
357 if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
358 if (!ctx->cipher->init(ctx, key, iv, enc))
363 ctx->block_mask = ctx->cipher->block_size - 1;
367 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
368 const unsigned char *in, int inl)
371 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
373 return EVP_DecryptUpdate(ctx, out, outl, in, inl);
376 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
379 return EVP_EncryptFinal_ex(ctx, out, outl);
381 return EVP_DecryptFinal_ex(ctx, out, outl);
384 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
387 return EVP_EncryptFinal(ctx, out, outl);
389 return EVP_DecryptFinal(ctx, out, outl);
392 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
393 const unsigned char *key, const unsigned char *iv)
395 return EVP_CipherInit(ctx, cipher, key, iv, 1);
398 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
399 ENGINE *impl, const unsigned char *key,
400 const unsigned char *iv)
402 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
405 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
406 const unsigned char *key, const unsigned char *iv)
408 return EVP_CipherInit(ctx, cipher, key, iv, 0);
411 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
412 ENGINE *impl, const unsigned char *key,
413 const unsigned char *iv)
415 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
419 * According to the letter of standard difference between pointers
420 * is specified to be valid only within same object. This makes
421 * it formally challenging to determine if input and output buffers
422 * are not partially overlapping with standard pointer arithmetic.
427 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
429 * Then we have VMS that distinguishes itself by adhering to
430 * sizeof(size_t)==4 even in 64-bit builds, which means that
431 * difference between two pointers might be truncated to 32 bits.
432 * In the context one can even wonder how comparison for
433 * equality is implemented. To be on the safe side we adhere to
434 * PTRDIFF_T even for comparison for equality.
436 # define PTRDIFF_T uint64_t
438 # define PTRDIFF_T size_t
441 int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
443 PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
445 * Check for partially overlapping buffers. [Binary logical
446 * operations are used instead of boolean to minimize number
447 * of conditional branches.]
449 int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
450 (diff > (0 - (PTRDIFF_T)len)));
455 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
456 unsigned char *out, int *outl,
457 const unsigned char *in, int inl)
459 int i, j, bl, cmpl = inl;
461 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
462 cmpl = (cmpl + 7) / 8;
464 bl = ctx->cipher->block_size;
466 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
467 /* If block size > 1 then the cipher will have to do this check */
468 if (bl == 1 && is_partially_overlapping(out, in, cmpl)) {
469 EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
473 i = ctx->cipher->do_cipher(ctx, out, in, inl);
485 if (is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
486 EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
490 if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
491 if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
500 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
503 memcpy(&(ctx->buf[i]), in, inl);
509 memcpy(&(ctx->buf[i]), in, j);
512 if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
522 if (!ctx->cipher->do_cipher(ctx, out, in, inl))
528 memcpy(ctx->buf, &(in[inl]), i);
534 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
535 const unsigned char *in, int inl)
541 /* Prevent accidental use of decryption context when encrypting */
543 EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_INVALID_OPERATION);
547 if (ctx->cipher == NULL) {
548 EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_NO_CIPHER_SET);
552 if (ctx->cipher->prov == NULL)
555 blocksize = EVP_CIPHER_CTX_block_size(ctx);
557 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
558 EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_UPDATE_ERROR);
561 ret = ctx->cipher->cupdate(ctx->provctx, out, &soutl,
562 inl + (blocksize == 1 ? 0 : blocksize), in,
566 if (soutl > INT_MAX) {
567 EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_UPDATE_ERROR);
575 /* TODO(3.0): Remove legacy code below */
578 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
581 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
584 ret = EVP_EncryptFinal_ex(ctx, out, outl);
588 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
591 unsigned int i, b, bl;
595 /* Prevent accidental use of decryption context when encrypting */
597 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
601 if (ctx->cipher == NULL) {
602 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_NO_CIPHER_SET);
605 if (ctx->cipher->prov == NULL)
608 blocksize = EVP_CIPHER_CTX_block_size(ctx);
610 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
611 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_FINAL_ERROR);
615 ret = ctx->cipher->cfinal(ctx->provctx, out, &soutl,
616 blocksize == 1 ? 0 : blocksize);
619 if (soutl > INT_MAX) {
620 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_FINAL_ERROR);
628 /* TODO(3.0): Remove legacy code below */
631 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
632 ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
640 b = ctx->cipher->block_size;
641 OPENSSL_assert(b <= sizeof(ctx->buf));
647 if (ctx->flags & EVP_CIPH_NO_PADDING) {
649 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,
650 EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
658 for (i = bl; i < b; i++)
660 ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
668 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
669 const unsigned char *in, int inl)
671 int fix_len, cmpl = inl, ret;
676 /* Prevent accidental use of encryption context when decrypting */
678 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_INVALID_OPERATION);
682 if (ctx->cipher == NULL) {
683 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_NO_CIPHER_SET);
686 if (ctx->cipher->prov == NULL)
689 blocksize = EVP_CIPHER_CTX_block_size(ctx);
691 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
692 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_UPDATE_ERROR);
695 ret = ctx->cipher->cupdate(ctx->provctx, out, &soutl,
696 inl + (blocksize == 1 ? 0 : blocksize), in,
700 if (soutl > INT_MAX) {
701 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_UPDATE_ERROR);
709 /* TODO(3.0): Remove legacy code below */
712 b = ctx->cipher->block_size;
714 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
715 cmpl = (cmpl + 7) / 8;
717 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
718 if (b == 1 && is_partially_overlapping(out, in, cmpl)) {
719 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
723 fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
737 if (ctx->flags & EVP_CIPH_NO_PADDING)
738 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
740 OPENSSL_assert(b <= sizeof(ctx->final));
742 if (ctx->final_used) {
743 /* see comment about PTRDIFF_T comparison above */
744 if (((PTRDIFF_T)out == (PTRDIFF_T)in)
745 || is_partially_overlapping(out, in, b)) {
746 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
749 memcpy(out, ctx->final, b);
755 if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
759 * if we have 'decrypted' a multiple of block size, make sure we have a
760 * copy of this last block
762 if (b > 1 && !ctx->buf_len) {
765 memcpy(ctx->final, &out[*outl], b);
775 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
778 ret = EVP_DecryptFinal_ex(ctx, out, outl);
782 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
790 /* Prevent accidental use of encryption context when decrypting */
792 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
796 if (ctx->cipher == NULL) {
797 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_NO_CIPHER_SET);
801 if (ctx->cipher->prov == NULL)
804 blocksize = EVP_CIPHER_CTX_block_size(ctx);
806 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
807 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_FINAL_ERROR);
811 ret = ctx->cipher->cfinal(ctx->provctx, out, &soutl,
812 blocksize == 1 ? 0 : blocksize);
815 if (soutl > INT_MAX) {
816 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_FINAL_ERROR);
824 /* TODO(3.0): Remove legacy code below */
828 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
829 i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
837 b = ctx->cipher->block_size;
838 if (ctx->flags & EVP_CIPH_NO_PADDING) {
840 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,
841 EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
848 if (ctx->buf_len || !ctx->final_used) {
849 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
852 OPENSSL_assert(b <= sizeof(ctx->final));
855 * The following assumes that the ciphertext has been authenticated.
856 * Otherwise it provides a padding oracle.
858 n = ctx->final[b - 1];
859 if (n == 0 || n > (int)b) {
860 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
863 for (i = 0; i < n; i++) {
864 if (ctx->final[--b] != n) {
865 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
869 n = ctx->cipher->block_size - n;
870 for (i = 0; i < n; i++)
871 out[i] = ctx->final[i];
878 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
880 if (c->cipher->prov != NULL) {
882 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
885 if (EVP_CIPHER_CTX_key_length(c) == keylen)
888 /* Check the cipher actually understands this parameter */
889 if (OSSL_PARAM_locate_const(EVP_CIPHER_settable_ctx_params(c->cipher),
890 OSSL_CIPHER_PARAM_KEYLEN) == NULL)
893 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
894 ok = evp_do_ciph_ctx_setparams(c->cipher, c->provctx, params);
896 return ok > 0 ? 1 : 0;
899 /* TODO(3.0) legacy code follows */
902 * Note there have never been any built-in ciphers that define this flag
903 * since it was first introduced.
905 if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
906 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
907 if (EVP_CIPHER_CTX_key_length(c) == keylen)
909 if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
913 EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH);
917 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
920 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
921 unsigned int pd = pad;
924 ctx->flags &= ~EVP_CIPH_NO_PADDING;
926 ctx->flags |= EVP_CIPH_NO_PADDING;
928 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
929 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
934 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
936 int ret = EVP_CTRL_RET_UNSUPPORTED;
940 OSSL_PARAM params[4] = {
941 OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END, OSSL_PARAM_END
944 if (ctx == NULL || ctx->cipher == NULL) {
945 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
949 if (ctx->cipher->prov == NULL)
953 case EVP_CTRL_SET_KEY_LENGTH:
954 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
956 case EVP_CTRL_RAND_KEY: /* Used by DES */
959 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
965 * TODO(3.0) EVP_CTRL_INIT is purely legacy, no provider counterpart
966 * As a matter of fact, this should be dead code, but some caller
967 * might still do a direct control call with this command, so...
968 * Legacy methods return 1 except for exceptional circumstances, so
969 * we do the same here to not be disruptive.
972 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
975 case EVP_CTRL_GET_IV:
977 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV,
980 case EVP_CTRL_AEAD_SET_IVLEN:
983 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
985 case EVP_CTRL_AEAD_SET_IV_FIXED:
986 params[0] = OSSL_PARAM_construct_octet_string(
987 OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED, ptr, sz);
989 case EVP_CTRL_GCM_IV_GEN:
992 sz = 0; /* special case that uses the iv length */
993 params[0] = OSSL_PARAM_construct_octet_string(
994 OSSL_CIPHER_PARAM_AEAD_TLS1_GET_IV_GEN, ptr, sz);
996 case EVP_CTRL_GCM_SET_IV_INV:
999 params[0] = OSSL_PARAM_construct_octet_string(
1000 OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV, ptr, sz);
1002 case EVP_CTRL_GET_RC5_ROUNDS:
1003 set_params = 0; /* Fall thru */
1004 case EVP_CTRL_SET_RC5_ROUNDS:
1007 i = (unsigned int)arg;
1008 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_ROUNDS, &i);
1010 case EVP_CTRL_SET_SPEED:
1013 i = (unsigned int)arg;
1014 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_SPEED, &i);
1016 case EVP_CTRL_AEAD_GET_TAG:
1017 set_params = 0; /* Fall thru */
1018 case EVP_CTRL_AEAD_SET_TAG:
1019 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1022 case EVP_CTRL_AEAD_TLS1_AAD:
1023 /* This one does a set and a get - since it returns a size */
1025 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1027 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1031 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1032 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
1036 #ifndef OPENSSL_NO_RC2
1037 case EVP_CTRL_GET_RC2_KEY_BITS:
1038 set_params = 0; /* Fall thru */
1039 case EVP_CTRL_SET_RC2_KEY_BITS:
1040 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_RC2_KEYBITS, &sz);
1042 #endif /* OPENSSL_NO_RC2 */
1043 #if !defined(OPENSSL_NO_MULTIBLOCK)
1044 case EVP_CTRL_TLS1_1_MULTIBLOCK_MAX_BUFSIZE:
1045 params[0] = OSSL_PARAM_construct_size_t(
1046 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_SEND_FRAGMENT, &sz);
1047 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1051 params[0] = OSSL_PARAM_construct_size_t(
1052 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_MAX_BUFSIZE, &sz);
1053 params[1] = OSSL_PARAM_construct_end();
1054 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
1058 case EVP_CTRL_TLS1_1_MULTIBLOCK_AAD: {
1059 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1060 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1062 if (arg < (int)sizeof(EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM))
1065 params[0] = OSSL_PARAM_construct_octet_string(
1066 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD, (void*)p->inp, p->len);
1067 params[1] = OSSL_PARAM_construct_uint(
1068 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1069 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1072 /* Retrieve the return values changed by the set */
1073 params[0] = OSSL_PARAM_construct_size_t(
1074 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_AAD_PACKLEN, &sz);
1075 params[1] = OSSL_PARAM_construct_uint(
1076 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1077 params[2] = OSSL_PARAM_construct_end();
1078 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
1083 case EVP_CTRL_TLS1_1_MULTIBLOCK_ENCRYPT: {
1084 EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *p =
1085 (EVP_CTRL_TLS1_1_MULTIBLOCK_PARAM *)ptr;
1087 params[0] = OSSL_PARAM_construct_octet_string(
1088 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC, p->out, p->len);
1090 params[1] = OSSL_PARAM_construct_octet_string(
1091 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_IN, (void*)p->inp,
1093 params[2] = OSSL_PARAM_construct_uint(
1094 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_INTERLEAVE, &p->interleave);
1095 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1098 params[0] = OSSL_PARAM_construct_size_t(
1099 OSSL_CIPHER_PARAM_TLS1_MULTIBLOCK_ENC_LEN, &sz);
1100 params[1] = OSSL_PARAM_construct_end();
1101 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
1106 #endif /* OPENSSL_NO_MULTIBLOCK */
1107 case EVP_CTRL_AEAD_SET_MAC_KEY:
1110 params[0] = OSSL_PARAM_construct_octet_string(
1111 OSSL_CIPHER_PARAM_AEAD_MAC_KEY, ptr, sz);
1116 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1118 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
1121 /* TODO(3.0): Remove legacy code below */
1123 if (ctx->cipher->ctrl == NULL) {
1124 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
1128 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
1131 if (ret == EVP_CTRL_RET_UNSUPPORTED) {
1132 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL,
1133 EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
1139 int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1141 if (cipher != NULL && cipher->get_params != NULL)
1142 return cipher->get_params(params);
1146 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1148 if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL)
1149 return ctx->cipher->set_ctx_params(ctx->provctx, params);
1153 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1155 if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1156 return ctx->cipher->get_ctx_params(ctx->provctx, params);
1160 const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1162 if (cipher != NULL && cipher->gettable_params != NULL)
1163 return cipher->gettable_params(
1164 ossl_provider_ctx(EVP_CIPHER_provider(cipher)));
1168 const OSSL_PARAM *EVP_CIPHER_settable_ctx_params(const EVP_CIPHER *cipher)
1170 if (cipher != NULL && cipher->settable_ctx_params != NULL)
1171 return cipher->settable_ctx_params(
1172 ossl_provider_ctx(EVP_CIPHER_provider(cipher)));
1176 const OSSL_PARAM *EVP_CIPHER_gettable_ctx_params(const EVP_CIPHER *cipher)
1178 if (cipher != NULL && cipher->gettable_ctx_params != NULL)
1179 return cipher->gettable_ctx_params(
1180 ossl_provider_ctx(EVP_CIPHER_provider(cipher)));
1184 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
1186 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1187 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1195 kl = EVP_CIPHER_CTX_key_length(ctx);
1196 if (kl <= 0 || RAND_priv_bytes(key, kl) <= 0)
1200 #endif /* FIPS_MODULE */
1203 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
1205 if ((in == NULL) || (in->cipher == NULL)) {
1206 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED);
1210 if (in->cipher->prov == NULL)
1213 if (in->cipher->dupctx == NULL) {
1214 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_NOT_ABLE_TO_COPY_CTX);
1218 EVP_CIPHER_CTX_reset(out);
1221 out->provctx = NULL;
1223 if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
1224 out->fetched_cipher = NULL;
1228 out->provctx = in->cipher->dupctx(in->provctx);
1229 if (out->provctx == NULL) {
1230 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_NOT_ABLE_TO_COPY_CTX);
1236 /* TODO(3.0): Remove legacy code below */
1239 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
1240 /* Make sure it's safe to copy a cipher context using an ENGINE */
1241 if (in->engine && !ENGINE_init(in->engine)) {
1242 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB);
1247 EVP_CIPHER_CTX_reset(out);
1248 memcpy(out, in, sizeof(*out));
1250 if (in->cipher_data && in->cipher->ctx_size) {
1251 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
1252 if (out->cipher_data == NULL) {
1254 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE);
1257 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
1260 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
1261 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
1263 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INITIALIZATION_ERROR);
1269 EVP_CIPHER *evp_cipher_new(void)
1271 EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1273 if (cipher != NULL) {
1274 cipher->lock = CRYPTO_THREAD_lock_new();
1275 if (cipher->lock == NULL) {
1276 OPENSSL_free(cipher);
1285 * FIPS module note: since internal fetches will be entirely
1286 * provider based, we know that none of its code depends on legacy
1287 * NIDs or any functionality that use them.
1290 /* TODO(3.x) get rid of the need for legacy NIDs */
1291 static void set_legacy_nid(const char *name, void *vlegacy_nid)
1294 int *legacy_nid = vlegacy_nid;
1296 * We use lowest level function to get the associated method, because
1297 * higher level functions such as EVP_get_cipherbyname() have changed
1298 * to look at providers too.
1300 const void *legacy_method = OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
1302 if (*legacy_nid == -1) /* We found a clash already */
1304 if (legacy_method == NULL)
1306 nid = EVP_CIPHER_nid(legacy_method);
1307 if (*legacy_nid != NID_undef && *legacy_nid != nid) {
1315 static void *evp_cipher_from_dispatch(const int name_id,
1316 const OSSL_DISPATCH *fns,
1317 OSSL_PROVIDER *prov)
1319 EVP_CIPHER *cipher = NULL;
1320 int fnciphcnt = 0, fnctxcnt = 0;
1322 if ((cipher = evp_cipher_new()) == NULL) {
1323 EVPerr(0, ERR_R_MALLOC_FAILURE);
1328 /* TODO(3.x) get rid of the need for legacy NIDs */
1329 cipher->nid = NID_undef;
1330 evp_names_do_all(prov, name_id, set_legacy_nid, &cipher->nid);
1331 if (cipher->nid == -1) {
1332 ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
1333 EVP_CIPHER_free(cipher);
1338 cipher->name_id = name_id;
1340 for (; fns->function_id != 0; fns++) {
1341 switch (fns->function_id) {
1342 case OSSL_FUNC_CIPHER_NEWCTX:
1343 if (cipher->newctx != NULL)
1345 cipher->newctx = OSSL_FUNC_cipher_newctx(fns);
1348 case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1349 if (cipher->einit != NULL)
1351 cipher->einit = OSSL_FUNC_cipher_encrypt_init(fns);
1354 case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1355 if (cipher->dinit != NULL)
1357 cipher->dinit = OSSL_FUNC_cipher_decrypt_init(fns);
1360 case OSSL_FUNC_CIPHER_UPDATE:
1361 if (cipher->cupdate != NULL)
1363 cipher->cupdate = OSSL_FUNC_cipher_update(fns);
1366 case OSSL_FUNC_CIPHER_FINAL:
1367 if (cipher->cfinal != NULL)
1369 cipher->cfinal = OSSL_FUNC_cipher_final(fns);
1372 case OSSL_FUNC_CIPHER_CIPHER:
1373 if (cipher->ccipher != NULL)
1375 cipher->ccipher = OSSL_FUNC_cipher_cipher(fns);
1377 case OSSL_FUNC_CIPHER_FREECTX:
1378 if (cipher->freectx != NULL)
1380 cipher->freectx = OSSL_FUNC_cipher_freectx(fns);
1383 case OSSL_FUNC_CIPHER_DUPCTX:
1384 if (cipher->dupctx != NULL)
1386 cipher->dupctx = OSSL_FUNC_cipher_dupctx(fns);
1388 case OSSL_FUNC_CIPHER_GET_PARAMS:
1389 if (cipher->get_params != NULL)
1391 cipher->get_params = OSSL_FUNC_cipher_get_params(fns);
1393 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1394 if (cipher->get_ctx_params != NULL)
1396 cipher->get_ctx_params = OSSL_FUNC_cipher_get_ctx_params(fns);
1398 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1399 if (cipher->set_ctx_params != NULL)
1401 cipher->set_ctx_params = OSSL_FUNC_cipher_set_ctx_params(fns);
1403 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1404 if (cipher->gettable_params != NULL)
1406 cipher->gettable_params = OSSL_FUNC_cipher_gettable_params(fns);
1408 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1409 if (cipher->gettable_ctx_params != NULL)
1411 cipher->gettable_ctx_params =
1412 OSSL_FUNC_cipher_gettable_ctx_params(fns);
1414 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1415 if (cipher->settable_ctx_params != NULL)
1417 cipher->settable_ctx_params =
1418 OSSL_FUNC_cipher_settable_ctx_params(fns);
1422 if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1423 || (fnciphcnt == 0 && cipher->ccipher == NULL)
1426 * In order to be a consistent set of functions we must have at least
1427 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1428 * functions, or a single "cipher" function. In all cases we need both
1429 * the "newctx" and "freectx" functions.
1431 EVP_CIPHER_free(cipher);
1432 EVPerr(EVP_F_EVP_CIPHER_FROM_DISPATCH, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1435 cipher->prov = prov;
1437 ossl_provider_up_ref(prov);
1442 static int evp_cipher_up_ref(void *cipher)
1444 return EVP_CIPHER_up_ref(cipher);
1447 static void evp_cipher_free(void *cipher)
1449 EVP_CIPHER_free(cipher);
1452 EVP_CIPHER *EVP_CIPHER_fetch(OPENSSL_CTX *ctx, const char *algorithm,
1453 const char *properties)
1455 EVP_CIPHER *cipher =
1456 evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1457 evp_cipher_from_dispatch, evp_cipher_up_ref,
1460 if (cipher != NULL && !evp_cipher_cache_constants(cipher)) {
1461 EVP_CIPHER_free(cipher);
1467 int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
1471 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
1475 void EVP_CIPHER_free(EVP_CIPHER *cipher)
1482 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
1485 ossl_provider_free(cipher->prov);
1486 CRYPTO_THREAD_lock_free(cipher->lock);
1487 OPENSSL_free(cipher);
1490 void EVP_CIPHER_do_all_provided(OPENSSL_CTX *libctx,
1491 void (*fn)(EVP_CIPHER *mac, void *arg),
1494 evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1495 (void (*)(void *, void *))fn, arg,
1496 evp_cipher_from_dispatch, evp_cipher_free);