* [PATCH v2 0/3] crypto: ecc - Fix P-521 public key generation
@ 2026-11-28 17:38 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
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Arun Iyengar via B4 Relay @ 2026-11-28 17:38 UTC (permalink / raw)
To: Lukas Wunner, Ignat Korchagin, Stefan Berger, Herbert Xu,
David S. Miller, Jarkko Sakkinen
Cc: linux-crypto, linux-kernel, Erwin Pawliczek, Laveesh Bansal,
Arun Iyengar
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/20261114-ecc-p521-small-scalars-v1-0-7b114f076b33@gmail.com
---
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,
--
Arun Iyengar <aiyengar1990@gmail.com>
^ permalink raw reply [flat|nested] 7+ messages in thread* [PATCH v2 1/3] crypto: ecc - Keep the random initial Z below 2^521 on P-521 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 ` Arun Iyengar via B4 Relay 2026-12-03 16:47 ` Stefan Berger 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-11-28 17:38 ` [PATCH v2 3/3] crypto: ecc - Add KUnit test for P-521 public key generation Arun Iyengar via B4 Relay 2 siblings, 1 reply; 7+ messages in thread From: Arun Iyengar via B4 Relay @ 2026-11-28 17:38 UTC (permalink / raw) To: Lukas Wunner, Ignat Korchagin, Stefan Berger, Herbert Xu, David S. Miller, Jarkko Sakkinen Cc: linux-crypto, linux-kernel, Erwin Pawliczek, Laveesh Bansal, Arun Iyengar 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> --- 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) { -- 2.55.0 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 1/3] crypto: ecc - Keep the random initial Z below 2^521 on P-521 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 0 siblings, 0 replies; 7+ messages in thread From: Stefan Berger @ 2026-12-03 16:47 UTC (permalink / raw) To: aiyengar1990, Lukas Wunner, Ignat Korchagin, Herbert Xu, David S. Miller, Jarkko Sakkinen Cc: linux-crypto, linux-kernel, Erwin Pawliczek, Laveesh Bansal 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) { > ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 2/3] crypto: ecc - Fix P-521 scalar multiplication for small scalars 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-11-28 17:38 ` 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 2 siblings, 1 reply; 7+ messages in thread From: Arun Iyengar via B4 Relay @ 2026-11-28 17:38 UTC (permalink / raw) To: Lukas Wunner, Ignat Korchagin, Stefan Berger, Herbert Xu, David S. Miller, Jarkko Sakkinen Cc: linux-crypto, linux-kernel, Erwin Pawliczek, Laveesh Bansal, Arun Iyengar 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 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@laveeshbansal.com> 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 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 2/3] crypto: ecc - Fix P-521 scalar multiplication for small scalars 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 0 siblings, 0 replies; 7+ messages in thread From: Stefan Berger @ 2026-12-03 16:52 UTC (permalink / raw) To: aiyengar1990, Lukas Wunner, Ignat Korchagin, Herbert Xu, David S. Miller, Jarkko Sakkinen Cc: linux-crypto, linux-kernel, Erwin Pawliczek, Laveesh Bansal On 11/28/26 12:38 PM, Arun Iyengar via B4 Relay wrote: > 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 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@laveeshbansal.com> > Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com> Tested-by: Stefan Berger <stefanb@linux.ibm.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); > ^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 3/3] crypto: ecc - Add KUnit test for P-521 public key generation 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-11-28 17:38 ` [PATCH v2 2/3] crypto: ecc - Fix P-521 scalar multiplication for small scalars Arun Iyengar via B4 Relay @ 2026-11-28 17:38 ` Arun Iyengar via B4 Relay 2026-12-03 16:56 ` Stefan Berger 2 siblings, 1 reply; 7+ messages in thread From: Arun Iyengar via B4 Relay @ 2026-11-28 17:38 UTC (permalink / raw) To: Lukas Wunner, Ignat Korchagin, Stefan Berger, Herbert Xu, David S. Miller, Jarkko Sakkinen Cc: linux-crypto, linux-kernel, Erwin Pawliczek, Laveesh Bansal, Arun Iyengar From: Arun Iyengar <aiyengar1990@gmail.com> 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. Suggested-by: Laveesh Bansal <laveeshb@laveeshbansal.com> Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com> --- crypto/Kconfig | 11 +++++++ crypto/Makefile | 1 + crypto/ecc_test.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) diff --git a/crypto/Kconfig b/crypto/Kconfig index 6682ac091b38..a4c8632ef4f4 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig @@ -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 diff --git a/crypto/Makefile b/crypto/Makefile index 42a4a0d5a466..32a3d3e7fd8e 100644 --- a/crypto/Makefile +++ b/crypto/Makefile @@ -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 diff --git a/crypto/ecc_test.c b/crypto/ecc_test.c new file mode 100644 index 000000000000..cf6522b7eea4 --- /dev/null +++ b/crypto/ecc_test.c @@ -0,0 +1,94 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for ecc_make_pub_key() on NIST P-521. + * + * The expected points were computed with a Python reference and checked + * against OpenSSL. + */ +#include <kunit/test.h> +#include <linux/module.h> +#include <crypto/ecdh.h> +#include <crypto/internal/ecc.h> + +#define P521_DIGITS ECC_CURVE_NIST_P521_DIGITS + +struct ecc_p521_vec { + const char *desc; + u64 k[P521_DIGITS]; + u64 x[P521_DIGITS]; + u64 y[P521_DIGITS]; +}; + +static const struct ecc_p521_vec ecc_p521_vecs[] = { + { + .desc = "k = 2", + .k = { 2 }, + .x = { 0xf43e3933ba6d783dull, 0xcf2fa364d60fd967ull, + 0xaa104a3a35c5af41ull, 0xb3b204da6ef55507ull, + 0x2c6e5505d769be97ull, 0x7403279b1ccc0635ull, + 0x2fcb288148c28274ull, 0x3c219024277e7e68ull, 0x43ull }, + .y = { 0x1be356d661f41b02ull, 0xeafcbe95edc0f4f7ull, + 0x93937fa99a3248f4ull, 0xb3e377de9f251f6bull, + 0xab21a29906c42dbbull, 0xc6b5107c4da97740ull, + 0xa7f3eceeeed3f0b5ull, 0xbb8cc7f86db26700ull, 0xf4ull }, + }, + { + .desc = "k = 2^259", + .k = { [4] = 0x8ull }, + .x = { 0xdfbfa8c0b9f59031ull, 0x683df70ba5e17023ull, + 0x07958fdc42ce28b7ull, 0xb8965b5bc6d4dc78ull, + 0x78441b9a6e4b4290ull, 0x3909e659ea4b4aa1ull, + 0x9c8f59c629b6cd62ull, 0xf400a01919a026ccull, 0xcfull }, + .y = { 0x09844edef77fa34full, 0x9da8cdc495b4acfaull, + 0x5bb762074d52835bull, 0xc39505e29c0584e1ull, + 0x4c9a0f296c4f318eull, 0xdd1a8f417d87f206ull, + 0x423805cd95ec312cull, 0x21639f519304343eull, 0x92ull }, + }, + { + .desc = "k = n - 3", + .k = { 0xbb6fb71e91386406ull, 0x3bb5c9b8899c47aeull, + 0x7fcc0148f709a5d0ull, 0x51868783bf2f966bull, + 0xfffffffffffffffaull, 0xffffffffffffffffull, + 0xffffffffffffffffull, 0xffffffffffffffffull, + 0x1ffull }, + .x = { 0xa5919d2ede37ad7dull, 0xaeb490862c32ea05ull, + 0x1da6bd16b59fe21bull, 0xad3f164a3a483205ull, + 0xe5ad7a112d7a8dd1ull, 0xb52a6e5b123d9ab9ull, + 0xd91d6a64b5959479ull, 0x3d352443de29195dull, + 0x1a7ull }, + .y = { 0xa0a7735e11793f1aull, 0x0efa36436c5a6fbdull, + 0xd2a5312e213c38f3ull, 0xd1d22b30723a8a4full, + 0x2d0754e05caa3113ull, 0x0eaa8057d562fce8ull, + 0x686079393547eb0dull, 0x64fc4682059d2226ull, 0xc1ull }, + }, +}; + +KUNIT_ARRAY_PARAM_DESC(ecc_p521, ecc_p521_vecs, desc); + +static void ecc_p521_make_pub_key(struct kunit *test) +{ + const struct ecc_p521_vec *v = test->param_value; + u64 pub[2 * P521_DIGITS], x[P521_DIGITS], y[P521_DIGITS]; + + KUNIT_ASSERT_EQ(test, ecc_make_pub_key(ECC_CURVE_NIST_P521, P521_DIGITS, + v->k, pub), 0); + ecc_swap_digits(pub, x, P521_DIGITS); + ecc_swap_digits(&pub[P521_DIGITS], y, P521_DIGITS); + KUNIT_EXPECT_MEMEQ(test, x, v->x, sizeof(x)); + KUNIT_EXPECT_MEMEQ(test, y, v->y, sizeof(y)); +} + +static struct kunit_case ecc_test_cases[] = { + KUNIT_CASE_PARAM(ecc_p521_make_pub_key, ecc_p521_gen_params), + {} +}; + +static struct kunit_suite ecc_test_suite = { + .name = "ecc", + .test_cases = ecc_test_cases, +}; + +kunit_test_suite(ecc_test_suite); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("KUnit tests for ECC public key generation"); -- 2.55.0 ^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 3/3] crypto: ecc - Add KUnit test for P-521 public key generation 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 0 siblings, 0 replies; 7+ messages in thread From: Stefan Berger @ 2026-12-03 16:56 UTC (permalink / raw) To: aiyengar1990, Lukas Wunner, Ignat Korchagin, Herbert Xu, David S. Miller, Jarkko Sakkinen Cc: linux-crypto, linux-kernel, Erwin Pawliczek, Laveesh Bansal On 11/28/26 12:38 PM, Arun Iyengar via B4 Relay wrote: > From: Arun Iyengar <aiyengar1990@gmail.com> > > 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. > > Suggested-by: Laveesh Bansal <laveeshb@laveeshbansal.com> > Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com> Tested-by: Stefan Berger <stefanb@linux.ibm.com> > --- > crypto/Kconfig | 11 +++++++ > crypto/Makefile | 1 + > crypto/ecc_test.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 106 insertions(+) > > diff --git a/crypto/Kconfig b/crypto/Kconfig > index 6682ac091b38..a4c8632ef4f4 100644 > --- a/crypto/Kconfig > +++ b/crypto/Kconfig > @@ -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 > diff --git a/crypto/Makefile b/crypto/Makefile > index 42a4a0d5a466..32a3d3e7fd8e 100644 > --- a/crypto/Makefile > +++ b/crypto/Makefile > @@ -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 > diff --git a/crypto/ecc_test.c b/crypto/ecc_test.c > new file mode 100644 > index 000000000000..cf6522b7eea4 > --- /dev/null > +++ b/crypto/ecc_test.c > @@ -0,0 +1,94 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * KUnit tests for ecc_make_pub_key() on NIST P-521. > + * > + * The expected points were computed with a Python reference and checked > + * against OpenSSL. > + */ > +#include <kunit/test.h> > +#include <linux/module.h> > +#include <crypto/ecdh.h> > +#include <crypto/internal/ecc.h> > + > +#define P521_DIGITS ECC_CURVE_NIST_P521_DIGITS > + > +struct ecc_p521_vec { > + const char *desc; > + u64 k[P521_DIGITS]; > + u64 x[P521_DIGITS]; > + u64 y[P521_DIGITS]; > +}; > + > +static const struct ecc_p521_vec ecc_p521_vecs[] = { > + { > + .desc = "k = 2", > + .k = { 2 }, > + .x = { 0xf43e3933ba6d783dull, 0xcf2fa364d60fd967ull, > + 0xaa104a3a35c5af41ull, 0xb3b204da6ef55507ull, > + 0x2c6e5505d769be97ull, 0x7403279b1ccc0635ull, > + 0x2fcb288148c28274ull, 0x3c219024277e7e68ull, 0x43ull }, > + .y = { 0x1be356d661f41b02ull, 0xeafcbe95edc0f4f7ull, > + 0x93937fa99a3248f4ull, 0xb3e377de9f251f6bull, > + 0xab21a29906c42dbbull, 0xc6b5107c4da97740ull, > + 0xa7f3eceeeed3f0b5ull, 0xbb8cc7f86db26700ull, 0xf4ull }, > + }, > + { > + .desc = "k = 2^259", > + .k = { [4] = 0x8ull }, > + .x = { 0xdfbfa8c0b9f59031ull, 0x683df70ba5e17023ull, > + 0x07958fdc42ce28b7ull, 0xb8965b5bc6d4dc78ull, > + 0x78441b9a6e4b4290ull, 0x3909e659ea4b4aa1ull, > + 0x9c8f59c629b6cd62ull, 0xf400a01919a026ccull, 0xcfull }, > + .y = { 0x09844edef77fa34full, 0x9da8cdc495b4acfaull, > + 0x5bb762074d52835bull, 0xc39505e29c0584e1ull, > + 0x4c9a0f296c4f318eull, 0xdd1a8f417d87f206ull, > + 0x423805cd95ec312cull, 0x21639f519304343eull, 0x92ull }, > + }, > + { > + .desc = "k = n - 3", > + .k = { 0xbb6fb71e91386406ull, 0x3bb5c9b8899c47aeull, > + 0x7fcc0148f709a5d0ull, 0x51868783bf2f966bull, > + 0xfffffffffffffffaull, 0xffffffffffffffffull, > + 0xffffffffffffffffull, 0xffffffffffffffffull, > + 0x1ffull }, > + .x = { 0xa5919d2ede37ad7dull, 0xaeb490862c32ea05ull, > + 0x1da6bd16b59fe21bull, 0xad3f164a3a483205ull, > + 0xe5ad7a112d7a8dd1ull, 0xb52a6e5b123d9ab9ull, > + 0xd91d6a64b5959479ull, 0x3d352443de29195dull, > + 0x1a7ull }, > + .y = { 0xa0a7735e11793f1aull, 0x0efa36436c5a6fbdull, > + 0xd2a5312e213c38f3ull, 0xd1d22b30723a8a4full, > + 0x2d0754e05caa3113ull, 0x0eaa8057d562fce8ull, > + 0x686079393547eb0dull, 0x64fc4682059d2226ull, 0xc1ull }, > + }, > +}; > + > +KUNIT_ARRAY_PARAM_DESC(ecc_p521, ecc_p521_vecs, desc); > + > +static void ecc_p521_make_pub_key(struct kunit *test) > +{ > + const struct ecc_p521_vec *v = test->param_value; > + u64 pub[2 * P521_DIGITS], x[P521_DIGITS], y[P521_DIGITS]; > + > + KUNIT_ASSERT_EQ(test, ecc_make_pub_key(ECC_CURVE_NIST_P521, P521_DIGITS, > + v->k, pub), 0); > + ecc_swap_digits(pub, x, P521_DIGITS); > + ecc_swap_digits(&pub[P521_DIGITS], y, P521_DIGITS); > + KUNIT_EXPECT_MEMEQ(test, x, v->x, sizeof(x)); > + KUNIT_EXPECT_MEMEQ(test, y, v->y, sizeof(y)); > +} > + > +static struct kunit_case ecc_test_cases[] = { > + KUNIT_CASE_PARAM(ecc_p521_make_pub_key, ecc_p521_gen_params), > + {} > +}; > + > +static struct kunit_suite ecc_test_suite = { > + .name = "ecc", > + .test_cases = ecc_test_cases, > +}; > + > +kunit_test_suite(ecc_test_suite); > + > +MODULE_LICENSE("GPL"); > +MODULE_DESCRIPTION("KUnit tests for ECC public key generation"); > ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-12-03 16:56 UTC | newest] Thread overview: 7+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 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
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox