algapi.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. down_write(&crypto_alg_sem);
  503. if (spawn->alg)
  504. list_del(&spawn->list);
  505. up_write(&crypto_alg_sem);
  506. }
  507. EXPORT_SYMBOL_GPL(crypto_drop_spawn);
  508. static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
  509. {
  510. struct crypto_alg *alg;
  511. down_read(&crypto_alg_sem);
  512. alg = spawn->alg;
  513. if (alg && !crypto_mod_get(alg)) {
  514. alg->cra_flags |= CRYPTO_ALG_DYING;
  515. alg = NULL;
  516. }
  517. up_read(&crypto_alg_sem);
  518. return alg ?: ERR_PTR(-EAGAIN);
  519. }
  520. struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
  521. u32 mask)
  522. {
  523. struct crypto_alg *alg;
  524. struct crypto_tfm *tfm;
  525. alg = crypto_spawn_alg(spawn);
  526. if (IS_ERR(alg))
  527. return ERR_CAST(alg);
  528. tfm = ERR_PTR(-EINVAL);
  529. if (unlikely((alg->cra_flags ^ type) & mask))
  530. goto out_put_alg;
  531. tfm = __crypto_alloc_tfm(alg, type, mask);
  532. if (IS_ERR(tfm))
  533. goto out_put_alg;
  534. return tfm;
  535. out_put_alg:
  536. crypto_mod_put(alg);
  537. return tfm;
  538. }
  539. EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
  540. void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
  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 = crypto_create_tfm(alg, spawn->frontend);
  548. if (IS_ERR(tfm))
  549. goto out_put_alg;
  550. return tfm;
  551. out_put_alg:
  552. crypto_mod_put(alg);
  553. return tfm;
  554. }
  555. EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
  556. int crypto_register_notifier(struct notifier_block *nb)
  557. {
  558. return blocking_notifier_chain_register(&crypto_chain, nb);
  559. }
  560. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  561. int crypto_unregister_notifier(struct notifier_block *nb)
  562. {
  563. return blocking_notifier_chain_unregister(&crypto_chain, nb);
  564. }
  565. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  566. struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
  567. {
  568. struct rtattr *rta = tb[0];
  569. struct crypto_attr_type *algt;
  570. if (!rta)
  571. return ERR_PTR(-ENOENT);
  572. if (RTA_PAYLOAD(rta) < sizeof(*algt))
  573. return ERR_PTR(-EINVAL);
  574. if (rta->rta_type != CRYPTOA_TYPE)
  575. return ERR_PTR(-EINVAL);
  576. algt = RTA_DATA(rta);
  577. return algt;
  578. }
  579. EXPORT_SYMBOL_GPL(crypto_get_attr_type);
  580. int crypto_check_attr_type(struct rtattr **tb, u32 type)
  581. {
  582. struct crypto_attr_type *algt;
  583. algt = crypto_get_attr_type(tb);
  584. if (IS_ERR(algt))
  585. return PTR_ERR(algt);
  586. if ((algt->type ^ type) & algt->mask)
  587. return -EINVAL;
  588. return 0;
  589. }
  590. EXPORT_SYMBOL_GPL(crypto_check_attr_type);
  591. const char *crypto_attr_alg_name(struct rtattr *rta)
  592. {
  593. struct crypto_attr_alg *alga;
  594. if (!rta)
  595. return ERR_PTR(-ENOENT);
  596. if (RTA_PAYLOAD(rta) < sizeof(*alga))
  597. return ERR_PTR(-EINVAL);
  598. if (rta->rta_type != CRYPTOA_ALG)
  599. return ERR_PTR(-EINVAL);
  600. alga = RTA_DATA(rta);
  601. alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
  602. return alga->name;
  603. }
  604. EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
  605. struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
  606. const struct crypto_type *frontend,
  607. u32 type, u32 mask)
  608. {
  609. const char *name;
  610. name = crypto_attr_alg_name(rta);
  611. if (IS_ERR(name))
  612. return ERR_CAST(name);
  613. return crypto_find_alg(name, frontend, type, mask);
  614. }
  615. EXPORT_SYMBOL_GPL(crypto_attr_alg2);
  616. int crypto_attr_u32(struct rtattr *rta, u32 *num)
  617. {
  618. struct crypto_attr_u32 *nu32;
  619. if (!rta)
  620. return -ENOENT;
  621. if (RTA_PAYLOAD(rta) < sizeof(*nu32))
  622. return -EINVAL;
  623. if (rta->rta_type != CRYPTOA_U32)
  624. return -EINVAL;
  625. nu32 = RTA_DATA(rta);
  626. *num = nu32->num;
  627. return 0;
  628. }
  629. EXPORT_SYMBOL_GPL(crypto_attr_u32);
  630. int crypto_inst_setname(struct crypto_instance *inst, const char *name,
  631. struct crypto_alg *alg)
  632. {
  633. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
  634. alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
  635. return -ENAMETOOLONG;
  636. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  637. name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  638. return -ENAMETOOLONG;
  639. return 0;
  640. }
  641. EXPORT_SYMBOL_GPL(crypto_inst_setname);
  642. void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg,
  643. unsigned int head)
  644. {
  645. struct crypto_instance *inst;
  646. char *p;
  647. int err;
  648. p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn),
  649. GFP_KERNEL);
  650. if (!p)
  651. return ERR_PTR(-ENOMEM);
  652. inst = (void *)(p + head);
  653. err = crypto_inst_setname(inst, name, alg);
  654. if (err)
  655. goto err_free_inst;
  656. return p;
  657. err_free_inst:
  658. kfree(p);
  659. return ERR_PTR(err);
  660. }
  661. EXPORT_SYMBOL_GPL(crypto_alloc_instance2);
  662. struct crypto_instance *crypto_alloc_instance(const char *name,
  663. struct crypto_alg *alg)
  664. {
  665. struct crypto_instance *inst;
  666. struct crypto_spawn *spawn;
  667. int err;
  668. inst = crypto_alloc_instance2(name, alg, 0);
  669. if (IS_ERR(inst))
  670. goto out;
  671. spawn = crypto_instance_ctx(inst);
  672. err = crypto_init_spawn(spawn, alg, inst,
  673. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  674. if (err)
  675. goto err_free_inst;
  676. return inst;
  677. err_free_inst:
  678. kfree(inst);
  679. inst = ERR_PTR(err);
  680. out:
  681. return inst;
  682. }
  683. EXPORT_SYMBOL_GPL(crypto_alloc_instance);
  684. void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
  685. {
  686. INIT_LIST_HEAD(&queue->list);
  687. queue->backlog = &queue->list;
  688. queue->qlen = 0;
  689. queue->max_qlen = max_qlen;
  690. }
  691. EXPORT_SYMBOL_GPL(crypto_init_queue);
  692. int crypto_enqueue_request(struct crypto_queue *queue,
  693. struct crypto_async_request *request)
  694. {
  695. int err = -EINPROGRESS;
  696. if (unlikely(queue->qlen >= queue->max_qlen)) {
  697. if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
  698. err = -ENOSPC;
  699. goto out;
  700. }
  701. err = -EBUSY;
  702. if (queue->backlog == &queue->list)
  703. queue->backlog = &request->list;
  704. }
  705. queue->qlen++;
  706. list_add_tail(&request->list, &queue->list);
  707. out:
  708. return err;
  709. }
  710. EXPORT_SYMBOL_GPL(crypto_enqueue_request);
  711. struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
  712. {
  713. struct list_head *request;
  714. if (unlikely(!queue->qlen))
  715. return NULL;
  716. queue->qlen--;
  717. if (queue->backlog != &queue->list)
  718. queue->backlog = queue->backlog->next;
  719. request = queue->list.next;
  720. list_del(request);
  721. return list_entry(request, struct crypto_async_request, list);
  722. }
  723. EXPORT_SYMBOL_GPL(crypto_dequeue_request);
  724. int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
  725. {
  726. struct crypto_async_request *req;
  727. list_for_each_entry(req, &queue->list, list) {
  728. if (req->tfm == tfm)
  729. return 1;
  730. }
  731. return 0;
  732. }
  733. EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
  734. static inline void crypto_inc_byte(u8 *a, unsigned int size)
  735. {
  736. u8 *b = (a + size);
  737. u8 c;
  738. for (; size; size--) {
  739. c = *--b + 1;
  740. *b = c;
  741. if (c)
  742. break;
  743. }
  744. }
  745. void crypto_inc(u8 *a, unsigned int size)
  746. {
  747. __be32 *b = (__be32 *)(a + size);
  748. u32 c;
  749. if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
  750. IS_ALIGNED((unsigned long)b, __alignof__(*b)))
  751. for (; size >= 4; size -= 4) {
  752. c = be32_to_cpu(*--b) + 1;
  753. *b = cpu_to_be32(c);
  754. if (likely(c))
  755. return;
  756. }
  757. crypto_inc_byte(a, size);
  758. }
  759. EXPORT_SYMBOL_GPL(crypto_inc);
  760. void __crypto_xor(u8 *dst, const u8 *src1, const u8 *src2, unsigned int len)
  761. {
  762. int relalign = 0;
  763. if (!IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) {
  764. int size = sizeof(unsigned long);
  765. int d = (((unsigned long)dst ^ (unsigned long)src1) |
  766. ((unsigned long)dst ^ (unsigned long)src2)) &
  767. (size - 1);
  768. relalign = d ? 1 << __ffs(d) : size;
  769. /*
  770. * If we care about alignment, process as many bytes as
  771. * needed to advance dst and src to values whose alignments
  772. * equal their relative alignment. This will allow us to
  773. * process the remainder of the input using optimal strides.
  774. */
  775. while (((unsigned long)dst & (relalign - 1)) && len > 0) {
  776. *dst++ = *src1++ ^ *src2++;
  777. len--;
  778. }
  779. }
  780. while (IS_ENABLED(CONFIG_64BIT) && len >= 8 && !(relalign & 7)) {
  781. *(u64 *)dst = *(u64 *)src1 ^ *(u64 *)src2;
  782. dst += 8;
  783. src1 += 8;
  784. src2 += 8;
  785. len -= 8;
  786. }
  787. while (len >= 4 && !(relalign & 3)) {
  788. *(u32 *)dst = *(u32 *)src1 ^ *(u32 *)src2;
  789. dst += 4;
  790. src1 += 4;
  791. src2 += 4;
  792. len -= 4;
  793. }
  794. while (len >= 2 && !(relalign & 1)) {
  795. *(u16 *)dst = *(u16 *)src1 ^ *(u16 *)src2;
  796. dst += 2;
  797. src1 += 2;
  798. src2 += 2;
  799. len -= 2;
  800. }
  801. while (len--)
  802. *dst++ = *src1++ ^ *src2++;
  803. }
  804. EXPORT_SYMBOL_GPL(__crypto_xor);
  805. unsigned int crypto_alg_extsize(struct crypto_alg *alg)
  806. {
  807. return alg->cra_ctxsize +
  808. (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  809. }
  810. EXPORT_SYMBOL_GPL(crypto_alg_extsize);
  811. int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
  812. u32 type, u32 mask)
  813. {
  814. int ret = 0;
  815. struct crypto_alg *alg = crypto_find_alg(name, frontend, type, mask);
  816. if (!IS_ERR(alg)) {
  817. crypto_mod_put(alg);
  818. ret = 1;
  819. }
  820. return ret;
  821. }
  822. EXPORT_SYMBOL_GPL(crypto_type_has_alg);
  823. static int __init crypto_algapi_init(void)
  824. {
  825. crypto_init_proc();
  826. return 0;
  827. }
  828. static void __exit crypto_algapi_exit(void)
  829. {
  830. crypto_exit_proc();
  831. }
  832. module_init(crypto_algapi_init);
  833. module_exit(crypto_algapi_exit);
  834. MODULE_LICENSE("GPL");
  835. MODULE_DESCRIPTION("Cryptographic algorithms API");