cts.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * CTS: Cipher Text Stealing mode
  3. *
  4. * COPYRIGHT (c) 2008
  5. * The Regents of the University of Michigan
  6. * ALL RIGHTS RESERVED
  7. *
  8. * Permission is granted to use, copy, create derivative works
  9. * and redistribute this software and such derivative works
  10. * for any purpose, so long as the name of The University of
  11. * Michigan is not used in any advertising or publicity
  12. * pertaining to the use of distribution of this software
  13. * without specific, written prior authorization. If the
  14. * above copyright notice or any other identification of the
  15. * University of Michigan is included in any copy of any
  16. * portion of this software, then the disclaimer below must
  17. * also be included.
  18. *
  19. * THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
  20. * FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
  21. * PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY OF
  22. * MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
  23. * WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
  24. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
  25. * REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
  26. * FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
  27. * CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
  28. * OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
  29. * IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGES.
  31. */
  32. /* Derived from various:
  33. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  34. */
  35. /*
  36. * This is the Cipher Text Stealing mode as described by
  37. * Section 8 of rfc2040 and referenced by rfc3962.
  38. * rfc3962 includes errata information in its Appendix A.
  39. */
  40. #include <crypto/algapi.h>
  41. #include <linux/err.h>
  42. #include <linux/init.h>
  43. #include <linux/kernel.h>
  44. #include <linux/log2.h>
  45. #include <linux/module.h>
  46. #include <linux/scatterlist.h>
  47. #include <crypto/scatterwalk.h>
  48. #include <linux/slab.h>
  49. struct crypto_cts_ctx {
  50. struct crypto_blkcipher *child;
  51. };
  52. static int crypto_cts_setkey(struct crypto_tfm *parent, const u8 *key,
  53. unsigned int keylen)
  54. {
  55. struct crypto_cts_ctx *ctx = crypto_tfm_ctx(parent);
  56. struct crypto_blkcipher *child = ctx->child;
  57. int err;
  58. crypto_blkcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  59. crypto_blkcipher_set_flags(child, crypto_tfm_get_flags(parent) &
  60. CRYPTO_TFM_REQ_MASK);
  61. err = crypto_blkcipher_setkey(child, key, keylen);
  62. crypto_tfm_set_flags(parent, crypto_blkcipher_get_flags(child) &
  63. CRYPTO_TFM_RES_MASK);
  64. return err;
  65. }
  66. static int cts_cbc_encrypt(struct crypto_cts_ctx *ctx,
  67. struct blkcipher_desc *desc,
  68. struct scatterlist *dst,
  69. struct scatterlist *src,
  70. unsigned int offset,
  71. unsigned int nbytes)
  72. {
  73. int bsize = crypto_blkcipher_blocksize(desc->tfm);
  74. u8 tmp[bsize], tmp2[bsize];
  75. struct blkcipher_desc lcldesc;
  76. struct scatterlist sgsrc[1], sgdst[1];
  77. int lastn = nbytes - bsize;
  78. u8 iv[bsize];
  79. u8 s[bsize * 2], d[bsize * 2];
  80. int err;
  81. if (lastn < 0)
  82. return -EINVAL;
  83. memset(s, 0, sizeof(s));
  84. scatterwalk_map_and_copy(s, src, offset, nbytes, 0);
  85. memcpy(iv, desc->info, bsize);
  86. lcldesc.tfm = ctx->child;
  87. lcldesc.info = iv;
  88. lcldesc.flags = desc->flags;
  89. sg_set_buf(&sgsrc[0], s, bsize);
  90. sg_set_buf(&sgdst[0], tmp, bsize);
  91. err = crypto_blkcipher_encrypt_iv(&lcldesc, sgdst, sgsrc, bsize);
  92. memcpy(d + bsize, tmp, lastn);
  93. lcldesc.info = tmp;
  94. sg_set_buf(&sgsrc[0], s + bsize, bsize);
  95. sg_set_buf(&sgdst[0], tmp2, bsize);
  96. err = crypto_blkcipher_encrypt_iv(&lcldesc, sgdst, sgsrc, bsize);
  97. memcpy(d, tmp2, bsize);
  98. scatterwalk_map_and_copy(d, dst, offset, nbytes, 1);
  99. memcpy(desc->info, tmp2, bsize);
  100. return err;
  101. }
  102. static int crypto_cts_encrypt(struct blkcipher_desc *desc,
  103. struct scatterlist *dst, struct scatterlist *src,
  104. unsigned int nbytes)
  105. {
  106. struct crypto_cts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  107. int bsize = crypto_blkcipher_blocksize(desc->tfm);
  108. int tot_blocks = (nbytes + bsize - 1) / bsize;
  109. int cbc_blocks = tot_blocks > 2 ? tot_blocks - 2 : 0;
  110. struct blkcipher_desc lcldesc;
  111. int err;
  112. lcldesc.tfm = ctx->child;
  113. lcldesc.info = desc->info;
  114. lcldesc.flags = desc->flags;
  115. if (tot_blocks == 1) {
  116. err = crypto_blkcipher_encrypt_iv(&lcldesc, dst, src, bsize);
  117. } else if (nbytes <= bsize * 2) {
  118. err = cts_cbc_encrypt(ctx, desc, dst, src, 0, nbytes);
  119. } else {
  120. /* do normal function for tot_blocks - 2 */
  121. err = crypto_blkcipher_encrypt_iv(&lcldesc, dst, src,
  122. cbc_blocks * bsize);
  123. if (err == 0) {
  124. /* do cts for final two blocks */
  125. err = cts_cbc_encrypt(ctx, desc, dst, src,
  126. cbc_blocks * bsize,
  127. nbytes - (cbc_blocks * bsize));
  128. }
  129. }
  130. return err;
  131. }
  132. static int cts_cbc_decrypt(struct crypto_cts_ctx *ctx,
  133. struct blkcipher_desc *desc,
  134. struct scatterlist *dst,
  135. struct scatterlist *src,
  136. unsigned int offset,
  137. unsigned int nbytes)
  138. {
  139. int bsize = crypto_blkcipher_blocksize(desc->tfm);
  140. u8 tmp[bsize];
  141. struct blkcipher_desc lcldesc;
  142. struct scatterlist sgsrc[1], sgdst[1];
  143. int lastn = nbytes - bsize;
  144. u8 iv[bsize];
  145. u8 s[bsize * 2], d[bsize * 2];
  146. int err;
  147. if (lastn < 0)
  148. return -EINVAL;
  149. scatterwalk_map_and_copy(s, src, offset, nbytes, 0);
  150. lcldesc.tfm = ctx->child;
  151. lcldesc.info = iv;
  152. lcldesc.flags = desc->flags;
  153. /* 1. Decrypt Cn-1 (s) to create Dn (tmp)*/
  154. memset(iv, 0, sizeof(iv));
  155. sg_set_buf(&sgsrc[0], s, bsize);
  156. sg_set_buf(&sgdst[0], tmp, bsize);
  157. err = crypto_blkcipher_decrypt_iv(&lcldesc, sgdst, sgsrc, bsize);
  158. if (err)
  159. return err;
  160. /* 2. Pad Cn with zeros at the end to create C of length BB */
  161. memset(iv, 0, sizeof(iv));
  162. memcpy(iv, s + bsize, lastn);
  163. /* 3. Exclusive-or Dn (tmp) with C (iv) to create Xn (tmp) */
  164. crypto_xor(tmp, iv, bsize);
  165. /* 4. Select the first Ln bytes of Xn (tmp) to create Pn */
  166. memcpy(d + bsize, tmp, lastn);
  167. /* 5. Append the tail (BB - Ln) bytes of Xn (tmp) to Cn to create En */
  168. memcpy(s + bsize + lastn, tmp + lastn, bsize - lastn);
  169. /* 6. Decrypt En to create Pn-1 */
  170. memset(iv, 0, sizeof(iv));
  171. sg_set_buf(&sgsrc[0], s + bsize, bsize);
  172. sg_set_buf(&sgdst[0], d, bsize);
  173. err = crypto_blkcipher_decrypt_iv(&lcldesc, sgdst, sgsrc, bsize);
  174. /* XOR with previous block */
  175. crypto_xor(d, desc->info, bsize);
  176. scatterwalk_map_and_copy(d, dst, offset, nbytes, 1);
  177. memcpy(desc->info, s, bsize);
  178. return err;
  179. }
  180. static int crypto_cts_decrypt(struct blkcipher_desc *desc,
  181. struct scatterlist *dst, struct scatterlist *src,
  182. unsigned int nbytes)
  183. {
  184. struct crypto_cts_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  185. int bsize = crypto_blkcipher_blocksize(desc->tfm);
  186. int tot_blocks = (nbytes + bsize - 1) / bsize;
  187. int cbc_blocks = tot_blocks > 2 ? tot_blocks - 2 : 0;
  188. struct blkcipher_desc lcldesc;
  189. int err;
  190. lcldesc.tfm = ctx->child;
  191. lcldesc.info = desc->info;
  192. lcldesc.flags = desc->flags;
  193. if (tot_blocks == 1) {
  194. err = crypto_blkcipher_decrypt_iv(&lcldesc, dst, src, bsize);
  195. } else if (nbytes <= bsize * 2) {
  196. err = cts_cbc_decrypt(ctx, desc, dst, src, 0, nbytes);
  197. } else {
  198. /* do normal function for tot_blocks - 2 */
  199. err = crypto_blkcipher_decrypt_iv(&lcldesc, dst, src,
  200. cbc_blocks * bsize);
  201. if (err == 0) {
  202. /* do cts for final two blocks */
  203. err = cts_cbc_decrypt(ctx, desc, dst, src,
  204. cbc_blocks * bsize,
  205. nbytes - (cbc_blocks * bsize));
  206. }
  207. }
  208. return err;
  209. }
  210. static int crypto_cts_init_tfm(struct crypto_tfm *tfm)
  211. {
  212. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  213. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  214. struct crypto_cts_ctx *ctx = crypto_tfm_ctx(tfm);
  215. struct crypto_blkcipher *cipher;
  216. cipher = crypto_spawn_blkcipher(spawn);
  217. if (IS_ERR(cipher))
  218. return PTR_ERR(cipher);
  219. ctx->child = cipher;
  220. return 0;
  221. }
  222. static void crypto_cts_exit_tfm(struct crypto_tfm *tfm)
  223. {
  224. struct crypto_cts_ctx *ctx = crypto_tfm_ctx(tfm);
  225. crypto_free_blkcipher(ctx->child);
  226. }
  227. static struct crypto_instance *crypto_cts_alloc(struct rtattr **tb)
  228. {
  229. struct crypto_instance *inst;
  230. struct crypto_alg *alg;
  231. int err;
  232. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
  233. if (err)
  234. return ERR_PTR(err);
  235. alg = crypto_attr_alg(tb[1], CRYPTO_ALG_TYPE_BLKCIPHER,
  236. CRYPTO_ALG_TYPE_MASK);
  237. err = PTR_ERR(alg);
  238. if (IS_ERR(alg))
  239. return ERR_PTR(err);
  240. inst = ERR_PTR(-EINVAL);
  241. if (!is_power_of_2(alg->cra_blocksize))
  242. goto out_put_alg;
  243. inst = crypto_alloc_instance("cts", alg);
  244. if (IS_ERR(inst))
  245. goto out_put_alg;
  246. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  247. inst->alg.cra_priority = alg->cra_priority;
  248. inst->alg.cra_blocksize = alg->cra_blocksize;
  249. inst->alg.cra_alignmask = alg->cra_alignmask;
  250. inst->alg.cra_type = &crypto_blkcipher_type;
  251. /* We access the data as u32s when xoring. */
  252. inst->alg.cra_alignmask |= __alignof__(u32) - 1;
  253. inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
  254. inst->alg.cra_blkcipher.min_keysize = alg->cra_blkcipher.min_keysize;
  255. inst->alg.cra_blkcipher.max_keysize = alg->cra_blkcipher.max_keysize;
  256. inst->alg.cra_blkcipher.geniv = "seqiv";
  257. inst->alg.cra_ctxsize = sizeof(struct crypto_cts_ctx);
  258. inst->alg.cra_init = crypto_cts_init_tfm;
  259. inst->alg.cra_exit = crypto_cts_exit_tfm;
  260. inst->alg.cra_blkcipher.setkey = crypto_cts_setkey;
  261. inst->alg.cra_blkcipher.encrypt = crypto_cts_encrypt;
  262. inst->alg.cra_blkcipher.decrypt = crypto_cts_decrypt;
  263. out_put_alg:
  264. crypto_mod_put(alg);
  265. return inst;
  266. }
  267. static void crypto_cts_free(struct crypto_instance *inst)
  268. {
  269. crypto_drop_spawn(crypto_instance_ctx(inst));
  270. kfree(inst);
  271. }
  272. static struct crypto_template crypto_cts_tmpl = {
  273. .name = "cts",
  274. .alloc = crypto_cts_alloc,
  275. .free = crypto_cts_free,
  276. .module = THIS_MODULE,
  277. };
  278. static int __init crypto_cts_module_init(void)
  279. {
  280. return crypto_register_template(&crypto_cts_tmpl);
  281. }
  282. static void __exit crypto_cts_module_exit(void)
  283. {
  284. crypto_unregister_template(&crypto_cts_tmpl);
  285. }
  286. module_init(crypto_cts_module_init);
  287. module_exit(crypto_cts_module_exit);
  288. MODULE_LICENSE("Dual BSD/GPL");
  289. MODULE_DESCRIPTION("CTS-CBC CipherText Stealing for CBC");