api.c 6.8 KB

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