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

Copy link
Copy Markdown
Contributor

Description

Table-free GHASH multiply using integer multiplies on operands masked to every fourth bit

aes.h now picks the GHASH implementation when none is chosen GCM_INT_MUL when WC_AES_GCM_GMULT_CACHE_RESIST is set (default), otherwise GCM_TABLE_4BIT. PPC/RISC-V/Arm asm builds keep GCM_TABLE_4BIT. Opt out with WC_NO_CACHE_RESISTANT (--disable-harden) or the new AES_GCM_GMULT_NO_CACHE_RESIST.

GCM_SMALL and GCM_WORD32 GMULT: mask instead of branching on H/state when cache resistant.

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

configure/CMake: add --enable-aesgcm=intmul; by default pass no GHASH selector and let aes.h decide (FIPS keeps GCM_TABLE_4BIT). CMake no longer forces 4bit over an explicit choice on little-endian.

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
Table-free GHASH multiply using integer multiplies on operands masked to
every fourth bit

aes.h now picks the GHASH implementation when none is chosen
GCM_INT_MUL when WC_AES_GCM_GMULT_CACHE_RESIST is set (default), otherwise
GCM_TABLE_4BIT. PPC/RISC-V/Arm asm builds keep GCM_TABLE_4BIT. Opt out
with WC_NO_CACHE_RESISTANT (--disable-harden) or the new
AES_GCM_GMULT_NO_CACHE_RESIST.

GCM_SMALL and GCM_WORD32 GMULT: mask instead of branching on H/state
when cache resistant.

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

configure/CMake: add --enable-aesgcm=intmul; by default pass no
GHASH selector and let aes.h decide (FIPS keeps GCM_TABLE_4BIT). CMake
no longer forces 4bit over an explicit choice on little-endian.
@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-m3

  • FLASH: .text +16 B (+0.0%, 132,037 B / 262,144 B, total: 50% used)

gcc-arm-cortex-m4

  • FLASH: .text +64 B (+0.0%, 209,356 B / 262,144 B, total: 80% used)

gcc-arm-cortex-m4-crypto-only

  • FLASH: .text +384 B (+0.2%, 182,419 B / 262,144 B, total: 70% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +64 B (+0.0%, 196,312 B / 1,048,576 B, total: 19% used)

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +20 B, text +10,296 B (+3.7%, 292,788 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
endif()

if("${WOLFSSL_AESGCM}" STREQUAL "intmul")
list(APPEND WOLFSSL_DEFINITIONS "-DGCM_INT_MUL")
Comment thread wolfcrypt/src/aes.c
{
#ifdef WC_AES_GCM_GMULT_CACHE_RESIST
/* Y is H: select V with a mask rather than branching on it. */
byte mask = (byte)(0U - (byte)(y >> 7));
Comment thread wolfcrypt/src/aes.c
typedef word32 GcmImWord;
#endif
/* Number of words in a block. */
#define GCM_IM_WORDS ((int)(WC_AES_BLOCK_SIZE / sizeof(GcmImWord)))
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