Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-misc-2.6
[linux-2.6-block.git] / crypto / algboss.c
CommitLineData
2b8c19db
HX
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
6fe4a28d 13#include <crypto/internal/aead.h>
2b8c19db
HX
14#include <linux/ctype.h>
15#include <linux/err.h>
16#include <linux/init.h>
cf02f5da 17#include <linux/kthread.h>
2b8c19db
HX
18#include <linux/module.h>
19#include <linux/notifier.h>
20#include <linux/rtnetlink.h>
6bfd4809 21#include <linux/sched.h>
2b8c19db 22#include <linux/string.h>
2b8c19db
HX
23
24#include "internal.h"
25
26struct cryptomgr_param {
39e1ee01 27 struct rtattr *tb[CRYPTO_MAX_ATTRS + 2];
ebc610e5
HX
28
29 struct {
30 struct rtattr attr;
31 struct crypto_attr_type data;
32 } type;
33
39e1ee01 34 union {
2b8c19db 35 struct rtattr attr;
39e1ee01
HX
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];
2b8c19db 47 char template[CRYPTO_MAX_ALG_NAME];
73d3864a
HX
48
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;
2b8c19db
HX
57};
58
cf02f5da 59static int cryptomgr_probe(void *data)
2b8c19db 60{
cf02f5da 61 struct cryptomgr_param *param = data;
2b8c19db
HX
62 struct crypto_template *tmpl;
63 struct crypto_instance *inst;
6bfd4809 64 int err;
2b8c19db
HX
65
66 tmpl = crypto_lookup_template(param->template);
67 if (!tmpl)
68 goto err;
69
6bfd4809 70 do {
f2ac72e8
HX
71 if (tmpl->create) {
72 err = tmpl->create(tmpl, param->tb);
73 continue;
74 }
75
ebc610e5 76 inst = tmpl->alloc(param->tb);
6bfd4809
HX
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));
2b8c19db
HX
82
83 crypto_tmpl_put(tmpl);
84
6bfd4809
HX
85 if (err)
86 goto err;
87
2b8c19db
HX
88out:
89 kfree(param);
cf02f5da 90 module_put_and_exit(0);
2b8c19db
HX
91
92err:
73d3864a 93 crypto_larval_error(param->larval, param->otype, param->omask);
2b8c19db
HX
94 goto out;
95}
96
97static int cryptomgr_schedule_probe(struct crypto_larval *larval)
98{
1605b847 99 struct task_struct *thread;
2b8c19db
HX
100 struct cryptomgr_param *param;
101 const char *name = larval->alg.cra_name;
102 const char *p;
103 unsigned int len;
39e1ee01 104 int i;
2b8c19db 105
cf02f5da
HX
106 if (!try_module_get(THIS_MODULE))
107 goto err;
108
ebc610e5 109 param = kzalloc(sizeof(*param), GFP_KERNEL);
2b8c19db 110 if (!param)
cf02f5da 111 goto err_put_module;
2b8c19db
HX
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);
2b8c19db 121
39e1ee01
HX
122 i = 0;
123 for (;;) {
124 int notnum = 0;
2b8c19db 125
39e1ee01
HX
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;
720a650f 145 p++;
39e1ee01 146 }
cf02f5da
HX
147
148 len = p - name;
39e1ee01
HX
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
720a650f 168 if (i >= CRYPTO_MAX_ATTRS)
39e1ee01
HX
169 goto err_free_param;
170
171 if (*p == ')')
172 break;
173
174 if (*p != ',')
175 goto err_free_param;
cf02f5da
HX
176 }
177
39e1ee01 178 if (!i)
2b8c19db
HX
179 goto err_free_param;
180
39e1ee01
HX
181 param->tb[i + 1] = NULL;
182
ebc610e5
HX
183 param->type.attr.rta_len = sizeof(param->type);
184 param->type.attr.rta_type = CRYPTOA_TYPE;
73d3864a
HX
185 param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
186 param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
39e1ee01 187 param->tb[0] = &param->type.attr;
2b8c19db 188
73d3864a
HX
189 param->otype = larval->alg.cra_flags;
190 param->omask = larval->mask;
191
39e1ee01 192 memcpy(param->larval, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
2b8c19db 193
73d3864a
HX
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
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
6fe4a28d 214 if (type & CRYPTO_ALG_TESTED)
73d3864a
HX
215 goto skiptest;
216
1aa4ecd9 217 err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
73d3864a
HX
218
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;
6fe4a28d 230 u32 type;
73d3864a
HX
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));
6fe4a28d
HX
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;
73d3864a
HX
254
255 thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
1605b847 256 if (IS_ERR(thread))
cf02f5da 257 goto err_free_param;
2b8c19db
HX
258
259 return NOTIFY_STOP;
260
261err_free_param:
262 kfree(param);
cf02f5da
HX
263err_put_module:
264 module_put(THIS_MODULE);
2b8c19db
HX
265err:
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);
73d3864a
HX
275 case CRYPTO_MSG_ALG_REGISTER:
276 return cryptomgr_schedule_test(data);
2b8c19db
HX
277 }
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{
f8b0d4d0 288 return crypto_register_notifier(&cryptomgr_notifier);
2b8c19db
HX
289}
290
291static void __exit cryptomgr_exit(void)
292{
293 int err = crypto_unregister_notifier(&cryptomgr_notifier);
294 BUG_ON(err);
295}
296
da7f033d 297subsys_initcall(cryptomgr_init);
2b8c19db
HX
298module_exit(cryptomgr_exit);
299
300MODULE_LICENSE("GPL");
301MODULE_DESCRIPTION("Crypto Algorithm Manager");