xts.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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), 0, 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), 0, 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 ||
  214. (err == -EBUSY &&
  215. req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  216. return err;
  217. }
  218. exit_crypt(req);
  219. return err;
  220. }
  221. static void encrypt_done(struct crypto_async_request *areq, int err)
  222. {
  223. struct skcipher_request *req = areq->data;
  224. struct skcipher_request *subreq;
  225. struct rctx *rctx;
  226. rctx = skcipher_request_ctx(req);
  227. if (err == -EINPROGRESS) {
  228. if (rctx->left != req->cryptlen)
  229. return;
  230. goto out;
  231. }
  232. subreq = &rctx->subreq;
  233. subreq->base.flags &= CRYPTO_TFM_REQ_MAY_BACKLOG;
  234. err = do_encrypt(req, err ?: post_crypt(req));
  235. if (rctx->left)
  236. return;
  237. out:
  238. skcipher_request_complete(req, err);
  239. }
  240. static int encrypt(struct skcipher_request *req)
  241. {
  242. return do_encrypt(req, init_crypt(req, encrypt_done));
  243. }
  244. static int do_decrypt(struct skcipher_request *req, int err)
  245. {
  246. struct rctx *rctx = skcipher_request_ctx(req);
  247. struct skcipher_request *subreq;
  248. subreq = &rctx->subreq;
  249. while (!err && rctx->left) {
  250. err = pre_crypt(req) ?:
  251. crypto_skcipher_decrypt(subreq) ?:
  252. post_crypt(req);
  253. if (err == -EINPROGRESS ||
  254. (err == -EBUSY &&
  255. req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  256. return err;
  257. }
  258. exit_crypt(req);
  259. return err;
  260. }
  261. static void decrypt_done(struct crypto_async_request *areq, int err)
  262. {
  263. struct skcipher_request *req = areq->data;
  264. struct skcipher_request *subreq;
  265. struct rctx *rctx;
  266. rctx = skcipher_request_ctx(req);
  267. if (err == -EINPROGRESS) {
  268. if (rctx->left != req->cryptlen)
  269. return;
  270. goto out;
  271. }
  272. subreq = &rctx->subreq;
  273. subreq->base.flags &= CRYPTO_TFM_REQ_MAY_BACKLOG;
  274. err = do_decrypt(req, err ?: post_crypt(req));
  275. if (rctx->left)
  276. return;
  277. out:
  278. skcipher_request_complete(req, err);
  279. }
  280. static int decrypt(struct skcipher_request *req)
  281. {
  282. return do_decrypt(req, init_crypt(req, decrypt_done));
  283. }
  284. int xts_crypt(struct blkcipher_desc *desc, struct scatterlist *sdst,
  285. struct scatterlist *ssrc, unsigned int nbytes,
  286. struct xts_crypt_req *req)
  287. {
  288. const unsigned int bsize = XTS_BLOCK_SIZE;
  289. const unsigned int max_blks = req->tbuflen / bsize;
  290. struct blkcipher_walk walk;
  291. unsigned int nblocks;
  292. le128 *src, *dst, *t;
  293. le128 *t_buf = req->tbuf;
  294. int err, i;
  295. BUG_ON(max_blks < 1);
  296. blkcipher_walk_init(&walk, sdst, ssrc, nbytes);
  297. err = blkcipher_walk_virt(desc, &walk);
  298. nbytes = walk.nbytes;
  299. if (!nbytes)
  300. return err;
  301. nblocks = min(nbytes / bsize, max_blks);
  302. src = (le128 *)walk.src.virt.addr;
  303. dst = (le128 *)walk.dst.virt.addr;
  304. /* calculate first value of T */
  305. req->tweak_fn(req->tweak_ctx, (u8 *)&t_buf[0], walk.iv);
  306. i = 0;
  307. goto first;
  308. for (;;) {
  309. do {
  310. for (i = 0; i < nblocks; i++) {
  311. gf128mul_x_ble(&t_buf[i], t);
  312. first:
  313. t = &t_buf[i];
  314. /* PP <- T xor P */
  315. le128_xor(dst + i, t, src + i);
  316. }
  317. /* CC <- E(Key2,PP) */
  318. req->crypt_fn(req->crypt_ctx, (u8 *)dst,
  319. nblocks * bsize);
  320. /* C <- T xor CC */
  321. for (i = 0; i < nblocks; i++)
  322. le128_xor(dst + i, dst + i, &t_buf[i]);
  323. src += nblocks;
  324. dst += nblocks;
  325. nbytes -= nblocks * bsize;
  326. nblocks = min(nbytes / bsize, max_blks);
  327. } while (nblocks > 0);
  328. *(le128 *)walk.iv = *t;
  329. err = blkcipher_walk_done(desc, &walk, nbytes);
  330. nbytes = walk.nbytes;
  331. if (!nbytes)
  332. break;
  333. nblocks = min(nbytes / bsize, max_blks);
  334. src = (le128 *)walk.src.virt.addr;
  335. dst = (le128 *)walk.dst.virt.addr;
  336. }
  337. return err;
  338. }
  339. EXPORT_SYMBOL_GPL(xts_crypt);
  340. static int init_tfm(struct crypto_skcipher *tfm)
  341. {
  342. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  343. struct xts_instance_ctx *ictx = skcipher_instance_ctx(inst);
  344. struct priv *ctx = crypto_skcipher_ctx(tfm);
  345. struct crypto_skcipher *child;
  346. struct crypto_cipher *tweak;
  347. child = crypto_spawn_skcipher(&ictx->spawn);
  348. if (IS_ERR(child))
  349. return PTR_ERR(child);
  350. ctx->child = child;
  351. tweak = crypto_alloc_cipher(ictx->name, 0, 0);
  352. if (IS_ERR(tweak)) {
  353. crypto_free_skcipher(ctx->child);
  354. return PTR_ERR(tweak);
  355. }
  356. ctx->tweak = tweak;
  357. crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(child) +
  358. sizeof(struct rctx));
  359. return 0;
  360. }
  361. static void exit_tfm(struct crypto_skcipher *tfm)
  362. {
  363. struct priv *ctx = crypto_skcipher_ctx(tfm);
  364. crypto_free_skcipher(ctx->child);
  365. crypto_free_cipher(ctx->tweak);
  366. }
  367. static void free(struct skcipher_instance *inst)
  368. {
  369. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  370. kfree(inst);
  371. }
  372. static int create(struct crypto_template *tmpl, struct rtattr **tb)
  373. {
  374. struct skcipher_instance *inst;
  375. struct crypto_attr_type *algt;
  376. struct xts_instance_ctx *ctx;
  377. struct skcipher_alg *alg;
  378. const char *cipher_name;
  379. u32 mask;
  380. int err;
  381. algt = crypto_get_attr_type(tb);
  382. if (IS_ERR(algt))
  383. return PTR_ERR(algt);
  384. if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
  385. return -EINVAL;
  386. cipher_name = crypto_attr_alg_name(tb[1]);
  387. if (IS_ERR(cipher_name))
  388. return PTR_ERR(cipher_name);
  389. inst = kzalloc(sizeof(*inst) + sizeof(*ctx), GFP_KERNEL);
  390. if (!inst)
  391. return -ENOMEM;
  392. ctx = skcipher_instance_ctx(inst);
  393. crypto_set_skcipher_spawn(&ctx->spawn, skcipher_crypto_instance(inst));
  394. mask = crypto_requires_off(algt->type, algt->mask,
  395. CRYPTO_ALG_NEED_FALLBACK |
  396. CRYPTO_ALG_ASYNC);
  397. err = crypto_grab_skcipher(&ctx->spawn, cipher_name, 0, mask);
  398. if (err == -ENOENT) {
  399. err = -ENAMETOOLONG;
  400. if (snprintf(ctx->name, CRYPTO_MAX_ALG_NAME, "ecb(%s)",
  401. cipher_name) >= CRYPTO_MAX_ALG_NAME)
  402. goto err_free_inst;
  403. err = crypto_grab_skcipher(&ctx->spawn, ctx->name, 0, mask);
  404. }
  405. if (err)
  406. goto err_free_inst;
  407. alg = crypto_skcipher_spawn_alg(&ctx->spawn);
  408. err = -EINVAL;
  409. if (alg->base.cra_blocksize != XTS_BLOCK_SIZE)
  410. goto err_drop_spawn;
  411. if (crypto_skcipher_alg_ivsize(alg))
  412. goto err_drop_spawn;
  413. err = crypto_inst_setname(skcipher_crypto_instance(inst), "xts",
  414. &alg->base);
  415. if (err)
  416. goto err_drop_spawn;
  417. err = -EINVAL;
  418. cipher_name = alg->base.cra_name;
  419. /* Alas we screwed up the naming so we have to mangle the
  420. * cipher name.
  421. */
  422. if (!strncmp(cipher_name, "ecb(", 4)) {
  423. unsigned len;
  424. len = strlcpy(ctx->name, cipher_name + 4, sizeof(ctx->name));
  425. if (len < 2 || len >= sizeof(ctx->name))
  426. goto err_drop_spawn;
  427. if (ctx->name[len - 1] != ')')
  428. goto err_drop_spawn;
  429. ctx->name[len - 1] = 0;
  430. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  431. "xts(%s)", ctx->name) >= CRYPTO_MAX_ALG_NAME)
  432. return -ENAMETOOLONG;
  433. } else
  434. goto err_drop_spawn;
  435. inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  436. inst->alg.base.cra_priority = alg->base.cra_priority;
  437. inst->alg.base.cra_blocksize = XTS_BLOCK_SIZE;
  438. inst->alg.base.cra_alignmask = alg->base.cra_alignmask |
  439. (__alignof__(u64) - 1);
  440. inst->alg.ivsize = XTS_BLOCK_SIZE;
  441. inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) * 2;
  442. inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) * 2;
  443. inst->alg.base.cra_ctxsize = sizeof(struct priv);
  444. inst->alg.init = init_tfm;
  445. inst->alg.exit = exit_tfm;
  446. inst->alg.setkey = setkey;
  447. inst->alg.encrypt = encrypt;
  448. inst->alg.decrypt = decrypt;
  449. inst->free = free;
  450. err = skcipher_register_instance(tmpl, inst);
  451. if (err)
  452. goto err_drop_spawn;
  453. out:
  454. return err;
  455. err_drop_spawn:
  456. crypto_drop_skcipher(&ctx->spawn);
  457. err_free_inst:
  458. kfree(inst);
  459. goto out;
  460. }
  461. static struct crypto_template crypto_tmpl = {
  462. .name = "xts",
  463. .create = create,
  464. .module = THIS_MODULE,
  465. };
  466. static int __init crypto_module_init(void)
  467. {
  468. return crypto_register_template(&crypto_tmpl);
  469. }
  470. static void __exit crypto_module_exit(void)
  471. {
  472. crypto_unregister_template(&crypto_tmpl);
  473. }
  474. module_init(crypto_module_init);
  475. module_exit(crypto_module_exit);
  476. MODULE_LICENSE("GPL");
  477. MODULE_DESCRIPTION("XTS block cipher mode");
  478. MODULE_ALIAS_CRYPTO("xts");