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) && IS_ENABLED(CONFIG_CRYPTO_SELFTESTS)
35 DEFINE_STATIC_KEY_FALSE(__crypto_boot_test_finished);
36 #endif
37
38 static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg,
39 u32 type, u32 mask);
40 static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
41 u32 mask);
42
crypto_mod_get(struct crypto_alg * alg)43 struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
44 {
45 return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
46 }
47 EXPORT_SYMBOL_GPL(crypto_mod_get);
48
crypto_mod_put(struct crypto_alg * alg)49 void crypto_mod_put(struct crypto_alg *alg)
50 {
51 struct module *module = alg->cra_module;
52
53 crypto_alg_put(alg);
54 module_put(module);
55 }
56 EXPORT_SYMBOL_GPL(crypto_mod_put);
57
__crypto_alg_lookup(const char * name,u32 type,u32 mask)58 static struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type,
59 u32 mask)
60 __must_hold_shared(&crypto_alg_sem)
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_obj(*larval);
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
397 return len;
398 }
399
crypto_shoot_alg(struct crypto_alg * alg)400 void crypto_shoot_alg(struct crypto_alg *alg)
401 {
402 down_write(&crypto_alg_sem);
403 alg->cra_flags |= CRYPTO_ALG_DYING;
404 up_write(&crypto_alg_sem);
405 }
406 EXPORT_SYMBOL_GPL(crypto_shoot_alg);
407
__crypto_alloc_tfmgfp(struct crypto_alg * alg,u32 type,u32 mask,gfp_t gfp)408 struct crypto_tfm *__crypto_alloc_tfmgfp(struct crypto_alg *alg, u32 type,
409 u32 mask, gfp_t gfp)
410 {
411 struct crypto_tfm *tfm;
412 unsigned int tfm_size;
413 int err = -ENOMEM;
414
415 tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask);
416 tfm = kzalloc(tfm_size, gfp);
417 if (tfm == NULL)
418 goto out_err;
419
420 tfm->__crt_alg = alg;
421 refcount_set(&tfm->refcnt, 1);
422
423 if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
424 goto cra_init_failed;
425
426 goto out;
427
428 cra_init_failed:
429 crypto_exit_ops(tfm);
430 if (err == -EAGAIN)
431 crypto_shoot_alg(alg);
432 kfree(tfm);
433 out_err:
434 tfm = ERR_PTR(err);
435 out:
436 return tfm;
437 }
438 EXPORT_SYMBOL_GPL(__crypto_alloc_tfmgfp);
439
__crypto_alloc_tfm(struct crypto_alg * alg,u32 type,u32 mask)440 struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
441 u32 mask)
442 {
443 return __crypto_alloc_tfmgfp(alg, type, mask, GFP_KERNEL);
444 }
445 EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);
446
447 /*
448 * crypto_alloc_base - Locate algorithm and allocate transform
449 * @alg_name: Name of algorithm
450 * @type: Type of algorithm
451 * @mask: Mask for type comparison
452 *
453 * This function should not be used by new algorithm types.
454 * Please use crypto_alloc_tfm instead.
455 *
456 * crypto_alloc_base() will first attempt to locate an already loaded
457 * algorithm. If that fails and the kernel supports dynamically loadable
458 * modules, it will then attempt to load a module of the same name or
459 * alias. If that fails it will send a query to any loaded crypto manager
460 * to construct an algorithm on the fly. A refcount is grabbed on the
461 * algorithm which is then associated with the new transform.
462 *
463 * The returned transform is of a non-determinate type. Most people
464 * should use one of the more specific allocation functions such as
465 * crypto_alloc_skcipher().
466 *
467 * In case of error the return value is an error pointer.
468 */
crypto_alloc_base(const char * alg_name,u32 type,u32 mask)469 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
470 {
471 struct crypto_tfm *tfm;
472 int err;
473
474 for (;;) {
475 struct crypto_alg *alg;
476
477 alg = crypto_alg_mod_lookup(alg_name, type, mask);
478 if (IS_ERR(alg)) {
479 err = PTR_ERR(alg);
480 goto err;
481 }
482
483 tfm = __crypto_alloc_tfm(alg, type, mask);
484 if (!IS_ERR(tfm))
485 return tfm;
486
487 crypto_mod_put(alg);
488 err = PTR_ERR(tfm);
489
490 err:
491 if (err != -EAGAIN)
492 break;
493 if (fatal_signal_pending(current)) {
494 err = -EINTR;
495 break;
496 }
497 }
498
499 return ERR_PTR(err);
500 }
501 EXPORT_SYMBOL_GPL(crypto_alloc_base);
502
crypto_alloc_tfmmem(struct crypto_alg * alg,const struct crypto_type * frontend,int node,gfp_t gfp)503 static void *crypto_alloc_tfmmem(struct crypto_alg *alg,
504 const struct crypto_type *frontend, int node,
505 gfp_t gfp)
506 {
507 struct crypto_tfm *tfm;
508 unsigned int tfmsize;
509 unsigned int total;
510 char *mem;
511
512 tfmsize = frontend->tfmsize;
513 total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
514
515 mem = kzalloc_node(total, gfp, node);
516 if (mem == NULL)
517 return ERR_PTR(-ENOMEM);
518
519 tfm = (struct crypto_tfm *)(mem + tfmsize);
520 tfm->__crt_alg = alg;
521 tfm->node = node;
522 refcount_set(&tfm->refcnt, 1);
523
524 return mem;
525 }
526
crypto_create_tfm_node(struct crypto_alg * alg,const struct crypto_type * frontend,int node)527 void *crypto_create_tfm_node(struct crypto_alg *alg,
528 const struct crypto_type *frontend,
529 int node)
530 {
531 struct crypto_tfm *tfm;
532 char *mem;
533 int err;
534
535 mem = crypto_alloc_tfmmem(alg, frontend, node, GFP_KERNEL);
536 if (IS_ERR(mem))
537 goto out;
538
539 tfm = (struct crypto_tfm *)(mem + frontend->tfmsize);
540 tfm->fb = tfm;
541
542 err = frontend->init_tfm(tfm);
543 if (err)
544 goto out_free_tfm;
545
546 if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
547 goto cra_init_failed;
548
549 goto out;
550
551 cra_init_failed:
552 crypto_exit_ops(tfm);
553 out_free_tfm:
554 if (err == -EAGAIN)
555 crypto_shoot_alg(alg);
556 kfree(mem);
557 mem = ERR_PTR(err);
558 out:
559 return mem;
560 }
561 EXPORT_SYMBOL_GPL(crypto_create_tfm_node);
562
crypto_clone_tfm(const struct crypto_type * frontend,struct crypto_tfm * otfm)563 void *crypto_clone_tfm(const struct crypto_type *frontend,
564 struct crypto_tfm *otfm)
565 {
566 struct crypto_alg *alg = otfm->__crt_alg;
567 struct crypto_tfm *tfm;
568 char *mem;
569
570 mem = ERR_PTR(-ESTALE);
571 if (unlikely(!crypto_mod_get(alg)))
572 goto out;
573
574 mem = crypto_alloc_tfmmem(alg, frontend, otfm->node, GFP_ATOMIC);
575 if (IS_ERR(mem)) {
576 crypto_mod_put(alg);
577 goto out;
578 }
579
580 tfm = (struct crypto_tfm *)(mem + frontend->tfmsize);
581 tfm->crt_flags = otfm->crt_flags;
582 tfm->fb = tfm;
583
584 out:
585 return mem;
586 }
587 EXPORT_SYMBOL_GPL(crypto_clone_tfm);
588
crypto_find_alg(const char * alg_name,const struct crypto_type * frontend,u32 type,u32 mask)589 struct crypto_alg *crypto_find_alg(const char *alg_name,
590 const struct crypto_type *frontend,
591 u32 type, u32 mask)
592 {
593 if (frontend) {
594 type &= frontend->maskclear;
595 mask &= frontend->maskclear;
596 type |= frontend->type;
597 mask |= frontend->maskset;
598 }
599
600 return crypto_alg_mod_lookup(alg_name, type, mask);
601 }
602 EXPORT_SYMBOL_GPL(crypto_find_alg);
603
604 /*
605 * crypto_alloc_tfm_node - Locate algorithm and allocate transform
606 * @alg_name: Name of algorithm
607 * @frontend: Frontend algorithm type
608 * @type: Type of algorithm
609 * @mask: Mask for type comparison
610 * @node: NUMA node in which users desire to put requests, if node is
611 * NUMA_NO_NODE, it means users have no special requirement.
612 *
613 * crypto_alloc_tfm() will first attempt to locate an already loaded
614 * algorithm. If that fails and the kernel supports dynamically loadable
615 * modules, it will then attempt to load a module of the same name or
616 * alias. If that fails it will send a query to any loaded crypto manager
617 * to construct an algorithm on the fly. A refcount is grabbed on the
618 * algorithm which is then associated with the new transform.
619 *
620 * The returned transform is of a non-determinate type. Most people
621 * should use one of the more specific allocation functions such as
622 * crypto_alloc_skcipher().
623 *
624 * In case of error the return value is an error pointer.
625 */
626
crypto_alloc_tfm_node(const char * alg_name,const struct crypto_type * frontend,u32 type,u32 mask,int node)627 void *crypto_alloc_tfm_node(const char *alg_name,
628 const struct crypto_type *frontend, u32 type, u32 mask,
629 int node)
630 {
631 void *tfm;
632 int err;
633
634 for (;;) {
635 struct crypto_alg *alg;
636
637 alg = crypto_find_alg(alg_name, frontend, type, mask);
638 if (IS_ERR(alg)) {
639 err = PTR_ERR(alg);
640 goto err;
641 }
642
643 tfm = crypto_create_tfm_node(alg, frontend, node);
644 if (!IS_ERR(tfm))
645 return tfm;
646
647 crypto_mod_put(alg);
648 err = PTR_ERR(tfm);
649
650 err:
651 if (err != -EAGAIN)
652 break;
653 if (fatal_signal_pending(current)) {
654 err = -EINTR;
655 break;
656 }
657 }
658
659 return ERR_PTR(err);
660 }
661 EXPORT_SYMBOL_GPL(crypto_alloc_tfm_node);
662
663 /*
664 * crypto_destroy_tfm - Free crypto transform
665 * @mem: Start of tfm slab
666 * @tfm: Transform to free
667 *
668 * This function frees up the transform and any associated resources,
669 * then drops the refcount on the associated algorithm.
670 */
crypto_destroy_tfm(void * mem,struct crypto_tfm * tfm)671 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
672 {
673 struct crypto_alg *alg;
674
675 if (IS_ERR_OR_NULL(mem))
676 return;
677
678 if (!refcount_dec_and_test(&tfm->refcnt))
679 return;
680 alg = tfm->__crt_alg;
681
682 if (!tfm->exit && alg->cra_exit)
683 alg->cra_exit(tfm);
684 crypto_exit_ops(tfm);
685 crypto_mod_put(alg);
686 kfree_sensitive(mem);
687 }
688 EXPORT_SYMBOL_GPL(crypto_destroy_tfm);
689
crypto_has_alg(const char * name,u32 type,u32 mask)690 int crypto_has_alg(const char *name, u32 type, u32 mask)
691 {
692 int ret = 0;
693 struct crypto_alg *alg = crypto_alg_mod_lookup(name, type, mask);
694
695 if (!IS_ERR(alg)) {
696 crypto_mod_put(alg);
697 ret = 1;
698 }
699
700 return ret;
701 }
702 EXPORT_SYMBOL_GPL(crypto_has_alg);
703
crypto_req_done(void * data,int err)704 void crypto_req_done(void *data, int err)
705 {
706 struct crypto_wait *wait = data;
707
708 if (err == -EINPROGRESS)
709 return;
710
711 wait->err = err;
712 complete(&wait->completion);
713 }
714 EXPORT_SYMBOL_GPL(crypto_req_done);
715
crypto_destroy_alg(struct crypto_alg * alg)716 void crypto_destroy_alg(struct crypto_alg *alg)
717 {
718 if (alg->cra_type && alg->cra_type->destroy)
719 alg->cra_type->destroy(alg);
720 if (alg->cra_destroy)
721 alg->cra_destroy(alg);
722 }
723 EXPORT_SYMBOL_GPL(crypto_destroy_alg);
724
crypto_request_clone(struct crypto_async_request * req,size_t total,gfp_t gfp)725 struct crypto_async_request *crypto_request_clone(
726 struct crypto_async_request *req, size_t total, gfp_t gfp)
727 {
728 struct crypto_tfm *tfm = req->tfm;
729 struct crypto_async_request *nreq;
730
731 nreq = kmemdup(req, total, gfp);
732 if (!nreq) {
733 req->tfm = tfm->fb;
734 return req;
735 }
736
737 nreq->flags &= ~CRYPTO_TFM_REQ_ON_STACK;
738 return nreq;
739 }
740 EXPORT_SYMBOL_GPL(crypto_request_clone);
741
742 MODULE_DESCRIPTION("Cryptographic core API");
743 MODULE_LICENSE("GPL");
744