| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Xu | 69c35ef | 2008-11-07 15:11:47 +0800 | [diff] [blame] | 33 |  | 
|  | 34 | #include <crypto/hash.h> | 
|  | 35 | #include <linux/err.h> | 
|  | 36 | #include <linux/init.h> | 
|  | 37 | #include <linux/kernel.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/module.h> | 
| Jean Delvare | 290e0e0 | 2016-01-20 14:58:06 -0800 | [diff] [blame] | 39 | #include <linux/crc32c.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 |  | 
| Herbert Xu | 69c35ef | 2008-11-07 15:11:47 +0800 | [diff] [blame] | 41 | static struct crypto_shash *tfm; | 
|  | 42 |  | 
|  | 43 | u32 crc32c(u32 crc, const void *address, unsigned int length) | 
|  | 44 | { | 
| Jan-Simon Möller | ea0e0de | 2012-07-02 12:54:28 +0200 | [diff] [blame] | 45 | SHASH_DESC_ON_STACK(shash, tfm); | 
|  | 46 | u32 *ctx = (u32 *)shash_desc_ctx(shash); | 
| Herbert Xu | 69c35ef | 2008-11-07 15:11:47 +0800 | [diff] [blame] | 47 | int err; | 
|  | 48 |  | 
| Jan-Simon Möller | ea0e0de | 2012-07-02 12:54:28 +0200 | [diff] [blame] | 49 | shash->tfm = tfm; | 
|  | 50 | shash->flags = 0; | 
|  | 51 | *ctx = crc; | 
| Herbert Xu | 69c35ef | 2008-11-07 15:11:47 +0800 | [diff] [blame] | 52 |  | 
| Jan-Simon Möller | ea0e0de | 2012-07-02 12:54:28 +0200 | [diff] [blame] | 53 | err = crypto_shash_update(shash, address, length); | 
| Herbert Xu | 69c35ef | 2008-11-07 15:11:47 +0800 | [diff] [blame] | 54 | BUG_ON(err); | 
|  | 55 |  | 
| Jan-Simon Möller | ea0e0de | 2012-07-02 12:54:28 +0200 | [diff] [blame] | 56 | return *ctx; | 
| Herbert Xu | 69c35ef | 2008-11-07 15:11:47 +0800 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
| Adrian-Ken Rueegsegger | 53b146a | 2008-11-11 12:14:00 +0800 | [diff] [blame] | 59 | EXPORT_SYMBOL(crc32c); | 
|  | 60 |  | 
| Herbert Xu | 69c35ef | 2008-11-07 15:11:47 +0800 | [diff] [blame] | 61 | static int __init libcrc32c_mod_init(void) | 
|  | 62 | { | 
|  | 63 | tfm = crypto_alloc_shash("crc32c", 0, 0); | 
| Fabian Frederick | f8eaf29 | 2014-06-04 16:11:51 -0700 | [diff] [blame] | 64 | return PTR_ERR_OR_ZERO(tfm); | 
| Herbert Xu | 69c35ef | 2008-11-07 15:11:47 +0800 | [diff] [blame] | 65 | } | 
|  | 66 |  | 
|  | 67 | static void __exit libcrc32c_mod_fini(void) | 
|  | 68 | { | 
|  | 69 | crypto_free_shash(tfm); | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | module_init(libcrc32c_mod_init); | 
|  | 73 | module_exit(libcrc32c_mod_fini); | 
|  | 74 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>"); | 
|  | 76 | MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations"); | 
|  | 77 | MODULE_LICENSE("GPL"); | 
| Jean Delvare | fd7f672 | 2016-01-18 17:06:05 +0100 | [diff] [blame] | 78 | MODULE_SOFTDEP("pre: crc32c"); |