Skip to content

Commit 57ccabb

Browse files
committed
linuxkm ecdh: force zero shared secret buffer, and clear old key.
1 parent bfab68f commit 57ccabb

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

linuxkm/lkcapi_ecdh_glue.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@
3030
#include <wolfssl/wolfcrypt/ecc.h>
3131
#include <crypto/ecdh.h>
3232

33+
/* need misc.c for ForceZero(). */
34+
#ifdef NO_INLINE
35+
#include <wolfssl/wolfcrypt/misc.h>
36+
#else
37+
#define WOLFSSL_MISC_INCLUDED
38+
#include <wolfcrypt/src/misc.c>
39+
#endif
40+
3341
#define WOLFKM_ECDH_DRIVER ("ecdh-wolfcrypt")
3442

3543
#define WOLFKM_ECDH_P192_NAME ("ecdh-nist-p192")
@@ -167,6 +175,24 @@ static int km_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
167175
return -EINVAL;
168176
}
169177

178+
if (ctx->key->type == ECC_PRIVATEKEY ||
179+
ctx->key->type == ECC_PRIVATEKEY_ONLY) {
180+
/* private key already set. force clear it. */
181+
wc_ecc_free(ctx->key);
182+
183+
err = wc_ecc_init(ctx->key);
184+
if (unlikely(err < 0)) {
185+
return -ENOMEM;
186+
}
187+
188+
#ifdef ECC_TIMING_RESISTANT
189+
err = wc_ecc_set_rng(ctx->key, &ctx->rng);
190+
if (unlikely(err < 0)) {
191+
return -ENOMEM;
192+
}
193+
#endif /* ECC_TIMING_RESISTANT */
194+
}
195+
170196
if (!params.key || !params.key_size) {
171197
/* Empty secret payload. Generate our own ecc key pair */
172198
err = wc_ecc_make_key_ex(&ctx->rng, ctx->curve_len, ctx->key,
@@ -544,7 +570,11 @@ static int km_ecdh_compute_shared_secret(struct kpp_request *req)
544570
scatterwalk_map_and_copy(shared_secret, req->dst, 0, shared_secret_len, 1);
545571

546572
ecdh_shared_secret_end:
547-
if (shared_secret) { free(shared_secret); shared_secret = NULL; }
573+
if (shared_secret) {
574+
ForceZero(shared_secret, shared_secret_len);
575+
free(shared_secret);
576+
shared_secret = NULL;
577+
}
548578
if (pub) { free(pub); pub = NULL; }
549579

550580
if (ecc_pub) {

0 commit comments

Comments
 (0)