algapi.c 13 KB

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