algapi.c 22 KB

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