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