algapi.c 22 KB

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