1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * OpenSSL/Cryptogams accelerated Poly1305 transform for arm64
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_arm64(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 (static_branch_likely(&have_neon) && likely(may_use_simd())) {
31 do {
32 unsigned int todo = min_t(unsigned int, len, SZ_4K);
33
34 kernel_neon_begin();
35 poly1305_blocks_neon(state, src, todo, padbit);
36 kernel_neon_end();
37
38 len -= todo;
39 src += todo;
40 } while (len);
41 } else
42 poly1305_blocks_arm64(state, src, len, padbit);
43 }
44
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 (cpu_have_named_feature(ASIMD))
49 static_branch_enable(&have_neon);
50 }
51