dm-crypt.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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. };
  36. /*
  37. * context holding the current state of a multi-part conversion
  38. */
  39. struct convert_context {
  40. struct bio *bio_in;
  41. struct bio *bio_out;
  42. unsigned int offset_in;
  43. unsigned int offset_out;
  44. unsigned int idx_in;
  45. unsigned int idx_out;
  46. sector_t sector;
  47. int write;
  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_set_buf(&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, int write)
  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. ctx->write = write;
  288. }
  289. /*
  290. * Encrypt / decrypt data from one bio to another one (can be the same one)
  291. */
  292. static int crypt_convert(struct crypt_config *cc,
  293. struct convert_context *ctx)
  294. {
  295. int r = 0;
  296. while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
  297. ctx->idx_out < ctx->bio_out->bi_vcnt) {
  298. struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
  299. struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
  300. struct scatterlist sg_in = {
  301. .page = bv_in->bv_page,
  302. .offset = bv_in->bv_offset + ctx->offset_in,
  303. .length = 1 << SECTOR_SHIFT
  304. };
  305. struct scatterlist sg_out = {
  306. .page = bv_out->bv_page,
  307. .offset = bv_out->bv_offset + ctx->offset_out,
  308. .length = 1 << SECTOR_SHIFT
  309. };
  310. ctx->offset_in += sg_in.length;
  311. if (ctx->offset_in >= bv_in->bv_len) {
  312. ctx->offset_in = 0;
  313. ctx->idx_in++;
  314. }
  315. ctx->offset_out += sg_out.length;
  316. if (ctx->offset_out >= bv_out->bv_len) {
  317. ctx->offset_out = 0;
  318. ctx->idx_out++;
  319. }
  320. r = crypt_convert_scatterlist(cc, &sg_out, &sg_in, sg_in.length,
  321. ctx->write, ctx->sector);
  322. if (r < 0)
  323. break;
  324. ctx->sector++;
  325. }
  326. return r;
  327. }
  328. static void dm_crypt_bio_destructor(struct bio *bio)
  329. {
  330. struct dm_crypt_io *io = bio->bi_private;
  331. struct crypt_config *cc = io->target->private;
  332. bio_free(bio, cc->bs);
  333. }
  334. /*
  335. * Generate a new unfragmented bio with the given size
  336. * This should never violate the device limitations
  337. * May return a smaller bio when running out of pages
  338. */
  339. static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
  340. {
  341. struct crypt_config *cc = io->target->private;
  342. struct bio *clone;
  343. unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
  344. gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
  345. unsigned int i;
  346. clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
  347. if (!clone)
  348. return NULL;
  349. clone_init(io, clone);
  350. for (i = 0; i < nr_iovecs; i++) {
  351. struct bio_vec *bv = bio_iovec_idx(clone, i);
  352. bv->bv_page = mempool_alloc(cc->page_pool, gfp_mask);
  353. if (!bv->bv_page)
  354. break;
  355. /*
  356. * if additional pages cannot be allocated without waiting,
  357. * return a partially allocated bio, the caller will then try
  358. * to allocate additional bios while submitting this partial bio
  359. */
  360. if (i == (MIN_BIO_PAGES - 1))
  361. gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
  362. bv->bv_offset = 0;
  363. if (size > PAGE_SIZE)
  364. bv->bv_len = PAGE_SIZE;
  365. else
  366. bv->bv_len = size;
  367. clone->bi_size += bv->bv_len;
  368. clone->bi_vcnt++;
  369. size -= bv->bv_len;
  370. }
  371. if (!clone->bi_size) {
  372. bio_put(clone);
  373. return NULL;
  374. }
  375. return clone;
  376. }
  377. static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
  378. {
  379. unsigned int i;
  380. struct bio_vec *bv;
  381. for (i = 0; i < clone->bi_vcnt; i++) {
  382. bv = bio_iovec_idx(clone, i);
  383. BUG_ON(!bv->bv_page);
  384. mempool_free(bv->bv_page, cc->page_pool);
  385. bv->bv_page = NULL;
  386. }
  387. }
  388. /*
  389. * One of the bios was finished. Check for completion of
  390. * the whole request and correctly clean up the buffer.
  391. */
  392. static void crypt_dec_pending(struct dm_crypt_io *io, int error)
  393. {
  394. struct crypt_config *cc = (struct crypt_config *) io->target->private;
  395. if (error < 0)
  396. io->error = error;
  397. if (!atomic_dec_and_test(&io->pending))
  398. return;
  399. bio_endio(io->base_bio, io->error);
  400. mempool_free(io, cc->io_pool);
  401. }
  402. /*
  403. * kcryptd/kcryptd_io:
  404. *
  405. * Needed because it would be very unwise to do decryption in an
  406. * interrupt context.
  407. *
  408. * kcryptd performs the actual encryption or decryption.
  409. *
  410. * kcryptd_io performs the IO submission.
  411. *
  412. * They must be separated as otherwise the final stages could be
  413. * starved by new requests which can block in the first stages due
  414. * to memory allocation.
  415. */
  416. static void kcryptd_do_work(struct work_struct *work);
  417. static void kcryptd_do_crypt(struct work_struct *work);
  418. static void kcryptd_queue_io(struct dm_crypt_io *io)
  419. {
  420. struct crypt_config *cc = io->target->private;
  421. INIT_WORK(&io->work, kcryptd_do_work);
  422. queue_work(cc->io_queue, &io->work);
  423. }
  424. static void kcryptd_queue_crypt(struct dm_crypt_io *io)
  425. {
  426. struct crypt_config *cc = io->target->private;
  427. INIT_WORK(&io->work, kcryptd_do_crypt);
  428. queue_work(cc->crypt_queue, &io->work);
  429. }
  430. static void crypt_endio(struct bio *clone, int error)
  431. {
  432. struct dm_crypt_io *io = clone->bi_private;
  433. struct crypt_config *cc = io->target->private;
  434. unsigned read_io = bio_data_dir(clone) == READ;
  435. /*
  436. * free the processed pages
  437. */
  438. if (!read_io) {
  439. crypt_free_buffer_pages(cc, clone);
  440. goto out;
  441. }
  442. if (unlikely(!bio_flagged(clone, BIO_UPTODATE))) {
  443. error = -EIO;
  444. goto out;
  445. }
  446. bio_put(clone);
  447. kcryptd_queue_crypt(io);
  448. return;
  449. out:
  450. bio_put(clone);
  451. crypt_dec_pending(io, error);
  452. }
  453. static void clone_init(struct dm_crypt_io *io, struct bio *clone)
  454. {
  455. struct crypt_config *cc = io->target->private;
  456. clone->bi_private = io;
  457. clone->bi_end_io = crypt_endio;
  458. clone->bi_bdev = cc->dev->bdev;
  459. clone->bi_rw = io->base_bio->bi_rw;
  460. clone->bi_destructor = dm_crypt_bio_destructor;
  461. }
  462. static void process_read(struct dm_crypt_io *io)
  463. {
  464. struct crypt_config *cc = io->target->private;
  465. struct bio *base_bio = io->base_bio;
  466. struct bio *clone;
  467. sector_t sector = base_bio->bi_sector - io->target->begin;
  468. atomic_inc(&io->pending);
  469. /*
  470. * The block layer might modify the bvec array, so always
  471. * copy the required bvecs because we need the original
  472. * one in order to decrypt the whole bio data *afterwards*.
  473. */
  474. clone = bio_alloc_bioset(GFP_NOIO, bio_segments(base_bio), cc->bs);
  475. if (unlikely(!clone)) {
  476. crypt_dec_pending(io, -ENOMEM);
  477. return;
  478. }
  479. clone_init(io, clone);
  480. clone->bi_idx = 0;
  481. clone->bi_vcnt = bio_segments(base_bio);
  482. clone->bi_size = base_bio->bi_size;
  483. clone->bi_sector = cc->start + sector;
  484. memcpy(clone->bi_io_vec, bio_iovec(base_bio),
  485. sizeof(struct bio_vec) * clone->bi_vcnt);
  486. generic_make_request(clone);
  487. }
  488. static void process_write(struct dm_crypt_io *io)
  489. {
  490. struct crypt_config *cc = io->target->private;
  491. struct bio *base_bio = io->base_bio;
  492. struct bio *clone;
  493. struct convert_context ctx;
  494. unsigned remaining = base_bio->bi_size;
  495. sector_t sector = base_bio->bi_sector - io->target->begin;
  496. atomic_inc(&io->pending);
  497. crypt_convert_init(cc, &ctx, NULL, base_bio, sector, 1);
  498. /*
  499. * The allocated buffers can be smaller than the whole bio,
  500. * so repeat the whole process until all the data can be handled.
  501. */
  502. while (remaining) {
  503. clone = crypt_alloc_buffer(io, remaining);
  504. if (unlikely(!clone)) {
  505. crypt_dec_pending(io, -ENOMEM);
  506. return;
  507. }
  508. ctx.bio_out = clone;
  509. ctx.idx_out = 0;
  510. if (unlikely(crypt_convert(cc, &ctx) < 0)) {
  511. crypt_free_buffer_pages(cc, clone);
  512. bio_put(clone);
  513. crypt_dec_pending(io, -EIO);
  514. return;
  515. }
  516. /* crypt_convert should have filled the clone bio */
  517. BUG_ON(ctx.idx_out < clone->bi_vcnt);
  518. clone->bi_sector = cc->start + sector;
  519. remaining -= clone->bi_size;
  520. sector += bio_sectors(clone);
  521. /* Grab another reference to the io struct
  522. * before we kick off the request */
  523. if (remaining)
  524. atomic_inc(&io->pending);
  525. generic_make_request(clone);
  526. /* Do not reference clone after this - it
  527. * may be gone already. */
  528. /* out of memory -> run queues */
  529. if (remaining)
  530. congestion_wait(WRITE, HZ/100);
  531. }
  532. }
  533. static void process_read_endio(struct dm_crypt_io *io)
  534. {
  535. struct crypt_config *cc = io->target->private;
  536. struct convert_context ctx;
  537. crypt_convert_init(cc, &ctx, io->base_bio, io->base_bio,
  538. io->base_bio->bi_sector - io->target->begin, 0);
  539. crypt_dec_pending(io, crypt_convert(cc, &ctx));
  540. }
  541. static void kcryptd_do_work(struct work_struct *work)
  542. {
  543. struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
  544. if (bio_data_dir(io->base_bio) == READ)
  545. process_read(io);
  546. }
  547. static void kcryptd_do_crypt(struct work_struct *work)
  548. {
  549. struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
  550. if (bio_data_dir(io->base_bio) == READ)
  551. process_read_endio(io);
  552. else
  553. process_write(io);
  554. }
  555. /*
  556. * Decode key from its hex representation
  557. */
  558. static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
  559. {
  560. char buffer[3];
  561. char *endp;
  562. unsigned int i;
  563. buffer[2] = '\0';
  564. for (i = 0; i < size; i++) {
  565. buffer[0] = *hex++;
  566. buffer[1] = *hex++;
  567. key[i] = (u8)simple_strtoul(buffer, &endp, 16);
  568. if (endp != &buffer[2])
  569. return -EINVAL;
  570. }
  571. if (*hex != '\0')
  572. return -EINVAL;
  573. return 0;
  574. }
  575. /*
  576. * Encode key into its hex representation
  577. */
  578. static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
  579. {
  580. unsigned int i;
  581. for (i = 0; i < size; i++) {
  582. sprintf(hex, "%02x", *key);
  583. hex += 2;
  584. key++;
  585. }
  586. }
  587. static int crypt_set_key(struct crypt_config *cc, char *key)
  588. {
  589. unsigned key_size = strlen(key) >> 1;
  590. if (cc->key_size && cc->key_size != key_size)
  591. return -EINVAL;
  592. cc->key_size = key_size; /* initial settings */
  593. if ((!key_size && strcmp(key, "-")) ||
  594. (key_size && crypt_decode_key(cc->key, key, key_size) < 0))
  595. return -EINVAL;
  596. set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  597. return 0;
  598. }
  599. static int crypt_wipe_key(struct crypt_config *cc)
  600. {
  601. clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
  602. memset(&cc->key, 0, cc->key_size * sizeof(u8));
  603. return 0;
  604. }
  605. /*
  606. * Construct an encryption mapping:
  607. * <cipher> <key> <iv_offset> <dev_path> <start>
  608. */
  609. static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
  610. {
  611. struct crypt_config *cc;
  612. struct crypto_blkcipher *tfm;
  613. char *tmp;
  614. char *cipher;
  615. char *chainmode;
  616. char *ivmode;
  617. char *ivopts;
  618. unsigned int key_size;
  619. unsigned long long tmpll;
  620. if (argc != 5) {
  621. ti->error = "Not enough arguments";
  622. return -EINVAL;
  623. }
  624. tmp = argv[0];
  625. cipher = strsep(&tmp, "-");
  626. chainmode = strsep(&tmp, "-");
  627. ivopts = strsep(&tmp, "-");
  628. ivmode = strsep(&ivopts, ":");
  629. if (tmp)
  630. DMWARN("Unexpected additional cipher options");
  631. key_size = strlen(argv[1]) >> 1;
  632. cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
  633. if (cc == NULL) {
  634. ti->error =
  635. "Cannot allocate transparent encryption context";
  636. return -ENOMEM;
  637. }
  638. if (crypt_set_key(cc, argv[1])) {
  639. ti->error = "Error decoding key";
  640. goto bad_cipher;
  641. }
  642. /* Compatiblity mode for old dm-crypt cipher strings */
  643. if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
  644. chainmode = "cbc";
  645. ivmode = "plain";
  646. }
  647. if (strcmp(chainmode, "ecb") && !ivmode) {
  648. ti->error = "This chaining mode requires an IV mechanism";
  649. goto bad_cipher;
  650. }
  651. if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  652. chainmode, cipher) >= CRYPTO_MAX_ALG_NAME) {
  653. ti->error = "Chain mode + cipher name is too long";
  654. goto bad_cipher;
  655. }
  656. tfm = crypto_alloc_blkcipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
  657. if (IS_ERR(tfm)) {
  658. ti->error = "Error allocating crypto tfm";
  659. goto bad_cipher;
  660. }
  661. strcpy(cc->cipher, cipher);
  662. strcpy(cc->chainmode, chainmode);
  663. cc->tfm = tfm;
  664. /*
  665. * Choose ivmode. Valid modes: "plain", "essiv:<esshash>", "benbi".
  666. * See comments at iv code
  667. */
  668. if (ivmode == NULL)
  669. cc->iv_gen_ops = NULL;
  670. else if (strcmp(ivmode, "plain") == 0)
  671. cc->iv_gen_ops = &crypt_iv_plain_ops;
  672. else if (strcmp(ivmode, "essiv") == 0)
  673. cc->iv_gen_ops = &crypt_iv_essiv_ops;
  674. else if (strcmp(ivmode, "benbi") == 0)
  675. cc->iv_gen_ops = &crypt_iv_benbi_ops;
  676. else if (strcmp(ivmode, "null") == 0)
  677. cc->iv_gen_ops = &crypt_iv_null_ops;
  678. else {
  679. ti->error = "Invalid IV mode";
  680. goto bad_ivmode;
  681. }
  682. if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
  683. cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
  684. goto bad_ivmode;
  685. cc->iv_size = crypto_blkcipher_ivsize(tfm);
  686. if (cc->iv_size)
  687. /* at least a 64 bit sector number should fit in our buffer */
  688. cc->iv_size = max(cc->iv_size,
  689. (unsigned int)(sizeof(u64) / sizeof(u8)));
  690. else {
  691. if (cc->iv_gen_ops) {
  692. DMWARN("Selected cipher does not support IVs");
  693. if (cc->iv_gen_ops->dtr)
  694. cc->iv_gen_ops->dtr(cc);
  695. cc->iv_gen_ops = NULL;
  696. }
  697. }
  698. cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
  699. if (!cc->io_pool) {
  700. ti->error = "Cannot allocate crypt io mempool";
  701. goto bad_slab_pool;
  702. }
  703. cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
  704. if (!cc->page_pool) {
  705. ti->error = "Cannot allocate page mempool";
  706. goto bad_page_pool;
  707. }
  708. cc->bs = bioset_create(MIN_IOS, MIN_IOS);
  709. if (!cc->bs) {
  710. ti->error = "Cannot allocate crypt bioset";
  711. goto bad_bs;
  712. }
  713. if (crypto_blkcipher_setkey(tfm, cc->key, key_size) < 0) {
  714. ti->error = "Error setting key";
  715. goto bad_device;
  716. }
  717. if (sscanf(argv[2], "%llu", &tmpll) != 1) {
  718. ti->error = "Invalid iv_offset sector";
  719. goto bad_device;
  720. }
  721. cc->iv_offset = tmpll;
  722. if (sscanf(argv[4], "%llu", &tmpll) != 1) {
  723. ti->error = "Invalid device sector";
  724. goto bad_device;
  725. }
  726. cc->start = tmpll;
  727. if (dm_get_device(ti, argv[3], cc->start, ti->len,
  728. dm_table_get_mode(ti->table), &cc->dev)) {
  729. ti->error = "Device lookup failed";
  730. goto bad_device;
  731. }
  732. if (ivmode && cc->iv_gen_ops) {
  733. if (ivopts)
  734. *(ivopts - 1) = ':';
  735. cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
  736. if (!cc->iv_mode) {
  737. ti->error = "Error kmallocing iv_mode string";
  738. goto bad_ivmode_string;
  739. }
  740. strcpy(cc->iv_mode, ivmode);
  741. } else
  742. cc->iv_mode = NULL;
  743. cc->io_queue = create_singlethread_workqueue("kcryptd_io");
  744. if (!cc->io_queue) {
  745. ti->error = "Couldn't create kcryptd io queue";
  746. goto bad_io_queue;
  747. }
  748. cc->crypt_queue = create_singlethread_workqueue("kcryptd");
  749. if (!cc->crypt_queue) {
  750. ti->error = "Couldn't create kcryptd queue";
  751. goto bad_crypt_queue;
  752. }
  753. ti->private = cc;
  754. return 0;
  755. bad_crypt_queue:
  756. destroy_workqueue(cc->io_queue);
  757. bad_io_queue:
  758. kfree(cc->iv_mode);
  759. bad_ivmode_string:
  760. dm_put_device(ti, cc->dev);
  761. bad_device:
  762. bioset_free(cc->bs);
  763. bad_bs:
  764. mempool_destroy(cc->page_pool);
  765. bad_page_pool:
  766. mempool_destroy(cc->io_pool);
  767. bad_slab_pool:
  768. if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
  769. cc->iv_gen_ops->dtr(cc);
  770. bad_ivmode:
  771. crypto_free_blkcipher(tfm);
  772. bad_cipher:
  773. /* Must zero key material before freeing */
  774. memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
  775. kfree(cc);
  776. return -EINVAL;
  777. }
  778. static void crypt_dtr(struct dm_target *ti)
  779. {
  780. struct crypt_config *cc = (struct crypt_config *) ti->private;
  781. destroy_workqueue(cc->io_queue);
  782. destroy_workqueue(cc->crypt_queue);
  783. bioset_free(cc->bs);
  784. mempool_destroy(cc->page_pool);
  785. mempool_destroy(cc->io_pool);
  786. kfree(cc->iv_mode);
  787. if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
  788. cc->iv_gen_ops->dtr(cc);
  789. crypto_free_blkcipher(cc->tfm);
  790. dm_put_device(ti, cc->dev);
  791. /* Must zero key material before freeing */
  792. memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
  793. kfree(cc);
  794. }
  795. static int crypt_map(struct dm_target *ti, struct bio *bio,
  796. union map_info *map_context)
  797. {
  798. struct crypt_config *cc = ti->private;
  799. struct dm_crypt_io *io;
  800. io = mempool_alloc(cc->io_pool, GFP_NOIO);
  801. io->target = ti;
  802. io->base_bio = bio;
  803. io->error = 0;
  804. atomic_set(&io->pending, 0);
  805. if (bio_data_dir(io->base_bio) == READ)
  806. kcryptd_queue_io(io);
  807. else
  808. kcryptd_queue_crypt(io);
  809. return DM_MAPIO_SUBMITTED;
  810. }
  811. static int crypt_status(struct dm_target *ti, status_type_t type,
  812. char *result, unsigned int maxlen)
  813. {
  814. struct crypt_config *cc = (struct crypt_config *) ti->private;
  815. unsigned int sz = 0;
  816. switch (type) {
  817. case STATUSTYPE_INFO:
  818. result[0] = '\0';
  819. break;
  820. case STATUSTYPE_TABLE:
  821. if (cc->iv_mode)
  822. DMEMIT("%s-%s-%s ", cc->cipher, cc->chainmode,
  823. cc->iv_mode);
  824. else
  825. DMEMIT("%s-%s ", cc->cipher, cc->chainmode);
  826. if (cc->key_size > 0) {
  827. if ((maxlen - sz) < ((cc->key_size << 1) + 1))
  828. return -ENOMEM;
  829. crypt_encode_key(result + sz, cc->key, cc->key_size);
  830. sz += cc->key_size << 1;
  831. } else {
  832. if (sz >= maxlen)
  833. return -ENOMEM;
  834. result[sz++] = '-';
  835. }
  836. DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
  837. cc->dev->name, (unsigned long long)cc->start);
  838. break;
  839. }
  840. return 0;
  841. }
  842. static void crypt_postsuspend(struct dm_target *ti)
  843. {
  844. struct crypt_config *cc = ti->private;
  845. set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  846. }
  847. static int crypt_preresume(struct dm_target *ti)
  848. {
  849. struct crypt_config *cc = ti->private;
  850. if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
  851. DMERR("aborting resume - crypt key is not set.");
  852. return -EAGAIN;
  853. }
  854. return 0;
  855. }
  856. static void crypt_resume(struct dm_target *ti)
  857. {
  858. struct crypt_config *cc = ti->private;
  859. clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
  860. }
  861. /* Message interface
  862. * key set <key>
  863. * key wipe
  864. */
  865. static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
  866. {
  867. struct crypt_config *cc = ti->private;
  868. if (argc < 2)
  869. goto error;
  870. if (!strnicmp(argv[0], MESG_STR("key"))) {
  871. if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
  872. DMWARN("not suspended during key manipulation.");
  873. return -EINVAL;
  874. }
  875. if (argc == 3 && !strnicmp(argv[1], MESG_STR("set")))
  876. return crypt_set_key(cc, argv[2]);
  877. if (argc == 2 && !strnicmp(argv[1], MESG_STR("wipe")))
  878. return crypt_wipe_key(cc);
  879. }
  880. error:
  881. DMWARN("unrecognised message received.");
  882. return -EINVAL;
  883. }
  884. static struct target_type crypt_target = {
  885. .name = "crypt",
  886. .version= {1, 5, 0},
  887. .module = THIS_MODULE,
  888. .ctr = crypt_ctr,
  889. .dtr = crypt_dtr,
  890. .map = crypt_map,
  891. .status = crypt_status,
  892. .postsuspend = crypt_postsuspend,
  893. .preresume = crypt_preresume,
  894. .resume = crypt_resume,
  895. .message = crypt_message,
  896. };
  897. static int __init dm_crypt_init(void)
  898. {
  899. int r;
  900. _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
  901. if (!_crypt_io_pool)
  902. return -ENOMEM;
  903. r = dm_register_target(&crypt_target);
  904. if (r < 0) {
  905. DMERR("register failed %d", r);
  906. kmem_cache_destroy(_crypt_io_pool);
  907. }
  908. return r;
  909. }
  910. static void __exit dm_crypt_exit(void)
  911. {
  912. int r = dm_unregister_target(&crypt_target);
  913. if (r < 0)
  914. DMERR("unregister failed %d", r);
  915. kmem_cache_destroy(_crypt_io_pool);
  916. }
  917. module_init(dm_crypt_init);
  918. module_exit(dm_crypt_exit);
  919. MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
  920. MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
  921. MODULE_LICENSE("GPL");