wolfSSL — Truncated Length Mask in the WC_PROTECT_ENCRYPTED_MEM Modular Exponentiation Ladders
The encrypted-memory variants of the SP modular exponentiation ladders select their working value with a conditional copy whose length mask is truncated to 8 bits on 8-bit limb builds. For moduli of 2041 bits or more the working value is copied with the wrong digit count and the exponentiation returns zero, another wrong value, or an error.
- Advisory
- BYTERAY-2026-0361
- CWE
- CWE-197
- CVSS
- 4.8 (CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:L/A:L)
- Vendor
- wolfSSL
- Product
- wolfSSL
- Reported
- 2026-09-28
- Disclosure
- 2026-12-03
- Affected
- wolfSSL 5.9.2 through 5.9.4, SP integer math builds with WC_PROTECT_ENCRYPTED_MEM, WC_NO_PTR_INT_CAST and SP_WORD_SIZE=8
- Fixed in
- Master commit a537c0812845862aa94409111d10c6f7c2388f73 (PR 11967, merged 2026-11-13). Not in a wolfSSL stable release as of 2026-12-03.
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
WC_PROTECT_ENCRYPTED_MEM selects alternative modular exponentiation ladders in sp_int.c. On each exponent bit they copy one of two working values into a temporary and square it. When WC_NO_PTR_INT_CAST is defined that copy is done by _sp_cond_copy(). _sp_cond_copy() masks the digit count with a mask that is only as wide as a limb. With 8-bit limbs the high byte of the 16-bit count is not copied. This is the defect described in BYTERAY-2026-0436; this advisory covers its effect in the encrypted-memory ladders.
For a 2048-bit modulus the selected value has 256 digits and the temporary starts empty. The copy leaves the temporary's count at zero, the ladder squares zero, and the zero is written back into the working value. The exponentiation returns zero or fails with MP_VAL. No memory is accessed out of bounds. The build needs three macros that no configure option sets. Default builds are not affected.
Affected Products and Versions
- Component: wolfCrypt SP integer math,
wolfcrypt/src/sp_int.c, theWC_PROTECT_ENCRYPTED_MEMdefinitions of_sp_exptmod_ex()and_sp_exptmod_mont_ex(). - Required build:
WC_PROTECT_ENCRYPTED_MEM,WC_NO_PTR_INT_CASTandSP_WORD_SIZE8, all defined by the integrator.WC_PROTECT_ENCRYPTED_MEMis not set byconfigure.acorsettings.h.WC_NO_PTR_INT_CASTis automatic only on CHERI pure-capability and Fil-C targets (wolfssl/wolfcrypt/settings.h:6017-6019). - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- The
v5.9.2-stableandv5.9.4-stabletags contain the same ladders and the same helper. Fixed on master by commit a537c0812845862aa94409111d10c6f7c2388f73 (PR 11967); no release contains the fix. - A default
./configurebuild does not compile these ladders. Earlier releases were not checked.
Technical Details
The encrypted-memory _sp_exptmod_ex() is at wolfcrypt/src/sp_int.c:13331-13400. Its four temporaries are initialised with used == 0 (wolfcrypt/src/sp_int.c:13342-13345). t[0] is set to 1 and t[1] to the base. On each exponent bit y, when WC_NO_PTR_INT_CAST is defined, the loop selects the value to square with (wolfcrypt/src/sp_int.c:13378-13379):
_sp_cond_copy(t[0], y^1, t[3], m->used);
_sp_cond_copy(t[1], y, t[3], m->used);
_sp_cond_copy() ends with r->used ^= (r->used ^ a->used) & (sp_size_t)mask; (wolfcrypt/src/sp_int.c:5144). mask is an sp_int_digit (wolfcrypt/src/sp_int.c:5138). With SP_WORD_SIZE == 8 the cast gives 0x00FF, while sp_size_t is 16 bits (wolfssl/wolfcrypt/sp_int.h:965-967).
With a 256-digit base and the first set exponent bit:
t[1]->usedis0x0100.t[3]->usedis 0 or 1 from earlier steps.- The copy at
wolfcrypt/src/sp_int.c:13379writes 256 digits intot[3]and sets the low byte oft[3]->usedto0x00. The high byte stays 0.t[3]now reads as zero. sp_sqrmod()atwolfcrypt/src/sp_int.c:13385returns zero._sp_copy_2_ct()atwolfcrypt/src/sp_int.c:13389stores that zero int[1]. This helper copiesusedat full width (wolfcrypt/src/sp_int.c:5532-5535), so the zero is propagated faithfully.- Every later step multiplies by that zero. Unless the set bit was the last bit of the exponent, the function returns 0 through
sp_copy()atwolfcrypt/src/sp_int.c:13395.
For larger moduli the low byte of the count is not zero and the value is cut to its low digits instead. When a stale high byte makes the count too large, sp_sqrmod() returns MP_VAL (wolfcrypt/src/sp_int.c:17604). The temporaries hold 2 * m->used + 1 digits and the count is wrong by at most 255, so it never points outside them. The encrypted-memory Montgomery ladder has the same two calls at wolfcrypt/src/sp_int.c:13667-13668, followed by sp_sqr() and the same _sp_copy_2_ct() (wolfcrypt/src/sp_int.c:13674-13681). It fails the same way.
Reachability and Preconditions
- The build must define all three macros listed above, and the modulus must have 256 or more 8-bit digits, which is 2041 bits or more.
sp_exptmod_ex()sends odd multi-digit moduli to the Montgomery ladder (wolfcrypt/src/sp_int.c:14319-14323). That is the variant reached by Diffie-Hellman agreement and RSA private-key operations, with ordinary keys._sp_exptmod_ex()itself is reached only for an even modulus (wolfcrypt/src/sp_int.c:14331-14334). Diffie-Hellman and RSA reject even moduli first (wolfcrypt/src/dh.c:2110-2112,wolfcrypt/src/rsa.c:3303-3306). Callers that do not, such aswc_DsaCheckPubKey()(wolfcrypt/src/dsa.c:166) andwolfSSL_BN_mod_exp()(src/ssl_bn.c:1963), can reach it.- No attacker input is required. Honest operands of the affected size trigger it.
Impact
On the affected build, modular exponentiation with a modulus of 2041 bits or more returns zero, another wrong value, or MP_VAL. Key agreement and private-key operations at those sizes fail or produce unusable output. Source review found no out-of-bounds read or write. Whether a wrong intermediate value could support a fault-style key recovery was not examined and is not claimed. The failure needs no crafted input, so it shows up in ordinary testing of such a build.
ByteRay did not build a proof of concept for this issue. The analysis is from source review.
Remediation and Mitigation
Fixed on master by commit a537c0812845862aa94409111d10c6f7c2388f73 (PR 11967, merged 2026-11-13). _sp_cond_copy() now computes the mask for used at the width of sp_size_t. The encrypted-memory ladders call the same helper and need no change of their own. The fix is on master only. No release contains it as of 2026-12-03.
Until a release does, build WC_PROTECT_ENCRYPTED_MEM targets with 16-bit or wider limbs, or keep moduli below 2041 bits on 8-bit limb builds.
Timeline
- 2026-09-28 — Reported to wolfSSL.
- 2026-11-03 — Pull request 11967 opened upstream.
- 2026-11-13 — Pull request 11967 merged to master.
- 2026-12-03 — Public disclosure.
Credits
Discovered and reported by Mehrun P. Hunter and Shahriyar Jalayeri of ByteRay Ltd.