Skip to contentSkip to conversation

crypto: ecc - Fix P-521 public key generation

PATCH New Arun Iyengar 1 participant 4 messages Nov 28

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover crypto: ecc - Fix P-521 public key generation

Two P-521 fixes for crypto/ecc.c and a KUnit test. Patch 1 keeps the random initial Z from commit cd65eaf17841 below 2^521: with 576 bits, every P-521 ecc_make_pub_key() call fails with -EAGAIN. Patch 2 fixes the scalar multiplication for scalars below about 2^259.5. Patch 3, suggested by Laveesh Bansal, tests P-521 key generation. No in-tree caller is affected yet.

This series was developed with Claude (Anthropic) as a coding assistant:
it wrote the code and the commit messages. I reviewed it and tested it as described in each patch.

---
Changes in v2:
- Add Tested-by from Laveesh Bansal to patches 1 and 2.
- Patch 2: note that k = n now runs the ladder on 2n.
- New patch 3: KUnit test for P-521 key generation.
- Link to v1: https://patch.msgid.link/[email protected]

---
Arun Iyengar (3):
crypto: ecc - Keep the random initial Z below 2^521 on P-521
crypto: ecc - Fix P-521 scalar multiplication for small scalars
crypto: ecc - Add KUnit test for P-521 public key generation

 crypto/Kconfig    | 11 +++++++
 crypto/Makefile   |  1 +
 crypto/ecc.c      | 31 +++++++++++-------
 crypto/ecc_test.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 125 insertions(+), 12 deletions(-)

---
base-commit: 9fb9278d13dca54ce7ecd3254c7df8839e59f03d
change-id: 20261114-ecc-p521-small-scalars-601dd880c8bb

Best regards,

Signature · 1 line
Arun Iyengar <[email protected]>
#1 lore raw

PATCH v2 1/3 crypto: ecc - Keep the random initial Z below 2^521 on P-521

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 <[email protected]>
---
 crypto/ecc.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)
crypto/ecc.c +12 -8
index 6696bf0d90a6..a56a33e4eccb 100644
@@ -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) {
Signature · 1 line
2.55.0
#2 lore raw

PATCH v2 2/3 crypto: ecc - Fix P-521 scalar multiplication for small scalars

In reply to Arun Iyengar

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")
---
 crypto/ecc.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
crypto/ecc.c +7 -4
index a56a33e4eccb..370c60595fa9 100644
@@ -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);
Signature · 1 line
2.55.0
#3 lore raw

PATCH v2 3/3 crypto: ecc - Add KUnit test for P-521 public key generation

In reply to Arun Iyengar

Nothing tests ecc_make_pub_key() on P-521. Add a KUnit test that compares the public keys for k = 2, 2^259 and n - 3 with known values. It fails without the previous two patches.

---
 crypto/Kconfig    | 11 +++++++
 crypto/Makefile   |  1 +
 crypto/ecc_test.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 106 insertions(+)
crypto/Kconfig +11 -0
index 6682ac091b38..a4c8632ef4f4 100644
@@ -296,6 +296,17 @@ config CRYPTO_DH_RFC7919_GROUPS
 config CRYPTO_ECC
 	tristate
 
+config CRYPTO_ECC_KUNIT_TEST
+	tristate "KUnit tests for ECC public key generation" if !KUNIT_ALL_TESTS
+	depends on KUNIT && CRYPTO_ECC
+	default KUNIT_ALL_TESTS
+	help
+	  Enable KUnit tests for ecc_make_pub_key().  The tests generate
+	  NIST P-521 public keys for small, mid-sized and large private
+	  keys and compare them with known values.
+
+	  If unsure, say N.
+
 config CRYPTO_ECDH
 	tristate "ECDH (Elliptic Curve Diffie-Hellman)"
 	select CRYPTO_ECC
crypto/Makefile +1 -0
index 42a4a0d5a466..32a3d3e7fd8e 100644
@@ -167,6 +167,7 @@ obj-$(CONFIG_CRYPTO_USER_API_RNG) += algif_rng.o
 obj-$(CONFIG_CRYPTO_USER_API_AEAD) += algif_aead.o
 obj-$(CONFIG_CRYPTO_ZSTD) += zstd.o
 obj-$(CONFIG_CRYPTO_ECC) += ecc.o
+obj-$(CONFIG_CRYPTO_ECC_KUNIT_TEST) += ecc_test.o
 obj-$(CONFIG_CRYPTO_ESSIV) += essiv.o
 
 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124949
crypto/ecc_test.c new file +94 -0

94 lines not on this page · raw message

Signature · 1 line
2.55.0