2 * Copyright (C) 2003 Christophe Saout <christophe@saout.de>
3 * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
5 * This file is released under the GPL.
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
11 #include <linux/bio.h>
12 #include <linux/blkdev.h>
13 #include <linux/mempool.h>
14 #include <linux/slab.h>
15 #include <linux/crypto.h>
16 #include <linux/workqueue.h>
17 #include <asm/atomic.h>
18 #include <asm/scatterlist.h>
26 * per bio private data
29 struct dm_target *target;
31 struct bio *first_clone;
32 struct work_struct work;
38 * context holding the current state of a multi-part conversion
40 struct convert_context {
43 unsigned int offset_in;
44 unsigned int offset_out;
53 struct crypt_iv_operations {
54 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
56 void (*dtr)(struct crypt_config *cc);
57 const char *(*status)(struct crypt_config *cc);
58 int (*generator)(struct crypt_config *cc, u8 *iv, sector_t sector);
62 * Crypt: maps a linear range of a block device
63 * and encrypts / decrypts at the same time.
70 * pool for per bio private data and
71 * for encryption buffer pages
79 struct crypt_iv_operations *iv_gen_ops;
85 struct crypto_tfm *tfm;
86 unsigned int key_size;
91 #define MIN_POOL_PAGES 32
92 #define MIN_BIO_PAGES 8
94 static kmem_cache_t *_crypt_io_pool;
97 * Mempool alloc and free functions for the page
99 static void *mempool_alloc_page(unsigned int __nocast gfp_mask, void *data)
101 return alloc_page(gfp_mask);
104 static void mempool_free_page(void *page, void *data)
111 * Different IV generation algorithms:
113 * plain: the initial vector is the 32-bit low-endian version of the sector
114 * number, padded with zeros if neccessary.
116 * ess_iv: "encrypted sector|salt initial vector", the sector number is
117 * encrypted with the bulk cipher using a salt as key. The salt
118 * should be derived from the bulk cipher's key via hashing.
120 * plumb: unimplemented, see:
121 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
124 static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
126 memset(iv, 0, cc->iv_size);
127 *(u32 *)iv = cpu_to_le32(sector & 0xffffffff);
132 static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
135 struct crypto_tfm *essiv_tfm;
136 struct crypto_tfm *hash_tfm;
137 struct scatterlist sg;
138 unsigned int saltsize;
142 ti->error = PFX "Digest algorithm missing for ESSIV mode";
146 /* Hash the cipher key with the given hash algorithm */
147 hash_tfm = crypto_alloc_tfm(opts, 0);
148 if (hash_tfm == NULL) {
149 ti->error = PFX "Error initializing ESSIV hash";
153 if (crypto_tfm_alg_type(hash_tfm) != CRYPTO_ALG_TYPE_DIGEST) {
154 ti->error = PFX "Expected digest algorithm for ESSIV hash";
155 crypto_free_tfm(hash_tfm);
159 saltsize = crypto_tfm_alg_digestsize(hash_tfm);
160 salt = kmalloc(saltsize, GFP_KERNEL);
162 ti->error = PFX "Error kmallocing salt storage in ESSIV";
163 crypto_free_tfm(hash_tfm);
167 sg.page = virt_to_page(cc->key);
168 sg.offset = offset_in_page(cc->key);
169 sg.length = cc->key_size;
170 crypto_digest_digest(hash_tfm, &sg, 1, salt);
171 crypto_free_tfm(hash_tfm);
173 /* Setup the essiv_tfm with the given salt */
174 essiv_tfm = crypto_alloc_tfm(crypto_tfm_alg_name(cc->tfm),
175 CRYPTO_TFM_MODE_ECB);
176 if (essiv_tfm == NULL) {
177 ti->error = PFX "Error allocating crypto tfm for ESSIV";
181 if (crypto_tfm_alg_blocksize(essiv_tfm)
182 != crypto_tfm_alg_ivsize(cc->tfm)) {
183 ti->error = PFX "Block size of ESSIV cipher does "
184 "not match IV size of block cipher";
185 crypto_free_tfm(essiv_tfm);
189 if (crypto_cipher_setkey(essiv_tfm, salt, saltsize) < 0) {
190 ti->error = PFX "Failed to set key for ESSIV cipher";
191 crypto_free_tfm(essiv_tfm);
197 cc->iv_gen_private = (void *)essiv_tfm;
201 static void crypt_iv_essiv_dtr(struct crypt_config *cc)
203 crypto_free_tfm((struct crypto_tfm *)cc->iv_gen_private);
204 cc->iv_gen_private = NULL;
207 static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
209 struct scatterlist sg = { NULL, };
211 memset(iv, 0, cc->iv_size);
212 *(u64 *)iv = cpu_to_le64(sector);
214 sg.page = virt_to_page(iv);
215 sg.offset = offset_in_page(iv);
216 sg.length = cc->iv_size;
217 crypto_cipher_encrypt((struct crypto_tfm *)cc->iv_gen_private,
218 &sg, &sg, cc->iv_size);
223 static struct crypt_iv_operations crypt_iv_plain_ops = {
224 .generator = crypt_iv_plain_gen
227 static struct crypt_iv_operations crypt_iv_essiv_ops = {
228 .ctr = crypt_iv_essiv_ctr,
229 .dtr = crypt_iv_essiv_dtr,
230 .generator = crypt_iv_essiv_gen
235 crypt_convert_scatterlist(struct crypt_config *cc, struct scatterlist *out,
236 struct scatterlist *in, unsigned int length,
237 int write, sector_t sector)
242 if (cc->iv_gen_ops) {
243 r = cc->iv_gen_ops->generator(cc, iv, sector);
248 r = crypto_cipher_encrypt_iv(cc->tfm, out, in, length, iv);
250 r = crypto_cipher_decrypt_iv(cc->tfm, out, in, length, iv);
253 r = crypto_cipher_encrypt(cc->tfm, out, in, length);
255 r = crypto_cipher_decrypt(cc->tfm, out, in, length);
262 crypt_convert_init(struct crypt_config *cc, struct convert_context *ctx,
263 struct bio *bio_out, struct bio *bio_in,
264 sector_t sector, int write)
266 ctx->bio_in = bio_in;
267 ctx->bio_out = bio_out;
270 ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
271 ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
272 ctx->sector = sector + cc->iv_offset;
277 * Encrypt / decrypt data from one bio to another one (can be the same one)
279 static int crypt_convert(struct crypt_config *cc,
280 struct convert_context *ctx)
284 while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
285 ctx->idx_out < ctx->bio_out->bi_vcnt) {
286 struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
287 struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
288 struct scatterlist sg_in = {
289 .page = bv_in->bv_page,
290 .offset = bv_in->bv_offset + ctx->offset_in,
291 .length = 1 << SECTOR_SHIFT
293 struct scatterlist sg_out = {
294 .page = bv_out->bv_page,
295 .offset = bv_out->bv_offset + ctx->offset_out,
296 .length = 1 << SECTOR_SHIFT
299 ctx->offset_in += sg_in.length;
300 if (ctx->offset_in >= bv_in->bv_len) {
305 ctx->offset_out += sg_out.length;
306 if (ctx->offset_out >= bv_out->bv_len) {
311 r = crypt_convert_scatterlist(cc, &sg_out, &sg_in, sg_in.length,
312 ctx->write, ctx->sector);
323 * Generate a new unfragmented bio with the given size
324 * This should never violate the device limitations
325 * May return a smaller bio when running out of pages
328 crypt_alloc_buffer(struct crypt_config *cc, unsigned int size,
329 struct bio *base_bio, unsigned int *bio_vec_idx)
332 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
333 int gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
337 * Use __GFP_NOMEMALLOC to tell the VM to act less aggressively and
338 * to fail earlier. This is not necessary but increases throughput.
339 * FIXME: Is this really intelligent?
342 bio = bio_clone(base_bio, GFP_NOIO|__GFP_NOMEMALLOC);
344 bio = bio_alloc(GFP_NOIO|__GFP_NOMEMALLOC, nr_iovecs);
348 /* if the last bio was not complete, continue where that one ended */
349 bio->bi_idx = *bio_vec_idx;
350 bio->bi_vcnt = *bio_vec_idx;
352 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
354 /* bio->bi_idx pages have already been allocated */
355 size -= bio->bi_idx * PAGE_SIZE;
357 for(i = bio->bi_idx; i < nr_iovecs; i++) {
358 struct bio_vec *bv = bio_iovec_idx(bio, i);
360 bv->bv_page = mempool_alloc(cc->page_pool, gfp_mask);
365 * if additional pages cannot be allocated without waiting,
366 * return a partially allocated bio, the caller will then try
367 * to allocate additional bios while submitting this partial bio
369 if ((i - bio->bi_idx) == (MIN_BIO_PAGES - 1))
370 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
373 if (size > PAGE_SIZE)
374 bv->bv_len = PAGE_SIZE;
378 bio->bi_size += bv->bv_len;
389 * Remember the last bio_vec allocated to be able
390 * to correctly continue after the splitting.
392 *bio_vec_idx = bio->bi_vcnt;
397 static void crypt_free_buffer_pages(struct crypt_config *cc,
398 struct bio *bio, unsigned int bytes)
400 unsigned int i, start, end;
404 * This is ugly, but Jens Axboe thinks that using bi_idx in the
405 * endio function is too dangerous at the moment, so I calculate the
406 * correct position using bi_vcnt and bi_size.
407 * The bv_offset and bv_len fields might already be modified but we
408 * know that we always allocated whole pages.
409 * A fix to the bi_idx issue in the kernel is in the works, so
410 * we will hopefully be able to revert to the cleaner solution soon.
412 i = bio->bi_vcnt - 1;
413 bv = bio_iovec_idx(bio, i);
414 end = (i << PAGE_SHIFT) + (bv->bv_offset + bv->bv_len) - bio->bi_size;
417 start >>= PAGE_SHIFT;
423 for(i = start; i < end; i++) {
424 bv = bio_iovec_idx(bio, i);
425 BUG_ON(!bv->bv_page);
426 mempool_free(bv->bv_page, cc->page_pool);
432 * One of the bios was finished. Check for completion of
433 * the whole request and correctly clean up the buffer.
435 static void dec_pending(struct crypt_io *io, int error)
437 struct crypt_config *cc = (struct crypt_config *) io->target->private;
442 if (!atomic_dec_and_test(&io->pending))
446 bio_put(io->first_clone);
448 bio_endio(io->bio, io->bio->bi_size, io->error);
450 mempool_free(io, cc->io_pool);
456 * Needed because it would be very unwise to do decryption in an
457 * interrupt context, so bios returning from read requests get
460 static struct workqueue_struct *_kcryptd_workqueue;
462 static void kcryptd_do_work(void *data)
464 struct crypt_io *io = (struct crypt_io *) data;
465 struct crypt_config *cc = (struct crypt_config *) io->target->private;
466 struct convert_context ctx;
469 crypt_convert_init(cc, &ctx, io->bio, io->bio,
470 io->bio->bi_sector - io->target->begin, 0);
471 r = crypt_convert(cc, &ctx);
476 static void kcryptd_queue_io(struct crypt_io *io)
478 INIT_WORK(&io->work, kcryptd_do_work, io);
479 queue_work(_kcryptd_workqueue, &io->work);
483 * Decode key from its hex representation
485 static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
493 for(i = 0; i < size; i++) {
497 key[i] = (u8)simple_strtoul(buffer, &endp, 16);
499 if (endp != &buffer[2])
510 * Encode key into its hex representation
512 static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
516 for(i = 0; i < size; i++) {
517 sprintf(hex, "%02x", *key);
524 * Construct an encryption mapping:
525 * <cipher> <key> <iv_offset> <dev_path> <start>
527 static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
529 struct crypt_config *cc;
530 struct crypto_tfm *tfm;
536 unsigned int crypto_flags;
537 unsigned int key_size;
540 ti->error = PFX "Not enough arguments";
545 cipher = strsep(&tmp, "-");
546 chainmode = strsep(&tmp, "-");
547 ivopts = strsep(&tmp, "-");
548 ivmode = strsep(&ivopts, ":");
551 DMWARN(PFX "Unexpected additional cipher options");
553 key_size = strlen(argv[1]) >> 1;
555 cc = kmalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
558 PFX "Cannot allocate transparent encryption context";
562 cc->key_size = key_size;
563 if ((!key_size && strcmp(argv[1], "-") != 0) ||
564 (key_size && crypt_decode_key(cc->key, argv[1], key_size) < 0)) {
565 ti->error = PFX "Error decoding key";
569 /* Compatiblity mode for old dm-crypt cipher strings */
570 if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
575 /* Choose crypto_flags according to chainmode */
576 if (strcmp(chainmode, "cbc") == 0)
577 crypto_flags = CRYPTO_TFM_MODE_CBC;
578 else if (strcmp(chainmode, "ecb") == 0)
579 crypto_flags = CRYPTO_TFM_MODE_ECB;
581 ti->error = PFX "Unknown chaining mode";
585 if (crypto_flags != CRYPTO_TFM_MODE_ECB && !ivmode) {
586 ti->error = PFX "This chaining mode requires an IV mechanism";
590 tfm = crypto_alloc_tfm(cipher, crypto_flags);
592 ti->error = PFX "Error allocating crypto tfm";
595 if (crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER) {
596 ti->error = PFX "Expected cipher algorithm";
603 * Choose ivmode. Valid modes: "plain", "essiv:<esshash>".
604 * See comments at iv code
608 cc->iv_gen_ops = NULL;
609 else if (strcmp(ivmode, "plain") == 0)
610 cc->iv_gen_ops = &crypt_iv_plain_ops;
611 else if (strcmp(ivmode, "essiv") == 0)
612 cc->iv_gen_ops = &crypt_iv_essiv_ops;
614 ti->error = PFX "Invalid IV mode";
618 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
619 cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
622 if (tfm->crt_cipher.cit_decrypt_iv && tfm->crt_cipher.cit_encrypt_iv)
623 /* at least a 64 bit sector number should fit in our buffer */
624 cc->iv_size = max(crypto_tfm_alg_ivsize(tfm),
625 (unsigned int)(sizeof(u64) / sizeof(u8)));
628 if (cc->iv_gen_ops) {
629 DMWARN(PFX "Selected cipher does not support IVs");
630 if (cc->iv_gen_ops->dtr)
631 cc->iv_gen_ops->dtr(cc);
632 cc->iv_gen_ops = NULL;
636 cc->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
637 mempool_free_slab, _crypt_io_pool);
639 ti->error = PFX "Cannot allocate crypt io mempool";
643 cc->page_pool = mempool_create(MIN_POOL_PAGES, mempool_alloc_page,
644 mempool_free_page, NULL);
645 if (!cc->page_pool) {
646 ti->error = PFX "Cannot allocate page mempool";
650 if (tfm->crt_cipher.cit_setkey(tfm, cc->key, key_size) < 0) {
651 ti->error = PFX "Error setting key";
655 if (sscanf(argv[2], SECTOR_FORMAT, &cc->iv_offset) != 1) {
656 ti->error = PFX "Invalid iv_offset sector";
660 if (sscanf(argv[4], SECTOR_FORMAT, &cc->start) != 1) {
661 ti->error = PFX "Invalid device sector";
665 if (dm_get_device(ti, argv[3], cc->start, ti->len,
666 dm_table_get_mode(ti->table), &cc->dev)) {
667 ti->error = PFX "Device lookup failed";
671 if (ivmode && cc->iv_gen_ops) {
674 cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
676 ti->error = PFX "Error kmallocing iv_mode string";
679 strcpy(cc->iv_mode, ivmode);
687 mempool_destroy(cc->page_pool);
689 mempool_destroy(cc->io_pool);
691 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
692 cc->iv_gen_ops->dtr(cc);
694 crypto_free_tfm(tfm);
700 static void crypt_dtr(struct dm_target *ti)
702 struct crypt_config *cc = (struct crypt_config *) ti->private;
704 mempool_destroy(cc->page_pool);
705 mempool_destroy(cc->io_pool);
708 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
709 cc->iv_gen_ops->dtr(cc);
710 crypto_free_tfm(cc->tfm);
711 dm_put_device(ti, cc->dev);
715 static int crypt_endio(struct bio *bio, unsigned int done, int error)
717 struct crypt_io *io = (struct crypt_io *) bio->bi_private;
718 struct crypt_config *cc = (struct crypt_config *) io->target->private;
720 if (bio_data_dir(bio) == WRITE) {
722 * free the processed pages, even if
723 * it's only a partially completed write
725 crypt_free_buffer_pages(cc, bio, done);
734 * successful reads are decrypted by the worker thread
736 if ((bio_data_dir(bio) == READ)
737 && bio_flagged(bio, BIO_UPTODATE)) {
738 kcryptd_queue_io(io);
742 dec_pending(io, error);
746 static inline struct bio *
747 crypt_clone(struct crypt_config *cc, struct crypt_io *io, struct bio *bio,
748 sector_t sector, unsigned int *bvec_idx,
749 struct convert_context *ctx)
753 if (bio_data_dir(bio) == WRITE) {
754 clone = crypt_alloc_buffer(cc, bio->bi_size,
755 io->first_clone, bvec_idx);
757 ctx->bio_out = clone;
758 if (crypt_convert(cc, ctx) < 0) {
759 crypt_free_buffer_pages(cc, clone,
767 * The block layer might modify the bvec array, so always
768 * copy the required bvecs because we need the original
769 * one in order to decrypt the whole bio data *afterwards*.
771 clone = bio_alloc(GFP_NOIO, bio_segments(bio));
774 clone->bi_vcnt = bio_segments(bio);
775 clone->bi_size = bio->bi_size;
776 memcpy(clone->bi_io_vec, bio_iovec(bio),
777 sizeof(struct bio_vec) * clone->bi_vcnt);
784 clone->bi_private = io;
785 clone->bi_end_io = crypt_endio;
786 clone->bi_bdev = cc->dev->bdev;
787 clone->bi_sector = cc->start + sector;
788 clone->bi_rw = bio->bi_rw;
793 static int crypt_map(struct dm_target *ti, struct bio *bio,
794 union map_info *map_context)
796 struct crypt_config *cc = (struct crypt_config *) ti->private;
797 struct crypt_io *io = mempool_alloc(cc->io_pool, GFP_NOIO);
798 struct convert_context ctx;
800 unsigned int remaining = bio->bi_size;
801 sector_t sector = bio->bi_sector - ti->begin;
802 unsigned int bvec_idx = 0;
806 io->first_clone = NULL;
808 atomic_set(&io->pending, 1); /* hold a reference */
810 if (bio_data_dir(bio) == WRITE)
811 crypt_convert_init(cc, &ctx, NULL, bio, sector, 1);
814 * The allocated buffers can be smaller than the whole bio,
815 * so repeat the whole process until all the data can be handled.
818 clone = crypt_clone(cc, io, bio, sector, &bvec_idx, &ctx);
822 if (!io->first_clone) {
824 * hold a reference to the first clone, because it
825 * holds the bio_vec array and that can't be freed
826 * before all other clones are released
829 io->first_clone = clone;
831 atomic_inc(&io->pending);
833 remaining -= clone->bi_size;
834 sector += bio_sectors(clone);
836 generic_make_request(clone);
838 /* out of memory -> run queues */
840 blk_congestion_wait(bio_data_dir(clone), HZ/100);
843 /* drop reference, clones could have returned before we reach this */
848 if (io->first_clone) {
849 dec_pending(io, -ENOMEM);
853 /* if no bio has been dispatched yet, we can directly return the error */
854 mempool_free(io, cc->io_pool);
858 static int crypt_status(struct dm_target *ti, status_type_t type,
859 char *result, unsigned int maxlen)
861 struct crypt_config *cc = (struct crypt_config *) ti->private;
863 const char *chainmode = NULL;
867 case STATUSTYPE_INFO:
871 case STATUSTYPE_TABLE:
872 cipher = crypto_tfm_alg_name(cc->tfm);
874 switch(cc->tfm->crt_cipher.cit_mode) {
875 case CRYPTO_TFM_MODE_CBC:
878 case CRYPTO_TFM_MODE_ECB:
886 DMEMIT("%s-%s-%s ", cipher, chainmode, cc->iv_mode);
888 DMEMIT("%s-%s ", cipher, chainmode);
890 if (cc->key_size > 0) {
891 if ((maxlen - sz) < ((cc->key_size << 1) + 1))
894 crypt_encode_key(result + sz, cc->key, cc->key_size);
895 sz += cc->key_size << 1;
902 DMEMIT(" " SECTOR_FORMAT " %s " SECTOR_FORMAT,
903 cc->iv_offset, cc->dev->name, cc->start);
909 static struct target_type crypt_target = {
912 .module = THIS_MODULE,
916 .status = crypt_status,
919 static int __init dm_crypt_init(void)
923 _crypt_io_pool = kmem_cache_create("dm-crypt_io",
924 sizeof(struct crypt_io),
929 _kcryptd_workqueue = create_workqueue("kcryptd");
930 if (!_kcryptd_workqueue) {
932 DMERR(PFX "couldn't create kcryptd");
936 r = dm_register_target(&crypt_target);
938 DMERR(PFX "register failed %d", r);
945 destroy_workqueue(_kcryptd_workqueue);
947 kmem_cache_destroy(_crypt_io_pool);
951 static void __exit dm_crypt_exit(void)
953 int r = dm_unregister_target(&crypt_target);
956 DMERR(PFX "unregister failed %d", r);
958 destroy_workqueue(_kcryptd_workqueue);
959 kmem_cache_destroy(_crypt_io_pool);
962 module_init(dm_crypt_init);
963 module_exit(dm_crypt_exit);
965 MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
966 MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
967 MODULE_LICENSE("GPL");