From: "Stephan Müller" <smueller@chronox.de>
To: Eric Biggers <ebiggers@kernel.org>, David Howells <dhowells@redhat.com>
Cc: dhowells@redhat.com, "Jason A. Donenfeld" <Jason@zx2c4.com>,
Ard Biesheuvel <ardb@kernel.org>,
Herbert Xu <herbert@gondor.apana.org.au>,
linux-crypto@vger.kernel.org, keyrings@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] lib/crypto: Add SHA3-224, SHA3-256, SHA3-384, SHA-512, SHAKE128, SHAKE256
Date: Fri, 19 Sep 2025 04:42:29 +0200 [thread overview]
Message-ID: <2952535.lGaqSPkdTl@graviton.chronox.de> (raw)
In-Reply-To: <3605112.1758233248@warthog.procyon.org.uk>
Am Freitag, 19. September 2025, 00:07:28 Mitteleuropäische Sommerzeit schrieb
David Howells:
Hi David,
as you mentioned that this patch as a basis for ML-DSA then may I outline the
following: the ML-DSA code requires a multi-staged squeeze operation. For
example:
squeeze(state, 10 bytes);
squeeze(state, 10 bytes);
must be identical to
squeeze(state, 20 bytes);
With this in mind, may I highlight that potentially the following code does
not support this notion:
> +/**
> + * sha3_final() - Finish computing a SHA3 message digest of any type
> + * @ctx: the context to finalize; must have been initialized
> + * @out: (output) the resulting message digest
> + *
> + * Finish the computation of a SHA3 message digest of any type and perform
> the + * "Keccak sponge squeezing" phase. The digest is written to @out
> buffer and + * the size of the digest is returned. Before returning, the
> context @ctx is + * cleared so that the caller does not need to do it.
> + */
> +int sha3_final(struct sha3_ctx *ctx, u8 *out)
> +{
> + struct sha3_state *state = &ctx->state;
> + unsigned int digest_size = ctx->digest_size;
> + unsigned int bsize = ctx->block_size;
> + u8 end_marker = 0x80;
> +
> + sha3_absorb_xorle(ctx, &ctx->padding, 1);
> + ctx->partial = bsize - 1;
> + sha3_absorb_xorle(ctx, &end_marker, 1);
> + sha3_keccakf(ctx->state.st);
This logic above should only be invoked for the first squeeze operation.
May I suggest you consider the code at:
https://github.com/smuellerDD/leancrypto/blob/master/hash/src/sha3_c.c#L625
> +
> +#ifdef __LITTLE_ENDIAN
> + for (;;) {
> + unsigned int part = umin(digest_size, bsize);
> +
> + memcpy(out, state->st, part);
> + digest_size -= part;
> + if (!digest_size)
> + goto done;
> + out += part;
> + sha3_keccakf(ctx->state.st);
> + }
This loop needs to honor a starting offset in case the previous call only
requested a subset of the rate.
May I suggest to consider the code at:
https://github.com/smuellerDD/leancrypto/blob/master/hash/src/sha3_c.c#L643
> +#else
> + __le64 *digest = (__le64 *)out, *s;
> +
> + while (digest_size >= bsize) {
> + for (int i = 0; i < bsize / 8; i++)
> + put_unaligned_le64(state->st[i], digest++);
> + digest_size -= bsize;
> + if (!digest_size)
> + goto done;
> + sha3_keccakf(ctx->state.st);
> + }
> +
> + s = state->st;
> + for (; digest_size >= 8; digest_size -= 8)
> + put_unaligned_le64(*s++, digest++);
> +
> + u8 *sc = (u8 *)s;
> + u8 *dc = (u8 *)digest;
> +
> + for (; digest_size >= 1; digest_size -= 1)
> + *dc++ = *sc++;
> +#endif
> +done:
> + digest_size = ctx->digest_size;
> + memzero_explicit(ctx, sizeof(*ctx));
For a multi-stage squeeze, it is perhaps not helpful to zeroize the context
here.
Ciao
Stephan
next prev parent reply other threads:[~2025-09-19 2:45 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-18 22:07 [PATCH] lib/crypto: Add SHA3-224, SHA3-256, SHA3-384, SHA-512, SHAKE128, SHAKE256 David Howells
2025-09-19 2:42 ` Stephan Müller [this message]
2025-09-19 6:17 ` David Howells
2025-09-19 13:59 ` Simo Sorce
2025-09-19 15:34 ` David Howells
2025-09-19 15:40 ` Stephan Müller
2025-09-19 10:10 ` kernel test robot
2025-09-19 10:20 ` kernel test robot
2025-09-19 11:33 ` kernel test robot
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=2952535.lGaqSPkdTl@graviton.chronox.de \
--to=smueller@chronox.de \
--cc=Jason@zx2c4.com \
--cc=ardb@kernel.org \
--cc=dhowells@redhat.com \
--cc=ebiggers@kernel.org \
--cc=herbert@gondor.apana.org.au \
--cc=keyrings@vger.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