Skip to content

Commit

Permalink
crypto: ecdh - avoid unaligned accesses in ecdh_set_secret()
Browse files Browse the repository at this point in the history
ecdh_set_secret() casts a void* pointer to a const u64* in order to
feed it into ecc_is_key_valid(). This is not generally permitted by
the C standard, and leads to actual misalignment faults on ARMv6
cores. In some cases, these are fixed up in software, but this still
leads to performance hits that are entirely avoidable.

So let's copy the key into the ctx buffer first, which we will do
anyway in the common case, and which guarantees correct alignment.

Cc: <stable@vger.kernel.org>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
  • Loading branch information
ardbiesheuvel authored and herbertx committed Dec 4, 2020
1 parent 05c2a70 commit 17858b1
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions crypto/ecdh.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
return ecc_gen_privkey(ctx->curve_id, ctx->ndigits,
ctx->private_key);

if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
(const u64 *)params.key, params.key_size) < 0)
return -EINVAL;

memcpy(ctx->private_key, params.key, params.key_size);

if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
ctx->private_key, params.key_size) < 0) {
memzero_explicit(ctx->private_key, params.key_size);
return -EINVAL;
}
return 0;
}

Expand Down

0 comments on commit 17858b1

Please sign in to comment.