mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Erwin Pawliczek <pawliczekerwin@gmail.com>
To: Herbert Xu <herbert@gondor.apana.org.au>
Cc: linux-crypto@vger.kernel.org, linux-kernel@vger.kernel.org,
	Ignat Korchagin <ignat@linux.win>,
	"David S . Miller" <davem@davemloft.net>,
	Lukas Wunner <lukas@wunner.de>,
	Stefan Berger <stefanb@linux.ibm.com>,
	Randy Dunlap <rdunlap@infradead.org>,
	Paul Louvel <paul.louvel@bootlin.com>
Subject: [PATCH] crypto: ecc - Randomize the initial Z in ecc_make_pub_key()
Date: Sun, 25 Oct 2026 22:44:29 -0500	[thread overview]
Message-ID: <20261026034429.742489-1-pawliczekerwin@gmail.com> (raw)
In-Reply-To: <20260928042641.307683-1-pawliczekerwin@gmail.com>

ecc_make_pub_key() calls ecc_point_mult() without an initial Z, so the
final Z, and therefore the input to vli_mod_inv(), is a deterministic
function of the private key. vli_mod_inv() is not constant time, so
key generation leaks information about the private key through its
running time.

crypto_ecdh_shared_secret() already passes a random initial Z, which
blinds the inversion input. Do the same in ecc_make_pub_key().

Key generation is used in the kernel by Bluetooth SMP and TPM2 HMAC
sessions, which generate ephemeral P-256 keys.

crypto_kpp_generate_public_key(), KVM guest on an i7-1165G7 (median
over 4 boots of the best of 9 runs of 300 calls):

    P-256:   744.9 us ->  753.2 us (+1.1%; within noise)
    P-384:  1674.2 us -> 1651.6 us (-1.4%; within noise)

Suggested-by: Ignat Korchagin <ignat@linux.win>
Signed-off-by: Erwin Pawliczek <pawliczekerwin@gmail.com>
---
Replaces "crypto: ecc - Use constant-time modular inversion in ecc_point_mult()":
https://lore.kernel.org/all/20260928042641.307683-1-pawliczekerwin@gmail.com/

 crypto/ecc.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/crypto/ecc.c b/crypto/ecc.c
index c7366eb7ce1f..6696bf0d90a6 100644
--- a/crypto/ecc.c
+++ b/crypto/ecc.c
@@ -1614,20 +1614,26 @@ int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
 {
 	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) {
+	if (!private_key || ndigits > ARRAY_SIZE(rand_z)) {
 		ret = -EINVAL;
 		goto out;
 	}
 
+	nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
+
+	get_random_bytes(rand_z, nbytes);
+
 	pk = ecc_alloc_point(ndigits);
 	if (!pk) {
 		ret = -ENOMEM;
 		goto out;
 	}
 
-	ecc_point_mult(pk, &curve->g, private_key, NULL, curve, ndigits);
+	ecc_point_mult(pk, &curve->g, private_key, rand_z, curve, ndigits);
 
 	/* SP800-56A rev 3 5.6.2.1.3 key check */
 	if (ecc_is_pubkey_valid_full(curve, pk)) {
@@ -1641,6 +1647,7 @@ int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
 err_free_point:
 	ecc_free_point(pk);
 out:
+	memzero_explicit(rand_z, sizeof(rand_z));
 	return ret;
 }
 EXPORT_SYMBOL(ecc_make_pub_key);
-- 
2.53.0


      parent reply	other threads:[~2026-10-26  3:44 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-28  4:26 [PATCH] crypto: ecc - Use constant-time modular inversion in ecc_point_mult() Erwin Pawliczek
2026-10-05  3:32 ` Herbert Xu
2026-10-13 10:00   ` Ignat Korchagin
2026-10-13 10:13 ` Ignat Korchagin
2026-10-26  3:44 ` Erwin Pawliczek [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=20261026034429.742489-1-pawliczekerwin@gmail.com \
    --to=pawliczekerwin@gmail.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=ignat@linux.win \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukas@wunner.de \
    --cc=paul.louvel@bootlin.com \
    --cc=rdunlap@infradead.org \
    --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®