Skip to content

Fix used digit handling on fast math - #12278

Open
yosuke-wolfssl wants to merge 3 commits into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f-tfm-digit
Open

yosuke-wolfssl wants to merge 3 commits into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f-tfm-digit

Conversation

@yosuke-wolfssl

@yosuke-wolfssl yosuke-wolfssl commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Fast math (USE_FAST_MATH) requires every digit of dp[] at and above used to be zero. s_fp_add(), the borrow read in fp_submod_ct() and the comba multipliers read past used and rely on it. Four writers leave non-zero digits there, so the result prints correctly but the next operation on it is wrong.

Fix (wolfcrypt/src/tfm.c)

  • CT helpers clear the one digit just above the modulus length after writing. The index depends only on the modulus size, so the time does not depend on the destination's previous value. Through fp_submod_ct(), this also clears the Montgomery carry digit in both fp_montgomery_reduce_ex() and fp_montgomery_reduce_mulx().
  • fp_mod_2d() negates only the digits below bmax.
  • mp_rand() (wolfmath.c) calls mp_zero() before setting used on fast math.

Closes f-18447, f-18450, f-18451, f-18454.

Tests

  • New helper mp_test_no_digits_above_used() in test.c.
  • mp_test_addsubmod_ct() and mp_test_mod_2d() now also runs on fast math.
  • test_mp_rand checks the digits above used on fast math.
  • New fastmath-mont-red-ct leg in os-check-linux.json.

Verification

  • Each new check fails on the unfixed code and passes with the fix.
  • testwolfcrypt and the wolfmath API group pass on fast math (±WOLFSSL_MONT_RED_CT, ALT_ECC_SIZE), sp_int and heap math; make check passes on fast math with WOLFSSL_MONT_RED_CT.
  • ASan + UBSan clean; GCC -Werror (Ubuntu 24.04) clean; 32-bit digits (i386) pass.

Not covered in CI: fp_montgomery_reduce_mulx() with WOLFSSL_MONT_RED_CT (needs --enable-intelasm). It shares the fixed fp_submod_ct() tail with the generic reduction.

- The negation loop in fp_mod_2d() stops at bmax.
- test.c gains mp_test_no_digits_above_used(). mp_test_mod_2d() runs
  on fast math, uses two-digit negative operands, and checks each
  result with it.

Issue: F-18451
@yosuke-wolfssl yosuke-wolfssl self-assigned this Nov 26, 2026
Copilot AI balanced review requested due to automatic review settings November 26, 2026 02:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The zero check can miss upper-half corruption in 64-bit digits, and the CI configuration does not enable the intended Koblitz curves, leaving their arithmetic untested.

2 open issues
What changed in this PR

This PR restores fast math’s requirement that digits at and above used remain zero, preventing stale digits from corrupting subsequent arithmetic in wolfCrypt.

Changes:

  • Clear stale destination digits in constant-time helpers and random-number generation.
  • Limit negative fp_mod_2d() processing to the relevant digits.
  • Expand regression checks and add a constant-time Montgomery-reduction CI configuration.
File Description
wolfcrypt/​test/​test.c Adds digit-invariant checks and extends arithmetic tests to fast math.
wolfcrypt/​src/​wolfmath.c Clears the destination before setting its random-number length.
wolfcrypt/​src/​tfm.c Fixes stale digits in modular arithmetic helpers.
tests/​api/​test_wolfmath.c Checks unused digits after mp_rand().
.github/​configs/​os-check-linux.json Adds fast-math CI coverage with constant-time Montgomery reduction.

Review effort: Balanced

Comment thread .github/configs/os-check-linux.json Resolved
Comment thread tests/api/test_wolfmath.c Resolved
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m4

  • FLASH: .text +64 B (+0.0%, 211,629 B / 262,144 B, total: 81% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +108 B, text +13,920 B (+5.0%, 296,500 B / 67,108,864 B, total: 0% used)
  • RAM: bss +49 B, datas +16 B (+0.1%, 58,423 B / 983,040 B, total: 6% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@yosuke-wolfssl

Copy link
Copy Markdown
Contributor Author

@claude review

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reviewed this PR and found no bugs. It changes constant-time helpers in TomsFastMath that ECC and the Montgomery reduction depend on, so a maintainer familiar with tfm.c should still look at it.

What was reviewed:

  • In fp_div_2_mod_ct, fp_submod_ct and fp_addmod_ct, the new loops that clear the high digits stop at the destination's old used. That is a public length, so they add no branch or loop that depends on secret data. Reading used before the write is also safe when the destination is the same object as a or b.
  • In fp_mod_2d, limiting negation to x < bmax matches the existing bmax fill loop and the mask on the top digit that follow it.
  • The guard on mp_test_mod_2d was removed, but mp_test itself is only built under WOLFSSL_SP_MATH_ALL or USE_FAST_MATH, so no build now reaches it without mp_mod_2d. I did not build or run the new tests or the CI leg.
Extended reasoning...

The change edits four arithmetic routines in wolfcrypt/src/tfm.c and mp_rand in wolfmath.c, adds fast-math checks to test.c and test_wolfmath.c, and adds one CI leg. The security-sensitive surface is constant-time modular arithmetic used by ECC and the Montgomery reduction. Reading the code, the fixes look correct and keep the constant-time property. I deferred instead of approving because this is core crypto math. Also, the two Copilot inline threads were resolved by the PR author, though later commits may have addressed them.

@yosuke-wolfssl yosuke-wolfssl changed the title Fix/f tfm digit Fix used digit handling on fast math Nov 26, 2026

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #12278

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-bugs
Not reviewed (no scan target covers them): .github/configs/os-check-linux.json
Coverage: 4 of 4 in-scope changed file(s) opened by the reviewer

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Max

Comment thread wolfcrypt/src/tfm.c Outdated
}
c->dp[i] = (c->dp[i] >> 1) | ((fp_digit)w << (DIGIT_BIT - 1));
c->used = i + 1;
for (i = c->used; i < oldused; i++) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function and others changed below must be constant time.
This changes make them not constant time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct.
It now clears only dp[modulus->used], a single store at a public index.

- fp_addmod_ct(), fp_submod_ct() and fp_div_2_mod_ct() zero the
  destination's digit at the modulus length after writing the
  result.
- mp_test_addsubmod_ct() runs on fast math and starts each
  destination one digit wider than the modulus; it and
  mp_test_mont() check the result with
  mp_test_no_digits_above_used().
- os-check-linux.json gains a fastmath-mont-red-ct leg.

Issue: F-18450, F-18447
- mp_rand() calls mp_zero() before setting used on fast math.
- test_mp_rand checks the digits above used on fast math.

Issue: F-18454
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants