1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * sha512-ce-glue.c - SHA-384/SHA-512 using ARMv8 Crypto Extensions
4  *
5  * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 
12 #include <asm/neon.h>
13 #include <asm/simd.h>
14 #include <asm/unaligned.h>
15 #include <crypto/internal/hash.h>
16 #include <crypto/internal/simd.h>
17 #include <crypto/sha2.h>
18 #include <crypto/sha512_base.h>
19 #include <linux/cpufeature.h>
20 #include <linux/crypto.h>
21 #include <linux/module.h>
22 
23 MODULE_DESCRIPTION("SHA-384/SHA-512 secure hash using ARMv8 Crypto Extensions");
24 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
25 MODULE_LICENSE("GPL v2");
26 MODULE_ALIAS_CRYPTO("sha384");
27 MODULE_ALIAS_CRYPTO("sha512");
28 
29 asmlinkage int sha512_ce_transform(struct sha512_state *sst, u8 const *src,
30 				   int blocks);
31 
32 asmlinkage void sha512_block_data_order(u64 *digest, u8 const *src, int blocks);
33 
34 static void __sha512_ce_transform(struct sha512_state *sst, u8 const *src,
35 				  int blocks)
36 {
37 	while (blocks) {
38 		int rem;
39 
40 		kernel_neon_begin();
41 		rem = sha512_ce_transform(sst, src, blocks);
42 		kernel_neon_end();
43 		src += (blocks - rem) * SHA512_BLOCK_SIZE;
44 		blocks = rem;
45 	}
46 }
47 
48 static void __sha512_block_data_order(struct sha512_state *sst, u8 const *src,
49 				      int blocks)
50 {
51 	sha512_block_data_order(sst->state, src, blocks);
52 }
53 
54 static int sha512_ce_update(struct shash_desc *desc, const u8 *data,
55 			    unsigned int len)
56 {
57 	sha512_block_fn *fn = crypto_simd_usable() ? __sha512_ce_transform
58 						   : __sha512_block_data_order;
59 
60 	sha512_base_do_update(desc, data, len, fn);
61 	return 0;
62 }
63 
64 static int sha512_ce_finup(struct shash_desc *desc, const u8 *data,
65 			   unsigned int len, u8 *out)
66 {
67 	sha512_block_fn *fn = crypto_simd_usable() ? __sha512_ce_transform
68 						   : __sha512_block_data_order;
69 
70 	sha512_base_do_update(desc, data, len, fn);
71 	sha512_base_do_finalize(desc, fn);
72 	return sha512_base_finish(desc, out);
73 }
74 
75 static int sha512_ce_final(struct shash_desc *desc, u8 *out)
76 {
77 	sha512_block_fn *fn = crypto_simd_usable() ? __sha512_ce_transform
78 						   : __sha512_block_data_order;
79 
80 	sha512_base_do_finalize(desc, fn);
81 	return sha512_base_finish(desc, out);
82 }
83 
84 static struct shash_alg algs[] = { {
85 	.init			= sha384_base_init,
86 	.update			= sha512_ce_update,
87 	.final			= sha512_ce_final,
88 	.finup			= sha512_ce_finup,
89 	.descsize		= sizeof(struct sha512_state),
90 	.digestsize		= SHA384_DIGEST_SIZE,
91 	.base.cra_name		= "sha384",
92 	.base.cra_driver_name	= "sha384-ce",
93 	.base.cra_priority	= 200,
94 	.base.cra_blocksize	= SHA512_BLOCK_SIZE,
95 	.base.cra_module	= THIS_MODULE,
96 }, {
97 	.init			= sha512_base_init,
98 	.update			= sha512_ce_update,
99 	.final			= sha512_ce_final,
100 	.finup			= sha512_ce_finup,
101 	.descsize		= sizeof(struct sha512_state),
102 	.digestsize		= SHA512_DIGEST_SIZE,
103 	.base.cra_name		= "sha512",
104 	.base.cra_driver_name	= "sha512-ce",
105 	.base.cra_priority	= 200,
106 	.base.cra_blocksize	= SHA512_BLOCK_SIZE,
107 	.base.cra_module	= THIS_MODULE,
108 } };
109 
110 static int __init sha512_ce_mod_init(void)
111 {
112 	return crypto_register_shashes(algs, ARRAY_SIZE(algs));
113 }
114 
115 static void __exit sha512_ce_mod_fini(void)
116 {
117 	crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
118 }
119 
120 module_cpu_feature_match(SHA512, sha512_ce_mod_init);
121 module_exit(sha512_ce_mod_fini);
122