algapi.c 20 KB

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