xref: /openbmc/linux/crypto/algboss.c (revision 9d56dd3b083a3bec56e9da35ce07baca81030b03)
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/ctype.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/kthread.h>
18 #include <linux/module.h>
19 #include <linux/notifier.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/sched.h>
22 #include <linux/string.h>
23 
24 #include "internal.h"
25 
26 struct cryptomgr_param {
27 	struct rtattr *tb[CRYPTO_MAX_ATTRS + 2];
28 
29 	struct {
30 		struct rtattr attr;
31 		struct crypto_attr_type data;
32 	} type;
33 
34 	union {
35 		struct rtattr attr;
36 		struct {
37 			struct rtattr attr;
38 			struct crypto_attr_alg data;
39 		} alg;
40 		struct {
41 			struct rtattr attr;
42 			struct crypto_attr_u32 data;
43 		} nu32;
44 	} attrs[CRYPTO_MAX_ATTRS];
45 
46 	char larval[CRYPTO_MAX_ALG_NAME];
47 	char template[CRYPTO_MAX_ALG_NAME];
48 
49 	u32 otype;
50 	u32 omask;
51 };
52 
53 struct crypto_test_param {
54 	char driver[CRYPTO_MAX_ALG_NAME];
55 	char alg[CRYPTO_MAX_ALG_NAME];
56 	u32 type;
57 };
58 
59 static int cryptomgr_probe(void *data)
60 {
61 	struct cryptomgr_param *param = data;
62 	struct crypto_template *tmpl;
63 	struct crypto_instance *inst;
64 	int err;
65 
66 	tmpl = crypto_lookup_template(param->template);
67 	if (!tmpl)
68 		goto err;
69 
70 	do {
71 		if (tmpl->create) {
72 			err = tmpl->create(tmpl, param->tb);
73 			continue;
74 		}
75 
76 		inst = tmpl->alloc(param->tb);
77 		if (IS_ERR(inst))
78 			err = PTR_ERR(inst);
79 		else if ((err = crypto_register_instance(tmpl, inst)))
80 			tmpl->free(inst);
81 	} while (err == -EAGAIN && !signal_pending(current));
82 
83 	crypto_tmpl_put(tmpl);
84 
85 	if (err)
86 		goto err;
87 
88 out:
89 	kfree(param);
90 	module_put_and_exit(0);
91 
92 err:
93 	crypto_larval_error(param->larval, param->otype, param->omask);
94 	goto out;
95 }
96 
97 static int cryptomgr_schedule_probe(struct crypto_larval *larval)
98 {
99 	struct task_struct *thread;
100 	struct cryptomgr_param *param;
101 	const char *name = larval->alg.cra_name;
102 	const char *p;
103 	unsigned int len;
104 	int i;
105 
106 	if (!try_module_get(THIS_MODULE))
107 		goto err;
108 
109 	param = kzalloc(sizeof(*param), GFP_KERNEL);
110 	if (!param)
111 		goto err_put_module;
112 
113 	for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
114 		;
115 
116 	len = p - name;
117 	if (!len || *p != '(')
118 		goto err_free_param;
119 
120 	memcpy(param->template, name, len);
121 
122 	i = 0;
123 	for (;;) {
124 		int notnum = 0;
125 
126 		name = ++p;
127 		len = 0;
128 
129 		for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
130 			notnum |= !isdigit(*p);
131 
132 		if (*p == '(') {
133 			int recursion = 0;
134 
135 			for (;;) {
136 				if (!*++p)
137 					goto err_free_param;
138 				if (*p == '(')
139 					recursion++;
140 				else if (*p == ')' && !recursion--)
141 					break;
142 			}
143 
144 			notnum = 1;
145 			p++;
146 		}
147 
148 		len = p - name;
149 		if (!len)
150 			goto err_free_param;
151 
152 		if (notnum) {
153 			param->attrs[i].alg.attr.rta_len =
154 				sizeof(param->attrs[i].alg);
155 			param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG;
156 			memcpy(param->attrs[i].alg.data.name, name, len);
157 		} else {
158 			param->attrs[i].nu32.attr.rta_len =
159 				sizeof(param->attrs[i].nu32);
160 			param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32;
161 			param->attrs[i].nu32.data.num =
162 				simple_strtol(name, NULL, 0);
163 		}
164 
165 		param->tb[i + 1] = &param->attrs[i].attr;
166 		i++;
167 
168 		if (i >= CRYPTO_MAX_ATTRS)
169 			goto err_free_param;
170 
171 		if (*p == ')')
172 			break;
173 
174 		if (*p != ',')
175 			goto err_free_param;
176 	}
177 
178 	if (!i)
179 		goto err_free_param;
180 
181 	param->tb[i + 1] = NULL;
182 
183 	param->type.attr.rta_len = sizeof(param->type);
184 	param->type.attr.rta_type = CRYPTOA_TYPE;
185 	param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
186 	param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
187 	param->tb[0] = &param->type.attr;
188 
189 	param->otype = larval->alg.cra_flags;
190 	param->omask = larval->mask;
191 
192 	memcpy(param->larval, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
193 
194 	thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
195 	if (IS_ERR(thread))
196 		goto err_free_param;
197 
198 	return NOTIFY_STOP;
199 
200 err_free_param:
201 	kfree(param);
202 err_put_module:
203 	module_put(THIS_MODULE);
204 err:
205 	return NOTIFY_OK;
206 }
207 
208 static int cryptomgr_test(void *data)
209 {
210 	struct crypto_test_param *param = data;
211 	u32 type = param->type;
212 	int err = 0;
213 
214 	if (type & CRYPTO_ALG_TESTED)
215 		goto skiptest;
216 
217 	err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
218 
219 skiptest:
220 	crypto_alg_tested(param->driver, err);
221 
222 	kfree(param);
223 	module_put_and_exit(0);
224 }
225 
226 static int cryptomgr_schedule_test(struct crypto_alg *alg)
227 {
228 	struct task_struct *thread;
229 	struct crypto_test_param *param;
230 	u32 type;
231 
232 	if (!try_module_get(THIS_MODULE))
233 		goto err;
234 
235 	param = kzalloc(sizeof(*param), GFP_KERNEL);
236 	if (!param)
237 		goto err_put_module;
238 
239 	memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
240 	memcpy(param->alg, alg->cra_name, sizeof(param->alg));
241 	type = alg->cra_flags;
242 
243 	/* This piece of crap needs to disappear into per-type test hooks. */
244 	if ((!((type ^ CRYPTO_ALG_TYPE_BLKCIPHER) &
245 	       CRYPTO_ALG_TYPE_BLKCIPHER_MASK) && !(type & CRYPTO_ALG_GENIV) &&
246 	     ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
247 	      CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
248 					  alg->cra_ablkcipher.ivsize)) ||
249 	    (!((type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK) &&
250 	     alg->cra_type == &crypto_nivaead_type && alg->cra_aead.ivsize))
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 
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 
286 static int __init cryptomgr_init(void)
287 {
288 	return crypto_register_notifier(&cryptomgr_notifier);
289 }
290 
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