api.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*
  2. * Scatterlist Cryptographic API.
  3. *
  4. * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
  5. * Copyright (c) 2002 David S. Miller (davem@redhat.com)
  6. * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
  7. *
  8. * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
  9. * and Nettle, by Niels Möller.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. *
  16. */
  17. #include <linux/err.h>
  18. #include <linux/errno.h>
  19. #include <linux/kernel.h>
  20. #include <linux/kmod.h>
  21. #include <linux/module.h>
  22. #include <linux/param.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/string.h>
  26. #include "internal.h"
  27. LIST_HEAD(crypto_alg_list);
  28. EXPORT_SYMBOL_GPL(crypto_alg_list);
  29. DECLARE_RWSEM(crypto_alg_sem);
  30. EXPORT_SYMBOL_GPL(crypto_alg_sem);
  31. BLOCKING_NOTIFIER_HEAD(crypto_chain);
  32. EXPORT_SYMBOL_GPL(crypto_chain);
  33. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg);
  34. static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
  35. {
  36. atomic_inc(&alg->cra_refcnt);
  37. return alg;
  38. }
  39. struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
  40. {
  41. return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
  42. }
  43. EXPORT_SYMBOL_GPL(crypto_mod_get);
  44. void crypto_mod_put(struct crypto_alg *alg)
  45. {
  46. struct module *module = alg->cra_module;
  47. crypto_alg_put(alg);
  48. module_put(module);
  49. }
  50. EXPORT_SYMBOL_GPL(crypto_mod_put);
  51. static inline int crypto_is_test_larval(struct crypto_larval *larval)
  52. {
  53. return larval->alg.cra_driver_name[0];
  54. }
  55. static struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type,
  56. u32 mask)
  57. {
  58. struct crypto_alg *q, *alg = NULL;
  59. int best = -2;
  60. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  61. int exact, fuzzy;
  62. if (crypto_is_moribund(q))
  63. continue;
  64. if ((q->cra_flags ^ type) & mask)
  65. continue;
  66. if (crypto_is_larval(q) &&
  67. !crypto_is_test_larval((struct crypto_larval *)q) &&
  68. ((struct crypto_larval *)q)->mask != mask)
  69. continue;
  70. exact = !strcmp(q->cra_driver_name, name);
  71. fuzzy = !strcmp(q->cra_name, name);
  72. if (!exact && !(fuzzy && q->cra_priority > best))
  73. continue;
  74. if (unlikely(!crypto_mod_get(q)))
  75. continue;
  76. best = q->cra_priority;
  77. if (alg)
  78. crypto_mod_put(alg);
  79. alg = q;
  80. if (exact)
  81. break;
  82. }
  83. return alg;
  84. }
  85. static void crypto_larval_destroy(struct crypto_alg *alg)
  86. {
  87. struct crypto_larval *larval = (void *)alg;
  88. BUG_ON(!crypto_is_larval(alg));
  89. if (larval->adult)
  90. crypto_mod_put(larval->adult);
  91. kfree(larval);
  92. }
  93. struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask)
  94. {
  95. struct crypto_larval *larval;
  96. larval = kzalloc(sizeof(*larval), GFP_KERNEL);
  97. if (!larval)
  98. return ERR_PTR(-ENOMEM);
  99. larval->mask = mask;
  100. larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type;
  101. larval->alg.cra_priority = -1;
  102. larval->alg.cra_destroy = crypto_larval_destroy;
  103. strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
  104. init_completion(&larval->completion);
  105. return larval;
  106. }
  107. EXPORT_SYMBOL_GPL(crypto_larval_alloc);
  108. static struct crypto_alg *crypto_larval_add(const char *name, u32 type,
  109. u32 mask)
  110. {
  111. struct crypto_alg *alg;
  112. struct crypto_larval *larval;
  113. larval = crypto_larval_alloc(name, type, mask);
  114. if (IS_ERR(larval))
  115. return ERR_CAST(larval);
  116. atomic_set(&larval->alg.cra_refcnt, 2);
  117. down_write(&crypto_alg_sem);
  118. alg = __crypto_alg_lookup(name, type, mask);
  119. if (!alg) {
  120. alg = &larval->alg;
  121. list_add(&alg->cra_list, &crypto_alg_list);
  122. }
  123. up_write(&crypto_alg_sem);
  124. //patch from kernel 3.13.7 (refer to https://www.kernel.org/ & https://lkml.org/lkml/2013/9/7/139)
  125. if (alg != &larval->alg) {
  126. kfree(larval);
  127. if (crypto_is_larval(alg))
  128. alg = crypto_larval_wait(alg);
  129. }
  130. return alg;
  131. }
  132. void crypto_larval_kill(struct crypto_alg *alg)
  133. {
  134. struct crypto_larval *larval = (void *)alg;
  135. down_write(&crypto_alg_sem);
  136. list_del(&alg->cra_list);
  137. up_write(&crypto_alg_sem);
  138. complete_all(&larval->completion);
  139. crypto_alg_put(alg);
  140. }
  141. EXPORT_SYMBOL_GPL(crypto_larval_kill);
  142. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
  143. {
  144. struct crypto_larval *larval = (void *)alg;
  145. long timeout;
  146. timeout = wait_for_completion_killable_timeout(
  147. &larval->completion, 60 * HZ);
  148. alg = larval->adult;
  149. if (timeout < 0)
  150. alg = ERR_PTR(-EINTR);
  151. else if (!timeout)
  152. alg = ERR_PTR(-ETIMEDOUT);
  153. else if (!alg)
  154. alg = ERR_PTR(-ENOENT);
  155. else if (crypto_is_test_larval(larval) &&
  156. !(alg->cra_flags & CRYPTO_ALG_TESTED))
  157. alg = ERR_PTR(-EAGAIN);
  158. else if (!crypto_mod_get(alg))
  159. alg = ERR_PTR(-EAGAIN);
  160. crypto_mod_put(&larval->alg);
  161. return alg;
  162. }
  163. struct crypto_alg *crypto_alg_lookup(const char *name, u32 type, u32 mask)
  164. {
  165. struct crypto_alg *alg;
  166. down_read(&crypto_alg_sem);
  167. alg = __crypto_alg_lookup(name, type, mask);
  168. up_read(&crypto_alg_sem);
  169. return alg;
  170. }
  171. EXPORT_SYMBOL_GPL(crypto_alg_lookup);
  172. struct crypto_alg *crypto_larval_lookup(const char *name, u32 type, u32 mask)
  173. {
  174. struct crypto_alg *alg;
  175. if (!name)
  176. return ERR_PTR(-ENOENT);
  177. mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
  178. type &= mask;
  179. alg = crypto_alg_lookup(name, type, mask);
  180. if (!alg) {
  181. request_module("%s", name);
  182. if (!((type ^ CRYPTO_ALG_NEED_FALLBACK) & mask &
  183. CRYPTO_ALG_NEED_FALLBACK))
  184. request_module("%s-all", name);
  185. alg = crypto_alg_lookup(name, type, mask);
  186. }
  187. if (alg)
  188. return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;
  189. return crypto_larval_add(name, type, mask);
  190. }
  191. EXPORT_SYMBOL_GPL(crypto_larval_lookup);
  192. int crypto_probing_notify(unsigned long val, void *v)
  193. {
  194. int ok;
  195. ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  196. if (ok == NOTIFY_DONE) {
  197. request_module("cryptomgr");
  198. ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  199. }
  200. return ok;
  201. }
  202. EXPORT_SYMBOL_GPL(crypto_probing_notify);
  203. struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
  204. {
  205. struct crypto_alg *alg;
  206. struct crypto_alg *larval;
  207. int ok;
  208. if (!((type | mask) & CRYPTO_ALG_TESTED)) {
  209. type |= CRYPTO_ALG_TESTED;
  210. mask |= CRYPTO_ALG_TESTED;
  211. }
  212. larval = crypto_larval_lookup(name, type, mask);
  213. if (IS_ERR(larval) || !crypto_is_larval(larval))
  214. return larval;
  215. ok = crypto_probing_notify(CRYPTO_MSG_ALG_REQUEST, larval);
  216. if (ok == NOTIFY_STOP)
  217. alg = crypto_larval_wait(larval);
  218. else {
  219. crypto_mod_put(larval);
  220. alg = ERR_PTR(-ENOENT);
  221. }
  222. crypto_larval_kill(larval);
  223. return alg;
  224. }
  225. EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
  226. static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  227. {
  228. const struct crypto_type *type_obj = tfm->__crt_alg->cra_type;
  229. if (type_obj)
  230. return type_obj->init(tfm, type, mask);
  231. switch (crypto_tfm_alg_type(tfm)) {
  232. case CRYPTO_ALG_TYPE_CIPHER:
  233. return crypto_init_cipher_ops(tfm);
  234. case CRYPTO_ALG_TYPE_COMPRESS:
  235. return crypto_init_compress_ops(tfm);
  236. default:
  237. break;
  238. }
  239. BUG();
  240. return -EINVAL;
  241. }
  242. static void crypto_exit_ops(struct crypto_tfm *tfm)
  243. {
  244. const struct crypto_type *type = tfm->__crt_alg->cra_type;
  245. if (type) {
  246. if (tfm->exit)
  247. tfm->exit(tfm);
  248. return;
  249. }
  250. switch (crypto_tfm_alg_type(tfm)) {
  251. case CRYPTO_ALG_TYPE_CIPHER:
  252. crypto_exit_cipher_ops(tfm);
  253. break;
  254. case CRYPTO_ALG_TYPE_COMPRESS:
  255. crypto_exit_compress_ops(tfm);
  256. break;
  257. default:
  258. BUG();
  259. }
  260. }
  261. static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask)
  262. {
  263. const struct crypto_type *type_obj = alg->cra_type;
  264. unsigned int len;
  265. len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
  266. if (type_obj)
  267. return len + type_obj->ctxsize(alg, type, mask);
  268. switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  269. default:
  270. BUG();
  271. case CRYPTO_ALG_TYPE_CIPHER:
  272. len += crypto_cipher_ctxsize(alg);
  273. break;
  274. case CRYPTO_ALG_TYPE_COMPRESS:
  275. len += crypto_compress_ctxsize(alg);
  276. break;
  277. }
  278. return len;
  279. }
  280. void crypto_shoot_alg(struct crypto_alg *alg)
  281. {
  282. down_write(&crypto_alg_sem);
  283. alg->cra_flags |= CRYPTO_ALG_DYING;
  284. up_write(&crypto_alg_sem);
  285. }
  286. EXPORT_SYMBOL_GPL(crypto_shoot_alg);
  287. #if FIPS_FUNC_TEST == 4
  288. int g_tfm_sz = 0;
  289. #endif
  290. struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
  291. u32 mask)
  292. {
  293. struct crypto_tfm *tfm = NULL;
  294. unsigned int tfm_size;
  295. int err = -ENOMEM;
  296. #ifdef CONFIG_CRYPTO_FIPS
  297. if (unlikely(in_fips_err()))
  298. return ERR_PTR(-EACCES);
  299. #endif
  300. tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask);
  301. tfm = kzalloc(tfm_size, GFP_KERNEL);
  302. if (tfm == NULL)
  303. goto out_err;
  304. #if FIPS_FUNC_TEST == 4
  305. g_tfm_sz = tfm_size;
  306. #endif
  307. tfm->__crt_alg = alg;
  308. err = crypto_init_ops(tfm, type, mask);
  309. if (err)
  310. goto out_free_tfm;
  311. if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
  312. goto cra_init_failed;
  313. goto out;
  314. cra_init_failed:
  315. crypto_exit_ops(tfm);
  316. out_free_tfm:
  317. if (err == -EAGAIN)
  318. crypto_shoot_alg(alg);
  319. kfree(tfm);
  320. out_err:
  321. tfm = ERR_PTR(err);
  322. out:
  323. return tfm;
  324. }
  325. EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);
  326. /*
  327. * crypto_alloc_base - Locate algorithm and allocate transform
  328. * @alg_name: Name of algorithm
  329. * @type: Type of algorithm
  330. * @mask: Mask for type comparison
  331. *
  332. * This function should not be used by new algorithm types.
  333. * Plesae use crypto_alloc_tfm instead.
  334. *
  335. * crypto_alloc_base() will first attempt to locate an already loaded
  336. * algorithm. If that fails and the kernel supports dynamically loadable
  337. * modules, it will then attempt to load a module of the same name or
  338. * alias. If that fails it will send a query to any loaded crypto manager
  339. * to construct an algorithm on the fly. A refcount is grabbed on the
  340. * algorithm which is then associated with the new transform.
  341. *
  342. * The returned transform is of a non-determinate type. Most people
  343. * should use one of the more specific allocation functions such as
  344. * crypto_alloc_blkcipher.
  345. *
  346. * In case of error the return value is an error pointer.
  347. */
  348. struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
  349. {
  350. struct crypto_tfm *tfm;
  351. int err;
  352. #ifdef CONFIG_CRYPTO_FIPS
  353. if (unlikely(in_fips_err()))
  354. return ERR_PTR(-EACCES);
  355. #endif
  356. for (;;) {
  357. struct crypto_alg *alg;
  358. alg = crypto_alg_mod_lookup(alg_name, type, mask);
  359. if (IS_ERR(alg)) {
  360. err = PTR_ERR(alg);
  361. goto err;
  362. }
  363. tfm = __crypto_alloc_tfm(alg, type, mask);
  364. if (!IS_ERR(tfm))
  365. return tfm;
  366. crypto_mod_put(alg);
  367. err = PTR_ERR(tfm);
  368. err:
  369. if (err != -EAGAIN)
  370. break;
  371. if (fatal_signal_pending(current)) {
  372. err = -EINTR;
  373. break;
  374. }
  375. }
  376. return ERR_PTR(err);
  377. }
  378. EXPORT_SYMBOL_GPL(crypto_alloc_base);
  379. void *crypto_create_tfm(struct crypto_alg *alg,
  380. const struct crypto_type *frontend)
  381. {
  382. char *mem;
  383. struct crypto_tfm *tfm = NULL;
  384. unsigned int tfmsize;
  385. unsigned int total;
  386. int err = -ENOMEM;
  387. #ifdef CONFIG_CRYPTO_FIPS
  388. if (unlikely(in_fips_err())) {
  389. printk(KERN_ERR
  390. "Fail crypto_create_tfm due to fips error state.\n");
  391. return ERR_PTR(-EACCES);
  392. }
  393. #endif
  394. tfmsize = frontend->tfmsize;
  395. total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
  396. mem = kzalloc(total, GFP_KERNEL);
  397. if (mem == NULL)
  398. goto out_err;
  399. tfm = (struct crypto_tfm *)(mem + tfmsize);
  400. tfm->__crt_alg = alg;
  401. err = frontend->init_tfm(tfm);
  402. if (err)
  403. goto out_free_tfm;
  404. if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
  405. goto cra_init_failed;
  406. goto out;
  407. cra_init_failed:
  408. crypto_exit_ops(tfm);
  409. out_free_tfm:
  410. if (err == -EAGAIN)
  411. crypto_shoot_alg(alg);
  412. kfree(mem);
  413. out_err:
  414. mem = ERR_PTR(err);
  415. out:
  416. return mem;
  417. }
  418. EXPORT_SYMBOL_GPL(crypto_create_tfm);
  419. struct crypto_alg *crypto_find_alg(const char *alg_name,
  420. const struct crypto_type *frontend,
  421. u32 type, u32 mask)
  422. {
  423. struct crypto_alg *(*lookup)(const char *name, u32 type, u32 mask) =
  424. crypto_alg_mod_lookup;
  425. if (frontend) {
  426. type &= frontend->maskclear;
  427. mask &= frontend->maskclear;
  428. type |= frontend->type;
  429. mask |= frontend->maskset;
  430. if (frontend->lookup)
  431. lookup = frontend->lookup;
  432. }
  433. return lookup(alg_name, type, mask);
  434. }
  435. EXPORT_SYMBOL_GPL(crypto_find_alg);
  436. /*
  437. * crypto_alloc_tfm - Locate algorithm and allocate transform
  438. * @alg_name: Name of algorithm
  439. * @frontend: Frontend algorithm type
  440. * @type: Type of algorithm
  441. * @mask: Mask for type comparison
  442. *
  443. * crypto_alloc_tfm() will first attempt to locate an already loaded
  444. * algorithm. If that fails and the kernel supports dynamically loadable
  445. * modules, it will then attempt to load a module of the same name or
  446. * alias. If that fails it will send a query to any loaded crypto manager
  447. * to construct an algorithm on the fly. A refcount is grabbed on the
  448. * algorithm which is then associated with the new transform.
  449. *
  450. * The returned transform is of a non-determinate type. Most people
  451. * should use one of the more specific allocation functions such as
  452. * crypto_alloc_blkcipher.
  453. *
  454. * In case of error the return value is an error pointer.
  455. */
  456. void *crypto_alloc_tfm(const char *alg_name,
  457. const struct crypto_type *frontend, u32 type, u32 mask)
  458. {
  459. void *tfm;
  460. int err;
  461. #ifdef CONFIG_CRYPTO_FIPS
  462. if (unlikely(in_fips_err()))
  463. return ERR_PTR(-EACCES);
  464. #endif
  465. for (;;) {
  466. struct crypto_alg *alg;
  467. alg = crypto_find_alg(alg_name, frontend, type, mask);
  468. if (IS_ERR(alg)) {
  469. err = PTR_ERR(alg);
  470. goto err;
  471. }
  472. tfm = crypto_create_tfm(alg, frontend);
  473. if (!IS_ERR(tfm))
  474. return tfm;
  475. crypto_mod_put(alg);
  476. err = PTR_ERR(tfm);
  477. err:
  478. if (err != -EAGAIN)
  479. break;
  480. if (fatal_signal_pending(current)) {
  481. err = -EINTR;
  482. break;
  483. }
  484. }
  485. return ERR_PTR(err);
  486. }
  487. EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
  488. /*
  489. * crypto_destroy_tfm - Free crypto transform
  490. * @mem: Start of tfm slab
  491. * @tfm: Transform to free
  492. *
  493. * This function frees up the transform and any associated resources,
  494. * then drops the refcount on the associated algorithm.
  495. */
  496. void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
  497. {
  498. struct crypto_alg *alg;
  499. if (unlikely(!mem))
  500. return;
  501. alg = tfm->__crt_alg;
  502. if (!tfm->exit && alg->cra_exit)
  503. alg->cra_exit(tfm);
  504. crypto_exit_ops(tfm);
  505. crypto_mod_put(alg);
  506. #if FIPS_FUNC_TEST == 4
  507. {
  508. extern void hexdump(unsigned char *, unsigned int);
  509. int t = ksize(mem);
  510. printk(KERN_ERR "FIPS: Zeroization crypto_destroy_tfm %d\n", t);
  511. hexdump(mem, t);
  512. #endif
  513. kzfree(mem);
  514. #if FIPS_FUNC_TEST == 4
  515. printk(KERN_ERR "FIPS: Zeroization crypto_destroy_tfm \n");
  516. hexdump(mem, t);
  517. }
  518. #endif
  519. }
  520. EXPORT_SYMBOL_GPL(crypto_destroy_tfm);
  521. int crypto_has_alg(const char *name, u32 type, u32 mask)
  522. {
  523. int ret = 0;
  524. struct crypto_alg *alg = crypto_alg_mod_lookup(name, type, mask);
  525. if (!IS_ERR(alg)) {
  526. crypto_mod_put(alg);
  527. ret = 1;
  528. }
  529. return ret;
  530. }
  531. EXPORT_SYMBOL_GPL(crypto_has_alg);
  532. MODULE_DESCRIPTION("Cryptographic core API");
  533. MODULE_LICENSE("GPL");