api.c 17 KB

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