net/mlx4_en: Fix mixed PFC and Global pause user control requests
[pandora-kernel.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/slab.h>
21 #include <linux/string.h>
22
23 #include "internal.h"
24
25 static LIST_HEAD(crypto_template_list);
26
27 void crypto_larval_error(const char *name, u32 type, u32 mask)
28 {
29         struct crypto_alg *alg;
30
31         alg = crypto_alg_lookup(name, type, mask);
32
33         if (alg) {
34                 if (crypto_is_larval(alg)) {
35                         struct crypto_larval *larval = (void *)alg;
36                         complete_all(&larval->completion);
37                 }
38                 crypto_mod_put(alg);
39         }
40 }
41 EXPORT_SYMBOL_GPL(crypto_larval_error);
42
43 static inline int crypto_set_driver_name(struct crypto_alg *alg)
44 {
45         static const char suffix[] = "-generic";
46         char *driver_name = alg->cra_driver_name;
47         int len;
48
49         if (*driver_name)
50                 return 0;
51
52         len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
53         if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
54                 return -ENAMETOOLONG;
55
56         memcpy(driver_name + len, suffix, sizeof(suffix));
57         return 0;
58 }
59
60 static int crypto_check_alg(struct crypto_alg *alg)
61 {
62         if (alg->cra_alignmask & (alg->cra_alignmask + 1))
63                 return -EINVAL;
64
65         if (alg->cra_blocksize > PAGE_SIZE / 8)
66                 return -EINVAL;
67
68         if (alg->cra_priority < 0)
69                 return -EINVAL;
70
71         return crypto_set_driver_name(alg);
72 }
73
74 static void crypto_destroy_instance(struct crypto_alg *alg)
75 {
76         struct crypto_instance *inst = (void *)alg;
77         struct crypto_template *tmpl = inst->tmpl;
78
79         tmpl->free(inst);
80         crypto_tmpl_put(tmpl);
81 }
82
83 static struct list_head *crypto_more_spawns(struct crypto_alg *alg,
84                                             struct list_head *stack,
85                                             struct list_head *top,
86                                             struct list_head *secondary_spawns)
87 {
88         struct crypto_spawn *spawn, *n;
89
90         if (list_empty(stack))
91                 return NULL;
92
93         spawn = list_first_entry(stack, struct crypto_spawn, list);
94         n = list_entry(spawn->list.next, struct crypto_spawn, list);
95
96         if (spawn->alg && &n->list != stack && !n->alg)
97                 n->alg = (n->list.next == stack) ? alg :
98                          &list_entry(n->list.next, struct crypto_spawn,
99                                      list)->inst->alg;
100
101         list_move(&spawn->list, secondary_spawns);
102
103         return &n->list == stack ? top : &n->inst->alg.cra_users;
104 }
105
106 static void crypto_remove_spawn(struct crypto_spawn *spawn,
107                                 struct list_head *list)
108 {
109         struct crypto_instance *inst = spawn->inst;
110         struct crypto_template *tmpl = inst->tmpl;
111
112         if (crypto_is_dead(&inst->alg))
113                 return;
114
115         inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
116         if (hlist_unhashed(&inst->list))
117                 return;
118
119         if (!tmpl || !crypto_tmpl_get(tmpl))
120                 return;
121
122         crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
123         list_move(&inst->alg.cra_list, list);
124         hlist_del(&inst->list);
125         inst->alg.cra_destroy = crypto_destroy_instance;
126
127         BUG_ON(!list_empty(&inst->alg.cra_users));
128 }
129
130 void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
131                           struct crypto_alg *nalg)
132 {
133         u32 new_type = (nalg ?: alg)->cra_flags;
134         struct crypto_spawn *spawn, *n;
135         LIST_HEAD(secondary_spawns);
136         struct list_head *spawns;
137         LIST_HEAD(stack);
138         LIST_HEAD(top);
139
140         spawns = &alg->cra_users;
141         list_for_each_entry_safe(spawn, n, spawns, list) {
142                 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
143                         continue;
144
145                 list_move(&spawn->list, &top);
146         }
147
148         spawns = &top;
149         do {
150                 while (!list_empty(spawns)) {
151                         struct crypto_instance *inst;
152
153                         spawn = list_first_entry(spawns, struct crypto_spawn,
154                                                  list);
155                         inst = spawn->inst;
156
157                         BUG_ON(&inst->alg == alg);
158
159                         list_move(&spawn->list, &stack);
160
161                         if (&inst->alg == nalg)
162                                 break;
163
164                         spawn->alg = NULL;
165                         spawns = &inst->alg.cra_users;
166
167                         /*
168                          * We may encounter an unregistered instance here, since
169                          * an instance's spawns are set up prior to the instance
170                          * being registered.  An unregistered instance will have
171                          * NULL ->cra_users.next, since ->cra_users isn't
172                          * properly initialized until registration.  But an
173                          * unregistered instance cannot have any users, so treat
174                          * it the same as ->cra_users being empty.
175                          */
176                         if (spawns->next == NULL)
177                                 break;
178                 }
179         } while ((spawns = crypto_more_spawns(alg, &stack, &top,
180                                               &secondary_spawns)));
181
182         list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
183                 if (spawn->alg)
184                         list_move(&spawn->list, &spawn->alg->cra_users);
185                 else
186                         crypto_remove_spawn(spawn, list);
187         }
188 }
189 EXPORT_SYMBOL_GPL(crypto_remove_spawns);
190
191 static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
192 {
193         struct crypto_alg *q;
194         struct crypto_larval *larval;
195         int ret = -EAGAIN;
196
197         if (crypto_is_dead(alg))
198                 goto err;
199
200         INIT_LIST_HEAD(&alg->cra_users);
201
202         /* No cheating! */
203         alg->cra_flags &= ~CRYPTO_ALG_TESTED;
204
205         ret = -EEXIST;
206
207         atomic_set(&alg->cra_refcnt, 1);
208         list_for_each_entry(q, &crypto_alg_list, cra_list) {
209                 if (q == alg)
210                         goto err;
211
212                 if (crypto_is_moribund(q))
213                         continue;
214
215                 if (crypto_is_larval(q)) {
216                         if (!strcmp(alg->cra_driver_name, q->cra_driver_name))
217                                 goto err;
218                         continue;
219                 }
220
221                 if (!strcmp(q->cra_driver_name, alg->cra_name) ||
222                     !strcmp(q->cra_name, alg->cra_driver_name))
223                         goto err;
224         }
225
226         larval = crypto_larval_alloc(alg->cra_name,
227                                      alg->cra_flags | CRYPTO_ALG_TESTED, 0);
228         if (IS_ERR(larval))
229                 goto out;
230
231         ret = -ENOENT;
232         larval->adult = crypto_mod_get(alg);
233         if (!larval->adult)
234                 goto free_larval;
235
236         atomic_set(&larval->alg.cra_refcnt, 1);
237         memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
238                CRYPTO_MAX_ALG_NAME);
239         larval->alg.cra_priority = alg->cra_priority;
240
241         list_add(&alg->cra_list, &crypto_alg_list);
242         list_add(&larval->alg.cra_list, &crypto_alg_list);
243
244 out:
245         return larval;
246
247 free_larval:
248         kfree(larval);
249 err:
250         larval = ERR_PTR(ret);
251         goto out;
252 }
253
254 void crypto_alg_tested(const char *name, int err)
255 {
256         struct crypto_larval *test;
257         struct crypto_alg *alg;
258         struct crypto_alg *q;
259         LIST_HEAD(list);
260
261         down_write(&crypto_alg_sem);
262         list_for_each_entry(q, &crypto_alg_list, cra_list) {
263                 if (crypto_is_moribund(q) || !crypto_is_larval(q))
264                         continue;
265
266                 test = (struct crypto_larval *)q;
267
268                 if (!strcmp(q->cra_driver_name, name))
269                         goto found;
270         }
271
272         printk(KERN_ERR "alg: Unexpected test result for %s: %d\n", name, err);
273         goto unlock;
274
275 found:
276         q->cra_flags |= CRYPTO_ALG_DEAD;
277         alg = test->adult;
278         if (err || list_empty(&alg->cra_list))
279                 goto complete;
280
281         alg->cra_flags |= CRYPTO_ALG_TESTED;
282
283         list_for_each_entry(q, &crypto_alg_list, cra_list) {
284                 if (q == alg)
285                         continue;
286
287                 if (crypto_is_moribund(q))
288                         continue;
289
290                 if (crypto_is_larval(q)) {
291                         struct crypto_larval *larval = (void *)q;
292
293                         /*
294                          * Check to see if either our generic name or
295                          * specific name can satisfy the name requested
296                          * by the larval entry q.
297                          */
298                         if (strcmp(alg->cra_name, q->cra_name) &&
299                             strcmp(alg->cra_driver_name, q->cra_name))
300                                 continue;
301
302                         if (larval->adult)
303                                 continue;
304                         if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
305                                 continue;
306                         if (!crypto_mod_get(alg))
307                                 continue;
308
309                         larval->adult = alg;
310                         complete_all(&larval->completion);
311                         continue;
312                 }
313
314                 if (strcmp(alg->cra_name, q->cra_name))
315                         continue;
316
317                 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
318                     q->cra_priority > alg->cra_priority)
319                         continue;
320
321                 crypto_remove_spawns(q, &list, alg);
322         }
323
324 complete:
325         complete_all(&test->completion);
326
327 unlock:
328         up_write(&crypto_alg_sem);
329
330         crypto_remove_final(&list);
331 }
332 EXPORT_SYMBOL_GPL(crypto_alg_tested);
333
334 void crypto_remove_final(struct list_head *list)
335 {
336         struct crypto_alg *alg;
337         struct crypto_alg *n;
338
339         list_for_each_entry_safe(alg, n, list, cra_list) {
340                 list_del_init(&alg->cra_list);
341                 crypto_alg_put(alg);
342         }
343 }
344 EXPORT_SYMBOL_GPL(crypto_remove_final);
345
346 static void crypto_wait_for_test(struct crypto_larval *larval)
347 {
348         int err;
349
350         err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
351         if (err != NOTIFY_STOP) {
352                 if (WARN_ON(err != NOTIFY_DONE))
353                         goto out;
354                 crypto_alg_tested(larval->alg.cra_driver_name, 0);
355         }
356
357         err = wait_for_completion_killable(&larval->completion);
358         WARN_ON(err);
359
360 out:
361         crypto_larval_kill(&larval->alg);
362 }
363
364 int crypto_register_alg(struct crypto_alg *alg)
365 {
366         struct crypto_larval *larval;
367         int err;
368
369         alg->cra_flags &= ~CRYPTO_ALG_DEAD;
370         err = crypto_check_alg(alg);
371         if (err)
372                 return err;
373
374         down_write(&crypto_alg_sem);
375         larval = __crypto_register_alg(alg);
376         up_write(&crypto_alg_sem);
377
378         if (IS_ERR(larval))
379                 return PTR_ERR(larval);
380
381         crypto_wait_for_test(larval);
382         return 0;
383 }
384 EXPORT_SYMBOL_GPL(crypto_register_alg);
385
386 static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
387 {
388         if (unlikely(list_empty(&alg->cra_list)))
389                 return -ENOENT;
390
391         alg->cra_flags |= CRYPTO_ALG_DEAD;
392
393         crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
394         list_del_init(&alg->cra_list);
395         crypto_remove_spawns(alg, list, NULL);
396
397         return 0;
398 }
399
400 int crypto_unregister_alg(struct crypto_alg *alg)
401 {
402         int ret;
403         LIST_HEAD(list);
404
405         down_write(&crypto_alg_sem);
406         ret = crypto_remove_alg(alg, &list);
407         up_write(&crypto_alg_sem);
408
409         if (ret)
410                 return ret;
411
412         BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
413         if (alg->cra_destroy)
414                 alg->cra_destroy(alg);
415
416         crypto_remove_final(&list);
417         return 0;
418 }
419 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
420
421 int crypto_register_template(struct crypto_template *tmpl)
422 {
423         struct crypto_template *q;
424         int err = -EEXIST;
425
426         down_write(&crypto_alg_sem);
427
428         list_for_each_entry(q, &crypto_template_list, list) {
429                 if (q == tmpl)
430                         goto out;
431         }
432
433         list_add(&tmpl->list, &crypto_template_list);
434         crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
435         err = 0;
436 out:
437         up_write(&crypto_alg_sem);
438         return err;
439 }
440 EXPORT_SYMBOL_GPL(crypto_register_template);
441
442 void crypto_unregister_template(struct crypto_template *tmpl)
443 {
444         struct crypto_instance *inst;
445         struct hlist_node *p, *n;
446         struct hlist_head *list;
447         LIST_HEAD(users);
448
449         down_write(&crypto_alg_sem);
450
451         BUG_ON(list_empty(&tmpl->list));
452         list_del_init(&tmpl->list);
453
454         list = &tmpl->instances;
455         hlist_for_each_entry(inst, p, list, list) {
456                 int err = crypto_remove_alg(&inst->alg, &users);
457                 BUG_ON(err);
458         }
459
460         crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
461
462         up_write(&crypto_alg_sem);
463
464         hlist_for_each_entry_safe(inst, p, n, list, list) {
465                 BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
466                 tmpl->free(inst);
467         }
468         crypto_remove_final(&users);
469 }
470 EXPORT_SYMBOL_GPL(crypto_unregister_template);
471
472 static struct crypto_template *__crypto_lookup_template(const char *name)
473 {
474         struct crypto_template *q, *tmpl = NULL;
475
476         down_read(&crypto_alg_sem);
477         list_for_each_entry(q, &crypto_template_list, list) {
478                 if (strcmp(q->name, name))
479                         continue;
480                 if (unlikely(!crypto_tmpl_get(q)))
481                         continue;
482
483                 tmpl = q;
484                 break;
485         }
486         up_read(&crypto_alg_sem);
487
488         return tmpl;
489 }
490
491 struct crypto_template *crypto_lookup_template(const char *name)
492 {
493         return try_then_request_module(__crypto_lookup_template(name),
494                                        "crypto-%s", name);
495 }
496 EXPORT_SYMBOL_GPL(crypto_lookup_template);
497
498 int crypto_register_instance(struct crypto_template *tmpl,
499                              struct crypto_instance *inst)
500 {
501         struct crypto_larval *larval;
502         int err;
503
504         err = crypto_check_alg(&inst->alg);
505         if (err)
506                 goto err;
507
508         inst->alg.cra_module = tmpl->module;
509         inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
510
511         down_write(&crypto_alg_sem);
512
513         larval = __crypto_register_alg(&inst->alg);
514         if (IS_ERR(larval))
515                 goto unlock;
516
517         hlist_add_head(&inst->list, &tmpl->instances);
518         inst->tmpl = tmpl;
519
520 unlock:
521         up_write(&crypto_alg_sem);
522
523         err = PTR_ERR(larval);
524         if (IS_ERR(larval))
525                 goto err;
526
527         crypto_wait_for_test(larval);
528         err = 0;
529
530 err:
531         return err;
532 }
533 EXPORT_SYMBOL_GPL(crypto_register_instance);
534
535 int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
536                       struct crypto_instance *inst, u32 mask)
537 {
538         int err = -EAGAIN;
539
540         spawn->inst = inst;
541         spawn->mask = mask;
542
543         down_write(&crypto_alg_sem);
544         if (!crypto_is_moribund(alg)) {
545                 list_add(&spawn->list, &alg->cra_users);
546                 spawn->alg = alg;
547                 err = 0;
548         }
549         up_write(&crypto_alg_sem);
550
551         return err;
552 }
553 EXPORT_SYMBOL_GPL(crypto_init_spawn);
554
555 int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg,
556                        struct crypto_instance *inst,
557                        const struct crypto_type *frontend)
558 {
559         int err = -EINVAL;
560
561         if ((alg->cra_flags ^ frontend->type) & frontend->maskset)
562                 goto out;
563
564         spawn->frontend = frontend;
565         err = crypto_init_spawn(spawn, alg, inst, frontend->maskset);
566
567 out:
568         return err;
569 }
570 EXPORT_SYMBOL_GPL(crypto_init_spawn2);
571
572 void crypto_drop_spawn(struct crypto_spawn *spawn)
573 {
574         if (!spawn->alg)
575                 return;
576
577         down_write(&crypto_alg_sem);
578         list_del(&spawn->list);
579         up_write(&crypto_alg_sem);
580 }
581 EXPORT_SYMBOL_GPL(crypto_drop_spawn);
582
583 static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
584 {
585         struct crypto_alg *alg;
586         struct crypto_alg *alg2;
587
588         down_read(&crypto_alg_sem);
589         alg = spawn->alg;
590         alg2 = alg;
591         if (alg2)
592                 alg2 = crypto_mod_get(alg2);
593         up_read(&crypto_alg_sem);
594
595         if (!alg2) {
596                 if (alg)
597                         crypto_shoot_alg(alg);
598                 return ERR_PTR(-EAGAIN);
599         }
600
601         return alg;
602 }
603
604 struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
605                                     u32 mask)
606 {
607         struct crypto_alg *alg;
608         struct crypto_tfm *tfm;
609
610         alg = crypto_spawn_alg(spawn);
611         if (IS_ERR(alg))
612                 return ERR_CAST(alg);
613
614         tfm = ERR_PTR(-EINVAL);
615         if (unlikely((alg->cra_flags ^ type) & mask))
616                 goto out_put_alg;
617
618         tfm = __crypto_alloc_tfm(alg, type, mask);
619         if (IS_ERR(tfm))
620                 goto out_put_alg;
621
622         return tfm;
623
624 out_put_alg:
625         crypto_mod_put(alg);
626         return tfm;
627 }
628 EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
629
630 void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
631 {
632         struct crypto_alg *alg;
633         struct crypto_tfm *tfm;
634
635         alg = crypto_spawn_alg(spawn);
636         if (IS_ERR(alg))
637                 return ERR_CAST(alg);
638
639         tfm = crypto_create_tfm(alg, spawn->frontend);
640         if (IS_ERR(tfm))
641                 goto out_put_alg;
642
643         return tfm;
644
645 out_put_alg:
646         crypto_mod_put(alg);
647         return tfm;
648 }
649 EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
650
651 int crypto_register_notifier(struct notifier_block *nb)
652 {
653         return blocking_notifier_chain_register(&crypto_chain, nb);
654 }
655 EXPORT_SYMBOL_GPL(crypto_register_notifier);
656
657 int crypto_unregister_notifier(struct notifier_block *nb)
658 {
659         return blocking_notifier_chain_unregister(&crypto_chain, nb);
660 }
661 EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
662
663 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
664 {
665         struct rtattr *rta = tb[0];
666         struct crypto_attr_type *algt;
667
668         if (!rta)
669                 return ERR_PTR(-ENOENT);
670         if (RTA_PAYLOAD(rta) < sizeof(*algt))
671                 return ERR_PTR(-EINVAL);
672         if (rta->rta_type != CRYPTOA_TYPE)
673                 return ERR_PTR(-EINVAL);
674
675         algt = RTA_DATA(rta);
676
677         return algt;
678 }
679 EXPORT_SYMBOL_GPL(crypto_get_attr_type);
680
681 int crypto_check_attr_type(struct rtattr **tb, u32 type)
682 {
683         struct crypto_attr_type *algt;
684
685         algt = crypto_get_attr_type(tb);
686         if (IS_ERR(algt))
687                 return PTR_ERR(algt);
688
689         if ((algt->type ^ type) & algt->mask)
690                 return -EINVAL;
691
692         return 0;
693 }
694 EXPORT_SYMBOL_GPL(crypto_check_attr_type);
695
696 const char *crypto_attr_alg_name(struct rtattr *rta)
697 {
698         struct crypto_attr_alg *alga;
699
700         if (!rta)
701                 return ERR_PTR(-ENOENT);
702         if (RTA_PAYLOAD(rta) < sizeof(*alga))
703                 return ERR_PTR(-EINVAL);
704         if (rta->rta_type != CRYPTOA_ALG)
705                 return ERR_PTR(-EINVAL);
706
707         alga = RTA_DATA(rta);
708         alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
709
710         return alga->name;
711 }
712 EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
713
714 struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
715                                     const struct crypto_type *frontend,
716                                     u32 type, u32 mask)
717 {
718         const char *name;
719         int err;
720
721         name = crypto_attr_alg_name(rta);
722         err = PTR_ERR(name);
723         if (IS_ERR(name))
724                 return ERR_PTR(err);
725
726         return crypto_find_alg(name, frontend, type, mask);
727 }
728 EXPORT_SYMBOL_GPL(crypto_attr_alg2);
729
730 int crypto_attr_u32(struct rtattr *rta, u32 *num)
731 {
732         struct crypto_attr_u32 *nu32;
733
734         if (!rta)
735                 return -ENOENT;
736         if (RTA_PAYLOAD(rta) < sizeof(*nu32))
737                 return -EINVAL;
738         if (rta->rta_type != CRYPTOA_U32)
739                 return -EINVAL;
740
741         nu32 = RTA_DATA(rta);
742         *num = nu32->num;
743
744         return 0;
745 }
746 EXPORT_SYMBOL_GPL(crypto_attr_u32);
747
748 void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg,
749                              unsigned int head)
750 {
751         struct crypto_instance *inst;
752         char *p;
753         int err;
754
755         p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn),
756                     GFP_KERNEL);
757         if (!p)
758                 return ERR_PTR(-ENOMEM);
759
760         inst = (void *)(p + head);
761
762         err = -ENAMETOOLONG;
763         if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
764                      alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
765                 goto err_free_inst;
766
767         if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
768                      name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
769                 goto err_free_inst;
770
771         return p;
772
773 err_free_inst:
774         kfree(p);
775         return ERR_PTR(err);
776 }
777 EXPORT_SYMBOL_GPL(crypto_alloc_instance2);
778
779 struct crypto_instance *crypto_alloc_instance(const char *name,
780                                               struct crypto_alg *alg)
781 {
782         struct crypto_instance *inst;
783         struct crypto_spawn *spawn;
784         int err;
785
786         inst = crypto_alloc_instance2(name, alg, 0);
787         if (IS_ERR(inst))
788                 goto out;
789
790         spawn = crypto_instance_ctx(inst);
791         err = crypto_init_spawn(spawn, alg, inst,
792                                 CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
793
794         if (err)
795                 goto err_free_inst;
796
797         return inst;
798
799 err_free_inst:
800         kfree(inst);
801         inst = ERR_PTR(err);
802
803 out:
804         return inst;
805 }
806 EXPORT_SYMBOL_GPL(crypto_alloc_instance);
807
808 void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
809 {
810         INIT_LIST_HEAD(&queue->list);
811         queue->backlog = &queue->list;
812         queue->qlen = 0;
813         queue->max_qlen = max_qlen;
814 }
815 EXPORT_SYMBOL_GPL(crypto_init_queue);
816
817 int crypto_enqueue_request(struct crypto_queue *queue,
818                            struct crypto_async_request *request)
819 {
820         int err = -EINPROGRESS;
821
822         if (unlikely(queue->qlen >= queue->max_qlen)) {
823                 err = -EBUSY;
824                 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
825                         goto out;
826                 if (queue->backlog == &queue->list)
827                         queue->backlog = &request->list;
828         }
829
830         queue->qlen++;
831         list_add_tail(&request->list, &queue->list);
832
833 out:
834         return err;
835 }
836 EXPORT_SYMBOL_GPL(crypto_enqueue_request);
837
838 void *__crypto_dequeue_request(struct crypto_queue *queue, unsigned int offset)
839 {
840         struct list_head *request;
841
842         if (unlikely(!queue->qlen))
843                 return NULL;
844
845         queue->qlen--;
846
847         if (queue->backlog != &queue->list)
848                 queue->backlog = queue->backlog->next;
849
850         request = queue->list.next;
851         list_del(request);
852
853         return (char *)list_entry(request, struct crypto_async_request, list) -
854                offset;
855 }
856 EXPORT_SYMBOL_GPL(__crypto_dequeue_request);
857
858 struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
859 {
860         return __crypto_dequeue_request(queue, 0);
861 }
862 EXPORT_SYMBOL_GPL(crypto_dequeue_request);
863
864 int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
865 {
866         struct crypto_async_request *req;
867
868         list_for_each_entry(req, &queue->list, list) {
869                 if (req->tfm == tfm)
870                         return 1;
871         }
872
873         return 0;
874 }
875 EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
876
877 static inline void crypto_inc_byte(u8 *a, unsigned int size)
878 {
879         u8 *b = (a + size);
880         u8 c;
881
882         for (; size; size--) {
883                 c = *--b + 1;
884                 *b = c;
885                 if (c)
886                         break;
887         }
888 }
889
890 void crypto_inc(u8 *a, unsigned int size)
891 {
892         __be32 *b = (__be32 *)(a + size);
893         u32 c;
894
895         for (; size >= 4; size -= 4) {
896                 c = be32_to_cpu(*--b) + 1;
897                 *b = cpu_to_be32(c);
898                 if (c)
899                         return;
900         }
901
902         crypto_inc_byte(a, size);
903 }
904 EXPORT_SYMBOL_GPL(crypto_inc);
905
906 static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
907 {
908         for (; size; size--)
909                 *a++ ^= *b++;
910 }
911
912 void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
913 {
914         u32 *a = (u32 *)dst;
915         u32 *b = (u32 *)src;
916
917         for (; size >= 4; size -= 4)
918                 *a++ ^= *b++;
919
920         crypto_xor_byte((u8 *)a, (u8 *)b, size);
921 }
922 EXPORT_SYMBOL_GPL(crypto_xor);
923
924 static int __init crypto_algapi_init(void)
925 {
926         crypto_init_proc();
927         return 0;
928 }
929
930 static void __exit crypto_algapi_exit(void)
931 {
932         crypto_exit_proc();
933 }
934
935 module_init(crypto_algapi_init);
936 module_exit(crypto_algapi_exit);
937
938 MODULE_LICENSE("GPL");
939 MODULE_DESCRIPTION("Cryptographic algorithms API");