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/unaligned/access_ok.h>
12 #include <linux/cpufeature.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/string.h>
17 #include <asm/mipsregs.h>
19 #include <crypto/internal/hash.h>
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" \
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"
53 #define _CRC32(crc, value, size, type) \
55 __asm__ __volatile__( \
58 #type #size " %0, %1, %0\n\t" \
64 #define CRC32(crc, value, size) \
65 _CRC32(crc, value, size, crc32)
67 #define CRC32C(crc, value, size) \
68 _CRC32(crc, value, size, crc32c)
70 static u32 crc32_mips_le_hw(u32 crc_, const u8 *p, unsigned int len)
75 while (len >= sizeof(u64)) {
76 u64 value = get_unaligned_le64(p);
83 if (len & sizeof(u32)) {
84 #else /* !CONFIG_64BIT */
85 while (len >= sizeof(u32)) {
87 u32 value = get_unaligned_le32(p);
94 if (len & sizeof(u16)) {
95 u16 value = get_unaligned_le16(p);
101 if (len & sizeof(u8)) {
104 CRC32(crc, value, b);
110 static u32 crc32c_mips_le_hw(u32 crc_, const u8 *p, unsigned int len)
115 while (len >= sizeof(u64)) {
116 u64 value = get_unaligned_le64(p);
118 CRC32C(crc, value, d);
123 if (len & sizeof(u32)) {
124 #else /* !CONFIG_64BIT */
125 while (len >= sizeof(u32)) {
127 u32 value = get_unaligned_le32(p);
129 CRC32C(crc, value, w);
134 if (len & sizeof(u16)) {
135 u16 value = get_unaligned_le16(p);
137 CRC32C(crc, value, h);
141 if (len & sizeof(u8)) {
144 CRC32C(crc, value, b);
149 #define CHKSUM_BLOCK_SIZE 1
150 #define CHKSUM_DIGEST_SIZE 4
156 struct chksum_desc_ctx {
160 static int chksum_init(struct shash_desc *desc)
162 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
163 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
165 ctx->crc = mctx->key;
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
175 static int chksum_setkey(struct crypto_shash *tfm, const u8 *key,
178 struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
180 if (keylen != sizeof(mctx->key)) {
181 crypto_shash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
184 mctx->key = get_unaligned_le32(key);
188 static int chksum_update(struct shash_desc *desc, const u8 *data,
191 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
193 ctx->crc = crc32_mips_le_hw(ctx->crc, data, length);
197 static int chksumc_update(struct shash_desc *desc, const u8 *data,
200 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
202 ctx->crc = crc32c_mips_le_hw(ctx->crc, data, length);
206 static int chksum_final(struct shash_desc *desc, u8 *out)
208 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
210 put_unaligned_le32(ctx->crc, out);
214 static int chksumc_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 __chksum_finup(u32 crc, const u8 *data, unsigned int len, u8 *out)
224 put_unaligned_le32(crc32_mips_le_hw(crc, data, len), out);
228 static int __chksumc_finup(u32 crc, const u8 *data, unsigned int len, u8 *out)
230 put_unaligned_le32(~crc32c_mips_le_hw(crc, data, len), out);
234 static int chksum_finup(struct shash_desc *desc, const u8 *data,
235 unsigned int len, u8 *out)
237 struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
239 return __chksum_finup(ctx->crc, data, len, out);
242 static int chksumc_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 __chksumc_finup(ctx->crc, data, len, out);
250 static int chksum_digest(struct shash_desc *desc, const u8 *data,
251 unsigned int length, u8 *out)
253 struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
255 return __chksum_finup(mctx->key, data, length, out);
258 static int chksumc_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 __chksumc_finup(mctx->key, data, length, out);
266 static int chksum_cra_init(struct crypto_tfm *tfm)
268 struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
274 static struct shash_alg crc32_alg = {
275 .digestsize = CHKSUM_DIGEST_SIZE,
276 .setkey = chksum_setkey,
278 .update = chksum_update,
279 .final = chksum_final,
280 .finup = chksum_finup,
281 .digest = chksum_digest,
282 .descsize = sizeof(struct chksum_desc_ctx),
285 .cra_driver_name = "crc32-mips-hw",
287 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
288 .cra_blocksize = CHKSUM_BLOCK_SIZE,
290 .cra_ctxsize = sizeof(struct chksum_ctx),
291 .cra_module = THIS_MODULE,
292 .cra_init = chksum_cra_init,
296 static struct shash_alg crc32c_alg = {
297 .digestsize = CHKSUM_DIGEST_SIZE,
298 .setkey = chksum_setkey,
300 .update = chksumc_update,
301 .final = chksumc_final,
302 .finup = chksumc_finup,
303 .digest = chksumc_digest,
304 .descsize = sizeof(struct chksum_desc_ctx),
306 .cra_name = "crc32c",
307 .cra_driver_name = "crc32c-mips-hw",
309 .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
310 .cra_blocksize = CHKSUM_BLOCK_SIZE,
312 .cra_ctxsize = sizeof(struct chksum_ctx),
313 .cra_module = THIS_MODULE,
314 .cra_init = chksum_cra_init,
318 static int __init crc32_mod_init(void)
322 err = crypto_register_shash(&crc32_alg);
327 err = crypto_register_shash(&crc32c_alg);
330 crypto_unregister_shash(&crc32_alg);
337 static void __exit crc32_mod_exit(void)
339 crypto_unregister_shash(&crc32_alg);
340 crypto_unregister_shash(&crc32c_alg);
343 MODULE_AUTHOR("Marcin Nowakowski <marcin.nowakowski@mips.com");
344 MODULE_DESCRIPTION("CRC32 and CRC32C using optional MIPS instructions");
345 MODULE_LICENSE("GPL v2");
347 module_cpu_feature_match(MIPS_CRC32, crc32_mod_init);
348 module_exit(crc32_mod_exit);