api.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * Scatterlist Cryptographic API.
  3. *
  4. * Copyright (c) 2002 James Morris <[email protected]>
  5. * Copyright (c) 2002 David S. Miller ([email protected])
  6. * Copyright (c) 2005 Herbert Xu <[email protected]>
  7. *
  8. * Portions derived from Cryptoapi, by Alexander Kjeldaas <[email protected]>
  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.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. atomic_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. struct crypto_alg *crypto_alg_lookup(const char *name, u32 type, u32 mask)
  159. {
  160. struct crypto_alg *alg;
  161. down_read(&crypto_alg_sem);
  162. alg = __crypto_alg_lookup(name, type, mask);
  163. up_read(&crypto_alg_sem);
  164. return alg;
  165. }
  166. EXPORT_SYMBOL_GPL(crypto_alg_lookup);
  167. struct crypto_alg *crypto_larval_lookup(const char *name, u32 type, u32 mask)
  168. {
  169. struct crypto_alg *alg;
  170. if (!name)
  171. return ERR_PTR(-ENOENT);
  172. mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
  173. type &= mask;
  174. alg = crypto_alg_lookup(name, type, mask);
  175. if (!alg && !(mask & CRYPTO_NOLOAD)) {
  176. request_module("crypto-%s", name);
  177. if (!((type ^ CRYPTO_ALG_NEED_FALLBACK) & mask &
  178. CRYPTO_ALG_NEED_FALLBACK))
  179. request_module("crypto-%s-all", name);
  180. alg = crypto_alg_lookup(name, type, mask);
  181. }
  182. if (alg)
  183. return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;
  184. return crypto_larval_add(name, type, mask);
  185. }
  186. EXPORT_SYMBOL_GPL(crypto_larval_lookup);
  187. int crypto_probing_notify(unsigned long val, void *v)
  188. {
  189. int ok;
  190. ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  191. if (ok == NOTIFY_DONE) {
  192. request_module("cryptomgr");
  193. ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  194. }
  195. return ok;
  196. }
  197. EXPORT_SYMBOL_GPL(crypto_probing_notify);
  198. struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
  199. {
  200. struct crypto_alg *alg;
  201. struct crypto_alg *larval;
  202. int ok;
  203. if (!((type | mask) & CRYPTO_ALG_TESTED)) {
  204. type |= CRYPTO_ALG_TESTED;
  205. mask |= CRYPTO_ALG_TESTED;
  206. }
  207. /*
  208. * If the internal flag is set for a cipher, require a caller to
  209. * to invoke the cipher with the internal flag to use that cipher.
  210. * Also, if a caller wants to allocate a cipher that may or may
  211. * not be an internal cipher, use type | CRYPTO_ALG_INTERNAL and
  212. * !(mask & CRYPTO_ALG_INTERNAL).
  213. */
  214. if (!((type | mask) & CRYPTO_ALG_INTERNAL))
  215. mask |= CRYPTO_ALG_INTERNAL;
  216. larval = crypto_larval_lookup(name, type, mask);
  217. if (IS_ERR(larval) || !crypto_is_larval(larval))
  218. return larval;
  219. ok = crypto_probing_notify(CRYPTO_MSG_ALG_REQUEST, larval);
  220. if (ok == NOTIFY_STOP)
  221. alg = crypto_larval_wait(larval);
  222. else {
  223. crypto_mod_put(larval);
  224. alg = ERR_PTR(-ENOENT);
  225. }
  226. crypto_larval_kill(larval);
  227. return alg;
  228. }
  229. EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
  230. static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  231. {
  232. const struct crypto_type *type_obj = tfm->__crt_alg->cra_type;
  233. if (type_obj)
  234. return type_obj->init(tfm, type, mask);
  235. switch (crypto_tfm_alg_type(tfm)) {
  236. case CRYPTO_ALG_TYPE_CIPHER:
  237. return crypto_init_cipher_ops(tfm);
  238. case CRYPTO_ALG_TYPE_COMPRESS:
  239. return crypto_init_compress_ops(tfm);
  240. default:
  241. break;
  242. }
  243. BUG();
  244. return -EINVAL;
  245. }
  246. static void crypto_exit_ops(struct crypto_tfm *tfm)
  247. {
  248. const struct crypto_type *type = tfm->__crt_alg->cra_type;
  249. if (type) {
  250. if (tfm->exit)
  251. tfm->exit(tfm);
  252. return;
  253. }
  254. switch (crypto_tfm_alg_type(tfm)) {
  255. case CRYPTO_ALG_TYPE_CIPHER:
  256. crypto_exit_cipher_ops(tfm);
  257. break;
  258. case CRYPTO_ALG_TYPE_COMPRESS:
  259. crypto_exit_compress_ops(tfm);
  260. break;
  261. default:
  262. BUG();
  263. }
  264. }
  265. static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask)
  266. {
  267. const struct crypto_type *type_obj = alg->cra_type;
  268. unsigned int len;
  269. len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
  270. if (type_obj)
  271. return len + type_obj->ctxsize(alg, type, mask);
  272. switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  273. default:
  274. BUG();
  275. case CRYPTO_ALG_TYPE_CIPHER:
  276. len += crypto_cipher_ctxsize(alg);
  277. break;
  278. case CRYPTO_ALG_TYPE_COMPRESS:
  279. len += crypto_compress_ctxsize(alg);
  280. break;
  281. }
  282. return len;
  283. }
  284. void crypto_shoot_alg(struct crypto_alg *alg)
  285. {
  286. down_write(&crypto_alg_sem);
  287. alg->cra_flags |= CRYPTO_ALG_DYING;
  288. up_write(&crypto_alg_sem);
  289. }
  290. EXPORT_SYMBOL_GPL(crypto_shoot_alg);
  291. struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
  292. u32 mask)
  293. {
  294. struct crypto_tfm *tfm = NULL;
  295. unsigned int tfm_size;
  296. int err = -ENOMEM;
  297. tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask);
  298. tfm = kzalloc(tfm_size, GFP_KERNEL);
  299. if (tfm == NULL)
  300. goto out_err;
  301. tfm->__crt_alg = alg;
  302. err = crypto_init_ops(tfm, type, mask);
  303. if (err)
  304. goto out_free_tfm;
  305. if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
  306. goto cra_init_failed;
  307. goto out;
  308. cra_init_failed:
  309. crypto_exit_ops(tfm);
  310. out_free_tfm:
  311. if (err == -EAGAIN)
  312. crypto_shoot_alg(alg);
  313. kfree(tfm);
  314. out_err:
  315. tfm = ERR_PTR(err);
  316. out:
  317. return tfm;
  318. }
  319. EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);
  320. /*
  321. * crypto_alloc_base - Locate algorithm and allocate transform
  322. * @alg_name: Name of algorithm
  323. * @type: Type of algorithm
  324. * @mask: Mask for type comparison
  325. *
  326. * This function should not be used by new algorithm types.
  327. * Please use crypto_alloc_tfm instead.
  328. *
  329. * crypto_alloc_base() will first attempt to locate an already loaded
  330. * algorithm. If that fails and the kernel supports dynamically loadable
  331. * modules, it will then attempt to load a module of the same name or
  332. * alias. If that fails it will send a query to any loaded crypto manager
  333. * to construct an algorithm on the fly. A refcount is grabbed on the
  334. * algorithm which is then associated with the new transform.
  335. *
  336. * The returned transform is of a non-determinate type. Most people
  337. * should use one of the more specific allocation functions such as
  338. * crypto_alloc_blkcipher.
  339. *
  340. * In case of error the return value is an error pointer.
  341. */
  342. struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
  343. {
  344. struct crypto_tfm *tfm;
  345. int err;
  346. for (;;) {
  347. struct crypto_alg *alg;
  348. alg = crypto_alg_mod_lookup(alg_name, type, mask);
  349. if (IS_ERR(alg)) {
  350. err = PTR_ERR(alg);
  351. goto err;
  352. }
  353. tfm = __crypto_alloc_tfm(alg, type, mask);
  354. if (!IS_ERR(tfm))
  355. return tfm;
  356. crypto_mod_put(alg);
  357. err = PTR_ERR(tfm);
  358. err:
  359. if (err != -EAGAIN)
  360. break;
  361. if (fatal_signal_pending(current)) {
  362. err = -EINTR;
  363. break;
  364. }
  365. }
  366. return ERR_PTR(err);
  367. }
  368. EXPORT_SYMBOL_GPL(crypto_alloc_base);
  369. void *crypto_create_tfm(struct crypto_alg *alg,
  370. const struct crypto_type *frontend)
  371. {
  372. char *mem;
  373. struct crypto_tfm *tfm = NULL;
  374. unsigned int tfmsize;
  375. unsigned int total;
  376. int err = -ENOMEM;
  377. tfmsize = frontend->tfmsize;
  378. total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
  379. mem = kzalloc(total, GFP_KERNEL);
  380. if (mem == NULL)
  381. goto out_err;
  382. tfm = (struct crypto_tfm *)(mem + tfmsize);
  383. tfm->__crt_alg = alg;
  384. err = frontend->init_tfm(tfm);
  385. if (err)
  386. goto out_free_tfm;
  387. if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
  388. goto cra_init_failed;
  389. goto out;
  390. cra_init_failed:
  391. crypto_exit_ops(tfm);
  392. out_free_tfm:
  393. if (err == -EAGAIN)
  394. crypto_shoot_alg(alg);
  395. kfree(mem);
  396. out_err:
  397. mem = ERR_PTR(err);
  398. out:
  399. return mem;
  400. }
  401. EXPORT_SYMBOL_GPL(crypto_create_tfm);
  402. struct crypto_alg *crypto_find_alg(const char *alg_name,
  403. const struct crypto_type *frontend,
  404. u32 type, u32 mask)
  405. {
  406. struct crypto_alg *(*lookup)(const char *name, u32 type, u32 mask) =
  407. crypto_alg_mod_lookup;
  408. if (frontend) {
  409. type &= frontend->maskclear;
  410. mask &= frontend->maskclear;
  411. type |= frontend->type;
  412. mask |= frontend->maskset;
  413. if (frontend->lookup)
  414. lookup = frontend->lookup;
  415. }
  416. return lookup(alg_name, type, mask);
  417. }
  418. EXPORT_SYMBOL_GPL(crypto_find_alg);
  419. /*
  420. * crypto_alloc_tfm - Locate algorithm and allocate transform
  421. * @alg_name: Name of algorithm
  422. * @frontend: Frontend algorithm type
  423. * @type: Type of algorithm
  424. * @mask: Mask for type comparison
  425. *
  426. * crypto_alloc_tfm() will first attempt to locate an already loaded
  427. * algorithm. If that fails and the kernel supports dynamically loadable
  428. * modules, it will then attempt to load a module of the same name or
  429. * alias. If that fails it will send a query to any loaded crypto manager
  430. * to construct an algorithm on the fly. A refcount is grabbed on the
  431. * algorithm which is then associated with the new transform.
  432. *
  433. * The returned transform is of a non-determinate type. Most people
  434. * should use one of the more specific allocation functions such as
  435. * crypto_alloc_blkcipher.
  436. *
  437. * In case of error the return value is an error pointer.
  438. */
  439. void *crypto_alloc_tfm(const char *alg_name,
  440. const struct crypto_type *frontend, u32 type, u32 mask)
  441. {
  442. void *tfm;
  443. int err;
  444. for (;;) {
  445. struct crypto_alg *alg;
  446. alg = crypto_find_alg(alg_name, frontend, type, mask);
  447. if (IS_ERR(alg)) {
  448. err = PTR_ERR(alg);
  449. goto err;
  450. }
  451. tfm = crypto_create_tfm(alg, frontend);
  452. if (!IS_ERR(tfm))
  453. return tfm;
  454. crypto_mod_put(alg);
  455. err = PTR_ERR(tfm);
  456. err:
  457. if (err != -EAGAIN)
  458. break;
  459. if (fatal_signal_pending(current)) {
  460. err = -EINTR;
  461. break;
  462. }
  463. }
  464. return ERR_PTR(err);
  465. }
  466. EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
  467. /*
  468. * crypto_destroy_tfm - Free crypto transform
  469. * @mem: Start of tfm slab
  470. * @tfm: Transform to free
  471. *
  472. * This function frees up the transform and any associated resources,
  473. * then drops the refcount on the associated algorithm.
  474. */
  475. void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
  476. {
  477. struct crypto_alg *alg;
  478. if (unlikely(!mem))
  479. return;
  480. alg = tfm->__crt_alg;
  481. if (!tfm->exit && alg->cra_exit)
  482. alg->cra_exit(tfm);
  483. crypto_exit_ops(tfm);
  484. crypto_mod_put(alg);
  485. kzfree(mem);
  486. }
  487. EXPORT_SYMBOL_GPL(crypto_destroy_tfm);
  488. int crypto_has_alg(const char *name, u32 type, u32 mask)
  489. {
  490. int ret = 0;
  491. struct crypto_alg *alg = crypto_alg_mod_lookup(name, type, mask);
  492. if (!IS_ERR(alg)) {
  493. crypto_mod_put(alg);
  494. ret = 1;
  495. }
  496. return ret;
  497. }
  498. EXPORT_SYMBOL_GPL(crypto_has_alg);
  499. void crypto_req_done(struct crypto_async_request *req, int err)
  500. {
  501. struct crypto_wait *wait = req->data;
  502. if (err == -EINPROGRESS)
  503. return;
  504. wait->err = err;
  505. complete(&wait->completion);
  506. }
  507. EXPORT_SYMBOL_GPL(crypto_req_done);
  508. MODULE_DESCRIPTION("Cryptographic core API");
  509. MODULE_LICENSE("GPL");