← ALL ADVISORIES

LOW BYTERAY-2026-0436Patched

wolfSSL — Integer Truncation of the Length Mask in the SP Integer Conditional Copy on 8-Bit Limb Builds

The constant-time conditional copy in sp_int.c builds its mask at limb width and reuses it for the 16-bit digit count. With 8-bit limbs only the low byte of the count is copied, so modular exponentiation with a modulus of 2041 bits or more returns a wrong result or an error.

Advisory
BYTERAY-2026-0436
CWE
CWE-190
CVSS
3.7 (CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:L/A:N)
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 SP_WORD_SIZE=8 and WC_NO_PTR_INT_CAST
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

_sp_cond_copy() copies one multi-precision integer into another under a mask, so that the copy does not branch on a secret bit. The mask is computed as a limb (sp_int_digit). The same mask is then cast to the type of the digit count (sp_size_t, 16 bits) and used to copy the used field. When limbs are 8 bits wide the cast gives 0x00FF, not 0xFFFF. The low byte of used is copied and the high byte keeps whatever the destination had before. An operand with 256 or more digits, which means a modulus of 2041 bits or more, is then copied with the wrong length unless the destination already had the same high byte.

The helper is used by the cache-resistant modular exponentiation ladders. On an affected build, a 2048-bit Diffie-Hellman agreement computes on a value whose length was copied as zero. The result is a wrong value or MP_VAL. There is no out-of-bounds access. The configuration is rare: 8-bit limbs are never selected automatically, and the helper is compiled only with WC_NO_PTR_INT_CAST. Default builds are not affected.

Affected Products and Versions

  • Component: wolfCrypt SP integer math, wolfcrypt/src/sp_int.c, function _sp_cond_copy() and the exponentiation ladders that call it.
  • Required build: SP_WORD_SIZE defined to 8, WC_NO_PTR_INT_CAST defined, and WC_NO_CACHE_RESISTANT not defined. SP_WORD_SIZE 8 has to be set by the integrator (wolfssl/wolfcrypt/sp_int.h:213-271 never picks it). WC_NO_PTR_INT_CAST is defined automatically only for 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-stable and v5.9.4-stable tags contain the same helper and the same calls. Fixed on master by commit a537c0812845862aa94409111d10c6f7c2388f73 (PR 11967); no release contains the fix.
  • A default ./configure build does not compile the helper. Earlier releases were not checked.

Technical Details

The helper, at wolfcrypt/src/sp_int.c:5136-5148:

static void _sp_cond_copy(const sp_int* a, int copy, sp_int* r, sp_size_t used)
{
    sp_int_digit mask = (sp_int_digit)0 - (sp_int_digit)copy;
    unsigned int i;

    for (i = 0; i < (unsigned int)used; i++) {
        r->dp[i] ^= (r->dp[i] ^ a->dp[i]) & mask;
    }
    r->used ^= (r->used ^ a->used) & (sp_size_t)mask;

With SP_WORD_SIZE == 8, sp_int_digit is an 8-bit type (wolfssl/wolfcrypt/sp_int.h:294-297) and sp_size_t is word16 (wolfssl/wolfcrypt/sp_int.h:965-967). For copy == 1 the mask is 0xFF, and (sp_size_t)mask is 0x00FF. The digit loop is correct. The last line copies bits 0 to 7 of a->used and leaves bits 8 to 15 of r->used alone. The result is right only when the two counts already agree in their high byte.

The Montgomery ladder shows the effect. Its temporaries start with used == 0 (wolfcrypt/src/sp_int.c:13455-13458). Each step selects the working value into t[3] with two _sp_cond_copy() calls and squares it (wolfcrypt/src/sp_int.c:13507-13514).

  1. t[0] holds the base in Montgomery form, a residue of a 2048-bit modulus. Unless its top byte happens to be zero it has 256 digits, so t[0]->used is 0x0100.
  2. The first copy at wolfcrypt/src/sp_int.c:13507 moves 256 digits into t[3] and computes t[3]->used ^= (0 ^ 0x0100) & 0x00FF, which leaves t[3]->used at 0.
  3. sp_sqr() at wolfcrypt/src/sp_int.c:13514 sees a zero operand and returns zero (wolfcrypt/src/sp_int.c:17426-17427).
  4. The copy back at wolfcrypt/src/sp_int.c:13519-13520 has the same truncation, and the ladder continues on corrupted lengths.

For other sizes the stale high byte can make used larger than the true length. The temporaries hold 2 * m->used + 1 digits and the truncation can add at most 255, so the inflated count stays inside the allocation. When the count is too large for the result, sp_sqr(), sp_mul() and sp_sqrmod() reject the operand with MP_VAL (wolfcrypt/src/sp_int.c:17415, wolfcrypt/src/sp_int.c:12184, wolfcrypt/src/sp_int.c:17604). The same pair of calls appears in the non-Montgomery ladder (wolfcrypt/src/sp_int.c:13245-13282) and in the encrypted-memory variants described in BYTERAY-2026-0361.

Reachability and Preconditions

  • The build must have 8-bit limbs and WC_NO_PTR_INT_CAST, as above.
  • The exponentiation modulus must have 256 or more digits. That covers Diffie-Hellman with a prime of 2048 bits or more (wolfcrypt/src/dh.c:2323-2327), RSA private operations that exponentiate modulo n on keys of 2048 bits or more (wolfcrypt/src/rsa.c:3068-3075), and RSA CRT on keys of 4096 bits or more, where each prime has at least 256 digits (wolfcrypt/src/rsa.c:3108-3112).
  • No attacker input is needed. Honest operands trigger it. A peer cannot steer the path either: in WOLFSSL_SP_MATH_ALL builds sp_exptmod_ex() sends every odd multi-digit modulus to the Montgomery ladder (wolfcrypt/src/sp_int.c:14319-14323), and Diffie-Hellman and RSA reject even moduli before they exponentiate (wolfcrypt/src/dh.c:2110-2112, wolfcrypt/src/rsa.c:3303-3306). No authentication state or user interaction is involved.

Impact

Modular exponentiation returns a wrong value or fails for moduli of 2041 bits or more on the affected build. Key agreement and private-key operations at those sizes do not work. The failure needs no crafted input, so a deployment in this configuration sees it in ordinary testing. Source review found no memory corruption: the wrong length never exceeds the size of the temporaries. Whether a wrong intermediate can be turned into a key-recovery fault attack was not examined and is not claimed.

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 derives a separate mask for used at the width of sp_size_t, and another for the sign. The commit message records the cause: the digit-wide mask dropped the high bits of used when SP_WORD_SIZE is 8 and an operand had 256 or more digits. The same commit makes this helper the default copy for all builds. The fix is on master only. No release contains it as of 2026-12-03.

Until a release does, do not combine SP_WORD_SIZE 8 with WC_NO_PTR_INT_CAST for keys of 2048 bits or more. A 16-bit or wider limb avoids the truncation.

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.

References