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(&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_alignmask
& alg
->cra_blocksize
)
69 if (alg
->cra_blocksize
> PAGE_SIZE
/ 8)
72 if (alg
->cra_priority
< 0)
75 return crypto_set_driver_name(alg
);
78 static void crypto_destroy_instance(struct crypto_alg
*alg
)
80 struct crypto_instance
*inst
= (void *)alg
;
81 struct crypto_template
*tmpl
= inst
->tmpl
;
84 crypto_tmpl_put(tmpl
);
87 static void crypto_remove_spawns(struct list_head
*spawns
,
88 struct list_head
*list
)
90 struct crypto_spawn
*spawn
, *n
;
92 list_for_each_entry_safe(spawn
, n
, spawns
, list
) {
93 struct crypto_instance
*inst
= spawn
->inst
;
94 struct crypto_template
*tmpl
= inst
->tmpl
;
96 list_del_init(&spawn
->list
);
99 if (crypto_is_dead(&inst
->alg
))
102 inst
->alg
.cra_flags
|= CRYPTO_ALG_DEAD
;
103 if (!tmpl
|| !crypto_tmpl_get(tmpl
))
106 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER
, &inst
->alg
);
107 list_move(&inst
->alg
.cra_list
, list
);
108 hlist_del(&inst
->list
);
109 inst
->alg
.cra_destroy
= crypto_destroy_instance
;
111 if (!list_empty(&inst
->alg
.cra_users
)) {
112 if (&n
->list
== spawns
)
113 n
= list_entry(inst
->alg
.cra_users
.next
,
115 __list_splice(&inst
->alg
.cra_users
, spawns
->prev
);
120 static int __crypto_register_alg(struct crypto_alg
*alg
,
121 struct list_head
*list
)
123 struct crypto_alg
*q
;
126 if (crypto_is_dead(alg
))
129 INIT_LIST_HEAD(&alg
->cra_users
);
133 atomic_set(&alg
->cra_refcnt
, 1);
134 list_for_each_entry(q
, &crypto_alg_list
, cra_list
) {
138 if (crypto_is_moribund(q
))
141 if (crypto_is_larval(q
)) {
142 struct crypto_larval
*larval
= (void *)q
;
144 if (strcmp(alg
->cra_name
, q
->cra_name
) &&
145 strcmp(alg
->cra_driver_name
, q
->cra_name
))
150 if ((q
->cra_flags
^ alg
->cra_flags
) & larval
->mask
)
152 if (!crypto_mod_get(alg
))
156 complete(&larval
->completion
);
160 if (strcmp(alg
->cra_name
, q
->cra_name
))
163 if (strcmp(alg
->cra_driver_name
, q
->cra_driver_name
) &&
164 q
->cra_priority
> alg
->cra_priority
)
167 crypto_remove_spawns(&q
->cra_users
, list
);
170 list_add(&alg
->cra_list
, &crypto_alg_list
);
172 crypto_notify(CRYPTO_MSG_ALG_REGISTER
, alg
);
179 static void crypto_remove_final(struct list_head
*list
)
181 struct crypto_alg
*alg
;
182 struct crypto_alg
*n
;
184 list_for_each_entry_safe(alg
, n
, list
, cra_list
) {
185 list_del_init(&alg
->cra_list
);
190 int crypto_register_alg(struct crypto_alg
*alg
)
195 err
= crypto_check_alg(alg
);
199 down_write(&crypto_alg_sem
);
200 err
= __crypto_register_alg(alg
, &list
);
201 up_write(&crypto_alg_sem
);
203 crypto_remove_final(&list
);
206 EXPORT_SYMBOL_GPL(crypto_register_alg
);
208 static int crypto_remove_alg(struct crypto_alg
*alg
, struct list_head
*list
)
210 if (unlikely(list_empty(&alg
->cra_list
)))
213 alg
->cra_flags
|= CRYPTO_ALG_DEAD
;
215 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER
, alg
);
216 list_del_init(&alg
->cra_list
);
217 crypto_remove_spawns(&alg
->cra_users
, list
);
222 int crypto_unregister_alg(struct crypto_alg
*alg
)
227 down_write(&crypto_alg_sem
);
228 ret
= crypto_remove_alg(alg
, &list
);
229 up_write(&crypto_alg_sem
);
234 BUG_ON(atomic_read(&alg
->cra_refcnt
) != 1);
235 if (alg
->cra_destroy
)
236 alg
->cra_destroy(alg
);
238 crypto_remove_final(&list
);
241 EXPORT_SYMBOL_GPL(crypto_unregister_alg
);
243 int crypto_register_template(struct crypto_template
*tmpl
)
245 struct crypto_template
*q
;
248 down_write(&crypto_alg_sem
);
250 list_for_each_entry(q
, &crypto_template_list
, list
) {
255 list_add(&tmpl
->list
, &crypto_template_list
);
256 crypto_notify(CRYPTO_MSG_TMPL_REGISTER
, tmpl
);
259 up_write(&crypto_alg_sem
);
262 EXPORT_SYMBOL_GPL(crypto_register_template
);
264 void crypto_unregister_template(struct crypto_template
*tmpl
)
266 struct crypto_instance
*inst
;
267 struct hlist_node
*p
, *n
;
268 struct hlist_head
*list
;
271 down_write(&crypto_alg_sem
);
273 BUG_ON(list_empty(&tmpl
->list
));
274 list_del_init(&tmpl
->list
);
276 list
= &tmpl
->instances
;
277 hlist_for_each_entry(inst
, p
, list
, list
) {
278 int err
= crypto_remove_alg(&inst
->alg
, &users
);
282 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER
, tmpl
);
284 up_write(&crypto_alg_sem
);
286 hlist_for_each_entry_safe(inst
, p
, n
, list
, list
) {
287 BUG_ON(atomic_read(&inst
->alg
.cra_refcnt
) != 1);
290 crypto_remove_final(&users
);
292 EXPORT_SYMBOL_GPL(crypto_unregister_template
);
294 static struct crypto_template
*__crypto_lookup_template(const char *name
)
296 struct crypto_template
*q
, *tmpl
= NULL
;
298 down_read(&crypto_alg_sem
);
299 list_for_each_entry(q
, &crypto_template_list
, list
) {
300 if (strcmp(q
->name
, name
))
302 if (unlikely(!crypto_tmpl_get(q
)))
308 up_read(&crypto_alg_sem
);
313 struct crypto_template
*crypto_lookup_template(const char *name
)
315 return try_then_request_module(__crypto_lookup_template(name
), name
);
317 EXPORT_SYMBOL_GPL(crypto_lookup_template
);
319 int crypto_register_instance(struct crypto_template
*tmpl
,
320 struct crypto_instance
*inst
)
325 if (inst
->alg
.cra_destroy
)
328 err
= crypto_check_alg(&inst
->alg
);
332 inst
->alg
.cra_module
= tmpl
->module
;
334 down_write(&crypto_alg_sem
);
336 err
= __crypto_register_alg(&inst
->alg
, &list
);
340 hlist_add_head(&inst
->list
, &tmpl
->instances
);
344 up_write(&crypto_alg_sem
);
346 crypto_remove_final(&list
);
351 EXPORT_SYMBOL_GPL(crypto_register_instance
);
353 int crypto_init_spawn(struct crypto_spawn
*spawn
, struct crypto_alg
*alg
,
354 struct crypto_instance
*inst
)
360 down_write(&crypto_alg_sem
);
361 if (!crypto_is_moribund(alg
)) {
362 list_add(&spawn
->list
, &alg
->cra_users
);
366 up_write(&crypto_alg_sem
);
370 EXPORT_SYMBOL_GPL(crypto_init_spawn
);
372 void crypto_drop_spawn(struct crypto_spawn
*spawn
)
374 down_write(&crypto_alg_sem
);
375 list_del(&spawn
->list
);
376 up_write(&crypto_alg_sem
);
378 EXPORT_SYMBOL_GPL(crypto_drop_spawn
);
380 struct crypto_tfm
*crypto_spawn_tfm(struct crypto_spawn
*spawn
, u32 type
,
383 struct crypto_alg
*alg
;
384 struct crypto_alg
*alg2
;
385 struct crypto_tfm
*tfm
;
387 down_read(&crypto_alg_sem
);
391 alg2
= crypto_mod_get(alg2
);
392 up_read(&crypto_alg_sem
);
396 crypto_shoot_alg(alg
);
397 return ERR_PTR(-EAGAIN
);
400 tfm
= ERR_PTR(-EINVAL
);
401 if (unlikely((alg
->cra_flags
^ type
) & mask
))
404 tfm
= __crypto_alloc_tfm(alg
, type
, mask
);
414 EXPORT_SYMBOL_GPL(crypto_spawn_tfm
);
416 int crypto_register_notifier(struct notifier_block
*nb
)
418 return blocking_notifier_chain_register(&crypto_chain
, nb
);
420 EXPORT_SYMBOL_GPL(crypto_register_notifier
);
422 int crypto_unregister_notifier(struct notifier_block
*nb
)
424 return blocking_notifier_chain_unregister(&crypto_chain
, nb
);
426 EXPORT_SYMBOL_GPL(crypto_unregister_notifier
);
428 struct crypto_alg
*crypto_get_attr_alg(void *param
, unsigned int len
,
431 struct rtattr
*rta
= param
;
432 struct crypto_attr_alg
*alga
;
434 if (!RTA_OK(rta
, len
))
435 return ERR_PTR(-EBADR
);
436 if (rta
->rta_type
!= CRYPTOA_ALG
|| RTA_PAYLOAD(rta
) < sizeof(*alga
))
437 return ERR_PTR(-EINVAL
);
439 alga
= RTA_DATA(rta
);
440 alga
->name
[CRYPTO_MAX_ALG_NAME
- 1] = 0;
442 return crypto_alg_mod_lookup(alga
->name
, type
, mask
);
444 EXPORT_SYMBOL_GPL(crypto_get_attr_alg
);
446 struct crypto_instance
*crypto_alloc_instance(const char *name
,
447 struct crypto_alg
*alg
)
449 struct crypto_instance
*inst
;
450 struct crypto_spawn
*spawn
;
453 inst
= kzalloc(sizeof(*inst
) + sizeof(*spawn
), GFP_KERNEL
);
455 return ERR_PTR(-ENOMEM
);
458 if (snprintf(inst
->alg
.cra_name
, CRYPTO_MAX_ALG_NAME
, "%s(%s)", name
,
459 alg
->cra_name
) >= CRYPTO_MAX_ALG_NAME
)
462 if (snprintf(inst
->alg
.cra_driver_name
, CRYPTO_MAX_ALG_NAME
, "%s(%s)",
463 name
, alg
->cra_driver_name
) >= CRYPTO_MAX_ALG_NAME
)
466 spawn
= crypto_instance_ctx(inst
);
467 err
= crypto_init_spawn(spawn
, alg
, inst
);
478 EXPORT_SYMBOL_GPL(crypto_alloc_instance
);
480 static int __init
crypto_algapi_init(void)
486 static void __exit
crypto_algapi_exit(void)
491 module_init(crypto_algapi_init
);
492 module_exit(crypto_algapi_exit
);
494 MODULE_LICENSE("GPL");
495 MODULE_DESCRIPTION("Cryptographic algorithms API");