xts.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /* XTS: as defined in IEEE1619/D16
  2. * http://grouper.ieee.org/groups/1619/email/pdf00086.pdf
  3. * (sector sizes which are not a multiple of 16 bytes are,
  4. * however currently unsupported)
  5. *
  6. * Copyright (c) 2007 Rik Snel <rsnel@cube.dyndns.org>
  7. *
  8. * Based on ecb.c
  9. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. */
  16. #include <crypto/internal/skcipher.h>
  17. #include <crypto/scatterwalk.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/slab.h>
  24. #include <crypto/xts.h>
  25. #include <crypto/b128ops.h>
  26. #include <crypto/gf128mul.h>
  27. #define XTS_BUFFER_SIZE 128u
  28. struct priv {
  29. struct crypto_skcipher *child;
  30. struct crypto_cipher *tweak;
  31. };
  32. struct xts_instance_ctx {
  33. struct crypto_skcipher_spawn spawn;
  34. char name[CRYPTO_MAX_ALG_NAME];
  35. };
  36. struct rctx {
  37. le128 buf[XTS_BUFFER_SIZE / sizeof(le128)];
  38. le128 t;
  39. le128 *ext;
  40. struct scatterlist srcbuf[2];
  41. struct scatterlist dstbuf[2];
  42. struct scatterlist *src;
  43. struct scatterlist *dst;
  44. unsigned int left;
  45. struct skcipher_request subreq;
  46. };
  47. static int setkey(struct crypto_skcipher *parent, const u8 *key,
  48. unsigned int keylen)
  49. {
  50. struct priv *ctx = crypto_skcipher_ctx(parent);
  51. struct crypto_skcipher *child;
  52. struct crypto_cipher *tweak;
  53. int err;
  54. err = xts_verify_key(parent, key, keylen);
  55. if (err)
  56. return err;
  57. keylen /= 2;
  58. /* we need two cipher instances: one to compute the initial 'tweak'
  59. * by encrypting the IV (usually the 'plain' iv) and the other
  60. * one to encrypt and decrypt the data */
  61. /* tweak cipher, uses Key2 i.e. the second half of *key */
  62. tweak = ctx->tweak;
  63. crypto_cipher_clear_flags(tweak, CRYPTO_TFM_REQ_MASK);
  64. crypto_cipher_set_flags(tweak, crypto_skcipher_get_flags(parent) &
  65. CRYPTO_TFM_REQ_MASK);
  66. err = crypto_cipher_setkey(tweak, key + keylen, keylen);
  67. crypto_skcipher_set_flags(parent, crypto_cipher_get_flags(tweak) &
  68. CRYPTO_TFM_RES_MASK);
  69. if (err)
  70. return err;
  71. /* data cipher, uses Key1 i.e. the first half of *key */
  72. child = ctx->child;
  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 int post_crypt(struct skcipher_request *req)
  82. {
  83. struct rctx *rctx = skcipher_request_ctx(req);
  84. le128 *buf = rctx->ext ?: rctx->buf;
  85. struct skcipher_request *subreq;
  86. const int bs = XTS_BLOCK_SIZE;
  87. struct skcipher_walk w;
  88. struct scatterlist *sg;
  89. unsigned offset;
  90. int err;
  91. subreq = &rctx->subreq;
  92. err = skcipher_walk_virt(&w, subreq, false);
  93. while (w.nbytes) {
  94. unsigned int avail = w.nbytes;
  95. le128 *wdst;
  96. wdst = w.dst.virt.addr;
  97. do {
  98. le128_xor(wdst, buf++, wdst);
  99. wdst++;
  100. } while ((avail -= bs) >= bs);
  101. err = skcipher_walk_done(&w, avail);
  102. }
  103. rctx->left -= subreq->cryptlen;
  104. if (err || !rctx->left)
  105. goto out;
  106. rctx->dst = rctx->dstbuf;
  107. scatterwalk_done(&w.out, 0, 1);
  108. sg = w.out.sg;
  109. offset = w.out.offset;
  110. if (rctx->dst != sg) {
  111. rctx->dst[0] = *sg;
  112. sg_unmark_end(rctx->dst);
  113. scatterwalk_crypto_chain(rctx->dst, sg_next(sg), 2);
  114. }
  115. rctx->dst[0].length -= offset - sg->offset;
  116. rctx->dst[0].offset = offset;
  117. out:
  118. return err;
  119. }
  120. static int pre_crypt(struct skcipher_request *req)
  121. {
  122. struct rctx *rctx = skcipher_request_ctx(req);
  123. le128 *buf = rctx->ext ?: rctx->buf;
  124. struct skcipher_request *subreq;
  125. const int bs = XTS_BLOCK_SIZE;
  126. struct skcipher_walk w;
  127. struct scatterlist *sg;
  128. unsigned cryptlen;
  129. unsigned offset;
  130. bool more;
  131. int err;
  132. subreq = &rctx->subreq;
  133. cryptlen = subreq->cryptlen;
  134. more = rctx->left > cryptlen;
  135. if (!more)
  136. cryptlen = rctx->left;
  137. skcipher_request_set_crypt(subreq, rctx->src, rctx->dst,
  138. cryptlen, NULL);
  139. err = skcipher_walk_virt(&w, subreq, false);
  140. while (w.nbytes) {
  141. unsigned int avail = w.nbytes;
  142. le128 *wsrc;
  143. le128 *wdst;
  144. wsrc = w.src.virt.addr;
  145. wdst = w.dst.virt.addr;
  146. do {
  147. *buf++ = rctx->t;
  148. le128_xor(wdst++, &rctx->t, wsrc++);
  149. gf128mul_x_ble(&rctx->t, &rctx->t);
  150. } while ((avail -= bs) >= bs);
  151. err = skcipher_walk_done(&w, avail);
  152. }
  153. skcipher_request_set_crypt(subreq, rctx->dst, rctx->dst,
  154. cryptlen, NULL);
  155. if (err || !more)
  156. goto out;
  157. rctx->src = rctx->srcbuf;
  158. scatterwalk_done(&w.in, 0, 1);
  159. sg = w.in.sg;
  160. offset = w.in.offset;
  161. if (rctx->src != sg) {
  162. rctx->src[0] = *sg;
  163. sg_unmark_end(rctx->src);
  164. scatterwalk_crypto_chain(rctx->src, sg_next(sg), 2);
  165. }
  166. rctx->src[0].length -= offset - sg->offset;
  167. rctx->src[0].offset = offset;
  168. out:
  169. return err;
  170. }
  171. static int init_crypt(struct skcipher_request *req, crypto_completion_t done)
  172. {
  173. struct priv *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
  174. struct rctx *rctx = skcipher_request_ctx(req);
  175. struct skcipher_request *subreq;
  176. gfp_t gfp;
  177. subreq = &rctx->subreq;
  178. skcipher_request_set_tfm(subreq, ctx->child);
  179. skcipher_request_set_callback(subreq, req->base.flags, done, req);
  180. gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  181. GFP_ATOMIC;
  182. rctx->ext = NULL;
  183. subreq->cryptlen = XTS_BUFFER_SIZE;
  184. if (req->cryptlen > XTS_BUFFER_SIZE) {
  185. unsigned int n = min(req->cryptlen, (unsigned int)PAGE_SIZE);
  186. rctx->ext = kmalloc(n, gfp);
  187. if (rctx->ext)
  188. subreq->cryptlen = n;
  189. }
  190. rctx->src = req->src;
  191. rctx->dst = req->dst;
  192. rctx->left = req->cryptlen;
  193. /* calculate first value of T */
  194. crypto_cipher_encrypt_one(ctx->tweak, (u8 *)&rctx->t, req->iv);
  195. return 0;
  196. }
  197. static void exit_crypt(struct skcipher_request *req)
  198. {
  199. struct rctx *rctx = skcipher_request_ctx(req);
  200. rctx->left = 0;
  201. if (rctx->ext)
  202. kzfree(rctx->ext);
  203. }
  204. static int do_encrypt(struct skcipher_request *req, int err)
  205. {
  206. struct rctx *rctx = skcipher_request_ctx(req);
  207. struct skcipher_request *subreq;
  208. subreq = &rctx->subreq;
  209. while (!err && rctx->left) {
  210. err = pre_crypt(req) ?:
  211. crypto_skcipher_encrypt(subreq) ?:
  212. post_crypt(req);
  213. if (err == -EINPROGRESS || err == -EBUSY)
  214. return err;
  215. }
  216. exit_crypt(req);
  217. return err;
  218. }
  219. static void encrypt_done(struct crypto_async_request *areq, int err)
  220. {
  221. struct skcipher_request *req = areq->data;
  222. struct skcipher_request *subreq;
  223. struct rctx *rctx;
  224. rctx = skcipher_request_ctx(req);
  225. if (err == -EINPROGRESS) {
  226. if (rctx->left != req->cryptlen)
  227. return;
  228. goto out;
  229. }
  230. subreq = &rctx->subreq;
  231. subreq->base.flags &= CRYPTO_TFM_REQ_MAY_BACKLOG;
  232. err = do_encrypt(req, err ?: post_crypt(req));
  233. if (rctx->left)
  234. return;
  235. out:
  236. skcipher_request_complete(req, err);
  237. }
  238. static int encrypt(struct skcipher_request *req)
  239. {
  240. return do_encrypt(req, init_crypt(req, encrypt_done));
  241. }
  242. static int do_decrypt(struct skcipher_request *req, int err)
  243. {
  244. struct rctx *rctx = skcipher_request_ctx(req);
  245. struct skcipher_request *subreq;
  246. subreq = &rctx->subreq;
  247. while (!err && rctx->left) {
  248. err = pre_crypt(req) ?:
  249. crypto_skcipher_decrypt(subreq) ?:
  250. post_crypt(req);
  251. if (err == -EINPROGRESS || err == -EBUSY)
  252. return err;
  253. }
  254. exit_crypt(req);
  255. return err;
  256. }
  257. static void decrypt_done(struct crypto_async_request *areq, int err)
  258. {
  259. struct skcipher_request *req = areq->data;
  260. struct skcipher_request *subreq;
  261. struct rctx *rctx;
  262. rctx = skcipher_request_ctx(req);
  263. if (err == -EINPROGRESS) {
  264. if (rctx->left != req->cryptlen)
  265. return;
  266. goto out;
  267. }
  268. subreq = &rctx->subreq;
  269. subreq->base.flags &= CRYPTO_TFM_REQ_MAY_BACKLOG;
  270. err = do_decrypt(req, err ?: post_crypt(req));
  271. if (rctx->left)
  272. return;
  273. out:
  274. skcipher_request_complete(req, err);
  275. }
  276. static int decrypt(struct skcipher_request *req)
  277. {
  278. return do_decrypt(req, init_crypt(req, decrypt_done));
  279. }
  280. static int init_tfm(struct crypto_skcipher *tfm)
  281. {
  282. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  283. struct xts_instance_ctx *ictx = skcipher_instance_ctx(inst);
  284. struct priv *ctx = crypto_skcipher_ctx(tfm);
  285. struct crypto_skcipher *child;
  286. struct crypto_cipher *tweak;
  287. child = crypto_spawn_skcipher(&ictx->spawn);
  288. if (IS_ERR(child))
  289. return PTR_ERR(child);
  290. ctx->child = child;
  291. tweak = crypto_alloc_cipher(ictx->name, 0, 0);
  292. if (IS_ERR(tweak)) {
  293. crypto_free_skcipher(ctx->child);
  294. return PTR_ERR(tweak);
  295. }
  296. ctx->tweak = tweak;
  297. crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(child) +
  298. sizeof(struct rctx));
  299. return 0;
  300. }
  301. static void exit_tfm(struct crypto_skcipher *tfm)
  302. {
  303. struct priv *ctx = crypto_skcipher_ctx(tfm);
  304. crypto_free_skcipher(ctx->child);
  305. crypto_free_cipher(ctx->tweak);
  306. }
  307. static void free(struct skcipher_instance *inst)
  308. {
  309. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  310. kfree(inst);
  311. }
  312. static int create(struct crypto_template *tmpl, struct rtattr **tb)
  313. {
  314. struct skcipher_instance *inst;
  315. struct crypto_attr_type *algt;
  316. struct xts_instance_ctx *ctx;
  317. struct skcipher_alg *alg;
  318. const char *cipher_name;
  319. u32 mask;
  320. int err;
  321. algt = crypto_get_attr_type(tb);
  322. if (IS_ERR(algt))
  323. return PTR_ERR(algt);
  324. if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
  325. return -EINVAL;
  326. cipher_name = crypto_attr_alg_name(tb[1]);
  327. if (IS_ERR(cipher_name))
  328. return PTR_ERR(cipher_name);
  329. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  330. if (!inst)
  331. return -ENOMEM;
  332. ctx = skcipher_instance_ctx(inst);
  333. crypto_set_skcipher_spawn(&ctx->spawn, skcipher_crypto_instance(inst));
  334. mask = crypto_requires_off(algt->type, algt->mask,
  335. CRYPTO_ALG_NEED_FALLBACK |
  336. CRYPTO_ALG_ASYNC);
  337. err = crypto_grab_skcipher(&ctx->spawn, cipher_name, 0, mask);
  338. if (err == -ENOENT) {
  339. err = -ENAMETOOLONG;
  340. if (snprintf(ctx->name, CRYPTO_MAX_ALG_NAME, "ecb(%s)",
  341. cipher_name) >= CRYPTO_MAX_ALG_NAME)
  342. goto err_free_inst;
  343. err = crypto_grab_skcipher(&ctx->spawn, ctx->name, 0, mask);
  344. }
  345. if (err)
  346. goto err_free_inst;
  347. alg = crypto_skcipher_spawn_alg(&ctx->spawn);
  348. err = -EINVAL;
  349. if (alg->base.cra_blocksize != XTS_BLOCK_SIZE)
  350. goto err_drop_spawn;
  351. if (crypto_skcipher_alg_ivsize(alg))
  352. goto err_drop_spawn;
  353. err = crypto_inst_setname(skcipher_crypto_instance(inst), "xts",
  354. &alg->base);
  355. if (err)
  356. goto err_drop_spawn;
  357. err = -EINVAL;
  358. cipher_name = alg->base.cra_name;
  359. /* Alas we screwed up the naming so we have to mangle the
  360. * cipher name.
  361. */
  362. if (!strncmp(cipher_name, "ecb(", 4)) {
  363. unsigned len;
  364. len = strlcpy(ctx->name, cipher_name + 4, sizeof(ctx->name));
  365. if (len < 2 || len >= sizeof(ctx->name))
  366. goto err_drop_spawn;
  367. if (ctx->name[len - 1] != ')')
  368. goto err_drop_spawn;
  369. ctx->name[len - 1] = 0;
  370. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  371. "xts(%s)", ctx->name) >= CRYPTO_MAX_ALG_NAME) {
  372. err = -ENAMETOOLONG;
  373. goto err_drop_spawn;
  374. }
  375. } else
  376. goto err_drop_spawn;
  377. inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  378. inst->alg.base.cra_priority = alg->base.cra_priority;
  379. inst->alg.base.cra_blocksize = XTS_BLOCK_SIZE;
  380. inst->alg.base.cra_alignmask = alg->base.cra_alignmask |
  381. (__alignof__(u64) - 1);
  382. inst->alg.ivsize = XTS_BLOCK_SIZE;
  383. inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) * 2;
  384. inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) * 2;
  385. inst->alg.base.cra_ctxsize = sizeof(struct priv);
  386. inst->alg.init = init_tfm;
  387. inst->alg.exit = exit_tfm;
  388. inst->alg.setkey = setkey;
  389. inst->alg.encrypt = encrypt;
  390. inst->alg.decrypt = decrypt;
  391. inst->free = free;
  392. err = skcipher_register_instance(tmpl, inst);
  393. if (err)
  394. goto err_drop_spawn;
  395. out:
  396. return err;
  397. err_drop_spawn:
  398. crypto_drop_skcipher(&ctx->spawn);
  399. err_free_inst:
  400. kfree(inst);
  401. goto out;
  402. }
  403. static struct crypto_template crypto_tmpl = {
  404. .name = "xts",
  405. .create = create,
  406. .module = THIS_MODULE,
  407. };
  408. static int __init crypto_module_init(void)
  409. {
  410. return crypto_register_template(&crypto_tmpl);
  411. }
  412. static void __exit crypto_module_exit(void)
  413. {
  414. crypto_unregister_template(&crypto_tmpl);
  415. }
  416. module_init(crypto_module_init);
  417. module_exit(crypto_module_exit);
  418. MODULE_LICENSE("GPL");
  419. MODULE_DESCRIPTION("XTS block cipher mode");
  420. MODULE_ALIAS_CRYPTO("xts");