1 // SPDX-License-Identifier: GPL-2.0
3 * crc32-mips.c - CRC32 and CRC32C using optional MIPSr6 instructions
5 * Module based on arm64/crypto/crc32-arm.c
7 * Copyright (C) 2014 Linaro Ltd <yazen.ghannam@linaro.org>
8 * Copyright (C) 2018 MIPS Tech, LLC
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 <linux/unaligned.h>
19 #include <crypto/internal/hash.h>
30 #ifndef TOOLCHAIN_SUPPORTS_CRC
31 #define _ASM_SET_CRC(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" \
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 #define _ASM_UNSET_CRC(op, SZ, TYPE) ".purgem " #op "\n\t"
41 #else /* !TOOLCHAIN_SUPPORTS_CRC */
42 #define _ASM_SET_CRC(op, SZ, TYPE) ".set\tcrc\n\t"
43 #define _ASM_UNSET_CRC(op, SZ, TYPE)
46 #define __CRC32(crc, value, op, SZ, TYPE) \
48 __asm__ __volatile__( \
50 _ASM_SET_CRC(op, SZ, TYPE) \
51 #op " %0, %1, %0\n\t" \
52 _ASM_UNSET_CRC(op, SZ, TYPE) \
58 #define _CRC32_crc32b(crc, value) __CRC32(crc, value, crc32b, 0, 0)
59 #define _CRC32_crc32h(crc, value) __CRC32(crc, value, crc32h, 1, 0)
60 #define _CRC32_crc32w(crc, value) __CRC32(crc, value, crc32w, 2, 0)
61 #define _CRC32_crc32d(crc, value) __CRC32(crc, value, crc32d, 3, 0)
62 #define _CRC32_crc32cb(crc, value) __CRC32(crc, value, crc32cb, 0, 1)
63 #define _CRC32_crc32ch(crc, value) __CRC32(crc, value, crc32ch, 1, 1)
64 #define _CRC32_crc32cw(crc, value) __CRC32(crc, value, crc32cw, 2, 1)
65 #define _CRC32_crc32cd(crc, value) __CRC32(crc, value, crc32cd, 3, 1)
67 #define _CRC32(crc, value, size, op) \
68 _CRC32_##op##size(crc, value)
70 #define CRC32(crc, value, size) \
71 _CRC32(crc, value, size, crc32)
73 #define CRC32C(crc, value, size) \
74 _CRC32(crc, value, size, crc32c)
76 static u32
crc32_mips_le_hw(u32 crc_
, const u8
*p
, unsigned int len
)
80 if (IS_ENABLED(CONFIG_64BIT
)) {
81 for (; len
>= sizeof(u64
); p
+= sizeof(u64
), len
-= sizeof(u64
)) {
82 u64 value
= get_unaligned_le64(p
);
87 if (len
& sizeof(u32
)) {
88 u32 value
= get_unaligned_le32(p
);
94 for (; len
>= sizeof(u32
); len
-= sizeof(u32
)) {
95 u32 value
= get_unaligned_le32(p
);
102 if (len
& sizeof(u16
)) {
103 u16 value
= get_unaligned_le16(p
);
105 CRC32(crc
, value
, h
);
109 if (len
& sizeof(u8
)) {
112 CRC32(crc
, value
, b
);
118 static u32
crc32c_mips_le_hw(u32 crc_
, const u8
*p
, unsigned int len
)
122 if (IS_ENABLED(CONFIG_64BIT
)) {
123 for (; len
>= sizeof(u64
); p
+= sizeof(u64
), len
-= sizeof(u64
)) {
124 u64 value
= get_unaligned_le64(p
);
126 CRC32C(crc
, value
, d
);
129 if (len
& sizeof(u32
)) {
130 u32 value
= get_unaligned_le32(p
);
132 CRC32C(crc
, value
, w
);
136 for (; len
>= sizeof(u32
); len
-= sizeof(u32
)) {
137 u32 value
= get_unaligned_le32(p
);
139 CRC32C(crc
, value
, w
);
144 if (len
& sizeof(u16
)) {
145 u16 value
= get_unaligned_le16(p
);
147 CRC32C(crc
, value
, h
);
151 if (len
& sizeof(u8
)) {
154 CRC32C(crc
, value
, b
);
159 #define CHKSUM_BLOCK_SIZE 1
160 #define CHKSUM_DIGEST_SIZE 4
166 struct chksum_desc_ctx
{
170 static int chksum_init(struct shash_desc
*desc
)
172 struct chksum_ctx
*mctx
= crypto_shash_ctx(desc
->tfm
);
173 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
175 ctx
->crc
= mctx
->key
;
181 * Setting the seed allows arbitrary accumulators and flexible XOR policy
182 * If your algorithm starts with ~0, then XOR with ~0 before you set
185 static int chksum_setkey(struct crypto_shash
*tfm
, const u8
*key
,
188 struct chksum_ctx
*mctx
= crypto_shash_ctx(tfm
);
190 if (keylen
!= sizeof(mctx
->key
))
192 mctx
->key
= get_unaligned_le32(key
);
196 static int chksum_update(struct shash_desc
*desc
, const u8
*data
,
199 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
201 ctx
->crc
= crc32_mips_le_hw(ctx
->crc
, data
, length
);
205 static int chksumc_update(struct shash_desc
*desc
, const u8
*data
,
208 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
210 ctx
->crc
= crc32c_mips_le_hw(ctx
->crc
, data
, length
);
214 static int chksum_final(struct shash_desc
*desc
, u8
*out
)
216 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
218 put_unaligned_le32(ctx
->crc
, out
);
222 static int chksumc_final(struct shash_desc
*desc
, u8
*out
)
224 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
226 put_unaligned_le32(~ctx
->crc
, out
);
230 static int __chksum_finup(u32 crc
, const u8
*data
, unsigned int len
, u8
*out
)
232 put_unaligned_le32(crc32_mips_le_hw(crc
, data
, len
), out
);
236 static int __chksumc_finup(u32 crc
, const u8
*data
, unsigned int len
, u8
*out
)
238 put_unaligned_le32(~crc32c_mips_le_hw(crc
, data
, len
), out
);
242 static int chksum_finup(struct shash_desc
*desc
, const u8
*data
,
243 unsigned int len
, u8
*out
)
245 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
247 return __chksum_finup(ctx
->crc
, data
, len
, out
);
250 static int chksumc_finup(struct shash_desc
*desc
, const u8
*data
,
251 unsigned int len
, u8
*out
)
253 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
255 return __chksumc_finup(ctx
->crc
, data
, len
, out
);
258 static int chksum_digest(struct shash_desc
*desc
, const u8
*data
,
259 unsigned int length
, u8
*out
)
261 struct chksum_ctx
*mctx
= crypto_shash_ctx(desc
->tfm
);
263 return __chksum_finup(mctx
->key
, data
, length
, out
);
266 static int chksumc_digest(struct shash_desc
*desc
, const u8
*data
,
267 unsigned int length
, u8
*out
)
269 struct chksum_ctx
*mctx
= crypto_shash_ctx(desc
->tfm
);
271 return __chksumc_finup(mctx
->key
, data
, length
, out
);
274 static int chksum_cra_init(struct crypto_tfm
*tfm
)
276 struct chksum_ctx
*mctx
= crypto_tfm_ctx(tfm
);
282 static struct shash_alg crc32_alg
= {
283 .digestsize
= CHKSUM_DIGEST_SIZE
,
284 .setkey
= chksum_setkey
,
286 .update
= chksum_update
,
287 .final
= chksum_final
,
288 .finup
= chksum_finup
,
289 .digest
= chksum_digest
,
290 .descsize
= sizeof(struct chksum_desc_ctx
),
293 .cra_driver_name
= "crc32-mips-hw",
295 .cra_flags
= CRYPTO_ALG_OPTIONAL_KEY
,
296 .cra_blocksize
= CHKSUM_BLOCK_SIZE
,
297 .cra_ctxsize
= sizeof(struct chksum_ctx
),
298 .cra_module
= THIS_MODULE
,
299 .cra_init
= chksum_cra_init
,
303 static struct shash_alg crc32c_alg
= {
304 .digestsize
= CHKSUM_DIGEST_SIZE
,
305 .setkey
= chksum_setkey
,
307 .update
= chksumc_update
,
308 .final
= chksumc_final
,
309 .finup
= chksumc_finup
,
310 .digest
= chksumc_digest
,
311 .descsize
= sizeof(struct chksum_desc_ctx
),
313 .cra_name
= "crc32c",
314 .cra_driver_name
= "crc32c-mips-hw",
316 .cra_flags
= CRYPTO_ALG_OPTIONAL_KEY
,
317 .cra_blocksize
= CHKSUM_BLOCK_SIZE
,
318 .cra_ctxsize
= sizeof(struct chksum_ctx
),
319 .cra_module
= THIS_MODULE
,
320 .cra_init
= chksum_cra_init
,
324 static int __init
crc32_mod_init(void)
328 err
= crypto_register_shash(&crc32_alg
);
333 err
= crypto_register_shash(&crc32c_alg
);
336 crypto_unregister_shash(&crc32_alg
);
343 static void __exit
crc32_mod_exit(void)
345 crypto_unregister_shash(&crc32_alg
);
346 crypto_unregister_shash(&crc32c_alg
);
349 MODULE_AUTHOR("Marcin Nowakowski <marcin.nowakowski@mips.com");
350 MODULE_DESCRIPTION("CRC32 and CRC32C using optional MIPS instructions");
351 MODULE_LICENSE("GPL v2");
353 module_cpu_feature_match(MIPS_CRC32
, crc32_mod_init
);
354 module_exit(crc32_mod_exit
);