api.c 6.3 KB

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