Skip to content

Fix the FP ECC order for oversized scalars and wipe SP modexp, invmod and division scratch - #12417

Open
dgarske wants to merge 1 commit into
wolfSSL:masterfrom
dgarske:ecc_bignum
Open

dgarske wants to merge 1 commit into
wolfSSL:masterfrom
dgarske:ecc_bignum

Conversation

@dgarske

@dgarske dgarske commented Dec 4, 2026

Copy link
Copy Markdown
Member

Description

  • accel_fp_mul() and accel_fp_mul2add() reduce a scalar wider than the modulus by the curve order, which they looked up as the first ecc_sets entry of the same size. A stale if (y == 66) --x back-off then selected the previous curve's order for P-521, and same-size curves such as secp256k1 and the Brainpool curves got P-256's order, giving a wrong point. A new fp_ecc_get_order() matches the curve by its prime. It returns ECC_BAD_ARG_E when no curve matches or when two compiled-in curves share the prime but have different orders (some opt-in SECG and X9.62 curves). (F-19394)
  • sp_int.c freed the scratch of its exponentiation, inversion, gcd and lcm functions without wiping it: the RSA-CRT half result and Montgomery base, and inverses of private values. _sp_exptmod_ex(), _sp_exptmod_mont_ex(), _sp_exptmod_nct(), _sp_exptmod_base_2*(), _sp_invmod*(), _sp_gcd() and _sp_lcm() now wipe it first, with new FREE_*_ZERO macros that take the length from the allocation. (F-17660)
  • The generated SP code (all backends) now wipes the modexp window tables, the division scratch holding the shifted divisor (a private prime), the whole RSA/DH exponentiation buffer including the result, and the non-CRT RSA private workspace holding d. Generator change: wolfSSL/scripts#739. (F-14668)
  • fp_cond_swap_ct_ex() in tfm.c swapped used and the digits but not sign, unlike integer.c and sp_int.c. (F-9976)

Resolves F-19394, F-17660, F-14668, F-9976.

Testing

New tests in tests/api: a P-521 FP ECC multiply-add with oversized scalars checked against the expected point, a whitebox check that the exptmod, base-2 exptmod, non-constant-time exptmod and invmod scratch is wiped when freed (sp-math-all with smallstack), and a conditional swap of signed values. Each fails without its fix. The generated SP wipes aren't observable from a test.

  • --enable-all, FP ECC with SP math and with fastmath, sp-math-all with smallstack (with and without WC_NO_CACHE_RESISTANT), SP C 64-bit (stack, smallstack, SP_RSA_PRIVATE_EXP_D) and SP_WORD_SIZE=32, and x86_64 SP assembly, with ASan and -Werror.
  • sp_arm32.c, sp_arm64.c, sp_cortexm.c and sp_armthumb.c cross-compiled with -Werror; sp_riscv64.c not compiled.

@dgarske dgarske self-assigned this Dec 4, 2026
Copilot AI balanced review requested due to automatic review settings December 4, 2026 23:22
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m0plus

  • FLASH: .text +56 B (+0.1%, 72,748 B / 262,144 B, total: 28% used)

gcc-arm-cortex-m3

  • FLASH: .text +64 B (+0.0%, 135,330 B / 262,144 B, total: 52% used)

gcc-arm-cortex-m4

  • FLASH: .text +64 B (+0.0%, 214,360 B / 262,144 B, total: 82% used)

gcc-arm-cortex-m4-baremetal

  • FLASH: .text +64 B (+0.1%, 75,508 B / 262,144 B, total: 29% used)

