From: Arun Iyengar via B4 Relay <devnull+aiyengar1990.gmail.com@kernel.org>
To: Lukas Wunner <lukas@wunner.de>, Ignat Korchagin <ignat@linux.win>,
Stefan Berger <stefanb@linux.ibm.com>,
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>,
Arun Iyengar <aiyengar1990@gmail.com>
Subject: [PATCH 2/2] crypto: ecc - Fix P-521 scalar multiplication for small scalars
Date: Sat, 14 Nov 2026 08:44:14 +0000 [thread overview]
Message-ID: <20261114-ecc-p521-small-scalars-v1-2-7b114f076b33@gmail.com> (raw)
In-Reply-To: <20261114-ecc-p521-small-scalars-v1-0-7b114f076b33@gmail.com>
From: Arun Iyengar <aiyengar1990@gmail.com>
Stefan Berger's commit 114e80437e0e ("crypto: ecc - Add special case
for NIST P521 in ecc_point_mult") made the ladder run on k + n or
k + 2n with bit 522 as the top bit.
For k < n, k + n never carries out of the nine digits, so k + 2n is
always used. Its bit 522 is set only for k >= 2^522 - 2n (about
2^259.5); for smaller k the result is (k + 2^522) * P instead of k * P.
Use k + n if its bit 521 is set, else k + 2n, and start the ladder at
bit 521.
No caller is affected today: the only P-521 caller,
ecc_is_pubkey_valid_full(), passes k = n.
Tested in UML on top of the previous patch: ecc_make_pub_key() on
P-521 now matches a Python reference for k = 2, 3, 4, 2^200 + 5, k
around 2^521 - n and 2^522 - 2n, and 40 random k. Before, every k
below 2^522 - 2n was wrong. The ecdsa-nist-p521 self-tests (k = n)
still pass.
Fixes: 114e80437e0e ("crypto: ecc - Add special case for NIST P521 in ecc_point_mult")
Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
crypto/ecc.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/crypto/ecc.c b/crypto/ecc.c
index a56a33e4eccb..370c60595fa9 100644
--- a/crypto/ecc.c
+++ b/crypto/ecc.c
@@ -1365,11 +1365,14 @@ static void ecc_point_mult(struct ecc_point *result,
carry = vli_add(sk[0], scalar, curve->n, ndigits);
vli_add(sk[1], sk[0], curve->n, ndigits);
- scalar = sk[!carry];
- if (curve->nbits == 521) /* NIST P521 */
- num_bits = curve->nbits + 2;
- else
+ if (curve->nbits == 521) { /* NIST P521 */
+ /* k + n or k + 2n, whichever has bit 521 as its top bit */
+ carry = !!vli_test_bit(sk[0], curve->nbits);
+ num_bits = curve->nbits + 1;
+ } else {
num_bits = sizeof(u64) * ndigits * 8 + 1;
+ }
+ scalar = sk[!carry];
vli_set(rx[1], point->x, ndigits);
vli_set(ry[1], point->y, ndigits);
--
2.55.0
prev parent reply other threads:[~2026-11-14 8:44 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-14 8:44 [PATCH 0/2] crypto: ecc - Fix P-521 public key generation Arun Iyengar via B4 Relay
2026-11-14 8:44 ` [PATCH 1/2] crypto: ecc - Keep the random initial Z below 2^521 on P-521 Arun Iyengar via B4 Relay
2026-11-14 8:44 ` Arun Iyengar via B4 Relay [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=20261114-ecc-p521-small-scalars-v1-2-7b114f076b33@gmail.com \
--to=devnull+aiyengar1990.gmail.com@kernel.org \
--cc=aiyengar1990@gmail.com \
--cc=davem@davemloft.net \
--cc=herbert@gondor.apana.org.au \
--cc=ignat@linux.win \
--cc=jarkko@kernel.org \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lukas@wunner.de \
--cc=pawliczekerwin@gmail.com \
--cc=stefanb@linux.ibm.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
all inboxes | Powered by JetHome®