lrw.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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. unsigned int n = min(req->cryptlen, (unsigned int)PAGE_SIZE);
  232. rctx->ext = kmalloc(n, gfp);
  233. if (rctx->ext)
  234. subreq->cryptlen = n;
  235. }
  236. rctx->src = req->src;
  237. rctx->dst = req->dst;
  238. rctx->left = req->cryptlen;
  239. /* calculate first value of T */
  240. memcpy(&rctx->t, req->iv, sizeof(rctx->t));
  241. /* T <- I*Key2 */
  242. gf128mul_64k_bbe(&rctx->t, ctx->table.table);
  243. return 0;
  244. }
  245. static void exit_crypt(struct skcipher_request *req)
  246. {
  247. struct rctx *rctx = skcipher_request_ctx(req);
  248. rctx->left = 0;
  249. if (rctx->ext)
  250. kfree(rctx->ext);
  251. }
  252. static int do_encrypt(struct skcipher_request *req, int err)
  253. {
  254. struct rctx *rctx = skcipher_request_ctx(req);
  255. struct skcipher_request *subreq;
  256. subreq = &rctx->subreq;
  257. while (!err && rctx->left) {
  258. err = pre_crypt(req) ?:
  259. crypto_skcipher_encrypt(subreq) ?:
  260. post_crypt(req);
  261. if (err == -EINPROGRESS ||
  262. (err == -EBUSY &&
  263. req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  264. return err;
  265. }
  266. exit_crypt(req);
  267. return err;
  268. }
  269. static void encrypt_done(struct crypto_async_request *areq, int err)
  270. {
  271. struct skcipher_request *req = areq->data;
  272. struct skcipher_request *subreq;
  273. struct rctx *rctx;
  274. rctx = skcipher_request_ctx(req);
  275. if (err == -EINPROGRESS) {
  276. if (rctx->left != req->cryptlen)
  277. return;
  278. goto out;
  279. }
  280. subreq = &rctx->subreq;
  281. subreq->base.flags &= CRYPTO_TFM_REQ_MAY_BACKLOG;
  282. err = do_encrypt(req, err ?: post_crypt(req));
  283. if (rctx->left)
  284. return;
  285. out:
  286. skcipher_request_complete(req, err);
  287. }
  288. static int encrypt(struct skcipher_request *req)
  289. {
  290. return do_encrypt(req, init_crypt(req, encrypt_done));
  291. }
  292. static int do_decrypt(struct skcipher_request *req, int err)
  293. {
  294. struct rctx *rctx = skcipher_request_ctx(req);
  295. struct skcipher_request *subreq;
  296. subreq = &rctx->subreq;
  297. while (!err && rctx->left) {
  298. err = pre_crypt(req) ?:
  299. crypto_skcipher_decrypt(subreq) ?:
  300. post_crypt(req);
  301. if (err == -EINPROGRESS ||
  302. (err == -EBUSY &&
  303. req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
  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. int lrw_crypt(struct blkcipher_desc *desc, struct scatterlist *sdst,
  333. struct scatterlist *ssrc, unsigned int nbytes,
  334. struct lrw_crypt_req *req)
  335. {
  336. const unsigned int bsize = LRW_BLOCK_SIZE;
  337. const unsigned int max_blks = req->tbuflen / bsize;
  338. struct lrw_table_ctx *ctx = req->table_ctx;
  339. struct blkcipher_walk walk;
  340. unsigned int nblocks;
  341. be128 *iv, *src, *dst, *t;
  342. be128 *t_buf = req->tbuf;
  343. int err, i;
  344. BUG_ON(max_blks < 1);
  345. blkcipher_walk_init(&walk, sdst, ssrc, nbytes);
  346. err = blkcipher_walk_virt(desc, &walk);
  347. nbytes = walk.nbytes;
  348. if (!nbytes)
  349. return err;
  350. nblocks = min(walk.nbytes / bsize, max_blks);
  351. src = (be128 *)walk.src.virt.addr;
  352. dst = (be128 *)walk.dst.virt.addr;
  353. /* calculate first value of T */
  354. iv = (be128 *)walk.iv;
  355. t_buf[0] = *iv;
  356. /* T <- I*Key2 */
  357. gf128mul_64k_bbe(&t_buf[0], ctx->table);
  358. i = 0;
  359. goto first;
  360. for (;;) {
  361. do {
  362. for (i = 0; i < nblocks; i++) {
  363. /* T <- I*Key2, using the optimization
  364. * discussed in the specification */
  365. be128_xor(&t_buf[i], t,
  366. &ctx->mulinc[get_index128(iv)]);
  367. inc(iv);
  368. first:
  369. t = &t_buf[i];
  370. /* PP <- T xor P */
  371. be128_xor(dst + i, t, src + i);
  372. }
  373. /* CC <- E(Key2,PP) */
  374. req->crypt_fn(req->crypt_ctx, (u8 *)dst,
  375. nblocks * bsize);
  376. /* C <- T xor CC */
  377. for (i = 0; i < nblocks; i++)
  378. be128_xor(dst + i, dst + i, &t_buf[i]);
  379. src += nblocks;
  380. dst += nblocks;
  381. nbytes -= nblocks * bsize;
  382. nblocks = min(nbytes / bsize, max_blks);
  383. } while (nblocks > 0);
  384. err = blkcipher_walk_done(desc, &walk, nbytes);
  385. nbytes = walk.nbytes;
  386. if (!nbytes)
  387. break;
  388. nblocks = min(nbytes / bsize, max_blks);
  389. src = (be128 *)walk.src.virt.addr;
  390. dst = (be128 *)walk.dst.virt.addr;
  391. }
  392. return err;
  393. }
  394. EXPORT_SYMBOL_GPL(lrw_crypt);
  395. static int init_tfm(struct crypto_skcipher *tfm)
  396. {
  397. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  398. struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
  399. struct priv *ctx = crypto_skcipher_ctx(tfm);
  400. struct crypto_skcipher *cipher;
  401. cipher = crypto_spawn_skcipher(spawn);
  402. if (IS_ERR(cipher))
  403. return PTR_ERR(cipher);
  404. ctx->child = cipher;
  405. crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(cipher) +
  406. sizeof(struct rctx));
  407. return 0;
  408. }
  409. static void exit_tfm(struct crypto_skcipher *tfm)
  410. {
  411. struct priv *ctx = crypto_skcipher_ctx(tfm);
  412. lrw_free_table(&ctx->table);
  413. crypto_free_skcipher(ctx->child);
  414. }
  415. static void free(struct skcipher_instance *inst)
  416. {
  417. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  418. kfree(inst);
  419. }
  420. static int create(struct crypto_template *tmpl, struct rtattr **tb)
  421. {
  422. struct crypto_skcipher_spawn *spawn;
  423. struct skcipher_instance *inst;
  424. struct crypto_attr_type *algt;
  425. struct skcipher_alg *alg;
  426. const char *cipher_name;
  427. char ecb_name[CRYPTO_MAX_ALG_NAME];
  428. int err;
  429. algt = crypto_get_attr_type(tb);
  430. if (IS_ERR(algt))
  431. return PTR_ERR(algt);
  432. if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
  433. return -EINVAL;
  434. cipher_name = crypto_attr_alg_name(tb[1]);
  435. if (IS_ERR(cipher_name))
  436. return PTR_ERR(cipher_name);
  437. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  438. if (!inst)
  439. return -ENOMEM;
  440. spawn = skcipher_instance_ctx(inst);
  441. crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
  442. err = crypto_grab_skcipher(spawn, cipher_name, 0,
  443. crypto_requires_sync(algt->type,
  444. algt->mask));
  445. if (err == -ENOENT) {
  446. err = -ENAMETOOLONG;
  447. if (snprintf(ecb_name, CRYPTO_MAX_ALG_NAME, "ecb(%s)",
  448. cipher_name) >= CRYPTO_MAX_ALG_NAME)
  449. goto err_free_inst;
  450. err = crypto_grab_skcipher(spawn, ecb_name, 0,
  451. crypto_requires_sync(algt->type,
  452. algt->mask));
  453. }
  454. if (err)
  455. goto err_free_inst;
  456. alg = crypto_skcipher_spawn_alg(spawn);
  457. err = -EINVAL;
  458. if (alg->base.cra_blocksize != LRW_BLOCK_SIZE)
  459. goto err_drop_spawn;
  460. if (crypto_skcipher_alg_ivsize(alg))
  461. goto err_drop_spawn;
  462. err = crypto_inst_setname(skcipher_crypto_instance(inst), "lrw",
  463. &alg->base);
  464. if (err)
  465. goto err_drop_spawn;
  466. err = -EINVAL;
  467. cipher_name = alg->base.cra_name;
  468. /* Alas we screwed up the naming so we have to mangle the
  469. * cipher name.
  470. */
  471. if (!strncmp(cipher_name, "ecb(", 4)) {
  472. unsigned len;
  473. len = strlcpy(ecb_name, cipher_name + 4, sizeof(ecb_name));
  474. if (len < 2 || len >= sizeof(ecb_name))
  475. goto err_drop_spawn;
  476. if (ecb_name[len - 1] != ')')
  477. goto err_drop_spawn;
  478. ecb_name[len - 1] = 0;
  479. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  480. "lrw(%s)", ecb_name) >= CRYPTO_MAX_ALG_NAME)
  481. return -ENAMETOOLONG;
  482. }
  483. inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  484. inst->alg.base.cra_priority = alg->base.cra_priority;
  485. inst->alg.base.cra_blocksize = LRW_BLOCK_SIZE;
  486. inst->alg.base.cra_alignmask = alg->base.cra_alignmask |
  487. (__alignof__(u64) - 1);
  488. inst->alg.ivsize = LRW_BLOCK_SIZE;
  489. inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) +
  490. LRW_BLOCK_SIZE;
  491. inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) +
  492. LRW_BLOCK_SIZE;
  493. inst->alg.base.cra_ctxsize = sizeof(struct priv);
  494. inst->alg.init = init_tfm;
  495. inst->alg.exit = exit_tfm;
  496. inst->alg.setkey = setkey;
  497. inst->alg.encrypt = encrypt;
  498. inst->alg.decrypt = decrypt;
  499. inst->free = free;
  500. err = skcipher_register_instance(tmpl, inst);
  501. if (err)
  502. goto err_drop_spawn;
  503. out:
  504. return err;
  505. err_drop_spawn:
  506. crypto_drop_skcipher(spawn);
  507. err_free_inst:
  508. kfree(inst);
  509. goto out;
  510. }
  511. static struct crypto_template crypto_tmpl = {
  512. .name = "lrw",
  513. .create = create,
  514. .module = THIS_MODULE,
  515. };
  516. static int __init crypto_module_init(void)
  517. {
  518. return crypto_register_template(&crypto_tmpl);
  519. }
  520. static void __exit crypto_module_exit(void)
  521. {
  522. crypto_unregister_template(&crypto_tmpl);
  523. }
  524. module_init(crypto_module_init);
  525. module_exit(crypto_module_exit);
  526. MODULE_LICENSE("GPL");
  527. MODULE_DESCRIPTION("LRW block cipher mode");
  528. MODULE_ALIAS_CRYPTO("lrw");