algapi.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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. if (!spawn->alg)
  547. return;
  548. down_write(&crypto_alg_sem);
  549. list_del(&spawn->list);
  550. up_write(&crypto_alg_sem);
  551. }
  552. EXPORT_SYMBOL_GPL(crypto_drop_spawn);
  553. static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
  554. {
  555. struct crypto_alg *alg;
  556. struct crypto_alg *alg2;
  557. down_read(&crypto_alg_sem);
  558. alg = spawn->alg;
  559. alg2 = alg;
  560. if (alg2)
  561. alg2 = crypto_mod_get(alg2);
  562. up_read(&crypto_alg_sem);
  563. if (!alg2) {
  564. if (alg)
  565. crypto_shoot_alg(alg);
  566. return ERR_PTR(-EAGAIN);
  567. }
  568. return alg;
  569. }
  570. struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
  571. u32 mask)
  572. {
  573. struct crypto_alg *alg;
  574. struct crypto_tfm *tfm;
  575. alg = crypto_spawn_alg(spawn);
  576. if (IS_ERR(alg))
  577. return ERR_CAST(alg);
  578. tfm = ERR_PTR(-EINVAL);
  579. if (unlikely((alg->cra_flags ^ type) & mask))
  580. goto out_put_alg;
  581. tfm = __crypto_alloc_tfm(alg, type, mask);
  582. if (IS_ERR(tfm))
  583. goto out_put_alg;
  584. return tfm;
  585. out_put_alg:
  586. crypto_mod_put(alg);
  587. return tfm;
  588. }
  589. EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
  590. void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
  591. {
  592. struct crypto_alg *alg;
  593. struct crypto_tfm *tfm;
  594. alg = crypto_spawn_alg(spawn);
  595. if (IS_ERR(alg))
  596. return ERR_CAST(alg);
  597. tfm = crypto_create_tfm(alg, spawn->frontend);
  598. if (IS_ERR(tfm))
  599. goto out_put_alg;
  600. return tfm;
  601. out_put_alg:
  602. crypto_mod_put(alg);
  603. return tfm;
  604. }
  605. EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
  606. int crypto_register_notifier(struct notifier_block *nb)
  607. {
  608. return blocking_notifier_chain_register(&crypto_chain, nb);
  609. }
  610. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  611. int crypto_unregister_notifier(struct notifier_block *nb)
  612. {
  613. return blocking_notifier_chain_unregister(&crypto_chain, nb);
  614. }
  615. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  616. struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
  617. {
  618. struct rtattr *rta = tb[0];
  619. struct crypto_attr_type *algt;
  620. if (!rta)
  621. return ERR_PTR(-ENOENT);
  622. if (RTA_PAYLOAD(rta) < sizeof(*algt))
  623. return ERR_PTR(-EINVAL);
  624. if (rta->rta_type != CRYPTOA_TYPE)
  625. return ERR_PTR(-EINVAL);
  626. algt = RTA_DATA(rta);
  627. return algt;
  628. }
  629. EXPORT_SYMBOL_GPL(crypto_get_attr_type);
  630. int crypto_check_attr_type(struct rtattr **tb, u32 type)
  631. {
  632. struct crypto_attr_type *algt;
  633. algt = crypto_get_attr_type(tb);
  634. if (IS_ERR(algt))
  635. return PTR_ERR(algt);
  636. if ((algt->type ^ type) & algt->mask)
  637. return -EINVAL;
  638. return 0;
  639. }
  640. EXPORT_SYMBOL_GPL(crypto_check_attr_type);
  641. const char *crypto_attr_alg_name(struct rtattr *rta)
  642. {
  643. struct crypto_attr_alg *alga;
  644. if (!rta)
  645. return ERR_PTR(-ENOENT);
  646. if (RTA_PAYLOAD(rta) < sizeof(*alga))
  647. return ERR_PTR(-EINVAL);
  648. if (rta->rta_type != CRYPTOA_ALG)
  649. return ERR_PTR(-EINVAL);
  650. alga = RTA_DATA(rta);
  651. alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
  652. return alga->name;
  653. }
  654. EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
  655. struct crypto_alg *crypto_attr_alg2(struct rtattr *rta,
  656. const struct crypto_type *frontend,
  657. u32 type, u32 mask)
  658. {
  659. const char *name;
  660. int err;
  661. name = crypto_attr_alg_name(rta);
  662. err = PTR_ERR(name);
  663. if (IS_ERR(name))
  664. return ERR_PTR(err);
  665. return crypto_find_alg(name, frontend, type, mask);
  666. }
  667. EXPORT_SYMBOL_GPL(crypto_attr_alg2);
  668. int crypto_attr_u32(struct rtattr *rta, u32 *num)
  669. {
  670. struct crypto_attr_u32 *nu32;
  671. if (!rta)
  672. return -ENOENT;
  673. if (RTA_PAYLOAD(rta) < sizeof(*nu32))
  674. return -EINVAL;
  675. if (rta->rta_type != CRYPTOA_U32)
  676. return -EINVAL;
  677. nu32 = RTA_DATA(rta);
  678. *num = nu32->num;
  679. return 0;
  680. }
  681. EXPORT_SYMBOL_GPL(crypto_attr_u32);
  682. void *crypto_alloc_instance2(const char *name, struct crypto_alg *alg,
  683. unsigned int head)
  684. {
  685. struct crypto_instance *inst;
  686. char *p;
  687. int err;
  688. #ifdef CONFIG_CRYPTO_FIPS
  689. if (unlikely(in_fips_err()))
  690. return ERR_PTR(-EACCES);
  691. #endif
  692. p = kzalloc(head + sizeof(*inst) + sizeof(struct crypto_spawn),
  693. GFP_KERNEL);
  694. if (!p)
  695. return ERR_PTR(-ENOMEM);
  696. inst = (void *)(p + head);
  697. err = -ENAMETOOLONG;
  698. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
  699. alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
  700. goto err_free_inst;
  701. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  702. name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  703. goto err_free_inst;
  704. return p;
  705. err_free_inst:
  706. kfree(p);
  707. return ERR_PTR(err);
  708. }
  709. EXPORT_SYMBOL_GPL(crypto_alloc_instance2);
  710. struct crypto_instance *crypto_alloc_instance(const char *name,
  711. struct crypto_alg *alg)
  712. {
  713. struct crypto_instance *inst;
  714. struct crypto_spawn *spawn;
  715. int err;
  716. #ifdef CONFIG_CRYPTO_FIPS
  717. if (unlikely(in_fips_err()))
  718. return ERR_PTR(-EACCES);
  719. #endif
  720. inst = crypto_alloc_instance2(name, alg, 0);
  721. if (IS_ERR(inst))
  722. goto out;
  723. spawn = crypto_instance_ctx(inst);
  724. err = crypto_init_spawn(spawn, alg, inst,
  725. CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC);
  726. if (err)
  727. goto err_free_inst;
  728. return inst;
  729. err_free_inst:
  730. kfree(inst);
  731. inst = ERR_PTR(err);
  732. out:
  733. return inst;
  734. }
  735. EXPORT_SYMBOL_GPL(crypto_alloc_instance);
  736. void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
  737. {
  738. INIT_LIST_HEAD(&queue->list);
  739. queue->backlog = &queue->list;
  740. queue->qlen = 0;
  741. queue->max_qlen = max_qlen;
  742. }
  743. EXPORT_SYMBOL_GPL(crypto_init_queue);
  744. int crypto_enqueue_request(struct crypto_queue *queue,
  745. struct crypto_async_request *request)
  746. {
  747. int err = -EINPROGRESS;
  748. #ifdef CONFIG_CRYPTO_FIPS
  749. if (unlikely(in_fips_err()))
  750. return -EACCES;
  751. #endif
  752. if (unlikely(queue->qlen >= queue->max_qlen)) {
  753. err = -EBUSY;
  754. if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  755. goto out;
  756. if (queue->backlog == &queue->list)
  757. queue->backlog = &request->list;
  758. }
  759. queue->qlen++;
  760. list_add_tail(&request->list, &queue->list);
  761. out:
  762. return err;
  763. }
  764. EXPORT_SYMBOL_GPL(crypto_enqueue_request);
  765. void *__crypto_dequeue_request(struct crypto_queue *queue, unsigned int offset)
  766. {
  767. struct list_head *request;
  768. if (unlikely(!queue->qlen))
  769. return NULL;
  770. queue->qlen--;
  771. if (queue->backlog != &queue->list)
  772. queue->backlog = queue->backlog->next;
  773. request = queue->list.next;
  774. list_del(request);
  775. return (char *)list_entry(request, struct crypto_async_request, list) -
  776. offset;
  777. }
  778. EXPORT_SYMBOL_GPL(__crypto_dequeue_request);
  779. struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
  780. {
  781. return __crypto_dequeue_request(queue, 0);
  782. }
  783. EXPORT_SYMBOL_GPL(crypto_dequeue_request);
  784. int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
  785. {
  786. struct crypto_async_request *req;
  787. list_for_each_entry(req, &queue->list, list) {
  788. if (req->tfm == tfm)
  789. return 1;
  790. }
  791. return 0;
  792. }
  793. EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
  794. static inline void crypto_inc_byte(u8 *a, unsigned int size)
  795. {
  796. u8 *b = (a + size);
  797. u8 c;
  798. for (; size; size--) {
  799. c = *--b + 1;
  800. *b = c;
  801. if (c)
  802. break;
  803. }
  804. }
  805. void crypto_inc(u8 *a, unsigned int size)
  806. {
  807. __be32 *b = (__be32 *)(a + size);
  808. u32 c;
  809. for (; size >= 4; size -= 4) {
  810. c = be32_to_cpu(*--b) + 1;
  811. *b = cpu_to_be32(c);
  812. if (c)
  813. return;
  814. }
  815. crypto_inc_byte(a, size);
  816. }
  817. EXPORT_SYMBOL_GPL(crypto_inc);
  818. static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
  819. {
  820. for (; size; size--)
  821. *a++ ^= *b++;
  822. }
  823. void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
  824. {
  825. u32 *a = (u32 *)dst;
  826. u32 *b = (u32 *)src;
  827. for (; size >= 4; size -= 4)
  828. *a++ ^= *b++;
  829. crypto_xor_byte((u8 *)a, (u8 *)b, size);
  830. }
  831. EXPORT_SYMBOL_GPL(crypto_xor);
  832. static int __init crypto_algapi_init(void)
  833. {
  834. #ifndef CONFIG_CRYPTO_FIPS
  835. crypto_init_proc();
  836. #else
  837. //Moved to testmgr*/
  838. #endif
  839. return 0;
  840. }
  841. static void __exit crypto_algapi_exit(void)
  842. {
  843. #ifndef CONFIG_CRYPTO_FIPS
  844. crypto_exit_proc();
  845. #else
  846. //Moved to testmgr*/
  847. #endif
  848. }
  849. module_init(crypto_algapi_init);
  850. module_exit(crypto_algapi_exit);
  851. MODULE_LICENSE("GPL");
  852. MODULE_DESCRIPTION("Cryptographic algorithms API");