1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Cryptographic API for algorithms (i.e., low-level API).
5 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
8 #include <crypto/algapi.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>
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",
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])
38 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
41 /* General maximums for all algs. */
42 if (alg->cra_alignmask > MAX_ALGAPI_ALIGNMASK)
45 if (alg->cra_blocksize > MAX_ALGAPI_BLOCKSIZE)
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)
54 if (alg->cra_blocksize > MAX_CIPHER_BLOCKSIZE)
58 if (alg->cra_priority < 0)
61 refcount_set(&alg->cra_refcnt, 1);
66 static void crypto_free_instance(struct crypto_instance *inst)
68 if (!inst->alg.cra_type->free) {
69 inst->tmpl->free(inst);
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);
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))
115 inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
116 if (hlist_unhashed(&inst->list))
119 if (!tmpl || !crypto_tmpl_get(tmpl))
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;
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)
144 list_move(&spawn->list, &top);
149 while (!list_empty(spawns)) {
150 struct crypto_instance *inst;
152 spawn = list_first_entry(spawns, struct crypto_spawn,
156 BUG_ON(&inst->alg == alg);
158 list_move(&spawn->list, &stack);
160 if (&inst->alg == nalg)
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)
178 } while ((spawns = crypto_more_spawns(alg, &stack, &top,
179 &secondary_spawns)));
181 list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
183 list_move(&spawn->list, &spawn->alg->cra_users);
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;
196 if (crypto_is_dead(alg))
199 INIT_LIST_HEAD(&alg->cra_users);
202 alg->cra_flags &= ~CRYPTO_ALG_TESTED;
206 list_for_each_entry(q, &crypto_alg_list, cra_list) {
210 if (crypto_is_moribund(q))
213 if (crypto_is_larval(q)) {
214 if (!strcmp(alg->cra_driver_name, q->cra_driver_name))
219 if (!strcmp(q->cra_driver_name, alg->cra_name) ||
220 !strcmp(q->cra_name, alg->cra_driver_name))
224 larval = crypto_larval_alloc(alg->cra_name,
225 alg->cra_flags | CRYPTO_ALG_TESTED, 0);
230 larval->adult = crypto_mod_get(alg);
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);
250 larval = ERR_PTR(ret);
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;
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))
267 test = (struct crypto_larval *)q;
269 if (!strcmp(q->cra_driver_name, name))
273 pr_err("alg: Unexpected test result for %s: %d\n", name, err);
277 q->cra_flags |= CRYPTO_ALG_DEAD;
279 if (err || list_empty(&alg->cra_list))
282 alg->cra_flags |= CRYPTO_ALG_TESTED;
284 /* Only satisfy larval waiters if we are the best. */
286 list_for_each_entry(q, &crypto_alg_list, cra_list) {
287 if (crypto_is_moribund(q) || !crypto_is_larval(q))
290 if (strcmp(alg->cra_name, q->cra_name))
293 if (q->cra_priority > alg->cra_priority) {
299 list_for_each_entry(q, &crypto_alg_list, cra_list) {
303 if (crypto_is_moribund(q))
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))
320 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
323 if (best && crypto_mod_get(alg))
326 larval->adult = ERR_PTR(-EAGAIN);
331 if (strcmp(alg->cra_name, q->cra_name))
334 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
335 q->cra_priority > alg->cra_priority)
338 crypto_remove_spawns(q, &list, alg);
342 complete_all(&test->completion);
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);
361 EXPORT_SYMBOL_GPL(crypto_remove_final);
363 static void crypto_wait_for_test(struct crypto_larval *larval)
367 err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
368 if (err != NOTIFY_STOP) {
369 if (WARN_ON(err != NOTIFY_DONE))
371 crypto_alg_tested(larval->alg.cra_driver_name, 0);
374 err = wait_for_completion_killable(&larval->completion);
377 crypto_probing_notify(CRYPTO_MSG_ALG_LOADED, larval);
380 crypto_larval_kill(&larval->alg);
383 int crypto_register_alg(struct crypto_alg *alg)
385 struct crypto_larval *larval;
388 alg->cra_flags &= ~CRYPTO_ALG_DEAD;
389 err = crypto_check_alg(alg);
393 down_write(&crypto_alg_sem);
394 larval = __crypto_register_alg(alg);
395 up_write(&crypto_alg_sem);
398 return PTR_ERR(larval);
400 crypto_wait_for_test(larval);
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)))
410 alg->cra_flags |= CRYPTO_ALG_DEAD;
412 list_del_init(&alg->cra_list);
413 crypto_remove_spawns(alg, list, NULL);
418 int crypto_unregister_alg(struct crypto_alg *alg)
423 down_write(&crypto_alg_sem);
424 ret = crypto_remove_alg(alg, &list);
425 up_write(&crypto_alg_sem);
430 BUG_ON(refcount_read(&alg->cra_refcnt) != 1);
431 if (alg->cra_destroy)
432 alg->cra_destroy(alg);
434 crypto_remove_final(&list);
437 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
439 int crypto_register_algs(struct crypto_alg *algs, int count)
443 for (i = 0; i < count; i++) {
444 ret = crypto_register_alg(&algs[i]);
452 for (--i; i >= 0; --i)
453 crypto_unregister_alg(&algs[i]);
457 EXPORT_SYMBOL_GPL(crypto_register_algs);
459 int crypto_unregister_algs(struct crypto_alg *algs, int count)
463 for (i = 0; i < count; i++) {
464 ret = crypto_unregister_alg(&algs[i]);
466 pr_err("Failed to unregister %s %s: %d\n",
467 algs[i].cra_driver_name, algs[i].cra_name, ret);
472 EXPORT_SYMBOL_GPL(crypto_unregister_algs);
474 int crypto_register_template(struct crypto_template *tmpl)
476 struct crypto_template *q;
479 down_write(&crypto_alg_sem);
481 crypto_check_module_sig(tmpl->module);
483 list_for_each_entry(q, &crypto_template_list, list) {
488 list_add(&tmpl->list, &crypto_template_list);
491 up_write(&crypto_alg_sem);
494 EXPORT_SYMBOL_GPL(crypto_register_template);
496 int crypto_register_templates(struct crypto_template *tmpls, int count)
500 for (i = 0; i < count; i++) {
501 err = crypto_register_template(&tmpls[i]);
508 for (--i; i >= 0; --i)
509 crypto_unregister_template(&tmpls[i]);
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;
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);
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)
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))
560 if (unlikely(!crypto_tmpl_get(q)))
566 up_read(&crypto_alg_sem);
571 struct crypto_template *crypto_lookup_template(const char *name)
573 return try_then_request_module(__crypto_lookup_template(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;
584 err = crypto_check_alg(&inst->alg);
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);
597 hlist_add_head(&inst->list, &tmpl->instances);
601 up_write(&crypto_alg_sem);
603 err = PTR_ERR(larval);
607 crypto_wait_for_test(larval);
613 EXPORT_SYMBOL_GPL(crypto_register_instance);
615 int crypto_unregister_instance(struct crypto_instance *inst)
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);
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)
637 if (WARN_ON_ONCE(inst == NULL))
643 down_write(&crypto_alg_sem);
644 if (!crypto_is_moribund(alg)) {
645 list_add(&spawn->list, &alg->cra_users);
649 up_write(&crypto_alg_sem);
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)
661 if ((alg->cra_flags ^ frontend->type) & frontend->maskset)
664 spawn->frontend = frontend;
665 err = crypto_init_spawn(spawn, alg, inst, frontend->maskset);
670 EXPORT_SYMBOL_GPL(crypto_init_spawn2);
672 int crypto_grab_spawn(struct crypto_spawn *spawn, const char *name,
675 struct crypto_alg *alg;
678 alg = crypto_find_alg(name, spawn->frontend, type, mask);
682 err = crypto_init_spawn(spawn, alg, spawn->inst, mask);
686 EXPORT_SYMBOL_GPL(crypto_grab_spawn);
688 void crypto_drop_spawn(struct crypto_spawn *spawn)
690 down_write(&crypto_alg_sem);
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);
703 if (alg && !crypto_mod_get(alg)) {
704 alg->cra_flags |= CRYPTO_ALG_DYING;
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,
715 struct crypto_alg *alg;
716 struct crypto_tfm *tfm;
718 alg = crypto_spawn_alg(spawn);
720 return ERR_CAST(alg);
722 tfm = ERR_PTR(-EINVAL);
723 if (unlikely((alg->cra_flags ^ type) & mask))
726 tfm = __crypto_alloc_tfm(alg, type, mask);
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);
745 return ERR_CAST(alg);
747 tfm = crypto_create_tfm(alg, spawn->frontend);
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;
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);
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);
795 return PTR_ERR(algt);
797 if ((algt->type ^ type) & algt->mask)
802 EXPORT_SYMBOL_GPL(crypto_check_attr_type);
804 const char *crypto_attr_alg_name(struct rtattr *rta)
806 struct crypto_attr_alg *alga;
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;
820 EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
822 struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
823 const struct crypto_type *frontend,
828 name = crypto_attr_alg_name(rta);
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;
842 if (RTA_PAYLOAD(rta) < sizeof(*nu32))
844 if (rta->rta_type != CRYPTOA_U32)
847 nu32 = RTA_DATA(rta);
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;
867 EXPORT_SYMBOL_GPL(crypto_inst_setname);
869 void *crypto_alloc_instance(const char *name, struct crypto_alg *alg,
872 struct crypto_instance *inst;
876 p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn),
879 return ERR_PTR(-ENOMEM);
881 inst = (void *)(p + head);
883 err = crypto_inst_setname(inst, name, alg);
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;
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)) {
915 if (queue->backlog == &queue->list)
916 queue->backlog = &request->list;
920 list_add_tail(&request->list, &queue->list);
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))
936 if (queue->backlog != &queue->list)
937 queue->backlog = queue->backlog->next;
939 request = queue->list.next;
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)
951 for (; size; size--) {
959 void crypto_inc(u8 *a, unsigned int size)
961 __be32 *b = (__be32 *)(a + size);
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;
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)
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)) &
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++;
1001 while (IS_ENABLED(CONFIG_64BIT) && len >= 8 && !(relalign & 7)) {
1002 *(u64 *)dst = *(u64 *)src1 ^ *(u64 *)src2;
1009 while (len >= 4 && !(relalign & 3)) {
1010 *(u32 *)dst = *(u32 *)src1 ^ *(u32 *)src2;
1017 while (len >= 2 && !(relalign & 1)) {
1018 *(u16 *)dst = *(u16 *)src1 ^ *(u16 *)src2;
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,
1041 struct crypto_alg *alg = crypto_find_alg(name, frontend, type, mask);
1044 crypto_mod_put(alg);
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);
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);
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,
1094 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1095 atomic64_inc(&alg->stats.aead.err_cnt);
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,
1107 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1108 atomic64_inc(&alg->stats.aead.err_cnt);
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);
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);
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);
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);
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);
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);
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);
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);
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)
1214 atomic64_inc(&alg->stats.kpp.err_cnt);
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)
1224 atomic64_inc(&alg->stats.kpp.err_cnt);
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)
1234 atomic64_inc(&alg->stats.kpp.err_cnt);
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);
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,
1254 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1255 atomic64_inc(&alg->stats.rng.err_cnt);
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);
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);
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);
1291 static int __init crypto_algapi_init(void)
1297 static void __exit crypto_algapi_exit(void)
1302 module_init(crypto_algapi_init);
1303 module_exit(crypto_algapi_exit);
1305 MODULE_LICENSE("GPL");
1306 MODULE_DESCRIPTION("Cryptographic algorithms API");