Skip to content

Commit

Permalink
crypto: ecc - regularize scalar for scalar multiplication
Browse files Browse the repository at this point in the history
[ Upstream commit 3da2c1d ]

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>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
vt-alt authored and gregkh committed Jan 26, 2019
1 parent 6e5be6e commit dbb97f7
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions crypto/ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -842,15 +842,23 @@ static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,

static void ecc_point_mult(struct ecc_point *result,
const struct ecc_point *point, const u64 *scalar,
u64 *initial_z, u64 *curve_prime,
u64 *initial_z, const struct ecc_curve *curve,
unsigned int ndigits)
{
/* R0 and R1 */
u64 rx[2][ECC_MAX_DIGITS];
u64 ry[2][ECC_MAX_DIGITS];
u64 z[ECC_MAX_DIGITS];
u64 sk[2][ECC_MAX_DIGITS];
u64 *curve_prime = curve->p;
int i, nb;
int num_bits = vli_num_bits(scalar, ndigits);
int num_bits;
int carry;

carry = vli_add(sk[0], scalar, curve->n, ndigits);
vli_add(sk[1], sk[0], curve->n, ndigits);
scalar = sk[!carry];
num_bits = sizeof(u64) * ndigits * 8 + 1;

vli_set(rx[1], point->x, ndigits);
vli_set(ry[1], point->y, ndigits);
Expand Down Expand Up @@ -1004,7 +1012,7 @@ int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
goto out;
}

ecc_point_mult(pk, &curve->g, priv, NULL, curve->p, ndigits);
ecc_point_mult(pk, &curve->g, priv, NULL, curve, ndigits);
if (ecc_point_is_zero(pk)) {
ret = -EAGAIN;
goto err_free_point;
Expand Down Expand Up @@ -1090,7 +1098,7 @@ int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
goto err_alloc_product;
}

ecc_point_mult(product, pk, priv, rand_z, curve->p, ndigits);
ecc_point_mult(product, pk, priv, rand_z, curve, ndigits);

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

Expand Down

0 comments on commit dbb97f7

Please sign in to comment.