xts.c 14 KB

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