xts.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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/algapi.h>
  17. #include <linux/err.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/slab.h>
  23. #include <crypto/xts.h>
  24. #include <crypto/b128ops.h>
  25. #include <crypto/gf128mul.h>
  26. struct priv {
  27. struct crypto_cipher *child;
  28. struct crypto_cipher *tweak;
  29. };
  30. static int setkey(struct crypto_tfm *parent, const u8 *key,
  31. unsigned int keylen)
  32. {
  33. struct priv *ctx = crypto_tfm_ctx(parent);
  34. struct crypto_cipher *child = ctx->tweak;
  35. int err;
  36. err = xts_check_key(parent, key, keylen);
  37. if (err)
  38. return err;
  39. /* we need two cipher instances: one to compute the initial 'tweak'
  40. * by encrypting the IV (usually the 'plain' iv) and the other
  41. * one to encrypt and decrypt the data */
  42. /* tweak cipher, uses Key2 i.e. the second half of *key */
  43. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  44. crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
  45. CRYPTO_TFM_REQ_MASK);
  46. err = crypto_cipher_setkey(child, key + keylen/2, keylen/2);
  47. if (err)
  48. return err;
  49. crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
  50. CRYPTO_TFM_RES_MASK);
  51. child = ctx->child;
  52. /* data cipher, uses Key1 i.e. the first half of *key */
  53. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  54. crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
  55. CRYPTO_TFM_REQ_MASK);
  56. err = crypto_cipher_setkey(child, key, keylen/2);
  57. if (err)
  58. return err;
  59. crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
  60. CRYPTO_TFM_RES_MASK);
  61. return 0;
  62. }
  63. struct sinfo {
  64. be128 *t;
  65. struct crypto_tfm *tfm;
  66. void (*fn)(struct crypto_tfm *, u8 *, const u8 *);
  67. };
  68. static inline void xts_round(struct sinfo *s, void *dst, const void *src)
  69. {
  70. be128_xor(dst, s->t, src); /* PP <- T xor P */
  71. s->fn(s->tfm, dst, dst); /* CC <- E(Key1,PP) */
  72. be128_xor(dst, dst, s->t); /* C <- T xor CC */
  73. }
  74. static int crypt(struct blkcipher_desc *d,
  75. struct blkcipher_walk *w, struct priv *ctx,
  76. void (*tw)(struct crypto_tfm *, u8 *, const u8 *),
  77. void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
  78. {
  79. int err;
  80. unsigned int avail;
  81. const int bs = XTS_BLOCK_SIZE;
  82. struct sinfo s = {
  83. .tfm = crypto_cipher_tfm(ctx->child),
  84. .fn = fn
  85. };
  86. u8 *wsrc;
  87. u8 *wdst;
  88. err = blkcipher_walk_virt(d, w);
  89. if (!w->nbytes)
  90. return err;
  91. s.t = (be128 *)w->iv;
  92. avail = w->nbytes;
  93. wsrc = w->src.virt.addr;
  94. wdst = w->dst.virt.addr;
  95. /* calculate first value of T */
  96. tw(crypto_cipher_tfm(ctx->tweak), w->iv, w->iv);
  97. goto first;
  98. for (;;) {
  99. do {
  100. gf128mul_x_ble(s.t, s.t);
  101. first:
  102. xts_round(&s, wdst, wsrc);
  103. wsrc += bs;
  104. wdst += bs;
  105. } while ((avail -= bs) >= bs);
  106. err = blkcipher_walk_done(d, w, avail);
  107. if (!w->nbytes)
  108. break;
  109. avail = w->nbytes;
  110. wsrc = w->src.virt.addr;
  111. wdst = w->dst.virt.addr;
  112. }
  113. return err;
  114. }
  115. static int encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  116. struct scatterlist *src, unsigned int nbytes)
  117. {
  118. struct priv *ctx = crypto_blkcipher_ctx(desc->tfm);
  119. struct blkcipher_walk w;
  120. blkcipher_walk_init(&w, dst, src, nbytes);
  121. return crypt(desc, &w, ctx, crypto_cipher_alg(ctx->tweak)->cia_encrypt,
  122. crypto_cipher_alg(ctx->child)->cia_encrypt);
  123. }
  124. static int decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  125. struct scatterlist *src, unsigned int nbytes)
  126. {
  127. struct priv *ctx = crypto_blkcipher_ctx(desc->tfm);
  128. struct blkcipher_walk w;
  129. blkcipher_walk_init(&w, dst, src, nbytes);
  130. return crypt(desc, &w, ctx, crypto_cipher_alg(ctx->tweak)->cia_encrypt,
  131. crypto_cipher_alg(ctx->child)->cia_decrypt);
  132. }
  133. int xts_crypt(struct blkcipher_desc *desc, struct scatterlist *sdst,
  134. struct scatterlist *ssrc, unsigned int nbytes,
  135. struct xts_crypt_req *req)
  136. {
  137. const unsigned int bsize = XTS_BLOCK_SIZE;
  138. const unsigned int max_blks = req->tbuflen / bsize;
  139. struct blkcipher_walk walk;
  140. unsigned int nblocks;
  141. be128 *src, *dst, *t;
  142. be128 *t_buf = req->tbuf;
  143. int err, i;
  144. BUG_ON(max_blks < 1);
  145. blkcipher_walk_init(&walk, sdst, ssrc, nbytes);
  146. err = blkcipher_walk_virt(desc, &walk);
  147. nbytes = walk.nbytes;
  148. if (!nbytes)
  149. return err;
  150. nblocks = min(nbytes / bsize, max_blks);
  151. src = (be128 *)walk.src.virt.addr;
  152. dst = (be128 *)walk.dst.virt.addr;
  153. /* calculate first value of T */
  154. req->tweak_fn(req->tweak_ctx, (u8 *)&t_buf[0], walk.iv);
  155. i = 0;
  156. goto first;
  157. for (;;) {
  158. do {
  159. for (i = 0; i < nblocks; i++) {
  160. gf128mul_x_ble(&t_buf[i], t);
  161. first:
  162. t = &t_buf[i];
  163. /* PP <- T xor P */
  164. be128_xor(dst + i, t, src + i);
  165. }
  166. /* CC <- E(Key2,PP) */
  167. req->crypt_fn(req->crypt_ctx, (u8 *)dst,
  168. nblocks * bsize);
  169. /* C <- T xor CC */
  170. for (i = 0; i < nblocks; i++)
  171. be128_xor(dst + i, dst + i, &t_buf[i]);
  172. src += nblocks;
  173. dst += nblocks;
  174. nbytes -= nblocks * bsize;
  175. nblocks = min(nbytes / bsize, max_blks);
  176. } while (nblocks > 0);
  177. *(be128 *)walk.iv = *t;
  178. err = blkcipher_walk_done(desc, &walk, nbytes);
  179. nbytes = walk.nbytes;
  180. if (!nbytes)
  181. break;
  182. nblocks = min(nbytes / bsize, max_blks);
  183. src = (be128 *)walk.src.virt.addr;
  184. dst = (be128 *)walk.dst.virt.addr;
  185. }
  186. return err;
  187. }
  188. EXPORT_SYMBOL_GPL(xts_crypt);
  189. static int init_tfm(struct crypto_tfm *tfm)
  190. {
  191. struct crypto_cipher *cipher;
  192. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  193. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  194. struct priv *ctx = crypto_tfm_ctx(tfm);
  195. u32 *flags = &tfm->crt_flags;
  196. cipher = crypto_spawn_cipher(spawn);
  197. if (IS_ERR(cipher))
  198. return PTR_ERR(cipher);
  199. if (crypto_cipher_blocksize(cipher) != XTS_BLOCK_SIZE) {
  200. *flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  201. crypto_free_cipher(cipher);
  202. return -EINVAL;
  203. }
  204. ctx->child = cipher;
  205. cipher = crypto_spawn_cipher(spawn);
  206. if (IS_ERR(cipher)) {
  207. crypto_free_cipher(ctx->child);
  208. return PTR_ERR(cipher);
  209. }
  210. /* this check isn't really needed, leave it here just in case */
  211. if (crypto_cipher_blocksize(cipher) != XTS_BLOCK_SIZE) {
  212. crypto_free_cipher(cipher);
  213. crypto_free_cipher(ctx->child);
  214. *flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  215. return -EINVAL;
  216. }
  217. ctx->tweak = cipher;
  218. return 0;
  219. }
  220. static void exit_tfm(struct crypto_tfm *tfm)
  221. {
  222. struct priv *ctx = crypto_tfm_ctx(tfm);
  223. crypto_free_cipher(ctx->child);
  224. crypto_free_cipher(ctx->tweak);
  225. }
  226. static struct crypto_instance *alloc(struct rtattr **tb)
  227. {
  228. struct crypto_instance *inst;
  229. struct crypto_alg *alg;
  230. int err;
  231. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
  232. if (err)
  233. return ERR_PTR(err);
  234. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
  235. CRYPTO_ALG_TYPE_MASK);
  236. if (IS_ERR(alg))
  237. return ERR_CAST(alg);
  238. inst = crypto_alloc_instance("xts", alg);
  239. if (IS_ERR(inst))
  240. goto out_put_alg;
  241. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  242. inst->alg.cra_priority = alg->cra_priority;
  243. inst->alg.cra_blocksize = alg->cra_blocksize;
  244. if (alg->cra_alignmask < 7)
  245. inst->alg.cra_alignmask = 7;
  246. else
  247. inst->alg.cra_alignmask = alg->cra_alignmask;
  248. inst->alg.cra_type = &crypto_blkcipher_type;
  249. inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
  250. inst->alg.cra_blkcipher.min_keysize =
  251. 2 * alg->cra_cipher.cia_min_keysize;
  252. inst->alg.cra_blkcipher.max_keysize =
  253. 2 * alg->cra_cipher.cia_max_keysize;
  254. inst->alg.cra_ctxsize = sizeof(struct priv);
  255. inst->alg.cra_init = init_tfm;
  256. inst->alg.cra_exit = exit_tfm;
  257. inst->alg.cra_blkcipher.setkey = setkey;
  258. inst->alg.cra_blkcipher.encrypt = encrypt;
  259. inst->alg.cra_blkcipher.decrypt = decrypt;
  260. out_put_alg:
  261. crypto_mod_put(alg);
  262. return inst;
  263. }
  264. static void free(struct crypto_instance *inst)
  265. {
  266. crypto_drop_spawn(crypto_instance_ctx(inst));
  267. kfree(inst);
  268. }
  269. static struct crypto_template crypto_tmpl = {
  270. .name = "xts",
  271. .alloc = alloc,
  272. .free = free,
  273. .module = THIS_MODULE,
  274. };
  275. static int __init crypto_module_init(void)
  276. {
  277. return crypto_register_template(&crypto_tmpl);
  278. }
  279. static void __exit crypto_module_exit(void)
  280. {
  281. crypto_unregister_template(&crypto_tmpl);
  282. }
  283. module_init(crypto_module_init);
  284. module_exit(crypto_module_exit);
  285. MODULE_LICENSE("GPL");
  286. MODULE_DESCRIPTION("XTS block cipher mode");
  287. MODULE_ALIAS_CRYPTO("xts");