2 * Copyright 1995-2019 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
12 #include "internal/cryptlib.h"
13 #include <openssl/evp.h>
14 #include <openssl/err.h>
15 #include <openssl/rand.h>
16 #include <openssl/rand_drbg.h>
17 #include <openssl/engine.h>
18 #include <openssl/params.h>
19 #include <openssl/core_names.h>
20 #include "internal/evp_int.h"
21 #include "internal/provider.h"
24 int EVP_CIPHER_CTX_reset(EVP_CIPHER_CTX *ctx)
29 if (ctx->cipher == NULL || ctx->cipher->prov == NULL)
32 if (ctx->provctx != NULL) {
33 if (ctx->cipher->freectx != NULL)
34 ctx->cipher->freectx(ctx->provctx);
37 if (ctx->fetched_cipher != NULL)
38 EVP_CIPHER_free(ctx->fetched_cipher);
39 memset(ctx, 0, sizeof(*ctx));
43 /* TODO(3.0): Remove legacy code below */
46 if (ctx->cipher != NULL) {
47 if (ctx->cipher->cleanup && !ctx->cipher->cleanup(ctx))
49 /* Cleanse cipher context data */
50 if (ctx->cipher_data && ctx->cipher->ctx_size)
51 OPENSSL_cleanse(ctx->cipher_data, ctx->cipher->ctx_size);
53 OPENSSL_free(ctx->cipher_data);
54 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
55 ENGINE_finish(ctx->engine);
57 memset(ctx, 0, sizeof(*ctx));
61 EVP_CIPHER_CTX *EVP_CIPHER_CTX_new(void)
63 return OPENSSL_zalloc(sizeof(EVP_CIPHER_CTX));
66 void EVP_CIPHER_CTX_free(EVP_CIPHER_CTX *ctx)
68 EVP_CIPHER_CTX_reset(ctx);
72 int EVP_CipherInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
73 const unsigned char *key, const unsigned char *iv, int enc)
76 EVP_CIPHER_CTX_reset(ctx);
77 return EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, enc);
80 int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
81 ENGINE *impl, const unsigned char *key,
82 const unsigned char *iv, int enc)
84 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
85 ENGINE *tmpimpl = NULL;
87 const EVP_CIPHER *tmpcipher;
90 * enc == 1 means we are encrypting.
91 * enc == 0 means we are decrypting.
92 * enc == -1 means, use the previously initialised value for encrypt/decrypt
102 if (cipher == NULL && ctx->cipher == NULL) {
103 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET);
107 /* TODO(3.0): Legacy work around code below. Remove this */
109 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
111 * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
112 * this context may already have an ENGINE! Try to avoid releasing the
113 * previous handle, re-querying for an ENGINE, and having a
114 * reinitialisation, when it may all be unnecessary.
116 if (ctx->engine && ctx->cipher
117 && (cipher == NULL || cipher->nid == ctx->cipher->nid))
120 if (cipher != NULL && impl == NULL) {
121 /* Ask if an ENGINE is reserved for this job */
122 tmpimpl = ENGINE_get_cipher_engine(cipher->nid);
127 * If there are engines involved then we should use legacy handling for now.
129 if (ctx->engine != NULL
130 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
134 if (ctx->cipher == ctx->fetched_cipher)
136 EVP_CIPHER_free(ctx->fetched_cipher);
137 ctx->fetched_cipher = NULL;
141 tmpcipher = (cipher == NULL) ? ctx->cipher : cipher;
143 if (tmpcipher->prov == NULL) {
144 switch(tmpcipher->nid) {
145 case NID_aes_256_ecb:
146 case NID_aes_192_ecb:
147 case NID_aes_128_ecb:
148 case NID_aes_256_cbc:
149 case NID_aes_192_cbc:
150 case NID_aes_128_cbc:
151 case NID_aes_256_ofb128:
152 case NID_aes_192_ofb128:
153 case NID_aes_128_ofb128:
154 case NID_aes_256_cfb128:
155 case NID_aes_192_cfb128:
156 case NID_aes_128_cfb128:
157 case NID_aes_256_cfb1:
158 case NID_aes_192_cfb1:
159 case NID_aes_128_cfb1:
160 case NID_aes_256_cfb8:
161 case NID_aes_192_cfb8:
162 case NID_aes_128_cfb8:
163 case NID_aes_256_ctr:
164 case NID_aes_192_ctr:
165 case NID_aes_128_ctr:
166 case NID_aes_128_xts:
167 case NID_aes_256_xts:
168 case NID_aes_256_gcm:
169 case NID_aes_192_gcm:
170 case NID_aes_128_gcm:
171 case NID_aria_256_gcm:
172 case NID_aria_192_gcm:
173 case NID_aria_128_gcm:
174 case NID_aes_256_ccm:
175 case NID_aes_192_ccm:
176 case NID_aes_128_ccm:
177 case NID_aria_256_ccm:
178 case NID_aria_192_ccm:
179 case NID_aria_128_ccm:
180 case NID_aria_256_ecb:
181 case NID_aria_192_ecb:
182 case NID_aria_128_ecb:
183 case NID_aria_256_cbc:
184 case NID_aria_192_cbc:
185 case NID_aria_128_cbc:
186 case NID_aria_256_ofb128:
187 case NID_aria_192_ofb128:
188 case NID_aria_128_ofb128:
189 case NID_aria_256_cfb128:
190 case NID_aria_192_cfb128:
191 case NID_aria_128_cfb128:
192 case NID_aria_256_cfb1:
193 case NID_aria_192_cfb1:
194 case NID_aria_128_cfb1:
195 case NID_aria_256_cfb8:
196 case NID_aria_192_cfb8:
197 case NID_aria_128_cfb8:
198 case NID_aria_256_ctr:
199 case NID_aria_192_ctr:
200 case NID_aria_128_ctr:
201 case NID_camellia_256_ecb:
202 case NID_camellia_192_ecb:
203 case NID_camellia_128_ecb:
204 case NID_camellia_256_cbc:
205 case NID_camellia_192_cbc:
206 case NID_camellia_128_cbc:
207 case NID_camellia_256_ofb128:
208 case NID_camellia_192_ofb128:
209 case NID_camellia_128_ofb128:
210 case NID_camellia_256_cfb128:
211 case NID_camellia_192_cfb128:
212 case NID_camellia_128_cfb128:
213 case NID_camellia_256_cfb1:
214 case NID_camellia_192_cfb1:
215 case NID_camellia_128_cfb1:
216 case NID_camellia_256_cfb8:
217 case NID_camellia_192_cfb8:
218 case NID_camellia_128_cfb8:
219 case NID_camellia_256_ctr:
220 case NID_camellia_192_ctr:
221 case NID_camellia_128_ctr:
222 case NID_des_ede3_cbc:
223 case NID_des_ede3_ecb:
224 case NID_des_ede3_ofb64:
225 case NID_des_ede3_cfb64:
226 case NID_des_ede3_cfb8:
227 case NID_des_ede3_cfb1:
228 case NID_des_ede_cbc:
229 case NID_des_ede_ecb:
230 case NID_des_ede_ofb64:
231 case NID_des_ede_cfb64:
233 case NID_id_smime_alg_CMS3DESwrap:
244 case NID_cast5_cfb64:
245 case NID_cast5_ofb64:
253 * Ensure a context left lying around from last time is cleared
256 if (cipher != NULL && ctx->cipher != NULL) {
257 OPENSSL_clear_free(ctx->cipher_data, ctx->cipher->ctx_size);
258 ctx->cipher_data = NULL;
262 /* TODO(3.0): Start of non-legacy code below */
264 /* Ensure a context left lying around from last time is cleared */
265 if (cipher != NULL && ctx->cipher != NULL) {
266 unsigned long flags = ctx->flags;
268 EVP_CIPHER_CTX_reset(ctx);
269 /* Restore encrypt and flags */
275 cipher = ctx->cipher;
277 if (cipher->prov == NULL) {
279 /* We only do explict fetches inside the FIPS module */
280 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
283 EVP_CIPHER *provciph =
284 EVP_CIPHER_fetch(NULL, OBJ_nid2sn(cipher->nid), "");
286 if (provciph == NULL) {
287 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
291 EVP_CIPHER_free(ctx->fetched_cipher);
292 ctx->fetched_cipher = provciph;
296 ctx->cipher = cipher;
297 if (ctx->provctx == NULL) {
298 ctx->provctx = ctx->cipher->newctx(ossl_provider_ctx(cipher->prov));
299 if (ctx->provctx == NULL) {
300 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
305 if ((ctx->flags & EVP_CIPH_NO_PADDING) != 0) {
307 * If this ctx was already set up for no padding then we need to tell
308 * the new cipher about it.
310 if (!EVP_CIPHER_CTX_set_padding(ctx, 0))
314 switch (EVP_CIPHER_mode(ctx->cipher)) {
315 case EVP_CIPH_CFB_MODE:
316 case EVP_CIPH_OFB_MODE:
317 case EVP_CIPH_CBC_MODE:
318 /* For these modes we remember the original IV for later use */
319 if (!ossl_assert(EVP_CIPHER_CTX_iv_length(ctx) <= (int)sizeof(ctx->oiv))) {
320 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
324 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
328 if (ctx->cipher->einit == NULL) {
329 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
333 return ctx->cipher->einit(ctx->provctx,
336 : EVP_CIPHER_CTX_key_length(ctx),
339 : EVP_CIPHER_CTX_iv_length(ctx));
342 if (ctx->cipher->dinit == NULL) {
343 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
347 return ctx->cipher->dinit(ctx->provctx,
350 : EVP_CIPHER_CTX_key_length(ctx),
353 : EVP_CIPHER_CTX_iv_length(ctx));
355 /* TODO(3.0): Remove legacy code below */
358 if (cipher != NULL) {
360 * Ensure a context left lying around from last time is cleared (we
361 * previously attempted to avoid this if the same ENGINE and
362 * EVP_CIPHER could be used).
365 unsigned long flags = ctx->flags;
366 EVP_CIPHER_CTX_reset(ctx);
367 /* Restore encrypt and flags */
371 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
373 if (!ENGINE_init(impl)) {
374 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
381 /* There's an ENGINE for this job ... (apparently) */
382 const EVP_CIPHER *c = ENGINE_get_cipher(impl, cipher->nid);
386 * One positive side-effect of US's export control history,
387 * is that we should at least be able to avoid using US
388 * misspellings of "initialisation"?
390 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
393 /* We'll use the ENGINE's private cipher definition */
396 * Store the ENGINE functional reference so we know 'cipher' came
397 * from an ENGINE and we need to release it when done.
405 ctx->cipher = cipher;
406 if (ctx->cipher->ctx_size) {
407 ctx->cipher_data = OPENSSL_zalloc(ctx->cipher->ctx_size);
408 if (ctx->cipher_data == NULL) {
410 EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
414 ctx->cipher_data = NULL;
416 ctx->key_len = cipher->key_len;
417 /* Preserve wrap enable flag, zero everything else */
418 ctx->flags &= EVP_CIPHER_CTX_FLAG_WRAP_ALLOW;
419 if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
420 if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
422 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
427 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
430 if (ctx->cipher == NULL)
433 /* we assume block size is a power of 2 in *cryptUpdate */
434 OPENSSL_assert(ctx->cipher->block_size == 1
435 || ctx->cipher->block_size == 8
436 || ctx->cipher->block_size == 16);
438 if (!(ctx->flags & EVP_CIPHER_CTX_FLAG_WRAP_ALLOW)
439 && EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_WRAP_MODE) {
440 EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_WRAP_MODE_NOT_ALLOWED);
444 if (!(EVP_CIPHER_flags(EVP_CIPHER_CTX_cipher(ctx)) & EVP_CIPH_CUSTOM_IV)) {
445 switch (EVP_CIPHER_CTX_mode(ctx)) {
447 case EVP_CIPH_STREAM_CIPHER:
448 case EVP_CIPH_ECB_MODE:
451 case EVP_CIPH_CFB_MODE:
452 case EVP_CIPH_OFB_MODE:
457 case EVP_CIPH_CBC_MODE:
459 OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
460 (int)sizeof(ctx->iv));
462 memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
463 memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
466 case EVP_CIPH_CTR_MODE:
468 /* Don't reuse IV for CTR mode */
470 memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
478 if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
479 if (!ctx->cipher->init(ctx, key, iv, enc))
484 ctx->block_mask = ctx->cipher->block_size - 1;
488 int EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
489 const unsigned char *in, int inl)
492 return EVP_EncryptUpdate(ctx, out, outl, in, inl);
494 return EVP_DecryptUpdate(ctx, out, outl, in, inl);
497 int EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
500 return EVP_EncryptFinal_ex(ctx, out, outl);
502 return EVP_DecryptFinal_ex(ctx, out, outl);
505 int EVP_CipherFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
508 return EVP_EncryptFinal(ctx, out, outl);
510 return EVP_DecryptFinal(ctx, out, outl);
513 int EVP_EncryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
514 const unsigned char *key, const unsigned char *iv)
516 return EVP_CipherInit(ctx, cipher, key, iv, 1);
519 int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
520 ENGINE *impl, const unsigned char *key,
521 const unsigned char *iv)
523 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 1);
526 int EVP_DecryptInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
527 const unsigned char *key, const unsigned char *iv)
529 return EVP_CipherInit(ctx, cipher, key, iv, 0);
532 int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
533 ENGINE *impl, const unsigned char *key,
534 const unsigned char *iv)
536 return EVP_CipherInit_ex(ctx, cipher, impl, key, iv, 0);
540 * According to the letter of standard difference between pointers
541 * is specified to be valid only within same object. This makes
542 * it formally challenging to determine if input and output buffers
543 * are not partially overlapping with standard pointer arithmetic.
548 #if defined(OPENSSL_SYS_VMS) && __INITIAL_POINTER_SIZE==64
550 * Then we have VMS that distinguishes itself by adhering to
551 * sizeof(size_t)==4 even in 64-bit builds, which means that
552 * difference between two pointers might be truncated to 32 bits.
553 * In the context one can even wonder how comparison for
554 * equality is implemented. To be on the safe side we adhere to
555 * PTRDIFF_T even for comparison for equality.
557 # define PTRDIFF_T uint64_t
559 # define PTRDIFF_T size_t
562 int is_partially_overlapping(const void *ptr1, const void *ptr2, int len)
564 PTRDIFF_T diff = (PTRDIFF_T)ptr1-(PTRDIFF_T)ptr2;
566 * Check for partially overlapping buffers. [Binary logical
567 * operations are used instead of boolean to minimize number
568 * of conditional branches.]
570 int overlapped = (len > 0) & (diff != 0) & ((diff < (PTRDIFF_T)len) |
571 (diff > (0 - (PTRDIFF_T)len)));
576 static int evp_EncryptDecryptUpdate(EVP_CIPHER_CTX *ctx,
577 unsigned char *out, int *outl,
578 const unsigned char *in, int inl)
580 int i, j, bl, cmpl = inl;
582 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
583 cmpl = (cmpl + 7) / 8;
585 bl = ctx->cipher->block_size;
587 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
588 /* If block size > 1 then the cipher will have to do this check */
589 if (bl == 1 && is_partially_overlapping(out, in, cmpl)) {
590 EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
594 i = ctx->cipher->do_cipher(ctx, out, in, inl);
606 if (is_partially_overlapping(out + ctx->buf_len, in, cmpl)) {
607 EVPerr(EVP_F_EVP_ENCRYPTDECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
611 if (ctx->buf_len == 0 && (inl & (ctx->block_mask)) == 0) {
612 if (ctx->cipher->do_cipher(ctx, out, in, inl)) {
621 OPENSSL_assert(bl <= (int)sizeof(ctx->buf));
624 memcpy(&(ctx->buf[i]), in, inl);
630 memcpy(&(ctx->buf[i]), in, j);
633 if (!ctx->cipher->do_cipher(ctx, out, ctx->buf, bl))
643 if (!ctx->cipher->do_cipher(ctx, out, in, inl))
649 memcpy(ctx->buf, &(in[inl]), i);
655 int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
656 const unsigned char *in, int inl)
662 /* Prevent accidental use of decryption context when encrypting */
664 EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_INVALID_OPERATION);
668 if (ctx->cipher == NULL) {
669 EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_NO_CIPHER_SET);
673 if (ctx->cipher->prov == NULL)
676 blocksize = EVP_CIPHER_CTX_block_size(ctx);
678 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
679 EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_UPDATE_ERROR);
682 ret = ctx->cipher->cupdate(ctx->provctx, out, &soutl,
683 inl + (blocksize == 1 ? 0 : blocksize), in,
687 if (soutl > INT_MAX) {
688 EVPerr(EVP_F_EVP_ENCRYPTUPDATE, EVP_R_UPDATE_ERROR);
696 /* TODO(3.0): Remove legacy code below */
699 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
702 int EVP_EncryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
705 ret = EVP_EncryptFinal_ex(ctx, out, outl);
709 int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
712 unsigned int i, b, bl;
716 /* Prevent accidental use of decryption context when encrypting */
718 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
722 if (ctx->cipher == NULL) {
723 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_NO_CIPHER_SET);
726 if (ctx->cipher->prov == NULL)
729 blocksize = EVP_CIPHER_CTX_block_size(ctx);
731 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
732 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_FINAL_ERROR);
736 ret = ctx->cipher->cfinal(ctx->provctx, out, &soutl,
737 blocksize == 1 ? 0 : blocksize);
740 if (soutl > INT_MAX) {
741 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX, EVP_R_FINAL_ERROR);
749 /* TODO(3.0): Remove legacy code below */
752 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
753 ret = ctx->cipher->do_cipher(ctx, out, NULL, 0);
761 b = ctx->cipher->block_size;
762 OPENSSL_assert(b <= sizeof(ctx->buf));
768 if (ctx->flags & EVP_CIPH_NO_PADDING) {
770 EVPerr(EVP_F_EVP_ENCRYPTFINAL_EX,
771 EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
779 for (i = bl; i < b; i++)
781 ret = ctx->cipher->do_cipher(ctx, out, ctx->buf, b);
789 int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,
790 const unsigned char *in, int inl)
792 int fix_len, cmpl = inl, ret;
797 /* Prevent accidental use of encryption context when decrypting */
799 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_INVALID_OPERATION);
803 if (ctx->cipher == NULL) {
804 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_NO_CIPHER_SET);
807 if (ctx->cipher->prov == NULL)
810 blocksize = EVP_CIPHER_CTX_block_size(ctx);
812 if (ctx->cipher->cupdate == NULL || blocksize < 1) {
813 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_UPDATE_ERROR);
816 ret = ctx->cipher->cupdate(ctx->provctx, out, &soutl,
817 inl + (blocksize == 1 ? 0 : blocksize), in,
821 if (soutl > INT_MAX) {
822 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_UPDATE_ERROR);
830 /* TODO(3.0): Remove legacy code below */
833 b = ctx->cipher->block_size;
835 if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
836 cmpl = (cmpl + 7) / 8;
838 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
839 if (b == 1 && is_partially_overlapping(out, in, cmpl)) {
840 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
844 fix_len = ctx->cipher->do_cipher(ctx, out, in, inl);
858 if (ctx->flags & EVP_CIPH_NO_PADDING)
859 return evp_EncryptDecryptUpdate(ctx, out, outl, in, inl);
861 OPENSSL_assert(b <= sizeof(ctx->final));
863 if (ctx->final_used) {
864 /* see comment about PTRDIFF_T comparison above */
865 if (((PTRDIFF_T)out == (PTRDIFF_T)in)
866 || is_partially_overlapping(out, in, b)) {
867 EVPerr(EVP_F_EVP_DECRYPTUPDATE, EVP_R_PARTIALLY_OVERLAPPING);
870 memcpy(out, ctx->final, b);
876 if (!evp_EncryptDecryptUpdate(ctx, out, outl, in, inl))
880 * if we have 'decrypted' a multiple of block size, make sure we have a
881 * copy of this last block
883 if (b > 1 && !ctx->buf_len) {
886 memcpy(ctx->final, &out[*outl], b);
896 int EVP_DecryptFinal(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
899 ret = EVP_DecryptFinal_ex(ctx, out, outl);
903 int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl)
911 /* Prevent accidental use of encryption context when decrypting */
913 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_INVALID_OPERATION);
917 if (ctx->cipher == NULL) {
918 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_NO_CIPHER_SET);
922 if (ctx->cipher->prov == NULL)
925 blocksize = EVP_CIPHER_CTX_block_size(ctx);
927 if (blocksize < 1 || ctx->cipher->cfinal == NULL) {
928 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_FINAL_ERROR);
932 ret = ctx->cipher->cfinal(ctx->provctx, out, &soutl,
933 blocksize == 1 ? 0 : blocksize);
936 if (soutl > INT_MAX) {
937 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_FINAL_ERROR);
945 /* TODO(3.0): Remove legacy code below */
949 if (ctx->cipher->flags & EVP_CIPH_FLAG_CUSTOM_CIPHER) {
950 i = ctx->cipher->do_cipher(ctx, out, NULL, 0);
958 b = ctx->cipher->block_size;
959 if (ctx->flags & EVP_CIPH_NO_PADDING) {
961 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX,
962 EVP_R_DATA_NOT_MULTIPLE_OF_BLOCK_LENGTH);
969 if (ctx->buf_len || !ctx->final_used) {
970 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_WRONG_FINAL_BLOCK_LENGTH);
973 OPENSSL_assert(b <= sizeof(ctx->final));
976 * The following assumes that the ciphertext has been authenticated.
977 * Otherwise it provides a padding oracle.
979 n = ctx->final[b - 1];
980 if (n == 0 || n > (int)b) {
981 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
984 for (i = 0; i < n; i++) {
985 if (ctx->final[--b] != n) {
986 EVPerr(EVP_F_EVP_DECRYPTFINAL_EX, EVP_R_BAD_DECRYPT);
990 n = ctx->cipher->block_size - n;
991 for (i = 0; i < n; i++)
992 out[i] = ctx->final[i];
999 int EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int keylen)
1002 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1003 size_t len = keylen;
1005 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &len);
1006 ok = evp_do_ciph_ctx_setparams(c->cipher, c->provctx, params);
1008 if (ok != EVP_CTRL_RET_UNSUPPORTED)
1011 /* TODO(3.0) legacy code follows */
1012 if (c->cipher->flags & EVP_CIPH_CUSTOM_KEY_LENGTH)
1013 return EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_KEY_LENGTH, keylen, NULL);
1014 if (EVP_CIPHER_CTX_key_length(c) == keylen)
1016 if ((keylen > 0) && (c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH)) {
1017 c->key_len = keylen;
1020 EVPerr(EVP_F_EVP_CIPHER_CTX_SET_KEY_LENGTH, EVP_R_INVALID_KEY_LENGTH);
1024 int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad)
1027 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1028 unsigned int pd = pad;
1031 ctx->flags &= ~EVP_CIPH_NO_PADDING;
1033 ctx->flags |= EVP_CIPH_NO_PADDING;
1035 params[0] = OSSL_PARAM_construct_uint(OSSL_CIPHER_PARAM_PADDING, &pd);
1036 ok = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1041 int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
1043 int ret = EVP_CTRL_RET_UNSUPPORTED;
1046 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
1048 if (ctx == NULL || ctx->cipher == NULL) {
1049 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
1053 if (ctx->cipher->prov == NULL)
1057 case EVP_CTRL_SET_KEY_LENGTH:
1058 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_KEYLEN, &sz);
1060 case EVP_CTRL_RAND_KEY: /* Used by DES */
1063 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY,
1067 case EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS: /* Used by DASYNC */
1068 case EVP_CTRL_INIT: /* TODO(3.0) Purely legacy, no provider counterpart */
1070 return EVP_CTRL_RET_UNSUPPORTED;
1071 case EVP_CTRL_GET_IV:
1073 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_IV,
1076 case EVP_CTRL_AEAD_SET_IVLEN:
1079 params[0] = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_IVLEN, &sz);
1081 case EVP_CTRL_GCM_SET_IV_FIXED:
1083 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED,
1086 case EVP_CTRL_AEAD_GET_TAG:
1087 set_params = 0; /* Fall thru */
1088 case EVP_CTRL_AEAD_SET_TAG:
1089 params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG,
1092 case EVP_CTRL_AEAD_TLS1_AAD:
1093 /* This one does a set and a get - since it returns a padding size */
1095 OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD,
1097 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1101 OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, &sz);
1102 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
1109 ret = evp_do_ciph_ctx_setparams(ctx->cipher, ctx->provctx, params);
1111 ret = evp_do_ciph_ctx_getparams(ctx->cipher, ctx->provctx, params);
1114 /* TODO(3.0): Remove legacy code below */
1116 if (ctx->cipher->ctrl == NULL) {
1117 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
1121 ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
1122 if (ret == EVP_CTRL_RET_UNSUPPORTED) {
1123 EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL,
1124 EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
1130 int EVP_CIPHER_get_params(EVP_CIPHER *cipher, OSSL_PARAM params[])
1132 if (cipher != NULL && cipher->get_params != NULL)
1133 return cipher->get_params(params);
1137 int EVP_CIPHER_CTX_set_params(EVP_CIPHER_CTX *ctx, const OSSL_PARAM params[])
1139 if (ctx->cipher != NULL && ctx->cipher->set_ctx_params != NULL)
1140 return ctx->cipher->set_ctx_params(ctx->provctx, params);
1144 int EVP_CIPHER_CTX_get_params(EVP_CIPHER_CTX *ctx, OSSL_PARAM params[])
1146 if (ctx->cipher != NULL && ctx->cipher->get_ctx_params != NULL)
1147 return ctx->cipher->get_ctx_params(ctx->provctx, params);
1151 const OSSL_PARAM *EVP_CIPHER_gettable_params(const EVP_CIPHER *cipher)
1153 if (cipher != NULL && cipher->gettable_params != NULL)
1154 return cipher->gettable_params();
1158 const OSSL_PARAM *EVP_CIPHER_CTX_settable_params(const EVP_CIPHER *cipher)
1160 if (cipher != NULL && cipher->settable_ctx_params != NULL)
1161 return cipher->settable_ctx_params();
1165 const OSSL_PARAM *EVP_CIPHER_CTX_gettable_params(const EVP_CIPHER *cipher)
1167 if (cipher != NULL && cipher->gettable_ctx_params != NULL)
1168 return cipher->gettable_ctx_params();
1172 int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key)
1174 if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
1175 return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
1183 kl = EVP_CIPHER_CTX_key_length(ctx);
1184 if (kl <= 0 || RAND_priv_bytes(key, kl) <= 0)
1188 #endif /* FIPS_MODE */
1191 int EVP_CIPHER_CTX_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
1193 if ((in == NULL) || (in->cipher == NULL)) {
1194 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INPUT_NOT_INITIALIZED);
1198 if (in->cipher->prov == NULL)
1201 if (in->cipher->dupctx == NULL) {
1202 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_NOT_ABLE_TO_COPY_CTX);
1206 EVP_CIPHER_CTX_reset(out);
1209 out->provctx = NULL;
1211 if (in->fetched_cipher != NULL && !EVP_CIPHER_up_ref(in->fetched_cipher)) {
1212 out->fetched_cipher = NULL;
1216 out->provctx = in->cipher->dupctx(in->provctx);
1217 if (out->provctx == NULL) {
1218 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_NOT_ABLE_TO_COPY_CTX);
1224 /* TODO(3.0): Remove legacy code below */
1227 #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE)
1228 /* Make sure it's safe to copy a cipher context using an ENGINE */
1229 if (in->engine && !ENGINE_init(in->engine)) {
1230 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_ENGINE_LIB);
1235 EVP_CIPHER_CTX_reset(out);
1236 memcpy(out, in, sizeof(*out));
1238 if (in->cipher_data && in->cipher->ctx_size) {
1239 out->cipher_data = OPENSSL_malloc(in->cipher->ctx_size);
1240 if (out->cipher_data == NULL) {
1242 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, ERR_R_MALLOC_FAILURE);
1245 memcpy(out->cipher_data, in->cipher_data, in->cipher->ctx_size);
1248 if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
1249 if (!in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out)) {
1251 EVPerr(EVP_F_EVP_CIPHER_CTX_COPY, EVP_R_INITIALIZATION_ERROR);
1257 EVP_CIPHER *evp_cipher_new(void)
1259 EVP_CIPHER *cipher = OPENSSL_zalloc(sizeof(EVP_CIPHER));
1261 if (cipher != NULL) {
1262 cipher->lock = CRYPTO_THREAD_lock_new();
1263 if (cipher->lock == NULL) {
1264 OPENSSL_free(cipher);
1272 static void *evp_cipher_from_dispatch(const char *name,
1273 const OSSL_DISPATCH *fns,
1274 OSSL_PROVIDER *prov,
1277 EVP_CIPHER *cipher = NULL;
1278 int fnciphcnt = 0, fnctxcnt = 0;
1280 if ((cipher = evp_cipher_new()) == NULL
1281 || (cipher->name = OPENSSL_strdup(name)) == NULL) {
1282 EVP_CIPHER_free(cipher);
1283 EVPerr(0, ERR_R_MALLOC_FAILURE);
1289 * FIPS module note: since internal fetches will be entirely
1290 * provider based, we know that none of its code depends on legacy
1291 * NIDs or any functionality that use them.
1293 * TODO(3.x) get rid of the need for legacy NIDs
1295 cipher->nid = OBJ_sn2nid(name);
1298 for (; fns->function_id != 0; fns++) {
1299 switch (fns->function_id) {
1300 case OSSL_FUNC_CIPHER_NEWCTX:
1301 if (cipher->newctx != NULL)
1303 cipher->newctx = OSSL_get_OP_cipher_newctx(fns);
1306 case OSSL_FUNC_CIPHER_ENCRYPT_INIT:
1307 if (cipher->einit != NULL)
1309 cipher->einit = OSSL_get_OP_cipher_encrypt_init(fns);
1312 case OSSL_FUNC_CIPHER_DECRYPT_INIT:
1313 if (cipher->dinit != NULL)
1315 cipher->dinit = OSSL_get_OP_cipher_decrypt_init(fns);
1318 case OSSL_FUNC_CIPHER_UPDATE:
1319 if (cipher->cupdate != NULL)
1321 cipher->cupdate = OSSL_get_OP_cipher_update(fns);
1324 case OSSL_FUNC_CIPHER_FINAL:
1325 if (cipher->cfinal != NULL)
1327 cipher->cfinal = OSSL_get_OP_cipher_final(fns);
1330 case OSSL_FUNC_CIPHER_CIPHER:
1331 if (cipher->ccipher != NULL)
1333 cipher->ccipher = OSSL_get_OP_cipher_cipher(fns);
1335 case OSSL_FUNC_CIPHER_FREECTX:
1336 if (cipher->freectx != NULL)
1338 cipher->freectx = OSSL_get_OP_cipher_freectx(fns);
1341 case OSSL_FUNC_CIPHER_DUPCTX:
1342 if (cipher->dupctx != NULL)
1344 cipher->dupctx = OSSL_get_OP_cipher_dupctx(fns);
1346 case OSSL_FUNC_CIPHER_GET_PARAMS:
1347 if (cipher->get_params != NULL)
1349 cipher->get_params = OSSL_get_OP_cipher_get_params(fns);
1351 case OSSL_FUNC_CIPHER_GET_CTX_PARAMS:
1352 if (cipher->get_ctx_params != NULL)
1354 cipher->get_ctx_params = OSSL_get_OP_cipher_get_ctx_params(fns);
1356 case OSSL_FUNC_CIPHER_SET_CTX_PARAMS:
1357 if (cipher->set_ctx_params != NULL)
1359 cipher->set_ctx_params = OSSL_get_OP_cipher_set_ctx_params(fns);
1361 case OSSL_FUNC_CIPHER_GETTABLE_PARAMS:
1362 if (cipher->gettable_params != NULL)
1364 cipher->gettable_params = OSSL_get_OP_cipher_gettable_params(fns);
1366 case OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS:
1367 if (cipher->gettable_ctx_params != NULL)
1369 cipher->gettable_ctx_params =
1370 OSSL_get_OP_cipher_gettable_ctx_params(fns);
1372 case OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS:
1373 if (cipher->settable_ctx_params != NULL)
1375 cipher->settable_ctx_params =
1376 OSSL_get_OP_cipher_settable_ctx_params(fns);
1380 if ((fnciphcnt != 0 && fnciphcnt != 3 && fnciphcnt != 4)
1381 || (fnciphcnt == 0 && cipher->ccipher == NULL)
1384 * In order to be a consistent set of functions we must have at least
1385 * a complete set of "encrypt" functions, or a complete set of "decrypt"
1386 * functions, or a single "cipher" function. In all cases we need both
1387 * the "newctx" and "freectx" functions.
1389 EVP_CIPHER_free(cipher);
1390 EVPerr(EVP_F_EVP_CIPHER_FROM_DISPATCH, EVP_R_INVALID_PROVIDER_FUNCTIONS);
1393 cipher->prov = prov;
1395 ossl_provider_up_ref(prov);
1400 static int evp_cipher_up_ref(void *cipher)
1402 return EVP_CIPHER_up_ref(cipher);
1405 static void evp_cipher_free(void *cipher)
1407 EVP_CIPHER_free(cipher);
1410 EVP_CIPHER *EVP_CIPHER_fetch(OPENSSL_CTX *ctx, const char *algorithm,
1411 const char *properties)
1413 EVP_CIPHER *cipher =
1414 evp_generic_fetch(ctx, OSSL_OP_CIPHER, algorithm, properties,
1415 evp_cipher_from_dispatch, NULL, evp_cipher_up_ref,
1421 int EVP_CIPHER_up_ref(EVP_CIPHER *cipher)
1425 CRYPTO_UP_REF(&cipher->refcnt, &ref, cipher->lock);
1429 void EVP_CIPHER_free(EVP_CIPHER *cipher)
1436 CRYPTO_DOWN_REF(&cipher->refcnt, &i, cipher->lock);
1439 ossl_provider_free(cipher->prov);
1440 OPENSSL_free(cipher->name);
1441 CRYPTO_THREAD_lock_free(cipher->lock);
1442 OPENSSL_free(cipher);
1445 void EVP_CIPHER_do_all_ex(OPENSSL_CTX *libctx,
1446 void (*fn)(EVP_CIPHER *mac, void *arg),
1449 evp_generic_do_all(libctx, OSSL_OP_CIPHER,
1450 (void (*)(void *, void *))fn, arg,
1451 evp_cipher_from_dispatch, NULL, evp_cipher_free);