[PATCH v2 2/3] crypto: ecc - Fix P-521 scalar multiplication for small scalars
From: Arun Iyengar via B4 Relay
Date: Sat Nov 28 2026 - 12:39:00 EST
From: Arun Iyengar <aiyengar1990@xxxxxxxxx>
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 that is always k + 2n, and 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.
The only in-tree P-521 caller, ecc_is_pubkey_valid_full(), passes
k = n and gets the same result. Its ladder now runs on 2n instead of
3n, as on P-192, P-256 and P-384.
Tested in UML against a Python reference for small k, k on both sides
of 2^522 - 2n and 40 random k. The ecdsa-nist-p521 self-tests still
pass.
Fixes: 114e80437e0e ("crypto: ecc - Add special case for NIST P521 in ecc_point_mult")
Tested-by: Laveesh Bansal <laveeshb@xxxxxxxxxxxxxxxxx>
Signed-off-by: Arun Iyengar <aiyengar1990@xxxxxxxxx>
---
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