blob: 491205e11cbe4bf1316c25544385abda90b5590f [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
Herbert Xu6bfd4802006-09-21 11:39:29 +100013#include <linux/err.h>
Herbert Xucce9e062006-08-21 21:08:13 +100014#include <linux/errno.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
Herbert Xu4cc77202006-08-06 21:16:34 +100017#include <linux/list.h>
Herbert Xucce9e062006-08-21 21:08:13 +100018#include <linux/module.h>
Herbert Xu7fed0bf2006-08-06 23:10:45 +100019#include <linux/rtnetlink.h>
Herbert Xucce9e062006-08-21 21:08:13 +100020#include <linux/string.h>
21
22#include "internal.h"
23
Herbert Xu4cc77202006-08-06 21:16:34 +100024static LIST_HEAD(crypto_template_list);
25
Herbert Xu492e2b62006-09-21 11:35:17 +100026void crypto_larval_error(const char *name, u32 type, u32 mask)
Herbert Xu28259822006-08-06 21:23:26 +100027{
28 struct crypto_alg *alg;
29
30 down_read(&crypto_alg_sem);
Herbert Xu492e2b62006-09-21 11:35:17 +100031 alg = __crypto_alg_lookup(name, type, mask);
Herbert Xu28259822006-08-06 21:23:26 +100032 up_read(&crypto_alg_sem);
33
34 if (alg) {
35 if (crypto_is_larval(alg)) {
36 struct crypto_larval *larval = (void *)alg;
37 complete(&larval->completion);
38 }
39 crypto_mod_put(alg);
40 }
41}
42EXPORT_SYMBOL_GPL(crypto_larval_error);
43
Herbert Xucce9e062006-08-21 21:08:13 +100044static inline int crypto_set_driver_name(struct crypto_alg *alg)
45{
46 static const char suffix[] = "-generic";
47 char *driver_name = alg->cra_driver_name;
48 int len;
49
50 if (*driver_name)
51 return 0;
52
53 len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
54 if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
55 return -ENAMETOOLONG;
56
57 memcpy(driver_name + len, suffix, sizeof(suffix));
58 return 0;
59}
60
Herbert Xu4cc77202006-08-06 21:16:34 +100061static int crypto_check_alg(struct crypto_alg *alg)
Herbert Xucce9e062006-08-21 21:08:13 +100062{
Herbert Xucce9e062006-08-21 21:08:13 +100063 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
64 return -EINVAL;
65
66 if (alg->cra_alignmask & alg->cra_blocksize)
67 return -EINVAL;
68
69 if (alg->cra_blocksize > PAGE_SIZE / 8)
70 return -EINVAL;
71
72 if (alg->cra_priority < 0)
73 return -EINVAL;
Herbert Xucce9e062006-08-21 21:08:13 +100074
Herbert Xu4cc77202006-08-06 21:16:34 +100075 return crypto_set_driver_name(alg);
76}
77
Herbert Xu6bfd4802006-09-21 11:39:29 +100078static void crypto_destroy_instance(struct crypto_alg *alg)
79{
80 struct crypto_instance *inst = (void *)alg;
81 struct crypto_template *tmpl = inst->tmpl;
82
83 tmpl->free(inst);
84 crypto_tmpl_put(tmpl);
85}
86
87static void crypto_remove_spawns(struct list_head *spawns,
88 struct list_head *list)
89{
90 struct crypto_spawn *spawn, *n;
91
92 list_for_each_entry_safe(spawn, n, spawns, list) {
93 struct crypto_instance *inst = spawn->inst;
94 struct crypto_template *tmpl = inst->tmpl;
95
96 list_del_init(&spawn->list);
97 spawn->alg = NULL;
98
99 if (crypto_is_dead(&inst->alg))
100 continue;
101
102 inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
103 if (!tmpl || !crypto_tmpl_get(tmpl))
104 continue;
105
106 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
107 list_move(&inst->alg.cra_list, list);
108 hlist_del(&inst->list);
109 inst->alg.cra_destroy = crypto_destroy_instance;
110
111 if (!list_empty(&inst->alg.cra_users)) {
112 if (&n->list == spawns)
113 n = list_entry(inst->alg.cra_users.next,
114 typeof(*n), list);
115 __list_splice(&inst->alg.cra_users, spawns->prev);
116 }
117 }
118}
119
120static int __crypto_register_alg(struct crypto_alg *alg,
121 struct list_head *list)
Herbert Xu4cc77202006-08-06 21:16:34 +1000122{
123 struct crypto_alg *q;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000124 int ret = -EAGAIN;
125
126 if (crypto_is_dead(alg))
127 goto out;
128
129 INIT_LIST_HEAD(&alg->cra_users);
130
131 ret = -EEXIST;
Herbert Xu4cc77202006-08-06 21:16:34 +1000132
Herbert Xu28259822006-08-06 21:23:26 +1000133 atomic_set(&alg->cra_refcnt, 1);
Herbert Xucce9e062006-08-21 21:08:13 +1000134 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xu4cc77202006-08-06 21:16:34 +1000135 if (q == alg)
Herbert Xucce9e062006-08-21 21:08:13 +1000136 goto out;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000137
138 if (crypto_is_moribund(q))
139 continue;
140
141 if (crypto_is_larval(q)) {
Herbert Xu28259822006-08-06 21:23:26 +1000142 struct crypto_larval *larval = (void *)q;
143
Herbert Xu6bfd4802006-09-21 11:39:29 +1000144 if (strcmp(alg->cra_name, q->cra_name) &&
145 strcmp(alg->cra_driver_name, q->cra_name))
146 continue;
147
148 if (larval->adult)
149 continue;
Herbert Xu492e2b62006-09-21 11:35:17 +1000150 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
151 continue;
Herbert Xu28259822006-08-06 21:23:26 +1000152 if (!crypto_mod_get(alg))
153 continue;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000154
Herbert Xu28259822006-08-06 21:23:26 +1000155 larval->adult = alg;
156 complete(&larval->completion);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000157 continue;
Herbert Xu28259822006-08-06 21:23:26 +1000158 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000159
160 if (strcmp(alg->cra_name, q->cra_name))
161 continue;
162
163 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
164 q->cra_priority > alg->cra_priority)
165 continue;
166
167 crypto_remove_spawns(&q->cra_users, list);
Herbert Xucce9e062006-08-21 21:08:13 +1000168 }
169
170 list_add(&alg->cra_list, &crypto_alg_list);
Herbert Xu28259822006-08-06 21:23:26 +1000171
172 crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg);
Herbert Xu4cc77202006-08-06 21:16:34 +1000173 ret = 0;
Herbert Xu28259822006-08-06 21:23:26 +1000174
Herbert Xucce9e062006-08-21 21:08:13 +1000175out:
Herbert Xucce9e062006-08-21 21:08:13 +1000176 return ret;
177}
Herbert Xu4cc77202006-08-06 21:16:34 +1000178
Herbert Xu6bfd4802006-09-21 11:39:29 +1000179static void crypto_remove_final(struct list_head *list)
180{
181 struct crypto_alg *alg;
182 struct crypto_alg *n;
183
184 list_for_each_entry_safe(alg, n, list, cra_list) {
185 list_del_init(&alg->cra_list);
186 crypto_alg_put(alg);
187 }
188}
189
Herbert Xu4cc77202006-08-06 21:16:34 +1000190int crypto_register_alg(struct crypto_alg *alg)
191{
Herbert Xu6bfd4802006-09-21 11:39:29 +1000192 LIST_HEAD(list);
Herbert Xu4cc77202006-08-06 21:16:34 +1000193 int err;
194
195 err = crypto_check_alg(alg);
196 if (err)
197 return err;
198
199 down_write(&crypto_alg_sem);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000200 err = __crypto_register_alg(alg, &list);
Herbert Xu4cc77202006-08-06 21:16:34 +1000201 up_write(&crypto_alg_sem);
202
Herbert Xu6bfd4802006-09-21 11:39:29 +1000203 crypto_remove_final(&list);
Herbert Xu4cc77202006-08-06 21:16:34 +1000204 return err;
205}
Herbert Xucce9e062006-08-21 21:08:13 +1000206EXPORT_SYMBOL_GPL(crypto_register_alg);
207
Herbert Xu6bfd4802006-09-21 11:39:29 +1000208static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
209{
210 if (unlikely(list_empty(&alg->cra_list)))
211 return -ENOENT;
212
213 alg->cra_flags |= CRYPTO_ALG_DEAD;
214
215 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
216 list_del_init(&alg->cra_list);
217 crypto_remove_spawns(&alg->cra_users, list);
218
219 return 0;
220}
221
Herbert Xucce9e062006-08-21 21:08:13 +1000222int crypto_unregister_alg(struct crypto_alg *alg)
223{
Herbert Xu6bfd4802006-09-21 11:39:29 +1000224 int ret;
225 LIST_HEAD(list);
Herbert Xucce9e062006-08-21 21:08:13 +1000226
227 down_write(&crypto_alg_sem);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000228 ret = crypto_remove_alg(alg, &list);
Herbert Xucce9e062006-08-21 21:08:13 +1000229 up_write(&crypto_alg_sem);
230
231 if (ret)
232 return ret;
233
234 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
235 if (alg->cra_destroy)
236 alg->cra_destroy(alg);
237
Herbert Xu6bfd4802006-09-21 11:39:29 +1000238 crypto_remove_final(&list);
Herbert Xucce9e062006-08-21 21:08:13 +1000239 return 0;
240}
241EXPORT_SYMBOL_GPL(crypto_unregister_alg);
242
Herbert Xu4cc77202006-08-06 21:16:34 +1000243int crypto_register_template(struct crypto_template *tmpl)
244{
245 struct crypto_template *q;
246 int err = -EEXIST;
247
248 down_write(&crypto_alg_sem);
249
250 list_for_each_entry(q, &crypto_template_list, list) {
251 if (q == tmpl)
252 goto out;
253 }
254
255 list_add(&tmpl->list, &crypto_template_list);
Herbert Xu28259822006-08-06 21:23:26 +1000256 crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
Herbert Xu4cc77202006-08-06 21:16:34 +1000257 err = 0;
258out:
259 up_write(&crypto_alg_sem);
260 return err;
261}
262EXPORT_SYMBOL_GPL(crypto_register_template);
263
264void crypto_unregister_template(struct crypto_template *tmpl)
265{
266 struct crypto_instance *inst;
267 struct hlist_node *p, *n;
268 struct hlist_head *list;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000269 LIST_HEAD(users);
Herbert Xu4cc77202006-08-06 21:16:34 +1000270
271 down_write(&crypto_alg_sem);
272
273 BUG_ON(list_empty(&tmpl->list));
274 list_del_init(&tmpl->list);
275
276 list = &tmpl->instances;
277 hlist_for_each_entry(inst, p, list, list) {
Herbert Xu6bfd4802006-09-21 11:39:29 +1000278 int err = crypto_remove_alg(&inst->alg, &users);
279 BUG_ON(err);
Herbert Xu4cc77202006-08-06 21:16:34 +1000280 }
281
Herbert Xu28259822006-08-06 21:23:26 +1000282 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
283
Herbert Xu4cc77202006-08-06 21:16:34 +1000284 up_write(&crypto_alg_sem);
285
286 hlist_for_each_entry_safe(inst, p, n, list, list) {
287 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
288 tmpl->free(inst);
289 }
Herbert Xu6bfd4802006-09-21 11:39:29 +1000290 crypto_remove_final(&users);
Herbert Xu4cc77202006-08-06 21:16:34 +1000291}
292EXPORT_SYMBOL_GPL(crypto_unregister_template);
293
294static struct crypto_template *__crypto_lookup_template(const char *name)
295{
296 struct crypto_template *q, *tmpl = NULL;
297
298 down_read(&crypto_alg_sem);
299 list_for_each_entry(q, &crypto_template_list, list) {
300 if (strcmp(q->name, name))
301 continue;
302 if (unlikely(!crypto_tmpl_get(q)))
303 continue;
304
305 tmpl = q;
306 break;
307 }
308 up_read(&crypto_alg_sem);
309
310 return tmpl;
311}
312
313struct crypto_template *crypto_lookup_template(const char *name)
314{
315 return try_then_request_module(__crypto_lookup_template(name), name);
316}
317EXPORT_SYMBOL_GPL(crypto_lookup_template);
318
319int crypto_register_instance(struct crypto_template *tmpl,
320 struct crypto_instance *inst)
321{
Herbert Xu6bfd4802006-09-21 11:39:29 +1000322 LIST_HEAD(list);
Herbert Xu4cc77202006-08-06 21:16:34 +1000323 int err = -EINVAL;
324
325 if (inst->alg.cra_destroy)
326 goto err;
327
328 err = crypto_check_alg(&inst->alg);
329 if (err)
330 goto err;
331
332 inst->alg.cra_module = tmpl->module;
333
334 down_write(&crypto_alg_sem);
335
Herbert Xu6bfd4802006-09-21 11:39:29 +1000336 err = __crypto_register_alg(&inst->alg, &list);
Herbert Xu4cc77202006-08-06 21:16:34 +1000337 if (err)
338 goto unlock;
339
340 hlist_add_head(&inst->list, &tmpl->instances);
341 inst->tmpl = tmpl;
342
343unlock:
344 up_write(&crypto_alg_sem);
345
Herbert Xu6bfd4802006-09-21 11:39:29 +1000346 crypto_remove_final(&list);
347
Herbert Xu4cc77202006-08-06 21:16:34 +1000348err:
349 return err;
350}
351EXPORT_SYMBOL_GPL(crypto_register_instance);
352
Herbert Xu6bfd4802006-09-21 11:39:29 +1000353int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
354 struct crypto_instance *inst)
355{
356 int err = -EAGAIN;
357
358 spawn->inst = inst;
359
360 down_write(&crypto_alg_sem);
361 if (!crypto_is_moribund(alg)) {
362 list_add(&spawn->list, &alg->cra_users);
363 spawn->alg = alg;
364 err = 0;
365 }
366 up_write(&crypto_alg_sem);
367
368 return err;
369}
370EXPORT_SYMBOL_GPL(crypto_init_spawn);
371
372void crypto_drop_spawn(struct crypto_spawn *spawn)
373{
374 down_write(&crypto_alg_sem);
375 list_del(&spawn->list);
376 up_write(&crypto_alg_sem);
377}
378EXPORT_SYMBOL_GPL(crypto_drop_spawn);
379
Herbert Xu2e306ee2006-12-17 10:05:58 +1100380struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
381 u32 mask)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000382{
383 struct crypto_alg *alg;
384 struct crypto_alg *alg2;
385 struct crypto_tfm *tfm;
386
387 down_read(&crypto_alg_sem);
388 alg = spawn->alg;
389 alg2 = alg;
390 if (alg2)
391 alg2 = crypto_mod_get(alg2);
392 up_read(&crypto_alg_sem);
393
394 if (!alg2) {
395 if (alg)
396 crypto_shoot_alg(alg);
397 return ERR_PTR(-EAGAIN);
398 }
399
Herbert Xu2e306ee2006-12-17 10:05:58 +1100400 tfm = ERR_PTR(-EINVAL);
401 if (unlikely((alg->cra_flags ^ type) & mask))
402 goto out_put_alg;
403
Herbert Xu27d2a332007-01-24 20:50:26 +1100404 tfm = __crypto_alloc_tfm(alg, type, mask);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000405 if (IS_ERR(tfm))
Herbert Xu2e306ee2006-12-17 10:05:58 +1100406 goto out_put_alg;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000407
408 return tfm;
Herbert Xu2e306ee2006-12-17 10:05:58 +1100409
410out_put_alg:
411 crypto_mod_put(alg);
412 return tfm;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000413}
414EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
415
Herbert Xu28259822006-08-06 21:23:26 +1000416int crypto_register_notifier(struct notifier_block *nb)
417{
418 return blocking_notifier_chain_register(&crypto_chain, nb);
419}
420EXPORT_SYMBOL_GPL(crypto_register_notifier);
421
422int crypto_unregister_notifier(struct notifier_block *nb)
423{
424 return blocking_notifier_chain_unregister(&crypto_chain, nb);
425}
426EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
427
Herbert Xuebc610e2007-01-01 18:37:02 +1100428struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000429{
Herbert Xuebc610e2007-01-01 18:37:02 +1100430 struct rtattr *rta = tb[CRYPTOA_TYPE - 1];
431 struct crypto_attr_type *algt;
432
433 if (!rta)
434 return ERR_PTR(-ENOENT);
435 if (RTA_PAYLOAD(rta) < sizeof(*algt))
436 return ERR_PTR(-EINVAL);
437
438 algt = RTA_DATA(rta);
439
440 return algt;
441}
442EXPORT_SYMBOL_GPL(crypto_get_attr_type);
443
444int crypto_check_attr_type(struct rtattr **tb, u32 type)
445{
446 struct crypto_attr_type *algt;
447
448 algt = crypto_get_attr_type(tb);
449 if (IS_ERR(algt))
450 return PTR_ERR(algt);
451
452 if ((algt->type ^ type) & algt->mask)
453 return -EINVAL;
454
455 return 0;
456}
457EXPORT_SYMBOL_GPL(crypto_check_attr_type);
458
459struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb, u32 type, u32 mask)
460{
461 struct rtattr *rta = tb[CRYPTOA_ALG - 1];
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000462 struct crypto_attr_alg *alga;
463
Herbert Xuebc610e2007-01-01 18:37:02 +1100464 if (!rta)
465 return ERR_PTR(-ENOENT);
466 if (RTA_PAYLOAD(rta) < sizeof(*alga))
Herbert Xu7fed0bf2006-08-06 23:10:45 +1000467 return ERR_PTR(-EINVAL);
468
469 alga = RTA_DATA(rta);
470 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
471
472 return crypto_alg_mod_lookup(alga->name, type, mask);
473}
474EXPORT_SYMBOL_GPL(crypto_get_attr_alg);
475
476struct crypto_instance *crypto_alloc_instance(const char *name,
477 struct crypto_alg *alg)
478{
479 struct crypto_instance *inst;
480 struct crypto_spawn *spawn;
481 int err;
482
483 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
484 if (!inst)
485 return ERR_PTR(-ENOMEM);
486
487 err = -ENAMETOOLONG;
488 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
489 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
490 goto err_free_inst;
491
492 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
493 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
494 goto err_free_inst;
495
496 spawn = crypto_instance_ctx(inst);
497 err = crypto_init_spawn(spawn, alg, inst);
498
499 if (err)
500 goto err_free_inst;
501
502 return inst;
503
504err_free_inst:
505 kfree(inst);
506 return ERR_PTR(err);
507}
508EXPORT_SYMBOL_GPL(crypto_alloc_instance);
509
Herbert Xucce9e062006-08-21 21:08:13 +1000510static int __init crypto_algapi_init(void)
511{
512 crypto_init_proc();
513 return 0;
514}
515
516static void __exit crypto_algapi_exit(void)
517{
518 crypto_exit_proc();
519}
520
521module_init(crypto_algapi_init);
522module_exit(crypto_algapi_exit);
523
524MODULE_LICENSE("GPL");
525MODULE_DESCRIPTION("Cryptographic algorithms API");