From 9acb3068d97d395aa9540cd16bd15ad6490f0f47 Mon Sep 17 00:00:00 2001 From: Emma Stensland Date: Mon, 9 Nov 2026 11:35:41 -0700 Subject: [PATCH] Size fast math mem-zero checks by the integer's digit count With ALT_ECC_SIZE, alt_fp_ints hold fewer digits than sizeof(a->dp), so the check ran into the next integer and aborted on wiped memory. --- .github/configs/os-check-linux.json | 4 ++++ wolfcrypt/src/tfm.c | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/.github/configs/os-check-linux.json b/.github/configs/os-check-linux.json index 2dcbdfba8d2..388d977cde1 100644 --- a/.github/configs/os-check-linux.json +++ b/.github/configs/os-check-linux.json @@ -271,6 +271,10 @@ "comment": "The user_settings.h header-driven build path is distinct from the autotools-driven --enable-all path; full make check.", "user_settings": "examples/configs/user_settings_all.h", "configure": ["--enable-usersettings"]}, +{"name": "fastmath-alt-ecc-check-mem-zero", "minutes": 2.0, + "comment": "Only entry that checks mem-zero with ALT_ECC_SIZE fast math, where ECC integers are alt_fp_ints smaller than an fp_int.", + "configure": ["--enable-fastmath", + "CPPFLAGS=-DALT_ECC_SIZE -DWOLFSSL_CHECK_MEM_ZERO"]}, {"name": "she-ext-cryptocb-sw-default", "minutes": 1.9, "configure": ["--enable-she=extended", "--enable-cryptocb", "--enable-cryptocbutils", "CPPFLAGS=-DWC_SHE_SW_DEFAULT"]}, diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index 3c3a15619de..5c604e506a2 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -6061,14 +6061,30 @@ int mp_lshd (mp_int * a, int b) } #ifdef WOLFSSL_CHECK_MEM_ZERO +/* Number of bytes of digits in the integer. With ALT_ECC_SIZE, an ECC integer + * is an alt_fp_int with fewer digits than an fp_int, so use the size it was + * initialized with, as fp_forcezero() does, to stay inside it. */ +static size_t mp_memzero_len(mp_int* a) +{ +#if defined(ALT_ECC_SIZE) || defined(HAVE_WOLF_BIGINT) + return (size_t)a->size * sizeof(fp_digit); +#else + return sizeof(a->dp); +#endif +} + void mp_memzero_add(const char* name, mp_int* a) { - wc_MemZero_Add(name, a->dp, sizeof(a->dp)); + if (a != NULL) { + wc_MemZero_Add(name, a->dp, mp_memzero_len(a)); + } } void mp_memzero_check(mp_int* a) { - wc_MemZero_Check(a->dp, sizeof(a->dp)); + if (a != NULL) { + wc_MemZero_Check(a->dp, mp_memzero_len(a)); + } } #endif /* WOLFSSL_CHECK_MEM_ZERO */