wolfSSL — Non-Constant-Time Base-2 Modular Exponentiation in TFM Fast Math
In the TFM backend, fp_exptmod sends a base of 2 to _fp_exptmod_base_2 before it selects the timing-resistant ladder. That routine does work that varies with the secret exponent bits, and DH key generation with generator 2 runs it on the private exponent.
- Advisory
- BYTERAY-2026-0541
- CWE
- CWE-1300
- CVSS
- 2.0 (CVSS:3.1/AV:P/AC:H/PR:N/UI:N/S:U/C:L/I:N/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 using the TFM fast-math backend (USE_FAST_MATH) with Diffie-Hellman parameters whose generator is 2
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
The TFM fast-math backend has a timing-resistant exponentiation ladder, selected with TFM_TIMING_RESISTANT. fp_exptmod() also has a shortcut for a base of 2. The shortcut is tested first and calls _fp_exptmod_base_2(), a compact routine that has no constant-time form under any build option.
In that routine the amount of shifting and reduction in each step depends on a window of secret exponent bits. The standard Diffie-Hellman generator is 2, so DH key generation runs this routine with the fresh private exponent on every key generation, and a TLS peer can trigger that by starting a DHE handshake.
An attacker who can measure the device's power, electromagnetic emissions or precise timing during the handshake can attempt to reconstruct the ephemeral private exponent and that handshake's session key. The secret is single-use and the attack needs physical measurement. Builds that do not use the TFM backend do not compile this routine.
Affected Products and Versions
- Component:
wolfcrypt/src/tfm.c,_fp_exptmod_base_2(), reached fromfp_exptmod(). - Required build: the TFM backend (
USE_FAST_MATH) with DH. The shipped ESP-IDF template selects TFM (IDE/Espressif/ESP-IDF/user_settings.h:165-166). - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- The routine is not gated by
WC_NO_CACHE_RESISTANTorTFM_TIMING_RESISTANT. A TFM build that hardens the general ladder still runs it for a base of 2. - Other releases were not checked.
Technical Details
fp_exptmod() checks for a base of 2 before it reaches the timing-resistant implementation (wolfcrypt/src/tfm.c:2992-2994):
else if (G->used == 1 && G->dp[0] == 2) {
return _fp_exptmod_base_2(X, X->used, P, Y);
}
fp_exptmod_ex() (wolfcrypt/src/tfm.c:3005-3095) has no such case and always calls _fp_exptmod_ct() (wolfcrypt/src/tfm.c:3090). _fp_exptmod_base_2() has two definitions (wolfcrypt/src/tfm.c:2590, wolfcrypt/src/tfm.c:2777-2900), and neither has a hardened variant.
The leak is in the shape of the work, not in operand addresses. The loop collects exponent bits into bitbuf (wolfcrypt/src/tfm.c:2848-2851; the first window at wolfcrypt/src/tfm.c:2810-2812), then calls fp_mul_2d(res, bitbuf, res) (wolfcrypt/src/tfm.c:2870), whose cost follows the value of the window, and then fp_mod (wolfcrypt/src/tfm.c:2875) on an operand whose size also follows it.
- A DHE handshake makes the device generate a DH key pair.
wc_DhGenerateKeyPairloads the private exponent and callsmp_exptmod(&key->g, x, &key->p, y)(wolfcrypt/src/dh.c:1438-1442).- With generator 2,
fp_exptmod()takes the base-2 branch (wolfcrypt/src/tfm.c:2992-2994). _fp_exptmod_base_2()shifts and reduces by amounts that depend on each window of the exponent.
On ESP32 the hardware wrapper in fp_exptmod() falls back to this software path on WC_HW_WAIT_E, MP_HW_FALLBACK or MP_HW_VALIDATION_ACTIVE (wolfcrypt/src/tfm.c:2929-2959). DH key agreement does not take the branch: it uses mp_exptmod_ex with the peer's public value as the base (wolfcrypt/src/dh.c:2323). RSA private operations could reach it with a chosen base of 2 only when WC_RSA_BLINDING is off, because blinding randomizes the base (wolfcrypt/src/rsa.c:3046-3060).
Reachability and Preconditions
- Build with the TFM backend and DH.
- The device negotiates TLS DHE with parameters whose generator is 2. No crafted input is needed; any peer can start the handshake without authentication.
- The attacker has physical or near-field access to sample power, electromagnetic emissions or timing during DH key generation.
- The exponent is fresh for each handshake, so recovery has to work from the traces of a single key generation.
Impact
Recovery of one ephemeral DH private exponent and the session key of the corresponding handshake. No long-term key is exposed through this path in the default configuration: key agreement uses a random peer base, and RSA is blinded by default. The attack needs laboratory-grade physical access and single-trace analysis.
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 close the dispatch gap: route every exponentiation with a secret exponent through the constant-time ladder, in fp_exptmod() and in the ESP32 software fallback. If the base-2 shortcut is kept for speed, it needs a variant whose work per iteration does not depend on the exponent: a fixed shift width, a masked conditional multiply and a constant reduction operand size.
Workaround: on physically exposed devices built with TFM, do not offer DHE cipher suites. That removes the one secret-bearing call that reaches the base-2 routine.
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.