• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Scatterlist Cryptographic API.
4  *
5  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
6  * Copyright (c) 2002 David S. Miller (davem@redhat.com)
7  * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
8  *
9  * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
10  * and Nettle, by Niels Möller.
11  */
12 
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/jump_label.h>
16 #include <linux/kernel.h>
17 #include <linux/kmod.h>
18 #include <linux/module.h>
19 #include <linux/param.h>
20 #include <linux/sched/signal.h>
21 #include <linux/slab.h>
22 #include <linux/string.h>
23 #include <linux/completion.h>
24 #include "internal.h"
25 
26 LIST_HEAD(crypto_alg_list);
27 EXPORT_SYMBOL_GPL(crypto_alg_list);
28 DECLARE_RWSEM(crypto_alg_sem);
29 EXPORT_SYMBOL_GPL(crypto_alg_sem);
30 
31 BLOCKING_NOTIFIER_HEAD(crypto_chain);
32 EXPORT_SYMBOL_GPL(crypto_chain);
33 
34 #if IS_BUILTIN(CONFIG_CRYPTO_ALGAPI) && \
35     !IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS)
36 DEFINE_STATIC_KEY_FALSE(__crypto_boot_test_finished);
37 #endif
38 
39 static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg,
40 					     u32 type, u32 mask);
41 static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
42 					    u32 mask);
43 
crypto_mod_get(struct crypto_alg * alg)44 struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
45 {
46 	return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
47 }
48 EXPORT_SYMBOL_GPL(crypto_mod_get);
49 
crypto_mod_put(struct crypto_alg * alg)50 void crypto_mod_put(struct crypto_alg *alg)
51 {
52 	struct module *module = alg->cra_module;
53 
54 	crypto_alg_put(alg);
55 	module_put(module);
56 }
57 EXPORT_SYMBOL_GPL(crypto_mod_put);
58 
__crypto_alg_lookup(const char * name,u32 type,u32 mask)59 static struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type,
60 					      u32 mask)
61 {
62 	struct crypto_alg *q, *alg = NULL;
63 	int best = -2;
64 
65 	list_for_each_entry(q, &crypto_alg_list, cra_list) {
66 		int exact, fuzzy;
67 
68 		if (crypto_is_moribund(q))
69 			continue;
70 
71 		if ((q->cra_flags ^ type) & mask)
72 			continue;
73 
74 		exact = !strcmp(q->cra_driver_name, name);
75 		fuzzy = !strcmp(q->cra_name, name);
76 		if (!exact && !(fuzzy && q->cra_priority > best))
77 			continue;
78 
79 		if (unlikely(!crypto_mod_get(q)))
80 			continue;
81 
82 		best = q->cra_priority;
83 		if (alg)
84 			crypto_mod_put(alg);
85 		alg = q;
86 
87 		if (exact)
88 			break;
89 	}
90 
91 	return alg;
92 }
93 
crypto_larval_destroy(struct crypto_alg * alg)94 static void crypto_larval_destroy(struct crypto_alg *alg)
95 {
96 	struct crypto_larval *larval = (void *)alg;
97 
98 	BUG_ON(!crypto_is_larval(alg));
99 	if (!IS_ERR_OR_NULL(larval->adult))
100 		crypto_mod_put(larval->adult);
101 	kfree(larval);
102 }
103 
crypto_larval_alloc(const char * name,u32 type,u32 mask)104 struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask)
105 {
106 	struct crypto_larval *larval;
107 
108 	larval = kzalloc(sizeof(*larval), GFP_KERNEL);
109 	if (!larval)
110 		return ERR_PTR(-ENOMEM);
111 
112 	type &= ~CRYPTO_ALG_TYPE_MASK | (mask ?: CRYPTO_ALG_TYPE_MASK);
113 
114 	larval->mask = mask;
115 	larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type;
116 	larval->alg.cra_priority = -1;
117 	larval->alg.cra_destroy = crypto_larval_destroy;
118 
119 	strscpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
120 	init_completion(&larval->completion);
121 
122 	return larval;
123 }
124 EXPORT_SYMBOL_GPL(crypto_larval_alloc);
125 
crypto_larval_add(const char * name,u32 type,u32 mask)126 static struct crypto_alg *crypto_larval_add(const char *name, u32 type,
127 					    u32 mask)
128 {
129 	struct crypto_alg *alg;
130 	struct crypto_larval *larval;
131 
132 	larval = crypto_larval_alloc(name, type, mask);
133 	if (IS_ERR(larval))
134 		return ERR_CAST(larval);
135 
136 	refcount_set(&larval->alg.cra_refcnt, 2);
137 
138 	down_write(&crypto_alg_sem);
139 	alg = __crypto_alg_lookup(name, type, mask);
140 	if (!alg) {
141 		alg = &larval->alg;
142 		list_add(&alg->cra_list, &crypto_alg_list);
143 	}
144 	up_write(&crypto_alg_sem);
145 
146 	if (alg != &larval->alg) {
147 		kfree(larval);
148 		if (crypto_is_larval(alg))
149 			alg = crypto_larval_wait(alg, type, mask);
150 	}
151 
152 	return alg;
153 }
154 
crypto_larval_kill(struct crypto_larval * larval)155 static void crypto_larval_kill(struct crypto_larval *larval)
156 {
157 	bool unlinked;
158 
159 	down_write(&crypto_alg_sem);
160 	unlinked = list_empty(&larval->alg.cra_list);
161 	if (!unlinked)
162 		list_del_init(&larval->alg.cra_list);
163 	up_write(&crypto_alg_sem);
164 
165 	if (unlinked)
166 		return;
167 
168 	complete_all(&larval->completion);
169 	crypto_alg_put(&larval->alg);
170 }
171 
crypto_schedule_test(struct crypto_larval * larval)172 void crypto_schedule_test(struct crypto_larval *larval)
173 {
174 	int err;
175 
176 	err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
177 	WARN_ON_ONCE(err != NOTIFY_STOP);
178 }
179 EXPORT_SYMBOL_GPL(crypto_schedule_test);
180 
crypto_start_test(struct crypto_larval * larval)181 static void crypto_start_test(struct crypto_larval *larval)
182 {
183 	if (!crypto_is_test_larval(larval))
184 		return;
185 
186 	if (larval->test_started)
187 		return;
188 
189 	down_write(&crypto_alg_sem);
190 	if (larval->test_started) {
191 		up_write(&crypto_alg_sem);
192 		return;
193 	}
194 
195 	larval->test_started = true;
196 	up_write(&crypto_alg_sem);
197 
198 	crypto_schedule_test(larval);
199 }
200 
crypto_larval_wait(struct crypto_alg * alg,u32 type,u32 mask)201 static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg,
202 					     u32 type, u32 mask)
203 {
204 	struct crypto_larval *larval;
205 	long time_left;
206 
207 again:
208 	larval = container_of(alg, struct crypto_larval, alg);
209 
210 	if (!crypto_boot_test_finished())
211 		crypto_start_test(larval);
212 
213 	time_left = wait_for_completion_killable_timeout(
214 		&larval->completion, 60 * HZ);
215 
216 	alg = larval->adult;
217 	if (time_left < 0)
218 		alg = ERR_PTR(-EINTR);
219 	else if (!time_left) {
220 		if (crypto_is_test_larval(larval))
221 			crypto_larval_kill(larval);
222 		alg = ERR_PTR(-ETIMEDOUT);
223 	} else if (!alg || PTR_ERR(alg) == -EEXIST) {
224 		int err = alg ? -EEXIST : -EAGAIN;
225 
226 		/*
227 		 * EEXIST is expected because two probes can be scheduled
228 		 * at the same time with one using alg_name and the other
229 		 * using driver_name.  Do a re-lookup but do not retry in
230 		 * case we hit a quirk like gcm_base(ctr(aes),...) which
231 		 * will never match.
232 		 */
233 		alg = &larval->alg;
234 		alg = crypto_alg_lookup(alg->cra_name, type, mask) ?:
235 		      ERR_PTR(err);
236 	} else if (IS_ERR(alg))
237 		;
238 	else if (crypto_is_test_larval(larval) &&
239 		 !(alg->cra_flags & CRYPTO_ALG_TESTED))
240 		alg = ERR_PTR(-EAGAIN);
241 	else if (alg->cra_flags & CRYPTO_ALG_FIPS_INTERNAL)
242 		alg = ERR_PTR(-EAGAIN);
243 	else if (!crypto_mod_get(alg))
244 		alg = ERR_PTR(-EAGAIN);
245 	crypto_mod_put(&larval->alg);
246 
247 	if (!IS_ERR(alg) && crypto_is_larval(alg))
248 		goto again;
249 
250 	return alg;
251 }
252 
crypto_alg_lookup(const char * name,u32 type,u32 mask)253 static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
254 					    u32 mask)
255 {
256 	const u32 fips = CRYPTO_ALG_FIPS_INTERNAL;
257 	struct crypto_alg *alg;
258 	u32 test = 0;
259 
260 	if (!((type | mask) & CRYPTO_ALG_TESTED))
261 		test |= CRYPTO_ALG_TESTED;
262 
263 	down_read(&crypto_alg_sem);
264 	alg = __crypto_alg_lookup(name, (type | test) & ~fips,
265 				  (mask | test) & ~fips);
266 	if (alg) {
267 		if (((type | mask) ^ fips) & fips)
268 			mask |= fips;
269 		mask &= fips;
270 
271 		if (!crypto_is_larval(alg) &&
272 		    ((type ^ alg->cra_flags) & mask)) {
273 			/* Algorithm is disallowed in FIPS mode. */
274 			crypto_mod_put(alg);
275 			alg = ERR_PTR(-ENOENT);
276 		}
277 	} else if (test) {
278 		alg = __crypto_alg_lookup(name, type, mask);
279 		if (alg && !crypto_is_larval(alg)) {
280 			/* Test failed */
281 			crypto_mod_put(alg);
282 			alg = ERR_PTR(-ELIBBAD);
283 		}
284 	}
285 	up_read(&crypto_alg_sem);
286 
287 	return alg;
288 }
289 
crypto_larval_lookup(const char * name,u32 type,u32 mask)290 static struct crypto_alg *crypto_larval_lookup(const char *name, u32 type,
291 					       u32 mask)
292 {
293 	struct crypto_alg *alg;
294 
295 	if (!name)
296 		return ERR_PTR(-ENOENT);
297 
298 	type &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
299 	mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
300 
301 	alg = crypto_alg_lookup(name, type, mask);
302 	if (!alg && !(mask & CRYPTO_NOLOAD)) {
303 		request_module("crypto-%s", name);
304 
305 		if (!((type ^ CRYPTO_ALG_NEED_FALLBACK) & mask &
306 		      CRYPTO_ALG_NEED_FALLBACK))
307 			request_module("crypto-%s-all", name);
308 
309 		alg = crypto_alg_lookup(name, type, mask);
310 	}
311 
312 	if (!IS_ERR_OR_NULL(alg) && crypto_is_larval(alg))
313 		alg = crypto_larval_wait(alg, type, mask);
314 	else if (alg)
315 		;
316 	else if (!(mask & CRYPTO_ALG_TESTED))
317 		alg = crypto_larval_add(name, type, mask);
318 	else
319 		alg = ERR_PTR(-ENOENT);
320 
321 	return alg;
322 }
323 
crypto_probing_notify(unsigned long val,void * v)324 int crypto_probing_notify(unsigned long val, void *v)
325 {
326 	int ok;
327 
328 	ok = blocking_notifier_call_chain(&crypto_chain, val, v);
329 	if (ok == NOTIFY_DONE) {
330 		request_module("cryptomgr");
331 		ok = blocking_notifier_call_chain(&crypto_chain, val, v);
332 	}
333 
334 	return ok;
335 }
336 EXPORT_SYMBOL_GPL(crypto_probing_notify);
337 
crypto_alg_mod_lookup(const char * name,u32 type,u32 mask)338 struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
339 {
340 	struct crypto_alg *alg;
341 	struct crypto_alg *larval;
342 	int ok;
343 
344 	/*
345 	 * If the internal flag is set for a cipher, require a caller to
346 	 * invoke the cipher with the internal flag to use that cipher.
347 	 * Also, if a caller wants to allocate a cipher that may or may
348 	 * not be an internal cipher, use type | CRYPTO_ALG_INTERNAL and
349 	 * !(mask & CRYPTO_ALG_INTERNAL).
350 	 */
351 	if (!((type | mask) & CRYPTO_ALG_INTERNAL))
352 		mask |= CRYPTO_ALG_INTERNAL;
353 
354 	larval = crypto_larval_lookup(name, type, mask);
355 	if (IS_ERR(larval) || !crypto_is_larval(larval))
356 		return larval;
357 
358 	ok = crypto_probing_notify(CRYPTO_MSG_ALG_REQUEST, larval);
359 
360 	if (ok == NOTIFY_STOP)
361 		alg = crypto_larval_wait(larval, type, mask);
362 	else {
363 		crypto_mod_put(larval);
364 		alg = ERR_PTR(-ENOENT);
365 	}
366 	crypto_larval_kill(container_of(larval, struct crypto_larval, alg));
367 	return alg;
368 }
369 EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
370 
crypto_exit_ops(struct crypto_tfm * tfm)371 static void crypto_exit_ops(struct crypto_tfm *tfm)
372 {
373 	const struct crypto_type *type = tfm->__crt_alg->cra_type;
374 
375 	if (type && tfm->exit)
376 		tfm->exit(tfm);
377 }
378 
crypto_ctxsize(struct crypto_alg * alg,u32 type,u32 mask)379 static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask)
380 {
381 	const struct crypto_type *type_obj = alg->cra_type;
382 	unsigned int len;
383 
384 	len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
385 	if (type_obj)
386 		return len + type_obj->ctxsize(alg, type, mask);
387 
388 	switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
389 	default:
390 		BUG();
391 
392 	case CRYPTO_ALG_TYPE_CIPHER:
393 		len += crypto_cipher_ctxsize(alg);
394 		break;
395 
396 	case CRYPTO_ALG_TYPE_COMPRESS:
397 		len += crypto_compress_ctxsize(alg);
398 		break;
399 	}
400 
401 	return len;
402 }
403 
crypto_shoot_alg(struct crypto_alg * alg)404 void crypto_shoot_alg(struct crypto_alg *alg)
405 {
406 	down_write(&crypto_alg_sem);
407 	alg->cra_flags |= CRYPTO_ALG_DYING;
408 	up_write(&crypto_alg_sem);
409 }
410 EXPORT_SYMBOL_GPL(crypto_shoot_alg);
411 
__crypto_alloc_tfmgfp(struct crypto_alg * alg,u32 type,u32 mask,gfp_t gfp)412 struct crypto_tfm *__crypto_alloc_tfmgfp(struct crypto_alg *alg, u32 type,
413 					 u32 mask, gfp_t gfp)
414 {
415 	struct crypto_tfm *tfm;
416 	unsigned int tfm_size;
417 	int err = -ENOMEM;
418 
419 	tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask);
420 	tfm = kzalloc(tfm_size, gfp);
421 	if (tfm == NULL)
422 		goto out_err;
423 
424 	tfm->__crt_alg = alg;
425 	refcount_set(&tfm->refcnt, 1);
426 
427 	if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
428 		goto cra_init_failed;
429 
430 	goto out;
431 
432 cra_init_failed:
433 	crypto_exit_ops(tfm);
434 	if (err == -EAGAIN)
435 		crypto_shoot_alg(alg);
436 	kfree(tfm);
437 out_err:
438 	tfm = ERR_PTR(err);
439 out:
440 	return tfm;
441 }
442 EXPORT_SYMBOL_GPL(__crypto_alloc_tfmgfp);
443 
__crypto_alloc_tfm(struct crypto_alg * alg,u32 type,u32 mask)444 struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
445 				      u32 mask)
446 {
447 	return __crypto_alloc_tfmgfp(alg, type, mask, GFP_KERNEL);
448 }
449 EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);
450 
451 /*
452  *	crypto_alloc_base - Locate algorithm and allocate transform
453  *	@alg_name: Name of algorithm
454  *	@type: Type of algorithm
455  *	@mask: Mask for type comparison
456  *
457  *	This function should not be used by new algorithm types.
458  *	Please use crypto_alloc_tfm instead.
459  *
460  *	crypto_alloc_base() will first attempt to locate an already loaded
461  *	algorithm.  If that fails and the kernel supports dynamically loadable
462  *	modules, it will then attempt to load a module of the same name or
463  *	alias.  If that fails it will send a query to any loaded crypto manager
464  *	to construct an algorithm on the fly.  A refcount is grabbed on the
465  *	algorithm which is then associated with the new transform.
466  *
467  *	The returned transform is of a non-determinate type.  Most people
468  *	should use one of the more specific allocation functions such as
469  *	crypto_alloc_skcipher().
470  *
471  *	In case of error the return value is an error pointer.
472  */
crypto_alloc_base(const char * alg_name,u32 type,u32 mask)473 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
474 {
475 	struct crypto_tfm *tfm;
476 	int err;
477 
478 	for (;;) {
479 		struct crypto_alg *alg;
480 
481 		alg = crypto_alg_mod_lookup(alg_name, type, mask);
482 		if (IS_ERR(alg)) {
483 			err = PTR_ERR(alg);
484 			goto err;
485 		}
486 
487 		tfm = __crypto_alloc_tfm(alg, type, mask);
488 		if (!IS_ERR(tfm))
489 			return tfm;
490 
491 		crypto_mod_put(alg);
492 		err = PTR_ERR(tfm);
493 
494 err:
495 		if (err != -EAGAIN)
496 			break;
497 		if (fatal_signal_pending(current)) {
498 			err = -EINTR;
499 			break;
500 		}
501 	}
502 
503 	return ERR_PTR(err);
504 }
505 EXPORT_SYMBOL_GPL(crypto_alloc_base);
506 
crypto_alloc_tfmmem(struct crypto_alg * alg,const struct crypto_type * frontend,int node,gfp_t gfp)507 static void *crypto_alloc_tfmmem(struct crypto_alg *alg,
508 				 const struct crypto_type *frontend, int node,
509 				 gfp_t gfp)
510 {
511 	struct crypto_tfm *tfm;
512 	unsigned int tfmsize;
513 	unsigned int total;
514 	char *mem;
515 
516 	tfmsize = frontend->tfmsize;
517 	total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
518 
519 	mem = kzalloc_node(total, gfp, node);
520 	if (mem == NULL)
521 		return ERR_PTR(-ENOMEM);
522 
523 	tfm = (struct crypto_tfm *)(mem + tfmsize);
524 	tfm->__crt_alg = alg;
525 	tfm->node = node;
526 	refcount_set(&tfm->refcnt, 1);
527 
528 	return mem;
529 }
530 
crypto_create_tfm_node(struct crypto_alg * alg,const struct crypto_type * frontend,int node)531 void *crypto_create_tfm_node(struct crypto_alg *alg,
532 			     const struct crypto_type *frontend,
533 			     int node)
534 {
535 	struct crypto_tfm *tfm;
536 	char *mem;
537 	int err;
538 
539 	mem = crypto_alloc_tfmmem(alg, frontend, node, GFP_KERNEL);
540 	if (IS_ERR(mem))
541 		goto out;
542 
543 	tfm = (struct crypto_tfm *)(mem + frontend->tfmsize);
544 
545 	err = frontend->init_tfm(tfm);
546 	if (err)
547 		goto out_free_tfm;
548 
549 	if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
550 		goto cra_init_failed;
551 
552 	goto out;
553 
554 cra_init_failed:
555 	crypto_exit_ops(tfm);
556 out_free_tfm:
557 	if (err == -EAGAIN)
558 		crypto_shoot_alg(alg);
559 	kfree(mem);
560 	mem = ERR_PTR(err);
561 out:
562 	return mem;
563 }
564 EXPORT_SYMBOL_GPL(crypto_create_tfm_node);
565 
crypto_clone_tfm(const struct crypto_type * frontend,struct crypto_tfm * otfm)566 void *crypto_clone_tfm(const struct crypto_type *frontend,
567 		       struct crypto_tfm *otfm)
568 {
569 	struct crypto_alg *alg = otfm->__crt_alg;
570 	struct crypto_tfm *tfm;
571 	char *mem;
572 
573 	mem = ERR_PTR(-ESTALE);
574 	if (unlikely(!crypto_mod_get(alg)))
575 		goto out;
576 
577 	mem = crypto_alloc_tfmmem(alg, frontend, otfm->node, GFP_ATOMIC);
578 	if (IS_ERR(mem)) {
579 		crypto_mod_put(alg);
580 		goto out;
581 	}
582 
583 	tfm = (struct crypto_tfm *)(mem + frontend->tfmsize);
584 	tfm->crt_flags = otfm->crt_flags;
585 	tfm->exit = otfm->exit;
586 
587 out:
588 	return mem;
589 }
590 EXPORT_SYMBOL_GPL(crypto_clone_tfm);
591 
crypto_find_alg(const char * alg_name,const struct crypto_type * frontend,u32 type,u32 mask)592 struct crypto_alg *crypto_find_alg(const char *alg_name,
593 				   const struct crypto_type *frontend,
594 				   u32 type, u32 mask)
595 {
596 	if (frontend) {
597 		type &= frontend->maskclear;
598 		mask &= frontend->maskclear;
599 		type |= frontend->type;
600 		mask |= frontend->maskset;
601 	}
602 
603 	return crypto_alg_mod_lookup(alg_name, type, mask);
604 }
605 EXPORT_SYMBOL_GPL(crypto_find_alg);
606 
607 /*
608  *	crypto_alloc_tfm_node - Locate algorithm and allocate transform
609  *	@alg_name: Name of algorithm
610  *	@frontend: Frontend algorithm type
611  *	@type: Type of algorithm
612  *	@mask: Mask for type comparison
613  *	@node: NUMA node in which users desire to put requests, if node is
614  *		NUMA_NO_NODE, it means users have no special requirement.
615  *
616  *	crypto_alloc_tfm() will first attempt to locate an already loaded
617  *	algorithm.  If that fails and the kernel supports dynamically loadable
618  *	modules, it will then attempt to load a module of the same name or
619  *	alias.  If that fails it will send a query to any loaded crypto manager
620  *	to construct an algorithm on the fly.  A refcount is grabbed on the
621  *	algorithm which is then associated with the new transform.
622  *
623  *	The returned transform is of a non-determinate type.  Most people
624  *	should use one of the more specific allocation functions such as
625  *	crypto_alloc_skcipher().
626  *
627  *	In case of error the return value is an error pointer.
628  */
629 
crypto_alloc_tfm_node(const char * alg_name,const struct crypto_type * frontend,u32 type,u32 mask,int node)630 void *crypto_alloc_tfm_node(const char *alg_name,
631 		       const struct crypto_type *frontend, u32 type, u32 mask,
632 		       int node)
633 {
634 	void *tfm;
635 	int err;
636 
637 	for (;;) {
638 		struct crypto_alg *alg;
639 
640 		alg = crypto_find_alg(alg_name, frontend, type, mask);
641 		if (IS_ERR(alg)) {
642 			err = PTR_ERR(alg);
643 			goto err;
644 		}
645 
646 		tfm = crypto_create_tfm_node(alg, frontend, node);
647 		if (!IS_ERR(tfm))
648 			return tfm;
649 
650 		crypto_mod_put(alg);
651 		err = PTR_ERR(tfm);
652 
653 err:
654 		if (err != -EAGAIN)
655 			break;
656 		if (fatal_signal_pending(current)) {
657 			err = -EINTR;
658 			break;
659 		}
660 	}
661 
662 	return ERR_PTR(err);
663 }
664 EXPORT_SYMBOL_GPL(crypto_alloc_tfm_node);
665 
666 /*
667  *	crypto_destroy_tfm - Free crypto transform
668  *	@mem: Start of tfm slab
669  *	@tfm: Transform to free
670  *
671  *	This function frees up the transform and any associated resources,
672  *	then drops the refcount on the associated algorithm.
673  */
crypto_destroy_tfm(void * mem,struct crypto_tfm * tfm)674 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
675 {
676 	struct crypto_alg *alg;
677 
678 	if (IS_ERR_OR_NULL(mem))
679 		return;
680 
681 	if (!refcount_dec_and_test(&tfm->refcnt))
682 		return;
683 	alg = tfm->__crt_alg;
684 
685 	if (!tfm->exit && alg->cra_exit)
686 		alg->cra_exit(tfm);
687 	crypto_exit_ops(tfm);
688 	crypto_mod_put(alg);
689 	kfree_sensitive(mem);
690 }
691 EXPORT_SYMBOL_GPL(crypto_destroy_tfm);
692 
crypto_has_alg(const char * name,u32 type,u32 mask)693 int crypto_has_alg(const char *name, u32 type, u32 mask)
694 {
695 	int ret = 0;
696 	struct crypto_alg *alg = crypto_alg_mod_lookup(name, type, mask);
697 
698 	if (!IS_ERR(alg)) {
699 		crypto_mod_put(alg);
700 		ret = 1;
701 	}
702 
703 	return ret;
704 }
705 EXPORT_SYMBOL_GPL(crypto_has_alg);
706 
crypto_req_done(void * data,int err)707 void crypto_req_done(void *data, int err)
708 {
709 	struct crypto_wait *wait = data;
710 
711 	if (err == -EINPROGRESS)
712 		return;
713 
714 	wait->err = err;
715 	complete(&wait->completion);
716 }
717 EXPORT_SYMBOL_GPL(crypto_req_done);
718 
719 MODULE_DESCRIPTION("Cryptographic core API");
720 MODULE_LICENSE("GPL");
721