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