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/module.h>
18 #include <linux/notifier.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/sched.h>
21 #include <linux/string.h>
22 #include <linux/workqueue.h>
26 struct cryptomgr_param {
27 struct work_struct work;
29 struct rtattr *tb[CRYPTOA_MAX];
33 struct crypto_attr_type data;
38 struct crypto_attr_alg data;
42 char name[CRYPTO_MAX_ALG_NAME];
45 char template[CRYPTO_MAX_ALG_NAME];
48 static void cryptomgr_probe(struct work_struct *work)
50 struct cryptomgr_param *param =
51 container_of(work, struct cryptomgr_param, work);
52 struct crypto_template *tmpl;
53 struct crypto_instance *inst;
56 tmpl = crypto_lookup_template(param->template);
61 inst = tmpl->alloc(param->tb);
64 else if ((err = crypto_register_instance(tmpl, inst)))
66 } while (err == -EAGAIN && !signal_pending(current));
68 crypto_tmpl_put(tmpl);
78 crypto_larval_error(param->larval.name, param->type.data.type,
79 param->type.data.mask);
83 static int cryptomgr_schedule_probe(struct crypto_larval *larval)
85 struct cryptomgr_param *param;
86 const char *name = larval->alg.cra_name;
90 param = kzalloc(sizeof(*param), GFP_KERNEL);
94 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
98 if (!len || *p != '(')
101 memcpy(param->template, name, len);
104 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
108 if (!len || *p != ')' || p[1])
111 param->type.attr.rta_len = sizeof(param->type);
112 param->type.attr.rta_type = CRYPTOA_TYPE;
113 param->type.data.type = larval->alg.cra_flags;
114 param->type.data.mask = larval->mask;
115 param->tb[CRYPTOA_TYPE - 1] = ¶m->type.attr;
117 param->alg.attr.rta_len = sizeof(param->alg);
118 param->alg.attr.rta_type = CRYPTOA_ALG;
119 memcpy(param->alg.data.name, name, len);
120 param->tb[CRYPTOA_ALG - 1] = ¶m->alg.attr;
122 memcpy(param->larval.name, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
124 INIT_WORK(¶m->work, cryptomgr_probe);
125 schedule_work(¶m->work);
135 static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
139 case CRYPTO_MSG_ALG_REQUEST:
140 return cryptomgr_schedule_probe(data);
146 static struct notifier_block cryptomgr_notifier = {
147 .notifier_call = cryptomgr_notify,
150 static int __init cryptomgr_init(void)
152 return crypto_register_notifier(&cryptomgr_notifier);
155 static void __exit cryptomgr_exit(void)
157 int err = crypto_unregister_notifier(&cryptomgr_notifier);
161 module_init(cryptomgr_init);
162 module_exit(cryptomgr_exit);
164 MODULE_LICENSE("GPL");
165 MODULE_DESCRIPTION("Crypto Algorithm Manager");