blob: b022702f643672ea6ad2873bb1b95b5bc828ade2 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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)
Herbert Xu5cb1454b2005-11-05 16:58:14 +11007 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
John Anthony Kazos Jr991d17402007-10-19 23:06:17 +020010 * and Nettle, by Niels Möller.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
Jesper Juhla61cc442005-07-06 13:54:31 -070012
Herbert Xu6bfd4802006-09-21 11:39:29 +100013#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/errno.h>
Herbert Xuadad5562021-09-17 08:26:19 +080015#include <linux/jump_label.h>
Herbert Xu5cb1454b2005-11-05 16:58:14 +110016#include <linux/kernel.h>
Adrian Bunk176c3652005-07-06 13:53:09 -070017#include <linux/kmod.h>
Herbert Xu2b8c19d2006-09-21 11:31:44 +100018#include <linux/module.h>
Herbert Xu28259822006-08-06 21:23:26 +100019#include <linux/param.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010020#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/slab.h>
Herbert Xu5cb1454b2005-11-05 16:58:14 +110022#include <linux/string.h>
Gilad Ben-Yossefada69a12017-10-18 08:00:38 +010023#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "internal.h"
25
26LIST_HEAD(crypto_alg_list);
Herbert Xucce9e062006-08-21 21:08:13 +100027EXPORT_SYMBOL_GPL(crypto_alg_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028DECLARE_RWSEM(crypto_alg_sem);
Herbert Xucce9e062006-08-21 21:08:13 +100029EXPORT_SYMBOL_GPL(crypto_alg_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Herbert Xu28259822006-08-06 21:23:26 +100031BLOCKING_NOTIFIER_HEAD(crypto_chain);
32EXPORT_SYMBOL_GPL(crypto_chain);
33
Eric Biggers1e15da52022-11-13 16:12:35 -080034#ifndef CONFIG_CRYPTO_MANAGER_DISABLE_TESTS
35DEFINE_STATIC_KEY_FALSE(__crypto_boot_test_finished);
36EXPORT_SYMBOL_GPL(__crypto_boot_test_finished);
37#endif
Herbert Xuadad5562021-09-17 08:26:19 +080038
Herbert Xu77dbd7a2013-09-08 14:33:50 +100039static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);
40
Herbert Xu28259822006-08-06 21:23:26 +100041struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
Herbert Xu6521f302006-08-06 20:28:44 +100042{
43 return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
44}
Herbert Xu28259822006-08-06 21:23:26 +100045EXPORT_SYMBOL_GPL(crypto_mod_get);
Herbert Xu6521f302006-08-06 20:28:44 +100046
Herbert Xu28259822006-08-06 21:23:26 +100047void crypto_mod_put(struct crypto_alg *alg)
Herbert Xu6521f302006-08-06 20:28:44 +100048{
Herbert Xuda7cd592007-05-19 14:51:00 +100049 struct module *module = alg->cra_module;
50
Herbert Xu6521f302006-08-06 20:28:44 +100051 crypto_alg_put(alg);
Herbert Xuda7cd592007-05-19 14:51:00 +100052 module_put(module);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
Herbert Xu28259822006-08-06 21:23:26 +100054EXPORT_SYMBOL_GPL(crypto_mod_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Herbert Xuc51b6c82008-08-04 11:44:59 +080056static struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type,
57 u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 struct crypto_alg *q, *alg = NULL;
Herbert Xu28259822006-08-06 21:23:26 +100060 int best = -2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xu5cb1454b2005-11-05 16:58:14 +110063 int exact, fuzzy;
64
Herbert Xu6bfd4802006-09-21 11:39:29 +100065 if (crypto_is_moribund(q))
66 continue;
67
Herbert Xu492e2b62006-09-21 11:35:17 +100068 if ((q->cra_flags ^ type) & mask)
69 continue;
70
71 if (crypto_is_larval(q) &&
Herbert Xu73d38642008-08-03 21:15:23 +080072 !crypto_is_test_larval((struct crypto_larval *)q) &&
Herbert Xu492e2b62006-09-21 11:35:17 +100073 ((struct crypto_larval *)q)->mask != mask)
74 continue;
75
Herbert Xu5cb1454b2005-11-05 16:58:14 +110076 exact = !strcmp(q->cra_driver_name, name);
77 fuzzy = !strcmp(q->cra_name, name);
78 if (!exact && !(fuzzy && q->cra_priority > best))
79 continue;
80
Herbert Xu72fa4912006-05-28 09:05:24 +100081 if (unlikely(!crypto_mod_get(q)))
Herbert Xu5cb1454b2005-11-05 16:58:14 +110082 continue;
83
84 best = q->cra_priority;
85 if (alg)
Herbert Xu72fa4912006-05-28 09:05:24 +100086 crypto_mod_put(alg);
Herbert Xu5cb1454b2005-11-05 16:58:14 +110087 alg = q;
88
89 if (exact)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Herbert Xu28259822006-08-06 21:23:26 +100092
93 return alg;
94}
Herbert Xu28259822006-08-06 21:23:26 +100095
96static void crypto_larval_destroy(struct crypto_alg *alg)
97{
98 struct crypto_larval *larval = (void *)alg;
99
100 BUG_ON(!crypto_is_larval(alg));
Herbert Xu2bbb3372019-12-11 10:50:11 +0800101 if (!IS_ERR_OR_NULL(larval->adult))
Herbert Xu28259822006-08-06 21:23:26 +1000102 crypto_mod_put(larval->adult);
103 kfree(larval);
104}
105
Herbert Xu73d38642008-08-03 21:15:23 +0800106struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask)
Herbert Xu28259822006-08-06 21:23:26 +1000107{
Herbert Xu28259822006-08-06 21:23:26 +1000108 struct crypto_larval *larval;
109
110 larval = kzalloc(sizeof(*larval), GFP_KERNEL);
111 if (!larval)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000112 return ERR_PTR(-ENOMEM);
Herbert Xu28259822006-08-06 21:23:26 +1000113
Herbert Xu492e2b62006-09-21 11:35:17 +1000114 larval->mask = mask;
115 larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type;
Herbert Xu28259822006-08-06 21:23:26 +1000116 larval->alg.cra_priority = -1;
117 larval->alg.cra_destroy = crypto_larval_destroy;
118
Wolfram Sangdd4f8ee2022-08-18 22:59:54 +0200119 strscpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
Herbert Xu28259822006-08-06 21:23:26 +1000120 init_completion(&larval->completion);
121
Herbert Xu73d38642008-08-03 21:15:23 +0800122 return larval;
123}
124EXPORT_SYMBOL_GPL(crypto_larval_alloc);
125
126static 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
Eric Biggersce8614a2017-12-29 10:00:46 -0600136 refcount_set(&larval->alg.cra_refcnt, 2);
Herbert Xu73d38642008-08-03 21:15:23 +0800137
Herbert Xu28259822006-08-06 21:23:26 +1000138 down_write(&crypto_alg_sem);
Herbert Xu492e2b62006-09-21 11:35:17 +1000139 alg = __crypto_alg_lookup(name, type, mask);
Herbert Xu28259822006-08-06 21:23:26 +1000140 if (!alg) {
141 alg = &larval->alg;
142 list_add(&alg->cra_list, &crypto_alg_list);
143 }
144 up_write(&crypto_alg_sem);
145
Herbert Xu77dbd7a2013-09-08 14:33:50 +1000146 if (alg != &larval->alg) {
Herbert Xu28259822006-08-06 21:23:26 +1000147 kfree(larval);
Herbert Xu77dbd7a2013-09-08 14:33:50 +1000148 if (crypto_is_larval(alg))
149 alg = crypto_larval_wait(alg);
150 }
Herbert Xu28259822006-08-06 21:23:26 +1000151
152 return alg;
153}
154
Herbert Xub9c55aa2007-12-04 12:46:48 +1100155void crypto_larval_kill(struct crypto_alg *alg)
Herbert Xu28259822006-08-06 21:23:26 +1000156{
157 struct crypto_larval *larval = (void *)alg;
158
159 down_write(&crypto_alg_sem);
160 list_del(&alg->cra_list);
161 up_write(&crypto_alg_sem);
Herbert Xufe3c5202007-05-19 17:51:40 +1000162 complete_all(&larval->completion);
Herbert Xu28259822006-08-06 21:23:26 +1000163 crypto_alg_put(alg);
164}
Herbert Xub9c55aa2007-12-04 12:46:48 +1100165EXPORT_SYMBOL_GPL(crypto_larval_kill);
Herbert Xu28259822006-08-06 21:23:26 +1000166
Herbert Xuadad5562021-09-17 08:26:19 +0800167void crypto_wait_for_test(struct crypto_larval *larval)
168{
169 int err;
170
171 err = crypto_probing_notify(CRYPTO_MSG_ALG_REGISTER, larval->adult);
Herbert Xucad439f2021-10-19 21:28:02 +0800172 if (WARN_ON_ONCE(err != NOTIFY_STOP))
173 goto out;
Herbert Xuadad5562021-09-17 08:26:19 +0800174
175 err = wait_for_completion_killable(&larval->completion);
176 WARN_ON(err);
Herbert Xuadad5562021-09-17 08:26:19 +0800177out:
178 crypto_larval_kill(&larval->alg);
179}
180EXPORT_SYMBOL_GPL(crypto_wait_for_test);
181
182static void crypto_start_test(struct crypto_larval *larval)
183{
184 if (!crypto_is_test_larval(larval))
185 return;
186
187 if (larval->test_started)
188 return;
189
190 down_write(&crypto_alg_sem);
191 if (larval->test_started) {
192 up_write(&crypto_alg_sem);
193 return;
194 }
195
196 larval->test_started = true;
197 up_write(&crypto_alg_sem);
198
199 crypto_wait_for_test(larval);
200}
201
Herbert Xu28259822006-08-06 21:23:26 +1000202static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
203{
204 struct crypto_larval *larval = (void *)alg;
Herbert Xu73d38642008-08-03 21:15:23 +0800205 long timeout;
Herbert Xu28259822006-08-06 21:23:26 +1000206
Eric Biggers1e15da52022-11-13 16:12:35 -0800207 if (!crypto_boot_test_finished())
Herbert Xuadad5562021-09-17 08:26:19 +0800208 crypto_start_test(larval);
209
Herbert Xu3fc89ad2015-10-19 18:23:57 +0800210 timeout = wait_for_completion_killable_timeout(
Herbert Xu73d38642008-08-03 21:15:23 +0800211 &larval->completion, 60 * HZ);
212
Herbert Xu28259822006-08-06 21:23:26 +1000213 alg = larval->adult;
Herbert Xu73d38642008-08-03 21:15:23 +0800214 if (timeout < 0)
215 alg = ERR_PTR(-EINTR);
216 else if (!timeout)
217 alg = ERR_PTR(-ETIMEDOUT);
218 else if (!alg)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000219 alg = ERR_PTR(-ENOENT);
Herbert Xu2bbb3372019-12-11 10:50:11 +0800220 else if (IS_ERR(alg))
221 ;
Herbert Xu73d38642008-08-03 21:15:23 +0800222 else if (crypto_is_test_larval(larval) &&
223 !(alg->cra_flags & CRYPTO_ALG_TESTED))
224 alg = ERR_PTR(-EAGAIN);
Nicolai Stanged6097b82022-02-21 13:10:58 +0100225 else if (alg->cra_flags & CRYPTO_ALG_FIPS_INTERNAL)
226 alg = ERR_PTR(-EAGAIN);
Herbert Xu73d38642008-08-03 21:15:23 +0800227 else if (!crypto_mod_get(alg))
228 alg = ERR_PTR(-EAGAIN);
Herbert Xu28259822006-08-06 21:23:26 +1000229 crypto_mod_put(&larval->alg);
230
231 return alg;
232}
233
Herbert Xu3ca1e992018-03-20 08:05:39 +0800234static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
235 u32 mask)
Herbert Xu28259822006-08-06 21:23:26 +1000236{
Nicolai Stanged6097b82022-02-21 13:10:58 +0100237 const u32 fips = CRYPTO_ALG_FIPS_INTERNAL;
Herbert Xu28259822006-08-06 21:23:26 +1000238 struct crypto_alg *alg;
Herbert Xueb02c382018-03-20 15:52:45 +0800239 u32 test = 0;
240
241 if (!((type | mask) & CRYPTO_ALG_TESTED))
242 test |= CRYPTO_ALG_TESTED;
Herbert Xu28259822006-08-06 21:23:26 +1000243
Herbert Xu28259822006-08-06 21:23:26 +1000244 down_read(&crypto_alg_sem);
Nicolai Stanged6097b82022-02-21 13:10:58 +0100245 alg = __crypto_alg_lookup(name, (type | test) & ~fips,
246 (mask | test) & ~fips);
247 if (alg) {
248 if (((type | mask) ^ fips) & fips)
249 mask |= fips;
250 mask &= fips;
251
252 if (!crypto_is_larval(alg) &&
253 ((type ^ alg->cra_flags) & mask)) {
254 /* Algorithm is disallowed in FIPS mode. */
255 crypto_mod_put(alg);
256 alg = ERR_PTR(-ENOENT);
257 }
258 } else if (test) {
Eric Biggersb346e492018-04-16 16:59:13 -0700259 alg = __crypto_alg_lookup(name, type, mask);
260 if (alg && !crypto_is_larval(alg)) {
261 /* Test failed */
262 crypto_mod_put(alg);
263 alg = ERR_PTR(-ELIBBAD);
264 }
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 up_read(&crypto_alg_sem);
Herbert Xu28259822006-08-06 21:23:26 +1000267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return alg;
269}
270
Eric Biggerscadc9ab2017-12-07 10:55:59 -0800271static struct crypto_alg *crypto_larval_lookup(const char *name, u32 type,
272 u32 mask)
Adrian Bunk176c3652005-07-06 13:53:09 -0700273{
Herbert Xu28259822006-08-06 21:23:26 +1000274 struct crypto_alg *alg;
Herbert Xu28259822006-08-06 21:23:26 +1000275
Herbert Xu6bfd4802006-09-21 11:39:29 +1000276 if (!name)
277 return ERR_PTR(-ENOENT);
278
Herbert Xu430b4412016-11-22 20:08:21 +0800279 type &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000280 mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
Herbert Xu492e2b62006-09-21 11:35:17 +1000281
Herbert Xua760a662009-02-26 14:06:31 +0800282 alg = crypto_alg_lookup(name, type, mask);
Matthew Garrette2861fa2018-06-08 14:57:42 -0700283 if (!alg && !(mask & CRYPTO_NOLOAD)) {
Kees Cook5d26a102014-11-20 17:05:53 -0800284 request_module("crypto-%s", name);
Herbert Xua760a662009-02-26 14:06:31 +0800285
Herbert Xu37fc3342009-04-21 13:27:16 +0800286 if (!((type ^ CRYPTO_ALG_NEED_FALLBACK) & mask &
Alex Riesenaa07a692009-06-02 14:13:14 +1000287 CRYPTO_ALG_NEED_FALLBACK))
Kees Cook5d26a102014-11-20 17:05:53 -0800288 request_module("crypto-%s-all", name);
Herbert Xua760a662009-02-26 14:06:31 +0800289
290 alg = crypto_alg_lookup(name, type, mask);
291 }
292
Herbert Xueb02c382018-03-20 15:52:45 +0800293 if (!IS_ERR_OR_NULL(alg) && crypto_is_larval(alg))
294 alg = crypto_larval_wait(alg);
295 else if (!alg)
296 alg = crypto_larval_add(name, type, mask);
Herbert Xu28259822006-08-06 21:23:26 +1000297
Herbert Xueb02c382018-03-20 15:52:45 +0800298 return alg;
Herbert Xub9c55aa2007-12-04 12:46:48 +1100299}
Herbert Xub9c55aa2007-12-04 12:46:48 +1100300
Herbert Xu73d38642008-08-03 21:15:23 +0800301int crypto_probing_notify(unsigned long val, void *v)
302{
303 int ok;
304
305 ok = blocking_notifier_call_chain(&crypto_chain, val, v);
306 if (ok == NOTIFY_DONE) {
307 request_module("cryptomgr");
308 ok = blocking_notifier_call_chain(&crypto_chain, val, v);
309 }
310
311 return ok;
312}
313EXPORT_SYMBOL_GPL(crypto_probing_notify);
314
Herbert Xub9c55aa2007-12-04 12:46:48 +1100315struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
316{
317 struct crypto_alg *alg;
318 struct crypto_alg *larval;
319 int ok;
320
Stephan Mueller06ca7f62015-03-30 21:55:52 +0200321 /*
322 * If the internal flag is set for a cipher, require a caller to
Jason Wangbc9d6da2022-08-11 20:13:49 +0800323 * invoke the cipher with the internal flag to use that cipher.
Stephan Mueller06ca7f62015-03-30 21:55:52 +0200324 * Also, if a caller wants to allocate a cipher that may or may
325 * not be an internal cipher, use type | CRYPTO_ALG_INTERNAL and
326 * !(mask & CRYPTO_ALG_INTERNAL).
327 */
328 if (!((type | mask) & CRYPTO_ALG_INTERNAL))
329 mask |= CRYPTO_ALG_INTERNAL;
330
Herbert Xub9c55aa2007-12-04 12:46:48 +1100331 larval = crypto_larval_lookup(name, type, mask);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000332 if (IS_ERR(larval) || !crypto_is_larval(larval))
Herbert Xu28259822006-08-06 21:23:26 +1000333 return larval;
334
Herbert Xu73d38642008-08-03 21:15:23 +0800335 ok = crypto_probing_notify(CRYPTO_MSG_ALG_REQUEST, larval);
Herbert Xu2b8c19d2006-09-21 11:31:44 +1000336
337 if (ok == NOTIFY_STOP)
Herbert Xu28259822006-08-06 21:23:26 +1000338 alg = crypto_larval_wait(larval);
339 else {
340 crypto_mod_put(larval);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000341 alg = ERR_PTR(-ENOENT);
Herbert Xu28259822006-08-06 21:23:26 +1000342 }
343 crypto_larval_kill(larval);
344 return alg;
Adrian Bunk176c3652005-07-06 13:53:09 -0700345}
Herbert Xu492e2b62006-09-21 11:35:17 +1000346EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
Adrian Bunk176c3652005-07-06 13:53:09 -0700347
Herbert Xu27d2a332007-01-24 20:50:26 +1100348static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Herbert Xu27d2a332007-01-24 20:50:26 +1100350 const struct crypto_type *type_obj = tfm->__crt_alg->cra_type;
Herbert Xue853c3cf2006-08-22 00:06:54 +1000351
Herbert Xu27d2a332007-01-24 20:50:26 +1100352 if (type_obj)
353 return type_obj->init(tfm, type, mask);
Eric Biggerse8cfed52019-12-02 13:42:30 -0800354 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357static void crypto_exit_ops(struct crypto_tfm *tfm)
358{
Herbert Xue853c3cf2006-08-22 00:06:54 +1000359 const struct crypto_type *type = tfm->__crt_alg->cra_type;
360
Eric Biggers9c8ae172016-10-07 14:13:35 -0700361 if (type && tfm->exit)
362 tfm->exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363}
364
Herbert Xu27d2a332007-01-24 20:50:26 +1100365static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask)
Herbert Xufbdae9f2005-07-06 13:53:29 -0700366{
Herbert Xu27d2a332007-01-24 20:50:26 +1100367 const struct crypto_type *type_obj = alg->cra_type;
Herbert Xufbdae9f2005-07-06 13:53:29 -0700368 unsigned int len;
369
Herbert Xue853c3cf2006-08-22 00:06:54 +1000370 len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
Herbert Xu27d2a332007-01-24 20:50:26 +1100371 if (type_obj)
372 return len + type_obj->ctxsize(alg, type, mask);
Herbert Xue853c3cf2006-08-22 00:06:54 +1000373
Herbert Xufbdae9f2005-07-06 13:53:29 -0700374 switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
375 default:
376 BUG();
377
378 case CRYPTO_ALG_TYPE_CIPHER:
Herbert Xuf1ddcaf2007-01-27 10:05:15 +1100379 len += crypto_cipher_ctxsize(alg);
Herbert Xufbdae9f2005-07-06 13:53:29 -0700380 break;
Herbert Xu6941c3a2009-07-12 13:58:04 +0800381
Herbert Xufbdae9f2005-07-06 13:53:29 -0700382 case CRYPTO_ALG_TYPE_COMPRESS:
Herbert Xuf1ddcaf2007-01-27 10:05:15 +1100383 len += crypto_compress_ctxsize(alg);
Herbert Xufbdae9f2005-07-06 13:53:29 -0700384 break;
385 }
386
Herbert Xue853c3cf2006-08-22 00:06:54 +1000387 return len;
Herbert Xufbdae9f2005-07-06 13:53:29 -0700388}
389
Herbert Xu66035232020-04-10 16:09:42 +1000390void crypto_shoot_alg(struct crypto_alg *alg)
Herbert Xu6bfd4802006-09-21 11:39:29 +1000391{
392 down_write(&crypto_alg_sem);
393 alg->cra_flags |= CRYPTO_ALG_DYING;
394 up_write(&crypto_alg_sem);
395}
Herbert Xu66035232020-04-10 16:09:42 +1000396EXPORT_SYMBOL_GPL(crypto_shoot_alg);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000397
Herbert Xu27d2a332007-01-24 20:50:26 +1100398struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
399 u32 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 struct crypto_tfm *tfm = NULL;
Herbert Xufbdae9f2005-07-06 13:53:29 -0700402 unsigned int tfm_size;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000403 int err = -ENOMEM;
Herbert Xufbdae9f2005-07-06 13:53:29 -0700404
Herbert Xu27d2a332007-01-24 20:50:26 +1100405 tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask);
Eric Sesterhennbbeb563f2006-03-06 21:42:07 +1100406 tfm = kzalloc(tfm_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (tfm == NULL)
Akinobu Mita9765d262006-10-11 22:29:51 +1000408 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 tfm->__crt_alg = alg;
Herbert Xu6bfd4802006-09-21 11:39:29 +1000411
Herbert Xu27d2a332007-01-24 20:50:26 +1100412 err = crypto_init_ops(tfm, type, mask);
Herbert Xu6bfd4802006-09-21 11:39:29 +1000413 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 goto out_free_tfm;
Herbert Xuc7fc0592006-05-24 13:02:26 +1000415
Herbert Xu4a779482008-09-13 18:19:03 -0700416 if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
Herbert Xuc7fc0592006-05-24 13:02:26 +1000417 goto cra_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 goto out;
420
Herbert Xuc7fc0592006-05-24 13:02:26 +1000421cra_init_failed:
422 crypto_exit_ops(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423out_free_tfm:
Herbert Xu4a779482008-09-13 18:19:03 -0700424 if (err == -EAGAIN)
425 crypto_shoot_alg(alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 kfree(tfm);
Akinobu Mita9765d262006-10-11 22:29:51 +1000427out_err:
Herbert Xu6bfd4802006-09-21 11:39:29 +1000428 tfm = ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429out:
430 return tfm;
431}
Herbert Xu6bfd4802006-09-21 11:39:29 +1000432EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);
433
Herbert Xu6d7d6842006-07-30 11:53:01 +1000434/*
435 * crypto_alloc_base - Locate algorithm and allocate transform
436 * @alg_name: Name of algorithm
437 * @type: Type of algorithm
438 * @mask: Mask for type comparison
439 *
Herbert Xu7b0bac62008-09-21 06:52:53 +0900440 * This function should not be used by new algorithm types.
Cristian Stoicafd1a1902013-06-28 15:56:20 +0300441 * Please use crypto_alloc_tfm instead.
Herbert Xu7b0bac62008-09-21 06:52:53 +0900442 *
Herbert Xu6d7d6842006-07-30 11:53:01 +1000443 * crypto_alloc_base() will first attempt to locate an already loaded
444 * algorithm. If that fails and the kernel supports dynamically loadable
445 * modules, it will then attempt to load a module of the same name or
446 * alias. If that fails it will send a query to any loaded crypto manager
447 * to construct an algorithm on the fly. A refcount is grabbed on the
448 * algorithm which is then associated with the new transform.
449 *
450 * The returned transform is of a non-determinate type. Most people
451 * should use one of the more specific allocation functions such as
Eric Biggersc65058b2019-10-25 12:41:12 -0700452 * crypto_alloc_skcipher().
Herbert Xu6d7d6842006-07-30 11:53:01 +1000453 *
454 * In case of error the return value is an error pointer.
455 */
456struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
457{
458 struct crypto_tfm *tfm;
459 int err;
460
461 for (;;) {
462 struct crypto_alg *alg;
463
464 alg = crypto_alg_mod_lookup(alg_name, type, mask);
Akinobu Mita9765d262006-10-11 22:29:51 +1000465 if (IS_ERR(alg)) {
466 err = PTR_ERR(alg);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000467 goto err;
Akinobu Mita9765d262006-10-11 22:29:51 +1000468 }
Herbert Xu6d7d6842006-07-30 11:53:01 +1000469
Herbert Xu27d2a332007-01-24 20:50:26 +1100470 tfm = __crypto_alloc_tfm(alg, type, mask);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000471 if (!IS_ERR(tfm))
Akinobu Mita9765d262006-10-11 22:29:51 +1000472 return tfm;
Herbert Xu6d7d6842006-07-30 11:53:01 +1000473
474 crypto_mod_put(alg);
475 err = PTR_ERR(tfm);
476
477err:
478 if (err != -EAGAIN)
479 break;
Herbert Xu3fc89ad2015-10-19 18:23:57 +0800480 if (fatal_signal_pending(current)) {
Herbert Xu6d7d6842006-07-30 11:53:01 +1000481 err = -EINTR;
482 break;
483 }
Akinobu Mita9765d262006-10-11 22:29:51 +1000484 }
Herbert Xu6d7d6842006-07-30 11:53:01 +1000485
Akinobu Mita9765d262006-10-11 22:29:51 +1000486 return ERR_PTR(err);
Herbert Xu6d7d6842006-07-30 11:53:01 +1000487}
488EXPORT_SYMBOL_GPL(crypto_alloc_base);
Herbert Xu7b0bac62008-09-21 06:52:53 +0900489
Barry Song7bc13b52020-07-05 21:18:58 +1200490void *crypto_create_tfm_node(struct crypto_alg *alg,
491 const struct crypto_type *frontend,
492 int node)
Herbert Xu7b0bac62008-09-21 06:52:53 +0900493{
494 char *mem;
495 struct crypto_tfm *tfm = NULL;
496 unsigned int tfmsize;
497 unsigned int total;
498 int err = -ENOMEM;
499
500 tfmsize = frontend->tfmsize;
Herbert Xu2ca33da2009-07-13 20:46:25 +0800501 total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
Herbert Xu7b0bac62008-09-21 06:52:53 +0900502
Barry Song7bc13b52020-07-05 21:18:58 +1200503 mem = kzalloc_node(total, GFP_KERNEL, node);
Herbert Xu7b0bac62008-09-21 06:52:53 +0900504 if (mem == NULL)
505 goto out_err;
506
507 tfm = (struct crypto_tfm *)(mem + tfmsize);
508 tfm->__crt_alg = alg;
Barry Song7bc13b52020-07-05 21:18:58 +1200509 tfm->node = node;
Herbert Xu7b0bac62008-09-21 06:52:53 +0900510
Herbert Xu2ca33da2009-07-13 20:46:25 +0800511 err = frontend->init_tfm(tfm);
Herbert Xu7b0bac62008-09-21 06:52:53 +0900512 if (err)
513 goto out_free_tfm;
514
515 if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
516 goto cra_init_failed;
517
518 goto out;
519
520cra_init_failed:
521 crypto_exit_ops(tfm);
522out_free_tfm:
523 if (err == -EAGAIN)
524 crypto_shoot_alg(alg);
525 kfree(mem);
526out_err:
Herbert Xu3f683d62009-02-18 16:56:59 +0800527 mem = ERR_PTR(err);
Herbert Xu7b0bac62008-09-21 06:52:53 +0900528out:
Herbert Xu3f683d62009-02-18 16:56:59 +0800529 return mem;
Herbert Xu7b0bac62008-09-21 06:52:53 +0900530}
Barry Song7bc13b52020-07-05 21:18:58 +1200531EXPORT_SYMBOL_GPL(crypto_create_tfm_node);
Herbert Xu7b0bac62008-09-21 06:52:53 +0900532
Herbert Xud06854f2009-07-08 17:53:16 +0800533struct crypto_alg *crypto_find_alg(const char *alg_name,
534 const struct crypto_type *frontend,
535 u32 type, u32 mask)
536{
Herbert Xud06854f2009-07-08 17:53:16 +0800537 if (frontend) {
538 type &= frontend->maskclear;
539 mask &= frontend->maskclear;
540 type |= frontend->type;
541 mask |= frontend->maskset;
Herbert Xud06854f2009-07-08 17:53:16 +0800542 }
543
Herbert Xu4989d4f2018-03-20 07:41:00 +0800544 return crypto_alg_mod_lookup(alg_name, type, mask);
Herbert Xud06854f2009-07-08 17:53:16 +0800545}
546EXPORT_SYMBOL_GPL(crypto_find_alg);
547
Herbert Xu7b0bac62008-09-21 06:52:53 +0900548/*
Barry Song7bc13b52020-07-05 21:18:58 +1200549 * crypto_alloc_tfm_node - Locate algorithm and allocate transform
Herbert Xu7b0bac62008-09-21 06:52:53 +0900550 * @alg_name: Name of algorithm
551 * @frontend: Frontend algorithm type
552 * @type: Type of algorithm
553 * @mask: Mask for type comparison
Barry Song7bc13b52020-07-05 21:18:58 +1200554 * @node: NUMA node in which users desire to put requests, if node is
555 * NUMA_NO_NODE, it means users have no special requirement.
Herbert Xu7b0bac62008-09-21 06:52:53 +0900556 *
557 * crypto_alloc_tfm() will first attempt to locate an already loaded
558 * algorithm. If that fails and the kernel supports dynamically loadable
559 * modules, it will then attempt to load a module of the same name or
560 * alias. If that fails it will send a query to any loaded crypto manager
561 * to construct an algorithm on the fly. A refcount is grabbed on the
562 * algorithm which is then associated with the new transform.
563 *
564 * The returned transform is of a non-determinate type. Most people
565 * should use one of the more specific allocation functions such as
Eric Biggers0a940d42019-11-29 10:16:48 -0800566 * crypto_alloc_skcipher().
Herbert Xu7b0bac62008-09-21 06:52:53 +0900567 *
568 * In case of error the return value is an error pointer.
569 */
Barry Song7bc13b52020-07-05 21:18:58 +1200570
571void *crypto_alloc_tfm_node(const char *alg_name,
572 const struct crypto_type *frontend, u32 type, u32 mask,
573 int node)
Herbert Xu7b0bac62008-09-21 06:52:53 +0900574{
Herbert Xu3f683d62009-02-18 16:56:59 +0800575 void *tfm;
Herbert Xu7b0bac62008-09-21 06:52:53 +0900576 int err;
577
Herbert Xu7b0bac62008-09-21 06:52:53 +0900578 for (;;) {
579 struct crypto_alg *alg;
580
Herbert Xud06854f2009-07-08 17:53:16 +0800581 alg = crypto_find_alg(alg_name, frontend, type, mask);
Herbert Xu7b0bac62008-09-21 06:52:53 +0900582 if (IS_ERR(alg)) {
583 err = PTR_ERR(alg);
584 goto err;
585 }
586
Barry Song7bc13b52020-07-05 21:18:58 +1200587 tfm = crypto_create_tfm_node(alg, frontend, node);
Herbert Xu7b0bac62008-09-21 06:52:53 +0900588 if (!IS_ERR(tfm))
589 return tfm;
590
591 crypto_mod_put(alg);
592 err = PTR_ERR(tfm);
593
594err:
595 if (err != -EAGAIN)
596 break;
Herbert Xu3fc89ad2015-10-19 18:23:57 +0800597 if (fatal_signal_pending(current)) {
Herbert Xu7b0bac62008-09-21 06:52:53 +0900598 err = -EINTR;
599 break;
600 }
601 }
602
603 return ERR_PTR(err);
604}
Barry Song7bc13b52020-07-05 21:18:58 +1200605EXPORT_SYMBOL_GPL(crypto_alloc_tfm_node);
Herbert Xu7b2cd922009-02-05 16:48:24 +1100606
Herbert Xu6d7d6842006-07-30 11:53:01 +1000607/*
Herbert Xu7b2cd922009-02-05 16:48:24 +1100608 * crypto_destroy_tfm - Free crypto transform
609 * @mem: Start of tfm slab
Herbert Xu6d7d6842006-07-30 11:53:01 +1000610 * @tfm: Transform to free
611 *
Herbert Xu7b2cd922009-02-05 16:48:24 +1100612 * This function frees up the transform and any associated resources,
Herbert Xu6d7d6842006-07-30 11:53:01 +1000613 * then drops the refcount on the associated algorithm.
614 */
Herbert Xu7b2cd922009-02-05 16:48:24 +1100615void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Jesper Juhla61cc442005-07-06 13:54:31 -0700617 struct crypto_alg *alg;
Jesper Juhla61cc442005-07-06 13:54:31 -0700618
Ard Biesheuvel83681f22021-03-02 21:33:03 +0100619 if (IS_ERR_OR_NULL(mem))
Jesper Juhla61cc442005-07-06 13:54:31 -0700620 return;
621
622 alg = tfm->__crt_alg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Herbert Xu4a779482008-09-13 18:19:03 -0700624 if (!tfm->exit && alg->cra_exit)
Herbert Xuc7fc0592006-05-24 13:02:26 +1000625 alg->cra_exit(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 crypto_exit_ops(tfm);
Herbert Xu72fa4912006-05-28 09:05:24 +1000627 crypto_mod_put(alg);
Waiman Long453431a2020-08-06 23:18:13 -0700628 kfree_sensitive(mem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
Herbert Xu7b2cd922009-02-05 16:48:24 +1100630EXPORT_SYMBOL_GPL(crypto_destroy_tfm);
Herbert Xufce32d72006-08-26 17:35:45 +1000631
632int crypto_has_alg(const char *name, u32 type, u32 mask)
633{
634 int ret = 0;
635 struct crypto_alg *alg = crypto_alg_mod_lookup(name, type, mask);
Richard Hartmann3d01a332010-02-16 20:26:46 +0800636
Herbert Xufce32d72006-08-26 17:35:45 +1000637 if (!IS_ERR(alg)) {
638 crypto_mod_put(alg);
639 ret = 1;
640 }
Richard Hartmann3d01a332010-02-16 20:26:46 +0800641
Herbert Xufce32d72006-08-26 17:35:45 +1000642 return ret;
643}
644EXPORT_SYMBOL_GPL(crypto_has_alg);
Sebastian Siewiorc3715cb92008-03-30 16:36:09 +0800645
Gilad Ben-Yossefada69a12017-10-18 08:00:38 +0100646void crypto_req_done(struct crypto_async_request *req, int err)
647{
648 struct crypto_wait *wait = req->data;
649
650 if (err == -EINPROGRESS)
651 return;
652
653 wait->err = err;
654 complete(&wait->completion);
655}
656EXPORT_SYMBOL_GPL(crypto_req_done);
657
Sebastian Siewiorc3715cb92008-03-30 16:36:09 +0800658MODULE_DESCRIPTION("Cryptographic core API");
659MODULE_LICENSE("GPL");