From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.182.15 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796348623; b=0juEkH50PahmmcKAEELxo6cZ83OWkl93Rwzo8wdTYMHPpoG81WPxkpYG+Yxx8rJtivsL 3MKTDimDq+jCb0GRJJQMd7tO87MReSio751LloLO2ECxYtJ2ZyBgHqrXSHRdVBENIH1VJ 3ATu6//I7t/fvbzKwuUdjdDIM4HPEKf+yZ688vJtjuZdBo/3Ukf/Q/dh4/FacnOWpWeqL bmt3mDk7QeK+419bbm3skLoL8fD7P1364XHvJbQHv7CZ5bdXSZkKUoy1ERZgcyyvOBgNH JHkG55IQVTj/WxLNu+shBPr8in9u+0oLa06Zlc6Gd1numVmGeKmG0su+0PFrA/rIm4w== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796348623; h=DMARC-Filter:DKIM-Signature:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:MIME-Version:Content-Transfer-Encoding: X-ZohoMailClient; bh=AbK2vyYHTiZgh0saIUn17WGctHy78hxx/JEbFD432CE=; b=Z6kBLt9h3metZUhlofece79AXj1g1xitZclId5iW/DzqhPtF5TlT5fT0H0Y61NUh/sDz knaSCNE5OV2rVwd/5IZC3NiAeoUnKDSQIuYcmmgg2sEvapgHwXWfK+2Ye48as1eHYCzJn VZG6+TX8F9JwbzCVamkH5AnKNeCbHM3s1ucqn431t/i0ozNJm+de1HWs/ydDv/wLRIson D6YMnsyLXn9xvAfuEtW7T5Z/IkyRGxvrnGhgIb4bT1STU8UN5wDSmWM2xG0l+0tuP8NcL DUI1DWrtx/2VNRmstRyhG1CzVYG4CBPhVGyAWQZE/y3zIieygGpwRyZOLRINZ2N3Vbg== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=none header.from=laveeshbansal.com; spf=pass smtp.mailfrom=laveeshbansal.com; dkim=pass header.d=laveeshbansal.com header.i=@laveeshbansal.com header.a=rsa-sha256 header.s=zoho header.b=NbLsTyOC; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.182.15 Received: from sender5-op-o15.zoho.com (sender5-op-o15.zoho.com [165.173.182.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA537606A322; Fri, 4 Dec 2026 01:43:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AA537606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=laveeshbansal.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=laveeshbansal.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=laveeshbansal.com header.i=@laveeshbansal.com header.a=rsa-sha256 header.s=zoho header.b=NbLsTyOC ARC-Seal: i=1; a=rsa-sha256; t=1796348613; cv=none; d=zohomail.com; s=zohoarc; b=GEh0vBTWSjc6uUzCJOz8LV5vSwk6OJBFSxSsdZpv0qGSovvshFTrjgj9wMjnp+CXTfIZ1TU0MV9fK6WpdkFvHCH7rxlkwhRGtz9qnu6fDXpJKrF2ByVQcArZuv3I5gmH/GpJPsnrL1Og4qSjberTcLN/ReEiIeToizcIj9CtaUs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1796348613; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=AbK2vyYHTiZgh0saIUn17WGctHy78hxx/JEbFD432CE=; b=oJG0KppojZKMITB3/8nS9iHRtl+IsAyGuTDCWMwxTwgtZO1/4v/n82DGCAHEhR3SsWqSC6ym5nFlKcLQHlRzxej3YA9bSCqr0hZqdJPFl//sXxl5nBkF48Gxvyi9IV+j7SnSnG3fdk1yDni2BkRlzvtyKC+5Pf+INub0BK0yMyY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=laveeshbansal.com; spf=pass smtp.mailfrom=laveeshb@laveeshbansal.com; dmarc=pass header.from= DKIM-Signature: a=rsa-sha256; b=NbLsTyOC+i4Oi2Q+OBOIwuYde/Euefx+WFS/Iqq6XLhGqxg5jJta99lXY9bntnVV2/AZgpZG1mtgwU1l5ah/OJc1ZSDricLdd3jaaGJnK181Kgmhdk1PwKl9EU2bGYQQmjFJwyOKaNhDzwFueLRoaBwpfSbcpnZtWsQShHwynjY=; c=relaxed/relaxed; s=zoho; d=laveeshbansal.com; v=1; bh=AbK2vyYHTiZgh0saIUn17WGctHy78hxx/JEbFD432CE=; h=Content-Description:Content-ID:Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Resent-Sender:Resent-Cc:Resent-Date:Resent-To:Reply-To:Resent-Message-ID:Resent-From:Sender:Subject:To:Date:From:Message-Id:Reply-To:Subject:To:Cc; Received: by smtp.zohomail.com with SMTPS id 17963486120181013.684359004431; Thu, 3 Dec 2026 17:43:32 -0800 (PST) From: Laveesh Bansal To: Herbert Xu , Eric Biggers Cc: "Jason A. Donenfeld" , Ard Biesheuvel , "David S. Miller" , linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] crypto: chacha - Consolidate XChaCha initialization Date: Thu, 3 Dec 2026 17:43:28 -0800 Message-ID: <20261204014328.89592-1-laveeshb@laveeshbansal.com> X-Mailer: git-send-email 2.54.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-ZohoMailClient: External 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 and use it in all three. No functional change. Signed-off-by: Laveesh Bansal --- Notes: Changes in v2: Addressed v1 feedback: https://lore.kernel.org/all/20261203183340.GB2267@sol/ I set the state words directly in xchacha_init() instead of going through a second chacha_init(). This drops the 'iv' buffer. 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 | 25 +++++++++++++++++++++++++ lib/crypto/chacha20poly1305.c | 21 +++++---------------- 4 files changed, 44 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..80b0c337b99e7 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,30 @@ 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]; + + /* Compute the subkey given the original key and first 128 nonce bits */ + chacha_init(state, key, nonce); + hchacha_block(state, subkey, nrounds); + + 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); + + 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