Skip to content

Commit 5f09d71

Browse files
committed
AES-GCM: add GCM_INT_MUL GHASH and make it default
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.
1 parent c131af0 commit 5f09d71

8 files changed

Lines changed: 902 additions & 29 deletions

File tree

‎.wolfssl_known_macro_extras‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ AES_CR_CPHASE
55
AES_CR_GCMPH_Msk
66
AES_CR_NPBLB_Msk
77
AES_GCM_GMULT_NCT
8+
AES_GCM_GMULT_NO_CACHE_RESIST
89
AES_ICR_CCF
910
AES_ISR_CCF
1011
AES_SR_CCF
@@ -359,6 +360,7 @@ FSL_FEATURE_SOC_MMCAU_COUNT
359360
FSL_FEATURE_SOC_RNG_COUNT
360361
FSL_FEATURE_SOC_TRNG_COUNT
361362
FUSION_RTOS
363+
GCM_INT_MUL_32_ONLY
362364
GENERATE_MACHINE_PARSEABLE_REPORT
363365
GE_P3_TOBYTES_IMPL
364366
GOAHEAD_WS
@@ -1353,6 +1355,13 @@ __ARCH_STRNCPY_NO_REDIRECT
13531355
__ARCH_STRSTR_NO_REDIRECT
13541356
__ARMEB__
13551357
__ARM_ARCH
1358+
__ARM_ARCH_4T__
1359+
__ARM_ARCH_4__
1360+
__ARM_ARCH_5E__
1361+
__ARM_ARCH_5TEJ__
1362+
__ARM_ARCH_5TE__
1363+
__ARM_ARCH_5T__
1364+
__ARM_ARCH_5__
13561365
__ARM_ARCH_6M__
13571366
__ARM_ARCH_7EM__
13581367
__ARM_ARCH_7M__
@@ -1502,6 +1511,7 @@ __ppc__
15021511
__riscv
15031512
__riscv_mul
15041513
__riscv_xlen
1514+
__riscv_zkt
15051515
__s390x__
15061516
__sparc
15071517
__sparc64__

‎CMakeLists.txt‎

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ if(WOLFSSL_HARDEN)
11661166
list(APPEND WOLFSSL_DEFINITIONS "-DWC_RSA_BLINDING")
11671167
endif()
11681168
else()
1169-
list(APPEND WOLFSSL_DEFINITIONS "-DWC_NO_HARDEN")
1169+
list(APPEND WOLFSSL_DEFINITIONS "-DWC_NO_HARDEN" "-DWC_NO_CACHE_RESISTANT")
11701170
endif()
11711171

11721172
# AES table cache-line touching: aes.c turns it on for RISC-V by default, and
@@ -1667,15 +1667,24 @@ endif()
16671667
# AES-GCM
16681668
add_option("WOLFSSL_AESGCM"
16691669
"Enable wolfSSL AES-GCM support (default: enabled)"
1670-
"yes" "yes;no;table;small;word32;4bit")
1670+
"yes" "yes;no;table;small;word32;4bit;intmul")
16711671

16721672
# leanpsk and leantls don't need gcm
16731673
if(WOLFSSL_LEAN_PSK OR (WOLFSSL_LEAN_TLS AND NOT WOLFSSL_TLS13))
16741674
override_cache(WOLFSSL_AESGCM "no")
16751675
endif()
16761676

1677-
if(WOLFSSL_AESGCM AND CMAKE_C_BYTE_ORDER STREQUAL "LITTLE_ENDIAN")
1678-
override_cache(WOLFSSL_AESGCM "4bit")
1677+
# Default GHASH implementation, as configure: FIPS modules before v7 don't
1678+
# have GCM_INT_MUL and keep the 4-bit table. Otherwise it is chosen once all
1679+
# options are known (see "Default GHASH implementation" below) and written to
1680+
# options.h, so applications get the library's Gcm layout.
1681+
set(_wolfssl_gcm_default_ghash FALSE)
1682+
if("${WOLFSSL_AESGCM}" STREQUAL "yes")
1683+
if(WOLFSSL_FIPS AND ("${FIPS_VERSION}" MATCHES "^(v1|v2|rand|v5|v6)"))
1684+
override_cache(WOLFSSL_AESGCM "4bit")
1685+
elseif(NOT WOLFSSL_LOW_RESOURCE)
1686+
set(_wolfssl_gcm_default_ghash TRUE)
1687+
endif()
16791688
endif()
16801689

