lrw.c 15 KB

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