Skip to content

ECC, TFM: constant time point arithmetic, signing and modexp - #12109

Open
SparkiDev wants to merge 1 commit into
wolfSSL:masterfrom
SparkiDev:ecc_sp_int_ct_fix
Open

SparkiDev wants to merge 1 commit into
wolfSSL:masterfrom
SparkiDev:ecc_sp_int_ct_fix

Conversation

@SparkiDev

Copy link
Copy Markdown
Contributor

Description

Use constant time Montgomery reduction and compares in ECC point operations, ECDH shared secret output and ECDSA signing mod n. Constant time, fixed length TFM modular exponentiation including base 2.

Fixes zd#22789, zd#22790, zd#22798

Testing

./configure --eable-sp-math-all
./configure --enable-fastmath

@SparkiDev SparkiDev self-assigned this Nov 12, 2026
Copilot AI balanced review requested due to automatic review settings November 12, 2026 05:57
@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m0plus

  • FLASH: .text +120 B (+0.2%, 71,287 B / 262,144 B, total: 27% used)

gcc-arm-cortex-m3

  • FLASH: .text +136 B (+0.1%, 132,349 B / 262,144 B, total: 50% used)

gcc-arm-cortex-m4

  • FLASH: .text +1,536 B (+0.7%, 211,084 B / 262,144 B, total: 81% used)

gcc-arm-cortex-m4-baremetal

  • FLASH: .text +128 B (+0.2%, 74,103 B / 262,144 B, total: 28% used)

gcc-arm-cortex-m4-crypto-only

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

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +896 B (+0.5%, 197,912 B / 1,048,576 B, total: 19% used)

gcc-arm-cortex-m4-min-ecc

  • FLASH: .text +128 B (+0.2%, 68,569 B / 262,144 B, total: 26% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text +1,024 B (+0.1%, 803,716 B / 1,048,576 B, total: 77% used)

gcc-arm-cortex-m4-pkcs7

  • FLASH: .text +896 B (+0.4%, 224,788 B / 262,144 B, total: 86% used)

gcc-arm-cortex-m4-pq

  • FLASH: .text +896 B (+0.3%, 314,832 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .text +64 B (+0.0%, 344,400 B / 1,048,576 B, total: 33% used)

gcc-arm-cortex-m4-sp-math

  • FLASH: .text +128 B (+0.2%, 68,569 B / 262,144 B, total: 26% used)

gcc-arm-cortex-m4-tls12

  • FLASH: .text +128 B (+0.1%, 133,137 B / 262,144 B, total: 51% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .text +896 B (+0.4%, 251,554 B / 262,144 B, total: 96% used)

gcc-arm-cortex-m7

  • FLASH: .text +1,536 B (+0.7%, 211,084 B / 262,144 B, total: 81% used)

gcc-arm-cortex-m7-pq

  • FLASH: .text +896 B (+0.3%, 315,664 B / 1,048,576 B, total: 30% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .text +896 B (+0.4%, 251,554 B / 262,144 B, total: 96% used)

stm32-sim-stm32h753

  • FLASH: .text +512 B (+0.2%, 205,932 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)

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

Critical secret-dependent timing and memory-access leaks remain in ECC reductions, signing, and shared-secret export, while exponentiation now rejects supported boundary-size moduli.

4 open issues
What changed in this PR

This PR strengthens timing resistance in wolfCrypt’s ECC operations and its TFM and SP arithmetic backends.

Changes:

  • Routes ECC point arithmetic, mapping, and signing through constant-time helpers, and uses fixed-length ECDH output encoding.
  • Adds fixed-width TFM modular exponentiation, including base-2 and nonblocking paths.
  • Revises SP Montgomery reduction with masked subtraction and constant-time result-length calculation.
File Description
wolfssl/​wolfcrypt/​tfm.h Declares constant-time comparison functions for TFM.
wolfcrypt/​src/​tfm.c Adds fixed-width exponentiation and constant-time reduction and comparison helpers.
wolfcrypt/​src/​sp_int.c Updates constant-time Montgomery reduction and inversion paths.
wolfcrypt/​src/​ecc.c Integrates timing-resistant arithmetic into ECC operations and shared-secret encoding.

Review effort: Balanced

Comment thread wolfcrypt/src/ecc.c Resolved
Comment thread wolfcrypt/src/ecc.c Resolved
Comment thread wolfcrypt/src/tfm.c Resolved
Comment thread wolfcrypt/src/tfm.c Resolved
@SparkiDev
SparkiDev requested a balanced review from Copilot November 12, 2026 06:43
@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.

🟡 Changes recommended

Four critical findings remain: an uninitialized multiplication temporary and capacity violations in multiplication, reduction, and padded export can produce invalid results or access memory out of bounds.

4 open issues
4 resolved since last review

Review effort: Balanced

Comment thread wolfcrypt/src/sp_int.c Resolved
Comment thread wolfcrypt/src/tfm.c Resolved
Comment thread wolfcrypt/src/tfm.c Resolved
Comment thread wolfcrypt/src/tfm.c Resolved
Comment thread wolfcrypt/src/tfm.c Resolved
Comment thread wolfcrypt/src/tfm.c Resolved
Comment thread wolfcrypt/src/tfm.c Resolved
@SparkiDev
SparkiDev requested a balanced review from Copilot November 12, 2026 07:14
@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.

🟡 Changes recommended

Unresolved critical defects can cause out-of-bounds memory access, silently incorrect exponentiation results after allocation failures, and secret-dependent timing in elliptic-curve operations.

4 open issues
4 resolved since last review

Review effort: Balanced

Comment thread wolfcrypt/src/ecc.c Resolved
Comment thread wolfcrypt/src/sp_int.c Resolved
Comment thread wolfcrypt/src/tfm.c Resolved
Comment thread wolfcrypt/src/tfm.c Resolved
Comment thread wolfcrypt/src/sp_int.c Resolved

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 newly accepted FP_SIZE / 2-digit modulus boundary lacks regression tests asserting successful returns and known results for both base 2 and a general base, including under ALT_ECC_SIZE and MULX configurations.

1 open issue
4 resolved since last review

Review effort: Balanced

Comment thread wolfcrypt/src/tfm.c Resolved

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

Critical issues remain: some ECC configurations permit out-of-bounds coordinate accesses, while others use secret-dependent, variable-time multiplication despite ECC timing resistance being enabled.

2 open issues
1 resolved since last review

Review effort: Balanced

Comment thread wolfcrypt/src/tfm.c Resolved
Comment thread wolfcrypt/src/tfm.c Resolved

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

Changes to core cryptographic arithmetic across multiple backends and configurations require specialist review and target-specific correctness and timing validation before approval.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

Use constant time Montgomery reduction and compares in ECC point
operations, ECDH shared secret output and ECDSA signing mod n.
Constant time, fixed length TFM modular exponentiation including base 2.
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.

2 participants