crc-t10dif.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * T10 Data Integrity Field CRC16 calculation
  3. *
  4. * Copyright (c) 2007 Oracle Corporation. All rights reserved.
  5. * Written by Martin K. Petersen <martin.petersen@oracle.com>
  6. *
  7. * This source code is licensed under the GNU General Public License,
  8. * Version 2. See the file COPYING for more details.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/module.h>
  12. #include <linux/crc-t10dif.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <crypto/hash.h>
  16. #include <linux/static_key.h>
  17. static struct crypto_shash *crct10dif_tfm;
  18. static struct static_key crct10dif_fallback __read_mostly;
  19. __u16 crc_t10dif_update(__u16 crc, const unsigned char *buffer, size_t len)
  20. {
  21. struct {
  22. struct shash_desc shash;
  23. char ctx[2];
  24. } desc;
  25. int err;
  26. if (static_key_false(&crct10dif_fallback))
  27. return crc_t10dif_generic(crc, buffer, len);
  28. desc.shash.tfm = crct10dif_tfm;
  29. desc.shash.flags = 0;
  30. *(__u16 *)desc.ctx = crc;
  31. err = crypto_shash_update(&desc.shash, buffer, len);
  32. BUG_ON(err);
  33. return *(__u16 *)desc.ctx;
  34. }
  35. EXPORT_SYMBOL(crc_t10dif_update);
  36. __u16 crc_t10dif(const unsigned char *buffer, size_t len)
  37. {
  38. return crc_t10dif_update(0, buffer, len);
  39. }
  40. EXPORT_SYMBOL(crc_t10dif);
  41. static int __init crc_t10dif_mod_init(void)
  42. {
  43. crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0);
  44. if (IS_ERR(crct10dif_tfm)) {
  45. static_key_slow_inc(&crct10dif_fallback);
  46. crct10dif_tfm = NULL;
  47. }
  48. return 0;
  49. }
  50. static void __exit crc_t10dif_mod_fini(void)
  51. {
  52. crypto_free_shash(crct10dif_tfm);
  53. }
  54. module_init(crc_t10dif_mod_init);
  55. module_exit(crc_t10dif_mod_fini);
  56. MODULE_DESCRIPTION("T10 DIF CRC calculation");
  57. MODULE_LICENSE("GPL");
  58. MODULE_SOFTDEP("pre: crct10dif");