api.c 14 KB

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