Skip to content

Commit 17ba610

Browse files
author
Eric Biggers
committed
lib/crypto: x86/sm3: Migrate optimized code into library
Instead of exposing the x86-optimized SM3 code via an x86-specific crypto_shash algorithm, instead just implement the sm3_blocks() library function. This is much simpler, it makes the SM3 library functions be x86-optimized, and it fixes the longstanding issue where the x86-optimized SM3 code was disabled by default. SM3 still remains available through crypto_shash, but individual architectures no longer need to handle it. Tweak the prototype of sm3_transform_avx() to match what the library expects, including changing the block count to size_t. Note that the assembly code actually already treated this argument as size_t. Acked-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20260321040935.410034-10-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 5f6bbba commit 17ba610

7 files changed

Lines changed: 47 additions & 123 deletions

File tree

arch/x86/crypto/Kconfig

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -331,17 +331,4 @@ config CRYPTO_AEGIS128_AESNI_SSE2
331331
- AES-NI (AES New Instructions)
332332
- SSE4.1 (Streaming SIMD Extensions 4.1)
333333

334-
config CRYPTO_SM3_AVX_X86_64
335-
tristate "Hash functions: SM3 (AVX)"
336-
depends on 64BIT
337-
select CRYPTO_HASH
338-
select CRYPTO_LIB_SM3
339-
help
340-
SM3 secure hash function as defined by OSCCA GM/T 0004-2012 SM3
341-
342-
Architecture: x86_64 using:
343-
- AVX (Advanced Vector Extensions)
344-
345-
If unsure, say N.
346-
347334
endmenu

arch/x86/crypto/Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ aesni-intel-$(CONFIG_64BIT) += aes-ctr-avx-x86_64.o \
5050
aes-gcm-vaes-avx512.o \
5151
aes-xts-avx-x86_64.o
5252

53-
obj-$(CONFIG_CRYPTO_SM3_AVX_X86_64) += sm3-avx-x86_64.o
54-
sm3-avx-x86_64-y := sm3-avx-asm_64.o sm3_avx_glue.o
55-
5653
obj-$(CONFIG_CRYPTO_SM4_AESNI_AVX_X86_64) += sm4-aesni-avx-x86_64.o
5754
sm4-aesni-avx-x86_64-y := sm4-aesni-avx-asm_64.o sm4_aesni_avx_glue.o
5855

arch/x86/crypto/sm3_avx_glue.c

Lines changed: 0 additions & 100 deletions
This file was deleted.

lib/crypto/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ config CRYPTO_LIB_SM3_ARCH
282282
default y if ARM64
283283
default y if RISCV && 64BIT && TOOLCHAIN_HAS_VECTOR_CRYPTO && \
284284
RISCV_EFFICIENT_VECTOR_UNALIGNED_ACCESS
285+
default y if X86_64
285286

286287
source "lib/crypto/tests/Kconfig"
287288

lib/crypto/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ CFLAGS_sm3.o += -I$(src)/$(SRCARCH)
375375
libsm3-$(CONFIG_ARM64) += arm64/sm3-ce-core.o \
376376
arm64/sm3-neon-core.o
377377
libsm3-$(CONFIG_RISCV) += riscv/sm3-riscv64-zvksh-zvkb.o
378+
libsm3-$(CONFIG_X86) += x86/sm3-avx-asm_64.o
378379
endif # CONFIG_CRYPTO_LIB_SM3_ARCH
379380

380381
################################################################################
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
*/
1313

1414
#include <linux/linkage.h>
15-
#include <linux/cfi_types.h>
1615
#include <asm/frame.h>
1716

18-
/* Context structure */
17+
/* State structure */
1918

2019
#define state_h0 0
2120
#define state_h1 4
@@ -325,13 +324,13 @@
325324
/*
326325
* Transform nblocks*64 bytes (nblocks*16 32-bit words) at DATA.
327326
*
328-
* void sm3_transform_avx(struct sm3_state *state,
329-
* const u8 *data, int nblocks);
327+
* void sm3_transform_avx(struct sm3_block_state *state,
328+
* const u8 *data, size_t nblocks);
330329
*/
331-
SYM_TYPED_FUNC_START(sm3_transform_avx)
330+
SYM_FUNC_START(sm3_transform_avx)
332331
/* input:
333-
* %rdi: ctx, CTX
334-
* %rsi: data (64*nblks bytes)
332+
* %rdi: state
333+
* %rsi: data
335334
* %rdx: nblocks
336335
*/
337336
vzeroupper;

lib/crypto/x86/sm3.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/*
3+
* SM3 optimized for x86_64
4+
*
5+
* Copyright 2026 Google LLC
6+
*/
7+
#include <asm/fpu/api.h>
8+
#include <linux/static_call.h>
9+
10+
asmlinkage void sm3_transform_avx(struct sm3_block_state *state,
11+
const u8 *data, size_t nblocks);
12+
13+
static void sm3_blocks_avx(struct sm3_block_state *state,
14+
const u8 *data, size_t nblocks)
15+
{
16+
if (likely(irq_fpu_usable())) {
17+
kernel_fpu_begin();
18+
sm3_transform_avx(state, data, nblocks);
19+
kernel_fpu_end();
20+
} else {
21+
sm3_blocks_generic(state, data, nblocks);
22+
}
23+
}
24+
25+
DEFINE_STATIC_CALL(sm3_blocks_x86, sm3_blocks_generic);
26+
27+
static void sm3_blocks(struct sm3_block_state *state,
28+
const u8 *data, size_t nblocks)
29+
{
30+
static_call(sm3_blocks_x86)(state, data, nblocks);
31+
}
32+
33+
#define sm3_mod_init_arch sm3_mod_init_arch
34+
static void sm3_mod_init_arch(void)
35+
{
36+
if (boot_cpu_has(X86_FEATURE_AVX) && boot_cpu_has(X86_FEATURE_BMI2) &&
37+
cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL))
38+
static_call_update(sm3_blocks_x86, sm3_blocks_avx);
39+
}

0 commit comments

Comments
 (0)