lrw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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_BUFFER_SIZE 128u
  30. #define LRW_BLOCK_SIZE 16
  31. struct priv {
  32. struct crypto_skcipher *child;
  33. /*
  34. * optimizes multiplying a random (non incrementing, as at the
  35. * start of a new sector) value with key2, we could also have
  36. * used 4k optimization tables or no optimization at all. In the
  37. * latter case we would have to store key2 here
  38. */
  39. struct gf128mul_64k *table;
  40. /*
  41. * stores:
  42. * key2*{ 0,0,...0,0,0,0,1 }, key2*{ 0,0,...0,0,0,1,1 },
  43. * key2*{ 0,0,...0,0,1,1,1 }, key2*{ 0,0,...0,1,1,1,1 }
  44. * key2*{ 0,0,...1,1,1,1,1 }, etc
  45. * needed for optimized multiplication of incrementing values
  46. * with key2
  47. */
  48. be128 mulinc[128];
  49. };
  50. struct rctx {
  51. be128 buf[LRW_BUFFER_SIZE / sizeof(be128)];
  52. be128 t;
  53. be128 *ext;
  54. struct scatterlist srcbuf[2];
  55. struct scatterlist dstbuf[2];
  56. struct scatterlist *src;
  57. struct scatterlist *dst;
  58. unsigned int left;
  59. struct skcipher_request subreq;
  60. };
  61. static inline void setbit128_bbe(void *b, int bit)
  62. {
  63. __set_bit(bit ^ (0x80 -
  64. #ifdef __BIG_ENDIAN
  65. BITS_PER_LONG
  66. #else
  67. BITS_PER_BYTE
  68. #endif
  69. ), b);
  70. }
  71. static int setkey(struct crypto_skcipher *parent, const u8 *key,
  72. unsigned int keylen)
  73. {
  74. struct priv *ctx = crypto_skcipher_ctx(parent);
  75. struct crypto_skcipher *child = ctx->child;
  76. int err, bsize = LRW_BLOCK_SIZE;
  77. const u8 *tweak = key + keylen - bsize;
  78. be128 tmp = { 0 };
  79. int i;
  80. crypto_skcipher_clear_flags(child, CRYPTO_TFM_REQ_MASK);
  81. crypto_skcipher_set_flags(child, crypto_skcipher_get_flags(parent) &
  82. CRYPTO_TFM_REQ_MASK);
  83. err = crypto_skcipher_setkey(child, key, keylen - bsize);
  84. crypto_skcipher_set_flags(parent, crypto_skcipher_get_flags(child) &
  85. CRYPTO_TFM_RES_MASK);
  86. if (err)
  87. return err;
  88. if (ctx->table)
  89. gf128mul_free_64k(ctx->table);
  90. /* initialize multiplication table for Key2 */
  91. ctx->table = gf128mul_init_64k_bbe((be128 *)tweak);
  92. if (!ctx->table)
  93. return -ENOMEM;
  94. /* initialize optimization table */
  95. for (i = 0; i < 128; i++) {
  96. setbit128_bbe(&tmp, i);
  97. ctx->mulinc[i] = tmp;
  98. gf128mul_64k_bbe(&ctx->mulinc[i], ctx->table);
  99. }
  100. return 0;
  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. /* this returns the number of consequative 1 bits starting
  109. * from the right, get_index128(00 00 00 00 00 00 ... 00 00 10 FB) = 2 */
  110. static inline int get_index128(be128 *block)
  111. {
  112. int x;
  113. __be32 *p = (__be32 *) block;
  114. for (p += 3, x = 0; x < 128; p--, x += 32) {
  115. u32 val = be32_to_cpup(p);
  116. if (!~val)
  117. continue;
  118. return x + ffz(val);
  119. }
  120. return x;
  121. }
  122. static int post_crypt(struct skcipher_request *req)
  123. {
  124. struct rctx *rctx = skcipher_request_ctx(req);
  125. be128 *buf = rctx->ext ?: rctx->buf;
  126. struct skcipher_request *subreq;
  127. const int bs = LRW_BLOCK_SIZE;
  128. struct skcipher_walk w;
  129. struct scatterlist *sg;
  130. unsigned offset;
  131. int err;
  132. subreq = &rctx->subreq;
  133. err = skcipher_walk_virt(&w, subreq, false);
  134. while (w.nbytes) {
  135. unsigned int avail = w.nbytes;
  136. be128 *wdst;
  137. wdst = w.dst.virt.addr;
  138. do {
  139. be128_xor(wdst, buf++, wdst);
  140. wdst++;
  141. } while ((avail -= bs) >= bs);
  142. err = skcipher_walk_done(&w, avail);
  143. }
  144. rctx->left -= subreq->cryptlen;
  145. if (err || !rctx->left)
  146. goto out;
  147. rctx->dst = rctx->dstbuf;
  148. scatterwalk_done(&w.out, 0, 1);
  149. sg = w.out.sg;
  150. offset = w.out.offset;
  151. if (rctx->dst != sg) {
  152. rctx->dst[0] = *sg;
  153. sg_unmark_end(rctx->dst);
  154. scatterwalk_crypto_chain(rctx->dst, sg_next(sg), 2);
  155. }
  156. rctx->dst[0].length -= offset - sg->offset;
  157. rctx->dst[0].offset = offset;
  158. out:
  159. return err;
  160. }
  161. static int pre_crypt(struct skcipher_request *req)
  162. {
  163. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  164. struct rctx *rctx = skcipher_request_ctx(req);
  165. struct priv *ctx = crypto_skcipher_ctx(tfm);
  166. be128 *buf = rctx->ext ?: rctx->buf;
  167. struct skcipher_request *subreq;
  168. const int bs = LRW_BLOCK_SIZE;
  169. struct skcipher_walk w;
  170. struct scatterlist *sg;
  171. unsigned cryptlen;
  172. unsigned offset;
  173. be128 *iv;
  174. bool more;
  175. int err;
  176. subreq = &rctx->subreq;
  177. skcipher_request_set_tfm(subreq, tfm);
  178. cryptlen = subreq->cryptlen;
  179. more = rctx->left > cryptlen;
  180. if (!more)
  181. cryptlen = rctx->left;
  182. skcipher_request_set_crypt(subreq, rctx->src, rctx->dst,
  183. cryptlen, req->iv);
  184. err = skcipher_walk_virt(&w, subreq, false);
  185. iv = w.iv;
  186. while (w.nbytes) {
  187. unsigned int avail = w.nbytes;
  188. be128 *wsrc;
  189. be128 *wdst;
  190. wsrc = w.src.virt.addr;
  191. wdst = w.dst.virt.addr;
  192. do {
  193. *buf++ = rctx->t;
  194. be128_xor(wdst++, &rctx->t, wsrc++);
  195. /* T <- I*Key2, using the optimization
  196. * discussed in the specification */
  197. be128_xor(&rctx->t, &rctx->t,
  198. &ctx->mulinc[get_index128(iv)]);
  199. inc(iv);
  200. } while ((avail -= bs) >= bs);
  201. err = skcipher_walk_done(&w, avail);
  202. }
  203. skcipher_request_set_tfm(subreq, ctx->child);
  204. skcipher_request_set_crypt(subreq, rctx->dst, rctx->dst,
  205. cryptlen, NULL);
  206. if (err || !more)
  207. goto out;
  208. rctx->src = rctx->srcbuf;
  209. scatterwalk_done(&w.in, 0, 1);
  210. sg = w.in.sg;
  211. offset = w.in.offset;
  212. if (rctx->src != sg) {
  213. rctx->src[0] = *sg;
  214. sg_unmark_end(rctx->src);
  215. scatterwalk_crypto_chain(rctx->src, sg_next(sg), 2);
  216. }
  217. rctx->src[0].length -= offset - sg->offset;
  218. rctx->src[0].offset = offset;
  219. out:
  220. return err;
  221. }
  222. static int init_crypt(struct skcipher_request *req, crypto_completion_t done)
  223. {
  224. struct priv *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
  225. struct rctx *rctx = skcipher_request_ctx(req);
  226. struct skcipher_request *subreq;
  227. gfp_t gfp;
  228. subreq = &rctx->subreq;
  229. skcipher_request_set_callback(subreq, req->base.flags, done, req);
  230. gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  231. GFP_ATOMIC;
  232. rctx->ext = NULL;
  233. subreq->cryptlen = LRW_BUFFER_SIZE;
  234. if (req->cryptlen > LRW_BUFFER_SIZE) {
  235. unsigned int n = min(req->cryptlen, (unsigned int)PAGE_SIZE);
  236. rctx->ext = kmalloc(n, gfp);
  237. if (rctx->ext)
  238. subreq->cryptlen = n;
  239. }
  240. rctx->src = req->src;
  241. rctx->dst = req->dst;
  242. rctx->left = req->cryptlen;
  243. /* calculate first value of T */
  244. memcpy(&rctx->t, req->iv, sizeof(rctx->t));
  245. /* T <- I*Key2 */
  246. gf128mul_64k_bbe(&rctx->t, ctx->table);
  247. return 0;
  248. }
  249. static void exit_crypt(struct skcipher_request *req)
  250. {
  251. struct rctx *rctx = skcipher_request_ctx(req);
  252. rctx->left = 0;
  253. if (rctx->ext)
  254. kzfree(rctx->ext);
  255. }
  256. static int do_encrypt(struct skcipher_request *req, int err)
  257. {
  258. struct rctx *rctx = skcipher_request_ctx(req);
  259. struct skcipher_request *subreq;
  260. subreq = &rctx->subreq;
  261. while (!err && rctx->left) {
  262. err = pre_crypt(req) ?:
  263. crypto_skcipher_encrypt(subreq) ?:
  264. post_crypt(req);
  265. if (err == -EINPROGRESS || err == -EBUSY)
  266. return err;
  267. }
  268. exit_crypt(req);
  269. return err;
  270. }
  271. static void encrypt_done(struct crypto_async_request *areq, int err)
  272. {
  273. struct skcipher_request *req = areq->data;
  274. struct skcipher_request *subreq;
  275. struct rctx *rctx;
  276. rctx = skcipher_request_ctx(req);
  277. if (err == -EINPROGRESS) {
  278. if (rctx->left != req->cryptlen)
  279. return;
  280. goto out;
  281. }
  282. subreq = &rctx->subreq;
  283. subreq->base.flags &= CRYPTO_TFM_REQ_MAY_BACKLOG;
  284. err = do_encrypt(req, err ?: post_crypt(req));
  285. if (rctx->left)
  286. return;
  287. out:
  288. skcipher_request_complete(req, err);
  289. }
  290. static int encrypt(struct skcipher_request *req)
  291. {
  292. return do_encrypt(req, init_crypt(req, encrypt_done));
  293. }
  294. static int do_decrypt(struct skcipher_request *req, int err)
  295. {
  296. struct rctx *rctx = skcipher_request_ctx(req);
  297. struct skcipher_request *subreq;
  298. subreq = &rctx->subreq;
  299. while (!err && rctx->left) {
  300. err = pre_crypt(req) ?:
  301. crypto_skcipher_decrypt(subreq) ?:
  302. post_crypt(req);
  303. if (err == -EINPROGRESS || err == -EBUSY)
  304. return err;
  305. }
  306. exit_crypt(req);
  307. return err;
  308. }
  309. static void decrypt_done(struct crypto_async_request *areq, int err)
  310. {
  311. struct skcipher_request *req = areq->data;
  312. struct skcipher_request *subreq;
  313. struct rctx *rctx;
  314. rctx = skcipher_request_ctx(req);
  315. if (err == -EINPROGRESS) {
  316. if (rctx->left != req->cryptlen)
  317. return;
  318. goto out;
  319. }
  320. subreq = &rctx->subreq;
  321. subreq->base.flags &= CRYPTO_TFM_REQ_MAY_BACKLOG;
  322. err = do_decrypt(req, err ?: post_crypt(req));
  323. if (rctx->left)
  324. return;
  325. out:
  326. skcipher_request_complete(req, err);
  327. }
  328. static int decrypt(struct skcipher_request *req)
  329. {
  330. return do_decrypt(req, init_crypt(req, decrypt_done));
  331. }
  332. static int init_tfm(struct crypto_skcipher *tfm)
  333. {
  334. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  335. struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
  336. struct priv *ctx = crypto_skcipher_ctx(tfm);
  337. struct crypto_skcipher *cipher;
  338. cipher = crypto_spawn_skcipher(spawn);
  339. if (IS_ERR(cipher))
  340. return PTR_ERR(cipher);
  341. ctx->child = cipher;
  342. crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(cipher) +
  343. sizeof(struct rctx));
  344. return 0;
  345. }
  346. static void exit_tfm(struct crypto_skcipher *tfm)
  347. {
  348. struct priv *ctx = crypto_skcipher_ctx(tfm);
  349. if (ctx->table)
  350. gf128mul_free_64k(ctx->table);
  351. crypto_free_skcipher(ctx->child);
  352. }
  353. static void free(struct skcipher_instance *inst)
  354. {
  355. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  356. kfree(inst);
  357. }
  358. static int create(struct crypto_template *tmpl, struct rtattr **tb)
  359. {
  360. struct crypto_skcipher_spawn *spawn;
  361. struct skcipher_instance *inst;
  362. struct crypto_attr_type *algt;
  363. struct skcipher_alg *alg;
  364. const char *cipher_name;
  365. char ecb_name[CRYPTO_MAX_ALG_NAME];
  366. int err;
  367. algt = crypto_get_attr_type(tb);
  368. if (IS_ERR(algt))
  369. return PTR_ERR(algt);
  370. if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
  371. return -EINVAL;
  372. cipher_name = crypto_attr_alg_name(tb[1]);
  373. if (IS_ERR(cipher_name))
  374. return PTR_ERR(cipher_name);
  375. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  376. if (!inst)
  377. return -ENOMEM;
  378. spawn = skcipher_instance_ctx(inst);
  379. crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
  380. err = crypto_grab_skcipher(spawn, cipher_name, 0,
  381. crypto_requires_sync(algt->type,
  382. algt->mask));
  383. if (err == -ENOENT) {
  384. err = -ENAMETOOLONG;
  385. if (snprintf(ecb_name, CRYPTO_MAX_ALG_NAME, "ecb(%s)",
  386. cipher_name) >= CRYPTO_MAX_ALG_NAME)
  387. goto err_free_inst;
  388. err = crypto_grab_skcipher(spawn, ecb_name, 0,
  389. crypto_requires_sync(algt->type,
  390. algt->mask));
  391. }
  392. if (err)
  393. goto err_free_inst;
  394. alg = crypto_skcipher_spawn_alg(spawn);
  395. err = -EINVAL;
  396. if (alg->base.cra_blocksize != LRW_BLOCK_SIZE)
  397. goto err_drop_spawn;
  398. if (crypto_skcipher_alg_ivsize(alg))
  399. goto err_drop_spawn;
  400. err = crypto_inst_setname(skcipher_crypto_instance(inst), "lrw",
  401. &alg->base);
  402. if (err)
  403. goto err_drop_spawn;
  404. err = -EINVAL;
  405. cipher_name = alg->base.cra_name;
  406. /* Alas we screwed up the naming so we have to mangle the
  407. * cipher name.
  408. */
  409. if (!strncmp(cipher_name, "ecb(", 4)) {
  410. unsigned len;
  411. len = strlcpy(ecb_name, cipher_name + 4, sizeof(ecb_name));
  412. if (len < 2 || len >= sizeof(ecb_name))
  413. goto err_drop_spawn;
  414. if (ecb_name[len - 1] != ')')
  415. goto err_drop_spawn;
  416. ecb_name[len - 1] = 0;
  417. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  418. "lrw(%s)", ecb_name) >= CRYPTO_MAX_ALG_NAME) {
  419. err = -ENAMETOOLONG;
  420. goto err_drop_spawn;
  421. }
  422. } else
  423. goto err_drop_spawn;
  424. inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  425. inst->alg.base.cra_priority = alg->base.cra_priority;
  426. inst->alg.base.cra_blocksize = LRW_BLOCK_SIZE;
  427. inst->alg.base.cra_alignmask = alg->base.cra_alignmask |
  428. (__alignof__(u64) - 1);
  429. inst->alg.ivsize = LRW_BLOCK_SIZE;
  430. inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) +
  431. LRW_BLOCK_SIZE;
  432. inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) +
  433. LRW_BLOCK_SIZE;
  434. inst->alg.base.cra_ctxsize = sizeof(struct priv);
  435. inst->alg.init = init_tfm;
  436. inst->alg.exit = exit_tfm;
  437. inst->alg.setkey = setkey;
  438. inst->alg.encrypt = encrypt;
  439. inst->alg.decrypt = decrypt;
  440. inst->free = free;
  441. err = skcipher_register_instance(tmpl, inst);
  442. if (err)
  443. goto err_drop_spawn;
  444. out:
  445. return err;
  446. err_drop_spawn:
  447. crypto_drop_skcipher(spawn);
  448. err_free_inst:
  449. kfree(inst);
  450. goto out;
  451. }
  452. static struct crypto_template crypto_tmpl = {
  453. .name = "lrw",
  454. .create = create,
  455. .module = THIS_MODULE,
  456. };
  457. static int __init crypto_module_init(void)
  458. {
  459. return crypto_register_template(&crypto_tmpl);
  460. }
  461. static void __exit crypto_module_exit(void)
  462. {
  463. crypto_unregister_template(&crypto_tmpl);
  464. }
  465. module_init(crypto_module_init);
  466. module_exit(crypto_module_exit);
  467. MODULE_LICENSE("GPL");
  468. MODULE_DESCRIPTION("LRW block cipher mode");
  469. MODULE_ALIAS_CRYPTO("lrw");