algapi.c 19 KB

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