lrw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. /*
  121. * If we get here, then x == 128 and we are incrementing the counter
  122. * from all ones to all zeros. This means we must return index 127, i.e.
  123. * the one corresponding to key2*{ 1,...,1 }.
  124. */
  125. return 127;
  126. }
  127. static int post_crypt(struct skcipher_request *req)
  128. {
  129. struct rctx *rctx = skcipher_request_ctx(req);
  130. be128 *buf = rctx->ext ?: rctx->buf;
  131. struct skcipher_request *subreq;
  132. const int bs = LRW_BLOCK_SIZE;
  133. struct skcipher_walk w;
  134. struct scatterlist *sg;
  135. unsigned offset;
  136. int err;
  137. subreq = &rctx->subreq;
  138. err = skcipher_walk_virt(&w, subreq, false);
  139. while (w.nbytes) {
  140. unsigned int avail = w.nbytes;
  141. be128 *wdst;
  142. wdst = w.dst.virt.addr;
  143. do {
  144. be128_xor(wdst, buf++, wdst);
  145. wdst++;
  146. } while ((avail -= bs) >= bs);
  147. err = skcipher_walk_done(&w, avail);
  148. }
  149. rctx->left -= subreq->cryptlen;
  150. if (err || !rctx->left)
  151. goto out;
  152. rctx->dst = rctx->dstbuf;
  153. scatterwalk_done(&w.out, 0, 1);
  154. sg = w.out.sg;
  155. offset = w.out.offset;
  156. if (rctx->dst != sg) {
  157. rctx->dst[0] = *sg;
  158. sg_unmark_end(rctx->dst);
  159. scatterwalk_crypto_chain(rctx->dst, sg_next(sg), 2);
  160. }
  161. rctx->dst[0].length -= offset - sg->offset;
  162. rctx->dst[0].offset = offset;
  163. out:
  164. return err;
  165. }
  166. static int pre_crypt(struct skcipher_request *req)
  167. {
  168. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  169. struct rctx *rctx = skcipher_request_ctx(req);
  170. struct priv *ctx = crypto_skcipher_ctx(tfm);
  171. be128 *buf = rctx->ext ?: rctx->buf;
  172. struct skcipher_request *subreq;
  173. const int bs = LRW_BLOCK_SIZE;
  174. struct skcipher_walk w;
  175. struct scatterlist *sg;
  176. unsigned cryptlen;
  177. unsigned offset;
  178. be128 *iv;
  179. bool more;
  180. int err;
  181. subreq = &rctx->subreq;
  182. skcipher_request_set_tfm(subreq, tfm);
  183. cryptlen = subreq->cryptlen;
  184. more = rctx->left > cryptlen;
  185. if (!more)
  186. cryptlen = rctx->left;
  187. skcipher_request_set_crypt(subreq, rctx->src, rctx->dst,
  188. cryptlen, req->iv);
  189. err = skcipher_walk_virt(&w, subreq, false);
  190. iv = w.iv;
  191. while (w.nbytes) {
  192. unsigned int avail = w.nbytes;
  193. be128 *wsrc;
  194. be128 *wdst;
  195. wsrc = w.src.virt.addr;
  196. wdst = w.dst.virt.addr;
  197. do {
  198. *buf++ = rctx->t;
  199. be128_xor(wdst++, &rctx->t, wsrc++);
  200. /* T <- I*Key2, using the optimization
  201. * discussed in the specification */
  202. be128_xor(&rctx->t, &rctx->t,
  203. &ctx->mulinc[get_index128(iv)]);
  204. inc(iv);
  205. } while ((avail -= bs) >= bs);
  206. err = skcipher_walk_done(&w, avail);
  207. }
  208. skcipher_request_set_tfm(subreq, ctx->child);
  209. skcipher_request_set_crypt(subreq, rctx->dst, rctx->dst,
  210. cryptlen, NULL);
  211. if (err || !more)
  212. goto out;
  213. rctx->src = rctx->srcbuf;
  214. scatterwalk_done(&w.in, 0, 1);
  215. sg = w.in.sg;
  216. offset = w.in.offset;
  217. if (rctx->src != sg) {
  218. rctx->src[0] = *sg;
  219. sg_unmark_end(rctx->src);
  220. scatterwalk_crypto_chain(rctx->src, sg_next(sg), 2);
  221. }
  222. rctx->src[0].length -= offset - sg->offset;
  223. rctx->src[0].offset = offset;
  224. out:
  225. return err;
  226. }
  227. static int init_crypt(struct skcipher_request *req, crypto_completion_t done)
  228. {
  229. struct priv *ctx = crypto_skcipher_ctx(crypto_skcipher_reqtfm(req));
  230. struct rctx *rctx = skcipher_request_ctx(req);
  231. struct skcipher_request *subreq;
  232. gfp_t gfp;
  233. subreq = &rctx->subreq;
  234. skcipher_request_set_callback(subreq, req->base.flags, done, req);
  235. gfp = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
  236. GFP_ATOMIC;
  237. rctx->ext = NULL;
  238. subreq->cryptlen = LRW_BUFFER_SIZE;
  239. if (req->cryptlen > LRW_BUFFER_SIZE) {
  240. unsigned int n = min(req->cryptlen, (unsigned int)PAGE_SIZE);
  241. rctx->ext = kmalloc(n, gfp);
  242. if (rctx->ext)
  243. subreq->cryptlen = n;
  244. }
  245. rctx->src = req->src;
  246. rctx->dst = req->dst;
  247. rctx->left = req->cryptlen;
  248. /* calculate first value of T */
  249. memcpy(&rctx->t, req->iv, sizeof(rctx->t));
  250. /* T <- I*Key2 */
  251. gf128mul_64k_bbe(&rctx->t, ctx->table);
  252. return 0;
  253. }
  254. static void exit_crypt(struct skcipher_request *req)
  255. {
  256. struct rctx *rctx = skcipher_request_ctx(req);
  257. rctx->left = 0;
  258. if (rctx->ext)
  259. kzfree(rctx->ext);
  260. }
  261. static int do_encrypt(struct skcipher_request *req, int err)
  262. {
  263. struct rctx *rctx = skcipher_request_ctx(req);
  264. struct skcipher_request *subreq;
  265. subreq = &rctx->subreq;
  266. while (!err && rctx->left) {
  267. err = pre_crypt(req) ?:
  268. crypto_skcipher_encrypt(subreq) ?:
  269. post_crypt(req);
  270. if (err == -EINPROGRESS || err == -EBUSY)
  271. return err;
  272. }
  273. exit_crypt(req);
  274. return err;
  275. }
  276. static void encrypt_done(struct crypto_async_request *areq, int err)
  277. {
  278. struct skcipher_request *req = areq->data;
  279. struct skcipher_request *subreq;
  280. struct rctx *rctx;
  281. rctx = skcipher_request_ctx(req);
  282. if (err == -EINPROGRESS) {
  283. if (rctx->left != req->cryptlen)
  284. return;
  285. goto out;
  286. }
  287. subreq = &rctx->subreq;
  288. subreq->base.flags &= CRYPTO_TFM_REQ_MAY_BACKLOG;
  289. err = do_encrypt(req, err ?: post_crypt(req));
  290. if (rctx->left)
  291. return;
  292. out:
  293. skcipher_request_complete(req, err);
  294. }
  295. static int encrypt(struct skcipher_request *req)
  296. {
  297. return do_encrypt(req, init_crypt(req, encrypt_done));
  298. }
  299. static int do_decrypt(struct skcipher_request *req, int err)
  300. {
  301. struct rctx *rctx = skcipher_request_ctx(req);
  302. struct skcipher_request *subreq;
  303. subreq = &rctx->subreq;
  304. while (!err && rctx->left) {
  305. err = pre_crypt(req) ?:
  306. crypto_skcipher_decrypt(subreq) ?:
  307. post_crypt(req);
  308. if (err == -EINPROGRESS || err == -EBUSY)
  309. return err;
  310. }
  311. exit_crypt(req);
  312. return err;
  313. }
  314. static void decrypt_done(struct crypto_async_request *areq, int err)
  315. {
  316. struct skcipher_request *req = areq->data;
  317. struct skcipher_request *subreq;
  318. struct rctx *rctx;
  319. rctx = skcipher_request_ctx(req);
  320. if (err == -EINPROGRESS) {
  321. if (rctx->left != req->cryptlen)
  322. return;
  323. goto out;
  324. }
  325. subreq = &rctx->subreq;
  326. subreq->base.flags &= CRYPTO_TFM_REQ_MAY_BACKLOG;
  327. err = do_decrypt(req, err ?: post_crypt(req));
  328. if (rctx->left)
  329. return;
  330. out:
  331. skcipher_request_complete(req, err);
  332. }
  333. static int decrypt(struct skcipher_request *req)
  334. {
  335. return do_decrypt(req, init_crypt(req, decrypt_done));
  336. }
  337. static int init_tfm(struct crypto_skcipher *tfm)
  338. {
  339. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  340. struct crypto_skcipher_spawn *spawn = skcipher_instance_ctx(inst);
  341. struct priv *ctx = crypto_skcipher_ctx(tfm);
  342. struct crypto_skcipher *cipher;
  343. cipher = crypto_spawn_skcipher(spawn);
  344. if (IS_ERR(cipher))
  345. return PTR_ERR(cipher);
  346. ctx->child = cipher;
  347. crypto_skcipher_set_reqsize(tfm, crypto_skcipher_reqsize(cipher) +
  348. sizeof(struct rctx));
  349. return 0;
  350. }
  351. static void exit_tfm(struct crypto_skcipher *tfm)
  352. {
  353. struct priv *ctx = crypto_skcipher_ctx(tfm);
  354. if (ctx->table)
  355. gf128mul_free_64k(ctx->table);
  356. crypto_free_skcipher(ctx->child);
  357. }
  358. static void free(struct skcipher_instance *inst)
  359. {
  360. crypto_drop_skcipher(skcipher_instance_ctx(inst));
  361. kfree(inst);
  362. }
  363. static int create(struct crypto_template *tmpl, struct rtattr **tb)
  364. {
  365. struct crypto_skcipher_spawn *spawn;
  366. struct skcipher_instance *inst;
  367. struct crypto_attr_type *algt;
  368. struct skcipher_alg *alg;
  369. const char *cipher_name;
  370. char ecb_name[CRYPTO_MAX_ALG_NAME];
  371. int err;
  372. algt = crypto_get_attr_type(tb);
  373. if (IS_ERR(algt))
  374. return PTR_ERR(algt);
  375. if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
  376. return -EINVAL;
  377. cipher_name = crypto_attr_alg_name(tb[1]);
  378. if (IS_ERR(cipher_name))
  379. return PTR_ERR(cipher_name);
  380. inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
  381. if (!inst)
  382. return -ENOMEM;
  383. spawn = skcipher_instance_ctx(inst);
  384. crypto_set_skcipher_spawn(spawn, skcipher_crypto_instance(inst));
  385. err = crypto_grab_skcipher(spawn, cipher_name, 0,
  386. crypto_requires_sync(algt->type,
  387. algt->mask));
  388. if (err == -ENOENT) {
  389. err = -ENAMETOOLONG;
  390. if (snprintf(ecb_name, CRYPTO_MAX_ALG_NAME, "ecb(%s)",
  391. cipher_name) >= CRYPTO_MAX_ALG_NAME)
  392. goto err_free_inst;
  393. err = crypto_grab_skcipher(spawn, ecb_name, 0,
  394. crypto_requires_sync(algt->type,
  395. algt->mask));
  396. }
  397. if (err)
  398. goto err_free_inst;
  399. alg = crypto_skcipher_spawn_alg(spawn);
  400. err = -EINVAL;
  401. if (alg->base.cra_blocksize != LRW_BLOCK_SIZE)
  402. goto err_drop_spawn;
  403. if (crypto_skcipher_alg_ivsize(alg))
  404. goto err_drop_spawn;
  405. err = crypto_inst_setname(skcipher_crypto_instance(inst), "lrw",
  406. &alg->base);
  407. if (err)
  408. goto err_drop_spawn;
  409. err = -EINVAL;
  410. cipher_name = alg->base.cra_name;
  411. /* Alas we screwed up the naming so we have to mangle the
  412. * cipher name.
  413. */
  414. if (!strncmp(cipher_name, "ecb(", 4)) {
  415. unsigned len;
  416. len = strlcpy(ecb_name, cipher_name + 4, sizeof(ecb_name));
  417. if (len < 2 || len >= sizeof(ecb_name))
  418. goto err_drop_spawn;
  419. if (ecb_name[len - 1] != ')')
  420. goto err_drop_spawn;
  421. ecb_name[len - 1] = 0;
  422. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  423. "lrw(%s)", ecb_name) >= CRYPTO_MAX_ALG_NAME) {
  424. err = -ENAMETOOLONG;
  425. goto err_drop_spawn;
  426. }
  427. } else
  428. goto err_drop_spawn;
  429. inst->alg.base.cra_flags = alg->base.cra_flags & CRYPTO_ALG_ASYNC;
  430. inst->alg.base.cra_priority = alg->base.cra_priority;
  431. inst->alg.base.cra_blocksize = LRW_BLOCK_SIZE;
  432. inst->alg.base.cra_alignmask = alg->base.cra_alignmask |
  433. (__alignof__(u64) - 1);
  434. inst->alg.ivsize = LRW_BLOCK_SIZE;
  435. inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(alg) +
  436. LRW_BLOCK_SIZE;
  437. inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(alg) +
  438. LRW_BLOCK_SIZE;
  439. inst->alg.base.cra_ctxsize = sizeof(struct priv);
  440. inst->alg.init = init_tfm;
  441. inst->alg.exit = exit_tfm;
  442. inst->alg.setkey = setkey;
  443. inst->alg.encrypt = encrypt;
  444. inst->alg.decrypt = decrypt;
  445. inst->free = free;
  446. err = skcipher_register_instance(tmpl, inst);
  447. if (err)
  448. goto err_drop_spawn;
  449. out:
  450. return err;
  451. err_drop_spawn:
  452. crypto_drop_skcipher(spawn);
  453. err_free_inst:
  454. kfree(inst);
  455. goto out;
  456. }
  457. static struct crypto_template crypto_tmpl = {
  458. .name = "lrw",
  459. .create = create,
  460. .module = THIS_MODULE,
  461. };
  462. static int __init crypto_module_init(void)
  463. {
  464. return crypto_register_template(&crypto_tmpl);
  465. }
  466. static void __exit crypto_module_exit(void)
  467. {
  468. crypto_unregister_template(&crypto_tmpl);
  469. }
  470. module_init(crypto_module_init);
  471. module_exit(crypto_module_exit);
  472. MODULE_LICENSE("GPL");
  473. MODULE_DESCRIPTION("LRW block cipher mode");
  474. MODULE_ALIAS_CRYPTO("lrw");