api.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. default:
  186. break;
  187. }
  188. BUG();
  189. return -EINVAL;
  190. }
  191. static int crypto_init_ops(struct crypto_tfm *tfm)
  192. {
  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. 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, int flags)
  223. {
  224. unsigned int len;
  225. switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  226. default:
  227. BUG();
  228. case CRYPTO_ALG_TYPE_CIPHER:
  229. len = crypto_cipher_ctxsize(alg, flags);
  230. break;
  231. case CRYPTO_ALG_TYPE_DIGEST:
  232. len = crypto_digest_ctxsize(alg, flags);
  233. break;
  234. case CRYPTO_ALG_TYPE_COMPRESS:
  235. len = crypto_compress_ctxsize(alg, flags);
  236. break;
  237. }
  238. return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  239. }
  240. void crypto_shoot_alg(struct crypto_alg *alg)
  241. {
  242. down_write(&crypto_alg_sem);
  243. alg->cra_flags |= CRYPTO_ALG_DYING;
  244. up_write(&crypto_alg_sem);
  245. }
  246. EXPORT_SYMBOL_GPL(crypto_shoot_alg);
  247. struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 flags)
  248. {
  249. struct crypto_tfm *tfm = NULL;
  250. unsigned int tfm_size;
  251. int err = -ENOMEM;
  252. tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
  253. tfm = kzalloc(tfm_size, GFP_KERNEL);
  254. if (tfm == NULL)
  255. goto out;
  256. tfm->__crt_alg = alg;
  257. err = crypto_init_flags(tfm, flags);
  258. if (err)
  259. goto out_free_tfm;
  260. err = crypto_init_ops(tfm);
  261. if (err)
  262. goto out_free_tfm;
  263. if (alg->cra_init && (err = alg->cra_init(tfm))) {
  264. if (err == -EAGAIN)
  265. crypto_shoot_alg(alg);
  266. goto cra_init_failed;
  267. }
  268. goto out;
  269. cra_init_failed:
  270. crypto_exit_ops(tfm);
  271. out_free_tfm:
  272. kfree(tfm);
  273. tfm = ERR_PTR(err);
  274. out:
  275. return tfm;
  276. }
  277. EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);
  278. struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
  279. {
  280. struct crypto_tfm *tfm = NULL;
  281. int err;
  282. do {
  283. struct crypto_alg *alg;
  284. alg = crypto_alg_mod_lookup(name, 0, 0);
  285. err = PTR_ERR(alg);
  286. if (IS_ERR(alg))
  287. continue;
  288. tfm = __crypto_alloc_tfm(alg, flags);
  289. err = 0;
  290. if (IS_ERR(tfm)) {
  291. crypto_mod_put(alg);
  292. err = PTR_ERR(tfm);
  293. tfm = NULL;
  294. }
  295. } while (err == -EAGAIN && !signal_pending(current));
  296. return tfm;
  297. }
  298. void crypto_free_tfm(struct crypto_tfm *tfm)
  299. {
  300. struct crypto_alg *alg;
  301. int size;
  302. if (unlikely(!tfm))
  303. return;
  304. alg = tfm->__crt_alg;
  305. size = sizeof(*tfm) + alg->cra_ctxsize;
  306. if (alg->cra_exit)
  307. alg->cra_exit(tfm);
  308. crypto_exit_ops(tfm);
  309. crypto_mod_put(alg);
  310. memset(tfm, 0, size);
  311. kfree(tfm);
  312. }
  313. int crypto_alg_available(const char *name, u32 flags)
  314. {
  315. int ret = 0;
  316. struct crypto_alg *alg = crypto_alg_mod_lookup(name, 0, 0);
  317. if (!IS_ERR(alg)) {
  318. crypto_mod_put(alg);
  319. ret = 1;
  320. }
  321. return ret;
  322. }
  323. EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
  324. EXPORT_SYMBOL_GPL(crypto_free_tfm);
  325. EXPORT_SYMBOL_GPL(crypto_alg_available);