From: Thomas Huth <thuth@redhat.com>
To: Eric Biggers <ebiggers@kernel.org>,
"Jason A. Donenfeld" <Jason@zx2c4.com>,
Ard Biesheuvel <ardb@kernel.org>
Cc: linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] lib/crypto: chacha20poly1305: Clear chacha_state in xchacha20poly1305_decrypt()
Date: Thu, 13 Aug 2026 15:01:47 +0200 [thread overview]
Message-ID: <20260813130147.949545-1-thuth@redhat.com> (raw)
From: Thomas Huth <thuth@redhat.com>
The chacha20poly1305_decrypt() already clears it chacha_state at the
end, so xchacha20poly1305_decrypt() should do the same, too, to avoid
leaking sensitive information on the stack. Use a __cleanup() marker
to avoid that we have to do it manually here. For symmetry, use the
__cleanup() marker in chacha20poly1305_decrypt(), too.
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
lib/crypto/chacha20poly1305.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
diff --git a/lib/crypto/chacha20poly1305.c b/lib/crypto/chacha20poly1305.c
index ea42a28f4ff7d..b40377dca8e02 100644
--- a/lib/crypto/chacha20poly1305.c
+++ b/lib/crypto/chacha20poly1305.c
@@ -172,7 +172,7 @@ bool chacha20poly1305_decrypt(u8 *dst, const u8 *src, const size_t src_len,
const u64 nonce,
const u8 key[at_least CHACHA20POLY1305_KEY_SIZE])
{
- struct chacha_state chacha_state;
+ struct chacha_state chacha_state __cleanup(chacha_zeroize_state);
u32 k[CHACHA_KEY_WORDS];
__le64 iv[2];
bool ret;
@@ -186,7 +186,6 @@ bool chacha20poly1305_decrypt(u8 *dst, const u8 *src, const size_t src_len,
ret = __chacha20poly1305_decrypt(dst, src, src_len, ad, ad_len,
&chacha_state);
- chacha_zeroize_state(&chacha_state);
memzero_explicit(iv, sizeof(iv));
memzero_explicit(k, sizeof(k));
return ret;
@@ -198,7 +197,7 @@ bool xchacha20poly1305_decrypt(u8 *dst, const u8 *src, const size_t src_len,
const u8 nonce[at_least XCHACHA20POLY1305_NONCE_SIZE],
const u8 key[at_least CHACHA20POLY1305_KEY_SIZE])
{
- struct chacha_state chacha_state;
+ struct chacha_state chacha_state __cleanup(chacha_zeroize_state);
xchacha_init(&chacha_state, key, nonce);
return __chacha20poly1305_decrypt(dst, src, src_len, ad, ad_len,
--
2.55.0
reply other threads:[~2026-08-13 13:01 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260813130147.949545-1-thuth@redhat.com \
--to=thuth@redhat.com \
--cc=Jason@zx2c4.com \
--cc=ardb@kernel.org \
--cc=ebiggers@kernel.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox