Skip to content

Commit 5f6bbba

Browse files
author
Eric Biggers
committed
lib/crypto: riscv/sm3: Migrate optimized code into library
Instead of exposing the riscv-optimized SM3 code via a riscv-specific crypto_shash algorithm, instead just implement the sm3_blocks() library function. This is much simpler, it makes the SM3 library functions be riscv-optimized, and it fixes the longstanding issue where the riscv-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_zvksh_zvkb() to match what the library expects, including changing the block count to size_t. Note that the assembly code already treated it as size_t. Note: to see the diff from arch/riscv/crypto/sm3-riscv64-glue.c to lib/crypto/riscv/sm3.h, view this commit with 'git show -M10'. Acked-by: Ard Biesheuvel <ardb@kernel.org> Link: https://lore.kernel.org/r/20260321040935.410034-9-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 9f69f52 commit 5f6bbba

7 files changed

Lines changed: 44 additions & 114 deletions

File tree

arch/riscv/crypto/Kconfig

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,6 @@ config CRYPTO_AES_RISCV64
1717
- Zvkb vector crypto extension (CTR)
1818
- Zvkg vector crypto extension (XTS)
1919

20-
config CRYPTO_SM3_RISCV64
21-
tristate "Hash functions: SM3 (ShangMi 3)"
22-
depends on 64BIT && TOOLCHAIN_HAS_VECTOR_CRYPTO && \
23-
RISCV_EFFICIENT_VECTOR_UNALIGNED_ACCESS
24-
select CRYPTO_HASH
25-
select CRYPTO_LIB_SM3
26-
help
27-
SM3 (ShangMi 3) secure hash function (OSCCA GM/T 0004-2012)
28-
29-
Architecture: riscv64 using:
30-
- Zvksh vector crypto extension
31-
- Zvkb vector crypto extension
32-
3320
config CRYPTO_SM4_RISCV64
3421
tristate "Ciphers: SM4 (ShangMi 4)"
3522
depends on 64BIT && TOOLCHAIN_HAS_VECTOR_CRYPTO && \

arch/riscv/crypto/Makefile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@ obj-$(CONFIG_CRYPTO_AES_RISCV64) += aes-riscv64.o
44
aes-riscv64-y := aes-riscv64-glue.o aes-riscv64-zvkned.o \
55
aes-riscv64-zvkned-zvbb-zvkg.o aes-riscv64-zvkned-zvkb.o
66

7-
obj-$(CONFIG_CRYPTO_SM3_RISCV64) += sm3-riscv64.o
8-
sm3-riscv64-y := sm3-riscv64-glue.o sm3-riscv64-zvksh-zvkb.o
9-
107
obj-$(CONFIG_CRYPTO_SM4_RISCV64) += sm4-riscv64.o
118
sm4-riscv64-y := sm4-riscv64-glue.o sm4-riscv64-zvksed-zvkb.o

arch/riscv/crypto/sm3-riscv64-glue.c

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

lib/crypto/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ config CRYPTO_LIB_SM3_ARCH
280280
bool
281281
depends on CRYPTO_LIB_SM3 && !UML
282282
default y if ARM64
283+
default y if RISCV && 64BIT && TOOLCHAIN_HAS_VECTOR_CRYPTO && \
284+
RISCV_EFFICIENT_VECTOR_UNALIGNED_ACCESS
283285

284286
source "lib/crypto/tests/Kconfig"
285287

lib/crypto/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ ifeq ($(CONFIG_CRYPTO_LIB_SM3_ARCH),y)
374374
CFLAGS_sm3.o += -I$(src)/$(SRCARCH)
375375
libsm3-$(CONFIG_ARM64) += arm64/sm3-ce-core.o \
376376
arm64/sm3-neon-core.o
377+
libsm3-$(CONFIG_RISCV) += riscv/sm3-riscv64-zvksh-zvkb.o
377378
endif # CONFIG_CRYPTO_LIB_SM3_ARCH
378379

379380
################################################################################

arch/riscv/crypto/sm3-riscv64-zvksh-zvkb.S renamed to lib/crypto/riscv/sm3-riscv64-zvksh-zvkb.S

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@
8080
// For the next 8 rounds, w0 and w1 are swapped.
8181
.endm
8282

83-
// void sm3_transform_zvksh_zvkb(u32 state[8], const u8 *data, int num_blocks);
83+
// void sm3_transform_zvksh_zvkb(struct sm3_block_state *state,
84+
// const u8 *data, size_t nblocks);
8485
SYM_FUNC_START(sm3_transform_zvksh_zvkb)
8586

8687
// Load the state and endian-swap each 32-bit word.

lib/crypto/riscv/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 using the RISC-V vector crypto extensions
4+
*
5+
* Copyright (C) 2023 VRULL GmbH
6+
* Author: Heiko Stuebner <heiko.stuebner@vrull.eu>
7+
*
8+
* Copyright (C) 2023 SiFive, Inc.
9+
* Author: Jerry Shih <jerry.shih@sifive.com>
10+
*/
11+
12+
#include <asm/simd.h>
13+
#include <asm/vector.h>
14+
15+
static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_extensions);
16+
17+
asmlinkage void sm3_transform_zvksh_zvkb(struct sm3_block_state *state,
18+
const u8 *data, size_t nblocks);
19+
20+
static void sm3_blocks(struct sm3_block_state *state,
21+
const u8 *data, size_t nblocks)
22+
{
23+
if (static_branch_likely(&have_extensions) && likely(may_use_simd())) {
24+
kernel_vector_begin();
25+
sm3_transform_zvksh_zvkb(state, data, nblocks);
26+
kernel_vector_end();
27+
} else {
28+
sm3_blocks_generic(state, data, nblocks);
29+
}
30+
}
31+
32+
#define sm3_mod_init_arch sm3_mod_init_arch
33+
static void sm3_mod_init_arch(void)
34+
{
35+
if (riscv_isa_extension_available(NULL, ZVKSH) &&
36+
riscv_isa_extension_available(NULL, ZVKB) &&
37+
riscv_vector_vlen() >= 128)
38+
static_branch_enable(&have_extensions);
39+
}

0 commit comments

Comments
 (0)