algapi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Cryptographic API for algorithms (i.e., low-level API).
  4. *
  5. * Copyright (c) 2006 Herbert Xu <[email protected]>
  6. */
  7. #include <crypto/algapi.h>
  8. #include <crypto/internal/simd.h>
  9. #include <linux/err.h>
  10. #include <linux/errno.h>
  11. #include <linux/fips.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/slab.h>
  18. #include <linux/string.h>
  19. #include "internal.h"
  20. static LIST_HEAD(crypto_template_list);
  21. #ifdef CONFIG_CRYPTO_MANAGER_EXTRA_TESTS
  22. DEFINE_PER_CPU(bool, crypto_simd_disabled_for_test);
  23. EXPORT_PER_CPU_SYMBOL_GPL(crypto_simd_disabled_for_test);
  24. #endif
  25. static inline void crypto_check_module_sig(struct module *mod)
  26. {
  27. if (fips_enabled && mod && !module_sig_ok(mod))
  28. panic("Module %s signature verification failed in FIPS mode\n",
  29. module_name(mod));
  30. }
  31. static int crypto_check_alg(struct crypto_alg *alg)
  32. {
  33. crypto_check_module_sig(alg->cra_module);
  34. if (!alg->cra_name[0] || !alg->cra_driver_name[0])
  35. return -EINVAL;
  36. if (alg->cra_alignmask & (alg->cra_alignmask + 1))
  37. return -EINVAL;
  38. /* General maximums for all algs. */
  39. if (alg->cra_alignmask > MAX_ALGAPI_ALIGNMASK)
  40. return -EINVAL;
  41. if (alg->cra_blocksize > MAX_ALGAPI_BLOCKSIZE)
  42. return -EINVAL;
  43. /* Lower maximums for specific alg types. */
  44. if (!alg->cra_type && (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  45. CRYPTO_ALG_TYPE_CIPHER) {
  46. if (alg->cra_alignmask > MAX_CIPHER_ALIGNMASK)
  47. return -EINVAL;
  48. if (alg->cra_blocksize > MAX_CIPHER_BLOCKSIZE)
  49. return -EINVAL;
  50. }
  51. if (alg->cra_priority < 0)
  52. return -EINVAL;
  53. refcount_set(&alg->cra_refcnt, 1);
  54. return 0;
  55. }
  56. static void crypto_free_instance(struct crypto_instance *inst)
  57. {
  58. inst->alg.cra_type->free(inst);
  59. }
  60. static void crypto_destroy_instance(struct crypto_alg *alg)
  61. {
  62. struct crypto_instance *inst = (void *)alg;
  63. struct crypto_template *tmpl = inst->tmpl;
  64. crypto_free_instance(inst);
  65. crypto_tmpl_put(tmpl);
  66. }
  67. /*
  68. * This function adds a spawn to the list secondary_spawns which
  69. * will be used at the end of crypto_remove_spawns to unregister
  70. * instances, unless the spawn happens to be one that is depended
  71. * on by the new algorithm (nalg in crypto_remove_spawns).
  72. *
  73. * This function is also responsible for resurrecting any algorithms
  74. * in the dependency chain of nalg by unsetting n->dead.
  75. */
  76. static struct list_head *crypto_more_spawns(struct crypto_alg *alg,
  77. struct list_head *stack,
  78. struct list_head *top,
  79. struct list_head *secondary_spawns)
  80. {
  81. struct crypto_spawn *spawn, *n;
  82. spawn = list_first_entry_or_null(stack, struct crypto_spawn, list);
  83. if (!spawn)
  84. return NULL;
  85. n = list_prev_entry(spawn, list);
  86. list_move(&spawn->list, secondary_spawns);
  87. if (list_is_last(&n->list, stack))
  88. return top;
  89. n = list_next_entry(n, list);
  90. if (!spawn->dead)
  91. n->dead = false;
  92. return &n->inst->alg.cra_users;
  93. }
  94. static void crypto_remove_instance(struct crypto_instance *inst,
  95. struct list_head *list)
  96. {
  97. struct crypto_template *tmpl = inst->tmpl;
  98. if (crypto_is_dead(&inst->alg))
  99. return;
  100. inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
  101. if (!tmpl || !crypto_tmpl_get(tmpl))
  102. return;
  103. list_move(&inst->alg.cra_list, list);
  104. hlist_del(&inst->list);
  105. inst->alg.cra_destroy = crypto_destroy_instance;
  106. BUG_ON(!list_empty(&inst->alg.cra_users));
  107. }
  108. /*
  109. * Given an algorithm alg, remove all algorithms that depend on it
  110. * through spawns. If nalg is not null, then exempt any algorithms
  111. * that is depended on by nalg. This is useful when nalg itself
  112. * depends on alg.
  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. /*
  130. * Perform a depth-first walk starting from alg through
  131. * the cra_users tree. The list stack records the path
  132. * from alg to the current spawn.
  133. */
  134. spawns = &top;
  135. do {
  136. while (!list_empty(spawns)) {
  137. struct crypto_instance *inst;
  138. spawn = list_first_entry(spawns, struct crypto_spawn,
  139. list);
  140. inst = spawn->inst;
  141. list_move(&spawn->list, &stack);
  142. spawn->dead = !spawn->registered || &inst->alg != nalg;
  143. if (!spawn->registered)
  144. break;
  145. BUG_ON(&inst->alg == alg);
  146. if (&inst->alg == nalg)
  147. break;
  148. spawns = &inst->alg.cra_users;
  149. /*
  150. * Even if spawn->registered is true, the
  151. * instance itself may still be unregistered.
  152. * This is because it may have failed during
  153. * registration. Therefore we still need to
  154. * make the following test.
  155. *
  156. * We may encounter an unregistered instance here, since
  157. * an instance's spawns are set up prior to the instance
  158. * being registered. An unregistered instance will have
  159. * NULL ->cra_users.next, since ->cra_users isn't
  160. * properly initialized until registration. But an
  161. * unregistered instance cannot have any users, so treat
  162. * it the same as ->cra_users being empty.
  163. */
  164. if (spawns->next == NULL)
  165. break;
  166. }
  167. } while ((spawns = crypto_more_spawns(alg, &stack, &top,
  168. &secondary_spawns)));
  169. /*
  170. * Remove all instances that are marked as dead. Also
  171. * complete the resurrection of the others by moving them
  172. * back to the cra_users list.
  173. */
  174. list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
  175. if (!spawn->dead)
  176. list_move(&spawn->list, &spawn->alg->cra_users);
  177. else if (spawn->registered)
  178. crypto_remove_instance(spawn->inst, list);
  179. }
  180. }
  181. EXPORT_SYMBOL_GPL(crypto_remove_spawns);
  182. static void crypto_alg_finish_registration(struct crypto_alg *alg,
  183. bool fulfill_requests,
  184. struct list_head *algs_to_put)
  185. {
  186. struct crypto_alg *q;
  187. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  188. if (q == alg)
  189. continue;
  190. if (crypto_is_moribund(q))
  191. continue;
  192. if (crypto_is_larval(q)) {
  193. struct crypto_larval *larval = (void *)q;
  194. /*
  195. * Check to see if either our generic name or
  196. * specific name can satisfy the name requested
  197. * by the larval entry q.
  198. */
  199. if (strcmp(alg->cra_name, q->cra_name) &&
  200. strcmp(alg->cra_driver_name, q->cra_name))
  201. continue;
  202. if (larval->adult)
  203. continue;
  204. if ((q->cra_flags ^ alg->cra_flags) & larval->mask)
  205. continue;
  206. if (fulfill_requests && crypto_mod_get(alg))
  207. larval->adult = alg;
  208. else
  209. larval->adult = ERR_PTR(-EAGAIN);
  210. continue;
  211. }
  212. if (strcmp(alg->cra_name, q->cra_name))
  213. continue;
  214. if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
  215. q->cra_priority > alg->cra_priority)
  216. continue;
  217. crypto_remove_spawns(q, algs_to_put, alg);
  218. }
  219. crypto_notify(CRYPTO_MSG_ALG_LOADED, alg);
  220. }
  221. static struct crypto_larval *crypto_alloc_test_larval(struct crypto_alg *alg)
  222. {
  223. struct crypto_larval *larval;
  224. if (!IS_ENABLED(CONFIG_CRYPTO_MANAGER) ||
  225. IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS) ||
  226. (alg->cra_flags & CRYPTO_ALG_INTERNAL))
  227. return NULL; /* No self-test needed */
  228. larval = crypto_larval_alloc(alg->cra_name,
  229. alg->cra_flags | CRYPTO_ALG_TESTED, 0);
  230. if (IS_ERR(larval))
  231. return larval;
  232. larval->adult = crypto_mod_get(alg);
  233. if (!larval->adult) {
  234. kfree(larval);
  235. return ERR_PTR(-ENOENT);
  236. }
  237. refcount_set(&larval->alg.cra_refcnt, 1);
  238. memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
  239. CRYPTO_MAX_ALG_NAME);
  240. larval->alg.cra_priority = alg->cra_priority;
  241. return larval;
  242. }
  243. static struct crypto_larval *
  244. __crypto_register_alg(struct crypto_alg *alg, struct list_head *algs_to_put)
  245. {
  246. struct crypto_alg *q;
  247. struct crypto_larval *larval;
  248. int ret = -EAGAIN;
  249. if (crypto_is_dead(alg))
  250. goto err;
  251. INIT_LIST_HEAD(&alg->cra_users);
  252. ret = -EEXIST;
  253. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  254. if (q == alg)
  255. goto err;
  256. if (crypto_is_moribund(q))
  257. continue;
  258. if (crypto_is_larval(q)) {
  259. if (!strcmp(alg->cra_driver_name, q->cra_driver_name))
  260. goto err;
  261. continue;
  262. }
  263. if (!strcmp(q->cra_driver_name, alg->cra_name) ||
  264. !strcmp(q->cra_name, alg->cra_driver_name))
  265. goto err;
  266. }
  267. larval = crypto_alloc_test_larval(alg);
  268. if (IS_ERR(larval))
  269. goto out;
  270. list_add(&alg->cra_list, &crypto_alg_list);
  271. crypto_stats_init(alg);
  272. if (larval) {
  273. /* No cheating! */
  274. alg->cra_flags &= ~CRYPTO_ALG_TESTED;
  275. list_add(&larval->alg.cra_list, &crypto_alg_list);
  276. } else {
  277. alg->cra_flags |= CRYPTO_ALG_TESTED;
  278. crypto_alg_finish_registration(alg, true, algs_to_put);
  279. }
  280. out:
  281. return larval;
  282. err:
  283. larval = ERR_PTR(ret);
  284. goto out;
  285. }
  286. void crypto_alg_tested(const char *name, int err)
  287. {
  288. struct crypto_larval *test;
  289. struct crypto_alg *alg;
  290. struct crypto_alg *q;
  291. LIST_HEAD(list);
  292. bool best;
  293. down_write(&crypto_alg_sem);
  294. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  295. if (crypto_is_moribund(q) || !crypto_is_larval(q))
  296. continue;
  297. test = (struct crypto_larval *)q;
  298. if (!strcmp(q->cra_driver_name, name))
  299. goto found;
  300. }
  301. pr_err("alg: Unexpected test result for %s: %d\n", name, err);
  302. goto unlock;
  303. found:
  304. q->cra_flags |= CRYPTO_ALG_DEAD;
  305. alg = test->adult;
  306. if (list_empty(&alg->cra_list))
  307. goto complete;
  308. if (err == -ECANCELED)
  309. alg->cra_flags |= CRYPTO_ALG_FIPS_INTERNAL;
  310. else if (err)
  311. goto complete;
  312. else
  313. alg->cra_flags &= ~CRYPTO_ALG_FIPS_INTERNAL;
  314. alg->cra_flags |= CRYPTO_ALG_TESTED;
  315. /*
  316. * If a higher-priority implementation of the same algorithm is
  317. * currently being tested, then don't fulfill request larvals.
  318. */
  319. best = true;
  320. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  321. if (crypto_is_moribund(q) || !crypto_is_larval(q))
  322. continue;
  323. if (strcmp(alg->cra_name, q->cra_name))
  324. continue;
  325. if (q->cra_priority > alg->cra_priority) {
  326. best = false;
  327. break;
  328. }
  329. }
  330. crypto_alg_finish_registration(alg, best, &list);
  331. complete:
  332. complete_all(&test->completion);
  333. unlock:
  334. up_write(&crypto_alg_sem);
  335. crypto_remove_final(&list);
  336. }
  337. EXPORT_SYMBOL_GPL(crypto_alg_tested);
  338. void crypto_remove_final(struct list_head *list)
  339. {
  340. struct crypto_alg *alg;
  341. struct crypto_alg *n;
  342. list_for_each_entry_safe(alg, n, list, cra_list) {
  343. list_del_init(&alg->cra_list);
  344. crypto_alg_put(alg);
  345. }
  346. }
  347. EXPORT_SYMBOL_GPL(crypto_remove_final);
  348. int crypto_register_alg(struct crypto_alg *alg)
  349. {
  350. struct crypto_larval *larval;
  351. LIST_HEAD(algs_to_put);
  352. bool test_started = false;
  353. int err;
  354. alg->cra_flags &= ~CRYPTO_ALG_DEAD;
  355. err = crypto_check_alg(alg);
  356. if (err)
  357. return err;
  358. down_write(&crypto_alg_sem);
  359. larval = __crypto_register_alg(alg, &algs_to_put);
  360. if (!IS_ERR_OR_NULL(larval)) {
  361. test_started = crypto_boot_test_finished();
  362. larval->test_started = test_started;
  363. }
  364. up_write(&crypto_alg_sem);
  365. if (IS_ERR(larval))
  366. return PTR_ERR(larval);
  367. if (test_started)
  368. crypto_wait_for_test(larval);
  369. crypto_remove_final(&algs_to_put);
  370. return 0;
  371. }
  372. EXPORT_SYMBOL_GPL(crypto_register_alg);
  373. static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
  374. {
  375. if (unlikely(list_empty(&alg->cra_list)))
  376. return -ENOENT;
  377. alg->cra_flags |= CRYPTO_ALG_DEAD;
  378. list_del_init(&alg->cra_list);
  379. crypto_remove_spawns(alg, list, NULL);
  380. return 0;
  381. }
  382. void crypto_unregister_alg(struct crypto_alg *alg)
  383. {
  384. int ret;
  385. LIST_HEAD(list);
  386. down_write(&crypto_alg_sem);
  387. ret = crypto_remove_alg(alg, &list);
  388. up_write(&crypto_alg_sem);
  389. if (WARN(ret, "Algorithm %s is not registered", alg->cra_driver_name))
  390. return;
  391. if (WARN_ON(refcount_read(&alg->cra_refcnt) != 1))
  392. return;
  393. if (alg->cra_destroy)
  394. alg->cra_destroy(alg);
  395. crypto_remove_final(&list);
  396. }
  397. EXPORT_SYMBOL_GPL(crypto_unregister_alg);
  398. int crypto_register_algs(struct crypto_alg *algs, int count)
  399. {
  400. int i, ret;
  401. for (i = 0; i < count; i++) {
  402. ret = crypto_register_alg(&algs[i]);
  403. if (ret)
  404. goto err;
  405. }
  406. return 0;
  407. err:
  408. for (--i; i >= 0; --i)
  409. crypto_unregister_alg(&algs[i]);
  410. return ret;
  411. }
  412. EXPORT_SYMBOL_GPL(crypto_register_algs);
  413. void crypto_unregister_algs(struct crypto_alg *algs, int count)
  414. {
  415. int i;
  416. for (i = 0; i < count; i++)
  417. crypto_unregister_alg(&algs[i]);
  418. }
  419. EXPORT_SYMBOL_GPL(crypto_unregister_algs);
  420. int crypto_register_template(struct crypto_template *tmpl)
  421. {
  422. struct crypto_template *q;
  423. int err = -EEXIST;
  424. down_write(&crypto_alg_sem);
  425. crypto_check_module_sig(tmpl->module);
  426. list_for_each_entry(q, &crypto_template_list, list) {
  427. if (q == tmpl)
  428. goto out;
  429. }
  430. list_add(&tmpl->list, &crypto_template_list);
  431. err = 0;
  432. out:
  433. up_write(&crypto_alg_sem);
  434. return err;
  435. }
  436. EXPORT_SYMBOL_GPL(crypto_register_template);
  437. int crypto_register_templates(struct crypto_template *tmpls, int count)
  438. {
  439. int i, err;
  440. for (i = 0; i < count; i++) {
  441. err = crypto_register_template(&tmpls[i]);
  442. if (err)
  443. goto out;
  444. }
  445. return 0;
  446. out:
  447. for (--i; i >= 0; --i)
  448. crypto_unregister_template(&tmpls[i]);
  449. return err;
  450. }
  451. EXPORT_SYMBOL_GPL(crypto_register_templates);
  452. void crypto_unregister_template(struct crypto_template *tmpl)
  453. {
  454. struct crypto_instance *inst;
  455. struct hlist_node *n;
  456. struct hlist_head *list;
  457. LIST_HEAD(users);
  458. down_write(&crypto_alg_sem);
  459. BUG_ON(list_empty(&tmpl->list));
  460. list_del_init(&tmpl->list);
  461. list = &tmpl->instances;
  462. hlist_for_each_entry(inst, list, list) {
  463. int err = crypto_remove_alg(&inst->alg, &users);
  464. BUG_ON(err);
  465. }
  466. up_write(&crypto_alg_sem);
  467. hlist_for_each_entry_safe(inst, n, list, list) {
  468. BUG_ON(refcount_read(&inst->alg.cra_refcnt) != 1);
  469. crypto_free_instance(inst);
  470. }
  471. crypto_remove_final(&users);
  472. }
  473. EXPORT_SYMBOL_GPL(crypto_unregister_template);
  474. void crypto_unregister_templates(struct crypto_template *tmpls, int count)
  475. {
  476. int i;
  477. for (i = count - 1; i >= 0; --i)
  478. crypto_unregister_template(&tmpls[i]);
  479. }
  480. EXPORT_SYMBOL_GPL(crypto_unregister_templates);
  481. static struct crypto_template *__crypto_lookup_template(const char *name)
  482. {
  483. struct crypto_template *q, *tmpl = NULL;
  484. down_read(&crypto_alg_sem);
  485. list_for_each_entry(q, &crypto_template_list, list) {
  486. if (strcmp(q->name, name))
  487. continue;
  488. if (unlikely(!crypto_tmpl_get(q)))
  489. continue;
  490. tmpl = q;
  491. break;
  492. }
  493. up_read(&crypto_alg_sem);
  494. return tmpl;
  495. }
  496. struct crypto_template *crypto_lookup_template(const char *name)
  497. {
  498. return try_then_request_module(__crypto_lookup_template(name),
  499. "crypto-%s", name);
  500. }
  501. EXPORT_SYMBOL_GPL(crypto_lookup_template);
  502. int crypto_register_instance(struct crypto_template *tmpl,
  503. struct crypto_instance *inst)
  504. {
  505. struct crypto_larval *larval;
  506. struct crypto_spawn *spawn;
  507. u32 fips_internal = 0;
  508. LIST_HEAD(algs_to_put);
  509. int err;
  510. err = crypto_check_alg(&inst->alg);
  511. if (err)
  512. return err;
  513. inst->alg.cra_module = tmpl->module;
  514. inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
  515. down_write(&crypto_alg_sem);
  516. larval = ERR_PTR(-EAGAIN);
  517. for (spawn = inst->spawns; spawn;) {
  518. struct crypto_spawn *next;
  519. if (spawn->dead)
  520. goto unlock;
  521. next = spawn->next;
  522. spawn->inst = inst;
  523. spawn->registered = true;
  524. fips_internal |= spawn->alg->cra_flags;
  525. crypto_mod_put(spawn->alg);
  526. spawn = next;
  527. }
  528. inst->alg.cra_flags |= (fips_internal & CRYPTO_ALG_FIPS_INTERNAL);
  529. larval = __crypto_register_alg(&inst->alg, &algs_to_put);
  530. if (IS_ERR(larval))
  531. goto unlock;
  532. else if (larval)
  533. larval->test_started = true;
  534. hlist_add_head(&inst->list, &tmpl->instances);
  535. inst->tmpl = tmpl;
  536. unlock:
  537. up_write(&crypto_alg_sem);
  538. if (IS_ERR(larval))
  539. return PTR_ERR(larval);
  540. if (larval)
  541. crypto_wait_for_test(larval);
  542. crypto_remove_final(&algs_to_put);
  543. return 0;
  544. }
  545. EXPORT_SYMBOL_GPL(crypto_register_instance);
  546. void crypto_unregister_instance(struct crypto_instance *inst)
  547. {
  548. LIST_HEAD(list);
  549. down_write(&crypto_alg_sem);
  550. crypto_remove_spawns(&inst->alg, &list, NULL);
  551. crypto_remove_instance(inst, &list);
  552. up_write(&crypto_alg_sem);
  553. crypto_remove_final(&list);
  554. }
  555. EXPORT_SYMBOL_GPL(crypto_unregister_instance);
  556. int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst,
  557. const char *name, u32 type, u32 mask)
  558. {
  559. struct crypto_alg *alg;
  560. int err = -EAGAIN;
  561. if (WARN_ON_ONCE(inst == NULL))
  562. return -EINVAL;
  563. /* Allow the result of crypto_attr_alg_name() to be passed directly */
  564. if (IS_ERR(name))
  565. return PTR_ERR(name);
  566. alg = crypto_find_alg(name, spawn->frontend,
  567. type | CRYPTO_ALG_FIPS_INTERNAL, mask);
  568. if (IS_ERR(alg))
  569. return PTR_ERR(alg);
  570. down_write(&crypto_alg_sem);
  571. if (!crypto_is_moribund(alg)) {
  572. list_add(&spawn->list, &alg->cra_users);
  573. spawn->alg = alg;
  574. spawn->mask = mask;
  575. spawn->next = inst->spawns;
  576. inst->spawns = spawn;
  577. inst->alg.cra_flags |=
  578. (alg->cra_flags & CRYPTO_ALG_INHERITED_FLAGS);
  579. err = 0;
  580. }
  581. up_write(&crypto_alg_sem);
  582. if (err)
  583. crypto_mod_put(alg);
  584. return err;
  585. }
  586. EXPORT_SYMBOL_GPL(crypto_grab_spawn);
  587. void crypto_drop_spawn(struct crypto_spawn *spawn)
  588. {
  589. if (!spawn->alg) /* not yet initialized? */
  590. return;
  591. down_write(&crypto_alg_sem);
  592. if (!spawn->dead)
  593. list_del(&spawn->list);
  594. up_write(&crypto_alg_sem);
  595. if (!spawn->registered)
  596. crypto_mod_put(spawn->alg);
  597. }
  598. EXPORT_SYMBOL_GPL(crypto_drop_spawn);
  599. static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
  600. {
  601. struct crypto_alg *alg = ERR_PTR(-EAGAIN);
  602. struct crypto_alg *target;
  603. bool shoot = false;
  604. down_read(&crypto_alg_sem);
  605. if (!spawn->dead) {
  606. alg = spawn->alg;
  607. if (!crypto_mod_get(alg)) {
  608. target = crypto_alg_get(alg);
  609. shoot = true;
  610. alg = ERR_PTR(-EAGAIN);
  611. }
  612. }
  613. up_read(&crypto_alg_sem);
  614. if (shoot) {
  615. crypto_shoot_alg(target);
  616. crypto_alg_put(target);
  617. }
  618. return alg;
  619. }
  620. struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
  621. u32 mask)
  622. {
  623. struct crypto_alg *alg;
  624. struct crypto_tfm *tfm;
  625. alg = crypto_spawn_alg(spawn);
  626. if (IS_ERR(alg))
  627. return ERR_CAST(alg);
  628. tfm = ERR_PTR(-EINVAL);
  629. if (unlikely((alg->cra_flags ^ type) & mask))
  630. goto out_put_alg;
  631. tfm = __crypto_alloc_tfm(alg, type, mask);
  632. if (IS_ERR(tfm))
  633. goto out_put_alg;
  634. return tfm;
  635. out_put_alg:
  636. crypto_mod_put(alg);
  637. return tfm;
  638. }
  639. EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
  640. void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
  641. {
  642. struct crypto_alg *alg;
  643. struct crypto_tfm *tfm;
  644. alg = crypto_spawn_alg(spawn);
  645. if (IS_ERR(alg))
  646. return ERR_CAST(alg);
  647. tfm = crypto_create_tfm(alg, spawn->frontend);
  648. if (IS_ERR(tfm))
  649. goto out_put_alg;
  650. return tfm;
  651. out_put_alg:
  652. crypto_mod_put(alg);
  653. return tfm;
  654. }
  655. EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
  656. int crypto_register_notifier(struct notifier_block *nb)
  657. {
  658. return blocking_notifier_chain_register(&crypto_chain, nb);
  659. }
  660. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  661. int crypto_unregister_notifier(struct notifier_block *nb)
  662. {
  663. return blocking_notifier_chain_unregister(&crypto_chain, nb);
  664. }
  665. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  666. struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
  667. {
  668. struct rtattr *rta = tb[0];
  669. struct crypto_attr_type *algt;
  670. if (!rta)
  671. return ERR_PTR(-ENOENT);
  672. if (RTA_PAYLOAD(rta) < sizeof(*algt))
  673. return ERR_PTR(-EINVAL);
  674. if (rta->rta_type != CRYPTOA_TYPE)
  675. return ERR_PTR(-EINVAL);
  676. algt = RTA_DATA(rta);
  677. return algt;
  678. }
  679. EXPORT_SYMBOL_GPL(crypto_get_attr_type);
  680. /**
  681. * crypto_check_attr_type() - check algorithm type and compute inherited mask
  682. * @tb: the template parameters
  683. * @type: the algorithm type the template would be instantiated as
  684. * @mask_ret: (output) the mask that should be passed to crypto_grab_*()
  685. * to restrict the flags of any inner algorithms
  686. *
  687. * Validate that the algorithm type the user requested is compatible with the
  688. * one the template would actually be instantiated as. E.g., if the user is
  689. * doing crypto_alloc_shash("cbc(aes)", ...), this would return an error because
  690. * the "cbc" template creates an "skcipher" algorithm, not an "shash" algorithm.
  691. *
  692. * Also compute the mask to use to restrict the flags of any inner algorithms.
  693. *
  694. * Return: 0 on success; -errno on failure
  695. */
  696. int crypto_check_attr_type(struct rtattr **tb, u32 type, u32 *mask_ret)
  697. {
  698. struct crypto_attr_type *algt;
  699. algt = crypto_get_attr_type(tb);
  700. if (IS_ERR(algt))
  701. return PTR_ERR(algt);
  702. if ((algt->type ^ type) & algt->mask)
  703. return -EINVAL;
  704. *mask_ret = crypto_algt_inherited_mask(algt);
  705. return 0;
  706. }
  707. EXPORT_SYMBOL_GPL(crypto_check_attr_type);
  708. const char *crypto_attr_alg_name(struct rtattr *rta)
  709. {
  710. struct crypto_attr_alg *alga;
  711. if (!rta)
  712. return ERR_PTR(-ENOENT);
  713. if (RTA_PAYLOAD(rta) < sizeof(*alga))
  714. return ERR_PTR(-EINVAL);
  715. if (rta->rta_type != CRYPTOA_ALG)
  716. return ERR_PTR(-EINVAL);
  717. alga = RTA_DATA(rta);
  718. alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
  719. return alga->name;
  720. }
  721. EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
  722. int crypto_inst_setname(struct crypto_instance *inst, const char *name,
  723. struct crypto_alg *alg)
  724. {
  725. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
  726. alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
  727. return -ENAMETOOLONG;
  728. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  729. name, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  730. return -ENAMETOOLONG;
  731. return 0;
  732. }
  733. EXPORT_SYMBOL_GPL(crypto_inst_setname);
  734. void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
  735. {
  736. INIT_LIST_HEAD(&queue->list);
  737. queue->backlog = &queue->list;
  738. queue->qlen = 0;
  739. queue->max_qlen = max_qlen;
  740. }
  741. EXPORT_SYMBOL_GPL(crypto_init_queue);
  742. int crypto_enqueue_request(struct crypto_queue *queue,
  743. struct crypto_async_request *request)
  744. {
  745. int err = -EINPROGRESS;
  746. if (unlikely(queue->qlen >= queue->max_qlen)) {
  747. if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
  748. err = -ENOSPC;
  749. goto out;
  750. }
  751. err = -EBUSY;
  752. if (queue->backlog == &queue->list)
  753. queue->backlog = &request->list;
  754. }
  755. queue->qlen++;
  756. list_add_tail(&request->list, &queue->list);
  757. out:
  758. return err;
  759. }
  760. EXPORT_SYMBOL_GPL(crypto_enqueue_request);
  761. void crypto_enqueue_request_head(struct crypto_queue *queue,
  762. struct crypto_async_request *request)
  763. {
  764. if (unlikely(queue->qlen >= queue->max_qlen))
  765. queue->backlog = queue->backlog->prev;
  766. queue->qlen++;
  767. list_add(&request->list, &queue->list);
  768. }
  769. EXPORT_SYMBOL_GPL(crypto_enqueue_request_head);
  770. struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
  771. {
  772. struct list_head *request;
  773. if (unlikely(!queue->qlen))
  774. return NULL;
  775. queue->qlen--;
  776. if (queue->backlog != &queue->list)
  777. queue->backlog = queue->backlog->next;
  778. request = queue->list.next;
  779. list_del(request);
  780. return list_entry(request, struct crypto_async_request, list);
  781. }
  782. EXPORT_SYMBOL_GPL(crypto_dequeue_request);
  783. static inline void crypto_inc_byte(u8 *a, unsigned int size)
  784. {
  785. u8 *b = (a + size);
  786. u8 c;
  787. for (; size; size--) {
  788. c = *--b + 1;
  789. *b = c;
  790. if (c)
  791. break;
  792. }
  793. }
  794. void crypto_inc(u8 *a, unsigned int size)
  795. {
  796. __be32 *b = (__be32 *)(a + size);
  797. u32 c;
  798. if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
  799. IS_ALIGNED((unsigned long)b, __alignof__(*b)))
  800. for (; size >= 4; size -= 4) {
  801. c = be32_to_cpu(*--b) + 1;
  802. *b = cpu_to_be32(c);
  803. if (likely(c))
  804. return;
  805. }
  806. crypto_inc_byte(a, size);
  807. }
  808. EXPORT_SYMBOL_GPL(crypto_inc);
  809. unsigned int crypto_alg_extsize(struct crypto_alg *alg)
  810. {
  811. return alg->cra_ctxsize +
  812. (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  813. }
  814. EXPORT_SYMBOL_GPL(crypto_alg_extsize);
  815. int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
  816. u32 type, u32 mask)
  817. {
  818. int ret = 0;
  819. struct crypto_alg *alg = crypto_find_alg(name, frontend, type, mask);
  820. if (!IS_ERR(alg)) {
  821. crypto_mod_put(alg);
  822. ret = 1;
  823. }
  824. return ret;
  825. }
  826. EXPORT_SYMBOL_GPL(crypto_type_has_alg);
  827. #ifdef CONFIG_CRYPTO_STATS
  828. void crypto_stats_init(struct crypto_alg *alg)
  829. {
  830. memset(&alg->stats, 0, sizeof(alg->stats));
  831. }
  832. EXPORT_SYMBOL_GPL(crypto_stats_init);
  833. void crypto_stats_get(struct crypto_alg *alg)
  834. {
  835. crypto_alg_get(alg);
  836. }
  837. EXPORT_SYMBOL_GPL(crypto_stats_get);
  838. void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg,
  839. int ret)
  840. {
  841. if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
  842. atomic64_inc(&alg->stats.aead.err_cnt);
  843. } else {
  844. atomic64_inc(&alg->stats.aead.encrypt_cnt);
  845. atomic64_add(cryptlen, &alg->stats.aead.encrypt_tlen);
  846. }
  847. crypto_alg_put(alg);
  848. }
  849. EXPORT_SYMBOL_GPL(crypto_stats_aead_encrypt);
  850. void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg,
  851. int ret)
  852. {
  853. if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
  854. atomic64_inc(&alg->stats.aead.err_cnt);
  855. } else {
  856. atomic64_inc(&alg->stats.aead.decrypt_cnt);
  857. atomic64_add(cryptlen, &alg->stats.aead.decrypt_tlen);
  858. }
  859. crypto_alg_put(alg);
  860. }
  861. EXPORT_SYMBOL_GPL(crypto_stats_aead_decrypt);
  862. void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret,
  863. struct crypto_alg *alg)
  864. {
  865. if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
  866. atomic64_inc(&alg->stats.akcipher.err_cnt);
  867. } else {
  868. atomic64_inc(&alg->stats.akcipher.encrypt_cnt);
  869. atomic64_add(src_len, &alg->stats.akcipher.encrypt_tlen);
  870. }
  871. crypto_alg_put(alg);
  872. }
  873. EXPORT_SYMBOL_GPL(crypto_stats_akcipher_encrypt);
  874. void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret,
  875. struct crypto_alg *alg)
  876. {
  877. if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
  878. atomic64_inc(&alg->stats.akcipher.err_cnt);
  879. } else {
  880. atomic64_inc(&alg->stats.akcipher.decrypt_cnt);
  881. atomic64_add(src_len, &alg->stats.akcipher.decrypt_tlen);
  882. }
  883. crypto_alg_put(alg);
  884. }
  885. EXPORT_SYMBOL_GPL(crypto_stats_akcipher_decrypt);
  886. void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg)
  887. {
  888. if (ret && ret != -EINPROGRESS && ret != -EBUSY)
  889. atomic64_inc(&alg->stats.akcipher.err_cnt);
  890. else
  891. atomic64_inc(&alg->stats.akcipher.sign_cnt);
  892. crypto_alg_put(alg);
  893. }
  894. EXPORT_SYMBOL_GPL(crypto_stats_akcipher_sign);
  895. void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg)
  896. {
  897. if (ret && ret != -EINPROGRESS && ret != -EBUSY)
  898. atomic64_inc(&alg->stats.akcipher.err_cnt);
  899. else
  900. atomic64_inc(&alg->stats.akcipher.verify_cnt);
  901. crypto_alg_put(alg);
  902. }
  903. EXPORT_SYMBOL_GPL(crypto_stats_akcipher_verify);
  904. void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg)
  905. {
  906. if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
  907. atomic64_inc(&alg->stats.compress.err_cnt);
  908. } else {
  909. atomic64_inc(&alg->stats.compress.compress_cnt);
  910. atomic64_add(slen, &alg->stats.compress.compress_tlen);
  911. }
  912. crypto_alg_put(alg);
  913. }
  914. EXPORT_SYMBOL_GPL(crypto_stats_compress);
  915. void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg)
  916. {
  917. if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
  918. atomic64_inc(&alg->stats.compress.err_cnt);
  919. } else {
  920. atomic64_inc(&alg->stats.compress.decompress_cnt);
  921. atomic64_add(slen, &alg->stats.compress.decompress_tlen);
  922. }
  923. crypto_alg_put(alg);
  924. }
  925. EXPORT_SYMBOL_GPL(crypto_stats_decompress);
  926. void crypto_stats_ahash_update(unsigned int nbytes, int ret,
  927. struct crypto_alg *alg)
  928. {
  929. if (ret && ret != -EINPROGRESS && ret != -EBUSY)
  930. atomic64_inc(&alg->stats.hash.err_cnt);
  931. else
  932. atomic64_add(nbytes, &alg->stats.hash.hash_tlen);
  933. crypto_alg_put(alg);
  934. }
  935. EXPORT_SYMBOL_GPL(crypto_stats_ahash_update);
  936. void crypto_stats_ahash_final(unsigned int nbytes, int ret,
  937. struct crypto_alg *alg)
  938. {
  939. if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
  940. atomic64_inc(&alg->stats.hash.err_cnt);
  941. } else {
  942. atomic64_inc(&alg->stats.hash.hash_cnt);
  943. atomic64_add(nbytes, &alg->stats.hash.hash_tlen);
  944. }
  945. crypto_alg_put(alg);
  946. }
  947. EXPORT_SYMBOL_GPL(crypto_stats_ahash_final);
  948. void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret)
  949. {
  950. if (ret)
  951. atomic64_inc(&alg->stats.kpp.err_cnt);
  952. else
  953. atomic64_inc(&alg->stats.kpp.setsecret_cnt);
  954. crypto_alg_put(alg);
  955. }
  956. EXPORT_SYMBOL_GPL(crypto_stats_kpp_set_secret);
  957. void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret)
  958. {
  959. if (ret)
  960. atomic64_inc(&alg->stats.kpp.err_cnt);
  961. else
  962. atomic64_inc(&alg->stats.kpp.generate_public_key_cnt);
  963. crypto_alg_put(alg);
  964. }
  965. EXPORT_SYMBOL_GPL(crypto_stats_kpp_generate_public_key);
  966. void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret)
  967. {
  968. if (ret)
  969. atomic64_inc(&alg->stats.kpp.err_cnt);
  970. else
  971. atomic64_inc(&alg->stats.kpp.compute_shared_secret_cnt);
  972. crypto_alg_put(alg);
  973. }
  974. EXPORT_SYMBOL_GPL(crypto_stats_kpp_compute_shared_secret);
  975. void crypto_stats_rng_seed(struct crypto_alg *alg, int ret)
  976. {
  977. if (ret && ret != -EINPROGRESS && ret != -EBUSY)
  978. atomic64_inc(&alg->stats.rng.err_cnt);
  979. else
  980. atomic64_inc(&alg->stats.rng.seed_cnt);
  981. crypto_alg_put(alg);
  982. }
  983. EXPORT_SYMBOL_GPL(crypto_stats_rng_seed);
  984. void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen,
  985. int ret)
  986. {
  987. if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
  988. atomic64_inc(&alg->stats.rng.err_cnt);
  989. } else {
  990. atomic64_inc(&alg->stats.rng.generate_cnt);
  991. atomic64_add(dlen, &alg->stats.rng.generate_tlen);
  992. }
  993. crypto_alg_put(alg);
  994. }
  995. EXPORT_SYMBOL_GPL(crypto_stats_rng_generate);
  996. void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret,
  997. struct crypto_alg *alg)
  998. {
  999. if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
  1000. atomic64_inc(&alg->stats.cipher.err_cnt);
  1001. } else {
  1002. atomic64_inc(&alg->stats.cipher.encrypt_cnt);
  1003. atomic64_add(cryptlen, &alg->stats.cipher.encrypt_tlen);
  1004. }
  1005. crypto_alg_put(alg);
  1006. }
  1007. EXPORT_SYMBOL_GPL(crypto_stats_skcipher_encrypt);
  1008. void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret,
  1009. struct crypto_alg *alg)
  1010. {
  1011. if (ret && ret != -EINPROGRESS && ret != -EBUSY) {
  1012. atomic64_inc(&alg->stats.cipher.err_cnt);
  1013. } else {
  1014. atomic64_inc(&alg->stats.cipher.decrypt_cnt);
  1015. atomic64_add(cryptlen, &alg->stats.cipher.decrypt_tlen);
  1016. }
  1017. crypto_alg_put(alg);
  1018. }
  1019. EXPORT_SYMBOL_GPL(crypto_stats_skcipher_decrypt);
  1020. #endif
  1021. static void __init crypto_start_tests(void)
  1022. {
  1023. if (IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS))
  1024. return;
  1025. for (;;) {
  1026. struct crypto_larval *larval = NULL;
  1027. struct crypto_alg *q;
  1028. down_write(&crypto_alg_sem);
  1029. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  1030. struct crypto_larval *l;
  1031. if (!crypto_is_larval(q))
  1032. continue;
  1033. l = (void *)q;
  1034. if (!crypto_is_test_larval(l))
  1035. continue;
  1036. if (l->test_started)
  1037. continue;
  1038. l->test_started = true;
  1039. larval = l;
  1040. break;
  1041. }
  1042. up_write(&crypto_alg_sem);
  1043. if (!larval)
  1044. break;
  1045. crypto_wait_for_test(larval);
  1046. }
  1047. set_crypto_boot_test_finished();
  1048. }
  1049. static int __init crypto_algapi_init(void)
  1050. {
  1051. crypto_init_proc();
  1052. crypto_start_tests();
  1053. return 0;
  1054. }
  1055. static void __exit crypto_algapi_exit(void)
  1056. {
  1057. crypto_exit_proc();
  1058. }
  1059. /*
  1060. * We run this at late_initcall so that all the built-in algorithms
  1061. * have had a chance to register themselves first.
  1062. */
  1063. late_initcall(crypto_algapi_init);
  1064. module_exit(crypto_algapi_exit);
  1065. MODULE_LICENSE("GPL");
  1066. MODULE_DESCRIPTION("Cryptographic algorithms API");
  1067. MODULE_SOFTDEP("pre: cryptomgr");