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/slab.h>
21 #include <linux/string.h>
25 static LIST_HEAD(crypto_template_list);
27 static inline int crypto_set_driver_name(struct crypto_alg *alg)
29 static const char suffix[] = "-generic";
30 char *driver_name = alg->cra_driver_name;
36 len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
37 if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
40 memcpy(driver_name + len, suffix, sizeof(suffix));
44 static inline void crypto_check_module_sig(struct module *mod)
46 #ifdef CONFIG_CRYPTO_FIPS
47 if (fips_enabled && mod && !mod->sig_ok)
48 panic("Module %s signature verification failed in FIPS mode\n",
54 static int crypto_check_alg(struct crypto_alg *alg)
56 crypto_check_module_sig(alg->cra_module);
58 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
61 if (alg->cra_blocksize > PAGE_SIZE / 8)
64 if (alg->cra_priority < 0)
67 atomic_set(&alg->cra_refcnt, 1);
69 return crypto_set_driver_name(alg);
72 static void crypto_destroy_instance(struct crypto_alg *alg)
74 struct crypto_instance *inst = (void *)alg;
75 struct crypto_template *tmpl = inst->tmpl;
78 crypto_tmpl_put(tmpl);
81 static struct list_head *crypto_more_spawns(struct crypto_alg *alg,
82 struct list_head *stack,
83 struct list_head *top,
84 struct list_head *secondary_spawns)
86 struct crypto_spawn *spawn, *n;
88 if (list_empty(stack))
91 spawn = list_first_entry(stack, struct crypto_spawn, list);
92 n = list_entry(spawn->list.next, struct crypto_spawn, list);
94 if (spawn->alg && &n->list != stack && !n->alg)
95 n->alg = (n->list.next == stack) ? alg :
96 &list_entry(n->list.next, struct crypto_spawn,
99 list_move(&spawn->list, secondary_spawns);
101 return &n->list == stack ? top : &n->inst->alg.cra_users;
104 static void crypto_remove_instance(struct crypto_instance *inst,
105 struct list_head *list)
107 struct crypto_template *tmpl = inst->tmpl;
109 if (crypto_is_dead(&inst->alg))
112 inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
113 if (hlist_unhashed(&inst->list))
116 if (!tmpl || !crypto_tmpl_get(tmpl))
119 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
120 list_move(&inst->alg.cra_list, list);
121 hlist_del(&inst->list);
122 inst->alg.cra_destroy = crypto_destroy_instance;
124 BUG_ON(!list_empty(&inst->alg.cra_users));
127 void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
128 struct crypto_alg *nalg)
130 u32 new_type = (nalg ?: alg)->cra_flags;
131 struct crypto_spawn *spawn, *n;
132 LIST_HEAD(secondary_spawns);
133 struct list_head *spawns;
137 spawns = &alg->cra_users;
138 list_for_each_entry_safe(spawn, n, spawns, list) {
139 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
142 list_move(&spawn->list, &top);
147 while (!list_empty(spawns)) {
148 struct crypto_instance *inst;
150 spawn = list_first_entry(spawns, struct crypto_spawn,
154 BUG_ON(&inst->alg == alg);
156 list_move(&spawn->list, &stack);
158 if (&inst->alg == nalg)
162 spawns = &inst->alg.cra_users;
164 } while ((spawns = crypto_more_spawns(alg, &stack, &top,
165 &secondary_spawns)));
167 list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
169 list_move(&spawn->list, &spawn->alg->cra_users);
171 crypto_remove_instance(spawn->inst, list);
174 EXPORT_SYMBOL_GPL(crypto_remove_spawns);
176 static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
178 struct crypto_alg *q;
179 struct crypto_larval *larval;
182 if (crypto_is_dead(alg))
185 INIT_LIST_HEAD(&alg->cra_users);
188 alg->cra_flags &= ~CRYPTO_ALG_TESTED;
192 list_for_each_entry(q, &crypto_alg_list, cra_list) {
196 if (crypto_is_moribund(q))
199 if (crypto_is_larval(q)) {
200 if (!strcmp(alg->cra_driver_name, q->cra_driver_name))
205 if (!strcmp(q->cra_driver_name, alg->cra_name) ||
206 !strcmp(q->cra_name, alg->cra_driver_name))
210 larval = crypto_larval_alloc(alg->cra_name,
211 alg->cra_flags | CRYPTO_ALG_TESTED, 0);
216 larval->adult = crypto_mod_get(alg);
220 atomic_set(&larval->alg.cra_refcnt, 1);
221 memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
222 CRYPTO_MAX_ALG_NAME);
223 larval->alg.cra_priority = alg->cra_priority;
225 list_add(&alg->cra_list, &crypto_alg_list);
226 list_add(&larval->alg.cra_list, &crypto_alg_list);
234 larval = ERR_PTR(ret);
238 void crypto_alg_tested(const char *name, int err)
240 struct crypto_larval *test;
241 struct crypto_alg *alg;
242 struct crypto_alg *q;
245 down_write(&crypto_alg_sem);
246 list_for_each_entry(q, &crypto_alg_list, cra_list) {
247 if (crypto_is_moribund(q) || !crypto_is_larval(q))
250 test = (struct crypto_larval *)q;
252 if (!strcmp(q->cra_driver_name, name))
256 printk(KERN_ERR "alg: Unexpected test result for %s: %d\n", name, err);
260 q->cra_flags |= CRYPTO_ALG_DEAD;
262 if (err || list_empty(&alg->cra_list))
265 alg->cra_flags |= CRYPTO_ALG_TESTED;
267 list_for_each_entry(q, &crypto_alg_list, cra_list) {
271 if (crypto_is_moribund(q))
274 if (crypto_is_larval(q)) {
275 struct crypto_larval *larval = (void *)q;
278 * Check to see if either our generic name or
279 * specific name can satisfy the name requested
280 * by the larval entry q.
282 if (strcmp(alg->cra_name, q->cra_name) &&
283 strcmp(alg->cra_driver_name, q->cra_name))
288 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
290 if (!crypto_mod_get(alg))
297 if (strcmp(alg->cra_name, q->cra_name))
300 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
301 q->cra_priority > alg->cra_priority)
304 crypto_remove_spawns(q, &list, alg);
308 complete_all(&test->completion);
311 up_write(&crypto_alg_sem);
313 crypto_remove_final(&list);
315 EXPORT_SYMBOL_GPL(crypto_alg_tested);
317 void crypto_remove_final(struct list_head *list)
319 struct crypto_alg *alg;
320 struct crypto_alg *n;
322 list_for_each_entry_safe(alg, n, list, cra_list) {
323 list_del_init(&alg->cra_list);
327 EXPORT_SYMBOL_GPL(crypto_remove_final);
329 static void crypto_wait_for_test(struct crypto_larval *larval)
333 err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
334 if (err != NOTIFY_STOP) {
335 if (WARN_ON(err != NOTIFY_DONE))
337 crypto_alg_tested(larval->alg.cra_driver_name, 0);
340 err = wait_for_completion_interruptible(&larval->completion);
344 crypto_larval_kill(&larval->alg);
347 int crypto_register_alg(struct crypto_alg *alg)
349 struct crypto_larval *larval;
352 err = crypto_check_alg(alg);
356 down_write(&crypto_alg_sem);
357 larval = __crypto_register_alg(alg);
358 up_write(&crypto_alg_sem);
361 return PTR_ERR(larval);
363 crypto_wait_for_test(larval);
366 EXPORT_SYMBOL_GPL(crypto_register_alg);
368 static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
370 if (unlikely(list_empty(&alg->cra_list)))
373 alg->cra_flags |= CRYPTO_ALG_DEAD;
375 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
376 list_del_init(&alg->cra_list);
377 crypto_remove_spawns(alg, list, NULL);
382 int crypto_unregister_alg(struct crypto_alg *alg)
387 down_write(&crypto_alg_sem);
388 ret = crypto_remove_alg(alg, &list);
389 up_write(&crypto_alg_sem);
394 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
395 if (alg->cra_destroy)
396 alg->cra_destroy(alg);
398 crypto_remove_final(&list);
401 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
403 int crypto_register_algs(struct crypto_alg *algs, int count)
407 for (i = 0; i < count; i++) {
408 ret = crypto_register_alg(&algs[i]);
416 for (--i; i >= 0; --i)
417 crypto_unregister_alg(&algs[i]);
421 EXPORT_SYMBOL_GPL(crypto_register_algs);
423 int crypto_unregister_algs(struct crypto_alg *algs, int count)
427 for (i = 0; i < count; i++) {
428 ret = crypto_unregister_alg(&algs[i]);
430 pr_err("Failed to unregister %s %s: %d\n",
431 algs[i].cra_driver_name, algs[i].cra_name, ret);
436 EXPORT_SYMBOL_GPL(crypto_unregister_algs);
438 int crypto_register_template(struct crypto_template *tmpl)
440 struct crypto_template *q;
443 down_write(&crypto_alg_sem);
445 crypto_check_module_sig(tmpl->module);
447 list_for_each_entry(q, &crypto_template_list, list) {
452 list_add(&tmpl->list, &crypto_template_list);
453 crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
456 up_write(&crypto_alg_sem);
459 EXPORT_SYMBOL_GPL(crypto_register_template);
461 void crypto_unregister_template(struct crypto_template *tmpl)
463 struct crypto_instance *inst;
464 struct hlist_node *n;
465 struct hlist_head *list;
468 down_write(&crypto_alg_sem);
470 BUG_ON(list_empty(&tmpl->list));
471 list_del_init(&tmpl->list);
473 list = &tmpl->instances;
474 hlist_for_each_entry(inst, list, list) {
475 int err = crypto_remove_alg(&inst->alg, &users);
480 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
482 up_write(&crypto_alg_sem);
484 hlist_for_each_entry_safe(inst, n, list, list) {
485 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
488 crypto_remove_final(&users);
490 EXPORT_SYMBOL_GPL(crypto_unregister_template);
492 static struct crypto_template *__crypto_lookup_template(const char *name)
494 struct crypto_template *q, *tmpl = NULL;
496 down_read(&crypto_alg_sem);
497 list_for_each_entry(q, &crypto_template_list, list) {
498 if (strcmp(q->name, name))
500 if (unlikely(!crypto_tmpl_get(q)))
506 up_read(&crypto_alg_sem);
511 struct crypto_template *crypto_lookup_template(const char *name)
513 return try_then_request_module(__crypto_lookup_template(name),
516 EXPORT_SYMBOL_GPL(crypto_lookup_template);
518 int crypto_register_instance(struct crypto_template *tmpl,
519 struct crypto_instance *inst)
521 struct crypto_larval *larval;
524 err = crypto_check_alg(&inst->alg);
528 inst->alg.cra_module = tmpl->module;
529 inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
531 if (unlikely(!crypto_mod_get(&inst->alg)))
534 down_write(&crypto_alg_sem);
536 larval = __crypto_register_alg(&inst->alg);
540 hlist_add_head(&inst->list, &tmpl->instances);
544 up_write(&crypto_alg_sem);
546 err = PTR_ERR(larval);
550 crypto_wait_for_test(larval);
552 /* Remove instance if test failed */
553 if (!(inst->alg.cra_flags & CRYPTO_ALG_TESTED))
554 crypto_unregister_instance(inst);
558 crypto_mod_put(&inst->alg);
561 EXPORT_SYMBOL_GPL(crypto_register_instance);
563 int crypto_unregister_instance(struct crypto_instance *inst)
567 down_write(&crypto_alg_sem);
569 crypto_remove_spawns(&inst->alg, &list, NULL);
570 crypto_remove_instance(inst, &list);
572 up_write(&crypto_alg_sem);
574 crypto_remove_final(&list);
578 EXPORT_SYMBOL_GPL(crypto_unregister_instance);
580 int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
581 struct crypto_instance *inst, u32 mask)
588 down_write(&crypto_alg_sem);
589 if (!crypto_is_moribund(alg)) {
590 list_add(&spawn->list, &alg->cra_users);
594 up_write(&crypto_alg_sem);
598 EXPORT_SYMBOL_GPL(crypto_init_spawn);
600 int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg,
601 struct crypto_instance *inst,
602 const struct crypto_type *frontend)
606 if ((alg->cra_flags ^ frontend->type) & frontend->maskset)
609 spawn->frontend = frontend;
610 err = crypto_init_spawn(spawn, alg, inst, frontend->maskset);
615 EXPORT_SYMBOL_GPL(crypto_init_spawn2);
617 void crypto_drop_spawn(struct crypto_spawn *spawn)
622 down_write(&crypto_alg_sem);
623 list_del(&spawn->list);
624 up_write(&crypto_alg_sem);
626 EXPORT_SYMBOL_GPL(crypto_drop_spawn);
628 static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
630 struct crypto_alg *alg;
631 struct crypto_alg *alg2;
633 down_read(&crypto_alg_sem);
637 alg2 = crypto_mod_get(alg2);
638 up_read(&crypto_alg_sem);
642 crypto_shoot_alg(alg);
643 return ERR_PTR(-EAGAIN);
649 struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
652 struct crypto_alg *alg;
653 struct crypto_tfm *tfm;
655 alg = crypto_spawn_alg(spawn);
657 return ERR_CAST(alg);
659 tfm = ERR_PTR(-EINVAL);
660 if (unlikely((alg->cra_flags ^ type) & mask))
663 tfm = __crypto_alloc_tfm(alg, type, mask);
673 EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
675 void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
677 struct crypto_alg *alg;
678 struct crypto_tfm *tfm;
680 alg = crypto_spawn_alg(spawn);
682 return ERR_CAST(alg);
684 tfm = crypto_create_tfm(alg, spawn->frontend);
694 EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
696 int crypto_register_notifier(struct notifier_block *nb)
698 return blocking_notifier_chain_register(&crypto_chain, nb);
700 EXPORT_SYMBOL_GPL(crypto_register_notifier);
702 int crypto_unregister_notifier(struct notifier_block *nb)
704 return blocking_notifier_chain_unregister(&crypto_chain, nb);
706 EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
708 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
710 struct rtattr *rta = tb[0];
711 struct crypto_attr_type *algt;
714 return ERR_PTR(-ENOENT);
715 if (RTA_PAYLOAD(rta) < sizeof(*algt))
716 return ERR_PTR(-EINVAL);
717 if (rta->rta_type != CRYPTOA_TYPE)
718 return ERR_PTR(-EINVAL);
720 algt = RTA_DATA(rta);
724 EXPORT_SYMBOL_GPL(crypto_get_attr_type);
726 int crypto_check_attr_type(struct rtattr **tb, u32 type)
728 struct crypto_attr_type *algt;
730 algt = crypto_get_attr_type(tb);
732 return PTR_ERR(algt);
734 if ((algt->type ^ type) & algt->mask)
739 EXPORT_SYMBOL_GPL(crypto_check_attr_type);
741 const char *crypto_attr_alg_name(struct rtattr *rta)
743 struct crypto_attr_alg *alga;
746 return ERR_PTR(-ENOENT);
747 if (RTA_PAYLOAD(rta) < sizeof(*alga))
748 return ERR_PTR(-EINVAL);
749 if (rta->rta_type != CRYPTOA_ALG)
750 return ERR_PTR(-EINVAL);
752 alga = RTA_DATA(rta);
753 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
757 EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
759 struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
760 const struct crypto_type *frontend,
765 name = crypto_attr_alg_name(rta);
767 return ERR_CAST(name);
769 return crypto_find_alg(name, frontend, type, mask);
771 EXPORT_SYMBOL_GPL(crypto_attr_alg2);
773 int crypto_attr_u32(struct rtattr *rta, u32 *num)
775 struct crypto_attr_u32 *nu32;
779 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
781 if (rta->rta_type != CRYPTOA_U32)
784 nu32 = RTA_DATA(rta);
789 EXPORT_SYMBOL_GPL(crypto_attr_u32);
791 void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg,
794 struct crypto_instance *inst;
798 p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn),
801 return ERR_PTR(-ENOMEM);
803 inst = (void *)(p + head);
806 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
807 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
810 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
811 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
820 EXPORT_SYMBOL_GPL(crypto_alloc_instance2);
822 struct crypto_instance *crypto_alloc_instance(const char *name,
823 struct crypto_alg *alg)
825 struct crypto_instance *inst;
826 struct crypto_spawn *spawn;
829 inst = crypto_alloc_instance2(name, alg, 0);
833 spawn = crypto_instance_ctx(inst);
834 err = crypto_init_spawn(spawn, alg, inst,
835 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
849 EXPORT_SYMBOL_GPL(crypto_alloc_instance);
851 void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
853 INIT_LIST_HEAD(&queue->list);
854 queue->backlog = &queue->list;
856 queue->max_qlen = max_qlen;
858 EXPORT_SYMBOL_GPL(crypto_init_queue);
860 int crypto_enqueue_request(struct crypto_queue *queue,
861 struct crypto_async_request *request)
863 int err = -EINPROGRESS;
865 if (unlikely(queue->qlen >= queue->max_qlen)) {
867 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
869 if (queue->backlog == &queue->list)
870 queue->backlog = &request->list;
874 list_add_tail(&request->list, &queue->list);
879 EXPORT_SYMBOL_GPL(crypto_enqueue_request);
881 void *__crypto_dequeue_request(struct crypto_queue *queue, unsigned int offset)
883 struct list_head *request;
885 if (unlikely(!queue->qlen))
890 if (queue->backlog != &queue->list)
891 queue->backlog = queue->backlog->next;
893 request = queue->list.next;
896 return (char *)list_entry(request, struct crypto_async_request, list) -
899 EXPORT_SYMBOL_GPL(__crypto_dequeue_request);
901 struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
903 return __crypto_dequeue_request(queue, 0);
905 EXPORT_SYMBOL_GPL(crypto_dequeue_request);
907 int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
909 struct crypto_async_request *req;
911 list_for_each_entry(req, &queue->list, list) {
918 EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
920 static inline void crypto_inc_byte(u8 *a, unsigned int size)
925 for (; size; size--) {
933 void crypto_inc(u8 *a, unsigned int size)
935 __be32 *b = (__be32 *)(a + size);
938 for (; size >= 4; size -= 4) {
939 c = be32_to_cpu(*--b) + 1;
945 crypto_inc_byte(a, size);
947 EXPORT_SYMBOL_GPL(crypto_inc);
949 static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
955 void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
960 for (; size >= 4; size -= 4)
963 crypto_xor_byte((u8 *)a, (u8 *)b, size);
965 EXPORT_SYMBOL_GPL(crypto_xor);
967 static int __init crypto_algapi_init(void)
973 static void __exit crypto_algapi_exit(void)
978 module_init(crypto_algapi_init);
979 module_exit(crypto_algapi_exit);
981 MODULE_LICENSE("GPL");
982 MODULE_DESCRIPTION("Cryptographic algorithms API");