algapi.c 22 KB

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