1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * crc32-mips.c - CRC32 and CRC32C using optional MIPSr6 instructions
4 *
5 * Module based on arm64/crypto/crc32-arm.c
6 *
7 * Copyright (C) 2014 Linaro Ltd <yazen.ghannam@linaro.org>
8 * Copyright (C) 2018 MIPS Tech, LLC
9 */
10
11 #include <linux/cpufeature.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/string.h>
16 #include <asm/mipsregs.h>
17 #include <asm/unaligned.h>
18
19 #include <crypto/internal/hash.h>
20
21 enum crc_op_size {
22 b, h, w, d,
23 };
24
25 enum crc_type {
26 crc32,
27 crc32c,
28 };
29
30 #ifndef TOOLCHAIN_SUPPORTS_CRC
31 #define _ASM_MACRO_CRC32(OP, SZ, TYPE) \
32 _ASM_MACRO_3R(OP, rt, rs, rt2, \
33 ".ifnc \\rt, \\rt2\n\t" \
34 ".error \"invalid operands \\\"" #OP " \\rt,\\rs,\\rt2\\\"\"\n\t" \
35 ".endif\n\t" \
36 _ASM_INSN_IF_MIPS(0x7c00000f | (__rt << 16) | (__rs << 21) | \
37 ((SZ) << 6) | ((TYPE) << 8)) \
38 _ASM_INSN32_IF_MM(0x00000030 | (__rs << 16) | (__rt << 21) | \
39 ((SZ) << 14) | ((TYPE) << 3)))
40 _ASM_MACRO_CRC32(crc32b, 0, 0);
41 _ASM_MACRO_CRC32(crc32h, 1, 0);
42 _ASM_MACRO_CRC32(crc32w, 2, 0);
43 _ASM_MACRO_CRC32(crc32d, 3, 0);
44 _ASM_MACRO_CRC32(crc32cb, 0, 1);
45 _ASM_MACRO_CRC32(crc32ch, 1, 1);
46 _ASM_MACRO_CRC32(crc32cw, 2, 1);
47 _ASM_MACRO_CRC32(crc32cd, 3, 1);
48 #define _ASM_SET_CRC ""
49 #else /* !TOOLCHAIN_SUPPORTS_CRC */
50 #define _ASM_SET_CRC ".set\tcrc\n\t"
51 #endif
52
53 #define _CRC32(crc, value, size, type) \
54 do { \
55 __asm__ __volatile__( \
56 ".set push\n\t" \
57 _ASM_SET_CRC \
58 #type #size " %0, %1, %0\n\t" \
59 ".set pop" \
60 : "+r" (crc) \
61 : "r" (value)); \
62 } while (0)
63
64 #define CRC32(crc, value, size) \
65 _CRC32(crc, value, size, crc32)
66
67 #define CRC32C(crc, value, size) \
68 _CRC32(crc, value, size, crc32c)
69
crc32_mips_le_hw(u32 crc_,const u8 * p,unsigned int len)70 static u32 crc32_mips_le_hw(u32 crc_, const u8 *p, unsigned int len)
71 {
72 u32 crc = crc_;
73
74 #ifdef CONFIG_64BIT
75 while (len >= sizeof(u64)) {
76 u64 value = get_unaligned_le64(p);
77
78 CRC32(crc, value, d);
79 p += sizeof(u64);
80 len -= sizeof(u64);
81 }
82
83 if (len & sizeof(u32)) {
84 #else /* !CONFIG_64BIT */
85 while (len >= sizeof(u32)) {
86 #endif
87 u32 value = get_unaligned_le32(p);
88
89 CRC32(crc, value, w);
90 p += sizeof(u32);
91 len -= sizeof(u32);
92 }
93
94 if (len & sizeof(u16)) {
95 u16 value = get_unaligned_le16(p);
96
97 CRC32(crc, value, h);
98 p += sizeof(u16);
99 }
100
101 if (len & sizeof(u8)) {
102 u8 value = *p++;
103
104 CRC32(crc, value, b);
105 }
106
107 return crc;
108 }
109
110 static u32 crc32c_mips_le_hw(u32 crc_, const u8 *p, unsigned int len)
111 {
112 u32 crc = crc_;
113
114 #ifdef CONFIG_64BIT
115 while (len >= sizeof(u64)) {
116 u64 value = get_unaligned_le64(p);
117
118 CRC32C(crc, value, d);
119 p += sizeof(u64);
120 len -= sizeof(u64);
121 }
122
123 if (len & sizeof(u32)) {
124 #else /* !CONFIG_64BIT */
125 while (len >= sizeof(u32)) {
126 #endif
127 u32 value = get_unaligned_le32(p);
128
129 CRC32C(crc, value, w);
130 p += sizeof(u32);
131 len -= sizeof(u32);
132 }
133
134 if (len & sizeof(u16)) {
135 u16 value = get_unaligned_le16(p);
136
137 CRC32C(crc, value, h);
138 p += sizeof(u16);
139 }
140
141 if (len & sizeof(u8)) {
142 u8 value = *p++;
143
144 CRC32C(crc, value, b);
145 }
146 return crc;
147 }
148
149 #define CHKSUM_BLOCK_SIZE 1
150 #define CHKSUM_DIGEST_SIZE 4
151
152 struct chksum_ctx {
153 u32 key;
154 };
155
156 struct chksum_desc_ctx {
157 u32 crc;
158 };
159
160 static int chksum_init(struct shash_desc *desc)
161 {
162 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
163 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
164
165 ctx->crc = mctx->key;
166
167 return 0;
168 }
169
170 /*
171 * Setting the seed allows arbitrary accumulators and flexible XOR policy
172 * If your algorithm starts with ~0, then XOR with ~0 before you set
173 * the seed.
174 */
175 static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
176 unsigned int keylen)
177 {
178 struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
179
180 if (keylen != sizeof(mctx->key))
181 return -EINVAL;
182 mctx->key = get_unaligned_le32(key);
183 return 0;
184 }
185
186 static int chksum_update(struct shash_desc *desc, const u8 *data,
187 unsigned int length)
188 {
189 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
190
191 ctx->crc = crc32_mips_le_hw(ctx->crc, data, length);
192 return 0;
193 }
194
195 static int chksumc_update(struct shash_desc *desc, const u8 *data,
196 unsigned int length)
197 {
198 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
199
200 ctx->crc = crc32c_mips_le_hw(ctx->crc, data, length);
201 return 0;
202 }
203
204 static int chksum_final(struct shash_desc *desc, u8 *out)
205 {
206 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
207
208 put_unaligned_le32(ctx->crc, out);
209 return 0;
210 }
211
212 static int chksumc_final(struct shash_desc *desc, u8 *out)
213 {
214 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
215
216 put_unaligned_le32(~ctx->crc, out);
217 return 0;
218 }
219
220 static int __chksum_finup(u32 crc, const u8 *data, unsigned int len, u8 *out)
221 {
222 put_unaligned_le32(crc32_mips_le_hw(crc, data, len), out);
223 return 0;
224 }
225
226 static int __chksumc_finup(u32 crc, const u8 *data, unsigned int len, u8 *out)
227 {
228 put_unaligned_le32(~crc32c_mips_le_hw(crc, data, len), out);
229 return 0;
230 }
231
232 static int chksum_finup(struct shash_desc *desc, const u8 *data,
233 unsigned int len, u8 *out)
234 {
235 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
236
237 return __chksum_finup(ctx->crc, data, len, out);
238 }
239
240 static int chksumc_finup(struct shash_desc *desc, const u8 *data,
241 unsigned int len, u8 *out)
242 {
243 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
244
245 return __chksumc_finup(ctx->crc, data, len, out);
246 }
247
248 static int chksum_digest(struct shash_desc *desc, const u8 *data,
249 unsigned int length, u8 *out)
250 {
251 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
252
253 return __chksum_finup(mctx->key, data, length, out);
254 }
255
256 static int chksumc_digest(struct shash_desc *desc, const u8 *data,
257 unsigned int length, u8 *out)
258 {
259 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
260
261 return __chksumc_finup(mctx->key, data, length, out);
262 }
263
264 static int chksum_cra_init(struct crypto_tfm *tfm)
265 {
266 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
267
268 mctx->key = ~0;
269 return 0;
270 }
271
272 static struct shash_alg crc32_alg = {
273 .digestsize = CHKSUM_DIGEST_SIZE,
274 .setkey = chksum_setkey,
275 .init = chksum_init,
276 .update = chksum_update,
277 .final = chksum_final,
278 .finup = chksum_finup,
279 .digest = chksum_digest,
280 .descsize = sizeof(struct chksum_desc_ctx),
281 .base = {
282 .cra_name = "crc32",
283 .cra_driver_name = "crc32-mips-hw",
284 .cra_priority = 300,
285 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
286 .cra_blocksize = CHKSUM_BLOCK_SIZE,
287 .cra_alignmask = 0,
288 .cra_ctxsize = sizeof(struct chksum_ctx),
289 .cra_module = THIS_MODULE,
290 .cra_init = chksum_cra_init,
291 }
292 };
293
294 static struct shash_alg crc32c_alg = {
295 .digestsize = CHKSUM_DIGEST_SIZE,
296 .setkey = chksum_setkey,
297 .init = chksum_init,
298 .update = chksumc_update,
299 .final = chksumc_final,
300 .finup = chksumc_finup,
301 .digest = chksumc_digest,
302 .descsize = sizeof(struct chksum_desc_ctx),
303 .base = {
304 .cra_name = "crc32c",
305 .cra_driver_name = "crc32c-mips-hw",
306 .cra_priority = 300,
307 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
308 .cra_blocksize = CHKSUM_BLOCK_SIZE,
309 .cra_alignmask = 0,
310 .cra_ctxsize = sizeof(struct chksum_ctx),
311 .cra_module = THIS_MODULE,
312 .cra_init = chksum_cra_init,
313 }
314 };
315
316 static int __init crc32_mod_init(void)
317 {
318 int err;
319
320 err = crypto_register_shash(&crc32_alg);
321
322 if (err)
323 return err;
324
325 err = crypto_register_shash(&crc32c_alg);
326
327 if (err) {
328 crypto_unregister_shash(&crc32_alg);
329 return err;
330 }
331
332 return 0;
333 }
334
335 static void __exit crc32_mod_exit(void)
336 {
337 crypto_unregister_shash(&crc32_alg);
338 crypto_unregister_shash(&crc32c_alg);
339 }
340
341 MODULE_AUTHOR("Marcin Nowakowski <marcin.nowakowski@mips.com");
342 MODULE_DESCRIPTION("CRC32 and CRC32C using optional MIPS instructions");
343 MODULE_LICENSE("GPL v2");
344
345 module_cpu_feature_match(MIPS_CRC32, crc32_mod_init);
346 module_exit(crc32_mod_exit);
347