algapi.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Cryptographic API for algorithms (i.e., low-level API).
  3. *
  4. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <linux/string.h>
  18. #include "internal.h"
  19. static LIST_HEAD(crypto_template_list);
  20. void crypto_larval_error(const char *name)
  21. {
  22. struct crypto_alg *alg;
  23. down_read(&crypto_alg_sem);
  24. alg = __crypto_alg_lookup(name);
  25. up_read(&crypto_alg_sem);
  26. if (alg) {
  27. if (crypto_is_larval(alg)) {
  28. struct crypto_larval *larval = (void *)alg;
  29. complete(&larval->completion);
  30. }
  31. crypto_mod_put(alg);
  32. }
  33. }
  34. EXPORT_SYMBOL_GPL(crypto_larval_error);
  35. static inline int crypto_set_driver_name(struct crypto_alg *alg)
  36. {
  37. static const char suffix[] = "-generic";
  38. char *driver_name = alg->cra_driver_name;
  39. int len;
  40. if (*driver_name)
  41. return 0;
  42. len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  43. if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
  44. return -ENAMETOOLONG;
  45. memcpy(driver_name + len, suffix, sizeof(suffix));
  46. return 0;
  47. }
  48. static int crypto_check_alg(struct crypto_alg *alg)
  49. {
  50. if (alg->cra_alignmask & (alg->cra_alignmask + 1))
  51. return -EINVAL;
  52. if (alg->cra_alignmask & alg->cra_blocksize)
  53. return -EINVAL;
  54. if (alg->cra_blocksize > PAGE_SIZE / 8)
  55. return -EINVAL;
  56. if (alg->cra_priority < 0)
  57. return -EINVAL;
  58. return crypto_set_driver_name(alg);
  59. }
  60. static int __crypto_register_alg(struct crypto_alg *alg)
  61. {
  62. struct crypto_alg *q;
  63. int ret = -EEXIST;
  64. atomic_set(&alg->cra_refcnt, 1);
  65. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  66. if (q == alg)
  67. goto out;
  68. if (crypto_is_larval(q) &&
  69. (!strcmp(alg->cra_name, q->cra_name) ||
  70. !strcmp(alg->cra_driver_name, q->cra_name))) {
  71. struct crypto_larval *larval = (void *)q;
  72. if (!crypto_mod_get(alg))
  73. continue;
  74. larval->adult = alg;
  75. complete(&larval->completion);
  76. }
  77. }
  78. list_add(&alg->cra_list, &crypto_alg_list);
  79. crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg);
  80. ret = 0;
  81. out:
  82. return ret;
  83. }
  84. int crypto_register_alg(struct crypto_alg *alg)
  85. {
  86. int err;
  87. err = crypto_check_alg(alg);
  88. if (err)
  89. return err;
  90. down_write(&crypto_alg_sem);
  91. err = __crypto_register_alg(alg);
  92. up_write(&crypto_alg_sem);
  93. return err;
  94. }
  95. EXPORT_SYMBOL_GPL(crypto_register_alg);
  96. int crypto_unregister_alg(struct crypto_alg *alg)
  97. {
  98. int ret = -ENOENT;
  99. down_write(&crypto_alg_sem);
  100. if (likely(!list_empty(&alg->cra_list))) {
  101. list_del_init(&alg->cra_list);
  102. ret = 0;
  103. }
  104. crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
  105. up_write(&crypto_alg_sem);
  106. if (ret)
  107. return ret;
  108. BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
  109. if (alg->cra_destroy)
  110. alg->cra_destroy(alg);
  111. return 0;
  112. }
  113. EXPORT_SYMBOL_GPL(crypto_unregister_alg);
  114. int crypto_register_template(struct crypto_template *tmpl)
  115. {
  116. struct crypto_template *q;
  117. int err = -EEXIST;
  118. down_write(&crypto_alg_sem);
  119. list_for_each_entry(q, &crypto_template_list, list) {
  120. if (q == tmpl)
  121. goto out;
  122. }
  123. list_add(&tmpl->list, &crypto_template_list);
  124. crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
  125. err = 0;
  126. out:
  127. up_write(&crypto_alg_sem);
  128. return err;
  129. }
  130. EXPORT_SYMBOL_GPL(crypto_register_template);
  131. void crypto_unregister_template(struct crypto_template *tmpl)
  132. {
  133. struct crypto_instance *inst;
  134. struct hlist_node *p, *n;
  135. struct hlist_head *list;
  136. down_write(&crypto_alg_sem);
  137. BUG_ON(list_empty(&tmpl->list));
  138. list_del_init(&tmpl->list);
  139. list = &tmpl->instances;
  140. hlist_for_each_entry(inst, p, list, list) {
  141. BUG_ON(list_empty(&inst->alg.cra_list));
  142. list_del_init(&inst->alg.cra_list);
  143. crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
  144. }
  145. crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
  146. up_write(&crypto_alg_sem);
  147. hlist_for_each_entry_safe(inst, p, n, list, list) {
  148. BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
  149. tmpl->free(inst);
  150. }
  151. }
  152. EXPORT_SYMBOL_GPL(crypto_unregister_template);
  153. static struct crypto_template *__crypto_lookup_template(const char *name)
  154. {
  155. struct crypto_template *q, *tmpl = NULL;
  156. down_read(&crypto_alg_sem);
  157. list_for_each_entry(q, &crypto_template_list, list) {
  158. if (strcmp(q->name, name))
  159. continue;
  160. if (unlikely(!crypto_tmpl_get(q)))
  161. continue;
  162. tmpl = q;
  163. break;
  164. }
  165. up_read(&crypto_alg_sem);
  166. return tmpl;
  167. }
  168. struct crypto_template *crypto_lookup_template(const char *name)
  169. {
  170. return try_then_request_module(__crypto_lookup_template(name), name);
  171. }
  172. EXPORT_SYMBOL_GPL(crypto_lookup_template);
  173. int crypto_register_instance(struct crypto_template *tmpl,
  174. struct crypto_instance *inst)
  175. {
  176. int err = -EINVAL;
  177. if (inst->alg.cra_destroy)
  178. goto err;
  179. err = crypto_check_alg(&inst->alg);
  180. if (err)
  181. goto err;
  182. inst->alg.cra_module = tmpl->module;
  183. down_write(&crypto_alg_sem);
  184. err = __crypto_register_alg(&inst->alg);
  185. if (err)
  186. goto unlock;
  187. hlist_add_head(&inst->list, &tmpl->instances);
  188. inst->tmpl = tmpl;
  189. unlock:
  190. up_write(&crypto_alg_sem);
  191. err:
  192. return err;
  193. }
  194. EXPORT_SYMBOL_GPL(crypto_register_instance);
  195. int crypto_register_notifier(struct notifier_block *nb)
  196. {
  197. return blocking_notifier_chain_register(&crypto_chain, nb);
  198. }
  199. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  200. int crypto_unregister_notifier(struct notifier_block *nb)
  201. {
  202. return blocking_notifier_chain_unregister(&crypto_chain, nb);
  203. }
  204. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  205. static int __init crypto_algapi_init(void)
  206. {
  207. crypto_init_proc();
  208. return 0;
  209. }
  210. static void __exit crypto_algapi_exit(void)
  211. {
  212. crypto_exit_proc();
  213. }
  214. module_init(crypto_algapi_init);
  215. module_exit(crypto_algapi_exit);
  216. MODULE_LICENSE("GPL");
  217. MODULE_DESCRIPTION("Cryptographic algorithms API");