net/mlx5: Fix FTE cleanup
[linux/fpc-iii.git] / crypto / algapi.c
blobfff52bc9d97d00eccfbf999f43e0c648758b923a
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Cryptographic API for algorithms (i.e., low-level API).
5 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
6 */
8 #include <crypto/algapi.h>
9 #include <linux/err.h>
10 #include <linux/errno.h>
11 #include <linux/fips.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/rtnetlink.h>
17 #include <linux/slab.h>
18 #include <linux/string.h>
20 #include "internal.h"
22 static LIST_HEAD(crypto_template_list);
24 static inline void crypto_check_module_sig(struct module *mod)
26 if (fips_enabled && mod && !module_sig_ok(mod))
27 panic("Module %s signature verification failed in FIPS mode\n",
28 module_name(mod));
31 static int crypto_check_alg(struct crypto_alg *alg)
33 crypto_check_module_sig(alg->cra_module);
35 if (!alg->cra_name[0] || !alg->cra_driver_name[0])
36 return -EINVAL;
38 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
39 return -EINVAL;
41 /* General maximums for all algs. */
42 if (alg->cra_alignmask > MAX_ALGAPI_ALIGNMASK)
43 return -EINVAL;
45 if (alg->cra_blocksize > MAX_ALGAPI_BLOCKSIZE)
46 return -EINVAL;
48 /* Lower maximums for specific alg types. */
49 if (!alg->cra_type && (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
50 CRYPTO_ALG_TYPE_CIPHER) {
51 if (alg->cra_alignmask > MAX_CIPHER_ALIGNMASK)
52 return -EINVAL;
54 if (alg->cra_blocksize > MAX_CIPHER_BLOCKSIZE)
55 return -EINVAL;
58 if (alg->cra_priority < 0)
59 return -EINVAL;
61 refcount_set(&alg->cra_refcnt, 1);
63 return 0;
66 static void crypto_free_instance(struct crypto_instance *inst)
68 if (!inst->alg.cra_type->free) {
69 inst->tmpl->free(inst);
70 return;
73 inst->alg.cra_type->free(inst);
76 static void crypto_destroy_instance(struct crypto_alg *alg)
78 struct crypto_instance *inst = (void *)alg;
79 struct crypto_template *tmpl = inst->tmpl;
81 crypto_free_instance(inst);
82 crypto_tmpl_put(tmpl);
85 static struct list_head *crypto_more_spawns(struct crypto_alg *alg,
86 struct list_head *stack,
87 struct list_head *top,
88 struct list_head *secondary_spawns)
90 struct crypto_spawn *spawn, *n;
92 spawn = list_first_entry_or_null(stack, struct crypto_spawn, list);
93 if (!spawn)
94 return NULL;
96 n = list_next_entry(spawn, list);
98 if (spawn->alg && &n->list != stack && !n->alg)
99 n->alg = (n->list.next == stack) ? alg :
100 &list_next_entry(n, list)->inst->alg;
102 list_move(&spawn->list, secondary_spawns);
104 return &n->list == stack ? top : &n->inst->alg.cra_users;
107 static void crypto_remove_instance(struct crypto_instance *inst,
108 struct list_head *list)
110 struct crypto_template *tmpl = inst->tmpl;
112 if (crypto_is_dead(&inst->alg))
113 return;
115 inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
116 if (hlist_unhashed(&inst->list))
117 return;
119 if (!tmpl || !crypto_tmpl_get(tmpl))
120 return;
122 list_move(&inst->alg.cra_list, list);
123 hlist_del(&inst->list);
124 inst->alg.cra_destroy = crypto_destroy_instance;
126 BUG_ON(!list_empty(&inst->alg.cra_users));
129 void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
130 struct crypto_alg *nalg)
132 u32 new_type = (nalg ?: alg)->cra_flags;
133 struct crypto_spawn *spawn, *n;
134 LIST_HEAD(secondary_spawns);
135 struct list_head *spawns;
136 LIST_HEAD(stack);
137 LIST_HEAD(top);
139 spawns = &alg->cra_users;
140 list_for_each_entry_safe(spawn, n, spawns, list) {
141 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
142 continue;
144 list_move(&spawn->list, &top);
147 spawns = &top;
148 do {
149 while (!list_empty(spawns)) {
150 struct crypto_instance *inst;
152 spawn = list_first_entry(spawns, struct crypto_spawn,
153 list);
154 inst = spawn->inst;
156 BUG_ON(&inst->alg == alg);
158 list_move(&spawn->list, &stack);
160 if (&inst->alg == nalg)
161 break;
163 spawn->alg = NULL;
164 spawns = &inst->alg.cra_users;
167 * We may encounter an unregistered instance here, since
168 * an instance's spawns are set up prior to the instance
169 * being registered. An unregistered instance will have
170 * NULL ->cra_users.next, since ->cra_users isn't
171 * properly initialized until registration. But an
172 * unregistered instance cannot have any users, so treat
173 * it the same as ->cra_users being empty.
175 if (spawns->next == NULL)
176 break;
178 } while ((spawns = crypto_more_spawns(alg, &stack, &top,
179 &secondary_spawns)));
181 list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
182 if (spawn->alg)
183 list_move(&spawn->list, &spawn->alg->cra_users);
184 else
185 crypto_remove_instance(spawn->inst, list);
188 EXPORT_SYMBOL_GPL(crypto_remove_spawns);
190 static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)
192 struct crypto_alg *q;
193 struct crypto_larval *larval;
194 int ret = -EAGAIN;
196 if (crypto_is_dead(alg))
197 goto err;
199 INIT_LIST_HEAD(&alg->cra_users);
201 /* No cheating! */
202 alg->cra_flags &= ~CRYPTO_ALG_TESTED;
204 ret = -EEXIST;
206 list_for_each_entry(q, &crypto_alg_list, cra_list) {
207 if (q == alg)
208 goto err;
210 if (crypto_is_moribund(q))
211 continue;
213 if (crypto_is_larval(q)) {
214 if (!strcmp(alg->cra_driver_name, q->cra_driver_name))
215 goto err;
216 continue;
219 if (!strcmp(q->cra_driver_name, alg->cra_name) ||
220 !strcmp(q->cra_name, alg->cra_driver_name))
221 goto err;
224 larval = crypto_larval_alloc(alg->cra_name,
225 alg->cra_flags | CRYPTO_ALG_TESTED, 0);
226 if (IS_ERR(larval))
227 goto out;
229 ret = -ENOENT;
230 larval->adult = crypto_mod_get(alg);
231 if (!larval->adult)
232 goto free_larval;
234 refcount_set(&larval->alg.cra_refcnt, 1);
235 memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
236 CRYPTO_MAX_ALG_NAME);
237 larval->alg.cra_priority = alg->cra_priority;
239 list_add(&alg->cra_list, &crypto_alg_list);
240 list_add(&larval->alg.cra_list, &crypto_alg_list);
242 crypto_stats_init(alg);
244 out:
245 return larval;
247 free_larval:
248 kfree(larval);
249 err:
250 larval = ERR_PTR(ret);
251 goto out;
254 void crypto_alg_tested(const char *name, int err)
256 struct crypto_larval *test;
257 struct crypto_alg *alg;
258 struct crypto_alg *q;
259 LIST_HEAD(list);
260 bool best;
262 down_write(&crypto_alg_sem);
263 list_for_each_entry(q, &crypto_alg_list, cra_list) {
264 if (crypto_is_moribund(q) || !crypto_is_larval(q))
265 continue;
267 test = (struct crypto_larval *)q;
269 if (!strcmp(q->cra_driver_name, name))
270 goto found;
273 pr_err("alg: Unexpected test result for %s: %d\n", name, err);
274 goto unlock;
276 found:
277 q->cra_flags |= CRYPTO_ALG_DEAD;
278 alg = test->adult;
279 if (err || list_empty(&alg->cra_list))
280 goto complete;
282 alg->cra_flags |= CRYPTO_ALG_TESTED;
284 /* Only satisfy larval waiters if we are the best. */
285 best = true;
286 list_for_each_entry(q, &crypto_alg_list, cra_list) {
287 if (crypto_is_moribund(q) || !crypto_is_larval(q))
288 continue;
290 if (strcmp(alg->cra_name, q->cra_name))
291 continue;
293 if (q->cra_priority > alg->cra_priority) {
294 best = false;
295 break;
299 list_for_each_entry(q, &crypto_alg_list, cra_list) {
300 if (q == alg)
301 continue;
303 if (crypto_is_moribund(q))
304 continue;
306 if (crypto_is_larval(q)) {
307 struct crypto_larval *larval = (void *)q;
310 * Check to see if either our generic name or
311 * specific name can satisfy the name requested
312 * by the larval entry q.
314 if (strcmp(alg->cra_name, q->cra_name) &&
315 strcmp(alg->cra_driver_name, q->cra_name))
316 continue;
318 if (larval->adult)
319 continue;
320 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
321 continue;
323 if (best && crypto_mod_get(alg))
324 larval->adult = alg;
325 else
326 larval->adult = ERR_PTR(-EAGAIN);
328 continue;
331 if (strcmp(alg->cra_name, q->cra_name))
332 continue;
334 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
335 q->cra_priority > alg->cra_priority)
336 continue;
338 crypto_remove_spawns(q, &list, alg);
341 complete:
342 complete_all(&test->completion);
344 unlock:
345 up_write(&crypto_alg_sem);
347 crypto_remove_final(&list);
349 EXPORT_SYMBOL_GPL(crypto_alg_tested);
351 void crypto_remove_final(struct list_head *list)
353 struct crypto_alg *alg;
354 struct crypto_alg *n;
356 list_for_each_entry_safe(alg, n, list, cra_list) {
357 list_del_init(&alg->cra_list);
358 crypto_alg_put(alg);
361 EXPORT_SYMBOL_GPL(crypto_remove_final);
363 static void crypto_wait_for_test(struct crypto_larval *larval)
365 int err;
367 err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
368 if (err != NOTIFY_STOP) {
369 if (WARN_ON(err != NOTIFY_DONE))
370 goto out;
371 crypto_alg_tested(larval->alg.cra_driver_name, 0);
374 err = wait_for_completion_killable(&larval->completion);
375 WARN_ON(err);
376 if (!err)
377 crypto_notify(CRYPTO_MSG_ALG_LOADED, larval);
379 out:
380 crypto_larval_kill(&larval->alg);
383 int crypto_register_alg(struct crypto_alg *alg)
385 struct crypto_larval *larval;
386 int err;
388 alg->cra_flags &= ~CRYPTO_ALG_DEAD;
389 err = crypto_check_alg(alg);
390 if (err)
391 return err;
393 down_write(&crypto_alg_sem);
394 larval = __crypto_register_alg(alg);
395 up_write(&crypto_alg_sem);
397 if (IS_ERR(larval))
398 return PTR_ERR(larval);
400 crypto_wait_for_test(larval);
401 return 0;
403 EXPORT_SYMBOL_GPL(crypto_register_alg);
405 static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
407 if (unlikely(list_empty(&alg->cra_list)))
408 return -ENOENT;
410 alg->cra_flags |= CRYPTO_ALG_DEAD;
412 list_del_init(&alg->cra_list);
413 crypto_remove_spawns(alg, list, NULL);
415 return 0;
418 int crypto_unregister_alg(struct crypto_alg *alg)
420 int ret;
421 LIST_HEAD(list);
423 down_write(&crypto_alg_sem);
424 ret = crypto_remove_alg(alg, &list);
425 up_write(&crypto_alg_sem);
427 if (ret)
428 return ret;
430 BUG_ON(refcount_read(&alg->cra_refcnt) != 1);
431 if (alg->cra_destroy)
432 alg->cra_destroy(alg);
434 crypto_remove_final(&list);
435 return 0;
437 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
439 int crypto_register_algs(struct crypto_alg *algs, int count)
441 int i, ret;
443 for (i = 0; i < count; i++) {
444 ret = crypto_register_alg(&algs[i]);
445 if (ret)
446 goto err;
449 return 0;
451 err:
452 for (--i; i >= 0; --i)
453 crypto_unregister_alg(&algs[i]);
455 return ret;
457 EXPORT_SYMBOL_GPL(crypto_register_algs);
459 int crypto_unregister_algs(struct crypto_alg *algs, int count)
461 int i, ret;
463 for (i = 0; i < count; i++) {
464 ret = crypto_unregister_alg(&algs[i]);
465 if (ret)
466 pr_err("Failed to unregister %s %s: %d\n",
467 algs[i].cra_driver_name, algs[i].cra_name, ret);
470 return 0;
472 EXPORT_SYMBOL_GPL(crypto_unregister_algs);
474 int crypto_register_template(struct crypto_template *tmpl)
476 struct crypto_template *q;
477 int err = -EEXIST;
479 down_write(&crypto_alg_sem);
481 crypto_check_module_sig(tmpl->module);
483 list_for_each_entry(q, &crypto_template_list, list) {
484 if (q == tmpl)
485 goto out;
488 list_add(&tmpl->list, &crypto_template_list);
489 err = 0;
490 out:
491 up_write(&crypto_alg_sem);
492 return err;
494 EXPORT_SYMBOL_GPL(crypto_register_template);
496 int crypto_register_templates(struct crypto_template *tmpls, int count)
498 int i, err;
500 for (i = 0; i < count; i++) {
501 err = crypto_register_template(&tmpls[i]);
502 if (err)
503 goto out;
505 return 0;
507 out:
508 for (--i; i >= 0; --i)
509 crypto_unregister_template(&tmpls[i]);
510 return err;
512 EXPORT_SYMBOL_GPL(crypto_register_templates);
514 void crypto_unregister_template(struct crypto_template *tmpl)
516 struct crypto_instance *inst;
517 struct hlist_node *n;
518 struct hlist_head *list;
519 LIST_HEAD(users);
521 down_write(&crypto_alg_sem);
523 BUG_ON(list_empty(&tmpl->list));
524 list_del_init(&tmpl->list);
526 list = &tmpl->instances;
527 hlist_for_each_entry(inst, list, list) {
528 int err = crypto_remove_alg(&inst->alg, &users);
530 BUG_ON(err);
533 up_write(&crypto_alg_sem);
535 hlist_for_each_entry_safe(inst, n, list, list) {
536 BUG_ON(refcount_read(&inst->alg.cra_refcnt) != 1);
537 crypto_free_instance(inst);
539 crypto_remove_final(&users);
541 EXPORT_SYMBOL_GPL(crypto_unregister_template);
543 void crypto_unregister_templates(struct crypto_template *tmpls, int count)
545 int i;
547 for (i = count - 1; i >= 0; --i)
548 crypto_unregister_template(&tmpls[i]);
550 EXPORT_SYMBOL_GPL(crypto_unregister_templates);
552 static struct crypto_template *__crypto_lookup_template(const char *name)
554 struct crypto_template *q, *tmpl = NULL;
556 down_read(&crypto_alg_sem);
557 list_for_each_entry(q, &crypto_template_list, list) {
558 if (strcmp(q->name, name))
559 continue;
560 if (unlikely(!crypto_tmpl_get(q)))
561 continue;
563 tmpl = q;
564 break;
566 up_read(&crypto_alg_sem);
568 return tmpl;
571 struct crypto_template *crypto_lookup_template(const char *name)
573 return try_then_request_module(__crypto_lookup_template(name),
574 "crypto-%s", name);
576 EXPORT_SYMBOL_GPL(crypto_lookup_template);
578 int crypto_register_instance(struct crypto_template *tmpl,
579 struct crypto_instance *inst)
581 struct crypto_larval *larval;
582 int err;
584 err = crypto_check_alg(&inst->alg);
585 if (err)
586 return err;
588 inst->alg.cra_module = tmpl->module;
589 inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
591 down_write(&crypto_alg_sem);
593 larval = __crypto_register_alg(&inst->alg);
594 if (IS_ERR(larval))
595 goto unlock;
597 hlist_add_head(&inst->list, &tmpl->instances);
598 inst->tmpl = tmpl;
600 unlock:
601 up_write(&crypto_alg_sem);
603 err = PTR_ERR(larval);
604 if (IS_ERR(larval))
605 goto err;
607 crypto_wait_for_test(larval);
608 err = 0;
610 err:
611 return err;
613 EXPORT_SYMBOL_GPL(crypto_register_instance);
615 int crypto_unregister_instance(struct crypto_instance *inst)
617 LIST_HEAD(list);
619 down_write(&crypto_alg_sem);
621 crypto_remove_spawns(&inst->alg, &list, NULL);
622 crypto_remove_instance(inst, &list);
624 up_write(&crypto_alg_sem);
626 crypto_remove_final(&list);
628 return 0;
630 EXPORT_SYMBOL_GPL(crypto_unregister_instance);
632 int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
633 struct crypto_instance *inst, u32 mask)
635 int err = -EAGAIN;
637 if (WARN_ON_ONCE(inst == NULL))
638 return -EINVAL;
640 spawn->inst = inst;
641 spawn->mask = mask;
643 down_write(&crypto_alg_sem);
644 if (!crypto_is_moribund(alg)) {
645 list_add(&spawn->list, &alg->cra_users);
646 spawn->alg = alg;
647 err = 0;
649 up_write(&crypto_alg_sem);
651 return err;
653 EXPORT_SYMBOL_GPL(crypto_init_spawn);
655 int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg,
656 struct crypto_instance *inst,
657 const struct crypto_type *frontend)
659 int err = -EINVAL;
661 if ((alg->cra_flags ^ frontend->type) & frontend->maskset)
662 goto out;
664 spawn->frontend = frontend;
665 err = crypto_init_spawn(spawn, alg, inst, frontend->maskset);
667 out:
668 return err;
670 EXPORT_SYMBOL_GPL(crypto_init_spawn2);
672 int crypto_grab_spawn(struct crypto_spawn *spawn, const char *name,
673 u32 type, u32 mask)
675 struct crypto_alg *alg;
676 int err;
678 alg = crypto_find_alg(name, spawn->frontend, type, mask);
679 if (IS_ERR(alg))
680 return PTR_ERR(alg);
682 err = crypto_init_spawn(spawn, alg, spawn->inst, mask);
683 crypto_mod_put(alg);
684 return err;
686 EXPORT_SYMBOL_GPL(crypto_grab_spawn);
688 void crypto_drop_spawn(struct crypto_spawn *spawn)
690 down_write(&crypto_alg_sem);
691 if (spawn->alg)
692 list_del(&spawn->list);
693 up_write(&crypto_alg_sem);
695 EXPORT_SYMBOL_GPL(crypto_drop_spawn);
697 static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
699 struct crypto_alg *alg;
701 down_read(&crypto_alg_sem);
702 alg = spawn->alg;
703 if (alg && !crypto_mod_get(alg)) {
704 alg->cra_flags |= CRYPTO_ALG_DYING;
705 alg = NULL;
707 up_read(&crypto_alg_sem);
709 return alg ?: ERR_PTR(-EAGAIN);
712 struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
713 u32 mask)
715 struct crypto_alg *alg;
716 struct crypto_tfm *tfm;
718 alg = crypto_spawn_alg(spawn);
719 if (IS_ERR(alg))
720 return ERR_CAST(alg);
722 tfm = ERR_PTR(-EINVAL);
723 if (unlikely((alg->cra_flags ^ type) & mask))
724 goto out_put_alg;
726 tfm = __crypto_alloc_tfm(alg, type, mask);
727 if (IS_ERR(tfm))
728 goto out_put_alg;
730 return tfm;
732 out_put_alg:
733 crypto_mod_put(alg);
734 return tfm;
736 EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
738 void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
740 struct crypto_alg *alg;
741 struct crypto_tfm *tfm;
743 alg = crypto_spawn_alg(spawn);
744 if (IS_ERR(alg))
745 return ERR_CAST(alg);
747 tfm = crypto_create_tfm(alg, spawn->frontend);
748 if (IS_ERR(tfm))
749 goto out_put_alg;
751 return tfm;
753 out_put_alg:
754 crypto_mod_put(alg);
755 return tfm;
757 EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
759 int crypto_register_notifier(struct notifier_block *nb)
761 return blocking_notifier_chain_register(&crypto_chain, nb);
763 EXPORT_SYMBOL_GPL(crypto_register_notifier);
765 int crypto_unregister_notifier(struct notifier_block *nb)
767 return blocking_notifier_chain_unregister(&crypto_chain, nb);
769 EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
771 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
773 struct rtattr *rta = tb[0];
774 struct crypto_attr_type *algt;
776 if (!rta)
777 return ERR_PTR(-ENOENT);
778 if (RTA_PAYLOAD(rta) < sizeof(*algt))
779 return ERR_PTR(-EINVAL);
780 if (rta->rta_type != CRYPTOA_TYPE)
781 return ERR_PTR(-EINVAL);
783 algt = RTA_DATA(rta);
785 return algt;
787 EXPORT_SYMBOL_GPL(crypto_get_attr_type);
789 int crypto_check_attr_type(struct rtattr **tb, u32 type)
791 struct crypto_attr_type *algt;
793 algt = crypto_get_attr_type(tb);
794 if (IS_ERR(algt))
795 return PTR_ERR(algt);
797 if ((algt->type ^ type) & algt->mask)
798 return -EINVAL;
800 return 0;
802 EXPORT_SYMBOL_GPL(crypto_check_attr_type);
804 const char *crypto_attr_alg_name(struct rtattr *rta)
806 struct crypto_attr_alg *alga;
808 if (!rta)
809 return ERR_PTR(-ENOENT);
810 if (RTA_PAYLOAD(rta) < sizeof(*alga))
811 return ERR_PTR(-EINVAL);
812 if (rta->rta_type != CRYPTOA_ALG)
813 return ERR_PTR(-EINVAL);
815 alga = RTA_DATA(rta);
816 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
818 return alga->name;
820 EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
822 struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
823 const struct crypto_type *frontend,
824 u32 type, u32 mask)
826 const char *name;
828 name = crypto_attr_alg_name(rta);
829 if (IS_ERR(name))
830 return ERR_CAST(name);
832 return crypto_find_alg(name, frontend, type, mask);
834 EXPORT_SYMBOL_GPL(crypto_attr_alg2);
836 int crypto_attr_u32(struct rtattr *rta, u32 *num)
838 struct crypto_attr_u32 *nu32;
840 if (!rta)
841 return -ENOENT;
842 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
843 return -EINVAL;
844 if (rta->rta_type != CRYPTOA_U32)
845 return -EINVAL;
847 nu32 = RTA_DATA(rta);
848 *num = nu32->num;
850 return 0;
852 EXPORT_SYMBOL_GPL(crypto_attr_u32);
854 int crypto_inst_setname(struct crypto_instance *inst, const char *name,
855 struct crypto_alg *alg)
857 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
858 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
859 return -ENAMETOOLONG;
861 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
862 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
863 return -ENAMETOOLONG;
865 return 0;
867 EXPORT_SYMBOL_GPL(crypto_inst_setname);
869 void *crypto_alloc_instance(const char *name, struct crypto_alg *alg,
870 unsigned int head)
872 struct crypto_instance *inst;
873 char *p;
874 int err;
876 p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn),
877 GFP_KERNEL);
878 if (!p)
879 return ERR_PTR(-ENOMEM);
881 inst = (void *)(p + head);
883 err = crypto_inst_setname(inst, name, alg);
884 if (err)
885 goto err_free_inst;
887 return p;
889 err_free_inst:
890 kfree(p);
891 return ERR_PTR(err);
893 EXPORT_SYMBOL_GPL(crypto_alloc_instance);
895 void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
897 INIT_LIST_HEAD(&queue->list);
898 queue->backlog = &queue->list;
899 queue->qlen = 0;
900 queue->max_qlen = max_qlen;
902 EXPORT_SYMBOL_GPL(crypto_init_queue);
904 int crypto_enqueue_request(struct crypto_queue *queue,
905 struct crypto_async_request *request)
907 int err = -EINPROGRESS;
909 if (unlikely(queue->qlen >= queue->max_qlen)) {
910 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
911 err = -ENOSPC;
912 goto out;
914 err = -EBUSY;
915 if (queue->backlog == &queue->list)
916 queue->backlog = &request->list;
919 queue->qlen++;
920 list_add_tail(&request->list, &queue->list);
922 out:
923 return err;
925 EXPORT_SYMBOL_GPL(crypto_enqueue_request);
927 struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
929 struct list_head *request;
931 if (unlikely(!queue->qlen))
932 return NULL;
934 queue->qlen--;
936 if (queue->backlog != &queue->list)
937 queue->backlog = queue->backlog->next;
939 request = queue->list.next;
940 list_del(request);
942 return list_entry(request, struct crypto_async_request, list);
944 EXPORT_SYMBOL_GPL(crypto_dequeue_request);
946 static inline void crypto_inc_byte(u8 *a, unsigned int size)
948 u8 *b = (a + size);
949 u8 c;
951 for (; size; size--) {
952 c = *--b + 1;
953 *b = c;
954 if (c)
955 break;
959 void crypto_inc(u8 *a, unsigned int size)
961 __be32 *b = (__be32 *)(a + size);
962 u32 c;
964 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
965 IS_ALIGNED((unsigned long)b, __alignof__(*b)))
966 for (; size >= 4; size -= 4) {
967 c = be32_to_cpu(*--b) + 1;
968 *b = cpu_to_be32(c);
969 if (likely(c))
970 return;
973 crypto_inc_byte(a, size);
975 EXPORT_SYMBOL_GPL(crypto_inc);
977 void __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int len)
979 int relalign = 0;
981 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {
982 int size = sizeof(unsigned long);
983 int d = (((unsigned long)dst ^ (unsigned long)src1) |
984 ((unsigned long)dst ^ (unsigned long)src2)) &
985 (size - 1);
987 relalign = d ? 1 << __ffs(d) : size;
990 * If we care about alignment, process as many bytes as
991 * needed to advance dst and src to values whose alignments
992 * equal their relative alignment. This will allow us to
993 * process the remainder of the input using optimal strides.
995 while (((unsigned long)dst & (relalign - 1)) && len > 0) {
996 *dst++ = *src1++ ^ *src2++;
997 len--;
1001 while (IS_ENABLED(CONFIG_64BIT) && len >= 8 && !(relalign & 7)) {
1002 *(u64 *)dst = *(u64 *)src1 ^ *(u64 *)src2;
1003 dst += 8;
1004 src1 += 8;
1005 src2 += 8;
1006 len -= 8;
1009 while (len >= 4 && !(relalign & 3)) {
1010 *(u32 *)dst = *(u32 *)src1 ^ *(u32 *)src2;
1011 dst += 4;
1012 src1 += 4;
1013 src2 += 4;
1014 len -= 4;
1017 while (len >= 2 && !(relalign & 1)) {
1018 *(u16 *)dst = *(u16 *)src1 ^ *(u16 *)src2;
1019 dst += 2;
1020 src1 += 2;
1021 src2 += 2;
1022 len -= 2;
1025 while (len--)
1026 *dst++ = *src1++ ^ *src2++;
1028 EXPORT_SYMBOL_GPL(__crypto_xor);
1030 unsigned int crypto_alg_extsize(struct crypto_alg *alg)
1032 return alg->cra_ctxsize +
1033 (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
1035 EXPORT_SYMBOL_GPL(crypto_alg_extsize);
1037 int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
1038 u32 type, u32 mask)
1040 int ret = 0;
1041 struct crypto_alg *alg = crypto_find_alg(name, frontend, type, mask);
1043 if (!IS_ERR(alg)) {
1044 crypto_mod_put(alg);
1045 ret = 1;
1048 return ret;
1050 EXPORT_SYMBOL_GPL(crypto_type_has_alg);
1052 #ifdef CONFIG_CRYPTO_STATS
1053 void crypto_stats_init(struct crypto_alg *alg)
1055 memset(&alg->stats, 0, sizeof(alg->stats));
1057 EXPORT_SYMBOL_GPL(crypto_stats_init);
1059 void crypto_stats_get(struct crypto_alg *alg)
1061 crypto_alg_get(alg);
1063 EXPORT_SYMBOL_GPL(crypto_stats_get);
1065 void crypto_stats_ablkcipher_encrypt(unsigned int nbytes, int ret,
1066 struct crypto_alg *alg)
1068 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1069 atomic64_inc(&alg->stats.cipher.err_cnt);
1070 } else {
1071 atomic64_inc(&alg->stats.cipher.encrypt_cnt);
1072 atomic64_add(nbytes, &alg->stats.cipher.encrypt_tlen);
1074 crypto_alg_put(alg);
1076 EXPORT_SYMBOL_GPL(crypto_stats_ablkcipher_encrypt);
1078 void crypto_stats_ablkcipher_decrypt(unsigned int nbytes, int ret,
1079 struct crypto_alg *alg)
1081 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1082 atomic64_inc(&alg->stats.cipher.err_cnt);
1083 } else {
1084 atomic64_inc(&alg->stats.cipher.decrypt_cnt);
1085 atomic64_add(nbytes, &alg->stats.cipher.decrypt_tlen);
1087 crypto_alg_put(alg);
1089 EXPORT_SYMBOL_GPL(crypto_stats_ablkcipher_decrypt);
1091 void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg,
1092 int ret)
1094 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1095 atomic64_inc(&alg->stats.aead.err_cnt);
1096 } else {
1097 atomic64_inc(&alg->stats.aead.encrypt_cnt);
1098 atomic64_add(cryptlen, &alg->stats.aead.encrypt_tlen);
1100 crypto_alg_put(alg);
1102 EXPORT_SYMBOL_GPL(crypto_stats_aead_encrypt);
1104 void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg,
1105 int ret)
1107 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1108 atomic64_inc(&alg->stats.aead.err_cnt);
1109 } else {
1110 atomic64_inc(&alg->stats.aead.decrypt_cnt);
1111 atomic64_add(cryptlen, &alg->stats.aead.decrypt_tlen);
1113 crypto_alg_put(alg);
1115 EXPORT_SYMBOL_GPL(crypto_stats_aead_decrypt);
1117 void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret,
1118 struct crypto_alg *alg)
1120 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1121 atomic64_inc(&alg->stats.akcipher.err_cnt);
1122 } else {
1123 atomic64_inc(&alg->stats.akcipher.encrypt_cnt);
1124 atomic64_add(src_len, &alg->stats.akcipher.encrypt_tlen);
1126 crypto_alg_put(alg);
1128 EXPORT_SYMBOL_GPL(crypto_stats_akcipher_encrypt);
1130 void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret,
1131 struct crypto_alg *alg)
1133 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1134 atomic64_inc(&alg->stats.akcipher.err_cnt);
1135 } else {
1136 atomic64_inc(&alg->stats.akcipher.decrypt_cnt);
1137 atomic64_add(src_len, &alg->stats.akcipher.decrypt_tlen);
1139 crypto_alg_put(alg);
1141 EXPORT_SYMBOL_GPL(crypto_stats_akcipher_decrypt);
1143 void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg)
1145 if (ret && ret != -EINPROGRESS && ret != -EBUSY)
1146 atomic64_inc(&alg->stats.akcipher.err_cnt);
1147 else
1148 atomic64_inc(&alg->stats.akcipher.sign_cnt);
1149 crypto_alg_put(alg);
1151 EXPORT_SYMBOL_GPL(crypto_stats_akcipher_sign);
1153 void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg)
1155 if (ret && ret != -EINPROGRESS && ret != -EBUSY)
1156 atomic64_inc(&alg->stats.akcipher.err_cnt);
1157 else
1158 atomic64_inc(&alg->stats.akcipher.verify_cnt);
1159 crypto_alg_put(alg);
1161 EXPORT_SYMBOL_GPL(crypto_stats_akcipher_verify);
1163 void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg)
1165 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1166 atomic64_inc(&alg->stats.compress.err_cnt);
1167 } else {
1168 atomic64_inc(&alg->stats.compress.compress_cnt);
1169 atomic64_add(slen, &alg->stats.compress.compress_tlen);
1171 crypto_alg_put(alg);
1173 EXPORT_SYMBOL_GPL(crypto_stats_compress);
1175 void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg)
1177 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1178 atomic64_inc(&alg->stats.compress.err_cnt);
1179 } else {
1180 atomic64_inc(&alg->stats.compress.decompress_cnt);
1181 atomic64_add(slen, &alg->stats.compress.decompress_tlen);
1183 crypto_alg_put(alg);
1185 EXPORT_SYMBOL_GPL(crypto_stats_decompress);
1187 void crypto_stats_ahash_update(unsigned int nbytes, int ret,
1188 struct crypto_alg *alg)
1190 if (ret && ret != -EINPROGRESS && ret != -EBUSY)
1191 atomic64_inc(&alg->stats.hash.err_cnt);
1192 else
1193 atomic64_add(nbytes, &alg->stats.hash.hash_tlen);
1194 crypto_alg_put(alg);
1196 EXPORT_SYMBOL_GPL(crypto_stats_ahash_update);
1198 void crypto_stats_ahash_final(unsigned int nbytes, int ret,
1199 struct crypto_alg *alg)
1201 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1202 atomic64_inc(&alg->stats.hash.err_cnt);
1203 } else {
1204 atomic64_inc(&alg->stats.hash.hash_cnt);
1205 atomic64_add(nbytes, &alg->stats.hash.hash_tlen);
1207 crypto_alg_put(alg);
1209 EXPORT_SYMBOL_GPL(crypto_stats_ahash_final);
1211 void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret)
1213 if (ret)
1214 atomic64_inc(&alg->stats.kpp.err_cnt);
1215 else
1216 atomic64_inc(&alg->stats.kpp.setsecret_cnt);
1217 crypto_alg_put(alg);
1219 EXPORT_SYMBOL_GPL(crypto_stats_kpp_set_secret);
1221 void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret)
1223 if (ret)
1224 atomic64_inc(&alg->stats.kpp.err_cnt);
1225 else
1226 atomic64_inc(&alg->stats.kpp.generate_public_key_cnt);
1227 crypto_alg_put(alg);
1229 EXPORT_SYMBOL_GPL(crypto_stats_kpp_generate_public_key);
1231 void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret)
1233 if (ret)
1234 atomic64_inc(&alg->stats.kpp.err_cnt);
1235 else
1236 atomic64_inc(&alg->stats.kpp.compute_shared_secret_cnt);
1237 crypto_alg_put(alg);
1239 EXPORT_SYMBOL_GPL(crypto_stats_kpp_compute_shared_secret);
1241 void crypto_stats_rng_seed(struct crypto_alg *alg, int ret)
1243 if (ret && ret != -EINPROGRESS && ret != -EBUSY)
1244 atomic64_inc(&alg->stats.rng.err_cnt);
1245 else
1246 atomic64_inc(&alg->stats.rng.seed_cnt);
1247 crypto_alg_put(alg);
1249 EXPORT_SYMBOL_GPL(crypto_stats_rng_seed);
1251 void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen,
1252 int ret)
1254 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1255 atomic64_inc(&alg->stats.rng.err_cnt);
1256 } else {
1257 atomic64_inc(&alg->stats.rng.generate_cnt);
1258 atomic64_add(dlen, &alg->stats.rng.generate_tlen);
1260 crypto_alg_put(alg);
1262 EXPORT_SYMBOL_GPL(crypto_stats_rng_generate);
1264 void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret,
1265 struct crypto_alg *alg)
1267 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1268 atomic64_inc(&alg->stats.cipher.err_cnt);
1269 } else {
1270 atomic64_inc(&alg->stats.cipher.encrypt_cnt);
1271 atomic64_add(cryptlen, &alg->stats.cipher.encrypt_tlen);
1273 crypto_alg_put(alg);
1275 EXPORT_SYMBOL_GPL(crypto_stats_skcipher_encrypt);
1277 void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret,
1278 struct crypto_alg *alg)
1280 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1281 atomic64_inc(&alg->stats.cipher.err_cnt);
1282 } else {
1283 atomic64_inc(&alg->stats.cipher.decrypt_cnt);
1284 atomic64_add(cryptlen, &alg->stats.cipher.decrypt_tlen);
1286 crypto_alg_put(alg);
1288 EXPORT_SYMBOL_GPL(crypto_stats_skcipher_decrypt);
1289 #endif
1291 static int __init crypto_algapi_init(void)
1293 crypto_init_proc();
1294 return 0;
1297 static void __exit crypto_algapi_exit(void)
1299 crypto_exit_proc();
1302 module_init(crypto_algapi_init);
1303 module_exit(crypto_algapi_exit);
1305 MODULE_LICENSE("GPL");
1306 MODULE_DESCRIPTION("Cryptographic algorithms API");