14a32e5dcSEric Biggers // SPDX-License-Identifier: GPL-2.0 24a32e5dcSEric Biggers /* 34a32e5dcSEric Biggers * OpenSSL/Cryptogams accelerated Poly1305 transform for ARM 44a32e5dcSEric Biggers * 54a32e5dcSEric Biggers * Copyright (C) 2019 Linaro Ltd. <ard.biesheuvel@linaro.org> 64a32e5dcSEric Biggers */ 74a32e5dcSEric Biggers 84a32e5dcSEric Biggers #include <asm/hwcap.h> 94a32e5dcSEric Biggers #include <asm/neon.h> 10*52c3e242SEric Biggers #include <asm/simd.h> 114a32e5dcSEric Biggers #include <crypto/internal/poly1305.h> 124a32e5dcSEric Biggers #include <linux/cpufeature.h> 134a32e5dcSEric Biggers #include <linux/jump_label.h> 144a32e5dcSEric Biggers #include <linux/kernel.h> 154a32e5dcSEric Biggers #include <linux/module.h> 164a32e5dcSEric Biggers #include <linux/unaligned.h> 174a32e5dcSEric Biggers 184a32e5dcSEric Biggers asmlinkage void poly1305_block_init_arch( 194a32e5dcSEric Biggers struct poly1305_block_state *state, 204a32e5dcSEric Biggers const u8 raw_key[POLY1305_BLOCK_SIZE]); 214a32e5dcSEric Biggers EXPORT_SYMBOL_GPL(poly1305_block_init_arch); 224a32e5dcSEric Biggers asmlinkage void poly1305_blocks_arm(struct poly1305_block_state *state, 234a32e5dcSEric Biggers const u8 *src, u32 len, u32 hibit); 244a32e5dcSEric Biggers asmlinkage void poly1305_blocks_neon(struct poly1305_block_state *state, 254a32e5dcSEric Biggers const u8 *src, u32 len, u32 hibit); 264a32e5dcSEric Biggers asmlinkage void poly1305_emit_arch(const struct poly1305_state *state, 274a32e5dcSEric Biggers u8 digest[POLY1305_DIGEST_SIZE], 284a32e5dcSEric Biggers const u32 nonce[4]); 294a32e5dcSEric Biggers EXPORT_SYMBOL_GPL(poly1305_emit_arch); 304a32e5dcSEric Biggers 314a32e5dcSEric Biggers void __weak poly1305_blocks_neon(struct poly1305_block_state *state, 324a32e5dcSEric Biggers const u8 *src, u32 len, u32 hibit) 334a32e5dcSEric Biggers { 344a32e5dcSEric Biggers } 354a32e5dcSEric Biggers 364a32e5dcSEric Biggers static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_neon); 374a32e5dcSEric Biggers 384a32e5dcSEric Biggers void poly1305_blocks_arch(struct poly1305_block_state *state, const u8 *src, 394a32e5dcSEric Biggers unsigned int len, u32 padbit) 404a32e5dcSEric Biggers { 414a32e5dcSEric Biggers len = round_down(len, POLY1305_BLOCK_SIZE); 424a32e5dcSEric Biggers if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && 43*52c3e242SEric Biggers static_branch_likely(&have_neon) && likely(may_use_simd())) { 444a32e5dcSEric Biggers do { 454a32e5dcSEric Biggers unsigned int todo = min_t(unsigned int, len, SZ_4K); 464a32e5dcSEric Biggers 474a32e5dcSEric Biggers kernel_neon_begin(); 484a32e5dcSEric Biggers poly1305_blocks_neon(state, src, todo, padbit); 494a32e5dcSEric Biggers kernel_neon_end(); 504a32e5dcSEric Biggers 514a32e5dcSEric Biggers len -= todo; 524a32e5dcSEric Biggers src += todo; 534a32e5dcSEric Biggers } while (len); 544a32e5dcSEric Biggers } else 554a32e5dcSEric Biggers poly1305_blocks_arm(state, src, len, padbit); 564a32e5dcSEric Biggers } 574a32e5dcSEric Biggers EXPORT_SYMBOL_GPL(poly1305_blocks_arch); 584a32e5dcSEric Biggers 594a32e5dcSEric Biggers bool poly1305_is_arch_optimized(void) 604a32e5dcSEric Biggers { 614a32e5dcSEric Biggers /* We always can use at least the ARM scalar implementation. */ 624a32e5dcSEric Biggers return true; 634a32e5dcSEric Biggers } 644a32e5dcSEric Biggers EXPORT_SYMBOL(poly1305_is_arch_optimized); 654a32e5dcSEric Biggers 664a32e5dcSEric Biggers static int __init arm_poly1305_mod_init(void) 674a32e5dcSEric Biggers { 684a32e5dcSEric Biggers if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && 694a32e5dcSEric Biggers (elf_hwcap & HWCAP_NEON)) 704a32e5dcSEric Biggers static_branch_enable(&have_neon); 714a32e5dcSEric Biggers return 0; 724a32e5dcSEric Biggers } 734a32e5dcSEric Biggers subsys_initcall(arm_poly1305_mod_init); 744a32e5dcSEric Biggers 754a32e5dcSEric Biggers static void __exit arm_poly1305_mod_exit(void) 764a32e5dcSEric Biggers { 774a32e5dcSEric Biggers } 784a32e5dcSEric Biggers module_exit(arm_poly1305_mod_exit); 794a32e5dcSEric Biggers 804a32e5dcSEric Biggers MODULE_DESCRIPTION("Accelerated Poly1305 transform for ARM"); 814a32e5dcSEric Biggers MODULE_LICENSE("GPL v2"); 82