2 * Create default crypto algorithm instances.
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
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)
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>
26 struct cryptomgr_param {
27 struct rtattr *tb[CRYPTO_MAX_ATTRS + 2];
31 struct crypto_attr_type data;
38 struct crypto_attr_alg data;
42 struct crypto_attr_u32 data;
44 } attrs[CRYPTO_MAX_ATTRS];
46 char larval[CRYPTO_MAX_ALG_NAME];
47 char template[CRYPTO_MAX_ALG_NAME];
53 struct crypto_test_param {
54 char driver[CRYPTO_MAX_ALG_NAME];
55 char alg[CRYPTO_MAX_ALG_NAME];
59 static int cryptomgr_probe(void *data)
61 struct cryptomgr_param *param = data;
62 struct crypto_template *tmpl;
63 struct crypto_instance *inst;
66 tmpl = crypto_lookup_template(param->template);
72 err = tmpl->create(tmpl, param->tb);
76 inst = tmpl->alloc(param->tb);
79 else if ((err = crypto_register_instance(tmpl, inst)))
81 } while (err == -EAGAIN && !signal_pending(current));
83 crypto_tmpl_put(tmpl);
90 module_put_and_exit(0);
93 crypto_larval_error(param->larval, param->otype, param->omask);
97 static int cryptomgr_schedule_probe(struct crypto_larval *larval)
99 struct task_struct *thread;
100 struct cryptomgr_param *param;
101 const char *name = larval->alg.cra_name;
106 if (!try_module_get(THIS_MODULE))
109 param = kzalloc(sizeof(*param), GFP_KERNEL);
113 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
117 if (!len || *p != '(')
120 memcpy(param->template, name, len);
129 for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
130 notnum |= !isdigit(*p);
140 else if (*p == ')' && !recursion--)
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);
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);
165 param->tb[i + 1] = ¶m->attrs[i].attr;
168 if (i >= CRYPTO_MAX_ATTRS)
181 param->tb[i + 1] = NULL;
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] = ¶m->type.attr;
189 param->otype = larval->alg.cra_flags;
190 param->omask = larval->mask;
192 memcpy(param->larval, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
194 thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
203 module_put(THIS_MODULE);
208 static int cryptomgr_test(void *data)
210 struct crypto_test_param *param = data;
211 u32 type = param->type;
214 if (type & CRYPTO_ALG_TESTED)
217 err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
220 crypto_alg_tested(param->driver, err);
223 module_put_and_exit(0);
226 static int cryptomgr_schedule_test(struct crypto_alg *alg)
228 struct task_struct *thread;
229 struct crypto_test_param *param;
232 if (!try_module_get(THIS_MODULE))
235 param = kzalloc(sizeof(*param), GFP_KERNEL);
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;
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;
255 thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
264 module_put(THIS_MODULE);
269 static int cryptomgr_notify(struct notifier_block *this, unsigned long 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);
282 static struct notifier_block cryptomgr_notifier = {
283 .notifier_call = cryptomgr_notify,
286 static int __init cryptomgr_init(void)
288 return crypto_register_notifier(&cryptomgr_notifier);
291 static void __exit cryptomgr_exit(void)
293 int err = crypto_unregister_notifier(&cryptomgr_notifier);
297 subsys_initcall(cryptomgr_init);
298 module_exit(cryptomgr_exit);
300 MODULE_LICENSE("GPL");
301 MODULE_DESCRIPTION("Crypto Algorithm Manager");