algapi.c 22 KB

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