algapi.c 22 KB

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