1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * sm3-ce-glue.c - SM3 secure hash using ARMv8.2 Crypto Extensions
4  *
5  * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
6  */
7 
8 #include <asm/neon.h>
9 #include <asm/simd.h>
10 #include <asm/unaligned.h>
11 #include <crypto/internal/hash.h>
12 #include <crypto/internal/simd.h>
13 #include <crypto/sm3.h>
14 #include <crypto/sm3_base.h>
15 #include <linux/cpufeature.h>
16 #include <linux/crypto.h>
17 #include <linux/module.h>
18 
19 MODULE_DESCRIPTION("SM3 secure hash using ARMv8 Crypto Extensions");
20 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
21 MODULE_LICENSE("GPL v2");
22 
23 asmlinkage void sm3_ce_transform(struct sm3_state *sst, u8 const *src,
24 				 int blocks);
25 
sm3_ce_update(struct shash_desc * desc,const u8 * data,unsigned int len)26 static int sm3_ce_update(struct shash_desc *desc, const u8 *data,
27 			 unsigned int len)
28 {
29 	if (!crypto_simd_usable()) {
30 		sm3_update(shash_desc_ctx(desc), data, len);
31 		return 0;
32 	}
33 
34 	kernel_neon_begin();
35 	sm3_base_do_update(desc, data, len, sm3_ce_transform);
36 	kernel_neon_end();
37 
38 	return 0;
39 }
40 
sm3_ce_final(struct shash_desc * desc,u8 * out)41 static int sm3_ce_final(struct shash_desc *desc, u8 *out)
42 {
43 	if (!crypto_simd_usable()) {
44 		sm3_final(shash_desc_ctx(desc), out);
45 		return 0;
46 	}
47 
48 	kernel_neon_begin();
49 	sm3_base_do_finalize(desc, sm3_ce_transform);
50 	kernel_neon_end();
51 
52 	return sm3_base_finish(desc, out);
53 }
54 
sm3_ce_finup(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)55 static int sm3_ce_finup(struct shash_desc *desc, const u8 *data,
56 			unsigned int len, u8 *out)
57 {
58 	if (!crypto_simd_usable()) {
59 		struct sm3_state *sctx = shash_desc_ctx(desc);
60 
61 		if (len)
62 			sm3_update(sctx, data, len);
63 		sm3_final(sctx, out);
64 		return 0;
65 	}
66 
67 	kernel_neon_begin();
68 	if (len)
69 		sm3_base_do_update(desc, data, len, sm3_ce_transform);
70 	sm3_base_do_finalize(desc, sm3_ce_transform);
71 	kernel_neon_end();
72 
73 	return sm3_base_finish(desc, out);
74 }
75 
76 static struct shash_alg sm3_alg = {
77 	.digestsize		= SM3_DIGEST_SIZE,
78 	.init			= sm3_base_init,
79 	.update			= sm3_ce_update,
80 	.final			= sm3_ce_final,
81 	.finup			= sm3_ce_finup,
82 	.descsize		= sizeof(struct sm3_state),
83 	.base.cra_name		= "sm3",
84 	.base.cra_driver_name	= "sm3-ce",
85 	.base.cra_blocksize	= SM3_BLOCK_SIZE,
86 	.base.cra_module	= THIS_MODULE,
87 	.base.cra_priority	= 400,
88 };
89 
sm3_ce_mod_init(void)90 static int __init sm3_ce_mod_init(void)
91 {
92 	return crypto_register_shash(&sm3_alg);
93 }
94 
sm3_ce_mod_fini(void)95 static void __exit sm3_ce_mod_fini(void)
96 {
97 	crypto_unregister_shash(&sm3_alg);
98 }
99 
100 module_cpu_feature_match(SM3, sm3_ce_mod_init);
101 module_exit(sm3_ce_mod_fini);
102