api.c 5.1 KB

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