algapi.c 21 KB

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