algapi.c 16 KB

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