algapi.c 21 KB

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