api.c 16 KB

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