From: Vladimir Kukharenko via B4 Relay <devnull+vladimir.genproof.com@kernel.org>
To: Herbert Xu <herbert@gondor.apana.org.au>,
"David S. Miller" <davem@davemloft.net>,
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,
linux-riscv@lists.infradead.org, linux-mips@vger.kernel.org,
linux-arm-kernel@lists.infradead.org,
Paul Walmsley <pjw@kernel.org>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Alexandre Ghiti <alex@ghiti.fr>,
Vladimir Kukharenko <vladimir@genproof.com>
Subject: [PATCH 5/5] lib/crypto: chacha: Take a size_t length in chacha_crypt()
Date: Mon, 12 Oct 2026 21:31:22 +0000 [thread overview]
Message-ID: <20261012-libcrypto-4gib-lengths-v1-5-ea566c129a2e@genproof.com> (raw)
In-Reply-To: <20261012-libcrypto-4gib-lengths-v1-0-ea566c129a2e@genproof.com>
From: Vladimir Kukharenko <vladimir@genproof.com>
chacha20poly1305_encrypt(), chacha20poly1305_decrypt() and the
xchacha20poly1305 variants take size_t lengths. They pass these lengths
to chacha20_crypt(), which takes an unsigned int.
For a message of 2^32 + k bytes, chacha20_crypt() processes only the
first k bytes. In-place encryption leaves the remaining bytes as
plaintext. Until poly1305_update() took a size_t, those bytes were not
authenticated either. Commit ed20078b7e33 ("crypto: chacha20poly1305 -
import construction and selftest from Zinc") introduced this behavior.
Change chacha_crypt() and chacha20_crypt() to take size_t lengths, as
poly1305_update() and the other library functions do. The architecture
code takes an unsigned int length. Split inputs larger than UINT_MAX
into chunks of at most UINT_MAX bytes. Round each non-final chunk down
to a multiple of CHACHA_BLOCK_SIZE to keep the keystream continuous.
Lengths up to UINT_MAX make the same single call as before. The
RFC 8439 limit of 2^38 - 64 bytes per message still applies.
No in-tree caller passes messages this large, so this is an ABI
robustness improvement. I found the bug while writing a Coq/VST proof
of this code with LLM agents, and reproduced it in userspace.
Assisted-by: LLM
Signed-off-by: Vladimir Kukharenko <vladimir@genproof.com>
---
include/crypto/chacha.h | 4 ++--
lib/crypto/chacha.c | 15 ++++++++++++++-
2 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/include/crypto/chacha.h b/include/crypto/chacha.h
index 1cc301a48469..4c18e99eb4f7 100644
--- a/include/crypto/chacha.h
+++ b/include/crypto/chacha.h
@@ -86,10 +86,10 @@ static inline void chacha_init(struct chacha_state *state,
}
void chacha_crypt(struct chacha_state *state, u8 *dst, const u8 *src,
- unsigned int bytes, int nrounds);
+ size_t bytes, int nrounds);
static inline void chacha20_crypt(struct chacha_state *state,
- u8 *dst, const u8 *src, unsigned int bytes)
+ u8 *dst, const u8 *src, size_t bytes)
{
chacha_crypt(state, dst, src, bytes, 20);
}
diff --git a/lib/crypto/chacha.c b/lib/crypto/chacha.c
index 86e5d382a4e0..ffa0c75ce501 100644
--- a/lib/crypto/chacha.c
+++ b/lib/crypto/chacha.c
@@ -39,8 +39,21 @@ chacha_crypt_generic(struct chacha_state *state, u8 *dst, const u8 *src,
#endif
void chacha_crypt(struct chacha_state *state, u8 *dst, const u8 *src,
- unsigned int bytes, int nrounds)
+ size_t bytes, int nrounds)
{
+ /*
+ * chacha_crypt_arch() takes an unsigned int length. Process longer
+ * messages in chunks that are a multiple of CHACHA_BLOCK_SIZE, so that
+ * the keystream continues correctly from one chunk to the next.
+ */
+ while (bytes > UINT_MAX) {
+ unsigned int l = round_down(UINT_MAX, CHACHA_BLOCK_SIZE);
+
+ chacha_crypt_arch(state, dst, src, l, nrounds);
+ dst += l;
+ src += l;
+ bytes -= l;
+ }
chacha_crypt_arch(state, dst, src, bytes, nrounds);
}
EXPORT_SYMBOL_GPL(chacha_crypt);
--
2.43.0
next prev parent reply other threads:[~2026-10-12 21:31 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-12 21:31 [PATCH 0/5] lib/crypto: Handle large lengths in poly1305 and chacha20poly1305 Vladimir Kukharenko via B4 Relay
2026-10-12 21:31 ` [PATCH 1/5] lib/crypto: riscv/poly1305: Fix 32-bit length treated as 64-bit Vladimir Kukharenko via B4 Relay
2026-10-12 21:31 ` [PATCH 2/5] lib/crypto: mips/poly1305: " Vladimir Kukharenko via B4 Relay
2026-10-12 21:31 ` [PATCH 3/5] lib/crypto: arm64/poly1305: " Vladimir Kukharenko via B4 Relay
2026-10-12 21:31 ` [PATCH 4/5] lib/crypto: poly1305: Take a size_t length in poly1305_update() Vladimir Kukharenko via B4 Relay
2026-10-12 21:31 ` Vladimir Kukharenko via B4 Relay [this message]
2026-10-13 17:35 ` [PATCH 0/5] lib/crypto: Handle large lengths in poly1305 and chacha20poly1305 Eric Biggers
2026-10-14 15:29 ` Eric Biggers
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=20261012-libcrypto-4gib-lengths-v1-5-ea566c129a2e@genproof.com \
--to=devnull+vladimir.genproof.com@kernel.org \
--cc=Jason@zx2c4.com \
--cc=alex@ghiti.fr \
--cc=aou@eecs.berkeley.edu \
--cc=ardb@kernel.org \
--cc=davem@davemloft.net \
--cc=ebiggers@kernel.org \
--cc=herbert@gondor.apana.org.au \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mips@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=vladimir@genproof.com \
/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
all inboxes | Powered by JetHome®