algapi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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, u32 type, u32 mask)
  21. {
  22. struct crypto_alg *alg;
  23. down_read(&crypto_alg_sem);
  24. alg = __crypto_alg_lookup(name, type, mask);
  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 ((q->cra_flags ^ alg->cra_flags) & larval->mask)
  73. continue;
  74. if (!crypto_mod_get(alg))
  75. continue;
  76. larval->adult = alg;
  77. complete(&larval->completion);
  78. }
  79. }
  80. list_add(&alg->cra_list, &crypto_alg_list);
  81. crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg);
  82. ret = 0;
  83. out:
  84. return ret;
  85. }
  86. int crypto_register_alg(struct crypto_alg *alg)
  87. {
  88. int err;
  89. err = crypto_check_alg(alg);
  90. if (err)
  91. return err;
  92. down_write(&crypto_alg_sem);
  93. err = __crypto_register_alg(alg);
  94. up_write(&crypto_alg_sem);
  95. return err;
  96. }
  97. EXPORT_SYMBOL_GPL(crypto_register_alg);
  98. int crypto_unregister_alg(struct crypto_alg *alg)
  99. {
  100. int ret = -ENOENT;
  101. down_write(&crypto_alg_sem);
  102. if (likely(!list_empty(&alg->cra_list))) {
  103. list_del_init(&alg->cra_list);
  104. ret = 0;
  105. }
  106. crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
  107. up_write(&crypto_alg_sem);
  108. if (ret)
  109. return ret;
  110. BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
  111. if (alg->cra_destroy)
  112. alg->cra_destroy(alg);
  113. return 0;
  114. }
  115. EXPORT_SYMBOL_GPL(crypto_unregister_alg);
  116. int crypto_register_template(struct crypto_template *tmpl)
  117. {
  118. struct crypto_template *q;
  119. int err = -EEXIST;
  120. down_write(&crypto_alg_sem);
  121. list_for_each_entry(q, &crypto_template_list, list) {
  122. if (q == tmpl)
  123. goto out;
  124. }
  125. list_add(&tmpl->list, &crypto_template_list);
  126. crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
  127. err = 0;
  128. out:
  129. up_write(&crypto_alg_sem);
  130. return err;
  131. }
  132. EXPORT_SYMBOL_GPL(crypto_register_template);
  133. void crypto_unregister_template(struct crypto_template *tmpl)
  134. {
  135. struct crypto_instance *inst;
  136. struct hlist_node *p, *n;
  137. struct hlist_head *list;
  138. down_write(&crypto_alg_sem);
  139. BUG_ON(list_empty(&tmpl->list));
  140. list_del_init(&tmpl->list);
  141. list = &tmpl->instances;
  142. hlist_for_each_entry(inst, p, list, list) {
  143. BUG_ON(list_empty(&inst->alg.cra_list));
  144. list_del_init(&inst->alg.cra_list);
  145. crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
  146. }
  147. crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
  148. up_write(&crypto_alg_sem);
  149. hlist_for_each_entry_safe(inst, p, n, list, list) {
  150. BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
  151. tmpl->free(inst);
  152. }
  153. }
  154. EXPORT_SYMBOL_GPL(crypto_unregister_template);
  155. static struct crypto_template *__crypto_lookup_template(const char *name)
  156. {
  157. struct crypto_template *q, *tmpl = NULL;
  158. down_read(&crypto_alg_sem);
  159. list_for_each_entry(q, &crypto_template_list, list) {
  160. if (strcmp(q->name, name))
  161. continue;
  162. if (unlikely(!crypto_tmpl_get(q)))
  163. continue;
  164. tmpl = q;
  165. break;
  166. }
  167. up_read(&crypto_alg_sem);
  168. return tmpl;
  169. }
  170. struct crypto_template *crypto_lookup_template(const char *name)
  171. {
  172. return try_then_request_module(__crypto_lookup_template(name), name);
  173. }
  174. EXPORT_SYMBOL_GPL(crypto_lookup_template);
  175. int crypto_register_instance(struct crypto_template *tmpl,
  176. struct crypto_instance *inst)
  177. {
  178. int err = -EINVAL;
  179. if (inst->alg.cra_destroy)
  180. goto err;
  181. err = crypto_check_alg(&inst->alg);
  182. if (err)
  183. goto err;
  184. inst->alg.cra_module = tmpl->module;
  185. down_write(&crypto_alg_sem);
  186. err = __crypto_register_alg(&inst->alg);
  187. if (err)
  188. goto unlock;
  189. hlist_add_head(&inst->list, &tmpl->instances);
  190. inst->tmpl = tmpl;
  191. unlock:
  192. up_write(&crypto_alg_sem);
  193. err:
  194. return err;
  195. }
  196. EXPORT_SYMBOL_GPL(crypto_register_instance);
  197. int crypto_register_notifier(struct notifier_block *nb)
  198. {
  199. return blocking_notifier_chain_register(&crypto_chain, nb);
  200. }
  201. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  202. int crypto_unregister_notifier(struct notifier_block *nb)
  203. {
  204. return blocking_notifier_chain_unregister(&crypto_chain, nb);
  205. }
  206. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  207. static int __init crypto_algapi_init(void)
  208. {
  209. crypto_init_proc();
  210. return 0;
  211. }
  212. static void __exit crypto_algapi_exit(void)
  213. {
  214. crypto_exit_proc();
  215. }
  216. module_init(crypto_algapi_init);
  217. module_exit(crypto_algapi_exit);
  218. MODULE_LICENSE("GPL");
  219. MODULE_DESCRIPTION("Cryptographic algorithms API");