Skip to content

AES-GCM: add GCM_INT_MUL GHASH and make it default - #12089

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

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

Conversation

@SparkiDev

@SparkiDev SparkiDev commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Description

Table-free GHASH multiply using integer multiplies on operands masked to
every fourth bit. Variants: 64x64 multiplies on 64-bit CPUs, 32x32->64
on 32-bit CPUs, and 32x32->32 only (GCM_INT_MUL_32_ONLY, default on
M-profile Arm without DSP, e.g. Cortex-M0/M3). The key prepared from H
is stored in Gcm.M0, sized for the variant.

aes.h now picks the GHASH implementation when none is chosen:

  • GCM_TABLE_4BIT when cache resistance is off (WC_NO_CACHE_RESISTANT,
    e.g. --disable-harden, or the new AES_GCM_GMULT_NO_CACHE_RESIST) and
    for Arm/PPC/RISC-V asm builds.
  • GCM_SMALL where the CPU multiply may take data-dependent time
    (WC_GCM_MUL_VAR_TIME): Armv4/Armv5, MIPS, 32-bit PowerPC and RISC-V
    without Zkt.
  • GCM_INT_MUL otherwise. Choosing it explicitly asserts the CPU
    multiply is constant time.

configure and CMake compile aes.h to get its choice and record it in
options.h, as the Gcm layout depends on it and the CPU macros depend on
flags like -march.

Arm/PPC asm GCM_SMALL bulk GHASH helpers no longer branch on H.

AES-GCM-SIV POLYVAL uses the GCM_INT_MUL multiply when it is compiled.

configure/CMake: add --enable-aesgcm=intmul. FIPS before v7 keeps
GCM_TABLE_4BIT. CMake no longer forces 4bit over an explicit choice on
little-endian, and WOLFSSL_HARDEN=no defines WC_NO_CACHE_RESISTANT as
configure does.

mcapi: CRYPT_AES_CTX grows to fit the larger Aes.

Fixes zd#22771

Testing

./configure --enable-aes-gcm=4bit
./configure --enable-aes-gcm=word32
./configure --enable-aes-gcm=small
./configure --enable-aes-gcm=intmul

