crc64-rocksoft.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/types.h>
  3. #include <linux/module.h>
  4. #include <linux/crc64.h>
  5. #include <linux/err.h>
  6. #include <linux/init.h>
  7. #include <crypto/hash.h>
  8. #include <crypto/algapi.h>
  9. #include <linux/static_key.h>
  10. #include <linux/notifier.h>
  11. static struct crypto_shash __rcu *crc64_rocksoft_tfm;
  12. static DEFINE_STATIC_KEY_TRUE(crc64_rocksoft_fallback);
  13. static DEFINE_MUTEX(crc64_rocksoft_mutex);
  14. static struct work_struct crc64_rocksoft_rehash_work;
  15. static int crc64_rocksoft_notify(struct notifier_block *self, unsigned long val, void *data)
  16. {
  17. struct crypto_alg *alg = data;
  18. if (val != CRYPTO_MSG_ALG_LOADED ||
  19. strcmp(alg->cra_name, CRC64_ROCKSOFT_STRING))
  20. return NOTIFY_DONE;
  21. schedule_work(&crc64_rocksoft_rehash_work);
  22. return NOTIFY_OK;
  23. }
  24. static void crc64_rocksoft_rehash(struct work_struct *work)
  25. {
  26. struct crypto_shash *new, *old;
  27. mutex_lock(&crc64_rocksoft_mutex);
  28. old = rcu_dereference_protected(crc64_rocksoft_tfm,
  29. lockdep_is_held(&crc64_rocksoft_mutex));
  30. new = crypto_alloc_shash(CRC64_ROCKSOFT_STRING, 0, 0);
  31. if (IS_ERR(new)) {
  32. mutex_unlock(&crc64_rocksoft_mutex);
  33. return;
  34. }
  35. rcu_assign_pointer(crc64_rocksoft_tfm, new);
  36. mutex_unlock(&crc64_rocksoft_mutex);
  37. if (old) {
  38. synchronize_rcu();
  39. crypto_free_shash(old);
  40. } else {
  41. static_branch_disable(&crc64_rocksoft_fallback);
  42. }
  43. }
  44. static struct notifier_block crc64_rocksoft_nb = {
  45. .notifier_call = crc64_rocksoft_notify,
  46. };
  47. u64 crc64_rocksoft_update(u64 crc, const unsigned char *buffer, size_t len)
  48. {
  49. struct {
  50. struct shash_desc shash;
  51. u64 crc;
  52. } desc;
  53. int err;
  54. if (static_branch_unlikely(&crc64_rocksoft_fallback))
  55. return crc64_rocksoft_generic(crc, buffer, len);
  56. rcu_read_lock();
  57. desc.shash.tfm = rcu_dereference(crc64_rocksoft_tfm);
  58. desc.crc = crc;
  59. err = crypto_shash_update(&desc.shash, buffer, len);
  60. rcu_read_unlock();
  61. BUG_ON(err);
  62. return desc.crc;
  63. }
  64. EXPORT_SYMBOL_GPL(crc64_rocksoft_update);
  65. u64 crc64_rocksoft(const unsigned char *buffer, size_t len)
  66. {
  67. return crc64_rocksoft_update(0, buffer, len);
  68. }
  69. EXPORT_SYMBOL_GPL(crc64_rocksoft);
  70. static int __init crc64_rocksoft_mod_init(void)
  71. {
  72. INIT_WORK(&crc64_rocksoft_rehash_work, crc64_rocksoft_rehash);
  73. crypto_register_notifier(&crc64_rocksoft_nb);
  74. crc64_rocksoft_rehash(&crc64_rocksoft_rehash_work);
  75. return 0;
  76. }
  77. static void __exit crc64_rocksoft_mod_fini(void)
  78. {
  79. crypto_unregister_notifier(&crc64_rocksoft_nb);
  80. cancel_work_sync(&crc64_rocksoft_rehash_work);
  81. crypto_free_shash(rcu_dereference_protected(crc64_rocksoft_tfm, 1));
  82. }
  83. module_init(crc64_rocksoft_mod_init);
  84. module_exit(crc64_rocksoft_mod_fini);
  85. static int crc64_rocksoft_transform_show(char *buffer, const struct kernel_param *kp)
  86. {
  87. struct crypto_shash *tfm;
  88. int len;
  89. if (static_branch_unlikely(&crc64_rocksoft_fallback))
  90. return sprintf(buffer, "fallback\n");
  91. rcu_read_lock();
  92. tfm = rcu_dereference(crc64_rocksoft_tfm);
  93. len = snprintf(buffer, PAGE_SIZE, "%s\n",
  94. crypto_shash_driver_name(tfm));
  95. rcu_read_unlock();
  96. return len;
  97. }
  98. module_param_call(transform, NULL, crc64_rocksoft_transform_show, NULL, 0444);
  99. MODULE_AUTHOR("Keith Busch <[email protected]>");
  100. MODULE_DESCRIPTION("Rocksoft model CRC64 calculation (library API)");
  101. MODULE_LICENSE("GPL");
  102. MODULE_SOFTDEP("pre: crc64");