algapi.c 16 KB

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