api.c 4.4 KB

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