crc-t10dif.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <crypto/algapi.h>
  17. #include <linux/static_key.h>
  18. #include <linux/notifier.h>
  19. static struct crypto_shash __rcu *crct10dif_tfm;
  20. static struct static_key crct10dif_fallback __read_mostly;
  21. DEFINE_MUTEX(crc_t10dif_mutex);
  22. static int crc_t10dif_rehash(struct notifier_block *self, unsigned long val, void *data)
  23. {
  24. struct crypto_alg *alg = data;
  25. struct crypto_shash *new, *old;
  26. if (val != CRYPTO_MSG_ALG_LOADED ||
  27. static_key_false(&crct10dif_fallback) ||
  28. strncmp(alg->cra_name, CRC_T10DIF_STRING, strlen(CRC_T10DIF_STRING)))
  29. return 0;
  30. mutex_lock(&crc_t10dif_mutex);
  31. old = rcu_dereference_protected(crct10dif_tfm,
  32. lockdep_is_held(&crc_t10dif_mutex));
  33. if (!old) {
  34. mutex_unlock(&crc_t10dif_mutex);
  35. return 0;
  36. }
  37. new = crypto_alloc_shash("crct10dif", 0, 0);
  38. if (IS_ERR(new)) {
  39. mutex_unlock(&crc_t10dif_mutex);
  40. return 0;
  41. }
  42. rcu_assign_pointer(crct10dif_tfm, new);
  43. mutex_unlock(&crc_t10dif_mutex);
  44. synchronize_rcu();
  45. crypto_free_shash(old);
  46. return 0;
  47. }
  48. static struct notifier_block crc_t10dif_nb = {
  49. .notifier_call = crc_t10dif_rehash,
  50. };
  51. __u16 crc_t10dif_update(__u16 crc, const unsigned char *buffer, size_t len)
  52. {
  53. struct {
  54. struct shash_desc shash;
  55. char ctx[2];
  56. } desc;
  57. int err;
  58. if (static_key_false(&crct10dif_fallback))
  59. return crc_t10dif_generic(crc, buffer, len);
  60. rcu_read_lock();
  61. desc.shash.tfm = rcu_dereference(crct10dif_tfm);
  62. desc.shash.flags = 0;
  63. *(__u16 *)desc.ctx = crc;
  64. err = crypto_shash_update(&desc.shash, buffer, len);
  65. rcu_read_unlock();
  66. BUG_ON(err);
  67. return *(__u16 *)desc.ctx;
  68. }
  69. EXPORT_SYMBOL(crc_t10dif_update);
  70. __u16 crc_t10dif(const unsigned char *buffer, size_t len)
  71. {
  72. return crc_t10dif_update(0, buffer, len);
  73. }
  74. EXPORT_SYMBOL(crc_t10dif);
  75. static int __init crc_t10dif_mod_init(void)
  76. {
  77. crypto_register_notifier(&crc_t10dif_nb);
  78. crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0);
  79. if (IS_ERR(crct10dif_tfm)) {
  80. static_key_slow_inc(&crct10dif_fallback);
  81. crct10dif_tfm = NULL;
  82. }
  83. return 0;
  84. }
  85. static void __exit crc_t10dif_mod_fini(void)
  86. {
  87. crypto_unregister_notifier(&crc_t10dif_nb);
  88. crypto_free_shash(crct10dif_tfm);
  89. }
  90. module_init(crc_t10dif_mod_init);
  91. module_exit(crc_t10dif_mod_fini);
  92. static int crc_t10dif_transform_show(char *buffer, const struct kernel_param *kp)
  93. {
  94. if (static_key_false(&crct10dif_fallback))
  95. return sprintf(buffer, "fallback\n");
  96. return sprintf(buffer, "%s\n",
  97. crypto_tfm_alg_driver_name(crypto_shash_tfm(crct10dif_tfm)));
  98. }
  99. module_param_call(transform, NULL, crc_t10dif_transform_show, NULL, 0644);
  100. MODULE_DESCRIPTION("T10 DIF CRC calculation");
  101. MODULE_LICENSE("GPL");
  102. MODULE_SOFTDEP("pre: crct10dif");