@SparkiDev SparkiDev self-assigned this Nov 11, 2026
Copilot AI balanced review requested due to automatic review settings November 11, 2026 02:17
@SparkiDev
SparkiDev force-pushed the aes_gcm_ghash_int_mul branch from 53ba1fc to 5a7a1ce Compare November 11, 2026 02:21
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m4-crypto-only

  • FLASH: .text +448 B (+0.2%, 184,592 B / 262,144 B, total: 70% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +108 B, text +14,184 B (+5.1%, 296,764 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.

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

Critical GHASH defects produce incorrect authentication tags on supported 16-bit-byte targets, while the missing generated-header selector can create incompatible library and application layouts.

3 open issues
What changed in this PR

Adds a table-free integer-multiply GHASH implementation to wolfCrypt and makes it the default for cache-resistant software AES-GCM configurations.

Changes:

  • Implements GCM_INT_MUL for GHASH and reuses it for AES-GCM-SIV POLYVAL.
  • Replaces secret-dependent branches with masks in cache-resistant GCM_SMALL and GCM_WORD32 multiplication.
  • Adds build-system selection and benchmark labeling for intmul, while preserving assembly and FIPS defaults.
File Description
wolfssl/​wolfcrypt/​aes.h Selects the default GHASH implementation and defines cache-resistance controls.
wolfcrypt/​src/​aes.c Implements integer-multiply GHASH, POLYVAL reuse, and branch-free masking.
wolfcrypt/​benchmark/​benchmark.c Labels intmul GMAC benchmark results.
configure.ac Adds intmul selection and delegates non-FIPS defaults to aes.h.
CMakeLists.txt Adds intmul selection and preserves explicit GHASH choices.

Review effort: Balanced

Comment thread CMakeLists.txt Resolved
Comment thread wolfcrypt/src/aes.c Resolved
Comment thread wolfcrypt/src/aes.c Resolved
@SparkiDev
SparkiDev force-pushed the aes_gcm_ghash_int_mul branch 3 times, most recently from bf1ac32 to 81203b1 Compare November 11, 2026 07:22
@SparkiDev
SparkiDev requested a balanced review from Copilot November 11, 2026 07:22
@SparkiDev

Copy link
Copy Markdown
Contributor Author

@claude review

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.

Comment thread wolfcrypt/src/aes.c Resolved
Comment thread wolfcrypt/src/aes.c Resolved
Comment thread wolfcrypt/src/aes.c Resolved
Comment thread wolfcrypt/src/aes.c Resolved
Comment thread wolfcrypt/src/aes.c Resolved
Comment thread CMakeLists.txt Resolved
Comment thread wolfcrypt/src/aes.c Resolved
@SparkiDev
SparkiDev force-pushed the aes_gcm_ghash_int_mul branch from 81203b1 to cd9ee4e Compare November 11, 2026 08:02
@SparkiDev

Copy link
Copy Markdown
Contributor Author

Jenkins: retest this please

@SparkiDev
SparkiDev force-pushed the aes_gcm_ghash_int_mul branch 3 times, most recently from 3066b57 to 5f09d71 Compare November 12, 2026 06:39
@Frauschi

Copy link
Copy Markdown
Member

GCM_INT_MUL is now the default on cores whose multiply can be variable time: MIPS32 4K/M4K, PowerPC 750/7400 (mullw early-out) and Cortex-M3 under armcc (which defines __TARGET_ARCH_7_M, not ARM_ARCH_7M); the masked GCM_SMALL may be the safer default there.

@Frauschi

Copy link
Copy Markdown
Member

The default GHASH now depends on -march (__ARM_ARCH, __riscv_zkt) and changes the Gcm layout (M0 only with GCM_INT_MUL), so an app built for rv64gc against a library built for rv64gc_zkt disagrees on sizeof(Aes). Could the choice be recorded in options.h?

@SparkiDev SparkiDev assigned wolfSSL-Bot and unassigned SparkiDev Nov 23, 2026
@SparkiDev
SparkiDev force-pushed the aes_gcm_ghash_int_mul branch from 5f09d71 to 429640f Compare November 26, 2026 01:52
@SparkiDev

SparkiDev commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Fixed the MIPS, PowerPC, Cortex-M3 to use small table by default.

configure and CMake now both record the GHASH choice in options.h

@Frauschi Frauschi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The new GHASH passes the GCM/GMAC/SIV KATs on 32/64-bit LE and BE targets under qemu; the asm default split in user_settings builds needs fixing before merge.

Comment thread wolfssl/wolfcrypt/aes.h Resolved
Comment thread configure.ac Resolved
Comment thread wolfssl/wolfcrypt/aes.h Resolved
Comment thread CMakeLists.txt Resolved
Comment thread wolfssl/wolfcrypt/aes.h Resolved
Comment thread configure.ac Resolved
Comment thread wolfcrypt/src/aes.c Resolved
Comment thread configure.ac Resolved
Table-free GHASH multiply using integer multiplies on operands masked to
every fourth bit. Variants (GCM_IM_64, GCM_IM_32W, GCM_IM_32): 64x64
multiplies on 64-bit CPUs, 32x32->64 on 32-bit CPUs, and 32x32->32 only
(GCM_INT_MUL_32_ONLY, default on M-profile Arm without DSP, e.g.
Cortex-M0/M3). The key prepared from H is stored in Gcm.M0, sized for
the variant.

settings.h now picks the GHASH implementation when none is chosen, so
the PPC, RISC-V and Arm assembly, which include only settings.h, agree
with the C code:
- GCM_TABLE_4BIT when cache resistance is off (WC_NO_CACHE_RESISTANT,
  e.g. --disable-harden, or the new AES_GCM_GMULT_NO_CACHE_RESIST) and
  for Arm/PPC/RISC-V asm builds.
- GCM_SMALL where the CPU multiply may take data-dependent time
  (WC_GCM_MUL_VAR_TIME): Armv4/Armv5, MIPS, 32-bit PowerPC, RISC-V
  without Zkt, and 16-bit or multiplier-less CPUs (AVR, MSP430).
- GCM_INT_MUL otherwise. Choosing it explicitly asserts the CPU
  multiply is constant time.

configure and CMake compile settings.h and aes.h to get the GHASH and
the GCM_INT_MUL variant and record both in options.h, also for an
explicit GCM_INT_MUL, as the Gcm layout depends on them and the CPU
macros depend on flags like -march. aes.h uses a recorded variant as is.

Arm/PPC asm GCM_SMALL bulk GHASH helpers no longer branch on H.

AES-GCM-SIV POLYVAL uses the GCM_INT_MUL multiply when it is compiled.

configure/CMake: add --enable-aesgcm=intmul, rejected with armasm.
--enable-aesgcm=word now selects the word64 GHASH (new GCM_WORD64);
CMake gains "word" too. FIPS before v7 keeps GCM_TABLE_4BIT. CMake no
longer forces 4bit over an explicit choice on little-endian, and
WOLFSSL_HARDEN=no defines WC_NO_CACHE_RESISTANT, recorded in options.h,
as configure does.

CI: multi-arch armhf leg for GCM_INT_MUL with 32x32->32 multiplies.

mcapi: CRYPT_AES_CTX grows to fit the larger Aes.
@SparkiDev SparkiDev removed their assignment Nov 27, 2026
Comment thread wolfssl/wolfcrypt/aes.h
#error "GCM_IM_64 and GCM_IM_32W need a 64-bit type (WORD64_AVAILABLE)"
#endif
#if defined(GCM_INT_MUL_32_ONLY) && !defined(GCM_IM_32)
#error "GCM_INT_MUL_32_ONLY needs GCM_IM_32"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

With -DGCM_INT_MUL_32_ONLY on a 64-bit build (configure or CMake), the probe records GCM_IM_64 because the WC_64BIT_CPU branch is tested first, so the build now stops at this #error; the previous head built. Check GCM_INT_MUL_32_ONLY before WC_64BIT_CPU in the default chain.

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