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 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 atomic_set(&alg
->cra_refcnt
, 1);
191 list_for_each_entry(q
, &crypto_alg_list
, cra_list
) {
195 if (crypto_is_moribund(q
))
198 if (crypto_is_larval(q
)) {
199 if (!strcmp(alg
->cra_driver_name
, q
->cra_driver_name
))
204 if (!strcmp(q
->cra_driver_name
, alg
->cra_name
) ||
205 !strcmp(q
->cra_name
, alg
->cra_driver_name
))
209 larval
= crypto_larval_alloc(alg
->cra_name
,
210 alg
->cra_flags
| CRYPTO_ALG_TESTED
, 0);
215 larval
->adult
= crypto_mod_get(alg
);
219 atomic_set(&larval
->alg
.cra_refcnt
, 1);
220 memcpy(larval
->alg
.cra_driver_name
, alg
->cra_driver_name
,
221 CRYPTO_MAX_ALG_NAME
);
222 larval
->alg
.cra_priority
= alg
->cra_priority
;
224 list_add(&alg
->cra_list
, &crypto_alg_list
);
225 list_add(&larval
->alg
.cra_list
, &crypto_alg_list
);
233 larval
= ERR_PTR(ret
);
237 void crypto_alg_tested(const char *name
, int err
)
239 struct crypto_larval
*test
;
240 struct crypto_alg
*alg
;
241 struct crypto_alg
*q
;
244 down_write(&crypto_alg_sem
);
245 list_for_each_entry(q
, &crypto_alg_list
, cra_list
) {
246 if (crypto_is_moribund(q
) || !crypto_is_larval(q
))
249 test
= (struct crypto_larval
*)q
;
251 if (!strcmp(q
->cra_driver_name
, name
))
255 printk(KERN_ERR
"alg: Unexpected test result for %s: %d\n", name
, err
);
259 q
->cra_flags
|= CRYPTO_ALG_DEAD
;
261 if (err
|| list_empty(&alg
->cra_list
))
264 alg
->cra_flags
|= CRYPTO_ALG_TESTED
;
266 list_for_each_entry(q
, &crypto_alg_list
, cra_list
) {
270 if (crypto_is_moribund(q
))
273 if (crypto_is_larval(q
)) {
274 struct crypto_larval
*larval
= (void *)q
;
277 * Check to see if either our generic name or
278 * specific name can satisfy the name requested
279 * by the larval entry q.
281 if (strcmp(alg
->cra_name
, q
->cra_name
) &&
282 strcmp(alg
->cra_driver_name
, q
->cra_name
))
287 if ((q
->cra_flags
^ alg
->cra_flags
) & larval
->mask
)
289 if (!crypto_mod_get(alg
))
296 if (strcmp(alg
->cra_name
, q
->cra_name
))
299 if (strcmp(alg
->cra_driver_name
, q
->cra_driver_name
) &&
300 q
->cra_priority
> alg
->cra_priority
)
303 crypto_remove_spawns(q
, &list
, alg
);
307 complete_all(&test
->completion
);
310 up_write(&crypto_alg_sem
);
312 crypto_remove_final(&list
);
314 EXPORT_SYMBOL_GPL(crypto_alg_tested
);
316 void crypto_remove_final(struct list_head
*list
)
318 struct crypto_alg
*alg
;
319 struct crypto_alg
*n
;
321 list_for_each_entry_safe(alg
, n
, list
, cra_list
) {
322 list_del_init(&alg
->cra_list
);
326 EXPORT_SYMBOL_GPL(crypto_remove_final
);
328 static void crypto_wait_for_test(struct crypto_larval
*larval
)
332 err
= crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER
, larval
->adult
);
333 if (err
!= NOTIFY_STOP
) {
334 if (WARN_ON(err
!= NOTIFY_DONE
))
336 crypto_alg_tested(larval
->alg
.cra_driver_name
, 0);
339 err
= wait_for_completion_interruptible(&larval
->completion
);
343 crypto_larval_kill(&larval
->alg
);
346 int crypto_register_alg(struct crypto_alg
*alg
)
348 struct crypto_larval
*larval
;
351 err
= crypto_check_alg(alg
);
355 down_write(&crypto_alg_sem
);
356 larval
= __crypto_register_alg(alg
);
357 up_write(&crypto_alg_sem
);
360 return PTR_ERR(larval
);
362 crypto_wait_for_test(larval
);
365 EXPORT_SYMBOL_GPL(crypto_register_alg
);
367 static int crypto_remove_alg(struct crypto_alg
*alg
, struct list_head
*list
)
369 if (unlikely(list_empty(&alg
->cra_list
)))
372 alg
->cra_flags
|= CRYPTO_ALG_DEAD
;
374 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER
, alg
);
375 list_del_init(&alg
->cra_list
);
376 crypto_remove_spawns(alg
, list
, NULL
);
381 int crypto_unregister_alg(struct crypto_alg
*alg
)
386 down_write(&crypto_alg_sem
);
387 ret
= crypto_remove_alg(alg
, &list
);
388 up_write(&crypto_alg_sem
);
393 BUG_ON(atomic_read(&alg
->cra_refcnt
) != 1);
394 if (alg
->cra_destroy
)
395 alg
->cra_destroy(alg
);
397 crypto_remove_final(&list
);
400 EXPORT_SYMBOL_GPL(crypto_unregister_alg
);
402 int crypto_register_algs(struct crypto_alg
*algs
, int count
)
406 for (i
= 0; i
< count
; i
++) {
407 ret
= crypto_register_alg(&algs
[i
]);
415 for (--i
; i
>= 0; --i
)
416 crypto_unregister_alg(&algs
[i
]);
420 EXPORT_SYMBOL_GPL(crypto_register_algs
);
422 int crypto_unregister_algs(struct crypto_alg
*algs
, int count
)
426 for (i
= 0; i
< count
; i
++) {
427 ret
= crypto_unregister_alg(&algs
[i
]);
429 pr_err("Failed to unregister %s %s: %d\n",
430 algs
[i
].cra_driver_name
, algs
[i
].cra_name
, ret
);
435 EXPORT_SYMBOL_GPL(crypto_unregister_algs
);
437 int crypto_register_template(struct crypto_template
*tmpl
)
439 struct crypto_template
*q
;
442 down_write(&crypto_alg_sem
);
444 crypto_check_module_sig(tmpl
->module
);
446 list_for_each_entry(q
, &crypto_template_list
, list
) {
451 list_add(&tmpl
->list
, &crypto_template_list
);
452 crypto_notify(CRYPTO_MSG_TMPL_REGISTER
, tmpl
);
455 up_write(&crypto_alg_sem
);
458 EXPORT_SYMBOL_GPL(crypto_register_template
);
460 void crypto_unregister_template(struct crypto_template
*tmpl
)
462 struct crypto_instance
*inst
;
463 struct hlist_node
*n
;
464 struct hlist_head
*list
;
467 down_write(&crypto_alg_sem
);
469 BUG_ON(list_empty(&tmpl
->list
));
470 list_del_init(&tmpl
->list
);
472 list
= &tmpl
->instances
;
473 hlist_for_each_entry(inst
, list
, list
) {
474 int err
= crypto_remove_alg(&inst
->alg
, &users
);
479 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER
, tmpl
);
481 up_write(&crypto_alg_sem
);
483 hlist_for_each_entry_safe(inst
, n
, list
, list
) {
484 BUG_ON(atomic_read(&inst
->alg
.cra_refcnt
) != 1);
487 crypto_remove_final(&users
);
489 EXPORT_SYMBOL_GPL(crypto_unregister_template
);
491 static struct crypto_template
*__crypto_lookup_template(const char *name
)
493 struct crypto_template
*q
, *tmpl
= NULL
;
495 down_read(&crypto_alg_sem
);
496 list_for_each_entry(q
, &crypto_template_list
, list
) {
497 if (strcmp(q
->name
, name
))
499 if (unlikely(!crypto_tmpl_get(q
)))
505 up_read(&crypto_alg_sem
);
510 struct crypto_template
*crypto_lookup_template(const char *name
)
512 return try_then_request_module(__crypto_lookup_template(name
),
515 EXPORT_SYMBOL_GPL(crypto_lookup_template
);
517 int crypto_register_instance(struct crypto_template
*tmpl
,
518 struct crypto_instance
*inst
)
520 struct crypto_larval
*larval
;
523 err
= crypto_check_alg(&inst
->alg
);
527 inst
->alg
.cra_module
= tmpl
->module
;
528 inst
->alg
.cra_flags
|= CRYPTO_ALG_INSTANCE
;
530 down_write(&crypto_alg_sem
);
532 larval
= __crypto_register_alg(&inst
->alg
);
536 hlist_add_head(&inst
->list
, &tmpl
->instances
);
540 up_write(&crypto_alg_sem
);
542 err
= PTR_ERR(larval
);
546 crypto_wait_for_test(larval
);
552 EXPORT_SYMBOL_GPL(crypto_register_instance
);
554 int crypto_unregister_instance(struct crypto_instance
*inst
)
558 down_write(&crypto_alg_sem
);
560 crypto_remove_spawns(&inst
->alg
, &list
, NULL
);
561 crypto_remove_instance(inst
, &list
);
563 up_write(&crypto_alg_sem
);
565 crypto_remove_final(&list
);
569 EXPORT_SYMBOL_GPL(crypto_unregister_instance
);
571 int crypto_init_spawn(struct crypto_spawn
*spawn
, struct crypto_alg
*alg
,
572 struct crypto_instance
*inst
, u32 mask
)
579 down_write(&crypto_alg_sem
);
580 if (!crypto_is_moribund(alg
)) {
581 list_add(&spawn
->list
, &alg
->cra_users
);
585 up_write(&crypto_alg_sem
);
589 EXPORT_SYMBOL_GPL(crypto_init_spawn
);
591 int crypto_init_spawn2(struct crypto_spawn
*spawn
, struct crypto_alg
*alg
,
592 struct crypto_instance
*inst
,
593 const struct crypto_type
*frontend
)
597 if ((alg
->cra_flags
^ frontend
->type
) & frontend
->maskset
)
600 spawn
->frontend
= frontend
;
601 err
= crypto_init_spawn(spawn
, alg
, inst
, frontend
->maskset
);
606 EXPORT_SYMBOL_GPL(crypto_init_spawn2
);
608 void crypto_drop_spawn(struct crypto_spawn
*spawn
)
613 down_write(&crypto_alg_sem
);
614 list_del(&spawn
->list
);
615 up_write(&crypto_alg_sem
);
617 EXPORT_SYMBOL_GPL(crypto_drop_spawn
);
619 static struct crypto_alg
*crypto_spawn_alg(struct crypto_spawn
*spawn
)
621 struct crypto_alg
*alg
;
622 struct crypto_alg
*alg2
;
624 down_read(&crypto_alg_sem
);
628 alg2
= crypto_mod_get(alg2
);
629 up_read(&crypto_alg_sem
);
633 crypto_shoot_alg(alg
);
634 return ERR_PTR(-EAGAIN
);
640 struct crypto_tfm
*crypto_spawn_tfm(struct crypto_spawn
*spawn
, u32 type
,
643 struct crypto_alg
*alg
;
644 struct crypto_tfm
*tfm
;
646 alg
= crypto_spawn_alg(spawn
);
648 return ERR_CAST(alg
);
650 tfm
= ERR_PTR(-EINVAL
);
651 if (unlikely((alg
->cra_flags
^ type
) & mask
))
654 tfm
= __crypto_alloc_tfm(alg
, type
, mask
);
664 EXPORT_SYMBOL_GPL(crypto_spawn_tfm
);
666 void *crypto_spawn_tfm2(struct crypto_spawn
*spawn
)
668 struct crypto_alg
*alg
;
669 struct crypto_tfm
*tfm
;
671 alg
= crypto_spawn_alg(spawn
);
673 return ERR_CAST(alg
);
675 tfm
= crypto_create_tfm(alg
, spawn
->frontend
);
685 EXPORT_SYMBOL_GPL(crypto_spawn_tfm2
);
687 int crypto_register_notifier(struct notifier_block
*nb
)
689 return blocking_notifier_chain_register(&crypto_chain
, nb
);
691 EXPORT_SYMBOL_GPL(crypto_register_notifier
);
693 int crypto_unregister_notifier(struct notifier_block
*nb
)
695 return blocking_notifier_chain_unregister(&crypto_chain
, nb
);
697 EXPORT_SYMBOL_GPL(crypto_unregister_notifier
);
699 struct crypto_attr_type
*crypto_get_attr_type(struct rtattr
**tb
)
701 struct rtattr
*rta
= tb
[0];
702 struct crypto_attr_type
*algt
;
705 return ERR_PTR(-ENOENT
);
706 if (RTA_PAYLOAD(rta
) < sizeof(*algt
))
707 return ERR_PTR(-EINVAL
);
708 if (rta
->rta_type
!= CRYPTOA_TYPE
)
709 return ERR_PTR(-EINVAL
);
711 algt
= RTA_DATA(rta
);
715 EXPORT_SYMBOL_GPL(crypto_get_attr_type
);
717 int crypto_check_attr_type(struct rtattr
**tb
, u32 type
)
719 struct crypto_attr_type
*algt
;
721 algt
= crypto_get_attr_type(tb
);
723 return PTR_ERR(algt
);
725 if ((algt
->type
^ type
) & algt
->mask
)
730 EXPORT_SYMBOL_GPL(crypto_check_attr_type
);
732 const char *crypto_attr_alg_name(struct rtattr
*rta
)
734 struct crypto_attr_alg
*alga
;
737 return ERR_PTR(-ENOENT
);
738 if (RTA_PAYLOAD(rta
) < sizeof(*alga
))
739 return ERR_PTR(-EINVAL
);
740 if (rta
->rta_type
!= CRYPTOA_ALG
)
741 return ERR_PTR(-EINVAL
);
743 alga
= RTA_DATA(rta
);
744 alga
->name
[CRYPTO_MAX_ALG_NAME
- 1] = 0;
748 EXPORT_SYMBOL_GPL(crypto_attr_alg_name
);
750 struct crypto_alg
*crypto_attr_alg2(struct rtattr
*rta
,
751 const struct crypto_type
*frontend
,
756 name
= crypto_attr_alg_name(rta
);
758 return ERR_CAST(name
);
760 return crypto_find_alg(name
, frontend
, type
, mask
);
762 EXPORT_SYMBOL_GPL(crypto_attr_alg2
);
764 int crypto_attr_u32(struct rtattr
*rta
, u32
*num
)
766 struct crypto_attr_u32
*nu32
;
770 if (RTA_PAYLOAD(rta
) < sizeof(*nu32
))
772 if (rta
->rta_type
!= CRYPTOA_U32
)
775 nu32
= RTA_DATA(rta
);
780 EXPORT_SYMBOL_GPL(crypto_attr_u32
);
782 void *crypto_alloc_instance2(const char *name
, struct crypto_alg
*alg
,
785 struct crypto_instance
*inst
;
789 p
= kzalloc(head
+ sizeof(*inst
) + sizeof(struct crypto_spawn
),
792 return ERR_PTR(-ENOMEM
);
794 inst
= (void *)(p
+ head
);
797 if (snprintf(inst
->alg
.cra_name
, CRYPTO_MAX_ALG_NAME
, "%s(%s)", name
,
798 alg
->cra_name
) >= CRYPTO_MAX_ALG_NAME
)
801 if (snprintf(inst
->alg
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
, "%s(%s)",
802 name
, alg
->cra_driver_name
) >= CRYPTO_MAX_ALG_NAME
)
811 EXPORT_SYMBOL_GPL(crypto_alloc_instance2
);
813 struct crypto_instance
*crypto_alloc_instance(const char *name
,
814 struct crypto_alg
*alg
)
816 struct crypto_instance
*inst
;
817 struct crypto_spawn
*spawn
;
820 inst
= crypto_alloc_instance2(name
, alg
, 0);
824 spawn
= crypto_instance_ctx(inst
);
825 err
= crypto_init_spawn(spawn
, alg
, inst
,
826 CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_ASYNC
);
840 EXPORT_SYMBOL_GPL(crypto_alloc_instance
);
842 void crypto_init_queue(struct crypto_queue
*queue
, unsigned int max_qlen
)
844 INIT_LIST_HEAD(&queue
->list
);
845 queue
->backlog
= &queue
->list
;
847 queue
->max_qlen
= max_qlen
;
849 EXPORT_SYMBOL_GPL(crypto_init_queue
);
851 int crypto_enqueue_request(struct crypto_queue
*queue
,
852 struct crypto_async_request
*request
)
854 int err
= -EINPROGRESS
;
856 if (unlikely(queue
->qlen
>= queue
->max_qlen
)) {
858 if (!(request
->flags
& CRYPTO_TFM_REQ_MAY_BACKLOG
))
860 if (queue
->backlog
== &queue
->list
)
861 queue
->backlog
= &request
->list
;
865 list_add_tail(&request
->list
, &queue
->list
);
870 EXPORT_SYMBOL_GPL(crypto_enqueue_request
);
872 void *__crypto_dequeue_request(struct crypto_queue
*queue
, unsigned int offset
)
874 struct list_head
*request
;
876 if (unlikely(!queue
->qlen
))
881 if (queue
->backlog
!= &queue
->list
)
882 queue
->backlog
= queue
->backlog
->next
;
884 request
= queue
->list
.next
;
887 return (char *)list_entry(request
, struct crypto_async_request
, list
) -
890 EXPORT_SYMBOL_GPL(__crypto_dequeue_request
);
892 struct crypto_async_request
*crypto_dequeue_request(struct crypto_queue
*queue
)
894 return __crypto_dequeue_request(queue
, 0);
896 EXPORT_SYMBOL_GPL(crypto_dequeue_request
);
898 int crypto_tfm_in_queue(struct crypto_queue
*queue
, struct crypto_tfm
*tfm
)
900 struct crypto_async_request
*req
;
902 list_for_each_entry(req
, &queue
->list
, list
) {
909 EXPORT_SYMBOL_GPL(crypto_tfm_in_queue
);
911 static inline void crypto_inc_byte(u8
*a
, unsigned int size
)
916 for (; size
; size
--) {
924 void crypto_inc(u8
*a
, unsigned int size
)
926 __be32
*b
= (__be32
*)(a
+ size
);
929 for (; size
>= 4; size
-= 4) {
930 c
= be32_to_cpu(*--b
) + 1;
936 crypto_inc_byte(a
, size
);
938 EXPORT_SYMBOL_GPL(crypto_inc
);
940 static inline void crypto_xor_byte(u8
*a
, const u8
*b
, unsigned int size
)
946 void crypto_xor(u8
*dst
, const u8
*src
, unsigned int size
)
951 for (; size
>= 4; size
-= 4)
954 crypto_xor_byte((u8
*)a
, (u8
*)b
, size
);
956 EXPORT_SYMBOL_GPL(crypto_xor
);
958 static int __init
crypto_algapi_init(void)
964 static void __exit
crypto_algapi_exit(void)
969 module_init(crypto_algapi_init
);
970 module_exit(crypto_algapi_exit
);
972 MODULE_LICENSE("GPL");
973 MODULE_DESCRIPTION("Cryptographic algorithms API");