blob: 3783cf97885016d8509e69d1ee16dd8a9fe9837f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Christophe Saout <christophe@saout.de>
3 * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
Milan Broze48d4bb2006-10-03 01:15:37 -07004 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This file is released under the GPL.
7 */
8
Herbert Xud1806f62006-08-22 20:29:17 +10009#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/kernel.h>
13#include <linux/bio.h>
14#include <linux/blkdev.h>
15#include <linux/mempool.h>
16#include <linux/slab.h>
17#include <linux/crypto.h>
18#include <linux/workqueue.h>
19#include <asm/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100020#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/page.h>
22
23#include "dm.h"
24
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "crypt"
Milan Broze48d4bb2006-10-03 01:15:37 -070026#define MESG_STR(x) x, sizeof(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28/*
29 * per bio private data
30 */
31struct crypt_io {
32 struct dm_target *target;
Milan Broz8b004452006-10-03 01:15:37 -070033 struct bio *base_bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 struct bio *first_clone;
35 struct work_struct work;
36 atomic_t pending;
37 int error;
38};
39
40/*
41 * context holding the current state of a multi-part conversion
42 */
43struct convert_context {
44 struct bio *bio_in;
45 struct bio *bio_out;
46 unsigned int offset_in;
47 unsigned int offset_out;
48 unsigned int idx_in;
49 unsigned int idx_out;
50 sector_t sector;
51 int write;
52};
53
54struct crypt_config;
55
56struct crypt_iv_operations {
57 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
58 const char *opts);
59 void (*dtr)(struct crypt_config *cc);
60 const char *(*status)(struct crypt_config *cc);
61 int (*generator)(struct crypt_config *cc, u8 *iv, sector_t sector);
62};
63
64/*
65 * Crypt: maps a linear range of a block device
66 * and encrypts / decrypts at the same time.
67 */
Milan Broze48d4bb2006-10-03 01:15:37 -070068enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
Linus Torvalds1da177e2005-04-16 15:20:36 -070069struct crypt_config {
70 struct dm_dev *dev;
71 sector_t start;
72
73 /*
74 * pool for per bio private data and
75 * for encryption buffer pages
76 */
77 mempool_t *io_pool;
78 mempool_t *page_pool;
79
80 /*
81 * crypto related data
82 */
83 struct crypt_iv_operations *iv_gen_ops;
84 char *iv_mode;
Herbert Xud1806f62006-08-22 20:29:17 +100085 struct crypto_cipher *iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 sector_t iv_offset;
87 unsigned int iv_size;
88
Herbert Xud1806f62006-08-22 20:29:17 +100089 char cipher[CRYPTO_MAX_ALG_NAME];
90 char chainmode[CRYPTO_MAX_ALG_NAME];
91 struct crypto_blkcipher *tfm;
Milan Broze48d4bb2006-10-03 01:15:37 -070092 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 unsigned int key_size;
94 u8 key[0];
95};
96
97#define MIN_IOS 256
98#define MIN_POOL_PAGES 32
99#define MIN_BIO_PAGES 8
100
101static kmem_cache_t *_crypt_io_pool;
102
103/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 * Different IV generation algorithms:
105 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000106 * plain: the initial vector is the 32-bit little-endian version of the sector
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * number, padded with zeros if neccessary.
108 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000109 * essiv: "encrypted sector|salt initial vector", the sector number is
110 * encrypted with the bulk cipher using a salt as key. The salt
111 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 *
113 * plumb: unimplemented, see:
114 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
115 */
116
117static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
118{
119 memset(iv, 0, cc->iv_size);
120 *(u32 *)iv = cpu_to_le32(sector & 0xffffffff);
121
122 return 0;
123}
124
125static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
126 const char *opts)
127{
Herbert Xud1806f62006-08-22 20:29:17 +1000128 struct crypto_cipher *essiv_tfm;
Herbert Xu35058682006-08-24 19:10:20 +1000129 struct crypto_hash *hash_tfm;
130 struct hash_desc desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct scatterlist sg;
132 unsigned int saltsize;
133 u8 *salt;
Herbert Xud1806f62006-08-22 20:29:17 +1000134 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 if (opts == NULL) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700137 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return -EINVAL;
139 }
140
141 /* Hash the cipher key with the given hash algorithm */
Herbert Xu35058682006-08-24 19:10:20 +1000142 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
143 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700144 ti->error = "Error initializing ESSIV hash";
Herbert Xu35058682006-08-24 19:10:20 +1000145 return PTR_ERR(hash_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147
Herbert Xu35058682006-08-24 19:10:20 +1000148 saltsize = crypto_hash_digestsize(hash_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 salt = kmalloc(saltsize, GFP_KERNEL);
150 if (salt == NULL) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700151 ti->error = "Error kmallocing salt storage in ESSIV";
Herbert Xu35058682006-08-24 19:10:20 +1000152 crypto_free_hash(hash_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return -ENOMEM;
154 }
155
David Hardeman378f0582005-09-17 17:55:31 +1000156 sg_set_buf(&sg, cc->key, cc->key_size);
Herbert Xu35058682006-08-24 19:10:20 +1000157 desc.tfm = hash_tfm;
158 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
159 err = crypto_hash_digest(&desc, &sg, cc->key_size, salt);
160 crypto_free_hash(hash_tfm);
161
162 if (err) {
163 ti->error = "Error calculating hash in ESSIV";
164 return err;
165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 /* Setup the essiv_tfm with the given salt */
Herbert Xud1806f62006-08-22 20:29:17 +1000168 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
169 if (IS_ERR(essiv_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700170 ti->error = "Error allocating crypto tfm for ESSIV";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 kfree(salt);
Herbert Xud1806f62006-08-22 20:29:17 +1000172 return PTR_ERR(essiv_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
Herbert Xud1806f62006-08-22 20:29:17 +1000174 if (crypto_cipher_blocksize(essiv_tfm) !=
175 crypto_blkcipher_ivsize(cc->tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700176 ti->error = "Block size of ESSIV cipher does "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 "not match IV size of block cipher";
Herbert Xud1806f62006-08-22 20:29:17 +1000178 crypto_free_cipher(essiv_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 kfree(salt);
180 return -EINVAL;
181 }
Herbert Xud1806f62006-08-22 20:29:17 +1000182 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
183 if (err) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700184 ti->error = "Failed to set key for ESSIV cipher";
Herbert Xud1806f62006-08-22 20:29:17 +1000185 crypto_free_cipher(essiv_tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 kfree(salt);
Herbert Xud1806f62006-08-22 20:29:17 +1000187 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189 kfree(salt);
190
Herbert Xud1806f62006-08-22 20:29:17 +1000191 cc->iv_gen_private = essiv_tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return 0;
193}
194
195static void crypt_iv_essiv_dtr(struct crypt_config *cc)
196{
Herbert Xud1806f62006-08-22 20:29:17 +1000197 crypto_free_cipher(cc->iv_gen_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 cc->iv_gen_private = NULL;
199}
200
201static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
202{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 memset(iv, 0, cc->iv_size);
204 *(u64 *)iv = cpu_to_le64(sector);
Herbert Xud1806f62006-08-22 20:29:17 +1000205 crypto_cipher_encrypt_one(cc->iv_gen_private, iv, iv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return 0;
207}
208
209static struct crypt_iv_operations crypt_iv_plain_ops = {
210 .generator = crypt_iv_plain_gen
211};
212
213static struct crypt_iv_operations crypt_iv_essiv_ops = {
214 .ctr = crypt_iv_essiv_ctr,
215 .dtr = crypt_iv_essiv_dtr,
216 .generator = crypt_iv_essiv_gen
217};
218
219
Arjan van de Ven858119e2006-01-14 13:20:43 -0800220static int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221crypt_convert_scatterlist(struct crypt_config *cc, struct scatterlist *out,
222 struct scatterlist *in, unsigned int length,
223 int write, sector_t sector)
224{
225 u8 iv[cc->iv_size];
Herbert Xud1806f62006-08-22 20:29:17 +1000226 struct blkcipher_desc desc = {
227 .tfm = cc->tfm,
228 .info = iv,
229 .flags = CRYPTO_TFM_REQ_MAY_SLEEP,
230 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int r;
232
233 if (cc->iv_gen_ops) {
234 r = cc->iv_gen_ops->generator(cc, iv, sector);
235 if (r < 0)
236 return r;
237
238 if (write)
Herbert Xud1806f62006-08-22 20:29:17 +1000239 r = crypto_blkcipher_encrypt_iv(&desc, out, in, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 else
Herbert Xud1806f62006-08-22 20:29:17 +1000241 r = crypto_blkcipher_decrypt_iv(&desc, out, in, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 } else {
243 if (write)
Herbert Xud1806f62006-08-22 20:29:17 +1000244 r = crypto_blkcipher_encrypt(&desc, out, in, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 else
Herbert Xud1806f62006-08-22 20:29:17 +1000246 r = crypto_blkcipher_decrypt(&desc, out, in, length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
249 return r;
250}
251
252static void
253crypt_convert_init(struct crypt_config *cc, struct convert_context *ctx,
254 struct bio *bio_out, struct bio *bio_in,
255 sector_t sector, int write)
256{
257 ctx->bio_in = bio_in;
258 ctx->bio_out = bio_out;
259 ctx->offset_in = 0;
260 ctx->offset_out = 0;
261 ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
262 ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
263 ctx->sector = sector + cc->iv_offset;
264 ctx->write = write;
265}
266
267/*
268 * Encrypt / decrypt data from one bio to another one (can be the same one)
269 */
270static int crypt_convert(struct crypt_config *cc,
271 struct convert_context *ctx)
272{
273 int r = 0;
274
275 while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
276 ctx->idx_out < ctx->bio_out->bi_vcnt) {
277 struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
278 struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
279 struct scatterlist sg_in = {
280 .page = bv_in->bv_page,
281 .offset = bv_in->bv_offset + ctx->offset_in,
282 .length = 1 << SECTOR_SHIFT
283 };
284 struct scatterlist sg_out = {
285 .page = bv_out->bv_page,
286 .offset = bv_out->bv_offset + ctx->offset_out,
287 .length = 1 << SECTOR_SHIFT
288 };
289
290 ctx->offset_in += sg_in.length;
291 if (ctx->offset_in >= bv_in->bv_len) {
292 ctx->offset_in = 0;
293 ctx->idx_in++;
294 }
295
296 ctx->offset_out += sg_out.length;
297 if (ctx->offset_out >= bv_out->bv_len) {
298 ctx->offset_out = 0;
299 ctx->idx_out++;
300 }
301
302 r = crypt_convert_scatterlist(cc, &sg_out, &sg_in, sg_in.length,
303 ctx->write, ctx->sector);
304 if (r < 0)
305 break;
306
307 ctx->sector++;
308 }
309
310 return r;
311}
312
313/*
314 * Generate a new unfragmented bio with the given size
315 * This should never violate the device limitations
316 * May return a smaller bio when running out of pages
317 */
318static struct bio *
319crypt_alloc_buffer(struct crypt_config *cc, unsigned int size,
320 struct bio *base_bio, unsigned int *bio_vec_idx)
321{
Milan Broz8b004452006-10-03 01:15:37 -0700322 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Virob4e3ca12005-10-21 03:22:34 -0400324 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 unsigned int i;
326
327 /*
Nick Pigginbd53b712005-05-01 08:58:37 -0700328 * Use __GFP_NOMEMALLOC to tell the VM to act less aggressively and
329 * to fail earlier. This is not necessary but increases throughput.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 * FIXME: Is this really intelligent?
331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (base_bio)
Milan Broz8b004452006-10-03 01:15:37 -0700333 clone = bio_clone(base_bio, GFP_NOIO|__GFP_NOMEMALLOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 else
Milan Broz8b004452006-10-03 01:15:37 -0700335 clone = bio_alloc(GFP_NOIO|__GFP_NOMEMALLOC, nr_iovecs);
336 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* if the last bio was not complete, continue where that one ended */
Milan Broz8b004452006-10-03 01:15:37 -0700340 clone->bi_idx = *bio_vec_idx;
341 clone->bi_vcnt = *bio_vec_idx;
342 clone->bi_size = 0;
343 clone->bi_flags &= ~(1 << BIO_SEG_VALID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Milan Broz8b004452006-10-03 01:15:37 -0700345 /* clone->bi_idx pages have already been allocated */
346 size -= clone->bi_idx * PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Milan Broz8b004452006-10-03 01:15:37 -0700348 for (i = clone->bi_idx; i < nr_iovecs; i++) {
349 struct bio_vec *bv = bio_iovec_idx(clone, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 bv->bv_page = mempool_alloc(cc->page_pool, gfp_mask);
352 if (!bv->bv_page)
353 break;
354
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 */
Milan Broz8b004452006-10-03 01:15:37 -0700360 if ((i - clone->bi_idx) == (MIN_BIO_PAGES - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
362
363 bv->bv_offset = 0;
364 if (size > PAGE_SIZE)
365 bv->bv_len = PAGE_SIZE;
366 else
367 bv->bv_len = size;
368
Milan Broz8b004452006-10-03 01:15:37 -0700369 clone->bi_size += bv->bv_len;
370 clone->bi_vcnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 size -= bv->bv_len;
372 }
373
Milan Broz8b004452006-10-03 01:15:37 -0700374 if (!clone->bi_size) {
375 bio_put(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return NULL;
377 }
378
379 /*
380 * Remember the last bio_vec allocated to be able
381 * to correctly continue after the splitting.
382 */
Milan Broz8b004452006-10-03 01:15:37 -0700383 *bio_vec_idx = clone->bi_vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Milan Broz8b004452006-10-03 01:15:37 -0700385 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388static void crypt_free_buffer_pages(struct crypt_config *cc,
Milan Broz8b004452006-10-03 01:15:37 -0700389 struct bio *clone, unsigned int bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 unsigned int i, start, end;
392 struct bio_vec *bv;
393
394 /*
395 * This is ugly, but Jens Axboe thinks that using bi_idx in the
396 * endio function is too dangerous at the moment, so I calculate the
397 * correct position using bi_vcnt and bi_size.
398 * The bv_offset and bv_len fields might already be modified but we
399 * know that we always allocated whole pages.
400 * A fix to the bi_idx issue in the kernel is in the works, so
401 * we will hopefully be able to revert to the cleaner solution soon.
402 */
Milan Broz8b004452006-10-03 01:15:37 -0700403 i = clone->bi_vcnt - 1;
404 bv = bio_iovec_idx(clone, i);
405 end = (i << PAGE_SHIFT) + (bv->bv_offset + bv->bv_len) - clone->bi_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 start = end - bytes;
407
408 start >>= PAGE_SHIFT;
Milan Broz8b004452006-10-03 01:15:37 -0700409 if (!clone->bi_size)
410 end = clone->bi_vcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 else
412 end >>= PAGE_SHIFT;
413
Milan Broz8b004452006-10-03 01:15:37 -0700414 for (i = start; i < end; i++) {
415 bv = bio_iovec_idx(clone, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 BUG_ON(!bv->bv_page);
417 mempool_free(bv->bv_page, cc->page_pool);
418 bv->bv_page = NULL;
419 }
420}
421
422/*
423 * One of the bios was finished. Check for completion of
424 * the whole request and correctly clean up the buffer.
425 */
426static void dec_pending(struct crypt_io *io, int error)
427{
428 struct crypt_config *cc = (struct crypt_config *) io->target->private;
429
430 if (error < 0)
431 io->error = error;
432
433 if (!atomic_dec_and_test(&io->pending))
434 return;
435
436 if (io->first_clone)
437 bio_put(io->first_clone);
438
Milan Broz8b004452006-10-03 01:15:37 -0700439 bio_endio(io->base_bio, io->base_bio->bi_size, io->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 mempool_free(io, cc->io_pool);
442}
443
444/*
445 * kcryptd:
446 *
447 * Needed because it would be very unwise to do decryption in an
448 * interrupt context, so bios returning from read requests get
449 * queued here.
450 */
451static struct workqueue_struct *_kcryptd_workqueue;
Milan Broz8b004452006-10-03 01:15:37 -0700452static void kcryptd_do_work(void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454static void kcryptd_queue_io(struct crypt_io *io)
455{
456 INIT_WORK(&io->work, kcryptd_do_work, io);
457 queue_work(_kcryptd_workqueue, &io->work);
458}
459
Milan Broz8b004452006-10-03 01:15:37 -0700460static int crypt_endio(struct bio *clone, unsigned int done, int error)
461{
462 struct crypt_io *io = clone->bi_private;
463 struct crypt_config *cc = io->target->private;
464 unsigned read_io = bio_data_dir(clone) == READ;
465
466 /*
467 * free the processed pages, even if
468 * it's only a partially completed write
469 */
470 if (!read_io)
471 crypt_free_buffer_pages(cc, clone, done);
472
473 if (unlikely(clone->bi_size))
474 return 1;
475
476 /*
477 * successful reads are decrypted by the worker thread
478 */
479 if (!read_io)
480 goto out;
481
482 if (unlikely(!bio_flagged(clone, BIO_UPTODATE))) {
483 error = -EIO;
484 goto out;
485 }
486
487 bio_put(clone);
488 kcryptd_queue_io(io);
489 return 0;
490
491out:
492 bio_put(clone);
493 dec_pending(io, error);
494 return error;
495}
496
497static void clone_init(struct crypt_io *io, struct bio *clone)
498{
499 struct crypt_config *cc = io->target->private;
500
501 clone->bi_private = io;
502 clone->bi_end_io = crypt_endio;
503 clone->bi_bdev = cc->dev->bdev;
504 clone->bi_rw = io->base_bio->bi_rw;
505}
506
507static struct bio *clone_read(struct crypt_io *io,
508 sector_t sector)
509{
510 struct crypt_config *cc = io->target->private;
511 struct bio *base_bio = io->base_bio;
512 struct bio *clone;
513
514 /*
515 * The block layer might modify the bvec array, so always
516 * copy the required bvecs because we need the original
517 * one in order to decrypt the whole bio data *afterwards*.
518 */
519 clone = bio_alloc(GFP_NOIO, bio_segments(base_bio));
520 if (unlikely(!clone))
521 return NULL;
522
523 clone_init(io, clone);
524 clone->bi_idx = 0;
525 clone->bi_vcnt = bio_segments(base_bio);
526 clone->bi_size = base_bio->bi_size;
527 memcpy(clone->bi_io_vec, bio_iovec(base_bio),
528 sizeof(struct bio_vec) * clone->bi_vcnt);
529 clone->bi_sector = cc->start + sector;
530
531 return clone;
532}
533
534static struct bio *clone_write(struct crypt_io *io,
535 sector_t sector,
536 unsigned *bvec_idx,
537 struct convert_context *ctx)
538{
539 struct crypt_config *cc = io->target->private;
540 struct bio *base_bio = io->base_bio;
541 struct bio *clone;
542
543 clone = crypt_alloc_buffer(cc, base_bio->bi_size,
544 io->first_clone, bvec_idx);
545 if (!clone)
546 return NULL;
547
548 ctx->bio_out = clone;
549
550 if (unlikely(crypt_convert(cc, ctx) < 0)) {
551 crypt_free_buffer_pages(cc, clone,
552 clone->bi_size);
553 bio_put(clone);
554 return NULL;
555 }
556
557 clone_init(io, clone);
558 clone->bi_sector = cc->start + sector;
559
560 return clone;
561}
562
563static void process_read_endio(struct crypt_io *io)
564{
565 struct crypt_config *cc = io->target->private;
566 struct convert_context ctx;
567
568 crypt_convert_init(cc, &ctx, io->base_bio, io->base_bio,
569 io->base_bio->bi_sector - io->target->begin, 0);
570
571 dec_pending(io, crypt_convert(cc, &ctx));
572}
573
574static void kcryptd_do_work(void *data)
575{
576 struct crypt_io *io = data;
577
578 process_read_endio(io);
579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/*
582 * Decode key from its hex representation
583 */
584static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
585{
586 char buffer[3];
587 char *endp;
588 unsigned int i;
589
590 buffer[2] = '\0';
591
Milan Broz8b004452006-10-03 01:15:37 -0700592 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 buffer[0] = *hex++;
594 buffer[1] = *hex++;
595
596 key[i] = (u8)simple_strtoul(buffer, &endp, 16);
597
598 if (endp != &buffer[2])
599 return -EINVAL;
600 }
601
602 if (*hex != '\0')
603 return -EINVAL;
604
605 return 0;
606}
607
608/*
609 * Encode key into its hex representation
610 */
611static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
612{
613 unsigned int i;
614
Milan Broz8b004452006-10-03 01:15:37 -0700615 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 sprintf(hex, "%02x", *key);
617 hex += 2;
618 key++;
619 }
620}
621
Milan Broze48d4bb2006-10-03 01:15:37 -0700622static int crypt_set_key(struct crypt_config *cc, char *key)
623{
624 unsigned key_size = strlen(key) >> 1;
625
626 if (cc->key_size && cc->key_size != key_size)
627 return -EINVAL;
628
629 cc->key_size = key_size; /* initial settings */
630
631 if ((!key_size && strcmp(key, "-")) ||
632 (key_size && crypt_decode_key(cc->key, key, key_size) < 0))
633 return -EINVAL;
634
635 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
636
637 return 0;
638}
639
640static int crypt_wipe_key(struct crypt_config *cc)
641{
642 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
643 memset(&cc->key, 0, cc->key_size * sizeof(u8));
644 return 0;
645}
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647/*
648 * Construct an encryption mapping:
649 * <cipher> <key> <iv_offset> <dev_path> <start>
650 */
651static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
652{
653 struct crypt_config *cc;
Herbert Xud1806f62006-08-22 20:29:17 +1000654 struct crypto_blkcipher *tfm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 char *tmp;
656 char *cipher;
657 char *chainmode;
658 char *ivmode;
659 char *ivopts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 unsigned int key_size;
Andrew Morton4ee218c2006-03-27 01:17:48 -0800661 unsigned long long tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (argc != 5) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700664 ti->error = "Not enough arguments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return -EINVAL;
666 }
667
668 tmp = argv[0];
669 cipher = strsep(&tmp, "-");
670 chainmode = strsep(&tmp, "-");
671 ivopts = strsep(&tmp, "-");
672 ivmode = strsep(&ivopts, ":");
673
674 if (tmp)
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700675 DMWARN("Unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 key_size = strlen(argv[1]) >> 1;
678
Milan Broze48d4bb2006-10-03 01:15:37 -0700679 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (cc == NULL) {
681 ti->error =
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700682 "Cannot allocate transparent encryption context";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return -ENOMEM;
684 }
685
Milan Broze48d4bb2006-10-03 01:15:37 -0700686 if (crypt_set_key(cc, argv[1])) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700687 ti->error = "Error decoding key";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 goto bad1;
689 }
690
691 /* Compatiblity mode for old dm-crypt cipher strings */
692 if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
693 chainmode = "cbc";
694 ivmode = "plain";
695 }
696
Herbert Xud1806f62006-08-22 20:29:17 +1000697 if (strcmp(chainmode, "ecb") && !ivmode) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700698 ti->error = "This chaining mode requires an IV mechanism";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 goto bad1;
700 }
701
Herbert Xud1806f62006-08-22 20:29:17 +1000702 if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)", chainmode,
703 cipher) >= CRYPTO_MAX_ALG_NAME) {
704 ti->error = "Chain mode + cipher name is too long";
705 goto bad1;
706 }
707
708 tfm = crypto_alloc_blkcipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
709 if (IS_ERR(tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700710 ti->error = "Error allocating crypto tfm";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 goto bad1;
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Herbert Xud1806f62006-08-22 20:29:17 +1000714 strcpy(cc->cipher, cipher);
715 strcpy(cc->chainmode, chainmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 cc->tfm = tfm;
717
718 /*
719 * Choose ivmode. Valid modes: "plain", "essiv:<esshash>".
720 * See comments at iv code
721 */
722
723 if (ivmode == NULL)
724 cc->iv_gen_ops = NULL;
725 else if (strcmp(ivmode, "plain") == 0)
726 cc->iv_gen_ops = &crypt_iv_plain_ops;
727 else if (strcmp(ivmode, "essiv") == 0)
728 cc->iv_gen_ops = &crypt_iv_essiv_ops;
729 else {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700730 ti->error = "Invalid IV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 goto bad2;
732 }
733
734 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
735 cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
736 goto bad2;
737
Herbert Xud1806f62006-08-22 20:29:17 +1000738 cc->iv_size = crypto_blkcipher_ivsize(tfm);
739 if (cc->iv_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 /* at least a 64 bit sector number should fit in our buffer */
Herbert Xud1806f62006-08-22 20:29:17 +1000741 cc->iv_size = max(cc->iv_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 (unsigned int)(sizeof(u64) / sizeof(u8)));
743 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (cc->iv_gen_ops) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700745 DMWARN("Selected cipher does not support IVs");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (cc->iv_gen_ops->dtr)
747 cc->iv_gen_ops->dtr(cc);
748 cc->iv_gen_ops = NULL;
749 }
750 }
751
Matthew Dobson93d23412006-03-26 01:37:50 -0800752 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (!cc->io_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700754 ti->error = "Cannot allocate crypt io mempool";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 goto bad3;
756 }
757
Matthew Dobsona19b27c2006-03-26 01:37:45 -0800758 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700760 ti->error = "Cannot allocate page mempool";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 goto bad4;
762 }
763
Herbert Xud1806f62006-08-22 20:29:17 +1000764 if (crypto_blkcipher_setkey(tfm, cc->key, key_size) < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700765 ti->error = "Error setting key";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 goto bad5;
767 }
768
Andrew Morton4ee218c2006-03-27 01:17:48 -0800769 if (sscanf(argv[2], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700770 ti->error = "Invalid iv_offset sector";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 goto bad5;
772 }
Andrew Morton4ee218c2006-03-27 01:17:48 -0800773 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Andrew Morton4ee218c2006-03-27 01:17:48 -0800775 if (sscanf(argv[4], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700776 ti->error = "Invalid device sector";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 goto bad5;
778 }
Andrew Morton4ee218c2006-03-27 01:17:48 -0800779 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 if (dm_get_device(ti, argv[3], cc->start, ti->len,
782 dm_table_get_mode(ti->table), &cc->dev)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700783 ti->error = "Device lookup failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 goto bad5;
785 }
786
787 if (ivmode && cc->iv_gen_ops) {
788 if (ivopts)
789 *(ivopts - 1) = ':';
790 cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
791 if (!cc->iv_mode) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700792 ti->error = "Error kmallocing iv_mode string";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 goto bad5;
794 }
795 strcpy(cc->iv_mode, ivmode);
796 } else
797 cc->iv_mode = NULL;
798
799 ti->private = cc;
800 return 0;
801
802bad5:
803 mempool_destroy(cc->page_pool);
804bad4:
805 mempool_destroy(cc->io_pool);
806bad3:
807 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
808 cc->iv_gen_ops->dtr(cc);
809bad2:
Herbert Xud1806f62006-08-22 20:29:17 +1000810 crypto_free_blkcipher(tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811bad1:
Stefan Rompf9d3520a2006-01-06 00:20:08 -0800812 /* Must zero key material before freeing */
813 memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 kfree(cc);
815 return -EINVAL;
816}
817
818static void crypt_dtr(struct dm_target *ti)
819{
820 struct crypt_config *cc = (struct crypt_config *) ti->private;
821
822 mempool_destroy(cc->page_pool);
823 mempool_destroy(cc->io_pool);
824
Jesper Juhl990a8ba2005-06-21 17:17:30 -0700825 kfree(cc->iv_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
827 cc->iv_gen_ops->dtr(cc);
Herbert Xud1806f62006-08-22 20:29:17 +1000828 crypto_free_blkcipher(cc->tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 dm_put_device(ti, cc->dev);
Stefan Rompf9d3520a2006-01-06 00:20:08 -0800830
831 /* Must zero key material before freeing */
832 memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 kfree(cc);
834}
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836static int crypt_map(struct dm_target *ti, struct bio *bio,
837 union map_info *map_context)
838{
Milan Broz8b004452006-10-03 01:15:37 -0700839 struct crypt_config *cc = ti->private;
Milan Broze48d4bb2006-10-03 01:15:37 -0700840 struct crypt_io *io;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct convert_context ctx;
842 struct bio *clone;
843 unsigned int remaining = bio->bi_size;
844 sector_t sector = bio->bi_sector - ti->begin;
845 unsigned int bvec_idx = 0;
846
Milan Broze48d4bb2006-10-03 01:15:37 -0700847 io = mempool_alloc(cc->io_pool, GFP_NOIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 io->target = ti;
Milan Broz8b004452006-10-03 01:15:37 -0700849 io->base_bio = bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 io->first_clone = NULL;
851 io->error = 0;
852 atomic_set(&io->pending, 1); /* hold a reference */
853
854 if (bio_data_dir(bio) == WRITE)
855 crypt_convert_init(cc, &ctx, NULL, bio, sector, 1);
856
857 /*
858 * The allocated buffers can be smaller than the whole bio,
859 * so repeat the whole process until all the data can be handled.
860 */
861 while (remaining) {
Milan Broz8b004452006-10-03 01:15:37 -0700862 if (bio_data_dir(bio) == WRITE)
863 clone = clone_write(io, sector, &bvec_idx, &ctx);
864 else
865 clone = clone_read(io, sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (!clone)
867 goto cleanup;
868
869 if (!io->first_clone) {
870 /*
871 * hold a reference to the first clone, because it
872 * holds the bio_vec array and that can't be freed
873 * before all other clones are released
874 */
875 bio_get(clone);
876 io->first_clone = clone;
877 }
878 atomic_inc(&io->pending);
879
880 remaining -= clone->bi_size;
881 sector += bio_sectors(clone);
882
883 generic_make_request(clone);
884
885 /* out of memory -> run queues */
886 if (remaining)
887 blk_congestion_wait(bio_data_dir(clone), HZ/100);
888 }
889
890 /* drop reference, clones could have returned before we reach this */
891 dec_pending(io, 0);
892 return 0;
893
894cleanup:
895 if (io->first_clone) {
896 dec_pending(io, -ENOMEM);
897 return 0;
898 }
899
900 /* if no bio has been dispatched yet, we can directly return the error */
901 mempool_free(io, cc->io_pool);
902 return -ENOMEM;
903}
904
905static int crypt_status(struct dm_target *ti, status_type_t type,
906 char *result, unsigned int maxlen)
907{
908 struct crypt_config *cc = (struct crypt_config *) ti->private;
909 const char *cipher;
910 const char *chainmode = NULL;
911 unsigned int sz = 0;
912
913 switch (type) {
914 case STATUSTYPE_INFO:
915 result[0] = '\0';
916 break;
917
918 case STATUSTYPE_TABLE:
Herbert Xud1806f62006-08-22 20:29:17 +1000919 cipher = crypto_blkcipher_name(cc->tfm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Herbert Xud1806f62006-08-22 20:29:17 +1000921 chainmode = cc->chainmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (cc->iv_mode)
924 DMEMIT("%s-%s-%s ", cipher, chainmode, cc->iv_mode);
925 else
926 DMEMIT("%s-%s ", cipher, chainmode);
927
928 if (cc->key_size > 0) {
929 if ((maxlen - sz) < ((cc->key_size << 1) + 1))
930 return -ENOMEM;
931
932 crypt_encode_key(result + sz, cc->key, cc->key_size);
933 sz += cc->key_size << 1;
934 } else {
935 if (sz >= maxlen)
936 return -ENOMEM;
937 result[sz++] = '-';
938 }
939
Andrew Morton4ee218c2006-03-27 01:17:48 -0800940 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
941 cc->dev->name, (unsigned long long)cc->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 break;
943 }
944 return 0;
945}
946
Milan Broze48d4bb2006-10-03 01:15:37 -0700947static void crypt_postsuspend(struct dm_target *ti)
948{
949 struct crypt_config *cc = ti->private;
950
951 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
952}
953
954static int crypt_preresume(struct dm_target *ti)
955{
956 struct crypt_config *cc = ti->private;
957
958 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
959 DMERR("aborting resume - crypt key is not set.");
960 return -EAGAIN;
961 }
962
963 return 0;
964}
965
966static void crypt_resume(struct dm_target *ti)
967{
968 struct crypt_config *cc = ti->private;
969
970 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
971}
972
973/* Message interface
974 * key set <key>
975 * key wipe
976 */
977static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
978{
979 struct crypt_config *cc = ti->private;
980
981 if (argc < 2)
982 goto error;
983
984 if (!strnicmp(argv[0], MESG_STR("key"))) {
985 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
986 DMWARN("not suspended during key manipulation.");
987 return -EINVAL;
988 }
989 if (argc == 3 && !strnicmp(argv[1], MESG_STR("set")))
990 return crypt_set_key(cc, argv[2]);
991 if (argc == 2 && !strnicmp(argv[1], MESG_STR("wipe")))
992 return crypt_wipe_key(cc);
993 }
994
995error:
996 DMWARN("unrecognised message received.");
997 return -EINVAL;
998}
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000static struct target_type crypt_target = {
1001 .name = "crypt",
Milan Broze48d4bb2006-10-03 01:15:37 -07001002 .version= {1, 2, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 .module = THIS_MODULE,
1004 .ctr = crypt_ctr,
1005 .dtr = crypt_dtr,
1006 .map = crypt_map,
1007 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07001008 .postsuspend = crypt_postsuspend,
1009 .preresume = crypt_preresume,
1010 .resume = crypt_resume,
1011 .message = crypt_message,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012};
1013
1014static int __init dm_crypt_init(void)
1015{
1016 int r;
1017
1018 _crypt_io_pool = kmem_cache_create("dm-crypt_io",
1019 sizeof(struct crypt_io),
1020 0, 0, NULL, NULL);
1021 if (!_crypt_io_pool)
1022 return -ENOMEM;
1023
1024 _kcryptd_workqueue = create_workqueue("kcryptd");
1025 if (!_kcryptd_workqueue) {
1026 r = -ENOMEM;
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001027 DMERR("couldn't create kcryptd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 goto bad1;
1029 }
1030
1031 r = dm_register_target(&crypt_target);
1032 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001033 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 goto bad2;
1035 }
1036
1037 return 0;
1038
1039bad2:
1040 destroy_workqueue(_kcryptd_workqueue);
1041bad1:
1042 kmem_cache_destroy(_crypt_io_pool);
1043 return r;
1044}
1045
1046static void __exit dm_crypt_exit(void)
1047{
1048 int r = dm_unregister_target(&crypt_target);
1049
1050 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001051 DMERR("unregister failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 destroy_workqueue(_kcryptd_workqueue);
1054 kmem_cache_destroy(_crypt_io_pool);
1055}
1056
1057module_init(dm_crypt_init);
1058module_exit(dm_crypt_exit);
1059
1060MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
1061MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
1062MODULE_LICENSE("GPL");