All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: sashiko-reviews@lists.linux.dev
Cc: Laveesh Bansal <laveeshb@laveeshbansal.com>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	"Jason A. Donenfeld" <Jason@zx2c4.com>,
	Ard Biesheuvel <ardb@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	linux-crypto@vger.kernel.org
Subject: Re: [PATCH] crypto: chacha - Consolidate XChaCha initialization
Date: Thu, 3 Dec 2026 10:33:40 -0800	[thread overview]
Message-ID: <20261203183340.GB2267@sol> (raw)
In-Reply-To: <sashiko-outbox-248206@kernel.org>

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

      reply	other threads:[~2026-12-03 18:33 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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=20261203183340.GB2267@sol \
    --to=ebiggers@kernel.org \
    --cc=Jason@zx2c4.com \
    --cc=ardb@kernel.org \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=laveeshb@laveeshbansal.com \
    --cc=linux-crypto@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.