Skip to content

Regenerate sp ecc code and add new test vectors - #12279

Merged
SparkiDev merged 3 commits into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f-sp-ecc
Nov 27, 2026
Merged

SparkiDev merged 3 commits into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f-sp-ecc

Conversation

@yosuke-wolfssl

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

Copy link
Copy Markdown
Contributor

Problem

Three defects in the generated SP ECC code.

Fix (wolfcrypt/src/sp_c32.c, sp_c64.c, and the other SP backends)

The SP C files are regenerated from wolfSSL/scripts#730 except for sp_dsp32.c, one commit per finding.
Closes F-19086, F-19090 and F-19132

Tests

test_wc_ecc_import_x963_off_curve also checks 10 crafted off-curve points that the faulty reduction accepted (P-256/384/521 for F-19090, P-521 for F-19132). It fails on the unpatched --enable-sp and --enable-sp=small builds, at both word sizes.

Verification

  • F-19086: an allocation-failure test (failing each allocation of wc_ecc_verify_hash() in turn) shows the forged signature accepted before the fix and rejected after, on sp_c64.c, sp_c32.c and sp_arm64.c. With the fix it also passes on sp_x86_64.c and, under qemu, sp_arm32.c and sp_riscv64.c. Not added to the suite.
  • Comparing sp_<N>_mod_* against exact arithmetic on 187,785 inputs: P-521 mod p gave 3343 wrong results (c64) and 1174 (c32) before, 0 after. P-256/P-384 mod p and all mod n results were correct before and after.
  • ECC API group, testwolfcrypt and the full unit.test pass for --enable-sp and --enable-sp=small at both word sizes and with FP_ECC + small stack. ASan + UBSan are clean.

Not in this PR

  • sp_dsp32.c already fails to compile for Hexagon with the IDE/HEXAGON settings (5 errors on master). This change adds no new diagnostics.

- sp_<N>_ecc_mulmod_*() in sp_c32.c, sp_c64.c, sp_arm32.c,
  sp_arm64.c, sp_armthumb.c, sp_cortexm.c, sp_riscv64.c and
  sp_x86_64.c (regenerated), and sp_256_ecc_mulmod_10() in
  sp_dsp32.c, checks the stripe table generation result.

Issue: F-19086
- Regenerate WOLFSSL_SP_SMALL variant of sp_<N>_lshift_<W>() in sp_c32.c
  and sp_c64.c.
- tests/api: test_wc_ecc_import_x963_off_curve also expects
  wc_ecc_import_x963_ex() to reject six off-curve P-256, P-384 and
  P-521 points.

Issue: F-19090
- Regenerate sp_256_div_9(), sp_384_div_15() and sp_521_div_21()
  in sp_c32.c, and sp_256_div_5(), sp_384_div_7()
  and sp_521_div_9() in sp_c64.c.
- tests/api: test_wc_ecc_import_x963_off_curve also expects
  wc_ecc_import_x963_ex() to reject four off-curve P-521 points.

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

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 tests leave the allocation-failure path and cache recovery unverified, while the existing sweep discards verification outputs and cannot detect acceptance of a forged signature.

1 open issue
What changed in this PR

Regenerates wolfCrypt’s single-precision (SP) ECC backends to address three defects in cached point multiplication and modular reduction, and adds off-curve regression vectors.

Changes:

  • Propagates table-generation failures and resets cache entries for safe retries across nine backends.
  • Corrects carry handling in shifts and borrow propagation in portable C reductions.
  • Adds ten crafted P-256, P-384, and P-521 off-curve test points.
File Description
wolfcrypt/​src/​sp_x86_64.c Fixes cache-generation error handling, including AVX2 paths.
wolfcrypt/​src/​sp_riscv64.c Fixes cache-generation error handling for RISC-V.
wolfcrypt/​src/​sp_dsp32.c Fixes P-256 cache-generation error handling for DSP.
wolfcrypt/​src/​sp_cortexm.c Fixes cache-generation error handling for Cortex-M.
wolfcrypt/​src/​sp_c64.c Fixes cache errors, shifts, and reduction in portable 64-bit C.
wolfcrypt/​src/​sp_c32.c Fixes cache errors, shifts, and reduction in portable 32-bit C.
wolfcrypt/​src/​sp_armthumb.c Fixes cache-generation error handling for ARM Thumb.
wolfcrypt/​src/​sp_arm64.c Fixes cache-generation error handling for ARM64.
wolfcrypt/​src/​sp_arm32.c Fixes cache-generation error handling for ARM32.
tests/​api/​test_ecc.c Adds ten off-curve import regression vectors.

Review effort: Balanced

Comment thread wolfcrypt/src/sp_c64.c Resolved
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m0plus

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

gcc-arm-cortex-m3

  • FLASH: .text +60 B (+0.0%, 134,258 B / 262,144 B, total: 51% used)

gcc-arm-cortex-m4-baremetal

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

gcc-arm-cortex-m4-min-ecc

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

gcc-arm-cortex-m4-sp-math

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

gcc-arm-cortex-m4-tls12

  • FLASH: .text +64 B (+0.0%, 134,990 B / 262,144 B, total: 51% 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 didn't find any bugs. It changes regenerated SP modular-arithmetic and ECC scalar-multiplication code across nine backends and closes three security findings, so a human with SP generator context should still look at it.

What was reviewed:

  • FP_ECC gen_stripe_table error handling in each sp_<N>_ecc_mulmod_*: on failure cache->cnt resets to 1 and neither mulmod path runs, so the error is returned.
  • The new low-to-high lshift loops with double-width accumulators (SMALL variants, plus the RSA/DH ones in sp_c64.c), and the div normalization that now starts at &t1[i] and moves the top-limb carry by hand.
  • Not done: no build or test run, so the off-curve vectors and the per-arch/asm variants were checked only by reading.
Extended reasoning...

The change touches wolfCrypt's generated SP math files (sp_c32/c64, arm32/arm64/armthumb/cortexm, riscv64, x86_64, dsp32) and adds off-curve point vectors to tests/api/test_ecc.c. This is security-sensitive crypto: ECC point multiplication, modular reduction and division, where a wrong carry can let invalid points or forged signatures through. The bug hunt found nothing, and the fixes look consistent across backends. Still, the change is large, cryptographic, and comes from a generator that isn't in this repo, so it doesn't qualify for automated approval. An inline Copilot thread on sp_c64.c was resolved only by the PR author.

@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 #12279

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-bugs
Not reviewed (no scan target covers them): wolfcrypt/src/sp_arm32.c, wolfcrypt/src/sp_arm64.c, wolfcrypt/src/sp_armthumb.c, wolfcrypt/src/sp_cortexm.c, wolfcrypt/src/sp_dsp32.c, wolfcrypt/src/sp_x86_64.c
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

@SparkiDev
SparkiDev merged commit 24238a0 into wolfSSL:master Nov 27, 2026
452 of 455 checks passed
@yosuke-wolfssl
yosuke-wolfssl deleted the fix/f-sp-ecc branch November 27, 2026 06:28
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