wolfSSL — Secret-Indexed Operands in SP Math Modular Exponentiation Without Cache Resistance
When WC_NO_CACHE_RESISTANT is defined, _sp_exptmod_ex in the SP math backend squares and multiplies into a temporary chosen by the current secret exponent bit. The masked constant-address variant in the same function is compiled out. No shipped configuration combines SP math with the macro.
- Advisory
- BYTERAY-2026-0408
- CWE
- CWE-1300
- CVSS
- 5.7 (CVSS:3.1/AV:P/AC:H/PR:N/UI:N/S:U/C:H/I:H/A:N)
- Vendor
- wolfSSL
- Product
- wolfSSL
- Reported
- 2026-09-28
- Disclosure
- 2026-12-03
- Affected
- wolfSSL 5.9.2 (tested at master commit d95483f), builds that combine SP math (WOLFSSL_SP_MATH or WOLFSSL_SP_MATH_ALL) with WC_NO_CACHE_RESISTANT
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
In the SP math backend, _sp_exptmod_ex() performs modular exponentiation for RSA private-key operations and DH shared-secret computation. It has two implementations selected at compile time.
When the build defines WC_NO_CACHE_RESISTANT, the compiled version walks the secret exponent bit by bit and chooses which temporary it squares and multiplies from the value of that bit. Iterations for a 1 bit and a 0 bit work on different operand addresses. The other version selects operands through masked copies at constant addresses and is compiled out by the macro.
An attacker who can physically measure a device running such a build during RSA or DH private operations can attempt to recover the RSA private exponent or the DH private exponent from many traces. No configuration shipped in the repository combines SP math with the macro. The Zephyr settings template, which defaults to SP math, carries a commented-out define that recommends the macro for systems with a cache, and an ESP32 build switched from TFM to SP math inherits the macro from the platform defaults.
Affected Products and Versions
- Component:
wolfcrypt/src/sp_int.c,_sp_exptmod_ex(). - Required build:
WOLFSSL_SP_MATHorWOLFSSL_SP_MATH_ALLtogether withWC_NO_CACHE_RESISTANT. - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- No shipped template enables the combination. The shipped SP math templates leave the macro commented out. The ESP32 platform defaults define the macro (
wolfssl/wolfcrypt/settings.h:727,wolfssl/wolfcrypt/settings.h:1068) but select TFM (IDE/Espressif/ESP-IDF/user_settings.h:165-166), so those builds do not reach this function. - Other releases were not checked.
Technical Details
Under WC_NO_CACHE_RESISTANT the workspace is two SP integers instead of three (wolfcrypt/src/sp_int.c:13183-13195), and the main loop is (wolfcrypt/src/sp_int.c:13229-13241):
err = sp_sqrmod(t[s], m, t[s]);
if (err == MP_OKAY) {
int y = (e->dp[i >> SP_WORD_SHIFT] >> (i & SP_WORD_MASK)) & 1;
int j = y & s;
s |= y;
err = _sp_mulmod(t[j], b, m, t[j]);
}
y is the current exponent bit, and t[j] is the multiply operand. The #else branch (wolfcrypt/src/sp_int.c:13242-13260) copies (t[0] & sp_off_on_addr[s^1]) + (t[1] & sp_off_on_addr[s]) into the third workspace integer and works on that, so the addresses used do not depend on the bit. The constant-time swap sp_cond_swap_ct_ex is removed by the same macro (wolfcrypt/src/sp_int.c:5623-5624).
- An attacker supplies RSA ciphertexts or DH public values to a device.
- The private operations (
wc_RsaPrivateDecryptEx,wc_DhAgree_Sync) callsp_exptmodorsp_exptmod_ex, which dispatch to_sp_exptmod_ex(); the even-modulus dispatcher is atwolfcrypt/src/sp_int.c:14331. - Each loop iteration reads one exponent bit (
wolfcrypt/src/sp_int.c:13231-13233) and multiplies intot[j]. - The per-bit difference in operand address shows in power, electromagnetic and cache measurements.
The Zephyr template contains //#define WC_NO_CACHE_RESISTANT with the comment "systems with cache should enable this for AES, ECC, RSA and DH" (zephyr/user_settings.h:486), in the same file that selects SP math (zephyr/user_settings.h:568-576). A build that follows that comment gets the ladder described here.
Reachability and Preconditions
- A custom build with SP math and
WC_NO_CACHE_RESISTANT. - The device performs RSA decryption or signing, or DH agreement. A remote party can supply the inputs.
- The attacker has physical or near-field access to measure power or electromagnetic emissions, synchronized to the operations.
- Many traces are collected.
Impact
On a build that compiles this ladder: recovery of the RSA private key or of the DH private exponent, which allows decryption of captured sessions and, for RSA, impersonation. The leak is the same kind as in the TFM ladder described in BYTERAY-2026-0542. No shipped image compiles it, so the issue applies to custom configurations.
ByteRay did not build a proof of concept for this issue. The analysis is from source review.
Remediation and Mitigation
ByteRay has not confirmed a fixed wolfSSL release for this issue as of 2026-12-03, and has not checked it against wolfSSL changes made after the tested commit.
The fix direction is to stop building this ladder: make the masked constant-address branch unconditional and always allocate the third workspace integer, or at least raise a compile error when WC_NO_CACHE_RESISTANT is defined together with SP math. The same applies to sp_cond_swap_ct_ex. The comment in the Zephyr template should be removed or corrected, and the platform defaults should not carry the macro into builds that may select SP math. Workaround: do not define WC_NO_CACHE_RESISTANT in SP math builds. When moving an ESP32 project from TFM to SP math, undefine it explicitly.
Timeline
- 2026-09-28 — Reported to wolfSSL.
- 2026-12-03 — Public disclosure.
Credits
Discovered and reported by Mehrun P. Hunter and Shahriyar Jalayeri of ByteRay Ltd.