From: Eric Biggers <ebiggers@kernel.org>
To: linux-crypto@vger.kernel.org
Cc: Ard Biesheuvel <ardb@kernel.org>,
"Jason A . Donenfeld" <Jason@zx2c4.com>,
Herbert Xu <herbert@gondor.apana.org.au>,
x86@kernel.org, Antonio Quartulli <antonio@mandelbit.com>,
Eric Biggers <ebiggers@kernel.org>
Subject: [PATCH 06/11] lib/crypto: x86/gf128hash: Optimize GHASH and POLYVAL with VPCLMULQDQ and AVX2
Date: Sun, 29 Nov 2026 12:02:40 -0800 [thread overview]
Message-ID: <20261129200245.146503-7-ebiggers@kernel.org> (raw)
In-Reply-To: <20261129200245.146503-1-ebiggers@kernel.org>
Add a VPCLMULQDQ + AVX2 implementation of GHASH and POLYVAL for x86_64
in lib/crypto/x86/gf128hash-vpclmul-avx2.S, providing:
- {ghash,polyval}_preparekey_vpclmul_avx2()
- {ghash,polyval}_blocks_vpclmul_avx2()
The file is derived from arch/x86/crypto/aes-gcm-vaes-avx2.S, so it
inherits its copyright and license. This speeds up both algorithms on
CPUs with VPCLMULQDQ and AVX2 by hashing two 128-bit blocks per YMM
vector (8 blocks per stride), and prepares for migrating the VAES+AVX2
AES-GCM code into lib/crypto/.
Like gf128hash-pclmul.S, the assembly uses macros to share code between
GHASH and POLYVAL and processes trailing 2..7 blocks in parallel with a
single deferred reduction. Key implementation choices for AVX2:
- The 8-block main loop uses 256-bit Karatsuba multiplication with
h_powers_xored stored in the permuted order (8,6,7,5,4,2,3,1) so all
8 XOR'd halves stay resident in two YMM registers without shuffles.
The 2..7-block tail uses schoolbook multiplication because when the
number of remaining blocks is odd it needs key-power pairs that the
permuted table doesn't support.
- Key preparation stores H^1 * x^-64 mod G(x) in key->h_times_xm64,
squares H^1 to H^2, precomputes H^2 * x^-64 mod G(x), and computes
[H^4, H^3], [H^6, H^5], and [H^8, H^7] in 2-wide YMM vectors using
1-step-reduction multiplications by [H^2, H^2].
- Single-block calls (nblocks == 1) use a strictly 128-bit XMM fast path
to avoid YMM overhead and vzeroupper, using key->h_times_xm64 for a
1-step reduction (as does the final single-block tail path).
Gate this path on VAES as well as AVX2 and VPCLMULQDQ so that
libgf128hash and libaes (whose VAES+AVX2 AES-GCM code requires VAES)
always agree on the layout of 'struct ghash_key'.
Benchmark results on AMD Ryzen 9 9950X (Zen 5) using ghash_kunit and
polyval_kunit:
Length GHASH before GHASH after POLYVAL before POLYVAL after
------ ------------ ----------- -------------- -------------
1 129 MB/s 129 MB/s 131 MB/s 131 MB/s
16 2105 MB/s 2157 MB/s 2278 MB/s 2232 MB/s
64 5881 MB/s 7286 MB/s 6015 MB/s 7156 MB/s
127 6017 MB/s 6615 MB/s 6161 MB/s 6785 MB/s
128 8796 MB/s 11769 MB/s 8726 MB/s 11663 MB/s
200 7658 MB/s 9112 MB/s 7728 MB/s 8863 MB/s
256 10651 MB/s 16585 MB/s 10620 MB/s 16529 MB/s
511 10212 MB/s 14640 MB/s 10478 MB/s 14630 MB/s
512 11991 MB/s 17740 MB/s 12082 MB/s 20235 MB/s
1024 13081 MB/s 20218 MB/s 12857 MB/s 22206 MB/s
3173 13096 MB/s 20521 MB/s 13118 MB/s 20545 MB/s
4096 13535 MB/s 21706 MB/s 13351 MB/s 20717 MB/s
16384 13446 MB/s 21577 MB/s 13233 MB/s 22174 MB/s
Signed-off-by: Eric Biggers <ebiggers@kernel.org>
---
include/crypto/gf128hash.h | 6 +-
lib/crypto/Makefile | 3 +-
lib/crypto/x86/gf128hash-cpu.h | 13 +
lib/crypto/x86/gf128hash-vpclmul-avx2.S | 537 ++++++++++++++++++++++++
lib/crypto/x86/gf128hash.h | 50 ++-
5 files changed, 601 insertions(+), 8 deletions(-)
create mode 100644 lib/crypto/x86/gf128hash-vpclmul-avx2.S
diff --git a/include/crypto/gf128hash.h b/include/crypto/gf128hash.h
index c8b523a2d33cf..4d2109a3362c0 100644
--- a/include/crypto/gf128hash.h
+++ b/include/crypto/gf128hash.h
@@ -38,7 +38,11 @@ struct polyval_elem {
struct gf128hash_key_x86 {
/* Powers of the hash key H^8 through H^1, in POLYVAL format */
struct polyval_elem h_powers[8];
- /* Halves of each entry of @h_powers XOR'd together */
+ /*
+ * Halves of each entry of @h_powers XOR'd together. If
+ * gf128hash_use_vpclmul_avx2(), they're in the order H^8, H^6, H^7,
+ * H^5, H^4, H^2, H^3, H^1. Otherwise they're in the order H^8..H^1.
+ */
__le64 h_powers_xored[8];
/*
* H^1 * x^-64 mod G(x) in POLYVAL format. This is used to optimize
diff --git a/lib/crypto/Makefile b/lib/crypto/Makefile
index 5b458b6f20e32..9d3ecc643a844 100644
--- a/lib/crypto/Makefile
+++ b/lib/crypto/Makefile
@@ -42,7 +42,8 @@ OBJECT_FILES_NON_STANDARD_powerpc/ghashp8-ppc.o := y
endif
libgf128hash-$(CONFIG_RISCV) += riscv/ghash-riscv64-zvkg.o
-libgf128hash-$(CONFIG_X86_64) += x86/gf128hash-pclmul.o
+libgf128hash-$(CONFIG_X86_64) += x86/gf128hash-pclmul.o \
+ x86/gf128hash-vpclmul-avx2.o
endif # CONFIG_CRYPTO_LIB_GF128HASH_ARCH
# clean-files must be defined unconditionally
diff --git a/lib/crypto/x86/gf128hash-cpu.h b/lib/crypto/x86/gf128hash-cpu.h
index 157da0a49c416..5045b41bf8d73 100644
--- a/lib/crypto/x86/gf128hash-cpu.h
+++ b/lib/crypto/x86/gf128hash-cpu.h
@@ -23,4 +23,17 @@ static inline bool gf128hash_use_pclmul_avx(void)
return gf128hash_use_pclmul() && cpu_feature_enabled(X86_FEATURE_AVX);
}
+/*
+ * Check VAES too, even though GHASH and POLYVAL don't use VAES instructions, so
+ * that struct ghash_key matches the layout expected by libaes's AES-GCM code
+ * (which falls back to its non-VAES code if VAES is disabled).
+ */
+static inline bool gf128hash_use_vpclmul_avx2(void)
+{
+ return gf128hash_use_pclmul_avx() &&
+ cpu_feature_enabled(X86_FEATURE_VAES) &&
+ cpu_feature_enabled(X86_FEATURE_AVX2) &&
+ cpu_feature_enabled(X86_FEATURE_VPCLMULQDQ);
+}
+
#endif /* _LIB_CRYPTO_X86_GF128HASH_CPU_H */
diff --git a/lib/crypto/x86/gf128hash-vpclmul-avx2.S b/lib/crypto/x86/gf128hash-vpclmul-avx2.S
new file mode 100644
index 0000000000000..9c38148445aa3
--- /dev/null
+++ b/lib/crypto/x86/gf128hash-vpclmul-avx2.S
@@ -0,0 +1,537 @@
+/* SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause */
+//
+// Accelerated GHASH and POLYVAL implementation with VPCLMULQDQ and AVX2
+//
+// Copyright 2025 Google LLC
+//
+// Author: Eric Biggers <ebiggers@google.com>
+//
+//------------------------------------------------------------------------------
+//
+// This file is dual-licensed, meaning that you can use it under your choice of
+// either of the following two licenses:
+//
+// Licensed under the Apache License 2.0 (the "License"). You may obtain a copy
+// of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+// or
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice,
+// this list of conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright
+// notice, this list of conditions and the following disclaimer in the
+// documentation and/or other materials provided with the distribution.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+//
+//------------------------------------------------------------------------------
+//
+// This file implements GHASH and POLYVAL using the VPCLMULQDQ instruction on
+// 256-bit vectors (ymm registers), each holding two 128-bit blocks. It also
+// requires AVX2 (for 256-bit vector integer operations). The math and the
+// overall design are the same as in gf128hash-pclmul.S, which documents them.
+// The main differences are:
+//
+// - The 8-block stride is processed as 4 vectors. Lane 0 of each vector holds
+// the earlier block and lane 1 the later one, so the data vectors are
+// multiplied by the key power vectors [H^8, H^7], [H^6, H^5], [H^4, H^3], and
+// [H^2, H^1] (written [lane 0, lane 1]), which stay in registers. The
+// unreduced products are summed in each lane, both lanes are reduced at once,
+// and the two reduced lanes are XOR'd together. This works because the
+// reduction is linear. The accumulator is kept in lane 0 with lane 1 zero,
+// so XOR'ing it into the first data vector adds it to the first block only.
+//
+// - The 8-block loop uses Karatsuba multiplication like gf128hash-pclmul.S.
+// To avoid any per-vector loads or shuffles of the precomputed XOR'd halves
+// of the key powers, h_powers_xored stores them in the order H^8, H^6, H^7,
+// H^5, H^4, H^2, H^3, H^1. The loop loads them into two registers:
+//
+// H_POW43_XORED = [(H^8)_x, (H^6)_x | (H^7)_x, (H^5)_x]
+// H_POW21_XORED = [(H^4)_x, (H^2)_x | (H^3)_x, (H^1)_x]
+//
+// where (H^k)_x = (H^k)_L + (H^k)_H, qwords are written low-to-high, and '|'
+// separates lanes 0 and 1. vpclmulqdq then selects either the low qword of
+// each lane (for H_POW4 = [H^8, H^7] and H_POW2 = [H^4, H^3]) or the high
+// qword (for H_POW3 = [H^6, H^5] and H_POW1 = [H^2, H^1]). The final 2..7
+// blocks use schoolbook multiplication instead, since when the number of
+// remaining blocks is odd they need key power pairs (e.g. [H^5, H^4]) that
+// this layout doesn't support.
+//
+// - Single-block multiplications (both nblocks == 1 and a single remaining
+// block after the 8-block loop) use the precomputed H^1 * x^-64 mod G(x) from
+// key->h_times_xm64 so that only one reduction step is needed, matching
+// gf128hash-pclmul.S. When nblocks == 1 at entry, a dedicated xmm-only fast
+// path is used to avoid ymm setup and vzeroupper; when 1 block remains after
+// the 8-block loop, ymm registers are already dirty so .Lblocks_1x falls
+// through to vzeroupper.
+//
+// - preparekey stores H^1 * x^-64 mod G(x) in key->h_times_xm64, squares H^1 to
+// get H^2, precomputes H^2 * x^-64 mod G(x), and then computes [H^4, H^3],
+// [H^6, H^5], and [H^8, H^7] by repeatedly multiplying by [H^2, H^2] using
+// the 1-step reduction.
+
+#include <linux/linkage.h>
+
+.section .rodata
+.p2align 4
+
+ // vpshufb mask that reverses the order of the bytes in a 16-byte block
+.Lbswap_mask:
+ .octa 0x000102030405060708090a0b0c0d0e0f
+
+ // Mask for multiplying by x mod G(x): G(x) - x^128 = x^127 + x^126 +
+ // x^121 + 1 (bits 127, 126, 121, and 0), and bit 64 for the carry from
+ // bit 63 that vpaddq doesn't propagate
+.Lgfpoly_and_internal_carrybit:
+ .octa 0xc2000000000000010000000000000001
+
+ // (G(x) - x^128 - 1) / x^64 = x^63 + x^62 + x^57, the constant used to
+ // multiply by x^-64 mod G(x)
+.Lgfpoly:
+ .quad 0xc200000000000000
+
+// Offsets in struct gf128hash_key_x86, using the 8-power permuted layout.
+// h_powers[0..7] holds H^8..H^1, h_powers_xored[0..7] holds the XORs of the two
+// 64-bit halves of H^8, H^6, H^7, H^5, H^4, H^2, H^3, H^1 (in that order), and
+// h_times_xm64 holds H^1 * x^-64 mod G(x).
+#define OFFSETOF_H_POWERS 0
+#define OFFSETOFEND_H_POWERS 128
+#define OFFSETOF_H_POWERS_XORED 128
+#define OFFSETOF_H_TIMES_XM64 192
+
+.text
+
+// Set each 128-bit lane of \dst to dot(lane of \a, lane of \b), given
+// \a_times_xm64 = \a * x^-64 mod G(x) in each lane (see gf128hash-pclmul.S for
+// the math). Works on xmm and ymm registers. \dst may alias \a,
+// \a_times_xm64, or \b. \gfpoly must contain .Lgfpoly in the low qword of each
+// lane. Clobbers \t0 and \t1. \gfpoly, \t0, and \t1 must be distinct from
+// each other and from the other operands.
+.macro _gf128hash_mul a, a_times_xm64, b, dst, gfpoly, t0, t1
+ vpclmulqdq $0x01, \a, \b, \t0 // a_L * b_H
+ vpclmulqdq $0x00, \a_times_xm64, \b, \t1 // (a*x^-64)_L * b_L
+ vpxor \t1, \t0, \t0 // MI
+ vpclmulqdq $0x11, \a, \b, \t1 // a_H * b_H
+ vpclmulqdq $0x10, \a_times_xm64, \b, \dst // (a*x^-64)_H * b_L
+ vpxor \t1, \dst, \dst // HI
+ vpclmulqdq $0x00, \gfpoly, \t0, \t1 // MI_L*(x^63+x^62+x^57)
+ vpshufd $0x4e, \t0, \t0 // MI_H + MI_L * x^64
+ vpxor \t0, \dst, \dst // Fold MI into HI (1)
+ vpxor \t1, \dst, \dst // Fold MI into HI (2)
+.endm
+
+// Like _gf128hash_mul, but compute dot(\a, \a) in each 128-bit lane. In
+// GF(2)[x], the cross terms cancel: (a_L + a_H * x^64)^2 = (a_L)^2 +
+// (a_H)^2 * x^128. So only 2 vpclmulqdq are needed for the product, and the
+// initial MI is 0, so the first reduction step sets MI = LO * x^-64. \dst may
+// alias \a. Clobbers \t0 and \t1. \gfpoly, \t0, and \t1 must be distinct from
+// each other and from \a and \dst.
+.macro _gf128hash_square a, dst, gfpoly, t0, t1
+ vpclmulqdq $0x00, \a, \a, \t0 // LO = (a_L)^2
+ vpclmulqdq $0x11, \a, \a, \dst // HI = (a_H)^2
+ vpclmulqdq $0x00, \gfpoly, \t0, \t1 // LO_L*(x^63+x^62+x^57)
+ vpshufd $0x4e, \t0, \t0 // LO_H + LO_L * x^64
+ vpxor \t0, \t1, \t1 // MI = LO * x^-64
+ vpclmulqdq $0x00, \gfpoly, \t1, \t0 // MI_L*(x^63+x^62+x^57)
+ vpshufd $0x4e, \t1, \t1 // MI_H + MI_L * x^64
+ vpxor \t1, \dst, \dst // Fold MI into HI (1)
+ vpxor \t0, \dst, \dst // Fold MI into HI (2)
+.endm
+
+// Reduce the 256-bit products LO + MI * x^64 + HI * x^128 given in each 128-bit
+// lane of \lo, \mi, and \hi: multiply them by x^-128 mod G(x) and store the
+// 128-bit results in \hi. This is done in two steps, MI += LO * x^-64 then
+// HI += MI * x^-64. Works on xmm and ymm registers. \gfpoly must contain
+// .Lgfpoly in the low qword of each lane. Clobbers \lo, \mi, and \tmp. All
+// operands must be distinct registers.
+.macro _gf128hash_reduce lo, mi, hi, gfpoly, tmp
+ vpclmulqdq $0x00, \gfpoly, \lo, \tmp // LO_L*(x^63+x^62+x^57)
+ vpshufd $0x4e, \lo, \lo // LO_H + LO_L * x^64
+ vpxor \lo, \mi, \mi // Fold LO into MI (1)
+ vpxor \tmp, \mi, \mi // Fold LO into MI (2)
+ vpclmulqdq $0x00, \gfpoly, \mi, \tmp // MI_L*(x^63+x^62+x^57)
+ vpshufd $0x4e, \mi, \mi // MI_H + MI_L * x^64
+ vpxor \mi, \hi, \hi // Fold MI into HI (1)
+ vpxor \tmp, \hi, \hi // Fold MI into HI (2)
+.endm
+
+// void {ghash,polyval}_preparekey_vpclmul_avx2(struct gf128hash_key_x86 *key,
+// const u8 raw_key[16]);
+//
+// Given the raw GHASH key (\is_ghash=1) or POLYVAL key (\is_ghash=0), set:
+// - key->h_powers[0..7] to H^8..H^1 in POLYVAL format
+// - key->h_powers_xored[0..7] to the XORs of the two 64-bit halves of H^8, H^6,
+// H^7, H^5, H^4, H^2, H^3, H^1 (in that order)
+// - key->h_times_xm64 to H^1 * x^-64 mod G(x)
+.macro _gf128hash_preparekey is_ghash
+ .set KEY, %rdi
+ .set RAW_KEY, %rsi
+ .set TMP0, %ymm0
+ .set TMP0_XMM, %xmm0
+ .set TMP1, %ymm1
+ .set TMP1_XMM, %xmm1
+ .set H_CUR, %ymm2
+ .set H_CUR_XMM, %xmm2
+ .set H_CUR2, %ymm3
+ .set H_INC, %ymm4
+ .set H_INC_XMM, %xmm4
+ .set H_INC_XM64, %ymm5
+ .set H_INC_XM64_XMM, %xmm5
+ .set GFPOLY, %ymm6
+ .set GFPOLY_XMM, %xmm6
+
+ vmovdqu (RAW_KEY), H_CUR_XMM
+.if \is_ghash
+ // Convert the GHASH key to POLYVAL format: byte-reverse it, then
+ // multiply it by x mod G(x). See gf128hash-pclmul.S for details.
+ vpshufb .Lbswap_mask(%rip), H_CUR_XMM, H_CUR_XMM
+ vpshufd $0xd3, H_CUR_XMM, TMP0_XMM
+ vpsrad $31, TMP0_XMM, TMP0_XMM
+ vpaddq H_CUR_XMM, H_CUR_XMM, H_CUR_XMM
+ vpand .Lgfpoly_and_internal_carrybit(%rip), TMP0_XMM, TMP0_XMM
+ vpxor TMP0_XMM, H_CUR_XMM, H_CUR_XMM
+.endif
+
+ vpbroadcastq .Lgfpoly(%rip), GFPOLY
+
+ // Compute and store H^1 * x^-64 mod G(x) in h_times_xm64.
+ vpclmulqdq $0x00, GFPOLY_XMM, H_CUR_XMM, TMP1_XMM
+ vpshufd $0x4e, H_CUR_XMM, TMP0_XMM
+ vpxor TMP0_XMM, TMP1_XMM, TMP1_XMM
+ vmovdqu TMP1_XMM, OFFSETOF_H_TIMES_XM64(KEY)
+
+ // Square H^1 to get H^2, and compute H^2 * x^-64 mod G(x).
+ _gf128hash_square H_CUR_XMM, H_INC_XMM, GFPOLY_XMM, TMP0_XMM, TMP1_XMM
+ vpclmulqdq $0x00, GFPOLY_XMM, H_INC_XMM, H_INC_XM64_XMM
+ vpshufd $0x4e, H_INC_XMM, TMP0_XMM
+ vpxor TMP0_XMM, H_INC_XM64_XMM, H_INC_XM64_XMM
+
+ // Create H_CUR = [H^2, H^1], H_INC = [H^2, H^2], and
+ // H_INC_XM64 = [H^2 * x^-64, H^2 * x^-64].
+ vinserti128 $1, H_CUR_XMM, H_INC, H_CUR
+ vinserti128 $1, H_INC_XMM, H_INC, H_INC
+ vinserti128 $1, H_INC_XM64_XMM, H_INC_XM64, H_INC_XM64
+
+ // Compute H_CUR2 = [H^4, H^3].
+ _gf128hash_mul H_INC, H_INC_XM64, H_CUR, H_CUR2, GFPOLY, TMP0, TMP1
+
+ // Store [H^4, H^3, H^2, H^1] in h_powers[4..7].
+ vmovdqu H_CUR, OFFSETOF_H_POWERS+3*32(KEY)
+ vmovdqu H_CUR2, OFFSETOF_H_POWERS+2*32(KEY)
+
+ // Store [(H^4)_x, (H^2)_x | (H^3)_x, (H^1)_x] in h_powers_xored[4..7].
+ // This layout requires only in-lane unpacking (in AT&T syntax, H_CUR2
+ // unpacks into the low qwords and H_CUR into the high qwords).
+ vpunpcklqdq H_CUR, H_CUR2, TMP0
+ vpunpckhqdq H_CUR, H_CUR2, TMP1
+ vpxor TMP1, TMP0, TMP0
+ vmovdqu TMP0, OFFSETOF_H_POWERS_XORED+1*32(KEY)
+
+ // Compute H_CUR = [H^6, H^5] and H_CUR2 = [H^8, H^7], and store
+ // [H^8, H^7, H^6, H^5] in h_powers[0..3].
+ _gf128hash_mul H_INC, H_INC_XM64, H_CUR2, H_CUR, GFPOLY, TMP0, TMP1
+ _gf128hash_mul H_INC, H_INC_XM64, H_CUR, H_CUR2, GFPOLY, TMP0, TMP1
+ vmovdqu H_CUR, OFFSETOF_H_POWERS+1*32(KEY)
+ vmovdqu H_CUR2, OFFSETOF_H_POWERS+0*32(KEY)
+
+ // Store [(H^8)_x, (H^6)_x | (H^7)_x, (H^5)_x] in h_powers_xored[0..3].
+ vpunpcklqdq H_CUR, H_CUR2, TMP0
+ vpunpckhqdq H_CUR, H_CUR2, TMP1
+ vpxor TMP1, TMP0, TMP0
+ vmovdqu TMP0, OFFSETOF_H_POWERS_XORED+0*32(KEY)
+
+ vzeroupper
+ RET
+.endm
+
+// Update ACC with 8 blocks (four 32-byte vectors) of data from DATA, using
+// aggregated Karatsuba multiplication. ACC must hold the accumulator in lane 0
+// and zero in lane 1, and this property is preserved.
+//
+// For each data vector M, vpunpckhqdq and vpxor compute M_L + M_H in the low
+// qword of each lane, for the Karatsuba middle product. Vectors 1..3 are
+// processed before vector 0 so that the loop-carried dependency on ACC enters
+// as late as possible.
+.macro _gf128hash_4x bswap_data
+ // Vector 1: [M_2, M_3] * [H^6, H^5], with the high qwords of
+ // H_POW43_XORED. Initialize LO, MI, and HI.
+ vmovdqu 1*32(DATA), TMP1
+.if \bswap_data
+ vpshufb BSWAP_MASK, TMP1, TMP1
+.endif
+ vpclmulqdq $0x00, H_POW3, TMP1, LO
+ vpclmulqdq $0x11, H_POW3, TMP1, HI
+ vpunpckhqdq TMP1, TMP1, TMP0
+ vpxor TMP1, TMP0, TMP0
+ vpclmulqdq $0x10, H_POW43_XORED, TMP0, MI
+
+ // Vector 2: [M_4, M_5] * [H^4, H^3], with the low qwords of
+ // H_POW21_XORED.
+ vmovdqu 2*32(DATA), TMP1
+.if \bswap_data
+ vpshufb BSWAP_MASK, TMP1, TMP1
+.endif
+ vpclmulqdq $0x00, H_POW2, TMP1, TMP0
+ vpxor TMP0, LO, LO
+ vpclmulqdq $0x11, H_POW2, TMP1, TMP0
+ vpxor TMP0, HI, HI
+ vpunpckhqdq TMP1, TMP1, TMP0
+ vpxor TMP1, TMP0, TMP0
+ vpclmulqdq $0x00, H_POW21_XORED, TMP0, TMP0
+ vpxor TMP0, MI, MI
+
+ // Vector 3: [M_6, M_7] * [H^2, H^1], with the high qwords of
+ // H_POW21_XORED.
+ vmovdqu 3*32(DATA), TMP1
+.if \bswap_data
+ vpshufb BSWAP_MASK, TMP1, TMP1
+.endif
+ vpclmulqdq $0x00, H_POW1, TMP1, TMP0
+ vpxor TMP0, LO, LO
+ vpclmulqdq $0x11, H_POW1, TMP1, TMP0
+ vpxor TMP0, HI, HI
+ vpunpckhqdq TMP1, TMP1, TMP0
+ vpxor TMP1, TMP0, TMP0
+ vpclmulqdq $0x10, H_POW21_XORED, TMP0, TMP0
+ vpxor TMP0, MI, MI
+
+ // Vector 0: [M_0 + ACC, M_1] * [H^8, H^7], with the low qwords of
+ // H_POW43_XORED. Finish MI by adding LO and HI to it (in parallel with
+ // vector 0's middle product), then reduce both lanes and XOR them
+ // together into ACC_XMM. Writing ACC_XMM zeroes lane 1 of ACC.
+ vmovdqu 0*32(DATA), TMP1
+.if \bswap_data
+ vpshufb BSWAP_MASK, TMP1, TMP1
+.endif
+ vpxor ACC, TMP1, TMP1
+ vpclmulqdq $0x00, H_POW4, TMP1, TMP0
+ vpxor TMP0, LO, LO
+ vpclmulqdq $0x11, H_POW4, TMP1, TMP0
+ vpxor TMP0, HI, HI
+ vpunpckhqdq TMP1, TMP1, TMP0
+ vpxor TMP1, TMP0, TMP0
+ vpclmulqdq $0x00, H_POW43_XORED, TMP0, TMP0
+ vpxor LO, MI, MI
+ vpxor HI, MI, MI
+ vpxor TMP0, MI, MI
+ _gf128hash_reduce LO, MI, HI, GFPOLY, TMP0
+ vextracti128 $1, HI, TMP0_XMM
+ vpxor TMP0_XMM, HI_XMM, ACC_XMM
+.endm
+
+// void {ghash,polyval}_blocks_vpclmul_avx2(struct polyval_elem *acc,
+// const struct gf128hash_key_x86 *key,
+// const u8 *data, size_t nblocks);
+//
+// Update the POLYVAL-format accumulator @acc with @nblocks >= 1 full 16-byte
+// blocks from @data. If \bswap_data=1 (GHASH), each block is byte-reversed to
+// convert it to POLYVAL format.
+.macro _gf128hash_blocks bswap_data
+ .set ACC_PTR, %rdi
+ .set KEY, %rsi
+ .set DATA, %rdx
+ .set NBLOCKS, %rcx
+ .set POWERS_PTR, %rax
+
+ .set TMP0, %ymm0
+ .set TMP0_XMM, %xmm0
+ .set TMP1, %ymm1
+ .set TMP1_XMM, %xmm1
+ .set TMP2, %ymm2
+ .set TMP2_XMM, %xmm2
+ .set LO, %ymm3
+ .set LO_XMM, %xmm3
+ .set MI, %ymm4
+ .set HI, %ymm5
+ .set HI_XMM, %xmm5
+ .set ACC, %ymm6
+ .set ACC_XMM, %xmm6
+ .set GFPOLY, %ymm7
+ .set GFPOLY_XMM, %xmm7
+ .set H_POW43_XORED, %ymm8
+ .set H_POW21_XORED, %ymm9
+ .set H_POW4, %ymm10
+ .set H_POW3, %ymm11
+ .set H_POW2, %ymm12
+ .set H_POW1, %ymm13
+ .set BSWAP_MASK, %ymm14
+ .set BSWAP_MASK_XMM, %xmm14
+
+ // Load the accumulator into lane 0 of ACC. This zeroes lane 1.
+ vmovdqu (ACC_PTR), ACC_XMM
+
+ // Handle nblocks == 1 specially, using only xmm registers. This case
+ // is common (e.g. for 1-block associated data, the incremental AES-GCM
+ // lengths block, and the HCTR2 length and tweak blocks).
+ cmp $1, NBLOCKS
+ jne .Lblocks_more_than_1block\@
+
+ vmovq .Lgfpoly(%rip), GFPOLY_XMM
+ vmovdqu (DATA), TMP0_XMM
+.if \bswap_data
+ vpshufb .Lbswap_mask(%rip), TMP0_XMM, TMP0_XMM
+.endif
+ vpxor TMP0_XMM, ACC_XMM, ACC_XMM
+ vmovdqu OFFSETOFEND_H_POWERS-16(KEY), TMP0_XMM // H^1
+ vmovdqu OFFSETOF_H_TIMES_XM64(KEY), TMP1_XMM // H^1 * x^-64
+ _gf128hash_mul TMP0_XMM, TMP1_XMM, ACC_XMM, ACC_XMM, GFPOLY_XMM, \
+ TMP2_XMM, LO_XMM
+ vmovdqu ACC_XMM, (ACC_PTR)
+ // No vzeroupper is needed since only xmm registers were used.
+ RET
+
+.Lblocks_more_than_1block\@:
+.if \bswap_data
+ vbroadcasti128 .Lbswap_mask(%rip), BSWAP_MASK
+.endif
+ vpbroadcastq .Lgfpoly(%rip), GFPOLY
+
+ // Process 8 blocks (128 bytes) at a time.
+ sub $8, NBLOCKS
+ jb .Lblocks_tail\@
+ // Load the XOR'd halves (in H^8,H^6|H^7,H^5 and H^4,H^2|H^3,H^1 order)
+ // and key powers.
+ vmovdqu OFFSETOF_H_POWERS_XORED+0*32(KEY), H_POW43_XORED
+ vmovdqu OFFSETOF_H_POWERS_XORED+1*32(KEY), H_POW21_XORED
+ vmovdqu OFFSETOF_H_POWERS+0*32(KEY), H_POW4 // [H^8, H^7]
+ vmovdqu OFFSETOF_H_POWERS+1*32(KEY), H_POW3 // [H^6, H^5]
+ vmovdqu OFFSETOF_H_POWERS+2*32(KEY), H_POW2 // [H^4, H^3]
+ vmovdqu OFFSETOF_H_POWERS+3*32(KEY), H_POW1 // [H^2, H^1]
+.Lblocks_loop_4x\@:
+ _gf128hash_4x \bswap_data
+ sub $-128, DATA // DATA += 128 (compact imm8)
+ sub $8, NBLOCKS
+ jae .Lblocks_loop_4x\@
+
+.Lblocks_tail\@:
+ add $8, NBLOCKS
+ jz .Lblocks_done\@
+ cmp $1, NBLOCKS
+ je .Lblocks_1x\@
+
+ // 2 <= n = NBLOCKS <= 7 blocks remain. Multiply them by H^n..H^1,
+ // starting at h_powers[8-n], using schoolbook multiplication. Sum the
+ // unreduced products, and reduce the sum once at the end.
+ mov NBLOCKS, POWERS_PTR
+ shl $4, POWERS_PTR
+ neg POWERS_PTR
+ lea OFFSETOFEND_H_POWERS(KEY,POWERS_PTR), POWERS_PTR
+
+ // Blocks 0 and 1. Since lane 1 of ACC is zero, vpxor adds ACC to
+ // block 0 only.
+ vmovdqu (DATA), TMP0
+.if \bswap_data
+ vpshufb BSWAP_MASK, TMP0, TMP0
+.endif
+ vpxor ACC, TMP0, TMP0
+ vmovdqu (POWERS_PTR), TMP1
+ vpclmulqdq $0x00, TMP1, TMP0, LO
+ vpclmulqdq $0x01, TMP1, TMP0, MI
+ vpclmulqdq $0x10, TMP1, TMP0, TMP2
+ vpclmulqdq $0x11, TMP1, TMP0, HI
+ vpxor TMP2, MI, MI
+ add $32, DATA
+ add $32, POWERS_PTR
+
+ // Any further pairs of blocks (0, 1, or 2 iterations). Subtract 4
+ // (2 for blocks 0..1 just processed, plus 2 for the next pair).
+ sub $4, NBLOCKS
+ jb .Lblocks_tail_odd\@
+.Lblocks_tail_loop\@:
+ vmovdqu (DATA), TMP0
+.if \bswap_data
+ vpshufb BSWAP_MASK, TMP0, TMP0
+.endif
+ vmovdqu (POWERS_PTR), TMP1
+ vpclmulqdq $0x00, TMP1, TMP0, TMP2
+ vpxor TMP2, LO, LO
+ vpclmulqdq $0x01, TMP1, TMP0, TMP2
+ vpxor TMP2, MI, MI
+ vpclmulqdq $0x10, TMP1, TMP0, TMP2
+ vpxor TMP2, MI, MI
+ vpclmulqdq $0x11, TMP1, TMP0, TMP2
+ vpxor TMP2, HI, HI
+ add $32, DATA
+ add $32, POWERS_PTR
+ sub $2, NBLOCKS
+ jae .Lblocks_tail_loop\@
+.Lblocks_tail_odd\@:
+ add $2, NBLOCKS
+ jz .Lblocks_tail_reduce\@
+
+ // Final block, if n is odd: multiply it by H^1 in lane 0. The 128-bit
+ // vpclmulqdq zeroes lane 1 of TMP2, so lane 1 of LO, MI, and HI is
+ // unchanged.
+ vmovdqu (DATA), TMP0_XMM
+.if \bswap_data
+ vpshufb BSWAP_MASK_XMM, TMP0_XMM, TMP0_XMM
+.endif
+ vmovdqu (POWERS_PTR), TMP1_XMM
+ vpclmulqdq $0x00, TMP1_XMM, TMP0_XMM, TMP2_XMM
+ vpxor TMP2, LO, LO
+ vpclmulqdq $0x01, TMP1_XMM, TMP0_XMM, TMP2_XMM
+ vpxor TMP2, MI, MI
+ vpclmulqdq $0x10, TMP1_XMM, TMP0_XMM, TMP2_XMM
+ vpxor TMP2, MI, MI
+ vpclmulqdq $0x11, TMP1_XMM, TMP0_XMM, TMP2_XMM
+ vpxor TMP2, HI, HI
+
+.Lblocks_tail_reduce\@:
+ // Reduce both lanes, and XOR them together into ACC_XMM.
+ _gf128hash_reduce LO, MI, HI, GFPOLY, TMP0
+ vextracti128 $1, HI, TMP0_XMM
+ vpxor TMP0_XMM, HI_XMM, ACC_XMM
+ jmp .Lblocks_done\@
+
+.Lblocks_1x\@:
+ // 1 block remains after 8-block stride(s): ACC = dot(ACC + M_0, H^1).
+ vmovdqu (DATA), TMP0_XMM
+.if \bswap_data
+ vpshufb BSWAP_MASK_XMM, TMP0_XMM, TMP0_XMM
+.endif
+ vpxor TMP0_XMM, ACC_XMM, ACC_XMM
+ vmovdqu OFFSETOFEND_H_POWERS-16(KEY), TMP0_XMM // H^1
+ vmovdqu OFFSETOF_H_TIMES_XM64(KEY), TMP1_XMM // H^1 * x^-64
+ _gf128hash_mul TMP0_XMM, TMP1_XMM, ACC_XMM, ACC_XMM, GFPOLY_XMM, \
+ TMP2_XMM, LO_XMM
+
+.Lblocks_done\@:
+ vmovdqu ACC_XMM, (ACC_PTR)
+ vzeroupper
+ RET
+.endm
+
+SYM_FUNC_START(ghash_preparekey_vpclmul_avx2)
+ _gf128hash_preparekey 1
+SYM_FUNC_END(ghash_preparekey_vpclmul_avx2)
+SYM_FUNC_START(polyval_preparekey_vpclmul_avx2)
+ _gf128hash_preparekey 0
+SYM_FUNC_END(polyval_preparekey_vpclmul_avx2)
+SYM_FUNC_START(ghash_blocks_vpclmul_avx2)
+ _gf128hash_blocks 1
+SYM_FUNC_END(ghash_blocks_vpclmul_avx2)
+SYM_FUNC_START(polyval_blocks_vpclmul_avx2)
+ _gf128hash_blocks 0
+SYM_FUNC_END(polyval_blocks_vpclmul_avx2)
diff --git a/lib/crypto/x86/gf128hash.h b/lib/crypto/x86/gf128hash.h
index 9abdea7f9ba86..4470be6dfea2d 100644
--- a/lib/crypto/x86/gf128hash.h
+++ b/lib/crypto/x86/gf128hash.h
@@ -19,6 +19,7 @@ static_assert(sizeof(struct gf128hash_key_x86) == 208);
static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_pclmul);
static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_pclmul_avx);
+static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_vpclmul_avx2);
void gf128hash_mul_pclmul(struct polyval_elem *acc,
const struct gf128hash_key_x86 *key);
@@ -29,10 +30,14 @@ void ghash_preparekey_pclmul(struct gf128hash_key_x86 *key,
const u8 raw_key[GHASH_BLOCK_SIZE]);
void ghash_preparekey_pclmul_avx(struct gf128hash_key_x86 *key,
const u8 raw_key[GHASH_BLOCK_SIZE]);
+void ghash_preparekey_vpclmul_avx2(struct gf128hash_key_x86 *key,
+ const u8 raw_key[GHASH_BLOCK_SIZE]);
void polyval_preparekey_pclmul(struct gf128hash_key_x86 *key,
const u8 raw_key[POLYVAL_BLOCK_SIZE]);
void polyval_preparekey_pclmul_avx(struct gf128hash_key_x86 *key,
const u8 raw_key[POLYVAL_BLOCK_SIZE]);
+void polyval_preparekey_vpclmul_avx2(struct gf128hash_key_x86 *key,
+ const u8 raw_key[POLYVAL_BLOCK_SIZE]);
void ghash_blocks_pclmul(struct polyval_elem *acc,
const struct gf128hash_key_x86 *key, const u8 *data,
@@ -40,12 +45,18 @@ void ghash_blocks_pclmul(struct polyval_elem *acc,
void ghash_blocks_pclmul_avx(struct polyval_elem *acc,
const struct gf128hash_key_x86 *key,
const u8 *data, size_t nblocks);
+void ghash_blocks_vpclmul_avx2(struct polyval_elem *acc,
+ const struct gf128hash_key_x86 *key,
+ const u8 *data, size_t nblocks);
void polyval_blocks_pclmul(struct polyval_elem *acc,
const struct gf128hash_key_x86 *key, const u8 *data,
size_t nblocks);
void polyval_blocks_pclmul_avx(struct polyval_elem *acc,
const struct gf128hash_key_x86 *key,
const u8 *data, size_t nblocks);
+void polyval_blocks_vpclmul_avx2(struct polyval_elem *acc,
+ const struct gf128hash_key_x86 *key,
+ const u8 *data, size_t nblocks);
static __always_inline void
__gf128hash_mul_x86_fpu(struct polyval_elem *acc,
@@ -62,7 +73,9 @@ __ghash_blocks_x86_fpu(struct polyval_elem *acc,
const struct gf128hash_key_x86 *key, const u8 *data,
size_t nblocks)
{
- if (static_branch_likely(&have_pclmul_avx))
+ if (static_branch_likely(&have_vpclmul_avx2))
+ ghash_blocks_vpclmul_avx2(acc, key, data, nblocks);
+ else if (static_branch_likely(&have_pclmul_avx))
ghash_blocks_pclmul_avx(acc, key, data, nblocks);
else
ghash_blocks_pclmul(acc, key, data, nblocks);
@@ -88,7 +101,12 @@ gf128hash_preparekey_x86(struct gf128hash_key_x86 *key, const u8 raw_key[16],
{
if (static_branch_likely(&have_pclmul) && likely(irq_fpu_usable())) {
kernel_fpu_begin_mask(0);
- if (static_branch_likely(&have_pclmul_avx)) {
+ if (static_branch_likely(&have_vpclmul_avx2)) {
+ if (is_ghash)
+ ghash_preparekey_vpclmul_avx2(key, raw_key);
+ else
+ polyval_preparekey_vpclmul_avx2(key, raw_key);
+ } else if (static_branch_likely(&have_pclmul_avx)) {
if (is_ghash)
ghash_preparekey_pclmul_avx(key, raw_key);
else
@@ -112,9 +130,24 @@ gf128hash_preparekey_x86(struct gf128hash_key_x86 *key, const u8 raw_key[16],
polyval_mul_generic(&key->h_powers[i],
&key->h_powers[NUM_H_POWERS - 1]);
}
- for (int i = 0; i < NUM_H_POWERS; i++)
- key->h_powers_xored[i] = key->h_powers[i].lo ^
- key->h_powers[i].hi;
+ /*
+ * Decide which key layout to use. This fallback code is reached when
+ * the static keys aren't set, which might be because
+ * gf128hash_mod_init() hasn't run yet. Check the CPU features directly
+ * here so that keys prepared before gf128hash_mod_init() have the same
+ * layout as keys prepared afterwards.
+ */
+ if (gf128hash_use_vpclmul_avx2()) {
+ static const u8 indices[8] = { 0, 2, 1, 3, 4, 6, 5, 7 };
+
+ for (int i = 0; i < NUM_H_POWERS; i++)
+ key->h_powers_xored[i] = key->h_powers[indices[i]].lo ^
+ key->h_powers[indices[i]].hi;
+ } else {
+ for (int i = 0; i < NUM_H_POWERS; i++)
+ key->h_powers_xored[i] = key->h_powers[i].lo ^
+ key->h_powers[i].hi;
+ }
polyval_mul_xm64_generic(&key->h_times_xm64,
&key->h_powers[NUM_H_POWERS - 1]);
}
@@ -174,6 +207,8 @@ gf128hash_blocks_x86(struct polyval_elem *acc,
kernel_fpu_begin_mask(0);
if (is_ghash)
__ghash_blocks_x86_fpu(acc, key, data, n);
+ else if (static_branch_likely(&have_vpclmul_avx2))
+ polyval_blocks_vpclmul_avx2(acc, key, data, n);
else if (static_branch_likely(&have_pclmul_avx))
polyval_blocks_pclmul_avx(acc, key, data, n);
else
@@ -216,7 +251,10 @@ static void gf128hash_mod_init_arch(void)
if (!gf128hash_use_pclmul())
return;
- if (gf128hash_use_pclmul_avx())
+ if (gf128hash_use_pclmul_avx()) {
static_branch_enable(&have_pclmul_avx);
+ if (gf128hash_use_vpclmul_avx2())
+ static_branch_enable(&have_vpclmul_avx2);
+ }
static_branch_enable(&have_pclmul);
}
--
2.56.1
next prev parent reply other threads:[~2026-11-29 20:04 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 20:02 [PATCH 00/11] Migrate x86_64 AES-GCM code into library Eric Biggers
2026-11-29 20:02 ` [PATCH 01/11] crypto: aes - Flush dcache in CBC-CTS and XTS linear fast paths Eric Biggers
2026-11-29 20:02 ` [PATCH 02/11] lib/crypto: gf128hash: Link libgf128hash before libaes Eric Biggers
2026-11-29 20:02 ` [PATCH 03/11] lib/crypto: aes-gcm: Add one-shot AES-GCM arch hooks Eric Biggers
2026-11-29 20:02 ` [PATCH 04/11] crypto: aes - Add linear fast path for AES-GCM Eric Biggers
2026-11-29 20:02 ` [PATCH 05/11] lib/crypto: x86/gf128hash: Optimize GHASH and POLYVAL with PCLMULQDQ and AVX Eric Biggers
2026-11-29 20:02 ` Eric Biggers [this message]
2026-11-29 20:02 ` [PATCH 07/11] lib/crypto: x86/gf128hash: Optimize GHASH and POLYVAL with VPCLMULQDQ and AVX512 Eric Biggers
2026-11-29 20:02 ` [PATCH 08/11] lib/crypto: x86/aes-gcm: Migrate AES-NI optimized code into library Eric Biggers
2026-11-29 20:02 ` [PATCH 09/11] lib/crypto: x86/aes-gcm: Migrate VAES+AVX2 " Eric Biggers
2026-11-29 20:02 ` [PATCH 10/11] lib/crypto: x86/aes-gcm: Migrate VAES+AVX512 " Eric Biggers
2026-11-29 20:02 ` [PATCH 11/11] crypto: x86/aesni-intel - Remove empty module Eric Biggers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261129200245.146503-7-ebiggers@kernel.org \
--to=ebiggers@kernel.org \
--cc=Jason@zx2c4.com \
--cc=antonio@mandelbit.com \
--cc=ardb@kernel.org \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox