api.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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, u32 type, 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 ((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 NULL;
  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. static 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(&larval->completion);
  120. crypto_alg_put(alg);
  121. }
  122. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
  123. {
  124. struct crypto_larval *larval = (void *)alg;
  125. wait_for_completion_interruptible_timeout(&larval->completion, 60 * HZ);
  126. alg = larval->adult;
  127. if (alg && !crypto_mod_get(alg))
  128. alg = NULL;
  129. crypto_mod_put(&larval->alg);
  130. return alg;
  131. }
  132. static struct crypto_alg *crypto_alg_lookup(const char *name, u32 type,
  133. u32 mask)
  134. {
  135. struct crypto_alg *alg;
  136. if (!name)
  137. return NULL;
  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. mask &= ~CRYPTO_ALG_LARVAL;
  149. type &= mask;
  150. alg = try_then_request_module(crypto_alg_lookup(name, type, mask),
  151. name);
  152. if (alg)
  153. return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;
  154. larval = crypto_larval_alloc(name, type, mask);
  155. if (!larval || !crypto_is_larval(larval))
  156. return larval;
  157. ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval);
  158. if (ok == NOTIFY_DONE) {
  159. request_module("cryptomgr");
  160. ok = crypto_notify(CRYPTO_MSG_ALG_REQUEST, larval);
  161. }
  162. if (ok == NOTIFY_STOP)
  163. alg = crypto_larval_wait(larval);
  164. else {
  165. crypto_mod_put(larval);
  166. alg = NULL;
  167. }
  168. crypto_larval_kill(larval);
  169. return alg;
  170. }
  171. EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
  172. static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
  173. {
  174. tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK;
  175. flags &= ~CRYPTO_TFM_REQ_MASK;
  176. switch (crypto_tfm_alg_type(tfm)) {
  177. case CRYPTO_ALG_TYPE_CIPHER:
  178. return crypto_init_cipher_flags(tfm, flags);
  179. case CRYPTO_ALG_TYPE_DIGEST:
  180. return crypto_init_digest_flags(tfm, flags);
  181. case CRYPTO_ALG_TYPE_COMPRESS:
  182. return crypto_init_compress_flags(tfm, flags);
  183. default:
  184. break;
  185. }
  186. BUG();
  187. return -EINVAL;
  188. }
  189. static int crypto_init_ops(struct crypto_tfm *tfm)
  190. {
  191. switch (crypto_tfm_alg_type(tfm)) {
  192. case CRYPTO_ALG_TYPE_CIPHER:
  193. return crypto_init_cipher_ops(tfm);
  194. case CRYPTO_ALG_TYPE_DIGEST:
  195. return crypto_init_digest_ops(tfm);
  196. case CRYPTO_ALG_TYPE_COMPRESS:
  197. return crypto_init_compress_ops(tfm);
  198. default:
  199. break;
  200. }
  201. BUG();
  202. return -EINVAL;
  203. }
  204. static void crypto_exit_ops(struct crypto_tfm *tfm)
  205. {
  206. switch (crypto_tfm_alg_type(tfm)) {
  207. case CRYPTO_ALG_TYPE_CIPHER:
  208. crypto_exit_cipher_ops(tfm);
  209. break;
  210. case CRYPTO_ALG_TYPE_DIGEST:
  211. crypto_exit_digest_ops(tfm);
  212. break;
  213. case CRYPTO_ALG_TYPE_COMPRESS:
  214. crypto_exit_compress_ops(tfm);
  215. break;
  216. default:
  217. BUG();
  218. }
  219. }
  220. static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
  221. {
  222. unsigned int len;
  223. switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  224. default:
  225. BUG();
  226. case CRYPTO_ALG_TYPE_CIPHER:
  227. len = crypto_cipher_ctxsize(alg, flags);
  228. break;
  229. case CRYPTO_ALG_TYPE_DIGEST:
  230. len = crypto_digest_ctxsize(alg, flags);
  231. break;
  232. case CRYPTO_ALG_TYPE_COMPRESS:
  233. len = crypto_compress_ctxsize(alg, flags);
  234. break;
  235. }
  236. return len + (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  237. }
  238. struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
  239. {
  240. struct crypto_tfm *tfm = NULL;
  241. struct crypto_alg *alg;
  242. unsigned int tfm_size;
  243. alg = crypto_alg_mod_lookup(name, 0, 0);
  244. if (alg == NULL)
  245. goto out;
  246. tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
  247. tfm = kzalloc(tfm_size, GFP_KERNEL);
  248. if (tfm == NULL)
  249. goto out_put;
  250. tfm->__crt_alg = alg;
  251. if (crypto_init_flags(tfm, flags))
  252. goto out_free_tfm;
  253. if (crypto_init_ops(tfm))
  254. goto out_free_tfm;
  255. if (alg->cra_init && alg->cra_init(tfm))
  256. goto cra_init_failed;
  257. goto out;
  258. cra_init_failed:
  259. crypto_exit_ops(tfm);
  260. out_free_tfm:
  261. kfree(tfm);
  262. tfm = NULL;
  263. out_put:
  264. crypto_mod_put(alg);
  265. out:
  266. return tfm;
  267. }
  268. void crypto_free_tfm(struct crypto_tfm *tfm)
  269. {
  270. struct crypto_alg *alg;
  271. int size;
  272. if (unlikely(!tfm))
  273. return;
  274. alg = tfm->__crt_alg;
  275. size = sizeof(*tfm) + alg->cra_ctxsize;
  276. if (alg->cra_exit)
  277. alg->cra_exit(tfm);
  278. crypto_exit_ops(tfm);
  279. crypto_mod_put(alg);
  280. memset(tfm, 0, size);
  281. kfree(tfm);
  282. }
  283. int crypto_alg_available(const char *name, u32 flags)
  284. {
  285. int ret = 0;
  286. struct crypto_alg *alg = crypto_alg_mod_lookup(name, 0, 0);
  287. if (alg) {
  288. crypto_mod_put(alg);
  289. ret = 1;
  290. }
  291. return ret;
  292. }
  293. EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
  294. EXPORT_SYMBOL_GPL(crypto_free_tfm);
  295. EXPORT_SYMBOL_GPL(crypto_alg_available);