1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Linux/arm64 port of the OpenSSL SHA512 implementation for AArch64
4  *
5  * Copyright (c) 2016 Linaro Ltd. <ard.biesheuvel@linaro.org>
6  */
7 
8 #include <crypto/internal/hash.h>
9 #include <linux/types.h>
10 #include <linux/string.h>
11 #include <crypto/sha2.h>
12 #include <crypto/sha512_base.h>
13 #include <asm/neon.h>
14 
15 MODULE_DESCRIPTION("SHA-384/SHA-512 secure hash for arm64");
16 MODULE_AUTHOR("Andy Polyakov <appro@openssl.org>");
17 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
18 MODULE_LICENSE("GPL v2");
19 MODULE_ALIAS_CRYPTO("sha384");
20 MODULE_ALIAS_CRYPTO("sha512");
21 
22 asmlinkage void sha512_block_data_order(u64 *digest, const void *data,
23 					unsigned int num_blks);
24 EXPORT_SYMBOL(sha512_block_data_order);
25 
__sha512_block_data_order(struct sha512_state * sst,u8 const * src,int blocks)26 static void __sha512_block_data_order(struct sha512_state *sst, u8 const *src,
27 				      int blocks)
28 {
29 	sha512_block_data_order(sst->state, src, blocks);
30 }
31 
sha512_update(struct shash_desc * desc,const u8 * data,unsigned int len)32 static int sha512_update(struct shash_desc *desc, const u8 *data,
33 			 unsigned int len)
34 {
35 	return sha512_base_do_update(desc, data, len,
36 				     __sha512_block_data_order);
37 }
38 
sha512_finup(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)39 static int sha512_finup(struct shash_desc *desc, const u8 *data,
40 			unsigned int len, u8 *out)
41 {
42 	if (len)
43 		sha512_base_do_update(desc, data, len,
44 				      __sha512_block_data_order);
45 	sha512_base_do_finalize(desc, __sha512_block_data_order);
46 
47 	return sha512_base_finish(desc, out);
48 }
49 
sha512_final(struct shash_desc * desc,u8 * out)50 static int sha512_final(struct shash_desc *desc, u8 *out)
51 {
52 	return sha512_finup(desc, NULL, 0, out);
53 }
54 
55 static struct shash_alg algs[] = { {
56 	.digestsize		= SHA512_DIGEST_SIZE,
57 	.init			= sha512_base_init,
58 	.update			= sha512_update,
59 	.final			= sha512_final,
60 	.finup			= sha512_finup,
61 	.descsize		= sizeof(struct sha512_state),
62 	.base.cra_name		= "sha512",
63 	.base.cra_driver_name	= "sha512-arm64",
64 	.base.cra_priority	= 150,
65 	.base.cra_blocksize	= SHA512_BLOCK_SIZE,
66 	.base.cra_module	= THIS_MODULE,
67 }, {
68 	.digestsize		= SHA384_DIGEST_SIZE,
69 	.init			= sha384_base_init,
70 	.update			= sha512_update,
71 	.final			= sha512_final,
72 	.finup			= sha512_finup,
73 	.descsize		= sizeof(struct sha512_state),
74 	.base.cra_name		= "sha384",
75 	.base.cra_driver_name	= "sha384-arm64",
76 	.base.cra_priority	= 150,
77 	.base.cra_blocksize	= SHA384_BLOCK_SIZE,
78 	.base.cra_module	= THIS_MODULE,
79 } };
80 
sha512_mod_init(void)81 static int __init sha512_mod_init(void)
82 {
83 	return crypto_register_shashes(algs, ARRAY_SIZE(algs));
84 }
85 
sha512_mod_fini(void)86 static void __exit sha512_mod_fini(void)
87 {
88 	crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
89 }
90 
91 module_init(sha512_mod_init);
92 module_exit(sha512_mod_fini);
93