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/neon.h>
10 #include <asm/simd.h>
11 #include <linux/cpufeature.h>
12 #include <linux/jump_label.h>
13 #include <linux/kernel.h>
14
15 asmlinkage void poly1305_block_init(struct poly1305_block_state *state,
16 const u8 raw_key[POLY1305_BLOCK_SIZE]);
17 asmlinkage void poly1305_blocks_arm(struct poly1305_block_state *state,
18 const u8 *src, u32 len, u32 hibit);
19 asmlinkage void poly1305_blocks_neon(struct poly1305_block_state *state,
20 const u8 *src, u32 len, u32 hibit);
21 asmlinkage void poly1305_emit(const struct poly1305_state *state,
22 u8 digest[POLY1305_DIGEST_SIZE],
23 const u32 nonce[4]);
24
25 static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_neon);
26
poly1305_blocks(struct poly1305_block_state * state,const u8 * src,unsigned int len,u32 padbit)27 static void poly1305_blocks(struct poly1305_block_state *state, const u8 *src,
28 unsigned int len, u32 padbit)
29 {
30 if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) &&
31 static_branch_likely(&have_neon) && likely(may_use_simd())) {
32 do {
33 unsigned int todo = min_t(unsigned int, len, SZ_4K);
34
35 kernel_neon_begin();
36 poly1305_blocks_neon(state, src, todo, padbit);
37 kernel_neon_end();
38
39 len -= todo;
40 src += todo;
41 } while (len);
42 } else
43 poly1305_blocks_arm(state, src, len, padbit);
44 }
45
46 #ifdef CONFIG_KERNEL_MODE_NEON
47 #define poly1305_mod_init_arch poly1305_mod_init_arch
poly1305_mod_init_arch(void)48 static void poly1305_mod_init_arch(void)
49 {
50 if (elf_hwcap & HWCAP_NEON)
51 static_branch_enable(&have_neon);
52 }
53 #endif /* CONFIG_KERNEL_MODE_NEON */
54