diff options
-rw-r--r-- | crypto/Kconfig | 8 | ||||
-rw-r--r-- | crypto/Makefile | 1 | ||||
-rw-r--r-- | crypto/api.c | 10 | ||||
-rw-r--r-- | crypto/cryptomgr.c | 146 | ||||
-rw-r--r-- | include/linux/crypto.h | 9 |
5 files changed, 173 insertions, 1 deletions
diff --git a/crypto/Kconfig b/crypto/Kconfig index aabc63195222..4ce509dba329 100644 --- a/crypto/Kconfig +++ b/crypto/Kconfig | |||
@@ -16,6 +16,14 @@ config CRYPTO_ALGAPI | |||
16 | help | 16 | help |
17 | This option provides the API for cryptographic algorithms. | 17 | This option provides the API for cryptographic algorithms. |
18 | 18 | ||
19 | config CRYPTO_MANAGER | ||
20 | tristate "Cryptographic algorithm manager" | ||
21 | select CRYPTO_ALGAPI | ||
22 | default m | ||
23 | help | ||
24 | Create default cryptographic template instantiations such as | ||
25 | cbc(aes). | ||
26 | |||
19 | config CRYPTO_HMAC | 27 | config CRYPTO_HMAC |
20 | bool "HMAC support" | 28 | bool "HMAC support" |
21 | help | 29 | help |
diff --git a/crypto/Makefile b/crypto/Makefile index 6d51f80753a1..b8745f3d3595 100644 --- a/crypto/Makefile +++ b/crypto/Makefile | |||
@@ -8,6 +8,7 @@ crypto_algapi-$(CONFIG_PROC_FS) += proc.o | |||
8 | crypto_algapi-objs := algapi.o $(crypto_algapi-y) | 8 | crypto_algapi-objs := algapi.o $(crypto_algapi-y) |
9 | obj-$(CONFIG_CRYPTO_ALGAPI) += crypto_algapi.o | 9 | obj-$(CONFIG_CRYPTO_ALGAPI) += crypto_algapi.o |
10 | 10 | ||
11 | obj-$(CONFIG_CRYPTO_MANAGER) += cryptomgr.o | ||
11 | obj-$(CONFIG_CRYPTO_HMAC) += hmac.o | 12 | obj-$(CONFIG_CRYPTO_HMAC) += hmac.o |
12 | obj-$(CONFIG_CRYPTO_NULL) += crypto_null.o | 13 | obj-$(CONFIG_CRYPTO_NULL) += crypto_null.o |
13 | obj-$(CONFIG_CRYPTO_MD4) += md4.o | 14 | obj-$(CONFIG_CRYPTO_MD4) += md4.o |
diff --git a/crypto/api.c b/crypto/api.c index 5a0d6a17cfd7..67cd6f87b74a 100644 --- a/crypto/api.c +++ b/crypto/api.c | |||
@@ -18,6 +18,7 @@ | |||
18 | #include <linux/errno.h> | 18 | #include <linux/errno.h> |
19 | #include <linux/kernel.h> | 19 | #include <linux/kernel.h> |
20 | #include <linux/kmod.h> | 20 | #include <linux/kmod.h> |
21 | #include <linux/module.h> | ||
21 | #include <linux/param.h> | 22 | #include <linux/param.h> |
22 | #include <linux/slab.h> | 23 | #include <linux/slab.h> |
23 | #include <linux/string.h> | 24 | #include <linux/string.h> |
@@ -170,6 +171,7 @@ static struct crypto_alg *crypto_alg_mod_lookup(const char *name) | |||
170 | { | 171 | { |
171 | struct crypto_alg *alg; | 172 | struct crypto_alg *alg; |
172 | struct crypto_alg *larval; | 173 | struct crypto_alg *larval; |
174 | int ok; | ||
173 | 175 | ||
174 | alg = try_then_request_module(crypto_alg_lookup(name), name); | 176 | alg = try_then_request_module(crypto_alg_lookup(name), name); |
175 | if (alg) | 177 | if (alg) |
@@ -179,7 +181,13 @@ static struct crypto_alg *crypto_alg_mod_lookup(const char *name) | |||
179 | if (!larval || !crypto_is_larval(larval)) | 181 | if (!larval || !crypto_is_larval(larval)) |
180 | return larval; | 182 | return larval; |
181 | 183 | ||
182 | if (crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval) == NOTIFY_STOP) | 184 | ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval); |
185 | if (ok == NOTIFY_DONE) { | ||
186 | request_module("cryptomgr"); | ||
187 | ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval); | ||
188 | } | ||
189 | |||
190 | if (ok == NOTIFY_STOP) | ||
183 | alg = crypto_larval_wait(larval); | 191 | alg = crypto_larval_wait(larval); |
184 | else { | 192 | else { |
185 | crypto_mod_put(larval); | 193 | crypto_mod_put(larval); |
diff --git a/crypto/cryptomgr.c b/crypto/cryptomgr.c new file mode 100644 index 000000000000..e0ebe1b44b99 --- /dev/null +++ b/crypto/cryptomgr.c | |||
@@ -0,0 +1,146 @@ | |||
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 | |||
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/string.h> | ||
21 | #include <linux/workqueue.h> | ||
22 | |||
23 | #include "internal.h" | ||
24 | |||
25 | struct cryptomgr_param { | ||
26 | struct work_struct work; | ||
27 | |||
28 | struct { | ||
29 | struct rtattr attr; | ||
30 | struct crypto_attr_alg data; | ||
31 | } alg; | ||
32 | |||
33 | struct { | ||
34 | char name[CRYPTO_MAX_ALG_NAME]; | ||
35 | } larval; | ||
36 | |||
37 | char template[CRYPTO_MAX_ALG_NAME]; | ||
38 | }; | ||
39 | |||
40 | static void cryptomgr_probe(void *data) | ||
41 | { | ||
42 | struct cryptomgr_param *param = data; | ||
43 | struct crypto_template *tmpl; | ||
44 | struct crypto_instance *inst; | ||
45 | |||
46 | tmpl = crypto_lookup_template(param->template); | ||
47 | if (!tmpl) | ||
48 | goto err; | ||
49 | |||
50 | inst = tmpl->alloc(¶m->alg, sizeof(param->alg)); | ||
51 | if (IS_ERR(inst)) | ||
52 | goto err; | ||
53 | else if ((err = crypto_register_instance(tmpl, inst))) { | ||
54 | tmpl->free(inst); | ||
55 | goto err; | ||
56 | } | ||
57 | |||
58 | crypto_tmpl_put(tmpl); | ||
59 | |||
60 | out: | ||
61 | kfree(param); | ||
62 | return; | ||
63 | |||
64 | err: | ||
65 | crypto_larval_error(param->larval.name); | ||
66 | goto out; | ||
67 | } | ||
68 | |||
69 | static int cryptomgr_schedule_probe(struct crypto_larval *larval) | ||
70 | { | ||
71 | struct cryptomgr_param *param; | ||
72 | const char *name = larval->alg.cra_name; | ||
73 | const char *p; | ||
74 | unsigned int len; | ||
75 | |||
76 | param = kmalloc(sizeof(*param), GFP_KERNEL); | ||
77 | if (!param) | ||
78 | goto err; | ||
79 | |||
80 | for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++) | ||
81 | ; | ||
82 | |||
83 | len = p - name; | ||
84 | if (!len || *p != '(') | ||
85 | goto err_free_param; | ||
86 | |||
87 | memcpy(param->template, name, len); | ||
88 | param->template[len] = 0; | ||
89 | |||
90 | name = p + 1; | ||
91 | for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++) | ||
92 | ; | ||
93 | |||
94 | len = p - name; | ||
95 | if (!len || *p != ')' || p[1]) | ||
96 | goto err_free_param; | ||
97 | |||
98 | param->alg.attr.rta_len = sizeof(param->alg); | ||
99 | param->alg.attr.rta_type = CRYPTOA_ALG; | ||
100 | memcpy(param->alg.data.name, name, len); | ||
101 | param->alg.data.name[len] = 0; | ||
102 | |||
103 | memcpy(param->larval.name, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME); | ||
104 | |||
105 | INIT_WORK(¶m->work, cryptomgr_probe, param); | ||
106 | schedule_work(¶m->work); | ||
107 | |||
108 | return NOTIFY_STOP; | ||
109 | |||
110 | err_free_param: | ||
111 | kfree(param); | ||
112 | err: | ||
113 | return NOTIFY_OK; | ||
114 | } | ||
115 | |||
116 | static int cryptomgr_notify(struct notifier_block *this, unsigned long msg, | ||
117 | void *data) | ||
118 | { | ||
119 | switch (msg) { | ||
120 | case CRYPTO_MSG_ALG_REQUEST: | ||
121 | return cryptomgr_schedule_probe(data); | ||
122 | } | ||
123 | |||
124 | return NOTIFY_DONE; | ||
125 | } | ||
126 | |||
127 | static struct notifier_block cryptomgr_notifier = { | ||
128 | .notifier_call = cryptomgr_notify, | ||
129 | }; | ||
130 | |||
131 | static int __init cryptomgr_init(void) | ||
132 | { | ||
133 | return crypto_register_notifier(&cryptomgr_notifier); | ||
134 | } | ||
135 | |||
136 | static void __exit cryptomgr_exit(void) | ||
137 | { | ||
138 | int err = crypto_unregister_notifier(&cryptomgr_notifier); | ||
139 | BUG_ON(err); | ||
140 | } | ||
141 | |||
142 | module_init(cryptomgr_init); | ||
143 | module_exit(cryptomgr_exit); | ||
144 | |||
145 | MODULE_LICENSE("GPL"); | ||
146 | MODULE_DESCRIPTION("Crypto Algorithm Manager"); | ||
diff --git a/include/linux/crypto.h b/include/linux/crypto.h index 3e3e95aff133..85f73c381913 100644 --- a/include/linux/crypto.h +++ b/include/linux/crypto.h | |||
@@ -260,6 +260,15 @@ struct crypto_tfm { | |||
260 | void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; | 260 | void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; |
261 | }; | 261 | }; |
262 | 262 | ||
263 | enum { | ||
264 | CRYPTOA_UNSPEC, | ||
265 | CRYPTOA_ALG, | ||
266 | }; | ||
267 | |||
268 | struct crypto_attr_alg { | ||
269 | char name[CRYPTO_MAX_ALG_NAME]; | ||
270 | }; | ||
271 | |||
263 | /* | 272 | /* |
264 | * Transform user interface. | 273 | * Transform user interface. |
265 | */ | 274 | */ |