cts.c 12 KB

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