lrw.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* LRW: as defined by Cyril Guyot in
  2. * http://grouper.ieee.org/groups/1619/email/pdf00017.pdf
  3. *
  4. * Copyright (c) 2006 Rik Snel <rsnel@cube.dyndns.org>
  5. *
  6. * Based om ecb.c
  7. * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. */
  14. /* This implementation is checked against the test vectors in the above
  15. * document and by a test vector provided by Ken Buchanan at
  16. * http://www.mail-archive.com/stds-p1619@listserv.ieee.org/msg00173.html
  17. *
  18. * The test vectors are included in the testing module tcrypt.[ch] */
  19. #include <crypto/algapi.h>
  20. #include <linux/err.h>
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/slab.h>
  26. #include <crypto/b128ops.h>
  27. #include <crypto/gf128mul.h>
  28. struct priv {
  29. struct crypto_cipher *child;
  30. /* optimizes multiplying a random (non incrementing, as at the
  31. * start of a new sector) value with key2, we could also have
  32. * used 4k optimization tables or no optimization at all. In the
  33. * latter case we would have to store key2 here */
  34. struct gf128mul_64k *table;
  35. /* stores:
  36. * key2*{ 0,0,...0,0,0,0,1 }, key2*{ 0,0,...0,0,0,1,1 },
  37. * key2*{ 0,0,...0,0,1,1,1 }, key2*{ 0,0,...0,1,1,1,1 }
  38. * key2*{ 0,0,...1,1,1,1,1 }, etc
  39. * needed for optimized multiplication of incrementing values
  40. * with key2 */
  41. be128 mulinc[128];
  42. };
  43. static inline void setbit128_bbe(void *b, int bit)
  44. {
  45. __set_bit(bit ^ 0x78, b);
  46. }
  47. static int setkey(struct crypto_tfm *parent, const u8 *key,
  48. unsigned int keylen)
  49. {
  50. struct priv *ctx = crypto_tfm_ctx(parent);
  51. struct crypto_cipher *child = ctx->child;
  52. int err, i;
  53. be128 tmp = { 0 };
  54. int bsize = crypto_cipher_blocksize(child);
  55. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  56. crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
  57. CRYPTO_TFM_REQ_MASK);
  58. if ((err = crypto_cipher_setkey(child, key, keylen - bsize)))
  59. return err;
  60. crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
  61. CRYPTO_TFM_RES_MASK);
  62. if (ctx->table)
  63. gf128mul_free_64k(ctx->table);
  64. /* initialize multiplication table for Key2 */
  65. ctx->table = gf128mul_init_64k_bbe((be128 *)(key + keylen - bsize));
  66. if (!ctx->table)
  67. return -ENOMEM;
  68. /* initialize optimization table */
  69. for (i = 0; i < 128; i++) {
  70. setbit128_bbe(&tmp, i);
  71. ctx->mulinc[i] = tmp;
  72. gf128mul_64k_bbe(&ctx->mulinc[i], ctx->table);
  73. }
  74. return 0;
  75. }
  76. struct sinfo {
  77. be128 t;
  78. struct crypto_tfm *tfm;
  79. void (*fn)(struct crypto_tfm *, u8 *, const u8 *);
  80. };
  81. static inline void inc(be128 *iv)
  82. {
  83. be64_add_cpu(&iv->b, 1);
  84. if (!iv->b)
  85. be64_add_cpu(&iv->a, 1);
  86. }
  87. static inline void lrw_round(struct sinfo *s, void *dst, const void *src)
  88. {
  89. be128_xor(dst, &s->t, src); /* PP <- T xor P */
  90. s->fn(s->tfm, dst, dst); /* CC <- E(Key2,PP) */
  91. be128_xor(dst, dst, &s->t); /* C <- T xor CC */
  92. }
  93. /* this returns the number of consequative 1 bits starting
  94. * from the right, get_index128(00 00 00 00 00 00 ... 00 00 10 FB) = 2 */
  95. static inline int get_index128(be128 *block)
  96. {
  97. int x;
  98. __be32 *p = (__be32 *) block;
  99. for (p += 3, x = 0; x < 128; p--, x += 32) {
  100. u32 val = be32_to_cpup(p);
  101. if (!~val)
  102. continue;
  103. return x + ffz(val);
  104. }
  105. return x;
  106. }
  107. static int crypt(struct blkcipher_desc *d,
  108. struct blkcipher_walk *w, struct priv *ctx,
  109. void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
  110. {
  111. int err;
  112. unsigned int avail;
  113. const int bs = crypto_cipher_blocksize(ctx->child);
  114. struct sinfo s = {
  115. .tfm = crypto_cipher_tfm(ctx->child),
  116. .fn = fn
  117. };
  118. be128 *iv;
  119. u8 *wsrc;
  120. u8 *wdst;
  121. err = blkcipher_walk_virt(d, w);
  122. if (!(avail = w->nbytes))
  123. return err;
  124. wsrc = w->src.virt.addr;
  125. wdst = w->dst.virt.addr;
  126. /* calculate first value of T */
  127. iv = (be128 *)w->iv;
  128. s.t = *iv;
  129. /* T <- I*Key2 */
  130. gf128mul_64k_bbe(&s.t, ctx->table);
  131. goto first;
  132. for (;;) {
  133. do {
  134. /* T <- I*Key2, using the optimization
  135. * discussed in the specification */
  136. be128_xor(&s.t, &s.t, &ctx->mulinc[get_index128(iv)]);
  137. inc(iv);
  138. first:
  139. lrw_round(&s, wdst, wsrc);
  140. wsrc += bs;
  141. wdst += bs;
  142. } while ((avail -= bs) >= bs);
  143. err = blkcipher_walk_done(d, w, avail);
  144. if (!(avail = w->nbytes))
  145. break;
  146. wsrc = w->src.virt.addr;
  147. wdst = w->dst.virt.addr;
  148. }
  149. return err;
  150. }
  151. static int encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  152. struct scatterlist *src, unsigned int nbytes)
  153. {
  154. struct priv *ctx = crypto_blkcipher_ctx(desc->tfm);
  155. struct blkcipher_walk w;
  156. blkcipher_walk_init(&w, dst, src, nbytes);
  157. return crypt(desc, &w, ctx,
  158. crypto_cipher_alg(ctx->child)->cia_encrypt);
  159. }
  160. static int decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  161. struct scatterlist *src, unsigned int nbytes)
  162. {
  163. struct priv *ctx = crypto_blkcipher_ctx(desc->tfm);
  164. struct blkcipher_walk w;
  165. blkcipher_walk_init(&w, dst, src, nbytes);
  166. return crypt(desc, &w, ctx,
  167. crypto_cipher_alg(ctx->child)->cia_decrypt);
  168. }
  169. static int init_tfm(struct crypto_tfm *tfm)
  170. {
  171. struct crypto_cipher *cipher;
  172. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  173. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  174. struct priv *ctx = crypto_tfm_ctx(tfm);
  175. u32 *flags = &tfm->crt_flags;
  176. cipher = crypto_spawn_cipher(spawn);
  177. if (IS_ERR(cipher))
  178. return PTR_ERR(cipher);
  179. if (crypto_cipher_blocksize(cipher) != 16) {
  180. *flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  181. return -EINVAL;
  182. }
  183. ctx->child = cipher;
  184. return 0;
  185. }
  186. static void exit_tfm(struct crypto_tfm *tfm)
  187. {
  188. struct priv *ctx = crypto_tfm_ctx(tfm);
  189. if (ctx->table)
  190. gf128mul_free_64k(ctx->table);
  191. crypto_free_cipher(ctx->child);
  192. }
  193. static struct crypto_instance *alloc(struct rtattr **tb)
  194. {
  195. struct crypto_instance *inst;
  196. struct crypto_alg *alg;
  197. int err;
  198. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
  199. if (err)
  200. return ERR_PTR(err);
  201. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
  202. CRYPTO_ALG_TYPE_MASK);
  203. if (IS_ERR(alg))
  204. return ERR_CAST(alg);
  205. inst = crypto_alloc_instance("lrw", alg);
  206. if (IS_ERR(inst))
  207. goto out_put_alg;
  208. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  209. inst->alg.cra_priority = alg->cra_priority;
  210. inst->alg.cra_blocksize = alg->cra_blocksize;
  211. if (alg->cra_alignmask < 7) inst->alg.cra_alignmask = 7;
  212. else inst->alg.cra_alignmask = alg->cra_alignmask;
  213. inst->alg.cra_type = &crypto_blkcipher_type;
  214. if (!(alg->cra_blocksize % 4))
  215. inst->alg.cra_alignmask |= 3;
  216. inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
  217. inst->alg.cra_blkcipher.min_keysize =
  218. alg->cra_cipher.cia_min_keysize + alg->cra_blocksize;
  219. inst->alg.cra_blkcipher.max_keysize =
  220. alg->cra_cipher.cia_max_keysize + alg->cra_blocksize;
  221. inst->alg.cra_ctxsize = sizeof(struct priv);
  222. inst->alg.cra_init = init_tfm;
  223. inst->alg.cra_exit = exit_tfm;
  224. inst->alg.cra_blkcipher.setkey = setkey;
  225. inst->alg.cra_blkcipher.encrypt = encrypt;
  226. inst->alg.cra_blkcipher.decrypt = decrypt;
  227. out_put_alg:
  228. crypto_mod_put(alg);
  229. return inst;
  230. }
  231. static void free(struct crypto_instance *inst)
  232. {
  233. crypto_drop_spawn(crypto_instance_ctx(inst));
  234. kfree(inst);
  235. }
  236. static struct crypto_template crypto_tmpl = {
  237. .name = "lrw",
  238. .alloc = alloc,
  239. .free = free,
  240. .module = THIS_MODULE,
  241. };
  242. static int __init crypto_module_init(void)
  243. {
  244. return crypto_register_template(&crypto_tmpl);
  245. }
  246. static void __exit crypto_module_exit(void)
  247. {
  248. crypto_unregister_template(&crypto_tmpl);
  249. }
  250. module_init(crypto_module_init);
  251. module_exit(crypto_module_exit);
  252. MODULE_LICENSE("GPL");
  253. MODULE_DESCRIPTION("LRW block cipher mode");