algapi.c 22 KB

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