Skip to content

Commit 3da2c1d

Browse files
vt-altherbertx
authored andcommitted
crypto: ecc - regularize scalar for scalar multiplication
ecc_point_mult is supposed to be used with a regularized scalar, otherwise, it's possible to deduce the position of the top bit of the scalar with timing attack. This is important when the scalar is a private key. ecc_point_mult is already using a regular algorithm (i.e. having an operation flow independent of the input scalar) but regularization step is not implemented. Arrange scalar to always have fixed top bit by adding a multiple of the curve order (n). References: The constant time regularization step is based on micro-ecc by Kenneth MacKay and also referenced in the literature (Bernstein, D. J., & Lange, T. (2017). Montgomery curves and the Montgomery ladder. (Cryptology ePrint Archive; Vol. 2017/293). s.l.: IACR. Chapter 4.6.2.) Signed-off-by: Vitaly Chikunov <vt@altlinux.org> Cc: kernel-hardening@lists.openwall.com Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 8a5a79d commit 3da2c1d

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

‎crypto/ecc.c‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -842,15 +842,23 @@ static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
842842

843843
static void ecc_point_mult(struct ecc_point *result,
844844
const struct ecc_point *point, const u64 *scalar,
845-
u64 *initial_z, u64 *curve_prime,
845+
u64 *initial_z, const struct ecc_curve *curve,
846846
unsigned int ndigits)
847847
{
848848
/* R0 and R1 */
849849
u64 rx[2][ECC_MAX_DIGITS];
850850
u64 ry[2][ECC_MAX_DIGITS];
851851
u64 z[ECC_MAX_DIGITS];
852+
u64 sk[2][ECC_MAX_DIGITS];
853+
u64 *curve_prime = curve->p;
852854
int i, nb;
853-
int num_bits = vli_num_bits(scalar, ndigits);
855+
int num_bits;
856+
int carry;
857+
858+
carry = vli_add(sk[0], scalar, curve->n, ndigits);
859+
vli_add(sk[1], sk[0], curve->n, ndigits);
860+
scalar = sk[!carry];
861+
num_bits = sizeof(u64) * ndigits * 8 + 1;
854862

855863
vli_set(rx[1], point->x, ndigits);
856864
vli_set(ry[1], point->y, ndigits);
@@ -1014,7 +1022,7 @@ int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
10141022
goto out;
10151023
}
10161024

1017-
ecc_point_mult(pk, &curve->g, priv, NULL, curve->p, ndigits);
1025+
ecc_point_mult(pk, &curve->g, priv, NULL, curve, ndigits);
10181026
if (ecc_point_is_zero(pk)) {
10191027
ret = -EAGAIN;
10201028
goto err_free_point;
@@ -1100,7 +1108,7 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
11001108
goto err_alloc_product;
11011109
}
11021110

1103-
ecc_point_mult(product, pk, priv, rand_z, curve->p, ndigits);
1111+
ecc_point_mult(product, pk, priv, rand_z, curve, ndigits);
11041112

11051113
ecc_swap_digits(product->x, secret, ndigits);
11061114

0 commit comments

Comments
 (0)