crc-t10dif.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. static struct crypto_shash *crct10dif_tfm;
  17. __u16 crc_t10dif(const unsigned char *buffer, size_t len)
  18. {
  19. struct {
  20. struct shash_desc shash;
  21. char ctx[2];
  22. } desc;
  23. int err;
  24. desc.shash.tfm = crct10dif_tfm;
  25. desc.shash.flags = 0;
  26. *(__u16 *)desc.ctx = 0;
  27. err = crypto_shash_update(&desc.shash, buffer, len);
  28. BUG_ON(err);
  29. return *(__u16 *)desc.ctx;
  30. }
  31. EXPORT_SYMBOL(crc_t10dif);
  32. static int __init crc_t10dif_mod_init(void)
  33. {
  34. crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0);
  35. if (IS_ERR(crct10dif_tfm))
  36. return PTR_ERR(crct10dif_tfm);
  37. return 0;
  38. }
  39. static void __exit crc_t10dif_mod_fini(void)
  40. {
  41. crypto_free_shash(crct10dif_tfm);
  42. }
  43. module_init(crc_t10dif_mod_init);
  44. module_exit(crc_t10dif_mod_fini);
  45. MODULE_DESCRIPTION("T10 DIF CRC calculation");
  46. MODULE_LICENSE("GPL");