Skip to content

Commit 68546e5

Browse files
author
Eric Biggers
committed
lib/crypto: curve25519: Consolidate into single module
Reorganize the Curve25519 library code: - Build a single libcurve25519 module, instead of up to three modules: libcurve25519, libcurve25519-generic, and an arch-specific module. - Move the arch-specific Curve25519 code from arch/$(SRCARCH)/crypto/ to lib/crypto/$(SRCARCH)/. Centralize the build rules into lib/crypto/Makefile and lib/crypto/Kconfig. - Include the arch-specific code directly in lib/crypto/curve25519.c via a header, rather than using a separate .c file. - Eliminate the entanglement with CRYPTO. CRYPTO_LIB_CURVE25519 no longer selects CRYPTO, and the arch-specific Curve25519 code no longer depends on CRYPTO. This brings Curve25519 in line with the latest conventions for lib/crypto/, used by other algorithms. The exception is that I kept the generic code in separate translation units for now. (Some of the function names collide between the x86 and generic Curve25519 code. And the Curve25519 functions are very long anyway, so inlining doesn't matter as much for Curve25519 as it does for some other algorithms.) Link: https://lore.kernel.org/r/20250906213523.84915-11-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 8c06b33 commit 68546e5

16 files changed

Lines changed: 86 additions & 188 deletions

File tree

arch/arm/crypto/Kconfig

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@
22

33
menu "Accelerated Cryptographic Algorithms for CPU (arm)"
44

5-
config CRYPTO_CURVE25519_NEON
6-
tristate
7-
depends on KERNEL_MODE_NEON
8-
select CRYPTO_LIB_CURVE25519_GENERIC
9-
select CRYPTO_ARCH_HAVE_LIB_CURVE25519
10-
default CRYPTO_LIB_CURVE25519_INTERNAL
11-
help
12-
Curve25519 algorithm
13-
14-
Architecture: arm with
15-
- NEON (Advanced SIMD) extensions
16-
175
config CRYPTO_GHASH_ARM_CE
186
tristate "Hash functions: GHASH (PMULL/NEON/ARMv8 Crypto Extensions)"
197
depends on KERNEL_MODE_NEON

arch/arm/crypto/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ obj-$(CONFIG_CRYPTO_AES_ARM) += aes-arm.o
77
obj-$(CONFIG_CRYPTO_AES_ARM_BS) += aes-arm-bs.o
88
obj-$(CONFIG_CRYPTO_BLAKE2B_NEON) += blake2b-neon.o
99
obj-$(CONFIG_CRYPTO_NHPOLY1305_NEON) += nhpoly1305-neon.o
10-
obj-$(CONFIG_CRYPTO_CURVE25519_NEON) += curve25519-neon.o
1110

1211
obj-$(CONFIG_CRYPTO_AES_ARM_CE) += aes-arm-ce.o
1312
obj-$(CONFIG_CRYPTO_GHASH_ARM_CE) += ghash-arm-ce.o
@@ -18,4 +17,3 @@ blake2b-neon-y := blake2b-neon-core.o blake2b-neon-glue.o
1817
aes-arm-ce-y := aes-ce-core.o aes-ce-glue.o
1918
ghash-arm-ce-y := ghash-ce-core.o ghash-ce-glue.o
2019
nhpoly1305-neon-y := nh-neon-core.o nhpoly1305-neon-glue.o
21-
curve25519-neon-y := curve25519-core.o curve25519-glue.o

arch/powerpc/crypto/Kconfig

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@
22

33
menu "Accelerated Cryptographic Algorithms for CPU (powerpc)"
44

5-
config CRYPTO_CURVE25519_PPC64
6-
tristate
7-
depends on PPC64 && CPU_LITTLE_ENDIAN
8-
select CRYPTO_LIB_CURVE25519_GENERIC
9-
select CRYPTO_ARCH_HAVE_LIB_CURVE25519
10-
default CRYPTO_LIB_CURVE25519_INTERNAL
11-
help
12-
Curve25519 algorithm
13-
14-
Architecture: PowerPC64
15-
- Little-endian
16-
175
config CRYPTO_AES_PPC_SPE
186
tristate "Ciphers: AES, modes: ECB/CBC/CTR/XTS (SPE)"
197
depends on SPE

arch/powerpc/crypto/Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@
88
obj-$(CONFIG_CRYPTO_AES_PPC_SPE) += aes-ppc-spe.o
99
obj-$(CONFIG_CRYPTO_AES_GCM_P10) += aes-gcm-p10-crypto.o
1010
obj-$(CONFIG_CRYPTO_DEV_VMX_ENCRYPT) += vmx-crypto.o
11-
obj-$(CONFIG_CRYPTO_CURVE25519_PPC64) += curve25519-ppc64le.o
1211

1312
aes-ppc-spe-y := aes-spe-core.o aes-spe-keys.o aes-tab-4k.o aes-spe-modes.o aes-spe-glue.o
1413
aes-gcm-p10-crypto-y := aes-gcm-p10-glue.o aes-gcm-p10.o ghashp10-ppc.o aesp10-ppc.o
1514
vmx-crypto-objs := vmx.o aesp8-ppc.o ghashp8-ppc.o aes.o aes_cbc.o aes_ctr.o aes_xts.o ghash.o
16-
curve25519-ppc64le-y := curve25519-ppc64le-core.o curve25519-ppc64le_asm.o
1715

1816
ifeq ($(CONFIG_CPU_LITTLE_ENDIAN),y)
1917
override flavour := linux-ppc64le

arch/x86/crypto/Kconfig

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@
22

33
menu "Accelerated Cryptographic Algorithms for CPU (x86)"
44

5-
config CRYPTO_CURVE25519_X86
6-
tristate
7-
depends on 64BIT
8-
select CRYPTO_LIB_CURVE25519_GENERIC
9-
select CRYPTO_ARCH_HAVE_LIB_CURVE25519
10-
default CRYPTO_LIB_CURVE25519_INTERNAL
11-
help
12-
Curve25519 algorithm
13-
14-
Architecture: x86_64 using:
15-
- ADX (large integer arithmetic)
16-
175
config CRYPTO_AES_NI_INTEL
186
tristate "Ciphers: AES, modes: ECB, CBC, CTS, CTR, XCTR, XTS, GCM (AES-NI/VAES)"
197
select CRYPTO_AEAD

arch/x86/crypto/Makefile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ nhpoly1305-sse2-y := nh-sse2-x86_64.o nhpoly1305-sse2-glue.o
6262
obj-$(CONFIG_CRYPTO_NHPOLY1305_AVX2) += nhpoly1305-avx2.o
6363
nhpoly1305-avx2-y := nh-avx2-x86_64.o nhpoly1305-avx2-glue.o
6464

65-
obj-$(CONFIG_CRYPTO_CURVE25519_X86) += curve25519-x86_64.o
66-
6765
obj-$(CONFIG_CRYPTO_SM3_AVX_X86_64) += sm3-avx-x86_64.o
6866
sm3-avx-x86_64-y := sm3-avx-asm_64.o sm3_avx_glue.o
6967

@@ -81,6 +79,3 @@ aria-aesni-avx2-x86_64-y := aria-aesni-avx2-asm_64.o aria_aesni_avx2_glue.o
8179

8280
obj-$(CONFIG_CRYPTO_ARIA_GFNI_AVX512_X86_64) += aria-gfni-avx512-x86_64.o
8381
aria-gfni-avx512-x86_64-y := aria-gfni-avx512-asm_64.o aria_gfni_avx512_glue.o
84-
85-
# Disable GCOV in odd or sensitive code
86-
GCOV_PROFILE_curve25519-x86_64.o := n

include/crypto/curve25519.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,10 @@ enum curve25519_lengths {
1313
CURVE25519_KEY_SIZE = 32
1414
};
1515

16-
extern const u8 curve25519_null_point[];
17-
extern const u8 curve25519_base_point[];
18-
1916
void curve25519_generic(u8 out[CURVE25519_KEY_SIZE],
2017
const u8 scalar[CURVE25519_KEY_SIZE],
2118
const u8 point[CURVE25519_KEY_SIZE]);
2219

23-
void curve25519_arch(u8 out[CURVE25519_KEY_SIZE],
24-
const u8 scalar[CURVE25519_KEY_SIZE],
25-
const u8 point[CURVE25519_KEY_SIZE]);
26-
27-
void curve25519_base_arch(u8 pub[CURVE25519_KEY_SIZE],
28-
const u8 secret[CURVE25519_KEY_SIZE]);
29-
3020
bool __must_check curve25519(u8 mypublic[CURVE25519_KEY_SIZE],
3121
const u8 secret[CURVE25519_KEY_SIZE],
3222
const u8 basepoint[CURVE25519_KEY_SIZE]);

lib/crypto/Kconfig

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,35 +54,24 @@ config CRYPTO_LIB_CHACHA_ARCH
5454
default y if S390
5555
default y if X86_64
5656

57-
config CRYPTO_ARCH_HAVE_LIB_CURVE25519
58-
bool
59-
help
60-
Declares whether the architecture provides an arch-specific
61-
accelerated implementation of the Curve25519 library interface,
62-
either builtin or as a module.
63-
64-
config CRYPTO_LIB_CURVE25519_GENERIC
57+
config CRYPTO_LIB_CURVE25519
6558
tristate
6659
select CRYPTO_LIB_UTILS
6760
help
68-
This symbol can be depended upon by arch implementations of the
69-
Curve25519 library interface that require the generic code as a
70-
fallback, e.g., for SIMD implementations. If no arch specific
71-
implementation is enabled, this implementation serves the users
72-
of CRYPTO_LIB_CURVE25519.
61+
The Curve25519 library functions. Select this if your module uses any
62+
of the functions from <crypto/curve25519.h>.
7363

74-
config CRYPTO_LIB_CURVE25519_INTERNAL
75-
tristate
76-
select CRYPTO_LIB_CURVE25519_GENERIC if CRYPTO_ARCH_HAVE_LIB_CURVE25519=n
64+
config CRYPTO_LIB_CURVE25519_ARCH
65+
bool
66+
depends on CRYPTO_LIB_CURVE25519 && !UML && !KMSAN
67+
default y if ARM && KERNEL_MODE_NEON
68+
default y if PPC64 && CPU_LITTLE_ENDIAN
69+
default y if X86_64
7770

78-
config CRYPTO_LIB_CURVE25519
79-
tristate
80-
select CRYPTO
81-
select CRYPTO_LIB_CURVE25519_INTERNAL
82-
help
83-
Enable the Curve25519 library interface. This interface may be
84-
fulfilled by either the generic implementation or an arch-specific
85-
one, if one is available and enabled.
71+
config CRYPTO_LIB_CURVE25519_GENERIC
72+
bool
73+
depends on CRYPTO_LIB_CURVE25519
74+
default y if !CRYPTO_LIB_CURVE25519_ARCH || ARM || X86_64
8675

8776
config CRYPTO_LIB_DES
8877
tristate

lib/crypto/Makefile

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,31 @@ obj-$(CONFIG_CRYPTO_LIB_CHACHA20POLY1305) += libchacha20poly1305.o
7676
libchacha20poly1305-y += chacha20poly1305.o
7777
libchacha20poly1305-$(CONFIG_CRYPTO_SELFTESTS) += chacha20poly1305-selftest.o
7878

79-
obj-$(CONFIG_CRYPTO_LIB_CURVE25519_GENERIC) += libcurve25519-generic.o
80-
libcurve25519-generic-y := curve25519-fiat32.o
81-
libcurve25519-generic-$(CONFIG_ARCH_SUPPORTS_INT128) := curve25519-hacl64.o
82-
libcurve25519-generic-y += curve25519-generic.o
79+
################################################################################
80+
81+
obj-$(CONFIG_CRYPTO_LIB_CURVE25519) += libcurve25519.o
82+
libcurve25519-y := curve25519.o
83+
84+
# Disable GCOV in odd or sensitive code
85+
GCOV_PROFILE_curve25519.o := n
86+
87+
ifeq ($(CONFIG_ARCH_SUPPORTS_INT128),y)
88+
libcurve25519-$(CONFIG_CRYPTO_LIB_CURVE25519_GENERIC) += curve25519-hacl64.o
89+
else
90+
libcurve25519-$(CONFIG_CRYPTO_LIB_CURVE25519_GENERIC) += curve25519-fiat32.o
91+
endif
8392
# clang versions prior to 18 may blow out the stack with KASAN
8493
ifeq ($(call clang-min-version, 180000),)
8594
KASAN_SANITIZE_curve25519-hacl64.o := n
8695
endif
8796

88-
obj-$(CONFIG_CRYPTO_LIB_CURVE25519) += libcurve25519.o
89-
libcurve25519-y += curve25519.o
97+
ifeq ($(CONFIG_CRYPTO_LIB_CURVE25519_ARCH),y)
98+
CFLAGS_curve25519.o += -I$(src)/$(SRCARCH)
99+
libcurve25519-$(CONFIG_ARM) += arm/curve25519-core.o
100+
libcurve25519-$(CONFIG_PPC) += powerpc/curve25519-ppc64le_asm.o
101+
endif
102+
103+
################################################################################
90104

91105
obj-$(CONFIG_CRYPTO_LIB_DES) += libdes.o
92106
libdes-y := des.o

0 commit comments

Comments
 (0)