Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Stefan Berger <stefanb@linux.ibm.com>
To: aiyengar1990@gmail.com, Lukas Wunner <lukas@wunner.de>,
	Ignat Korchagin <ignat@linux.win>,
	Herbert Xu <herbert@gondor.apana.org.au>,
	"David S. Miller" <davem@davemloft.net>,
	Jarkko Sakkinen <jarkko@kernel.org>
Cc: linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org,
	Erwin Pawliczek <pawliczekerwin@gmail.com>,
	Laveesh Bansal <laveeshb@laveeshbansal.com>
Subject: Re: [PATCH v2 1/3] crypto: ecc - Keep the random initial Z below 2^521 on P-521
Date: Thu, 3 Dec 2026 11:47:49 -0500	[thread overview]
Message-ID: <3bd39cbf-7c3f-47c5-9e3e-27cfd24ae33b@linux.ibm.com> (raw)
In-Reply-To: <20261128-ecc-p521-small-scalars-v2-1-0648f492bca4@gmail.com>



On 11/28/26 12:38 PM, Arun Iyengar via B4 Relay wrote:
> From: Arun Iyengar <aiyengar1990@gmail.com>
> 
> Erwin Pawliczek's commit cd65eaf17841 ("crypto: ecc - Randomize the
> initial Z in ecc_make_pub_key()") takes the initial Z from ndigits * 8
> random bytes, as crypto_ecdh_shared_secret() already did. On P-521 that
> is a 576-bit number, but vli_mmod_fast_521() only reduces products of
> numbers below 2^521. With a 576-bit Z, every P-521 ecc_make_pub_key()
> call returns -EAGAIN. There is no in-tree P-521 caller yet.
> 
> Get Z from one helper at both sites that keeps the random bits to the
> curve's bit length. The other curves are unchanged.
> 
> Tested in UML: P-521 ecc_make_pub_key() now matches a Python reference
> for the tested keys from 2^522 - 2n up to n - 3. Smaller keys are fixed
> by the next patch. The ecdsa-nist-p521 self-tests still pass.
> 
> Fixes: cd65eaf17841 ("crypto: ecc - Randomize the initial Z in ecc_make_pub_key()")
> Cc: Erwin Pawliczek <pawliczekerwin@gmail.com>
> Tested-by: Laveesh Bansal <laveeshb@laveeshbansal.com>
> Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>

Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>

> ---
>   crypto/ecc.c | 20 ++++++++++++--------
>   1 file changed, 12 insertions(+), 8 deletions(-)
> 
> diff --git a/crypto/ecc.c b/crypto/ecc.c
> index 6696bf0d90a6..a56a33e4eccb 100644
> --- a/crypto/ecc.c
> +++ b/crypto/ecc.c
> @@ -1609,13 +1609,22 @@ int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits,
>   }
>   EXPORT_SYMBOL(ecc_gen_privkey);
>   
> +/* Random initial Z for ecc_point_mult(), below 2^nbits */
> +static void ecc_rand_z(const struct ecc_curve *curve, u64 *z,
> +		       unsigned int ndigits)
> +{
> +	get_random_bytes(z, ndigits << ECC_DIGITS_TO_BYTES_SHIFT);
> +	/* P-521: the fast reduction only takes products of 521-bit numbers */
> +	if (curve->nbits % 64)
> +		z[ndigits - 1] &= GENMASK_ULL(curve->nbits % 64 - 1, 0);
> +}
> +
>   int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
>   		     const u64 *private_key, u64 *public_key)
>   {
>   	int ret = 0;
>   	struct ecc_point *pk;
>   	u64 rand_z[ECC_MAX_DIGITS];
> -	unsigned int nbytes;
>   	const struct ecc_curve *curve = ecc_get_curve(curve_id);
>   
>   	if (!private_key || ndigits > ARRAY_SIZE(rand_z)) {
> @@ -1623,9 +1632,7 @@ int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
>   		goto out;
>   	}
>   
> -	nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
> -
> -	get_random_bytes(rand_z, nbytes);
> +	ecc_rand_z(curve, rand_z, ndigits);
>   
>   	pk = ecc_alloc_point(ndigits);
>   	if (!pk) {
> @@ -1719,7 +1726,6 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
>   	int ret = 0;
>   	struct ecc_point *product, *pk;
>   	u64 rand_z[ECC_MAX_DIGITS];
> -	unsigned int nbytes;
>   	const struct ecc_curve *curve = ecc_get_curve(curve_id);
>   
>   	if (!private_key || !public_key || ndigits > ARRAY_SIZE(rand_z)) {
> @@ -1727,9 +1733,7 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
>   		goto out;
>   	}
>   
> -	nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
> -
> -	get_random_bytes(rand_z, nbytes);
> +	ecc_rand_z(curve, rand_z, ndigits);
>   
>   	pk = ecc_alloc_point(ndigits);
>   	if (!pk) {
> 


  reply	other threads:[~2026-12-03 16:48 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-28 17:38 [PATCH v2 0/3] crypto: ecc - Fix P-521 public key generation Arun Iyengar via B4 Relay
2026-11-28 17:38 ` [PATCH v2 1/3] crypto: ecc - Keep the random initial Z below 2^521 on P-521 Arun Iyengar via B4 Relay
2026-12-03 16:47   ` Stefan Berger [this message]
2026-11-28 17:38 ` [PATCH v2 2/3] crypto: ecc - Fix P-521 scalar multiplication for small scalars Arun Iyengar via B4 Relay
2026-12-03 16:52   ` Stefan Berger
2026-11-28 17:38 ` [PATCH v2 3/3] crypto: ecc - Add KUnit test for P-521 public key generation Arun Iyengar via B4 Relay
2026-12-03 16:56   ` Stefan Berger

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=3bd39cbf-7c3f-47c5-9e3e-27cfd24ae33b@linux.ibm.com \
    --to=stefanb@linux.ibm.com \
    --cc=aiyengar1990@gmail.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=ignat@linux.win \
    --cc=jarkko@kernel.org \
    --cc=laveeshb@laveeshbansal.com \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukas@wunner.de \
    --cc=pawliczekerwin@gmail.com \
    /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