dm-crypt.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /*
  2. * Copyright (C) 2003 Christophe Saout <christophe@saout.de>
  3. * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. *
  6. * This file is released under the GPL.
  7. */
  8. #include <linux/err.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/bio.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/mempool.h>
  15. #include <linux/slab.h>
  16. #include <linux/crypto.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/backing-dev.h>
  19. #include <asm/atomic.h>
  20. #include <linux/scatterlist.h>
  21. #include <asm/page.h>
  22. #include <asm/unaligned.h>
  23. #include "dm.h"
  24. #define DM_MSG_PREFIX "crypt"
  25. #define MESG_STR(x) x, sizeof(x)
  26. /*
  27. * per bio private data
  28. */
  29. struct dm_crypt_io {
  30. struct dm_target *target;
  31. struct bio *base_bio;
  32. struct work_struct work;
  33. atomic_t pending;
  34. int error;
  35. int post_process;
  36. };
  37. /*
  38. * context holding the current state of a multi-part conversion
  39. */
  40. struct convert_context {
  41. struct bio *bio_in;
  42. struct bio *bio_out;
  43. unsigned int offset_in;
  44. unsigned int offset_out;
  45. unsigned int idx_in;
  46. unsigned int idx_out;
  47. sector_t sector;
  48. int write;
  49. };
  50. struct crypt_config;
  51. struct crypt_iv_operations {
  52. int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
  53. const char *opts);
  54. void (*dtr)(struct crypt_config *cc);
  55. const char *(*status)(struct crypt_config *cc);
  56. int (*generator)(struct crypt_config *cc, u8 *iv, sector_t sector);
  57. };
  58. /*
  59. * Crypt: maps a linear range of a block device
  60. * and encrypts / decrypts at the same time.
  61. */
  62. enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
  63. struct crypt_config {
  64. struct dm_dev *dev;
  65. sector_t start;
  66. /*
  67. * pool for per bio private data and
  68. * for encryption buffer pages
  69. */
  70. mempool_t *io_pool;
  71. mempool_t *page_pool;
  72. struct bio_set *bs;
  73. /*
  74. * crypto related data
  75. */
  76. struct crypt_iv_operations *iv_gen_ops;
  77. char *iv_mode;
  78. union {
  79. struct crypto_cipher *essiv_tfm;
  80. int benbi_shift;
  81. } iv_gen_private;
  82. sector_t iv_offset;
  83. unsigned int iv_size;
  84. char cipher[CRYPTO_MAX_ALG_NAME];
  85. char chainmode[CRYPTO_MAX_ALG_NAME];
  86. struct crypto_blkcipher *tfm;
  87. unsigned long flags;
  88. unsigned int key_size;
  89. u8 key[0];
  90. };
  91. #define MIN_IOS 16
  92. #define MIN_POOL_PAGES 32
  93. #define MIN_BIO_PAGES 8
  94. static struct kmem_cache *_crypt_io_pool;
  95. static void clone_init(struct dm_crypt_io *, struct bio *);
  96. /*
  97. * Different IV generation algorithms:
  98. *
  99. * plain: the initial vector is the 32-bit little-endian version of the sector
  100. * number, padded with zeros if neccessary.
  101. *
  102. * essiv: "encrypted sector|salt initial vector", the sector number is
  103. * encrypted with the bulk cipher using a salt as key. The salt
  104. * should be derived from the bulk cipher's key via hashing.
  105. *
  106. * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
  107. * (needed for LRW-32-AES and possible other narrow block modes)
  108. *
  109. * null: the initial vector is always zero. Provides compatibility with
  110. * obsolete loop_fish2 devices. Do not use for new devices.
  111. *
  112. * plumb: unimplemented, see:
  113. * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
  114. */
  115. static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
  116. {
  117. memset(iv, 0, cc->iv_size);
  118. *(u32 *)iv = cpu_to_le32(sector & 0xffffffff);
  119. return 0;
  120. }
  121. static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
  122. const char *opts)
  123. {
  124. struct crypto_cipher *essiv_tfm;
  125. struct crypto_hash *hash_tfm;
  126. struct hash_desc desc;
  127. struct scatterlist sg;
  128. unsigned int saltsize;
  129. u8 *salt;
  130. int err;
  131. if (opts == NULL) {
  132. ti->error = "Digest algorithm missing for ESSIV mode";
  133. return -EINVAL;
  134. }
  135. /* Hash the cipher key with the given hash algorithm */
  136. hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
  137. if (IS_ERR(hash_tfm)) {
  138. ti->error = "Error initializing ESSIV hash";
  139. return PTR_ERR(hash_tfm);
  140. }
  141. saltsize = crypto_hash_digestsize(hash_tfm);
  142. salt = kmalloc(saltsize, GFP_KERNEL);
  143. if (salt == NULL) {
  144. ti->error = "Error kmallocing salt storage in ESSIV";
  145. crypto_free_hash(hash_tfm);
  146. return -ENOMEM;
  147. }
  148. sg_set_buf(&sg, cc->key, cc->key_size);
  149. desc.tfm = hash_tfm;
  150. desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
  151. err = crypto_hash_digest(&desc, &sg, cc->key_size, salt);
  152. crypto_free_hash(hash_tfm);
  153. if (err) {
  154. ti->error = "Error calculating hash in ESSIV";
  155. return err;
  156. }
  157. /* Setup the essiv_tfm with the given salt */
  158. essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
  159. if (IS_ERR(essiv_tfm)) {
  160. ti->error = "Error allocating crypto tfm for ESSIV";
  161. kfree(salt);
  162. return PTR_ERR(essiv_tfm);
  163. }
  164. if (crypto_cipher_blocksize(essiv_tfm) !=
  165. crypto_blkcipher_ivsize(cc->tfm)) {
  166. ti->error = "Block size of ESSIV cipher does "
  167. "not match IV size of block cipher";
  168. crypto_free_cipher(essiv_tfm);
  169. kfree(salt);
  170. return -EINVAL;
  171. }
  172. err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
  173. if (err) {
  174. ti->error = "Failed to set key for ESSIV cipher";
  175. crypto_free_cipher(essiv_tfm);
  176. kfree(salt);
  177. return err;
  178. }
  179. kfree(salt);
  180. cc->iv_gen_private.essiv_tfm = essiv_tfm;
  181. return 0;
  182. }
  183. static void crypt_iv_essiv_dtr(struct crypt_config *cc)
  184. {
  185. crypto_free_cipher(cc->iv_gen_private.essiv_tfm);
  186. cc->iv_gen_private.essiv_tfm = NULL;
  187. }
  188. static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
  189. {
  190. memset(iv, 0, cc->iv_size);
  191. *(u64 *)iv = cpu_to_le64(sector);
  192. crypto_cipher_encrypt_one(cc->iv_gen_private.essiv_tfm, iv, iv);
  193. return 0;
  194. }
  195. static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
  196. const char *opts)
  197. {
  198. unsigned int bs = crypto_blkcipher_blocksize(cc->tfm);
  199. int log = ilog2(bs);
  200. /* we need to calculate how far we must shift the sector count
  201. * to get the cipher block count, we use this shift in _gen */
  202. if (1 << log != bs) {
  203. ti->error = "cypher blocksize is not a power of 2";
  204. return -EINVAL;
  205. }
  206. if (log > 9) {
  207. ti->error = "cypher blocksize is > 512";
  208. return -EINVAL;
  209. }
  210. cc->iv_gen_private.benbi_shift = 9 - log;
  211. return 0;
  212. }
  213. static void crypt_iv_benbi_dtr(struct crypt_config *cc)
  214. {
  215. }
  216. static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
  217. {
  218. __be64 val;
  219. memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
  220. val = cpu_to_be64(((u64)sector << cc->iv_gen_private.benbi_shift) + 1);
  221. put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
  222. return 0;
  223. }
  224. static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
  225. {
  226. memset(iv, 0, cc->iv_size);
  227. return 0;
  228. }
  229. static struct crypt_iv_operations crypt_iv_plain_ops = {
  230. .generator = crypt_iv_plain_gen
  231. };
  232. static struct crypt_iv_operations crypt_iv_essiv_ops = {
  233. .ctr = crypt_iv_essiv_ctr,
  234. .dtr = crypt_iv_essiv_dtr,
  235. .generator = crypt_iv_essiv_gen
  236. };
  237. static struct crypt_iv_operations crypt_iv_benbi_ops = {
  238. .ctr = crypt_iv_benbi_ctr,
  239. .dtr = crypt_iv_benbi_dtr,
  240. .generator = crypt_iv_benbi_gen
  241. };
  242. static struct crypt_iv_operations crypt_iv_null_ops = {
  243. .generator = crypt_iv_null_gen
  244. };
  245. static int
  246. crypt_convert_scatterlist(struct crypt_config *cc, struct scatterlist *out,
  247. struct scatterlist *in, unsigned int length,
  248. int write, sector_t sector)
  249. {
  250. u8 iv[cc->iv_size] __attribute__ ((aligned(__alignof__(u64))));
  251. struct blkcipher_desc desc = {
  252. .tfm = cc->tfm,
  253. .info = iv,
  254. .flags = CRYPTO_TFM_REQ_MAY_SLEEP,
  255. };
  256. int r;
  257. if (cc->iv_gen_ops) {
  258. r = cc->iv_gen_ops->generator(cc, iv, sector);
  259. if (r < 0)
  260. return r;
  261. if (write)
  262. r = crypto_blkcipher_encrypt_iv(&desc, out, in, length);
  263. else
  264. r = crypto_blkcipher_decrypt_iv(&desc, out, in, length);
  265. } else {
  266. if (write)
  267. r = crypto_blkcipher_encrypt(&desc, out, in, length);
  268. else
  269. r = crypto_blkcipher_decrypt(&desc, out, in, length);
  270. }
  271. return r;
  272. }
  273. static void
  274. crypt_convert_init(struct crypt_config *cc, struct convert_context *ctx,
  275. struct bio *bio_out, struct bio *bio_in,
  276. sector_t sector, int write)
  277. {
  278. ctx->bio_in = bio_in;
  279. ctx->bio_out = bio_out;
  280. ctx->offset_in = 0;
  281. ctx->offset_out = 0;
  282. ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
  283. ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
  284. ctx->sector = sector + cc->iv_offset;
  285. ctx->write = write;
  286. }
  287. /*
  288. * Encrypt / decrypt data from one bio to another one (can be the same one)
  289. */
  290. static int crypt_convert(struct crypt_config *cc,
  291. struct convert_context *ctx)
  292. {
  293. int r = 0;
  294. while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
  295. ctx->idx_out < ctx->bio_out->bi_vcnt) {
  296. struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
  297. struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
  298. struct scatterlist sg_in = {
  299. .page = bv_in->bv_page,
  300. .offset = bv_in->bv_offset + ctx->offset_in,
  301. .length = 1 << SECTOR_SHIFT
  302. };
  303. struct scatterlist sg_out = {
  304. .page = bv_out->bv_page,
  305. .offset = bv_out->bv_offset + ctx->offset_out,
  306. .length = 1 << SECTOR_SHIFT
  307. };
  308. ctx->offset_in += sg_in.length;
  309. if (ctx->offset_in >= bv_in->bv_len) {
  310. ctx->offset_in = 0;
  311. ctx->idx_in++;
  312. }
  313. ctx->offset_out += sg_out.length;
  314. if (ctx->offset_out >= bv_out->bv_len) {
  315. ctx->offset_out = 0;
  316. ctx->idx_out++;
  317. }
  318. r = crypt_convert_scatterlist(cc, &sg_out, &sg_in, sg_in.length,
  319. ctx->write, ctx->sector);
  320. if (r < 0)
  321. break;
  322. ctx->sector++;
  323. }
  324. return r;
  325. }
  326. static void dm_crypt_bio_destructor(struct bio *bio)
  327. {
  328. struct dm_crypt_io *io = bio->bi_private;
  329. struct crypt_config *cc = io->target->private;
  330. bio_free(bio, cc->bs);
  331. }
  332. /*
  333. * Generate a new unfragmented bio with the given size
  334. * This should never violate the device limitations
  335. * May return a smaller bio when running out of pages
  336. */
  337. static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
  338. {
  339. struct crypt_config *cc = io->target->private;
  340. struct bio *clone;
  341. unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  342. gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
  343. unsigned int i;
  344. clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
  345. if (!clone)
  346. return NULL;
  347. clone_init(io, clone);
  348. for (i = 0; i < nr_iovecs; i++) {
  349. struct bio_vec *bv = bio_iovec_idx(clone, i);
  350. bv->bv_page = mempool_alloc(cc->page_pool, gfp_mask);
  351. if (!bv->bv_page)
  352. break;
  353. /*
  354. * if additional pages cannot be allocated without waiting,
  355. * return a partially allocated bio, the caller will then try
  356. * to allocate additional bios while submitting this partial bio
  357. */
  358. if (i == (MIN_BIO_PAGES - 1))
  359. gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
  360. bv->bv_offset = 0;
  361. if (size > PAGE_SIZE)
  362. bv->bv_len = PAGE_SIZE;
  363. else
  364. bv->bv_len = size;
  365. clone->bi_size += bv->bv_len;
  366. clone->bi_vcnt++;
  367. size -= bv->bv_len;
  368. }
  369. if (!clone->bi_size) {
  370. bio_put(clone);
  371. return NULL;
  372. }
  373. return clone;
  374. }
  375. static void crypt_free_buffer_pages(struct crypt_config *cc,
  376. struct bio *clone, unsigned int bytes)
  377. {
  378. unsigned int i, start, end;
  379. struct bio_vec *bv;
  380. /*
  381. * This is ugly, but Jens Axboe thinks that using bi_idx in the
  382. * endio function is too dangerous at the moment, so I calculate the
  383. * correct position using bi_vcnt and bi_size.
  384. * The bv_offset and bv_len fields might already be modified but we
  385. * know that we always allocated whole pages.
  386. * A fix to the bi_idx issue in the kernel is in the works, so
  387. * we will hopefully be able to revert to the cleaner solution soon.
  388. */
  389. i = clone->bi_vcnt - 1;
  390. bv = bio_iovec_idx(clone, i);
  391. end = (i << PAGE_SHIFT) + (bv->bv_offset + bv->bv_len) - clone->bi_size;
  392. start = end - bytes;
  393. start >>= PAGE_SHIFT;
  394. if (!clone->bi_size)
  395. end = clone->bi_vcnt;
  396. else
  397. end >>= PAGE_SHIFT;
  398. for (i = start; i < end; i++) {
  399. bv = bio_iovec_idx(clone, i);
  400. BUG_ON(!bv->bv_page);
  401. mempool_free(bv->bv_page, cc->page_pool);
  402. bv->bv_page = NULL;
  403. }
  404. }
  405. /*
  406. * One of the bios was finished. Check for completion of
  407. * the whole request and correctly clean up the buffer.
  408. */
  409. static void dec_pending(struct dm_crypt_io *io, int error)
  410. {
  411. struct crypt_config *cc = (struct crypt_config *) io->target->private;
  412. if (error < 0)
  413. io->error = error;
  414. if (!atomic_dec_and_test(&io->pending))
  415. return;
  416. bio_endio(io->base_bio, io->error);
  417. mempool_free(io, cc->io_pool);
  418. }
  419. /*
  420. * kcryptd:
  421. *
  422. * Needed because it would be very unwise to do decryption in an
  423. * interrupt context.
  424. */
  425. static struct workqueue_struct *_kcryptd_workqueue;
  426. static void kcryptd_do_work(struct work_struct *work);
  427. static void kcryptd_queue_io(struct dm_crypt_io *io)
  428. {
  429. INIT_WORK(&io->work, kcryptd_do_work);
  430. queue_work(_kcryptd_workqueue, &io->work);
  431. }
  432. static void crypt_endio(struct bio *clone, int error)
  433. {
  434. struct dm_crypt_io *io = clone->bi_private;
  435. struct crypt_config *cc = io->target->private;
  436. unsigned read_io = bio_data_dir(clone) == READ;
  437. /*
  438. * free the processed pages
  439. */
  440. if (!read_io) {
  441. crypt_free_buffer_pages(cc, clone, clone->bi_size);
  442. goto out;
  443. }
  444. if (unlikely(!bio_flagged(clone, BIO_UPTODATE))) {
  445. error = -EIO;
  446. goto out;
  447. }
  448. bio_put(clone);
  449. io->post_process = 1;
  450. kcryptd_queue_io(io);
  451. return;
  452. out:
  453. bio_put(clone);
  454. dec_pending(io, error);
  455. }
  456. static void clone_init(struct dm_crypt_io *io, struct bio *clone)
  457. {
  458. struct crypt_config *cc = io->target->private;
  459. clone->bi_private = io;
  460. clone->bi_end_io = crypt_endio;
  461. clone->bi_bdev = cc->dev->bdev;
  462. clone->bi_rw = io->base_bio->bi_rw;
  463. clone->bi_destructor = dm_crypt_bio_destructor;
  464. }
  465. static void process_read(struct dm_crypt_io *io)
  466. {
  467. struct crypt_config *cc = io->target->private;
  468. struct bio *base_bio = io->base_bio;
  469. struct bio *clone;
  470. sector_t sector = base_bio->bi_sector - io->target->begin;
  471. atomic_inc(&io->pending);
  472. /*
  473. * The block layer might modify the bvec array, so always
  474. * copy the required bvecs because we need the original
  475. * one in order to decrypt the whole bio data *afterwards*.
  476. */
  477. clone = bio_alloc_bioset(GFP_NOIO, bio_segments(base_bio), cc->bs);
  478. if (unlikely(!clone)) {
  479. dec_pending(io, -ENOMEM);
  480. return;
  481. }
  482. clone_init(io, clone);
  483. clone->bi_idx = 0;
  484. clone->bi_vcnt = bio_segments(base_bio);
  485. clone->bi_size = base_bio->bi_size;
  486. clone->bi_sector = cc->start + sector;
  487. memcpy(clone->bi_io_vec, bio_iovec(base_bio),
  488. sizeof(struct bio_vec) * clone->bi_vcnt);
  489. generic_make_request(clone);
  490. }
  491. static void process_write(struct dm_crypt_io *io)
  492. {
  493. struct crypt_config *cc = io->target->private;
  494. struct bio *base_bio = io->base_bio;
  495. struct bio *clone;
  496. struct convert_context ctx;
  497. unsigned remaining = base_bio->bi_size;
  498. sector_t sector = base_bio->bi_sector - io->target->begin;
  499. atomic_inc(&io->pending);
  500. crypt_convert_init(cc, &ctx, NULL, base_bio, sector, 1);
  501. /*
  502. * The allocated buffers can be smaller than the whole bio,
  503. * so repeat the whole process until all the data can be handled.
  504. */
  505. while (remaining) {
  506. clone = crypt_alloc_buffer(io, remaining);
  507. if (unlikely(!clone)) {
  508. dec_pending(io, -ENOMEM);
  509. return;
  510. }
  511. ctx.bio_out = clone;
  512. ctx.idx_out = 0;
  513. if (unlikely(crypt_convert(cc, &ctx) < 0)) {
  514. crypt_free_buffer_pages(cc, clone, clone->bi_size);
  515. bio_put(clone);
  516. dec_pending(io, -EIO);
  517. return;
  518. }
  519. /* crypt_convert should have filled the clone bio */
  520. BUG_ON(ctx.idx_out < clone->bi_vcnt);
  521. clone->bi_sector = cc->start + sector;
  522. remaining -= clone->bi_size;
  523. sector += bio_sectors(clone);
  524. /* Grab another reference to the io struct
  525. * before we kick off the request */
  526. if (remaining)
  527. atomic_inc(&io->pending);
  528. generic_make_request(clone);
  529. /* Do not reference clone after this - it
  530. * may be gone already. */
  531. /* out of memory -> run queues */
  532. if (remaining)
  533. congestion_wait(WRITE, HZ/100);
  534. }
  535. }
  536. static void process_read_endio(struct dm_crypt_io *io)
  537. {
  538. struct crypt_config *cc = io->target->private;
  539. struct convert_context ctx;
  540. crypt_convert_init(cc, &ctx, io->base_bio, io->base_bio,
  541. io->base_bio->bi_sector - io->target->begin, 0);
  542. dec_pending(io, crypt_convert(cc, &ctx));
  543. }
  544. static void kcryptd_do_work(struct work_struct *work)
  545. {
  546. struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
  547. if (io->post_process)
  548. process_read_endio(io);
  549. else if (bio_data_dir(io->base_bio) == READ)
  550. process_read(io);
  551. else
  552. process_write(io);
  553. }
  554. /*
  555. * Decode key from its hex representation
  556. */
  557. static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
  558. {
  559. char buffer[3];
  560. char *endp;
  561. unsigned int i;
  562. buffer[2] = '\0';
  563. for (i = 0; i < size; i++) {
  564. buffer[0] = *hex++;
  565. buffer[1] = *hex++;
  566. key[i] = (u8)simple_strtoul(buffer, &endp, 16);
  567. if (endp != &buffer[2])
  568. return -EINVAL;
  569. }
  570. if (*hex != '\0')
  571. return -EINVAL;
  572. return 0;
  573. }
  574. /*
  575. * Encode key into its hex representation
  576. */
  577. static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
  578. {
  579. unsigned int i;
  580. for (i = 0; i < size; i++) {
  581. sprintf(hex, "%02x", *key);
  582. hex += 2;
  583. key++;
  584. }
  585. }
  586. static int crypt_set_key(struct crypt_config *cc, char *key)
  587. {
  588. unsigned key_size = strlen(key) >> 1;
  589. if (cc->key_size && cc->key_size != key_size)
  590. return -EINVAL;
  591. cc->key_size = key_size; /* initial settings */
  592. if ((!key_size && strcmp(key, "-")) ||
  593. (key_size && crypt_decode_key(cc->key, key, key_size) < 0))
  594. return -EINVAL;
  595. set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  596. return 0;
  597. }
  598. static int crypt_wipe_key(struct crypt_config *cc)
  599. {
  600. clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  601. memset(&cc->key, 0, cc->key_size * sizeof(u8));
  602. return 0;
  603. }
  604. /*
  605. * Construct an encryption mapping:
  606. * <cipher> <key> <iv_offset> <dev_path> <start>
  607. */
  608. static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  609. {
  610. struct crypt_config *cc;
  611. struct crypto_blkcipher *tfm;
  612. char *tmp;
  613. char *cipher;
  614. char *chainmode;
  615. char *ivmode;
  616. char *ivopts;
  617. unsigned int key_size;
  618. unsigned long long tmpll;
  619. if (argc != 5) {
  620. ti->error = "Not enough arguments";
  621. return -EINVAL;
  622. }
  623. tmp = argv[0];
  624. cipher = strsep(&tmp, "-");
  625. chainmode = strsep(&tmp, "-");
  626. ivopts = strsep(&tmp, "-");
  627. ivmode = strsep(&ivopts, ":");
  628. if (tmp)
  629. DMWARN("Unexpected additional cipher options");
  630. key_size = strlen(argv[1]) >> 1;
  631. cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
  632. if (cc == NULL) {
  633. ti->error =
  634. "Cannot allocate transparent encryption context";
  635. return -ENOMEM;
  636. }
  637. if (crypt_set_key(cc, argv[1])) {
  638. ti->error = "Error decoding key";
  639. goto bad1;
  640. }
  641. /* Compatiblity mode for old dm-crypt cipher strings */
  642. if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
  643. chainmode = "cbc";
  644. ivmode = "plain";
  645. }
  646. if (strcmp(chainmode, "ecb") && !ivmode) {
  647. ti->error = "This chaining mode requires an IV mechanism";
  648. goto bad1;
  649. }
  650. if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)", chainmode,
  651. cipher) >= CRYPTO_MAX_ALG_NAME) {
  652. ti->error = "Chain mode + cipher name is too long";
  653. goto bad1;
  654. }
  655. tfm = crypto_alloc_blkcipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
  656. if (IS_ERR(tfm)) {
  657. ti->error = "Error allocating crypto tfm";
  658. goto bad1;
  659. }
  660. strcpy(cc->cipher, cipher);
  661. strcpy(cc->chainmode, chainmode);
  662. cc->tfm = tfm;
  663. /*
  664. * Choose ivmode. Valid modes: "plain", "essiv:<esshash>", "benbi".
  665. * See comments at iv code
  666. */
  667. if (ivmode == NULL)
  668. cc->iv_gen_ops = NULL;
  669. else if (strcmp(ivmode, "plain") == 0)
  670. cc->iv_gen_ops = &crypt_iv_plain_ops;
  671. else if (strcmp(ivmode, "essiv") == 0)
  672. cc->iv_gen_ops = &crypt_iv_essiv_ops;
  673. else if (strcmp(ivmode, "benbi") == 0)
  674. cc->iv_gen_ops = &crypt_iv_benbi_ops;
  675. else if (strcmp(ivmode, "null") == 0)
  676. cc->iv_gen_ops = &crypt_iv_null_ops;
  677. else {
  678. ti->error = "Invalid IV mode";
  679. goto bad2;
  680. }
  681. if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
  682. cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
  683. goto bad2;
  684. cc->iv_size = crypto_blkcipher_ivsize(tfm);
  685. if (cc->iv_size)
  686. /* at least a 64 bit sector number should fit in our buffer */
  687. cc->iv_size = max(cc->iv_size,
  688. (unsigned int)(sizeof(u64) / sizeof(u8)));
  689. else {
  690. if (cc->iv_gen_ops) {
  691. DMWARN("Selected cipher does not support IVs");
  692. if (cc->iv_gen_ops->dtr)
  693. cc->iv_gen_ops->dtr(cc);
  694. cc->iv_gen_ops = NULL;
  695. }
  696. }
  697. cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
  698. if (!cc->io_pool) {
  699. ti->error = "Cannot allocate crypt io mempool";
  700. goto bad3;
  701. }
  702. cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
  703. if (!cc->page_pool) {
  704. ti->error = "Cannot allocate page mempool";
  705. goto bad4;
  706. }
  707. cc->bs = bioset_create(MIN_IOS, MIN_IOS);
  708. if (!cc->bs) {
  709. ti->error = "Cannot allocate crypt bioset";
  710. goto bad_bs;
  711. }
  712. if (crypto_blkcipher_setkey(tfm, cc->key, key_size) < 0) {
  713. ti->error = "Error setting key";
  714. goto bad5;
  715. }
  716. if (sscanf(argv[2], "%llu", &tmpll) != 1) {
  717. ti->error = "Invalid iv_offset sector";
  718. goto bad5;
  719. }
  720. cc->iv_offset = tmpll;
  721. if (sscanf(argv[4], "%llu", &tmpll) != 1) {
  722. ti->error = "Invalid device sector";
  723. goto bad5;
  724. }
  725. cc->start = tmpll;
  726. if (dm_get_device(ti, argv[3], cc->start, ti->len,
  727. dm_table_get_mode(ti->table), &cc->dev)) {
  728. ti->error = "Device lookup failed";
  729. goto bad5;
  730. }
  731. if (ivmode && cc->iv_gen_ops) {
  732. if (ivopts)
  733. *(ivopts - 1) = ':';
  734. cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
  735. if (!cc->iv_mode) {
  736. ti->error = "Error kmallocing iv_mode string";
  737. goto bad5;
  738. }
  739. strcpy(cc->iv_mode, ivmode);
  740. } else
  741. cc->iv_mode = NULL;
  742. ti->private = cc;
  743. return 0;
  744. bad5:
  745. bioset_free(cc->bs);
  746. bad_bs:
  747. mempool_destroy(cc->page_pool);
  748. bad4:
  749. mempool_destroy(cc->io_pool);
  750. bad3:
  751. if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
  752. cc->iv_gen_ops->dtr(cc);
  753. bad2:
  754. crypto_free_blkcipher(tfm);
  755. bad1:
  756. /* Must zero key material before freeing */
  757. memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
  758. kfree(cc);
  759. return -EINVAL;
  760. }
  761. static void crypt_dtr(struct dm_target *ti)
  762. {
  763. struct crypt_config *cc = (struct crypt_config *) ti->private;
  764. flush_workqueue(_kcryptd_workqueue);
  765. bioset_free(cc->bs);
  766. mempool_destroy(cc->page_pool);
  767. mempool_destroy(cc->io_pool);
  768. kfree(cc->iv_mode);
  769. if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
  770. cc->iv_gen_ops->dtr(cc);
  771. crypto_free_blkcipher(cc->tfm);
  772. dm_put_device(ti, cc->dev);
  773. /* Must zero key material before freeing */
  774. memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
  775. kfree(cc);
  776. }
  777. static int crypt_map(struct dm_target *ti, struct bio *bio,
  778. union map_info *map_context)
  779. {
  780. struct crypt_config *cc = ti->private;
  781. struct dm_crypt_io *io;
  782. io = mempool_alloc(cc->io_pool, GFP_NOIO);
  783. io->target = ti;
  784. io->base_bio = bio;
  785. io->error = io->post_process = 0;
  786. atomic_set(&io->pending, 0);
  787. kcryptd_queue_io(io);
  788. return DM_MAPIO_SUBMITTED;
  789. }
  790. static int crypt_status(struct dm_target *ti, status_type_t type,
  791. char *result, unsigned int maxlen)
  792. {
  793. struct crypt_config *cc = (struct crypt_config *) ti->private;
  794. unsigned int sz = 0;
  795. switch (type) {
  796. case STATUSTYPE_INFO:
  797. result[0] = '\0';
  798. break;
  799. case STATUSTYPE_TABLE:
  800. if (cc->iv_mode)
  801. DMEMIT("%s-%s-%s ", cc->cipher, cc->chainmode,
  802. cc->iv_mode);
  803. else
  804. DMEMIT("%s-%s ", cc->cipher, cc->chainmode);
  805. if (cc->key_size > 0) {
  806. if ((maxlen - sz) < ((cc->key_size << 1) + 1))
  807. return -ENOMEM;
  808. crypt_encode_key(result + sz, cc->key, cc->key_size);
  809. sz += cc->key_size << 1;
  810. } else {
  811. if (sz >= maxlen)
  812. return -ENOMEM;
  813. result[sz++] = '-';
  814. }
  815. DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
  816. cc->dev->name, (unsigned long long)cc->start);
  817. break;
  818. }
  819. return 0;
  820. }
  821. static void crypt_postsuspend(struct dm_target *ti)
  822. {
  823. struct crypt_config *cc = ti->private;
  824. set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  825. }
  826. static int crypt_preresume(struct dm_target *ti)
  827. {
  828. struct crypt_config *cc = ti->private;
  829. if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
  830. DMERR("aborting resume - crypt key is not set.");
  831. return -EAGAIN;
  832. }
  833. return 0;
  834. }
  835. static void crypt_resume(struct dm_target *ti)
  836. {
  837. struct crypt_config *cc = ti->private;
  838. clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  839. }
  840. /* Message interface
  841. * key set <key>
  842. * key wipe
  843. */
  844. static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
  845. {
  846. struct crypt_config *cc = ti->private;
  847. if (argc < 2)
  848. goto error;
  849. if (!strnicmp(argv[0], MESG_STR("key"))) {
  850. if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
  851. DMWARN("not suspended during key manipulation.");
  852. return -EINVAL;
  853. }
  854. if (argc == 3 && !strnicmp(argv[1], MESG_STR("set")))
  855. return crypt_set_key(cc, argv[2]);
  856. if (argc == 2 && !strnicmp(argv[1], MESG_STR("wipe")))
  857. return crypt_wipe_key(cc);
  858. }
  859. error:
  860. DMWARN("unrecognised message received.");
  861. return -EINVAL;
  862. }
  863. static struct target_type crypt_target = {
  864. .name = "crypt",
  865. .version= {1, 5, 0},
  866. .module = THIS_MODULE,
  867. .ctr = crypt_ctr,
  868. .dtr = crypt_dtr,
  869. .map = crypt_map,
  870. .status = crypt_status,
  871. .postsuspend = crypt_postsuspend,
  872. .preresume = crypt_preresume,
  873. .resume = crypt_resume,
  874. .message = crypt_message,
  875. };
  876. static int __init dm_crypt_init(void)
  877. {
  878. int r;
  879. _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
  880. if (!_crypt_io_pool)
  881. return -ENOMEM;
  882. _kcryptd_workqueue = create_workqueue("kcryptd");
  883. if (!_kcryptd_workqueue) {
  884. r = -ENOMEM;
  885. DMERR("couldn't create kcryptd");
  886. goto bad1;
  887. }
  888. r = dm_register_target(&crypt_target);
  889. if (r < 0) {
  890. DMERR("register failed %d", r);
  891. goto bad2;
  892. }
  893. return 0;
  894. bad2:
  895. destroy_workqueue(_kcryptd_workqueue);
  896. bad1:
  897. kmem_cache_destroy(_crypt_io_pool);
  898. return r;
  899. }
  900. static void __exit dm_crypt_exit(void)
  901. {
  902. int r = dm_unregister_target(&crypt_target);
  903. if (r < 0)
  904. DMERR("unregister failed %d", r);
  905. destroy_workqueue(_kcryptd_workqueue);
  906. kmem_cache_destroy(_crypt_io_pool);
  907. }
  908. module_init(dm_crypt_init);
  909. module_exit(dm_crypt_exit);
  910. MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
  911. MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
  912. MODULE_LICENSE("GPL");