6 *@Article{castagnoli-crc,
7 * author = { Guy Castagnoli and Stefan Braeuer and Martin Herrman},
8 * title = {{Optimization of Cyclic Redundancy-Check Codes with 24
9 * and 32 Parity Bits}},
10 * journal = IEEE Transactions on Communication,
17 * Used by the iSCSI driver, possibly others, and derived from the
18 * the iscsi-crc.c module of the linux-iscsi driver at
19 * http://linux-iscsi.sourceforge.net.
21 * Following the example of lib/crc32, this function is intended to be
22 * flexible and useful for all users. Modules that currently have their
23 * own crc32c, but hopefully may be able to use this one are:
24 * net/sctp (please add all your doco to here if you change to
28 * Copyright (c) 2004 Cisco Systems, Inc.
29 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
31 * This program is free software; you can redistribute it and/or modify it
32 * under the terms of the GNU General Public License as published by the Free
33 * Software Foundation; either version 2 of the License, or (at your option)
38 #include <crypto/internal/hash.h>
39 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/string.h>
42 #include <linux/kernel.h>
43 #include <linux/crc32.h>
45 #define CHKSUM_BLOCK_SIZE 1
46 #define CHKSUM_DIGEST_SIZE 4
52 struct chksum_desc_ctx
{
57 * Steps through buffer one byte at at time, calculates reflected
61 static int chksum_init(struct shash_desc
*desc
)
63 struct chksum_ctx
*mctx
= crypto_shash_ctx(desc
->tfm
);
64 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
72 * Setting the seed allows arbitrary accumulators and flexible XOR policy
73 * If your algorithm starts with ~0, then XOR with ~0 before you set
76 static int chksum_setkey(struct crypto_shash
*tfm
, const u8
*key
,
79 struct chksum_ctx
*mctx
= crypto_shash_ctx(tfm
);
81 if (keylen
!= sizeof(mctx
->key
)) {
82 crypto_shash_set_flags(tfm
, CRYPTO_TFM_RES_BAD_KEY_LEN
);
85 mctx
->key
= le32_to_cpu(*(__le32
*)key
);
89 static int chksum_update(struct shash_desc
*desc
, const u8
*data
,
92 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
94 ctx
->crc
= __crc32c_le(ctx
->crc
, data
, length
);
98 static int chksum_final(struct shash_desc
*desc
, u8
*out
)
100 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
102 *(__le32
*)out
= ~cpu_to_le32p(&ctx
->crc
);
106 static int __chksum_finup(u32
*crcp
, const u8
*data
, unsigned int len
, u8
*out
)
108 *(__le32
*)out
= ~cpu_to_le32(__crc32c_le(*crcp
, data
, len
));
112 static int chksum_finup(struct shash_desc
*desc
, const u8
*data
,
113 unsigned int len
, u8
*out
)
115 struct chksum_desc_ctx
*ctx
= shash_desc_ctx(desc
);
117 return __chksum_finup(&ctx
->crc
, data
, len
, out
);
120 static int chksum_digest(struct shash_desc
*desc
, const u8
*data
,
121 unsigned int length
, u8
*out
)
123 struct chksum_ctx
*mctx
= crypto_shash_ctx(desc
->tfm
);
125 return __chksum_finup(&mctx
->key
, data
, length
, out
);
128 static int crc32c_cra_init(struct crypto_tfm
*tfm
)
130 struct chksum_ctx
*mctx
= crypto_tfm_ctx(tfm
);
136 static struct shash_alg alg
= {
137 .digestsize
= CHKSUM_DIGEST_SIZE
,
138 .setkey
= chksum_setkey
,
140 .update
= chksum_update
,
141 .final
= chksum_final
,
142 .finup
= chksum_finup
,
143 .digest
= chksum_digest
,
144 .descsize
= sizeof(struct chksum_desc_ctx
),
146 .cra_name
= "crc32c",
147 .cra_driver_name
= "crc32c-generic",
149 .cra_blocksize
= CHKSUM_BLOCK_SIZE
,
151 .cra_ctxsize
= sizeof(struct chksum_ctx
),
152 .cra_module
= THIS_MODULE
,
153 .cra_init
= crc32c_cra_init
,
157 static int __init
crc32c_mod_init(void)
159 return crypto_register_shash(&alg
);
162 static void __exit
crc32c_mod_fini(void)
164 crypto_unregister_shash(&alg
);
167 module_init(crc32c_mod_init
);
168 module_exit(crc32c_mod_fini
);
170 MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
171 MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations wrapper for lib/crc32c");
172 MODULE_LICENSE("GPL");
173 MODULE_ALIAS("crc32c");
174 MODULE_SOFTDEP("pre: crc32c");