1*2f164822SMin Zhou // SPDX-License-Identifier: GPL-2.0
2*2f164822SMin Zhou /*
3*2f164822SMin Zhou * crc32.c - CRC32 and CRC32C using LoongArch crc* instructions
4*2f164822SMin Zhou *
5*2f164822SMin Zhou * Module based on mips/crypto/crc32-mips.c
6*2f164822SMin Zhou *
7*2f164822SMin Zhou * Copyright (C) 2014 Linaro Ltd <yazen.ghannam@linaro.org>
8*2f164822SMin Zhou * Copyright (C) 2018 MIPS Tech, LLC
9*2f164822SMin Zhou * Copyright (C) 2020-2023 Loongson Technology Corporation Limited
10*2f164822SMin Zhou */
11*2f164822SMin Zhou
12*2f164822SMin Zhou #include <linux/module.h>
13*2f164822SMin Zhou #include <crypto/internal/hash.h>
14*2f164822SMin Zhou
15*2f164822SMin Zhou #include <asm/cpu-features.h>
16*2f164822SMin Zhou #include <asm/unaligned.h>
17*2f164822SMin Zhou
18*2f164822SMin Zhou #define _CRC32(crc, value, size, type) \
19*2f164822SMin Zhou do { \
20*2f164822SMin Zhou __asm__ __volatile__( \
21*2f164822SMin Zhou #type ".w." #size ".w" " %0, %1, %0\n\t"\
22*2f164822SMin Zhou : "+r" (crc) \
23*2f164822SMin Zhou : "r" (value) \
24*2f164822SMin Zhou : "memory"); \
25*2f164822SMin Zhou } while (0)
26*2f164822SMin Zhou
27*2f164822SMin Zhou #define CRC32(crc, value, size) _CRC32(crc, value, size, crc)
28*2f164822SMin Zhou #define CRC32C(crc, value, size) _CRC32(crc, value, size, crcc)
29*2f164822SMin Zhou
crc32_loongarch_hw(u32 crc_,const u8 * p,unsigned int len)30*2f164822SMin Zhou static u32 crc32_loongarch_hw(u32 crc_, const u8 *p, unsigned int len)
31*2f164822SMin Zhou {
32*2f164822SMin Zhou u32 crc = crc_;
33*2f164822SMin Zhou
34*2f164822SMin Zhou while (len >= sizeof(u64)) {
35*2f164822SMin Zhou u64 value = get_unaligned_le64(p);
36*2f164822SMin Zhou
37*2f164822SMin Zhou CRC32(crc, value, d);
38*2f164822SMin Zhou p += sizeof(u64);
39*2f164822SMin Zhou len -= sizeof(u64);
40*2f164822SMin Zhou }
41*2f164822SMin Zhou
42*2f164822SMin Zhou if (len & sizeof(u32)) {
43*2f164822SMin Zhou u32 value = get_unaligned_le32(p);
44*2f164822SMin Zhou
45*2f164822SMin Zhou CRC32(crc, value, w);
46*2f164822SMin Zhou p += sizeof(u32);
47*2f164822SMin Zhou }
48*2f164822SMin Zhou
49*2f164822SMin Zhou if (len & sizeof(u16)) {
50*2f164822SMin Zhou u16 value = get_unaligned_le16(p);
51*2f164822SMin Zhou
52*2f164822SMin Zhou CRC32(crc, value, h);
53*2f164822SMin Zhou p += sizeof(u16);
54*2f164822SMin Zhou }
55*2f164822SMin Zhou
56*2f164822SMin Zhou if (len & sizeof(u8)) {
57*2f164822SMin Zhou u8 value = *p++;
58*2f164822SMin Zhou
59*2f164822SMin Zhou CRC32(crc, value, b);
60*2f164822SMin Zhou }
61*2f164822SMin Zhou
62*2f164822SMin Zhou return crc;
63*2f164822SMin Zhou }
64*2f164822SMin Zhou
crc32c_loongarch_hw(u32 crc_,const u8 * p,unsigned int len)65*2f164822SMin Zhou static u32 crc32c_loongarch_hw(u32 crc_, const u8 *p, unsigned int len)
66*2f164822SMin Zhou {
67*2f164822SMin Zhou u32 crc = crc_;
68*2f164822SMin Zhou
69*2f164822SMin Zhou while (len >= sizeof(u64)) {
70*2f164822SMin Zhou u64 value = get_unaligned_le64(p);
71*2f164822SMin Zhou
72*2f164822SMin Zhou CRC32C(crc, value, d);
73*2f164822SMin Zhou p += sizeof(u64);
74*2f164822SMin Zhou len -= sizeof(u64);
75*2f164822SMin Zhou }
76*2f164822SMin Zhou
77*2f164822SMin Zhou if (len & sizeof(u32)) {
78*2f164822SMin Zhou u32 value = get_unaligned_le32(p);
79*2f164822SMin Zhou
80*2f164822SMin Zhou CRC32C(crc, value, w);
81*2f164822SMin Zhou p += sizeof(u32);
82*2f164822SMin Zhou }
83*2f164822SMin Zhou
84*2f164822SMin Zhou if (len & sizeof(u16)) {
85*2f164822SMin Zhou u16 value = get_unaligned_le16(p);
86*2f164822SMin Zhou
87*2f164822SMin Zhou CRC32C(crc, value, h);
88*2f164822SMin Zhou p += sizeof(u16);
89*2f164822SMin Zhou }
90*2f164822SMin Zhou
91*2f164822SMin Zhou if (len & sizeof(u8)) {
92*2f164822SMin Zhou u8 value = *p++;
93*2f164822SMin Zhou
94*2f164822SMin Zhou CRC32C(crc, value, b);
95*2f164822SMin Zhou }
96*2f164822SMin Zhou
97*2f164822SMin Zhou return crc;
98*2f164822SMin Zhou }
99*2f164822SMin Zhou
100*2f164822SMin Zhou #define CHKSUM_BLOCK_SIZE 1
101*2f164822SMin Zhou #define CHKSUM_DIGEST_SIZE 4
102*2f164822SMin Zhou
103*2f164822SMin Zhou struct chksum_ctx {
104*2f164822SMin Zhou u32 key;
105*2f164822SMin Zhou };
106*2f164822SMin Zhou
107*2f164822SMin Zhou struct chksum_desc_ctx {
108*2f164822SMin Zhou u32 crc;
109*2f164822SMin Zhou };
110*2f164822SMin Zhou
chksum_init(struct shash_desc * desc)111*2f164822SMin Zhou static int chksum_init(struct shash_desc *desc)
112*2f164822SMin Zhou {
113*2f164822SMin Zhou struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
114*2f164822SMin Zhou struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
115*2f164822SMin Zhou
116*2f164822SMin Zhou ctx->crc = mctx->key;
117*2f164822SMin Zhou
118*2f164822SMin Zhou return 0;
119*2f164822SMin Zhou }
120*2f164822SMin Zhou
121*2f164822SMin Zhou /*
122*2f164822SMin Zhou * Setting the seed allows arbitrary accumulators and flexible XOR policy
123*2f164822SMin Zhou * If your algorithm starts with ~0, then XOR with ~0 before you set the seed.
124*2f164822SMin Zhou */
chksum_setkey(struct crypto_shash * tfm,const u8 * key,unsigned int keylen)125*2f164822SMin Zhou static int chksum_setkey(struct crypto_shash *tfm, const u8 *key, unsigned int keylen)
126*2f164822SMin Zhou {
127*2f164822SMin Zhou struct chksum_ctx *mctx = crypto_shash_ctx(tfm);
128*2f164822SMin Zhou
129*2f164822SMin Zhou if (keylen != sizeof(mctx->key))
130*2f164822SMin Zhou return -EINVAL;
131*2f164822SMin Zhou
132*2f164822SMin Zhou mctx->key = get_unaligned_le32(key);
133*2f164822SMin Zhou
134*2f164822SMin Zhou return 0;
135*2f164822SMin Zhou }
136*2f164822SMin Zhou
chksum_update(struct shash_desc * desc,const u8 * data,unsigned int length)137*2f164822SMin Zhou static int chksum_update(struct shash_desc *desc, const u8 *data, unsigned int length)
138*2f164822SMin Zhou {
139*2f164822SMin Zhou struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
140*2f164822SMin Zhou
141*2f164822SMin Zhou ctx->crc = crc32_loongarch_hw(ctx->crc, data, length);
142*2f164822SMin Zhou return 0;
143*2f164822SMin Zhou }
144*2f164822SMin Zhou
chksumc_update(struct shash_desc * desc,const u8 * data,unsigned int length)145*2f164822SMin Zhou static int chksumc_update(struct shash_desc *desc, const u8 *data, unsigned int length)
146*2f164822SMin Zhou {
147*2f164822SMin Zhou struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
148*2f164822SMin Zhou
149*2f164822SMin Zhou ctx->crc = crc32c_loongarch_hw(ctx->crc, data, length);
150*2f164822SMin Zhou return 0;
151*2f164822SMin Zhou }
152*2f164822SMin Zhou
chksum_final(struct shash_desc * desc,u8 * out)153*2f164822SMin Zhou static int chksum_final(struct shash_desc *desc, u8 *out)
154*2f164822SMin Zhou {
155*2f164822SMin Zhou struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
156*2f164822SMin Zhou
157*2f164822SMin Zhou put_unaligned_le32(ctx->crc, out);
158*2f164822SMin Zhou return 0;
159*2f164822SMin Zhou }
160*2f164822SMin Zhou
chksumc_final(struct shash_desc * desc,u8 * out)161*2f164822SMin Zhou static int chksumc_final(struct shash_desc *desc, u8 *out)
162*2f164822SMin Zhou {
163*2f164822SMin Zhou struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
164*2f164822SMin Zhou
165*2f164822SMin Zhou put_unaligned_le32(~ctx->crc, out);
166*2f164822SMin Zhou return 0;
167*2f164822SMin Zhou }
168*2f164822SMin Zhou
__chksum_finup(u32 crc,const u8 * data,unsigned int len,u8 * out)169*2f164822SMin Zhou static int __chksum_finup(u32 crc, const u8 *data, unsigned int len, u8 *out)
170*2f164822SMin Zhou {
171*2f164822SMin Zhou put_unaligned_le32(crc32_loongarch_hw(crc, data, len), out);
172*2f164822SMin Zhou return 0;
173*2f164822SMin Zhou }
174*2f164822SMin Zhou
__chksumc_finup(u32 crc,const u8 * data,unsigned int len,u8 * out)175*2f164822SMin Zhou static int __chksumc_finup(u32 crc, const u8 *data, unsigned int len, u8 *out)
176*2f164822SMin Zhou {
177*2f164822SMin Zhou put_unaligned_le32(~crc32c_loongarch_hw(crc, data, len), out);
178*2f164822SMin Zhou return 0;
179*2f164822SMin Zhou }
180*2f164822SMin Zhou
chksum_finup(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)181*2f164822SMin Zhou static int chksum_finup(struct shash_desc *desc, const u8 *data, unsigned int len, u8 *out)
182*2f164822SMin Zhou {
183*2f164822SMin Zhou struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
184*2f164822SMin Zhou
185*2f164822SMin Zhou return __chksum_finup(ctx->crc, data, len, out);
186*2f164822SMin Zhou }
187*2f164822SMin Zhou
chksumc_finup(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)188*2f164822SMin Zhou static int chksumc_finup(struct shash_desc *desc, const u8 *data, unsigned int len, u8 *out)
189*2f164822SMin Zhou {
190*2f164822SMin Zhou struct chksum_desc_ctx *ctx = shash_desc_ctx(desc);
191*2f164822SMin Zhou
192*2f164822SMin Zhou return __chksumc_finup(ctx->crc, data, len, out);
193*2f164822SMin Zhou }
194*2f164822SMin Zhou
chksum_digest(struct shash_desc * desc,const u8 * data,unsigned int length,u8 * out)195*2f164822SMin Zhou static int chksum_digest(struct shash_desc *desc, const u8 *data, unsigned int length, u8 *out)
196*2f164822SMin Zhou {
197*2f164822SMin Zhou struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
198*2f164822SMin Zhou
199*2f164822SMin Zhou return __chksum_finup(mctx->key, data, length, out);
200*2f164822SMin Zhou }
201*2f164822SMin Zhou
chksumc_digest(struct shash_desc * desc,const u8 * data,unsigned int length,u8 * out)202*2f164822SMin Zhou static int chksumc_digest(struct shash_desc *desc, const u8 *data, unsigned int length, u8 *out)
203*2f164822SMin Zhou {
204*2f164822SMin Zhou struct chksum_ctx *mctx = crypto_shash_ctx(desc->tfm);
205*2f164822SMin Zhou
206*2f164822SMin Zhou return __chksumc_finup(mctx->key, data, length, out);
207*2f164822SMin Zhou }
208*2f164822SMin Zhou
chksum_cra_init(struct crypto_tfm * tfm)209*2f164822SMin Zhou static int chksum_cra_init(struct crypto_tfm *tfm)
210*2f164822SMin Zhou {
211*2f164822SMin Zhou struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
212*2f164822SMin Zhou
213*2f164822SMin Zhou mctx->key = 0;
214*2f164822SMin Zhou return 0;
215*2f164822SMin Zhou }
216*2f164822SMin Zhou
chksumc_cra_init(struct crypto_tfm * tfm)217*2f164822SMin Zhou static int chksumc_cra_init(struct crypto_tfm *tfm)
218*2f164822SMin Zhou {
219*2f164822SMin Zhou struct chksum_ctx *mctx = crypto_tfm_ctx(tfm);
220*2f164822SMin Zhou
221*2f164822SMin Zhou mctx->key = ~0;
222*2f164822SMin Zhou return 0;
223*2f164822SMin Zhou }
224*2f164822SMin Zhou
225*2f164822SMin Zhou static struct shash_alg crc32_alg = {
226*2f164822SMin Zhou .digestsize = CHKSUM_DIGEST_SIZE,
227*2f164822SMin Zhou .setkey = chksum_setkey,
228*2f164822SMin Zhou .init = chksum_init,
229*2f164822SMin Zhou .update = chksum_update,
230*2f164822SMin Zhou .final = chksum_final,
231*2f164822SMin Zhou .finup = chksum_finup,
232*2f164822SMin Zhou .digest = chksum_digest,
233*2f164822SMin Zhou .descsize = sizeof(struct chksum_desc_ctx),
234*2f164822SMin Zhou .base = {
235*2f164822SMin Zhou .cra_name = "crc32",
236*2f164822SMin Zhou .cra_driver_name = "crc32-loongarch",
237*2f164822SMin Zhou .cra_priority = 300,
238*2f164822SMin Zhou .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
239*2f164822SMin Zhou .cra_blocksize = CHKSUM_BLOCK_SIZE,
240*2f164822SMin Zhou .cra_alignmask = 0,
241*2f164822SMin Zhou .cra_ctxsize = sizeof(struct chksum_ctx),
242*2f164822SMin Zhou .cra_module = THIS_MODULE,
243*2f164822SMin Zhou .cra_init = chksum_cra_init,
244*2f164822SMin Zhou }
245*2f164822SMin Zhou };
246*2f164822SMin Zhou
247*2f164822SMin Zhou static struct shash_alg crc32c_alg = {
248*2f164822SMin Zhou .digestsize = CHKSUM_DIGEST_SIZE,
249*2f164822SMin Zhou .setkey = chksum_setkey,
250*2f164822SMin Zhou .init = chksum_init,
251*2f164822SMin Zhou .update = chksumc_update,
252*2f164822SMin Zhou .final = chksumc_final,
253*2f164822SMin Zhou .finup = chksumc_finup,
254*2f164822SMin Zhou .digest = chksumc_digest,
255*2f164822SMin Zhou .descsize = sizeof(struct chksum_desc_ctx),
256*2f164822SMin Zhou .base = {
257*2f164822SMin Zhou .cra_name = "crc32c",
258*2f164822SMin Zhou .cra_driver_name = "crc32c-loongarch",
259*2f164822SMin Zhou .cra_priority = 300,
260*2f164822SMin Zhou .cra_flags = CRYPTO_ALG_OPTIONAL_KEY,
261*2f164822SMin Zhou .cra_blocksize = CHKSUM_BLOCK_SIZE,
262*2f164822SMin Zhou .cra_alignmask = 0,
263*2f164822SMin Zhou .cra_ctxsize = sizeof(struct chksum_ctx),
264*2f164822SMin Zhou .cra_module = THIS_MODULE,
265*2f164822SMin Zhou .cra_init = chksumc_cra_init,
266*2f164822SMin Zhou }
267*2f164822SMin Zhou };
268*2f164822SMin Zhou
crc32_mod_init(void)269*2f164822SMin Zhou static int __init crc32_mod_init(void)
270*2f164822SMin Zhou {
271*2f164822SMin Zhou int err;
272*2f164822SMin Zhou
273*2f164822SMin Zhou if (!cpu_has(CPU_FEATURE_CRC32))
274*2f164822SMin Zhou return 0;
275*2f164822SMin Zhou
276*2f164822SMin Zhou err = crypto_register_shash(&crc32_alg);
277*2f164822SMin Zhou if (err)
278*2f164822SMin Zhou return err;
279*2f164822SMin Zhou
280*2f164822SMin Zhou err = crypto_register_shash(&crc32c_alg);
281*2f164822SMin Zhou if (err)
282*2f164822SMin Zhou return err;
283*2f164822SMin Zhou
284*2f164822SMin Zhou return 0;
285*2f164822SMin Zhou }
286*2f164822SMin Zhou
crc32_mod_exit(void)287*2f164822SMin Zhou static void __exit crc32_mod_exit(void)
288*2f164822SMin Zhou {
289*2f164822SMin Zhou if (!cpu_has(CPU_FEATURE_CRC32))
290*2f164822SMin Zhou return;
291*2f164822SMin Zhou
292*2f164822SMin Zhou crypto_unregister_shash(&crc32_alg);
293*2f164822SMin Zhou crypto_unregister_shash(&crc32c_alg);
294*2f164822SMin Zhou }
295*2f164822SMin Zhou
296*2f164822SMin Zhou module_init(crc32_mod_init);
297*2f164822SMin Zhou module_exit(crc32_mod_exit);
298*2f164822SMin Zhou
299*2f164822SMin Zhou MODULE_AUTHOR("Min Zhou <zhoumin@loongson.cn>");
300*2f164822SMin Zhou MODULE_AUTHOR("Huacai Chen <chenhuacai@loongson.cn>");
301*2f164822SMin Zhou MODULE_DESCRIPTION("CRC32 and CRC32C using LoongArch crc* instructions");
302*2f164822SMin Zhou MODULE_LICENSE("GPL v2");
303