Skip to content

Commit bd955a4

Browse files
stefanbergerherbertx
authored andcommitted
crypto: ecdh - Pass private key in proper byte order to check valid key
ecc_is_key_valid expects a key with the most significant digit in the last entry of the digit array. Currently ecdh_set_secret passes a reversed key to ecc_is_key_valid that then passes the rather simple test checking whether the private key is in range [2, n-3]. For all current ecdh- supported curves (NIST P192/256/384) the 'n' parameter is a rather large number, therefore easily passing this test. Throughout the ecdh and ecc codebase the variable 'priv' is used for a private_key holding the bytes in proper byte order. Therefore, introduce priv in ecdh_set_secret and copy the bytes from ctx->private_key into priv in proper byte order by using ecc_swap_digits. Pass priv to ecc_is_valid_key. Cc: Ard Biesheuvel <[email protected]> Cc: Salvatore Benedetto <[email protected]> Signed-off-by: Stefan Berger <[email protected]> Acked-by: Jarkko Sakkinen <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 5ae6d3f commit bd955a4

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

crypto/ecdh.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
2727
unsigned int len)
2828
{
2929
struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
30+
u64 priv[ECC_MAX_DIGITS];
3031
struct ecdh params;
32+
int ret = 0;
3133

3234
if (crypto_ecdh_decode_key(buf, len, &params) < 0 ||
3335
params.key_size > sizeof(u64) * ctx->ndigits)
@@ -40,13 +42,16 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
4042
ctx->private_key);
4143

4244
memcpy(ctx->private_key, params.key, params.key_size);
45+
ecc_swap_digits(ctx->private_key, priv, ctx->ndigits);
4346

4447
if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
45-
ctx->private_key, params.key_size) < 0) {
48+
priv, params.key_size) < 0) {
4649
memzero_explicit(ctx->private_key, params.key_size);
47-
return -EINVAL;
50+
ret = -EINVAL;
4851
}
49-
return 0;
52+
memzero_explicit(priv, sizeof(priv));
53+
54+
return ret;
5055
}
5156

5257
static int ecdh_compute_value(struct kpp_request *req)

0 commit comments

Comments
 (0)