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.
6 * This file is released under the GPL.
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>
20 #include <linux/scatterlist.h>
25 #define DM_MSG_PREFIX "crypt"
26 #define MESG_STR(x) x, sizeof(x)
29 * per bio private data
32 struct dm_target
*target
;
34 struct bio
*first_clone
;
35 struct work_struct work
;
42 * context holding the current state of a multi-part conversion
44 struct convert_context
{
47 unsigned int offset_in
;
48 unsigned int offset_out
;
57 struct crypt_iv_operations
{
58 int (*ctr
)(struct crypt_config
*cc
, struct dm_target
*ti
,
60 void (*dtr
)(struct crypt_config
*cc
);
61 const char *(*status
)(struct crypt_config
*cc
);
62 int (*generator
)(struct crypt_config
*cc
, u8
*iv
, sector_t sector
);
66 * Crypt: maps a linear range of a block device
67 * and encrypts / decrypts at the same time.
69 enum flags
{ DM_CRYPT_SUSPENDED
, DM_CRYPT_KEY_VALID
};
75 * pool for per bio private data and
76 * for encryption buffer pages
84 struct crypt_iv_operations
*iv_gen_ops
;
86 struct crypto_cipher
*iv_gen_private
;
90 char cipher
[CRYPTO_MAX_ALG_NAME
];
91 char chainmode
[CRYPTO_MAX_ALG_NAME
];
92 struct crypto_blkcipher
*tfm
;
94 unsigned int key_size
;
99 #define MIN_POOL_PAGES 32
100 #define MIN_BIO_PAGES 8
102 static kmem_cache_t
*_crypt_io_pool
;
105 * Different IV generation algorithms:
107 * plain: the initial vector is the 32-bit little-endian version of the sector
108 * number, padded with zeros if neccessary.
110 * essiv: "encrypted sector|salt initial vector", the sector number is
111 * encrypted with the bulk cipher using a salt as key. The salt
112 * should be derived from the bulk cipher's key via hashing.
114 * plumb: unimplemented, see:
115 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
118 static int crypt_iv_plain_gen(struct crypt_config
*cc
, u8
*iv
, sector_t sector
)
120 memset(iv
, 0, cc
->iv_size
);
121 *(u32
*)iv
= cpu_to_le32(sector
& 0xffffffff);
126 static int crypt_iv_essiv_ctr(struct crypt_config
*cc
, struct dm_target
*ti
,
129 struct crypto_cipher
*essiv_tfm
;
130 struct crypto_hash
*hash_tfm
;
131 struct hash_desc desc
;
132 struct scatterlist sg
;
133 unsigned int saltsize
;
138 ti
->error
= "Digest algorithm missing for ESSIV mode";
142 /* Hash the cipher key with the given hash algorithm */
143 hash_tfm
= crypto_alloc_hash(opts
, 0, CRYPTO_ALG_ASYNC
);
144 if (IS_ERR(hash_tfm
)) {
145 ti
->error
= "Error initializing ESSIV hash";
146 return PTR_ERR(hash_tfm
);
149 saltsize
= crypto_hash_digestsize(hash_tfm
);
150 salt
= kmalloc(saltsize
, GFP_KERNEL
);
152 ti
->error
= "Error kmallocing salt storage in ESSIV";
153 crypto_free_hash(hash_tfm
);
157 sg_set_buf(&sg
, cc
->key
, cc
->key_size
);
159 desc
.flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
160 err
= crypto_hash_digest(&desc
, &sg
, cc
->key_size
, salt
);
161 crypto_free_hash(hash_tfm
);
164 ti
->error
= "Error calculating hash in ESSIV";
168 /* Setup the essiv_tfm with the given salt */
169 essiv_tfm
= crypto_alloc_cipher(cc
->cipher
, 0, CRYPTO_ALG_ASYNC
);
170 if (IS_ERR(essiv_tfm
)) {
171 ti
->error
= "Error allocating crypto tfm for ESSIV";
173 return PTR_ERR(essiv_tfm
);
175 if (crypto_cipher_blocksize(essiv_tfm
) !=
176 crypto_blkcipher_ivsize(cc
->tfm
)) {
177 ti
->error
= "Block size of ESSIV cipher does "
178 "not match IV size of block cipher";
179 crypto_free_cipher(essiv_tfm
);
183 err
= crypto_cipher_setkey(essiv_tfm
, salt
, saltsize
);
185 ti
->error
= "Failed to set key for ESSIV cipher";
186 crypto_free_cipher(essiv_tfm
);
192 cc
->iv_gen_private
= essiv_tfm
;
196 static void crypt_iv_essiv_dtr(struct crypt_config
*cc
)
198 crypto_free_cipher(cc
->iv_gen_private
);
199 cc
->iv_gen_private
= NULL
;
202 static int crypt_iv_essiv_gen(struct crypt_config
*cc
, u8
*iv
, sector_t sector
)
204 memset(iv
, 0, cc
->iv_size
);
205 *(u64
*)iv
= cpu_to_le64(sector
);
206 crypto_cipher_encrypt_one(cc
->iv_gen_private
, iv
, iv
);
210 static struct crypt_iv_operations crypt_iv_plain_ops
= {
211 .generator
= crypt_iv_plain_gen
214 static struct crypt_iv_operations crypt_iv_essiv_ops
= {
215 .ctr
= crypt_iv_essiv_ctr
,
216 .dtr
= crypt_iv_essiv_dtr
,
217 .generator
= crypt_iv_essiv_gen
222 crypt_convert_scatterlist(struct crypt_config
*cc
, struct scatterlist
*out
,
223 struct scatterlist
*in
, unsigned int length
,
224 int write
, sector_t sector
)
227 struct blkcipher_desc desc
= {
230 .flags
= CRYPTO_TFM_REQ_MAY_SLEEP
,
234 if (cc
->iv_gen_ops
) {
235 r
= cc
->iv_gen_ops
->generator(cc
, iv
, sector
);
240 r
= crypto_blkcipher_encrypt_iv(&desc
, out
, in
, length
);
242 r
= crypto_blkcipher_decrypt_iv(&desc
, out
, in
, length
);
245 r
= crypto_blkcipher_encrypt(&desc
, out
, in
, length
);
247 r
= crypto_blkcipher_decrypt(&desc
, out
, in
, length
);
254 crypt_convert_init(struct crypt_config
*cc
, struct convert_context
*ctx
,
255 struct bio
*bio_out
, struct bio
*bio_in
,
256 sector_t sector
, int write
)
258 ctx
->bio_in
= bio_in
;
259 ctx
->bio_out
= bio_out
;
262 ctx
->idx_in
= bio_in
? bio_in
->bi_idx
: 0;
263 ctx
->idx_out
= bio_out
? bio_out
->bi_idx
: 0;
264 ctx
->sector
= sector
+ cc
->iv_offset
;
269 * Encrypt / decrypt data from one bio to another one (can be the same one)
271 static int crypt_convert(struct crypt_config
*cc
,
272 struct convert_context
*ctx
)
276 while(ctx
->idx_in
< ctx
->bio_in
->bi_vcnt
&&
277 ctx
->idx_out
< ctx
->bio_out
->bi_vcnt
) {
278 struct bio_vec
*bv_in
= bio_iovec_idx(ctx
->bio_in
, ctx
->idx_in
);
279 struct bio_vec
*bv_out
= bio_iovec_idx(ctx
->bio_out
, ctx
->idx_out
);
280 struct scatterlist sg_in
= {
281 .page
= bv_in
->bv_page
,
282 .offset
= bv_in
->bv_offset
+ ctx
->offset_in
,
283 .length
= 1 << SECTOR_SHIFT
285 struct scatterlist sg_out
= {
286 .page
= bv_out
->bv_page
,
287 .offset
= bv_out
->bv_offset
+ ctx
->offset_out
,
288 .length
= 1 << SECTOR_SHIFT
291 ctx
->offset_in
+= sg_in
.length
;
292 if (ctx
->offset_in
>= bv_in
->bv_len
) {
297 ctx
->offset_out
+= sg_out
.length
;
298 if (ctx
->offset_out
>= bv_out
->bv_len
) {
303 r
= crypt_convert_scatterlist(cc
, &sg_out
, &sg_in
, sg_in
.length
,
304 ctx
->write
, ctx
->sector
);
315 * Generate a new unfragmented bio with the given size
316 * This should never violate the device limitations
317 * May return a smaller bio when running out of pages
320 crypt_alloc_buffer(struct crypt_config
*cc
, unsigned int size
,
321 struct bio
*base_bio
, unsigned int *bio_vec_idx
)
324 unsigned int nr_iovecs
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
325 gfp_t gfp_mask
= GFP_NOIO
| __GFP_HIGHMEM
;
329 * Use __GFP_NOMEMALLOC to tell the VM to act less aggressively and
330 * to fail earlier. This is not necessary but increases throughput.
331 * FIXME: Is this really intelligent?
334 clone
= bio_clone(base_bio
, GFP_NOIO
|__GFP_NOMEMALLOC
);
336 clone
= bio_alloc(GFP_NOIO
|__GFP_NOMEMALLOC
, nr_iovecs
);
340 /* if the last bio was not complete, continue where that one ended */
341 clone
->bi_idx
= *bio_vec_idx
;
342 clone
->bi_vcnt
= *bio_vec_idx
;
344 clone
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
346 /* clone->bi_idx pages have already been allocated */
347 size
-= clone
->bi_idx
* PAGE_SIZE
;
349 for (i
= clone
->bi_idx
; i
< nr_iovecs
; i
++) {
350 struct bio_vec
*bv
= bio_iovec_idx(clone
, i
);
352 bv
->bv_page
= mempool_alloc(cc
->page_pool
, gfp_mask
);
357 * if additional pages cannot be allocated without waiting,
358 * return a partially allocated bio, the caller will then try
359 * to allocate additional bios while submitting this partial bio
361 if ((i
- clone
->bi_idx
) == (MIN_BIO_PAGES
- 1))
362 gfp_mask
= (gfp_mask
| __GFP_NOWARN
) & ~__GFP_WAIT
;
365 if (size
> PAGE_SIZE
)
366 bv
->bv_len
= PAGE_SIZE
;
370 clone
->bi_size
+= bv
->bv_len
;
375 if (!clone
->bi_size
) {
381 * Remember the last bio_vec allocated to be able
382 * to correctly continue after the splitting.
384 *bio_vec_idx
= clone
->bi_vcnt
;
389 static void crypt_free_buffer_pages(struct crypt_config
*cc
,
390 struct bio
*clone
, unsigned int bytes
)
392 unsigned int i
, start
, end
;
396 * This is ugly, but Jens Axboe thinks that using bi_idx in the
397 * endio function is too dangerous at the moment, so I calculate the
398 * correct position using bi_vcnt and bi_size.
399 * The bv_offset and bv_len fields might already be modified but we
400 * know that we always allocated whole pages.
401 * A fix to the bi_idx issue in the kernel is in the works, so
402 * we will hopefully be able to revert to the cleaner solution soon.
404 i
= clone
->bi_vcnt
- 1;
405 bv
= bio_iovec_idx(clone
, i
);
406 end
= (i
<< PAGE_SHIFT
) + (bv
->bv_offset
+ bv
->bv_len
) - clone
->bi_size
;
409 start
>>= PAGE_SHIFT
;
411 end
= clone
->bi_vcnt
;
415 for (i
= start
; i
< end
; i
++) {
416 bv
= bio_iovec_idx(clone
, i
);
417 BUG_ON(!bv
->bv_page
);
418 mempool_free(bv
->bv_page
, cc
->page_pool
);
424 * One of the bios was finished. Check for completion of
425 * the whole request and correctly clean up the buffer.
427 static void dec_pending(struct crypt_io
*io
, int error
)
429 struct crypt_config
*cc
= (struct crypt_config
*) io
->target
->private;
434 if (!atomic_dec_and_test(&io
->pending
))
438 bio_put(io
->first_clone
);
440 bio_endio(io
->base_bio
, io
->base_bio
->bi_size
, io
->error
);
442 mempool_free(io
, cc
->io_pool
);
448 * Needed because it would be very unwise to do decryption in an
451 static struct workqueue_struct
*_kcryptd_workqueue
;
452 static void kcryptd_do_work(void *data
);
454 static void kcryptd_queue_io(struct crypt_io
*io
)
456 INIT_WORK(&io
->work
, kcryptd_do_work
, io
);
457 queue_work(_kcryptd_workqueue
, &io
->work
);
460 static int crypt_endio(struct bio
*clone
, unsigned int done
, int error
)
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
;
467 * free the processed pages, even if
468 * it's only a partially completed write
471 crypt_free_buffer_pages(cc
, clone
, done
);
473 /* keep going - not finished yet */
474 if (unlikely(clone
->bi_size
))
480 if (unlikely(!bio_flagged(clone
, BIO_UPTODATE
))) {
486 io
->post_process
= 1;
487 kcryptd_queue_io(io
);
492 dec_pending(io
, error
);
496 static void clone_init(struct crypt_io
*io
, struct bio
*clone
)
498 struct crypt_config
*cc
= io
->target
->private;
500 clone
->bi_private
= io
;
501 clone
->bi_end_io
= crypt_endio
;
502 clone
->bi_bdev
= cc
->dev
->bdev
;
503 clone
->bi_rw
= io
->base_bio
->bi_rw
;
506 static void process_read(struct crypt_io
*io
)
508 struct crypt_config
*cc
= io
->target
->private;
509 struct bio
*base_bio
= io
->base_bio
;
511 sector_t sector
= base_bio
->bi_sector
- io
->target
->begin
;
513 atomic_inc(&io
->pending
);
516 * The block layer might modify the bvec array, so always
517 * copy the required bvecs because we need the original
518 * one in order to decrypt the whole bio data *afterwards*.
520 clone
= bio_alloc(GFP_NOIO
, bio_segments(base_bio
));
521 if (unlikely(!clone
)) {
522 dec_pending(io
, -ENOMEM
);
526 clone_init(io
, clone
);
528 clone
->bi_vcnt
= bio_segments(base_bio
);
529 clone
->bi_size
= base_bio
->bi_size
;
530 clone
->bi_sector
= cc
->start
+ sector
;
531 memcpy(clone
->bi_io_vec
, bio_iovec(base_bio
),
532 sizeof(struct bio_vec
) * clone
->bi_vcnt
);
534 generic_make_request(clone
);
537 static void process_write(struct crypt_io
*io
)
539 struct crypt_config
*cc
= io
->target
->private;
540 struct bio
*base_bio
= io
->base_bio
;
542 struct convert_context ctx
;
543 unsigned remaining
= base_bio
->bi_size
;
544 sector_t sector
= base_bio
->bi_sector
- io
->target
->begin
;
545 unsigned bvec_idx
= 0;
547 atomic_inc(&io
->pending
);
549 crypt_convert_init(cc
, &ctx
, NULL
, base_bio
, sector
, 1);
552 * The allocated buffers can be smaller than the whole bio,
553 * so repeat the whole process until all the data can be handled.
556 clone
= crypt_alloc_buffer(cc
, base_bio
->bi_size
,
557 io
->first_clone
, &bvec_idx
);
558 if (unlikely(!clone
)) {
559 dec_pending(io
, -ENOMEM
);
565 if (unlikely(crypt_convert(cc
, &ctx
) < 0)) {
566 crypt_free_buffer_pages(cc
, clone
, clone
->bi_size
);
568 dec_pending(io
, -EIO
);
572 clone_init(io
, clone
);
573 clone
->bi_sector
= cc
->start
+ sector
;
575 if (!io
->first_clone
) {
577 * hold a reference to the first clone, because it
578 * holds the bio_vec array and that can't be freed
579 * before all other clones are released
582 io
->first_clone
= clone
;
585 remaining
-= clone
->bi_size
;
586 sector
+= bio_sectors(clone
);
588 /* prevent bio_put of first_clone */
590 atomic_inc(&io
->pending
);
592 generic_make_request(clone
);
594 /* out of memory -> run queues */
596 blk_congestion_wait(bio_data_dir(clone
), HZ
/100);
601 static void process_read_endio(struct crypt_io
*io
)
603 struct crypt_config
*cc
= io
->target
->private;
604 struct convert_context ctx
;
606 crypt_convert_init(cc
, &ctx
, io
->base_bio
, io
->base_bio
,
607 io
->base_bio
->bi_sector
- io
->target
->begin
, 0);
609 dec_pending(io
, crypt_convert(cc
, &ctx
));
612 static void kcryptd_do_work(void *data
)
614 struct crypt_io
*io
= data
;
616 if (io
->post_process
)
617 process_read_endio(io
);
618 else if (bio_data_dir(io
->base_bio
) == READ
)
625 * Decode key from its hex representation
627 static int crypt_decode_key(u8
*key
, char *hex
, unsigned int size
)
635 for (i
= 0; i
< size
; i
++) {
639 key
[i
] = (u8
)simple_strtoul(buffer
, &endp
, 16);
641 if (endp
!= &buffer
[2])
652 * Encode key into its hex representation
654 static void crypt_encode_key(char *hex
, u8
*key
, unsigned int size
)
658 for (i
= 0; i
< size
; i
++) {
659 sprintf(hex
, "%02x", *key
);
665 static int crypt_set_key(struct crypt_config
*cc
, char *key
)
667 unsigned key_size
= strlen(key
) >> 1;
669 if (cc
->key_size
&& cc
->key_size
!= key_size
)
672 cc
->key_size
= key_size
; /* initial settings */
674 if ((!key_size
&& strcmp(key
, "-")) ||
675 (key_size
&& crypt_decode_key(cc
->key
, key
, key_size
) < 0))
678 set_bit(DM_CRYPT_KEY_VALID
, &cc
->flags
);
683 static int crypt_wipe_key(struct crypt_config
*cc
)
685 clear_bit(DM_CRYPT_KEY_VALID
, &cc
->flags
);
686 memset(&cc
->key
, 0, cc
->key_size
* sizeof(u8
));
691 * Construct an encryption mapping:
692 * <cipher> <key> <iv_offset> <dev_path> <start>
694 static int crypt_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
696 struct crypt_config
*cc
;
697 struct crypto_blkcipher
*tfm
;
703 unsigned int key_size
;
704 unsigned long long tmpll
;
707 ti
->error
= "Not enough arguments";
712 cipher
= strsep(&tmp
, "-");
713 chainmode
= strsep(&tmp
, "-");
714 ivopts
= strsep(&tmp
, "-");
715 ivmode
= strsep(&ivopts
, ":");
718 DMWARN("Unexpected additional cipher options");
720 key_size
= strlen(argv
[1]) >> 1;
722 cc
= kzalloc(sizeof(*cc
) + key_size
* sizeof(u8
), GFP_KERNEL
);
725 "Cannot allocate transparent encryption context";
729 if (crypt_set_key(cc
, argv
[1])) {
730 ti
->error
= "Error decoding key";
734 /* Compatiblity mode for old dm-crypt cipher strings */
735 if (!chainmode
|| (strcmp(chainmode
, "plain") == 0 && !ivmode
)) {
740 if (strcmp(chainmode
, "ecb") && !ivmode
) {
741 ti
->error
= "This chaining mode requires an IV mechanism";
745 if (snprintf(cc
->cipher
, CRYPTO_MAX_ALG_NAME
, "%s(%s)", chainmode
,
746 cipher
) >= CRYPTO_MAX_ALG_NAME
) {
747 ti
->error
= "Chain mode + cipher name is too long";
751 tfm
= crypto_alloc_blkcipher(cc
->cipher
, 0, CRYPTO_ALG_ASYNC
);
753 ti
->error
= "Error allocating crypto tfm";
757 strcpy(cc
->cipher
, cipher
);
758 strcpy(cc
->chainmode
, chainmode
);
762 * Choose ivmode. Valid modes: "plain", "essiv:<esshash>".
763 * See comments at iv code
767 cc
->iv_gen_ops
= NULL
;
768 else if (strcmp(ivmode
, "plain") == 0)
769 cc
->iv_gen_ops
= &crypt_iv_plain_ops
;
770 else if (strcmp(ivmode
, "essiv") == 0)
771 cc
->iv_gen_ops
= &crypt_iv_essiv_ops
;
773 ti
->error
= "Invalid IV mode";
777 if (cc
->iv_gen_ops
&& cc
->iv_gen_ops
->ctr
&&
778 cc
->iv_gen_ops
->ctr(cc
, ti
, ivopts
) < 0)
781 cc
->iv_size
= crypto_blkcipher_ivsize(tfm
);
783 /* at least a 64 bit sector number should fit in our buffer */
784 cc
->iv_size
= max(cc
->iv_size
,
785 (unsigned int)(sizeof(u64
) / sizeof(u8
)));
787 if (cc
->iv_gen_ops
) {
788 DMWARN("Selected cipher does not support IVs");
789 if (cc
->iv_gen_ops
->dtr
)
790 cc
->iv_gen_ops
->dtr(cc
);
791 cc
->iv_gen_ops
= NULL
;
795 cc
->io_pool
= mempool_create_slab_pool(MIN_IOS
, _crypt_io_pool
);
797 ti
->error
= "Cannot allocate crypt io mempool";
801 cc
->page_pool
= mempool_create_page_pool(MIN_POOL_PAGES
, 0);
802 if (!cc
->page_pool
) {
803 ti
->error
= "Cannot allocate page mempool";
807 if (crypto_blkcipher_setkey(tfm
, cc
->key
, key_size
) < 0) {
808 ti
->error
= "Error setting key";
812 if (sscanf(argv
[2], "%llu", &tmpll
) != 1) {
813 ti
->error
= "Invalid iv_offset sector";
816 cc
->iv_offset
= tmpll
;
818 if (sscanf(argv
[4], "%llu", &tmpll
) != 1) {
819 ti
->error
= "Invalid device sector";
824 if (dm_get_device(ti
, argv
[3], cc
->start
, ti
->len
,
825 dm_table_get_mode(ti
->table
), &cc
->dev
)) {
826 ti
->error
= "Device lookup failed";
830 if (ivmode
&& cc
->iv_gen_ops
) {
833 cc
->iv_mode
= kmalloc(strlen(ivmode
) + 1, GFP_KERNEL
);
835 ti
->error
= "Error kmallocing iv_mode string";
838 strcpy(cc
->iv_mode
, ivmode
);
846 mempool_destroy(cc
->page_pool
);
848 mempool_destroy(cc
->io_pool
);
850 if (cc
->iv_gen_ops
&& cc
->iv_gen_ops
->dtr
)
851 cc
->iv_gen_ops
->dtr(cc
);
853 crypto_free_blkcipher(tfm
);
855 /* Must zero key material before freeing */
856 memset(cc
, 0, sizeof(*cc
) + cc
->key_size
* sizeof(u8
));
861 static void crypt_dtr(struct dm_target
*ti
)
863 struct crypt_config
*cc
= (struct crypt_config
*) ti
->private;
865 mempool_destroy(cc
->page_pool
);
866 mempool_destroy(cc
->io_pool
);
869 if (cc
->iv_gen_ops
&& cc
->iv_gen_ops
->dtr
)
870 cc
->iv_gen_ops
->dtr(cc
);
871 crypto_free_blkcipher(cc
->tfm
);
872 dm_put_device(ti
, cc
->dev
);
874 /* Must zero key material before freeing */
875 memset(cc
, 0, sizeof(*cc
) + cc
->key_size
* sizeof(u8
));
879 static int crypt_map(struct dm_target
*ti
, struct bio
*bio
,
880 union map_info
*map_context
)
882 struct crypt_config
*cc
= ti
->private;
885 io
= mempool_alloc(cc
->io_pool
, GFP_NOIO
);
888 io
->first_clone
= NULL
;
889 io
->error
= io
->post_process
= 0;
890 atomic_set(&io
->pending
, 0);
891 kcryptd_queue_io(io
);
896 static int crypt_status(struct dm_target
*ti
, status_type_t type
,
897 char *result
, unsigned int maxlen
)
899 struct crypt_config
*cc
= (struct crypt_config
*) ti
->private;
901 const char *chainmode
= NULL
;
905 case STATUSTYPE_INFO
:
909 case STATUSTYPE_TABLE
:
910 cipher
= crypto_blkcipher_name(cc
->tfm
);
912 chainmode
= cc
->chainmode
;
915 DMEMIT("%s-%s-%s ", cipher
, chainmode
, cc
->iv_mode
);
917 DMEMIT("%s-%s ", cipher
, chainmode
);
919 if (cc
->key_size
> 0) {
920 if ((maxlen
- sz
) < ((cc
->key_size
<< 1) + 1))
923 crypt_encode_key(result
+ sz
, cc
->key
, cc
->key_size
);
924 sz
+= cc
->key_size
<< 1;
931 DMEMIT(" %llu %s %llu", (unsigned long long)cc
->iv_offset
,
932 cc
->dev
->name
, (unsigned long long)cc
->start
);
938 static void crypt_postsuspend(struct dm_target
*ti
)
940 struct crypt_config
*cc
= ti
->private;
942 set_bit(DM_CRYPT_SUSPENDED
, &cc
->flags
);
945 static int crypt_preresume(struct dm_target
*ti
)
947 struct crypt_config
*cc
= ti
->private;
949 if (!test_bit(DM_CRYPT_KEY_VALID
, &cc
->flags
)) {
950 DMERR("aborting resume - crypt key is not set.");
957 static void crypt_resume(struct dm_target
*ti
)
959 struct crypt_config
*cc
= ti
->private;
961 clear_bit(DM_CRYPT_SUSPENDED
, &cc
->flags
);
968 static int crypt_message(struct dm_target
*ti
, unsigned argc
, char **argv
)
970 struct crypt_config
*cc
= ti
->private;
975 if (!strnicmp(argv
[0], MESG_STR("key"))) {
976 if (!test_bit(DM_CRYPT_SUSPENDED
, &cc
->flags
)) {
977 DMWARN("not suspended during key manipulation.");
980 if (argc
== 3 && !strnicmp(argv
[1], MESG_STR("set")))
981 return crypt_set_key(cc
, argv
[2]);
982 if (argc
== 2 && !strnicmp(argv
[1], MESG_STR("wipe")))
983 return crypt_wipe_key(cc
);
987 DMWARN("unrecognised message received.");
991 static struct target_type crypt_target
= {
994 .module
= THIS_MODULE
,
998 .status
= crypt_status
,
999 .postsuspend
= crypt_postsuspend
,
1000 .preresume
= crypt_preresume
,
1001 .resume
= crypt_resume
,
1002 .message
= crypt_message
,
1005 static int __init
dm_crypt_init(void)
1009 _crypt_io_pool
= kmem_cache_create("dm-crypt_io",
1010 sizeof(struct crypt_io
),
1012 if (!_crypt_io_pool
)
1015 _kcryptd_workqueue
= create_workqueue("kcryptd");
1016 if (!_kcryptd_workqueue
) {
1018 DMERR("couldn't create kcryptd");
1022 r
= dm_register_target(&crypt_target
);
1024 DMERR("register failed %d", r
);
1031 destroy_workqueue(_kcryptd_workqueue
);
1033 kmem_cache_destroy(_crypt_io_pool
);
1037 static void __exit
dm_crypt_exit(void)
1039 int r
= dm_unregister_target(&crypt_target
);
1042 DMERR("unregister failed %d", r
);
1044 destroy_workqueue(_kcryptd_workqueue
);
1045 kmem_cache_destroy(_crypt_io_pool
);
1048 module_init(dm_crypt_init
);
1049 module_exit(dm_crypt_exit
);
1051 MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
1052 MODULE_DESCRIPTION(DM_NAME
" target for transparent encryption / decryption");
1053 MODULE_LICENSE("GPL");