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_larval(q
)) {
153 if (!strcmp(alg
->cra_driver_name
, q
->cra_driver_name
))
158 if (!strcmp(q
->cra_driver_name
, alg
->cra_name
) ||
159 !strcmp(q
->cra_name
, alg
->cra_driver_name
))
163 larval
= crypto_larval_alloc(alg
->cra_name
,
164 alg
->cra_flags
| CRYPTO_ALG_TESTED
, 0);
169 larval
->adult
= crypto_mod_get(alg
);
173 atomic_set(&larval
->alg
.cra_refcnt
, 1);
174 memcpy(larval
->alg
.cra_driver_name
, alg
->cra_driver_name
,
175 CRYPTO_MAX_ALG_NAME
);
176 larval
->alg
.cra_priority
= alg
->cra_priority
;
178 list_add(&alg
->cra_list
, &crypto_alg_list
);
179 list_add(&larval
->alg
.cra_list
, &crypto_alg_list
);
187 larval
= ERR_PTR(ret
);
191 void crypto_alg_tested(const char *name
, int err
)
193 struct crypto_larval
*test
;
194 struct crypto_alg
*alg
;
195 struct crypto_alg
*q
;
198 down_write(&crypto_alg_sem
);
199 list_for_each_entry(q
, &crypto_alg_list
, cra_list
) {
200 if (!crypto_is_larval(q
))
203 test
= (struct crypto_larval
*)q
;
205 if (!strcmp(q
->cra_driver_name
, name
))
209 printk(KERN_ERR
"alg: Unexpected test result for %s: %d\n", name
, err
);
214 if (err
|| list_empty(&alg
->cra_list
))
217 alg
->cra_flags
|= CRYPTO_ALG_TESTED
;
219 list_for_each_entry(q
, &crypto_alg_list
, cra_list
) {
223 if (crypto_is_moribund(q
))
226 if (crypto_is_larval(q
)) {
227 struct crypto_larval
*larval
= (void *)q
;
230 * Check to see if either our generic name or
231 * specific name can satisfy the name requested
232 * by the larval entry q.
234 if (strcmp(alg
->cra_name
, q
->cra_name
) &&
235 strcmp(alg
->cra_driver_name
, q
->cra_name
))
240 if ((q
->cra_flags
^ alg
->cra_flags
) & larval
->mask
)
242 if (!crypto_mod_get(alg
))
246 complete_all(&larval
->completion
);
250 if (strcmp(alg
->cra_name
, q
->cra_name
))
253 if (strcmp(alg
->cra_driver_name
, q
->cra_driver_name
) &&
254 q
->cra_priority
> alg
->cra_priority
)
257 crypto_remove_spawns(&q
->cra_users
, &list
, alg
->cra_flags
);
261 complete_all(&test
->completion
);
264 up_write(&crypto_alg_sem
);
266 crypto_remove_final(&list
);
268 EXPORT_SYMBOL_GPL(crypto_alg_tested
);
270 static void crypto_remove_final(struct list_head
*list
)
272 struct crypto_alg
*alg
;
273 struct crypto_alg
*n
;
275 list_for_each_entry_safe(alg
, n
, list
, cra_list
) {
276 list_del_init(&alg
->cra_list
);
281 static void crypto_wait_for_test(struct crypto_larval
*larval
)
285 err
= crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER
, larval
->adult
);
286 if (err
!= NOTIFY_STOP
) {
287 if (WARN_ON(err
!= NOTIFY_DONE
))
289 crypto_alg_tested(larval
->alg
.cra_driver_name
, 0);
292 err
= wait_for_completion_interruptible(&larval
->completion
);
296 crypto_larval_kill(&larval
->alg
);
299 int crypto_register_alg(struct crypto_alg
*alg
)
301 struct crypto_larval
*larval
;
304 err
= crypto_check_alg(alg
);
308 down_write(&crypto_alg_sem
);
309 larval
= __crypto_register_alg(alg
);
310 up_write(&crypto_alg_sem
);
313 return PTR_ERR(larval
);
315 crypto_wait_for_test(larval
);
318 EXPORT_SYMBOL_GPL(crypto_register_alg
);
320 static int crypto_remove_alg(struct crypto_alg
*alg
, struct list_head
*list
)
322 if (unlikely(list_empty(&alg
->cra_list
)))
325 alg
->cra_flags
|= CRYPTO_ALG_DEAD
;
327 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER
, alg
);
328 list_del_init(&alg
->cra_list
);
329 crypto_remove_spawns(&alg
->cra_users
, list
, alg
->cra_flags
);
334 int crypto_unregister_alg(struct crypto_alg
*alg
)
339 down_write(&crypto_alg_sem
);
340 ret
= crypto_remove_alg(alg
, &list
);
341 up_write(&crypto_alg_sem
);
346 BUG_ON(atomic_read(&alg
->cra_refcnt
) != 1);
347 if (alg
->cra_destroy
)
348 alg
->cra_destroy(alg
);
350 crypto_remove_final(&list
);
353 EXPORT_SYMBOL_GPL(crypto_unregister_alg
);
355 int crypto_register_template(struct crypto_template
*tmpl
)
357 struct crypto_template
*q
;
360 down_write(&crypto_alg_sem
);
362 list_for_each_entry(q
, &crypto_template_list
, list
) {
367 list_add(&tmpl
->list
, &crypto_template_list
);
368 crypto_notify(CRYPTO_MSG_TMPL_REGISTER
, tmpl
);
371 up_write(&crypto_alg_sem
);
374 EXPORT_SYMBOL_GPL(crypto_register_template
);
376 void crypto_unregister_template(struct crypto_template
*tmpl
)
378 struct crypto_instance
*inst
;
379 struct hlist_node
*p
, *n
;
380 struct hlist_head
*list
;
383 down_write(&crypto_alg_sem
);
385 BUG_ON(list_empty(&tmpl
->list
));
386 list_del_init(&tmpl
->list
);
388 list
= &tmpl
->instances
;
389 hlist_for_each_entry(inst
, p
, list
, list
) {
390 int err
= crypto_remove_alg(&inst
->alg
, &users
);
394 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER
, tmpl
);
396 up_write(&crypto_alg_sem
);
398 hlist_for_each_entry_safe(inst
, p
, n
, list
, list
) {
399 BUG_ON(atomic_read(&inst
->alg
.cra_refcnt
) != 1);
402 crypto_remove_final(&users
);
404 EXPORT_SYMBOL_GPL(crypto_unregister_template
);
406 static struct crypto_template
*__crypto_lookup_template(const char *name
)
408 struct crypto_template
*q
, *tmpl
= NULL
;
410 down_read(&crypto_alg_sem
);
411 list_for_each_entry(q
, &crypto_template_list
, list
) {
412 if (strcmp(q
->name
, name
))
414 if (unlikely(!crypto_tmpl_get(q
)))
420 up_read(&crypto_alg_sem
);
425 struct crypto_template
*crypto_lookup_template(const char *name
)
427 return try_then_request_module(__crypto_lookup_template(name
), name
);
429 EXPORT_SYMBOL_GPL(crypto_lookup_template
);
431 int crypto_register_instance(struct crypto_template
*tmpl
,
432 struct crypto_instance
*inst
)
434 struct crypto_larval
*larval
;
437 err
= crypto_check_alg(&inst
->alg
);
441 inst
->alg
.cra_module
= tmpl
->module
;
443 down_write(&crypto_alg_sem
);
445 larval
= __crypto_register_alg(&inst
->alg
);
449 hlist_add_head(&inst
->list
, &tmpl
->instances
);
453 up_write(&crypto_alg_sem
);
455 err
= PTR_ERR(larval
);
459 crypto_wait_for_test(larval
);
465 EXPORT_SYMBOL_GPL(crypto_register_instance
);
467 int crypto_init_spawn(struct crypto_spawn
*spawn
, struct crypto_alg
*alg
,
468 struct crypto_instance
*inst
, u32 mask
)
475 down_write(&crypto_alg_sem
);
476 if (!crypto_is_moribund(alg
)) {
477 list_add(&spawn
->list
, &alg
->cra_users
);
481 up_write(&crypto_alg_sem
);
485 EXPORT_SYMBOL_GPL(crypto_init_spawn
);
487 void crypto_drop_spawn(struct crypto_spawn
*spawn
)
489 down_write(&crypto_alg_sem
);
490 list_del(&spawn
->list
);
491 up_write(&crypto_alg_sem
);
493 EXPORT_SYMBOL_GPL(crypto_drop_spawn
);
495 struct crypto_tfm
*crypto_spawn_tfm(struct crypto_spawn
*spawn
, u32 type
,
498 struct crypto_alg
*alg
;
499 struct crypto_alg
*alg2
;
500 struct crypto_tfm
*tfm
;
502 down_read(&crypto_alg_sem
);
506 alg2
= crypto_mod_get(alg2
);
507 up_read(&crypto_alg_sem
);
511 crypto_shoot_alg(alg
);
512 return ERR_PTR(-EAGAIN
);
515 tfm
= ERR_PTR(-EINVAL
);
516 if (unlikely((alg
->cra_flags
^ type
) & mask
))
519 tfm
= __crypto_alloc_tfm(alg
, type
, mask
);
529 EXPORT_SYMBOL_GPL(crypto_spawn_tfm
);
531 int crypto_register_notifier(struct notifier_block
*nb
)
533 return blocking_notifier_chain_register(&crypto_chain
, nb
);
535 EXPORT_SYMBOL_GPL(crypto_register_notifier
);
537 int crypto_unregister_notifier(struct notifier_block
*nb
)
539 return blocking_notifier_chain_unregister(&crypto_chain
, nb
);
541 EXPORT_SYMBOL_GPL(crypto_unregister_notifier
);
543 struct crypto_attr_type
*crypto_get_attr_type(struct rtattr
**tb
)
545 struct rtattr
*rta
= tb
[0];
546 struct crypto_attr_type
*algt
;
549 return ERR_PTR(-ENOENT
);
550 if (RTA_PAYLOAD(rta
) < sizeof(*algt
))
551 return ERR_PTR(-EINVAL
);
552 if (rta
->rta_type
!= CRYPTOA_TYPE
)
553 return ERR_PTR(-EINVAL
);
555 algt
= RTA_DATA(rta
);
559 EXPORT_SYMBOL_GPL(crypto_get_attr_type
);
561 int crypto_check_attr_type(struct rtattr
**tb
, u32 type
)
563 struct crypto_attr_type
*algt
;
565 algt
= crypto_get_attr_type(tb
);
567 return PTR_ERR(algt
);
569 if ((algt
->type
^ type
) & algt
->mask
)
574 EXPORT_SYMBOL_GPL(crypto_check_attr_type
);
576 const char *crypto_attr_alg_name(struct rtattr
*rta
)
578 struct crypto_attr_alg
*alga
;
581 return ERR_PTR(-ENOENT
);
582 if (RTA_PAYLOAD(rta
) < sizeof(*alga
))
583 return ERR_PTR(-EINVAL
);
584 if (rta
->rta_type
!= CRYPTOA_ALG
)
585 return ERR_PTR(-EINVAL
);
587 alga
= RTA_DATA(rta
);
588 alga
->name
[CRYPTO_MAX_ALG_NAME
- 1] = 0;
592 EXPORT_SYMBOL_GPL(crypto_attr_alg_name
);
594 struct crypto_alg
*crypto_attr_alg(struct rtattr
*rta
, u32 type
, u32 mask
)
599 name
= crypto_attr_alg_name(rta
);
604 return crypto_alg_mod_lookup(name
, type
, mask
);
606 EXPORT_SYMBOL_GPL(crypto_attr_alg
);
608 int crypto_attr_u32(struct rtattr
*rta
, u32
*num
)
610 struct crypto_attr_u32
*nu32
;
614 if (RTA_PAYLOAD(rta
) < sizeof(*nu32
))
616 if (rta
->rta_type
!= CRYPTOA_U32
)
619 nu32
= RTA_DATA(rta
);
624 EXPORT_SYMBOL_GPL(crypto_attr_u32
);
626 struct crypto_instance
*crypto_alloc_instance(const char *name
,
627 struct crypto_alg
*alg
)
629 struct crypto_instance
*inst
;
630 struct crypto_spawn
*spawn
;
633 inst
= kzalloc(sizeof(*inst
) + sizeof(*spawn
), GFP_KERNEL
);
635 return ERR_PTR(-ENOMEM
);
638 if (snprintf(inst
->alg
.cra_name
, CRYPTO_MAX_ALG_NAME
, "%s(%s)", name
,
639 alg
->cra_name
) >= CRYPTO_MAX_ALG_NAME
)
642 if (snprintf(inst
->alg
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
, "%s(%s)",
643 name
, alg
->cra_driver_name
) >= CRYPTO_MAX_ALG_NAME
)
646 spawn
= crypto_instance_ctx(inst
);
647 err
= crypto_init_spawn(spawn
, alg
, inst
,
648 CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_ASYNC
);
659 EXPORT_SYMBOL_GPL(crypto_alloc_instance
);
661 void crypto_init_queue(struct crypto_queue
*queue
, unsigned int max_qlen
)
663 INIT_LIST_HEAD(&queue
->list
);
664 queue
->backlog
= &queue
->list
;
666 queue
->max_qlen
= max_qlen
;
668 EXPORT_SYMBOL_GPL(crypto_init_queue
);
670 int crypto_enqueue_request(struct crypto_queue
*queue
,
671 struct crypto_async_request
*request
)
673 int err
= -EINPROGRESS
;
675 if (unlikely(queue
->qlen
>= queue
->max_qlen
)) {
677 if (!(request
->flags
& CRYPTO_TFM_REQ_MAY_BACKLOG
))
679 if (queue
->backlog
== &queue
->list
)
680 queue
->backlog
= &request
->list
;
684 list_add_tail(&request
->list
, &queue
->list
);
689 EXPORT_SYMBOL_GPL(crypto_enqueue_request
);
691 struct crypto_async_request
*crypto_dequeue_request(struct crypto_queue
*queue
)
693 struct list_head
*request
;
695 if (unlikely(!queue
->qlen
))
700 if (queue
->backlog
!= &queue
->list
)
701 queue
->backlog
= queue
->backlog
->next
;
703 request
= queue
->list
.next
;
706 return list_entry(request
, struct crypto_async_request
, list
);
708 EXPORT_SYMBOL_GPL(crypto_dequeue_request
);
710 int crypto_tfm_in_queue(struct crypto_queue
*queue
, struct crypto_tfm
*tfm
)
712 struct crypto_async_request
*req
;
714 list_for_each_entry(req
, &queue
->list
, list
) {
721 EXPORT_SYMBOL_GPL(crypto_tfm_in_queue
);
723 static inline void crypto_inc_byte(u8
*a
, unsigned int size
)
728 for (; size
; size
--) {
736 void crypto_inc(u8
*a
, unsigned int size
)
738 __be32
*b
= (__be32
*)(a
+ size
);
741 for (; size
>= 4; size
-= 4) {
742 c
= be32_to_cpu(*--b
) + 1;
748 crypto_inc_byte(a
, size
);
750 EXPORT_SYMBOL_GPL(crypto_inc
);
752 static inline void crypto_xor_byte(u8
*a
, const u8
*b
, unsigned int size
)
758 void crypto_xor(u8
*dst
, const u8
*src
, unsigned int size
)
763 for (; size
>= 4; size
-= 4)
766 crypto_xor_byte((u8
*)a
, (u8
*)b
, size
);
768 EXPORT_SYMBOL_GPL(crypto_xor
);
770 static int __init
crypto_algapi_init(void)
776 static void __exit
crypto_algapi_exit(void)
781 module_init(crypto_algapi_init
);
782 module_exit(crypto_algapi_exit
);
784 MODULE_LICENSE("GPL");
785 MODULE_DESCRIPTION("Cryptographic algorithms API");