algapi.c 14 KB

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