lrw.c 7.5 KB

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