Skip to content

Commit 28091e8

Browse files
authored
Merge pull request #8685 from philljj/linuxkm_ecdh_forcezero
linuxkm ecdh: force zero shared secret buffer, and clear old key.
2 parents 5b3e19c + 57ccabb commit 28091e8

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,
@@ -546,7 +572,11 @@ static int km_ecdh_compute_shared_secret(struct kpp_request *req)
546572
scatterwalk_map_and_copy(shared_secret, req->dst, 0, shared_secret_len, 1);
547573

548574
ecdh_shared_secret_end:
549-
if (shared_secret) { free(shared_secret); shared_secret = NULL; }
575+
if (shared_secret) {
576+
ForceZero(shared_secret, shared_secret_len);
577+
free(shared_secret);
578+
shared_secret = NULL;
579+
}
550580
if (pub) { free(pub); pub = NULL; }
551581

552582
if (ecc_pub) {

0 commit comments

Comments
 (0)