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

lib/crypto: arm64/nh: Migrate optimized code into library

Migrate the arm64 NEON implementation of NH into lib/crypto/. This
makes the nh() function be optimized on arm64 kernels.

Note: this temporarily makes the adiantum template not utilize the arm64
optimized NH code. This is resolved in a later commit that converts the
adiantum template to use nh() instead of "nhpoly1305".

Link: https://lore.kernel.org/r/20251211011846.8179-5-ebiggers@kernel.org
Signed-off-by: Eric Biggers <ebiggers@kernel.org>

+37 -94
-10
arch/arm64/crypto/Kconfig
··· 15 15 Architecture: arm64 using: 16 16 - ARMv8 Crypto Extensions 17 17 18 - config CRYPTO_NHPOLY1305_NEON 19 - tristate "Hash functions: NHPoly1305 (NEON)" 20 - depends on KERNEL_MODE_NEON 21 - select CRYPTO_NHPOLY1305 22 - help 23 - NHPoly1305 hash function (Adiantum) 24 - 25 - Architecture: arm64 using: 26 - - NEON (Advanced SIMD) extensions 27 - 28 18 config CRYPTO_SM3_NEON 29 19 tristate "Hash functions: SM3 (NEON)" 30 20 depends on KERNEL_MODE_NEON
-3
arch/arm64/crypto/Makefile
··· 41 41 obj-$(CONFIG_CRYPTO_AES_ARM64_NEON_BLK) += aes-neon-blk.o 42 42 aes-neon-blk-y := aes-glue-neon.o aes-neon.o 43 43 44 - obj-$(CONFIG_CRYPTO_NHPOLY1305_NEON) += nhpoly1305-neon.o 45 - nhpoly1305-neon-y := nh-neon-core.o nhpoly1305-neon-glue.o 46 - 47 44 obj-$(CONFIG_CRYPTO_AES_ARM64) += aes-arm64.o 48 45 aes-arm64-y := aes-cipher-core.o aes-cipher-glue.o 49 46
+1 -2
arch/arm64/crypto/nh-neon-core.S lib/crypto/arm64/nh-neon-core.S
··· 8 8 */ 9 9 10 10 #include <linux/linkage.h> 11 - #include <linux/cfi_types.h> 12 11 13 12 KEY .req x0 14 13 MESSAGE .req x1 ··· 62 63 * 63 64 * It's guaranteed that message_len % 16 == 0. 64 65 */ 65 - SYM_TYPED_FUNC_START(nh_neon) 66 + SYM_FUNC_START(nh_neon) 66 67 67 68 ld1 {K0.4s,K1.4s}, [KEY], #32 68 69 movi PASS0_SUMS.2d, #0
-79
arch/arm64/crypto/nhpoly1305-neon-glue.c
··· 1 - // SPDX-License-Identifier: GPL-2.0 2 - /* 3 - * NHPoly1305 - ε-almost-∆-universal hash function for Adiantum 4 - * (ARM64 NEON accelerated version) 5 - * 6 - * Copyright 2018 Google LLC 7 - */ 8 - 9 - #include <asm/neon.h> 10 - #include <asm/simd.h> 11 - #include <crypto/internal/hash.h> 12 - #include <crypto/internal/simd.h> 13 - #include <crypto/nhpoly1305.h> 14 - #include <linux/module.h> 15 - 16 - asmlinkage void nh_neon(const u32 *key, const u8 *message, size_t message_len, 17 - __le64 hash[NH_NUM_PASSES]); 18 - 19 - static int nhpoly1305_neon_update(struct shash_desc *desc, 20 - const u8 *src, unsigned int srclen) 21 - { 22 - if (srclen < 64 || !crypto_simd_usable()) 23 - return crypto_nhpoly1305_update(desc, src, srclen); 24 - 25 - do { 26 - unsigned int n = min_t(unsigned int, srclen, SZ_4K); 27 - 28 - scoped_ksimd() 29 - crypto_nhpoly1305_update_helper(desc, src, n, nh_neon); 30 - src += n; 31 - srclen -= n; 32 - } while (srclen); 33 - return 0; 34 - } 35 - 36 - static int nhpoly1305_neon_digest(struct shash_desc *desc, 37 - const u8 *src, unsigned int srclen, u8 *out) 38 - { 39 - return crypto_nhpoly1305_init(desc) ?: 40 - nhpoly1305_neon_update(desc, src, srclen) ?: 41 - crypto_nhpoly1305_final(desc, out); 42 - } 43 - 44 - static struct shash_alg nhpoly1305_alg = { 45 - .base.cra_name = "nhpoly1305", 46 - .base.cra_driver_name = "nhpoly1305-neon", 47 - .base.cra_priority = 200, 48 - .base.cra_ctxsize = sizeof(struct nhpoly1305_key), 49 - .base.cra_module = THIS_MODULE, 50 - .digestsize = POLY1305_DIGEST_SIZE, 51 - .init = crypto_nhpoly1305_init, 52 - .update = nhpoly1305_neon_update, 53 - .final = crypto_nhpoly1305_final, 54 - .digest = nhpoly1305_neon_digest, 55 - .setkey = crypto_nhpoly1305_setkey, 56 - .descsize = sizeof(struct nhpoly1305_state), 57 - }; 58 - 59 - static int __init nhpoly1305_mod_init(void) 60 - { 61 - if (!cpu_have_named_feature(ASIMD)) 62 - return -ENODEV; 63 - 64 - return crypto_register_shash(&nhpoly1305_alg); 65 - } 66 - 67 - static void __exit nhpoly1305_mod_exit(void) 68 - { 69 - crypto_unregister_shash(&nhpoly1305_alg); 70 - } 71 - 72 - module_init(nhpoly1305_mod_init); 73 - module_exit(nhpoly1305_mod_exit); 74 - 75 - MODULE_DESCRIPTION("NHPoly1305 ε-almost-∆-universal hash function (NEON-accelerated)"); 76 - MODULE_LICENSE("GPL v2"); 77 - MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>"); 78 - MODULE_ALIAS_CRYPTO("nhpoly1305"); 79 - MODULE_ALIAS_CRYPTO("nhpoly1305-neon");
+1
lib/crypto/Kconfig
··· 118 118 bool 119 119 depends on CRYPTO_LIB_NH && !UML 120 120 default y if ARM && KERNEL_MODE_NEON 121 + default y if ARM64 && KERNEL_MODE_NEON 121 122 122 123 config CRYPTO_LIB_POLY1305 123 124 tristate
+1
lib/crypto/Makefile
··· 136 136 ifeq ($(CONFIG_CRYPTO_LIB_NH_ARCH),y) 137 137 CFLAGS_nh.o += -I$(src)/$(SRCARCH) 138 138 libnh-$(CONFIG_ARM) += arm/nh-neon-core.o 139 + libnh-$(CONFIG_ARM64) += arm64/nh-neon-core.o 139 140 endif 140 141 141 142 ################################################################################
+34
lib/crypto/arm64/nh.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* 3 + * ARM64 accelerated implementation of NH 4 + * 5 + * Copyright 2018 Google LLC 6 + */ 7 + 8 + #include <asm/hwcap.h> 9 + #include <asm/simd.h> 10 + #include <linux/cpufeature.h> 11 + 12 + static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_neon); 13 + 14 + asmlinkage void nh_neon(const u32 *key, const u8 *message, size_t message_len, 15 + __le64 hash[NH_NUM_PASSES]); 16 + 17 + static bool nh_arch(const u32 *key, const u8 *message, size_t message_len, 18 + __le64 hash[NH_NUM_PASSES]) 19 + { 20 + if (static_branch_likely(&have_neon) && message_len >= 64 && 21 + may_use_simd()) { 22 + scoped_ksimd() 23 + nh_neon(key, message, message_len, hash); 24 + return true; 25 + } 26 + return false; 27 + } 28 + 29 + #define nh_mod_init_arch nh_mod_init_arch 30 + static void nh_mod_init_arch(void) 31 + { 32 + if (cpu_have_named_feature(ASIMD)) 33 + static_branch_enable(&have_neon); 34 + }