cts.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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/internal/skcipher.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_skcipher *child;
  51. };
  52. struct crypto_cts_reqctx {
  53. struct scatterlist sg[2];
  54. unsigned offset;
  55. struct skcipher_request subreq;
  56. };
  57. static inline u8 *crypto_cts_reqctx_space(struct skcipher_request *req)
  58. {
  59. struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
  60. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  61. struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
  62. struct crypto_skcipher *child = ctx->child;
  63. return PTR_ALIGN((u8 *)(rctx + 1) + crypto_skcipher_reqsize(child),
  64. crypto_skcipher_alignmask(tfm) + 1);
  65. }
  66. static int crypto_cts_setkey(struct crypto_skcipher *parent, const u8 *key,
  67. unsigned int keylen)
  68. {
  69. struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(parent);
  70. struct crypto_skcipher *child = ctx->child;
  71. int err;
  72. crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  73. crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
  74. CRYPTO_TFM_REQ_MASK);
  75. err = crypto_skcipher_setkey(child, key, keylen);
  76. crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
  77. CRYPTO_TFM_RES_MASK);
  78. return err;
  79. }
  80. static void cts_cbc_crypt_done(struct crypto_async_request *areq, int err)
  81. {
  82. struct skcipher_request *req = areq->data;
  83. if (err == -EINPROGRESS)
  84. return;
  85. skcipher_request_complete(req, err);
  86. }
  87. static int cts_cbc_encrypt(struct skcipher_request *req)
  88. {
  89. struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
  90. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  91. struct skcipher_request *subreq = &rctx->subreq;
  92. int bsize = crypto_skcipher_blocksize(tfm);
  93. u8 d[bsize * 2] __attribute__ ((aligned(__alignof__(u32))));
  94. struct scatterlist *sg;
  95. unsigned int offset;
  96. int lastn;
  97. offset = rctx->offset;
  98. lastn = req->cryptlen - offset;
  99. sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize);
  100. scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0);
  101. memset(d, 0, bsize);
  102. scatterwalk_map_and_copy(d, req->src, offset, lastn, 0);
  103. scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1);
  104. memzero_explicit(d, sizeof(d));
  105. skcipher_request_set_callback(subreq, req->base.flags &
  106. CRYPTO_TFM_REQ_MAY_BACKLOG,
  107. cts_cbc_crypt_done, req);
  108. skcipher_request_set_crypt(subreq, sg, sg, bsize, req->iv);
  109. return crypto_skcipher_encrypt(subreq);
  110. }
  111. static void crypto_cts_encrypt_done(struct crypto_async_request *areq, int err)
  112. {
  113. struct skcipher_request *req = areq->data;
  114. if (err)
  115. goto out;
  116. err = cts_cbc_encrypt(req);
  117. if (err == -EINPROGRESS ||
  118. (err == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  119. return;
  120. out:
  121. skcipher_request_complete(req, err);
  122. }
  123. static int crypto_cts_encrypt(struct skcipher_request *req)
  124. {
  125. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  126. struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
  127. struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
  128. struct skcipher_request *subreq = &rctx->subreq;
  129. int bsize = crypto_skcipher_blocksize(tfm);
  130. unsigned int nbytes = req->cryptlen;
  131. int cbc_blocks = (nbytes + bsize - 1) / bsize - 1;
  132. unsigned int offset;
  133. skcipher_request_set_tfm(subreq, ctx->child);
  134. if (cbc_blocks <= 0) {
  135. skcipher_request_set_callback(subreq, req->base.flags,
  136. req->base.complete,
  137. req->base.data);
  138. skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes,
  139. req->iv);
  140. return crypto_skcipher_encrypt(subreq);
  141. }
  142. offset = cbc_blocks * bsize;
  143. rctx->offset = offset;
  144. skcipher_request_set_callback(subreq, req->base.flags,
  145. crypto_cts_encrypt_done, req);
  146. skcipher_request_set_crypt(subreq, req->src, req->dst,
  147. offset, req->iv);
  148. return crypto_skcipher_encrypt(subreq) ?:
  149. cts_cbc_encrypt(req);
  150. }
  151. static int cts_cbc_decrypt(struct skcipher_request *req)
  152. {
  153. struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
  154. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  155. struct skcipher_request *subreq = &rctx->subreq;
  156. int bsize = crypto_skcipher_blocksize(tfm);
  157. u8 d[bsize * 2] __attribute__ ((aligned(__alignof__(u32))));
  158. struct scatterlist *sg;
  159. unsigned int offset;
  160. u8 *space;
  161. int lastn;
  162. offset = rctx->offset;
  163. lastn = req->cryptlen - offset;
  164. sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize);
  165. /* 1. Decrypt Cn-1 (s) to create Dn */
  166. scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0);
  167. space = crypto_cts_reqctx_space(req);
  168. crypto_xor(d + bsize, space, bsize);
  169. /* 2. Pad Cn with zeros at the end to create C of length BB */
  170. memset(d, 0, bsize);
  171. scatterwalk_map_and_copy(d, req->src, offset, lastn, 0);
  172. /* 3. Exclusive-or Dn with C to create Xn */
  173. /* 4. Select the first Ln bytes of Xn to create Pn */
  174. crypto_xor(d + bsize, d, lastn);
  175. /* 5. Append the tail (BB - Ln) bytes of Xn to Cn to create En */
  176. memcpy(d + lastn, d + bsize + lastn, bsize - lastn);
  177. /* 6. Decrypt En to create Pn-1 */
  178. scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1);
  179. memzero_explicit(d, sizeof(d));
  180. skcipher_request_set_callback(subreq, req->base.flags &
  181. CRYPTO_TFM_REQ_MAY_BACKLOG,
  182. cts_cbc_crypt_done, req);
  183. skcipher_request_set_crypt(subreq, sg, sg, bsize, space);
  184. return crypto_skcipher_decrypt(subreq);
  185. }
  186. static void crypto_cts_decrypt_done(struct crypto_async_request *areq, int err)
  187. {
  188. struct skcipher_request *req = areq->data;
  189. if (err)
  190. goto out;
  191. err = cts_cbc_decrypt(req);
  192. if (err == -EINPROGRESS ||
  193. (err == -EBUSY && req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  194. return;
  195. out:
  196. skcipher_request_complete(req, err);
  197. }
  198. static int crypto_cts_decrypt(struct skcipher_request *req)
  199. {
  200. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  201. struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
  202. struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
  203. struct skcipher_request *subreq = &rctx->subreq;
  204. int bsize = crypto_skcipher_blocksize(tfm);
  205. unsigned int nbytes = req->cryptlen;
  206. int cbc_blocks = (nbytes + bsize - 1) / bsize - 1;
  207. unsigned int offset;
  208. u8 *space;
  209. skcipher_request_set_tfm(subreq, ctx->child);
  210. if (cbc_blocks <= 0) {
  211. skcipher_request_set_callback(subreq, req->base.flags,
  212. req->base.complete,
  213. req->base.data);
  214. skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes,
  215. req->iv);
  216. return crypto_skcipher_decrypt(subreq);
  217. }
  218. skcipher_request_set_callback(subreq, req->base.flags,
  219. crypto_cts_decrypt_done, req);
  220. space = crypto_cts_reqctx_space(req);
  221. offset = cbc_blocks * bsize;
  222. rctx->offset = offset;
  223. if (cbc_blocks <= 1)
  224. memcpy(space, req->iv, bsize);
  225. else
  226. scatterwalk_map_and_copy(space, req->src, offset - 2 * bsize,
  227. bsize, 0);
  228. skcipher_request_set_crypt(subreq, req->src, req->dst,
  229. offset, req->iv);
  230. return crypto_skcipher_decrypt(subreq) ?:
  231. cts_cbc_decrypt(req);
  232. }
  233. static int crypto_cts_init_tfm(struct crypto_skcipher *tfm)
  234. {
  235. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  236. struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
  237. struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
  238. struct crypto_skcipher *cipher;
  239. unsigned reqsize;
  240. unsigned bsize;
  241. unsigned align;
  242. cipher = crypto_spawn_skcipher2(spawn);
  243. if (IS_ERR(cipher))
  244. return PTR_ERR(cipher);
  245. ctx->child = cipher;
  246. align = crypto_skcipher_alignmask(tfm);
  247. bsize = crypto_skcipher_blocksize(cipher);
  248. reqsize = ALIGN(sizeof(struct crypto_cts_reqctx) +
  249. crypto_skcipher_reqsize(cipher),
  250. crypto_tfm_ctx_alignment()) +
  251. (align & ~(crypto_tfm_ctx_alignment() - 1)) + bsize;
  252. crypto_skcipher_set_reqsize(tfm, reqsize);
  253. return 0;
  254. }
  255. static void crypto_cts_exit_tfm(struct crypto_skcipher *tfm)
  256. {
  257. struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
  258. crypto_free_skcipher(ctx->child);
  259. }
  260. static void crypto_cts_free(struct skcipher_instance *inst)
  261. {
  262. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  263. kfree(inst);
  264. }
  265. static int crypto_cts_create(struct crypto_template *tmpl, struct rtattr **tb)
  266. {
  267. struct crypto_skcipher_spawn *spawn;
  268. struct skcipher_instance *inst;
  269. struct crypto_attr_type *algt;
  270. struct skcipher_alg *alg;
  271. const char *cipher_name;
  272. int err;
  273. algt = crypto_get_attr_type(tb);
  274. if (IS_ERR(algt))
  275. return PTR_ERR(algt);
  276. if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
  277. return -EINVAL;
  278. cipher_name = crypto_attr_alg_name(tb[1]);
  279. if (IS_ERR(cipher_name))
  280. return PTR_ERR(cipher_name);
  281. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  282. if (!inst)
  283. return -ENOMEM;
  284. spawn = skcipher_instance_ctx(inst);
  285. crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
  286. err = crypto_grab_skcipher2(spawn, cipher_name, 0,
  287. crypto_requires_sync(algt->type,
  288. algt->mask));
  289. if (err)
  290. goto err_free_inst;
  291. alg = crypto_spawn_skcipher_alg(spawn);
  292. err = -EINVAL;
  293. if (crypto_skcipher_alg_ivsize(alg) != alg->base.cra_blocksize)
  294. goto err_drop_spawn;
  295. if (strncmp(alg->base.cra_name, "cbc(", 4))
  296. goto err_drop_spawn;
  297. err = crypto_inst_setname(skcipher_crypto_instance(inst), "cts",
  298. &alg->base);
  299. if (err)
  300. goto err_drop_spawn;
  301. inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  302. inst->alg.base.cra_priority = alg->base.cra_priority;
  303. inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
  304. inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
  305. /* We access the data as u32s when xoring. */
  306. inst->alg.base.cra_alignmask |= __alignof__(u32) - 1;
  307. inst->alg.ivsize = alg->base.cra_blocksize;
  308. inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
  309. inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
  310. inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
  311. inst->alg.base.cra_ctxsize = sizeof(struct crypto_cts_ctx);
  312. inst->alg.init = crypto_cts_init_tfm;
  313. inst->alg.exit = crypto_cts_exit_tfm;
  314. inst->alg.setkey = crypto_cts_setkey;
  315. inst->alg.encrypt = crypto_cts_encrypt;
  316. inst->alg.decrypt = crypto_cts_decrypt;
  317. inst->free = crypto_cts_free;
  318. err = skcipher_register_instance(tmpl, inst);
  319. if (err)
  320. goto err_drop_spawn;
  321. out:
  322. return err;
  323. err_drop_spawn:
  324. crypto_drop_skcipher(spawn);
  325. err_free_inst:
  326. kfree(inst);
  327. goto out;
  328. }
  329. static struct crypto_template crypto_cts_tmpl = {
  330. .name = "cts",
  331. .create = crypto_cts_create,
  332. .module = THIS_MODULE,
  333. };
  334. static int __init crypto_cts_module_init(void)
  335. {
  336. return crypto_register_template(&crypto_cts_tmpl);
  337. }
  338. static void __exit crypto_cts_module_exit(void)
  339. {
  340. crypto_unregister_template(&crypto_cts_tmpl);
  341. }
  342. module_init(crypto_cts_module_init);
  343. module_exit(crypto_cts_module_exit);
  344. MODULE_LICENSE("Dual BSD/GPL");
  345. MODULE_DESCRIPTION("CTS-CBC CipherText Stealing for CBC");
  346. MODULE_ALIAS_CRYPTO("cts");