gcc-arm-cortex-m4-crypto-only

  • FLASH: .text +192 B (+0.1%, 185,211 B / 262,144 B, total: 71% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +192 B (+0.1%, 201,828 B / 1,048,576 B, total: 19% used)

gcc-arm-cortex-m4-min-ecc

  • FLASH: .text +64 B (+0.1%, 69,974 B / 262,144 B, total: 27% used)

gcc-arm-cortex-m4-openssl-compat

  • FLASH: .text +256 B (+0.0%, 808,296 B / 1,048,576 B, total: 77% used)

gcc-arm-cortex-m4-pkcs7

  • FLASH: .text +256 B (+0.1%, 227,644 B / 262,144 B, total: 87% used)

gcc-arm-cortex-m4-pq

  • FLASH: .text +256 B (+0.1%, 320,544 B / 1,048,576 B, total: 31% used)

gcc-arm-cortex-m4-rsa-only

  • FLASH: .text +320 B (+0.1%, 350,296 B / 1,048,576 B, total: 33% used)

gcc-arm-cortex-m4-sp-math

  • FLASH: .text +64 B (+0.1%, 69,974 B / 262,144 B, total: 27% used)

gcc-arm-cortex-m4-tls12

  • FLASH: .text +64 B (+0.0%, 136,078 B / 262,144 B, total: 52% used)

gcc-arm-cortex-m4-tls13

  • FLASH: .text +256 B (+0.1%, 257,010 B / 262,144 B, total: 98% used)

gcc-arm-cortex-m7

  • FLASH: .text +64 B (+0.0%, 214,360 B / 262,144 B, total: 82% used)

gcc-arm-cortex-m7-pq

  • FLASH: .text +256 B (+0.1%, 321,376 B / 1,048,576 B, total: 31% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .text +256 B (+0.1%, 257,010 B / 262,144 B, total: 98% used)

stm32-sim-stm32h753

  • FLASH: .text +768 B (+0.3%, 222,648 B / 2,097,152 B, total: 11% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +140 B, text +14,672 B (+5.2%, 297,284 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

Signed SP math configurations, such as OPENSSL_EXTRA, now fail to link unit.test when WC_NO_CACHE_RESISTANT is defined or ECC_TIMING_RESISTANT is absent: the test is enabled although sp_cond_swap_ct is unavailable. Separately, the P-521 regression does not cover same-size curve selection or ambiguous-prime rejection, leaving the other order-selection fix and new rejection behavior unprotected.

2 open issues
What changed in this PR

This PR corrects elliptic-curve arithmetic and strengthens sensitive-data cleanup in wolfCrypt’s math implementations.

Changes:

  • Select curve orders by prime when reducing oversized FP ECC scalars.
  • Wipe temporary arithmetic buffers across SP backends.
  • Preserve signs during conditional swaps and add regression tests.
File Description
wolfcrypt/​src/​tfm.c Swap signs alongside integer values.
wolfcrypt/​src/​sp_x86_64.c Wipe x86-64 arithmetic scratch.
wolfcrypt/​src/​sp_riscv64.c Wipe RISC-V arithmetic scratch.
wolfcrypt/​src/​sp_int.c Add zeroizing cleanup for integer temporaries.
wolfcrypt/​src/​sp_cortexm.c Wipe Cortex-M arithmetic scratch.
wolfcrypt/​src/​sp_c64.c Wipe portable 64-bit arithmetic scratch.
wolfcrypt/​src/​sp_c32.c Wipe portable 32-bit arithmetic scratch.
wolfcrypt/​src/​sp_armthumb.c Wipe ARM Thumb arithmetic scratch.
wolfcrypt/​src/​sp_arm64.c Wipe ARM64 arithmetic scratch.
wolfcrypt/​src/​sp_arm32.c Wipe ARM32 arithmetic scratch.
wolfcrypt/​src/​ecc.c Match curve orders by prime; reject ambiguity.
tests/​api/​test_wolfmath.h Register math regression tests.
tests/​api/​test_wolfmath.c Test signed swaps and scratch cleanup.
tests/​api/​test_ecc.h Register oversized-scalar regression.
tests/​api/​test_ecc.c Test P-521 multiply-add with oversized scalars.

Review effort: Balanced

Comment thread tests/api/test_wolfmath.c Resolved
Comment thread wolfcrypt/src/ecc.c Resolved
@dgarske

dgarske commented Dec 5, 2026

Copy link
Copy Markdown
Member Author

retest this please

@dgarske

dgarske commented Dec 5, 2026

Copy link
Copy Markdown
Member Author

retest this please

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