2 * Cryptographic API for algorithms (i.e., low-level API).
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
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)
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/string.h>
24 static void crypto_remove_final(struct list_head *list);
26 static LIST_HEAD(crypto_template_list);
28 void crypto_larval_error(const char *name, u32 type, u32 mask)
30 struct crypto_alg *alg;
32 alg = crypto_alg_lookup(name, type, mask);
35 if (crypto_is_larval(alg)) {
36 struct crypto_larval *larval = (void *)alg;
37 complete_all(&larval->completion);
42 EXPORT_SYMBOL_GPL(crypto_larval_error);
44 static inline int crypto_set_driver_name(struct crypto_alg *alg)
46 static const char suffix[] = "-generic";
47 char *driver_name = alg->cra_driver_name;
53 len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
54 if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
57 memcpy(driver_name + len, suffix, sizeof(suffix));
61 static int crypto_check_alg(struct crypto_alg *alg)
63 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
66 if (alg->cra_blocksize > PAGE_SIZE / 8)
69 if (alg->cra_priority < 0)
72 return crypto_set_driver_name(alg);
75 static void crypto_destroy_instance(struct crypto_alg *alg)
77 struct crypto_instance *inst = (void *)alg;
78 struct crypto_template *tmpl = inst->tmpl;
81 crypto_tmpl_put(tmpl);
84 static void crypto_remove_spawn(struct crypto_spawn *spawn,
85 struct list_head *list,
86 struct list_head *secondary_spawns)
88 struct crypto_instance *inst = spawn->inst;
89 struct crypto_template *tmpl = inst->tmpl;
91 list_del_init(&spawn->list);
94 if (crypto_is_dead(&inst->alg))
97 inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
98 if (hlist_unhashed(&inst->list))
101 if (!tmpl || !crypto_tmpl_get(tmpl))
104 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
105 list_move(&inst->alg.cra_list, list);
106 hlist_del(&inst->list);
107 inst->alg.cra_destroy = crypto_destroy_instance;
109 list_splice(&inst->alg.cra_users, secondary_spawns);
112 static void crypto_remove_spawns(struct list_head *spawns,
113 struct list_head *list, u32 new_type)
115 struct crypto_spawn *spawn, *n;
116 LIST_HEAD(secondary_spawns);
118 list_for_each_entry_safe(spawn, n, spawns, list) {
119 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
122 crypto_remove_spawn(spawn, list, &secondary_spawns);
125 while (!list_empty(&secondary_spawns)) {
126 list_for_each_entry_safe(spawn, n, &secondary_spawns, list)
127 crypto_remove_spawn(spawn, list, &secondary_spawns);
131 static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
133 struct crypto_alg *q;
134 struct crypto_larval *larval;
137 if (crypto_is_dead(alg))
140 INIT_LIST_HEAD(&alg->cra_users);
143 alg->cra_flags &= ~CRYPTO_ALG_TESTED;
147 atomic_set(&alg->cra_refcnt, 1);
148 list_for_each_entry(q, &crypto_alg_list, cra_list) {
152 if (crypto_is_moribund(q))
155 if (crypto_is_larval(q)) {
156 if (!strcmp(alg->cra_driver_name, q->cra_driver_name))
161 if (!strcmp(q->cra_driver_name, alg->cra_name) ||
162 !strcmp(q->cra_name, alg->cra_driver_name))
166 larval = crypto_larval_alloc(alg->cra_name,
167 alg->cra_flags | CRYPTO_ALG_TESTED, 0);
172 larval->adult = crypto_mod_get(alg);
176 atomic_set(&larval->alg.cra_refcnt, 1);
177 memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
178 CRYPTO_MAX_ALG_NAME);
179 larval->alg.cra_priority = alg->cra_priority;
181 list_add(&alg->cra_list, &crypto_alg_list);
182 list_add(&larval->alg.cra_list, &crypto_alg_list);
190 larval = ERR_PTR(ret);
194 void crypto_alg_tested(const char *name, int err)
196 struct crypto_larval *test;
197 struct crypto_alg *alg;
198 struct crypto_alg *q;
201 down_write(&crypto_alg_sem);
202 list_for_each_entry(q, &crypto_alg_list, cra_list) {
203 if (crypto_is_moribund(q) || !crypto_is_larval(q))
206 test = (struct crypto_larval *)q;
208 if (!strcmp(q->cra_driver_name, name))
212 printk(KERN_ERR "alg: Unexpected test result for %s: %d\n", name, err);
216 q->cra_flags |= CRYPTO_ALG_DEAD;
218 if (err || list_empty(&alg->cra_list))
221 alg->cra_flags |= CRYPTO_ALG_TESTED;
223 list_for_each_entry(q, &crypto_alg_list, cra_list) {
227 if (crypto_is_moribund(q))
230 if (crypto_is_larval(q)) {
231 struct crypto_larval *larval = (void *)q;
234 * Check to see if either our generic name or
235 * specific name can satisfy the name requested
236 * by the larval entry q.
238 if (strcmp(alg->cra_name, q->cra_name) &&
239 strcmp(alg->cra_driver_name, q->cra_name))
244 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
246 if (!crypto_mod_get(alg))
250 complete_all(&larval->completion);
254 if (strcmp(alg->cra_name, q->cra_name))
257 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
258 q->cra_priority > alg->cra_priority)
261 crypto_remove_spawns(&q->cra_users, &list, alg->cra_flags);
265 complete_all(&test->completion);
268 up_write(&crypto_alg_sem);
270 crypto_remove_final(&list);
272 EXPORT_SYMBOL_GPL(crypto_alg_tested);
274 static void crypto_remove_final(struct list_head *list)
276 struct crypto_alg *alg;
277 struct crypto_alg *n;
279 list_for_each_entry_safe(alg, n, list, cra_list) {
280 list_del_init(&alg->cra_list);
285 static void crypto_wait_for_test(struct crypto_larval *larval)
289 err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
290 if (err != NOTIFY_STOP) {
291 if (WARN_ON(err != NOTIFY_DONE))
293 crypto_alg_tested(larval->alg.cra_driver_name, 0);
296 err = wait_for_completion_interruptible(&larval->completion);
300 crypto_larval_kill(&larval->alg);
303 int crypto_register_alg(struct crypto_alg *alg)
305 struct crypto_larval *larval;
308 err = crypto_check_alg(alg);
312 down_write(&crypto_alg_sem);
313 larval = __crypto_register_alg(alg);
314 up_write(&crypto_alg_sem);
317 return PTR_ERR(larval);
319 crypto_wait_for_test(larval);
322 EXPORT_SYMBOL_GPL(crypto_register_alg);
324 static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
326 if (unlikely(list_empty(&alg->cra_list)))
329 alg->cra_flags |= CRYPTO_ALG_DEAD;
331 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
332 list_del_init(&alg->cra_list);
333 crypto_remove_spawns(&alg->cra_users, list, alg->cra_flags);
338 int crypto_unregister_alg(struct crypto_alg *alg)
343 down_write(&crypto_alg_sem);
344 ret = crypto_remove_alg(alg, &list);
345 up_write(&crypto_alg_sem);
350 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
351 if (alg->cra_destroy)
352 alg->cra_destroy(alg);
354 crypto_remove_final(&list);
357 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
359 int crypto_register_template(struct crypto_template *tmpl)
361 struct crypto_template *q;
364 down_write(&crypto_alg_sem);
366 list_for_each_entry(q, &crypto_template_list, list) {
371 list_add(&tmpl->list, &crypto_template_list);
372 crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
375 up_write(&crypto_alg_sem);
378 EXPORT_SYMBOL_GPL(crypto_register_template);
380 void crypto_unregister_template(struct crypto_template *tmpl)
382 struct crypto_instance *inst;
383 struct hlist_node *p, *n;
384 struct hlist_head *list;
387 down_write(&crypto_alg_sem);
389 BUG_ON(list_empty(&tmpl->list));
390 list_del_init(&tmpl->list);
392 list = &tmpl->instances;
393 hlist_for_each_entry(inst, p, list, list) {
394 int err = crypto_remove_alg(&inst->alg, &users);
398 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
400 up_write(&crypto_alg_sem);
402 hlist_for_each_entry_safe(inst, p, n, list, list) {
403 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
406 crypto_remove_final(&users);
408 EXPORT_SYMBOL_GPL(crypto_unregister_template);
410 static struct crypto_template *__crypto_lookup_template(const char *name)
412 struct crypto_template *q, *tmpl = NULL;
414 down_read(&crypto_alg_sem);
415 list_for_each_entry(q, &crypto_template_list, list) {
416 if (strcmp(q->name, name))
418 if (unlikely(!crypto_tmpl_get(q)))
424 up_read(&crypto_alg_sem);
429 struct crypto_template *crypto_lookup_template(const char *name)
431 return try_then_request_module(__crypto_lookup_template(name), name);
433 EXPORT_SYMBOL_GPL(crypto_lookup_template);
435 int crypto_register_instance(struct crypto_template *tmpl,
436 struct crypto_instance *inst)
438 struct crypto_larval *larval;
441 err = crypto_check_alg(&inst->alg);
445 inst->alg.cra_module = tmpl->module;
447 down_write(&crypto_alg_sem);
449 larval = __crypto_register_alg(&inst->alg);
453 hlist_add_head(&inst->list, &tmpl->instances);
457 up_write(&crypto_alg_sem);
459 err = PTR_ERR(larval);
463 crypto_wait_for_test(larval);
469 EXPORT_SYMBOL_GPL(crypto_register_instance);
471 int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
472 struct crypto_instance *inst, u32 mask)
479 down_write(&crypto_alg_sem);
480 if (!crypto_is_moribund(alg)) {
481 list_add(&spawn->list, &alg->cra_users);
485 up_write(&crypto_alg_sem);
489 EXPORT_SYMBOL_GPL(crypto_init_spawn);
491 void crypto_drop_spawn(struct crypto_spawn *spawn)
493 down_write(&crypto_alg_sem);
494 list_del(&spawn->list);
495 up_write(&crypto_alg_sem);
497 EXPORT_SYMBOL_GPL(crypto_drop_spawn);
499 struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
502 struct crypto_alg *alg;
503 struct crypto_alg *alg2;
504 struct crypto_tfm *tfm;
506 down_read(&crypto_alg_sem);
510 alg2 = crypto_mod_get(alg2);
511 up_read(&crypto_alg_sem);
515 crypto_shoot_alg(alg);
516 return ERR_PTR(-EAGAIN);
519 tfm = ERR_PTR(-EINVAL);
520 if (unlikely((alg->cra_flags ^ type) & mask))
523 tfm = __crypto_alloc_tfm(alg, type, mask);
533 EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
535 int crypto_register_notifier(struct notifier_block *nb)
537 return blocking_notifier_chain_register(&crypto_chain, nb);
539 EXPORT_SYMBOL_GPL(crypto_register_notifier);
541 int crypto_unregister_notifier(struct notifier_block *nb)
543 return blocking_notifier_chain_unregister(&crypto_chain, nb);
545 EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
547 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
549 struct rtattr *rta = tb[0];
550 struct crypto_attr_type *algt;
553 return ERR_PTR(-ENOENT);
554 if (RTA_PAYLOAD(rta) < sizeof(*algt))
555 return ERR_PTR(-EINVAL);
556 if (rta->rta_type != CRYPTOA_TYPE)
557 return ERR_PTR(-EINVAL);
559 algt = RTA_DATA(rta);
563 EXPORT_SYMBOL_GPL(crypto_get_attr_type);
565 int crypto_check_attr_type(struct rtattr **tb, u32 type)
567 struct crypto_attr_type *algt;
569 algt = crypto_get_attr_type(tb);
571 return PTR_ERR(algt);
573 if ((algt->type ^ type) & algt->mask)
578 EXPORT_SYMBOL_GPL(crypto_check_attr_type);
580 const char *crypto_attr_alg_name(struct rtattr *rta)
582 struct crypto_attr_alg *alga;
585 return ERR_PTR(-ENOENT);
586 if (RTA_PAYLOAD(rta) < sizeof(*alga))
587 return ERR_PTR(-EINVAL);
588 if (rta->rta_type != CRYPTOA_ALG)
589 return ERR_PTR(-EINVAL);
591 alga = RTA_DATA(rta);
592 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
596 EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
598 struct crypto_alg *crypto_attr_alg(struct rtattr *rta, u32 type, u32 mask)
603 name = crypto_attr_alg_name(rta);
608 return crypto_alg_mod_lookup(name, type, mask);
610 EXPORT_SYMBOL_GPL(crypto_attr_alg);
612 int crypto_attr_u32(struct rtattr *rta, u32 *num)
614 struct crypto_attr_u32 *nu32;
618 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
620 if (rta->rta_type != CRYPTOA_U32)
623 nu32 = RTA_DATA(rta);
628 EXPORT_SYMBOL_GPL(crypto_attr_u32);
630 struct crypto_instance *crypto_alloc_instance(const char *name,
631 struct crypto_alg *alg)
633 struct crypto_instance *inst;
634 struct crypto_spawn *spawn;
637 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
639 return ERR_PTR(-ENOMEM);
642 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
643 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
646 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
647 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
650 spawn = crypto_instance_ctx(inst);
651 err = crypto_init_spawn(spawn, alg, inst,
652 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
663 EXPORT_SYMBOL_GPL(crypto_alloc_instance);
665 void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
667 INIT_LIST_HEAD(&queue->list);
668 queue->backlog = &queue->list;
670 queue->max_qlen = max_qlen;
672 EXPORT_SYMBOL_GPL(crypto_init_queue);
674 int crypto_enqueue_request(struct crypto_queue *queue,
675 struct crypto_async_request *request)
677 int err = -EINPROGRESS;
679 if (unlikely(queue->qlen >= queue->max_qlen)) {
681 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
683 if (queue->backlog == &queue->list)
684 queue->backlog = &request->list;
688 list_add_tail(&request->list, &queue->list);
693 EXPORT_SYMBOL_GPL(crypto_enqueue_request);
695 struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
697 struct list_head *request;
699 if (unlikely(!queue->qlen))
704 if (queue->backlog != &queue->list)
705 queue->backlog = queue->backlog->next;
707 request = queue->list.next;
710 return list_entry(request, struct crypto_async_request, list);
712 EXPORT_SYMBOL_GPL(crypto_dequeue_request);
714 int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
716 struct crypto_async_request *req;
718 list_for_each_entry(req, &queue->list, list) {
725 EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
727 static inline void crypto_inc_byte(u8 *a, unsigned int size)
732 for (; size; size--) {
740 void crypto_inc(u8 *a, unsigned int size)
742 __be32 *b = (__be32 *)(a + size);
745 for (; size >= 4; size -= 4) {
746 c = be32_to_cpu(*--b) + 1;
752 crypto_inc_byte(a, size);
754 EXPORT_SYMBOL_GPL(crypto_inc);
756 static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
762 void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
767 for (; size >= 4; size -= 4)
770 crypto_xor_byte((u8 *)a, (u8 *)b, size);
772 EXPORT_SYMBOL_GPL(crypto_xor);
774 static int __init crypto_algapi_init(void)
780 static void __exit crypto_algapi_exit(void)
785 module_init(crypto_algapi_init);
786 module_exit(crypto_algapi_exit);
788 MODULE_LICENSE("GPL");
789 MODULE_DESCRIPTION("Cryptographic algorithms API");