xref: /openbmc/linux/arch/arm64/crypto/sha1-ce-glue.c (revision 9b93eb47)
1 /*
2  * sha1-ce-glue.c - SHA-1 secure hash using ARMv8 Crypto Extensions
3  *
4  * Copyright (C) 2014 - 2017 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/sha.h>
17 #include <crypto/sha1_base.h>
18 #include <linux/cpufeature.h>
19 #include <linux/crypto.h>
20 #include <linux/module.h>
21 
22 MODULE_DESCRIPTION("SHA1 secure hash using ARMv8 Crypto Extensions");
23 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
24 MODULE_LICENSE("GPL v2");
25 
26 struct sha1_ce_state {
27 	struct sha1_state	sst;
28 	u32			finalize;
29 };
30 
31 asmlinkage void sha1_ce_transform(struct sha1_ce_state *sst, u8 const *src,
32 				  int blocks);
33 
34 const u32 sha1_ce_offsetof_count = offsetof(struct sha1_ce_state, sst.count);
35 const u32 sha1_ce_offsetof_finalize = offsetof(struct sha1_ce_state, finalize);
36 
37 static int sha1_ce_update(struct shash_desc *desc, const u8 *data,
38 			  unsigned int len)
39 {
40 	struct sha1_ce_state *sctx = shash_desc_ctx(desc);
41 
42 	if (!crypto_simd_usable())
43 		return crypto_sha1_update(desc, data, len);
44 
45 	sctx->finalize = 0;
46 	kernel_neon_begin();
47 	sha1_base_do_update(desc, data, len,
48 			    (sha1_block_fn *)sha1_ce_transform);
49 	kernel_neon_end();
50 
51 	return 0;
52 }
53 
54 static int sha1_ce_finup(struct shash_desc *desc, const u8 *data,
55 			 unsigned int len, u8 *out)
56 {
57 	struct sha1_ce_state *sctx = shash_desc_ctx(desc);
58 	bool finalize = !sctx->sst.count && !(len % SHA1_BLOCK_SIZE);
59 
60 	if (!crypto_simd_usable())
61 		return crypto_sha1_finup(desc, data, len, out);
62 
63 	/*
64 	 * Allow the asm code to perform the finalization if there is no
65 	 * partial data and the input is a round multiple of the block size.
66 	 */
67 	sctx->finalize = finalize;
68 
69 	kernel_neon_begin();
70 	sha1_base_do_update(desc, data, len,
71 			    (sha1_block_fn *)sha1_ce_transform);
72 	if (!finalize)
73 		sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
74 	kernel_neon_end();
75 	return sha1_base_finish(desc, out);
76 }
77 
78 static int sha1_ce_final(struct shash_desc *desc, u8 *out)
79 {
80 	struct sha1_ce_state *sctx = shash_desc_ctx(desc);
81 
82 	if (!crypto_simd_usable())
83 		return crypto_sha1_finup(desc, NULL, 0, out);
84 
85 	sctx->finalize = 0;
86 	kernel_neon_begin();
87 	sha1_base_do_finalize(desc, (sha1_block_fn *)sha1_ce_transform);
88 	kernel_neon_end();
89 	return sha1_base_finish(desc, out);
90 }
91 
92 static struct shash_alg alg = {
93 	.init			= sha1_base_init,
94 	.update			= sha1_ce_update,
95 	.final			= sha1_ce_final,
96 	.finup			= sha1_ce_finup,
97 	.descsize		= sizeof(struct sha1_ce_state),
98 	.digestsize		= SHA1_DIGEST_SIZE,
99 	.base			= {
100 		.cra_name		= "sha1",
101 		.cra_driver_name	= "sha1-ce",
102 		.cra_priority		= 200,
103 		.cra_blocksize		= SHA1_BLOCK_SIZE,
104 		.cra_module		= THIS_MODULE,
105 	}
106 };
107 
108 static int __init sha1_ce_mod_init(void)
109 {
110 	return crypto_register_shash(&alg);
111 }
112 
113 static void __exit sha1_ce_mod_fini(void)
114 {
115 	crypto_unregister_shash(&alg);
116 }
117 
118 module_cpu_feature_match(SHA1, sha1_ce_mod_init);
119 module_exit(sha1_ce_mod_fini);
120