libcrc32c.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. */
  33. #include <crypto/hash.h>
  34. #include <linux/err.h>
  35. #include <linux/init.h>
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. static struct crypto_shash *tfm;
  39. u32 crc32c(u32 crc, const void *address, unsigned int length)
  40. {
  41. struct {
  42. struct shash_desc shash;
  43. char ctx[crypto_shash_descsize(tfm)];
  44. } desc;
  45. int err;
  46. desc.shash.tfm = tfm;
  47. desc.shash.flags = 0;
  48. *(u32 *)desc.ctx = crc;
  49. err = crypto_shash_update(&desc.shash, address, length);
  50. BUG_ON(err);
  51. return *(u32 *)desc.ctx;
  52. }
  53. EXPORT_SYMBOL(crc32c);
  54. static int __init libcrc32c_mod_init(void)
  55. {
  56. tfm = crypto_alloc_shash("crc32c", 0, 0);
  57. if (IS_ERR(tfm))
  58. return PTR_ERR(tfm);
  59. return 0;
  60. }
  61. static void __exit libcrc32c_mod_fini(void)
  62. {
  63. crypto_free_shash(tfm);
  64. }
  65. module_init(libcrc32c_mod_init);
  66. module_exit(libcrc32c_mod_fini);
  67. MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
  68. MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations");
  69. MODULE_LICENSE("GPL");