Skip to content

Fix CT leaks in CTZ, div_rem, nlimbs, and Montgomery mul - #1360

Merged
tarcieri merged 7 commits into
masterfrom
fix-ct-issues
Oct 23, 2026
Merged

tarcieri merged 7 commits into
masterfrom
fix-ct-issues

Conversation

@tarcieri

@tarcieri tarcieri commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Fix secret-dependent branches in zero counting, bit-length rounding, division, and Montgomery multiplication. Reuse the hardened primitives and Choice operations from RustCrypto/utils#1570 and RustCrypto/utils#1571.

Add cargo checkct regressions across Thumb-1, Thumb-2, RISCV32, and x86-64, including unoptimized coverage for nlimbs.

@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.32%. Comparing base (fddbfd7) to head (18637b5).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1360      +/-   ##
==========================================
+ Coverage   91.30%   91.32%   +0.01%     
==========================================
  Files         190      190              
  Lines       22976    22982       +6     
==========================================
+ Hits        20979    20989      +10     
+ Misses       1997     1993       -4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

tarcieri added a commit to RustCrypto/utils that referenced this pull request Oct 23, 2026
Adds a set of functions extracted from RustCrypto/crypto-bigint#1360
which provide a set of reusable integer primitive operations where we
were previously generating non-constant-time code on certain platforms.

Validates the generated assembly is constant time using `cargo
checkct`/BINSEC.
@tarcieri

tarcieri commented Oct 23, 2026 •

Copy link
Copy Markdown
Member Author

Note: this relies on a newly added ctutils::primitives module I haven't yet released: RustCrypto/utils#1570 (which, as it were, is why semver-checks is failing).

If you think that's a bad idea and everything should just continue to live in src/primitives.rs, let me know as there's still an opportunity not to ship that since it's currently being sourced through git.

I thought it would be interesting to try to push down everything that requires e.g. Thumb-1 or 32-bit specific workarounds into that, similar to how we force down various const fn operations on Choice.

Edit: ctutils v0.4.5 release PR up: RustCrypto/utils#1575

@tarcieri
tarcieri requested a review from fjarri October 23, 2026 03:20
@tarcieri tarcieri changed the title [WIP] Fix CT leaks in CTZ, div_rem, nlimbs, and Montgomery mul Fix CT leaks in CTZ, div_rem, nlimbs, and Montgomery mul Oct 23, 2026
@tarcieri
tarcieri marked this pull request as ready for review October 23, 2026 04:04
@tarcieri
tarcieri merged commit 946a433 into master Oct 23, 2026
34 checks passed
@tarcieri
tarcieri deleted the fix-ct-issues branch October 23, 2026 04:21
@tarcieri tarcieri mentioned this pull request Oct 23, 2026
tarcieri added a commit that referenced this pull request Oct 23, 2026
## Added
- Implement `Zeroize` for `EncodedUint` (#1342)

## Changed
- Faster `floor_sqrt`, `checked_sqrt` implementation (#1312)
- Bump `ctutils` to v0.4.4; MSRV 1.87 (#1350, #1359)

## Fixed
- `BoxedUint` GCD with mixed-precision zero operands (#1291)
- Carry result from Karatsuba `wrapping_square` for larger integers (#1309)
- Exact division edge cases (#1310)
- Only use classic binary GCD in Jacobi symbol calculation (#1313)
- Montgomery reduction in repeated squaring (#1344)
- safegcd iteration count overflow at precisions of 93558 bits and above (#1346)
- Carry returned by `Uint::try_sub_with_carry` (#1354)

## Security
- Make `UintRef::PartialEq` constant-time (#1311)
- Avoid branching on invertibility in `BoxedMontyForm::invert` (#1355)
- `borrowing_sub` constant-timeness; use `cargo checkct` (#1358)
- CT leaks in CTZ, `div_rem`, `nlimbs`, and Montgomery mul (#1360)
tarcieri added a commit to RustCrypto/RSA that referenced this pull request Oct 23, 2026
Adds some initial testing of operations intended to be constant-time
using `cargo checkct`, a wrapper for BINSEC that is able to static
analysis looking for constant-time issues.

Namely this checks that `i2osp_zeroizing` avoids secret-dependent
control flow and memory accesses for small fixed-width encoding,
padding, and truncation cases.

We currently check RISCV32, Thumb-2, and x86-64, since these are what
are supported by `cargo checkct`. Unlike other places we've deployed
this, we can't test Thumb-1 due to use of `Arc`.

These tests have been configured to run in CI to detect regressions.

See also:
- RustCrypto/crypto-bigint#1358
- RustCrypto/crypto-bigint#1360
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.

1 participant