api.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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 inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg)
  34. {
  35. atomic_inc(&alg->cra_refcnt);
  36. return alg;
  37. }
  38. struct crypto_alg *crypto_mod_get(struct crypto_alg *alg)
  39. {
  40. return try_module_get(alg->cra_module) ? crypto_alg_get(alg) : NULL;
  41. }
  42. EXPORT_SYMBOL_GPL(crypto_mod_get);
  43. void crypto_mod_put(struct crypto_alg *alg)
  44. {
  45. struct module *module = alg->cra_module;
  46. crypto_alg_put(alg);
  47. module_put(module);
  48. }
  49. EXPORT_SYMBOL_GPL(crypto_mod_put);
  50. static inline int crypto_is_test_larval(struct crypto_larval *larval)
  51. {
  52. return larval->alg.cra_driver_name[0];
  53. }
  54. static struct crypto_alg *__crypto_alg_lookup(const char *name, u32 type,
  55. u32 mask)
  56. {
  57. struct crypto_alg *q, *alg = NULL;
  58. int best = -2;
  59. list_for_each_entry(q, &crypto_alg_list, cra_list) {
  60. int exact, fuzzy;
  61. if (crypto_is_moribund(q))
  62. continue;
  63. if ((q->cra_flags ^ type) & mask)
  64. continue;
  65. if (crypto_is_larval(q) &&
  66. !crypto_is_test_larval((struct crypto_larval *)q) &&
  67. ((struct crypto_larval *)q)->mask != mask)
  68. continue;
  69. exact = !strcmp(q->cra_driver_name, name);
  70. fuzzy = !strcmp(q->cra_name, name);
  71. if (!exact && !(fuzzy && q->cra_priority > best))
  72. continue;
  73. if (unlikely(!crypto_mod_get(q)))
  74. continue;
  75. best = q->cra_priority;
  76. if (alg)
  77. crypto_mod_put(alg);
  78. alg = q;
  79. if (exact)
  80. break;
  81. }
  82. return alg;
  83. }
  84. static void crypto_larval_destroy(struct crypto_alg *alg)
  85. {
  86. struct crypto_larval *larval = (void *)alg;
  87. BUG_ON(!crypto_is_larval(alg));
  88. if (larval->adult)
  89. crypto_mod_put(larval->adult);
  90. kfree(larval);
  91. }
  92. struct crypto_larval *crypto_larval_alloc(const char *name, u32 type, u32 mask)
  93. {
  94. struct crypto_larval *larval;
  95. larval = kzalloc(sizeof(*larval), GFP_KERNEL);
  96. if (!larval)
  97. return ERR_PTR(-ENOMEM);
  98. larval->mask = mask;
  99. larval->alg.cra_flags = CRYPTO_ALG_LARVAL | type;
  100. larval->alg.cra_priority = -1;
  101. larval->alg.cra_destroy = crypto_larval_destroy;
  102. strlcpy(larval->alg.cra_name, name, CRYPTO_MAX_ALG_NAME);
  103. init_completion(&larval->completion);
  104. return larval;
  105. }
  106. EXPORT_SYMBOL_GPL(crypto_larval_alloc);
  107. static struct crypto_alg *crypto_larval_add(const char *name, u32 type,
  108. u32 mask)
  109. {
  110. struct crypto_alg *alg;
  111. struct crypto_larval *larval;
  112. larval = crypto_larval_alloc(name, type, mask);
  113. if (IS_ERR(larval))
  114. return ERR_CAST(larval);
  115. atomic_set(&larval->alg.cra_refcnt, 2);
  116. down_write(&crypto_alg_sem);
  117. alg = __crypto_alg_lookup(name, type, mask);
  118. if (!alg) {
  119. alg = &larval->alg;
  120. list_add(&alg->cra_list, &crypto_alg_list);
  121. }
  122. up_write(&crypto_alg_sem);
  123. if (alg != &larval->alg)
  124. kfree(larval);
  125. return alg;
  126. }
  127. void crypto_larval_kill(struct crypto_alg *alg)
  128. {
  129. struct crypto_larval *larval = (void *)alg;
  130. down_write(&crypto_alg_sem);
  131. list_del(&alg->cra_list);
  132. up_write(&crypto_alg_sem);
  133. complete_all(&larval->completion);
  134. crypto_alg_put(alg);
  135. }
  136. EXPORT_SYMBOL_GPL(crypto_larval_kill);
  137. static struct crypto_alg *crypto_larval_wait(struct crypto_alg *alg)
  138. {
  139. struct crypto_larval *larval = (void *)alg;
  140. long timeout;
  141. timeout = wait_for_completion_interruptible_timeout(
  142. &larval->completion, 60 * HZ);
  143. alg = larval->adult;
  144. if (timeout < 0)
  145. alg = ERR_PTR(-EINTR);
  146. else if (!timeout)
  147. alg = ERR_PTR(-ETIMEDOUT);
  148. else if (!alg)
  149. alg = ERR_PTR(-ENOENT);
  150. else if (crypto_is_test_larval(larval) &&
  151. !(alg->cra_flags & CRYPTO_ALG_TESTED))
  152. alg = ERR_PTR(-EAGAIN);
  153. else if (!crypto_mod_get(alg))
  154. alg = ERR_PTR(-EAGAIN);
  155. crypto_mod_put(&larval->alg);
  156. return alg;
  157. }
  158. struct crypto_alg *crypto_alg_lookup(const char *name, u32 type, u32 mask)
  159. {
  160. struct crypto_alg *alg;
  161. down_read(&crypto_alg_sem);
  162. alg = __crypto_alg_lookup(name, type, mask);
  163. up_read(&crypto_alg_sem);
  164. return alg;
  165. }
  166. EXPORT_SYMBOL_GPL(crypto_alg_lookup);
  167. struct crypto_alg *crypto_larval_lookup(const char *name, u32 type, u32 mask)
  168. {
  169. struct crypto_alg *alg;
  170. if (!name)
  171. return ERR_PTR(-ENOENT);
  172. mask &= ~(CRYPTO_ALG_LARVAL | CRYPTO_ALG_DEAD);
  173. type &= mask;
  174. alg = crypto_alg_lookup(name, type, mask);
  175. if (!alg) {
  176. char tmp[CRYPTO_MAX_ALG_NAME];
  177. request_module(name);
  178. if (!((type ^ CRYPTO_ALG_NEED_FALLBACK) & mask &
  179. CRYPTO_ALG_NEED_FALLBACK) &&
  180. snprintf(tmp, sizeof(tmp), "%s-all", name) < sizeof(tmp))
  181. request_module(tmp);
  182. alg = crypto_alg_lookup(name, type, mask);
  183. }
  184. if (alg)
  185. return crypto_is_larval(alg) ? crypto_larval_wait(alg) : alg;
  186. return crypto_larval_add(name, type, mask);
  187. }
  188. EXPORT_SYMBOL_GPL(crypto_larval_lookup);
  189. int crypto_probing_notify(unsigned long val, void *v)
  190. {
  191. int ok;
  192. ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  193. if (ok == NOTIFY_DONE) {
  194. request_module("cryptomgr");
  195. ok = blocking_notifier_call_chain(&crypto_chain, val, v);
  196. }
  197. return ok;
  198. }
  199. EXPORT_SYMBOL_GPL(crypto_probing_notify);
  200. struct crypto_alg *crypto_alg_mod_lookup(const char *name, u32 type, u32 mask)
  201. {
  202. struct crypto_alg *alg;
  203. struct crypto_alg *larval;
  204. int ok;
  205. if (!((type | mask) & CRYPTO_ALG_TESTED)) {
  206. type |= CRYPTO_ALG_TESTED;
  207. mask |= CRYPTO_ALG_TESTED;
  208. }
  209. larval = crypto_larval_lookup(name, type, mask);
  210. if (IS_ERR(larval) || !crypto_is_larval(larval))
  211. return larval;
  212. ok = crypto_probing_notify(CRYPTO_MSG_ALG_REQUEST, larval);
  213. if (ok == NOTIFY_STOP)
  214. alg = crypto_larval_wait(larval);
  215. else {
  216. crypto_mod_put(larval);
  217. alg = ERR_PTR(-ENOENT);
  218. }
  219. crypto_larval_kill(larval);
  220. return alg;
  221. }
  222. EXPORT_SYMBOL_GPL(crypto_alg_mod_lookup);
  223. static int crypto_init_ops(struct crypto_tfm *tfm, u32 type, u32 mask)
  224. {
  225. const struct crypto_type *type_obj = tfm->__crt_alg->cra_type;
  226. if (type_obj)
  227. return type_obj->init(tfm, type, mask);
  228. switch (crypto_tfm_alg_type(tfm)) {
  229. case CRYPTO_ALG_TYPE_CIPHER:
  230. return crypto_init_cipher_ops(tfm);
  231. case CRYPTO_ALG_TYPE_DIGEST:
  232. if ((mask & CRYPTO_ALG_TYPE_HASH_MASK) !=
  233. CRYPTO_ALG_TYPE_HASH_MASK)
  234. return crypto_init_digest_ops_async(tfm);
  235. else
  236. return crypto_init_digest_ops(tfm);
  237. case CRYPTO_ALG_TYPE_COMPRESS:
  238. return crypto_init_compress_ops(tfm);
  239. default:
  240. break;
  241. }
  242. BUG();
  243. return -EINVAL;
  244. }
  245. static void crypto_exit_ops(struct crypto_tfm *tfm)
  246. {
  247. const struct crypto_type *type = tfm->__crt_alg->cra_type;
  248. if (type) {
  249. if (tfm->exit)
  250. tfm->exit(tfm);
  251. return;
  252. }
  253. switch (crypto_tfm_alg_type(tfm)) {
  254. case CRYPTO_ALG_TYPE_CIPHER:
  255. crypto_exit_cipher_ops(tfm);
  256. break;
  257. case CRYPTO_ALG_TYPE_DIGEST:
  258. crypto_exit_digest_ops(tfm);
  259. break;
  260. case CRYPTO_ALG_TYPE_COMPRESS:
  261. crypto_exit_compress_ops(tfm);
  262. break;
  263. default:
  264. BUG();
  265. }
  266. }
  267. static unsigned int crypto_ctxsize(struct crypto_alg *alg, u32 type, u32 mask)
  268. {
  269. const struct crypto_type *type_obj = alg->cra_type;
  270. unsigned int len;
  271. len = alg->cra_alignmask & ~(crypto_tfm_ctx_alignment() - 1);
  272. if (type_obj)
  273. return len + type_obj->ctxsize(alg, type, mask);
  274. switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
  275. default:
  276. BUG();
  277. case CRYPTO_ALG_TYPE_CIPHER:
  278. len += crypto_cipher_ctxsize(alg);
  279. break;
  280. case CRYPTO_ALG_TYPE_DIGEST:
  281. len += crypto_digest_ctxsize(alg);
  282. break;
  283. case CRYPTO_ALG_TYPE_COMPRESS:
  284. len += crypto_compress_ctxsize(alg);
  285. break;
  286. }
  287. return len;
  288. }
  289. void crypto_shoot_alg(struct crypto_alg *alg)
  290. {
  291. down_write(&crypto_alg_sem);
  292. alg->cra_flags |= CRYPTO_ALG_DYING;
  293. up_write(&crypto_alg_sem);
  294. }
  295. EXPORT_SYMBOL_GPL(crypto_shoot_alg);
  296. struct crypto_tfm *__crypto_alloc_tfm(struct crypto_alg *alg, u32 type,
  297. u32 mask)
  298. {
  299. struct crypto_tfm *tfm = NULL;
  300. unsigned int tfm_size;
  301. int err = -ENOMEM;
  302. tfm_size = sizeof(*tfm) + crypto_ctxsize(alg, type, mask);
  303. tfm = kzalloc(tfm_size, GFP_KERNEL);
  304. if (tfm == NULL)
  305. goto out_err;
  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. for (;;) {
  352. struct crypto_alg *alg;
  353. alg = crypto_alg_mod_lookup(alg_name, type, mask);
  354. if (IS_ERR(alg)) {
  355. err = PTR_ERR(alg);
  356. goto err;
  357. }
  358. tfm = __crypto_alloc_tfm(alg, type, mask);
  359. if (!IS_ERR(tfm))
  360. return tfm;
  361. crypto_mod_put(alg);
  362. err = PTR_ERR(tfm);
  363. err:
  364. if (err != -EAGAIN)
  365. break;
  366. if (signal_pending(current)) {
  367. err = -EINTR;
  368. break;
  369. }
  370. }
  371. return ERR_PTR(err);
  372. }
  373. EXPORT_SYMBOL_GPL(crypto_alloc_base);
  374. void *crypto_create_tfm(struct crypto_alg *alg,
  375. const struct crypto_type *frontend)
  376. {
  377. char *mem;
  378. struct crypto_tfm *tfm = NULL;
  379. unsigned int tfmsize;
  380. unsigned int total;
  381. int err = -ENOMEM;
  382. tfmsize = frontend->tfmsize;
  383. total = tfmsize + sizeof(*tfm) + frontend->extsize(alg, frontend);
  384. mem = kzalloc(total, GFP_KERNEL);
  385. if (mem == NULL)
  386. goto out_err;
  387. tfm = (struct crypto_tfm *)(mem + tfmsize);
  388. tfm->__crt_alg = alg;
  389. err = frontend->init_tfm(tfm, frontend);
  390. if (err)
  391. goto out_free_tfm;
  392. if (!tfm->exit && alg->cra_init && (err = alg->cra_init(tfm)))
  393. goto cra_init_failed;
  394. goto out;
  395. cra_init_failed:
  396. crypto_exit_ops(tfm);
  397. out_free_tfm:
  398. if (err == -EAGAIN)
  399. crypto_shoot_alg(alg);
  400. kfree(mem);
  401. out_err:
  402. mem = ERR_PTR(err);
  403. out:
  404. return mem;
  405. }
  406. EXPORT_SYMBOL_GPL(crypto_create_tfm);
  407. /*
  408. * crypto_alloc_tfm - Locate algorithm and allocate transform
  409. * @alg_name: Name of algorithm
  410. * @frontend: Frontend algorithm type
  411. * @type: Type of algorithm
  412. * @mask: Mask for type comparison
  413. *
  414. * crypto_alloc_tfm() will first attempt to locate an already loaded
  415. * algorithm. If that fails and the kernel supports dynamically loadable
  416. * modules, it will then attempt to load a module of the same name or
  417. * alias. If that fails it will send a query to any loaded crypto manager
  418. * to construct an algorithm on the fly. A refcount is grabbed on the
  419. * algorithm which is then associated with the new transform.
  420. *
  421. * The returned transform is of a non-determinate type. Most people
  422. * should use one of the more specific allocation functions such as
  423. * crypto_alloc_blkcipher.
  424. *
  425. * In case of error the return value is an error pointer.
  426. */
  427. void *crypto_alloc_tfm(const char *alg_name,
  428. const struct crypto_type *frontend, u32 type, u32 mask)
  429. {
  430. struct crypto_alg *(*lookup)(const char *name, u32 type, u32 mask);
  431. void *tfm;
  432. int err;
  433. type &= frontend->maskclear;
  434. mask &= frontend->maskclear;
  435. type |= frontend->type;
  436. mask |= frontend->maskset;
  437. lookup = frontend->lookup ?: crypto_alg_mod_lookup;
  438. for (;;) {
  439. struct crypto_alg *alg;
  440. alg = lookup(alg_name, type, mask);
  441. if (IS_ERR(alg)) {
  442. err = PTR_ERR(alg);
  443. goto err;
  444. }
  445. tfm = crypto_create_tfm(alg, frontend);
  446. if (!IS_ERR(tfm))
  447. return tfm;
  448. crypto_mod_put(alg);
  449. err = PTR_ERR(tfm);
  450. err:
  451. if (err != -EAGAIN)
  452. break;
  453. if (signal_pending(current)) {
  454. err = -EINTR;
  455. break;
  456. }
  457. }
  458. return ERR_PTR(err);
  459. }
  460. EXPORT_SYMBOL_GPL(crypto_alloc_tfm);
  461. /*
  462. * crypto_destroy_tfm - Free crypto transform
  463. * @mem: Start of tfm slab
  464. * @tfm: Transform to free
  465. *
  466. * This function frees up the transform and any associated resources,
  467. * then drops the refcount on the associated algorithm.
  468. */
  469. void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
  470. {
  471. struct crypto_alg *alg;
  472. int size;
  473. if (unlikely(!mem))
  474. return;
  475. alg = tfm->__crt_alg;
  476. size = ksize(mem);
  477. if (!tfm->exit && alg->cra_exit)
  478. alg->cra_exit(tfm);
  479. crypto_exit_ops(tfm);
  480. crypto_mod_put(alg);
  481. memset(mem, 0, size);
  482. kfree(mem);
  483. }
  484. EXPORT_SYMBOL_GPL(crypto_destroy_tfm);
  485. int crypto_has_alg(const char *name, u32 type, u32 mask)
  486. {
  487. int ret = 0;
  488. struct crypto_alg *alg = crypto_alg_mod_lookup(name, type, mask);
  489. if (!IS_ERR(alg)) {
  490. crypto_mod_put(alg);
  491. ret = 1;
  492. }
  493. return ret;
  494. }
  495. EXPORT_SYMBOL_GPL(crypto_has_alg);
  496. MODULE_DESCRIPTION("Cryptographic core API");
  497. MODULE_LICENSE("GPL");