Skip to content

SAKKE: constant time operations on secrets, optional RNG blinding - #12120

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

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

Conversation

@SparkiDev

Copy link
Copy Markdown
Contributor

Description

  • Without an RNG, operations on secrets (z_T, RSK, r) are constant time: reduction, inversion, field add/sub, ModExp and point mapping.
  • New wc_SetSakkeRng(): blinding allows faster algorithms to be used. wc_MakeSakkeRsk() uses it rather than instantiating an RNG per call.
  • Table lookups cache resistant unless WC_NO_CACHE_RESISTANT.
  • SP P1024: constant time lookups (AVX2 on x86_64), table multiply and ModExp; fix AVX2 entry over-read, add vzeroupper; fix p1024_order guard for NO_ECC_CHECK_KEY builds.
  • ecc_map_ex() constant time branch uses constant time reduction.
  • Fix zMont not reset when making or importing a key.
  • Tests and docs for wc_SetSakkeRng().

Fixes zd#22803

Testing

./configure --enable-sakke

@SparkiDev SparkiDev self-assigned this Nov 13, 2026
Copilot AI balanced review requested due to automatic review settings November 13, 2026 01:05

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.

🔵 Needs a closer look

Security-sensitive constant-time arithmetic changes across ECC, six SP backends, and assembly implementations require specialist cryptographic review and target-specific correctness and side-channel validation beyond source inspection.

No open issues 🎉

What changed in this PR

This PR strengthens side-channel protection in wolfCrypt’s SAKKE implementation through constant-time arithmetic and cache-resistant lookups, with an optional RNG interface for blinding.

Changes:

  • Harden secret-dependent P1024 operations across supported backends and use constant-time reduction in ECC point mapping.
  • Add the wc_SetSakkeRng() interface, documentation, and tests for blinded operations.
  • Correct x86 AVX2 table-read bounds, add vector-state cleanup, and adjust P1024 guards and key-generation buffer sizing.
File Description
wolfssl/​wolfcrypt/​sakke.h Declare the RNG setter and store a non-owned RNG pointer.
wolfssl/​wolfcrypt/​ecc.h Allow extra random bytes to reduce key-generation bias.
wolfcrypt/​test/​test.c Test RNG setter arguments and repeat SAKKE operations with blinding.
wolfcrypt/​src/​sp_x86_64.c Harden P1024 lookups, multiplication, and exponentiation.
wolfcrypt/​src/​sp_x86_64_asm.S Add AVX2 lookups, fix read bounds, and clear upper vector state.
wolfcrypt/​src/​sp_x86_64_asm.asm Apply equivalent AVX2 changes to Windows assembly.
wolfcrypt/​src/​sp_riscv64.c Harden RISC-V P1024 lookups and secret exponentiation.
wolfcrypt/​src/​sp_c64.c Harden portable 64-bit P1024 operations.
wolfcrypt/​src/​sp_c32.c Harden portable 32-bit P1024 operations.
wolfcrypt/​src/​sp_armthumb.c Harden ARM Thumb P1024 operations.
wolfcrypt/​src/​sp_arm64.c Harden ARM64 P1024 operations.
wolfcrypt/​src/​ecc.c Keep constant-time point mapping in Montgomery form until final conversion.
doc/​dox_comments/​header_files/​sakke.h Document RNG configuration, ownership, and intended performance benefits.
doc/​dox_comments/​header_files/​doxygen_groups.h Recommend RNG setup in SAKKE usage flows.

Review effort: Balanced

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m0plus

  • FLASH: .text +16 B (+0.0%, 72,500 B / 262,144 B, total: 28% used)

gcc-arm-cortex-m3

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

