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