api.c 14 KB

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