wolfSSL — Integer Truncation of the Length Mask in the SP Integer Conditional Swap on 8-Bit Limb Builds
The constant-time conditional swap in sp_int.c masks the 16-bit digit counts of its operands with a limb-wide mask. With 8-bit limbs only the low byte of each count is swapped. The in-tree ECC ladder never passes operands large enough to show it; the defect is latent unless the ECC size limit is raised past 2040 bits or the function is called directly.
- Advisory
- BYTERAY-2026-0360
- CWE
- CWE-190
- CVSS
- 5.6 (CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:L/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 SP_WORD_SIZE=8, HAVE_ECC and ECC_TIMING_RESISTANT, when operands have 256 or more digits
- Credit
- Mehrun P. Hunter and Shahriyar Jalayeri, ByteRay Ltd.
Executive Summary
sp_cond_swap_ct_ex() swaps two multi-precision integers when a flag is set, without branching on the flag. It builds one mask of limb type (sp_int_digit) and uses it for the digits and, after a cast, for the 16-bit used field that holds the digit count.
With 8-bit limbs the cast mask is 0x00FF. A swap then exchanges the low bytes of the two counts and leaves the high bytes where they were. If the two operands have digit counts that differ above bit 7, both end up with a wrong length.
The only caller in wolfSSL is the ECC scalar multiplication ladder. That ladder is reached only after the curve modulus has been checked against the configured ECC size limit, which is at most 1025 bits in any in-tree configuration, or 129 eight-bit digits. Counts that small fit in the low byte, so the swap is correct there. The defect has no effect in a shipped configuration. It becomes live if an integrator raises MAX_ECC_BITS above 2040 for a custom curve, or calls the swap directly with large operands. It is not fixed on master.
Affected Products and Versions
- Component: wolfCrypt SP integer math,
wolfcrypt/src/sp_int.c,sp_cond_swap_ct_ex()and its wrappersp_cond_swap_ct(). - Required build:
SP_WORD_SIZE8 (set by the integrator, never chosen automatically),HAVE_ECC,ECC_TIMING_RESISTANT, andWC_NO_CACHE_RESISTANTnot defined (wolfcrypt/src/sp_int.c:5623-5624). At least one operand must have 256 or more digits. - Tested: wolfSSL master d95483fff72d6e987394b72edca433d4b580a0f4 (version string 5.9.2).
- The
v5.9.2-stableandv5.9.4-stabletags contain the same function. Not fixed on master. - A default
./configurebuild compiles the function with 32-bit or 64-bit limbs, where the mask is at least as wide as the count and the swap is correct. Earlier releases were not checked.
Technical Details
From wolfcrypt/src/sp_int.c:5635-5641:
int sp_cond_swap_ct_ex(sp_int* a, sp_int* b, int cnt, int swap, sp_int* t)
{
unsigned int i;
volatile sp_int_digit mask = (sp_int_digit)0 - (sp_int_digit)swap;
/* XOR other fields in sp_int into temp - mask set when swapping. */
t->used = (a->used ^ b->used) & (sp_size_t)mask;
t->used is then XORed into a->used and b->used (wolfcrypt/src/sp_int.c:5652, wolfcrypt/src/sp_int.c:5661). With SP_WORD_SIZE == 8, sp_int_digit is 8 bits (wolfssl/wolfcrypt/sp_int.h:294-297) and sp_size_t is word16 (wolfssl/wolfcrypt/sp_int.h:965-967). For swap == 1, (sp_size_t)mask is 0x00FF, so t->used carries only the low byte of the difference.
Example: a->used is 256 (0x0100) and b->used is 255 (0x00FF). The difference is 0x01FF, masked to 0x00FF. After the swap a->used is 0x01FF and b->used is 0x0000. The digits were swapped correctly; the lengths are 511 and 0. The digit loops run to the caller's cnt and are not affected. Only the length fields are wrong. What follows depends on the caller: a zero length makes the value read as zero, and an inflated length makes later arithmetic treat stale digits above the real value as part of it, or fail its size check.
The same pattern in _sp_cond_copy() is described in BYTERAY-2026-0436 and was corrected on master. The swap was not changed by that commit.
Reachability and Preconditions
- Build: 8-bit limbs with timing-resistant ECC, as above.
- In-tree callers: twelve
mp_cond_swap_ct_ex()calls in the ECC ladder (wolfcrypt/src/ecc.c:3358-3435). No other library code calls the swap; the wolfCrypt test program exercises it with small operands. - Size limit in front of the ladder:
wc_ecc_mulmod_ex()andwc_ecc_mulmod_ex2()callecc_point_to_mont()first (wolfcrypt/src/ecc.c:3870,wolfcrypt/src/ecc.c:4091), which returnsWC_KEY_SIZE_Ewhen the modulus exceedsMAX_ECC_BITS_USE(wolfcrypt/src/ecc.c:3659-3661).MAX_ECC_BITSdefaults to the largest enabled curve: 521 bits, or 1024 with SAKKE (wolfssl/wolfcrypt/ecc.h:105-148). - To reach the truncation through ECC, the integrator must define
MAX_ECC_BITSabove 2040 and use a custom curve of that size. A remote peer cannot cause this. - Direct callers:
sp_cond_swap_ct()andsp_cond_swap_ct_ex()are library-internal unless the build definesWOLFSSL_PUBLIC_MP(wolfssl/wolfcrypt/sp_int.h:1202-1203). An application that calls them with operands of 256 or more digits gets the wrong lengths.
Impact
Confirmed from source: on 8-bit limb builds the conditional swap corrupts the digit counts of operands whose counts differ in their high byte.
Not supported by the source: an effect on ECDH or ECDSA with any curve wolfSSL ships. The ECC size limit keeps operands below 256 digits, so TLS and other protocol peers cannot trigger the defect. Where an integrator has raised the limit, the consequence would be wrong scalar multiplication results or arithmetic errors; that case was not analysed further.
ByteRay did not build a proof of concept for this issue. The analysis is from source review.
Remediation and Mitigation
Not fixed on wolfSSL master as of commit e4b0841b32e7 (2026-12-03).
The fix direction is the one already applied to the conditional copy: derive the mask for used at the width of sp_size_t, and the mask for the sign at the width of the sign type, instead of casting the limb mask.
No workaround is needed for standard curves. Builds that combine 8-bit limbs with ECC operands above 2040 bits should use 16-bit or wider limbs until the function is corrected.
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.