algapi.c 20 KB

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