1 /*
2  * Create default crypto algorithm instances.
3  *
4  * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  */
12 
13 #include <crypto/internal/aead.h>
14 #include <linux/completion.h>
15 #include <linux/ctype.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/kthread.h>
19 #include <linux/module.h>
20 #include <linux/notifier.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/sched/signal.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 
26 #include "internal.h"
27 
28 struct cryptomgr_param {
29 	struct rtattr *tb[CRYPTO_MAX_ATTRS + 2];
30 
31 	struct {
32 		struct rtattr attr;
33 		struct crypto_attr_type data;
34 	} type;
35 
36 	union {
37 		struct rtattr attr;
38 		struct {
39 			struct rtattr attr;
40 			struct crypto_attr_alg data;
41 		} alg;
42 		struct {
43 			struct rtattr attr;
44 			struct crypto_attr_u32 data;
45 		} nu32;
46 	} attrs[CRYPTO_MAX_ATTRS];
47 
48 	char template[CRYPTO_MAX_ALG_NAME];
49 
50 	struct crypto_larval *larval;
51 
52 	u32 otype;
53 	u32 omask;
54 };
55 
56 struct crypto_test_param {
57 	char driver[CRYPTO_MAX_ALG_NAME];
58 	char alg[CRYPTO_MAX_ALG_NAME];
59 	u32 type;
60 };
61 
cryptomgr_probe(void * data)62 static int cryptomgr_probe(void *data)
63 {
64 	struct cryptomgr_param *param = data;
65 	struct crypto_template *tmpl;
66 	struct crypto_instance *inst;
67 	int err;
68 
69 	tmpl = crypto_lookup_template(param->template);
70 	if (!tmpl)
71 		goto out;
72 
73 	do {
74 		if (tmpl->create) {
75 			err = tmpl->create(tmpl, param->tb);
76 			continue;
77 		}
78 
79 		inst = tmpl->alloc(param->tb);
80 		if (IS_ERR(inst))
81 			err = PTR_ERR(inst);
82 		else if ((err = crypto_register_instance(tmpl, inst)))
83 			tmpl->free(inst);
84 	} while (err == -EAGAIN && !signal_pending(current));
85 
86 	crypto_tmpl_put(tmpl);
87 
88 out:
89 	complete_all(&param->larval->completion);
90 	crypto_alg_put(&param->larval->alg);
91 	kfree(param);
92 	module_put_and_exit(0);
93 }
94 
cryptomgr_schedule_probe(struct crypto_larval * larval)95 static int cryptomgr_schedule_probe(struct crypto_larval *larval)
96 {
97 	struct task_struct *thread;
98 	struct cryptomgr_param *param;
99 	const char *name = larval->alg.cra_name;
100 	const char *p;
101 	unsigned int len;
102 	int i;
103 
104 	if (!try_module_get(THIS_MODULE))
105 		goto err;
106 
107 	param = kzalloc(sizeof(*param), GFP_KERNEL);
108 	if (!param)
109 		goto err_put_module;
110 
111 	for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
112 		;
113 
114 	len = p - name;
115 	if (!len || *p != '(')
116 		goto err_free_param;
117 
118 	memcpy(param->template, name, len);
119 
120 	i = 0;
121 	for (;;) {
122 		int notnum = 0;
123 
124 		name = ++p;
125 
126 		for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
127 			notnum |= !isdigit(*p);
128 
129 		if (*p == '(') {
130 			int recursion = 0;
131 
132 			for (;;) {
133 				if (!*++p)
134 					goto err_free_param;
135 				if (*p == '(')
136 					recursion++;
137 				else if (*p == ')' && !recursion--)
138 					break;
139 			}
140 
141 			notnum = 1;
142 			p++;
143 		}
144 
145 		len = p - name;
146 		if (!len)
147 			goto err_free_param;
148 
149 		if (notnum) {
150 			param->attrs[i].alg.attr.rta_len =
151 				sizeof(param->attrs[i].alg);
152 			param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG;
153 			memcpy(param->attrs[i].alg.data.name, name, len);
154 		} else {
155 			param->attrs[i].nu32.attr.rta_len =
156 				sizeof(param->attrs[i].nu32);
157 			param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32;
158 			param->attrs[i].nu32.data.num =
159 				simple_strtol(name, NULL, 0);
160 		}
161 
162 		param->tb[i + 1] = &param->attrs[i].attr;
163 		i++;
164 
165 		if (i >= CRYPTO_MAX_ATTRS)
166 			goto err_free_param;
167 
168 		if (*p == ')')
169 			break;
170 
171 		if (*p != ',')
172 			goto err_free_param;
173 	}
174 
175 	if (!i)
176 		goto err_free_param;
177 
178 	param->tb[i + 1] = NULL;
179 
180 	param->type.attr.rta_len = sizeof(param->type);
181 	param->type.attr.rta_type = CRYPTOA_TYPE;
182 	param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
183 	param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
184 	param->tb[0] = &param->type.attr;
185 
186 	param->otype = larval->alg.cra_flags;
187 	param->omask = larval->mask;
188 
189 	crypto_alg_get(&larval->alg);
190 	param->larval = larval;
191 
192 	thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
193 	if (IS_ERR(thread))
194 		goto err_put_larval;
195 
196 	wait_for_completion_interruptible(&larval->completion);
197 
198 	return NOTIFY_STOP;
199 
200 err_put_larval:
201 	crypto_alg_put(&larval->alg);
202 err_free_param:
203 	kfree(param);
204 err_put_module:
205 	module_put(THIS_MODULE);
206 err:
207 	return NOTIFY_OK;
208 }
209 
cryptomgr_test(void * data)210 static int cryptomgr_test(void *data)
211 {
212 	struct crypto_test_param *param = data;
213 	u32 type = param->type;
214 	int err = 0;
215 
216 #ifdef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
217 	goto skiptest;
218 #endif
219 
220 	if (type & CRYPTO_ALG_TESTED)
221 		goto skiptest;
222 
223 	err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
224 
225 skiptest:
226 	crypto_alg_tested(param->driver, err);
227 
228 	kfree(param);
229 	module_put_and_exit(0);
230 }
231 
cryptomgr_schedule_test(struct crypto_alg * alg)232 static int cryptomgr_schedule_test(struct crypto_alg *alg)
233 {
234 	struct task_struct *thread;
235 	struct crypto_test_param *param;
236 	u32 type;
237 
238 	if (!try_module_get(THIS_MODULE))
239 		goto err;
240 
241 	param = kzalloc(sizeof(*param), GFP_KERNEL);
242 	if (!param)
243 		goto err_put_module;
244 
245 	memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
246 	memcpy(param->alg, alg->cra_name, sizeof(param->alg));
247 	type = alg->cra_flags;
248 
249 	/* Do not test internal algorithms. */
250 	if (type & CRYPTO_ALG_INTERNAL)
251 		type |= CRYPTO_ALG_TESTED;
252 
253 	param->type = type;
254 
255 	thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
256 	if (IS_ERR(thread))
257 		goto err_free_param;
258 
259 	return NOTIFY_STOP;
260 
261 err_free_param:
262 	kfree(param);
263 err_put_module:
264 	module_put(THIS_MODULE);
265 err:
266 	return NOTIFY_OK;
267 }
268 
cryptomgr_notify(struct notifier_block * this,unsigned long msg,void * data)269 static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
270 			    void *data)
271 {
272 	switch (msg) {
273 	case CRYPTO_MSG_ALG_REQUEST:
274 		return cryptomgr_schedule_probe(data);
275 	case CRYPTO_MSG_ALG_REGISTER:
276 		return cryptomgr_schedule_test(data);
277 	}
278 
279 	return NOTIFY_DONE;
280 }
281 
282 static struct notifier_block cryptomgr_notifier = {
283 	.notifier_call = cryptomgr_notify,
284 };
285 
cryptomgr_init(void)286 static int __init cryptomgr_init(void)
287 {
288 	return crypto_register_notifier(&cryptomgr_notifier);
289 }
290 
cryptomgr_exit(void)291 static void __exit cryptomgr_exit(void)
292 {
293 	int err = crypto_unregister_notifier(&cryptomgr_notifier);
294 	BUG_ON(err);
295 }
296 
297 subsys_initcall(cryptomgr_init);
298 module_exit(cryptomgr_exit);
299 
300 MODULE_LICENSE("GPL");
301 MODULE_DESCRIPTION("Crypto Algorithm Manager");
302