algapi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. static inline int crypto_set_driver_name(struct crypto_alg *alg)
  21. {
  22. static const char suffix[] = "-generic";
  23. char *driver_name = alg->cra_driver_name;
  24. int len;
  25. if (*driver_name)
  26. return 0;
  27. len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  28. if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
  29. return -ENAMETOOLONG;
  30. memcpy(driver_name + len, suffix, sizeof(suffix));
  31. return 0;
  32. }
  33. static int crypto_check_alg(struct crypto_alg *alg)
  34. {
  35. if (alg->cra_alignmask & (alg->cra_alignmask + 1))
  36. return -EINVAL;
  37. if (alg->cra_alignmask & alg->cra_blocksize)
  38. return -EINVAL;
  39. if (alg->cra_blocksize > PAGE_SIZE / 8)
  40. return -EINVAL;
  41. if (alg->cra_priority < 0)
  42. return -EINVAL;
  43. return crypto_set_driver_name(alg);
  44. }
  45. static int __crypto_register_alg(struct crypto_alg *alg)
  46. {
  47. struct crypto_alg *q;
  48. int ret = -EEXIST;
  49. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  50. if (q == alg)
  51. goto out;
  52. }
  53. list_add(&alg->cra_list, &crypto_alg_list);
  54. atomic_set(&alg->cra_refcnt, 1);
  55. ret = 0;
  56. out:
  57. return ret;
  58. }
  59. int crypto_register_alg(struct crypto_alg *alg)
  60. {
  61. int err;
  62. err = crypto_check_alg(alg);
  63. if (err)
  64. return err;
  65. down_write(&crypto_alg_sem);
  66. err = __crypto_register_alg(alg);
  67. up_write(&crypto_alg_sem);
  68. return err;
  69. }
  70. EXPORT_SYMBOL_GPL(crypto_register_alg);
  71. int crypto_unregister_alg(struct crypto_alg *alg)
  72. {
  73. int ret = -ENOENT;
  74. down_write(&crypto_alg_sem);
  75. if (likely(!list_empty(&alg->cra_list))) {
  76. list_del_init(&alg->cra_list);
  77. ret = 0;
  78. }
  79. up_write(&crypto_alg_sem);
  80. if (ret)
  81. return ret;
  82. BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
  83. if (alg->cra_destroy)
  84. alg->cra_destroy(alg);
  85. return 0;
  86. }
  87. EXPORT_SYMBOL_GPL(crypto_unregister_alg);
  88. int crypto_register_template(struct crypto_template *tmpl)
  89. {
  90. struct crypto_template *q;
  91. int err = -EEXIST;
  92. down_write(&crypto_alg_sem);
  93. list_for_each_entry(q, &crypto_template_list, list) {
  94. if (q == tmpl)
  95. goto out;
  96. }
  97. list_add(&tmpl->list, &crypto_template_list);
  98. err = 0;
  99. out:
  100. up_write(&crypto_alg_sem);
  101. return err;
  102. }
  103. EXPORT_SYMBOL_GPL(crypto_register_template);
  104. void crypto_unregister_template(struct crypto_template *tmpl)
  105. {
  106. struct crypto_instance *inst;
  107. struct hlist_node *p, *n;
  108. struct hlist_head *list;
  109. down_write(&crypto_alg_sem);
  110. BUG_ON(list_empty(&tmpl->list));
  111. list_del_init(&tmpl->list);
  112. list = &tmpl->instances;
  113. hlist_for_each_entry(inst, p, list, list) {
  114. BUG_ON(list_empty(&inst->alg.cra_list));
  115. list_del_init(&inst->alg.cra_list);
  116. }
  117. up_write(&crypto_alg_sem);
  118. hlist_for_each_entry_safe(inst, p, n, list, list) {
  119. BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
  120. tmpl->free(inst);
  121. }
  122. }
  123. EXPORT_SYMBOL_GPL(crypto_unregister_template);
  124. static struct crypto_template *__crypto_lookup_template(const char *name)
  125. {
  126. struct crypto_template *q, *tmpl = NULL;
  127. down_read(&crypto_alg_sem);
  128. list_for_each_entry(q, &crypto_template_list, list) {
  129. if (strcmp(q->name, name))
  130. continue;
  131. if (unlikely(!crypto_tmpl_get(q)))
  132. continue;
  133. tmpl = q;
  134. break;
  135. }
  136. up_read(&crypto_alg_sem);
  137. return tmpl;
  138. }
  139. struct crypto_template *crypto_lookup_template(const char *name)
  140. {
  141. return try_then_request_module(__crypto_lookup_template(name), name);
  142. }
  143. EXPORT_SYMBOL_GPL(crypto_lookup_template);
  144. int crypto_register_instance(struct crypto_template *tmpl,
  145. struct crypto_instance *inst)
  146. {
  147. int err = -EINVAL;
  148. if (inst->alg.cra_destroy)
  149. goto err;
  150. err = crypto_check_alg(&inst->alg);
  151. if (err)
  152. goto err;
  153. inst->alg.cra_module = tmpl->module;
  154. down_write(&crypto_alg_sem);
  155. err = __crypto_register_alg(&inst->alg);
  156. if (err)
  157. goto unlock;
  158. hlist_add_head(&inst->list, &tmpl->instances);
  159. inst->tmpl = tmpl;
  160. unlock:
  161. up_write(&crypto_alg_sem);
  162. err:
  163. return err;
  164. }
  165. EXPORT_SYMBOL_GPL(crypto_register_instance);
  166. static int __init crypto_algapi_init(void)
  167. {
  168. crypto_init_proc();
  169. return 0;
  170. }
  171. static void __exit crypto_algapi_exit(void)
  172. {
  173. crypto_exit_proc();
  174. }
  175. module_init(crypto_algapi_init);
  176. module_exit(crypto_algapi_exit);
  177. MODULE_LICENSE("GPL");
  178. MODULE_DESCRIPTION("Cryptographic algorithms API");