From a4d502ce9449277850148d24661176bf68eaf961 Mon Sep 17 00:00:00 2001 From: kaleb-himes Date: Mon, 16 Nov 2026 15:09:47 -0700 Subject: [PATCH 1/2] Free key->pubkey digits before re-init in ecc_make_pub_ex --- wolfcrypt/src/ecc.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index 27c73a84c0c..877555c0041 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -5996,6 +5996,13 @@ static int ecc_make_pub_ex(ecc_key* key, ecc_curve_spec* curve, ECC_PRIVATEKEY_ONLY */ pub = &key->pubkey; key->type = ECC_PRIVATEKEY_ONLY; + #ifndef ALT_ECC_SIZE + /* wc_ecc_init() set up key->pubkey; free any digits a previous call + * grew before mp_init_multi() below drops them. */ + mp_clear(pub->x); + mp_clear(pub->y); + mp_clear(pub->z); + #endif } if (err == MP_OKAY) { From 61e10390e9fdafecbc893219e710653dd2703c05 Mon Sep 17 00:00:00 2001 From: kaleb-himes Date: Mon, 16 Nov 2026 15:27:34 -0700 Subject: [PATCH 2/2] Wipe key->k before re-init in _ecc_make_key_ex and test regenerating a key --- tests/api/test_ecc.c | 8 ++++++++ wolfcrypt/src/ecc.c | 3 +++ 2 files changed, 11 insertions(+) diff --git a/tests/api/test_ecc.c b/tests/api/test_ecc.c index e8790ffa513..3eec344eff4 100644 --- a/tests/api/test_ecc.c +++ b/tests/api/test_ecc.c @@ -293,6 +293,14 @@ int test_wc_ecc_make_key(void) #endif ExpectIntEQ(ret, 0); + /* Generate again into the same key; the first private scalar and public + * point must be released, not orphaned. */ + ret = wc_ecc_make_key(&rng, KEY14, &key); +#if defined(WOLFSSL_ASYNC_CRYPT) + ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE); +#endif + ExpectIntEQ(ret, 0); + /* Pass in bad args. */ ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index 877555c0041..094435ecee2 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -6458,6 +6458,9 @@ static int _ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key, /* setup the key variables */ #ifndef ALT_ECC_SIZE + /* wc_ecc_init() set up key->k; wipe and free any earlier private + * scalar before mp_init() below drops it. */ + mp_forcezero(key->k); err = mp_init(key->k); #else err = 0;