1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * OpenSSL/Cryptogams accelerated Poly1305 transform for ARM
4 *
5 * Copyright (C) 2019 Linaro Ltd. <ard.biesheuvel@linaro.org>
6 */
7
8 #include <asm/hwcap.h>
9 #include <asm/simd.h>
10 #include <linux/cpufeature.h>
11 #include <linux/jump_label.h>
12 #include <linux/kernel.h>
13
14 asmlinkage void poly1305_block_init(struct poly1305_block_state *state,
15 const u8 raw_key[POLY1305_BLOCK_SIZE]);
16 asmlinkage void poly1305_blocks_arm(struct poly1305_block_state *state,
17 const u8 *src, u32 len, u32 hibit);
18 asmlinkage void poly1305_blocks_neon(struct poly1305_block_state *state,
19 const u8 *src, u32 len, u32 hibit);
20 asmlinkage void poly1305_emit(const struct poly1305_state *state,
21 u8 digest[POLY1305_DIGEST_SIZE],
22 const u32 nonce[4]);
23
24 static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_neon);
25
poly1305_blocks(struct poly1305_block_state * state,const u8 * src,unsigned int len,u32 padbit)26 static void poly1305_blocks(struct poly1305_block_state *state, const u8 *src,
27 unsigned int len, u32 padbit)
28 {
29 if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) &&
30 static_branch_likely(&have_neon) && likely(may_use_simd())) {
31 do {
32 unsigned int todo = min_t(unsigned int, len, SZ_4K);
33
34 scoped_ksimd()
35 poly1305_blocks_neon(state, src, todo, padbit);
36
37 len -= todo;
38 src += todo;
39 } while (len);
40 } else
41 poly1305_blocks_arm(state, src, len, padbit);
42 }
43
44 #ifdef CONFIG_KERNEL_MODE_NEON
45 #define poly1305_mod_init_arch poly1305_mod_init_arch
poly1305_mod_init_arch(void)46 static void poly1305_mod_init_arch(void)
47 {
48 if (elf_hwcap & HWCAP_NEON)
49 static_branch_enable(&have_neon);
50 }
51 #endif /* CONFIG_KERNEL_MODE_NEON */
52