crypto: ecdh - Pass private key in proper byte order to check valid key
[linux-block.git] / crypto / ecdh.c
index 3049f147e0117b2c37f2a072f98a9bbef229ce5c..c02c9a2b9682db720cd37730b557e0aaf7ffe9b2 100644 (file)
@@ -27,7 +27,9 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
                           unsigned int len)
 {
        struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
+       u64 priv[ECC_MAX_DIGITS];
        struct ecdh params;
+       int ret = 0;
 
        if (crypto_ecdh_decode_key(buf, len, &params) < 0 ||
            params.key_size > sizeof(u64) * ctx->ndigits)
@@ -40,13 +42,16 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
                                       ctx->private_key);
 
        memcpy(ctx->private_key, params.key, params.key_size);
+       ecc_swap_digits(ctx->private_key, priv, ctx->ndigits);
 
        if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
-                            ctx->private_key, params.key_size) < 0) {
+                            priv, params.key_size) < 0) {
                memzero_explicit(ctx->private_key, params.key_size);
-               return -EINVAL;
+               ret = -EINVAL;
        }
-       return 0;
+       memzero_explicit(priv, sizeof(priv));
+
+       return ret;
 }
 
 static int ecdh_compute_value(struct kpp_request *req)