api.c 9.7 KB

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