dm-crypt.c 26 KB

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