* [PATCH] crypto: chacha - Consolidate XChaCha initialization
@ 2026-12-03 16:18 Laveesh Bansal
2026-12-03 16:36 ` sashiko-bot
0 siblings, 1 reply; 3+ messages in thread
From: Laveesh Bansal @ 2026-12-03 16:18 UTC (permalink / raw)
To: Herbert Xu, Eric Biggers
Cc: Jason A. Donenfeld, Ard Biesheuvel, David S. Miller, linux-crypto,
linux-kernel
XChaCha initialization is repeated in three places. Each one derives a
subkey with HChaCha from the key and the first 16 bytes of the nonce,
then builds the ChaCha state from that subkey and the last 8 bytes of
the nonce.
Add xchacha_init() to <crypto/chacha.h> and use it in all three. No
functional change.
Signed-off-by: Laveesh Bansal <laveeshb@laveeshbansal.com>
---
Notes:
1. This depends on the adiantum patches. I am sending it now so it can
be reviewed while they wait. I can resend once they are applied.
2. I tested on arm64, x86_64 and s390x (all under QEMU).
The selftests and KUnit pass. The output of chacha20,
xchacha20 and xchacha12 is the same as before.
I used an LLM for research, the initial code, tests and a review
pass. I made the final code changes and went through them myself.
crypto/adiantum.c | 25 +++----------------------
crypto/chacha.c | 30 +++++++++++-------------------
include/crypto/chacha.h | 23 +++++++++++++++++++++++
lib/crypto/chacha20poly1305.c | 21 +++++----------------
4 files changed, 42 insertions(+), 57 deletions(-)
diff --git a/crypto/adiantum.c b/crypto/adiantum.c
index 5ee65c8ae1fdb..93abfe32e913c 100644
--- a/crypto/adiantum.c
+++ b/crypto/adiantum.c
@@ -40,7 +40,6 @@
* needing to derive per-file keys. So instead we use two blocks, or 32 bytes.
*/
#define TWEAK_SIZE 32
-#define XCHACHA_NONCE_SIZE 24
struct adiantum_tfm_ctx {
struct aes_key aes;
@@ -74,24 +73,6 @@ struct nhpoly1305_ctx {
__le64 nh_hash[NH_NUM_PASSES];
};
-/* Initialize @state for XChaCha, starting at stream position 0. */
-static void adiantum_xchacha_init(struct chacha_state *state,
- const u32 key[CHACHA_KEY_WORDS], int nrounds,
- const u8 nonce[XCHACHA_NONCE_SIZE])
-{
- u32 subkey[HCHACHA_OUT_WORDS];
- u8 real_iv[CHACHA_IV_SIZE] = {};
-
- chacha_init(state, key, nonce);
- hchacha_block(state, subkey, nrounds);
-
- memcpy(&real_iv[8], nonce + 16, 8);
-
- chacha_init(state, subkey, real_iv);
-
- memzero_explicit(subkey, sizeof(subkey));
-}
-
/*
* Given the XChaCha stream key K_S, derive the block cipher key K_E and the
* hash key K_H as follows:
@@ -118,7 +99,7 @@ static int adiantum_setkey(struct crypto_skcipher *tfm,
/* Derive the subkeys */
memset(tctx->derived_keys, 0, sizeof(tctx->derived_keys));
- adiantum_xchacha_init(&state, tctx->stream_key, nrounds, nonce);
+ xchacha_init(&state, tctx->stream_key, nonce, nrounds);
chacha_crypt(&state, tctx->derived_keys, tctx->derived_keys,
sizeof(tctx->derived_keys), nrounds);
chacha_zeroize_state(&state);
@@ -488,8 +469,8 @@ static int adiantum_crypt(struct skcipher_request *req, bool enc)
if (round_up(stream_len, CHACHA_BLOCK_SIZE) <= req->cryptlen)
stream_len = round_up(stream_len, CHACHA_BLOCK_SIZE);
- adiantum_xchacha_init(&state, tctx->stream_key, tctx->nrounds,
- rbuf.bytes);
+ xchacha_init(&state, tctx->stream_key, rbuf.bytes,
+ tctx->nrounds);
adiantum_stream_xor(&state, tctx->nrounds, src, dst,
stream_len);
chacha_zeroize_state(&state);
diff --git a/crypto/chacha.c b/crypto/chacha.c
index ec16d5a33f3cd..de6c991c3c7e4 100644
--- a/crypto/chacha.c
+++ b/crypto/chacha.c
@@ -46,25 +46,21 @@ static int chacha12_setkey(struct crypto_skcipher *tfm,
}
static int chacha_stream_xor(struct skcipher_request *req,
- const struct chacha_ctx *ctx,
- const u8 iv[CHACHA_IV_SIZE])
+ struct chacha_state *state, int nrounds)
{
struct skcipher_walk walk;
- struct chacha_state state;
int err;
err = skcipher_walk_virt(&walk, req, false);
- chacha_init(&state, ctx->key, iv);
-
while (walk.nbytes > 0) {
unsigned int nbytes = walk.nbytes;
if (nbytes < walk.total)
nbytes = round_down(nbytes, CHACHA_BLOCK_SIZE);
- chacha_crypt(&state, walk.dst.virt.addr, walk.src.virt.addr,
- nbytes, ctx->nrounds);
+ chacha_crypt(state, walk.dst.virt.addr, walk.src.virt.addr,
+ nbytes, nrounds);
err = skcipher_walk_done(&walk, walk.nbytes - nbytes);
}
@@ -75,29 +71,25 @@ static int crypto_chacha_crypt(struct skcipher_request *req)
{
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
const struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
+ struct chacha_state state;
- return chacha_stream_xor(req, ctx, req->iv);
+ chacha_init(&state, ctx->key, req->iv);
+ return chacha_stream_xor(req, &state, ctx->nrounds);
}
static int crypto_xchacha_crypt(struct skcipher_request *req)
{
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
const struct chacha_ctx *ctx = crypto_skcipher_ctx(tfm);
- struct chacha_ctx subctx;
struct chacha_state state;
- u8 real_iv[16];
- /* Compute the subkey given the original key and first 128 nonce bits */
- chacha_init(&state, ctx->key, req->iv);
- hchacha_block(&state, subctx.key, ctx->nrounds);
- subctx.nrounds = ctx->nrounds;
+ xchacha_init(&state, ctx->key, req->iv, ctx->nrounds);
- /* Build the real IV */
- memcpy(&real_iv[0], req->iv + 24, 8); /* stream position */
- memcpy(&real_iv[8], req->iv + 16, 8); /* remaining 64 nonce bits */
+ /* The stream position comes after the 192-bit nonce in the IV */
+ state.x[12] = get_unaligned_le32(req->iv + 24);
+ state.x[13] = get_unaligned_le32(req->iv + 28);
- /* Generate the stream and XOR it with the data */
- return chacha_stream_xor(req, &subctx, real_iv);
+ return chacha_stream_xor(req, &state, ctx->nrounds);
}
static struct skcipher_alg algs[] = {
diff --git a/include/crypto/chacha.h b/include/crypto/chacha.h
index bacf253781d52..10071d7b03043 100644
--- a/include/crypto/chacha.h
+++ b/include/crypto/chacha.h
@@ -32,6 +32,7 @@
/* 192-bit nonce, then 64-bit stream position */
#define XCHACHA_IV_SIZE 32
+#define XCHACHA_NONCE_SIZE 24
struct chacha_state {
u32 x[CHACHA_STATE_WORDS];
@@ -85,6 +86,28 @@ static inline void chacha_init(struct chacha_state *state,
state->x[15] = get_unaligned_le32(iv + 12);
}
+/*
+ * Initialize @state for XChaCha with the given key and 192-bit nonce,
+ * starting at stream position 0.
+ */
+static inline void xchacha_init(struct chacha_state *state,
+ const u32 key[at_least CHACHA_KEY_WORDS],
+ const u8 nonce[at_least XCHACHA_NONCE_SIZE],
+ int nrounds)
+{
+ u32 subkey[HCHACHA_OUT_WORDS];
+ u8 iv[CHACHA_IV_SIZE] = {};
+
+ /* Compute the subkey given the original key and first 128 nonce bits */
+ chacha_init(state, key, nonce);
+ hchacha_block(state, subkey, nrounds);
+
+ memcpy(&iv[8], nonce + 16, 8);
+ chacha_init(state, subkey, iv);
+
+ memzero_explicit(subkey, sizeof(subkey));
+}
+
void chacha_crypt(struct chacha_state *state, u8 *dst, const u8 *src,
size_t bytes, int nrounds);
diff --git a/lib/crypto/chacha20poly1305.c b/lib/crypto/chacha20poly1305.c
index c3995ac8acb7c..20b6eadcda996 100644
--- a/lib/crypto/chacha20poly1305.c
+++ b/lib/crypto/chacha20poly1305.c
@@ -30,25 +30,14 @@ static void chacha_load_key(u32 *k, const u8 *in)
k[7] = get_unaligned_le32(in + 28);
}
-static void xchacha_init(struct chacha_state *chacha_state,
- const u8 *key, const u8 *nonce)
+static void xchacha20_init(struct chacha_state *chacha_state,
+ const u8 *key, const u8 *nonce)
{
u32 k[CHACHA_KEY_WORDS];
- u8 iv[CHACHA_IV_SIZE];
-
- memset(iv, 0, 8);
- memcpy(iv + 8, nonce + 16, 8);
chacha_load_key(k, key);
-
- /* Compute the subkey given the original key and first 128 nonce bits */
- chacha_init(chacha_state, k, nonce);
- hchacha_block(chacha_state, k, 20);
-
- chacha_init(chacha_state, k, iv);
-
+ xchacha_init(chacha_state, k, nonce, 20);
memzero_explicit(k, sizeof(k));
- memzero_explicit(iv, sizeof(iv));
}
static void
@@ -116,7 +105,7 @@ void xchacha20poly1305_encrypt(u8 *dst, const u8 *src, const size_t src_len,
{
struct chacha_state chacha_state;
- xchacha_init(&chacha_state, key, nonce);
+ xchacha20_init(&chacha_state, key, nonce);
__chacha20poly1305_encrypt(dst, src, src_len, ad, ad_len,
&chacha_state);
}
@@ -203,7 +192,7 @@ bool xchacha20poly1305_decrypt(u8 *dst, const u8 *src, const size_t src_len,
if (unlikely(src_len < POLY1305_DIGEST_SIZE))
return false;
- xchacha_init(&chacha_state, key, nonce);
+ xchacha20_init(&chacha_state, key, nonce);
return __chacha20poly1305_decrypt(dst, src, src_len, ad, ad_len,
&chacha_state);
}
base-commit: e1ec0b1e859b0f2261d64b3db95991c2da627af4
prerequisite-patch-id: 5268ec1436ad909487f950765a39a0cc6eac0c6a
prerequisite-patch-id: 0edefdce5d51065803ab84e3492634dcb9e0e53e
prerequisite-patch-id: 3b15e1e9837e10fe9a471cb5ef16e9f1d9d2f022
prerequisite-patch-id: c8aef1697a5fb33adb332f5a8fb27e6ee538969d
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] crypto: chacha - Consolidate XChaCha initialization
2026-12-03 16:18 [PATCH] crypto: chacha - Consolidate XChaCha initialization Laveesh Bansal
@ 2026-12-03 16:36 ` sashiko-bot
2026-12-03 18:33 ` Eric Biggers
0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-12-03 16:36 UTC (permalink / raw)
To: Laveesh Bansal, Herbert Xu, Eric Biggers
Cc: Jason A. Donenfeld, Ard Biesheuvel, David S. Miller, linux-crypto
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] crypto: chacha - xchacha_init() leaves stack iv buffer
unzeroized
--
commit cb60a2e5b6777695110805ae396df4d61a659318
Author: Laveesh Bansal <laveeshb@laveeshbansal.com>
crypto: chacha - Consolidate XChaCha initialization
This commit adds a shared xchacha_init() helper to include/crypto/chacha.h
and replaces the duplicated XChaCha initialization code in
crypto/adiantum.c, crypto/chacha.c, and lib/crypto/chacha20poly1305.c.
> diff --git a/include/crypto/chacha.h b/include/crypto/chacha.h
> index bacf253781d52..10071d7b03043 100644
> --- a/include/crypto/chacha.h
> +++ b/include/crypto/chacha.h
[ ... ]
> @@ -85,6 +86,28 @@ static inline void chacha_init(struct chacha_state *state,
> state->x[15] = get_unaligned_le32(iv + 12);
> }
>
> +/*
> + * Initialize @state for XChaCha with the given key and 192-bit nonce,
> + * starting at stream position 0.
> + */
> +static inline void xchacha_init(struct chacha_state *state,
> + const u32 key[at_least CHACHA_KEY_WORDS],
> + const u8 nonce[at_least XCHACHA_NONCE_SIZE],
> + int nrounds)
> +{
> + u32 subkey[HCHACHA_OUT_WORDS];
> + u8 iv[CHACHA_IV_SIZE] = {};
> +
> + /* Compute the subkey given the original key and first 128 nonce bits */
> + chacha_init(state, key, nonce);
> + hchacha_block(state, subkey, nrounds);
> +
> + memcpy(&iv[8], nonce + 16, 8);
> + chacha_init(state, subkey, iv);
> +
> + memzero_explicit(subkey, sizeof(subkey));
> +}
[Severity: Low]
Should xchacha_init() also zeroize iv with memzero_explicit() before
returning?
Previously, xchacha_init() in lib/crypto/chacha20poly1305.c cleared its
stack iv buffer with memzero_explicit(iv, sizeof(iv)). Leaving iv
uncleared here leaves the upper 8 nonce bytes on the stack on every
xchacha20poly1305_encrypt() or xchacha20poly1305_decrypt() call.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203161849.85971-1-laveeshb@laveeshbansal.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] crypto: chacha - Consolidate XChaCha initialization
2026-12-03 16:36 ` sashiko-bot
@ 2026-12-03 18:33 ` Eric Biggers
0 siblings, 0 replies; 3+ messages in thread
From: Eric Biggers @ 2026-12-03 18:33 UTC (permalink / raw)
To: sashiko-reviews
Cc: Laveesh Bansal, Herbert Xu, Jason A. Donenfeld, Ard Biesheuvel,
David S. Miller, linux-crypto
On Thu, Dec 03, 2026 at 04:36:56PM +0000, sashiko-bot@kernel.org wrote:
> > +static inline void xchacha_init(struct chacha_state *state,
> > + const u32 key[at_least CHACHA_KEY_WORDS],
> > + const u8 nonce[at_least XCHACHA_NONCE_SIZE],
> > + int nrounds)
> > +{
> > + u32 subkey[HCHACHA_OUT_WORDS];
> > + u8 iv[CHACHA_IV_SIZE] = {};
> > +
> > + /* Compute the subkey given the original key and first 128 nonce bits */
> > + chacha_init(state, key, nonce);
> > + hchacha_block(state, subkey, nrounds);
> > +
> > + memcpy(&iv[8], nonce + 16, 8);
> > + chacha_init(state, subkey, iv);
> > +
> > + memzero_explicit(subkey, sizeof(subkey));
> > +}
>
> [Severity: Low]
> Should xchacha_init() also zeroize iv with memzero_explicit() before
> returning?
>
> Previously, xchacha_init() in lib/crypto/chacha20poly1305.c cleared its
> stack iv buffer with memzero_explicit(iv, sizeof(iv)). Leaving iv
> uncleared here leaves the upper 8 nonce bytes on the stack on every
> xchacha20poly1305_encrypt() or xchacha20poly1305_decrypt() call.
We can avoid the 'iv' buffer by replacing the second chacha_init() with
the following instead:
memcpy(&state->x[4], subkey, sizeof(subkey));
state->x[12] = 0;
state->x[13] = 0;
state->x[14] = get_unaligned_le32(nonce + 16);
state->x[15] = get_unaligned_le32(nonce + 20);
state->x[0..3] still contain the constants from the first init.
- Eric
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-03 18:33 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 16:18 [PATCH] crypto: chacha - Consolidate XChaCha initialization Laveesh Bansal
2026-12-03 16:36 ` sashiko-bot
2026-12-03 18:33 ` Eric Biggers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox