cts.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. unsigned int offset;
  133. skcipher_request_set_tfm(subreq, ctx->child);
  134. if (nbytes < bsize)
  135. return -EINVAL;
  136. if (nbytes == bsize) {
  137. skcipher_request_set_callback(subreq, req->base.flags,
  138. req->base.complete,
  139. req->base.data);
  140. skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes,
  141. req->iv);
  142. return crypto_skcipher_encrypt(subreq);
  143. }
  144. offset = rounddown(nbytes - 1, bsize);
  145. rctx->offset = offset;
  146. skcipher_request_set_callback(subreq, req->base.flags,
  147. crypto_cts_encrypt_done, req);
  148. skcipher_request_set_crypt(subreq, req->src, req->dst,
  149. offset, req->iv);
  150. return crypto_skcipher_encrypt(subreq) ?:
  151. cts_cbc_encrypt(req);
  152. }
  153. static int cts_cbc_decrypt(struct skcipher_request *req)
  154. {
  155. struct crypto_cts_reqctx *rctx = skcipher_request_ctx(req);
  156. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  157. struct skcipher_request *subreq = &rctx->subreq;
  158. int bsize = crypto_skcipher_blocksize(tfm);
  159. u8 d[MAX_CIPHER_BLOCKSIZE * 2] __aligned(__alignof__(u32));
  160. struct scatterlist *sg;
  161. unsigned int offset;
  162. u8 *space;
  163. int lastn;
  164. offset = rctx->offset;
  165. lastn = req->cryptlen - offset;
  166. sg = scatterwalk_ffwd(rctx->sg, req->dst, offset - bsize);
  167. /* 1. Decrypt Cn-1 (s) to create Dn */
  168. scatterwalk_map_and_copy(d + bsize, sg, 0, bsize, 0);
  169. space = crypto_cts_reqctx_space(req);
  170. crypto_xor(d + bsize, space, bsize);
  171. /* 2. Pad Cn with zeros at the end to create C of length BB */
  172. memset(d, 0, bsize);
  173. scatterwalk_map_and_copy(d, req->src, offset, lastn, 0);
  174. /* 3. Exclusive-or Dn with C to create Xn */
  175. /* 4. Select the first Ln bytes of Xn to create Pn */
  176. crypto_xor(d + bsize, d, lastn);
  177. /* 5. Append the tail (BB - Ln) bytes of Xn to Cn to create En */
  178. memcpy(d + lastn, d + bsize + lastn, bsize - lastn);
  179. /* 6. Decrypt En to create Pn-1 */
  180. scatterwalk_map_and_copy(d, sg, 0, bsize + lastn, 1);
  181. memzero_explicit(d, sizeof(d));
  182. skcipher_request_set_callback(subreq, req->base.flags &
  183. CRYPTO_TFM_REQ_MAY_BACKLOG,
  184. cts_cbc_crypt_done, req);
  185. skcipher_request_set_crypt(subreq, sg, sg, bsize, space);
  186. return crypto_skcipher_decrypt(subreq);
  187. }
  188. static void crypto_cts_decrypt_done(struct crypto_async_request *areq, int err)
  189. {
  190. struct skcipher_request *req = areq->data;
  191. if (err)
  192. goto out;
  193. err = cts_cbc_decrypt(req);
  194. if (err == -EINPROGRESS || err == -EBUSY)
  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. unsigned int offset;
  208. u8 *space;
  209. skcipher_request_set_tfm(subreq, ctx->child);
  210. if (nbytes < bsize)
  211. return -EINVAL;
  212. if (nbytes == bsize) {
  213. skcipher_request_set_callback(subreq, req->base.flags,
  214. req->base.complete,
  215. req->base.data);
  216. skcipher_request_set_crypt(subreq, req->src, req->dst, nbytes,
  217. req->iv);
  218. return crypto_skcipher_decrypt(subreq);
  219. }
  220. skcipher_request_set_callback(subreq, req->base.flags,
  221. crypto_cts_decrypt_done, req);
  222. space = crypto_cts_reqctx_space(req);
  223. offset = rounddown(nbytes - 1, bsize);
  224. rctx->offset = offset;
  225. if (offset <= bsize)
  226. memcpy(space, req->iv, bsize);
  227. else
  228. scatterwalk_map_and_copy(space, req->src, offset - 2 * bsize,
  229. bsize, 0);
  230. skcipher_request_set_crypt(subreq, req->src, req->dst,
  231. offset, req->iv);
  232. return crypto_skcipher_decrypt(subreq) ?:
  233. cts_cbc_decrypt(req);
  234. }
  235. static int crypto_cts_init_tfm(struct crypto_skcipher *tfm)
  236. {
  237. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  238. struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
  239. struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
  240. struct crypto_skcipher *cipher;
  241. unsigned reqsize;
  242. unsigned bsize;
  243. unsigned align;
  244. cipher = crypto_spawn_skcipher(spawn);
  245. if (IS_ERR(cipher))
  246. return PTR_ERR(cipher);
  247. ctx->child = cipher;
  248. align = crypto_skcipher_alignmask(tfm);
  249. bsize = crypto_skcipher_blocksize(cipher);
  250. reqsize = ALIGN(sizeof(struct crypto_cts_reqctx) +
  251. crypto_skcipher_reqsize(cipher),
  252. crypto_tfm_ctx_alignment()) +
  253. (align & ~(crypto_tfm_ctx_alignment() - 1)) + bsize;
  254. crypto_skcipher_set_reqsize(tfm, reqsize);
  255. return 0;
  256. }
  257. static void crypto_cts_exit_tfm(struct crypto_skcipher *tfm)
  258. {
  259. struct crypto_cts_ctx *ctx = crypto_skcipher_ctx(tfm);
  260. crypto_free_skcipher(ctx->child);
  261. }
  262. static void crypto_cts_free(struct skcipher_instance *inst)
  263. {
  264. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  265. kfree(inst);
  266. }
  267. static int crypto_cts_create(struct crypto_template *tmpl, struct rtattr **tb)
  268. {
  269. struct crypto_skcipher_spawn *spawn;
  270. struct skcipher_instance *inst;
  271. struct crypto_attr_type *algt;
  272. struct skcipher_alg *alg;
  273. const char *cipher_name;
  274. int err;
  275. algt = crypto_get_attr_type(tb);
  276. if (IS_ERR(algt))
  277. return PTR_ERR(algt);
  278. if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
  279. return -EINVAL;
  280. cipher_name = crypto_attr_alg_name(tb[1]);
  281. if (IS_ERR(cipher_name))
  282. return PTR_ERR(cipher_name);
  283. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  284. if (!inst)
  285. return -ENOMEM;
  286. spawn = skcipher_instance_ctx(inst);
  287. crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
  288. err = crypto_grab_skcipher(spawn, cipher_name, 0,
  289. crypto_requires_sync(algt->type,
  290. algt->mask));
  291. if (err)
  292. goto err_free_inst;
  293. alg = crypto_spawn_skcipher_alg(spawn);
  294. err = -EINVAL;
  295. if (crypto_skcipher_alg_ivsize(alg) != alg->base.cra_blocksize)
  296. goto err_drop_spawn;
  297. if (strncmp(alg->base.cra_name, "cbc(", 4))
  298. goto err_drop_spawn;
  299. err = crypto_inst_setname(skcipher_crypto_instance(inst), "cts",
  300. &alg->base);
  301. if (err)
  302. goto err_drop_spawn;
  303. inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  304. inst->alg.base.cra_priority = alg->base.cra_priority;
  305. inst->alg.base.cra_blocksize = alg->base.cra_blocksize;
  306. inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
  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. subsys_initcall(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");