api.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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) && \
  31. !IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS)
  32. DEFINE_STATIC_KEY_FALSE(__crypto_boot_test_finished);
  33. #endif
  34. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg,
  35. u32 type, u32 mask);
  36. static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
  37. u32 mask);
  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. void crypto_mod_put(struct crypto_alg *alg)
  44. {
  45. struct module *module = alg->cra_module;
  46. crypto_alg_put(alg);
  47. module_put(module);
  48. }
  49. EXPORT_SYMBOL_GPL(crypto_mod_put);
  50. static struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type,
  51. u32 mask)
  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(sizeof(*larval), GFP_KERNEL);
  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. case CRYPTO_ALG_TYPE_COMPRESS:
  319. len += crypto_compress_ctxsize(alg);
  320. break;
  321. }
  322. return len;
  323. }
  324. void crypto_shoot_alg(struct crypto_alg *alg)
  325. {
  326. down_write(&crypto_alg_sem);
  327. alg->cra_flags |= CRYPTO_ALG_DYING;
  328. up_write(&crypto_alg_sem);
  329. }
  330. EXPORT_SYMBOL_GPL(crypto_shoot_alg);
  331. struct crypto_tfm *__crypto_alloc_tfmgfp(struct crypto_alg *alg, u32 type,
  332. u32 mask, gfp_t gfp)
  333. {
  334. struct crypto_tfm *tfm;
  335. unsigned int tfm_size;
  336. int err = -ENOMEM;
  337. tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask);
  338. tfm = kzalloc(tfm_size, gfp);
  339. if (tfm == NULL)
  340. goto out_err;
  341. tfm->__crt_alg = alg;
  342. refcount_set(&tfm->refcnt, 1);
  343. if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
  344. goto cra_init_failed;
  345. goto out;
  346. cra_init_failed:
  347. crypto_exit_ops(tfm);
  348. if (err == -EAGAIN)
  349. crypto_shoot_alg(alg);
  350. kfree(tfm);
  351. out_err:
  352. tfm = ERR_PTR(err);
  353. out:
  354. return tfm;
  355. }
  356. EXPORT_SYMBOL_GPL(__crypto_alloc_tfmgfp);
  357. struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
  358. u32 mask)
  359. {
  360. return __crypto_alloc_tfmgfp(alg, type, mask, GFP_KERNEL);
  361. }
  362. EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);
  363. /*
  364. * crypto_alloc_base - Locate algorithm and allocate transform
  365. * @alg_name: Name of algorithm
  366. * @type: Type of algorithm
  367. * @mask: Mask for type comparison
  368. *
  369. * This function should not be used by new algorithm types.
  370. * Please use crypto_alloc_tfm instead.
  371. *
  372. * crypto_alloc_base() will first attempt to locate an already loaded
  373. * algorithm. If that fails and the kernel supports dynamically loadable
  374. * modules, it will then attempt to load a module of the same name or
  375. * alias. If that fails it will send a query to any loaded crypto manager
  376. * to construct an algorithm on the fly. A refcount is grabbed on the
  377. * algorithm which is then associated with the new transform.
  378. *
  379. * The returned transform is of a non-determinate type. Most people
  380. * should use one of the more specific allocation functions such as
  381. * crypto_alloc_skcipher().
  382. *
  383. * In case of error the return value is an error pointer.
  384. */
  385. struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
  386. {
  387. struct crypto_tfm *tfm;
  388. int err;
  389. for (;;) {
  390. struct crypto_alg *alg;
  391. alg = crypto_alg_mod_lookup(alg_name, type, mask);
  392. if (IS_ERR(alg)) {
  393. err = PTR_ERR(alg);
  394. goto err;
  395. }
  396. tfm = __crypto_alloc_tfm(alg, type, mask);
  397. if (!IS_ERR(tfm))
  398. return tfm;
  399. crypto_mod_put(alg);
  400. err = PTR_ERR(tfm);
  401. err:
  402. if (err != -EAGAIN)
  403. break;
  404. if (fatal_signal_pending(current)) {
  405. err = -EINTR;
  406. break;
  407. }
  408. }
  409. return ERR_PTR(err);
  410. }
  411. EXPORT_SYMBOL_GPL(crypto_alloc_base);
  412. static void *crypto_alloc_tfmmem(struct crypto_alg *alg,
  413. const struct crypto_type *frontend, int node,
  414. gfp_t gfp)
  415. {
  416. struct crypto_tfm *tfm;
  417. unsigned int tfmsize;
  418. unsigned int total;
  419. char *mem;
  420. tfmsize = frontend->tfmsize;
  421. total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
  422. mem = kzalloc_node(total, gfp, node);
  423. if (mem == NULL)
  424. return ERR_PTR(-ENOMEM);
  425. tfm = (struct crypto_tfm *)(mem + tfmsize);
  426. tfm->__crt_alg = alg;
  427. tfm->node = node;
  428. refcount_set(&tfm->refcnt, 1);
  429. return mem;
  430. }
  431. void *crypto_create_tfm_node(struct crypto_alg *alg,
  432. const struct crypto_type *frontend,
  433. int node)
  434. {
  435. struct crypto_tfm *tfm;
  436. char *mem;
  437. int err;
  438. mem = crypto_alloc_tfmmem(alg, frontend, node, GFP_KERNEL);
  439. if (IS_ERR(mem))
  440. goto out;
  441. tfm = (struct crypto_tfm *)(mem + frontend->tfmsize);
  442. err = frontend->init_tfm(tfm);
  443. if (err)
  444. goto out_free_tfm;
  445. if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
  446. goto cra_init_failed;
  447. goto out;
  448. cra_init_failed:
  449. crypto_exit_ops(tfm);
  450. out_free_tfm:
  451. if (err == -EAGAIN)
  452. crypto_shoot_alg(alg);
  453. kfree(mem);
  454. mem = ERR_PTR(err);
  455. out:
  456. return mem;
  457. }
  458. EXPORT_SYMBOL_GPL(crypto_create_tfm_node);
  459. void *crypto_clone_tfm(const struct crypto_type *frontend,
  460. struct crypto_tfm *otfm)
  461. {
  462. struct crypto_alg *alg = otfm->__crt_alg;
  463. struct crypto_tfm *tfm;
  464. char *mem;
  465. mem = ERR_PTR(-ESTALE);
  466. if (unlikely(!crypto_mod_get(alg)))
  467. goto out;
  468. mem = crypto_alloc_tfmmem(alg, frontend, otfm->node, GFP_ATOMIC);
  469. if (IS_ERR(mem)) {
  470. crypto_mod_put(alg);
  471. goto out;
  472. }
  473. tfm = (struct crypto_tfm *)(mem + frontend->tfmsize);
  474. tfm->crt_flags = otfm->crt_flags;
  475. tfm->exit = otfm->exit;
  476. out:
  477. return mem;
  478. }
  479. EXPORT_SYMBOL_GPL(crypto_clone_tfm);
  480. struct crypto_alg *crypto_find_alg(const char *alg_name,
  481. const struct crypto_type *frontend,
  482. u32 type, u32 mask)
  483. {
  484. if (frontend) {
  485. type &= frontend->maskclear;
  486. mask &= frontend->maskclear;
  487. type |= frontend->type;
  488. mask |= frontend->maskset;
  489. }
  490. return crypto_alg_mod_lookup(alg_name, type, mask);
  491. }
  492. EXPORT_SYMBOL_GPL(crypto_find_alg);
  493. /*
  494. * crypto_alloc_tfm_node - Locate algorithm and allocate transform
  495. * @alg_name: Name of algorithm
  496. * @frontend: Frontend algorithm type
  497. * @type: Type of algorithm
  498. * @mask: Mask for type comparison
  499. * @node: NUMA node in which users desire to put requests, if node is
  500. * NUMA_NO_NODE, it means users have no special requirement.
  501. *
  502. * crypto_alloc_tfm() will first attempt to locate an already loaded
  503. * algorithm. If that fails and the kernel supports dynamically loadable
  504. * modules, it will then attempt to load a module of the same name or
  505. * alias. If that fails it will send a query to any loaded crypto manager
  506. * to construct an algorithm on the fly. A refcount is grabbed on the
  507. * algorithm which is then associated with the new transform.
  508. *
  509. * The returned transform is of a non-determinate type. Most people
  510. * should use one of the more specific allocation functions such as
  511. * crypto_alloc_skcipher().
  512. *
  513. * In case of error the return value is an error pointer.
  514. */
  515. void *crypto_alloc_tfm_node(const char *alg_name,
  516. const struct crypto_type *frontend, u32 type, u32 mask,
  517. int node)
  518. {
  519. void *tfm;
  520. int err;
  521. for (;;) {
  522. struct crypto_alg *alg;
  523. alg = crypto_find_alg(alg_name, frontend, type, mask);
  524. if (IS_ERR(alg)) {
  525. err = PTR_ERR(alg);
  526. goto err;
  527. }
  528. tfm = crypto_create_tfm_node(alg, frontend, node);
  529. if (!IS_ERR(tfm))
  530. return tfm;
  531. crypto_mod_put(alg);
  532. err = PTR_ERR(tfm);
  533. err:
  534. if (err != -EAGAIN)
  535. break;
  536. if (fatal_signal_pending(current)) {
  537. err = -EINTR;
  538. break;
  539. }
  540. }
  541. return ERR_PTR(err);
  542. }
  543. EXPORT_SYMBOL_GPL(crypto_alloc_tfm_node);
  544. /*
  545. * crypto_destroy_tfm - Free crypto transform
  546. * @mem: Start of tfm slab
  547. * @tfm: Transform to free
  548. *
  549. * This function frees up the transform and any associated resources,
  550. * then drops the refcount on the associated algorithm.
  551. */
  552. void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
  553. {
  554. struct crypto_alg *alg;
  555. if (IS_ERR_OR_NULL(mem))
  556. return;
  557. if (!refcount_dec_and_test(&tfm->refcnt))
  558. return;
  559. alg = tfm->__crt_alg;
  560. if (!tfm->exit && alg->cra_exit)
  561. alg->cra_exit(tfm);
  562. crypto_exit_ops(tfm);
  563. crypto_mod_put(alg);
  564. kfree_sensitive(mem);
  565. }
  566. EXPORT_SYMBOL_GPL(crypto_destroy_tfm);
  567. int crypto_has_alg(const char *name, u32 type, u32 mask)
  568. {
  569. int ret = 0;
  570. struct crypto_alg *alg = crypto_alg_mod_lookup(name, type, mask);
  571. if (!IS_ERR(alg)) {
  572. crypto_mod_put(alg);
  573. ret = 1;
  574. }
  575. return ret;
  576. }
  577. EXPORT_SYMBOL_GPL(crypto_has_alg);
  578. void crypto_req_done(void *data, int err)
  579. {
  580. struct crypto_wait *wait = data;
  581. if (err == -EINPROGRESS)
  582. return;
  583. wait->err = err;
  584. complete(&wait->completion);
  585. }
  586. EXPORT_SYMBOL_GPL(crypto_req_done);
  587. MODULE_DESCRIPTION("Cryptographic core API");
  588. MODULE_LICENSE("GPL");