libcrc32c.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  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
  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.
  27. */
  28. #include <crypto/hash.h>
  29. #include <linux/err.h>
  30. #include <linux/init.h>
  31. #include <linux/kernel.h>
  32. #include <linux/module.h>
  33. #include <linux/crc32c.h>
  34. static struct crypto_shash *tfm;
  35. u32 crc32c(u32 crc, const void *address, unsigned int length)
  36. {
  37. SHASH_DESC_ON_STACK(shash, tfm);
  38. u32 ret, *ctx = (u32 *)shash_desc_ctx(shash);
  39. int err;
  40. shash->tfm = tfm;
  41. *ctx = crc;
  42. err = crypto_shash_update(shash, address, length);
  43. BUG_ON(err);
  44. ret = *ctx;
  45. barrier_data(ctx);
  46. return ret;
  47. }
  48. EXPORT_SYMBOL(crc32c);
  49. static int __init libcrc32c_mod_init(void)
  50. {
  51. tfm = crypto_alloc_shash("crc32c", 0, 0);
  52. return PTR_ERR_OR_ZERO(tfm);
  53. }
  54. static void __exit libcrc32c_mod_fini(void)
  55. {
  56. crypto_free_shash(tfm);
  57. }
  58. const char *crc32c_impl(void)
  59. {
  60. return crypto_shash_driver_name(tfm);
  61. }
  62. EXPORT_SYMBOL(crc32c_impl);
  63. module_init(libcrc32c_mod_init);
  64. module_exit(libcrc32c_mod_fini);
  65. MODULE_AUTHOR("Clay Haapala <chaapala@cisco.com>");
  66. MODULE_DESCRIPTION("CRC32c (Castagnoli) calculations");
  67. MODULE_LICENSE("GPL");
  68. MODULE_SOFTDEP("pre: crc32c");