algapi.c 20 KB

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