algapi.c 21 KB

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