api.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. *
  7. * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
  8. * and Nettle, by Niels Möller.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option)
  13. * any later version.
  14. *
  15. */
  16. #include <linux/compiler.h>
  17. #include <linux/init.h>
  18. #include <linux/crypto.h>
  19. #include <linux/errno.h>
  20. #include <linux/kmod.h>
  21. #include <linux/rwsem.h>
  22. #include <linux/slab.h>
  23. #include "internal.h"
  24. LIST_HEAD(crypto_alg_list);
  25. DECLARE_RWSEM(crypto_alg_sem);
  26. static inline int crypto_alg_get(struct crypto_alg *alg)
  27. {
  28. return try_module_get(alg->cra_module);
  29. }
  30. static inline void crypto_alg_put(struct crypto_alg *alg)
  31. {
  32. module_put(alg->cra_module);
  33. }
  34. static struct crypto_alg *crypto_alg_lookup(const char *name)
  35. {
  36. struct crypto_alg *q, *alg = NULL;
  37. if (!name)
  38. return NULL;
  39. down_read(&crypto_alg_sem);
  40. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  41. if (!(strcmp(q->cra_name, name))) {
  42. if (crypto_alg_get(q))
  43. alg = q;
  44. break;
  45. }
  46. }
  47. up_read(&crypto_alg_sem);
  48. return alg;
  49. }
  50. /* A far more intelligent version of this is planned. For now, just
  51. * try an exact match on the name of the algorithm. */
  52. static inline struct crypto_alg *crypto_alg_mod_lookup(const char *name)
  53. {
  54. return try_then_request_module(crypto_alg_lookup(name), name);
  55. }
  56. static int crypto_init_flags(struct crypto_tfm *tfm, u32 flags)
  57. {
  58. tfm->crt_flags = flags & CRYPTO_TFM_REQ_MASK;
  59. flags &= ~CRYPTO_TFM_REQ_MASK;
  60. switch (crypto_tfm_alg_type(tfm)) {
  61. case CRYPTO_ALG_TYPE_CIPHER:
  62. return crypto_init_cipher_flags(tfm, flags);
  63. case CRYPTO_ALG_TYPE_DIGEST:
  64. return crypto_init_digest_flags(tfm, flags);
  65. case CRYPTO_ALG_TYPE_COMPRESS:
  66. return crypto_init_compress_flags(tfm, flags);
  67. default:
  68. break;
  69. }
  70. BUG();
  71. return -EINVAL;
  72. }
  73. static int crypto_init_ops(struct crypto_tfm *tfm)
  74. {
  75. switch (crypto_tfm_alg_type(tfm)) {
  76. case CRYPTO_ALG_TYPE_CIPHER:
  77. return crypto_init_cipher_ops(tfm);
  78. case CRYPTO_ALG_TYPE_DIGEST:
  79. return crypto_init_digest_ops(tfm);
  80. case CRYPTO_ALG_TYPE_COMPRESS:
  81. return crypto_init_compress_ops(tfm);
  82. default:
  83. break;
  84. }
  85. BUG();
  86. return -EINVAL;
  87. }
  88. static void crypto_exit_ops(struct crypto_tfm *tfm)
  89. {
  90. switch (crypto_tfm_alg_type(tfm)) {
  91. case CRYPTO_ALG_TYPE_CIPHER:
  92. crypto_exit_cipher_ops(tfm);
  93. break;
  94. case CRYPTO_ALG_TYPE_DIGEST:
  95. crypto_exit_digest_ops(tfm);
  96. break;
  97. case CRYPTO_ALG_TYPE_COMPRESS:
  98. crypto_exit_compress_ops(tfm);
  99. break;
  100. default:
  101. BUG();
  102. }
  103. }
  104. static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
  105. {
  106. unsigned int len;
  107. switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  108. default:
  109. BUG();
  110. case CRYPTO_ALG_TYPE_CIPHER:
  111. len = crypto_cipher_ctxsize(alg, flags);
  112. break;
  113. case CRYPTO_ALG_TYPE_DIGEST:
  114. len = crypto_digest_ctxsize(alg, flags);
  115. break;
  116. case CRYPTO_ALG_TYPE_COMPRESS:
  117. len = crypto_compress_ctxsize(alg, flags);
  118. break;
  119. }
  120. return len + alg->cra_alignmask;
  121. }
  122. struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
  123. {
  124. struct crypto_tfm *tfm = NULL;
  125. struct crypto_alg *alg;
  126. unsigned int tfm_size;
  127. alg = crypto_alg_mod_lookup(name);
  128. if (alg == NULL)
  129. goto out;
  130. tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
  131. tfm = kmalloc(tfm_size, GFP_KERNEL);
  132. if (tfm == NULL)
  133. goto out_put;
  134. memset(tfm, 0, tfm_size);
  135. tfm->__crt_alg = alg;
  136. if (crypto_init_flags(tfm, flags))
  137. goto out_free_tfm;
  138. if (crypto_init_ops(tfm)) {
  139. crypto_exit_ops(tfm);
  140. goto out_free_tfm;
  141. }
  142. goto out;
  143. out_free_tfm:
  144. kfree(tfm);
  145. tfm = NULL;
  146. out_put:
  147. crypto_alg_put(alg);
  148. out:
  149. return tfm;
  150. }
  151. void crypto_free_tfm(struct crypto_tfm *tfm)
  152. {
  153. struct crypto_alg *alg;
  154. int size;
  155. if (unlikely(!tfm))
  156. return;
  157. alg = tfm->__crt_alg;
  158. size = sizeof(*tfm) + alg->cra_ctxsize;
  159. crypto_exit_ops(tfm);
  160. crypto_alg_put(alg);
  161. memset(tfm, 0, size);
  162. kfree(tfm);
  163. }
  164. int crypto_register_alg(struct crypto_alg *alg)
  165. {
  166. int ret = 0;
  167. struct crypto_alg *q;
  168. if (alg->cra_alignmask & (alg->cra_alignmask + 1))
  169. return -EINVAL;
  170. if (alg->cra_alignmask > PAGE_SIZE)
  171. return -EINVAL;
  172. down_write(&crypto_alg_sem);
  173. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  174. if (!(strcmp(q->cra_name, alg->cra_name))) {
  175. ret = -EEXIST;
  176. goto out;
  177. }
  178. }
  179. list_add_tail(&alg->cra_list, &crypto_alg_list);
  180. out:
  181. up_write(&crypto_alg_sem);
  182. return ret;
  183. }
  184. int crypto_unregister_alg(struct crypto_alg *alg)
  185. {
  186. int ret = -ENOENT;
  187. struct crypto_alg *q;
  188. BUG_ON(!alg->cra_module);
  189. down_write(&crypto_alg_sem);
  190. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  191. if (alg == q) {
  192. list_del(&alg->cra_list);
  193. ret = 0;
  194. goto out;
  195. }
  196. }
  197. out:
  198. up_write(&crypto_alg_sem);
  199. return ret;
  200. }
  201. int crypto_alg_available(const char *name, u32 flags)
  202. {
  203. int ret = 0;
  204. struct crypto_alg *alg = crypto_alg_mod_lookup(name);
  205. if (alg) {
  206. crypto_alg_put(alg);
  207. ret = 1;
  208. }
  209. return ret;
  210. }
  211. static int __init init_crypto(void)
  212. {
  213. printk(KERN_INFO "Initializing Cryptographic API\n");
  214. crypto_init_proc();
  215. return 0;
  216. }
  217. __initcall(init_crypto);
  218. EXPORT_SYMBOL_GPL(crypto_register_alg);
  219. EXPORT_SYMBOL_GPL(crypto_unregister_alg);
  220. EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
  221. EXPORT_SYMBOL_GPL(crypto_free_tfm);
  222. EXPORT_SYMBOL_GPL(crypto_alg_available);