api.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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.h>
  24. #include <linux/slab.h>
  25. #include <linux/string.h>
  26. #include "internal.h"
  27. LIST_HEAD(crypto_alg_list);
  28. EXPORT_SYMBOL_GPL(crypto_alg_list);
  29. DECLARE_RWSEM(crypto_alg_sem);
  30. EXPORT_SYMBOL_GPL(crypto_alg_sem);
  31. BLOCKING_NOTIFIER_HEAD(crypto_chain);
  32. EXPORT_SYMBOL_GPL(crypto_chain);
  33. static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
  34. {
  35. atomic_inc(&alg->cra_refcnt);
  36. return alg;
  37. }
  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. crypto_alg_put(alg);
  46. module_put(alg->cra_module);
  47. }
  48. EXPORT_SYMBOL_GPL(crypto_mod_put);
  49. struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type, u32 mask)
  50. {
  51. struct crypto_alg *q, *alg = NULL;
  52. int best = -2;
  53. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  54. int exact, fuzzy;
  55. if (crypto_is_moribund(q))
  56. continue;
  57. if ((q->cra_flags ^ type) & mask)
  58. continue;
  59. if (crypto_is_larval(q) &&
  60. ((struct crypto_larval *)q)->mask != mask)
  61. continue;
  62. exact = !strcmp(q->cra_driver_name, name);
  63. fuzzy = !strcmp(q->cra_name, name);
  64. if (!exact && !(fuzzy && q->cra_priority > best))
  65. continue;
  66. if (unlikely(!crypto_mod_get(q)))
  67. continue;
  68. best = q->cra_priority;
  69. if (alg)
  70. crypto_mod_put(alg);
  71. alg = q;
  72. if (exact)
  73. break;
  74. }
  75. return alg;
  76. }
  77. EXPORT_SYMBOL_GPL(__crypto_alg_lookup);
  78. static void crypto_larval_destroy(struct crypto_alg *alg)
  79. {
  80. struct crypto_larval *larval = (void *)alg;
  81. BUG_ON(!crypto_is_larval(alg));
  82. if (larval->adult)
  83. crypto_mod_put(larval->adult);
  84. kfree(larval);
  85. }
  86. static struct crypto_alg *crypto_larval_alloc(const char *name, u32 type,
  87. u32 mask)
  88. {
  89. struct crypto_alg *alg;
  90. struct crypto_larval *larval;
  91. larval = kzalloc(sizeof(*larval), GFP_KERNEL);
  92. if (!larval)
  93. return ERR_PTR(-ENOMEM);
  94. larval->mask = mask;
  95. larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type;
  96. larval->alg.cra_priority = -1;
  97. larval->alg.cra_destroy = crypto_larval_destroy;
  98. atomic_set(&larval->alg.cra_refcnt, 2);
  99. strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
  100. init_completion(&larval->completion);
  101. down_write(&crypto_alg_sem);
  102. alg = __crypto_alg_lookup(name, type, mask);
  103. if (!alg) {
  104. alg = &larval->alg;
  105. list_add(&alg->cra_list, &crypto_alg_list);
  106. }
  107. up_write(&crypto_alg_sem);
  108. if (alg != &larval->alg)
  109. kfree(larval);
  110. return alg;
  111. }
  112. static void crypto_larval_kill(struct crypto_alg *alg)
  113. {
  114. struct crypto_larval *larval = (void *)alg;
  115. down_write(&crypto_alg_sem);
  116. list_del(&alg->cra_list);
  117. up_write(&crypto_alg_sem);
  118. complete(&larval->completion);
  119. crypto_alg_put(alg);
  120. }
  121. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
  122. {
  123. struct crypto_larval *larval = (void *)alg;
  124. wait_for_completion_interruptible_timeout(&larval->completion, 60 * HZ);
  125. alg = larval->adult;
  126. if (alg) {
  127. if (!crypto_mod_get(alg))
  128. alg = ERR_PTR(-EAGAIN);
  129. } else
  130. alg = ERR_PTR(-ENOENT);
  131. crypto_mod_put(&larval->alg);
  132. return alg;
  133. }
  134. static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
  135. u32 mask)
  136. {
  137. struct crypto_alg *alg;
  138. down_read(&crypto_alg_sem);
  139. alg = __crypto_alg_lookup(name, type, mask);
  140. up_read(&crypto_alg_sem);
  141. return alg;
  142. }
  143. struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
  144. {
  145. struct crypto_alg *alg;
  146. struct crypto_alg *larval;
  147. int ok;
  148. if (!name)
  149. return ERR_PTR(-ENOENT);
  150. mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
  151. type &= mask;
  152. alg = try_then_request_module(crypto_alg_lookup(name, type, mask),
  153. name);
  154. if (alg)
  155. return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;
  156. larval = crypto_larval_alloc(name, type, mask);
  157. if (IS_ERR(larval) || !crypto_is_larval(larval))
  158. return larval;
  159. ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval);
  160. if (ok == NOTIFY_DONE) {
  161. request_module("cryptomgr");
  162. ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval);
  163. }
  164. if (ok == NOTIFY_STOP)
  165. alg = crypto_larval_wait(larval);
  166. else {
  167. crypto_mod_put(larval);
  168. alg = ERR_PTR(-ENOENT);
  169. }
  170. crypto_larval_kill(larval);
  171. return alg;
  172. }
  173. EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
  174. static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
  175. {
  176. tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK;
  177. flags &= ~CRYPTO_TFM_REQ_MASK;
  178. switch (crypto_tfm_alg_type(tfm)) {
  179. case CRYPTO_ALG_TYPE_CIPHER:
  180. return crypto_init_cipher_flags(tfm, flags);
  181. case CRYPTO_ALG_TYPE_DIGEST:
  182. return crypto_init_digest_flags(tfm, flags);
  183. case CRYPTO_ALG_TYPE_COMPRESS:
  184. return crypto_init_compress_flags(tfm, flags);
  185. }
  186. return 0;
  187. }
  188. static int crypto_init_ops(struct crypto_tfm *tfm)
  189. {
  190. const struct crypto_type *type = tfm->__crt_alg->cra_type;
  191. if (type)
  192. return type->init(tfm);
  193. switch (crypto_tfm_alg_type(tfm)) {
  194. case CRYPTO_ALG_TYPE_CIPHER:
  195. return crypto_init_cipher_ops(tfm);
  196. case CRYPTO_ALG_TYPE_DIGEST:
  197. return crypto_init_digest_ops(tfm);
  198. case CRYPTO_ALG_TYPE_COMPRESS:
  199. return crypto_init_compress_ops(tfm);
  200. default:
  201. break;
  202. }
  203. BUG();
  204. return -EINVAL;
  205. }
  206. static void crypto_exit_ops(struct crypto_tfm *tfm)
  207. {
  208. const struct crypto_type *type = tfm->__crt_alg->cra_type;
  209. if (type) {
  210. if (type->exit)
  211. type->exit(tfm);
  212. return;
  213. }
  214. switch (crypto_tfm_alg_type(tfm)) {
  215. case CRYPTO_ALG_TYPE_CIPHER:
  216. crypto_exit_cipher_ops(tfm);
  217. break;
  218. case CRYPTO_ALG_TYPE_DIGEST:
  219. crypto_exit_digest_ops(tfm);
  220. break;
  221. case CRYPTO_ALG_TYPE_COMPRESS:
  222. crypto_exit_compress_ops(tfm);
  223. break;
  224. default:
  225. BUG();
  226. }
  227. }
  228. static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
  229. {
  230. const struct crypto_type *type = alg->cra_type;
  231. unsigned int len;
  232. len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
  233. if (type)
  234. return len + type->ctxsize(alg);
  235. switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  236. default:
  237. BUG();
  238. case CRYPTO_ALG_TYPE_CIPHER:
  239. len += crypto_cipher_ctxsize(alg, flags);
  240. break;
  241. case CRYPTO_ALG_TYPE_DIGEST:
  242. len += crypto_digest_ctxsize(alg, flags);
  243. break;
  244. case CRYPTO_ALG_TYPE_COMPRESS:
  245. len += crypto_compress_ctxsize(alg, flags);
  246. break;
  247. }
  248. return len;
  249. }
  250. void crypto_shoot_alg(struct crypto_alg *alg)
  251. {
  252. down_write(&crypto_alg_sem);
  253. alg->cra_flags |= CRYPTO_ALG_DYING;
  254. up_write(&crypto_alg_sem);
  255. }
  256. EXPORT_SYMBOL_GPL(crypto_shoot_alg);
  257. struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 flags)
  258. {
  259. struct crypto_tfm *tfm = NULL;
  260. unsigned int tfm_size;
  261. int err = -ENOMEM;
  262. tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
  263. tfm = kzalloc(tfm_size, GFP_KERNEL);
  264. if (tfm == NULL)
  265. goto out;
  266. tfm->__crt_alg = alg;
  267. err = crypto_init_flags(tfm, flags);
  268. if (err)
  269. goto out_free_tfm;
  270. err = crypto_init_ops(tfm);
  271. if (err)
  272. goto out_free_tfm;
  273. if (alg->cra_init && (err = alg->cra_init(tfm))) {
  274. if (err == -EAGAIN)
  275. crypto_shoot_alg(alg);
  276. goto cra_init_failed;
  277. }
  278. goto out;
  279. cra_init_failed:
  280. crypto_exit_ops(tfm);
  281. out_free_tfm:
  282. kfree(tfm);
  283. tfm = ERR_PTR(err);
  284. out:
  285. return tfm;
  286. }
  287. EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);
  288. struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
  289. {
  290. struct crypto_tfm *tfm = NULL;
  291. int err;
  292. do {
  293. struct crypto_alg *alg;
  294. alg = crypto_alg_mod_lookup(name, 0, CRYPTO_ALG_ASYNC);
  295. err = PTR_ERR(alg);
  296. if (IS_ERR(alg))
  297. continue;
  298. tfm = __crypto_alloc_tfm(alg, flags);
  299. err = 0;
  300. if (IS_ERR(tfm)) {
  301. crypto_mod_put(alg);
  302. err = PTR_ERR(tfm);
  303. tfm = NULL;
  304. }
  305. } while (err == -EAGAIN && !signal_pending(current));
  306. return tfm;
  307. }
  308. /*
  309. * crypto_alloc_base - Locate algorithm and allocate transform
  310. * @alg_name: Name of algorithm
  311. * @type: Type of algorithm
  312. * @mask: Mask for type comparison
  313. *
  314. * crypto_alloc_base() will first attempt to locate an already loaded
  315. * algorithm. If that fails and the kernel supports dynamically loadable
  316. * modules, it will then attempt to load a module of the same name or
  317. * alias. If that fails it will send a query to any loaded crypto manager
  318. * to construct an algorithm on the fly. A refcount is grabbed on the
  319. * algorithm which is then associated with the new transform.
  320. *
  321. * The returned transform is of a non-determinate type. Most people
  322. * should use one of the more specific allocation functions such as
  323. * crypto_alloc_blkcipher.
  324. *
  325. * In case of error the return value is an error pointer.
  326. */
  327. struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
  328. {
  329. struct crypto_tfm *tfm;
  330. int err;
  331. for (;;) {
  332. struct crypto_alg *alg;
  333. alg = crypto_alg_mod_lookup(alg_name, type, mask);
  334. err = PTR_ERR(alg);
  335. tfm = ERR_PTR(err);
  336. if (IS_ERR(alg))
  337. goto err;
  338. tfm = __crypto_alloc_tfm(alg, 0);
  339. if (!IS_ERR(tfm))
  340. break;
  341. crypto_mod_put(alg);
  342. err = PTR_ERR(tfm);
  343. err:
  344. if (err != -EAGAIN)
  345. break;
  346. if (signal_pending(current)) {
  347. err = -EINTR;
  348. break;
  349. }
  350. };
  351. return tfm;
  352. }
  353. EXPORT_SYMBOL_GPL(crypto_alloc_base);
  354. /*
  355. * crypto_free_tfm - Free crypto transform
  356. * @tfm: Transform to free
  357. *
  358. * crypto_free_tfm() frees up the transform and any associated resources,
  359. * then drops the refcount on the associated algorithm.
  360. */
  361. void crypto_free_tfm(struct crypto_tfm *tfm)
  362. {
  363. struct crypto_alg *alg;
  364. int size;
  365. if (unlikely(!tfm))
  366. return;
  367. alg = tfm->__crt_alg;
  368. size = sizeof(*tfm) + alg->cra_ctxsize;
  369. if (alg->cra_exit)
  370. alg->cra_exit(tfm);
  371. crypto_exit_ops(tfm);
  372. crypto_mod_put(alg);
  373. memset(tfm, 0, size);
  374. kfree(tfm);
  375. }
  376. int crypto_alg_available(const char *name, u32 flags)
  377. {
  378. int ret = 0;
  379. struct crypto_alg *alg = crypto_alg_mod_lookup(name, 0,
  380. CRYPTO_ALG_ASYNC);
  381. if (!IS_ERR(alg)) {
  382. crypto_mod_put(alg);
  383. ret = 1;
  384. }
  385. return ret;
  386. }
  387. EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
  388. EXPORT_SYMBOL_GPL(crypto_free_tfm);
  389. EXPORT_SYMBOL_GPL(crypto_alg_available);
  390. int crypto_has_alg(const char *name, u32 type, u32 mask)
  391. {
  392. int ret = 0;
  393. struct crypto_alg *alg = crypto_alg_mod_lookup(name, type, mask);
  394. if (!IS_ERR(alg)) {
  395. crypto_mod_put(alg);
  396. ret = 1;
  397. }
  398. return ret;
  399. }
  400. EXPORT_SYMBOL_GPL(crypto_has_alg);