blob: 232b37d8161368a01ab3f3e3f29cd0520fca750b [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 Xucce9e062006-08-21 21:08:13 +100024static inline int crypto_set_driver_name(struct crypto_alg *alg)
25{
26 static const char suffix[] = "-generic";
27 char *driver_name = alg->cra_driver_name;
28 int len;
29
30 if (*driver_name)
31 return 0;
32
33 len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
34 if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
35 return -ENAMETOOLONG;
36
37 memcpy(driver_name + len, suffix, sizeof(suffix));
38 return 0;
39}
40
Herbert Xu4cc77202006-08-06 21:16:34 +100041static int crypto_check_alg(struct crypto_alg *alg)
Herbert Xucce9e062006-08-21 21:08:13 +100042{
Herbert Xucce9e062006-08-21 21:08:13 +100043 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
44 return -EINVAL;
45
46 if (alg->cra_alignmask & alg->cra_blocksize)
47 return -EINVAL;
48
49 if (alg->cra_blocksize > PAGE_SIZE / 8)
50 return -EINVAL;
51
52 if (alg->cra_priority < 0)
53 return -EINVAL;
Herbert Xucce9e062006-08-21 21:08:13 +100054
Herbert Xu4cc77202006-08-06 21:16:34 +100055 return crypto_set_driver_name(alg);
56}
57
58static int __crypto_register_alg(struct crypto_alg *alg)
59{
60 struct crypto_alg *q;
61 int ret = -EEXIST;
62
Herbert Xucce9e062006-08-21 21:08:13 +100063 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xu4cc77202006-08-06 21:16:34 +100064 if (q == alg)
Herbert Xucce9e062006-08-21 21:08:13 +100065 goto out;
Herbert Xucce9e062006-08-21 21:08:13 +100066 }
67
68 list_add(&alg->cra_list, &crypto_alg_list);
69 atomic_set(&alg->cra_refcnt, 1);
Herbert Xu4cc77202006-08-06 21:16:34 +100070 ret = 0;
Herbert Xucce9e062006-08-21 21:08:13 +100071out:
Herbert Xucce9e062006-08-21 21:08:13 +100072 return ret;
73}
Herbert Xu4cc77202006-08-06 21:16:34 +100074
75int crypto_register_alg(struct crypto_alg *alg)
76{
77 int err;
78
79 err = crypto_check_alg(alg);
80 if (err)
81 return err;
82
83 down_write(&crypto_alg_sem);
84 err = __crypto_register_alg(alg);
85 up_write(&crypto_alg_sem);
86
87 return err;
88}
Herbert Xucce9e062006-08-21 21:08:13 +100089EXPORT_SYMBOL_GPL(crypto_register_alg);
90
91int crypto_unregister_alg(struct crypto_alg *alg)
92{
93 int ret = -ENOENT;
Herbert Xucce9e062006-08-21 21:08:13 +100094
95 down_write(&crypto_alg_sem);
Herbert Xu4cc77202006-08-06 21:16:34 +100096 if (likely(!list_empty(&alg->cra_list))) {
97 list_del_init(&alg->cra_list);
98 ret = 0;
Herbert Xucce9e062006-08-21 21:08:13 +100099 }
Herbert Xucce9e062006-08-21 21:08:13 +1000100 up_write(&crypto_alg_sem);
101
102 if (ret)
103 return ret;
104
105 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
106 if (alg->cra_destroy)
107 alg->cra_destroy(alg);
108
109 return 0;
110}
111EXPORT_SYMBOL_GPL(crypto_unregister_alg);
112
Herbert Xu4cc77202006-08-06 21:16:34 +1000113int crypto_register_template(struct crypto_template *tmpl)
114{
115 struct crypto_template *q;
116 int err = -EEXIST;
117
118 down_write(&crypto_alg_sem);
119
120 list_for_each_entry(q, &crypto_template_list, list) {
121 if (q == tmpl)
122 goto out;
123 }
124
125 list_add(&tmpl->list, &crypto_template_list);
126 err = 0;
127out:
128 up_write(&crypto_alg_sem);
129 return err;
130}
131EXPORT_SYMBOL_GPL(crypto_register_template);
132
133void crypto_unregister_template(struct crypto_template *tmpl)
134{
135 struct crypto_instance *inst;
136 struct hlist_node *p, *n;
137 struct hlist_head *list;
138
139 down_write(&crypto_alg_sem);
140
141 BUG_ON(list_empty(&tmpl->list));
142 list_del_init(&tmpl->list);
143
144 list = &tmpl->instances;
145 hlist_for_each_entry(inst, p, list, list) {
146 BUG_ON(list_empty(&inst->alg.cra_list));
147 list_del_init(&inst->alg.cra_list);
148 }
149
150 up_write(&crypto_alg_sem);
151
152 hlist_for_each_entry_safe(inst, p, n, list, list) {
153 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
154 tmpl->free(inst);
155 }
156}
157EXPORT_SYMBOL_GPL(crypto_unregister_template);
158
159static struct crypto_template *__crypto_lookup_template(const char *name)
160{
161 struct crypto_template *q, *tmpl = NULL;
162
163 down_read(&crypto_alg_sem);
164 list_for_each_entry(q, &crypto_template_list, list) {
165 if (strcmp(q->name, name))
166 continue;
167 if (unlikely(!crypto_tmpl_get(q)))
168 continue;
169
170 tmpl = q;
171 break;
172 }
173 up_read(&crypto_alg_sem);
174
175 return tmpl;
176}
177
178struct crypto_template *crypto_lookup_template(const char *name)
179{
180 return try_then_request_module(__crypto_lookup_template(name), name);
181}
182EXPORT_SYMBOL_GPL(crypto_lookup_template);
183
184int crypto_register_instance(struct crypto_template *tmpl,
185 struct crypto_instance *inst)
186{
187 int err = -EINVAL;
188
189 if (inst->alg.cra_destroy)
190 goto err;
191
192 err = crypto_check_alg(&inst->alg);
193 if (err)
194 goto err;
195
196 inst->alg.cra_module = tmpl->module;
197
198 down_write(&crypto_alg_sem);
199
200 err = __crypto_register_alg(&inst->alg);
201 if (err)
202 goto unlock;
203
204 hlist_add_head(&inst->list, &tmpl->instances);
205 inst->tmpl = tmpl;
206
207unlock:
208 up_write(&crypto_alg_sem);
209
210err:
211 return err;
212}
213EXPORT_SYMBOL_GPL(crypto_register_instance);
214
Herbert Xucce9e062006-08-21 21:08:13 +1000215static int __init crypto_algapi_init(void)
216{
217 crypto_init_proc();
218 return 0;
219}
220
221static void __exit crypto_algapi_exit(void)
222{
223 crypto_exit_proc();
224}
225
226module_init(crypto_algapi_init);
227module_exit(crypto_algapi_exit);
228
229MODULE_LICENSE("GPL");
230MODULE_DESCRIPTION("Cryptographic algorithms API");