xref: /openbmc/linux/arch/arm64/crypto/sm3-ce-glue.c (revision 09bae3b6)
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/sm3.h>
16 #include <crypto/sm3_base.h>
17 #include <linux/cpufeature.h>
18 #include <linux/crypto.h>
19 #include <linux/module.h>
20 
21 MODULE_DESCRIPTION("SM3 secure hash using ARMv8 Crypto Extensions");
22 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
23 MODULE_LICENSE("GPL v2");
24 
25 asmlinkage void sm3_ce_transform(struct sm3_state *sst, u8 const *src,
26 				 int blocks);
27 
28 static int sm3_ce_update(struct shash_desc *desc, const u8 *data,
29 			 unsigned int len)
30 {
31 	if (!may_use_simd())
32 		return crypto_sm3_update(desc, data, len);
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 
41 static int sm3_ce_final(struct shash_desc *desc, u8 *out)
42 {
43 	if (!may_use_simd())
44 		return crypto_sm3_finup(desc, NULL, 0, out);
45 
46 	kernel_neon_begin();
47 	sm3_base_do_finalize(desc, sm3_ce_transform);
48 	kernel_neon_end();
49 
50 	return sm3_base_finish(desc, out);
51 }
52 
53 static int sm3_ce_finup(struct shash_desc *desc, const u8 *data,
54 			unsigned int len, u8 *out)
55 {
56 	if (!may_use_simd())
57 		return crypto_sm3_finup(desc, data, len, out);
58 
59 	kernel_neon_begin();
60 	sm3_base_do_update(desc, data, len, sm3_ce_transform);
61 	kernel_neon_end();
62 
63 	return sm3_ce_final(desc, out);
64 }
65 
66 static struct shash_alg sm3_alg = {
67 	.digestsize		= SM3_DIGEST_SIZE,
68 	.init			= sm3_base_init,
69 	.update			= sm3_ce_update,
70 	.final			= sm3_ce_final,
71 	.finup			= sm3_ce_finup,
72 	.descsize		= sizeof(struct sm3_state),
73 	.base.cra_name		= "sm3",
74 	.base.cra_driver_name	= "sm3-ce",
75 	.base.cra_blocksize	= SM3_BLOCK_SIZE,
76 	.base.cra_module	= THIS_MODULE,
77 	.base.cra_priority	= 200,
78 };
79 
80 static int __init sm3_ce_mod_init(void)
81 {
82 	return crypto_register_shash(&sm3_alg);
83 }
84 
85 static void __exit sm3_ce_mod_fini(void)
86 {
87 	crypto_unregister_shash(&sm3_alg);
88 }
89 
90 module_cpu_feature_match(SM3, sm3_ce_mod_init);
91 module_exit(sm3_ce_mod_fini);
92