|
30 | 30 | #include <wolfssl/wolfcrypt/ecc.h> |
31 | 31 | #include <crypto/ecdh.h> |
32 | 32 |
|
| 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 | + |
33 | 41 | #define WOLFKM_ECDH_DRIVER ("ecdh-wolfcrypt") |
34 | 42 |
|
35 | 43 | #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, |
167 | 175 | return -EINVAL; |
168 | 176 | } |
169 | 177 |
|
| 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 | + |
170 | 196 | if (!params.key || !params.key_size) { |
171 | 197 | /* Empty secret payload. Generate our own ecc key pair */ |
172 | 198 | 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) |
546 | 572 | scatterwalk_map_and_copy(shared_secret, req->dst, 0, shared_secret_len, 1); |
547 | 573 |
|
548 | 574 | 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 | + } |
550 | 580 | if (pub) { free(pub); pub = NULL; } |
551 | 581 |
|
552 | 582 | if (ecc_pub) { |
|
0 commit comments