crc-t10dif.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. return PTR_RET(crct10dif_tfm);
  36. }
  37. static void __exit crc_t10dif_mod_fini(void)
  38. {
  39. crypto_free_shash(crct10dif_tfm);
  40. }
  41. module_init(crc_t10dif_mod_init);
  42. module_exit(crc_t10dif_mod_fini);
  43. MODULE_DESCRIPTION("T10 DIF CRC calculation");
  44. MODULE_LICENSE("GPL");