Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* 
0003  * CRC32C
0004  *@Article{castagnoli-crc,
0005  * author =       { Guy Castagnoli and Stefan Braeuer and Martin Herrman},
0006  * title =        {{Optimization of Cyclic Redundancy-Check Codes with 24
0007  *                 and 32 Parity Bits}},
0008  * journal =      IEEE Transactions on Communication,
0009  * year =         {1993},
0010  * volume =       {41},
0011  * number =       {6},
0012  * pages =        {},
0013  * month =        {June},
0014  *}
0015  * Used by the iSCSI driver, possibly others, and derived from
0016  * the iscsi-crc.c module of the linux-iscsi driver at
0017  * http://linux-iscsi.sourceforge.net.
0018  *
0019  * Following the example of lib/crc32, this function is intended to be
0020  * flexible and useful for all users.  Modules that currently have their
0021  * own crc32c, but hopefully may be able to use this one are:
0022  *  net/sctp (please add all your doco to here if you change to
0023  *            use this one!)
0024  *  <endoflist>
0025  *
0026  * Copyright (c) 2004 Cisco Systems, Inc.
0027  */
0028 
0029 #include <crypto/hash.h>
0030 #include <linux/err.h>
0031 #include <linux/init.h>
0032 #include <linux/kernel.h>
0033 #include <linux/module.h>
0034 #include <linux/crc32c.h>
0035 
0036 static struct crypto_shash *tfm;
0037 
0038 u32 crc32c(u32 crc, const void *address, unsigned int length)
0039 {
0040     SHASH_DESC_ON_STACK(shash, tfm);
0041     u32 ret, *ctx = (u32 *)shash_desc_ctx(shash);
0042     int err;
0043 
0044     shash->tfm = tfm;
0045     *ctx = crc;
0046 
0047     err = crypto_shash_update(shash, address, length);
0048     BUG_ON(err);
0049 
0050     ret = *ctx;
0051     barrier_data(ctx);
0052     return ret;
0053 }
0054 
0055 EXPORT_SYMBOL(crc32c);
0056 
0057 static int __init libcrc32c_mod_init(void)
0058 {
0059     tfm = crypto_alloc_shash("crc32c", 0, 0);
0060     return PTR_ERR_OR_ZERO(tfm);
0061 }
0062 
0063 static void __exit libcrc32c_mod_fini(void)
0064 {
0065     crypto_free_shash(tfm);
0066 }
0067 
0068 const char *crc32c_impl(void)
0069 {
0070     return crypto_shash_driver_name(tfm);
0071 }
0072 EXPORT_SYMBOL(crc32c_impl);
0073 
0074 module_init(libcrc32c_mod_init);
0075 module_exit(libcrc32c_mod_fini);
0076 
0077 MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
0078 MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations");
0079 MODULE_LICENSE("GPL");
0080 MODULE_SOFTDEP("pre: crc32c");