16811690
if(WOLFSSL_AESGCM)
@@ -1699,6 +1708,11 @@ if(WOLFSSL_AESGCM)
16991708
override_cache(WOLFSSL_AESGCM "yes")
17001709
endif()
17011710

1711+
if("${WOLFSSL_AESGCM}" STREQUAL "intmul")
1712+
list(APPEND WOLFSSL_DEFINITIONS "-DGCM_INT_MUL")
1713+
override_cache(WOLFSSL_AESGCM "yes")
1714+
endif()
1715+
17021716
list(APPEND WOLFSSL_DEFINITIONS "-DHAVE_AESGCM")
17031717
endif()
17041718

@@ -4776,6 +4790,53 @@ if(WOLFSSL_PPC64_ASM AND WOLFSSL_ASM)
47764790
list(APPEND WOLFSSL_DEFINITIONS "-DWOLFSSL_PPC64_ASM")
47774791
endif()
47784792

4793+
# Default GHASH implementation, chosen as wolfssl/wolfcrypt/aes.h would:
4794+
# the 4-bit table when cache resistance is off or the Arm/PPC/RISC-V assembly
4795+
# GHASH is used, GCM_SMALL where the multiply may not be constant time,
4796+
# otherwise GCM_INT_MUL. The selector is recorded in options.h:
4797+
# the opt-out macros may only be in the compiler flags, which options.h does
4798+
# not record.
4799+
if(_wolfssl_gcm_default_ghash)
4800+
string(TOUPPER "${CMAKE_BUILD_TYPE}" _wolfssl_build_type)
4801+
set(_wolfssl_gcm_flags "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${_wolfssl_build_type}} ${WOLFSSL_DEFINITIONS}")
4802+
if(("${_wolfssl_gcm_flags}" MATCHES "(^|[ ;])-DWC_NO_CACHE_RESISTANT([ ;=]|$)") OR
4803+
("${_wolfssl_gcm_flags}" MATCHES "(^|[ ;])-DAES_GCM_GMULT_NO_CACHE_RESIST([ ;=]|$)") OR
4804+
("${_wolfssl_gcm_flags}" MATCHES "(^|[ ;])-DWOLFSSL_ARMASM([ ;=]|$)") OR
4805+
(WOLFSSL_ASM AND (WOLFSSL_PPC32_ASM OR WOLFSSL_PPC64_ASM OR WOLFSSL_RISCV_ASM)))
4806+
list(APPEND WOLFSSL_DEFINITIONS "-DGCM_TABLE_4BIT")
4807+
else()
4808+
# Targets whose multiply may take data-dependent time get GCM_SMALL,
4809+
# as in aes.h. Compiled with the target compiler and flags (e.g.
4810+
# -march=..._zkt) to see its architecture macros.
4811+
unset(WOLFSSL_GCM_MUL_CONST_TIME CACHE)
4812+
check_c_source_compiles("
4813+
#if defined(__arm__) && defined(__ARM_ARCH) && __ARM_ARCH < 6
4814+
#error Armv4/Armv5 multiply terminates early
4815+
#endif
4816+
#if defined(__arm__) && (defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__))
4817+
#error Armv4 multiply terminates early
4818+
#endif
4819+
#if defined(__arm__) && (defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5T__))
4820+
#error Armv5 multiply terminates early
4821+
#endif
4822+
#if defined(__arm__) && (defined(__ARM_ARCH_5E__) || defined(__ARM_ARCH_5TE__))
4823+
#error Armv5 multiply terminates early
4824+
#endif
4825+
#if defined(__arm__) && defined(__ARM_ARCH_5TEJ__)
4826+
#error Armv5 multiply terminates early
4827+
#endif
4828+
#if defined(__riscv) && !defined(__riscv_zkt)
4829+
#error RISC-V multiply is constant time only with Zkt
4830+
#endif
4831+
int main(void) { return 0; }" WOLFSSL_GCM_MUL_CONST_TIME)
4832+
if(WOLFSSL_GCM_MUL_CONST_TIME)
4833+
list(APPEND WOLFSSL_DEFINITIONS "-DGCM_INT_MUL")
4834+
else()
4835+
list(APPEND WOLFSSL_DEFINITIONS "-DGCM_SMALL")
4836+
endif()
4837+
endif()
4838+
endif()
4839+
47794840
# All options are declared by this point; report any force_option() that no
47804841
# add_option() consumed (would otherwise be a silent no-op).
47814842
wolfssl_warn_unconsumed_forces()

‎cmake/options.h.in‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ extern "C" {
7575
#cmakedefine GCM_TABLE_4BIT
7676
#undef GCM_WORD32
7777
#cmakedefine GCM_WORD32
78+
#undef GCM_INT_MUL
79+
#cmakedefine GCM_INT_MUL
7880
#undef HAVE___UINT128_T
7981
#cmakedefine HAVE___UINT128_T 1
8082
#undef HAVE_AES_KEYWRAP

‎configure.ac‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4523,7 +4523,11 @@ then
45234523
ENABLED_AESGCM=no
45244524
fi
45254525

4526-
if test "$ENABLED_AESGCM" = "yes"
4526+
# Default GHASH implementation: none is passed and aes.h chooses (GCM_INT_MUL
4527+
# when cache resistant, otherwise the 4-bit table). FIPS modules before v7
4528+
# don't have GCM_INT_MUL and keep the 4-bit table.
4529+
if test "$ENABLED_AESGCM" = "yes" && test "$ENABLED_FIPS" != "no" &&
4530+
test "$HAVE_FIPS_VERSION_MAJOR" -lt 7
45274531
then
45284532
ENABLED_AESGCM="4bit"
45294533
fi
@@ -13998,11 +14002,18 @@ then
1399814002
ENABLED_AESGCM=yes
1399914003
fi
1400014004

14005+
if test "$ENABLED_AESGCM" = "intmul"
14006+
then
14007+
AM_CFLAGS="$AM_CFLAGS -DGCM_INT_MUL"
14008+
AM_CCASFLAGS="$AM_CCASFLAGS -DGCM_INT_MUL"
14009+
ENABLED_AESGCM=yes
14010+
fi
14011+
1400114012
# The GCM table layout macro must reach assembled (.S) sources too: the
1400214013
# PPC asm GCM_gmult_len is a single file with both layouts guarded by
1400314014
# GCM_TABLE / GCM_TABLE_4BIT. Default (no explicit table choice) is 4bit.
1400414015
case " $AM_CCASFLAGS " in
14005-
*" -DGCM_TABLE "*|*" -DGCM_TABLE_4BIT "*|*" -DGCM_SMALL "*|*" -DGCM_WORD32 "*) ;;
14016+
*" -DGCM_TABLE "*|*" -DGCM_TABLE_4BIT "*|*" -DGCM_SMALL "*|*" -DGCM_WORD32 "*|*" -DGCM_INT_MUL "*) ;;
1400614017
*) AM_CCASFLAGS="$AM_CCASFLAGS -DGCM_TABLE_4BIT" ;;
1400714018
esac
1400814019

‎mcapi/crypto.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,9 @@ enum {
173173
typedef struct CRYPT_AES_CTX {
174174
/* big enough to hold internal, but check on init */
175175
#ifdef WOLF_PRIVATE_KEY_ID
176-
int holder[114];
176+
int holder[128];
177177
#else
178-
int holder[96];
178+
int holder[104];
179179
#endif
180180
} CRYPT_AES_CTX;
181181

‎wolfcrypt/benchmark/benchmark.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6592,6 +6592,8 @@ void bench_gmac(int useDeviceID)
65926592
const char* gmacStr = "GMAC Table 4-bit";
65936593
#elif defined(GCM_WORD32)
65946594
const char* gmacStr = "GMAC Word32";
6595+
#elif defined(GCM_INT_MUL)
6596+
const char* gmacStr = "GMAC IntMul";
65956597
#else
65966598
const char* gmacStr = "GMAC Default";
65976599
#endif

0 commit comments

Comments
 (0)