api.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. static 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. #if FIPS_FUNC_TEST == 4
  287. int g_tfm_sz = 0;
  288. #endif
  289. struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
  290. u32 mask)
  291. {
  292. struct crypto_tfm *tfm = NULL;
  293. unsigned int tfm_size;
  294. int err = -ENOMEM;
  295. #ifdef CONFIG_CRYPTO_FIPS
  296. if (unlikely(in_fips_err()))
  297. return ERR_PTR(-EACCES);
  298. #endif
  299. tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask);
  300. tfm = kzalloc(tfm_size, GFP_KERNEL);
  301. if (tfm == NULL)
  302. goto out_err;
  303. #if FIPS_FUNC_TEST == 4
  304. g_tfm_sz = tfm_size;
  305. #endif
  306. tfm->__crt_alg = alg;
  307. err = crypto_init_ops(tfm, type, mask);
  308. if (err)
  309. goto out_free_tfm;
  310. if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
  311. goto cra_init_failed;
  312. goto out;
  313. cra_init_failed:
  314. crypto_exit_ops(tfm);
  315. out_free_tfm:
  316. if (err == -EAGAIN)
  317. crypto_shoot_alg(alg);
  318. kfree(tfm);
  319. out_err:
  320. tfm = ERR_PTR(err);
  321. out:
  322. return tfm;
  323. }
  324. EXPORT_SYMBOL_GPL(__crypto_alloc_tfm);
  325. /*
  326. * crypto_alloc_base - Locate algorithm and allocate transform
  327. * @alg_name: Name of algorithm
  328. * @type: Type of algorithm
  329. * @mask: Mask for type comparison
  330. *
  331. * This function should not be used by new algorithm types.
  332. * Plesae use crypto_alloc_tfm instead.
  333. *
  334. * crypto_alloc_base() will first attempt to locate an already loaded
  335. * algorithm. If that fails and the kernel supports dynamically loadable
  336. * modules, it will then attempt to load a module of the same name or
  337. * alias. If that fails it will send a query to any loaded crypto manager
  338. * to construct an algorithm on the fly. A refcount is grabbed on the
  339. * algorithm which is then associated with the new transform.
  340. *
  341. * The returned transform is of a non-determinate type. Most people
  342. * should use one of the more specific allocation functions such as
  343. * crypto_alloc_blkcipher.
  344. *
  345. * In case of error the return value is an error pointer.
  346. */
  347. struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask)
  348. {
  349. struct crypto_tfm *tfm;
  350. int err;
  351. #ifdef CONFIG_CRYPTO_FIPS
  352. if (unlikely(in_fips_err()))
  353. return ERR_PTR(-EACCES);
  354. #endif
  355. for (;;) {
  356. struct crypto_alg *alg;
  357. alg = crypto_alg_mod_lookup(alg_name, type, mask);
  358. if (IS_ERR(alg)) {
  359. err = PTR_ERR(alg);
  360. goto err;
  361. }
  362. tfm = __crypto_alloc_tfm(alg, type, mask);
  363. if (!IS_ERR(tfm))
  364. return tfm;
  365. crypto_mod_put(alg);
  366. err = PTR_ERR(tfm);
  367. err:
  368. if (err != -EAGAIN)
  369. break;
  370. if (fatal_signal_pending(current)) {
  371. err = -EINTR;
  372. break;
  373. }
  374. }
  375. return ERR_PTR(err);
  376. }
  377. EXPORT_SYMBOL_GPL(crypto_alloc_base);
  378. void *crypto_create_tfm(struct crypto_alg *alg,
  379. const struct crypto_type *frontend)
  380. {
  381. char *mem;
  382. struct crypto_tfm *tfm = NULL;
  383. unsigned int tfmsize;
  384. unsigned int total;
  385. int err = -ENOMEM;
  386. #ifdef CONFIG_CRYPTO_FIPS
  387. if (unlikely(in_fips_err())) {
  388. printk(KERN_ERR
  389. "Fail crypto_create_tfm due to fips error state.\n");
  390. return ERR_PTR(-EACCES);
  391. }
  392. #endif
  393. tfmsize = frontend->tfmsize;
  394. total = tfmsize + sizeof(*tfm) + frontend->extsize(alg);
  395. mem = kzalloc(total, GFP_KERNEL);
  396. if (mem == NULL)
  397. goto out_err;
  398. tfm = (struct crypto_tfm *)(mem + tfmsize);
  399. tfm->__crt_alg = alg;
  400. err = frontend->init_tfm(tfm);
  401. if (err)
  402. goto out_free_tfm;
  403. if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
  404. goto cra_init_failed;
  405. goto out;
  406. cra_init_failed:
  407. crypto_exit_ops(tfm);
  408. out_free_tfm:
  409. if (err == -EAGAIN)
  410. crypto_shoot_alg(alg);
  411. kfree(mem);
  412. out_err:
  413. mem = ERR_PTR(err);
  414. out:
  415. return mem;
  416. }
  417. EXPORT_SYMBOL_GPL(crypto_create_tfm);
  418. struct crypto_alg *crypto_find_alg(const char *alg_name,
  419. const struct crypto_type *frontend,
  420. u32 type, u32 mask)
  421. {
  422. struct crypto_alg *(*lookup)(const char *name, u32 type, u32 mask) =
  423. crypto_alg_mod_lookup;
  424. if (frontend) {
  425. type &= frontend->maskclear;
  426. mask &= frontend->maskclear;
  427. type |= frontend->type;
  428. mask |= frontend->maskset;
  429. if (frontend->lookup)
  430. lookup = frontend->lookup;
  431. }
  432. return lookup(alg_name, type, mask);
  433. }
  434. EXPORT_SYMBOL_GPL(crypto_find_alg);
  435. /*
  436. * crypto_alloc_tfm - Locate algorithm and allocate transform
  437. * @alg_name: Name of algorithm
  438. * @frontend: Frontend algorithm type
  439. * @type: Type of algorithm
  440. * @mask: Mask for type comparison
  441. *
  442. * crypto_alloc_tfm() will first attempt to locate an already loaded
  443. * algorithm. If that fails and the kernel supports dynamically loadable
  444. * modules, it will then attempt to load a module of the same name or
  445. * alias. If that fails it will send a query to any loaded crypto manager
  446. * to construct an algorithm on the fly. A refcount is grabbed on the
  447. * algorithm which is then associated with the new transform.
  448. *
  449. * The returned transform is of a non-determinate type. Most people
  450. * should use one of the more specific allocation functions such as
  451. * crypto_alloc_blkcipher.
  452. *
  453. * In case of error the return value is an error pointer.
  454. */
  455. void *crypto_alloc_tfm(const char *alg_name,
  456. const struct crypto_type *frontend, u32 type, u32 mask)
  457. {
  458. void *tfm;
  459. int err;
  460. #ifdef CONFIG_CRYPTO_FIPS
  461. if (unlikely(in_fips_err()))
  462. return ERR_PTR(-EACCES);
  463. #endif
  464. for (;;) {
  465. struct crypto_alg *alg;
  466. alg = crypto_find_alg(alg_name, frontend, type, mask);
  467. if (IS_ERR(alg)) {
  468. err = PTR_ERR(alg);
  469. goto err;
  470. }
  471. tfm = crypto_create_tfm(alg, frontend);
  472. if (!IS_ERR(tfm))
  473. return tfm;
  474. crypto_mod_put(alg);
  475. err = PTR_ERR(tfm);
  476. err:
  477. if (err != -EAGAIN)
  478. break;
  479. if (fatal_signal_pending(current)) {
  480. err = -EINTR;
  481. break;
  482. }
  483. }
  484. return ERR_PTR(err);
  485. }
  486. EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
  487. /*
  488. * crypto_destroy_tfm - Free crypto transform
  489. * @mem: Start of tfm slab
  490. * @tfm: Transform to free
  491. *
  492. * This function frees up the transform and any associated resources,
  493. * then drops the refcount on the associated algorithm.
  494. */
  495. void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
  496. {
  497. struct crypto_alg *alg;
  498. if (unlikely(!mem))
  499. return;
  500. alg = tfm->__crt_alg;
  501. if (!tfm->exit && alg->cra_exit)
  502. alg->cra_exit(tfm);
  503. crypto_exit_ops(tfm);
  504. crypto_mod_put(alg);
  505. #if FIPS_FUNC_TEST == 4
  506. {
  507. extern void hexdump(unsigned char *, unsigned int);
  508. int t = ksize(mem);
  509. printk(KERN_ERR "FIPS: Zeroization crypto_destroy_tfm %d\n", t);
  510. hexdump(mem, t);
  511. #endif
  512. kzfree(mem);
  513. #if FIPS_FUNC_TEST == 4
  514. printk(KERN_ERR "FIPS: Zeroization crypto_destroy_tfm \n");
  515. hexdump(mem, t);
  516. }
  517. #endif
  518. }
  519. EXPORT_SYMBOL_GPL(crypto_destroy_tfm);
  520. int crypto_has_alg(const char *name, u32 type, u32 mask)
  521. {
  522. int ret = 0;
  523. struct crypto_alg *alg = crypto_alg_mod_lookup(name, type, mask);
  524. if (!IS_ERR(alg)) {
  525. crypto_mod_put(alg);
  526. ret = 1;
  527. }
  528. return ret;
  529. }
  530. EXPORT_SYMBOL_GPL(crypto_has_alg);
  531. MODULE_DESCRIPTION("Cryptographic core API");
  532. MODULE_LICENSE("GPL");