gcc-arm-cortex-m4

  • FLASH: .text +960 B (+0.5%, 212,589 B / 262,144 B, total: 81% 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-crypto-only

  • FLASH: .text +128 B (+0.1%, 184,272 B / 262,144 B, total: 70% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +640 B (+0.3%, 200,164 B / 1,048,576 B, total: 19% 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-openssl-compat

  • FLASH: .text +640 B (+0.1%, 804,872 B / 1,048,576 B, total: 77% used)

gcc-arm-cortex-m4-pkcs7

  • FLASH: .text +704 B (+0.3%, 226,513 B / 262,144 B, total: 86% used)

gcc-arm-cortex-m4-pq

  • FLASH: .text +640 B (+0.2%, 319,024 B / 1,048,576 B, total: 30% 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)

gcc-arm-cortex-m4-tls13

  • FLASH: .text +640 B (+0.3%, 255,367 B / 262,144 B, total: 97% used)

gcc-arm-cortex-m7

  • FLASH: .text +960 B (+0.5%, 212,589 B / 262,144 B, total: 81% used)

gcc-arm-cortex-m7-pq

  • FLASH: .text +640 B (+0.2%, 319,856 B / 1,048,576 B, total: 31% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .text +640 B (+0.3%, 255,367 B / 262,144 B, total: 97% used)

stm32-sim-stm32h753

  • FLASH: .text +512 B (+0.2%, 210,604 B / 2,097,152 B, total: 10% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +108 B, text +13,880 B (+5.0%, 296,460 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.

@SparkiDev
SparkiDev force-pushed the sakke_encap_ct branch 2 times, most recently from f121bbe to 05cff92 Compare November 13, 2026 01:30
@SparkiDev
SparkiDev requested a balanced review from Copilot November 13, 2026 01:30

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.

🔵 Needs a closer look

Constant-time changes across five arithmetic backends and two assembly formats require human security review and target-specific correctness and side-channel validation beyond the supplied configure-only testing.

No open issues 🎉

Review effort: Balanced

@SparkiDev
SparkiDev force-pushed the sakke_encap_ct branch 3 times, most recently from 6a7bfe1 to e039009 Compare November 13, 2026 06:16
@SparkiDev
SparkiDev force-pushed the sakke_encap_ct branch 2 times, most recently from e4a326d to ac74e92 Compare November 23, 2026 05:30
@SparkiDev

Copy link
Copy Markdown
Contributor Author

Jenkins: retest this please

@SparkiDev
SparkiDev force-pushed the sakke_encap_ct branch 3 times, most recently from d88b9a8 to 7423f01 Compare November 25, 2026 06:44
@SparkiDev

Copy link
Copy Markdown
Contributor Author

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

@SparkiDev

SparkiDev commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Jenkins: retest this please

1 similar comment
@SparkiDev

Copy link
Copy Markdown
Contributor Author

Jenkins: retest this please

- Without an RNG, operations on secrets (z_T, RSK, r) are constant time:
  reduction, inversion, field add/sub, ModExp and point mapping.
- New wc_SetSakkeRng(): blinding allows faster algorithms to be used.
  wc_MakeSakkeRsk() uses it rather than instantiating an RNG per call.
  Blinding values are generated by SAKKE rather than wc_ecc_gen_k() so the
  ECC random buffer size (ECC_MAXSIZE_GEN in FIPS v6) doesn't limit them.
- Table lookups cache resistant unless WC_NO_CACHE_RESISTANT.
- SP P1024: constant time lookups (AVX2 on x86_64), table multiply and
  ModExp; fix AVX2 entry over-read, add vzeroupper; fix p1024_order
  guard for NO_ECC_CHECK_KEY builds.
- ecc_map_ex() constant time branch uses constant time reduction.
- ECC generic projective add/double: constant time Montgomery reduction
  and point equality check when timing resistant. Public only operations
  (ecc_mul2add(), verify) keep the faster reduction.
- sp_int: sp_invmod_mont_ct() reductions are constant time; cheaper
  constant time final subtraction and input masking in Montgomery
  reduction.
- TFM: honor the constant time flag of Montgomery reduction (plain
  reduction unchanged unless WOLFSSL_MONT_RED_CT), fixed length carry
  propagation, constant time clamp, fp_invmod_mont_ct() constant time
  reductions; fix fp_submod_ct() leaving a stale top digit.
- ECCSI signing: r * SSK, HE + r * SSK, inversion and multiply by j are
  constant time (Fermat inversion in Montgomery form) with TFM and
  SP math all; other math builds keep blinded inversion.
- Public only operations use the faster multiplication: ECCSI verify
  (non-SP curves) computes Y and J with ecc_mul2add(), and the public key
  order check uses ecc_mul2add() (uncached when FP_ECC).
- Regenerate SP x86_64 assembly (32-bit parameters zero extended).
- SP Karatsuba squaring (ARM, RISC-V): |a_lo - a_hi| by constant time
  negation rather than selecting operand pointers by the secret sign.
- SP point add: doubling check and infinity masks read through volatiles
  so the compiler cannot branch on their parts.
- Tests and docs for wc_SetSakkeRng(); white-box tests updated for
  changed static helpers.

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

Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-src, wolfssl-bugs
Not reviewed (no scan target covers them): .github/configs/os-check-linux.json, doc/ASM_AND_MATH_DEFINES.md, doc/dox_comments/header_files/doxygen_groups.h, doc/dox_comments/header_files/sakke.h, wolfcrypt/src/sp_arm32.c, wolfcrypt/src/sp_arm64.c, wolfcrypt/src/sp_armthumb.c, wolfcrypt/src/sp_cortexm.c, wolfcrypt/src/sp_x86_64.c, wolfcrypt/src/sp_x86_64_asm.S, wolfcrypt/src/sp_x86_64_asm.asm
Coverage: 12 of 14 in-scope changed file(s) opened by the reviewer; not opened: tests/unit-mcdc/test_sakke_fault_whitebox.c, tests/unit-mcdc/test_sakke_whitebox.c

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Review tier: Max

Comment thread wolfcrypt/src/sp_int.c
#ifndef WOLFSSL_NO_CT_OPS
if (ct == 1) {
/* Zero digits at and above used without depending on used. */
sp_int_digit aUsed = (sp_int_digit)a->used;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_sp_mont_red constant-time input mask truncates used and the digit index to 8 bits when SP_WORD_SIZE=8 · Logic errors

With SP_WORD_SIZE=8, sp_int_digit is 8 bits, so casting a->used and i to it wraps both values at 256. For moduli of 1024 bits or more, the ct path then clears digits that hold data and keeps stale ones. This corrupts mp_montgomery_reduce_ct (RSA blinding removal, SAKKE) and sp_invmod_mont_ct; the replaced ctMaskIntGTE compared at int width.

Suggested fix: Compare i with a->used at unsigned int width, as the replaced ctMaskIntGTE call did, and only then narrow the resulting 0/1 to a full digit mask.
Basis: C11 6.3.1.3p2: converting a value to an unsigned integer type reduces it modulo one more than the type's maximum value, so an 8-bit sp_int_digit cannot hold indices of 256 or more.

Related known findings (similar but distinct; listed for context, not part of this finding)

  • F-16439: that finding is _sp_cond_copy applying an 8-bit limb mask to the used field; this one is the ct input masking in _sp_mont_red, which compares a truncated index with a truncated used.

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