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/fips.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
26 static LIST_HEAD(crypto_template_list);
28 static inline int crypto_set_driver_name(struct crypto_alg *alg)
30 static const char suffix[] = "-generic";
31 char *driver_name = alg->cra_driver_name;
37 len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
38 if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
41 memcpy(driver_name + len, suffix, sizeof(suffix));
45 static inline void crypto_check_module_sig(struct module *mod)
47 if (fips_enabled && mod && !module_sig_ok(mod))
48 panic("Module %s signature verification failed in FIPS mode\n",
52 static int crypto_check_alg(struct crypto_alg *alg)
54 crypto_check_module_sig(alg->cra_module);
56 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
59 if (alg->cra_blocksize > PAGE_SIZE / 8)
62 if (alg->cra_priority < 0)
65 atomic_set(&alg->cra_refcnt, 1);
67 return crypto_set_driver_name(alg);
70 static void crypto_destroy_instance(struct crypto_alg *alg)
72 struct crypto_instance *inst = (void *)alg;
73 struct crypto_template *tmpl = inst->tmpl;
76 crypto_tmpl_put(tmpl);
79 static struct list_head *crypto_more_spawns(struct crypto_alg *alg,
80 struct list_head *stack,
81 struct list_head *top,
82 struct list_head *secondary_spawns)
84 struct crypto_spawn *spawn, *n;
86 if (list_empty(stack))
89 spawn = list_first_entry(stack, struct crypto_spawn, list);
90 n = list_entry(spawn->list.next, struct crypto_spawn, list);
92 if (spawn->alg && &n->list != stack && !n->alg)
93 n->alg = (n->list.next == stack) ? alg :
94 &list_entry(n->list.next, struct crypto_spawn,
97 list_move(&spawn->list, secondary_spawns);
99 return &n->list == stack ? top : &n->inst->alg.cra_users;
102 static void crypto_remove_instance(struct crypto_instance *inst,
103 struct list_head *list)
105 struct crypto_template *tmpl = inst->tmpl;
107 if (crypto_is_dead(&inst->alg))
110 inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
111 if (hlist_unhashed(&inst->list))
114 if (!tmpl || !crypto_tmpl_get(tmpl))
117 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
118 list_move(&inst->alg.cra_list, list);
119 hlist_del(&inst->list);
120 inst->alg.cra_destroy = crypto_destroy_instance;
122 BUG_ON(!list_empty(&inst->alg.cra_users));
125 void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
126 struct crypto_alg *nalg)
128 u32 new_type = (nalg ?: alg)->cra_flags;
129 struct crypto_spawn *spawn, *n;
130 LIST_HEAD(secondary_spawns);
131 struct list_head *spawns;
135 spawns = &alg->cra_users;
136 list_for_each_entry_safe(spawn, n, spawns, list) {
137 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
140 list_move(&spawn->list, &top);
145 while (!list_empty(spawns)) {
146 struct crypto_instance *inst;
148 spawn = list_first_entry(spawns, struct crypto_spawn,
152 BUG_ON(&inst->alg == alg);
154 list_move(&spawn->list, &stack);
156 if (&inst->alg == nalg)
160 spawns = &inst->alg.cra_users;
162 } while ((spawns = crypto_more_spawns(alg, &stack, &top,
163 &secondary_spawns)));
165 list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
167 list_move(&spawn->list, &spawn->alg->cra_users);
169 crypto_remove_instance(spawn->inst, list);
172 EXPORT_SYMBOL_GPL(crypto_remove_spawns);
174 static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
176 struct crypto_alg *q;
177 struct crypto_larval *larval;
180 if (crypto_is_dead(alg))
183 INIT_LIST_HEAD(&alg->cra_users);
186 alg->cra_flags &= ~CRYPTO_ALG_TESTED;
190 list_for_each_entry(q, &crypto_alg_list, cra_list) {
194 if (crypto_is_moribund(q))
197 if (crypto_is_larval(q)) {
198 if (!strcmp(alg->cra_driver_name, q->cra_driver_name))
203 if (!strcmp(q->cra_driver_name, alg->cra_name) ||
204 !strcmp(q->cra_name, alg->cra_driver_name))
208 larval = crypto_larval_alloc(alg->cra_name,
209 alg->cra_flags | CRYPTO_ALG_TESTED, 0);
214 larval->adult = crypto_mod_get(alg);
218 atomic_set(&larval->alg.cra_refcnt, 1);
219 memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
220 CRYPTO_MAX_ALG_NAME);
221 larval->alg.cra_priority = alg->cra_priority;
223 list_add(&alg->cra_list, &crypto_alg_list);
224 list_add(&larval->alg.cra_list, &crypto_alg_list);
232 larval = ERR_PTR(ret);
236 void crypto_alg_tested(const char *name, int err)
238 struct crypto_larval *test;
239 struct crypto_alg *alg;
240 struct crypto_alg *q;
243 down_write(&crypto_alg_sem);
244 list_for_each_entry(q, &crypto_alg_list, cra_list) {
245 if (crypto_is_moribund(q) || !crypto_is_larval(q))
248 test = (struct crypto_larval *)q;
250 if (!strcmp(q->cra_driver_name, name))
254 printk(KERN_ERR "alg: Unexpected test result for %s: %d\n", name, err);
258 q->cra_flags |= CRYPTO_ALG_DEAD;
260 if (err || list_empty(&alg->cra_list))
263 alg->cra_flags |= CRYPTO_ALG_TESTED;
265 list_for_each_entry(q, &crypto_alg_list, cra_list) {
269 if (crypto_is_moribund(q))
272 if (crypto_is_larval(q)) {
273 struct crypto_larval *larval = (void *)q;
276 * Check to see if either our generic name or
277 * specific name can satisfy the name requested
278 * by the larval entry q.
280 if (strcmp(alg->cra_name, q->cra_name) &&
281 strcmp(alg->cra_driver_name, q->cra_name))
286 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
288 if (!crypto_mod_get(alg))
295 if (strcmp(alg->cra_name, q->cra_name))
298 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
299 q->cra_priority > alg->cra_priority)
302 crypto_remove_spawns(q, &list, alg);
306 complete_all(&test->completion);
309 up_write(&crypto_alg_sem);
311 crypto_remove_final(&list);
313 EXPORT_SYMBOL_GPL(crypto_alg_tested);
315 void crypto_remove_final(struct list_head *list)
317 struct crypto_alg *alg;
318 struct crypto_alg *n;
320 list_for_each_entry_safe(alg, n, list, cra_list) {
321 list_del_init(&alg->cra_list);
325 EXPORT_SYMBOL_GPL(crypto_remove_final);
327 static void crypto_wait_for_test(struct crypto_larval *larval)
331 err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
332 if (err != NOTIFY_STOP) {
333 if (WARN_ON(err != NOTIFY_DONE))
335 crypto_alg_tested(larval->alg.cra_driver_name, 0);
338 err = wait_for_completion_interruptible(&larval->completion);
342 crypto_larval_kill(&larval->alg);
345 int crypto_register_alg(struct crypto_alg *alg)
347 struct crypto_larval *larval;
350 err = crypto_check_alg(alg);
354 down_write(&crypto_alg_sem);
355 larval = __crypto_register_alg(alg);
356 up_write(&crypto_alg_sem);
359 return PTR_ERR(larval);
361 crypto_wait_for_test(larval);
364 EXPORT_SYMBOL_GPL(crypto_register_alg);
366 static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
368 if (unlikely(list_empty(&alg->cra_list)))
371 alg->cra_flags |= CRYPTO_ALG_DEAD;
373 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
374 list_del_init(&alg->cra_list);
375 crypto_remove_spawns(alg, list, NULL);
380 int crypto_unregister_alg(struct crypto_alg *alg)
385 down_write(&crypto_alg_sem);
386 ret = crypto_remove_alg(alg, &list);
387 up_write(&crypto_alg_sem);
392 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
393 if (alg->cra_destroy)
394 alg->cra_destroy(alg);
396 crypto_remove_final(&list);
399 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
401 int crypto_register_algs(struct crypto_alg *algs, int count)
405 for (i = 0; i < count; i++) {
406 ret = crypto_register_alg(&algs[i]);
414 for (--i; i >= 0; --i)
415 crypto_unregister_alg(&algs[i]);
419 EXPORT_SYMBOL_GPL(crypto_register_algs);
421 int crypto_unregister_algs(struct crypto_alg *algs, int count)
425 for (i = 0; i < count; i++) {
426 ret = crypto_unregister_alg(&algs[i]);
428 pr_err("Failed to unregister %s %s: %d\n",
429 algs[i].cra_driver_name, algs[i].cra_name, ret);
434 EXPORT_SYMBOL_GPL(crypto_unregister_algs);
436 int crypto_register_template(struct crypto_template *tmpl)
438 struct crypto_template *q;
441 down_write(&crypto_alg_sem);
443 crypto_check_module_sig(tmpl->module);
445 list_for_each_entry(q, &crypto_template_list, list) {
450 list_add(&tmpl->list, &crypto_template_list);
451 crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
454 up_write(&crypto_alg_sem);
457 EXPORT_SYMBOL_GPL(crypto_register_template);
459 void crypto_unregister_template(struct crypto_template *tmpl)
461 struct crypto_instance *inst;
462 struct hlist_node *n;
463 struct hlist_head *list;
466 down_write(&crypto_alg_sem);
468 BUG_ON(list_empty(&tmpl->list));
469 list_del_init(&tmpl->list);
471 list = &tmpl->instances;
472 hlist_for_each_entry(inst, list, list) {
473 int err = crypto_remove_alg(&inst->alg, &users);
478 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
480 up_write(&crypto_alg_sem);
482 hlist_for_each_entry_safe(inst, n, list, list) {
483 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
486 crypto_remove_final(&users);
488 EXPORT_SYMBOL_GPL(crypto_unregister_template);
490 static struct crypto_template *__crypto_lookup_template(const char *name)
492 struct crypto_template *q, *tmpl = NULL;
494 down_read(&crypto_alg_sem);
495 list_for_each_entry(q, &crypto_template_list, list) {
496 if (strcmp(q->name, name))
498 if (unlikely(!crypto_tmpl_get(q)))
504 up_read(&crypto_alg_sem);
509 struct crypto_template *crypto_lookup_template(const char *name)
511 return try_then_request_module(__crypto_lookup_template(name),
514 EXPORT_SYMBOL_GPL(crypto_lookup_template);
516 int crypto_register_instance(struct crypto_template *tmpl,
517 struct crypto_instance *inst)
519 struct crypto_larval *larval;
522 err = crypto_check_alg(&inst->alg);
526 inst->alg.cra_module = tmpl->module;
527 inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
529 if (unlikely(!crypto_mod_get(&inst->alg)))
532 down_write(&crypto_alg_sem);
534 larval = __crypto_register_alg(&inst->alg);
538 hlist_add_head(&inst->list, &tmpl->instances);
542 up_write(&crypto_alg_sem);
544 err = PTR_ERR(larval);
548 crypto_wait_for_test(larval);
550 /* Remove instance if test failed */
551 if (!(inst->alg.cra_flags & CRYPTO_ALG_TESTED))
552 crypto_unregister_instance(inst);
556 crypto_mod_put(&inst->alg);
559 EXPORT_SYMBOL_GPL(crypto_register_instance);
561 int crypto_unregister_instance(struct crypto_instance *inst)
565 down_write(&crypto_alg_sem);
567 crypto_remove_spawns(&inst->alg, &list, NULL);
568 crypto_remove_instance(inst, &list);
570 up_write(&crypto_alg_sem);
572 crypto_remove_final(&list);
576 EXPORT_SYMBOL_GPL(crypto_unregister_instance);
578 int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
579 struct crypto_instance *inst, u32 mask)
586 down_write(&crypto_alg_sem);
587 if (!crypto_is_moribund(alg)) {
588 list_add(&spawn->list, &alg->cra_users);
592 up_write(&crypto_alg_sem);
596 EXPORT_SYMBOL_GPL(crypto_init_spawn);
598 int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg,
599 struct crypto_instance *inst,
600 const struct crypto_type *frontend)
604 if ((alg->cra_flags ^ frontend->type) & frontend->maskset)
607 spawn->frontend = frontend;
608 err = crypto_init_spawn(spawn, alg, inst, frontend->maskset);
613 EXPORT_SYMBOL_GPL(crypto_init_spawn2);
615 int crypto_grab_spawn(struct crypto_spawn *spawn, const char *name,
618 struct crypto_alg *alg;
621 alg = crypto_find_alg(name, spawn->frontend, type, mask);
625 err = crypto_init_spawn(spawn, alg, spawn->inst, mask);
629 EXPORT_SYMBOL_GPL(crypto_grab_spawn);
631 void crypto_drop_spawn(struct crypto_spawn *spawn)
636 down_write(&crypto_alg_sem);
637 list_del(&spawn->list);
638 up_write(&crypto_alg_sem);
640 EXPORT_SYMBOL_GPL(crypto_drop_spawn);
642 static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
644 struct crypto_alg *alg;
645 struct crypto_alg *alg2;
647 down_read(&crypto_alg_sem);
651 alg2 = crypto_mod_get(alg2);
652 up_read(&crypto_alg_sem);
656 crypto_shoot_alg(alg);
657 return ERR_PTR(-EAGAIN);
663 struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
666 struct crypto_alg *alg;
667 struct crypto_tfm *tfm;
669 alg = crypto_spawn_alg(spawn);
671 return ERR_CAST(alg);
673 tfm = ERR_PTR(-EINVAL);
674 if (unlikely((alg->cra_flags ^ type) & mask))
677 tfm = __crypto_alloc_tfm(alg, type, mask);
687 EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
689 void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
691 struct crypto_alg *alg;
692 struct crypto_tfm *tfm;
694 alg = crypto_spawn_alg(spawn);
696 return ERR_CAST(alg);
698 tfm = crypto_create_tfm(alg, spawn->frontend);
708 EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
710 int crypto_register_notifier(struct notifier_block *nb)
712 return blocking_notifier_chain_register(&crypto_chain, nb);
714 EXPORT_SYMBOL_GPL(crypto_register_notifier);
716 int crypto_unregister_notifier(struct notifier_block *nb)
718 return blocking_notifier_chain_unregister(&crypto_chain, nb);
720 EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
722 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
724 struct rtattr *rta = tb[0];
725 struct crypto_attr_type *algt;
728 return ERR_PTR(-ENOENT);
729 if (RTA_PAYLOAD(rta) < sizeof(*algt))
730 return ERR_PTR(-EINVAL);
731 if (rta->rta_type != CRYPTOA_TYPE)
732 return ERR_PTR(-EINVAL);
734 algt = RTA_DATA(rta);
738 EXPORT_SYMBOL_GPL(crypto_get_attr_type);
740 int crypto_check_attr_type(struct rtattr **tb, u32 type)
742 struct crypto_attr_type *algt;
744 algt = crypto_get_attr_type(tb);
746 return PTR_ERR(algt);
748 if ((algt->type ^ type) & algt->mask)
753 EXPORT_SYMBOL_GPL(crypto_check_attr_type);
755 const char *crypto_attr_alg_name(struct rtattr *rta)
757 struct crypto_attr_alg *alga;
760 return ERR_PTR(-ENOENT);
761 if (RTA_PAYLOAD(rta) < sizeof(*alga))
762 return ERR_PTR(-EINVAL);
763 if (rta->rta_type != CRYPTOA_ALG)
764 return ERR_PTR(-EINVAL);
766 alga = RTA_DATA(rta);
767 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
771 EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
773 struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
774 const struct crypto_type *frontend,
779 name = crypto_attr_alg_name(rta);
781 return ERR_CAST(name);
783 return crypto_find_alg(name, frontend, type, mask);
785 EXPORT_SYMBOL_GPL(crypto_attr_alg2);
787 int crypto_attr_u32(struct rtattr *rta, u32 *num)
789 struct crypto_attr_u32 *nu32;
793 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
795 if (rta->rta_type != CRYPTOA_U32)
798 nu32 = RTA_DATA(rta);
803 EXPORT_SYMBOL_GPL(crypto_attr_u32);
805 void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg,
808 struct crypto_instance *inst;
812 p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn),
815 return ERR_PTR(-ENOMEM);
817 inst = (void *)(p + head);
820 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
821 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
824 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
825 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
834 EXPORT_SYMBOL_GPL(crypto_alloc_instance2);
836 struct crypto_instance *crypto_alloc_instance(const char *name,
837 struct crypto_alg *alg)
839 struct crypto_instance *inst;
840 struct crypto_spawn *spawn;
843 inst = crypto_alloc_instance2(name, alg, 0);
847 spawn = crypto_instance_ctx(inst);
848 err = crypto_init_spawn(spawn, alg, inst,
849 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
863 EXPORT_SYMBOL_GPL(crypto_alloc_instance);
865 void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
867 INIT_LIST_HEAD(&queue->list);
868 queue->backlog = &queue->list;
870 queue->max_qlen = max_qlen;
872 EXPORT_SYMBOL_GPL(crypto_init_queue);
874 int crypto_enqueue_request(struct crypto_queue *queue,
875 struct crypto_async_request *request)
877 int err = -EINPROGRESS;
879 if (unlikely(queue->qlen >= queue->max_qlen)) {
881 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
883 if (queue->backlog == &queue->list)
884 queue->backlog = &request->list;
888 list_add_tail(&request->list, &queue->list);
893 EXPORT_SYMBOL_GPL(crypto_enqueue_request);
895 void *__crypto_dequeue_request(struct crypto_queue *queue, unsigned int offset)
897 struct list_head *request;
899 if (unlikely(!queue->qlen))
904 if (queue->backlog != &queue->list)
905 queue->backlog = queue->backlog->next;
907 request = queue->list.next;
910 return (char *)list_entry(request, struct crypto_async_request, list) -
913 EXPORT_SYMBOL_GPL(__crypto_dequeue_request);
915 struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
917 return __crypto_dequeue_request(queue, 0);
919 EXPORT_SYMBOL_GPL(crypto_dequeue_request);
921 int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
923 struct crypto_async_request *req;
925 list_for_each_entry(req, &queue->list, list) {
932 EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
934 static inline void crypto_inc_byte(u8 *a, unsigned int size)
939 for (; size; size--) {
947 void crypto_inc(u8 *a, unsigned int size)
949 __be32 *b = (__be32 *)(a + size);
952 for (; size >= 4; size -= 4) {
953 c = be32_to_cpu(*--b) + 1;
959 crypto_inc_byte(a, size);
961 EXPORT_SYMBOL_GPL(crypto_inc);
963 static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
969 void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
974 for (; size >= 4; size -= 4)
977 crypto_xor_byte((u8 *)a, (u8 *)b, size);
979 EXPORT_SYMBOL_GPL(crypto_xor);
981 unsigned int crypto_alg_extsize(struct crypto_alg *alg)
983 return alg->cra_ctxsize +
984 (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
986 EXPORT_SYMBOL_GPL(crypto_alg_extsize);
988 static int __init crypto_algapi_init(void)
994 static void __exit crypto_algapi_exit(void)
999 module_init(crypto_algapi_init);
1000 module_exit(crypto_algapi_exit);
1002 MODULE_LICENSE("GPL");
1003 MODULE_DESCRIPTION("Cryptographic algorithms API");