blob: 77ab839644c553d67ad71758d9297807f9ad3320 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * CRC32C
4 *@Article{castagnoli-crc,
5 * author = { Guy Castagnoli and Stefan Braeuer and Martin Herrman},
6 * title = {{Optimization of Cyclic Redundancy-Check Codes with 24
7 * and 32 Parity Bits}},
8 * journal = IEEE Transactions on Communication,
9 * year = {1993},
10 * volume = {41},
11 * number = {6},
12 * pages = {},
13 * month = {June},
14 *}
15 * Used by the iSCSI driver, possibly others, and derived from the
16 * the iscsi-crc.c module of the linux-iscsi driver at
17 * http://linux-iscsi.sourceforge.net.
18 *
19 * Following the example of lib/crc32, this function is intended to be
20 * flexible and useful for all users. Modules that currently have their
21 * own crc32c, but hopefully may be able to use this one are:
22 * net/sctp (please add all your doco to here if you change to
23 * use this one!)
24 * <endoflist>
25 *
26 * Copyright (c) 2004 Cisco Systems, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
Herbert Xu69c35ef2008-11-07 15:11:47 +080028
29#include <crypto/hash.h>
30#include <linux/err.h>
31#include <linux/init.h>
32#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/module.h>
Jean Delvare290e0e02016-01-20 14:58:06 -080034#include <linux/crc32c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Herbert Xu69c35ef2008-11-07 15:11:47 +080036static struct crypto_shash *tfm;
37
38u32 crc32c(u32 crc, const void *address, unsigned int length)
39{
Jan-Simon Möllerea0e0de2012-07-02 12:54:28 +020040 SHASH_DESC_ON_STACK(shash, tfm);
David Millerd41519a2017-06-02 11:28:54 -040041 u32 ret, *ctx = (u32 *)shash_desc_ctx(shash);
Herbert Xu69c35ef2008-11-07 15:11:47 +080042 int err;
43
Jan-Simon Möllerea0e0de2012-07-02 12:54:28 +020044 shash->tfm = tfm;
Jan-Simon Möllerea0e0de2012-07-02 12:54:28 +020045 *ctx = crc;
Herbert Xu69c35ef2008-11-07 15:11:47 +080046
Jan-Simon Möllerea0e0de2012-07-02 12:54:28 +020047 err = crypto_shash_update(shash, address, length);
Herbert Xu69c35ef2008-11-07 15:11:47 +080048 BUG_ON(err);
49
David Millerd41519a2017-06-02 11:28:54 -040050 ret = *ctx;
51 barrier_data(ctx);
52 return ret;
Herbert Xu69c35ef2008-11-07 15:11:47 +080053}
54
Adrian-Ken Rueegsegger53b146a2008-11-11 12:14:00 +080055EXPORT_SYMBOL(crc32c);
56
Herbert Xu69c35ef2008-11-07 15:11:47 +080057static int __init libcrc32c_mod_init(void)
58{
59 tfm = crypto_alloc_shash("crc32c", 0, 0);
Fabian Frederickf8eaf292014-06-04 16:11:51 -070060 return PTR_ERR_OR_ZERO(tfm);
Herbert Xu69c35ef2008-11-07 15:11:47 +080061}
62
63static void __exit libcrc32c_mod_fini(void)
64{
65 crypto_free_shash(tfm);
66}
67
Nikolay Borisovdf91f562018-01-08 11:45:04 +020068const char *crc32c_impl(void)
69{
70 return crypto_shash_driver_name(tfm);
71}
72EXPORT_SYMBOL(crc32c_impl);
73
Herbert Xu69c35ef2008-11-07 15:11:47 +080074module_init(libcrc32c_mod_init);
75module_exit(libcrc32c_mod_fini);
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
78MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations");
79MODULE_LICENSE("GPL");
Jean Delvarefd7f6722016-01-18 17:06:05 +010080MODULE_SOFTDEP("pre: crc32c");