algapi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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 <herbert@gondor.apana.org.au>
  6. */
  7. #include <crypto/algapi.h>
  8. #include <linux/err.h>
  9. #include <linux/errno.h>
  10. #include <linux/fips.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/list.h>
  14. #include <linux/module.h>
  15. #include <linux/rtnetlink.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include <linux/workqueue.h>
  19. #include "internal.h"
  20. static LIST_HEAD(crypto_template_list);
  21. static inline void crypto_check_module_sig(struct module *mod)
  22. {
  23. if (fips_enabled && mod && !module_sig_ok(mod))
  24. panic("Module %s signature verification failed in FIPS mode\n",
  25. module_name(mod));
  26. }
  27. static int crypto_check_alg(struct crypto_alg *alg)
  28. {
  29. crypto_check_module_sig(alg->cra_module);
  30. if (!alg->cra_name[0] || !alg->cra_driver_name[0])
  31. return -EINVAL;
  32. if (alg->cra_alignmask & (alg->cra_alignmask + 1))
  33. return -EINVAL;
  34. /* General maximums for all algs. */
  35. if (alg->cra_alignmask > MAX_ALGAPI_ALIGNMASK)
  36. return -EINVAL;
  37. if (alg->cra_blocksize > MAX_ALGAPI_BLOCKSIZE)
  38. return -EINVAL;
  39. /* Lower maximums for specific alg types. */
  40. if (!alg->cra_type && (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  41. CRYPTO_ALG_TYPE_CIPHER) {
  42. if (alg->cra_alignmask > MAX_CIPHER_ALIGNMASK)
  43. return -EINVAL;
  44. if (alg->cra_blocksize > MAX_CIPHER_BLOCKSIZE)
  45. return -EINVAL;
  46. }
  47. if (alg->cra_priority < 0)
  48. return -EINVAL;
  49. refcount_set(&alg->cra_refcnt, 1);
  50. return 0;
  51. }
  52. static void crypto_free_instance(struct crypto_instance *inst)
  53. {
  54. inst->alg.cra_type->free(inst);
  55. }
  56. static void crypto_destroy_instance_workfn(struct work_struct *w)
  57. {
  58. struct crypto_template *tmpl = container_of(w, struct crypto_template,
  59. free_work);
  60. struct crypto_instance *inst;
  61. struct hlist_node *n;
  62. HLIST_HEAD(list);
  63. down_write(&crypto_alg_sem);
  64. hlist_for_each_entry_safe(inst, n, &tmpl->dead, list) {
  65. if (refcount_read(&inst->alg.cra_refcnt) != -1)
  66. continue;
  67. hlist_del(&inst->list);
  68. hlist_add_head(&inst->list, &list);
  69. }
  70. up_write(&crypto_alg_sem);
  71. hlist_for_each_entry_safe(inst, n, &list, list)
  72. crypto_free_instance(inst);
  73. }
  74. static void crypto_destroy_instance(struct crypto_alg *alg)
  75. {
  76. struct crypto_instance *inst = container_of(alg,
  77. struct crypto_instance,
  78. alg);
  79. struct crypto_template *tmpl = inst->tmpl;
  80. refcount_set(&alg->cra_refcnt, -1);
  81. schedule_work(&tmpl->free_work);
  82. }
  83. /*
  84. * This function adds a spawn to the list secondary_spawns which
  85. * will be used at the end of crypto_remove_spawns to unregister
  86. * instances, unless the spawn happens to be one that is depended
  87. * on by the new algorithm (nalg in crypto_remove_spawns).
  88. *
  89. * This function is also responsible for resurrecting any algorithms
  90. * in the dependency chain of nalg by unsetting n->dead.
  91. */
  92. static struct list_head *crypto_more_spawns(struct crypto_alg *alg,
  93. struct list_head *stack,
  94. struct list_head *top,
  95. struct list_head *secondary_spawns)
  96. {
  97. struct crypto_spawn *spawn, *n;
  98. spawn = list_first_entry_or_null(stack, struct crypto_spawn, list);
  99. if (!spawn)
  100. return NULL;
  101. n = list_prev_entry(spawn, list);
  102. list_move(&spawn->list, secondary_spawns);
  103. if (list_is_last(&n->list, stack))
  104. return top;
  105. n = list_next_entry(n, list);
  106. if (!spawn->dead)
  107. n->dead = false;
  108. return &n->inst->alg.cra_users;
  109. }
  110. static void crypto_remove_instance(struct crypto_instance *inst,
  111. struct list_head *list)
  112. {
  113. struct crypto_template *tmpl = inst->tmpl;
  114. if (crypto_is_dead(&inst->alg))
  115. return;
  116. inst->alg.cra_flags |= CRYPTO_ALG_DEAD;
  117. if (!tmpl)
  118. return;
  119. list_del_init(&inst->alg.cra_list);
  120. hlist_del(&inst->list);
  121. hlist_add_head(&inst->list, &tmpl->dead);
  122. BUG_ON(!list_empty(&inst->alg.cra_users));
  123. crypto_alg_put(&inst->alg);
  124. }
  125. /*
  126. * Given an algorithm alg, remove all algorithms that depend on it
  127. * through spawns. If nalg is not null, then exempt any algorithms
  128. * that is depended on by nalg. This is useful when nalg itself
  129. * depends on alg.
  130. */
  131. void crypto_remove_spawns(struct crypto_alg *alg, struct list_head *list,
  132. struct crypto_alg *nalg)
  133. {
  134. u32 new_type = (nalg ?: alg)->cra_flags;
  135. struct crypto_spawn *spawn, *n;
  136. LIST_HEAD(secondary_spawns);
  137. struct list_head *spawns;
  138. LIST_HEAD(stack);
  139. LIST_HEAD(top);
  140. spawns = &alg->cra_users;
  141. list_for_each_entry_safe(spawn, n, spawns, list) {
  142. if ((spawn->alg->cra_flags ^ new_type) & spawn->mask)
  143. continue;
  144. list_move(&spawn->list, &top);
  145. }
  146. /*
  147. * Perform a depth-first walk starting from alg through
  148. * the cra_users tree. The list stack records the path
  149. * from alg to the current spawn.
  150. */
  151. spawns = &top;
  152. do {
  153. while (!list_empty(spawns)) {
  154. struct crypto_instance *inst;
  155. spawn = list_first_entry(spawns, struct crypto_spawn,
  156. list);
  157. inst = spawn->inst;
  158. list_move(&spawn->list, &stack);
  159. spawn->dead = !spawn->registered || &inst->alg != nalg;
  160. if (!spawn->registered)
  161. break;
  162. BUG_ON(&inst->alg == alg);
  163. if (&inst->alg == nalg)
  164. break;
  165. spawns = &inst->alg.cra_users;
  166. /*
  167. * Even if spawn->registered is true, the
  168. * instance itself may still be unregistered.
  169. * This is because it may have failed during
  170. * registration. Therefore we still need to
  171. * make the following test.
  172. *
  173. * We may encounter an unregistered instance here, since
  174. * an instance's spawns are set up prior to the instance
  175. * being registered. An unregistered instance will have
  176. * NULL ->cra_users.next, since ->cra_users isn't
  177. * properly initialized until registration. But an
  178. * unregistered instance cannot have any users, so treat
  179. * it the same as ->cra_users being empty.
  180. */
  181. if (spawns->next == NULL)
  182. break;
  183. }
  184. } while ((spawns = crypto_more_spawns(alg, &stack, &top,
  185. &secondary_spawns)));
  186. /*
  187. * Remove all instances that are marked as dead. Also
  188. * complete the resurrection of the others by moving them
  189. * back to the cra_users list.
  190. */
  191. list_for_each_entry_safe(spawn, n, &secondary_spawns, list) {
  192. if (!spawn->dead)
  193. list_move(&spawn->list, &spawn->alg->cra_users);
  194. else if (spawn->registered)
  195. crypto_remove_instance(spawn->inst, list);
  196. }
  197. }
  198. EXPORT_SYMBOL_GPL(crypto_remove_spawns);
  199. static void crypto_alg_finish_registration(struct crypto_alg *alg,
  200. struct list_head *algs_to_put)
  201. __must_hold(&crypto_alg_sem)
  202. {
  203. struct crypto_alg *q;
  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. continue;
  211. if (strcmp(alg->cra_name, q->cra_name))
  212. continue;
  213. if (strcmp(alg->cra_driver_name, q->cra_driver_name) &&
  214. q->cra_priority > alg->cra_priority)
  215. continue;
  216. crypto_remove_spawns(q, algs_to_put, alg);
  217. }
  218. crypto_notify(CRYPTO_MSG_ALG_LOADED, alg);
  219. }
  220. static struct crypto_larval *crypto_alloc_test_larval(struct crypto_alg *alg)
  221. {
  222. struct crypto_larval *larval;
  223. if (!IS_ENABLED(CONFIG_CRYPTO_SELFTESTS) ||
  224. (alg->cra_flags & CRYPTO_ALG_INTERNAL))
  225. return NULL; /* No self-test needed */
  226. larval = crypto_larval_alloc(alg->cra_name,
  227. alg->cra_flags | CRYPTO_ALG_TESTED, 0);
  228. if (IS_ERR(larval))
  229. return larval;
  230. larval->adult = crypto_mod_get(alg);
  231. if (!larval->adult) {
  232. kfree(larval);
  233. return ERR_PTR(-ENOENT);
  234. }
  235. refcount_set(&larval->alg.cra_refcnt, 1);
  236. memcpy(larval->alg.cra_driver_name, alg->cra_driver_name,
  237. CRYPTO_MAX_ALG_NAME);
  238. larval->alg.cra_priority = alg->cra_priority;
  239. return larval;
  240. }
  241. static struct crypto_larval *
  242. __crypto_register_alg(struct crypto_alg *alg, struct list_head *algs_to_put)
  243. __must_hold(&crypto_alg_sem)
  244. {
  245. struct crypto_alg *q;
  246. struct crypto_larval *larval;
  247. int ret = -EAGAIN;
  248. if (crypto_is_dead(alg))
  249. goto err;
  250. INIT_LIST_HEAD(&alg->cra_users);
  251. ret = -EEXIST;
  252. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  253. if (q == alg)
  254. goto err;
  255. if (crypto_is_moribund(q))
  256. continue;
  257. if (crypto_is_larval(q)) {
  258. if (!strcmp(alg->cra_driver_name, q->cra_driver_name))
  259. goto err;
  260. continue;
  261. }
  262. if (!strcmp(q->cra_driver_name, alg->cra_name) ||
  263. !strcmp(q->cra_driver_name, alg->cra_driver_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. if (larval) {
  272. /* No cheating! */
  273. alg->cra_flags &= ~CRYPTO_ALG_TESTED;
  274. list_add(&larval->alg.cra_list, &crypto_alg_list);
  275. } else {
  276. alg->cra_flags |= CRYPTO_ALG_TESTED;
  277. crypto_alg_finish_registration(alg, algs_to_put);
  278. }
  279. out:
  280. return larval;
  281. err:
  282. larval = ERR_PTR(ret);
  283. goto out;
  284. }
  285. void crypto_alg_tested(const char *name, int err)
  286. {
  287. struct crypto_larval *test;
  288. struct crypto_alg *alg;
  289. struct crypto_alg *q;
  290. LIST_HEAD(list);
  291. down_write(&crypto_alg_sem);
  292. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  293. if (crypto_is_moribund(q) || !crypto_is_larval(q))
  294. continue;
  295. test = (struct crypto_larval *)q;
  296. if (!strcmp(q->cra_driver_name, name))
  297. goto found;
  298. }
  299. pr_err("alg: Unexpected test result for %s: %d\n", name, err);
  300. up_write(&crypto_alg_sem);
  301. return;
  302. found:
  303. q->cra_flags |= CRYPTO_ALG_DEAD;
  304. alg = test->adult;
  305. if (crypto_is_dead(alg))
  306. goto complete;
  307. if (err == -ECANCELED)
  308. alg->cra_flags |= CRYPTO_ALG_FIPS_INTERNAL;
  309. else if (err)
  310. goto complete;
  311. else
  312. alg->cra_flags &= ~CRYPTO_ALG_FIPS_INTERNAL;
  313. alg->cra_flags |= CRYPTO_ALG_TESTED;
  314. crypto_alg_finish_registration(alg, &list);
  315. complete:
  316. list_del_init(&test->alg.cra_list);
  317. complete_all(&test->completion);
  318. up_write(&crypto_alg_sem);
  319. crypto_alg_put(&test->alg);
  320. crypto_remove_final(&list);
  321. }
  322. EXPORT_SYMBOL_GPL(crypto_alg_tested);
  323. void crypto_remove_final(struct list_head *list)
  324. {
  325. struct crypto_alg *alg;
  326. struct crypto_alg *n;
  327. list_for_each_entry_safe(alg, n, list, cra_list) {
  328. list_del_init(&alg->cra_list);
  329. crypto_alg_put(alg);
  330. }
  331. }
  332. EXPORT_SYMBOL_GPL(crypto_remove_final);
  333. static void crypto_free_alg(struct crypto_alg *alg)
  334. {
  335. unsigned int algsize = alg->cra_type->algsize;
  336. u8 *p = (u8 *)alg - algsize;
  337. crypto_destroy_alg(alg);
  338. kfree(p);
  339. }
  340. int crypto_register_alg(struct crypto_alg *alg)
  341. {
  342. struct crypto_larval *larval;
  343. bool test_started = false;
  344. LIST_HEAD(algs_to_put);
  345. int err;
  346. alg->cra_flags &= ~CRYPTO_ALG_DEAD;
  347. err = crypto_check_alg(alg);
  348. if (err)
  349. return err;
  350. if (alg->cra_flags & CRYPTO_ALG_DUP_FIRST &&
  351. !WARN_ON_ONCE(alg->cra_destroy)) {
  352. unsigned int algsize = alg->cra_type->algsize;
  353. u8 *p = (u8 *)alg - algsize;
  354. p = kmemdup(p, algsize + sizeof(*alg), GFP_KERNEL);
  355. if (!p)
  356. return -ENOMEM;
  357. alg = (void *)(p + algsize);
  358. alg->cra_destroy = crypto_free_alg;
  359. }
  360. down_write(&crypto_alg_sem);
  361. larval = __crypto_register_alg(alg, &algs_to_put);
  362. if (!IS_ERR_OR_NULL(larval)) {
  363. test_started = crypto_boot_test_finished();
  364. larval->test_started = test_started;
  365. }
  366. up_write(&crypto_alg_sem);
  367. if (IS_ERR(larval)) {
  368. crypto_alg_put(alg);
  369. return PTR_ERR(larval);
  370. }
  371. if (test_started)
  372. crypto_schedule_test(larval);
  373. else
  374. crypto_remove_final(&algs_to_put);
  375. return 0;
  376. }
  377. EXPORT_SYMBOL_GPL(crypto_register_alg);
  378. static int crypto_remove_alg(struct crypto_alg *alg, struct list_head *list)
  379. {
  380. if (unlikely(list_empty(&alg->cra_list)))
  381. return -ENOENT;
  382. alg->cra_flags |= CRYPTO_ALG_DEAD;
  383. list_del_init(&alg->cra_list);
  384. crypto_remove_spawns(alg, list, NULL);
  385. return 0;
  386. }
  387. void crypto_unregister_alg(struct crypto_alg *alg)
  388. {
  389. int ret;
  390. LIST_HEAD(list);
  391. down_write(&crypto_alg_sem);
  392. ret = crypto_remove_alg(alg, &list);
  393. up_write(&crypto_alg_sem);
  394. if (WARN(ret, "Algorithm %s is not registered", alg->cra_driver_name))
  395. return;
  396. WARN_ON(!alg->cra_destroy && refcount_read(&alg->cra_refcnt) != 1);
  397. list_add(&alg->cra_list, &list);
  398. crypto_remove_final(&list);
  399. }
  400. EXPORT_SYMBOL_GPL(crypto_unregister_alg);
  401. int crypto_register_algs(struct crypto_alg *algs, int count)
  402. {
  403. int i, ret;
  404. for (i = 0; i < count; i++) {
  405. ret = crypto_register_alg(&algs[i]);
  406. if (ret) {
  407. crypto_unregister_algs(algs, i);
  408. return ret;
  409. }
  410. }
  411. return 0;
  412. }
  413. EXPORT_SYMBOL_GPL(crypto_register_algs);
  414. void crypto_unregister_algs(struct crypto_alg *algs, int count)
  415. {
  416. int i;
  417. for (i = count - 1; i >= 0; --i)
  418. crypto_unregister_alg(&algs[i]);
  419. }
  420. EXPORT_SYMBOL_GPL(crypto_unregister_algs);
  421. int crypto_register_template(struct crypto_template *tmpl)
  422. {
  423. struct crypto_template *q;
  424. int err = -EEXIST;
  425. INIT_WORK(&tmpl->free_work, crypto_destroy_instance_workfn);
  426. down_write(&crypto_alg_sem);
  427. crypto_check_module_sig(tmpl->module);
  428. list_for_each_entry(q, &crypto_template_list, list) {
  429. if (q == tmpl)
  430. goto out;
  431. }
  432. list_add(&tmpl->list, &crypto_template_list);
  433. err = 0;
  434. out:
  435. up_write(&crypto_alg_sem);
  436. return err;
  437. }
  438. EXPORT_SYMBOL_GPL(crypto_register_template);
  439. int crypto_register_templates(struct crypto_template *tmpls, int count)
  440. {
  441. int i, err;
  442. for (i = 0; i < count; i++) {
  443. err = crypto_register_template(&tmpls[i]);
  444. if (err)
  445. goto out;
  446. }
  447. return 0;
  448. out:
  449. for (--i; i >= 0; --i)
  450. crypto_unregister_template(&tmpls[i]);
  451. return err;
  452. }
  453. EXPORT_SYMBOL_GPL(crypto_register_templates);
  454. void crypto_unregister_template(struct crypto_template *tmpl)
  455. {
  456. struct crypto_instance *inst;
  457. struct hlist_node *n;
  458. struct hlist_head *list;
  459. LIST_HEAD(users);
  460. down_write(&crypto_alg_sem);
  461. BUG_ON(list_empty(&tmpl->list));
  462. list_del_init(&tmpl->list);
  463. list = &tmpl->instances;
  464. hlist_for_each_entry(inst, list, list) {
  465. int err = crypto_remove_alg(&inst->alg, &users);
  466. BUG_ON(err);
  467. }
  468. up_write(&crypto_alg_sem);
  469. hlist_for_each_entry_safe(inst, n, list, list) {
  470. BUG_ON(refcount_read(&inst->alg.cra_refcnt) != 1);
  471. crypto_free_instance(inst);
  472. }
  473. crypto_remove_final(&users);
  474. flush_work(&tmpl->free_work);
  475. }
  476. EXPORT_SYMBOL_GPL(crypto_unregister_template);
  477. void crypto_unregister_templates(struct crypto_template *tmpls, int count)
  478. {
  479. int i;
  480. for (i = count - 1; i >= 0; --i)
  481. crypto_unregister_template(&tmpls[i]);
  482. }
  483. EXPORT_SYMBOL_GPL(crypto_unregister_templates);
  484. static struct crypto_template *__crypto_lookup_template(const char *name)
  485. {
  486. struct crypto_template *q, *tmpl = NULL;
  487. down_read(&crypto_alg_sem);
  488. list_for_each_entry(q, &crypto_template_list, list) {
  489. if (strcmp(q->name, name))
  490. continue;
  491. if (unlikely(!crypto_tmpl_get(q)))
  492. continue;
  493. tmpl = q;
  494. break;
  495. }
  496. up_read(&crypto_alg_sem);
  497. return tmpl;
  498. }
  499. struct crypto_template *crypto_lookup_template(const char *name)
  500. {
  501. return try_then_request_module(__crypto_lookup_template(name),
  502. "crypto-%s", name);
  503. }
  504. EXPORT_SYMBOL_GPL(crypto_lookup_template);
  505. int crypto_register_instance(struct crypto_template *tmpl,
  506. struct crypto_instance *inst)
  507. {
  508. struct crypto_larval *larval;
  509. struct crypto_spawn *spawn;
  510. u32 fips_internal = 0;
  511. LIST_HEAD(algs_to_put);
  512. int err;
  513. err = crypto_check_alg(&inst->alg);
  514. if (err)
  515. return err;
  516. inst->alg.cra_module = tmpl->module;
  517. inst->alg.cra_flags |= CRYPTO_ALG_INSTANCE;
  518. inst->alg.cra_destroy = crypto_destroy_instance;
  519. down_write(&crypto_alg_sem);
  520. larval = ERR_PTR(-EAGAIN);
  521. for (spawn = inst->spawns; spawn;) {
  522. struct crypto_spawn *next;
  523. if (spawn->dead)
  524. goto unlock;
  525. next = spawn->next;
  526. spawn->inst = inst;
  527. spawn->registered = true;
  528. fips_internal |= spawn->alg->cra_flags;
  529. crypto_mod_put(spawn->alg);
  530. spawn = next;
  531. }
  532. inst->alg.cra_flags |= (fips_internal & CRYPTO_ALG_FIPS_INTERNAL);
  533. larval = __crypto_register_alg(&inst->alg, &algs_to_put);
  534. if (IS_ERR(larval))
  535. goto unlock;
  536. else if (larval)
  537. larval->test_started = true;
  538. hlist_add_head(&inst->list, &tmpl->instances);
  539. inst->tmpl = tmpl;
  540. unlock:
  541. up_write(&crypto_alg_sem);
  542. if (IS_ERR(larval))
  543. return PTR_ERR(larval);
  544. if (larval)
  545. crypto_schedule_test(larval);
  546. else
  547. crypto_remove_final(&algs_to_put);
  548. return 0;
  549. }
  550. EXPORT_SYMBOL_GPL(crypto_register_instance);
  551. void crypto_unregister_instance(struct crypto_instance *inst)
  552. {
  553. LIST_HEAD(list);
  554. down_write(&crypto_alg_sem);
  555. crypto_remove_spawns(&inst->alg, &list, NULL);
  556. crypto_remove_instance(inst, &list);
  557. up_write(&crypto_alg_sem);
  558. crypto_remove_final(&list);
  559. }
  560. EXPORT_SYMBOL_GPL(crypto_unregister_instance);
  561. int crypto_grab_spawn(struct crypto_spawn *spawn, struct crypto_instance *inst,
  562. const char *name, u32 type, u32 mask)
  563. {
  564. struct crypto_alg *alg;
  565. int err = -EAGAIN;
  566. if (WARN_ON_ONCE(inst == NULL))
  567. return -EINVAL;
  568. /* Allow the result of crypto_attr_alg_name() to be passed directly */
  569. if (IS_ERR(name))
  570. return PTR_ERR(name);
  571. alg = crypto_find_alg(name, spawn->frontend,
  572. type | CRYPTO_ALG_FIPS_INTERNAL, mask);
  573. if (IS_ERR(alg))
  574. return PTR_ERR(alg);
  575. down_write(&crypto_alg_sem);
  576. if (!crypto_is_moribund(alg)) {
  577. list_add(&spawn->list, &alg->cra_users);
  578. spawn->alg = alg;
  579. spawn->mask = mask;
  580. spawn->next = inst->spawns;
  581. inst->spawns = spawn;
  582. inst->alg.cra_flags |=
  583. (alg->cra_flags & CRYPTO_ALG_INHERITED_FLAGS);
  584. err = 0;
  585. }
  586. up_write(&crypto_alg_sem);
  587. if (err)
  588. crypto_mod_put(alg);
  589. return err;
  590. }
  591. EXPORT_SYMBOL_GPL(crypto_grab_spawn);
  592. void crypto_drop_spawn(struct crypto_spawn *spawn)
  593. {
  594. if (!spawn->alg) /* not yet initialized? */
  595. return;
  596. down_write(&crypto_alg_sem);
  597. if (!spawn->dead)
  598. list_del(&spawn->list);
  599. up_write(&crypto_alg_sem);
  600. if (!spawn->registered)
  601. crypto_mod_put(spawn->alg);
  602. }
  603. EXPORT_SYMBOL_GPL(crypto_drop_spawn);
  604. static struct crypto_alg *crypto_spawn_alg(struct crypto_spawn *spawn)
  605. {
  606. struct crypto_alg *alg = ERR_PTR(-EAGAIN);
  607. struct crypto_alg *target;
  608. bool shoot = false;
  609. down_read(&crypto_alg_sem);
  610. if (!spawn->dead) {
  611. alg = spawn->alg;
  612. if (!crypto_mod_get(alg)) {
  613. target = crypto_alg_get(alg);
  614. shoot = true;
  615. alg = ERR_PTR(-EAGAIN);
  616. }
  617. }
  618. up_read(&crypto_alg_sem);
  619. if (shoot) {
  620. crypto_shoot_alg(target);
  621. crypto_alg_put(target);
  622. }
  623. return alg;
  624. }
  625. struct crypto_tfm *crypto_spawn_tfm(struct crypto_spawn *spawn, u32 type,
  626. u32 mask)
  627. {
  628. struct crypto_alg *alg;
  629. struct crypto_tfm *tfm;
  630. alg = crypto_spawn_alg(spawn);
  631. if (IS_ERR(alg))
  632. return ERR_CAST(alg);
  633. tfm = ERR_PTR(-EINVAL);
  634. if (unlikely((alg->cra_flags ^ type) & mask))
  635. goto out_put_alg;
  636. tfm = __crypto_alloc_tfm(alg, type, mask);
  637. if (IS_ERR(tfm))
  638. goto out_put_alg;
  639. return tfm;
  640. out_put_alg:
  641. crypto_mod_put(alg);
  642. return tfm;
  643. }
  644. EXPORT_SYMBOL_GPL(crypto_spawn_tfm);
  645. void *crypto_spawn_tfm2(struct crypto_spawn *spawn)
  646. {
  647. struct crypto_alg *alg;
  648. struct crypto_tfm *tfm;
  649. alg = crypto_spawn_alg(spawn);
  650. if (IS_ERR(alg))
  651. return ERR_CAST(alg);
  652. tfm = crypto_create_tfm(alg, spawn->frontend);
  653. if (IS_ERR(tfm))
  654. goto out_put_alg;
  655. return tfm;
  656. out_put_alg:
  657. crypto_mod_put(alg);
  658. return tfm;
  659. }
  660. EXPORT_SYMBOL_GPL(crypto_spawn_tfm2);
  661. int crypto_register_notifier(struct notifier_block *nb)
  662. {
  663. return blocking_notifier_chain_register(&crypto_chain, nb);
  664. }
  665. EXPORT_SYMBOL_GPL(crypto_register_notifier);
  666. int crypto_unregister_notifier(struct notifier_block *nb)
  667. {
  668. return blocking_notifier_chain_unregister(&crypto_chain, nb);
  669. }
  670. EXPORT_SYMBOL_GPL(crypto_unregister_notifier);
  671. struct crypto_attr_type *crypto_get_attr_type(struct rtattr **tb)
  672. {
  673. struct rtattr *rta = tb[0];
  674. struct crypto_attr_type *algt;
  675. if (!rta)
  676. return ERR_PTR(-ENOENT);
  677. if (RTA_PAYLOAD(rta) < sizeof(*algt))
  678. return ERR_PTR(-EINVAL);
  679. if (rta->rta_type != CRYPTOA_TYPE)
  680. return ERR_PTR(-EINVAL);
  681. algt = RTA_DATA(rta);
  682. return algt;
  683. }
  684. EXPORT_SYMBOL_GPL(crypto_get_attr_type);
  685. /**
  686. * crypto_check_attr_type() - check algorithm type and compute inherited mask
  687. * @tb: the template parameters
  688. * @type: the algorithm type the template would be instantiated as
  689. * @mask_ret: (output) the mask that should be passed to crypto_grab_*()
  690. * to restrict the flags of any inner algorithms
  691. *
  692. * Validate that the algorithm type the user requested is compatible with the
  693. * one the template would actually be instantiated as. E.g., if the user is
  694. * doing crypto_alloc_shash("cbc(aes)", ...), this would return an error because
  695. * the "cbc" template creates an "skcipher" algorithm, not an "shash" algorithm.
  696. *
  697. * Also compute the mask to use to restrict the flags of any inner algorithms.
  698. *
  699. * Return: 0 on success; -errno on failure
  700. */
  701. int crypto_check_attr_type(struct rtattr **tb, u32 type, u32 *mask_ret)
  702. {
  703. struct crypto_attr_type *algt;
  704. algt = crypto_get_attr_type(tb);
  705. if (IS_ERR(algt))
  706. return PTR_ERR(algt);
  707. if ((algt->type ^ type) & algt->mask)
  708. return -EINVAL;
  709. *mask_ret = crypto_algt_inherited_mask(algt);
  710. return 0;
  711. }
  712. EXPORT_SYMBOL_GPL(crypto_check_attr_type);
  713. const char *crypto_attr_alg_name(struct rtattr *rta)
  714. {
  715. struct crypto_attr_alg *alga;
  716. if (!rta)
  717. return ERR_PTR(-ENOENT);
  718. if (RTA_PAYLOAD(rta) < sizeof(*alga))
  719. return ERR_PTR(-EINVAL);
  720. if (rta->rta_type != CRYPTOA_ALG)
  721. return ERR_PTR(-EINVAL);
  722. alga = RTA_DATA(rta);
  723. alga->name[CRYPTO_MAX_ALG_NAME - 1] = 0;
  724. return alga->name;
  725. }
  726. EXPORT_SYMBOL_GPL(crypto_attr_alg_name);
  727. int __crypto_inst_setname(struct crypto_instance *inst, const char *name,
  728. const char *driver, struct crypto_alg *alg)
  729. {
  730. if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME, "%s(%s)", name,
  731. alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
  732. return -ENAMETOOLONG;
  733. if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  734. driver, alg->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  735. return -ENAMETOOLONG;
  736. return 0;
  737. }
  738. EXPORT_SYMBOL_GPL(__crypto_inst_setname);
  739. void crypto_init_queue(struct crypto_queue *queue, unsigned int max_qlen)
  740. {
  741. INIT_LIST_HEAD(&queue->list);
  742. queue->backlog = &queue->list;
  743. queue->qlen = 0;
  744. queue->max_qlen = max_qlen;
  745. }
  746. EXPORT_SYMBOL_GPL(crypto_init_queue);
  747. int crypto_enqueue_request(struct crypto_queue *queue,
  748. struct crypto_async_request *request)
  749. {
  750. int err = -EINPROGRESS;
  751. if (unlikely(queue->qlen >= queue->max_qlen)) {
  752. if (!(request->flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
  753. err = -ENOSPC;
  754. goto out;
  755. }
  756. err = -EBUSY;
  757. if (queue->backlog == &queue->list)
  758. queue->backlog = &request->list;
  759. }
  760. queue->qlen++;
  761. list_add_tail(&request->list, &queue->list);
  762. out:
  763. return err;
  764. }
  765. EXPORT_SYMBOL_GPL(crypto_enqueue_request);
  766. void crypto_enqueue_request_head(struct crypto_queue *queue,
  767. struct crypto_async_request *request)
  768. {
  769. if (unlikely(queue->qlen >= queue->max_qlen))
  770. queue->backlog = queue->backlog->prev;
  771. queue->qlen++;
  772. list_add(&request->list, &queue->list);
  773. }
  774. EXPORT_SYMBOL_GPL(crypto_enqueue_request_head);
  775. struct crypto_async_request *crypto_dequeue_request(struct crypto_queue *queue)
  776. {
  777. struct list_head *request;
  778. if (unlikely(!queue->qlen))
  779. return NULL;
  780. queue->qlen--;
  781. if (queue->backlog != &queue->list)
  782. queue->backlog = queue->backlog->next;
  783. request = queue->list.next;
  784. list_del_init(request);
  785. return list_entry(request, struct crypto_async_request, list);
  786. }
  787. EXPORT_SYMBOL_GPL(crypto_dequeue_request);
  788. static inline void crypto_inc_byte(u8 *a, unsigned int size)
  789. {
  790. u8 *b = (a + size);
  791. u8 c;
  792. for (; size; size--) {
  793. c = *--b + 1;
  794. *b = c;
  795. if (c)
  796. break;
  797. }
  798. }
  799. void crypto_inc(u8 *a, unsigned int size)
  800. {
  801. __be32 *b = (__be32 *)(a + size);
  802. u32 c;
  803. if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) ||
  804. IS_ALIGNED((unsigned long)b, __alignof__(*b)))
  805. for (; size >= 4; size -= 4) {
  806. c = be32_to_cpu(*--b) + 1;
  807. *b = cpu_to_be32(c);
  808. if (likely(c))
  809. return;
  810. }
  811. crypto_inc_byte(a, size);
  812. }
  813. EXPORT_SYMBOL_GPL(crypto_inc);
  814. unsigned int crypto_alg_extsize(struct crypto_alg *alg)
  815. {
  816. return alg->cra_ctxsize +
  817. (alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1));
  818. }
  819. EXPORT_SYMBOL_GPL(crypto_alg_extsize);
  820. int crypto_type_has_alg(const char *name, const struct crypto_type *frontend,
  821. u32 type, u32 mask)
  822. {
  823. int ret = 0;
  824. struct crypto_alg *alg = crypto_find_alg(name, frontend, type, mask);
  825. if (!IS_ERR(alg)) {
  826. crypto_mod_put(alg);
  827. ret = 1;
  828. }
  829. return ret;
  830. }
  831. EXPORT_SYMBOL_GPL(crypto_type_has_alg);
  832. static void __init crypto_start_tests(void)
  833. {
  834. if (!IS_BUILTIN(CONFIG_CRYPTO_ALGAPI))
  835. return;
  836. if (!IS_ENABLED(CONFIG_CRYPTO_SELFTESTS))
  837. return;
  838. set_crypto_boot_test_finished();
  839. for (;;) {
  840. struct crypto_larval *larval = NULL;
  841. struct crypto_alg *q;
  842. down_write(&crypto_alg_sem);
  843. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  844. struct crypto_larval *l;
  845. if (!crypto_is_larval(q))
  846. continue;
  847. l = (void *)q;
  848. if (!crypto_is_test_larval(l))
  849. continue;
  850. if (l->test_started)
  851. continue;
  852. l->test_started = true;
  853. larval = l;
  854. break;
  855. }
  856. up_write(&crypto_alg_sem);
  857. if (!larval)
  858. break;
  859. crypto_schedule_test(larval);
  860. }
  861. }
  862. static int __init crypto_algapi_init(void)
  863. {
  864. crypto_init_proc();
  865. crypto_start_tests();
  866. return 0;
  867. }
  868. static void __exit crypto_algapi_exit(void)
  869. {
  870. crypto_exit_proc();
  871. }
  872. /*
  873. * We run this at late_initcall so that all the built-in algorithms
  874. * have had a chance to register themselves first.
  875. */
  876. late_initcall(crypto_algapi_init);
  877. module_exit(crypto_algapi_exit);
  878. MODULE_LICENSE("GPL");
  879. MODULE_DESCRIPTION("Cryptographic algorithms API");
  880. MODULE_SOFTDEP("pre: cryptomgr");