Skip to content

SP int: fix constant time - #12075

Merged
JacobBarthelmeh merged 1 commit into
wolfSSL:masterfrom
SparkiDev:dh_modexp_sp_int_ct_fix
Nov 12, 2026
Merged

JacobBarthelmeh merged 1 commit into
wolfSSL:masterfrom
SparkiDev:dh_modexp_sp_int_ct_fix

Conversation

@SparkiDev

Copy link
Copy Markdown
Contributor

Description

Added WC_MODEXP_TIMING_RESISTANT to perform constant time operations at a finer level.
Improved code to mitigate some of the performance loss.

Fixes zd#22770

Testing

./configure
./configure --enable-sp-math-all=huge
./configure --enable-sp-math-all=huge CFLAGS=-DWC_MODEXP_TIMING_RESISTANT

@SparkiDev SparkiDev self-assigned this Nov 10, 2026
Copilot AI balanced review requested due to automatic review settings November 10, 2026 02:19
@SparkiDev

Copy link
Copy Markdown
Contributor Author

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m0plus

  • FLASH: .text -28 B (-0.0%, 71,167 B / 262,144 B, total: 27% used)

gcc-arm-cortex-m3

  • FLASH: .text -16 B (-0.0%, 132,213 B / 262,144 B, total: 50% used)

gcc-arm-cortex-m4-crypto-only

  • FLASH: .text +448 B (+0.2%, 182,739 B / 262,144 B, total: 70% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +512 B (+0.3%, 197,016 B / 1,048,576 B, total: 19% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text +448 B (+0.1%, 802,628 B / 1,048,576 B, total: 77% used)

gcc-arm-cortex-m4-pkcs7

  • FLASH: .text +512 B (+0.2%, 223,892 B / 262,144 B, total: 85% used)

gcc-arm-cortex-m4-pq

  • FLASH: .text +512 B (+0.2%, 313,936 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .text +384 B (+0.1%, 344,336 B / 1,048,576 B, total: 33% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .text +512 B (+0.2%, 250,658 B / 262,144 B, total: 96% used)

gcc-arm-cortex-m7-pq

  • FLASH: .text +512 B (+0.2%, 314,768 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .text +512 B (+0.2%, 250,658 B / 262,144 B, total: 96% used)

stm32-sim-stm32h753

  • FLASH: .text +512 B (+0.2%, 205,420 B / 2,097,152 B, total: 10% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +36 B, text +11,680 B (+4.2%, 294,188 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.

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

Integer-promotion errors can corrupt results with 8- or 16-bit digits, while assembly-enabled huge builds retain secret-dependent timing despite timing-resistant hardening.

2 open issues
What changed in this PR

This PR adds optional finer-grained timing hardening to wolfCrypt’s SP integer modular exponentiation while refactoring arithmetic helpers to reduce overhead.

Changes:

  • Introduces WC_MODEXP_TIMING_RESISTANT for fixed-length working values and constant-time Montgomery reduction.
  • Adds a hardened base-2 exponentiation path and separates unclamped multiplication and squaring from public wrappers.
  • Revises constant-time clamping and Montgomery subtraction.
File Description
wolfcrypt/​src/​sp_int.c Implements exponentiation hardening and refactors supporting arithmetic.
.wolfssl_known_macro_extras Registers the new timing-resistance macro.

Review effort: Balanced

Comment thread wolfcrypt/src/sp_int.c Resolved
Comment thread wolfcrypt/src/sp_int.c Resolved
@SparkiDev
SparkiDev force-pushed the dh_modexp_sp_int_ct_fix branch 2 times, most recently from b071e69 to 5dbd362 Compare November 10, 2026 08:34
@SparkiDev

Copy link
Copy Markdown
Contributor Author

Jenkins: retest this please

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 hardened exponentiation path bypasses sign bookkeeping with WOLFSSL_SP_INT_NEGATIVE, causing negative bases to produce incorrect results such as -9 instead of 9 when squared.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment thread wolfcrypt/src/sp_int.c Resolved
@SparkiDev
SparkiDev force-pushed the dh_modexp_sp_int_ct_fix branch from b7c7ce9 to cecf475 Compare November 11, 2026 00:54
@SparkiDev SparkiDev assigned wolfSSL-Bot and unassigned SparkiDev Nov 11, 2026
@SparkiDev
SparkiDev requested a balanced review from Copilot November 11, 2026 07:23
@SparkiDev

Copy link
Copy Markdown
Contributor Author

@claude review

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.

🔵 Needs a closer look

The cryptographic constant-time changes span C, architecture-specific assembly, and word sizes, requiring specialist review and target-level disassembly and timing validation beyond source inspection.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@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.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Comment thread wolfcrypt/src/sp_int.c Resolved
@SparkiDev
SparkiDev force-pushed the dh_modexp_sp_int_ct_fix branch from cecf475 to 7772f86 Compare November 11, 2026 07:39
Generic modular exponentiation is now hardened at a finer level by default
(when WC_NO_HARDEN is not defined). Define WC_NO_MODEXP_TIMING_RESISTANT to
disable.
Improved code to mitigate some of the performance loss.
@SparkiDev

Copy link
Copy Markdown
Contributor Author

Rebased and fixed .wolfssl_known_macro_extras conflict.

@JacobBarthelmeh
JacobBarthelmeh merged commit 2798e5b into wolfSSL:master Nov 12, 2026
443 checks passed
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.

4 participants