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/algapi.h>
  41. #include <crypto/internal/skcipher.h>
  42. #include <linux/err.h>
  43. #include <linux/init.h>
  44. #include <linux/kernel.h>
  45. #include <linux/log2.h>
  46. #include <linux/module.h>
  47. #include <linux/scatterlist.h>
  48. #include <crypto/scatterwalk.h>
  49. #include <linux/slab.h>
  50. #include <linux/compiler.h>
  51. struct crypto_cts_ctx {
  52. struct crypto_skcipher *child;
  53. };
  54. struct crypto_cts_reqctx {
  55. struct scatterlist sg[2];
  56. unsigned offset;
  57. struct skcipher_request subreq;
  58. };
  59. static inline u8 *crypto_cts_reqctx_space(struct skcipher_request *req)
  60. {
  61. struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
  62. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  63. struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
  64. struct crypto_skcipher *child = ctx->child;
  65. return PTR_ALIGN((u8 *)(rctx + 1) + crypto_skcipher_reqsize(child),
  66. crypto_skcipher_alignmask(tfm) + 1);
  67. }
  68. static int crypto_cts_setkey(struct crypto_skcipher *parent, const u8 *key,
  69. unsigned int keylen)
  70. {
  71. struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(parent);
  72. struct crypto_skcipher *child = ctx->child;
  73. int err;
  74. crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  75. crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
  76. CRYPTO_TFM_REQ_MASK);
  77. err = crypto_skcipher_setkey(child, key, keylen);
  78. crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
  79. CRYPTO_TFM_RES_MASK);
  80. return err;
  81. }
  82. static void cts_cbc_crypt_done(struct crypto_async_request *areq, int err)
  83. {
  84. struct skcipher_request *req = areq->data;
  85. if (err == -EINPROGRESS)
  86. return;
  87. skcipher_request_complete(req, err);
  88. }
  89. static int cts_cbc_encrypt(struct skcipher_request *req)
  90. {
  91. struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
  92. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  93. struct skcipher_request *subreq = &rctx->subreq;
  94. int bsize = crypto_skcipher_blocksize(tfm);
  95. u8 d[MAX_CIPHER_BLOCKSIZE * 2] __aligned(__alignof__(u32));
  96. struct scatterlist *sg;
  97. unsigned int offset;
  98. int lastn;
  99. offset = rctx->offset;
  100. lastn = req->cryptlen - offset;
  101. sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize);
  102. scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0);
  103. memset(d, 0, bsize);
  104. scatterwalk_map_and_copy(d, req->src, offset, lastn, 0);
  105. scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1);
  106. memzero_explicit(d, sizeof(d));
  107. skcipher_request_set_callback(subreq, req->base.flags &
  108. CRYPTO_TFM_REQ_MAY_BACKLOG,
  109. cts_cbc_crypt_done, req);
  110. skcipher_request_set_crypt(subreq, sg, sg, bsize, req->iv);
  111. return crypto_skcipher_encrypt(subreq);
  112. }
  113. static void crypto_cts_encrypt_done(struct crypto_async_request *areq, int err)
  114. {
  115. struct skcipher_request *req = areq->data;
  116. if (err)
  117. goto out;
  118. err = cts_cbc_encrypt(req);
  119. if (err == -EINPROGRESS || err == -EBUSY)
  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[MAX_CIPHER_BLOCKSIZE * 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 || err == -EBUSY)
  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_skcipher(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_skcipher(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. inst->alg.ivsize = alg->base.cra_blocksize;
  306. inst->alg.chunksize = crypto_skcipher_alg_chunksize(alg);
  307. inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg);
  308. inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg);
  309. inst->alg.base.cra_ctxsize = sizeof(struct crypto_cts_ctx);
  310. inst->alg.init = crypto_cts_init_tfm;
  311. inst->alg.exit = crypto_cts_exit_tfm;
  312. inst->alg.setkey = crypto_cts_setkey;
  313. inst->alg.encrypt = crypto_cts_encrypt;
  314. inst->alg.decrypt = crypto_cts_decrypt;
  315. inst->free = crypto_cts_free;
  316. err = skcipher_register_instance(tmpl, inst);
  317. if (err)
  318. goto err_drop_spawn;
  319. out:
  320. return err;
  321. err_drop_spawn:
  322. crypto_drop_skcipher(spawn);
  323. err_free_inst:
  324. kfree(inst);
  325. goto out;
  326. }
  327. static struct crypto_template crypto_cts_tmpl = {
  328. .name = "cts",
  329. .create = crypto_cts_create,
  330. .module = THIS_MODULE,
  331. };
  332. static int __init crypto_cts_module_init(void)
  333. {
  334. return crypto_register_template(&crypto_cts_tmpl);
  335. }
  336. static void __exit crypto_cts_module_exit(void)
  337. {
  338. crypto_unregister_template(&crypto_cts_tmpl);
  339. }
  340. module_init(crypto_cts_module_init);
  341. module_exit(crypto_cts_module_exit);
  342. MODULE_LICENSE("Dual BSD/GPL");
  343. MODULE_DESCRIPTION("CTS-CBC CipherText Stealing for CBC");
  344. MODULE_ALIAS_CRYPTO("cts");