blob: 412241ce4cfae25822a6beccf9360c9f52ccc89f [file] [log] [blame]
Herbert Xu2b8c19d2006-09-21 11:31:44 +10001/*
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
Herbert Xu6fe4a282009-02-18 21:41:29 +080013#include <crypto/internal/aead.h>
Herbert Xu2b8c19d2006-09-21 11:31:44 +100014#include <linux/ctype.h>
15#include <linux/err.h>
16#include <linux/init.h>
Herbert Xucf02f5d2007-03-29 17:32:59 +100017#include <linux/kthread.h>
Herbert Xu2b8c19d2006-09-21 11:31:44 +100018#include <linux/module.h>
19#include <linux/notifier.h>
20#include <linux/rtnetlink.h>
Herbert Xu6bfd4802006-09-21 11:39:29 +100021#include <linux/sched.h>
Herbert Xu2b8c19d2006-09-21 11:31:44 +100022#include <linux/string.h>
Herbert Xu2b8c19d2006-09-21 11:31:44 +100023
24#include "internal.h"
25
26struct cryptomgr_param {
Herbert Xu39e1ee012007-08-29 19:27:26 +080027 struct rtattr *tb[CRYPTO_MAX_ATTRS + 2];
Herbert Xuebc610e2007-01-01 18:37:02 +110028
29 struct {
30 struct rtattr attr;
31 struct crypto_attr_type data;
32 } type;
33
Herbert Xu39e1ee012007-08-29 19:27:26 +080034 union {
Herbert Xu2b8c19d2006-09-21 11:31:44 +100035 struct rtattr attr;
Herbert Xu39e1ee012007-08-29 19:27:26 +080036 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];
Herbert Xu2b8c19d2006-09-21 11:31:44 +100045
Herbert Xu39e1ee012007-08-29 19:27:26 +080046 char larval[CRYPTO_MAX_ALG_NAME];
Herbert Xu2b8c19d2006-09-21 11:31:44 +100047 char template[CRYPTO_MAX_ALG_NAME];
Herbert Xu73d38642008-08-03 21:15:23 +080048
49 u32 otype;
50 u32 omask;
51};
52
53struct crypto_test_param {
54 char driver[CRYPTO_MAX_ALG_NAME];
55 char alg[CRYPTO_MAX_ALG_NAME];
56 u32 type;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100057};
58
Herbert Xucf02f5d2007-03-29 17:32:59 +100059static int cryptomgr_probe(void *data)
Herbert Xu2b8c19d2006-09-21 11:31:44 +100060{
Herbert Xucf02f5d2007-03-29 17:32:59 +100061 struct cryptomgr_param *param = data;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100062 struct crypto_template *tmpl;
63 struct crypto_instance *inst;
Herbert Xu6bfd4802006-09-21 11:39:29 +100064 int err;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100065
66 tmpl = crypto_lookup_template(param->template);
67 if (!tmpl)
68 goto err;
69
Herbert Xu6bfd4802006-09-21 11:39:29 +100070 do {
Herbert Xuf2ac72e2009-07-07 12:30:33 +080071 if (tmpl->create) {
72 err = tmpl->create(tmpl, param->tb);
73 continue;
74 }
75
Herbert Xuebc610e2007-01-01 18:37:02 +110076 inst = tmpl->alloc(param->tb);
Herbert Xu6bfd4802006-09-21 11:39:29 +100077 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));
Herbert Xu2b8c19d2006-09-21 11:31:44 +100082
83 crypto_tmpl_put(tmpl);
84
Herbert Xu6bfd4802006-09-21 11:39:29 +100085 if (err)
86 goto err;
87
Herbert Xu2b8c19d2006-09-21 11:31:44 +100088out:
89 kfree(param);
Herbert Xucf02f5d2007-03-29 17:32:59 +100090 module_put_and_exit(0);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100091
92err:
Herbert Xu73d38642008-08-03 21:15:23 +080093 crypto_larval_error(param->larval, param->otype, param->omask);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100094 goto out;
95}
96
97static int cryptomgr_schedule_probe(struct crypto_larval *larval)
98{
Herbert Xu1605b842007-05-09 13:04:39 +100099 struct task_struct *thread;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000100 struct cryptomgr_param *param;
101 const char *name = larval->alg.cra_name;
102 const char *p;
103 unsigned int len;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800104 int i;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000105
Herbert Xucf02f5d2007-03-29 17:32:59 +1000106 if (!try_module_get(THIS_MODULE))
107 goto err;
108
Herbert Xuebc610e2007-01-01 18:37:02 +1100109 param = kzalloc(sizeof(*param), GFP_KERNEL);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000110 if (!param)
Herbert Xucf02f5d2007-03-29 17:32:59 +1000111 goto err_put_module;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000112
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);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000121
Herbert Xu39e1ee012007-08-29 19:27:26 +0800122 i = 0;
123 for (;;) {
124 int notnum = 0;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000125
Herbert Xu39e1ee012007-08-29 19:27:26 +0800126 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;
Herbert Xu720a6502007-09-28 09:06:11 +0800145 p++;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800146 }
Herbert Xucf02f5d2007-03-29 17:32:59 +1000147
148 len = p - name;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800149 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
Herbert Xu720a6502007-09-28 09:06:11 +0800168 if (i >= CRYPTO_MAX_ATTRS)
Herbert Xu39e1ee012007-08-29 19:27:26 +0800169 goto err_free_param;
170
171 if (*p == ')')
172 break;
173
174 if (*p != ',')
175 goto err_free_param;
Herbert Xucf02f5d2007-03-29 17:32:59 +1000176 }
177
Herbert Xu39e1ee012007-08-29 19:27:26 +0800178 if (!i)
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000179 goto err_free_param;
180
Herbert Xu39e1ee012007-08-29 19:27:26 +0800181 param->tb[i + 1] = NULL;
182
Herbert Xuebc610e2007-01-01 18:37:02 +1100183 param->type.attr.rta_len = sizeof(param->type);
184 param->type.attr.rta_type = CRYPTOA_TYPE;
Herbert Xu73d38642008-08-03 21:15:23 +0800185 param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
186 param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800187 param->tb[0] = &param->type.attr;
Herbert Xuebc610e2007-01-01 18:37:02 +1100188
Herbert Xu73d38642008-08-03 21:15:23 +0800189 param->otype = larval->alg.cra_flags;
190 param->omask = larval->mask;
191
Herbert Xu39e1ee012007-08-29 19:27:26 +0800192 memcpy(param->larval, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000193
Herbert Xu73d38642008-08-03 21:15:23 +0800194 thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
195 if (IS_ERR(thread))
196 goto err_free_param;
197
198 return NOTIFY_STOP;
199
200err_free_param:
201 kfree(param);
202err_put_module:
203 module_put(THIS_MODULE);
204err:
205 return NOTIFY_OK;
206}
207
208static int cryptomgr_test(void *data)
209{
210 struct crypto_test_param *param = data;
211 u32 type = param->type;
212 int err = 0;
213
Herbert Xu6fe4a282009-02-18 21:41:29 +0800214 if (type & CRYPTO_ALG_TESTED)
Herbert Xu73d38642008-08-03 21:15:23 +0800215 goto skiptest;
216
Herbert Xu1aa4ecd2008-08-17 17:01:56 +1000217 err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
Herbert Xu73d38642008-08-03 21:15:23 +0800218
219skiptest:
220 crypto_alg_tested(param->driver, err);
221
222 kfree(param);
223 module_put_and_exit(0);
224}
225
226static int cryptomgr_schedule_test(struct crypto_alg *alg)
227{
228 struct task_struct *thread;
229 struct crypto_test_param *param;
Herbert Xu6fe4a282009-02-18 21:41:29 +0800230 u32 type;
Herbert Xu73d38642008-08-03 21:15:23 +0800231
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));
Herbert Xu6fe4a282009-02-18 21:41:29 +0800241 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;
Herbert Xu73d38642008-08-03 21:15:23 +0800254
255 thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
Herbert Xu1605b842007-05-09 13:04:39 +1000256 if (IS_ERR(thread))
Herbert Xucf02f5d2007-03-29 17:32:59 +1000257 goto err_free_param;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000258
259 return NOTIFY_STOP;
260
261err_free_param:
262 kfree(param);
Herbert Xucf02f5d2007-03-29 17:32:59 +1000263err_put_module:
264 module_put(THIS_MODULE);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000265err:
266 return NOTIFY_OK;
267}
268
269static 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);
Herbert Xu73d38642008-08-03 21:15:23 +0800275 case CRYPTO_MSG_ALG_REGISTER:
276 return cryptomgr_schedule_test(data);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000277 }
278
279 return NOTIFY_DONE;
280}
281
282static struct notifier_block cryptomgr_notifier = {
283 .notifier_call = cryptomgr_notify,
284};
285
286static int __init cryptomgr_init(void)
287{
Herbert Xuf8b0d4d2009-05-06 14:15:47 +0800288 return crypto_register_notifier(&cryptomgr_notifier);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000289}
290
291static void __exit cryptomgr_exit(void)
292{
293 int err = crypto_unregister_notifier(&cryptomgr_notifier);
294 BUG_ON(err);
295}
296
Herbert Xuda7f0332008-07-31 17:08:25 +0800297subsys_initcall(cryptomgr_init);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000298module_exit(cryptomgr_exit);
299
300MODULE_LICENSE("GPL");
301MODULE_DESCRIPTION("Crypto Algorithm Manager");