algapi.c 10 KB

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