api.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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/kernel.h>
  15. #include <linux/kmod.h>
  16. #include <linux/module.h>
  17. #include <linux/param.h>
  18. #include <linux/sched/signal.h>
  19. #include <linux/slab.h>
  20. #include <linux/string.h>
  21. #include <linux/completion.h>
  22. #include "internal.h"
  23. LIST_HEAD(crypto_alg_list);
  24. EXPORT_SYMBOL_GPL(crypto_alg_list);
  25. DECLARE_RWSEM(crypto_alg_sem);
  26. EXPORT_SYMBOL_GPL(crypto_alg_sem);
  27. BLOCKING_NOTIFIER_HEAD(crypto_chain);
  28. EXPORT_SYMBOL_GPL(crypto_chain);
  29. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);
  30. struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
  31. {
  32. return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
  33. }
  34. EXPORT_SYMBOL_GPL(crypto_mod_get);
  35. void crypto_mod_put(struct crypto_alg *alg)
  36. {
  37. struct module *module = alg->cra_module;
  38. crypto_alg_put(alg);
  39. module_put(module);
  40. }
  41. EXPORT_SYMBOL_GPL(crypto_mod_put);
  42. static inline int crypto_is_test_larval(struct crypto_larval *larval)
  43. {
  44. return larval->alg.cra_driver_name[0];
  45. }
  46. static struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type,
  47. u32 mask)
  48. {
  49. struct crypto_alg *q, *alg = NULL;
  50. int best = -2;
  51. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  52. int exact, fuzzy;
  53. if (crypto_is_moribund(q))
  54. continue;
  55. if ((q->cra_flags ^ type) & mask)
  56. continue;
  57. if (crypto_is_larval(q) &&
  58. !crypto_is_test_larval((struct crypto_larval *)q) &&
  59. ((struct crypto_larval *)q)->mask != 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. 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. strlcpy(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. void crypto_larval_kill(struct crypto_alg *alg)
  123. {
  124. struct crypto_larval *larval = (void *)alg;
  125. down_write(&crypto_alg_sem);
  126. list_del(&alg->cra_list);
  127. up_write(&crypto_alg_sem);
  128. complete_all(&larval->completion);
  129. crypto_alg_put(alg);
  130. }
  131. EXPORT_SYMBOL_GPL(crypto_larval_kill);
  132. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
  133. {
  134. struct crypto_larval *larval = (void *)alg;
  135. long timeout;
  136. timeout = wait_for_completion_killable_timeout(
  137. &larval->completion, 60 * HZ);
  138. alg = larval->adult;
  139. if (timeout < 0)
  140. alg = ERR_PTR(-EINTR);
  141. else if (!timeout)
  142. alg = ERR_PTR(-ETIMEDOUT);
  143. else if (!alg)
  144. alg = ERR_PTR(-ENOENT);
  145. else if (IS_ERR(alg))
  146. ;
  147. else if (crypto_is_test_larval(larval) &&
  148. !(alg->cra_flags & CRYPTO_ALG_TESTED))
  149. alg = ERR_PTR(-EAGAIN);
  150. else if (!crypto_mod_get(alg))
  151. alg = ERR_PTR(-EAGAIN);
  152. crypto_mod_put(&larval->alg);
  153. return alg;
  154. }
  155. static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
  156. u32 mask)
  157. {
  158. struct crypto_alg *alg;
  159. u32 test = 0;
  160. if (!((type | mask) & CRYPTO_ALG_TESTED))
  161. test |= CRYPTO_ALG_TESTED;
  162. down_read(&crypto_alg_sem);
  163. alg = __crypto_alg_lookup(name, type | test, mask | test);
  164. if (!alg && test) {
  165. alg = __crypto_alg_lookup(name, type, mask);
  166. if (alg && !crypto_is_larval(alg)) {
  167. /* Test failed */
  168. crypto_mod_put(alg);
  169. alg = ERR_PTR(-ELIBBAD);
  170. }
  171. }
  172. up_read(&crypto_alg_sem);
  173. return alg;
  174. }
  175. static struct crypto_alg *crypto_larval_lookup(const char *name, u32 type,
  176. u32 mask)
  177. {
  178. struct crypto_alg *alg;
  179. if (!name)
  180. return ERR_PTR(-ENOENT);
  181. type &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
  182. mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
  183. alg = crypto_alg_lookup(name, type, mask);
  184. if (!alg && !(mask & CRYPTO_NOLOAD)) {
  185. request_module("crypto-%s", name);
  186. if (!((type ^ CRYPTO_ALG_NEED_FALLBACK) & mask &
  187. CRYPTO_ALG_NEED_FALLBACK))
  188. request_module("crypto-%s-all", name);
  189. alg = crypto_alg_lookup(name, type, mask);
  190. }
  191. if (!IS_ERR_OR_NULL(alg) && crypto_is_larval(alg))
  192. alg = crypto_larval_wait(alg);
  193. else if (!alg)
  194. alg = crypto_larval_add(name, type, mask);
  195. return alg;
  196. }
  197. int crypto_probing_notify(unsigned long val, void *v)
  198. {
  199. int ok;
  200. ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  201. if (ok == NOTIFY_DONE) {
  202. request_module("cryptomgr");
  203. ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  204. }
  205. return ok;
  206. }
  207. EXPORT_SYMBOL_GPL(crypto_probing_notify);
  208. struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
  209. {
  210. struct crypto_alg *alg;
  211. struct crypto_alg *larval;
  212. int ok;
  213. /*
  214. * If the internal flag is set for a cipher, require a caller to
  215. * to invoke the cipher with the internal flag to use that cipher.
  216. * Also, if a caller wants to allocate a cipher that may or may
  217. * not be an internal cipher, use type | CRYPTO_ALG_INTERNAL and
  218. * !(mask & CRYPTO_ALG_INTERNAL).
  219. */
  220. if (!((type | mask) & CRYPTO_ALG_INTERNAL))
  221. mask |= CRYPTO_ALG_INTERNAL;
  222. larval = crypto_larval_lookup(name, type, mask);
  223. if (IS_ERR(larval) || !crypto_is_larval(larval))
  224. return larval;
  225. ok = crypto_probing_notify(CRYPTO_MSG_ALG_REQUEST, larval);
  226. if (ok == NOTIFY_STOP)
  227. alg = crypto_larval_wait(larval);
  228. else {
  229. crypto_mod_put(larval);
  230. alg = ERR_PTR(-ENOENT);
  231. }
  232. crypto_larval_kill(larval);
  233. return alg;
  234. }
  235. EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
  236. static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  237. {
  238. const struct crypto_type *type_obj = tfm->__crt_alg->cra_type;
  239. if (type_obj)
  240. return type_obj->init(tfm, type, mask);
  241. return 0;
  242. }
  243. static void crypto_exit_ops(struct crypto_tfm *tfm)
  244. {
  245. const struct crypto_type *type = tfm->__crt_alg->cra_type;
  246. if (type && tfm->exit)
  247. tfm->exit(tfm);
  248. }
  249. static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask)
  250. {
  251. const struct crypto_type *type_obj = alg->cra_type;
  252. unsigned int len;
  253. len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
  254. if (type_obj)
  255. return len + type_obj->ctxsize(alg, type, mask);
  256. switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  257. default:
  258. BUG();
  259. case CRYPTO_ALG_TYPE_CIPHER:
  260. len += crypto_cipher_ctxsize(alg);
  261. break;
  262. case CRYPTO_ALG_TYPE_COMPRESS:
  263. len += crypto_compress_ctxsize(alg);
  264. break;
  265. }
  266. return len;
  267. }
  268. void crypto_shoot_alg(struct crypto_alg *alg)
  269. {
  270. down_write(&crypto_alg_sem);
  271. alg->cra_flags |= CRYPTO_ALG_DYING;
  272. up_write(&crypto_alg_sem);
  273. }
  274. EXPORT_SYMBOL_GPL(crypto_shoot_alg);
  275. struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
  276. u32 mask)
  277. {
  278. struct crypto_tfm *tfm = NULL;
  279. unsigned int tfm_size;
  280. int err = -ENOMEM;
  281. tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask);
  282. tfm = kzalloc(tfm_size, GFP_KERNEL);
  283. if (tfm == NULL)
  284. goto out_err;
  285. tfm->__crt_alg = alg;
  286. err = crypto_init_ops(tfm, type, mask);
  287. if (err)
  288. goto out_free_tfm;
  289. if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
  290. goto cra_init_failed;
  291. goto out;
  292. cra_init_failed:
  293. crypto_exit_ops(tfm);
  294. out_free_tfm:
  295. if (err == -EAGAIN)
  296. crypto_shoot_alg(alg);
  297. kfree(tfm);
  298. out_err:
  299. tfm = ERR_PTR(err);
  300. out:
  301. return tfm;
  302. }
  303. EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);
  304. /*
  305. * crypto_alloc_base - Locate algorithm and allocate transform
  306. * @alg_name: Name of algorithm
  307. * @type: Type of algorithm
  308. * @mask: Mask for type comparison
  309. *
  310. * This function should not be used by new algorithm types.
  311. * Please use crypto_alloc_tfm instead.
  312. *
  313. * crypto_alloc_base() will first attempt to locate an already loaded
  314. * algorithm. If that fails and the kernel supports dynamically loadable
  315. * modules, it will then attempt to load a module of the same name or
  316. * alias. If that fails it will send a query to any loaded crypto manager
  317. * to construct an algorithm on the fly. A refcount is grabbed on the
  318. * algorithm which is then associated with the new transform.
  319. *
  320. * The returned transform is of a non-determinate type. Most people
  321. * should use one of the more specific allocation functions such as
  322. * crypto_alloc_skcipher().
  323. *
  324. * In case of error the return value is an error pointer.
  325. */
  326. struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
  327. {
  328. struct crypto_tfm *tfm;
  329. int err;
  330. for (;;) {
  331. struct crypto_alg *alg;
  332. alg = crypto_alg_mod_lookup(alg_name, type, mask);
  333. if (IS_ERR(alg)) {
  334. err = PTR_ERR(alg);
  335. goto err;
  336. }
  337. tfm = __crypto_alloc_tfm(alg, type, mask);
  338. if (!IS_ERR(tfm))
  339. return tfm;
  340. crypto_mod_put(alg);
  341. err = PTR_ERR(tfm);
  342. err:
  343. if (err != -EAGAIN)
  344. break;
  345. if (fatal_signal_pending(current)) {
  346. err = -EINTR;
  347. break;
  348. }
  349. }
  350. return ERR_PTR(err);
  351. }
  352. EXPORT_SYMBOL_GPL(crypto_alloc_base);
  353. void *crypto_create_tfm_node(struct crypto_alg *alg,
  354. const struct crypto_type *frontend,
  355. int node)
  356. {
  357. char *mem;
  358. struct crypto_tfm *tfm = NULL;
  359. unsigned int tfmsize;
  360. unsigned int total;
  361. int err = -ENOMEM;
  362. tfmsize = frontend->tfmsize;
  363. total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
  364. mem = kzalloc_node(total, GFP_KERNEL, node);
  365. if (mem == NULL)
  366. goto out_err;
  367. tfm = (struct crypto_tfm *)(mem + tfmsize);
  368. tfm->__crt_alg = alg;
  369. tfm->node = node;
  370. err = frontend->init_tfm(tfm);
  371. if (err)
  372. goto out_free_tfm;
  373. if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
  374. goto cra_init_failed;
  375. goto out;
  376. cra_init_failed:
  377. crypto_exit_ops(tfm);
  378. out_free_tfm:
  379. if (err == -EAGAIN)
  380. crypto_shoot_alg(alg);
  381. kfree(mem);
  382. out_err:
  383. mem = ERR_PTR(err);
  384. out:
  385. return mem;
  386. }
  387. EXPORT_SYMBOL_GPL(crypto_create_tfm_node);
  388. struct crypto_alg *crypto_find_alg(const char *alg_name,
  389. const struct crypto_type *frontend,
  390. u32 type, u32 mask)
  391. {
  392. if (frontend) {
  393. type &= frontend->maskclear;
  394. mask &= frontend->maskclear;
  395. type |= frontend->type;
  396. mask |= frontend->maskset;
  397. }
  398. return crypto_alg_mod_lookup(alg_name, type, mask);
  399. }
  400. EXPORT_SYMBOL_GPL(crypto_find_alg);
  401. /*
  402. * crypto_alloc_tfm_node - Locate algorithm and allocate transform
  403. * @alg_name: Name of algorithm
  404. * @frontend: Frontend algorithm type
  405. * @type: Type of algorithm
  406. * @mask: Mask for type comparison
  407. * @node: NUMA node in which users desire to put requests, if node is
  408. * NUMA_NO_NODE, it means users have no special requirement.
  409. *
  410. * crypto_alloc_tfm() will first attempt to locate an already loaded
  411. * algorithm. If that fails and the kernel supports dynamically loadable
  412. * modules, it will then attempt to load a module of the same name or
  413. * alias. If that fails it will send a query to any loaded crypto manager
  414. * to construct an algorithm on the fly. A refcount is grabbed on the
  415. * algorithm which is then associated with the new transform.
  416. *
  417. * The returned transform is of a non-determinate type. Most people
  418. * should use one of the more specific allocation functions such as
  419. * crypto_alloc_skcipher().
  420. *
  421. * In case of error the return value is an error pointer.
  422. */
  423. void *crypto_alloc_tfm_node(const char *alg_name,
  424. const struct crypto_type *frontend, u32 type, u32 mask,
  425. int node)
  426. {
  427. void *tfm;
  428. int err;
  429. for (;;) {
  430. struct crypto_alg *alg;
  431. alg = crypto_find_alg(alg_name, frontend, type, mask);
  432. if (IS_ERR(alg)) {
  433. err = PTR_ERR(alg);
  434. goto err;
  435. }
  436. tfm = crypto_create_tfm_node(alg, frontend, node);
  437. if (!IS_ERR(tfm))
  438. return tfm;
  439. crypto_mod_put(alg);
  440. err = PTR_ERR(tfm);
  441. err:
  442. if (err != -EAGAIN)
  443. break;
  444. if (fatal_signal_pending(current)) {
  445. err = -EINTR;
  446. break;
  447. }
  448. }
  449. return ERR_PTR(err);
  450. }
  451. EXPORT_SYMBOL_GPL(crypto_alloc_tfm_node);
  452. /*
  453. * crypto_destroy_tfm - Free crypto transform
  454. * @mem: Start of tfm slab
  455. * @tfm: Transform to free
  456. *
  457. * This function frees up the transform and any associated resources,
  458. * then drops the refcount on the associated algorithm.
  459. */
  460. void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
  461. {
  462. struct crypto_alg *alg;
  463. if (IS_ERR_OR_NULL(mem))
  464. return;
  465. alg = tfm->__crt_alg;
  466. if (!tfm->exit && alg->cra_exit)
  467. alg->cra_exit(tfm);
  468. crypto_exit_ops(tfm);
  469. crypto_mod_put(alg);
  470. kfree_sensitive(mem);
  471. }
  472. EXPORT_SYMBOL_GPL(crypto_destroy_tfm);
  473. int crypto_has_alg(const char *name, u32 type, u32 mask)
  474. {
  475. int ret = 0;
  476. struct crypto_alg *alg = crypto_alg_mod_lookup(name, type, mask);
  477. if (!IS_ERR(alg)) {
  478. crypto_mod_put(alg);
  479. ret = 1;
  480. }
  481. return ret;
  482. }
  483. EXPORT_SYMBOL_GPL(crypto_has_alg);
  484. void crypto_req_done(struct crypto_async_request *req, int err)
  485. {
  486. struct crypto_wait *wait = req->data;
  487. if (err == -EINPROGRESS)
  488. return;
  489. wait->err = err;
  490. complete(&wait->completion);
  491. }
  492. EXPORT_SYMBOL_GPL(crypto_req_done);
  493. MODULE_DESCRIPTION("Cryptographic core API");
  494. MODULE_LICENSE("GPL");