algapi.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. void *__crypto_dequeue_request(struct crypto_queue *queue, unsigned int offset)
  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 (char *)list_entry(request, struct crypto_async_request, list) -
  701. offset;
  702. }
  703. EXPORT_SYMBOL_GPL(__crypto_dequeue_request);
  704. struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
  705. {
  706. return __crypto_dequeue_request(queue, 0);
  707. }
  708. EXPORT_SYMBOL_GPL(crypto_dequeue_request);
  709. int crypto_tfm_in_queue(struct crypto_queue *queue, struct crypto_tfm *tfm)
  710. {
  711. struct crypto_async_request *req;
  712. list_for_each_entry(req, &queue->list, list) {
  713. if (req->tfm == tfm)
  714. return 1;
  715. }
  716. return 0;
  717. }
  718. EXPORT_SYMBOL_GPL(crypto_tfm_in_queue);
  719. static inline void crypto_inc_byte(u8 *a, unsigned int size)
  720. {
  721. u8 *b = (a + size);
  722. u8 c;
  723. for (; size; size--) {
  724. c = *--b + 1;
  725. *b = c;
  726. if (c)
  727. break;
  728. }
  729. }
  730. void crypto_inc(u8 *a, unsigned int size)
  731. {
  732. __be32 *b = (__be32 *)(a + size);
  733. u32 c;
  734. for (; size >= 4; size -= 4) {
  735. c = be32_to_cpu(*--b) + 1;
  736. *b = cpu_to_be32(c);
  737. if (c)
  738. return;
  739. }
  740. crypto_inc_byte(a, size);
  741. }
  742. EXPORT_SYMBOL_GPL(crypto_inc);
  743. static inline void crypto_xor_byte(u8 *a, const u8 *b, unsigned int size)
  744. {
  745. for (; size; size--)
  746. *a++ ^= *b++;
  747. }
  748. void crypto_xor(u8 *dst, const u8 *src, unsigned int size)
  749. {
  750. u32 *a = (u32 *)dst;
  751. u32 *b = (u32 *)src;
  752. for (; size >= 4; size -= 4)
  753. *a++ ^= *b++;
  754. crypto_xor_byte((u8 *)a, (u8 *)b, size);
  755. }
  756. EXPORT_SYMBOL_GPL(crypto_xor);
  757. unsigned int crypto_alg_extsize(struct crypto_alg *alg)
  758. {
  759. return alg->cra_ctxsize +
  760. (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  761. }
  762. EXPORT_SYMBOL_GPL(crypto_alg_extsize);
  763. static int __init crypto_algapi_init(void)
  764. {
  765. crypto_init_proc();
  766. return 0;
  767. }
  768. static void __exit crypto_algapi_exit(void)
  769. {
  770. crypto_exit_proc();
  771. }
  772. module_init(crypto_algapi_init);
  773. module_exit(crypto_algapi_exit);
  774. MODULE_LICENSE("GPL");
  775. MODULE_DESCRIPTION("Cryptographic algorithms API");