]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/md/dm-crypt.c
[PATCH] dm crypt: add key msg
[mirror_ubuntu-jammy-kernel.git] / drivers / md / dm-crypt.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 2003 Christophe Saout <christophe@saout.de>
3 * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
e48d4bbf 4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
1da177e4
LT
5 *
6 * This file is released under the GPL.
7 */
8
d1806f6a 9#include <linux/err.h>
1da177e4
LT
10#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>
378f058c 20#include <linux/scatterlist.h>
1da177e4
LT
21#include <asm/page.h>
22
23#include "dm.h"
24
72d94861 25#define DM_MSG_PREFIX "crypt"
e48d4bbf 26#define MESG_STR(x) x, sizeof(x)
1da177e4
LT
27
28/*
29 * per bio private data
30 */
31struct crypt_io {
32 struct dm_target *target;
33 struct bio *bio;
34 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 */
e48d4bbf 68enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
1da177e4
LT
69struct 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;
d1806f6a 85 struct crypto_cipher *iv_gen_private;
1da177e4
LT
86 sector_t iv_offset;
87 unsigned int iv_size;
88
d1806f6a
HX
89 char cipher[CRYPTO_MAX_ALG_NAME];
90 char chainmode[CRYPTO_MAX_ALG_NAME];
91 struct crypto_blkcipher *tfm;
e48d4bbf 92 unsigned long flags;
1da177e4
LT
93 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
1da177e4
LT
103/*
104 * Different IV generation algorithms:
105 *
3c164bd8 106 * plain: the initial vector is the 32-bit little-endian version of the sector
1da177e4
LT
107 * number, padded with zeros if neccessary.
108 *
3c164bd8
RS
109 * 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.
1da177e4
LT
112 *
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{
d1806f6a 128 struct crypto_cipher *essiv_tfm;
35058687
HX
129 struct crypto_hash *hash_tfm;
130 struct hash_desc desc;
1da177e4
LT
131 struct scatterlist sg;
132 unsigned int saltsize;
133 u8 *salt;
d1806f6a 134 int err;
1da177e4
LT
135
136 if (opts == NULL) {
72d94861 137 ti->error = "Digest algorithm missing for ESSIV mode";
1da177e4
LT
138 return -EINVAL;
139 }
140
141 /* Hash the cipher key with the given hash algorithm */
35058687
HX
142 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
143 if (IS_ERR(hash_tfm)) {
72d94861 144 ti->error = "Error initializing ESSIV hash";
35058687 145 return PTR_ERR(hash_tfm);
1da177e4
LT
146 }
147
35058687 148 saltsize = crypto_hash_digestsize(hash_tfm);
1da177e4
LT
149 salt = kmalloc(saltsize, GFP_KERNEL);
150 if (salt == NULL) {
72d94861 151 ti->error = "Error kmallocing salt storage in ESSIV";
35058687 152 crypto_free_hash(hash_tfm);
1da177e4
LT
153 return -ENOMEM;
154 }
155
378f058c 156 sg_set_buf(&sg, cc->key, cc->key_size);
35058687
HX
157 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 }
1da177e4
LT
166
167 /* Setup the essiv_tfm with the given salt */
d1806f6a
HX
168 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
169 if (IS_ERR(essiv_tfm)) {
72d94861 170 ti->error = "Error allocating crypto tfm for ESSIV";
1da177e4 171 kfree(salt);
d1806f6a 172 return PTR_ERR(essiv_tfm);
1da177e4 173 }
d1806f6a
HX
174 if (crypto_cipher_blocksize(essiv_tfm) !=
175 crypto_blkcipher_ivsize(cc->tfm)) {
72d94861 176 ti->error = "Block size of ESSIV cipher does "
1da177e4 177 "not match IV size of block cipher";
d1806f6a 178 crypto_free_cipher(essiv_tfm);
1da177e4
LT
179 kfree(salt);
180 return -EINVAL;
181 }
d1806f6a
HX
182 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
183 if (err) {
72d94861 184 ti->error = "Failed to set key for ESSIV cipher";
d1806f6a 185 crypto_free_cipher(essiv_tfm);
1da177e4 186 kfree(salt);
d1806f6a 187 return err;
1da177e4
LT
188 }
189 kfree(salt);
190
d1806f6a 191 cc->iv_gen_private = essiv_tfm;
1da177e4
LT
192 return 0;
193}
194
195static void crypt_iv_essiv_dtr(struct crypt_config *cc)
196{
d1806f6a 197 crypto_free_cipher(cc->iv_gen_private);
1da177e4
LT
198 cc->iv_gen_private = NULL;
199}
200
201static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
202{
1da177e4
LT
203 memset(iv, 0, cc->iv_size);
204 *(u64 *)iv = cpu_to_le64(sector);
d1806f6a 205 crypto_cipher_encrypt_one(cc->iv_gen_private, iv, iv);
1da177e4
LT
206 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
858119e1 220static int
1da177e4
LT
221crypt_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];
d1806f6a
HX
226 struct blkcipher_desc desc = {
227 .tfm = cc->tfm,
228 .info = iv,
229 .flags = CRYPTO_TFM_REQ_MAY_SLEEP,
230 };
1da177e4
LT
231 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)
d1806f6a 239 r = crypto_blkcipher_encrypt_iv(&desc, out, in, length);
1da177e4 240 else
d1806f6a 241 r = crypto_blkcipher_decrypt_iv(&desc, out, in, length);
1da177e4
LT
242 } else {
243 if (write)
d1806f6a 244 r = crypto_blkcipher_encrypt(&desc, out, in, length);
1da177e4 245 else
d1806f6a 246 r = crypto_blkcipher_decrypt(&desc, out, in, length);
1da177e4
LT
247 }
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{
322 struct bio *bio;
323 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
b4e3ca1a 324 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
1da177e4
LT
325 unsigned int i;
326
327 /*
bd53b714
NP
328 * Use __GFP_NOMEMALLOC to tell the VM to act less aggressively and
329 * to fail earlier. This is not necessary but increases throughput.
1da177e4
LT
330 * FIXME: Is this really intelligent?
331 */
1da177e4 332 if (base_bio)
bd53b714 333 bio = bio_clone(base_bio, GFP_NOIO|__GFP_NOMEMALLOC);
1da177e4 334 else
bd53b714
NP
335 bio = bio_alloc(GFP_NOIO|__GFP_NOMEMALLOC, nr_iovecs);
336 if (!bio)
1da177e4 337 return NULL;
1da177e4
LT
338
339 /* if the last bio was not complete, continue where that one ended */
340 bio->bi_idx = *bio_vec_idx;
341 bio->bi_vcnt = *bio_vec_idx;
342 bio->bi_size = 0;
343 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
344
345 /* bio->bi_idx pages have already been allocated */
346 size -= bio->bi_idx * PAGE_SIZE;
347
348 for(i = bio->bi_idx; i < nr_iovecs; i++) {
349 struct bio_vec *bv = bio_iovec_idx(bio, i);
350
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 */
360 if ((i - bio->bi_idx) == (MIN_BIO_PAGES - 1))
361 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
369 bio->bi_size += bv->bv_len;
370 bio->bi_vcnt++;
371 size -= bv->bv_len;
372 }
373
1da177e4
LT
374 if (!bio->bi_size) {
375 bio_put(bio);
376 return NULL;
377 }
378
379 /*
380 * Remember the last bio_vec allocated to be able
381 * to correctly continue after the splitting.
382 */
383 *bio_vec_idx = bio->bi_vcnt;
384
385 return bio;
386}
387
388static void crypt_free_buffer_pages(struct crypt_config *cc,
389 struct bio *bio, unsigned int bytes)
390{
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 */
403 i = bio->bi_vcnt - 1;
404 bv = bio_iovec_idx(bio, i);
405 end = (i << PAGE_SHIFT) + (bv->bv_offset + bv->bv_len) - bio->bi_size;
406 start = end - bytes;
407
408 start >>= PAGE_SHIFT;
409 if (!bio->bi_size)
410 end = bio->bi_vcnt;
411 else
412 end >>= PAGE_SHIFT;
413
414 for(i = start; i < end; i++) {
415 bv = bio_iovec_idx(bio, i);
416 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
439 bio_endio(io->bio, io->bio->bi_size, io->error);
440
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;
452
453static void kcryptd_do_work(void *data)
454{
455 struct crypt_io *io = (struct crypt_io *) data;
456 struct crypt_config *cc = (struct crypt_config *) io->target->private;
457 struct convert_context ctx;
458 int r;
459
460 crypt_convert_init(cc, &ctx, io->bio, io->bio,
461 io->bio->bi_sector - io->target->begin, 0);
462 r = crypt_convert(cc, &ctx);
463
464 dec_pending(io, r);
465}
466
467static void kcryptd_queue_io(struct crypt_io *io)
468{
469 INIT_WORK(&io->work, kcryptd_do_work, io);
470 queue_work(_kcryptd_workqueue, &io->work);
471}
472
473/*
474 * Decode key from its hex representation
475 */
476static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
477{
478 char buffer[3];
479 char *endp;
480 unsigned int i;
481
482 buffer[2] = '\0';
483
484 for(i = 0; i < size; i++) {
485 buffer[0] = *hex++;
486 buffer[1] = *hex++;
487
488 key[i] = (u8)simple_strtoul(buffer, &endp, 16);
489
490 if (endp != &buffer[2])
491 return -EINVAL;
492 }
493
494 if (*hex != '\0')
495 return -EINVAL;
496
497 return 0;
498}
499
500/*
501 * Encode key into its hex representation
502 */
503static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
504{
505 unsigned int i;
506
507 for(i = 0; i < size; i++) {
508 sprintf(hex, "%02x", *key);
509 hex += 2;
510 key++;
511 }
512}
513
e48d4bbf
MB
514static int crypt_set_key(struct crypt_config *cc, char *key)
515{
516 unsigned key_size = strlen(key) >> 1;
517
518 if (cc->key_size && cc->key_size != key_size)
519 return -EINVAL;
520
521 cc->key_size = key_size; /* initial settings */
522
523 if ((!key_size && strcmp(key, "-")) ||
524 (key_size && crypt_decode_key(cc->key, key, key_size) < 0))
525 return -EINVAL;
526
527 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
528
529 return 0;
530}
531
532static int crypt_wipe_key(struct crypt_config *cc)
533{
534 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
535 memset(&cc->key, 0, cc->key_size * sizeof(u8));
536 return 0;
537}
538
1da177e4
LT
539/*
540 * Construct an encryption mapping:
541 * <cipher> <key> <iv_offset> <dev_path> <start>
542 */
543static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
544{
545 struct crypt_config *cc;
d1806f6a 546 struct crypto_blkcipher *tfm;
1da177e4
LT
547 char *tmp;
548 char *cipher;
549 char *chainmode;
550 char *ivmode;
551 char *ivopts;
1da177e4 552 unsigned int key_size;
4ee218cd 553 unsigned long long tmpll;
1da177e4
LT
554
555 if (argc != 5) {
72d94861 556 ti->error = "Not enough arguments";
1da177e4
LT
557 return -EINVAL;
558 }
559
560 tmp = argv[0];
561 cipher = strsep(&tmp, "-");
562 chainmode = strsep(&tmp, "-");
563 ivopts = strsep(&tmp, "-");
564 ivmode = strsep(&ivopts, ":");
565
566 if (tmp)
72d94861 567 DMWARN("Unexpected additional cipher options");
1da177e4
LT
568
569 key_size = strlen(argv[1]) >> 1;
570
e48d4bbf 571 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1da177e4
LT
572 if (cc == NULL) {
573 ti->error =
72d94861 574 "Cannot allocate transparent encryption context";
1da177e4
LT
575 return -ENOMEM;
576 }
577
e48d4bbf 578 if (crypt_set_key(cc, argv[1])) {
72d94861 579 ti->error = "Error decoding key";
1da177e4
LT
580 goto bad1;
581 }
582
583 /* Compatiblity mode for old dm-crypt cipher strings */
584 if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
585 chainmode = "cbc";
586 ivmode = "plain";
587 }
588
d1806f6a
HX
589 if (strcmp(chainmode, "ecb") && !ivmode) {
590 ti->error = "This chaining mode requires an IV mechanism";
1da177e4
LT
591 goto bad1;
592 }
593
d1806f6a
HX
594 if (snprintf(cc->cipher, CRYPTO_MAX_ALG_NAME, "%s(%s)", chainmode,
595 cipher) >= CRYPTO_MAX_ALG_NAME) {
596 ti->error = "Chain mode + cipher name is too long";
1da177e4
LT
597 goto bad1;
598 }
599
d1806f6a
HX
600 tfm = crypto_alloc_blkcipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
601 if (IS_ERR(tfm)) {
72d94861 602 ti->error = "Error allocating crypto tfm";
1da177e4
LT
603 goto bad1;
604 }
1da177e4 605
d1806f6a
HX
606 strcpy(cc->cipher, cipher);
607 strcpy(cc->chainmode, chainmode);
1da177e4
LT
608 cc->tfm = tfm;
609
610 /*
611 * Choose ivmode. Valid modes: "plain", "essiv:<esshash>".
612 * See comments at iv code
613 */
614
615 if (ivmode == NULL)
616 cc->iv_gen_ops = NULL;
617 else if (strcmp(ivmode, "plain") == 0)
618 cc->iv_gen_ops = &crypt_iv_plain_ops;
619 else if (strcmp(ivmode, "essiv") == 0)
620 cc->iv_gen_ops = &crypt_iv_essiv_ops;
621 else {
72d94861 622 ti->error = "Invalid IV mode";
1da177e4
LT
623 goto bad2;
624 }
625
626 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
627 cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
628 goto bad2;
629
d1806f6a
HX
630 cc->iv_size = crypto_blkcipher_ivsize(tfm);
631 if (cc->iv_size)
1da177e4 632 /* at least a 64 bit sector number should fit in our buffer */
d1806f6a 633 cc->iv_size = max(cc->iv_size,
1da177e4
LT
634 (unsigned int)(sizeof(u64) / sizeof(u8)));
635 else {
1da177e4 636 if (cc->iv_gen_ops) {
72d94861 637 DMWARN("Selected cipher does not support IVs");
1da177e4
LT
638 if (cc->iv_gen_ops->dtr)
639 cc->iv_gen_ops->dtr(cc);
640 cc->iv_gen_ops = NULL;
641 }
642 }
643
93d2341c 644 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
1da177e4 645 if (!cc->io_pool) {
72d94861 646 ti->error = "Cannot allocate crypt io mempool";
1da177e4
LT
647 goto bad3;
648 }
649
a19b27ce 650 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
1da177e4 651 if (!cc->page_pool) {
72d94861 652 ti->error = "Cannot allocate page mempool";
1da177e4
LT
653 goto bad4;
654 }
655
d1806f6a 656 if (crypto_blkcipher_setkey(tfm, cc->key, key_size) < 0) {
72d94861 657 ti->error = "Error setting key";
1da177e4
LT
658 goto bad5;
659 }
660
4ee218cd 661 if (sscanf(argv[2], "%llu", &tmpll) != 1) {
72d94861 662 ti->error = "Invalid iv_offset sector";
1da177e4
LT
663 goto bad5;
664 }
4ee218cd 665 cc->iv_offset = tmpll;
1da177e4 666
4ee218cd 667 if (sscanf(argv[4], "%llu", &tmpll) != 1) {
72d94861 668 ti->error = "Invalid device sector";
1da177e4
LT
669 goto bad5;
670 }
4ee218cd 671 cc->start = tmpll;
1da177e4
LT
672
673 if (dm_get_device(ti, argv[3], cc->start, ti->len,
674 dm_table_get_mode(ti->table), &cc->dev)) {
72d94861 675 ti->error = "Device lookup failed";
1da177e4
LT
676 goto bad5;
677 }
678
679 if (ivmode && cc->iv_gen_ops) {
680 if (ivopts)
681 *(ivopts - 1) = ':';
682 cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
683 if (!cc->iv_mode) {
72d94861 684 ti->error = "Error kmallocing iv_mode string";
1da177e4
LT
685 goto bad5;
686 }
687 strcpy(cc->iv_mode, ivmode);
688 } else
689 cc->iv_mode = NULL;
690
691 ti->private = cc;
692 return 0;
693
694bad5:
695 mempool_destroy(cc->page_pool);
696bad4:
697 mempool_destroy(cc->io_pool);
698bad3:
699 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
700 cc->iv_gen_ops->dtr(cc);
701bad2:
d1806f6a 702 crypto_free_blkcipher(tfm);
1da177e4 703bad1:
9d3520a3
SR
704 /* Must zero key material before freeing */
705 memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
1da177e4
LT
706 kfree(cc);
707 return -EINVAL;
708}
709
710static void crypt_dtr(struct dm_target *ti)
711{
712 struct crypt_config *cc = (struct crypt_config *) ti->private;
713
714 mempool_destroy(cc->page_pool);
715 mempool_destroy(cc->io_pool);
716
990a8baf 717 kfree(cc->iv_mode);
1da177e4
LT
718 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
719 cc->iv_gen_ops->dtr(cc);
d1806f6a 720 crypto_free_blkcipher(cc->tfm);
1da177e4 721 dm_put_device(ti, cc->dev);
9d3520a3
SR
722
723 /* Must zero key material before freeing */
724 memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
1da177e4
LT
725 kfree(cc);
726}
727
728static int crypt_endio(struct bio *bio, unsigned int done, int error)
729{
730 struct crypt_io *io = (struct crypt_io *) bio->bi_private;
731 struct crypt_config *cc = (struct crypt_config *) io->target->private;
732
733 if (bio_data_dir(bio) == WRITE) {
734 /*
735 * free the processed pages, even if
736 * it's only a partially completed write
737 */
738 crypt_free_buffer_pages(cc, bio, done);
739 }
740
741 if (bio->bi_size)
742 return 1;
743
744 bio_put(bio);
745
746 /*
747 * successful reads are decrypted by the worker thread
748 */
749 if ((bio_data_dir(bio) == READ)
750 && bio_flagged(bio, BIO_UPTODATE)) {
751 kcryptd_queue_io(io);
752 return 0;
753 }
754
755 dec_pending(io, error);
756 return error;
757}
758
759static inline struct bio *
760crypt_clone(struct crypt_config *cc, struct crypt_io *io, struct bio *bio,
761 sector_t sector, unsigned int *bvec_idx,
762 struct convert_context *ctx)
763{
764 struct bio *clone;
765
766 if (bio_data_dir(bio) == WRITE) {
767 clone = crypt_alloc_buffer(cc, bio->bi_size,
768 io->first_clone, bvec_idx);
769 if (clone) {
770 ctx->bio_out = clone;
771 if (crypt_convert(cc, ctx) < 0) {
772 crypt_free_buffer_pages(cc, clone,
773 clone->bi_size);
774 bio_put(clone);
775 return NULL;
776 }
777 }
778 } else {
779 /*
780 * The block layer might modify the bvec array, so always
781 * copy the required bvecs because we need the original
782 * one in order to decrypt the whole bio data *afterwards*.
783 */
784 clone = bio_alloc(GFP_NOIO, bio_segments(bio));
785 if (clone) {
786 clone->bi_idx = 0;
787 clone->bi_vcnt = bio_segments(bio);
788 clone->bi_size = bio->bi_size;
789 memcpy(clone->bi_io_vec, bio_iovec(bio),
790 sizeof(struct bio_vec) * clone->bi_vcnt);
791 }
792 }
793
794 if (!clone)
795 return NULL;
796
797 clone->bi_private = io;
798 clone->bi_end_io = crypt_endio;
799 clone->bi_bdev = cc->dev->bdev;
800 clone->bi_sector = cc->start + sector;
801 clone->bi_rw = bio->bi_rw;
802
803 return clone;
804}
805
806static int crypt_map(struct dm_target *ti, struct bio *bio,
807 union map_info *map_context)
808{
809 struct crypt_config *cc = (struct crypt_config *) ti->private;
e48d4bbf 810 struct crypt_io *io;
1da177e4
LT
811 struct convert_context ctx;
812 struct bio *clone;
813 unsigned int remaining = bio->bi_size;
814 sector_t sector = bio->bi_sector - ti->begin;
815 unsigned int bvec_idx = 0;
816
e48d4bbf 817 io = mempool_alloc(cc->io_pool, GFP_NOIO);
1da177e4
LT
818 io->target = ti;
819 io->bio = bio;
820 io->first_clone = NULL;
821 io->error = 0;
822 atomic_set(&io->pending, 1); /* hold a reference */
823
824 if (bio_data_dir(bio) == WRITE)
825 crypt_convert_init(cc, &ctx, NULL, bio, sector, 1);
826
827 /*
828 * The allocated buffers can be smaller than the whole bio,
829 * so repeat the whole process until all the data can be handled.
830 */
831 while (remaining) {
832 clone = crypt_clone(cc, io, bio, sector, &bvec_idx, &ctx);
833 if (!clone)
834 goto cleanup;
835
836 if (!io->first_clone) {
837 /*
838 * hold a reference to the first clone, because it
839 * holds the bio_vec array and that can't be freed
840 * before all other clones are released
841 */
842 bio_get(clone);
843 io->first_clone = clone;
844 }
845 atomic_inc(&io->pending);
846
847 remaining -= clone->bi_size;
848 sector += bio_sectors(clone);
849
850 generic_make_request(clone);
851
852 /* out of memory -> run queues */
853 if (remaining)
854 blk_congestion_wait(bio_data_dir(clone), HZ/100);
855 }
856
857 /* drop reference, clones could have returned before we reach this */
858 dec_pending(io, 0);
859 return 0;
860
861cleanup:
862 if (io->first_clone) {
863 dec_pending(io, -ENOMEM);
864 return 0;
865 }
866
867 /* if no bio has been dispatched yet, we can directly return the error */
868 mempool_free(io, cc->io_pool);
869 return -ENOMEM;
870}
871
872static int crypt_status(struct dm_target *ti, status_type_t type,
873 char *result, unsigned int maxlen)
874{
875 struct crypt_config *cc = (struct crypt_config *) ti->private;
876 const char *cipher;
877 const char *chainmode = NULL;
878 unsigned int sz = 0;
879
880 switch (type) {
881 case STATUSTYPE_INFO:
882 result[0] = '\0';
883 break;
884
885 case STATUSTYPE_TABLE:
d1806f6a 886 cipher = crypto_blkcipher_name(cc->tfm);
1da177e4 887
d1806f6a 888 chainmode = cc->chainmode;
1da177e4
LT
889
890 if (cc->iv_mode)
891 DMEMIT("%s-%s-%s ", cipher, chainmode, cc->iv_mode);
892 else
893 DMEMIT("%s-%s ", cipher, chainmode);
894
895 if (cc->key_size > 0) {
896 if ((maxlen - sz) < ((cc->key_size << 1) + 1))
897 return -ENOMEM;
898
899 crypt_encode_key(result + sz, cc->key, cc->key_size);
900 sz += cc->key_size << 1;
901 } else {
902 if (sz >= maxlen)
903 return -ENOMEM;
904 result[sz++] = '-';
905 }
906
4ee218cd
AM
907 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
908 cc->dev->name, (unsigned long long)cc->start);
1da177e4
LT
909 break;
910 }
911 return 0;
912}
913
e48d4bbf
MB
914static void crypt_postsuspend(struct dm_target *ti)
915{
916 struct crypt_config *cc = ti->private;
917
918 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
919}
920
921static int crypt_preresume(struct dm_target *ti)
922{
923 struct crypt_config *cc = ti->private;
924
925 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
926 DMERR("aborting resume - crypt key is not set.");
927 return -EAGAIN;
928 }
929
930 return 0;
931}
932
933static void crypt_resume(struct dm_target *ti)
934{
935 struct crypt_config *cc = ti->private;
936
937 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
938}
939
940/* Message interface
941 * key set <key>
942 * key wipe
943 */
944static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
945{
946 struct crypt_config *cc = ti->private;
947
948 if (argc < 2)
949 goto error;
950
951 if (!strnicmp(argv[0], MESG_STR("key"))) {
952 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
953 DMWARN("not suspended during key manipulation.");
954 return -EINVAL;
955 }
956 if (argc == 3 && !strnicmp(argv[1], MESG_STR("set")))
957 return crypt_set_key(cc, argv[2]);
958 if (argc == 2 && !strnicmp(argv[1], MESG_STR("wipe")))
959 return crypt_wipe_key(cc);
960 }
961
962error:
963 DMWARN("unrecognised message received.");
964 return -EINVAL;
965}
966
1da177e4
LT
967static struct target_type crypt_target = {
968 .name = "crypt",
e48d4bbf 969 .version= {1, 2, 0},
1da177e4
LT
970 .module = THIS_MODULE,
971 .ctr = crypt_ctr,
972 .dtr = crypt_dtr,
973 .map = crypt_map,
974 .status = crypt_status,
e48d4bbf
MB
975 .postsuspend = crypt_postsuspend,
976 .preresume = crypt_preresume,
977 .resume = crypt_resume,
978 .message = crypt_message,
1da177e4
LT
979};
980
981static int __init dm_crypt_init(void)
982{
983 int r;
984
985 _crypt_io_pool = kmem_cache_create("dm-crypt_io",
986 sizeof(struct crypt_io),
987 0, 0, NULL, NULL);
988 if (!_crypt_io_pool)
989 return -ENOMEM;
990
991 _kcryptd_workqueue = create_workqueue("kcryptd");
992 if (!_kcryptd_workqueue) {
993 r = -ENOMEM;
72d94861 994 DMERR("couldn't create kcryptd");
1da177e4
LT
995 goto bad1;
996 }
997
998 r = dm_register_target(&crypt_target);
999 if (r < 0) {
72d94861 1000 DMERR("register failed %d", r);
1da177e4
LT
1001 goto bad2;
1002 }
1003
1004 return 0;
1005
1006bad2:
1007 destroy_workqueue(_kcryptd_workqueue);
1008bad1:
1009 kmem_cache_destroy(_crypt_io_pool);
1010 return r;
1011}
1012
1013static void __exit dm_crypt_exit(void)
1014{
1015 int r = dm_unregister_target(&crypt_target);
1016
1017 if (r < 0)
72d94861 1018 DMERR("unregister failed %d", r);
1da177e4
LT
1019
1020 destroy_workqueue(_kcryptd_workqueue);
1021 kmem_cache_destroy(_crypt_io_pool);
1022}
1023
1024module_init(dm_crypt_init);
1025module_exit(dm_crypt_exit);
1026
1027MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
1028MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
1029MODULE_LICENSE("GPL");