xref: /openbmc/linux/arch/arm64/crypto/sm3-ce-glue.c (revision abade675e02e1b73da0c20ffaf08fbe309038298)
1 /*
2  * sm3-ce-glue.c - SM3 secure hash using ARMv8.2 Crypto Extensions
3  *
4  * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include <asm/neon.h>
12 #include <asm/simd.h>
13 #include <asm/unaligned.h>
14 #include <crypto/internal/hash.h>
15 #include <crypto/internal/simd.h>
16 #include <crypto/sm3.h>
17 #include <crypto/sm3_base.h>
18 #include <linux/cpufeature.h>
19 #include <linux/crypto.h>
20 #include <linux/module.h>
21 
22 MODULE_DESCRIPTION("SM3 secure hash using ARMv8 Crypto Extensions");
23 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
24 MODULE_LICENSE("GPL v2");
25 
26 asmlinkage void sm3_ce_transform(struct sm3_state *sst, u8 const *src,
27 				 int blocks);
28 
29 static int sm3_ce_update(struct shash_desc *desc, const u8 *data,
30 			 unsigned int len)
31 {
32 	if (!crypto_simd_usable())
33 		return crypto_sm3_update(desc, data, len);
34 
35 	kernel_neon_begin();
36 	sm3_base_do_update(desc, data, len, sm3_ce_transform);
37 	kernel_neon_end();
38 
39 	return 0;
40 }
41 
42 static int sm3_ce_final(struct shash_desc *desc, u8 *out)
43 {
44 	if (!crypto_simd_usable())
45 		return crypto_sm3_finup(desc, NULL, 0, out);
46 
47 	kernel_neon_begin();
48 	sm3_base_do_finalize(desc, sm3_ce_transform);
49 	kernel_neon_end();
50 
51 	return sm3_base_finish(desc, out);
52 }
53 
54 static int sm3_ce_finup(struct shash_desc *desc, const u8 *data,
55 			unsigned int len, u8 *out)
56 {
57 	if (!crypto_simd_usable())
58 		return crypto_sm3_finup(desc, data, len, out);
59 
60 	kernel_neon_begin();
61 	sm3_base_do_update(desc, data, len, sm3_ce_transform);
62 	kernel_neon_end();
63 
64 	return sm3_ce_final(desc, out);
65 }
66 
67 static struct shash_alg sm3_alg = {
68 	.digestsize		= SM3_DIGEST_SIZE,
69 	.init			= sm3_base_init,
70 	.update			= sm3_ce_update,
71 	.final			= sm3_ce_final,
72 	.finup			= sm3_ce_finup,
73 	.descsize		= sizeof(struct sm3_state),
74 	.base.cra_name		= "sm3",
75 	.base.cra_driver_name	= "sm3-ce",
76 	.base.cra_blocksize	= SM3_BLOCK_SIZE,
77 	.base.cra_module	= THIS_MODULE,
78 	.base.cra_priority	= 200,
79 };
80 
81 static int __init sm3_ce_mod_init(void)
82 {
83 	return crypto_register_shash(&sm3_alg);
84 }
85 
86 static void __exit sm3_ce_mod_fini(void)
87 {
88 	crypto_unregister_shash(&sm3_alg);
89 }
90 
91 module_cpu_feature_match(SM3, sm3_ce_mod_init);
92 module_exit(sm3_ce_mod_fini);
93