lrw.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 ^ (0x80 -
  46. #ifdef __BIG_ENDIAN
  47. BITS_PER_LONG
  48. #else
  49. BITS_PER_BYTE
  50. #endif
  51. ), b);
  52. }
  53. static int setkey(struct crypto_tfm *parent, const u8 *key,
  54. unsigned int keylen)
  55. {
  56. struct priv *ctx = crypto_tfm_ctx(parent);
  57. struct crypto_cipher *child = ctx->child;
  58. int err, i;
  59. be128 tmp = { 0 };
  60. int bsize = crypto_cipher_blocksize(child);
  61. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  62. crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
  63. CRYPTO_TFM_REQ_MASK);
  64. if ((err = crypto_cipher_setkey(child, key, keylen - bsize)))
  65. return err;
  66. crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
  67. CRYPTO_TFM_RES_MASK);
  68. if (ctx->table)
  69. gf128mul_free_64k(ctx->table);
  70. /* initialize multiplication table for Key2 */
  71. ctx->table = gf128mul_init_64k_bbe((be128 *)(key + keylen - bsize));
  72. if (!ctx->table)
  73. return -ENOMEM;
  74. /* initialize optimization table */
  75. for (i = 0; i < 128; i++) {
  76. setbit128_bbe(&tmp, i);
  77. ctx->mulinc[i] = tmp;
  78. gf128mul_64k_bbe(&ctx->mulinc[i], ctx->table);
  79. }
  80. return 0;
  81. }
  82. struct sinfo {
  83. be128 t;
  84. struct crypto_tfm *tfm;
  85. void (*fn)(struct crypto_tfm *, u8 *, const u8 *);
  86. };
  87. static inline void inc(be128 *iv)
  88. {
  89. be64_add_cpu(&iv->b, 1);
  90. if (!iv->b)
  91. be64_add_cpu(&iv->a, 1);
  92. }
  93. static inline void lrw_round(struct sinfo *s, void *dst, const void *src)
  94. {
  95. be128_xor(dst, &s->t, src); /* PP <- T xor P */
  96. s->fn(s->tfm, dst, dst); /* CC <- E(Key2,PP) */
  97. be128_xor(dst, dst, &s->t); /* C <- T xor CC */
  98. }
  99. /* this returns the number of consequative 1 bits starting
  100. * from the right, get_index128(00 00 00 00 00 00 ... 00 00 10 FB) = 2 */
  101. static inline int get_index128(be128 *block)
  102. {
  103. int x;
  104. __be32 *p = (__be32 *) block;
  105. for (p += 3, x = 0; x < 128; p--, x += 32) {
  106. u32 val = be32_to_cpup(p);
  107. if (!~val)
  108. continue;
  109. return x + ffz(val);
  110. }
  111. return x;
  112. }
  113. static int crypt(struct blkcipher_desc *d,
  114. struct blkcipher_walk *w, struct priv *ctx,
  115. void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
  116. {
  117. int err;
  118. unsigned int avail;
  119. const int bs = crypto_cipher_blocksize(ctx->child);
  120. struct sinfo s = {
  121. .tfm = crypto_cipher_tfm(ctx->child),
  122. .fn = fn
  123. };
  124. be128 *iv;
  125. u8 *wsrc;
  126. u8 *wdst;
  127. err = blkcipher_walk_virt(d, w);
  128. if (!(avail = w->nbytes))
  129. return err;
  130. wsrc = w->src.virt.addr;
  131. wdst = w->dst.virt.addr;
  132. /* calculate first value of T */
  133. iv = (be128 *)w->iv;
  134. s.t = *iv;
  135. /* T <- I*Key2 */
  136. gf128mul_64k_bbe(&s.t, ctx->table);
  137. goto first;
  138. for (;;) {
  139. do {
  140. /* T <- I*Key2, using the optimization
  141. * discussed in the specification */
  142. be128_xor(&s.t, &s.t, &ctx->mulinc[get_index128(iv)]);
  143. inc(iv);
  144. first:
  145. lrw_round(&s, wdst, wsrc);
  146. wsrc += bs;
  147. wdst += bs;
  148. } while ((avail -= bs) >= bs);
  149. err = blkcipher_walk_done(d, w, avail);
  150. if (!(avail = w->nbytes))
  151. break;
  152. wsrc = w->src.virt.addr;
  153. wdst = w->dst.virt.addr;
  154. }
  155. return err;
  156. }
  157. static int encrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  158. struct scatterlist *src, unsigned int nbytes)
  159. {
  160. struct priv *ctx = crypto_blkcipher_ctx(desc->tfm);
  161. struct blkcipher_walk w;
  162. blkcipher_walk_init(&w, dst, src, nbytes);
  163. return crypt(desc, &w, ctx,
  164. crypto_cipher_alg(ctx->child)->cia_encrypt);
  165. }
  166. static int decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  167. struct scatterlist *src, unsigned int nbytes)
  168. {
  169. struct priv *ctx = crypto_blkcipher_ctx(desc->tfm);
  170. struct blkcipher_walk w;
  171. blkcipher_walk_init(&w, dst, src, nbytes);
  172. return crypt(desc, &w, ctx,
  173. crypto_cipher_alg(ctx->child)->cia_decrypt);
  174. }
  175. static int init_tfm(struct crypto_tfm *tfm)
  176. {
  177. struct crypto_cipher *cipher;
  178. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  179. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  180. struct priv *ctx = crypto_tfm_ctx(tfm);
  181. u32 *flags = &tfm->crt_flags;
  182. cipher = crypto_spawn_cipher(spawn);
  183. if (IS_ERR(cipher))
  184. return PTR_ERR(cipher);
  185. if (crypto_cipher_blocksize(cipher) != 16) {
  186. *flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  187. return -EINVAL;
  188. }
  189. ctx->child = cipher;
  190. return 0;
  191. }
  192. static void exit_tfm(struct crypto_tfm *tfm)
  193. {
  194. struct priv *ctx = crypto_tfm_ctx(tfm);
  195. if (ctx->table)
  196. gf128mul_free_64k(ctx->table);
  197. crypto_free_cipher(ctx->child);
  198. }
  199. static struct crypto_instance *alloc(struct rtattr **tb)
  200. {
  201. struct crypto_instance *inst;
  202. struct crypto_alg *alg;
  203. int err;
  204. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
  205. if (err)
  206. return ERR_PTR(err);
  207. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
  208. CRYPTO_ALG_TYPE_MASK);
  209. if (IS_ERR(alg))
  210. return ERR_CAST(alg);
  211. inst = crypto_alloc_instance("lrw", alg);
  212. if (IS_ERR(inst))
  213. goto out_put_alg;
  214. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  215. inst->alg.cra_priority = alg->cra_priority;
  216. inst->alg.cra_blocksize = alg->cra_blocksize;
  217. if (alg->cra_alignmask < 7) inst->alg.cra_alignmask = 7;
  218. else inst->alg.cra_alignmask = alg->cra_alignmask;
  219. inst->alg.cra_type = &crypto_blkcipher_type;
  220. if (!(alg->cra_blocksize % 4))
  221. inst->alg.cra_alignmask |= 3;
  222. inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
  223. inst->alg.cra_blkcipher.min_keysize =
  224. alg->cra_cipher.cia_min_keysize + alg->cra_blocksize;
  225. inst->alg.cra_blkcipher.max_keysize =
  226. alg->cra_cipher.cia_max_keysize + alg->cra_blocksize;
  227. inst->alg.cra_ctxsize = sizeof(struct priv);
  228. inst->alg.cra_init = init_tfm;
  229. inst->alg.cra_exit = exit_tfm;
  230. inst->alg.cra_blkcipher.setkey = setkey;
  231. inst->alg.cra_blkcipher.encrypt = encrypt;
  232. inst->alg.cra_blkcipher.decrypt = decrypt;
  233. out_put_alg:
  234. crypto_mod_put(alg);
  235. return inst;
  236. }
  237. static void free(struct crypto_instance *inst)
  238. {
  239. crypto_drop_spawn(crypto_instance_ctx(inst));
  240. kfree(inst);
  241. }
  242. static struct crypto_template crypto_tmpl = {
  243. .name = "lrw",
  244. .alloc = alloc,
  245. .free = free,
  246. .module = THIS_MODULE,
  247. };
  248. static int __init crypto_module_init(void)
  249. {
  250. return crypto_register_template(&crypto_tmpl);
  251. }
  252. static void __exit crypto_module_exit(void)
  253. {
  254. crypto_unregister_template(&crypto_tmpl);
  255. }
  256. module_init(crypto_module_init);
  257. module_exit(crypto_module_exit);
  258. MODULE_LICENSE("GPL");
  259. MODULE_DESCRIPTION("LRW block cipher mode");