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