2874c5fd28426 (Thomas Gleixner 2019-05-27 08:55:01 +0200 1) // SPDX-License-Identifier: GPL-2.0-or-later
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 2) /*
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 3) * CRC32C
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 4) *@Article{castagnoli-crc,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 5) * author = { Guy Castagnoli and Stefan Braeuer and Martin Herrman},
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 6) * title = {{Optimization of Cyclic Redundancy-Check Codes with 24
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 7) * and 32 Parity Bits}},
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 8) * journal = IEEE Transactions on Communication,
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 9) * year = {1993},
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 10) * volume = {41},
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 11) * number = {6},
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 12) * pages = {},
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 13) * month = {June},
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 14) *}
f1e594acb1bdf (Randy Dunlap 2020-10-15 20:10:48 -0700 15) * Used by the iSCSI driver, possibly others, and derived from
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 16) * the iscsi-crc.c module of the linux-iscsi driver at
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 17) * http://linux-iscsi.sourceforge.net.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 18) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 19) * Following the example of lib/crc32, this function is intended to be
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 20) * flexible and useful for all users. Modules that currently have their
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 21) * own crc32c, but hopefully may be able to use this one are:
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 22) * net/sctp (please add all your doco to here if you change to
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 23) * use this one!)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 24) * <endoflist>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 25) *
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 26) * Copyright (c) 2004 Cisco Systems, Inc.
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 27) */
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 28)
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 29) #include <crypto/hash.h>
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 30) #include <linux/err.h>
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 31) #include <linux/init.h>
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 32) #include <linux/kernel.h>
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 33) #include <linux/module.h>
290e0e0f2b54b (Jean Delvare 2016-01-20 14:58:06 -0800 34) #include <linux/crc32c.h>
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 35)
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 36) static struct crypto_shash *tfm;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 37)
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 38) u32 crc32c(u32 crc, const void *address, unsigned int length)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 39) {
ea0e0de69fc41 (Jan-Simon Möller 2012-07-02 12:54:28 +0200 40) SHASH_DESC_ON_STACK(shash, tfm);
d41519a69b35b (David Miller 2017-06-02 11:28:54 -0400 41) u32 ret, *ctx = (u32 *)shash_desc_ctx(shash);
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 42) int err;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 43)
ea0e0de69fc41 (Jan-Simon Möller 2012-07-02 12:54:28 +0200 44) shash->tfm = tfm;
ea0e0de69fc41 (Jan-Simon Möller 2012-07-02 12:54:28 +0200 45) *ctx = crc;
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 46)
ea0e0de69fc41 (Jan-Simon Möller 2012-07-02 12:54:28 +0200 47) err = crypto_shash_update(shash, address, length);
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 48) BUG_ON(err);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 49)
d41519a69b35b (David Miller 2017-06-02 11:28:54 -0400 50) ret = *ctx;
d41519a69b35b (David Miller 2017-06-02 11:28:54 -0400 51) barrier_data(ctx);
d41519a69b35b (David Miller 2017-06-02 11:28:54 -0400 52) return ret;
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 53) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 54)
53b146ae59826 (Adrian-Ken Rueegsegger 2008-11-11 12:14:00 +0800 55) EXPORT_SYMBOL(crc32c);
53b146ae59826 (Adrian-Ken Rueegsegger 2008-11-11 12:14:00 +0800 56)
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 57) static int __init libcrc32c_mod_init(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 58) {
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 59) tfm = crypto_alloc_shash("crc32c", 0, 0);
f8eaf298c8dc0 (Fabian Frederick 2014-06-04 16:11:51 -0700 60) return PTR_ERR_OR_ZERO(tfm);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 61) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 62)
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 63) static void __exit libcrc32c_mod_fini(void)
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 64) {
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 65) crypto_free_shash(tfm);
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 66) }
^1da177e4c3f4 (Linus Torvalds 2005-04-16 15:20:36 -0700 67)
df91f56adce1f (Nikolay Borisov 2018-01-08 11:45:04 +0200 68) const char *crc32c_impl(void)
df91f56adce1f (Nikolay Borisov 2018-01-08 11:45:04 +0200 69) {
df91f56adce1f (Nikolay Borisov 2018-01-08 11:45:04 +0200 70) return crypto_shash_driver_name(tfm);
df91f56adce1f (Nikolay Borisov 2018-01-08 11:45:04 +0200 71) }
df91f56adce1f (Nikolay Borisov 2018-01-08 11:45:04 +0200 72) EXPORT_SYMBOL(crc32c_impl);
df91f56adce1f (Nikolay Borisov 2018-01-08 11:45:04 +0200 73)
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 74) module_init(libcrc32c_mod_init);
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 75) module_exit(libcrc32c_mod_fini);
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 76)
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 77) MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 78) MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations");
69c35efcf1576 (Herbert Xu 2008-11-07 15:11:47 +0800 79) MODULE_LICENSE("GPL");
fd7f6727102a1 (Jean Delvare 2016-01-18 17:06:05 +0100 80) MODULE_SOFTDEP("pre: crc32c");