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 <crypto/internal/poly1305.h>
11 #include <linux/cpufeature.h>
12 #include <linux/jump_label.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/unaligned.h>
16 
17 asmlinkage void poly1305_block_init_arch(
18 	struct poly1305_block_state *state,
19 	const u8 raw_key[POLY1305_BLOCK_SIZE]);
20 EXPORT_SYMBOL_GPL(poly1305_block_init_arch);
21 asmlinkage void poly1305_blocks_arm(struct poly1305_block_state *state,
22 				    const u8 *src, u32 len, u32 hibit);
23 asmlinkage void poly1305_blocks_neon(struct poly1305_block_state *state,
24 				     const u8 *src, u32 len, u32 hibit);
25 asmlinkage void poly1305_emit_arch(const struct poly1305_state *state,
26 				   u8 digest[POLY1305_DIGEST_SIZE],
27 				   const u32 nonce[4]);
28 EXPORT_SYMBOL_GPL(poly1305_emit_arch);
29 
30 void __weak poly1305_blocks_neon(struct poly1305_block_state *state,
31 				 const u8 *src, u32 len, u32 hibit)
32 {
33 }
34 
35 static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_neon);
36 
37 void poly1305_blocks_arch(struct poly1305_block_state *state, const u8 *src,
38 			  unsigned int len, u32 padbit)
39 {
40 	len = round_down(len, POLY1305_BLOCK_SIZE);
41 	if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) &&
42 	    static_branch_likely(&have_neon)) {
43 		do {
44 			unsigned int todo = min_t(unsigned int, len, SZ_4K);
45 
46 			kernel_neon_begin();
47 			poly1305_blocks_neon(state, src, todo, padbit);
48 			kernel_neon_end();
49 
50 			len -= todo;
51 			src += todo;
52 		} while (len);
53 	} else
54 		poly1305_blocks_arm(state, src, len, padbit);
55 }
56 EXPORT_SYMBOL_GPL(poly1305_blocks_arch);
57 
58 bool poly1305_is_arch_optimized(void)
59 {
60 	/* We always can use at least the ARM scalar implementation. */
61 	return true;
62 }
63 EXPORT_SYMBOL(poly1305_is_arch_optimized);
64 
65 static int __init arm_poly1305_mod_init(void)
66 {
67 	if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) &&
68 	    (elf_hwcap & HWCAP_NEON))
69 		static_branch_enable(&have_neon);
70 	return 0;
71 }
72 subsys_initcall(arm_poly1305_mod_init);
73 
74 static void __exit arm_poly1305_mod_exit(void)
75 {
76 }
77 module_exit(arm_poly1305_mod_exit);
78 
79 MODULE_DESCRIPTION("Accelerated Poly1305 transform for ARM");
80 MODULE_LICENSE("GPL v2");
81