xref: /openbmc/linux/arch/arm/crypto/crc32-ce-glue.c (revision 8730046c)
1 /*
2  * Accelerated CRC32(C) using ARM CRC, NEON and Crypto Extensions instructions
3  *
4  * Copyright (C) 2016 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 <linux/crc32.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/string.h>
16 
17 #include <crypto/internal/hash.h>
18 
19 #include <asm/hwcap.h>
20 #include <asm/neon.h>
21 #include <asm/simd.h>
22 #include <asm/unaligned.h>
23 
24 #define PMULL_MIN_LEN		64L	/* minimum size of buffer
25 					 * for crc32_pmull_le_16 */
26 #define SCALE_F			16L	/* size of NEON register */
27 
28 asmlinkage u32 crc32_pmull_le(const u8 buf[], u32 len, u32 init_crc);
29 asmlinkage u32 crc32_armv8_le(u32 init_crc, const u8 buf[], u32 len);
30 
31 asmlinkage u32 crc32c_pmull_le(const u8 buf[], u32 len, u32 init_crc);
32 asmlinkage u32 crc32c_armv8_le(u32 init_crc, const u8 buf[], u32 len);
33 
34 static u32 (*fallback_crc32)(u32 init_crc, const u8 buf[], u32 len);
35 static u32 (*fallback_crc32c)(u32 init_crc, const u8 buf[], u32 len);
36 
37 static int crc32_cra_init(struct crypto_tfm *tfm)
38 {
39 	u32 *key = crypto_tfm_ctx(tfm);
40 
41 	*key = 0;
42 	return 0;
43 }
44 
45 static int crc32c_cra_init(struct crypto_tfm *tfm)
46 {
47 	u32 *key = crypto_tfm_ctx(tfm);
48 
49 	*key = ~0;
50 	return 0;
51 }
52 
53 static int crc32_setkey(struct crypto_shash *hash, const u8 *key,
54 			unsigned int keylen)
55 {
56 	u32 *mctx = crypto_shash_ctx(hash);
57 
58 	if (keylen != sizeof(u32)) {
59 		crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
60 		return -EINVAL;
61 	}
62 	*mctx = le32_to_cpup((__le32 *)key);
63 	return 0;
64 }
65 
66 static int crc32_init(struct shash_desc *desc)
67 {
68 	u32 *mctx = crypto_shash_ctx(desc->tfm);
69 	u32 *crc = shash_desc_ctx(desc);
70 
71 	*crc = *mctx;
72 	return 0;
73 }
74 
75 static int crc32_update(struct shash_desc *desc, const u8 *data,
76 			unsigned int length)
77 {
78 	u32 *crc = shash_desc_ctx(desc);
79 
80 	*crc = crc32_armv8_le(*crc, data, length);
81 	return 0;
82 }
83 
84 static int crc32c_update(struct shash_desc *desc, const u8 *data,
85 			 unsigned int length)
86 {
87 	u32 *crc = shash_desc_ctx(desc);
88 
89 	*crc = crc32c_armv8_le(*crc, data, length);
90 	return 0;
91 }
92 
93 static int crc32_final(struct shash_desc *desc, u8 *out)
94 {
95 	u32 *crc = shash_desc_ctx(desc);
96 
97 	put_unaligned_le32(*crc, out);
98 	return 0;
99 }
100 
101 static int crc32c_final(struct shash_desc *desc, u8 *out)
102 {
103 	u32 *crc = shash_desc_ctx(desc);
104 
105 	put_unaligned_le32(~*crc, out);
106 	return 0;
107 }
108 
109 static int crc32_pmull_update(struct shash_desc *desc, const u8 *data,
110 			      unsigned int length)
111 {
112 	u32 *crc = shash_desc_ctx(desc);
113 	unsigned int l;
114 
115 	if (may_use_simd()) {
116 		if ((u32)data % SCALE_F) {
117 			l = min_t(u32, length, SCALE_F - ((u32)data % SCALE_F));
118 
119 			*crc = fallback_crc32(*crc, data, l);
120 
121 			data += l;
122 			length -= l;
123 		}
124 
125 		if (length >= PMULL_MIN_LEN) {
126 			l = round_down(length, SCALE_F);
127 
128 			kernel_neon_begin();
129 			*crc = crc32_pmull_le(data, l, *crc);
130 			kernel_neon_end();
131 
132 			data += l;
133 			length -= l;
134 		}
135 	}
136 
137 	if (length > 0)
138 		*crc = fallback_crc32(*crc, data, length);
139 
140 	return 0;
141 }
142 
143 static int crc32c_pmull_update(struct shash_desc *desc, const u8 *data,
144 			       unsigned int length)
145 {
146 	u32 *crc = shash_desc_ctx(desc);
147 	unsigned int l;
148 
149 	if (may_use_simd()) {
150 		if ((u32)data % SCALE_F) {
151 			l = min_t(u32, length, SCALE_F - ((u32)data % SCALE_F));
152 
153 			*crc = fallback_crc32c(*crc, data, l);
154 
155 			data += l;
156 			length -= l;
157 		}
158 
159 		if (length >= PMULL_MIN_LEN) {
160 			l = round_down(length, SCALE_F);
161 
162 			kernel_neon_begin();
163 			*crc = crc32c_pmull_le(data, l, *crc);
164 			kernel_neon_end();
165 
166 			data += l;
167 			length -= l;
168 		}
169 	}
170 
171 	if (length > 0)
172 		*crc = fallback_crc32c(*crc, data, length);
173 
174 	return 0;
175 }
176 
177 static struct shash_alg crc32_pmull_algs[] = { {
178 	.setkey			= crc32_setkey,
179 	.init			= crc32_init,
180 	.update			= crc32_update,
181 	.final			= crc32_final,
182 	.descsize		= sizeof(u32),
183 	.digestsize		= sizeof(u32),
184 
185 	.base.cra_ctxsize	= sizeof(u32),
186 	.base.cra_init		= crc32_cra_init,
187 	.base.cra_name		= "crc32",
188 	.base.cra_driver_name	= "crc32-arm-ce",
189 	.base.cra_priority	= 200,
190 	.base.cra_blocksize	= 1,
191 	.base.cra_module	= THIS_MODULE,
192 }, {
193 	.setkey			= crc32_setkey,
194 	.init			= crc32_init,
195 	.update			= crc32c_update,
196 	.final			= crc32c_final,
197 	.descsize		= sizeof(u32),
198 	.digestsize		= sizeof(u32),
199 
200 	.base.cra_ctxsize	= sizeof(u32),
201 	.base.cra_init		= crc32c_cra_init,
202 	.base.cra_name		= "crc32c",
203 	.base.cra_driver_name	= "crc32c-arm-ce",
204 	.base.cra_priority	= 200,
205 	.base.cra_blocksize	= 1,
206 	.base.cra_module	= THIS_MODULE,
207 } };
208 
209 static int __init crc32_pmull_mod_init(void)
210 {
211 	if (elf_hwcap2 & HWCAP2_PMULL) {
212 		crc32_pmull_algs[0].update = crc32_pmull_update;
213 		crc32_pmull_algs[1].update = crc32c_pmull_update;
214 
215 		if (elf_hwcap2 & HWCAP2_CRC32) {
216 			fallback_crc32 = crc32_armv8_le;
217 			fallback_crc32c = crc32c_armv8_le;
218 		} else {
219 			fallback_crc32 = crc32_le;
220 			fallback_crc32c = __crc32c_le;
221 		}
222 	} else if (!(elf_hwcap2 & HWCAP2_CRC32)) {
223 		return -ENODEV;
224 	}
225 
226 	return crypto_register_shashes(crc32_pmull_algs,
227 				       ARRAY_SIZE(crc32_pmull_algs));
228 }
229 
230 static void __exit crc32_pmull_mod_exit(void)
231 {
232 	crypto_unregister_shashes(crc32_pmull_algs,
233 				  ARRAY_SIZE(crc32_pmull_algs));
234 }
235 
236 module_init(crc32_pmull_mod_init);
237 module_exit(crc32_pmull_mod_exit);
238 
239 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
240 MODULE_LICENSE("GPL v2");
241 MODULE_ALIAS_CRYPTO("crc32");
242 MODULE_ALIAS_CRYPTO("crc32c");
243