algapi.c 21 KB

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