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/sha.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 
27 asmlinkage void sha512_ce_transform(struct sha512_state *sst, u8 const *src,
28 				    int blocks);
29 
30 asmlinkage void sha512_block_data_order(u64 *digest, u8 const *src, int blocks);
31 
32 static int sha512_ce_update(struct shash_desc *desc, const u8 *data,
33 			    unsigned int len)
34 {
35 	if (!crypto_simd_usable())
36 		return sha512_base_do_update(desc, data, len,
37 				(sha512_block_fn *)sha512_block_data_order);
38 
39 	kernel_neon_begin();
40 	sha512_base_do_update(desc, data, len,
41 			      (sha512_block_fn *)sha512_ce_transform);
42 	kernel_neon_end();
43 
44 	return 0;
45 }
46 
47 static int sha512_ce_finup(struct shash_desc *desc, const u8 *data,
48 			   unsigned int len, u8 *out)
49 {
50 	if (!crypto_simd_usable()) {
51 		if (len)
52 			sha512_base_do_update(desc, data, len,
53 				(sha512_block_fn *)sha512_block_data_order);
54 		sha512_base_do_finalize(desc,
55 				(sha512_block_fn *)sha512_block_data_order);
56 		return sha512_base_finish(desc, out);
57 	}
58 
59 	kernel_neon_begin();
60 	sha512_base_do_update(desc, data, len,
61 			      (sha512_block_fn *)sha512_ce_transform);
62 	sha512_base_do_finalize(desc, (sha512_block_fn *)sha512_ce_transform);
63 	kernel_neon_end();
64 	return sha512_base_finish(desc, out);
65 }
66 
67 static int sha512_ce_final(struct shash_desc *desc, u8 *out)
68 {
69 	if (!crypto_simd_usable()) {
70 		sha512_base_do_finalize(desc,
71 				(sha512_block_fn *)sha512_block_data_order);
72 		return sha512_base_finish(desc, out);
73 	}
74 
75 	kernel_neon_begin();
76 	sha512_base_do_finalize(desc, (sha512_block_fn *)sha512_ce_transform);
77 	kernel_neon_end();
78 	return sha512_base_finish(desc, out);
79 }
80 
81 static struct shash_alg algs[] = { {
82 	.init			= sha384_base_init,
83 	.update			= sha512_ce_update,
84 	.final			= sha512_ce_final,
85 	.finup			= sha512_ce_finup,
86 	.descsize		= sizeof(struct sha512_state),
87 	.digestsize		= SHA384_DIGEST_SIZE,
88 	.base.cra_name		= "sha384",
89 	.base.cra_driver_name	= "sha384-ce",
90 	.base.cra_priority	= 200,
91 	.base.cra_blocksize	= SHA512_BLOCK_SIZE,
92 	.base.cra_module	= THIS_MODULE,
93 }, {
94 	.init			= sha512_base_init,
95 	.update			= sha512_ce_update,
96 	.final			= sha512_ce_final,
97 	.finup			= sha512_ce_finup,
98 	.descsize		= sizeof(struct sha512_state),
99 	.digestsize		= SHA512_DIGEST_SIZE,
100 	.base.cra_name		= "sha512",
101 	.base.cra_driver_name	= "sha512-ce",
102 	.base.cra_priority	= 200,
103 	.base.cra_blocksize	= SHA512_BLOCK_SIZE,
104 	.base.cra_module	= THIS_MODULE,
105 } };
106 
107 static int __init sha512_ce_mod_init(void)
108 {
109 	return crypto_register_shashes(algs, ARRAY_SIZE(algs));
110 }
111 
112 static void __exit sha512_ce_mod_fini(void)
113 {
114 	crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
115 }
116 
117 module_cpu_feature_match(SHA512, sha512_ce_mod_init);
118 module_exit(sha512_ce_mod_fini);
119