Skip to content

Regenerate SP sources: constant-time sp_*_from_mp#10898

Open
danielinux wants to merge 1 commit into
wolfSSL:masterfrom
danielinux:sp-from-mp-const-time
Open

Regenerate SP sources: constant-time sp_*_from_mp#10898
danielinux wants to merge 1 commit into
wolfSSL:masterfrom
danielinux:sp-from-mp-const-time

Conversation

@danielinux

Copy link
Copy Markdown
Member

Description

Regenerated the single-precision C sources so that sp__from_mp() converts secret inputs (ECDH/ECDSA private keys and nonces) in constant time: a fixed-count loop bounded by the output size with masked reads at/after a->used, instead of looping a->used times (which leaked the value's magnitude through the executed-instruction count).

Testing

Fixes the ct-callgrind constant-time failures for P-521 (ec_p521_kex, ec_p521_sign) and hardens the same pattern across all curves and key sizes (108 sp_*_from_mp functions in sp_c32/sp_c64, sp_arm32/sp_arm64/sp_armthumb/ sp_cortexm and sp_x86_64).

Verified (--enable-sp, gcc 15.2): ec_p521_kex diff 156 -> 0, ec_p521_sign diff 367 -> 55 (tol 300); testwolfcrypt RSA/ECC KATs pass.

Generated by https://github.com/wolfSSL/scripts/pull/626

Copilot AI review requested due to automatic review settings July 14, 2026 09:45

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.

Pull request overview

This PR regenerates the single-precision (“sp”) implementation sources to make sp_<N>_from_mp() convert secret mp_int inputs in constant time by iterating a fixed amount (bounded by output size) and masking digits at/after a->used.

Changes:

  • Regenerated multiple sp_*_from_mp() implementations to avoid looping based on a->used (magnitude-dependent timing).
  • Added branchless masking logic to zero out digits beyond a->used during conversion.
  • Applied the same pattern across several architecture-specific and generic SP backends.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
wolfcrypt/src/sp_x86_64.c Regenerated constant-time masking conversion for sp_*_from_mp() in SP x86_64 backend.
wolfcrypt/src/sp_cortexm.c Regenerated constant-time masking conversion for sp_*_from_mp() in Cortex-M backend.
wolfcrypt/src/sp_c64.c Regenerated constant-time masking conversion for sp_*_from_mp() in 64-bit C backend.
wolfcrypt/src/sp_c32.c Regenerated constant-time masking conversion for sp_*_from_mp() in 32-bit C backend.
wolfcrypt/src/sp_armthumb.c Regenerated constant-time masking conversion for sp_*_from_mp() in ARM Thumb backend.
wolfcrypt/src/sp_arm64.c Regenerated constant-time masking conversion for sp_*_from_mp() in ARM64 backend.
wolfcrypt/src/sp_arm32.c Regenerated constant-time masking conversion for sp_*_from_mp() in ARM32 backend.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread wolfcrypt/src/sp_c32.c Outdated
Comment thread wolfcrypt/src/sp_c64.c Outdated
Comment thread wolfcrypt/src/sp_x86_64.c Outdated
Comment thread wolfcrypt/src/sp_arm64.c Outdated
Comment thread wolfcrypt/src/sp_arm32.c Outdated
Comment thread wolfcrypt/src/sp_armthumb.c Outdated
Comment thread wolfcrypt/src/sp_cortexm.c Outdated
@github-actions

github-actions Bot commented Jul 14, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

gcc-arm-cortex-m0plus

  • FLASH: .text +16 B (+0.0%, 64,199 B / 262,144 B, total: 24% used)

gcc-arm-cortex-m3

  • FLASH: .text +12 B (+0.0%, 122,517 B / 262,144 B, total: 47% used)

gcc-arm-cortex-m4-rsa-only

@danielinux danielinux self-assigned this Jul 14, 2026
danielinux added a commit to danielinux/wolfssl that referenced this pull request Jul 14, 2026
Regenerated with the updated generator so sp_<N>_from_mp() clamps the read
index to a valid digit (0 when a->used == 0), avoiding an out-of-bounds or
NULL digit read in the fixed-count constant-time loop while keeping the
conversion constant time.

Addresses the Copilot review comments on this PR (wolfSSL#10898) and
on wolfSSL/scripts#626. Verified (--enable-sp, gcc 15.2): ec_p521_kex diff 0,
ec_p521_sign diff 55; make check passes.

Generated by wolfSSL/scripts#626
@danielinux danielinux assigned wolfSSL-Bot and unassigned danielinux Jul 14, 2026
@danielinux
danielinux force-pushed the sp-from-mp-const-time branch from fe810aa to 0046469 Compare July 14, 2026 12:08
Frauschi
Frauschi previously approved these changes Jul 14, 2026

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

LGTM

@danielinux

Copy link
Copy Markdown
Member Author

Jenkins retest this please

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

Scan targets checked: wolfcrypt-bugs, wolfcrypt-src

No new issues found in the changed files. ✅

@danielinux

Copy link
Copy Markdown
Member Author

Jenkins retest this please

@danielinux
danielinux force-pushed the sp-from-mp-const-time branch 2 times, most recently from e14b237 to 4c36537 Compare July 17, 2026 06:57
Regenerated the single-precision C sources so that sp_<N>_from_mp() converts
secret inputs (ECDH/ECDSA private keys and nonces) in constant time: a
fixed-count loop bounded by the output size with masked reads at/after
a->used, instead of looping a->used times (which leaked the value's
magnitude through the executed-instruction count).

Fixes the ct-callgrind constant-time failures for P-521 (ec_p521_kex,
ec_p521_sign) and hardens the same pattern across all curves and key sizes
(108 sp_*_from_mp functions in sp_c32/sp_c64, sp_arm32/sp_arm64/sp_armthumb/
sp_cortexm and sp_x86_64).

Verified (--enable-sp, gcc 15.2): ec_p521_kex diff 156 -> 0, ec_p521_sign
diff 367 -> 55 (tol 300); testwolfcrypt RSA/ECC KATs pass.

Generated by wolfSSL/scripts#626
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.

6 participants