ablkcipher.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Asynchronous block chaining cipher operations.
  3. *
  4. * This is the asynchronous version of blkcipher.c indicating completion
  5. * via a callback.
  6. *
  7. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. */
  15. #include <crypto/internal/skcipher.h>
  16. #include <linux/cpumask.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <linux/seq_file.h>
  25. #include "internal.h"
  26. static const char *skcipher_default_geniv __read_mostly;
  27. static int setkey_unaligned(struct crypto_ablkcipher *tfm, const u8 *key,
  28. unsigned int keylen)
  29. {
  30. struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
  31. unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
  32. int ret;
  33. u8 *buffer, *alignbuffer;
  34. unsigned long absize;
  35. absize = keylen + alignmask;
  36. buffer = kmalloc(absize, GFP_ATOMIC);
  37. if (!buffer)
  38. return -ENOMEM;
  39. alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
  40. memcpy(alignbuffer, key, keylen);
  41. ret = cipher->setkey(tfm, alignbuffer, keylen);
  42. memset(alignbuffer, 0, keylen);
  43. kfree(buffer);
  44. return ret;
  45. }
  46. static int setkey(struct crypto_ablkcipher *tfm, const u8 *key,
  47. unsigned int keylen)
  48. {
  49. struct ablkcipher_alg *cipher = crypto_ablkcipher_alg(tfm);
  50. unsigned long alignmask = crypto_ablkcipher_alignmask(tfm);
  51. if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
  52. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  53. return -EINVAL;
  54. }
  55. if ((unsigned long)key & alignmask)
  56. return setkey_unaligned(tfm, key, keylen);
  57. return cipher->setkey(tfm, key, keylen);
  58. }
  59. static unsigned int crypto_ablkcipher_ctxsize(struct crypto_alg *alg, u32 type,
  60. u32 mask)
  61. {
  62. return alg->cra_ctxsize;
  63. }
  64. int skcipher_null_givencrypt(struct skcipher_givcrypt_request *req)
  65. {
  66. return crypto_ablkcipher_encrypt(&req->creq);
  67. }
  68. int skcipher_null_givdecrypt(struct skcipher_givcrypt_request *req)
  69. {
  70. return crypto_ablkcipher_decrypt(&req->creq);
  71. }
  72. static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
  73. u32 mask)
  74. {
  75. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  76. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  77. if (alg->ivsize > PAGE_SIZE / 8)
  78. return -EINVAL;
  79. crt->setkey = setkey;
  80. crt->encrypt = alg->encrypt;
  81. crt->decrypt = alg->decrypt;
  82. if (!alg->ivsize) {
  83. crt->givencrypt = skcipher_null_givencrypt;
  84. crt->givdecrypt = skcipher_null_givdecrypt;
  85. }
  86. crt->base = __crypto_ablkcipher_cast(tfm);
  87. crt->ivsize = alg->ivsize;
  88. return 0;
  89. }
  90. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  91. __attribute__ ((unused));
  92. static void crypto_ablkcipher_show(struct seq_file *m, struct crypto_alg *alg)
  93. {
  94. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  95. seq_printf(m, "type : ablkcipher\n");
  96. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  97. "yes" : "no");
  98. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  99. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  100. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  101. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  102. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<default>");
  103. }
  104. const struct crypto_type crypto_ablkcipher_type = {
  105. .ctxsize = crypto_ablkcipher_ctxsize,
  106. .init = crypto_init_ablkcipher_ops,
  107. #ifdef CONFIG_PROC_FS
  108. .show = crypto_ablkcipher_show,
  109. #endif
  110. };
  111. EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
  112. static int no_givdecrypt(struct skcipher_givcrypt_request *req)
  113. {
  114. return -ENOSYS;
  115. }
  116. static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
  117. u32 mask)
  118. {
  119. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  120. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  121. if (alg->ivsize > PAGE_SIZE / 8)
  122. return -EINVAL;
  123. crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
  124. alg->setkey : setkey;
  125. crt->encrypt = alg->encrypt;
  126. crt->decrypt = alg->decrypt;
  127. crt->givencrypt = alg->givencrypt;
  128. crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
  129. crt->base = __crypto_ablkcipher_cast(tfm);
  130. crt->ivsize = alg->ivsize;
  131. return 0;
  132. }
  133. static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  134. __attribute__ ((unused));
  135. static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  136. {
  137. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  138. seq_printf(m, "type : givcipher\n");
  139. seq_printf(m, "async : %s\n", alg->cra_flags & CRYPTO_ALG_ASYNC ?
  140. "yes" : "no");
  141. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  142. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  143. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  144. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  145. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<built-in>");
  146. }
  147. const struct crypto_type crypto_givcipher_type = {
  148. .ctxsize = crypto_ablkcipher_ctxsize,
  149. .init = crypto_init_givcipher_ops,
  150. #ifdef CONFIG_PROC_FS
  151. .show = crypto_givcipher_show,
  152. #endif
  153. };
  154. EXPORT_SYMBOL_GPL(crypto_givcipher_type);
  155. const char *crypto_default_geniv(const struct crypto_alg *alg)
  156. {
  157. if (((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  158. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  159. alg->cra_ablkcipher.ivsize) !=
  160. alg->cra_blocksize)
  161. return "chainiv";
  162. return alg->cra_flags & CRYPTO_ALG_ASYNC ?
  163. "eseqiv" : skcipher_default_geniv;
  164. }
  165. static int crypto_givcipher_default(struct crypto_alg *alg, u32 type, u32 mask)
  166. {
  167. struct rtattr *tb[3];
  168. struct {
  169. struct rtattr attr;
  170. struct crypto_attr_type data;
  171. } ptype;
  172. struct {
  173. struct rtattr attr;
  174. struct crypto_attr_alg data;
  175. } palg;
  176. struct crypto_template *tmpl;
  177. struct crypto_instance *inst;
  178. struct crypto_alg *larval;
  179. const char *geniv;
  180. int err;
  181. larval = crypto_larval_lookup(alg->cra_driver_name,
  182. (type & ~CRYPTO_ALG_TYPE_MASK) |
  183. CRYPTO_ALG_TYPE_GIVCIPHER,
  184. mask | CRYPTO_ALG_TYPE_MASK);
  185. err = PTR_ERR(larval);
  186. if (IS_ERR(larval))
  187. goto out;
  188. err = -EAGAIN;
  189. if (!crypto_is_larval(larval))
  190. goto drop_larval;
  191. ptype.attr.rta_len = sizeof(ptype);
  192. ptype.attr.rta_type = CRYPTOA_TYPE;
  193. ptype.data.type = type | CRYPTO_ALG_GENIV;
  194. /* GENIV tells the template that we're making a default geniv. */
  195. ptype.data.mask = mask | CRYPTO_ALG_GENIV;
  196. tb[0] = &ptype.attr;
  197. palg.attr.rta_len = sizeof(palg);
  198. palg.attr.rta_type = CRYPTOA_ALG;
  199. /* Must use the exact name to locate ourselves. */
  200. memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
  201. tb[1] = &palg.attr;
  202. tb[2] = NULL;
  203. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  204. CRYPTO_ALG_TYPE_BLKCIPHER)
  205. geniv = alg->cra_blkcipher.geniv;
  206. else
  207. geniv = alg->cra_ablkcipher.geniv;
  208. if (!geniv)
  209. geniv = crypto_default_geniv(alg);
  210. tmpl = crypto_lookup_template(geniv);
  211. err = -ENOENT;
  212. if (!tmpl)
  213. goto kill_larval;
  214. inst = tmpl->alloc(tb);
  215. err = PTR_ERR(inst);
  216. if (IS_ERR(inst))
  217. goto put_tmpl;
  218. if ((err = crypto_register_instance(tmpl, inst))) {
  219. tmpl->free(inst);
  220. goto put_tmpl;
  221. }
  222. /* Redo the lookup to use the instance we just registered. */
  223. err = -EAGAIN;
  224. put_tmpl:
  225. crypto_tmpl_put(tmpl);
  226. kill_larval:
  227. crypto_larval_kill(larval);
  228. drop_larval:
  229. crypto_mod_put(larval);
  230. out:
  231. crypto_mod_put(alg);
  232. return err;
  233. }
  234. static struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type,
  235. u32 mask)
  236. {
  237. struct crypto_alg *alg;
  238. alg = crypto_alg_mod_lookup(name, type, mask);
  239. if (IS_ERR(alg))
  240. return alg;
  241. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  242. CRYPTO_ALG_TYPE_GIVCIPHER)
  243. return alg;
  244. if (!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  245. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  246. alg->cra_ablkcipher.ivsize))
  247. return alg;
  248. crypto_mod_put(alg);
  249. alg = crypto_alg_mod_lookup(name, type | CRYPTO_ALG_TESTED,
  250. mask & ~CRYPTO_ALG_TESTED);
  251. if (IS_ERR(alg))
  252. return alg;
  253. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  254. CRYPTO_ALG_TYPE_GIVCIPHER) {
  255. if ((alg->cra_flags ^ type ^ ~mask) & CRYPTO_ALG_TESTED) {
  256. crypto_mod_put(alg);
  257. alg = ERR_PTR(-ENOENT);
  258. }
  259. return alg;
  260. }
  261. BUG_ON(!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  262. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  263. alg->cra_ablkcipher.ivsize));
  264. return ERR_PTR(crypto_givcipher_default(alg, type, mask));
  265. }
  266. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
  267. u32 type, u32 mask)
  268. {
  269. struct crypto_alg *alg;
  270. int err;
  271. type = crypto_skcipher_type(type);
  272. mask = crypto_skcipher_mask(mask);
  273. alg = crypto_lookup_skcipher(name, type, mask);
  274. if (IS_ERR(alg))
  275. return PTR_ERR(alg);
  276. err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
  277. crypto_mod_put(alg);
  278. return err;
  279. }
  280. EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
  281. struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
  282. u32 type, u32 mask)
  283. {
  284. struct crypto_tfm *tfm;
  285. int err;
  286. type = crypto_skcipher_type(type);
  287. mask = crypto_skcipher_mask(mask);
  288. for (;;) {
  289. struct crypto_alg *alg;
  290. alg = crypto_lookup_skcipher(alg_name, type, mask);
  291. if (IS_ERR(alg)) {
  292. err = PTR_ERR(alg);
  293. goto err;
  294. }
  295. tfm = __crypto_alloc_tfm(alg, type, mask);
  296. if (!IS_ERR(tfm))
  297. return __crypto_ablkcipher_cast(tfm);
  298. crypto_mod_put(alg);
  299. err = PTR_ERR(tfm);
  300. err:
  301. if (err != -EAGAIN)
  302. break;
  303. if (signal_pending(current)) {
  304. err = -EINTR;
  305. break;
  306. }
  307. }
  308. return ERR_PTR(err);
  309. }
  310. EXPORT_SYMBOL_GPL(crypto_alloc_ablkcipher);
  311. static int __init skcipher_module_init(void)
  312. {
  313. skcipher_default_geniv = num_possible_cpus() > 1 ?
  314. "eseqiv" : "chainiv";
  315. return 0;
  316. }
  317. static void skcipher_module_exit(void)
  318. {
  319. }
  320. module_init(skcipher_module_init);
  321. module_exit(skcipher_module_exit);