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 struct list_head
*crypto_more_spawns(struct crypto_alg
*alg
,
85 struct list_head
*stack
,
86 struct list_head
*top
,
87 struct list_head
*secondary_spawns
)
89 struct crypto_spawn
*spawn
, *n
;
91 if (list_empty(stack
))
94 spawn
= list_first_entry(stack
, struct crypto_spawn
, list
);
95 n
= list_entry(spawn
->list
.next
, struct crypto_spawn
, list
);
97 if (spawn
->alg
&& &n
->list
!= stack
&& !n
->alg
)
98 n
->alg
= (n
->list
.next
== stack
) ? alg
:
99 &list_entry(n
->list
.next
, struct crypto_spawn
,
102 list_move(&spawn
->list
, secondary_spawns
);
104 return &n
->list
== stack
? top
: &n
->inst
->alg
.cra_users
;
107 static void crypto_remove_spawn(struct crypto_spawn
*spawn
,
108 struct list_head
*list
)
110 struct crypto_instance
*inst
= spawn
->inst
;
111 struct crypto_template
*tmpl
= inst
->tmpl
;
113 if (crypto_is_dead(&inst
->alg
))
116 inst
->alg
.cra_flags
|= CRYPTO_ALG_DEAD
;
117 if (hlist_unhashed(&inst
->list
))
120 if (!tmpl
|| !crypto_tmpl_get(tmpl
))
123 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER
, &inst
->alg
);
124 list_move(&inst
->alg
.cra_list
, list
);
125 hlist_del(&inst
->list
);
126 inst
->alg
.cra_destroy
= crypto_destroy_instance
;
128 BUG_ON(!list_empty(&inst
->alg
.cra_users
));
131 static void crypto_remove_spawns(struct crypto_alg
*alg
,
132 struct list_head
*list
,
133 struct crypto_alg
*nalg
)
135 u32 new_type
= (nalg
?: alg
)->cra_flags
;
136 struct crypto_spawn
*spawn
, *n
;
137 LIST_HEAD(secondary_spawns
);
138 struct list_head
*spawns
;
142 spawns
= &alg
->cra_users
;
143 list_for_each_entry_safe(spawn
, n
, spawns
, list
) {
144 if ((spawn
->alg
->cra_flags
^ new_type
) & spawn
->mask
)
147 list_move(&spawn
->list
, &top
);
152 while (!list_empty(spawns
)) {
153 struct crypto_instance
*inst
;
155 spawn
= list_first_entry(spawns
, struct crypto_spawn
,
159 BUG_ON(&inst
->alg
== alg
);
161 list_move(&spawn
->list
, &stack
);
163 if (&inst
->alg
== nalg
)
167 spawns
= &inst
->alg
.cra_users
;
169 } while ((spawns
= crypto_more_spawns(alg
, &stack
, &top
,
170 &secondary_spawns
)));
172 list_for_each_entry_safe(spawn
, n
, &secondary_spawns
, list
) {
174 list_move(&spawn
->list
, &spawn
->alg
->cra_users
);
176 crypto_remove_spawn(spawn
, list
);
180 static struct crypto_larval
*__crypto_register_alg(struct crypto_alg
*alg
)
182 struct crypto_alg
*q
;
183 struct crypto_larval
*larval
;
186 if (crypto_is_dead(alg
))
189 INIT_LIST_HEAD(&alg
->cra_users
);
192 alg
->cra_flags
&= ~CRYPTO_ALG_TESTED
;
196 atomic_set(&alg
->cra_refcnt
, 1);
197 list_for_each_entry(q
, &crypto_alg_list
, cra_list
) {
201 if (crypto_is_moribund(q
))
204 if (crypto_is_larval(q
)) {
205 if (!strcmp(alg
->cra_driver_name
, q
->cra_driver_name
))
210 if (!strcmp(q
->cra_driver_name
, alg
->cra_name
) ||
211 !strcmp(q
->cra_name
, alg
->cra_driver_name
))
215 larval
= crypto_larval_alloc(alg
->cra_name
,
216 alg
->cra_flags
| CRYPTO_ALG_TESTED
, 0);
221 larval
->adult
= crypto_mod_get(alg
);
225 atomic_set(&larval
->alg
.cra_refcnt
, 1);
226 memcpy(larval
->alg
.cra_driver_name
, alg
->cra_driver_name
,
227 CRYPTO_MAX_ALG_NAME
);
228 larval
->alg
.cra_priority
= alg
->cra_priority
;
230 list_add(&alg
->cra_list
, &crypto_alg_list
);
231 list_add(&larval
->alg
.cra_list
, &crypto_alg_list
);
239 larval
= ERR_PTR(ret
);
243 void crypto_alg_tested(const char *name
, int err
)
245 struct crypto_larval
*test
;
246 struct crypto_alg
*alg
;
247 struct crypto_alg
*q
;
250 down_write(&crypto_alg_sem
);
251 list_for_each_entry(q
, &crypto_alg_list
, cra_list
) {
252 if (crypto_is_moribund(q
) || !crypto_is_larval(q
))
255 test
= (struct crypto_larval
*)q
;
257 if (!strcmp(q
->cra_driver_name
, name
))
261 printk(KERN_ERR
"alg: Unexpected test result for %s: %d\n", name
, err
);
265 q
->cra_flags
|= CRYPTO_ALG_DEAD
;
267 if (err
|| list_empty(&alg
->cra_list
))
270 alg
->cra_flags
|= CRYPTO_ALG_TESTED
;
272 list_for_each_entry(q
, &crypto_alg_list
, cra_list
) {
276 if (crypto_is_moribund(q
))
279 if (crypto_is_larval(q
)) {
280 struct crypto_larval
*larval
= (void *)q
;
283 * Check to see if either our generic name or
284 * specific name can satisfy the name requested
285 * by the larval entry q.
287 if (strcmp(alg
->cra_name
, q
->cra_name
) &&
288 strcmp(alg
->cra_driver_name
, q
->cra_name
))
293 if ((q
->cra_flags
^ alg
->cra_flags
) & larval
->mask
)
295 if (!crypto_mod_get(alg
))
299 complete_all(&larval
->completion
);
303 if (strcmp(alg
->cra_name
, q
->cra_name
))
306 if (strcmp(alg
->cra_driver_name
, q
->cra_driver_name
) &&
307 q
->cra_priority
> alg
->cra_priority
)
310 crypto_remove_spawns(q
, &list
, alg
);
314 complete_all(&test
->completion
);
317 up_write(&crypto_alg_sem
);
319 crypto_remove_final(&list
);
321 EXPORT_SYMBOL_GPL(crypto_alg_tested
);
323 static void crypto_remove_final(struct list_head
*list
)
325 struct crypto_alg
*alg
;
326 struct crypto_alg
*n
;
328 list_for_each_entry_safe(alg
, n
, list
, cra_list
) {
329 list_del_init(&alg
->cra_list
);
334 static void crypto_wait_for_test(struct crypto_larval
*larval
)
338 err
= crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER
, larval
->adult
);
339 if (err
!= NOTIFY_STOP
) {
340 if (WARN_ON(err
!= NOTIFY_DONE
))
342 crypto_alg_tested(larval
->alg
.cra_driver_name
, 0);
345 err
= wait_for_completion_interruptible(&larval
->completion
);
349 crypto_larval_kill(&larval
->alg
);
352 int crypto_register_alg(struct crypto_alg
*alg
)
354 struct crypto_larval
*larval
;
357 err
= crypto_check_alg(alg
);
361 down_write(&crypto_alg_sem
);
362 larval
= __crypto_register_alg(alg
);
363 up_write(&crypto_alg_sem
);
366 return PTR_ERR(larval
);
368 crypto_wait_for_test(larval
);
371 EXPORT_SYMBOL_GPL(crypto_register_alg
);
373 static int crypto_remove_alg(struct crypto_alg
*alg
, struct list_head
*list
)
375 if (unlikely(list_empty(&alg
->cra_list
)))
378 alg
->cra_flags
|= CRYPTO_ALG_DEAD
;
380 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER
, alg
);
381 list_del_init(&alg
->cra_list
);
382 crypto_remove_spawns(alg
, list
, NULL
);
387 int crypto_unregister_alg(struct crypto_alg
*alg
)
392 down_write(&crypto_alg_sem
);
393 ret
= crypto_remove_alg(alg
, &list
);
394 up_write(&crypto_alg_sem
);
399 BUG_ON(atomic_read(&alg
->cra_refcnt
) != 1);
400 if (alg
->cra_destroy
)
401 alg
->cra_destroy(alg
);
403 crypto_remove_final(&list
);
406 EXPORT_SYMBOL_GPL(crypto_unregister_alg
);
408 int crypto_register_template(struct crypto_template
*tmpl
)
410 struct crypto_template
*q
;
413 down_write(&crypto_alg_sem
);
415 list_for_each_entry(q
, &crypto_template_list
, list
) {
420 list_add(&tmpl
->list
, &crypto_template_list
);
421 crypto_notify(CRYPTO_MSG_TMPL_REGISTER
, tmpl
);
424 up_write(&crypto_alg_sem
);
427 EXPORT_SYMBOL_GPL(crypto_register_template
);
429 void crypto_unregister_template(struct crypto_template
*tmpl
)
431 struct crypto_instance
*inst
;
432 struct hlist_node
*p
, *n
;
433 struct hlist_head
*list
;
436 down_write(&crypto_alg_sem
);
438 BUG_ON(list_empty(&tmpl
->list
));
439 list_del_init(&tmpl
->list
);
441 list
= &tmpl
->instances
;
442 hlist_for_each_entry(inst
, p
, list
, list
) {
443 int err
= crypto_remove_alg(&inst
->alg
, &users
);
447 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER
, tmpl
);
449 up_write(&crypto_alg_sem
);
451 hlist_for_each_entry_safe(inst
, p
, n
, list
, list
) {
452 BUG_ON(atomic_read(&inst
->alg
.cra_refcnt
) != 1);
455 crypto_remove_final(&users
);
457 EXPORT_SYMBOL_GPL(crypto_unregister_template
);
459 static struct crypto_template
*__crypto_lookup_template(const char *name
)
461 struct crypto_template
*q
, *tmpl
= NULL
;
463 down_read(&crypto_alg_sem
);
464 list_for_each_entry(q
, &crypto_template_list
, list
) {
465 if (strcmp(q
->name
, name
))
467 if (unlikely(!crypto_tmpl_get(q
)))
473 up_read(&crypto_alg_sem
);
478 struct crypto_template
*crypto_lookup_template(const char *name
)
480 return try_then_request_module(__crypto_lookup_template(name
), name
);
482 EXPORT_SYMBOL_GPL(crypto_lookup_template
);
484 int crypto_register_instance(struct crypto_template
*tmpl
,
485 struct crypto_instance
*inst
)
487 struct crypto_larval
*larval
;
490 err
= crypto_check_alg(&inst
->alg
);
494 inst
->alg
.cra_module
= tmpl
->module
;
496 down_write(&crypto_alg_sem
);
498 larval
= __crypto_register_alg(&inst
->alg
);
502 hlist_add_head(&inst
->list
, &tmpl
->instances
);
506 up_write(&crypto_alg_sem
);
508 err
= PTR_ERR(larval
);
512 crypto_wait_for_test(larval
);
518 EXPORT_SYMBOL_GPL(crypto_register_instance
);
520 int crypto_init_spawn(struct crypto_spawn
*spawn
, struct crypto_alg
*alg
,
521 struct crypto_instance
*inst
, u32 mask
)
528 down_write(&crypto_alg_sem
);
529 if (!crypto_is_moribund(alg
)) {
530 list_add(&spawn
->list
, &alg
->cra_users
);
534 up_write(&crypto_alg_sem
);
538 EXPORT_SYMBOL_GPL(crypto_init_spawn
);
540 int crypto_init_spawn2(struct crypto_spawn
*spawn
, struct crypto_alg
*alg
,
541 struct crypto_instance
*inst
,
542 const struct crypto_type
*frontend
)
546 if (frontend
&& (alg
->cra_flags
^ frontend
->type
) & frontend
->maskset
)
549 spawn
->frontend
= frontend
;
550 err
= crypto_init_spawn(spawn
, alg
, inst
, frontend
->maskset
);
555 EXPORT_SYMBOL_GPL(crypto_init_spawn2
);
557 void crypto_drop_spawn(struct crypto_spawn
*spawn
)
562 down_write(&crypto_alg_sem
);
563 list_del(&spawn
->list
);
564 up_write(&crypto_alg_sem
);
566 EXPORT_SYMBOL_GPL(crypto_drop_spawn
);
568 static struct crypto_alg
*crypto_spawn_alg(struct crypto_spawn
*spawn
)
570 struct crypto_alg
*alg
;
571 struct crypto_alg
*alg2
;
573 down_read(&crypto_alg_sem
);
577 alg2
= crypto_mod_get(alg2
);
578 up_read(&crypto_alg_sem
);
582 crypto_shoot_alg(alg
);
583 return ERR_PTR(-EAGAIN
);
589 struct crypto_tfm
*crypto_spawn_tfm(struct crypto_spawn
*spawn
, u32 type
,
592 struct crypto_alg
*alg
;
593 struct crypto_tfm
*tfm
;
595 alg
= crypto_spawn_alg(spawn
);
597 return ERR_CAST(alg
);
599 tfm
= ERR_PTR(-EINVAL
);
600 if (unlikely((alg
->cra_flags
^ type
) & mask
))
603 tfm
= __crypto_alloc_tfm(alg
, type
, mask
);
613 EXPORT_SYMBOL_GPL(crypto_spawn_tfm
);
615 void *crypto_spawn_tfm2(struct crypto_spawn
*spawn
)
617 struct crypto_alg
*alg
;
618 struct crypto_tfm
*tfm
;
620 alg
= crypto_spawn_alg(spawn
);
622 return ERR_CAST(alg
);
624 tfm
= crypto_create_tfm(alg
, spawn
->frontend
);
634 EXPORT_SYMBOL_GPL(crypto_spawn_tfm2
);
636 int crypto_register_notifier(struct notifier_block
*nb
)
638 return blocking_notifier_chain_register(&crypto_chain
, nb
);
640 EXPORT_SYMBOL_GPL(crypto_register_notifier
);
642 int crypto_unregister_notifier(struct notifier_block
*nb
)
644 return blocking_notifier_chain_unregister(&crypto_chain
, nb
);
646 EXPORT_SYMBOL_GPL(crypto_unregister_notifier
);
648 struct crypto_attr_type
*crypto_get_attr_type(struct rtattr
**tb
)
650 struct rtattr
*rta
= tb
[0];
651 struct crypto_attr_type
*algt
;
654 return ERR_PTR(-ENOENT
);
655 if (RTA_PAYLOAD(rta
) < sizeof(*algt
))
656 return ERR_PTR(-EINVAL
);
657 if (rta
->rta_type
!= CRYPTOA_TYPE
)
658 return ERR_PTR(-EINVAL
);
660 algt
= RTA_DATA(rta
);
664 EXPORT_SYMBOL_GPL(crypto_get_attr_type
);
666 int crypto_check_attr_type(struct rtattr
**tb
, u32 type
)
668 struct crypto_attr_type
*algt
;
670 algt
= crypto_get_attr_type(tb
);
672 return PTR_ERR(algt
);
674 if ((algt
->type
^ type
) & algt
->mask
)
679 EXPORT_SYMBOL_GPL(crypto_check_attr_type
);
681 const char *crypto_attr_alg_name(struct rtattr
*rta
)
683 struct crypto_attr_alg
*alga
;
686 return ERR_PTR(-ENOENT
);
687 if (RTA_PAYLOAD(rta
) < sizeof(*alga
))
688 return ERR_PTR(-EINVAL
);
689 if (rta
->rta_type
!= CRYPTOA_ALG
)
690 return ERR_PTR(-EINVAL
);
692 alga
= RTA_DATA(rta
);
693 alga
->name
[CRYPTO_MAX_ALG_NAME
- 1] = 0;
697 EXPORT_SYMBOL_GPL(crypto_attr_alg_name
);
699 struct crypto_alg
*crypto_attr_alg2(struct rtattr
*rta
,
700 const struct crypto_type
*frontend
,
706 name
= crypto_attr_alg_name(rta
);
711 return crypto_find_alg(name
, frontend
, type
, mask
);
713 EXPORT_SYMBOL_GPL(crypto_attr_alg2
);
715 int crypto_attr_u32(struct rtattr
*rta
, u32
*num
)
717 struct crypto_attr_u32
*nu32
;
721 if (RTA_PAYLOAD(rta
) < sizeof(*nu32
))
723 if (rta
->rta_type
!= CRYPTOA_U32
)
726 nu32
= RTA_DATA(rta
);
731 EXPORT_SYMBOL_GPL(crypto_attr_u32
);
733 void *crypto_alloc_instance2(const char *name
, struct crypto_alg
*alg
,
736 struct crypto_instance
*inst
;
740 p
= kzalloc(head
+ sizeof(*inst
) + sizeof(struct crypto_spawn
),
743 return ERR_PTR(-ENOMEM
);
745 inst
= (void *)(p
+ head
);
748 if (snprintf(inst
->alg
.cra_name
, CRYPTO_MAX_ALG_NAME
, "%s(%s)", name
,
749 alg
->cra_name
) >= CRYPTO_MAX_ALG_NAME
)
752 if (snprintf(inst
->alg
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
, "%s(%s)",
753 name
, alg
->cra_driver_name
) >= CRYPTO_MAX_ALG_NAME
)
762 EXPORT_SYMBOL_GPL(crypto_alloc_instance2
);
764 struct crypto_instance
*crypto_alloc_instance(const char *name
,
765 struct crypto_alg
*alg
)
767 struct crypto_instance
*inst
;
768 struct crypto_spawn
*spawn
;
771 inst
= crypto_alloc_instance2(name
, alg
, 0);
775 spawn
= crypto_instance_ctx(inst
);
776 err
= crypto_init_spawn(spawn
, alg
, inst
,
777 CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_ASYNC
);
791 EXPORT_SYMBOL_GPL(crypto_alloc_instance
);
793 void crypto_init_queue(struct crypto_queue
*queue
, unsigned int max_qlen
)
795 INIT_LIST_HEAD(&queue
->list
);
796 queue
->backlog
= &queue
->list
;
798 queue
->max_qlen
= max_qlen
;
800 EXPORT_SYMBOL_GPL(crypto_init_queue
);
802 int crypto_enqueue_request(struct crypto_queue
*queue
,
803 struct crypto_async_request
*request
)
805 int err
= -EINPROGRESS
;
807 if (unlikely(queue
->qlen
>= queue
->max_qlen
)) {
809 if (!(request
->flags
& CRYPTO_TFM_REQ_MAY_BACKLOG
))
811 if (queue
->backlog
== &queue
->list
)
812 queue
->backlog
= &request
->list
;
816 list_add_tail(&request
->list
, &queue
->list
);
821 EXPORT_SYMBOL_GPL(crypto_enqueue_request
);
823 void *__crypto_dequeue_request(struct crypto_queue
*queue
, unsigned int offset
)
825 struct list_head
*request
;
827 if (unlikely(!queue
->qlen
))
832 if (queue
->backlog
!= &queue
->list
)
833 queue
->backlog
= queue
->backlog
->next
;
835 request
= queue
->list
.next
;
838 return (char *)list_entry(request
, struct crypto_async_request
, list
) -
841 EXPORT_SYMBOL_GPL(__crypto_dequeue_request
);
843 struct crypto_async_request
*crypto_dequeue_request(struct crypto_queue
*queue
)
845 return __crypto_dequeue_request(queue
, 0);
847 EXPORT_SYMBOL_GPL(crypto_dequeue_request
);
849 int crypto_tfm_in_queue(struct crypto_queue
*queue
, struct crypto_tfm
*tfm
)
851 struct crypto_async_request
*req
;
853 list_for_each_entry(req
, &queue
->list
, list
) {
860 EXPORT_SYMBOL_GPL(crypto_tfm_in_queue
);
862 static inline void crypto_inc_byte(u8
*a
, unsigned int size
)
867 for (; size
; size
--) {
875 void crypto_inc(u8
*a
, unsigned int size
)
877 __be32
*b
= (__be32
*)(a
+ size
);
880 for (; size
>= 4; size
-= 4) {
881 c
= be32_to_cpu(*--b
) + 1;
887 crypto_inc_byte(a
, size
);
889 EXPORT_SYMBOL_GPL(crypto_inc
);
891 static inline void crypto_xor_byte(u8
*a
, const u8
*b
, unsigned int size
)
897 void crypto_xor(u8
*dst
, const u8
*src
, unsigned int size
)
902 for (; size
>= 4; size
-= 4)
905 crypto_xor_byte((u8
*)a
, (u8
*)b
, size
);
907 EXPORT_SYMBOL_GPL(crypto_xor
);
909 static int __init
crypto_algapi_init(void)
915 static void __exit
crypto_algapi_exit(void)
920 module_init(crypto_algapi_init
);
921 module_exit(crypto_algapi_exit
);
923 MODULE_LICENSE("GPL");
924 MODULE_DESCRIPTION("Cryptographic algorithms API");