api.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. struct crypto_tfm *crypto_alloc_tfm(const char *name, u32 flags)
  103. {
  104. struct crypto_tfm *tfm = NULL;
  105. struct crypto_alg *alg;
  106. alg = crypto_alg_mod_lookup(name);
  107. if (alg == NULL)
  108. goto out;
  109. tfm = kmalloc(sizeof(*tfm) + alg->cra_ctxsize, GFP_KERNEL);
  110. if (tfm == NULL)
  111. goto out_put;
  112. memset(tfm, 0, sizeof(*tfm) + alg->cra_ctxsize);
  113. tfm->__crt_alg = alg;
  114. if (crypto_init_flags(tfm, flags))
  115. goto out_free_tfm;
  116. if (crypto_init_ops(tfm)) {
  117. crypto_exit_ops(tfm);
  118. goto out_free_tfm;
  119. }
  120. goto out;
  121. out_free_tfm:
  122. kfree(tfm);
  123. tfm = NULL;
  124. out_put:
  125. crypto_alg_put(alg);
  126. out:
  127. return tfm;
  128. }
  129. void crypto_free_tfm(struct crypto_tfm *tfm)
  130. {
  131. struct crypto_alg *alg = tfm->__crt_alg;
  132. int size = sizeof(*tfm) + alg->cra_ctxsize;
  133. crypto_exit_ops(tfm);
  134. crypto_alg_put(alg);
  135. memset(tfm, 0, size);
  136. kfree(tfm);
  137. }
  138. int crypto_register_alg(struct crypto_alg *alg)
  139. {
  140. int ret = 0;
  141. struct crypto_alg *q;
  142. if (alg->cra_alignmask & (alg->cra_alignmask + 1))
  143. return -EINVAL;
  144. if (alg->cra_alignmask > PAGE_SIZE)
  145. return -EINVAL;
  146. down_write(&crypto_alg_sem);
  147. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  148. if (!(strcmp(q->cra_name, alg->cra_name))) {
  149. ret = -EEXIST;
  150. goto out;
  151. }
  152. }
  153. list_add_tail(&alg->cra_list, &crypto_alg_list);
  154. out:
  155. up_write(&crypto_alg_sem);
  156. return ret;
  157. }
  158. int crypto_unregister_alg(struct crypto_alg *alg)
  159. {
  160. int ret = -ENOENT;
  161. struct crypto_alg *q;
  162. BUG_ON(!alg->cra_module);
  163. down_write(&crypto_alg_sem);
  164. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  165. if (alg == q) {
  166. list_del(&alg->cra_list);
  167. ret = 0;
  168. goto out;
  169. }
  170. }
  171. out:
  172. up_write(&crypto_alg_sem);
  173. return ret;
  174. }
  175. int crypto_alg_available(const char *name, u32 flags)
  176. {
  177. int ret = 0;
  178. struct crypto_alg *alg = crypto_alg_mod_lookup(name);
  179. if (alg) {
  180. crypto_alg_put(alg);
  181. ret = 1;
  182. }
  183. return ret;
  184. }
  185. static int __init init_crypto(void)
  186. {
  187. printk(KERN_INFO "Initializing Cryptographic API\n");
  188. crypto_init_proc();
  189. return 0;
  190. }
  191. __initcall(init_crypto);
  192. EXPORT_SYMBOL_GPL(crypto_register_alg);
  193. EXPORT_SYMBOL_GPL(crypto_unregister_alg);
  194. EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
  195. EXPORT_SYMBOL_GPL(crypto_free_tfm);
  196. EXPORT_SYMBOL_GPL(crypto_alg_available);