ablkcipher.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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, "blocksize : %u\n", alg->cra_blocksize);
  95. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  96. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  97. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  98. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<default>");
  99. }
  100. const struct crypto_type crypto_ablkcipher_type = {
  101. .ctxsize = crypto_ablkcipher_ctxsize,
  102. .init = crypto_init_ablkcipher_ops,
  103. #ifdef CONFIG_PROC_FS
  104. .show = crypto_ablkcipher_show,
  105. #endif
  106. };
  107. EXPORT_SYMBOL_GPL(crypto_ablkcipher_type);
  108. static int no_givdecrypt(struct skcipher_givcrypt_request *req)
  109. {
  110. return -ENOSYS;
  111. }
  112. static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
  113. u32 mask)
  114. {
  115. struct ablkcipher_alg *alg = &tfm->__crt_alg->cra_ablkcipher;
  116. struct ablkcipher_tfm *crt = &tfm->crt_ablkcipher;
  117. if (alg->ivsize > PAGE_SIZE / 8)
  118. return -EINVAL;
  119. crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
  120. alg->setkey : setkey;
  121. crt->encrypt = alg->encrypt;
  122. crt->decrypt = alg->decrypt;
  123. crt->givencrypt = alg->givencrypt;
  124. crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
  125. crt->base = __crypto_ablkcipher_cast(tfm);
  126. crt->ivsize = alg->ivsize;
  127. return 0;
  128. }
  129. static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  130. __attribute__ ((unused));
  131. static void crypto_givcipher_show(struct seq_file *m, struct crypto_alg *alg)
  132. {
  133. struct ablkcipher_alg *ablkcipher = &alg->cra_ablkcipher;
  134. seq_printf(m, "type : givcipher\n");
  135. seq_printf(m, "blocksize : %u\n", alg->cra_blocksize);
  136. seq_printf(m, "min keysize : %u\n", ablkcipher->min_keysize);
  137. seq_printf(m, "max keysize : %u\n", ablkcipher->max_keysize);
  138. seq_printf(m, "ivsize : %u\n", ablkcipher->ivsize);
  139. seq_printf(m, "geniv : %s\n", ablkcipher->geniv ?: "<built-in>");
  140. }
  141. const struct crypto_type crypto_givcipher_type = {
  142. .ctxsize = crypto_ablkcipher_ctxsize,
  143. .init = crypto_init_givcipher_ops,
  144. #ifdef CONFIG_PROC_FS
  145. .show = crypto_givcipher_show,
  146. #endif
  147. };
  148. EXPORT_SYMBOL_GPL(crypto_givcipher_type);
  149. const char *crypto_default_geniv(const struct crypto_alg *alg)
  150. {
  151. return alg->cra_flags & CRYPTO_ALG_ASYNC ? "eseqiv" : "chainiv";
  152. }
  153. static int crypto_givcipher_default(struct crypto_alg *alg, u32 type, u32 mask)
  154. {
  155. struct rtattr *tb[3];
  156. struct {
  157. struct rtattr attr;
  158. struct crypto_attr_type data;
  159. } ptype;
  160. struct {
  161. struct rtattr attr;
  162. struct crypto_attr_alg data;
  163. } palg;
  164. struct crypto_template *tmpl;
  165. struct crypto_instance *inst;
  166. struct crypto_alg *larval;
  167. const char *geniv;
  168. int err;
  169. larval = crypto_larval_lookup(alg->cra_driver_name,
  170. CRYPTO_ALG_TYPE_GIVCIPHER,
  171. CRYPTO_ALG_TYPE_MASK);
  172. err = PTR_ERR(larval);
  173. if (IS_ERR(larval))
  174. goto out;
  175. err = -EAGAIN;
  176. if (!crypto_is_larval(larval))
  177. goto drop_larval;
  178. ptype.attr.rta_len = sizeof(ptype);
  179. ptype.attr.rta_type = CRYPTOA_TYPE;
  180. ptype.data.type = type | CRYPTO_ALG_GENIV;
  181. /* GENIV tells the template that we're making a default geniv. */
  182. ptype.data.mask = mask | CRYPTO_ALG_GENIV;
  183. tb[0] = &ptype.attr;
  184. palg.attr.rta_len = sizeof(palg);
  185. palg.attr.rta_type = CRYPTOA_ALG;
  186. /* Must use the exact name to locate ourselves. */
  187. memcpy(palg.data.name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME);
  188. tb[1] = &palg.attr;
  189. tb[2] = NULL;
  190. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  191. CRYPTO_ALG_TYPE_BLKCIPHER)
  192. geniv = alg->cra_blkcipher.geniv;
  193. else
  194. geniv = alg->cra_ablkcipher.geniv;
  195. if (!geniv)
  196. geniv = crypto_default_geniv(alg);
  197. tmpl = crypto_lookup_template(geniv);
  198. err = -ENOENT;
  199. if (!tmpl)
  200. goto kill_larval;
  201. inst = tmpl->alloc(tb);
  202. err = PTR_ERR(inst);
  203. if (IS_ERR(inst))
  204. goto put_tmpl;
  205. if ((err = crypto_register_instance(tmpl, inst))) {
  206. tmpl->free(inst);
  207. goto put_tmpl;
  208. }
  209. /* Redo the lookup to use the instance we just registered. */
  210. err = -EAGAIN;
  211. put_tmpl:
  212. crypto_tmpl_put(tmpl);
  213. kill_larval:
  214. crypto_larval_kill(larval);
  215. drop_larval:
  216. crypto_mod_put(larval);
  217. out:
  218. crypto_mod_put(alg);
  219. return err;
  220. }
  221. static struct crypto_alg *crypto_lookup_skcipher(const char *name, u32 type,
  222. u32 mask)
  223. {
  224. struct crypto_alg *alg;
  225. alg = crypto_alg_mod_lookup(name, type, mask);
  226. if (IS_ERR(alg))
  227. return alg;
  228. if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  229. CRYPTO_ALG_TYPE_GIVCIPHER)
  230. return alg;
  231. if (!((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
  232. CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
  233. alg->cra_ablkcipher.ivsize))
  234. return alg;
  235. return ERR_PTR(crypto_givcipher_default(alg, type, mask));
  236. }
  237. int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
  238. u32 type, u32 mask)
  239. {
  240. struct crypto_alg *alg;
  241. int err;
  242. type = crypto_skcipher_type(type);
  243. mask = crypto_skcipher_mask(mask);
  244. alg = crypto_lookup_skcipher(name, type, mask);
  245. if (IS_ERR(alg))
  246. return PTR_ERR(alg);
  247. err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
  248. crypto_mod_put(alg);
  249. return err;
  250. }
  251. EXPORT_SYMBOL_GPL(crypto_grab_skcipher);
  252. struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name,
  253. u32 type, u32 mask)
  254. {
  255. struct crypto_tfm *tfm;
  256. int err;
  257. type = crypto_skcipher_type(type);
  258. mask = crypto_skcipher_mask(mask);
  259. for (;;) {
  260. struct crypto_alg *alg;
  261. alg = crypto_lookup_skcipher(alg_name, type, mask);
  262. if (IS_ERR(alg)) {
  263. err = PTR_ERR(alg);
  264. goto err;
  265. }
  266. tfm = __crypto_alloc_tfm(alg, type, mask);
  267. if (!IS_ERR(tfm))
  268. return __crypto_ablkcipher_cast(tfm);
  269. crypto_mod_put(alg);
  270. err = PTR_ERR(tfm);
  271. err:
  272. if (err != -EAGAIN)
  273. break;
  274. if (signal_pending(current)) {
  275. err = -EINTR;
  276. break;
  277. }
  278. }
  279. return ERR_PTR(err);
  280. }
  281. EXPORT_SYMBOL_GPL(crypto_alloc_ablkcipher);
  282. MODULE_LICENSE("GPL");
  283. MODULE_DESCRIPTION("Asynchronous block chaining cipher type");