algapi.c 13 KB

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