algapi.c 23 KB

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