Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

lib/crypto: blake2s: Consolidate into single C translation unit

As was done with the other algorithms, reorganize the BLAKE2s code so
that the generic implementation and the arch-specific "glue" code is
consolidated into a single translation unit, so that the compiler will
inline the functions and automatically decide whether to include the
generic code in the resulting binary or not.

Similarly, also consolidate the build rules into
lib/crypto/{Makefile,Kconfig}. This removes the last uses of
lib/crypto/{arm,x86}/{Makefile,Kconfig}, so remove those too.

Don't keep the !KMSAN dependency. It was needed only for other
algorithms such as ChaCha that initialize memory from assembly code.

Reviewed-by: Ard Biesheuvel <ardb@kernel.org>
Link: https://lore.kernel.org/r/20250827151131.27733-12-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>

+47 -111
-19
include/crypto/internal/blake2s.h
··· 1 - /* SPDX-License-Identifier: GPL-2.0 OR MIT */ 2 - /* 3 - * Helper functions for BLAKE2s implementations. 4 - * Keep this in sync with the corresponding BLAKE2b header. 5 - */ 6 - 7 - #ifndef _CRYPTO_INTERNAL_BLAKE2S_H 8 - #define _CRYPTO_INTERNAL_BLAKE2S_H 9 - 10 - #include <crypto/blake2s.h> 11 - #include <linux/string.h> 12 - 13 - void blake2s_compress_generic(struct blake2s_state *state, const u8 *block, 14 - size_t nblocks, const u32 inc); 15 - 16 - void blake2s_compress(struct blake2s_state *state, const u8 *block, 17 - size_t nblocks, const u32 inc); 18 - 19 - #endif /* _CRYPTO_INTERNAL_BLAKE2S_H */
+6 -23
lib/crypto/Kconfig
··· 28 28 config CRYPTO_LIB_GF128MUL 29 29 tristate 30 30 31 - config CRYPTO_ARCH_HAVE_LIB_BLAKE2S 32 - bool 33 - help 34 - Declares whether the architecture provides an arch-specific 35 - accelerated implementation of the Blake2s library interface, 36 - either builtin or as a module. 31 + # BLAKE2s support is always built-in, so there's no CRYPTO_LIB_BLAKE2S option. 37 32 38 - config CRYPTO_LIB_BLAKE2S_GENERIC 39 - def_bool !CRYPTO_ARCH_HAVE_LIB_BLAKE2S 40 - help 41 - This symbol can be depended upon by arch implementations of the 42 - Blake2s library interface that require the generic code as a 43 - fallback, e.g., for SIMD implementations. If no arch specific 44 - implementation is enabled, this implementation serves the users 45 - of CRYPTO_LIB_BLAKE2S. 33 + config CRYPTO_LIB_BLAKE2S_ARCH 34 + bool 35 + depends on !UML 36 + default y if ARM 37 + default y if X86_64 46 38 47 39 config CRYPTO_LIB_CHACHA 48 40 tristate ··· 199 207 tristate 200 208 201 209 source "lib/crypto/tests/Kconfig" 202 - 203 - if !KMSAN # avoid false positives from assembly 204 - if ARM 205 - source "lib/crypto/arm/Kconfig" 206 - endif 207 - if X86 208 - source "lib/crypto/x86/Kconfig" 209 - endif 210 - endif 211 210 212 211 endmenu
+8 -5
lib/crypto/Makefile
··· 29 29 30 30 obj-$(CONFIG_CRYPTO_LIB_GF128MUL) += gf128mul.o 31 31 32 + ################################################################################ 33 + 32 34 # blake2s is used by the /dev/random driver which is always builtin 33 - obj-y += libblake2s.o 34 - libblake2s-y := blake2s.o 35 + obj-y += blake2s.o 36 + ifeq ($(CONFIG_CRYPTO_LIB_BLAKE2S_ARCH),y) 37 + CFLAGS_blake2s.o += -I$(src)/$(SRCARCH) 38 + obj-$(CONFIG_ARM) += arm/blake2s-core.o 39 + obj-$(CONFIG_X86) += x86/blake2s-core.o 40 + endif 35 41 36 42 ################################################################################ 37 43 ··· 261 255 262 256 obj-$(CONFIG_CRYPTO_LIB_SM3) += libsm3.o 263 257 libsm3-y := sm3.o 264 - 265 - obj-$(CONFIG_ARM) += arm/ 266 - obj-$(CONFIG_X86) += x86/ 267 258 268 259 # clean-files must be defined unconditionally 269 260 clean-files += arm/sha256-core.S arm/sha512-core.S
-14
lib/crypto/arm/Kconfig
··· 1 - # SPDX-License-Identifier: GPL-2.0-only 2 - 3 - config CRYPTO_BLAKE2S_ARM 4 - def_bool y 5 - select CRYPTO_ARCH_HAVE_LIB_BLAKE2S 6 - help 7 - BLAKE2s cryptographic hash function (RFC 7693) 8 - 9 - Architecture: arm 10 - 11 - This is faster than the generic implementations of BLAKE2s and 12 - BLAKE2b, but slower than the NEON implementation of BLAKE2b. 13 - There is no NEON implementation of BLAKE2s, since NEON doesn't 14 - really help with it.
-4
lib/crypto/arm/Makefile
··· 1 - # SPDX-License-Identifier: GPL-2.0-only 2 - 3 - obj-$(CONFIG_CRYPTO_BLAKE2S_ARM) += libblake2s-arm.o 4 - libblake2s-arm-y := blake2s-core.o blake2s-glue.o
+4 -1
lib/crypto/arm/blake2s-core.S
··· 1 1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 2 /* 3 - * BLAKE2s digest algorithm, ARM scalar implementation 3 + * BLAKE2s digest algorithm, ARM scalar implementation. This is faster 4 + * than the generic implementations of BLAKE2s and BLAKE2b, but slower 5 + * than the NEON implementation of BLAKE2b. There is no NEON 6 + * implementation of BLAKE2s, since NEON doesn't really help with it. 4 7 * 5 8 * Copyright 2020 Google LLC 6 9 *
-7
lib/crypto/arm/blake2s-glue.c
··· 1 - // SPDX-License-Identifier: GPL-2.0-or-later 2 - 3 - #include <crypto/internal/blake2s.h> 4 - #include <linux/module.h> 5 - 6 - /* defined in blake2s-core.S */ 7 - EXPORT_SYMBOL(blake2s_compress);
+5
lib/crypto/arm/blake2s.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + 3 + /* defined in blake2s-core.S */ 4 + void blake2s_compress(struct blake2s_state *state, const u8 *block, 5 + size_t nblocks, u32 inc);
+19 -10
lib/crypto/blake2s.c
··· 8 8 * 9 9 */ 10 10 11 - #include <crypto/internal/blake2s.h> 11 + #include <crypto/blake2s.h> 12 12 #include <linux/bug.h> 13 13 #include <linux/export.h> 14 14 #include <linux/kernel.h> ··· 16 16 #include <linux/string.h> 17 17 #include <linux/types.h> 18 18 19 - #ifdef CONFIG_CRYPTO_LIB_BLAKE2S_GENERIC 20 19 static const u8 blake2s_sigma[10][16] = { 21 20 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, 22 21 { 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }, ··· 36 37 state->t[1] += (state->t[0] < inc); 37 38 } 38 39 39 - void blake2s_compress(struct blake2s_state *state, const u8 *block, 40 - size_t nblocks, const u32 inc) 41 - __weak __alias(blake2s_compress_generic); 42 - 43 - void blake2s_compress_generic(struct blake2s_state *state, const u8 *block, 44 - size_t nblocks, const u32 inc) 40 + static void __maybe_unused 41 + blake2s_compress_generic(struct blake2s_state *state, const u8 *block, 42 + size_t nblocks, const u32 inc) 45 43 { 46 44 u32 m[16]; 47 45 u32 v[16]; ··· 103 107 --nblocks; 104 108 } 105 109 } 106 - EXPORT_SYMBOL(blake2s_compress_generic); 107 - #endif /* CONFIG_CRYPTO_LIB_BLAKE2S_GENERIC */ 110 + 111 + #ifdef CONFIG_CRYPTO_LIB_BLAKE2S_ARCH 112 + #include "blake2s.h" /* $(SRCARCH)/blake2s.h */ 113 + #else 114 + #define blake2s_compress blake2s_compress_generic 115 + #endif 108 116 109 117 static inline void blake2s_set_lastblock(struct blake2s_state *state) 110 118 { ··· 151 151 memzero_explicit(state, sizeof(*state)); 152 152 } 153 153 EXPORT_SYMBOL(blake2s_final); 154 + 155 + #ifdef blake2s_mod_init_arch 156 + static int __init blake2s_mod_init(void) 157 + { 158 + blake2s_mod_init_arch(); 159 + return 0; 160 + } 161 + subsys_initcall(blake2s_mod_init); 162 + #endif 154 163 155 164 MODULE_DESCRIPTION("BLAKE2s hash function"); 156 165 MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");
-13
lib/crypto/x86/Kconfig
··· 1 - # SPDX-License-Identifier: GPL-2.0-only 2 - 3 - config CRYPTO_BLAKE2S_X86 4 - def_bool y 5 - depends on 64BIT 6 - select CRYPTO_LIB_BLAKE2S_GENERIC 7 - select CRYPTO_ARCH_HAVE_LIB_BLAKE2S 8 - help 9 - BLAKE2s cryptographic hash function (RFC 7693) 10 - 11 - Architecture: x86_64 using: 12 - - SSSE3 (Supplemental SSE3) 13 - - AVX-512 (Advanced Vector Extensions-512)
-4
lib/crypto/x86/Makefile
··· 1 - # SPDX-License-Identifier: GPL-2.0-only 2 - 3 - obj-$(CONFIG_CRYPTO_BLAKE2S_X86) += libblake2s-x86_64.o 4 - libblake2s-x86_64-y := blake2s-core.o blake2s-glue.o
+5 -11
lib/crypto/x86/blake2s-glue.c lib/crypto/x86/blake2s.h
··· 1 - // SPDX-License-Identifier: GPL-2.0 OR MIT 1 + /* SPDX-License-Identifier: GPL-2.0 OR MIT */ 2 2 /* 3 3 * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. 4 4 */ ··· 7 7 #include <asm/fpu/api.h> 8 8 #include <asm/processor.h> 9 9 #include <asm/simd.h> 10 - #include <crypto/internal/blake2s.h> 11 - #include <linux/init.h> 12 10 #include <linux/jump_label.h> 13 11 #include <linux/kernel.h> 14 12 #include <linux/sizes.h> ··· 21 23 static __ro_after_init DEFINE_STATIC_KEY_FALSE(blake2s_use_ssse3); 22 24 static __ro_after_init DEFINE_STATIC_KEY_FALSE(blake2s_use_avx512); 23 25 24 - void blake2s_compress(struct blake2s_state *state, const u8 *block, 25 - size_t nblocks, const u32 inc) 26 + static void blake2s_compress(struct blake2s_state *state, const u8 *block, 27 + size_t nblocks, const u32 inc) 26 28 { 27 29 /* SIMD disables preemption, so relax after processing each page. */ 28 30 BUILD_BUG_ON(SZ_4K / BLAKE2S_BLOCK_SIZE < 8); ··· 47 49 block += blocks * BLAKE2S_BLOCK_SIZE; 48 50 } while (nblocks); 49 51 } 50 - EXPORT_SYMBOL(blake2s_compress); 51 52 52 - static int __init blake2s_mod_init(void) 53 + #define blake2s_mod_init_arch blake2s_mod_init_arch 54 + static void blake2s_mod_init_arch(void) 53 55 { 54 56 if (boot_cpu_has(X86_FEATURE_SSSE3)) 55 57 static_branch_enable(&blake2s_use_ssse3); ··· 61 63 cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM | 62 64 XFEATURE_MASK_AVX512, NULL)) 63 65 static_branch_enable(&blake2s_use_avx512); 64 - 65 - return 0; 66 66 } 67 - 68 - subsys_initcall(blake2s_mod_init);