ablkcipher.c 9.3 KB

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