blob: 6a08ce7d6adc054966658d9b6de0dfb36e2297e3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * CRC32C
3 *@Article{castagnoli-crc,
4 * author = { Guy Castagnoli and Stefan Braeuer and Martin Herrman},
5 * title = {{Optimization of Cyclic Redundancy-Check Codes with 24
6 * and 32 Parity Bits}},
7 * journal = IEEE Transactions on Communication,
8 * year = {1993},
9 * volume = {41},
10 * number = {6},
11 * pages = {},
12 * month = {June},
13 *}
14 * Used by the iSCSI driver, possibly others, and derived from the
15 * the iscsi-crc.c module of the linux-iscsi driver at
16 * http://linux-iscsi.sourceforge.net.
17 *
18 * Following the example of lib/crc32, this function is intended to be
19 * flexible and useful for all users. Modules that currently have their
20 * own crc32c, but hopefully may be able to use this one are:
21 * net/sctp (please add all your doco to here if you change to
22 * use this one!)
23 * <endoflist>
24 *
25 * Copyright (c) 2004 Cisco Systems, Inc.
26 *
27 * This program is free software; you can redistribute it and/or modify it
28 * under the terms of the GNU General Public License as published by the Free
29 * Software Foundation; either version 2 of the License, or (at your option)
30 * any later version.
31 *
32 */
Herbert Xu69c35ef2008-11-07 15:11:47 +080033
34#include <crypto/hash.h>
35#include <linux/err.h>
36#include <linux/init.h>
37#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Herbert Xu69c35ef2008-11-07 15:11:47 +080040static struct crypto_shash *tfm;
41
42u32 crc32c(u32 crc, const void *address, unsigned int length)
43{
Jan-Simon Möllerea0e0de2012-07-02 12:54:28 +020044 SHASH_DESC_ON_STACK(shash, tfm);
45 u32 *ctx = (u32 *)shash_desc_ctx(shash);
Herbert Xu69c35ef2008-11-07 15:11:47 +080046 int err;
47
Jan-Simon Möllerea0e0de2012-07-02 12:54:28 +020048 shash->tfm = tfm;
49 shash->flags = 0;
50 *ctx = crc;
Herbert Xu69c35ef2008-11-07 15:11:47 +080051
Jan-Simon Möllerea0e0de2012-07-02 12:54:28 +020052 err = crypto_shash_update(shash, address, length);
Herbert Xu69c35ef2008-11-07 15:11:47 +080053 BUG_ON(err);
54
Jan-Simon Möllerea0e0de2012-07-02 12:54:28 +020055 return *ctx;
Herbert Xu69c35ef2008-11-07 15:11:47 +080056}
57
Adrian-Ken Rueegsegger53b146a2008-11-11 12:14:00 +080058EXPORT_SYMBOL(crc32c);
59
Herbert Xu69c35ef2008-11-07 15:11:47 +080060static int __init libcrc32c_mod_init(void)
61{
62 tfm = crypto_alloc_shash("crc32c", 0, 0);
Fabian Frederickf8eaf292014-06-04 16:11:51 -070063 return PTR_ERR_OR_ZERO(tfm);
Herbert Xu69c35ef2008-11-07 15:11:47 +080064}
65
66static void __exit libcrc32c_mod_fini(void)
67{
68 crypto_free_shash(tfm);
69}
70
71module_init(libcrc32c_mod_init);
72module_exit(libcrc32c_mod_fini);
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
75MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations");
76MODULE_LICENSE("GPL");