algapi.c 20 KB

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