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