xref: /linux/crypto/algapi.c (revision ae7d45fb7ca75e94b478e2404709ba3024774334)
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 <crypto/internal/simd.h>
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/fips.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/list.h>
16 #include <linux/module.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 
21 #include "internal.h"
22 
23 static LIST_HEAD(crypto_template_list);
24 
25 #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
26 DEFINE_PER_CPU(bool, crypto_simd_disabled_for_test);
27 EXPORT_PER_CPU_SYMBOL_GPL(crypto_simd_disabled_for_test);
28 #endif
29 
30 static inline void crypto_check_module_sig(struct module *mod)
31 {
32 	if (fips_enabled && mod && !module_sig_ok(mod))
33 		panic("Module %s signature verification failed in FIPS mode\n",
34 		      module_name(mod));
35 }
36 
37 static int crypto_check_alg(struct crypto_alg *alg)
38 {
39 	crypto_check_module_sig(alg->cra_module);
40 
41 	if (!alg->cra_name[0] || !alg->cra_driver_name[0])
42 		return -EINVAL;
43 
44 	if (alg->cra_alignmask & (alg->cra_alignmask + 1))
45 		return -EINVAL;
46 
47 	/* General maximums for all algs. */
48 	if (alg->cra_alignmask > MAX_ALGAPI_ALIGNMASK)
49 		return -EINVAL;
50 
51 	if (alg->cra_blocksize > MAX_ALGAPI_BLOCKSIZE)
52 		return -EINVAL;
53 
54 	/* Lower maximums for specific alg types. */
55 	if (!alg->cra_type && (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
56 			       CRYPTO_ALG_TYPE_CIPHER) {
57 		if (alg->cra_alignmask > MAX_CIPHER_ALIGNMASK)
58 			return -EINVAL;
59 
60 		if (alg->cra_blocksize > MAX_CIPHER_BLOCKSIZE)
61 			return -EINVAL;
62 	}
63 
64 	if (alg->cra_priority < 0)
65 		return -EINVAL;
66 
67 	refcount_set(&alg->cra_refcnt, 1);
68 
69 	return 0;
70 }
71 
72 static void crypto_free_instance(struct crypto_instance *inst)
73 {
74 	inst->alg.cra_type->free(inst);
75 }
76 
77 static void crypto_destroy_instance(struct crypto_alg *alg)
78 {
79 	struct crypto_instance *inst = (void *)alg;
80 	struct crypto_template *tmpl = inst->tmpl;
81 
82 	crypto_free_instance(inst);
83 	crypto_tmpl_put(tmpl);
84 }
85 
86 /*
87  * This function adds a spawn to the list secondary_spawns which
88  * will be used at the end of crypto_remove_spawns to unregister
89  * instances, unless the spawn happens to be one that is depended
90  * on by the new algorithm (nalg in crypto_remove_spawns).
91  *
92  * This function is also responsible for resurrecting any algorithms
93  * in the dependency chain of nalg by unsetting n->dead.
94  */
95 static struct list_head *crypto_more_spawns(struct crypto_alg *alg,
96 					    struct list_head *stack,
97 					    struct list_head *top,
98 					    struct list_head *secondary_spawns)
99 {
100 	struct crypto_spawn *spawn, *n;
101 
102 	spawn = list_first_entry_or_null(stack, struct crypto_spawn, list);
103 	if (!spawn)
104 		return NULL;
105 
106 	n = list_prev_entry(spawn, list);
107 	list_move(&spawn->list, secondary_spawns);
108 
109 	if (list_is_last(&n->list, stack))
110 		return top;
111 
112 	n = list_next_entry(n, list);
113 	if (!spawn->dead)
114 		n->dead = false;
115 
116 	return &n->inst->alg.cra_users;
117 }
118 
119 static void crypto_remove_instance(struct crypto_instance *inst,
120 				   struct list_head *list)
121 {
122 	struct crypto_template *tmpl = inst->tmpl;
123 
124 	if (crypto_is_dead(&inst->alg))
125 		return;
126 
127 	inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
128 
129 	if (!tmpl || !crypto_tmpl_get(tmpl))
130 		return;
131 
132 	list_move(&inst->alg.cra_list, list);
133 	hlist_del(&inst->list);
134 	inst->alg.cra_destroy = crypto_destroy_instance;
135 
136 	BUG_ON(!list_empty(&inst->alg.cra_users));
137 }
138 
139 /*
140  * Given an algorithm alg, remove all algorithms that depend on it
141  * through spawns.  If nalg is not null, then exempt any algorithms
142  * that is depended on by nalg.  This is useful when nalg itself
143  * depends on alg.
144  */
145 void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
146 			  struct crypto_alg *nalg)
147 {
148 	u32 new_type = (nalg ?: alg)->cra_flags;
149 	struct crypto_spawn *spawn, *n;
150 	LIST_HEAD(secondary_spawns);
151 	struct list_head *spawns;
152 	LIST_HEAD(stack);
153 	LIST_HEAD(top);
154 
155 	spawns = &alg->cra_users;
156 	list_for_each_entry_safe(spawn, n, spawns, list) {
157 		if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
158 			continue;
159 
160 		list_move(&spawn->list, &top);
161 	}
162 
163 	/*
164 	 * Perform a depth-first walk starting from alg through
165 	 * the cra_users tree.  The list stack records the path
166 	 * from alg to the current spawn.
167 	 */
168 	spawns = &top;
169 	do {
170 		while (!list_empty(spawns)) {
171 			struct crypto_instance *inst;
172 
173 			spawn = list_first_entry(spawns, struct crypto_spawn,
174 						 list);
175 			inst = spawn->inst;
176 
177 			list_move(&spawn->list, &stack);
178 			spawn->dead = !spawn->registered || &inst->alg != nalg;
179 
180 			if (!spawn->registered)
181 				break;
182 
183 			BUG_ON(&inst->alg == alg);
184 
185 			if (&inst->alg == nalg)
186 				break;
187 
188 			spawns = &inst->alg.cra_users;
189 
190 			/*
191 			 * Even if spawn->registered is true, the
192 			 * instance itself may still be unregistered.
193 			 * This is because it may have failed during
194 			 * registration.  Therefore we still need to
195 			 * make the following test.
196 			 *
197 			 * We may encounter an unregistered instance here, since
198 			 * an instance's spawns are set up prior to the instance
199 			 * being registered.  An unregistered instance will have
200 			 * NULL ->cra_users.next, since ->cra_users isn't
201 			 * properly initialized until registration.  But an
202 			 * unregistered instance cannot have any users, so treat
203 			 * it the same as ->cra_users being empty.
204 			 */
205 			if (spawns->next == NULL)
206 				break;
207 		}
208 	} while ((spawns = crypto_more_spawns(alg, &stack, &top,
209 					      &secondary_spawns)));
210 
211 	/*
212 	 * Remove all instances that are marked as dead.  Also
213 	 * complete the resurrection of the others by moving them
214 	 * back to the cra_users list.
215 	 */
216 	list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
217 		if (!spawn->dead)
218 			list_move(&spawn->list, &spawn->alg->cra_users);
219 		else if (spawn->registered)
220 			crypto_remove_instance(spawn->inst, list);
221 	}
222 }
223 EXPORT_SYMBOL_GPL(crypto_remove_spawns);
224 
225 static void crypto_alg_finish_registration(struct crypto_alg *alg,
226 					   bool fulfill_requests,
227 					   struct list_head *algs_to_put)
228 {
229 	struct crypto_alg *q;
230 
231 	list_for_each_entry(q, &crypto_alg_list, cra_list) {
232 		if (q == alg)
233 			continue;
234 
235 		if (crypto_is_moribund(q))
236 			continue;
237 
238 		if (crypto_is_larval(q)) {
239 			struct crypto_larval *larval = (void *)q;
240 
241 			/*
242 			 * Check to see if either our generic name or
243 			 * specific name can satisfy the name requested
244 			 * by the larval entry q.
245 			 */
246 			if (strcmp(alg->cra_name, q->cra_name) &&
247 			    strcmp(alg->cra_driver_name, q->cra_name))
248 				continue;
249 
250 			if (larval->adult)
251 				continue;
252 			if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
253 				continue;
254 
255 			if (fulfill_requests && crypto_mod_get(alg))
256 				larval->adult = alg;
257 			else
258 				larval->adult = ERR_PTR(-EAGAIN);
259 
260 			continue;
261 		}
262 
263 		if (strcmp(alg->cra_name, q->cra_name))
264 			continue;
265 
266 		if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
267 		    q->cra_priority > alg->cra_priority)
268 			continue;
269 
270 		crypto_remove_spawns(q, algs_to_put, alg);
271 	}
272 
273 	crypto_notify(CRYPTO_MSG_ALG_LOADED, alg);
274 }
275 
276 static struct crypto_larval *crypto_alloc_test_larval(struct crypto_alg *alg)
277 {
278 	struct crypto_larval *larval;
279 
280 	if (!IS_ENABLED(CONFIG_CRYPTO_MANAGER) ||
281 	    IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS) ||
282 	    (alg->cra_flags & CRYPTO_ALG_INTERNAL))
283 		return NULL; /* No self-test needed */
284 
285 	larval = crypto_larval_alloc(alg->cra_name,
286 				     alg->cra_flags | CRYPTO_ALG_TESTED, 0);
287 	if (IS_ERR(larval))
288 		return larval;
289 
290 	larval->adult = crypto_mod_get(alg);
291 	if (!larval->adult) {
292 		kfree(larval);
293 		return ERR_PTR(-ENOENT);
294 	}
295 
296 	refcount_set(&larval->alg.cra_refcnt, 1);
297 	memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
298 	       CRYPTO_MAX_ALG_NAME);
299 	larval->alg.cra_priority = alg->cra_priority;
300 
301 	return larval;
302 }
303 
304 static struct crypto_larval *
305 __crypto_register_alg(struct crypto_alg *alg, struct list_head *algs_to_put)
306 {
307 	struct crypto_alg *q;
308 	struct crypto_larval *larval;
309 	int ret = -EAGAIN;
310 
311 	if (crypto_is_dead(alg))
312 		goto err;
313 
314 	INIT_LIST_HEAD(&alg->cra_users);
315 
316 	ret = -EEXIST;
317 
318 	list_for_each_entry(q, &crypto_alg_list, cra_list) {
319 		if (q == alg)
320 			goto err;
321 
322 		if (crypto_is_moribund(q))
323 			continue;
324 
325 		if (crypto_is_larval(q)) {
326 			if (!strcmp(alg->cra_driver_name, q->cra_driver_name))
327 				goto err;
328 			continue;
329 		}
330 
331 		if (!strcmp(q->cra_driver_name, alg->cra_name) ||
332 		    !strcmp(q->cra_name, alg->cra_driver_name))
333 			goto err;
334 	}
335 
336 	larval = crypto_alloc_test_larval(alg);
337 	if (IS_ERR(larval))
338 		goto out;
339 
340 	list_add(&alg->cra_list, &crypto_alg_list);
341 
342 	if (larval) {
343 		/* No cheating! */
344 		alg->cra_flags &= ~CRYPTO_ALG_TESTED;
345 
346 		list_add(&larval->alg.cra_list, &crypto_alg_list);
347 	} else {
348 		alg->cra_flags |= CRYPTO_ALG_TESTED;
349 		crypto_alg_finish_registration(alg, true, algs_to_put);
350 	}
351 
352 out:
353 	return larval;
354 
355 err:
356 	larval = ERR_PTR(ret);
357 	goto out;
358 }
359 
360 void crypto_alg_tested(const char *name, int err)
361 {
362 	struct crypto_larval *test;
363 	struct crypto_alg *alg;
364 	struct crypto_alg *q;
365 	LIST_HEAD(list);
366 	bool best;
367 
368 	down_write(&crypto_alg_sem);
369 	list_for_each_entry(q, &crypto_alg_list, cra_list) {
370 		if (crypto_is_moribund(q) || !crypto_is_larval(q))
371 			continue;
372 
373 		test = (struct crypto_larval *)q;
374 
375 		if (!strcmp(q->cra_driver_name, name))
376 			goto found;
377 	}
378 
379 	pr_err("alg: Unexpected test result for %s: %d\n", name, err);
380 	goto unlock;
381 
382 found:
383 	q->cra_flags |= CRYPTO_ALG_DEAD;
384 	alg = test->adult;
385 
386 	if (list_empty(&alg->cra_list))
387 		goto complete;
388 
389 	if (err == -ECANCELED)
390 		alg->cra_flags |= CRYPTO_ALG_FIPS_INTERNAL;
391 	else if (err)
392 		goto complete;
393 	else
394 		alg->cra_flags &= ~CRYPTO_ALG_FIPS_INTERNAL;
395 
396 	alg->cra_flags |= CRYPTO_ALG_TESTED;
397 
398 	/*
399 	 * If a higher-priority implementation of the same algorithm is
400 	 * currently being tested, then don't fulfill request larvals.
401 	 */
402 	best = true;
403 	list_for_each_entry(q, &crypto_alg_list, cra_list) {
404 		if (crypto_is_moribund(q) || !crypto_is_larval(q))
405 			continue;
406 
407 		if (strcmp(alg->cra_name, q->cra_name))
408 			continue;
409 
410 		if (q->cra_priority > alg->cra_priority) {
411 			best = false;
412 			break;
413 		}
414 	}
415 
416 	crypto_alg_finish_registration(alg, best, &list);
417 
418 complete:
419 	complete_all(&test->completion);
420 
421 unlock:
422 	up_write(&crypto_alg_sem);
423 
424 	crypto_remove_final(&list);
425 }
426 EXPORT_SYMBOL_GPL(crypto_alg_tested);
427 
428 void crypto_remove_final(struct list_head *list)
429 {
430 	struct crypto_alg *alg;
431 	struct crypto_alg *n;
432 
433 	list_for_each_entry_safe(alg, n, list, cra_list) {
434 		list_del_init(&alg->cra_list);
435 		crypto_alg_put(alg);
436 	}
437 }
438 EXPORT_SYMBOL_GPL(crypto_remove_final);
439 
440 int crypto_register_alg(struct crypto_alg *alg)
441 {
442 	struct crypto_larval *larval;
443 	LIST_HEAD(algs_to_put);
444 	bool test_started = false;
445 	int err;
446 
447 	alg->cra_flags &= ~CRYPTO_ALG_DEAD;
448 	err = crypto_check_alg(alg);
449 	if (err)
450 		return err;
451 
452 	down_write(&crypto_alg_sem);
453 	larval = __crypto_register_alg(alg, &algs_to_put);
454 	if (!IS_ERR_OR_NULL(larval)) {
455 		test_started = crypto_boot_test_finished();
456 		larval->test_started = test_started;
457 	}
458 	up_write(&crypto_alg_sem);
459 
460 	if (IS_ERR(larval))
461 		return PTR_ERR(larval);
462 	if (test_started)
463 		crypto_wait_for_test(larval);
464 	crypto_remove_final(&algs_to_put);
465 	return 0;
466 }
467 EXPORT_SYMBOL_GPL(crypto_register_alg);
468 
469 static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
470 {
471 	if (unlikely(list_empty(&alg->cra_list)))
472 		return -ENOENT;
473 
474 	alg->cra_flags |= CRYPTO_ALG_DEAD;
475 
476 	list_del_init(&alg->cra_list);
477 	crypto_remove_spawns(alg, list, NULL);
478 
479 	return 0;
480 }
481 
482 void crypto_unregister_alg(struct crypto_alg *alg)
483 {
484 	int ret;
485 	LIST_HEAD(list);
486 
487 	down_write(&crypto_alg_sem);
488 	ret = crypto_remove_alg(alg, &list);
489 	up_write(&crypto_alg_sem);
490 
491 	if (WARN(ret, "Algorithm %s is not registered", alg->cra_driver_name))
492 		return;
493 
494 	BUG_ON(refcount_read(&alg->cra_refcnt) != 1);
495 	if (alg->cra_destroy)
496 		alg->cra_destroy(alg);
497 
498 	crypto_remove_final(&list);
499 }
500 EXPORT_SYMBOL_GPL(crypto_unregister_alg);
501 
502 int crypto_register_algs(struct crypto_alg *algs, int count)
503 {
504 	int i, ret;
505 
506 	for (i = 0; i < count; i++) {
507 		ret = crypto_register_alg(&algs[i]);
508 		if (ret)
509 			goto err;
510 	}
511 
512 	return 0;
513 
514 err:
515 	for (--i; i >= 0; --i)
516 		crypto_unregister_alg(&algs[i]);
517 
518 	return ret;
519 }
520 EXPORT_SYMBOL_GPL(crypto_register_algs);
521 
522 void crypto_unregister_algs(struct crypto_alg *algs, int count)
523 {
524 	int i;
525 
526 	for (i = 0; i < count; i++)
527 		crypto_unregister_alg(&algs[i]);
528 }
529 EXPORT_SYMBOL_GPL(crypto_unregister_algs);
530 
531 int crypto_register_template(struct crypto_template *tmpl)
532 {
533 	struct crypto_template *q;
534 	int err = -EEXIST;
535 
536 	down_write(&crypto_alg_sem);
537 
538 	crypto_check_module_sig(tmpl->module);
539 
540 	list_for_each_entry(q, &crypto_template_list, list) {
541 		if (q == tmpl)
542 			goto out;
543 	}
544 
545 	list_add(&tmpl->list, &crypto_template_list);
546 	err = 0;
547 out:
548 	up_write(&crypto_alg_sem);
549 	return err;
550 }
551 EXPORT_SYMBOL_GPL(crypto_register_template);
552 
553 int crypto_register_templates(struct crypto_template *tmpls, int count)
554 {
555 	int i, err;
556 
557 	for (i = 0; i < count; i++) {
558 		err = crypto_register_template(&tmpls[i]);
559 		if (err)
560 			goto out;
561 	}
562 	return 0;
563 
564 out:
565 	for (--i; i >= 0; --i)
566 		crypto_unregister_template(&tmpls[i]);
567 	return err;
568 }
569 EXPORT_SYMBOL_GPL(crypto_register_templates);
570 
571 void crypto_unregister_template(struct crypto_template *tmpl)
572 {
573 	struct crypto_instance *inst;
574 	struct hlist_node *n;
575 	struct hlist_head *list;
576 	LIST_HEAD(users);
577 
578 	down_write(&crypto_alg_sem);
579 
580 	BUG_ON(list_empty(&tmpl->list));
581 	list_del_init(&tmpl->list);
582 
583 	list = &tmpl->instances;
584 	hlist_for_each_entry(inst, list, list) {
585 		int err = crypto_remove_alg(&inst->alg, &users);
586 
587 		BUG_ON(err);
588 	}
589 
590 	up_write(&crypto_alg_sem);
591 
592 	hlist_for_each_entry_safe(inst, n, list, list) {
593 		BUG_ON(refcount_read(&inst->alg.cra_refcnt) != 1);
594 		crypto_free_instance(inst);
595 	}
596 	crypto_remove_final(&users);
597 }
598 EXPORT_SYMBOL_GPL(crypto_unregister_template);
599 
600 void crypto_unregister_templates(struct crypto_template *tmpls, int count)
601 {
602 	int i;
603 
604 	for (i = count - 1; i >= 0; --i)
605 		crypto_unregister_template(&tmpls[i]);
606 }
607 EXPORT_SYMBOL_GPL(crypto_unregister_templates);
608 
609 static struct crypto_template *__crypto_lookup_template(const char *name)
610 {
611 	struct crypto_template *q, *tmpl = NULL;
612 
613 	down_read(&crypto_alg_sem);
614 	list_for_each_entry(q, &crypto_template_list, list) {
615 		if (strcmp(q->name, name))
616 			continue;
617 		if (unlikely(!crypto_tmpl_get(q)))
618 			continue;
619 
620 		tmpl = q;
621 		break;
622 	}
623 	up_read(&crypto_alg_sem);
624 
625 	return tmpl;
626 }
627 
628 struct crypto_template *crypto_lookup_template(const char *name)
629 {
630 	return try_then_request_module(__crypto_lookup_template(name),
631 				       "crypto-%s", name);
632 }
633 EXPORT_SYMBOL_GPL(crypto_lookup_template);
634 
635 int crypto_register_instance(struct crypto_template *tmpl,
636 			     struct crypto_instance *inst)
637 {
638 	struct crypto_larval *larval;
639 	struct crypto_spawn *spawn;
640 	u32 fips_internal = 0;
641 	LIST_HEAD(algs_to_put);
642 	int err;
643 
644 	err = crypto_check_alg(&inst->alg);
645 	if (err)
646 		return err;
647 
648 	inst->alg.cra_module = tmpl->module;
649 	inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
650 
651 	down_write(&crypto_alg_sem);
652 
653 	larval = ERR_PTR(-EAGAIN);
654 	for (spawn = inst->spawns; spawn;) {
655 		struct crypto_spawn *next;
656 
657 		if (spawn->dead)
658 			goto unlock;
659 
660 		next = spawn->next;
661 		spawn->inst = inst;
662 		spawn->registered = true;
663 
664 		fips_internal |= spawn->alg->cra_flags;
665 
666 		crypto_mod_put(spawn->alg);
667 
668 		spawn = next;
669 	}
670 
671 	inst->alg.cra_flags |= (fips_internal & CRYPTO_ALG_FIPS_INTERNAL);
672 
673 	larval = __crypto_register_alg(&inst->alg, &algs_to_put);
674 	if (IS_ERR(larval))
675 		goto unlock;
676 	else if (larval)
677 		larval->test_started = true;
678 
679 	hlist_add_head(&inst->list, &tmpl->instances);
680 	inst->tmpl = tmpl;
681 
682 unlock:
683 	up_write(&crypto_alg_sem);
684 
685 	if (IS_ERR(larval))
686 		return PTR_ERR(larval);
687 	if (larval)
688 		crypto_wait_for_test(larval);
689 	crypto_remove_final(&algs_to_put);
690 	return 0;
691 }
692 EXPORT_SYMBOL_GPL(crypto_register_instance);
693 
694 void crypto_unregister_instance(struct crypto_instance *inst)
695 {
696 	LIST_HEAD(list);
697 
698 	down_write(&crypto_alg_sem);
699 
700 	crypto_remove_spawns(&inst->alg, &list, NULL);
701 	crypto_remove_instance(inst, &list);
702 
703 	up_write(&crypto_alg_sem);
704 
705 	crypto_remove_final(&list);
706 }
707 EXPORT_SYMBOL_GPL(crypto_unregister_instance);
708 
709 int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst,
710 		      const char *name, u32 type, u32 mask)
711 {
712 	struct crypto_alg *alg;
713 	int err = -EAGAIN;
714 
715 	if (WARN_ON_ONCE(inst == NULL))
716 		return -EINVAL;
717 
718 	/* Allow the result of crypto_attr_alg_name() to be passed directly */
719 	if (IS_ERR(name))
720 		return PTR_ERR(name);
721 
722 	alg = crypto_find_alg(name, spawn->frontend,
723 			      type | CRYPTO_ALG_FIPS_INTERNAL, mask);
724 	if (IS_ERR(alg))
725 		return PTR_ERR(alg);
726 
727 	down_write(&crypto_alg_sem);
728 	if (!crypto_is_moribund(alg)) {
729 		list_add(&spawn->list, &alg->cra_users);
730 		spawn->alg = alg;
731 		spawn->mask = mask;
732 		spawn->next = inst->spawns;
733 		inst->spawns = spawn;
734 		inst->alg.cra_flags |=
735 			(alg->cra_flags & CRYPTO_ALG_INHERITED_FLAGS);
736 		err = 0;
737 	}
738 	up_write(&crypto_alg_sem);
739 	if (err)
740 		crypto_mod_put(alg);
741 	return err;
742 }
743 EXPORT_SYMBOL_GPL(crypto_grab_spawn);
744 
745 void crypto_drop_spawn(struct crypto_spawn *spawn)
746 {
747 	if (!spawn->alg) /* not yet initialized? */
748 		return;
749 
750 	down_write(&crypto_alg_sem);
751 	if (!spawn->dead)
752 		list_del(&spawn->list);
753 	up_write(&crypto_alg_sem);
754 
755 	if (!spawn->registered)
756 		crypto_mod_put(spawn->alg);
757 }
758 EXPORT_SYMBOL_GPL(crypto_drop_spawn);
759 
760 static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
761 {
762 	struct crypto_alg *alg = ERR_PTR(-EAGAIN);
763 	struct crypto_alg *target;
764 	bool shoot = false;
765 
766 	down_read(&crypto_alg_sem);
767 	if (!spawn->dead) {
768 		alg = spawn->alg;
769 		if (!crypto_mod_get(alg)) {
770 			target = crypto_alg_get(alg);
771 			shoot = true;
772 			alg = ERR_PTR(-EAGAIN);
773 		}
774 	}
775 	up_read(&crypto_alg_sem);
776 
777 	if (shoot) {
778 		crypto_shoot_alg(target);
779 		crypto_alg_put(target);
780 	}
781 
782 	return alg;
783 }
784 
785 struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
786 				    u32 mask)
787 {
788 	struct crypto_alg *alg;
789 	struct crypto_tfm *tfm;
790 
791 	alg = crypto_spawn_alg(spawn);
792 	if (IS_ERR(alg))
793 		return ERR_CAST(alg);
794 
795 	tfm = ERR_PTR(-EINVAL);
796 	if (unlikely((alg->cra_flags ^ type) & mask))
797 		goto out_put_alg;
798 
799 	tfm = __crypto_alloc_tfm(alg, type, mask);
800 	if (IS_ERR(tfm))
801 		goto out_put_alg;
802 
803 	return tfm;
804 
805 out_put_alg:
806 	crypto_mod_put(alg);
807 	return tfm;
808 }
809 EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
810 
811 void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
812 {
813 	struct crypto_alg *alg;
814 	struct crypto_tfm *tfm;
815 
816 	alg = crypto_spawn_alg(spawn);
817 	if (IS_ERR(alg))
818 		return ERR_CAST(alg);
819 
820 	tfm = crypto_create_tfm(alg, spawn->frontend);
821 	if (IS_ERR(tfm))
822 		goto out_put_alg;
823 
824 	return tfm;
825 
826 out_put_alg:
827 	crypto_mod_put(alg);
828 	return tfm;
829 }
830 EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
831 
832 int crypto_register_notifier(struct notifier_block *nb)
833 {
834 	return blocking_notifier_chain_register(&crypto_chain, nb);
835 }
836 EXPORT_SYMBOL_GPL(crypto_register_notifier);
837 
838 int crypto_unregister_notifier(struct notifier_block *nb)
839 {
840 	return blocking_notifier_chain_unregister(&crypto_chain, nb);
841 }
842 EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
843 
844 struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
845 {
846 	struct rtattr *rta = tb[0];
847 	struct crypto_attr_type *algt;
848 
849 	if (!rta)
850 		return ERR_PTR(-ENOENT);
851 	if (RTA_PAYLOAD(rta) < sizeof(*algt))
852 		return ERR_PTR(-EINVAL);
853 	if (rta->rta_type != CRYPTOA_TYPE)
854 		return ERR_PTR(-EINVAL);
855 
856 	algt = RTA_DATA(rta);
857 
858 	return algt;
859 }
860 EXPORT_SYMBOL_GPL(crypto_get_attr_type);
861 
862 /**
863  * crypto_check_attr_type() - check algorithm type and compute inherited mask
864  * @tb: the template parameters
865  * @type: the algorithm type the template would be instantiated as
866  * @mask_ret: (output) the mask that should be passed to crypto_grab_*()
867  *	      to restrict the flags of any inner algorithms
868  *
869  * Validate that the algorithm type the user requested is compatible with the
870  * one the template would actually be instantiated as.  E.g., if the user is
871  * doing crypto_alloc_shash("cbc(aes)", ...), this would return an error because
872  * the "cbc" template creates an "skcipher" algorithm, not an "shash" algorithm.
873  *
874  * Also compute the mask to use to restrict the flags of any inner algorithms.
875  *
876  * Return: 0 on success; -errno on failure
877  */
878 int crypto_check_attr_type(struct rtattr **tb, u32 type, u32 *mask_ret)
879 {
880 	struct crypto_attr_type *algt;
881 
882 	algt = crypto_get_attr_type(tb);
883 	if (IS_ERR(algt))
884 		return PTR_ERR(algt);
885 
886 	if ((algt->type ^ type) & algt->mask)
887 		return -EINVAL;
888 
889 	*mask_ret = crypto_algt_inherited_mask(algt);
890 	return 0;
891 }
892 EXPORT_SYMBOL_GPL(crypto_check_attr_type);
893 
894 const char *crypto_attr_alg_name(struct rtattr *rta)
895 {
896 	struct crypto_attr_alg *alga;
897 
898 	if (!rta)
899 		return ERR_PTR(-ENOENT);
900 	if (RTA_PAYLOAD(rta) < sizeof(*alga))
901 		return ERR_PTR(-EINVAL);
902 	if (rta->rta_type != CRYPTOA_ALG)
903 		return ERR_PTR(-EINVAL);
904 
905 	alga = RTA_DATA(rta);
906 	alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
907 
908 	return alga->name;
909 }
910 EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
911 
912 int crypto_inst_setname(struct crypto_instance *inst, const char *name,
913 			struct crypto_alg *alg)
914 {
915 	if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
916 		     alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
917 		return -ENAMETOOLONG;
918 
919 	if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
920 		     name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
921 		return -ENAMETOOLONG;
922 
923 	return 0;
924 }
925 EXPORT_SYMBOL_GPL(crypto_inst_setname);
926 
927 void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
928 {
929 	INIT_LIST_HEAD(&queue->list);
930 	queue->backlog = &queue->list;
931 	queue->qlen = 0;
932 	queue->max_qlen = max_qlen;
933 }
934 EXPORT_SYMBOL_GPL(crypto_init_queue);
935 
936 int crypto_enqueue_request(struct crypto_queue *queue,
937 			   struct crypto_async_request *request)
938 {
939 	int err = -EINPROGRESS;
940 
941 	if (unlikely(queue->qlen >= queue->max_qlen)) {
942 		if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
943 			err = -ENOSPC;
944 			goto out;
945 		}
946 		err = -EBUSY;
947 		if (queue->backlog == &queue->list)
948 			queue->backlog = &request->list;
949 	}
950 
951 	queue->qlen++;
952 	list_add_tail(&request->list, &queue->list);
953 
954 out:
955 	return err;
956 }
957 EXPORT_SYMBOL_GPL(crypto_enqueue_request);
958 
959 void crypto_enqueue_request_head(struct crypto_queue *queue,
960 				 struct crypto_async_request *request)
961 {
962 	queue->qlen++;
963 	list_add(&request->list, &queue->list);
964 }
965 EXPORT_SYMBOL_GPL(crypto_enqueue_request_head);
966 
967 struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
968 {
969 	struct list_head *request;
970 
971 	if (unlikely(!queue->qlen))
972 		return NULL;
973 
974 	queue->qlen--;
975 
976 	if (queue->backlog != &queue->list)
977 		queue->backlog = queue->backlog->next;
978 
979 	request = queue->list.next;
980 	list_del(request);
981 
982 	return list_entry(request, struct crypto_async_request, list);
983 }
984 EXPORT_SYMBOL_GPL(crypto_dequeue_request);
985 
986 static inline void crypto_inc_byte(u8 *a, unsigned int size)
987 {
988 	u8 *b = (a + size);
989 	u8 c;
990 
991 	for (; size; size--) {
992 		c = *--b + 1;
993 		*b = c;
994 		if (c)
995 			break;
996 	}
997 }
998 
999 void crypto_inc(u8 *a, unsigned int size)
1000 {
1001 	__be32 *b = (__be32 *)(a + size);
1002 	u32 c;
1003 
1004 	if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
1005 	    IS_ALIGNED((unsigned long)b, __alignof__(*b)))
1006 		for (; size >= 4; size -= 4) {
1007 			c = be32_to_cpu(*--b) + 1;
1008 			*b = cpu_to_be32(c);
1009 			if (likely(c))
1010 				return;
1011 		}
1012 
1013 	crypto_inc_byte(a, size);
1014 }
1015 EXPORT_SYMBOL_GPL(crypto_inc);
1016 
1017 unsigned int crypto_alg_extsize(struct crypto_alg *alg)
1018 {
1019 	return alg->cra_ctxsize +
1020 	       (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
1021 }
1022 EXPORT_SYMBOL_GPL(crypto_alg_extsize);
1023 
1024 int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
1025 			u32 type, u32 mask)
1026 {
1027 	int ret = 0;
1028 	struct crypto_alg *alg = crypto_find_alg(name, frontend, type, mask);
1029 
1030 	if (!IS_ERR(alg)) {
1031 		crypto_mod_put(alg);
1032 		ret = 1;
1033 	}
1034 
1035 	return ret;
1036 }
1037 EXPORT_SYMBOL_GPL(crypto_type_has_alg);
1038 
1039 static void __init crypto_start_tests(void)
1040 {
1041 	if (IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS))
1042 		return;
1043 
1044 	for (;;) {
1045 		struct crypto_larval *larval = NULL;
1046 		struct crypto_alg *q;
1047 
1048 		down_write(&crypto_alg_sem);
1049 
1050 		list_for_each_entry(q, &crypto_alg_list, cra_list) {
1051 			struct crypto_larval *l;
1052 
1053 			if (!crypto_is_larval(q))
1054 				continue;
1055 
1056 			l = (void *)q;
1057 
1058 			if (!crypto_is_test_larval(l))
1059 				continue;
1060 
1061 			if (l->test_started)
1062 				continue;
1063 
1064 			l->test_started = true;
1065 			larval = l;
1066 			break;
1067 		}
1068 
1069 		up_write(&crypto_alg_sem);
1070 
1071 		if (!larval)
1072 			break;
1073 
1074 		crypto_wait_for_test(larval);
1075 	}
1076 
1077 	set_crypto_boot_test_finished();
1078 }
1079 
1080 static int __init crypto_algapi_init(void)
1081 {
1082 	crypto_init_proc();
1083 	crypto_start_tests();
1084 	return 0;
1085 }
1086 
1087 static void __exit crypto_algapi_exit(void)
1088 {
1089 	crypto_exit_proc();
1090 }
1091 
1092 /*
1093  * We run this at late_initcall so that all the built-in algorithms
1094  * have had a chance to register themselves first.
1095  */
1096 late_initcall(crypto_algapi_init);
1097 module_exit(crypto_algapi_exit);
1098 
1099 MODULE_LICENSE("GPL");
1100 MODULE_DESCRIPTION("Cryptographic algorithms API");
1101 MODULE_SOFTDEP("pre: cryptomgr");
1102