xts.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 om 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/b128ops.h>
  24. #include <crypto/gf128mul.h>
  25. struct priv {
  26. struct crypto_cipher *child;
  27. struct crypto_cipher *tweak;
  28. };
  29. static int setkey(struct crypto_tfm *parent, const u8 *key,
  30. unsigned int keylen)
  31. {
  32. struct priv *ctx = crypto_tfm_ctx(parent);
  33. struct crypto_cipher *child = ctx->tweak;
  34. u32 *flags = &parent->crt_flags;
  35. int err;
  36. /* key consists of keys of equal size concatenated, therefore
  37. * the length must be even */
  38. if (keylen % 2) {
  39. /* tell the user why there was an error */
  40. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  41. return -EINVAL;
  42. }
  43. /* we need two cipher instances: one to compute the inital 'tweak'
  44. * by encrypting the IV (usually the 'plain' iv) and the other
  45. * one to encrypt and decrypt the data */
  46. /* tweak cipher, uses Key2 i.e. the second half of *key */
  47. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  48. crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
  49. CRYPTO_TFM_REQ_MASK);
  50. err = crypto_cipher_setkey(child, key + keylen/2, keylen/2);
  51. if (err)
  52. return err;
  53. crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
  54. CRYPTO_TFM_RES_MASK);
  55. child = ctx->child;
  56. /* data cipher, uses Key1 i.e. the first half of *key */
  57. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  58. crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
  59. CRYPTO_TFM_REQ_MASK);
  60. err = crypto_cipher_setkey(child, key, keylen/2);
  61. if (err)
  62. return err;
  63. crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
  64. CRYPTO_TFM_RES_MASK);
  65. return 0;
  66. }
  67. struct sinfo {
  68. be128 t;
  69. struct crypto_tfm *tfm;
  70. void (*fn)(struct crypto_tfm *, u8 *, const u8 *);
  71. };
  72. static inline void xts_round(struct sinfo *s, void *dst, const void *src)
  73. {
  74. be128_xor(dst, &s->t, src); /* PP <- T xor P */
  75. s->fn(s->tfm, dst, dst); /* CC <- E(Key1,PP) */
  76. be128_xor(dst, dst, &s->t); /* C <- T xor CC */
  77. }
  78. static int crypt(struct blkcipher_desc *d,
  79. struct blkcipher_walk *w, struct priv *ctx,
  80. void (*tw)(struct crypto_tfm *, u8 *, const u8 *),
  81. void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
  82. {
  83. int err;
  84. unsigned int avail;
  85. const int bs = crypto_cipher_blocksize(ctx->child);
  86. struct sinfo s = {
  87. .tfm = crypto_cipher_tfm(ctx->child),
  88. .fn = fn
  89. };
  90. be128 *iv;
  91. u8 *wsrc;
  92. u8 *wdst;
  93. err = blkcipher_walk_virt(d, w);
  94. if (!w->nbytes)
  95. return err;
  96. avail = w->nbytes;
  97. wsrc = w->src.virt.addr;
  98. wdst = w->dst.virt.addr;
  99. /* calculate first value of T */
  100. iv = (be128 *)w->iv;
  101. tw(crypto_cipher_tfm(ctx->tweak), (void *)&s.t, w->iv);
  102. goto first;
  103. for (;;) {
  104. do {
  105. gf128mul_x_ble(&s.t, &s.t);
  106. first:
  107. xts_round(&s, wdst, wsrc);
  108. wsrc += bs;
  109. wdst += bs;
  110. } while ((avail -= bs) >= bs);
  111. err = blkcipher_walk_done(d, w, avail);
  112. if (!w->nbytes)
  113. break;
  114. avail = w->nbytes;
  115. wsrc = w->src.virt.addr;
  116. wdst = w->dst.virt.addr;
  117. }
  118. return err;
  119. }
  120. static int encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  121. struct scatterlist *src, unsigned int nbytes)
  122. {
  123. struct priv *ctx = crypto_blkcipher_ctx(desc->tfm);
  124. struct blkcipher_walk w;
  125. blkcipher_walk_init(&w, dst, src, nbytes);
  126. return crypt(desc, &w, ctx, crypto_cipher_alg(ctx->tweak)->cia_encrypt,
  127. crypto_cipher_alg(ctx->child)->cia_encrypt);
  128. }
  129. static int decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  130. struct scatterlist *src, unsigned int nbytes)
  131. {
  132. struct priv *ctx = crypto_blkcipher_ctx(desc->tfm);
  133. struct blkcipher_walk w;
  134. blkcipher_walk_init(&w, dst, src, nbytes);
  135. return crypt(desc, &w, ctx, crypto_cipher_alg(ctx->tweak)->cia_encrypt,
  136. crypto_cipher_alg(ctx->child)->cia_decrypt);
  137. }
  138. static int init_tfm(struct crypto_tfm *tfm)
  139. {
  140. struct crypto_cipher *cipher;
  141. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  142. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  143. struct priv *ctx = crypto_tfm_ctx(tfm);
  144. u32 *flags = &tfm->crt_flags;
  145. cipher = crypto_spawn_cipher(spawn);
  146. if (IS_ERR(cipher))
  147. return PTR_ERR(cipher);
  148. if (crypto_cipher_blocksize(cipher) != 16) {
  149. *flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  150. crypto_free_cipher(cipher);
  151. return -EINVAL;
  152. }
  153. ctx->child = cipher;
  154. cipher = crypto_spawn_cipher(spawn);
  155. if (IS_ERR(cipher)) {
  156. crypto_free_cipher(ctx->child);
  157. return PTR_ERR(cipher);
  158. }
  159. /* this check isn't really needed, leave it here just in case */
  160. if (crypto_cipher_blocksize(cipher) != 16) {
  161. crypto_free_cipher(cipher);
  162. crypto_free_cipher(ctx->child);
  163. *flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  164. return -EINVAL;
  165. }
  166. ctx->tweak = cipher;
  167. return 0;
  168. }
  169. static void exit_tfm(struct crypto_tfm *tfm)
  170. {
  171. struct priv *ctx = crypto_tfm_ctx(tfm);
  172. crypto_free_cipher(ctx->child);
  173. crypto_free_cipher(ctx->tweak);
  174. }
  175. static struct crypto_instance *alloc(struct rtattr **tb)
  176. {
  177. struct crypto_instance *inst;
  178. struct crypto_alg *alg;
  179. int err;
  180. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
  181. if (err)
  182. return ERR_PTR(err);
  183. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
  184. CRYPTO_ALG_TYPE_MASK);
  185. if (IS_ERR(alg))
  186. return ERR_PTR(PTR_ERR(alg));
  187. inst = crypto_alloc_instance("xts", alg);
  188. if (IS_ERR(inst))
  189. goto out_put_alg;
  190. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  191. inst->alg.cra_priority = alg->cra_priority;
  192. inst->alg.cra_blocksize = alg->cra_blocksize;
  193. if (alg->cra_alignmask < 7)
  194. inst->alg.cra_alignmask = 7;
  195. else
  196. inst->alg.cra_alignmask = alg->cra_alignmask;
  197. inst->alg.cra_type = &crypto_blkcipher_type;
  198. inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
  199. inst->alg.cra_blkcipher.min_keysize =
  200. 2 * alg->cra_cipher.cia_min_keysize;
  201. inst->alg.cra_blkcipher.max_keysize =
  202. 2 * alg->cra_cipher.cia_max_keysize;
  203. inst->alg.cra_ctxsize = sizeof(struct priv);
  204. inst->alg.cra_init = init_tfm;
  205. inst->alg.cra_exit = exit_tfm;
  206. inst->alg.cra_blkcipher.setkey = setkey;
  207. inst->alg.cra_blkcipher.encrypt = encrypt;
  208. inst->alg.cra_blkcipher.decrypt = decrypt;
  209. out_put_alg:
  210. crypto_mod_put(alg);
  211. return inst;
  212. }
  213. static void free(struct crypto_instance *inst)
  214. {
  215. crypto_drop_spawn(crypto_instance_ctx(inst));
  216. kfree(inst);
  217. }
  218. static struct crypto_template crypto_tmpl = {
  219. .name = "xts",
  220. .alloc = alloc,
  221. .free = free,
  222. .module = THIS_MODULE,
  223. };
  224. static int __init crypto_module_init(void)
  225. {
  226. return crypto_register_template(&crypto_tmpl);
  227. }
  228. static void __exit crypto_module_exit(void)
  229. {
  230. crypto_unregister_template(&crypto_tmpl);
  231. }
  232. module_init(crypto_module_init);
  233. module_exit(crypto_module_exit);
  234. MODULE_LICENSE("GPL");
  235. MODULE_DESCRIPTION("XTS block cipher mode");