algapi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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_all(&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_spawn(struct crypto_spawn *spawn,
  70. struct list_head *list,
  71. struct list_head *secondary_spawns)
  72. {
  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. return;
  79. inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
  80. if (!tmpl || !crypto_tmpl_get(tmpl))
  81. return;
  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. list_splice(&inst->alg.cra_users, secondary_spawns);
  87. }
  88. static void crypto_remove_spawns(struct list_head *spawns,
  89. struct list_head *list, u32 new_type)
  90. {
  91. struct crypto_spawn *spawn, *n;
  92. LIST_HEAD(secondary_spawns);
  93. list_for_each_entry_safe(spawn, n, spawns, list) {
  94. if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
  95. continue;
  96. crypto_remove_spawn(spawn, list, &secondary_spawns);
  97. }
  98. while (!list_empty(&secondary_spawns)) {
  99. list_for_each_entry_safe(spawn, n, &secondary_spawns, list)
  100. crypto_remove_spawn(spawn, list, &secondary_spawns);
  101. }
  102. }
  103. static int __crypto_register_alg(struct crypto_alg *alg,
  104. struct list_head *list)
  105. {
  106. struct crypto_alg *q;
  107. int ret = -EAGAIN;
  108. if (crypto_is_dead(alg))
  109. goto out;
  110. INIT_LIST_HEAD(&alg->cra_users);
  111. ret = -EEXIST;
  112. atomic_set(&alg->cra_refcnt, 1);
  113. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  114. if (q == alg)
  115. goto out;
  116. if (crypto_is_moribund(q))
  117. continue;
  118. if (crypto_is_larval(q)) {
  119. struct crypto_larval *larval = (void *)q;
  120. if (strcmp(alg->cra_name, q->cra_name) &&
  121. strcmp(alg->cra_driver_name, q->cra_name))
  122. continue;
  123. if (larval->adult)
  124. continue;
  125. if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
  126. continue;
  127. if (!crypto_mod_get(alg))
  128. continue;
  129. larval->adult = alg;
  130. complete_all(&larval->completion);
  131. continue;
  132. }
  133. if (strcmp(alg->cra_name, q->cra_name))
  134. continue;
  135. if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
  136. q->cra_priority > alg->cra_priority)
  137. continue;
  138. crypto_remove_spawns(&q->cra_users, list, alg->cra_flags);
  139. }
  140. list_add(&alg->cra_list, &crypto_alg_list);
  141. crypto_notify(CRYPTO_MSG_ALG_REGISTER, alg);
  142. ret = 0;
  143. out:
  144. return ret;
  145. }
  146. static void crypto_remove_final(struct list_head *list)
  147. {
  148. struct crypto_alg *alg;
  149. struct crypto_alg *n;
  150. list_for_each_entry_safe(alg, n, list, cra_list) {
  151. list_del_init(&alg->cra_list);
  152. crypto_alg_put(alg);
  153. }
  154. }
  155. int crypto_register_alg(struct crypto_alg *alg)
  156. {
  157. LIST_HEAD(list);
  158. int err;
  159. err = crypto_check_alg(alg);
  160. if (err)
  161. return err;
  162. down_write(&crypto_alg_sem);
  163. err = __crypto_register_alg(alg, &list);
  164. up_write(&crypto_alg_sem);
  165. crypto_remove_final(&list);
  166. return err;
  167. }
  168. EXPORT_SYMBOL_GPL(crypto_register_alg);
  169. static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
  170. {
  171. if (unlikely(list_empty(&alg->cra_list)))
  172. return -ENOENT;
  173. alg->cra_flags |= CRYPTO_ALG_DEAD;
  174. crypto_notify(CRYPTO_MSG_ALG_UNREGISTER, alg);
  175. list_del_init(&alg->cra_list);
  176. crypto_remove_spawns(&alg->cra_users, list, alg->cra_flags);
  177. return 0;
  178. }
  179. int crypto_unregister_alg(struct crypto_alg *alg)
  180. {
  181. int ret;
  182. LIST_HEAD(list);
  183. down_write(&crypto_alg_sem);
  184. ret = crypto_remove_alg(alg, &list);
  185. up_write(&crypto_alg_sem);
  186. if (ret)
  187. return ret;
  188. BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
  189. if (alg->cra_destroy)
  190. alg->cra_destroy(alg);
  191. crypto_remove_final(&list);
  192. return 0;
  193. }
  194. EXPORT_SYMBOL_GPL(crypto_unregister_alg);
  195. int crypto_register_template(struct crypto_template *tmpl)
  196. {
  197. struct crypto_template *q;
  198. int err = -EEXIST;
  199. down_write(&crypto_alg_sem);
  200. list_for_each_entry(q, &crypto_template_list, list) {
  201. if (q == tmpl)
  202. goto out;
  203. }
  204. list_add(&tmpl->list, &crypto_template_list);
  205. crypto_notify(CRYPTO_MSG_TMPL_REGISTER, tmpl);
  206. err = 0;
  207. out:
  208. up_write(&crypto_alg_sem);
  209. return err;
  210. }
  211. EXPORT_SYMBOL_GPL(crypto_register_template);
  212. void crypto_unregister_template(struct crypto_template *tmpl)
  213. {
  214. struct crypto_instance *inst;
  215. struct hlist_node *p, *n;
  216. struct hlist_head *list;
  217. LIST_HEAD(users);
  218. down_write(&crypto_alg_sem);
  219. BUG_ON(list_empty(&tmpl->list));
  220. list_del_init(&tmpl->list);
  221. list = &tmpl->instances;
  222. hlist_for_each_entry(inst, p, list, list) {
  223. int err = crypto_remove_alg(&inst->alg, &users);
  224. BUG_ON(err);
  225. }
  226. crypto_notify(CRYPTO_MSG_TMPL_UNREGISTER, tmpl);
  227. up_write(&crypto_alg_sem);
  228. hlist_for_each_entry_safe(inst, p, n, list, list) {
  229. BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1);
  230. tmpl->free(inst);
  231. }
  232. crypto_remove_final(&users);
  233. }
  234. EXPORT_SYMBOL_GPL(crypto_unregister_template);
  235. static struct crypto_template *__crypto_lookup_template(const char *name)
  236. {
  237. struct crypto_template *q, *tmpl = NULL;
  238. down_read(&crypto_alg_sem);
  239. list_for_each_entry(q, &crypto_template_list, list) {
  240. if (strcmp(q->name, name))
  241. continue;
  242. if (unlikely(!crypto_tmpl_get(q)))
  243. continue;
  244. tmpl = q;
  245. break;
  246. }
  247. up_read(&crypto_alg_sem);
  248. return tmpl;
  249. }
  250. struct crypto_template *crypto_lookup_template(const char *name)
  251. {
  252. return try_then_request_module(__crypto_lookup_template(name), name);
  253. }
  254. EXPORT_SYMBOL_GPL(crypto_lookup_template);
  255. int crypto_register_instance(struct crypto_template *tmpl,
  256. struct crypto_instance *inst)
  257. {
  258. LIST_HEAD(list);
  259. int err = -EINVAL;
  260. if (inst->alg.cra_destroy)
  261. goto err;
  262. err = crypto_check_alg(&inst->alg);
  263. if (err)
  264. goto err;
  265. inst->alg.cra_module = tmpl->module;
  266. down_write(&crypto_alg_sem);
  267. err = __crypto_register_alg(&inst->alg, &list);
  268. if (err)
  269. goto unlock;
  270. hlist_add_head(&inst->list, &tmpl->instances);
  271. inst->tmpl = tmpl;
  272. unlock:
  273. up_write(&crypto_alg_sem);
  274. crypto_remove_final(&list);
  275. err:
  276. return err;
  277. }
  278. EXPORT_SYMBOL_GPL(crypto_register_instance);
  279. int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
  280. struct crypto_instance *inst, u32 mask)
  281. {
  282. int err = -EAGAIN;
  283. spawn->inst = inst;
  284. spawn->mask = mask;
  285. down_write(&crypto_alg_sem);
  286. if (!crypto_is_moribund(alg)) {
  287. list_add(&spawn->list, &alg->cra_users);
  288. spawn->alg = alg;
  289. err = 0;
  290. }
  291. up_write(&crypto_alg_sem);
  292. return err;
  293. }
  294. EXPORT_SYMBOL_GPL(crypto_init_spawn);
  295. void crypto_drop_spawn(struct crypto_spawn *spawn)
  296. {
  297. down_write(&crypto_alg_sem);
  298. list_del(&spawn->list);
  299. up_write(&crypto_alg_sem);
  300. }
  301. EXPORT_SYMBOL_GPL(crypto_drop_spawn);
  302. struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
  303. u32 mask)
  304. {
  305. struct crypto_alg *alg;
  306. struct crypto_alg *alg2;
  307. struct crypto_tfm *tfm;
  308. down_read(&crypto_alg_sem);
  309. alg = spawn->alg;
  310. alg2 = alg;
  311. if (alg2)
  312. alg2 = crypto_mod_get(alg2);
  313. up_read(&crypto_alg_sem);
  314. if (!alg2) {
  315. if (alg)
  316. crypto_shoot_alg(alg);
  317. return ERR_PTR(-EAGAIN);
  318. }
  319. tfm = ERR_PTR(-EINVAL);
  320. if (unlikely((alg->cra_flags ^ type) & mask))
  321. goto out_put_alg;
  322. tfm = __crypto_alloc_tfm(alg, type, mask);
  323. if (IS_ERR(tfm))
  324. goto out_put_alg;
  325. return tfm;
  326. out_put_alg:
  327. crypto_mod_put(alg);
  328. return tfm;
  329. }
  330. EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
  331. int crypto_register_notifier(struct notifier_block *nb)
  332. {
  333. return blocking_notifier_chain_register(&crypto_chain, nb);
  334. }
  335. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  336. int crypto_unregister_notifier(struct notifier_block *nb)
  337. {
  338. return blocking_notifier_chain_unregister(&crypto_chain, nb);
  339. }
  340. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  341. struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
  342. {
  343. struct rtattr *rta = tb[CRYPTOA_TYPE - 1];
  344. struct crypto_attr_type *algt;
  345. if (!rta)
  346. return ERR_PTR(-ENOENT);
  347. if (RTA_PAYLOAD(rta) < sizeof(*algt))
  348. return ERR_PTR(-EINVAL);
  349. algt = RTA_DATA(rta);
  350. return algt;
  351. }
  352. EXPORT_SYMBOL_GPL(crypto_get_attr_type);
  353. int crypto_check_attr_type(struct rtattr **tb, u32 type)
  354. {
  355. struct crypto_attr_type *algt;
  356. algt = crypto_get_attr_type(tb);
  357. if (IS_ERR(algt))
  358. return PTR_ERR(algt);
  359. if ((algt->type ^ type) & algt->mask)
  360. return -EINVAL;
  361. return 0;
  362. }
  363. EXPORT_SYMBOL_GPL(crypto_check_attr_type);
  364. struct crypto_alg *crypto_get_attr_alg(struct rtattr **tb, u32 type, u32 mask)
  365. {
  366. struct rtattr *rta = tb[CRYPTOA_ALG - 1];
  367. struct crypto_attr_alg *alga;
  368. if (!rta)
  369. return ERR_PTR(-ENOENT);
  370. if (RTA_PAYLOAD(rta) < sizeof(*alga))
  371. return ERR_PTR(-EINVAL);
  372. alga = RTA_DATA(rta);
  373. alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
  374. return crypto_alg_mod_lookup(alga->name, type, mask);
  375. }
  376. EXPORT_SYMBOL_GPL(crypto_get_attr_alg);
  377. struct crypto_instance *crypto_alloc_instance(const char *name,
  378. struct crypto_alg *alg)
  379. {
  380. struct crypto_instance *inst;
  381. struct crypto_spawn *spawn;
  382. int err;
  383. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  384. if (!inst)
  385. return ERR_PTR(-ENOMEM);
  386. err = -ENAMETOOLONG;
  387. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
  388. alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
  389. goto err_free_inst;
  390. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  391. name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  392. goto err_free_inst;
  393. spawn = crypto_instance_ctx(inst);
  394. err = crypto_init_spawn(spawn, alg, inst,
  395. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  396. if (err)
  397. goto err_free_inst;
  398. return inst;
  399. err_free_inst:
  400. kfree(inst);
  401. return ERR_PTR(err);
  402. }
  403. EXPORT_SYMBOL_GPL(crypto_alloc_instance);
  404. void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
  405. {
  406. INIT_LIST_HEAD(&queue->list);
  407. queue->backlog = &queue->list;
  408. queue->qlen = 0;
  409. queue->max_qlen = max_qlen;
  410. }
  411. EXPORT_SYMBOL_GPL(crypto_init_queue);
  412. int crypto_enqueue_request(struct crypto_queue *queue,
  413. struct crypto_async_request *request)
  414. {
  415. int err = -EINPROGRESS;
  416. if (unlikely(queue->qlen >= queue->max_qlen)) {
  417. err = -EBUSY;
  418. if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  419. goto out;
  420. if (queue->backlog == &queue->list)
  421. queue->backlog = &request->list;
  422. }
  423. queue->qlen++;
  424. list_add_tail(&request->list, &queue->list);
  425. out:
  426. return err;
  427. }
  428. EXPORT_SYMBOL_GPL(crypto_enqueue_request);
  429. struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
  430. {
  431. struct list_head *request;
  432. if (unlikely(!queue->qlen))
  433. return NULL;
  434. queue->qlen--;
  435. if (queue->backlog != &queue->list)
  436. queue->backlog = queue->backlog->next;
  437. request = queue->list.next;
  438. list_del(request);
  439. return list_entry(request, struct crypto_async_request, list);
  440. }
  441. EXPORT_SYMBOL_GPL(crypto_dequeue_request);
  442. int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
  443. {
  444. struct crypto_async_request *req;
  445. list_for_each_entry(req, &queue->list, list) {
  446. if (req->tfm == tfm)
  447. return 1;
  448. }
  449. return 0;
  450. }
  451. EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
  452. static int __init crypto_algapi_init(void)
  453. {
  454. crypto_init_proc();
  455. return 0;
  456. }
  457. static void __exit crypto_algapi_exit(void)
  458. {
  459. crypto_exit_proc();
  460. }
  461. module_init(crypto_algapi_init);
  462. module_exit(crypto_algapi_exit);
  463. MODULE_LICENSE("GPL");
  464. MODULE_DESCRIPTION("Cryptographic algorithms API");