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 <linux/crypto.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];
50 static int cryptomgr_probe(void *data)
52 struct cryptomgr_param *param = data;
53 struct crypto_template *tmpl;
54 struct crypto_instance *inst;
57 tmpl = crypto_lookup_template(param->template);
62 inst = tmpl->alloc(param->tb);
65 else if ((err = crypto_register_instance(tmpl, inst)))
67 } while (err == -EAGAIN && !signal_pending(current));
69 crypto_tmpl_put(tmpl);
76 module_put_and_exit(0);
79 crypto_larval_error(param->larval, param->type.data.type,
80 param->type.data.mask);
84 static int cryptomgr_schedule_probe(struct crypto_larval *larval)
86 struct task_struct *thread;
87 struct cryptomgr_param *param;
88 const char *name = larval->alg.cra_name;
93 if (!try_module_get(THIS_MODULE))
96 param = kzalloc(sizeof(*param), GFP_KERNEL);
100 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
104 if (!len || *p != '(')
107 memcpy(param->template, name, len);
116 for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
117 notnum |= !isdigit(*p);
127 else if (*p == ')' && !recursion--)
140 param->attrs[i].alg.attr.rta_len =
141 sizeof(param->attrs[i].alg);
142 param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG;
143 memcpy(param->attrs[i].alg.data.name, name, len);
145 param->attrs[i].nu32.attr.rta_len =
146 sizeof(param->attrs[i].nu32);
147 param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32;
148 param->attrs[i].nu32.data.num =
149 simple_strtol(name, NULL, 0);
152 param->tb[i + 1] = ¶m->attrs[i].attr;
155 if (i >= CRYPTO_MAX_ATTRS)
168 param->tb[i + 1] = NULL;
170 param->type.attr.rta_len = sizeof(param->type);
171 param->type.attr.rta_type = CRYPTOA_TYPE;
172 param->type.data.type = larval->alg.cra_flags;
173 param->type.data.mask = larval->mask;
174 param->tb[0] = ¶m->type.attr;
176 memcpy(param->larval, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
178 thread = kthread_run(cryptomgr_probe, param, "cryptomgr");
187 module_put(THIS_MODULE);
192 static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
196 case CRYPTO_MSG_ALG_REQUEST:
197 return cryptomgr_schedule_probe(data);
203 static struct notifier_block cryptomgr_notifier = {
204 .notifier_call = cryptomgr_notify,
207 static int __init cryptomgr_init(void)
209 return crypto_register_notifier(&cryptomgr_notifier);
212 static void __exit cryptomgr_exit(void)
214 int err = crypto_unregister_notifier(&cryptomgr_notifier);
218 module_init(cryptomgr_init);
219 module_exit(cryptomgr_exit);
221 MODULE_LICENSE("GPL");
222 MODULE_DESCRIPTION("Crypto Algorithm Manager");