lrw.c 8.0 KB

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