linux-crypto.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Biggers <ebiggers@kernel.org>
To: David Howells <dhowells@redhat.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
	Luis Chamberlain <mcgrof@kernel.org>,
	Petr Pavlu <petr.pavlu@suse.com>,
	Daniel Gomez <da.gomez@kernel.org>,
	Sami Tolvanen <samitolvanen@google.com>,
	"Jason A . Donenfeld" <Jason@zx2c4.com>,
	Ard Biesheuvel <ardb@kernel.org>,
	Stephan Mueller <smueller@chronox.de>,
	Lukas Wunner <lukas@wunner.de>,
	Ignat Korchagin <ignat@cloudflare.com>,
	linux-crypto@vger.kernel.org, keyrings@vger.kernel.org,
	linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support
Date: Mon, 24 Nov 2025 20:10:50 -0800	[thread overview]
Message-ID: <20251125041050.GA1608@sol> (raw)
In-Reply-To: <20251121013716.GE3532564@google.com>

On Fri, Nov 21, 2025 at 01:37:16AM +0000, Eric Biggers wrote:
> On Mon, Nov 17, 2025 at 02:55:51PM +0000, David Howells wrote:
> > +/*
> > + * @brief poly_uniform - Sample polynomial with uniformly random coefficients
> > + *			 in [0,Q-1] by performing rejection sampling on the
> > + *			 output stream of SHAKE128(seed|nonce).
> > + *
> > + * @param [out] a pointer to output polynomial
> > + * @param [in] seed byte array with seed of length DILITHIUM_SEEDBYTES
> > + * @param [in] nonce 2-byte nonce
> > + */
> > +void poly_uniform(poly *a, const uint8_t seed[DILITHIUM_SEEDBYTES],
> > +		  __le16 nonce, void *ws_buf)
> > +{
> > +	struct shake_ctx hash_ctx;
> > +	unsigned int i, ctr, off;
> > +	unsigned int buflen = POLY_UNIFORM_NBLOCKS * SHAKE128_BLOCK_SIZE;
> > +	uint8_t *buf = ws_buf;
> > +
> > +	shake128_init(&hash_ctx);
> > +	shake_update(&hash_ctx, seed, DILITHIUM_SEEDBYTES);
> > +	shake_update(&hash_ctx, (uint8_t *)&nonce, sizeof(nonce));
> > +	shake_squeeze(&hash_ctx, buf, buflen);
> > +
> > +	ctr = rej_uniform(a->coeffs, DILITHIUM_N, buf, buflen);
> > +
> > +	while (ctr < DILITHIUM_N) {
> > +		off = buflen % 3;
> > +		for (i = 0; i < off; ++i)
> > +			buf[i] = buf[buflen - off + i];
> > +
> > +		shake_squeeze(&hash_ctx, buf + off, SHAKE128_BLOCK_SIZE);
> > +		buflen = DILITHIUM_SEEDBYTES + off;
> > +		ctr += rej_uniform(a->coeffs + ctr, DILITHIUM_N - ctr, buf,
> > +				   buflen);
> > +	}
> > +
> > +	shake_zeroize_ctx(&hash_ctx);
> > +}
> 
> By the way, the above has a bug.  In the second and later squeezes, it
> squeezes SHAKE128_BLOCK_SIZE (168) bytes, but then it uses only the
> first DILITHIUM_SEEDBYTES (32) bytes.
> 
> Now, that 32 is on top of the 840-byte first squeeze, so there are 872
> correct bytes which is enough for 290 samples.  So an incorrect matrix
> would be generated only if more than 290 samples happen to be required
> to get the 256 coefficients.  q / 2^23 = ~99.9% of coefficients are
> accepted, so that number of rejections would be pretty unlikely.
> 
> Still, it's a bug.  Anyway, we're not going to use this code (we'll use
> my code that does this correctly and in a simpler way), but I thought
> I'd point it out so that Stephan can fix it.  This seems to be a
> "leancrypto" specific bug.
> 
> Note: this feedback should not be taken as implying that I've reviewed
> the entire 4800 lines of code.  I just happened to notice this.

No reply from Stephan yet, so to make sure this doesn't get missed I
also opened an issue at
https://github.com/smuellerDD/leancrypto/issues/42

- Eric

  reply	other threads:[~2025-11-25  4:12 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-17 14:55 [PATCH v9 0/9] lib/crypto: Add ML-DSA signing David Howells
2025-11-17 14:55 ` [PATCH v9 1/9] crypto: Add support for shake256 through crypto_shash David Howells
2025-11-17 16:50   ` Eric Biggers
2025-11-17 14:55 ` [PATCH v9 2/9] crypto: Add ML-DSA/Dilithium verify support David Howells
2025-11-17 17:10   ` Eric Biggers
2025-11-25 10:10     ` Ignat Korchagin
2025-11-25 20:24       ` Eric Biggers
2025-11-25 20:51         ` Ignat Korchagin
2025-11-17 19:52   ` David Howells
2025-11-17 20:12     ` Eric Biggers
2025-11-19  3:59       ` Eric Biggers
2025-11-19 14:20       ` David Howells
2025-11-17 20:19     ` James Bottomley
2025-11-18  8:39     ` David Howells
2025-11-18 12:59       ` James Bottomley
2025-11-17 20:05   ` David Howells
2025-11-21  1:37   ` Eric Biggers
2025-11-25  4:10     ` Eric Biggers [this message]
2025-11-25  8:32       ` Stephan Müller
2025-11-17 14:55 ` [PATCH v9 3/9] mldsa: Add a simpler API David Howells
2025-11-17 14:55 ` [PATCH v9 4/9] crypto: Add ML-DSA-44 pure rejection test vectors as a kunit test David Howells
2025-11-17 14:55 ` [PATCH v9 5/9] crypto: Add ML-DSA-65 " David Howells
2025-11-17 14:55 ` [PATCH v9 6/9] crypto: Add ML-DSA-87 " David Howells
2025-11-17 14:55 ` [PATCH v9 7/9] pkcs7: Allow the signing algo to calculate the digest itself David Howells
2025-11-17 14:55 ` [PATCH v9 8/9] pkcs7, x509: Add ML-DSA support David Howells
2025-11-17 14:55 ` [PATCH v9 9/9] modsign: Enable ML-DSA module signing David Howells
2025-11-17 15:22 ` Pick up keys-pqc branch for linux-next? David Howells
2025-11-17 17:11   ` Eric Biggers
2025-11-20  9:53     ` Stephen Rothwell
2025-11-21  2:48       ` Eric Biggers
2025-11-17 16:01 ` Where to add FIPS tests David Howells
2025-11-17 16:54   ` 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=20251125041050.GA1608@sol \
    --to=ebiggers@kernel.org \
    --cc=Jason@zx2c4.com \
    --cc=ardb@kernel.org \
    --cc=da.gomez@kernel.org \
    --cc=dhowells@redhat.com \
    --cc=herbert@gondor.apana.org.au \
    --cc=ignat@cloudflare.com \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=lukas@wunner.de \
    --cc=mcgrof@kernel.org \
    --cc=petr.pavlu@suse.com \
    --cc=samitolvanen@google.com \
    --cc=smueller@chronox.de \
    /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;
as well as URLs for NNTP newsgroup(s).