Skip to content

Commit 96a7616

Browse files
author
tb
committed
Fix EVP_DecryptFinal() for CCM ciphers
There is an old trap that you must not call EVP_*Final() when using AES-CCM. While encrypting this happens to be a noop and succeeds, but when decrypting, the call fails. This behavior changed in OpenSSL and BoringSSL, making the trap even worse since we now fail when the others succeed. This is an adaptation of OpenSSL commit 197421b1 to fix this. See also sfackler/rust-openssl#1805 (comment) ok beck kenjiro
1 parent f1973c7 commit 96a7616

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/lib/libcrypto/evp/e_aes.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: e_aes.c,v 1.69 2025/06/03 08:42:15 kenjiro Exp $ */
1+
/* $OpenBSD: e_aes.c,v 1.70 2025/06/06 07:41:01 tb Exp $ */
22
/* ====================================================================
33
* Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved.
44
*
@@ -2032,7 +2032,14 @@ aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
20322032
CCM128_CONTEXT *ccm = &cctx->ccm;
20332033

20342034
/* If not set up, return error */
2035-
if (!cctx->iv_set && !cctx->key_set)
2035+
if (!cctx->key_set)
2036+
return -1;
2037+
2038+
/* EVP_*Final() doesn't return any data */
2039+
if (in == NULL && out != NULL)
2040+
return 0;
2041+
2042+
if (!cctx->iv_set)
20362043
return -1;
20372044
if (!ctx->encrypt && !cctx->tag_set)
20382045
return -1;
@@ -2051,9 +2058,7 @@ aes_ccm_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
20512058
CRYPTO_ccm128_aad(ccm, in, len);
20522059
return len;
20532060
}
2054-
/* EVP_*Final() doesn't return any data */
2055-
if (!in)
2056-
return 0;
2061+
20572062
/* If not set length yet do it */
20582063
if (!cctx->len_set) {
20592064
if (CRYPTO_ccm128_setiv(ccm, ctx->iv, 15 - cctx->L, len))

0 commit comments

Comments
 (0)