crypto: api - Fix built-in testing dependency failures
[linux-2.6-microblaze.git] / crypto / api.c
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 DEFINE_STATIC_KEY_FALSE(crypto_boot_test_finished);
35
36 static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);
37
38 struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
39 {
40         return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
41 }
42 EXPORT_SYMBOL_GPL(crypto_mod_get);
43
44 void crypto_mod_put(struct crypto_alg *alg)
45 {
46         struct module *module = alg->cra_module;
47
48         crypto_alg_put(alg);
49         module_put(module);
50 }
51 EXPORT_SYMBOL_GPL(crypto_mod_put);
52
53 static struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type,
54                                               u32 mask)
55 {
56         struct crypto_alg *q, *alg = NULL;
57         int best = -2;
58
59         list_for_each_entry(q, &crypto_alg_list, cra_list) {
60                 int exact, fuzzy;
61
62                 if (crypto_is_moribund(q))
63                         continue;
64
65                 if ((q->cra_flags ^ type) & mask)
66                         continue;
67
68                 if (crypto_is_larval(q) &&
69                     !crypto_is_test_larval((struct crypto_larval *)q) &&
70                     ((struct crypto_larval *)q)->mask != mask)
71                         continue;
72
73                 exact = !strcmp(q->cra_driver_name, name);
74                 fuzzy = !strcmp(q->cra_name, name);
75                 if (!exact && !(fuzzy && q->cra_priority > best))
76                         continue;
77
78                 if (unlikely(!crypto_mod_get(q)))
79                         continue;
80
81                 best = q->cra_priority;
82                 if (alg)
83                         crypto_mod_put(alg);
84                 alg = q;
85
86                 if (exact)
87                         break;
88         }
89
90         return alg;
91 }
92
93 static void crypto_larval_destroy(struct crypto_alg *alg)
94 {
95         struct crypto_larval *larval = (void *)alg;
96
97         BUG_ON(!crypto_is_larval(alg));
98         if (!IS_ERR_OR_NULL(larval->adult))
99                 crypto_mod_put(larval->adult);
100         kfree(larval);
101 }
102
103 struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask)
104 {
105         struct crypto_larval *larval;
106
107         larval = kzalloc(sizeof(*larval), GFP_KERNEL);
108         if (!larval)
109                 return ERR_PTR(-ENOMEM);
110
111         larval->mask = mask;
112         larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type;
113         larval->alg.cra_priority = -1;
114         larval->alg.cra_destroy = crypto_larval_destroy;
115
116         strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
117         init_completion(&larval->completion);
118
119         return larval;
120 }
121 EXPORT_SYMBOL_GPL(crypto_larval_alloc);
122
123 static struct crypto_alg *crypto_larval_add(const char *name, u32 type,
124                                             u32 mask)
125 {
126         struct crypto_alg *alg;
127         struct crypto_larval *larval;
128
129         larval = crypto_larval_alloc(name, type, mask);
130         if (IS_ERR(larval))
131                 return ERR_CAST(larval);
132
133         refcount_set(&larval->alg.cra_refcnt, 2);
134
135         down_write(&crypto_alg_sem);
136         alg = __crypto_alg_lookup(name, type, mask);
137         if (!alg) {
138                 alg = &larval->alg;
139                 list_add(&alg->cra_list, &crypto_alg_list);
140         }
141         up_write(&crypto_alg_sem);
142
143         if (alg != &larval->alg) {
144                 kfree(larval);
145                 if (crypto_is_larval(alg))
146                         alg = crypto_larval_wait(alg);
147         }
148
149         return alg;
150 }
151
152 void crypto_larval_kill(struct crypto_alg *alg)
153 {
154         struct crypto_larval *larval = (void *)alg;
155
156         down_write(&crypto_alg_sem);
157         list_del(&alg->cra_list);
158         up_write(&crypto_alg_sem);
159         complete_all(&larval->completion);
160         crypto_alg_put(alg);
161 }
162 EXPORT_SYMBOL_GPL(crypto_larval_kill);
163
164 void crypto_wait_for_test(struct crypto_larval *larval)
165 {
166         int err;
167
168         err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
169         if (err != NOTIFY_STOP) {
170                 if (WARN_ON(err != NOTIFY_DONE))
171                         goto out;
172                 crypto_alg_tested(larval->alg.cra_driver_name, 0);
173         }
174
175         err = wait_for_completion_killable(&larval->completion);
176         WARN_ON(err);
177         if (!err)
178                 crypto_notify(CRYPTO_MSG_ALG_LOADED, larval);
179
180 out:
181         crypto_larval_kill(&larval->alg);
182 }
183 EXPORT_SYMBOL_GPL(crypto_wait_for_test);
184
185 static void crypto_start_test(struct crypto_larval *larval)
186 {
187         if (!crypto_is_test_larval(larval))
188                 return;
189
190         if (larval->test_started)
191                 return;
192
193         down_write(&crypto_alg_sem);
194         if (larval->test_started) {
195                 up_write(&crypto_alg_sem);
196                 return;
197         }
198
199         larval->test_started = true;
200         up_write(&crypto_alg_sem);
201
202         crypto_wait_for_test(larval);
203 }
204
205 static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
206 {
207         struct crypto_larval *larval = (void *)alg;
208         long timeout;
209
210         if (!static_branch_likely(&crypto_boot_test_finished))
211                 crypto_start_test(larval);
212
213         timeout = wait_for_completion_killable_timeout(
214                 &larval->completion, 60 * HZ);
215
216         alg = larval->adult;
217         if (timeout < 0)
218                 alg = ERR_PTR(-EINTR);
219         else if (!timeout)
220                 alg = ERR_PTR(-ETIMEDOUT);
221         else if (!alg)
222                 alg = ERR_PTR(-ENOENT);
223         else if (IS_ERR(alg))
224                 ;
225         else if (crypto_is_test_larval(larval) &&
226                  !(alg->cra_flags & CRYPTO_ALG_TESTED))
227                 alg = ERR_PTR(-EAGAIN);
228         else if (!crypto_mod_get(alg))
229                 alg = ERR_PTR(-EAGAIN);
230         crypto_mod_put(&larval->alg);
231
232         return alg;
233 }
234
235 static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
236                                             u32 mask)
237 {
238         struct crypto_alg *alg;
239         u32 test = 0;
240
241         if (!((type | mask) & CRYPTO_ALG_TESTED))
242                 test |= CRYPTO_ALG_TESTED;
243
244         down_read(&crypto_alg_sem);
245         alg = __crypto_alg_lookup(name, type | test, mask | test);
246         if (!alg && test) {
247                 alg = __crypto_alg_lookup(name, type, mask);
248                 if (alg && !crypto_is_larval(alg)) {
249                         /* Test failed */
250                         crypto_mod_put(alg);
251                         alg = ERR_PTR(-ELIBBAD);
252                 }
253         }
254         up_read(&crypto_alg_sem);
255
256         return alg;
257 }
258
259 static struct crypto_alg *crypto_larval_lookup(const char *name, u32 type,
260                                                u32 mask)
261 {
262         struct crypto_alg *alg;
263
264         if (!name)
265                 return ERR_PTR(-ENOENT);
266
267         type &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
268         mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
269
270         alg = crypto_alg_lookup(name, type, mask);
271         if (!alg && !(mask & CRYPTO_NOLOAD)) {
272                 request_module("crypto-%s", name);
273
274                 if (!((type ^ CRYPTO_ALG_NEED_FALLBACK) & mask &
275                       CRYPTO_ALG_NEED_FALLBACK))
276                         request_module("crypto-%s-all", name);
277
278                 alg = crypto_alg_lookup(name, type, mask);
279         }
280
281         if (!IS_ERR_OR_NULL(alg) && crypto_is_larval(alg))
282                 alg = crypto_larval_wait(alg);
283         else if (!alg)
284                 alg = crypto_larval_add(name, type, mask);
285
286         return alg;
287 }
288
289 int crypto_probing_notify(unsigned long val, void *v)
290 {
291         int ok;
292
293         ok = blocking_notifier_call_chain(&crypto_chain, val, v);
294         if (ok == NOTIFY_DONE) {
295                 request_module("cryptomgr");
296                 ok = blocking_notifier_call_chain(&crypto_chain, val, v);
297         }
298
299         return ok;
300 }
301 EXPORT_SYMBOL_GPL(crypto_probing_notify);
302
303 struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
304 {
305         struct crypto_alg *alg;
306         struct crypto_alg *larval;
307         int ok;
308
309         /*
310          * If the internal flag is set for a cipher, require a caller to
311          * to invoke the cipher with the internal flag to use that cipher.
312          * Also, if a caller wants to allocate a cipher that may or may
313          * not be an internal cipher, use type | CRYPTO_ALG_INTERNAL and
314          * !(mask & CRYPTO_ALG_INTERNAL).
315          */
316         if (!((type | mask) & CRYPTO_ALG_INTERNAL))
317                 mask |= CRYPTO_ALG_INTERNAL;
318
319         larval = crypto_larval_lookup(name, type, mask);
320         if (IS_ERR(larval) || !crypto_is_larval(larval))
321                 return larval;
322
323         ok = crypto_probing_notify(CRYPTO_MSG_ALG_REQUEST, larval);
324
325         if (ok == NOTIFY_STOP)
326                 alg = crypto_larval_wait(larval);
327         else {
328                 crypto_mod_put(larval);
329                 alg = ERR_PTR(-ENOENT);
330         }
331         crypto_larval_kill(larval);
332         return alg;
333 }
334 EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
335
336 static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
337 {
338         const struct crypto_type *type_obj = tfm->__crt_alg->cra_type;
339
340         if (type_obj)
341                 return type_obj->init(tfm, type, mask);
342         return 0;
343 }
344
345 static void crypto_exit_ops(struct crypto_tfm *tfm)
346 {
347         const struct crypto_type *type = tfm->__crt_alg->cra_type;
348
349         if (type && tfm->exit)
350                 tfm->exit(tfm);
351 }
352
353 static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask)
354 {
355         const struct crypto_type *type_obj = alg->cra_type;
356         unsigned int len;
357
358         len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
359         if (type_obj)
360                 return len + type_obj->ctxsize(alg, type, mask);
361
362         switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
363         default:
364                 BUG();
365
366         case CRYPTO_ALG_TYPE_CIPHER:
367                 len += crypto_cipher_ctxsize(alg);
368                 break;
369
370         case CRYPTO_ALG_TYPE_COMPRESS:
371                 len += crypto_compress_ctxsize(alg);
372                 break;
373         }
374
375         return len;
376 }
377
378 void crypto_shoot_alg(struct crypto_alg *alg)
379 {
380         down_write(&crypto_alg_sem);
381         alg->cra_flags |= CRYPTO_ALG_DYING;
382         up_write(&crypto_alg_sem);
383 }
384 EXPORT_SYMBOL_GPL(crypto_shoot_alg);
385
386 struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
387                                       u32 mask)
388 {
389         struct crypto_tfm *tfm = NULL;
390         unsigned int tfm_size;
391         int err = -ENOMEM;
392
393         tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask);
394         tfm = kzalloc(tfm_size, GFP_KERNEL);
395         if (tfm == NULL)
396                 goto out_err;
397
398         tfm->__crt_alg = alg;
399
400         err = crypto_init_ops(tfm, type, mask);
401         if (err)
402                 goto out_free_tfm;
403
404         if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
405                 goto cra_init_failed;
406
407         goto out;
408
409 cra_init_failed:
410         crypto_exit_ops(tfm);
411 out_free_tfm:
412         if (err == -EAGAIN)
413                 crypto_shoot_alg(alg);
414         kfree(tfm);
415 out_err:
416         tfm = ERR_PTR(err);
417 out:
418         return tfm;
419 }
420 EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);
421
422 /*
423  *      crypto_alloc_base - Locate algorithm and allocate transform
424  *      @alg_name: Name of algorithm
425  *      @type: Type of algorithm
426  *      @mask: Mask for type comparison
427  *
428  *      This function should not be used by new algorithm types.
429  *      Please use crypto_alloc_tfm instead.
430  *
431  *      crypto_alloc_base() will first attempt to locate an already loaded
432  *      algorithm.  If that fails and the kernel supports dynamically loadable
433  *      modules, it will then attempt to load a module of the same name or
434  *      alias.  If that fails it will send a query to any loaded crypto manager
435  *      to construct an algorithm on the fly.  A refcount is grabbed on the
436  *      algorithm which is then associated with the new transform.
437  *
438  *      The returned transform is of a non-determinate type.  Most people
439  *      should use one of the more specific allocation functions such as
440  *      crypto_alloc_skcipher().
441  *
442  *      In case of error the return value is an error pointer.
443  */
444 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
445 {
446         struct crypto_tfm *tfm;
447         int err;
448
449         for (;;) {
450                 struct crypto_alg *alg;
451
452                 alg = crypto_alg_mod_lookup(alg_name, type, mask);
453                 if (IS_ERR(alg)) {
454                         err = PTR_ERR(alg);
455                         goto err;
456                 }
457
458                 tfm = __crypto_alloc_tfm(alg, type, mask);
459                 if (!IS_ERR(tfm))
460                         return tfm;
461
462                 crypto_mod_put(alg);
463                 err = PTR_ERR(tfm);
464
465 err:
466                 if (err != -EAGAIN)
467                         break;
468                 if (fatal_signal_pending(current)) {
469                         err = -EINTR;
470                         break;
471                 }
472         }
473
474         return ERR_PTR(err);
475 }
476 EXPORT_SYMBOL_GPL(crypto_alloc_base);
477
478 void *crypto_create_tfm_node(struct crypto_alg *alg,
479                         const struct crypto_type *frontend,
480                         int node)
481 {
482         char *mem;
483         struct crypto_tfm *tfm = NULL;
484         unsigned int tfmsize;
485         unsigned int total;
486         int err = -ENOMEM;
487
488         tfmsize = frontend->tfmsize;
489         total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
490
491         mem = kzalloc_node(total, GFP_KERNEL, node);
492         if (mem == NULL)
493                 goto out_err;
494
495         tfm = (struct crypto_tfm *)(mem + tfmsize);
496         tfm->__crt_alg = alg;
497         tfm->node = node;
498
499         err = frontend->init_tfm(tfm);
500         if (err)
501                 goto out_free_tfm;
502
503         if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
504                 goto cra_init_failed;
505
506         goto out;
507
508 cra_init_failed:
509         crypto_exit_ops(tfm);
510 out_free_tfm:
511         if (err == -EAGAIN)
512                 crypto_shoot_alg(alg);
513         kfree(mem);
514 out_err:
515         mem = ERR_PTR(err);
516 out:
517         return mem;
518 }
519 EXPORT_SYMBOL_GPL(crypto_create_tfm_node);
520
521 struct crypto_alg *crypto_find_alg(const char *alg_name,
522                                    const struct crypto_type *frontend,
523                                    u32 type, u32 mask)
524 {
525         if (frontend) {
526                 type &= frontend->maskclear;
527                 mask &= frontend->maskclear;
528                 type |= frontend->type;
529                 mask |= frontend->maskset;
530         }
531
532         return crypto_alg_mod_lookup(alg_name, type, mask);
533 }
534 EXPORT_SYMBOL_GPL(crypto_find_alg);
535
536 /*
537  *      crypto_alloc_tfm_node - Locate algorithm and allocate transform
538  *      @alg_name: Name of algorithm
539  *      @frontend: Frontend algorithm type
540  *      @type: Type of algorithm
541  *      @mask: Mask for type comparison
542  *      @node: NUMA node in which users desire to put requests, if node is
543  *              NUMA_NO_NODE, it means users have no special requirement.
544  *
545  *      crypto_alloc_tfm() will first attempt to locate an already loaded
546  *      algorithm.  If that fails and the kernel supports dynamically loadable
547  *      modules, it will then attempt to load a module of the same name or
548  *      alias.  If that fails it will send a query to any loaded crypto manager
549  *      to construct an algorithm on the fly.  A refcount is grabbed on the
550  *      algorithm which is then associated with the new transform.
551  *
552  *      The returned transform is of a non-determinate type.  Most people
553  *      should use one of the more specific allocation functions such as
554  *      crypto_alloc_skcipher().
555  *
556  *      In case of error the return value is an error pointer.
557  */
558
559 void *crypto_alloc_tfm_node(const char *alg_name,
560                        const struct crypto_type *frontend, u32 type, u32 mask,
561                        int node)
562 {
563         void *tfm;
564         int err;
565
566         for (;;) {
567                 struct crypto_alg *alg;
568
569                 alg = crypto_find_alg(alg_name, frontend, type, mask);
570                 if (IS_ERR(alg)) {
571                         err = PTR_ERR(alg);
572                         goto err;
573                 }
574
575                 tfm = crypto_create_tfm_node(alg, frontend, node);
576                 if (!IS_ERR(tfm))
577                         return tfm;
578
579                 crypto_mod_put(alg);
580                 err = PTR_ERR(tfm);
581
582 err:
583                 if (err != -EAGAIN)
584                         break;
585                 if (fatal_signal_pending(current)) {
586                         err = -EINTR;
587                         break;
588                 }
589         }
590
591         return ERR_PTR(err);
592 }
593 EXPORT_SYMBOL_GPL(crypto_alloc_tfm_node);
594
595 /*
596  *      crypto_destroy_tfm - Free crypto transform
597  *      @mem: Start of tfm slab
598  *      @tfm: Transform to free
599  *
600  *      This function frees up the transform and any associated resources,
601  *      then drops the refcount on the associated algorithm.
602  */
603 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
604 {
605         struct crypto_alg *alg;
606
607         if (IS_ERR_OR_NULL(mem))
608                 return;
609
610         alg = tfm->__crt_alg;
611
612         if (!tfm->exit && alg->cra_exit)
613                 alg->cra_exit(tfm);
614         crypto_exit_ops(tfm);
615         crypto_mod_put(alg);
616         kfree_sensitive(mem);
617 }
618 EXPORT_SYMBOL_GPL(crypto_destroy_tfm);
619
620 int crypto_has_alg(const char *name, u32 type, u32 mask)
621 {
622         int ret = 0;
623         struct crypto_alg *alg = crypto_alg_mod_lookup(name, type, mask);
624
625         if (!IS_ERR(alg)) {
626                 crypto_mod_put(alg);
627                 ret = 1;
628         }
629
630         return ret;
631 }
632 EXPORT_SYMBOL_GPL(crypto_has_alg);
633
634 void crypto_req_done(struct crypto_async_request *req, int err)
635 {
636         struct crypto_wait *wait = req->data;
637
638         if (err == -EINPROGRESS)
639                 return;
640
641         wait->err = err;
642         complete(&wait->completion);
643 }
644 EXPORT_SYMBOL_GPL(crypto_req_done);
645
646 MODULE_DESCRIPTION("Cryptographic core API");
647 MODULE_LICENSE("GPL");
648 MODULE_SOFTDEP("pre: cryptomgr");