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 initial '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. u8 *wsrc;
  91. u8 *wdst;
  92. err = blkcipher_walk_virt(d, w);
  93. if (!w->nbytes)
  94. return err;
  95. s.t = (be128 *)w->iv;
  96. avail = w->nbytes;
  97. wsrc = w->src.virt.addr;
  98. wdst = w->dst.virt.addr;
  99. /* calculate first value of T */
  100. tw(crypto_cipher_tfm(ctx->tweak), w->iv, w->iv);
  101. goto first;
  102. for (;;) {
  103. do {
  104. gf128mul_x_ble(s.t, s.t);
  105. first:
  106. xts_round(&s, wdst, wsrc);
  107. wsrc += bs;
  108. wdst += bs;
  109. } while ((avail -= bs) >= bs);
  110. err = blkcipher_walk_done(d, w, avail);
  111. if (!w->nbytes)
  112. break;
  113. avail = w->nbytes;
  114. wsrc = w->src.virt.addr;
  115. wdst = w->dst.virt.addr;
  116. }
  117. return err;
  118. }
  119. static int encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  120. struct scatterlist *src, unsigned int nbytes)
  121. {
  122. struct priv *ctx = crypto_blkcipher_ctx(desc->tfm);
  123. struct blkcipher_walk w;
  124. blkcipher_walk_init(&w, dst, src, nbytes);
  125. return crypt(desc, &w, ctx, crypto_cipher_alg(ctx->tweak)->cia_encrypt,
  126. crypto_cipher_alg(ctx->child)->cia_encrypt);
  127. }
  128. static int decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  129. struct scatterlist *src, unsigned int nbytes)
  130. {
  131. struct priv *ctx = crypto_blkcipher_ctx(desc->tfm);
  132. struct blkcipher_walk w;
  133. blkcipher_walk_init(&w, dst, src, nbytes);
  134. return crypt(desc, &w, ctx, crypto_cipher_alg(ctx->tweak)->cia_encrypt,
  135. crypto_cipher_alg(ctx->child)->cia_decrypt);
  136. }
  137. static int init_tfm(struct crypto_tfm *tfm)
  138. {
  139. struct crypto_cipher *cipher;
  140. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  141. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  142. struct priv *ctx = crypto_tfm_ctx(tfm);
  143. u32 *flags = &tfm->crt_flags;
  144. cipher = crypto_spawn_cipher(spawn);
  145. if (IS_ERR(cipher))
  146. return PTR_ERR(cipher);
  147. if (crypto_cipher_blocksize(cipher) != 16) {
  148. *flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  149. crypto_free_cipher(cipher);
  150. return -EINVAL;
  151. }
  152. ctx->child = cipher;
  153. cipher = crypto_spawn_cipher(spawn);
  154. if (IS_ERR(cipher)) {
  155. crypto_free_cipher(ctx->child);
  156. return PTR_ERR(cipher);
  157. }
  158. /* this check isn't really needed, leave it here just in case */
  159. if (crypto_cipher_blocksize(cipher) != 16) {
  160. crypto_free_cipher(cipher);
  161. crypto_free_cipher(ctx->child);
  162. *flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  163. return -EINVAL;
  164. }
  165. ctx->tweak = cipher;
  166. return 0;
  167. }
  168. static void exit_tfm(struct crypto_tfm *tfm)
  169. {
  170. struct priv *ctx = crypto_tfm_ctx(tfm);
  171. crypto_free_cipher(ctx->child);
  172. crypto_free_cipher(ctx->tweak);
  173. }
  174. static struct crypto_instance *alloc(struct rtattr **tb)
  175. {
  176. struct crypto_instance *inst;
  177. struct crypto_alg *alg;
  178. int err;
  179. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
  180. if (err)
  181. return ERR_PTR(err);
  182. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
  183. CRYPTO_ALG_TYPE_MASK);
  184. if (IS_ERR(alg))
  185. return ERR_CAST(alg);
  186. inst = crypto_alloc_instance("xts", alg);
  187. if (IS_ERR(inst))
  188. goto out_put_alg;
  189. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  190. inst->alg.cra_priority = alg->cra_priority;
  191. inst->alg.cra_blocksize = alg->cra_blocksize;
  192. if (alg->cra_alignmask < 7)
  193. inst->alg.cra_alignmask = 7;
  194. else
  195. inst->alg.cra_alignmask = alg->cra_alignmask;
  196. inst->alg.cra_type = &crypto_blkcipher_type;
  197. inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
  198. inst->alg.cra_blkcipher.min_keysize =
  199. 2 * alg->cra_cipher.cia_min_keysize;
  200. inst->alg.cra_blkcipher.max_keysize =
  201. 2 * alg->cra_cipher.cia_max_keysize;
  202. inst->alg.cra_ctxsize = sizeof(struct priv);
  203. inst->alg.cra_init = init_tfm;
  204. inst->alg.cra_exit = exit_tfm;
  205. inst->alg.cra_blkcipher.setkey = setkey;
  206. inst->alg.cra_blkcipher.encrypt = encrypt;
  207. inst->alg.cra_blkcipher.decrypt = decrypt;
  208. out_put_alg:
  209. crypto_mod_put(alg);
  210. return inst;
  211. }
  212. static void free(struct crypto_instance *inst)
  213. {
  214. crypto_drop_spawn(crypto_instance_ctx(inst));
  215. kfree(inst);
  216. }
  217. static struct crypto_template crypto_tmpl = {
  218. .name = "xts",
  219. .alloc = alloc,
  220. .free = free,
  221. .module = THIS_MODULE,
  222. };
  223. static int __init crypto_module_init(void)
  224. {
  225. return crypto_register_template(&crypto_tmpl);
  226. }
  227. static void __exit crypto_module_exit(void)
  228. {
  229. crypto_unregister_template(&crypto_tmpl);
  230. }
  231. module_init(crypto_module_init);
  232. module_exit(crypto_module_exit);
  233. MODULE_LICENSE("GPL");
  234. MODULE_DESCRIPTION("XTS block cipher mode");