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