lrw.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 on 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. #include <crypto/lrw.h>
  29. struct priv {
  30. struct crypto_cipher *child;
  31. struct lrw_table_ctx table;
  32. };
  33. static inline void setbit128_bbe(void *b, int bit)
  34. {
  35. __set_bit(bit ^ (0x80 -
  36. #ifdef __BIG_ENDIAN
  37. BITS_PER_LONG
  38. #else
  39. BITS_PER_BYTE
  40. #endif
  41. ), b);
  42. }
  43. int lrw_init_table(struct lrw_table_ctx *ctx, const u8 *tweak)
  44. {
  45. be128 tmp = { 0 };
  46. int i;
  47. if (ctx->table)
  48. gf128mul_free_64k(ctx->table);
  49. /* initialize multiplication table for Key2 */
  50. ctx->table = gf128mul_init_64k_bbe((be128 *)tweak);
  51. if (!ctx->table)
  52. return -ENOMEM;
  53. /* initialize optimization table */
  54. for (i = 0; i < 128; i++) {
  55. setbit128_bbe(&tmp, i);
  56. ctx->mulinc[i] = tmp;
  57. gf128mul_64k_bbe(&ctx->mulinc[i], ctx->table);
  58. }
  59. return 0;
  60. }
  61. EXPORT_SYMBOL_GPL(lrw_init_table);
  62. void lrw_free_table(struct lrw_table_ctx *ctx)
  63. {
  64. if (ctx->table)
  65. gf128mul_free_64k(ctx->table);
  66. }
  67. EXPORT_SYMBOL_GPL(lrw_free_table);
  68. static int setkey(struct crypto_tfm *parent, const u8 *key,
  69. unsigned int keylen)
  70. {
  71. struct priv *ctx = crypto_tfm_ctx(parent);
  72. struct crypto_cipher *child = ctx->child;
  73. int err, bsize = LRW_BLOCK_SIZE;
  74. const u8 *tweak = key + keylen - bsize;
  75. crypto_cipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  76. crypto_cipher_set_flags(child, crypto_tfm_get_flags(parent) &
  77. CRYPTO_TFM_REQ_MASK);
  78. err = crypto_cipher_setkey(child, key, keylen - bsize);
  79. if (err)
  80. return err;
  81. crypto_tfm_set_flags(parent, crypto_cipher_get_flags(child) &
  82. CRYPTO_TFM_RES_MASK);
  83. return lrw_init_table(&ctx->table, tweak);
  84. }
  85. struct sinfo {
  86. be128 t;
  87. struct crypto_tfm *tfm;
  88. void (*fn)(struct crypto_tfm *, u8 *, const u8 *);
  89. };
  90. static inline void inc(be128 *iv)
  91. {
  92. be64_add_cpu(&iv->b, 1);
  93. if (!iv->b)
  94. be64_add_cpu(&iv->a, 1);
  95. }
  96. static inline void lrw_round(struct sinfo *s, void *dst, const void *src)
  97. {
  98. be128_xor(dst, &s->t, src); /* PP <- T xor P */
  99. s->fn(s->tfm, dst, dst); /* CC <- E(Key2,PP) */
  100. be128_xor(dst, dst, &s->t); /* C <- T xor CC */
  101. }
  102. /* this returns the number of consequative 1 bits starting
  103. * from the right, get_index128(00 00 00 00 00 00 ... 00 00 10 FB) = 2 */
  104. static inline int get_index128(be128 *block)
  105. {
  106. int x;
  107. __be32 *p = (__be32 *) block;
  108. for (p += 3, x = 0; x < 128; p--, x += 32) {
  109. u32 val = be32_to_cpup(p);
  110. if (!~val)
  111. continue;
  112. return x + ffz(val);
  113. }
  114. /*
  115. * If we get here, then x == 128 and we are incrementing the counter
  116. * from all ones to all zeros. This means we must return index 127, i.e.
  117. * the one corresponding to key2*{ 1,...,1 }.
  118. */
  119. return 127;
  120. }
  121. static int crypt(struct blkcipher_desc *d,
  122. struct blkcipher_walk *w, struct priv *ctx,
  123. void (*fn)(struct crypto_tfm *, u8 *, const u8 *))
  124. {
  125. int err;
  126. unsigned int avail;
  127. const int bs = LRW_BLOCK_SIZE;
  128. struct sinfo s = {
  129. .tfm = crypto_cipher_tfm(ctx->child),
  130. .fn = fn
  131. };
  132. be128 *iv;
  133. u8 *wsrc;
  134. u8 *wdst;
  135. err = blkcipher_walk_virt(d, w);
  136. if (!(avail = w->nbytes))
  137. return err;
  138. wsrc = w->src.virt.addr;
  139. wdst = w->dst.virt.addr;
  140. /* calculate first value of T */
  141. iv = (be128 *)w->iv;
  142. s.t = *iv;
  143. /* T <- I*Key2 */
  144. gf128mul_64k_bbe(&s.t, ctx->table.table);
  145. goto first;
  146. for (;;) {
  147. do {
  148. /* T <- I*Key2, using the optimization
  149. * discussed in the specification */
  150. be128_xor(&s.t, &s.t,
  151. &ctx->table.mulinc[get_index128(iv)]);
  152. inc(iv);
  153. first:
  154. lrw_round(&s, wdst, wsrc);
  155. wsrc += bs;
  156. wdst += bs;
  157. } while ((avail -= bs) >= bs);
  158. err = blkcipher_walk_done(d, w, avail);
  159. if (!(avail = w->nbytes))
  160. break;
  161. wsrc = w->src.virt.addr;
  162. wdst = w->dst.virt.addr;
  163. }
  164. return err;
  165. }
  166. static int encrypt(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_encrypt);
  174. }
  175. static int decrypt(struct blkcipher_desc *desc, struct scatterlist *dst,
  176. struct scatterlist *src, unsigned int nbytes)
  177. {
  178. struct priv *ctx = crypto_blkcipher_ctx(desc->tfm);
  179. struct blkcipher_walk w;
  180. blkcipher_walk_init(&w, dst, src, nbytes);
  181. return crypt(desc, &w, ctx,
  182. crypto_cipher_alg(ctx->child)->cia_decrypt);
  183. }
  184. int lrw_crypt(struct blkcipher_desc *desc, struct scatterlist *sdst,
  185. struct scatterlist *ssrc, unsigned int nbytes,
  186. struct lrw_crypt_req *req)
  187. {
  188. const unsigned int bsize = LRW_BLOCK_SIZE;
  189. const unsigned int max_blks = req->tbuflen / bsize;
  190. struct lrw_table_ctx *ctx = req->table_ctx;
  191. struct blkcipher_walk walk;
  192. unsigned int nblocks;
  193. be128 *iv, *src, *dst, *t;
  194. be128 *t_buf = req->tbuf;
  195. int err, i;
  196. BUG_ON(max_blks < 1);
  197. blkcipher_walk_init(&walk, sdst, ssrc, nbytes);
  198. err = blkcipher_walk_virt(desc, &walk);
  199. nbytes = walk.nbytes;
  200. if (!nbytes)
  201. return err;
  202. nblocks = min(walk.nbytes / bsize, max_blks);
  203. src = (be128 *)walk.src.virt.addr;
  204. dst = (be128 *)walk.dst.virt.addr;
  205. /* calculate first value of T */
  206. iv = (be128 *)walk.iv;
  207. t_buf[0] = *iv;
  208. /* T <- I*Key2 */
  209. gf128mul_64k_bbe(&t_buf[0], ctx->table);
  210. i = 0;
  211. goto first;
  212. for (;;) {
  213. do {
  214. for (i = 0; i < nblocks; i++) {
  215. /* T <- I*Key2, using the optimization
  216. * discussed in the specification */
  217. be128_xor(&t_buf[i], t,
  218. &ctx->mulinc[get_index128(iv)]);
  219. inc(iv);
  220. first:
  221. t = &t_buf[i];
  222. /* PP <- T xor P */
  223. be128_xor(dst + i, t, src + i);
  224. }
  225. /* CC <- E(Key2,PP) */
  226. req->crypt_fn(req->crypt_ctx, (u8 *)dst,
  227. nblocks * bsize);
  228. /* C <- T xor CC */
  229. for (i = 0; i < nblocks; i++)
  230. be128_xor(dst + i, dst + i, &t_buf[i]);
  231. src += nblocks;
  232. dst += nblocks;
  233. nbytes -= nblocks * bsize;
  234. nblocks = min(nbytes / bsize, max_blks);
  235. } while (nblocks > 0);
  236. err = blkcipher_walk_done(desc, &walk, nbytes);
  237. nbytes = walk.nbytes;
  238. if (!nbytes)
  239. break;
  240. nblocks = min(nbytes / bsize, max_blks);
  241. src = (be128 *)walk.src.virt.addr;
  242. dst = (be128 *)walk.dst.virt.addr;
  243. }
  244. return err;
  245. }
  246. EXPORT_SYMBOL_GPL(lrw_crypt);
  247. static int init_tfm(struct crypto_tfm *tfm)
  248. {
  249. struct crypto_cipher *cipher;
  250. struct crypto_instance *inst = (void *)tfm->__crt_alg;
  251. struct crypto_spawn *spawn = crypto_instance_ctx(inst);
  252. struct priv *ctx = crypto_tfm_ctx(tfm);
  253. u32 *flags = &tfm->crt_flags;
  254. cipher = crypto_spawn_cipher(spawn);
  255. if (IS_ERR(cipher))
  256. return PTR_ERR(cipher);
  257. if (crypto_cipher_blocksize(cipher) != LRW_BLOCK_SIZE) {
  258. *flags |= CRYPTO_TFM_RES_BAD_BLOCK_LEN;
  259. crypto_free_cipher(cipher);
  260. return -EINVAL;
  261. }
  262. ctx->child = cipher;
  263. return 0;
  264. }
  265. static void exit_tfm(struct crypto_tfm *tfm)
  266. {
  267. struct priv *ctx = crypto_tfm_ctx(tfm);
  268. lrw_free_table(&ctx->table);
  269. crypto_free_cipher(ctx->child);
  270. }
  271. static struct crypto_instance *alloc(struct rtattr **tb)
  272. {
  273. struct crypto_instance *inst;
  274. struct crypto_alg *alg;
  275. int err;
  276. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_BLKCIPHER);
  277. if (err)
  278. return ERR_PTR(err);
  279. alg = crypto_get_attr_alg(tb, CRYPTO_ALG_TYPE_CIPHER,
  280. CRYPTO_ALG_TYPE_MASK);
  281. if (IS_ERR(alg))
  282. return ERR_CAST(alg);
  283. inst = crypto_alloc_instance("lrw", alg);
  284. if (IS_ERR(inst))
  285. goto out_put_alg;
  286. inst->alg.cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER;
  287. inst->alg.cra_priority = alg->cra_priority;
  288. inst->alg.cra_blocksize = alg->cra_blocksize;
  289. if (alg->cra_alignmask < 7) inst->alg.cra_alignmask = 7;
  290. else inst->alg.cra_alignmask = alg->cra_alignmask;
  291. inst->alg.cra_type = &crypto_blkcipher_type;
  292. if (!(alg->cra_blocksize % 4))
  293. inst->alg.cra_alignmask |= 3;
  294. inst->alg.cra_blkcipher.ivsize = alg->cra_blocksize;
  295. inst->alg.cra_blkcipher.min_keysize =
  296. alg->cra_cipher.cia_min_keysize + alg->cra_blocksize;
  297. inst->alg.cra_blkcipher.max_keysize =
  298. alg->cra_cipher.cia_max_keysize + alg->cra_blocksize;
  299. inst->alg.cra_ctxsize = sizeof(struct priv);
  300. inst->alg.cra_init = init_tfm;
  301. inst->alg.cra_exit = exit_tfm;
  302. inst->alg.cra_blkcipher.setkey = setkey;
  303. inst->alg.cra_blkcipher.encrypt = encrypt;
  304. inst->alg.cra_blkcipher.decrypt = decrypt;
  305. out_put_alg:
  306. crypto_mod_put(alg);
  307. return inst;
  308. }
  309. static void free(struct crypto_instance *inst)
  310. {
  311. crypto_drop_spawn(crypto_instance_ctx(inst));
  312. kfree(inst);
  313. }
  314. static struct crypto_template crypto_tmpl = {
  315. .name = "lrw",
  316. .alloc = alloc,
  317. .free = free,
  318. .module = THIS_MODULE,
  319. };
  320. static int __init crypto_module_init(void)
  321. {
  322. return crypto_register_template(&crypto_tmpl);
  323. }
  324. static void __exit crypto_module_exit(void)
  325. {
  326. crypto_unregister_template(&crypto_tmpl);
  327. }
  328. module_init(crypto_module_init);
  329. module_exit(crypto_module_exit);
  330. MODULE_LICENSE("GPL");
  331. MODULE_DESCRIPTION("LRW block cipher mode");
  332. MODULE_ALIAS_CRYPTO("lrw");