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 alg = crypto_alg_lookup(name, type, mask);
33 if (crypto_is_larval(alg)) {
34 struct crypto_larval *larval = (void *)alg;
35 complete_all(&larval->completion);
40 EXPORT_SYMBOL_GPL(crypto_larval_error);
42 static inline int crypto_set_driver_name(struct crypto_alg *alg)
44 static const char suffix[] = "-generic";
45 char *driver_name = alg->cra_driver_name;
51 len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
52 if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
55 memcpy(driver_name + len, suffix, sizeof(suffix));
59 static int crypto_check_alg(struct crypto_alg *alg)
61 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
64 if (alg->cra_blocksize > PAGE_SIZE / 8)
67 if (alg->cra_priority < 0)
70 return crypto_set_driver_name(alg);
73 static void crypto_destroy_instance(struct crypto_alg *alg)
75 struct crypto_instance *inst = (void *)alg;
76 struct crypto_template *tmpl = inst->tmpl;
79 crypto_tmpl_put(tmpl);
82 static void crypto_remove_spawn(struct crypto_spawn *spawn,
83 struct list_head *list,
84 struct list_head *secondary_spawns)
86 struct crypto_instance *inst = spawn->inst;
87 struct crypto_template *tmpl = inst->tmpl;
89 list_del_init(&spawn->list);
92 if (crypto_is_dead(&inst->alg))
95 inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
96 if (hlist_unhashed(&inst->list))
99 if (!tmpl || !crypto_tmpl_get(tmpl))
102 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
103 list_move(&inst->alg.cra_list, list);
104 hlist_del(&inst->list);
105 inst->alg.cra_destroy = crypto_destroy_instance;
107 list_splice(&inst->alg.cra_users, secondary_spawns);
110 static void crypto_remove_spawns(struct list_head *spawns,
111 struct list_head *list, u32 new_type)
113 struct crypto_spawn *spawn, *n;
114 LIST_HEAD(secondary_spawns);
116 list_for_each_entry_safe(spawn, n, spawns, list) {
117 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
120 crypto_remove_spawn(spawn, list, &secondary_spawns);
123 while (!list_empty(&secondary_spawns)) {
124 list_for_each_entry_safe(spawn, n, &secondary_spawns, list)
125 crypto_remove_spawn(spawn, list, &secondary_spawns);
129 static int __crypto_register_alg(struct crypto_alg *alg,
130 struct list_head *list)
132 struct crypto_alg *q;
135 if (crypto_is_dead(alg))
138 INIT_LIST_HEAD(&alg->cra_users);
142 atomic_set(&alg->cra_refcnt, 1);
143 list_for_each_entry(q, &crypto_alg_list, cra_list) {
147 if (crypto_is_moribund(q))
150 if (crypto_is_larval(q)) {
151 struct crypto_larval *larval = (void *)q;
154 * Check to see if either our generic name or
155 * specific name can satisfy the name requested
156 * by the larval entry q.
158 if (strcmp(alg->cra_name, q->cra_name) &&
159 strcmp(alg->cra_driver_name, q->cra_name))
164 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
166 if (!crypto_mod_get(alg))
170 complete_all(&larval->completion);
174 if (strcmp(alg->cra_name, q->cra_name))
177 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
178 q->cra_priority > alg->cra_priority)
181 crypto_remove_spawns(&q->cra_users, list, alg->cra_flags);
184 list_add(&alg->cra_list, &crypto_alg_list);
186 crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg);
193 static void crypto_remove_final(struct list_head *list)
195 struct crypto_alg *alg;
196 struct crypto_alg *n;
198 list_for_each_entry_safe(alg, n, list, cra_list) {
199 list_del_init(&alg->cra_list);
204 int crypto_register_alg(struct crypto_alg *alg)
209 err = crypto_check_alg(alg);
213 down_write(&crypto_alg_sem);
214 err = __crypto_register_alg(alg, &list);
215 up_write(&crypto_alg_sem);
217 crypto_remove_final(&list);
220 EXPORT_SYMBOL_GPL(crypto_register_alg);
222 static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
224 if (unlikely(list_empty(&alg->cra_list)))
227 alg->cra_flags |= CRYPTO_ALG_DEAD;
229 crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
230 list_del_init(&alg->cra_list);
231 crypto_remove_spawns(&alg->cra_users, list, alg->cra_flags);
236 int crypto_unregister_alg(struct crypto_alg *alg)
241 down_write(&crypto_alg_sem);
242 ret = crypto_remove_alg(alg, &list);
243 up_write(&crypto_alg_sem);
248 BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
249 if (alg->cra_destroy)
250 alg->cra_destroy(alg);
252 crypto_remove_final(&list);
255 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
257 int crypto_register_template(struct crypto_template *tmpl)
259 struct crypto_template *q;
262 down_write(&crypto_alg_sem);
264 list_for_each_entry(q, &crypto_template_list, list) {
269 list_add(&tmpl->list, &crypto_template_list);
270 crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
273 up_write(&crypto_alg_sem);
276 EXPORT_SYMBOL_GPL(crypto_register_template);
278 void crypto_unregister_template(struct crypto_template *tmpl)
280 struct crypto_instance *inst;
281 struct hlist_node *p, *n;
282 struct hlist_head *list;
285 down_write(&crypto_alg_sem);
287 BUG_ON(list_empty(&tmpl->list));
288 list_del_init(&tmpl->list);
290 list = &tmpl->instances;
291 hlist_for_each_entry(inst, p, list, list) {
292 int err = crypto_remove_alg(&inst->alg, &users);
296 crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
298 up_write(&crypto_alg_sem);
300 hlist_for_each_entry_safe(inst, p, n, list, list) {
301 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
304 crypto_remove_final(&users);
306 EXPORT_SYMBOL_GPL(crypto_unregister_template);
308 static struct crypto_template *__crypto_lookup_template(const char *name)
310 struct crypto_template *q, *tmpl = NULL;
312 down_read(&crypto_alg_sem);
313 list_for_each_entry(q, &crypto_template_list, list) {
314 if (strcmp(q->name, name))
316 if (unlikely(!crypto_tmpl_get(q)))
322 up_read(&crypto_alg_sem);
327 struct crypto_template *crypto_lookup_template(const char *name)
329 return try_then_request_module(__crypto_lookup_template(name), name);
331 EXPORT_SYMBOL_GPL(crypto_lookup_template);
333 int crypto_register_instance(struct crypto_template *tmpl,
334 struct crypto_instance *inst)
339 err = crypto_check_alg(&inst->alg);
343 inst->alg.cra_module = tmpl->module;
345 down_write(&crypto_alg_sem);
347 err = __crypto_register_alg(&inst->alg, &list);
351 hlist_add_head(&inst->list, &tmpl->instances);
355 up_write(&crypto_alg_sem);
357 crypto_remove_final(&list);
362 EXPORT_SYMBOL_GPL(crypto_register_instance);
364 int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
365 struct crypto_instance *inst, u32 mask)
372 down_write(&crypto_alg_sem);
373 if (!crypto_is_moribund(alg)) {
374 list_add(&spawn->list, &alg->cra_users);
378 up_write(&crypto_alg_sem);
382 EXPORT_SYMBOL_GPL(crypto_init_spawn);
384 void crypto_drop_spawn(struct crypto_spawn *spawn)
386 down_write(&crypto_alg_sem);
387 list_del(&spawn->list);
388 up_write(&crypto_alg_sem);
390 EXPORT_SYMBOL_GPL(crypto_drop_spawn);
392 struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
395 struct crypto_alg *alg;
396 struct crypto_alg *alg2;
397 struct crypto_tfm *tfm;
399 down_read(&crypto_alg_sem);
403 alg2 = crypto_mod_get(alg2);
404 up_read(&crypto_alg_sem);
408 crypto_shoot_alg(alg);
409 return ERR_PTR(-EAGAIN);
412 tfm = ERR_PTR(-EINVAL);
413 if (unlikely((alg->cra_flags ^ type) & mask))
416 tfm = __crypto_alloc_tfm(alg, type, mask);
426 EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
428 int crypto_register_notifier(struct notifier_block *nb)
430 return blocking_notifier_chain_register(&crypto_chain, nb);
432 EXPORT_SYMBOL_GPL(crypto_register_notifier);
434 int crypto_unregister_notifier(struct notifier_block *nb)
436 return blocking_notifier_chain_unregister(&crypto_chain, nb);
438 EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
440 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
442 struct rtattr *rta = tb[0];
443 struct crypto_attr_type *algt;
446 return ERR_PTR(-ENOENT);
447 if (RTA_PAYLOAD(rta) < sizeof(*algt))
448 return ERR_PTR(-EINVAL);
449 if (rta->rta_type != CRYPTOA_TYPE)
450 return ERR_PTR(-EINVAL);
452 algt = RTA_DATA(rta);
456 EXPORT_SYMBOL_GPL(crypto_get_attr_type);
458 int crypto_check_attr_type(struct rtattr **tb, u32 type)
460 struct crypto_attr_type *algt;
462 algt = crypto_get_attr_type(tb);
464 return PTR_ERR(algt);
466 if ((algt->type ^ type) & algt->mask)
471 EXPORT_SYMBOL_GPL(crypto_check_attr_type);
473 const char *crypto_attr_alg_name(struct rtattr *rta)
475 struct crypto_attr_alg *alga;
478 return ERR_PTR(-ENOENT);
479 if (RTA_PAYLOAD(rta) < sizeof(*alga))
480 return ERR_PTR(-EINVAL);
481 if (rta->rta_type != CRYPTOA_ALG)
482 return ERR_PTR(-EINVAL);
484 alga = RTA_DATA(rta);
485 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
489 EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
491 struct crypto_alg *crypto_attr_alg(struct rtattr *rta, u32 type, u32 mask)
496 name = crypto_attr_alg_name(rta);
501 return crypto_alg_mod_lookup(name, type, mask);
503 EXPORT_SYMBOL_GPL(crypto_attr_alg);
505 int crypto_attr_u32(struct rtattr *rta, u32 *num)
507 struct crypto_attr_u32 *nu32;
511 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
513 if (rta->rta_type != CRYPTOA_U32)
516 nu32 = RTA_DATA(rta);
521 EXPORT_SYMBOL_GPL(crypto_attr_u32);
523 struct crypto_instance *crypto_alloc_instance(const char *name,
524 struct crypto_alg *alg)
526 struct crypto_instance *inst;
527 struct crypto_spawn *spawn;
530 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
532 return ERR_PTR(-ENOMEM);
535 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
536 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
539 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
540 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
543 spawn = crypto_instance_ctx(inst);
544 err = crypto_init_spawn(spawn, alg, inst,
545 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
556 EXPORT_SYMBOL_GPL(crypto_alloc_instance);
558 void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
560 INIT_LIST_HEAD(&queue->list);
561 queue->backlog = &queue->list;
563 queue->max_qlen = max_qlen;
565 EXPORT_SYMBOL_GPL(crypto_init_queue);
567 int crypto_enqueue_request(struct crypto_queue *queue,
568 struct crypto_async_request *request)
570 int err = -EINPROGRESS;
572 if (unlikely(queue->qlen >= queue->max_qlen)) {
574 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
576 if (queue->backlog == &queue->list)
577 queue->backlog = &request->list;
581 list_add_tail(&request->list, &queue->list);
586 EXPORT_SYMBOL_GPL(crypto_enqueue_request);
588 struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
590 struct list_head *request;
592 if (unlikely(!queue->qlen))
597 if (queue->backlog != &queue->list)
598 queue->backlog = queue->backlog->next;
600 request = queue->list.next;
603 return list_entry(request, struct crypto_async_request, list);
605 EXPORT_SYMBOL_GPL(crypto_dequeue_request);
607 int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
609 struct crypto_async_request *req;
611 list_for_each_entry(req, &queue->list, list) {
618 EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
620 static inline void crypto_inc_byte(u8 *a, unsigned int size)
625 for (; size; size--) {
633 void crypto_inc(u8 *a, unsigned int size)
635 __be32 *b = (__be32 *)(a + size);
638 for (; size >= 4; size -= 4) {
639 c = be32_to_cpu(*--b) + 1;
645 crypto_inc_byte(a, size);
647 EXPORT_SYMBOL_GPL(crypto_inc);
649 static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
655 void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
660 for (; size >= 4; size -= 4)
663 crypto_xor_byte((u8 *)a, (u8 *)b, size);
665 EXPORT_SYMBOL_GPL(crypto_xor);
667 static int __init crypto_algapi_init(void)
673 static void __exit crypto_algapi_exit(void)
678 module_init(crypto_algapi_init);
679 module_exit(crypto_algapi_exit);
681 MODULE_LICENSE("GPL");
682 MODULE_DESCRIPTION("Cryptographic algorithms API");