algapi.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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/err.h>
  13. #include <linux/errno.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/list.h>
  17. #include <linux/module.h>
  18. #include <linux/string.h>
  19. #include "internal.h"
  20. static LIST_HEAD(crypto_template_list);
  21. void crypto_larval_error(const char *name, u32 type, u32 mask)
  22. {
  23. struct crypto_alg *alg;
  24. down_read(&crypto_alg_sem);
  25. alg = __crypto_alg_lookup(name, type, mask);
  26. up_read(&crypto_alg_sem);
  27. if (alg) {
  28. if (crypto_is_larval(alg)) {
  29. struct crypto_larval *larval = (void *)alg;
  30. complete(&larval->completion);
  31. }
  32. crypto_mod_put(alg);
  33. }
  34. }
  35. EXPORT_SYMBOL_GPL(crypto_larval_error);
  36. static inline int crypto_set_driver_name(struct crypto_alg *alg)
  37. {
  38. static const char suffix[] = "-generic";
  39. char *driver_name = alg->cra_driver_name;
  40. int len;
  41. if (*driver_name)
  42. return 0;
  43. len = strlcpy(driver_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
  44. if (len + sizeof(suffix) > CRYPTO_MAX_ALG_NAME)
  45. return -ENAMETOOLONG;
  46. memcpy(driver_name + len, suffix, sizeof(suffix));
  47. return 0;
  48. }
  49. static int crypto_check_alg(struct crypto_alg *alg)
  50. {
  51. if (alg->cra_alignmask & (alg->cra_alignmask + 1))
  52. return -EINVAL;
  53. if (alg->cra_alignmask & alg->cra_blocksize)
  54. return -EINVAL;
  55. if (alg->cra_blocksize > PAGE_SIZE / 8)
  56. return -EINVAL;
  57. if (alg->cra_priority < 0)
  58. return -EINVAL;
  59. return crypto_set_driver_name(alg);
  60. }
  61. static void crypto_destroy_instance(struct crypto_alg *alg)
  62. {
  63. struct crypto_instance *inst = (void *)alg;
  64. struct crypto_template *tmpl = inst->tmpl;
  65. tmpl->free(inst);
  66. crypto_tmpl_put(tmpl);
  67. }
  68. static void crypto_remove_spawns(struct list_head *spawns,
  69. struct list_head *list)
  70. {
  71. struct crypto_spawn *spawn, *n;
  72. list_for_each_entry_safe(spawn, n, spawns, list) {
  73. struct crypto_instance *inst = spawn->inst;
  74. struct crypto_template *tmpl = inst->tmpl;
  75. list_del_init(&spawn->list);
  76. spawn->alg = NULL;
  77. if (crypto_is_dead(&inst->alg))
  78. continue;
  79. inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
  80. if (!tmpl || !crypto_tmpl_get(tmpl))
  81. continue;
  82. crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, &inst->alg);
  83. list_move(&inst->alg.cra_list, list);
  84. hlist_del(&inst->list);
  85. inst->alg.cra_destroy = crypto_destroy_instance;
  86. if (!list_empty(&inst->alg.cra_users)) {
  87. if (&n->list == spawns)
  88. n = list_entry(inst->alg.cra_users.next,
  89. typeof(*n), list);
  90. __list_splice(&inst->alg.cra_users, spawns->prev);
  91. }
  92. }
  93. }
  94. static int __crypto_register_alg(struct crypto_alg *alg,
  95. struct list_head *list)
  96. {
  97. struct crypto_alg *q;
  98. int ret = -EAGAIN;
  99. if (crypto_is_dead(alg))
  100. goto out;
  101. INIT_LIST_HEAD(&alg->cra_users);
  102. ret = -EEXIST;
  103. atomic_set(&alg->cra_refcnt, 1);
  104. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  105. if (q == alg)
  106. goto out;
  107. if (crypto_is_moribund(q))
  108. continue;
  109. if (crypto_is_larval(q)) {
  110. struct crypto_larval *larval = (void *)q;
  111. if (strcmp(alg->cra_name, q->cra_name) &&
  112. strcmp(alg->cra_driver_name, q->cra_name))
  113. continue;
  114. if (larval->adult)
  115. continue;
  116. if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
  117. continue;
  118. if (!crypto_mod_get(alg))
  119. continue;
  120. larval->adult = alg;
  121. complete(&larval->completion);
  122. continue;
  123. }
  124. if (strcmp(alg->cra_name, q->cra_name))
  125. continue;
  126. if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
  127. q->cra_priority > alg->cra_priority)
  128. continue;
  129. crypto_remove_spawns(&q->cra_users, list);
  130. }
  131. list_add(&alg->cra_list, &crypto_alg_list);
  132. crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg);
  133. ret = 0;
  134. out:
  135. return ret;
  136. }
  137. static void crypto_remove_final(struct list_head *list)
  138. {
  139. struct crypto_alg *alg;
  140. struct crypto_alg *n;
  141. list_for_each_entry_safe(alg, n, list, cra_list) {
  142. list_del_init(&alg->cra_list);
  143. crypto_alg_put(alg);
  144. }
  145. }
  146. int crypto_register_alg(struct crypto_alg *alg)
  147. {
  148. LIST_HEAD(list);
  149. int err;
  150. err = crypto_check_alg(alg);
  151. if (err)
  152. return err;
  153. down_write(&crypto_alg_sem);
  154. err = __crypto_register_alg(alg, &list);
  155. up_write(&crypto_alg_sem);
  156. crypto_remove_final(&list);
  157. return err;
  158. }
  159. EXPORT_SYMBOL_GPL(crypto_register_alg);
  160. static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
  161. {
  162. if (unlikely(list_empty(&alg->cra_list)))
  163. return -ENOENT;
  164. alg->cra_flags |= CRYPTO_ALG_DEAD;
  165. crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
  166. list_del_init(&alg->cra_list);
  167. crypto_remove_spawns(&alg->cra_users, list);
  168. return 0;
  169. }
  170. int crypto_unregister_alg(struct crypto_alg *alg)
  171. {
  172. int ret;
  173. LIST_HEAD(list);
  174. down_write(&crypto_alg_sem);
  175. ret = crypto_remove_alg(alg, &list);
  176. up_write(&crypto_alg_sem);
  177. if (ret)
  178. return ret;
  179. BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
  180. if (alg->cra_destroy)
  181. alg->cra_destroy(alg);
  182. crypto_remove_final(&list);
  183. return 0;
  184. }
  185. EXPORT_SYMBOL_GPL(crypto_unregister_alg);
  186. int crypto_register_template(struct crypto_template *tmpl)
  187. {
  188. struct crypto_template *q;
  189. int err = -EEXIST;
  190. down_write(&crypto_alg_sem);
  191. list_for_each_entry(q, &crypto_template_list, list) {
  192. if (q == tmpl)
  193. goto out;
  194. }
  195. list_add(&tmpl->list, &crypto_template_list);
  196. crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
  197. err = 0;
  198. out:
  199. up_write(&crypto_alg_sem);
  200. return err;
  201. }
  202. EXPORT_SYMBOL_GPL(crypto_register_template);
  203. void crypto_unregister_template(struct crypto_template *tmpl)
  204. {
  205. struct crypto_instance *inst;
  206. struct hlist_node *p, *n;
  207. struct hlist_head *list;
  208. LIST_HEAD(users);
  209. down_write(&crypto_alg_sem);
  210. BUG_ON(list_empty(&tmpl->list));
  211. list_del_init(&tmpl->list);
  212. list = &tmpl->instances;
  213. hlist_for_each_entry(inst, p, list, list) {
  214. int err = crypto_remove_alg(&inst->alg, &users);
  215. BUG_ON(err);
  216. }
  217. crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
  218. up_write(&crypto_alg_sem);
  219. hlist_for_each_entry_safe(inst, p, n, list, list) {
  220. BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
  221. tmpl->free(inst);
  222. }
  223. crypto_remove_final(&users);
  224. }
  225. EXPORT_SYMBOL_GPL(crypto_unregister_template);
  226. static struct crypto_template *__crypto_lookup_template(const char *name)
  227. {
  228. struct crypto_template *q, *tmpl = NULL;
  229. down_read(&crypto_alg_sem);
  230. list_for_each_entry(q, &crypto_template_list, list) {
  231. if (strcmp(q->name, name))
  232. continue;
  233. if (unlikely(!crypto_tmpl_get(q)))
  234. continue;
  235. tmpl = q;
  236. break;
  237. }
  238. up_read(&crypto_alg_sem);
  239. return tmpl;
  240. }
  241. struct crypto_template *crypto_lookup_template(const char *name)
  242. {
  243. return try_then_request_module(__crypto_lookup_template(name), name);
  244. }
  245. EXPORT_SYMBOL_GPL(crypto_lookup_template);
  246. int crypto_register_instance(struct crypto_template *tmpl,
  247. struct crypto_instance *inst)
  248. {
  249. LIST_HEAD(list);
  250. int err = -EINVAL;
  251. if (inst->alg.cra_destroy)
  252. goto err;
  253. err = crypto_check_alg(&inst->alg);
  254. if (err)
  255. goto err;
  256. inst->alg.cra_module = tmpl->module;
  257. down_write(&crypto_alg_sem);
  258. err = __crypto_register_alg(&inst->alg, &list);
  259. if (err)
  260. goto unlock;
  261. hlist_add_head(&inst->list, &tmpl->instances);
  262. inst->tmpl = tmpl;
  263. unlock:
  264. up_write(&crypto_alg_sem);
  265. crypto_remove_final(&list);
  266. err:
  267. return err;
  268. }
  269. EXPORT_SYMBOL_GPL(crypto_register_instance);
  270. int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
  271. struct crypto_instance *inst)
  272. {
  273. int err = -EAGAIN;
  274. spawn->inst = inst;
  275. down_write(&crypto_alg_sem);
  276. if (!crypto_is_moribund(alg)) {
  277. list_add(&spawn->list, &alg->cra_users);
  278. spawn->alg = alg;
  279. err = 0;
  280. }
  281. up_write(&crypto_alg_sem);
  282. return err;
  283. }
  284. EXPORT_SYMBOL_GPL(crypto_init_spawn);
  285. void crypto_drop_spawn(struct crypto_spawn *spawn)
  286. {
  287. down_write(&crypto_alg_sem);
  288. list_del(&spawn->list);
  289. up_write(&crypto_alg_sem);
  290. }
  291. EXPORT_SYMBOL_GPL(crypto_drop_spawn);
  292. struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn)
  293. {
  294. struct crypto_alg *alg;
  295. struct crypto_alg *alg2;
  296. struct crypto_tfm *tfm;
  297. down_read(&crypto_alg_sem);
  298. alg = spawn->alg;
  299. alg2 = alg;
  300. if (alg2)
  301. alg2 = crypto_mod_get(alg2);
  302. up_read(&crypto_alg_sem);
  303. if (!alg2) {
  304. if (alg)
  305. crypto_shoot_alg(alg);
  306. return ERR_PTR(-EAGAIN);
  307. }
  308. tfm = __crypto_alloc_tfm(alg, 0);
  309. if (IS_ERR(tfm))
  310. crypto_mod_put(alg);
  311. return tfm;
  312. }
  313. EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
  314. int crypto_register_notifier(struct notifier_block *nb)
  315. {
  316. return blocking_notifier_chain_register(&crypto_chain, nb);
  317. }
  318. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  319. int crypto_unregister_notifier(struct notifier_block *nb)
  320. {
  321. return blocking_notifier_chain_unregister(&crypto_chain, nb);
  322. }
  323. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  324. static int __init crypto_algapi_init(void)
  325. {
  326. crypto_init_proc();
  327. return 0;
  328. }
  329. static void __exit crypto_algapi_exit(void)
  330. {
  331. crypto_exit_proc();
  332. }
  333. module_init(crypto_algapi_init);
  334. module_exit(crypto_algapi_exit);
  335. MODULE_LICENSE("GPL");
  336. MODULE_DESCRIPTION("Cryptographic algorithms API");