algapi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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 *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, 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, 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), "%s",
  386. name);
  387. }
  388. EXPORT_SYMBOL_GPL(crypto_lookup_template);
  389. int crypto_register_instance(struct crypto_template *tmpl,
  390. struct crypto_instance *inst)
  391. {
  392. struct crypto_larval *larval;
  393. int err;
  394. err = crypto_check_alg(&inst->alg);
  395. if (err)
  396. goto err;
  397. inst->alg.cra_module = tmpl->module;
  398. inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
  399. down_write(&crypto_alg_sem);
  400. larval = __crypto_register_alg(&inst->alg);
  401. if (IS_ERR(larval))
  402. goto unlock;
  403. hlist_add_head(&inst->list, &tmpl->instances);
  404. inst->tmpl = tmpl;
  405. unlock:
  406. up_write(&crypto_alg_sem);
  407. err = PTR_ERR(larval);
  408. if (IS_ERR(larval))
  409. goto err;
  410. crypto_wait_for_test(larval);
  411. err = 0;
  412. err:
  413. return err;
  414. }
  415. EXPORT_SYMBOL_GPL(crypto_register_instance);
  416. int crypto_unregister_instance(struct crypto_alg *alg)
  417. {
  418. int err;
  419. struct crypto_instance *inst = (void *)alg;
  420. struct crypto_template *tmpl = inst->tmpl;
  421. LIST_HEAD(users);
  422. if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
  423. return -EINVAL;
  424. BUG_ON(atomic_read(&alg->cra_refcnt) != 1);
  425. down_write(&crypto_alg_sem);
  426. hlist_del_init(&inst->list);
  427. err = crypto_remove_alg(alg, &users);
  428. up_write(&crypto_alg_sem);
  429. if (err)
  430. return err;
  431. tmpl->free(inst);
  432. crypto_remove_final(&users);
  433. return 0;
  434. }
  435. EXPORT_SYMBOL_GPL(crypto_unregister_instance);
  436. int crypto_init_spawn(struct crypto_spawn *spawn, struct crypto_alg *alg,
  437. struct crypto_instance *inst, u32 mask)
  438. {
  439. int err = -EAGAIN;
  440. spawn->inst = inst;
  441. spawn->mask = mask;
  442. down_write(&crypto_alg_sem);
  443. if (!crypto_is_moribund(alg)) {
  444. list_add(&spawn->list, &alg->cra_users);
  445. spawn->alg = alg;
  446. err = 0;
  447. }
  448. up_write(&crypto_alg_sem);
  449. return err;
  450. }
  451. EXPORT_SYMBOL_GPL(crypto_init_spawn);
  452. int crypto_init_spawn2(struct crypto_spawn *spawn, struct crypto_alg *alg,
  453. struct crypto_instance *inst,
  454. const struct crypto_type *frontend)
  455. {
  456. int err = -EINVAL;
  457. if ((alg->cra_flags ^ frontend->type) & frontend->maskset)
  458. goto out;
  459. spawn->frontend = frontend;
  460. err = crypto_init_spawn(spawn, alg, inst, frontend->maskset);
  461. out:
  462. return err;
  463. }
  464. EXPORT_SYMBOL_GPL(crypto_init_spawn2);
  465. void crypto_drop_spawn(struct crypto_spawn *spawn)
  466. {
  467. if (!spawn->alg)
  468. return;
  469. down_write(&crypto_alg_sem);
  470. list_del(&spawn->list);
  471. up_write(&crypto_alg_sem);
  472. }
  473. EXPORT_SYMBOL_GPL(crypto_drop_spawn);
  474. static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
  475. {
  476. struct crypto_alg *alg;
  477. struct crypto_alg *alg2;
  478. down_read(&crypto_alg_sem);
  479. alg = spawn->alg;
  480. alg2 = alg;
  481. if (alg2)
  482. alg2 = crypto_mod_get(alg2);
  483. up_read(&crypto_alg_sem);
  484. if (!alg2) {
  485. if (alg)
  486. crypto_shoot_alg(alg);
  487. return ERR_PTR(-EAGAIN);
  488. }
  489. return alg;
  490. }
  491. struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
  492. u32 mask)
  493. {
  494. struct crypto_alg *alg;
  495. struct crypto_tfm *tfm;
  496. alg = crypto_spawn_alg(spawn);
  497. if (IS_ERR(alg))
  498. return ERR_CAST(alg);
  499. tfm = ERR_PTR(-EINVAL);
  500. if (unlikely((alg->cra_flags ^ type) & mask))
  501. goto out_put_alg;
  502. tfm = __crypto_alloc_tfm(alg, type, mask);
  503. if (IS_ERR(tfm))
  504. goto out_put_alg;
  505. return tfm;
  506. out_put_alg:
  507. crypto_mod_put(alg);
  508. return tfm;
  509. }
  510. EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
  511. void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
  512. {
  513. struct crypto_alg *alg;
  514. struct crypto_tfm *tfm;
  515. alg = crypto_spawn_alg(spawn);
  516. if (IS_ERR(alg))
  517. return ERR_CAST(alg);
  518. tfm = crypto_create_tfm(alg, spawn->frontend);
  519. if (IS_ERR(tfm))
  520. goto out_put_alg;
  521. return tfm;
  522. out_put_alg:
  523. crypto_mod_put(alg);
  524. return tfm;
  525. }
  526. EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
  527. int crypto_register_notifier(struct notifier_block *nb)
  528. {
  529. return blocking_notifier_chain_register(&crypto_chain, nb);
  530. }
  531. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  532. int crypto_unregister_notifier(struct notifier_block *nb)
  533. {
  534. return blocking_notifier_chain_unregister(&crypto_chain, nb);
  535. }
  536. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  537. struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
  538. {
  539. struct rtattr *rta = tb[0];
  540. struct crypto_attr_type *algt;
  541. if (!rta)
  542. return ERR_PTR(-ENOENT);
  543. if (RTA_PAYLOAD(rta) < sizeof(*algt))
  544. return ERR_PTR(-EINVAL);
  545. if (rta->rta_type != CRYPTOA_TYPE)
  546. return ERR_PTR(-EINVAL);
  547. algt = RTA_DATA(rta);
  548. return algt;
  549. }
  550. EXPORT_SYMBOL_GPL(crypto_get_attr_type);
  551. int crypto_check_attr_type(struct rtattr **tb, u32 type)
  552. {
  553. struct crypto_attr_type *algt;
  554. algt = crypto_get_attr_type(tb);
  555. if (IS_ERR(algt))
  556. return PTR_ERR(algt);
  557. if ((algt->type ^ type) & algt->mask)
  558. return -EINVAL;
  559. return 0;
  560. }
  561. EXPORT_SYMBOL_GPL(crypto_check_attr_type);
  562. const char *crypto_attr_alg_name(struct rtattr *rta)
  563. {
  564. struct crypto_attr_alg *alga;
  565. if (!rta)
  566. return ERR_PTR(-ENOENT);
  567. if (RTA_PAYLOAD(rta) < sizeof(*alga))
  568. return ERR_PTR(-EINVAL);
  569. if (rta->rta_type != CRYPTOA_ALG)
  570. return ERR_PTR(-EINVAL);
  571. alga = RTA_DATA(rta);
  572. alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
  573. return alga->name;
  574. }
  575. EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
  576. struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
  577. const struct crypto_type *frontend,
  578. u32 type, u32 mask)
  579. {
  580. const char *name;
  581. name = crypto_attr_alg_name(rta);
  582. if (IS_ERR(name))
  583. return ERR_CAST(name);
  584. return crypto_find_alg(name, frontend, type, mask);
  585. }
  586. EXPORT_SYMBOL_GPL(crypto_attr_alg2);
  587. int crypto_attr_u32(struct rtattr *rta, u32 *num)
  588. {
  589. struct crypto_attr_u32 *nu32;
  590. if (!rta)
  591. return -ENOENT;
  592. if (RTA_PAYLOAD(rta) < sizeof(*nu32))
  593. return -EINVAL;
  594. if (rta->rta_type != CRYPTOA_U32)
  595. return -EINVAL;
  596. nu32 = RTA_DATA(rta);
  597. *num = nu32->num;
  598. return 0;
  599. }
  600. EXPORT_SYMBOL_GPL(crypto_attr_u32);
  601. void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg,
  602. unsigned int head)
  603. {
  604. struct crypto_instance *inst;
  605. char *p;
  606. int err;
  607. p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn),
  608. GFP_KERNEL);
  609. if (!p)
  610. return ERR_PTR(-ENOMEM);
  611. inst = (void *)(p + head);
  612. err = -ENAMETOOLONG;
  613. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
  614. alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
  615. goto err_free_inst;
  616. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  617. name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  618. goto err_free_inst;
  619. return p;
  620. err_free_inst:
  621. kfree(p);
  622. return ERR_PTR(err);
  623. }
  624. EXPORT_SYMBOL_GPL(crypto_alloc_instance2);
  625. struct crypto_instance *crypto_alloc_instance(const char *name,
  626. struct crypto_alg *alg)
  627. {
  628. struct crypto_instance *inst;
  629. struct crypto_spawn *spawn;
  630. int err;
  631. inst = crypto_alloc_instance2(name, alg, 0);
  632. if (IS_ERR(inst))
  633. goto out;
  634. spawn = crypto_instance_ctx(inst);
  635. err = crypto_init_spawn(spawn, alg, inst,
  636. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  637. if (err)
  638. goto err_free_inst;
  639. return inst;
  640. err_free_inst:
  641. kfree(inst);
  642. inst = ERR_PTR(err);
  643. out:
  644. return inst;
  645. }
  646. EXPORT_SYMBOL_GPL(crypto_alloc_instance);
  647. void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
  648. {
  649. INIT_LIST_HEAD(&queue->list);
  650. queue->backlog = &queue->list;
  651. queue->qlen = 0;
  652. queue->max_qlen = max_qlen;
  653. }
  654. EXPORT_SYMBOL_GPL(crypto_init_queue);
  655. int crypto_enqueue_request(struct crypto_queue *queue,
  656. struct crypto_async_request *request)
  657. {
  658. int err = -EINPROGRESS;
  659. if (unlikely(queue->qlen >= queue->max_qlen)) {
  660. err = -EBUSY;
  661. if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  662. goto out;
  663. if (queue->backlog == &queue->list)
  664. queue->backlog = &request->list;
  665. }
  666. queue->qlen++;
  667. list_add_tail(&request->list, &queue->list);
  668. out:
  669. return err;
  670. }
  671. EXPORT_SYMBOL_GPL(crypto_enqueue_request);
  672. void *__crypto_dequeue_request(struct crypto_queue *queue, unsigned int offset)
  673. {
  674. struct list_head *request;
  675. if (unlikely(!queue->qlen))
  676. return NULL;
  677. queue->qlen--;
  678. if (queue->backlog != &queue->list)
  679. queue->backlog = queue->backlog->next;
  680. request = queue->list.next;
  681. list_del(request);
  682. return (char *)list_entry(request, struct crypto_async_request, list) -
  683. offset;
  684. }
  685. EXPORT_SYMBOL_GPL(__crypto_dequeue_request);
  686. struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
  687. {
  688. return __crypto_dequeue_request(queue, 0);
  689. }
  690. EXPORT_SYMBOL_GPL(crypto_dequeue_request);
  691. int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
  692. {
  693. struct crypto_async_request *req;
  694. list_for_each_entry(req, &queue->list, list) {
  695. if (req->tfm == tfm)
  696. return 1;
  697. }
  698. return 0;
  699. }
  700. EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
  701. static inline void crypto_inc_byte(u8 *a, unsigned int size)
  702. {
  703. u8 *b = (a + size);
  704. u8 c;
  705. for (; size; size--) {
  706. c = *--b + 1;
  707. *b = c;
  708. if (c)
  709. break;
  710. }
  711. }
  712. void crypto_inc(u8 *a, unsigned int size)
  713. {
  714. __be32 *b = (__be32 *)(a + size);
  715. u32 c;
  716. for (; size >= 4; size -= 4) {
  717. c = be32_to_cpu(*--b) + 1;
  718. *b = cpu_to_be32(c);
  719. if (c)
  720. return;
  721. }
  722. crypto_inc_byte(a, size);
  723. }
  724. EXPORT_SYMBOL_GPL(crypto_inc);
  725. static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
  726. {
  727. for (; size; size--)
  728. *a++ ^= *b++;
  729. }
  730. void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
  731. {
  732. u32 *a = (u32 *)dst;
  733. u32 *b = (u32 *)src;
  734. for (; size >= 4; size -= 4)
  735. *a++ ^= *b++;
  736. crypto_xor_byte((u8 *)a, (u8 *)b, size);
  737. }
  738. EXPORT_SYMBOL_GPL(crypto_xor);
  739. static int __init crypto_algapi_init(void)
  740. {
  741. crypto_init_proc();
  742. return 0;
  743. }
  744. static void __exit crypto_algapi_exit(void)
  745. {
  746. crypto_exit_proc();
  747. }
  748. module_init(crypto_algapi_init);
  749. module_exit(crypto_algapi_exit);
  750. MODULE_LICENSE("GPL");
  751. MODULE_DESCRIPTION("Cryptographic algorithms API");