xref: /openbmc/linux/arch/arm64/crypto/sha512-ce-glue.c (revision e52b7023cdad005756cd91d7c54fa90ef6b43d32)
1bb6c8c46SArd Biesheuvel /* SPDX-License-Identifier: GPL-2.0 */
2bb6c8c46SArd Biesheuvel /*
3bb6c8c46SArd Biesheuvel  * sha512-ce-glue.c - SHA-384/SHA-512 using ARMv8 Crypto Extensions
4bb6c8c46SArd Biesheuvel  *
5bb6c8c46SArd Biesheuvel  * Copyright (C) 2018 Linaro Ltd <ard.biesheuvel@linaro.org>
6bb6c8c46SArd Biesheuvel  *
7bb6c8c46SArd Biesheuvel  * This program is free software; you can redistribute it and/or modify
8bb6c8c46SArd Biesheuvel  * it under the terms of the GNU General Public License version 2 as
9bb6c8c46SArd Biesheuvel  * published by the Free Software Foundation.
10bb6c8c46SArd Biesheuvel  */
11bb6c8c46SArd Biesheuvel 
12bb6c8c46SArd Biesheuvel #include <asm/neon.h>
13bb6c8c46SArd Biesheuvel #include <asm/simd.h>
14bb6c8c46SArd Biesheuvel #include <asm/unaligned.h>
15bb6c8c46SArd Biesheuvel #include <crypto/internal/hash.h>
16*e52b7023SEric Biggers #include <crypto/internal/simd.h>
17bb6c8c46SArd Biesheuvel #include <crypto/sha.h>
18bb6c8c46SArd Biesheuvel #include <crypto/sha512_base.h>
19bb6c8c46SArd Biesheuvel #include <linux/cpufeature.h>
20bb6c8c46SArd Biesheuvel #include <linux/crypto.h>
21bb6c8c46SArd Biesheuvel #include <linux/module.h>
22bb6c8c46SArd Biesheuvel 
23bb6c8c46SArd Biesheuvel MODULE_DESCRIPTION("SHA-384/SHA-512 secure hash using ARMv8 Crypto Extensions");
24bb6c8c46SArd Biesheuvel MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
25bb6c8c46SArd Biesheuvel MODULE_LICENSE("GPL v2");
26bb6c8c46SArd Biesheuvel 
27bb6c8c46SArd Biesheuvel asmlinkage void sha512_ce_transform(struct sha512_state *sst, u8 const *src,
28bb6c8c46SArd Biesheuvel 				    int blocks);
29bb6c8c46SArd Biesheuvel 
30bb6c8c46SArd Biesheuvel asmlinkage void sha512_block_data_order(u64 *digest, u8 const *src, int blocks);
31bb6c8c46SArd Biesheuvel 
32bb6c8c46SArd Biesheuvel static int sha512_ce_update(struct shash_desc *desc, const u8 *data,
33bb6c8c46SArd Biesheuvel 			    unsigned int len)
34bb6c8c46SArd Biesheuvel {
35*e52b7023SEric Biggers 	if (!crypto_simd_usable())
36bb6c8c46SArd Biesheuvel 		return sha512_base_do_update(desc, data, len,
37bb6c8c46SArd Biesheuvel 				(sha512_block_fn *)sha512_block_data_order);
38bb6c8c46SArd Biesheuvel 
39bb6c8c46SArd Biesheuvel 	kernel_neon_begin();
40bb6c8c46SArd Biesheuvel 	sha512_base_do_update(desc, data, len,
41bb6c8c46SArd Biesheuvel 			      (sha512_block_fn *)sha512_ce_transform);
42bb6c8c46SArd Biesheuvel 	kernel_neon_end();
43bb6c8c46SArd Biesheuvel 
44bb6c8c46SArd Biesheuvel 	return 0;
45bb6c8c46SArd Biesheuvel }
46bb6c8c46SArd Biesheuvel 
47bb6c8c46SArd Biesheuvel static int sha512_ce_finup(struct shash_desc *desc, const u8 *data,
48bb6c8c46SArd Biesheuvel 			   unsigned int len, u8 *out)
49bb6c8c46SArd Biesheuvel {
50*e52b7023SEric Biggers 	if (!crypto_simd_usable()) {
51bb6c8c46SArd Biesheuvel 		if (len)
52bb6c8c46SArd Biesheuvel 			sha512_base_do_update(desc, data, len,
53bb6c8c46SArd Biesheuvel 				(sha512_block_fn *)sha512_block_data_order);
54bb6c8c46SArd Biesheuvel 		sha512_base_do_finalize(desc,
55bb6c8c46SArd Biesheuvel 				(sha512_block_fn *)sha512_block_data_order);
56bb6c8c46SArd Biesheuvel 		return sha512_base_finish(desc, out);
57bb6c8c46SArd Biesheuvel 	}
58bb6c8c46SArd Biesheuvel 
59bb6c8c46SArd Biesheuvel 	kernel_neon_begin();
60bb6c8c46SArd Biesheuvel 	sha512_base_do_update(desc, data, len,
61bb6c8c46SArd Biesheuvel 			      (sha512_block_fn *)sha512_ce_transform);
62bb6c8c46SArd Biesheuvel 	sha512_base_do_finalize(desc, (sha512_block_fn *)sha512_ce_transform);
63bb6c8c46SArd Biesheuvel 	kernel_neon_end();
64bb6c8c46SArd Biesheuvel 	return sha512_base_finish(desc, out);
65bb6c8c46SArd Biesheuvel }
66bb6c8c46SArd Biesheuvel 
67bb6c8c46SArd Biesheuvel static int sha512_ce_final(struct shash_desc *desc, u8 *out)
68bb6c8c46SArd Biesheuvel {
69*e52b7023SEric Biggers 	if (!crypto_simd_usable()) {
70bb6c8c46SArd Biesheuvel 		sha512_base_do_finalize(desc,
71bb6c8c46SArd Biesheuvel 				(sha512_block_fn *)sha512_block_data_order);
72bb6c8c46SArd Biesheuvel 		return sha512_base_finish(desc, out);
73bb6c8c46SArd Biesheuvel 	}
74bb6c8c46SArd Biesheuvel 
75bb6c8c46SArd Biesheuvel 	kernel_neon_begin();
76bb6c8c46SArd Biesheuvel 	sha512_base_do_finalize(desc, (sha512_block_fn *)sha512_ce_transform);
77bb6c8c46SArd Biesheuvel 	kernel_neon_end();
78bb6c8c46SArd Biesheuvel 	return sha512_base_finish(desc, out);
79bb6c8c46SArd Biesheuvel }
80bb6c8c46SArd Biesheuvel 
81bb6c8c46SArd Biesheuvel static struct shash_alg algs[] = { {
82bb6c8c46SArd Biesheuvel 	.init			= sha384_base_init,
83bb6c8c46SArd Biesheuvel 	.update			= sha512_ce_update,
84bb6c8c46SArd Biesheuvel 	.final			= sha512_ce_final,
85bb6c8c46SArd Biesheuvel 	.finup			= sha512_ce_finup,
86bb6c8c46SArd Biesheuvel 	.descsize		= sizeof(struct sha512_state),
87bb6c8c46SArd Biesheuvel 	.digestsize		= SHA384_DIGEST_SIZE,
88bb6c8c46SArd Biesheuvel 	.base.cra_name		= "sha384",
89bb6c8c46SArd Biesheuvel 	.base.cra_driver_name	= "sha384-ce",
90bb6c8c46SArd Biesheuvel 	.base.cra_priority	= 200,
91bb6c8c46SArd Biesheuvel 	.base.cra_blocksize	= SHA512_BLOCK_SIZE,
92bb6c8c46SArd Biesheuvel 	.base.cra_module	= THIS_MODULE,
93bb6c8c46SArd Biesheuvel }, {
94bb6c8c46SArd Biesheuvel 	.init			= sha512_base_init,
95bb6c8c46SArd Biesheuvel 	.update			= sha512_ce_update,
96bb6c8c46SArd Biesheuvel 	.final			= sha512_ce_final,
97bb6c8c46SArd Biesheuvel 	.finup			= sha512_ce_finup,
98bb6c8c46SArd Biesheuvel 	.descsize		= sizeof(struct sha512_state),
99bb6c8c46SArd Biesheuvel 	.digestsize		= SHA512_DIGEST_SIZE,
100bb6c8c46SArd Biesheuvel 	.base.cra_name		= "sha512",
101bb6c8c46SArd Biesheuvel 	.base.cra_driver_name	= "sha512-ce",
102bb6c8c46SArd Biesheuvel 	.base.cra_priority	= 200,
103bb6c8c46SArd Biesheuvel 	.base.cra_blocksize	= SHA512_BLOCK_SIZE,
104bb6c8c46SArd Biesheuvel 	.base.cra_module	= THIS_MODULE,
105bb6c8c46SArd Biesheuvel } };
106bb6c8c46SArd Biesheuvel 
107bb6c8c46SArd Biesheuvel static int __init sha512_ce_mod_init(void)
108bb6c8c46SArd Biesheuvel {
109bb6c8c46SArd Biesheuvel 	return crypto_register_shashes(algs, ARRAY_SIZE(algs));
110bb6c8c46SArd Biesheuvel }
111bb6c8c46SArd Biesheuvel 
112bb6c8c46SArd Biesheuvel static void __exit sha512_ce_mod_fini(void)
113bb6c8c46SArd Biesheuvel {
114bb6c8c46SArd Biesheuvel 	crypto_unregister_shashes(algs, ARRAY_SIZE(algs));
115bb6c8c46SArd Biesheuvel }
116bb6c8c46SArd Biesheuvel 
117bb6c8c46SArd Biesheuvel module_cpu_feature_match(SHA512, sha512_ce_mod_init);
118bb6c8c46SArd Biesheuvel module_exit(sha512_ce_mod_fini);
119