algapi.c 23 KB

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