xref: /linux/arch/arm/crypto/nhpoly1305-neon-glue.c (revision bd628c1bed7902ec1f24ba0fe70758949146abbe)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * NHPoly1305 - ε-almost-∆-universal hash function for Adiantum
4  * (NEON accelerated version)
5  *
6  * Copyright 2018 Google LLC
7  */
8 
9 #include <asm/neon.h>
10 #include <asm/simd.h>
11 #include <crypto/internal/hash.h>
12 #include <crypto/nhpoly1305.h>
13 #include <linux/module.h>
14 
15 asmlinkage void nh_neon(const u32 *key, const u8 *message, size_t message_len,
16 			u8 hash[NH_HASH_BYTES]);
17 
18 /* wrapper to avoid indirect call to assembly, which doesn't work with CFI */
19 static void _nh_neon(const u32 *key, const u8 *message, size_t message_len,
20 		     __le64 hash[NH_NUM_PASSES])
21 {
22 	nh_neon(key, message, message_len, (u8 *)hash);
23 }
24 
25 static int nhpoly1305_neon_update(struct shash_desc *desc,
26 				  const u8 *src, unsigned int srclen)
27 {
28 	if (srclen < 64 || !may_use_simd())
29 		return crypto_nhpoly1305_update(desc, src, srclen);
30 
31 	do {
32 		unsigned int n = min_t(unsigned int, srclen, PAGE_SIZE);
33 
34 		kernel_neon_begin();
35 		crypto_nhpoly1305_update_helper(desc, src, n, _nh_neon);
36 		kernel_neon_end();
37 		src += n;
38 		srclen -= n;
39 	} while (srclen);
40 	return 0;
41 }
42 
43 static struct shash_alg nhpoly1305_alg = {
44 	.base.cra_name		= "nhpoly1305",
45 	.base.cra_driver_name	= "nhpoly1305-neon",
46 	.base.cra_priority	= 200,
47 	.base.cra_ctxsize	= sizeof(struct nhpoly1305_key),
48 	.base.cra_module	= THIS_MODULE,
49 	.digestsize		= POLY1305_DIGEST_SIZE,
50 	.init			= crypto_nhpoly1305_init,
51 	.update			= nhpoly1305_neon_update,
52 	.final			= crypto_nhpoly1305_final,
53 	.setkey			= crypto_nhpoly1305_setkey,
54 	.descsize		= sizeof(struct nhpoly1305_state),
55 };
56 
57 static int __init nhpoly1305_mod_init(void)
58 {
59 	if (!(elf_hwcap & HWCAP_NEON))
60 		return -ENODEV;
61 
62 	return crypto_register_shash(&nhpoly1305_alg);
63 }
64 
65 static void __exit nhpoly1305_mod_exit(void)
66 {
67 	crypto_unregister_shash(&nhpoly1305_alg);
68 }
69 
70 module_init(nhpoly1305_mod_init);
71 module_exit(nhpoly1305_mod_exit);
72 
73 MODULE_DESCRIPTION("NHPoly1305 ε-almost-∆-universal hash function (NEON-accelerated)");
74 MODULE_LICENSE("GPL v2");
75 MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
76 MODULE_ALIAS_CRYPTO("nhpoly1305");
77 MODULE_ALIAS_CRYPTO("nhpoly1305-neon");
78