blob: f0df85fc1f50b720c1eafe443a77f36aac61195b [file] [log] [blame]
Herbert Xucce9e062006-08-21 21:08:13 +10001/*
2 * Cryptographic API for algorithms (i.e., low-level API).
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/errno.h>
14#include <linux/init.h>
15#include <linux/kernel.h>
Herbert Xu4cc77202006-08-06 21:16:34 +100016#include <linux/list.h>
Herbert Xucce9e062006-08-21 21:08:13 +100017#include <linux/module.h>
18#include <linux/string.h>
19
20#include "internal.h"
21
Herbert Xu4cc77202006-08-06 21:16:34 +100022static LIST_HEAD(crypto_template_list);
23
Herbert Xu28259822006-08-06 21:23:26 +100024void crypto_larval_error(const char *name)
25{
26 struct crypto_alg *alg;
27
28 down_read(&crypto_alg_sem);
29 alg = __crypto_alg_lookup(name);
30 up_read(&crypto_alg_sem);
31
32 if (alg) {
33 if (crypto_is_larval(alg)) {
34 struct crypto_larval *larval = (void *)alg;
35 complete(&larval->completion);
36 }
37 crypto_mod_put(alg);
38 }
39}
40EXPORT_SYMBOL_GPL(crypto_larval_error);
41
Herbert Xucce9e062006-08-21 21:08:13 +100042static inline int crypto_set_driver_name(struct crypto_alg *alg)
43{
44 static const char suffix[] = "-generic";
45 char *driver_name = alg->cra_driver_name;
46 int len;
47
48 if (*driver_name)
49 return 0;
50
51 len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
52 if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
53 return -ENAMETOOLONG;
54
55 memcpy(driver_name + len, suffix, sizeof(suffix));
56 return 0;
57}
58
Herbert Xu4cc77202006-08-06 21:16:34 +100059static int crypto_check_alg(struct crypto_alg *alg)
Herbert Xucce9e062006-08-21 21:08:13 +100060{
Herbert Xucce9e062006-08-21 21:08:13 +100061 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
62 return -EINVAL;
63
64 if (alg->cra_alignmask & alg->cra_blocksize)
65 return -EINVAL;
66
67 if (alg->cra_blocksize > PAGE_SIZE / 8)
68 return -EINVAL;
69
70 if (alg->cra_priority < 0)
71 return -EINVAL;
Herbert Xucce9e062006-08-21 21:08:13 +100072
Herbert Xu4cc77202006-08-06 21:16:34 +100073 return crypto_set_driver_name(alg);
74}
75
76static int __crypto_register_alg(struct crypto_alg *alg)
77{
78 struct crypto_alg *q;
79 int ret = -EEXIST;
80
Herbert Xu28259822006-08-06 21:23:26 +100081 atomic_set(&alg->cra_refcnt, 1);
Herbert Xucce9e062006-08-21 21:08:13 +100082 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xu4cc77202006-08-06 21:16:34 +100083 if (q == alg)
Herbert Xucce9e062006-08-21 21:08:13 +100084 goto out;
Herbert Xu28259822006-08-06 21:23:26 +100085 if (crypto_is_larval(q) &&
86 (!strcmp(alg->cra_name, q->cra_name) ||
87 !strcmp(alg->cra_driver_name, q->cra_name))) {
88 struct crypto_larval *larval = (void *)q;
89
90 if (!crypto_mod_get(alg))
91 continue;
92 larval->adult = alg;
93 complete(&larval->completion);
94 }
Herbert Xucce9e062006-08-21 21:08:13 +100095 }
96
97 list_add(&alg->cra_list, &crypto_alg_list);
Herbert Xu28259822006-08-06 21:23:26 +100098
99 crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg);
Herbert Xu4cc77202006-08-06 21:16:34 +1000100 ret = 0;
Herbert Xu28259822006-08-06 21:23:26 +1000101
Herbert Xucce9e062006-08-21 21:08:13 +1000102out:
Herbert Xucce9e062006-08-21 21:08:13 +1000103 return ret;
104}
Herbert Xu4cc77202006-08-06 21:16:34 +1000105
106int crypto_register_alg(struct crypto_alg *alg)
107{
108 int err;
109
110 err = crypto_check_alg(alg);
111 if (err)
112 return err;
113
114 down_write(&crypto_alg_sem);
115 err = __crypto_register_alg(alg);
116 up_write(&crypto_alg_sem);
117
118 return err;
119}
Herbert Xucce9e062006-08-21 21:08:13 +1000120EXPORT_SYMBOL_GPL(crypto_register_alg);
121
122int crypto_unregister_alg(struct crypto_alg *alg)
123{
124 int ret = -ENOENT;
Herbert Xucce9e062006-08-21 21:08:13 +1000125
126 down_write(&crypto_alg_sem);
Herbert Xu4cc77202006-08-06 21:16:34 +1000127 if (likely(!list_empty(&alg->cra_list))) {
128 list_del_init(&alg->cra_list);
129 ret = 0;
Herbert Xucce9e062006-08-21 21:08:13 +1000130 }
Herbert Xu28259822006-08-06 21:23:26 +1000131 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
Herbert Xucce9e062006-08-21 21:08:13 +1000132 up_write(&crypto_alg_sem);
133
134 if (ret)
135 return ret;
136
137 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
138 if (alg->cra_destroy)
139 alg->cra_destroy(alg);
140
141 return 0;
142}
143EXPORT_SYMBOL_GPL(crypto_unregister_alg);
144
Herbert Xu4cc77202006-08-06 21:16:34 +1000145int crypto_register_template(struct crypto_template *tmpl)
146{
147 struct crypto_template *q;
148 int err = -EEXIST;
149
150 down_write(&crypto_alg_sem);
151
152 list_for_each_entry(q, &crypto_template_list, list) {
153 if (q == tmpl)
154 goto out;
155 }
156
157 list_add(&tmpl->list, &crypto_template_list);
Herbert Xu28259822006-08-06 21:23:26 +1000158 crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
Herbert Xu4cc77202006-08-06 21:16:34 +1000159 err = 0;
160out:
161 up_write(&crypto_alg_sem);
162 return err;
163}
164EXPORT_SYMBOL_GPL(crypto_register_template);
165
166void crypto_unregister_template(struct crypto_template *tmpl)
167{
168 struct crypto_instance *inst;
169 struct hlist_node *p, *n;
170 struct hlist_head *list;
171
172 down_write(&crypto_alg_sem);
173
174 BUG_ON(list_empty(&tmpl->list));
175 list_del_init(&tmpl->list);
176
177 list = &tmpl->instances;
178 hlist_for_each_entry(inst, p, list, list) {
179 BUG_ON(list_empty(&inst->alg.cra_list));
180 list_del_init(&inst->alg.cra_list);
Herbert Xu28259822006-08-06 21:23:26 +1000181 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
Herbert Xu4cc77202006-08-06 21:16:34 +1000182 }
183
Herbert Xu28259822006-08-06 21:23:26 +1000184 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
185
Herbert Xu4cc77202006-08-06 21:16:34 +1000186 up_write(&crypto_alg_sem);
187
188 hlist_for_each_entry_safe(inst, p, n, list, list) {
189 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
190 tmpl->free(inst);
191 }
192}
193EXPORT_SYMBOL_GPL(crypto_unregister_template);
194
195static struct crypto_template *__crypto_lookup_template(const char *name)
196{
197 struct crypto_template *q, *tmpl = NULL;
198
199 down_read(&crypto_alg_sem);
200 list_for_each_entry(q, &crypto_template_list, list) {
201 if (strcmp(q->name, name))
202 continue;
203 if (unlikely(!crypto_tmpl_get(q)))
204 continue;
205
206 tmpl = q;
207 break;
208 }
209 up_read(&crypto_alg_sem);
210
211 return tmpl;
212}
213
214struct crypto_template *crypto_lookup_template(const char *name)
215{
216 return try_then_request_module(__crypto_lookup_template(name), name);
217}
218EXPORT_SYMBOL_GPL(crypto_lookup_template);
219
220int crypto_register_instance(struct crypto_template *tmpl,
221 struct crypto_instance *inst)
222{
223 int err = -EINVAL;
224
225 if (inst->alg.cra_destroy)
226 goto err;
227
228 err = crypto_check_alg(&inst->alg);
229 if (err)
230 goto err;
231
232 inst->alg.cra_module = tmpl->module;
233
234 down_write(&crypto_alg_sem);
235
236 err = __crypto_register_alg(&inst->alg);
237 if (err)
238 goto unlock;
239
240 hlist_add_head(&inst->list, &tmpl->instances);
241 inst->tmpl = tmpl;
242
243unlock:
244 up_write(&crypto_alg_sem);
245
246err:
247 return err;
248}
249EXPORT_SYMBOL_GPL(crypto_register_instance);
250
Herbert Xu28259822006-08-06 21:23:26 +1000251int crypto_register_notifier(struct notifier_block *nb)
252{
253 return blocking_notifier_chain_register(&crypto_chain, nb);
254}
255EXPORT_SYMBOL_GPL(crypto_register_notifier);
256
257int crypto_unregister_notifier(struct notifier_block *nb)
258{
259 return blocking_notifier_chain_unregister(&crypto_chain, nb);
260}
261EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
262
Herbert Xucce9e062006-08-21 21:08:13 +1000263static int __init crypto_algapi_init(void)
264{
265 crypto_init_proc();
266 return 0;
267}
268
269static void __exit crypto_algapi_exit(void)
270{
271 crypto_exit_proc();
272}
273
274module_init(crypto_algapi_init);
275module_exit(crypto_algapi_exit);
276
277MODULE_LICENSE("GPL");
278MODULE_DESCRIPTION("Cryptographic algorithms API");