Skip to content

Size ECC curve parameters for the largest curve with SP math - #12042

Open
Frauschi wants to merge 1 commit into
wolfSSL:masterfrom
Frauschi:ecc_spec_ints_sp_math
Open

Frauschi wants to merge 1 commit into
wolfSSL:masterfrom
Frauschi:ecc_spec_ints_sp_math

Conversation

@Frauschi

@Frauschi Frauschi commented Nov 6, 2026

Copy link
Copy Markdown
Member

Description

With WOLFSSL_SP_MATH, DECLARE_CURVE_SPECS keeps up to six full SP_INT_BITS mp_ints on the stack, so an RSA-enabled build needs 7.5 KB of stack to verify a P-256 signature. This extends the sized curve-parameter layout sp-math-all already uses to WOLFSSL_SP_MATH.

Stack usage in bytes with --enable-sp-math --enable-sp on aarch64:

Function Before After
wc_ecc_verify_hash_ex 7584 1424
wc_ecc_make_pub_ex 6384 1104
wc_ecc_point_is_on_curve 3264 624

Changes

  • Size curve-spec mp_ints for the smaller of MAX_ECC_BITS_USE and SP_INT_BITS in both sp_int builds
  • Back the stack buffer with an sp_int_digit array so the carved-out mp_ints are aligned
  • Leave MAX_ECC_BITS_USE unchanged, so sign and verify key size checks behave as before
  • Stack use in ECC-only, sp-math-all and small-stack builds is unchanged

Testing

  • No new tests; existing ECC tests in testwolfcrypt and unit.test cover the changed path
  • multi-arch CI *-o2-sp-math legs (arm64, armhf, armel) build sp-math and run testwolfcrypt

Copilot AI balanced review requested due to automatic review settings November 6, 2026 16:06

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.

🟢 Approval recommended

No active review issues remain for the change reducing ECC curve-parameter stack usage in SP math builds.

No open issues 🎉

What changed in this PR

Reduces ECC curve-parameter stack usage in SP math builds by sizing temporary integers for the largest supported curve rather than the full SP integer capacity.

Changes:

  • Extends sized curve-parameter storage to WOLFSSL_SP_MATH, capped at SP_INT_BITS.
  • Uses an sp_int_digit array to align stack storage.
  • Preserves MAX_ECC_BITS_USE and existing key-size checks.
File Description
wolfcrypt/​src/​ecc.c Updates curve-parameter allocation, alignment, and initialization for sized SP integers.

Review effort: Balanced

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m3

  • FLASH: .text +4 B (+0.0%, 131,681 B / 262,144 B, total: 50% used)

zephyr-frdm_rw612

  • FLASH: log_const_area -8 B, rodata -8 B, text +1,536 B (+0.5%, 283,984 B / 67,108,864 B, total: 0% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@Frauschi

Frauschi commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

@claude review

@Frauschi Frauschi self-assigned this Nov 6, 2026

@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 how ECC curve-parameter mp_ints are laid out in memory in WOLFSSL_SP_MATH builds, which is core crypto code, so a maintainer should still take a look.

What was reviewed:

  • Per-slot stride and alignment. MP_INT_SIZEOF is sizeof(sp_int_minimal) plus whole digits, so each slot stays a multiple of the digit size. That matches the MP_INT_SIZEOF_DIGITS stack array and the XMALLOC size.
  • Sizing. MP_BITS_CNT(SP_INT_BITS) equals SP_INT_DIGITS, so capping at SP_INT_BITS keeps sp_init_size within its size > SP_INT_DIGITS check. In SP_MATH builds this is the same capacity mp_init gave before.
  • SP_MATH-only builds. MP_INT_SIZEOF, MP_INT_SIZEOF_DIGITS, MP_BITS_CNT and the mp_init_size -> sp_init_size mapping are defined unconditionally in sp_int.h.
Extended reasoning...

The change touches only wolfcrypt/src/ecc.c. It extends the sized curve-spec storage that sp-math-all builds already use to WOLFSSL_SP_MATH builds, and changes the stack buffer to an sp_int_digit array. It is core crypto code, and the right memory layout depends on build-configuration macros (SP_INT_BITS, MAX_ECC_BITS_USE, WOLFSSL_SMALL_STACK), so a mismatch could compile cleanly and still corrupt memory at runtime. The stride, alignment and size checks I ran all hold. Because this changes memory layout in crypto paths across several build configurations, a maintainer should still look before merge rather than relying on an automated approval.

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

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs
Coverage: 1 of 1 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

DECLARE_CURVE_SPECS keeps up to six curve parameters on the stack. With
WOLFSSL_SP_MATH_ALL they are sized for the largest ECC curve, but with
WOLFSSL_SP_MATH they are full mp_ints of SP_INT_BITS, which RSA makes
3072 or 4096 bits. wc_ecc_verify_hash_ex() then needs 7.5 KB of stack
for a P-256 signature even though the SP code it calls never touches
the parameters.

Use the sized layout for both sp_int builds, with the digit count for
MAX_ECC_BITS_USE capped at SP_INT_DIGITS, so a build that defines its
own SP_INT_DIGITS still loads its curves. MAX_ECC_BITS_USE itself is
unchanged, so the key size checks in sign and verify behave as before.
The stack buffer is now an sp_int_digit array, so the mp_ints carved
out of it are aligned.

With --enable-sp-math --enable-sp on aarch64:
- wc_ecc_verify_hash_ex: 7584 -> 1424 bytes
- wc_ecc_make_pub_ex: 6384 -> 1104 bytes
- wc_ecc_point_is_on_curve: 3264 -> 624 bytes

Stack use in ECC-only, sp-math-all and small-stack builds is unchanged.
@Frauschi
Frauschi force-pushed the ecc_spec_ints_sp_math branch from 3c0e544 to 4aae476 Compare November 6, 2026 19:46
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