Skip to content

Commit 17858b1

Browse files
ardbiesheuvelherbertx
authored andcommitted
crypto: ecdh - avoid unaligned accesses in ecdh_set_secret()
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>
1 parent 05c2a70 commit 17858b1

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

crypto/ecdh.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
5353
return ecc_gen_privkey(ctx->curve_id, ctx->ndigits,
5454
ctx->private_key);
5555

56-
if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
57-
(const u64 *)params.key, params.key_size) < 0)
58-
return -EINVAL;
59-
6056
memcpy(ctx->private_key, params.key, params.key_size);
6157

58+
if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
59+
ctx->private_key, params.key_size) < 0) {
60+
memzero_explicit(ctx->private_key, params.key_size);
61+
return -EINVAL;
62+
}
6263
return 0;
6364
}
6465

0 commit comments

Comments
 (0)