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 LIST_HEAD(crypto_template_list
);
26 void crypto_larval_error(const char *name
, u32 type
, u32 mask
)
28 struct crypto_alg
*alg
;
30 down_read(&crypto_alg_sem
);
31 alg
= __crypto_alg_lookup(name
, type
, mask
);
32 up_read(&crypto_alg_sem
);
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 (!tmpl
|| !crypto_tmpl_get(tmpl
))
101 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER
, &inst
->alg
);
102 list_move(&inst
->alg
.cra_list
, list
);
103 hlist_del(&inst
->list
);
104 inst
->alg
.cra_destroy
= crypto_destroy_instance
;
106 list_splice(&inst
->alg
.cra_users
, secondary_spawns
);
109 static void crypto_remove_spawns(struct list_head
*spawns
,
110 struct list_head
*list
, u32 new_type
)
112 struct crypto_spawn
*spawn
, *n
;
113 LIST_HEAD(secondary_spawns
);
115 list_for_each_entry_safe(spawn
, n
, spawns
, list
) {
116 if ((spawn
->alg
->cra_flags
^ new_type
) & spawn
->mask
)
119 crypto_remove_spawn(spawn
, list
, &secondary_spawns
);
122 while (!list_empty(&secondary_spawns
)) {
123 list_for_each_entry_safe(spawn
, n
, &secondary_spawns
, list
)
124 crypto_remove_spawn(spawn
, list
, &secondary_spawns
);
128 static int __crypto_register_alg(struct crypto_alg
*alg
,
129 struct list_head
*list
)
131 struct crypto_alg
*q
;
134 if (crypto_is_dead(alg
))
137 INIT_LIST_HEAD(&alg
->cra_users
);
141 atomic_set(&alg
->cra_refcnt
, 1);
142 list_for_each_entry(q
, &crypto_alg_list
, cra_list
) {
146 if (crypto_is_moribund(q
))
149 if (crypto_is_larval(q
)) {
150 struct crypto_larval
*larval
= (void *)q
;
152 if (strcmp(alg
->cra_name
, q
->cra_name
) &&
153 strcmp(alg
->cra_driver_name
, q
->cra_name
))
158 if ((q
->cra_flags
^ alg
->cra_flags
) & larval
->mask
)
160 if (!crypto_mod_get(alg
))
164 complete_all(&larval
->completion
);
168 if (strcmp(alg
->cra_name
, q
->cra_name
))
171 if (strcmp(alg
->cra_driver_name
, q
->cra_driver_name
) &&
172 q
->cra_priority
> alg
->cra_priority
)
175 crypto_remove_spawns(&q
->cra_users
, list
, alg
->cra_flags
);
178 list_add(&alg
->cra_list
, &crypto_alg_list
);
180 crypto_notify(CRYPTO_MSG_ALG_REGISTER
, alg
);
187 static void crypto_remove_final(struct list_head
*list
)
189 struct crypto_alg
*alg
;
190 struct crypto_alg
*n
;
192 list_for_each_entry_safe(alg
, n
, list
, cra_list
) {
193 list_del_init(&alg
->cra_list
);
198 int crypto_register_alg(struct crypto_alg
*alg
)
203 err
= crypto_check_alg(alg
);
207 down_write(&crypto_alg_sem
);
208 err
= __crypto_register_alg(alg
, &list
);
209 up_write(&crypto_alg_sem
);
211 crypto_remove_final(&list
);
214 EXPORT_SYMBOL_GPL(crypto_register_alg
);
216 static int crypto_remove_alg(struct crypto_alg
*alg
, struct list_head
*list
)
218 if (unlikely(list_empty(&alg
->cra_list
)))
221 alg
->cra_flags
|= CRYPTO_ALG_DEAD
;
223 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER
, alg
);
224 list_del_init(&alg
->cra_list
);
225 crypto_remove_spawns(&alg
->cra_users
, list
, alg
->cra_flags
);
230 int crypto_unregister_alg(struct crypto_alg
*alg
)
235 down_write(&crypto_alg_sem
);
236 ret
= crypto_remove_alg(alg
, &list
);
237 up_write(&crypto_alg_sem
);
242 BUG_ON(atomic_read(&alg
->cra_refcnt
) != 1);
243 if (alg
->cra_destroy
)
244 alg
->cra_destroy(alg
);
246 crypto_remove_final(&list
);
249 EXPORT_SYMBOL_GPL(crypto_unregister_alg
);
251 int crypto_register_template(struct crypto_template
*tmpl
)
253 struct crypto_template
*q
;
256 down_write(&crypto_alg_sem
);
258 list_for_each_entry(q
, &crypto_template_list
, list
) {
263 list_add(&tmpl
->list
, &crypto_template_list
);
264 crypto_notify(CRYPTO_MSG_TMPL_REGISTER
, tmpl
);
267 up_write(&crypto_alg_sem
);
270 EXPORT_SYMBOL_GPL(crypto_register_template
);
272 void crypto_unregister_template(struct crypto_template
*tmpl
)
274 struct crypto_instance
*inst
;
275 struct hlist_node
*p
, *n
;
276 struct hlist_head
*list
;
279 down_write(&crypto_alg_sem
);
281 BUG_ON(list_empty(&tmpl
->list
));
282 list_del_init(&tmpl
->list
);
284 list
= &tmpl
->instances
;
285 hlist_for_each_entry(inst
, p
, list
, list
) {
286 int err
= crypto_remove_alg(&inst
->alg
, &users
);
290 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER
, tmpl
);
292 up_write(&crypto_alg_sem
);
294 hlist_for_each_entry_safe(inst
, p
, n
, list
, list
) {
295 BUG_ON(atomic_read(&inst
->alg
.cra_refcnt
) != 1);
298 crypto_remove_final(&users
);
300 EXPORT_SYMBOL_GPL(crypto_unregister_template
);
302 static struct crypto_template
*__crypto_lookup_template(const char *name
)
304 struct crypto_template
*q
, *tmpl
= NULL
;
306 down_read(&crypto_alg_sem
);
307 list_for_each_entry(q
, &crypto_template_list
, list
) {
308 if (strcmp(q
->name
, name
))
310 if (unlikely(!crypto_tmpl_get(q
)))
316 up_read(&crypto_alg_sem
);
321 struct crypto_template
*crypto_lookup_template(const char *name
)
323 return try_then_request_module(__crypto_lookup_template(name
), name
);
325 EXPORT_SYMBOL_GPL(crypto_lookup_template
);
327 int crypto_register_instance(struct crypto_template
*tmpl
,
328 struct crypto_instance
*inst
)
333 if (inst
->alg
.cra_destroy
)
336 err
= crypto_check_alg(&inst
->alg
);
340 inst
->alg
.cra_module
= tmpl
->module
;
342 down_write(&crypto_alg_sem
);
344 err
= __crypto_register_alg(&inst
->alg
, &list
);
348 hlist_add_head(&inst
->list
, &tmpl
->instances
);
352 up_write(&crypto_alg_sem
);
354 crypto_remove_final(&list
);
359 EXPORT_SYMBOL_GPL(crypto_register_instance
);
361 int crypto_init_spawn(struct crypto_spawn
*spawn
, struct crypto_alg
*alg
,
362 struct crypto_instance
*inst
, u32 mask
)
369 down_write(&crypto_alg_sem
);
370 if (!crypto_is_moribund(alg
)) {
371 list_add(&spawn
->list
, &alg
->cra_users
);
375 up_write(&crypto_alg_sem
);
379 EXPORT_SYMBOL_GPL(crypto_init_spawn
);
381 void crypto_drop_spawn(struct crypto_spawn
*spawn
)
383 down_write(&crypto_alg_sem
);
384 list_del(&spawn
->list
);
385 up_write(&crypto_alg_sem
);
387 EXPORT_SYMBOL_GPL(crypto_drop_spawn
);
389 struct crypto_tfm
*crypto_spawn_tfm(struct crypto_spawn
*spawn
, u32 type
,
392 struct crypto_alg
*alg
;
393 struct crypto_alg
*alg2
;
394 struct crypto_tfm
*tfm
;
396 down_read(&crypto_alg_sem
);
400 alg2
= crypto_mod_get(alg2
);
401 up_read(&crypto_alg_sem
);
405 crypto_shoot_alg(alg
);
406 return ERR_PTR(-EAGAIN
);
409 tfm
= ERR_PTR(-EINVAL
);
410 if (unlikely((alg
->cra_flags
^ type
) & mask
))
413 tfm
= __crypto_alloc_tfm(alg
, type
, mask
);
423 EXPORT_SYMBOL_GPL(crypto_spawn_tfm
);
425 int crypto_register_notifier(struct notifier_block
*nb
)
427 return blocking_notifier_chain_register(&crypto_chain
, nb
);
429 EXPORT_SYMBOL_GPL(crypto_register_notifier
);
431 int crypto_unregister_notifier(struct notifier_block
*nb
)
433 return blocking_notifier_chain_unregister(&crypto_chain
, nb
);
435 EXPORT_SYMBOL_GPL(crypto_unregister_notifier
);
437 struct crypto_attr_type
*crypto_get_attr_type(struct rtattr
**tb
)
439 struct rtattr
*rta
= tb
[0];
440 struct crypto_attr_type
*algt
;
443 return ERR_PTR(-ENOENT
);
444 if (RTA_PAYLOAD(rta
) < sizeof(*algt
))
445 return ERR_PTR(-EINVAL
);
446 if (rta
->rta_type
!= CRYPTOA_TYPE
)
447 return ERR_PTR(-EINVAL
);
449 algt
= RTA_DATA(rta
);
453 EXPORT_SYMBOL_GPL(crypto_get_attr_type
);
455 int crypto_check_attr_type(struct rtattr
**tb
, u32 type
)
457 struct crypto_attr_type
*algt
;
459 algt
= crypto_get_attr_type(tb
);
461 return PTR_ERR(algt
);
463 if ((algt
->type
^ type
) & algt
->mask
)
468 EXPORT_SYMBOL_GPL(crypto_check_attr_type
);
470 struct crypto_alg
*crypto_attr_alg(struct rtattr
*rta
, u32 type
, u32 mask
)
472 struct crypto_attr_alg
*alga
;
475 return ERR_PTR(-ENOENT
);
476 if (RTA_PAYLOAD(rta
) < sizeof(*alga
))
477 return ERR_PTR(-EINVAL
);
478 if (rta
->rta_type
!= CRYPTOA_ALG
)
479 return ERR_PTR(-EINVAL
);
481 alga
= RTA_DATA(rta
);
482 alga
->name
[CRYPTO_MAX_ALG_NAME
- 1] = 0;
484 return crypto_alg_mod_lookup(alga
->name
, type
, mask
);
486 EXPORT_SYMBOL_GPL(crypto_attr_alg
);
488 int crypto_attr_u32(struct rtattr
*rta
, u32
*num
)
490 struct crypto_attr_u32
*nu32
;
494 if (RTA_PAYLOAD(rta
) < sizeof(*nu32
))
496 if (rta
->rta_type
!= CRYPTOA_U32
)
499 nu32
= RTA_DATA(rta
);
504 EXPORT_SYMBOL_GPL(crypto_attr_u32
);
506 struct crypto_instance
*crypto_alloc_instance(const char *name
,
507 struct crypto_alg
*alg
)
509 struct crypto_instance
*inst
;
510 struct crypto_spawn
*spawn
;
513 inst
= kzalloc(sizeof(*inst
) + sizeof(*spawn
), GFP_KERNEL
);
515 return ERR_PTR(-ENOMEM
);
518 if (snprintf(inst
->alg
.cra_name
, CRYPTO_MAX_ALG_NAME
, "%s(%s)", name
,
519 alg
->cra_name
) >= CRYPTO_MAX_ALG_NAME
)
522 if (snprintf(inst
->alg
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
, "%s(%s)",
523 name
, alg
->cra_driver_name
) >= CRYPTO_MAX_ALG_NAME
)
526 spawn
= crypto_instance_ctx(inst
);
527 err
= crypto_init_spawn(spawn
, alg
, inst
,
528 CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_ASYNC
);
539 EXPORT_SYMBOL_GPL(crypto_alloc_instance
);
541 void crypto_init_queue(struct crypto_queue
*queue
, unsigned int max_qlen
)
543 INIT_LIST_HEAD(&queue
->list
);
544 queue
->backlog
= &queue
->list
;
546 queue
->max_qlen
= max_qlen
;
548 EXPORT_SYMBOL_GPL(crypto_init_queue
);
550 int crypto_enqueue_request(struct crypto_queue
*queue
,
551 struct crypto_async_request
*request
)
553 int err
= -EINPROGRESS
;
555 if (unlikely(queue
->qlen
>= queue
->max_qlen
)) {
557 if (!(request
->flags
& CRYPTO_TFM_REQ_MAY_BACKLOG
))
559 if (queue
->backlog
== &queue
->list
)
560 queue
->backlog
= &request
->list
;
564 list_add_tail(&request
->list
, &queue
->list
);
569 EXPORT_SYMBOL_GPL(crypto_enqueue_request
);
571 struct crypto_async_request
*crypto_dequeue_request(struct crypto_queue
*queue
)
573 struct list_head
*request
;
575 if (unlikely(!queue
->qlen
))
580 if (queue
->backlog
!= &queue
->list
)
581 queue
->backlog
= queue
->backlog
->next
;
583 request
= queue
->list
.next
;
586 return list_entry(request
, struct crypto_async_request
, list
);
588 EXPORT_SYMBOL_GPL(crypto_dequeue_request
);
590 int crypto_tfm_in_queue(struct crypto_queue
*queue
, struct crypto_tfm
*tfm
)
592 struct crypto_async_request
*req
;
594 list_for_each_entry(req
, &queue
->list
, list
) {
601 EXPORT_SYMBOL_GPL(crypto_tfm_in_queue
);
603 static int __init
crypto_algapi_init(void)
609 static void __exit
crypto_algapi_exit(void)
614 module_init(crypto_algapi_init
);
615 module_exit(crypto_algapi_exit
);
617 MODULE_LICENSE("GPL");
618 MODULE_DESCRIPTION("Cryptographic algorithms API");