api.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 = 0;
  59. switch (crypto_tfm_alg_type(tfm)) {
  60. case CRYPTO_ALG_TYPE_CIPHER:
  61. return crypto_init_cipher_flags(tfm, flags);
  62. case CRYPTO_ALG_TYPE_DIGEST:
  63. return crypto_init_digest_flags(tfm, flags);
  64. case CRYPTO_ALG_TYPE_COMPRESS:
  65. return crypto_init_compress_flags(tfm, flags);
  66. default:
  67. break;
  68. }
  69. BUG();
  70. return -EINVAL;
  71. }
  72. static int crypto_init_ops(struct crypto_tfm *tfm)
  73. {
  74. switch (crypto_tfm_alg_type(tfm)) {
  75. case CRYPTO_ALG_TYPE_CIPHER:
  76. return crypto_init_cipher_ops(tfm);
  77. case CRYPTO_ALG_TYPE_DIGEST:
  78. return crypto_init_digest_ops(tfm);
  79. case CRYPTO_ALG_TYPE_COMPRESS:
  80. return crypto_init_compress_ops(tfm);
  81. default:
  82. break;
  83. }
  84. BUG();
  85. return -EINVAL;
  86. }
  87. static void crypto_exit_ops(struct crypto_tfm *tfm)
  88. {
  89. switch (crypto_tfm_alg_type(tfm)) {
  90. case CRYPTO_ALG_TYPE_CIPHER:
  91. crypto_exit_cipher_ops(tfm);
  92. break;
  93. case CRYPTO_ALG_TYPE_DIGEST:
  94. crypto_exit_digest_ops(tfm);
  95. break;
  96. case CRYPTO_ALG_TYPE_COMPRESS:
  97. crypto_exit_compress_ops(tfm);
  98. break;
  99. default:
  100. BUG();
  101. }
  102. }
  103. static unsigned int crypto_ctxsize(struct crypto_alg *alg, int flags)
  104. {
  105. unsigned int len;
  106. switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  107. default:
  108. BUG();
  109. case CRYPTO_ALG_TYPE_CIPHER:
  110. len = crypto_cipher_ctxsize(alg, flags);
  111. break;
  112. case CRYPTO_ALG_TYPE_DIGEST:
  113. len = crypto_digest_ctxsize(alg, flags);
  114. break;
  115. case CRYPTO_ALG_TYPE_COMPRESS:
  116. len = crypto_compress_ctxsize(alg, flags);
  117. break;
  118. }
  119. return len + alg->cra_alignmask;
  120. }
  121. struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
  122. {
  123. struct crypto_tfm *tfm = NULL;
  124. struct crypto_alg *alg;
  125. unsigned int tfm_size;
  126. alg = crypto_alg_mod_lookup(name);
  127. if (alg == NULL)
  128. goto out;
  129. tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, flags);
  130. tfm = kmalloc(tfm_size, GFP_KERNEL);
  131. if (tfm == NULL)
  132. goto out_put;
  133. memset(tfm, 0, tfm_size);
  134. tfm->__crt_alg = alg;
  135. if (crypto_init_flags(tfm, flags))
  136. goto out_free_tfm;
  137. if (crypto_init_ops(tfm)) {
  138. crypto_exit_ops(tfm);
  139. goto out_free_tfm;
  140. }
  141. goto out;
  142. out_free_tfm:
  143. kfree(tfm);
  144. tfm = NULL;
  145. out_put:
  146. crypto_alg_put(alg);
  147. out:
  148. return tfm;
  149. }
  150. void crypto_free_tfm(struct crypto_tfm *tfm)
  151. {
  152. struct crypto_alg *alg;
  153. int size;
  154. if (unlikely(!tfm))
  155. return;
  156. alg = tfm->__crt_alg;
  157. size = sizeof(*tfm) + alg->cra_ctxsize;
  158. crypto_exit_ops(tfm);
  159. crypto_alg_put(alg);
  160. memset(tfm, 0, size);
  161. kfree(tfm);
  162. }
  163. int crypto_register_alg(struct crypto_alg *alg)
  164. {
  165. int ret = 0;
  166. struct crypto_alg *q;
  167. if (alg->cra_alignmask & (alg->cra_alignmask + 1))
  168. return -EINVAL;
  169. if (alg->cra_alignmask > PAGE_SIZE)
  170. return -EINVAL;
  171. down_write(&crypto_alg_sem);
  172. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  173. if (!(strcmp(q->cra_name, alg->cra_name))) {
  174. ret = -EEXIST;
  175. goto out;
  176. }
  177. }
  178. list_add_tail(&alg->cra_list, &crypto_alg_list);
  179. out:
  180. up_write(&crypto_alg_sem);
  181. return ret;
  182. }
  183. int crypto_unregister_alg(struct crypto_alg *alg)
  184. {
  185. int ret = -ENOENT;
  186. struct crypto_alg *q;
  187. BUG_ON(!alg->cra_module);
  188. down_write(&crypto_alg_sem);
  189. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  190. if (alg == q) {
  191. list_del(&alg->cra_list);
  192. ret = 0;
  193. goto out;
  194. }
  195. }
  196. out:
  197. up_write(&crypto_alg_sem);
  198. return ret;
  199. }
  200. int crypto_alg_available(const char *name, u32 flags)
  201. {
  202. int ret = 0;
  203. struct crypto_alg *alg = crypto_alg_mod_lookup(name);
  204. if (alg) {
  205. crypto_alg_put(alg);
  206. ret = 1;
  207. }
  208. return ret;
  209. }
  210. static int __init init_crypto(void)
  211. {
  212. printk(KERN_INFO "Initializing Cryptographic API\n");
  213. crypto_init_proc();
  214. return 0;
  215. }
  216. __initcall(init_crypto);
  217. EXPORT_SYMBOL_GPL(crypto_register_alg);
  218. EXPORT_SYMBOL_GPL(crypto_unregister_alg);
  219. EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
  220. EXPORT_SYMBOL_GPL(crypto_free_tfm);
  221. EXPORT_SYMBOL_GPL(crypto_alg_available);