algapi.c 20 KB

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