algapi.c 19 KB

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