api.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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/errno.h>
  18. #include <linux/kernel.h>
  19. #include <linux/kmod.h>
  20. #include <linux/module.h>
  21. #include <linux/param.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include "internal.h"
  25. LIST_HEAD(crypto_alg_list);
  26. EXPORT_SYMBOL_GPL(crypto_alg_list);
  27. DECLARE_RWSEM(crypto_alg_sem);
  28. EXPORT_SYMBOL_GPL(crypto_alg_sem);
  29. BLOCKING_NOTIFIER_HEAD(crypto_chain);
  30. EXPORT_SYMBOL_GPL(crypto_chain);
  31. static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
  32. {
  33. atomic_inc(&alg->cra_refcnt);
  34. return alg;
  35. }
  36. static inline void crypto_alg_put(struct crypto_alg *alg)
  37. {
  38. if (atomic_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy)
  39. alg->cra_destroy(alg);
  40. }
  41. struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
  42. {
  43. return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
  44. }
  45. EXPORT_SYMBOL_GPL(crypto_mod_get);
  46. void crypto_mod_put(struct crypto_alg *alg)
  47. {
  48. crypto_alg_put(alg);
  49. module_put(alg->cra_module);
  50. }
  51. EXPORT_SYMBOL_GPL(crypto_mod_put);
  52. struct crypto_alg *__crypto_alg_lookup(const char *name)
  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. exact = !strcmp(q->cra_driver_name, name);
  59. fuzzy = !strcmp(q->cra_name, name);
  60. if (!exact && !(fuzzy && q->cra_priority > best))
  61. continue;
  62. if (unlikely(!crypto_mod_get(q)))
  63. continue;
  64. best = q->cra_priority;
  65. if (alg)
  66. crypto_mod_put(alg);
  67. alg = q;
  68. if (exact)
  69. break;
  70. }
  71. return alg;
  72. }
  73. EXPORT_SYMBOL_GPL(__crypto_alg_lookup);
  74. static void crypto_larval_destroy(struct crypto_alg *alg)
  75. {
  76. struct crypto_larval *larval = (void *)alg;
  77. BUG_ON(!crypto_is_larval(alg));
  78. if (larval->adult)
  79. crypto_mod_put(larval->adult);
  80. kfree(larval);
  81. }
  82. static struct crypto_alg *crypto_larval_alloc(const char *name)
  83. {
  84. struct crypto_alg *alg;
  85. struct crypto_larval *larval;
  86. larval = kzalloc(sizeof(*larval), GFP_KERNEL);
  87. if (!larval)
  88. return NULL;
  89. larval->alg.cra_flags = CRYPTO_ALG_LARVAL;
  90. larval->alg.cra_priority = -1;
  91. larval->alg.cra_destroy = crypto_larval_destroy;
  92. atomic_set(&larval->alg.cra_refcnt, 2);
  93. strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
  94. init_completion(&larval->completion);
  95. down_write(&crypto_alg_sem);
  96. alg = __crypto_alg_lookup(name);
  97. if (!alg) {
  98. alg = &larval->alg;
  99. list_add(&alg->cra_list, &crypto_alg_list);
  100. }
  101. up_write(&crypto_alg_sem);
  102. if (alg != &larval->alg)
  103. kfree(larval);
  104. return alg;
  105. }
  106. static void crypto_larval_kill(struct crypto_alg *alg)
  107. {
  108. struct crypto_larval *larval = (void *)alg;
  109. down_write(&crypto_alg_sem);
  110. list_del(&alg->cra_list);
  111. up_write(&crypto_alg_sem);
  112. complete(&larval->completion);
  113. crypto_alg_put(alg);
  114. }
  115. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
  116. {
  117. struct crypto_larval *larval = (void *)alg;
  118. wait_for_completion_interruptible_timeout(&larval->completion, 60 * HZ);
  119. alg = larval->adult;
  120. if (alg && !crypto_mod_get(alg))
  121. alg = NULL;
  122. crypto_mod_put(&larval->alg);
  123. return alg;
  124. }
  125. static struct crypto_alg *crypto_alg_lookup(const char *name)
  126. {
  127. struct crypto_alg *alg;
  128. if (!name)
  129. return NULL;
  130. down_read(&crypto_alg_sem);
  131. alg = __crypto_alg_lookup(name);
  132. up_read(&crypto_alg_sem);
  133. return alg;
  134. }
  135. /* A far more intelligent version of this is planned. For now, just
  136. * try an exact match on the name of the algorithm. */
  137. static struct crypto_alg *crypto_alg_mod_lookup(const char *name)
  138. {
  139. struct crypto_alg *alg;
  140. struct crypto_alg *larval;
  141. int ok;
  142. alg = try_then_request_module(crypto_alg_lookup(name), name);
  143. if (alg)
  144. return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;
  145. larval = crypto_larval_alloc(name);
  146. if (!larval || !crypto_is_larval(larval))
  147. return larval;
  148. ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval);
  149. if (ok == NOTIFY_DONE) {
  150. request_module("cryptomgr");
  151. ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval);
  152. }
  153. if (ok == NOTIFY_STOP)
  154. alg = crypto_larval_wait(larval);
  155. else {
  156. crypto_mod_put(larval);
  157. alg = NULL;
  158. }
  159. crypto_larval_kill(larval);
  160. return alg;
  161. }
  162. static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
  163. {
  164. tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK;
  165. flags &= ~CRYPTO_TFM_REQ_MASK;
  166. switch (crypto_tfm_alg_type(tfm)) {
  167. case CRYPTO_ALG_TYPE_CIPHER:
  168. return crypto_init_cipher_flags(tfm, flags);
  169. case CRYPTO_ALG_TYPE_DIGEST:
  170. return crypto_init_digest_flags(tfm, flags);
  171. case CRYPTO_ALG_TYPE_COMPRESS:
  172. return crypto_init_compress_flags(tfm, flags);
  173. default:
  174. break;
  175. }
  176. BUG();
  177. return -EINVAL;
  178. }
  179. static int crypto_init_ops(struct crypto_tfm *tfm)
  180. {
  181. switch (crypto_tfm_alg_type(tfm)) {
  182. case CRYPTO_ALG_TYPE_CIPHER:
  183. return crypto_init_cipher_ops(tfm);
  184. case CRYPTO_ALG_TYPE_DIGEST:
  185. return crypto_init_digest_ops(tfm);
  186. case CRYPTO_ALG_TYPE_COMPRESS:
  187. return crypto_init_compress_ops(tfm);
  188. default:
  189. break;
  190. }
  191. BUG();
  192. return -EINVAL;
  193. }
  194. static void crypto_exit_ops(struct crypto_tfm *tfm)
  195. {
  196. switch (crypto_tfm_alg_type(tfm)) {
  197. case CRYPTO_ALG_TYPE_CIPHER:
  198. crypto_exit_cipher_ops(tfm);
  199. break;
  200. case CRYPTO_ALG_TYPE_DIGEST:
  201. crypto_exit_digest_ops(tfm);
  202. break;
  203. case CRYPTO_ALG_TYPE_COMPRESS:
  204. crypto_exit_compress_ops(tfm);
  205. break;
  206. default:
  207. BUG();
  208. }
  209. }
  210. static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
  211. {
  212. unsigned int len;
  213. switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  214. default:
  215. BUG();
  216. case CRYPTO_ALG_TYPE_CIPHER:
  217. len = crypto_cipher_ctxsize(alg, flags);
  218. break;
  219. case CRYPTO_ALG_TYPE_DIGEST:
  220. len = crypto_digest_ctxsize(alg, flags);
  221. break;
  222. case CRYPTO_ALG_TYPE_COMPRESS:
  223. len = crypto_compress_ctxsize(alg, flags);
  224. break;
  225. }
  226. return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  227. }
  228. struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
  229. {
  230. struct crypto_tfm *tfm = NULL;
  231. struct crypto_alg *alg;
  232. unsigned int tfm_size;
  233. alg = crypto_alg_mod_lookup(name);
  234. if (alg == NULL)
  235. goto out;
  236. tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
  237. tfm = kzalloc(tfm_size, GFP_KERNEL);
  238. if (tfm == NULL)
  239. goto out_put;
  240. tfm->__crt_alg = alg;
  241. if (crypto_init_flags(tfm, flags))
  242. goto out_free_tfm;
  243. if (crypto_init_ops(tfm))
  244. goto out_free_tfm;
  245. if (alg->cra_init && alg->cra_init(tfm))
  246. goto cra_init_failed;
  247. goto out;
  248. cra_init_failed:
  249. crypto_exit_ops(tfm);
  250. out_free_tfm:
  251. kfree(tfm);
  252. tfm = NULL;
  253. out_put:
  254. crypto_mod_put(alg);
  255. out:
  256. return tfm;
  257. }
  258. void crypto_free_tfm(struct crypto_tfm *tfm)
  259. {
  260. struct crypto_alg *alg;
  261. int size;
  262. if (unlikely(!tfm))
  263. return;
  264. alg = tfm->__crt_alg;
  265. size = sizeof(*tfm) + alg->cra_ctxsize;
  266. if (alg->cra_exit)
  267. alg->cra_exit(tfm);
  268. crypto_exit_ops(tfm);
  269. crypto_mod_put(alg);
  270. memset(tfm, 0, size);
  271. kfree(tfm);
  272. }
  273. int crypto_alg_available(const char *name, u32 flags)
  274. {
  275. int ret = 0;
  276. struct crypto_alg *alg = crypto_alg_mod_lookup(name);
  277. if (alg) {
  278. crypto_mod_put(alg);
  279. ret = 1;
  280. }
  281. return ret;
  282. }
  283. EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
  284. EXPORT_SYMBOL_GPL(crypto_free_tfm);
  285. EXPORT_SYMBOL_GPL(crypto_alg_available);