Skip to content

X25519/Ed25519: fix CT for field operations for specific compilers - #12092

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

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

Conversation

@SparkiDev

Copy link
Copy Markdown
Contributor

Description

  • fe_cswap/fe_cmov: put an optimization barrier on the mask so the compiler can't turn the swap into a branch (GCC -O3 on PPC64).
  • 128-bit field mul/sq/sq2: apply the x19/x2 multiples to 64-bit limbs before the products, so every product is a plain signed 64x64->128 multiply. Removes multiplies whose operand is a limb's sign word, which have operand-dependent latency on Cortex-A55. Faster on x86_64 (9-17%). Now the default. NO_CURVED25519_128BIT_FOLDED keeps the original form.
  • Cortex-A55/A53: WOLFSSL_CORTEX_A55/WOLFSSL_CORTEX_A53 turn on WOLFSSL_CURVE25519_FIXED_LATENCY_MUL. That forces the folded form and computes the 32-bit field's small multiples in unsigned arithmetic so Clang can't widen them into 64-bit multiplies. configure sets these when -mcpu/-mtune names cortex-a55 or cortex-a53.

Fixes zd#22787, zd#22796

Testing

Tested PPC64, Cortex-A55, x86_64.
./configure --enable-x25519 --enable-ed25519

- fe_cswap/fe_cmov: put an optimization barrier on the mask so the
  compiler can't turn the swap into a branch (GCC -O3 on PPC64).
- 128-bit field mul/sq/sq2: apply the x19/x2 multiples to 64-bit limbs
  before the products, so every product is a plain signed 64x64->128
  multiply. Removes multiplies whose operand is a limb's sign word, which
  have operand-dependent latency on Cortex-A55. Faster on x86_64 (9-17%).
  Now the default. NO_CURVED25519_128BIT_FOLDED keeps the original form.
- Cortex-A55/A53: WOLFSSL_CORTEX_A55/WOLFSSL_CORTEX_A53 turn on
  WOLFSSL_CURVE25519_FIXED_LATENCY_MUL. That forces the folded form and
  computes the 32-bit field's small multiples in unsigned arithmetic so
  Clang can't widen them into 64-bit multiplies. configure sets these
  when -mcpu/-mtune names cortex-a55 or cortex-a53.
@SparkiDev SparkiDev self-assigned this Nov 11, 2026
Copilot AI balanced review requested due to automatic review settings November 11, 2026 08:00
@SparkiDev

Copy link
Copy Markdown
Contributor Author

Code generated with PR:
https://github.com/wolfSSL/scripts/pull/713

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 detection omits EXTRA_CFLAGS and EXTRA_CPPFLAGS, allowing Cortex-A55/A53 builds to select the 32-bit field implementation without enabling its timing mitigation.

1 open issue
What changed in this PR

This PR hardens wolfCrypt’s X25519/Ed25519 field operations against compiler-generated timing variations, while improving 128-bit arithmetic performance.

Changes:

  • Add optimization barriers to conditional swaps and moves.
  • Make folded 128-bit multiplication and squaring the default, with an opt-out.
  • Add Cortex-A55/A53 detection and fixed-latency multiplication safeguards.
File Description
wolfcrypt/​src/​fe_x25519_128.h Add mask barriers and folded multiplication/squaring implementations.
wolfcrypt/​src/​fe_operations.c Define hardening controls and protect 32-bit small-multiple arithmetic.
configure.ac Detect Cortex-A55/A53 targets to enable timing mitigations.

Review effort: Balanced

Comment thread configure.ac
# fixed latency multiply code for the Curve25519/Ed25519 field operations when
# building for these cores (-mcpu/-mtune, including big.LITTLE e.g.
# cortex-a76.cortex-a55).
case "$CC $CFLAGS $CPPFLAGS" in
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +20 B, text +10,016 B (+3.6%, 292,508 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.

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