1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * sha512-glue.c - accelerated SHA-384/512 for ARM
4  *
5  * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>
6  */
7 
8 #include <crypto/internal/hash.h>
9 #include <crypto/sha2.h>
10 #include <crypto/sha512_base.h>
11 #include <linux/crypto.h>
12 #include <linux/module.h>
13 
14 #include <asm/hwcap.h>
15 #include <asm/neon.h>
16 
17 #include "sha512.h"
18 
19 MODULE_DESCRIPTION("Accelerated SHA-384/SHA-512 secure hash for ARM");
20 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
21 MODULE_LICENSE("GPL v2");
22 
23 MODULE_ALIAS_CRYPTO("sha384");
24 MODULE_ALIAS_CRYPTO("sha512");
25 MODULE_ALIAS_CRYPTO("sha384-arm");
26 MODULE_ALIAS_CRYPTO("sha512-arm");
27 
28 asmlinkage void sha512_block_data_order(u64 *state, u8 const *src, int blocks);
29 
sha512_arm_update(struct shash_desc * desc,const u8 * data,unsigned int len)30 int sha512_arm_update(struct shash_desc *desc, const u8 *data,
31 		      unsigned int len)
32 {
33 	return sha512_base_do_update(desc, data, len,
34 		(sha512_block_fn *)sha512_block_data_order);
35 }
36 
sha512_arm_final(struct shash_desc * desc,u8 * out)37 static int sha512_arm_final(struct shash_desc *desc, u8 *out)
38 {
39 	sha512_base_do_finalize(desc,
40 		(sha512_block_fn *)sha512_block_data_order);
41 	return sha512_base_finish(desc, out);
42 }
43 
sha512_arm_finup(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)44 int sha512_arm_finup(struct shash_desc *desc, const u8 *data,
45 		     unsigned int len, u8 *out)
46 {
47 	sha512_base_do_update(desc, data, len,
48 		(sha512_block_fn *)sha512_block_data_order);
49 	return sha512_arm_final(desc, out);
50 }
51 
52 static struct shash_alg sha512_arm_algs[] = { {
53 	.init			= sha384_base_init,
54 	.update			= sha512_arm_update,
55 	.final			= sha512_arm_final,
56 	.finup			= sha512_arm_finup,
57 	.descsize		= sizeof(struct sha512_state),
58 	.digestsize		= SHA384_DIGEST_SIZE,
59 	.base			= {
60 		.cra_name		= "sha384",
61 		.cra_driver_name	= "sha384-arm",
62 		.cra_priority		= 250,
63 		.cra_blocksize		= SHA512_BLOCK_SIZE,
64 		.cra_module		= THIS_MODULE,
65 	}
66 },  {
67 	.init			= sha512_base_init,
68 	.update			= sha512_arm_update,
69 	.final			= sha512_arm_final,
70 	.finup			= sha512_arm_finup,
71 	.descsize		= sizeof(struct sha512_state),
72 	.digestsize		= SHA512_DIGEST_SIZE,
73 	.base			= {
74 		.cra_name		= "sha512",
75 		.cra_driver_name	= "sha512-arm",
76 		.cra_priority		= 250,
77 		.cra_blocksize		= SHA512_BLOCK_SIZE,
78 		.cra_module		= THIS_MODULE,
79 	}
80 } };
81 
sha512_arm_mod_init(void)82 static int __init sha512_arm_mod_init(void)
83 {
84 	int err;
85 
86 	err = crypto_register_shashes(sha512_arm_algs,
87 				      ARRAY_SIZE(sha512_arm_algs));
88 	if (err)
89 		return err;
90 
91 	if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon()) {
92 		err = crypto_register_shashes(sha512_neon_algs,
93 					      ARRAY_SIZE(sha512_neon_algs));
94 		if (err)
95 			goto err_unregister;
96 	}
97 	return 0;
98 
99 err_unregister:
100 	crypto_unregister_shashes(sha512_arm_algs,
101 				  ARRAY_SIZE(sha512_arm_algs));
102 
103 	return err;
104 }
105 
sha512_arm_mod_fini(void)106 static void __exit sha512_arm_mod_fini(void)
107 {
108 	crypto_unregister_shashes(sha512_arm_algs,
109 				  ARRAY_SIZE(sha512_arm_algs));
110 	if (IS_ENABLED(CONFIG_KERNEL_MODE_NEON) && cpu_has_neon())
111 		crypto_unregister_shashes(sha512_neon_algs,
112 					  ARRAY_SIZE(sha512_neon_algs));
113 }
114 
115 module_init(sha512_arm_mod_init);
116 module_exit(sha512_arm_mod_fini);
117