crypto: api - Export crypto_alg_lookup instead of __crypto_alg_lookup
[linux-flexiantxendom0-natty.git] / crypto / algapi.c
1 /*
2  * Cryptographic API for algorithms (i.e., low-level API).
3  *
4  * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
5  *
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)
9  * any later version.
10  *
11  */
12
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>
21
22 #include "internal.h"
23
24 static LIST_HEAD(crypto_template_list);
25
26 void crypto_larval_error(const char *name, u32 type, u32 mask)
27 {
28         struct crypto_alg *alg;
29
30         alg = crypto_alg_lookup(name, type, mask);
31
32         if (alg) {
33                 if (crypto_is_larval(alg)) {
34                         struct crypto_larval *larval = (void *)alg;
35                         complete_all(&larval->completion);
36                 }
37                 crypto_mod_put(alg);
38         }
39 }
40 EXPORT_SYMBOL_GPL(crypto_larval_error);
41
42 static inline int crypto_set_driver_name(struct crypto_alg *alg)
43 {
44         static const char suffix[] = "-generic";
45         char *driver_name = alg->cra_driver_name;
46         int len;
47
48         if (*driver_name)
49                 return 0;
50
51         len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
52         if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
53                 return -ENAMETOOLONG;
54
55         memcpy(driver_name + len, suffix, sizeof(suffix));
56         return 0;
57 }
58
59 static int crypto_check_alg(struct crypto_alg *alg)
60 {
61         if (alg->cra_alignmask & (alg->cra_alignmask + 1))
62                 return -EINVAL;
63
64         if (alg->cra_blocksize > PAGE_SIZE / 8)
65                 return -EINVAL;
66
67         if (alg->cra_priority < 0)
68                 return -EINVAL;
69
70         return crypto_set_driver_name(alg);
71 }
72
73 static void crypto_destroy_instance(struct crypto_alg *alg)
74 {
75         struct crypto_instance *inst = (void *)alg;
76         struct crypto_template *tmpl = inst->tmpl;
77
78         tmpl->free(inst);
79         crypto_tmpl_put(tmpl);
80 }
81
82 static void crypto_remove_spawn(struct crypto_spawn *spawn,
83                                 struct list_head *list,
84                                 struct list_head *secondary_spawns)
85 {
86         struct crypto_instance *inst = spawn->inst;
87         struct crypto_template *tmpl = inst->tmpl;
88
89         list_del_init(&spawn->list);
90         spawn->alg = NULL;
91
92         if (crypto_is_dead(&inst->alg))
93                 return;
94
95         inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
96         if (hlist_unhashed(&inst->list))
97                 return;
98
99         if (!tmpl || !crypto_tmpl_get(tmpl))
100                 return;
101
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;
106
107         list_splice(&inst->alg.cra_users, secondary_spawns);
108 }
109
110 static void crypto_remove_spawns(struct list_head *spawns,
111                                  struct list_head *list, u32 new_type)
112 {
113         struct crypto_spawn *spawn, *n;
114         LIST_HEAD(secondary_spawns);
115
116         list_for_each_entry_safe(spawn, n, spawns, list) {
117                 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
118                         continue;
119
120                 crypto_remove_spawn(spawn, list, &secondary_spawns);
121         }
122
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);
126         }
127 }
128
129 static int __crypto_register_alg(struct crypto_alg *alg,
130                                  struct list_head *list)
131 {
132         struct crypto_alg *q;
133         int ret = -EAGAIN;
134
135         if (crypto_is_dead(alg))
136                 goto out;
137
138         INIT_LIST_HEAD(&alg->cra_users);
139
140         ret = -EEXIST;
141
142         atomic_set(&alg->cra_refcnt, 1);
143         list_for_each_entry(q, &crypto_alg_list, cra_list) {
144                 if (q == alg)
145                         goto out;
146
147                 if (crypto_is_moribund(q))
148                         continue;
149
150                 if (crypto_is_larval(q)) {
151                         struct crypto_larval *larval = (void *)q;
152
153                         /*
154                          * Check to see if either our generic name or
155                          * specific name can satisfy the name requested
156                          * by the larval entry q.
157                          */
158                         if (strcmp(alg->cra_name, q->cra_name) &&
159                             strcmp(alg->cra_driver_name, q->cra_name))
160                                 continue;
161
162                         if (larval->adult)
163                                 continue;
164                         if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
165                                 continue;
166                         if (!crypto_mod_get(alg))
167                                 continue;
168
169                         larval->adult = alg;
170                         complete_all(&larval->completion);
171                         continue;
172                 }
173
174                 if (strcmp(alg->cra_name, q->cra_name))
175                         continue;
176
177                 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
178                     q->cra_priority > alg->cra_priority)
179                         continue;
180
181                 crypto_remove_spawns(&q->cra_users, list, alg->cra_flags);
182         }
183         
184         list_add(&alg->cra_list, &crypto_alg_list);
185
186         crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg);
187         ret = 0;
188
189 out:    
190         return ret;
191 }
192
193 static void crypto_remove_final(struct list_head *list)
194 {
195         struct crypto_alg *alg;
196         struct crypto_alg *n;
197
198         list_for_each_entry_safe(alg, n, list, cra_list) {
199                 list_del_init(&alg->cra_list);
200                 crypto_alg_put(alg);
201         }
202 }
203
204 int crypto_register_alg(struct crypto_alg *alg)
205 {
206         LIST_HEAD(list);
207         int err;
208
209         err = crypto_check_alg(alg);
210         if (err)
211                 return err;
212
213         down_write(&crypto_alg_sem);
214         err = __crypto_register_alg(alg, &list);
215         up_write(&crypto_alg_sem);
216
217         crypto_remove_final(&list);
218         return err;
219 }
220 EXPORT_SYMBOL_GPL(crypto_register_alg);
221
222 static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
223 {
224         if (unlikely(list_empty(&alg->cra_list)))
225                 return -ENOENT;
226
227         alg->cra_flags |= CRYPTO_ALG_DEAD;
228
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);
232
233         return 0;
234 }
235
236 int crypto_unregister_alg(struct crypto_alg *alg)
237 {
238         int ret;
239         LIST_HEAD(list);
240         
241         down_write(&crypto_alg_sem);
242         ret = crypto_remove_alg(alg, &list);
243         up_write(&crypto_alg_sem);
244
245         if (ret)
246                 return ret;
247
248         BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
249         if (alg->cra_destroy)
250                 alg->cra_destroy(alg);
251
252         crypto_remove_final(&list);
253         return 0;
254 }
255 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
256
257 int crypto_register_template(struct crypto_template *tmpl)
258 {
259         struct crypto_template *q;
260         int err = -EEXIST;
261
262         down_write(&crypto_alg_sem);
263
264         list_for_each_entry(q, &crypto_template_list, list) {
265                 if (q == tmpl)
266                         goto out;
267         }
268
269         list_add(&tmpl->list, &crypto_template_list);
270         crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
271         err = 0;
272 out:
273         up_write(&crypto_alg_sem);
274         return err;
275 }
276 EXPORT_SYMBOL_GPL(crypto_register_template);
277
278 void crypto_unregister_template(struct crypto_template *tmpl)
279 {
280         struct crypto_instance *inst;
281         struct hlist_node *p, *n;
282         struct hlist_head *list;
283         LIST_HEAD(users);
284
285         down_write(&crypto_alg_sem);
286
287         BUG_ON(list_empty(&tmpl->list));
288         list_del_init(&tmpl->list);
289
290         list = &tmpl->instances;
291         hlist_for_each_entry(inst, p, list, list) {
292                 int err = crypto_remove_alg(&inst->alg, &users);
293                 BUG_ON(err);
294         }
295
296         crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
297
298         up_write(&crypto_alg_sem);
299
300         hlist_for_each_entry_safe(inst, p, n, list, list) {
301                 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
302                 tmpl->free(inst);
303         }
304         crypto_remove_final(&users);
305 }
306 EXPORT_SYMBOL_GPL(crypto_unregister_template);
307
308 static struct crypto_template *__crypto_lookup_template(const char *name)
309 {
310         struct crypto_template *q, *tmpl = NULL;
311
312         down_read(&crypto_alg_sem);
313         list_for_each_entry(q, &crypto_template_list, list) {
314                 if (strcmp(q->name, name))
315                         continue;
316                 if (unlikely(!crypto_tmpl_get(q)))
317                         continue;
318
319                 tmpl = q;
320                 break;
321         }
322         up_read(&crypto_alg_sem);
323
324         return tmpl;
325 }
326
327 struct crypto_template *crypto_lookup_template(const char *name)
328 {
329         return try_then_request_module(__crypto_lookup_template(name), name);
330 }
331 EXPORT_SYMBOL_GPL(crypto_lookup_template);
332
333 int crypto_register_instance(struct crypto_template *tmpl,
334                              struct crypto_instance *inst)
335 {
336         LIST_HEAD(list);
337         int err = -EINVAL;
338
339         err = crypto_check_alg(&inst->alg);
340         if (err)
341                 goto err;
342
343         inst->alg.cra_module = tmpl->module;
344
345         down_write(&crypto_alg_sem);
346
347         err = __crypto_register_alg(&inst->alg, &list);
348         if (err)
349                 goto unlock;
350
351         hlist_add_head(&inst->list, &tmpl->instances);
352         inst->tmpl = tmpl;
353
354 unlock:
355         up_write(&crypto_alg_sem);
356
357         crypto_remove_final(&list);
358
359 err:
360         return err;
361 }
362 EXPORT_SYMBOL_GPL(crypto_register_instance);
363
364 int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
365                       struct crypto_instance *inst, u32 mask)
366 {
367         int err = -EAGAIN;
368
369         spawn->inst = inst;
370         spawn->mask = mask;
371
372         down_write(&crypto_alg_sem);
373         if (!crypto_is_moribund(alg)) {
374                 list_add(&spawn->list, &alg->cra_users);
375                 spawn->alg = alg;
376                 err = 0;
377         }
378         up_write(&crypto_alg_sem);
379
380         return err;
381 }
382 EXPORT_SYMBOL_GPL(crypto_init_spawn);
383
384 void crypto_drop_spawn(struct crypto_spawn *spawn)
385 {
386         down_write(&crypto_alg_sem);
387         list_del(&spawn->list);
388         up_write(&crypto_alg_sem);
389 }
390 EXPORT_SYMBOL_GPL(crypto_drop_spawn);
391
392 struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
393                                     u32 mask)
394 {
395         struct crypto_alg *alg;
396         struct crypto_alg *alg2;
397         struct crypto_tfm *tfm;
398
399         down_read(&crypto_alg_sem);
400         alg = spawn->alg;
401         alg2 = alg;
402         if (alg2)
403                 alg2 = crypto_mod_get(alg2);
404         up_read(&crypto_alg_sem);
405
406         if (!alg2) {
407                 if (alg)
408                         crypto_shoot_alg(alg);
409                 return ERR_PTR(-EAGAIN);
410         }
411
412         tfm = ERR_PTR(-EINVAL);
413         if (unlikely((alg->cra_flags ^ type) & mask))
414                 goto out_put_alg;
415
416         tfm = __crypto_alloc_tfm(alg, type, mask);
417         if (IS_ERR(tfm))
418                 goto out_put_alg;
419
420         return tfm;
421
422 out_put_alg:
423         crypto_mod_put(alg);
424         return tfm;
425 }
426 EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
427
428 int crypto_register_notifier(struct notifier_block *nb)
429 {
430         return blocking_notifier_chain_register(&crypto_chain, nb);
431 }
432 EXPORT_SYMBOL_GPL(crypto_register_notifier);
433
434 int crypto_unregister_notifier(struct notifier_block *nb)
435 {
436         return blocking_notifier_chain_unregister(&crypto_chain, nb);
437 }
438 EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
439
440 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
441 {
442         struct rtattr *rta = tb[0];
443         struct crypto_attr_type *algt;
444
445         if (!rta)
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);
451
452         algt = RTA_DATA(rta);
453
454         return algt;
455 }
456 EXPORT_SYMBOL_GPL(crypto_get_attr_type);
457
458 int crypto_check_attr_type(struct rtattr **tb, u32 type)
459 {
460         struct crypto_attr_type *algt;
461
462         algt = crypto_get_attr_type(tb);
463         if (IS_ERR(algt))
464                 return PTR_ERR(algt);
465
466         if ((algt->type ^ type) & algt->mask)
467                 return -EINVAL;
468
469         return 0;
470 }
471 EXPORT_SYMBOL_GPL(crypto_check_attr_type);
472
473 const char *crypto_attr_alg_name(struct rtattr *rta)
474 {
475         struct crypto_attr_alg *alga;
476
477         if (!rta)
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);
483
484         alga = RTA_DATA(rta);
485         alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
486
487         return alga->name;
488 }
489 EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
490
491 struct crypto_alg *crypto_attr_alg(struct rtattr *rta, u32 type, u32 mask)
492 {
493         const char *name;
494         int err;
495
496         name = crypto_attr_alg_name(rta);
497         err = PTR_ERR(name);
498         if (IS_ERR(name))
499                 return ERR_PTR(err);
500
501         return crypto_alg_mod_lookup(name, type, mask);
502 }
503 EXPORT_SYMBOL_GPL(crypto_attr_alg);
504
505 int crypto_attr_u32(struct rtattr *rta, u32 *num)
506 {
507         struct crypto_attr_u32 *nu32;
508
509         if (!rta)
510                 return -ENOENT;
511         if (RTA_PAYLOAD(rta) < sizeof(*nu32))
512                 return -EINVAL;
513         if (rta->rta_type != CRYPTOA_U32)
514                 return -EINVAL;
515
516         nu32 = RTA_DATA(rta);
517         *num = nu32->num;
518
519         return 0;
520 }
521 EXPORT_SYMBOL_GPL(crypto_attr_u32);
522
523 struct crypto_instance *crypto_alloc_instance(const char *name,
524                                               struct crypto_alg *alg)
525 {
526         struct crypto_instance *inst;
527         struct crypto_spawn *spawn;
528         int err;
529
530         inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
531         if (!inst)
532                 return ERR_PTR(-ENOMEM);
533
534         err = -ENAMETOOLONG;
535         if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
536                      alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
537                 goto err_free_inst;
538
539         if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
540                      name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
541                 goto err_free_inst;
542
543         spawn = crypto_instance_ctx(inst);
544         err = crypto_init_spawn(spawn, alg, inst,
545                                 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
546
547         if (err)
548                 goto err_free_inst;
549
550         return inst;
551
552 err_free_inst:
553         kfree(inst);
554         return ERR_PTR(err);
555 }
556 EXPORT_SYMBOL_GPL(crypto_alloc_instance);
557
558 void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
559 {
560         INIT_LIST_HEAD(&queue->list);
561         queue->backlog = &queue->list;
562         queue->qlen = 0;
563         queue->max_qlen = max_qlen;
564 }
565 EXPORT_SYMBOL_GPL(crypto_init_queue);
566
567 int crypto_enqueue_request(struct crypto_queue *queue,
568                            struct crypto_async_request *request)
569 {
570         int err = -EINPROGRESS;
571
572         if (unlikely(queue->qlen >= queue->max_qlen)) {
573                 err = -EBUSY;
574                 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
575                         goto out;
576                 if (queue->backlog == &queue->list)
577                         queue->backlog = &request->list;
578         }
579
580         queue->qlen++;
581         list_add_tail(&request->list, &queue->list);
582
583 out:
584         return err;
585 }
586 EXPORT_SYMBOL_GPL(crypto_enqueue_request);
587
588 struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
589 {
590         struct list_head *request;
591
592         if (unlikely(!queue->qlen))
593                 return NULL;
594
595         queue->qlen--;
596
597         if (queue->backlog != &queue->list)
598                 queue->backlog = queue->backlog->next;
599
600         request = queue->list.next;
601         list_del(request);
602
603         return list_entry(request, struct crypto_async_request, list);
604 }
605 EXPORT_SYMBOL_GPL(crypto_dequeue_request);
606
607 int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
608 {
609         struct crypto_async_request *req;
610
611         list_for_each_entry(req, &queue->list, list) {
612                 if (req->tfm == tfm)
613                         return 1;
614         }
615
616         return 0;
617 }
618 EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
619
620 static inline void crypto_inc_byte(u8 *a, unsigned int size)
621 {
622         u8 *b = (a + size);
623         u8 c;
624
625         for (; size; size--) {
626                 c = *--b + 1;
627                 *b = c;
628                 if (c)
629                         break;
630         }
631 }
632
633 void crypto_inc(u8 *a, unsigned int size)
634 {
635         __be32 *b = (__be32 *)(a + size);
636         u32 c;
637
638         for (; size >= 4; size -= 4) {
639                 c = be32_to_cpu(*--b) + 1;
640                 *b = cpu_to_be32(c);
641                 if (c)
642                         return;
643         }
644
645         crypto_inc_byte(a, size);
646 }
647 EXPORT_SYMBOL_GPL(crypto_inc);
648
649 static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
650 {
651         for (; size; size--)
652                 *a++ ^= *b++;
653 }
654
655 void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
656 {
657         u32 *a = (u32 *)dst;
658         u32 *b = (u32 *)src;
659
660         for (; size >= 4; size -= 4)
661                 *a++ ^= *b++;
662
663         crypto_xor_byte((u8 *)a, (u8 *)b, size);
664 }
665 EXPORT_SYMBOL_GPL(crypto_xor);
666
667 static int __init crypto_algapi_init(void)
668 {
669         crypto_init_proc();
670         return 0;
671 }
672
673 static void __exit crypto_algapi_exit(void)
674 {
675         crypto_exit_proc();
676 }
677
678 module_init(crypto_algapi_init);
679 module_exit(crypto_algapi_exit);
680
681 MODULE_LICENSE("GPL");
682 MODULE_DESCRIPTION("Cryptographic algorithms API");