1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Cryptographic API for algorithms (i.e., low-level API).
4 *
5 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
6 */
7
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>
19
20 #include "internal.h"
21
22 static LIST_HEAD(crypto_template_list);
23
crypto_check_module_sig(struct module * mod)24 static inline void crypto_check_module_sig(struct module *mod)
25 {
26 if (fips_enabled && mod && !module_sig_ok(mod))
27 panic("Module %s signature verification failed in FIPS mode\n",
28 module_name(mod));
29 }
30
crypto_check_alg(struct crypto_alg * alg)31 static int crypto_check_alg(struct crypto_alg *alg)
32 {
33 crypto_check_module_sig(alg->cra_module);
34
35 if (!alg->cra_name[0] || !alg->cra_driver_name[0])
36 return -EINVAL;
37
38 if (alg->cra_alignmask & (alg->cra_alignmask + 1))
39 return -EINVAL;
40
41 /* General maximums for all algs. */
42 if (alg->cra_alignmask > MAX_ALGAPI_ALIGNMASK)
43 return -EINVAL;
44
45 if (alg->cra_blocksize > MAX_ALGAPI_BLOCKSIZE)
46 return -EINVAL;
47
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;
53
54 if (alg->cra_blocksize > MAX_CIPHER_BLOCKSIZE)
55 return -EINVAL;
56 }
57
58 if (alg->cra_priority < 0)
59 return -EINVAL;
60
61 refcount_set(&alg->cra_refcnt, 1);
62
63 return 0;
64 }
65
crypto_free_instance(struct crypto_instance * inst)66 static void crypto_free_instance(struct crypto_instance *inst)
67 {
68 inst->alg.cra_type->free(inst);
69 }
70
crypto_destroy_instance(struct crypto_alg * alg)71 static void crypto_destroy_instance(struct crypto_alg *alg)
72 {
73 struct crypto_instance *inst = (void *)alg;
74 struct crypto_template *tmpl = inst->tmpl;
75
76 crypto_free_instance(inst);
77 crypto_tmpl_put(tmpl);
78 }
79
80 /*
81 * This function adds a spawn to the list secondary_spawns which
82 * will be used at the end of crypto_remove_spawns to unregister
83 * instances, unless the spawn happens to be one that is depended
84 * on by the new algorithm (nalg in crypto_remove_spawns).
85 *
86 * This function is also responsible for resurrecting any algorithms
87 * in the dependency chain of nalg by unsetting n->dead.
88 */
crypto_more_spawns(struct crypto_alg * alg,struct list_head * stack,struct list_head * top,struct list_head * secondary_spawns)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 spawn = list_first_entry_or_null(stack, struct crypto_spawn, list);
97 if (!spawn)
98 return NULL;
99
100 n = list_prev_entry(spawn, list);
101 list_move(&spawn->list, secondary_spawns);
102
103 if (list_is_last(&n->list, stack))
104 return top;
105
106 n = list_next_entry(n, list);
107 if (!spawn->dead)
108 n->dead = false;
109
110 return &n->inst->alg.cra_users;
111 }
112
crypto_remove_instance(struct crypto_instance * inst,struct list_head * list)113 static void crypto_remove_instance(struct crypto_instance *inst,
114 struct list_head *list)
115 {
116 struct crypto_template *tmpl = inst->tmpl;
117
118 if (crypto_is_dead(&inst->alg))
119 return;
120
121 inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
122
123 if (!tmpl || !crypto_tmpl_get(tmpl))
124 return;
125
126 list_move(&inst->alg.cra_list, list);
127 hlist_del(&inst->list);
128 inst->alg.cra_destroy = crypto_destroy_instance;
129
130 BUG_ON(!list_empty(&inst->alg.cra_users));
131 }
132
133 /*
134 * Given an algorithm alg, remove all algorithms that depend on it
135 * through spawns. If nalg is not null, then exempt any algorithms
136 * that is depended on by nalg. This is useful when nalg itself
137 * depends on alg.
138 */
crypto_remove_spawns(struct crypto_alg * alg,struct list_head * list,struct crypto_alg * nalg)139 void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
140 struct crypto_alg *nalg)
141 {
142 u32 new_type = (nalg ?: alg)->cra_flags;
143 struct crypto_spawn *spawn, *n;
144 LIST_HEAD(secondary_spawns);
145 struct list_head *spawns;
146 LIST_HEAD(stack);
147 LIST_HEAD(top);
148
149 spawns = &alg->cra_users;
150 list_for_each_entry_safe(spawn, n, spawns, list) {
151 if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
152 continue;
153
154 list_move(&spawn->list, &top);
155 }
156
157 /*
158 * Perform a depth-first walk starting from alg through
159 * the cra_users tree. The list stack records the path
160 * from alg to the current spawn.
161 */
162 spawns = ⊤
163 do {
164 while (!list_empty(spawns)) {
165 struct crypto_instance *inst;
166
167 spawn = list_first_entry(spawns, struct crypto_spawn,
168 list);
169 inst = spawn->inst;
170
171 list_move(&spawn->list, &stack);
172 spawn->dead = !spawn->registered || &inst->alg != nalg;
173
174 if (!spawn->registered)
175 break;
176
177 BUG_ON(&inst->alg == alg);
178
179 if (&inst->alg == nalg)
180 break;
181
182 spawns = &inst->alg.cra_users;
183
184 /*
185 * Even if spawn->registered is true, the
186 * instance itself may still be unregistered.
187 * This is because it may have failed during
188 * registration. Therefore we still need to
189 * make the following test.
190 *
191 * We may encounter an unregistered instance here, since
192 * an instance's spawns are set up prior to the instance
193 * being registered. An unregistered instance will have
194 * NULL ->cra_users.next, since ->cra_users isn't
195 * properly initialized until registration. But an
196 * unregistered instance cannot have any users, so treat
197 * it the same as ->cra_users being empty.
198 */
199 if (spawns->next == NULL)
200 break;
201 }
202 } while ((spawns = crypto_more_spawns(alg, &stack, &top,
203 &secondary_spawns)));
204
205 /*
206 * Remove all instances that are marked as dead. Also
207 * complete the resurrection of the others by moving them
208 * back to the cra_users list.
209 */
210 list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
211 if (!spawn->dead)
212 list_move(&spawn->list, &spawn->alg->cra_users);
213 else if (spawn->registered)
214 crypto_remove_instance(spawn->inst, list);
215 }
216 }
217 EXPORT_SYMBOL_GPL(crypto_remove_spawns);
218
crypto_alg_finish_registration(struct crypto_alg * alg,bool fulfill_requests,struct list_head * algs_to_put)219 static void crypto_alg_finish_registration(struct crypto_alg *alg,
220 bool fulfill_requests,
221 struct list_head *algs_to_put)
222 {
223 struct crypto_alg *q;
224
225 list_for_each_entry(q, &crypto_alg_list, cra_list) {
226 if (q == alg)
227 continue;
228
229 if (crypto_is_moribund(q))
230 continue;
231
232 if (crypto_is_larval(q)) {
233 struct crypto_larval *larval = (void *)q;
234
235 /*
236 * Check to see if either our generic name or
237 * specific name can satisfy the name requested
238 * by the larval entry q.
239 */
240 if (strcmp(alg->cra_name, q->cra_name) &&
241 strcmp(alg->cra_driver_name, q->cra_name))
242 continue;
243
244 if (larval->adult)
245 continue;
246 if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
247 continue;
248
249 if (fulfill_requests && crypto_mod_get(alg))
250 larval->adult = alg;
251 else
252 larval->adult = ERR_PTR(-EAGAIN);
253
254 continue;
255 }
256
257 if (strcmp(alg->cra_name, q->cra_name))
258 continue;
259
260 if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
261 q->cra_priority > alg->cra_priority)
262 continue;
263
264 crypto_remove_spawns(q, algs_to_put, alg);
265 }
266
267 crypto_notify(CRYPTO_MSG_ALG_LOADED, alg);
268 }
269
crypto_alloc_test_larval(struct crypto_alg * alg)270 static struct crypto_larval *crypto_alloc_test_larval(struct crypto_alg *alg)
271 {
272 struct crypto_larval *larval;
273
274 if (!IS_ENABLED(CONFIG_CRYPTO_MANAGER) ||
275 IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS) ||
276 (alg->cra_flags & CRYPTO_ALG_INTERNAL))
277 return NULL; /* No self-test needed */
278
279 larval = crypto_larval_alloc(alg->cra_name,
280 alg->cra_flags | CRYPTO_ALG_TESTED, 0);
281 if (IS_ERR(larval))
282 return larval;
283
284 larval->adult = crypto_mod_get(alg);
285 if (!larval->adult) {
286 kfree(larval);
287 return ERR_PTR(-ENOENT);
288 }
289
290 refcount_set(&larval->alg.cra_refcnt, 1);
291 memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
292 CRYPTO_MAX_ALG_NAME);
293 larval->alg.cra_priority = alg->cra_priority;
294
295 return larval;
296 }
297
298 static struct crypto_larval *
__crypto_register_alg(struct crypto_alg * alg,struct list_head * algs_to_put)299 __crypto_register_alg(struct crypto_alg *alg, struct list_head *algs_to_put)
300 {
301 struct crypto_alg *q;
302 struct crypto_larval *larval;
303 int ret = -EAGAIN;
304
305 if (crypto_is_dead(alg))
306 goto err;
307
308 INIT_LIST_HEAD(&alg->cra_users);
309
310 ret = -EEXIST;
311
312 list_for_each_entry(q, &crypto_alg_list, cra_list) {
313 if (q == alg)
314 goto err;
315
316 if (crypto_is_moribund(q))
317 continue;
318
319 if (crypto_is_larval(q)) {
320 if (!strcmp(alg->cra_driver_name, q->cra_driver_name))
321 goto err;
322 continue;
323 }
324
325 if (!strcmp(q->cra_driver_name, alg->cra_name) ||
326 !strcmp(q->cra_driver_name, alg->cra_driver_name) ||
327 !strcmp(q->cra_name, alg->cra_driver_name))
328 goto err;
329 }
330
331 larval = crypto_alloc_test_larval(alg);
332 if (IS_ERR(larval))
333 goto out;
334
335 list_add(&alg->cra_list, &crypto_alg_list);
336
337 crypto_stats_init(alg);
338
339 if (larval) {
340 /* No cheating! */
341 alg->cra_flags &= ~CRYPTO_ALG_TESTED;
342
343 list_add(&larval->alg.cra_list, &crypto_alg_list);
344 } else {
345 alg->cra_flags |= CRYPTO_ALG_TESTED;
346 crypto_alg_finish_registration(alg, true, algs_to_put);
347 }
348
349 out:
350 return larval;
351
352 err:
353 larval = ERR_PTR(ret);
354 goto out;
355 }
356
crypto_alg_tested(const char * name,int err)357 void crypto_alg_tested(const char *name, int err)
358 {
359 struct crypto_larval *test;
360 struct crypto_alg *alg;
361 struct crypto_alg *q;
362 LIST_HEAD(list);
363 bool best;
364
365 down_write(&crypto_alg_sem);
366 list_for_each_entry(q, &crypto_alg_list, cra_list) {
367 if (crypto_is_moribund(q) || !crypto_is_larval(q))
368 continue;
369
370 test = (struct crypto_larval *)q;
371
372 if (!strcmp(q->cra_driver_name, name))
373 goto found;
374 }
375
376 pr_err("alg: Unexpected test result for %s: %d\n", name, err);
377 goto unlock;
378
379 found:
380 q->cra_flags |= CRYPTO_ALG_DEAD;
381 alg = test->adult;
382 if (err || list_empty(&alg->cra_list))
383 goto complete;
384
385 alg->cra_flags |= CRYPTO_ALG_TESTED;
386
387 /*
388 * If a higher-priority implementation of the same algorithm is
389 * currently being tested, then don't fulfill request larvals.
390 */
391 best = true;
392 list_for_each_entry(q, &crypto_alg_list, cra_list) {
393 if (crypto_is_moribund(q) || !crypto_is_larval(q))
394 continue;
395
396 if (strcmp(alg->cra_name, q->cra_name))
397 continue;
398
399 if (q->cra_priority > alg->cra_priority) {
400 best = false;
401 break;
402 }
403 }
404
405 crypto_alg_finish_registration(alg, best, &list);
406
407 complete:
408 complete_all(&test->completion);
409
410 unlock:
411 up_write(&crypto_alg_sem);
412
413 crypto_remove_final(&list);
414 }
415 EXPORT_SYMBOL_GPL(crypto_alg_tested);
416
crypto_remove_final(struct list_head * list)417 void crypto_remove_final(struct list_head *list)
418 {
419 struct crypto_alg *alg;
420 struct crypto_alg *n;
421
422 list_for_each_entry_safe(alg, n, list, cra_list) {
423 list_del_init(&alg->cra_list);
424 crypto_alg_put(alg);
425 }
426 }
427 EXPORT_SYMBOL_GPL(crypto_remove_final);
428
crypto_register_alg(struct crypto_alg * alg)429 int crypto_register_alg(struct crypto_alg *alg)
430 {
431 struct crypto_larval *larval;
432 LIST_HEAD(algs_to_put);
433 bool test_started = false;
434 int err;
435
436 alg->cra_flags &= ~CRYPTO_ALG_DEAD;
437 err = crypto_check_alg(alg);
438 if (err)
439 return err;
440
441 down_write(&crypto_alg_sem);
442 larval = __crypto_register_alg(alg, &algs_to_put);
443 if (!IS_ERR_OR_NULL(larval)) {
444 test_started = crypto_boot_test_finished();
445 larval->test_started = test_started;
446 }
447 up_write(&crypto_alg_sem);
448
449 if (IS_ERR(larval))
450 return PTR_ERR(larval);
451 if (test_started)
452 crypto_wait_for_test(larval);
453 crypto_remove_final(&algs_to_put);
454 return 0;
455 }
456 EXPORT_SYMBOL_GPL(crypto_register_alg);
457
crypto_remove_alg(struct crypto_alg * alg,struct list_head * list)458 static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
459 {
460 if (unlikely(list_empty(&alg->cra_list)))
461 return -ENOENT;
462
463 alg->cra_flags |= CRYPTO_ALG_DEAD;
464
465 list_del_init(&alg->cra_list);
466 crypto_remove_spawns(alg, list, NULL);
467
468 return 0;
469 }
470
crypto_unregister_alg(struct crypto_alg * alg)471 void crypto_unregister_alg(struct crypto_alg *alg)
472 {
473 int ret;
474 LIST_HEAD(list);
475
476 down_write(&crypto_alg_sem);
477 ret = crypto_remove_alg(alg, &list);
478 up_write(&crypto_alg_sem);
479
480 if (WARN(ret, "Algorithm %s is not registered", alg->cra_driver_name))
481 return;
482
483 if (WARN_ON(refcount_read(&alg->cra_refcnt) != 1))
484 return;
485
486 if (alg->cra_destroy)
487 alg->cra_destroy(alg);
488
489 crypto_remove_final(&list);
490 }
491 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
492
crypto_register_algs(struct crypto_alg * algs,int count)493 int crypto_register_algs(struct crypto_alg *algs, int count)
494 {
495 int i, ret;
496
497 for (i = 0; i < count; i++) {
498 ret = crypto_register_alg(&algs[i]);
499 if (ret)
500 goto err;
501 }
502
503 return 0;
504
505 err:
506 for (--i; i >= 0; --i)
507 crypto_unregister_alg(&algs[i]);
508
509 return ret;
510 }
511 EXPORT_SYMBOL_GPL(crypto_register_algs);
512
crypto_unregister_algs(struct crypto_alg * algs,int count)513 void crypto_unregister_algs(struct crypto_alg *algs, int count)
514 {
515 int i;
516
517 for (i = 0; i < count; i++)
518 crypto_unregister_alg(&algs[i]);
519 }
520 EXPORT_SYMBOL_GPL(crypto_unregister_algs);
521
crypto_register_template(struct crypto_template * tmpl)522 int crypto_register_template(struct crypto_template *tmpl)
523 {
524 struct crypto_template *q;
525 int err = -EEXIST;
526
527 down_write(&crypto_alg_sem);
528
529 crypto_check_module_sig(tmpl->module);
530
531 list_for_each_entry(q, &crypto_template_list, list) {
532 if (q == tmpl)
533 goto out;
534 }
535
536 list_add(&tmpl->list, &crypto_template_list);
537 err = 0;
538 out:
539 up_write(&crypto_alg_sem);
540 return err;
541 }
542 EXPORT_SYMBOL_GPL(crypto_register_template);
543
crypto_register_templates(struct crypto_template * tmpls,int count)544 int crypto_register_templates(struct crypto_template *tmpls, int count)
545 {
546 int i, err;
547
548 for (i = 0; i < count; i++) {
549 err = crypto_register_template(&tmpls[i]);
550 if (err)
551 goto out;
552 }
553 return 0;
554
555 out:
556 for (--i; i >= 0; --i)
557 crypto_unregister_template(&tmpls[i]);
558 return err;
559 }
560 EXPORT_SYMBOL_GPL(crypto_register_templates);
561
crypto_unregister_template(struct crypto_template * tmpl)562 void crypto_unregister_template(struct crypto_template *tmpl)
563 {
564 struct crypto_instance *inst;
565 struct hlist_node *n;
566 struct hlist_head *list;
567 LIST_HEAD(users);
568
569 down_write(&crypto_alg_sem);
570
571 BUG_ON(list_empty(&tmpl->list));
572 list_del_init(&tmpl->list);
573
574 list = &tmpl->instances;
575 hlist_for_each_entry(inst, list, list) {
576 int err = crypto_remove_alg(&inst->alg, &users);
577
578 BUG_ON(err);
579 }
580
581 up_write(&crypto_alg_sem);
582
583 hlist_for_each_entry_safe(inst, n, list, list) {
584 BUG_ON(refcount_read(&inst->alg.cra_refcnt) != 1);
585 crypto_free_instance(inst);
586 }
587 crypto_remove_final(&users);
588 }
589 EXPORT_SYMBOL_GPL(crypto_unregister_template);
590
crypto_unregister_templates(struct crypto_template * tmpls,int count)591 void crypto_unregister_templates(struct crypto_template *tmpls, int count)
592 {
593 int i;
594
595 for (i = count - 1; i >= 0; --i)
596 crypto_unregister_template(&tmpls[i]);
597 }
598 EXPORT_SYMBOL_GPL(crypto_unregister_templates);
599
__crypto_lookup_template(const char * name)600 static struct crypto_template *__crypto_lookup_template(const char *name)
601 {
602 struct crypto_template *q, *tmpl = NULL;
603
604 down_read(&crypto_alg_sem);
605 list_for_each_entry(q, &crypto_template_list, list) {
606 if (strcmp(q->name, name))
607 continue;
608 if (unlikely(!crypto_tmpl_get(q)))
609 continue;
610
611 tmpl = q;
612 break;
613 }
614 up_read(&crypto_alg_sem);
615
616 return tmpl;
617 }
618
crypto_lookup_template(const char * name)619 struct crypto_template *crypto_lookup_template(const char *name)
620 {
621 return try_then_request_module(__crypto_lookup_template(name),
622 "crypto-%s", name);
623 }
624 EXPORT_SYMBOL_GPL(crypto_lookup_template);
625
crypto_register_instance(struct crypto_template * tmpl,struct crypto_instance * inst)626 int crypto_register_instance(struct crypto_template *tmpl,
627 struct crypto_instance *inst)
628 {
629 struct crypto_larval *larval;
630 struct crypto_spawn *spawn;
631 LIST_HEAD(algs_to_put);
632 int err;
633
634 err = crypto_check_alg(&inst->alg);
635 if (err)
636 return err;
637
638 inst->alg.cra_module = tmpl->module;
639 inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
640
641 down_write(&crypto_alg_sem);
642
643 larval = ERR_PTR(-EAGAIN);
644 for (spawn = inst->spawns; spawn;) {
645 struct crypto_spawn *next;
646
647 if (spawn->dead)
648 goto unlock;
649
650 next = spawn->next;
651 spawn->inst = inst;
652 spawn->registered = true;
653
654 crypto_mod_put(spawn->alg);
655
656 spawn = next;
657 }
658
659 larval = __crypto_register_alg(&inst->alg, &algs_to_put);
660 if (IS_ERR(larval))
661 goto unlock;
662 else if (larval)
663 larval->test_started = true;
664
665 hlist_add_head(&inst->list, &tmpl->instances);
666 inst->tmpl = tmpl;
667
668 unlock:
669 up_write(&crypto_alg_sem);
670
671 if (IS_ERR(larval))
672 return PTR_ERR(larval);
673 if (larval)
674 crypto_wait_for_test(larval);
675 crypto_remove_final(&algs_to_put);
676 return 0;
677 }
678 EXPORT_SYMBOL_GPL(crypto_register_instance);
679
crypto_unregister_instance(struct crypto_instance * inst)680 void crypto_unregister_instance(struct crypto_instance *inst)
681 {
682 LIST_HEAD(list);
683
684 down_write(&crypto_alg_sem);
685
686 crypto_remove_spawns(&inst->alg, &list, NULL);
687 crypto_remove_instance(inst, &list);
688
689 up_write(&crypto_alg_sem);
690
691 crypto_remove_final(&list);
692 }
693 EXPORT_SYMBOL_GPL(crypto_unregister_instance);
694
crypto_grab_spawn(struct crypto_spawn * spawn,struct crypto_instance * inst,const char * name,u32 type,u32 mask)695 int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst,
696 const char *name, u32 type, u32 mask)
697 {
698 struct crypto_alg *alg;
699 int err = -EAGAIN;
700
701 if (WARN_ON_ONCE(inst == NULL))
702 return -EINVAL;
703
704 /* Allow the result of crypto_attr_alg_name() to be passed directly */
705 if (IS_ERR(name))
706 return PTR_ERR(name);
707
708 alg = crypto_find_alg(name, spawn->frontend, type, mask);
709 if (IS_ERR(alg))
710 return PTR_ERR(alg);
711
712 down_write(&crypto_alg_sem);
713 if (!crypto_is_moribund(alg)) {
714 list_add(&spawn->list, &alg->cra_users);
715 spawn->alg = alg;
716 spawn->mask = mask;
717 spawn->next = inst->spawns;
718 inst->spawns = spawn;
719 inst->alg.cra_flags |=
720 (alg->cra_flags & CRYPTO_ALG_INHERITED_FLAGS);
721 err = 0;
722 }
723 up_write(&crypto_alg_sem);
724 if (err)
725 crypto_mod_put(alg);
726 return err;
727 }
728 EXPORT_SYMBOL_GPL(crypto_grab_spawn);
729
crypto_drop_spawn(struct crypto_spawn * spawn)730 void crypto_drop_spawn(struct crypto_spawn *spawn)
731 {
732 if (!spawn->alg) /* not yet initialized? */
733 return;
734
735 down_write(&crypto_alg_sem);
736 if (!spawn->dead)
737 list_del(&spawn->list);
738 up_write(&crypto_alg_sem);
739
740 if (!spawn->registered)
741 crypto_mod_put(spawn->alg);
742 }
743 EXPORT_SYMBOL_GPL(crypto_drop_spawn);
744
crypto_spawn_alg(struct crypto_spawn * spawn)745 static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
746 {
747 struct crypto_alg *alg = ERR_PTR(-EAGAIN);
748 struct crypto_alg *target;
749 bool shoot = false;
750
751 down_read(&crypto_alg_sem);
752 if (!spawn->dead) {
753 alg = spawn->alg;
754 if (!crypto_mod_get(alg)) {
755 target = crypto_alg_get(alg);
756 shoot = true;
757 alg = ERR_PTR(-EAGAIN);
758 }
759 }
760 up_read(&crypto_alg_sem);
761
762 if (shoot) {
763 crypto_shoot_alg(target);
764 crypto_alg_put(target);
765 }
766
767 return alg;
768 }
769
crypto_spawn_tfm(struct crypto_spawn * spawn,u32 type,u32 mask)770 struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
771 u32 mask)
772 {
773 struct crypto_alg *alg;
774 struct crypto_tfm *tfm;
775
776 alg = crypto_spawn_alg(spawn);
777 if (IS_ERR(alg))
778 return ERR_CAST(alg);
779
780 tfm = ERR_PTR(-EINVAL);
781 if (unlikely((alg->cra_flags ^ type) & mask))
782 goto out_put_alg;
783
784 tfm = __crypto_alloc_tfm(alg, type, mask);
785 if (IS_ERR(tfm))
786 goto out_put_alg;
787
788 return tfm;
789
790 out_put_alg:
791 crypto_mod_put(alg);
792 return tfm;
793 }
794 EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
795
crypto_spawn_tfm2(struct crypto_spawn * spawn)796 void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
797 {
798 struct crypto_alg *alg;
799 struct crypto_tfm *tfm;
800
801 alg = crypto_spawn_alg(spawn);
802 if (IS_ERR(alg))
803 return ERR_CAST(alg);
804
805 tfm = crypto_create_tfm(alg, spawn->frontend);
806 if (IS_ERR(tfm))
807 goto out_put_alg;
808
809 return tfm;
810
811 out_put_alg:
812 crypto_mod_put(alg);
813 return tfm;
814 }
815 EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
816
crypto_register_notifier(struct notifier_block * nb)817 int crypto_register_notifier(struct notifier_block *nb)
818 {
819 return blocking_notifier_chain_register(&crypto_chain, nb);
820 }
821 EXPORT_SYMBOL_GPL(crypto_register_notifier);
822
crypto_unregister_notifier(struct notifier_block * nb)823 int crypto_unregister_notifier(struct notifier_block *nb)
824 {
825 return blocking_notifier_chain_unregister(&crypto_chain, nb);
826 }
827 EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
828
crypto_get_attr_type(struct rtattr ** tb)829 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
830 {
831 struct rtattr *rta = tb[0];
832 struct crypto_attr_type *algt;
833
834 if (!rta)
835 return ERR_PTR(-ENOENT);
836 if (RTA_PAYLOAD(rta) < sizeof(*algt))
837 return ERR_PTR(-EINVAL);
838 if (rta->rta_type != CRYPTOA_TYPE)
839 return ERR_PTR(-EINVAL);
840
841 algt = RTA_DATA(rta);
842
843 return algt;
844 }
845 EXPORT_SYMBOL_GPL(crypto_get_attr_type);
846
847 /**
848 * crypto_check_attr_type() - check algorithm type and compute inherited mask
849 * @tb: the template parameters
850 * @type: the algorithm type the template would be instantiated as
851 * @mask_ret: (output) the mask that should be passed to crypto_grab_*()
852 * to restrict the flags of any inner algorithms
853 *
854 * Validate that the algorithm type the user requested is compatible with the
855 * one the template would actually be instantiated as. E.g., if the user is
856 * doing crypto_alloc_shash("cbc(aes)", ...), this would return an error because
857 * the "cbc" template creates an "skcipher" algorithm, not an "shash" algorithm.
858 *
859 * Also compute the mask to use to restrict the flags of any inner algorithms.
860 *
861 * Return: 0 on success; -errno on failure
862 */
crypto_check_attr_type(struct rtattr ** tb,u32 type,u32 * mask_ret)863 int crypto_check_attr_type(struct rtattr **tb, u32 type, u32 *mask_ret)
864 {
865 struct crypto_attr_type *algt;
866
867 algt = crypto_get_attr_type(tb);
868 if (IS_ERR(algt))
869 return PTR_ERR(algt);
870
871 if ((algt->type ^ type) & algt->mask)
872 return -EINVAL;
873
874 *mask_ret = crypto_algt_inherited_mask(algt);
875 return 0;
876 }
877 EXPORT_SYMBOL_GPL(crypto_check_attr_type);
878
crypto_attr_alg_name(struct rtattr * rta)879 const char *crypto_attr_alg_name(struct rtattr *rta)
880 {
881 struct crypto_attr_alg *alga;
882
883 if (!rta)
884 return ERR_PTR(-ENOENT);
885 if (RTA_PAYLOAD(rta) < sizeof(*alga))
886 return ERR_PTR(-EINVAL);
887 if (rta->rta_type != CRYPTOA_ALG)
888 return ERR_PTR(-EINVAL);
889
890 alga = RTA_DATA(rta);
891 alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
892
893 return alga->name;
894 }
895 EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
896
crypto_inst_setname(struct crypto_instance * inst,const char * name,struct crypto_alg * alg)897 int crypto_inst_setname(struct crypto_instance *inst, const char *name,
898 struct crypto_alg *alg)
899 {
900 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
901 alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
902 return -ENAMETOOLONG;
903
904 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
905 name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
906 return -ENAMETOOLONG;
907
908 return 0;
909 }
910 EXPORT_SYMBOL_GPL(crypto_inst_setname);
911
crypto_init_queue(struct crypto_queue * queue,unsigned int max_qlen)912 void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
913 {
914 INIT_LIST_HEAD(&queue->list);
915 queue->backlog = &queue->list;
916 queue->qlen = 0;
917 queue->max_qlen = max_qlen;
918 }
919 EXPORT_SYMBOL_GPL(crypto_init_queue);
920
crypto_enqueue_request(struct crypto_queue * queue,struct crypto_async_request * request)921 int crypto_enqueue_request(struct crypto_queue *queue,
922 struct crypto_async_request *request)
923 {
924 int err = -EINPROGRESS;
925
926 if (unlikely(queue->qlen >= queue->max_qlen)) {
927 if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
928 err = -ENOSPC;
929 goto out;
930 }
931 err = -EBUSY;
932 if (queue->backlog == &queue->list)
933 queue->backlog = &request->list;
934 }
935
936 queue->qlen++;
937 list_add_tail(&request->list, &queue->list);
938
939 out:
940 return err;
941 }
942 EXPORT_SYMBOL_GPL(crypto_enqueue_request);
943
crypto_enqueue_request_head(struct crypto_queue * queue,struct crypto_async_request * request)944 void crypto_enqueue_request_head(struct crypto_queue *queue,
945 struct crypto_async_request *request)
946 {
947 if (unlikely(queue->qlen >= queue->max_qlen))
948 queue->backlog = queue->backlog->prev;
949
950 queue->qlen++;
951 list_add(&request->list, &queue->list);
952 }
953 EXPORT_SYMBOL_GPL(crypto_enqueue_request_head);
954
crypto_dequeue_request(struct crypto_queue * queue)955 struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
956 {
957 struct list_head *request;
958
959 if (unlikely(!queue->qlen))
960 return NULL;
961
962 queue->qlen--;
963
964 if (queue->backlog != &queue->list)
965 queue->backlog = queue->backlog->next;
966
967 request = queue->list.next;
968 list_del(request);
969
970 return list_entry(request, struct crypto_async_request, list);
971 }
972 EXPORT_SYMBOL_GPL(crypto_dequeue_request);
973
crypto_inc_byte(u8 * a,unsigned int size)974 static inline void crypto_inc_byte(u8 *a, unsigned int size)
975 {
976 u8 *b = (a + size);
977 u8 c;
978
979 for (; size; size--) {
980 c = *--b + 1;
981 *b = c;
982 if (c)
983 break;
984 }
985 }
986
crypto_inc(u8 * a,unsigned int size)987 void crypto_inc(u8 *a, unsigned int size)
988 {
989 __be32 *b = (__be32 *)(a + size);
990 u32 c;
991
992 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
993 IS_ALIGNED((unsigned long)b, __alignof__(*b)))
994 for (; size >= 4; size -= 4) {
995 c = be32_to_cpu(*--b) + 1;
996 *b = cpu_to_be32(c);
997 if (likely(c))
998 return;
999 }
1000
1001 crypto_inc_byte(a, size);
1002 }
1003 EXPORT_SYMBOL_GPL(crypto_inc);
1004
__crypto_xor(u8 * dst,const u8 * src1,const u8 * src2,unsigned int len)1005 void __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int len)
1006 {
1007 int relalign = 0;
1008
1009 if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {
1010 int size = sizeof(unsigned long);
1011 int d = (((unsigned long)dst ^ (unsigned long)src1) |
1012 ((unsigned long)dst ^ (unsigned long)src2)) &
1013 (size - 1);
1014
1015 relalign = d ? 1 << __ffs(d) : size;
1016
1017 /*
1018 * If we care about alignment, process as many bytes as
1019 * needed to advance dst and src to values whose alignments
1020 * equal their relative alignment. This will allow us to
1021 * process the remainder of the input using optimal strides.
1022 */
1023 while (((unsigned long)dst & (relalign - 1)) && len > 0) {
1024 *dst++ = *src1++ ^ *src2++;
1025 len--;
1026 }
1027 }
1028
1029 while (IS_ENABLED(CONFIG_64BIT) && len >= 8 && !(relalign & 7)) {
1030 *(u64 *)dst = *(u64 *)src1 ^ *(u64 *)src2;
1031 dst += 8;
1032 src1 += 8;
1033 src2 += 8;
1034 len -= 8;
1035 }
1036
1037 while (len >= 4 && !(relalign & 3)) {
1038 *(u32 *)dst = *(u32 *)src1 ^ *(u32 *)src2;
1039 dst += 4;
1040 src1 += 4;
1041 src2 += 4;
1042 len -= 4;
1043 }
1044
1045 while (len >= 2 && !(relalign & 1)) {
1046 *(u16 *)dst = *(u16 *)src1 ^ *(u16 *)src2;
1047 dst += 2;
1048 src1 += 2;
1049 src2 += 2;
1050 len -= 2;
1051 }
1052
1053 while (len--)
1054 *dst++ = *src1++ ^ *src2++;
1055 }
1056 EXPORT_SYMBOL_GPL(__crypto_xor);
1057
crypto_alg_extsize(struct crypto_alg * alg)1058 unsigned int crypto_alg_extsize(struct crypto_alg *alg)
1059 {
1060 return alg->cra_ctxsize +
1061 (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
1062 }
1063 EXPORT_SYMBOL_GPL(crypto_alg_extsize);
1064
crypto_type_has_alg(const char * name,const struct crypto_type * frontend,u32 type,u32 mask)1065 int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
1066 u32 type, u32 mask)
1067 {
1068 int ret = 0;
1069 struct crypto_alg *alg = crypto_find_alg(name, frontend, type, mask);
1070
1071 if (!IS_ERR(alg)) {
1072 crypto_mod_put(alg);
1073 ret = 1;
1074 }
1075
1076 return ret;
1077 }
1078 EXPORT_SYMBOL_GPL(crypto_type_has_alg);
1079
1080 #ifdef CONFIG_CRYPTO_STATS
crypto_stats_init(struct crypto_alg * alg)1081 void crypto_stats_init(struct crypto_alg *alg)
1082 {
1083 memset(&alg->stats, 0, sizeof(alg->stats));
1084 }
1085 EXPORT_SYMBOL_GPL(crypto_stats_init);
1086
crypto_stats_get(struct crypto_alg * alg)1087 void crypto_stats_get(struct crypto_alg *alg)
1088 {
1089 crypto_alg_get(alg);
1090 }
1091 EXPORT_SYMBOL_GPL(crypto_stats_get);
1092
crypto_stats_aead_encrypt(unsigned int cryptlen,struct crypto_alg * alg,int ret)1093 void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg,
1094 int ret)
1095 {
1096 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1097 atomic64_inc(&alg->stats.aead.err_cnt);
1098 } else {
1099 atomic64_inc(&alg->stats.aead.encrypt_cnt);
1100 atomic64_add(cryptlen, &alg->stats.aead.encrypt_tlen);
1101 }
1102 crypto_alg_put(alg);
1103 }
1104 EXPORT_SYMBOL_GPL(crypto_stats_aead_encrypt);
1105
crypto_stats_aead_decrypt(unsigned int cryptlen,struct crypto_alg * alg,int ret)1106 void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg,
1107 int ret)
1108 {
1109 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1110 atomic64_inc(&alg->stats.aead.err_cnt);
1111 } else {
1112 atomic64_inc(&alg->stats.aead.decrypt_cnt);
1113 atomic64_add(cryptlen, &alg->stats.aead.decrypt_tlen);
1114 }
1115 crypto_alg_put(alg);
1116 }
1117 EXPORT_SYMBOL_GPL(crypto_stats_aead_decrypt);
1118
crypto_stats_akcipher_encrypt(unsigned int src_len,int ret,struct crypto_alg * alg)1119 void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret,
1120 struct crypto_alg *alg)
1121 {
1122 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1123 atomic64_inc(&alg->stats.akcipher.err_cnt);
1124 } else {
1125 atomic64_inc(&alg->stats.akcipher.encrypt_cnt);
1126 atomic64_add(src_len, &alg->stats.akcipher.encrypt_tlen);
1127 }
1128 crypto_alg_put(alg);
1129 }
1130 EXPORT_SYMBOL_GPL(crypto_stats_akcipher_encrypt);
1131
crypto_stats_akcipher_decrypt(unsigned int src_len,int ret,struct crypto_alg * alg)1132 void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret,
1133 struct crypto_alg *alg)
1134 {
1135 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1136 atomic64_inc(&alg->stats.akcipher.err_cnt);
1137 } else {
1138 atomic64_inc(&alg->stats.akcipher.decrypt_cnt);
1139 atomic64_add(src_len, &alg->stats.akcipher.decrypt_tlen);
1140 }
1141 crypto_alg_put(alg);
1142 }
1143 EXPORT_SYMBOL_GPL(crypto_stats_akcipher_decrypt);
1144
crypto_stats_akcipher_sign(int ret,struct crypto_alg * alg)1145 void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg)
1146 {
1147 if (ret && ret != -EINPROGRESS && ret != -EBUSY)
1148 atomic64_inc(&alg->stats.akcipher.err_cnt);
1149 else
1150 atomic64_inc(&alg->stats.akcipher.sign_cnt);
1151 crypto_alg_put(alg);
1152 }
1153 EXPORT_SYMBOL_GPL(crypto_stats_akcipher_sign);
1154
crypto_stats_akcipher_verify(int ret,struct crypto_alg * alg)1155 void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg)
1156 {
1157 if (ret && ret != -EINPROGRESS && ret != -EBUSY)
1158 atomic64_inc(&alg->stats.akcipher.err_cnt);
1159 else
1160 atomic64_inc(&alg->stats.akcipher.verify_cnt);
1161 crypto_alg_put(alg);
1162 }
1163 EXPORT_SYMBOL_GPL(crypto_stats_akcipher_verify);
1164
crypto_stats_compress(unsigned int slen,int ret,struct crypto_alg * alg)1165 void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg)
1166 {
1167 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1168 atomic64_inc(&alg->stats.compress.err_cnt);
1169 } else {
1170 atomic64_inc(&alg->stats.compress.compress_cnt);
1171 atomic64_add(slen, &alg->stats.compress.compress_tlen);
1172 }
1173 crypto_alg_put(alg);
1174 }
1175 EXPORT_SYMBOL_GPL(crypto_stats_compress);
1176
crypto_stats_decompress(unsigned int slen,int ret,struct crypto_alg * alg)1177 void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg)
1178 {
1179 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1180 atomic64_inc(&alg->stats.compress.err_cnt);
1181 } else {
1182 atomic64_inc(&alg->stats.compress.decompress_cnt);
1183 atomic64_add(slen, &alg->stats.compress.decompress_tlen);
1184 }
1185 crypto_alg_put(alg);
1186 }
1187 EXPORT_SYMBOL_GPL(crypto_stats_decompress);
1188
crypto_stats_ahash_update(unsigned int nbytes,int ret,struct crypto_alg * alg)1189 void crypto_stats_ahash_update(unsigned int nbytes, int ret,
1190 struct crypto_alg *alg)
1191 {
1192 if (ret && ret != -EINPROGRESS && ret != -EBUSY)
1193 atomic64_inc(&alg->stats.hash.err_cnt);
1194 else
1195 atomic64_add(nbytes, &alg->stats.hash.hash_tlen);
1196 crypto_alg_put(alg);
1197 }
1198 EXPORT_SYMBOL_GPL(crypto_stats_ahash_update);
1199
crypto_stats_ahash_final(unsigned int nbytes,int ret,struct crypto_alg * alg)1200 void crypto_stats_ahash_final(unsigned int nbytes, int ret,
1201 struct crypto_alg *alg)
1202 {
1203 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1204 atomic64_inc(&alg->stats.hash.err_cnt);
1205 } else {
1206 atomic64_inc(&alg->stats.hash.hash_cnt);
1207 atomic64_add(nbytes, &alg->stats.hash.hash_tlen);
1208 }
1209 crypto_alg_put(alg);
1210 }
1211 EXPORT_SYMBOL_GPL(crypto_stats_ahash_final);
1212
crypto_stats_kpp_set_secret(struct crypto_alg * alg,int ret)1213 void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret)
1214 {
1215 if (ret)
1216 atomic64_inc(&alg->stats.kpp.err_cnt);
1217 else
1218 atomic64_inc(&alg->stats.kpp.setsecret_cnt);
1219 crypto_alg_put(alg);
1220 }
1221 EXPORT_SYMBOL_GPL(crypto_stats_kpp_set_secret);
1222
crypto_stats_kpp_generate_public_key(struct crypto_alg * alg,int ret)1223 void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret)
1224 {
1225 if (ret)
1226 atomic64_inc(&alg->stats.kpp.err_cnt);
1227 else
1228 atomic64_inc(&alg->stats.kpp.generate_public_key_cnt);
1229 crypto_alg_put(alg);
1230 }
1231 EXPORT_SYMBOL_GPL(crypto_stats_kpp_generate_public_key);
1232
crypto_stats_kpp_compute_shared_secret(struct crypto_alg * alg,int ret)1233 void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret)
1234 {
1235 if (ret)
1236 atomic64_inc(&alg->stats.kpp.err_cnt);
1237 else
1238 atomic64_inc(&alg->stats.kpp.compute_shared_secret_cnt);
1239 crypto_alg_put(alg);
1240 }
1241 EXPORT_SYMBOL_GPL(crypto_stats_kpp_compute_shared_secret);
1242
crypto_stats_rng_seed(struct crypto_alg * alg,int ret)1243 void crypto_stats_rng_seed(struct crypto_alg *alg, int ret)
1244 {
1245 if (ret && ret != -EINPROGRESS && ret != -EBUSY)
1246 atomic64_inc(&alg->stats.rng.err_cnt);
1247 else
1248 atomic64_inc(&alg->stats.rng.seed_cnt);
1249 crypto_alg_put(alg);
1250 }
1251 EXPORT_SYMBOL_GPL(crypto_stats_rng_seed);
1252
crypto_stats_rng_generate(struct crypto_alg * alg,unsigned int dlen,int ret)1253 void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen,
1254 int ret)
1255 {
1256 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1257 atomic64_inc(&alg->stats.rng.err_cnt);
1258 } else {
1259 atomic64_inc(&alg->stats.rng.generate_cnt);
1260 atomic64_add(dlen, &alg->stats.rng.generate_tlen);
1261 }
1262 crypto_alg_put(alg);
1263 }
1264 EXPORT_SYMBOL_GPL(crypto_stats_rng_generate);
1265
crypto_stats_skcipher_encrypt(unsigned int cryptlen,int ret,struct crypto_alg * alg)1266 void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret,
1267 struct crypto_alg *alg)
1268 {
1269 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1270 atomic64_inc(&alg->stats.cipher.err_cnt);
1271 } else {
1272 atomic64_inc(&alg->stats.cipher.encrypt_cnt);
1273 atomic64_add(cryptlen, &alg->stats.cipher.encrypt_tlen);
1274 }
1275 crypto_alg_put(alg);
1276 }
1277 EXPORT_SYMBOL_GPL(crypto_stats_skcipher_encrypt);
1278
crypto_stats_skcipher_decrypt(unsigned int cryptlen,int ret,struct crypto_alg * alg)1279 void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret,
1280 struct crypto_alg *alg)
1281 {
1282 if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
1283 atomic64_inc(&alg->stats.cipher.err_cnt);
1284 } else {
1285 atomic64_inc(&alg->stats.cipher.decrypt_cnt);
1286 atomic64_add(cryptlen, &alg->stats.cipher.decrypt_tlen);
1287 }
1288 crypto_alg_put(alg);
1289 }
1290 EXPORT_SYMBOL_GPL(crypto_stats_skcipher_decrypt);
1291 #endif
1292
crypto_start_tests(void)1293 static void __init crypto_start_tests(void)
1294 {
1295 if (IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS))
1296 return;
1297
1298 for (;;) {
1299 struct crypto_larval *larval = NULL;
1300 struct crypto_alg *q;
1301
1302 down_write(&crypto_alg_sem);
1303
1304 list_for_each_entry(q, &crypto_alg_list, cra_list) {
1305 struct crypto_larval *l;
1306
1307 if (!crypto_is_larval(q))
1308 continue;
1309
1310 l = (void *)q;
1311
1312 if (!crypto_is_test_larval(l))
1313 continue;
1314
1315 if (l->test_started)
1316 continue;
1317
1318 l->test_started = true;
1319 larval = l;
1320 break;
1321 }
1322
1323 up_write(&crypto_alg_sem);
1324
1325 if (!larval)
1326 break;
1327
1328 crypto_wait_for_test(larval);
1329 }
1330
1331 set_crypto_boot_test_finished();
1332 }
1333
crypto_algapi_init(void)1334 static int __init crypto_algapi_init(void)
1335 {
1336 crypto_init_proc();
1337 crypto_start_tests();
1338 return 0;
1339 }
1340
crypto_algapi_exit(void)1341 static void __exit crypto_algapi_exit(void)
1342 {
1343 crypto_exit_proc();
1344 }
1345
1346 /*
1347 * We run this at late_initcall so that all the built-in algorithms
1348 * have had a chance to register themselves first.
1349 */
1350 late_initcall(crypto_algapi_init);
1351 module_exit(crypto_algapi_exit);
1352
1353 MODULE_LICENSE("GPL");
1354 MODULE_DESCRIPTION("Cryptographic algorithms API");
1355 MODULE_SOFTDEP("pre: cryptomgr");
1356