blob: c83884fec5f928fd8e75bf8a82205411c4004dff [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
13#include <linux/crypto.h>
14#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];
48};
49
Herbert Xucf02f5d2007-03-29 17:32:59 +100050static int cryptomgr_probe(void *data)
Herbert Xu2b8c19d2006-09-21 11:31:44 +100051{
Herbert Xucf02f5d2007-03-29 17:32:59 +100052 struct cryptomgr_param *param = data;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100053 struct crypto_template *tmpl;
54 struct crypto_instance *inst;
Herbert Xu6bfd4802006-09-21 11:39:29 +100055 int err;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100056
57 tmpl = crypto_lookup_template(param->template);
58 if (!tmpl)
59 goto err;
60
Herbert Xu6bfd4802006-09-21 11:39:29 +100061 do {
Herbert Xuebc610e2007-01-01 18:37:02 +110062 inst = tmpl->alloc(param->tb);
Herbert Xu6bfd4802006-09-21 11:39:29 +100063 if (IS_ERR(inst))
64 err = PTR_ERR(inst);
65 else if ((err = crypto_register_instance(tmpl, inst)))
66 tmpl->free(inst);
67 } while (err == -EAGAIN && !signal_pending(current));
Herbert Xu2b8c19d2006-09-21 11:31:44 +100068
69 crypto_tmpl_put(tmpl);
70
Herbert Xu6bfd4802006-09-21 11:39:29 +100071 if (err)
72 goto err;
73
Herbert Xu2b8c19d2006-09-21 11:31:44 +100074out:
75 kfree(param);
Herbert Xucf02f5d2007-03-29 17:32:59 +100076 module_put_and_exit(0);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100077
78err:
Herbert Xu39e1ee012007-08-29 19:27:26 +080079 crypto_larval_error(param->larval, param->type.data.type,
Herbert Xuebc610e2007-01-01 18:37:02 +110080 param->type.data.mask);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100081 goto out;
82}
83
84static int cryptomgr_schedule_probe(struct crypto_larval *larval)
85{
Herbert Xu1605b842007-05-09 13:04:39 +100086 struct task_struct *thread;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100087 struct cryptomgr_param *param;
88 const char *name = larval->alg.cra_name;
89 const char *p;
90 unsigned int len;
Herbert Xu39e1ee012007-08-29 19:27:26 +080091 int i;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100092
Herbert Xucf02f5d2007-03-29 17:32:59 +100093 if (!try_module_get(THIS_MODULE))
94 goto err;
95
Herbert Xuebc610e2007-01-01 18:37:02 +110096 param = kzalloc(sizeof(*param), GFP_KERNEL);
Herbert Xu2b8c19d2006-09-21 11:31:44 +100097 if (!param)
Herbert Xucf02f5d2007-03-29 17:32:59 +100098 goto err_put_module;
Herbert Xu2b8c19d2006-09-21 11:31:44 +100099
100 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
101 ;
102
103 len = p - name;
104 if (!len || *p != '(')
105 goto err_free_param;
106
107 memcpy(param->template, name, len);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000108
Herbert Xu39e1ee012007-08-29 19:27:26 +0800109 i = 0;
110 for (;;) {
111 int notnum = 0;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000112
Herbert Xu39e1ee012007-08-29 19:27:26 +0800113 name = ++p;
114 len = 0;
115
116 for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
117 notnum |= !isdigit(*p);
118
119 if (*p == '(') {
120 int recursion = 0;
121
122 for (;;) {
123 if (!*++p)
124 goto err_free_param;
125 if (*p == '(')
126 recursion++;
127 else if (*p == ')' && !recursion--)
128 break;
129 }
130
131 notnum = 1;
132 }
Herbert Xucf02f5d2007-03-29 17:32:59 +1000133
134 len = p - name;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800135 if (!len)
136 goto err_free_param;
137
138 if (notnum) {
139 param->attrs[i].alg.attr.rta_len =
140 sizeof(param->attrs[i].alg);
141 param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG;
142 memcpy(param->attrs[i].alg.data.name, name, len);
143 } else {
144 param->attrs[i].nu32.attr.rta_len =
145 sizeof(param->attrs[i].nu32);
146 param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32;
147 param->attrs[i].nu32.data.num =
148 simple_strtol(name, NULL, 0);
149 }
150
151 param->tb[i + 1] = &param->attrs[i].attr;
152 i++;
153
154 if (WARN_ON(i >= CRYPTO_MAX_ATTRS))
155 goto err_free_param;
156
157 if (*p == ')')
158 break;
159
160 if (*p != ',')
161 goto err_free_param;
Herbert Xucf02f5d2007-03-29 17:32:59 +1000162 }
163
Herbert Xu39e1ee012007-08-29 19:27:26 +0800164 if (!i)
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000165 goto err_free_param;
166
Herbert Xu39e1ee012007-08-29 19:27:26 +0800167 param->tb[i + 1] = NULL;
168
Herbert Xuebc610e2007-01-01 18:37:02 +1100169 param->type.attr.rta_len = sizeof(param->type);
170 param->type.attr.rta_type = CRYPTOA_TYPE;
171 param->type.data.type = larval->alg.cra_flags;
172 param->type.data.mask = larval->mask;
Herbert Xu39e1ee012007-08-29 19:27:26 +0800173 param->tb[0] = &param->type.attr;
Herbert Xuebc610e2007-01-01 18:37:02 +1100174
Herbert Xu39e1ee012007-08-29 19:27:26 +0800175 memcpy(param->larval, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000176
Herbert Xu1605b842007-05-09 13:04:39 +1000177 thread = kthread_run(cryptomgr_probe, param, "cryptomgr");
178 if (IS_ERR(thread))
Herbert Xucf02f5d2007-03-29 17:32:59 +1000179 goto err_free_param;
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000180
181 return NOTIFY_STOP;
182
183err_free_param:
184 kfree(param);
Herbert Xucf02f5d2007-03-29 17:32:59 +1000185err_put_module:
186 module_put(THIS_MODULE);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000187err:
188 return NOTIFY_OK;
189}
190
191static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
192 void *data)
193{
194 switch (msg) {
195 case CRYPTO_MSG_ALG_REQUEST:
196 return cryptomgr_schedule_probe(data);
197 }
198
199 return NOTIFY_DONE;
200}
201
202static struct notifier_block cryptomgr_notifier = {
203 .notifier_call = cryptomgr_notify,
204};
205
206static int __init cryptomgr_init(void)
207{
208 return crypto_register_notifier(&cryptomgr_notifier);
209}
210
211static void __exit cryptomgr_exit(void)
212{
213 int err = crypto_unregister_notifier(&cryptomgr_notifier);
214 BUG_ON(err);
215}
216
217module_init(cryptomgr_init);
218module_exit(cryptomgr_exit);
219
220MODULE_LICENSE("GPL");
221MODULE_DESCRIPTION("Crypto Algorithm Manager");