lrw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/internal/skcipher.h>
  20. #include <crypto/scatterwalk.h>
  21. #include <linux/err.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/scatterlist.h>
  26. #include <linux/slab.h>
  27. #include <crypto/b128ops.h>
  28. #include <crypto/gf128mul.h>
  29. #define LRW_BLOCK_SIZE 16
  30. struct priv {
  31. struct crypto_skcipher *child;
  32. /*
  33. * optimizes multiplying a random (non incrementing, as at the
  34. * start of a new sector) value with key2, we could also have
  35. * used 4k optimization tables or no optimization at all. In the
  36. * latter case we would have to store key2 here
  37. */
  38. struct gf128mul_64k *table;
  39. /*
  40. * stores:
  41. * key2*{ 0,0,...0,0,0,0,1 }, key2*{ 0,0,...0,0,0,1,1 },
  42. * key2*{ 0,0,...0,0,1,1,1 }, key2*{ 0,0,...0,1,1,1,1 }
  43. * key2*{ 0,0,...1,1,1,1,1 }, etc
  44. * needed for optimized multiplication of incrementing values
  45. * with key2
  46. */
  47. be128 mulinc[128];
  48. };
  49. struct rctx {
  50. be128 t;
  51. struct skcipher_request subreq;
  52. };
  53. static inline void setbit128_bbe(void *b, int bit)
  54. {
  55. __set_bit(bit ^ (0x80 -
  56. #ifdef __BIG_ENDIAN
  57. BITS_PER_LONG
  58. #else
  59. BITS_PER_BYTE
  60. #endif
  61. ), b);
  62. }
  63. static int setkey(struct crypto_skcipher *parent, const u8 *key,
  64. unsigned int keylen)
  65. {
  66. struct priv *ctx = crypto_skcipher_ctx(parent);
  67. struct crypto_skcipher *child = ctx->child;
  68. int err, bsize = LRW_BLOCK_SIZE;
  69. const u8 *tweak = key + keylen - bsize;
  70. be128 tmp = { 0 };
  71. int i;
  72. crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  73. crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
  74. CRYPTO_TFM_REQ_MASK);
  75. err = crypto_skcipher_setkey(child, key, keylen - bsize);
  76. crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
  77. CRYPTO_TFM_RES_MASK);
  78. if (err)
  79. return err;
  80. if (ctx->table)
  81. gf128mul_free_64k(ctx->table);
  82. /* initialize multiplication table for Key2 */
  83. ctx->table = gf128mul_init_64k_bbe((be128 *)tweak);
  84. if (!ctx->table)
  85. return -ENOMEM;
  86. /* initialize optimization table */
  87. for (i = 0; i < 128; i++) {
  88. setbit128_bbe(&tmp, i);
  89. ctx->mulinc[i] = tmp;
  90. gf128mul_64k_bbe(&ctx->mulinc[i], ctx->table);
  91. }
  92. return 0;
  93. }
  94. /*
  95. * Returns the number of trailing '1' bits in the words of the counter, which is
  96. * represented by 4 32-bit words, arranged from least to most significant.
  97. * At the same time, increments the counter by one.
  98. *
  99. * For example:
  100. *
  101. * u32 counter[4] = { 0xFFFFFFFF, 0x1, 0x0, 0x0 };
  102. * int i = next_index(&counter);
  103. * // i == 33, counter == { 0x0, 0x2, 0x0, 0x0 }
  104. */
  105. static int next_index(u32 *counter)
  106. {
  107. int i, res = 0;
  108. for (i = 0; i < 4; i++) {
  109. if (counter[i] + 1 != 0)
  110. return res + ffz(counter[i]++);
  111. counter[i] = 0;
  112. res += 32;
  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. /*
  122. * We compute the tweak masks twice (both before and after the ECB encryption or
  123. * decryption) to avoid having to allocate a temporary buffer and/or make
  124. * mutliple calls to the 'ecb(..)' instance, which usually would be slower than
  125. * just doing the next_index() calls again.
  126. */
  127. static int xor_tweak(struct skcipher_request *req, bool second_pass)
  128. {
  129. const int bs = LRW_BLOCK_SIZE;
  130. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  131. struct priv *ctx = crypto_skcipher_ctx(tfm);
  132. struct rctx *rctx = skcipher_request_ctx(req);
  133. be128 t = rctx->t;
  134. struct skcipher_walk w;
  135. __be32 *iv;
  136. u32 counter[4];
  137. int err;
  138. if (second_pass) {
  139. req = &rctx->subreq;
  140. /* set to our TFM to enforce correct alignment: */
  141. skcipher_request_set_tfm(req, tfm);
  142. }
  143. err = skcipher_walk_virt(&w, req, false);
  144. iv = (__be32 *)w.iv;
  145. counter[0] = be32_to_cpu(iv[3]);
  146. counter[1] = be32_to_cpu(iv[2]);
  147. counter[2] = be32_to_cpu(iv[1]);
  148. counter[3] = be32_to_cpu(iv[0]);
  149. while (w.nbytes) {
  150. unsigned int avail = w.nbytes;
  151. be128 *wsrc;
  152. be128 *wdst;
  153. wsrc = w.src.virt.addr;
  154. wdst = w.dst.virt.addr;
  155. do {
  156. be128_xor(wdst++, &t, wsrc++);
  157. /* T <- I*Key2, using the optimization
  158. * discussed in the specification */
  159. be128_xor(&t, &t, &ctx->mulinc[next_index(counter)]);
  160. } while ((avail -= bs) >= bs);
  161. if (second_pass && w.nbytes == w.total) {
  162. iv[0] = cpu_to_be32(counter[3]);
  163. iv[1] = cpu_to_be32(counter[2]);
  164. iv[2] = cpu_to_be32(counter[1]);
  165. iv[3] = cpu_to_be32(counter[0]);
  166. }
  167. err = skcipher_walk_done(&w, avail);
  168. }
  169. return err;
  170. }
  171. static int xor_tweak_pre(struct skcipher_request *req)
  172. {
  173. return xor_tweak(req, false);
  174. }
  175. static int xor_tweak_post(struct skcipher_request *req)
  176. {
  177. return xor_tweak(req, true);
  178. }
  179. static void crypt_done(struct crypto_async_request *areq, int err)
  180. {
  181. struct skcipher_request *req = areq->data;
  182. if (!err)
  183. err = xor_tweak_post(req);
  184. skcipher_request_complete(req, err);
  185. }
  186. static void init_crypt(struct skcipher_request *req)
  187. {
  188. struct priv *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
  189. struct rctx *rctx = skcipher_request_ctx(req);
  190. struct skcipher_request *subreq = &rctx->subreq;
  191. skcipher_request_set_tfm(subreq, ctx->child);
  192. skcipher_request_set_callback(subreq, req->base.flags, crypt_done, req);
  193. /* pass req->iv as IV (will be used by xor_tweak, ECB will ignore it) */
  194. skcipher_request_set_crypt(subreq, req->dst, req->dst,
  195. req->cryptlen, req->iv);
  196. /* calculate first value of T */
  197. memcpy(&rctx->t, req->iv, sizeof(rctx->t));
  198. /* T <- I*Key2 */
  199. gf128mul_64k_bbe(&rctx->t, ctx->table);
  200. }
  201. static int encrypt(struct skcipher_request *req)
  202. {
  203. struct rctx *rctx = skcipher_request_ctx(req);
  204. struct skcipher_request *subreq = &rctx->subreq;
  205. init_crypt(req);
  206. return xor_tweak_pre(req) ?:
  207. crypto_skcipher_encrypt(subreq) ?:
  208. xor_tweak_post(req);
  209. }
  210. static int decrypt(struct skcipher_request *req)
  211. {
  212. struct rctx *rctx = skcipher_request_ctx(req);
  213. struct skcipher_request *subreq = &rctx->subreq;
  214. init_crypt(req);
  215. return xor_tweak_pre(req) ?:
  216. crypto_skcipher_decrypt(subreq) ?:
  217. xor_tweak_post(req);
  218. }
  219. static int init_tfm(struct crypto_skcipher *tfm)
  220. {
  221. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  222. struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
  223. struct priv *ctx = crypto_skcipher_ctx(tfm);
  224. struct crypto_skcipher *cipher;
  225. cipher = crypto_spawn_skcipher(spawn);
  226. if (IS_ERR(cipher))
  227. return PTR_ERR(cipher);
  228. ctx->child = cipher;
  229. crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(cipher) +
  230. sizeof(struct rctx));
  231. return 0;
  232. }
  233. static void exit_tfm(struct crypto_skcipher *tfm)
  234. {
  235. struct priv *ctx = crypto_skcipher_ctx(tfm);
  236. if (ctx->table)
  237. gf128mul_free_64k(ctx->table);
  238. crypto_free_skcipher(ctx->child);
  239. }
  240. static void free(struct skcipher_instance *inst)
  241. {
  242. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  243. kfree(inst);
  244. }
  245. static int create(struct crypto_template *tmpl, struct rtattr **tb)
  246. {
  247. struct crypto_skcipher_spawn *spawn;
  248. struct skcipher_instance *inst;
  249. struct crypto_attr_type *algt;
  250. struct skcipher_alg *alg;
  251. const char *cipher_name;
  252. char ecb_name[CRYPTO_MAX_ALG_NAME];
  253. int err;
  254. algt = crypto_get_attr_type(tb);
  255. if (IS_ERR(algt))
  256. return PTR_ERR(algt);
  257. if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
  258. return -EINVAL;
  259. cipher_name = crypto_attr_alg_name(tb[1]);
  260. if (IS_ERR(cipher_name))
  261. return PTR_ERR(cipher_name);
  262. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  263. if (!inst)
  264. return -ENOMEM;
  265. spawn = skcipher_instance_ctx(inst);
  266. crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
  267. err = crypto_grab_skcipher(spawn, cipher_name, 0,
  268. crypto_requires_sync(algt->type,
  269. algt->mask));
  270. if (err == -ENOENT) {
  271. err = -ENAMETOOLONG;
  272. if (snprintf(ecb_name, CRYPTO_MAX_ALG_NAME, "ecb(%s)",
  273. cipher_name) >= CRYPTO_MAX_ALG_NAME)
  274. goto err_free_inst;
  275. err = crypto_grab_skcipher(spawn, ecb_name, 0,
  276. crypto_requires_sync(algt->type,
  277. algt->mask));
  278. }
  279. if (err)
  280. goto err_free_inst;
  281. alg = crypto_skcipher_spawn_alg(spawn);
  282. err = -EINVAL;
  283. if (alg->base.cra_blocksize != LRW_BLOCK_SIZE)
  284. goto err_drop_spawn;
  285. if (crypto_skcipher_alg_ivsize(alg))
  286. goto err_drop_spawn;
  287. err = crypto_inst_setname(skcipher_crypto_instance(inst), "lrw",
  288. &alg->base);
  289. if (err)
  290. goto err_drop_spawn;
  291. err = -EINVAL;
  292. cipher_name = alg->base.cra_name;
  293. /* Alas we screwed up the naming so we have to mangle the
  294. * cipher name.
  295. */
  296. if (!strncmp(cipher_name, "ecb(", 4)) {
  297. unsigned len;
  298. len = strlcpy(ecb_name, cipher_name + 4, sizeof(ecb_name));
  299. if (len < 2 || len >= sizeof(ecb_name))
  300. goto err_drop_spawn;
  301. if (ecb_name[len - 1] != ')')
  302. goto err_drop_spawn;
  303. ecb_name[len - 1] = 0;
  304. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  305. "lrw(%s)", ecb_name) >= CRYPTO_MAX_ALG_NAME) {
  306. err = -ENAMETOOLONG;
  307. goto err_drop_spawn;
  308. }
  309. } else
  310. goto err_drop_spawn;
  311. inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  312. inst->alg.base.cra_priority = alg->base.cra_priority;
  313. inst->alg.base.cra_blocksize = LRW_BLOCK_SIZE;
  314. inst->alg.base.cra_alignmask = alg->base.cra_alignmask |
  315. (__alignof__(__be32) - 1);
  316. inst->alg.ivsize = LRW_BLOCK_SIZE;
  317. inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) +
  318. LRW_BLOCK_SIZE;
  319. inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) +
  320. LRW_BLOCK_SIZE;
  321. inst->alg.base.cra_ctxsize = sizeof(struct priv);
  322. inst->alg.init = init_tfm;
  323. inst->alg.exit = exit_tfm;
  324. inst->alg.setkey = setkey;
  325. inst->alg.encrypt = encrypt;
  326. inst->alg.decrypt = decrypt;
  327. inst->free = free;
  328. err = skcipher_register_instance(tmpl, inst);
  329. if (err)
  330. goto err_drop_spawn;
  331. out:
  332. return err;
  333. err_drop_spawn:
  334. crypto_drop_skcipher(spawn);
  335. err_free_inst:
  336. kfree(inst);
  337. goto out;
  338. }
  339. static struct crypto_template crypto_tmpl = {
  340. .name = "lrw",
  341. .create = create,
  342. .module = THIS_MODULE,
  343. };
  344. static int __init crypto_module_init(void)
  345. {
  346. return crypto_register_template(&crypto_tmpl);
  347. }
  348. static void __exit crypto_module_exit(void)
  349. {
  350. crypto_unregister_template(&crypto_tmpl);
  351. }
  352. module_init(crypto_module_init);
  353. module_exit(crypto_module_exit);
  354. MODULE_LICENSE("GPL");
  355. MODULE_DESCRIPTION("LRW block cipher mode");
  356. MODULE_ALIAS_CRYPTO("lrw");