Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 
0003 #include <linux/types.h>
0004 #include <linux/module.h>
0005 #include <linux/crc64.h>
0006 #include <linux/err.h>
0007 #include <linux/init.h>
0008 #include <crypto/hash.h>
0009 #include <crypto/algapi.h>
0010 #include <linux/static_key.h>
0011 #include <linux/notifier.h>
0012 
0013 static struct crypto_shash __rcu *crc64_rocksoft_tfm;
0014 static DEFINE_STATIC_KEY_TRUE(crc64_rocksoft_fallback);
0015 static DEFINE_MUTEX(crc64_rocksoft_mutex);
0016 static struct work_struct crc64_rocksoft_rehash_work;
0017 
0018 static int crc64_rocksoft_notify(struct notifier_block *self, unsigned long val, void *data)
0019 {
0020     struct crypto_alg *alg = data;
0021 
0022     if (val != CRYPTO_MSG_ALG_LOADED ||
0023         strcmp(alg->cra_name, CRC64_ROCKSOFT_STRING))
0024         return NOTIFY_DONE;
0025 
0026     schedule_work(&crc64_rocksoft_rehash_work);
0027     return NOTIFY_OK;
0028 }
0029 
0030 static void crc64_rocksoft_rehash(struct work_struct *work)
0031 {
0032     struct crypto_shash *new, *old;
0033 
0034     mutex_lock(&crc64_rocksoft_mutex);
0035     old = rcu_dereference_protected(crc64_rocksoft_tfm,
0036                     lockdep_is_held(&crc64_rocksoft_mutex));
0037     new = crypto_alloc_shash(CRC64_ROCKSOFT_STRING, 0, 0);
0038     if (IS_ERR(new)) {
0039         mutex_unlock(&crc64_rocksoft_mutex);
0040         return;
0041     }
0042     rcu_assign_pointer(crc64_rocksoft_tfm, new);
0043     mutex_unlock(&crc64_rocksoft_mutex);
0044 
0045     if (old) {
0046         synchronize_rcu();
0047         crypto_free_shash(old);
0048     } else {
0049         static_branch_disable(&crc64_rocksoft_fallback);
0050     }
0051 }
0052 
0053 static struct notifier_block crc64_rocksoft_nb = {
0054     .notifier_call = crc64_rocksoft_notify,
0055 };
0056 
0057 u64 crc64_rocksoft_update(u64 crc, const unsigned char *buffer, size_t len)
0058 {
0059     struct {
0060         struct shash_desc shash;
0061         u64 crc;
0062     } desc;
0063     int err;
0064 
0065     if (static_branch_unlikely(&crc64_rocksoft_fallback))
0066         return crc64_rocksoft_generic(crc, buffer, len);
0067 
0068     rcu_read_lock();
0069     desc.shash.tfm = rcu_dereference(crc64_rocksoft_tfm);
0070     desc.crc = crc;
0071     err = crypto_shash_update(&desc.shash, buffer, len);
0072     rcu_read_unlock();
0073 
0074     BUG_ON(err);
0075 
0076     return desc.crc;
0077 }
0078 EXPORT_SYMBOL_GPL(crc64_rocksoft_update);
0079 
0080 u64 crc64_rocksoft(const unsigned char *buffer, size_t len)
0081 {
0082     return crc64_rocksoft_update(0, buffer, len);
0083 }
0084 EXPORT_SYMBOL_GPL(crc64_rocksoft);
0085 
0086 static int __init crc64_rocksoft_mod_init(void)
0087 {
0088     INIT_WORK(&crc64_rocksoft_rehash_work, crc64_rocksoft_rehash);
0089     crypto_register_notifier(&crc64_rocksoft_nb);
0090     crc64_rocksoft_rehash(&crc64_rocksoft_rehash_work);
0091     return 0;
0092 }
0093 
0094 static void __exit crc64_rocksoft_mod_fini(void)
0095 {
0096     crypto_unregister_notifier(&crc64_rocksoft_nb);
0097     cancel_work_sync(&crc64_rocksoft_rehash_work);
0098     crypto_free_shash(rcu_dereference_protected(crc64_rocksoft_tfm, 1));
0099 }
0100 
0101 module_init(crc64_rocksoft_mod_init);
0102 module_exit(crc64_rocksoft_mod_fini);
0103 
0104 static int crc64_rocksoft_transform_show(char *buffer, const struct kernel_param *kp)
0105 {
0106     struct crypto_shash *tfm;
0107     int len;
0108 
0109     if (static_branch_unlikely(&crc64_rocksoft_fallback))
0110         return sprintf(buffer, "fallback\n");
0111 
0112     rcu_read_lock();
0113     tfm = rcu_dereference(crc64_rocksoft_tfm);
0114     len = snprintf(buffer, PAGE_SIZE, "%s\n",
0115                crypto_shash_driver_name(tfm));
0116     rcu_read_unlock();
0117 
0118     return len;
0119 }
0120 
0121 module_param_call(transform, NULL, crc64_rocksoft_transform_show, NULL, 0444);
0122 
0123 MODULE_AUTHOR("Keith Busch <kbusch@kernel.org>");
0124 MODULE_DESCRIPTION("Rocksoft model CRC64 calculation (library API)");
0125 MODULE_LICENSE("GPL");
0126 MODULE_SOFTDEP("pre: crc64");