1 // SPDX-License-Identifier: GPL-2.0-or-later
7 *@Article{castagnoli-crc,
8 * author = { Guy Castagnoli and Stefan Braeuer and Martin Herrman},
9 * title = {{Optimization of Cyclic Redundancy-Check Codes with 24
10 * and 32 Parity Bits}},
11 * journal = IEEE Transactions on Communication,
18 * Used by the iSCSI driver, possibly others, and derived from
19 * the iscsi-crc.c module of the linux-iscsi driver at
20 * http://linux-iscsi.sourceforge.net.
22 * Following the example of lib/crc32, this function is intended to be
23 * flexible and useful for all users. Modules that currently have their
24 * own crc32c, but hopefully may be able to use this one are:
25 * net/sctp (please add all your doco to here if you change to
29 * Copyright (c) 2004 Cisco Systems, Inc.
30 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
33 #include <linux/unaligned.h>
34 #include <crypto/internal/hash.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
38 #include <linux/kernel.h>
39 #include <linux/crc32.h>
41 #define CHKSUM_BLOCK_SIZE 1
42 #define CHKSUM_DIGEST_SIZE 4
48 struct chksum_desc_ctx
{
53 * Steps through buffer one byte at a time, calculates reflected
57 static int chksum_init(struct shash_desc
*desc
)
59 struct chksum_ctx
*mctx
= crypto_shash_ctx(desc
->tfm
);
60 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
68 * Setting the seed allows arbitrary accumulators and flexible XOR policy
69 * If your algorithm starts with ~0, then XOR with ~0 before you set
72 static int chksum_setkey(struct crypto_shash
*tfm
, const u8
*key
,
75 struct chksum_ctx
*mctx
= crypto_shash_ctx(tfm
);
77 if (keylen
!= sizeof(mctx
->key
))
79 mctx
->key
= get_unaligned_le32(key
);
83 static int chksum_update(struct shash_desc
*desc
, const u8
*data
,
86 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
88 ctx
->crc
= __crc32c_le_base(ctx
->crc
, data
, length
);
92 static int chksum_update_arch(struct shash_desc
*desc
, const u8
*data
,
95 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
97 ctx
->crc
= __crc32c_le(ctx
->crc
, data
, length
);
101 static int chksum_final(struct shash_desc
*desc
, u8
*out
)
103 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
105 put_unaligned_le32(~ctx
->crc
, out
);
109 static int __chksum_finup(u32
*crcp
, const u8
*data
, unsigned int len
, u8
*out
)
111 put_unaligned_le32(~__crc32c_le_base(*crcp
, data
, len
), out
);
115 static int __chksum_finup_arch(u32
*crcp
, const u8
*data
, unsigned int len
,
118 put_unaligned_le32(~__crc32c_le(*crcp
, data
, len
), out
);
122 static int chksum_finup(struct shash_desc
*desc
, const u8
*data
,
123 unsigned int len
, u8
*out
)
125 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
127 return __chksum_finup(&ctx
->crc
, data
, len
, out
);
130 static int chksum_finup_arch(struct shash_desc
*desc
, const u8
*data
,
131 unsigned int len
, u8
*out
)
133 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
135 return __chksum_finup_arch(&ctx
->crc
, data
, len
, out
);
138 static int chksum_digest(struct shash_desc
*desc
, const u8
*data
,
139 unsigned int length
, u8
*out
)
141 struct chksum_ctx
*mctx
= crypto_shash_ctx(desc
->tfm
);
143 return __chksum_finup(&mctx
->key
, data
, length
, out
);
146 static int chksum_digest_arch(struct shash_desc
*desc
, const u8
*data
,
147 unsigned int length
, u8
*out
)
149 struct chksum_ctx
*mctx
= crypto_shash_ctx(desc
->tfm
);
151 return __chksum_finup_arch(&mctx
->key
, data
, length
, out
);
154 static int crc32c_cra_init(struct crypto_tfm
*tfm
)
156 struct chksum_ctx
*mctx
= crypto_tfm_ctx(tfm
);
162 static struct shash_alg algs
[] = {{
163 .digestsize
= CHKSUM_DIGEST_SIZE
,
164 .setkey
= chksum_setkey
,
166 .update
= chksum_update
,
167 .final
= chksum_final
,
168 .finup
= chksum_finup
,
169 .digest
= chksum_digest
,
170 .descsize
= sizeof(struct chksum_desc_ctx
),
172 .base
.cra_name
= "crc32c",
173 .base
.cra_driver_name
= "crc32c-generic",
174 .base
.cra_priority
= 100,
175 .base
.cra_flags
= CRYPTO_ALG_OPTIONAL_KEY
,
176 .base
.cra_blocksize
= CHKSUM_BLOCK_SIZE
,
177 .base
.cra_ctxsize
= sizeof(struct chksum_ctx
),
178 .base
.cra_module
= THIS_MODULE
,
179 .base
.cra_init
= crc32c_cra_init
,
181 .digestsize
= CHKSUM_DIGEST_SIZE
,
182 .setkey
= chksum_setkey
,
184 .update
= chksum_update_arch
,
185 .final
= chksum_final
,
186 .finup
= chksum_finup_arch
,
187 .digest
= chksum_digest_arch
,
188 .descsize
= sizeof(struct chksum_desc_ctx
),
190 .base
.cra_name
= "crc32c",
191 .base
.cra_driver_name
= "crc32c-" __stringify(ARCH
),
192 .base
.cra_priority
= 150,
193 .base
.cra_flags
= CRYPTO_ALG_OPTIONAL_KEY
,
194 .base
.cra_blocksize
= CHKSUM_BLOCK_SIZE
,
195 .base
.cra_ctxsize
= sizeof(struct chksum_ctx
),
196 .base
.cra_module
= THIS_MODULE
,
197 .base
.cra_init
= crc32c_cra_init
,
200 static int __init
crc32c_mod_init(void)
202 /* register the arch flavor only if it differs from the generic one */
203 return crypto_register_shashes(algs
, 1 + (&__crc32c_le
!= &__crc32c_le_base
));
206 static void __exit
crc32c_mod_fini(void)
208 crypto_unregister_shashes(algs
, 1 + (&__crc32c_le
!= &__crc32c_le_base
));
211 subsys_initcall(crc32c_mod_init
);
212 module_exit(crc32c_mod_fini
);
214 MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
215 MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
216 MODULE_LICENSE("GPL");
217 MODULE_ALIAS_CRYPTO("crc32c");
218 MODULE_ALIAS_CRYPTO("crc32c-generic");