Skip to content

Commit cd65eaf

Browse files
ErwinPawliczekherbertx
authored andcommitted
crypto: ecc - Randomize the initial Z in ecc_make_pub_key()
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 <[email protected]> Signed-off-by: Erwin Pawliczek <[email protected]> Reviewed-by: Ignat Korchagin <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 99f506c commit cd65eaf

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

‎crypto/ecc.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,20 +1614,26 @@ int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
16141614
{
16151615
int ret = 0;
16161616
struct ecc_point *pk;
1617+
u64 rand_z[ECC_MAX_DIGITS];
1618+
unsigned int nbytes;
16171619
const struct ecc_curve *curve = ecc_get_curve(curve_id);
16181620

1619-
if (!private_key) {
1621+
if (!private_key || ndigits > ARRAY_SIZE(rand_z)) {
16201622
ret = -EINVAL;
16211623
goto out;
16221624
}
16231625

1626+
nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
1627+
1628+
get_random_bytes(rand_z, nbytes);
1629+
16241630
pk = ecc_alloc_point(ndigits);
16251631
if (!pk) {
16261632
ret = -ENOMEM;
16271633
goto out;
16281634
}
16291635

1630-
ecc_point_mult(pk, &curve->g, private_key, NULL, curve, ndigits);
1636+
ecc_point_mult(pk, &curve->g, private_key, rand_z, curve, ndigits);
16311637

16321638
/* SP800-56A rev 3 5.6.2.1.3 key check */
16331639
if (ecc_is_pubkey_valid_full(curve, pk)) {
@@ -1641,6 +1647,7 @@ int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
16411647
err_free_point:
16421648
ecc_free_point(pk);
16431649
out:
1650+
memzero_explicit(rand_z, sizeof(rand_z));
16441651
return ret;
16451652
}
16461653
EXPORT_SYMBOL(ecc_make_pub_key);

0 commit comments

Comments
 (0)