api.c 5.3 KB

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