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 <linux/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(gfp_t 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, CRYPTO_TFM_REQ_MAY_SLEEP);
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_set_buf(&sg, cc->key, cc->key_size);
168 crypto_digest_digest(hash_tfm, &sg, 1, salt);
169 crypto_free_tfm(hash_tfm);
171 /* Setup the essiv_tfm with the given salt */
172 essiv_tfm = crypto_alloc_tfm(crypto_tfm_alg_name(cc->tfm),
173 CRYPTO_TFM_MODE_ECB |
174 CRYPTO_TFM_REQ_MAY_SLEEP);
175 if (essiv_tfm == NULL) {
176 ti->error = PFX "Error allocating crypto tfm for ESSIV";
180 if (crypto_tfm_alg_blocksize(essiv_tfm)
181 != crypto_tfm_alg_ivsize(cc->tfm)) {
182 ti->error = PFX "Block size of ESSIV cipher does "
183 "not match IV size of block cipher";
184 crypto_free_tfm(essiv_tfm);
188 if (crypto_cipher_setkey(essiv_tfm, salt, saltsize) < 0) {
189 ti->error = PFX "Failed to set key for ESSIV cipher";
190 crypto_free_tfm(essiv_tfm);
196 cc->iv_gen_private = (void *)essiv_tfm;
200 static void crypt_iv_essiv_dtr(struct crypt_config *cc)
202 crypto_free_tfm((struct crypto_tfm *)cc->iv_gen_private);
203 cc->iv_gen_private = NULL;
206 static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, sector_t sector)
208 struct scatterlist sg;
210 memset(iv, 0, cc->iv_size);
211 *(u64 *)iv = cpu_to_le64(sector);
213 sg_set_buf(&sg, iv, cc->iv_size);
214 crypto_cipher_encrypt((struct crypto_tfm *)cc->iv_gen_private,
215 &sg, &sg, cc->iv_size);
220 static struct crypt_iv_operations crypt_iv_plain_ops = {
221 .generator = crypt_iv_plain_gen
224 static struct crypt_iv_operations crypt_iv_essiv_ops = {
225 .ctr = crypt_iv_essiv_ctr,
226 .dtr = crypt_iv_essiv_dtr,
227 .generator = crypt_iv_essiv_gen
232 crypt_convert_scatterlist(struct crypt_config *cc, struct scatterlist *out,
233 struct scatterlist *in, unsigned int length,
234 int write, sector_t sector)
239 if (cc->iv_gen_ops) {
240 r = cc->iv_gen_ops->generator(cc, iv, sector);
245 r = crypto_cipher_encrypt_iv(cc->tfm, out, in, length, iv);
247 r = crypto_cipher_decrypt_iv(cc->tfm, out, in, length, iv);
250 r = crypto_cipher_encrypt(cc->tfm, out, in, length);
252 r = crypto_cipher_decrypt(cc->tfm, out, in, length);
259 crypt_convert_init(struct crypt_config *cc, struct convert_context *ctx,
260 struct bio *bio_out, struct bio *bio_in,
261 sector_t sector, int write)
263 ctx->bio_in = bio_in;
264 ctx->bio_out = bio_out;
267 ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
268 ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
269 ctx->sector = sector + cc->iv_offset;
274 * Encrypt / decrypt data from one bio to another one (can be the same one)
276 static int crypt_convert(struct crypt_config *cc,
277 struct convert_context *ctx)
281 while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
282 ctx->idx_out < ctx->bio_out->bi_vcnt) {
283 struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
284 struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
285 struct scatterlist sg_in = {
286 .page = bv_in->bv_page,
287 .offset = bv_in->bv_offset + ctx->offset_in,
288 .length = 1 << SECTOR_SHIFT
290 struct scatterlist sg_out = {
291 .page = bv_out->bv_page,
292 .offset = bv_out->bv_offset + ctx->offset_out,
293 .length = 1 << SECTOR_SHIFT
296 ctx->offset_in += sg_in.length;
297 if (ctx->offset_in >= bv_in->bv_len) {
302 ctx->offset_out += sg_out.length;
303 if (ctx->offset_out >= bv_out->bv_len) {
308 r = crypt_convert_scatterlist(cc, &sg_out, &sg_in, sg_in.length,
309 ctx->write, ctx->sector);
320 * Generate a new unfragmented bio with the given size
321 * This should never violate the device limitations
322 * May return a smaller bio when running out of pages
325 crypt_alloc_buffer(struct crypt_config *cc, unsigned int size,
326 struct bio *base_bio, unsigned int *bio_vec_idx)
329 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
330 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
334 * Use __GFP_NOMEMALLOC to tell the VM to act less aggressively and
335 * to fail earlier. This is not necessary but increases throughput.
336 * FIXME: Is this really intelligent?
339 bio = bio_clone(base_bio, GFP_NOIO|__GFP_NOMEMALLOC);
341 bio = bio_alloc(GFP_NOIO|__GFP_NOMEMALLOC, nr_iovecs);
345 /* if the last bio was not complete, continue where that one ended */
346 bio->bi_idx = *bio_vec_idx;
347 bio->bi_vcnt = *bio_vec_idx;
349 bio->bi_flags &= ~(1 << BIO_SEG_VALID);
351 /* bio->bi_idx pages have already been allocated */
352 size -= bio->bi_idx * PAGE_SIZE;
354 for(i = bio->bi_idx; i < nr_iovecs; i++) {
355 struct bio_vec *bv = bio_iovec_idx(bio, i);
357 bv->bv_page = mempool_alloc(cc->page_pool, gfp_mask);
362 * if additional pages cannot be allocated without waiting,
363 * return a partially allocated bio, the caller will then try
364 * to allocate additional bios while submitting this partial bio
366 if ((i - bio->bi_idx) == (MIN_BIO_PAGES - 1))
367 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
370 if (size > PAGE_SIZE)
371 bv->bv_len = PAGE_SIZE;
375 bio->bi_size += bv->bv_len;
386 * Remember the last bio_vec allocated to be able
387 * to correctly continue after the splitting.
389 *bio_vec_idx = bio->bi_vcnt;
394 static void crypt_free_buffer_pages(struct crypt_config *cc,
395 struct bio *bio, unsigned int bytes)
397 unsigned int i, start, end;
401 * This is ugly, but Jens Axboe thinks that using bi_idx in the
402 * endio function is too dangerous at the moment, so I calculate the
403 * correct position using bi_vcnt and bi_size.
404 * The bv_offset and bv_len fields might already be modified but we
405 * know that we always allocated whole pages.
406 * A fix to the bi_idx issue in the kernel is in the works, so
407 * we will hopefully be able to revert to the cleaner solution soon.
409 i = bio->bi_vcnt - 1;
410 bv = bio_iovec_idx(bio, i);
411 end = (i << PAGE_SHIFT) + (bv->bv_offset + bv->bv_len) - bio->bi_size;
414 start >>= PAGE_SHIFT;
420 for(i = start; i < end; i++) {
421 bv = bio_iovec_idx(bio, i);
422 BUG_ON(!bv->bv_page);
423 mempool_free(bv->bv_page, cc->page_pool);
429 * One of the bios was finished. Check for completion of
430 * the whole request and correctly clean up the buffer.
432 static void dec_pending(struct crypt_io *io, int error)
434 struct crypt_config *cc = (struct crypt_config *) io->target->private;
439 if (!atomic_dec_and_test(&io->pending))
443 bio_put(io->first_clone);
445 bio_endio(io->bio, io->bio->bi_size, io->error);
447 mempool_free(io, cc->io_pool);
453 * Needed because it would be very unwise to do decryption in an
454 * interrupt context, so bios returning from read requests get
457 static struct workqueue_struct *_kcryptd_workqueue;
459 static void kcryptd_do_work(void *data)
461 struct crypt_io *io = (struct crypt_io *) data;
462 struct crypt_config *cc = (struct crypt_config *) io->target->private;
463 struct convert_context ctx;
466 crypt_convert_init(cc, &ctx, io->bio, io->bio,
467 io->bio->bi_sector - io->target->begin, 0);
468 r = crypt_convert(cc, &ctx);
473 static void kcryptd_queue_io(struct crypt_io *io)
475 INIT_WORK(&io->work, kcryptd_do_work, io);
476 queue_work(_kcryptd_workqueue, &io->work);
480 * Decode key from its hex representation
482 static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
490 for(i = 0; i < size; i++) {
494 key[i] = (u8)simple_strtoul(buffer, &endp, 16);
496 if (endp != &buffer[2])
507 * Encode key into its hex representation
509 static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
513 for(i = 0; i < size; i++) {
514 sprintf(hex, "%02x", *key);
521 * Construct an encryption mapping:
522 * <cipher> <key> <iv_offset> <dev_path> <start>
524 static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
526 struct crypt_config *cc;
527 struct crypto_tfm *tfm;
533 unsigned int crypto_flags;
534 unsigned int key_size;
537 ti->error = PFX "Not enough arguments";
542 cipher = strsep(&tmp, "-");
543 chainmode = strsep(&tmp, "-");
544 ivopts = strsep(&tmp, "-");
545 ivmode = strsep(&ivopts, ":");
548 DMWARN(PFX "Unexpected additional cipher options");
550 key_size = strlen(argv[1]) >> 1;
552 cc = kmalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
555 PFX "Cannot allocate transparent encryption context";
559 cc->key_size = key_size;
560 if ((!key_size && strcmp(argv[1], "-") != 0) ||
561 (key_size && crypt_decode_key(cc->key, argv[1], key_size) < 0)) {
562 ti->error = PFX "Error decoding key";
566 /* Compatiblity mode for old dm-crypt cipher strings */
567 if (!chainmode || (strcmp(chainmode, "plain") == 0 && !ivmode)) {
572 /* Choose crypto_flags according to chainmode */
573 if (strcmp(chainmode, "cbc") == 0)
574 crypto_flags = CRYPTO_TFM_MODE_CBC;
575 else if (strcmp(chainmode, "ecb") == 0)
576 crypto_flags = CRYPTO_TFM_MODE_ECB;
578 ti->error = PFX "Unknown chaining mode";
582 if (crypto_flags != CRYPTO_TFM_MODE_ECB && !ivmode) {
583 ti->error = PFX "This chaining mode requires an IV mechanism";
587 tfm = crypto_alloc_tfm(cipher, crypto_flags | CRYPTO_TFM_REQ_MAY_SLEEP);
589 ti->error = PFX "Error allocating crypto tfm";
592 if (crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER) {
593 ti->error = PFX "Expected cipher algorithm";
600 * Choose ivmode. Valid modes: "plain", "essiv:<esshash>".
601 * See comments at iv code
605 cc->iv_gen_ops = NULL;
606 else if (strcmp(ivmode, "plain") == 0)
607 cc->iv_gen_ops = &crypt_iv_plain_ops;
608 else if (strcmp(ivmode, "essiv") == 0)
609 cc->iv_gen_ops = &crypt_iv_essiv_ops;
611 ti->error = PFX "Invalid IV mode";
615 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr &&
616 cc->iv_gen_ops->ctr(cc, ti, ivopts) < 0)
619 if (tfm->crt_cipher.cit_decrypt_iv && tfm->crt_cipher.cit_encrypt_iv)
620 /* at least a 64 bit sector number should fit in our buffer */
621 cc->iv_size = max(crypto_tfm_alg_ivsize(tfm),
622 (unsigned int)(sizeof(u64) / sizeof(u8)));
625 if (cc->iv_gen_ops) {
626 DMWARN(PFX "Selected cipher does not support IVs");
627 if (cc->iv_gen_ops->dtr)
628 cc->iv_gen_ops->dtr(cc);
629 cc->iv_gen_ops = NULL;
633 cc->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
634 mempool_free_slab, _crypt_io_pool);
636 ti->error = PFX "Cannot allocate crypt io mempool";
640 cc->page_pool = mempool_create(MIN_POOL_PAGES, mempool_alloc_page,
641 mempool_free_page, NULL);
642 if (!cc->page_pool) {
643 ti->error = PFX "Cannot allocate page mempool";
647 if (tfm->crt_cipher.cit_setkey(tfm, cc->key, key_size) < 0) {
648 ti->error = PFX "Error setting key";
652 if (sscanf(argv[2], SECTOR_FORMAT, &cc->iv_offset) != 1) {
653 ti->error = PFX "Invalid iv_offset sector";
657 if (sscanf(argv[4], SECTOR_FORMAT, &cc->start) != 1) {
658 ti->error = PFX "Invalid device sector";
662 if (dm_get_device(ti, argv[3], cc->start, ti->len,
663 dm_table_get_mode(ti->table), &cc->dev)) {
664 ti->error = PFX "Device lookup failed";
668 if (ivmode && cc->iv_gen_ops) {
671 cc->iv_mode = kmalloc(strlen(ivmode) + 1, GFP_KERNEL);
673 ti->error = PFX "Error kmallocing iv_mode string";
676 strcpy(cc->iv_mode, ivmode);
684 mempool_destroy(cc->page_pool);
686 mempool_destroy(cc->io_pool);
688 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
689 cc->iv_gen_ops->dtr(cc);
691 crypto_free_tfm(tfm);
693 /* Must zero key material before freeing */
694 memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
699 static void crypt_dtr(struct dm_target *ti)
701 struct crypt_config *cc = (struct crypt_config *) ti->private;
703 mempool_destroy(cc->page_pool);
704 mempool_destroy(cc->io_pool);
707 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
708 cc->iv_gen_ops->dtr(cc);
709 crypto_free_tfm(cc->tfm);
710 dm_put_device(ti, cc->dev);
712 /* Must zero key material before freeing */
713 memset(cc, 0, sizeof(*cc) + cc->key_size * sizeof(u8));
717 static int crypt_endio(struct bio *bio, unsigned int done, int error)
719 struct crypt_io *io = (struct crypt_io *) bio->bi_private;
720 struct crypt_config *cc = (struct crypt_config *) io->target->private;
722 if (bio_data_dir(bio) == WRITE) {
724 * free the processed pages, even if
725 * it's only a partially completed write
727 crypt_free_buffer_pages(cc, bio, done);
736 * successful reads are decrypted by the worker thread
738 if ((bio_data_dir(bio) == READ)
739 && bio_flagged(bio, BIO_UPTODATE)) {
740 kcryptd_queue_io(io);
744 dec_pending(io, error);
748 static inline struct bio *
749 crypt_clone(struct crypt_config *cc, struct crypt_io *io, struct bio *bio,
750 sector_t sector, unsigned int *bvec_idx,
751 struct convert_context *ctx)
755 if (bio_data_dir(bio) == WRITE) {
756 clone = crypt_alloc_buffer(cc, bio->bi_size,
757 io->first_clone, bvec_idx);
759 ctx->bio_out = clone;
760 if (crypt_convert(cc, ctx) < 0) {
761 crypt_free_buffer_pages(cc, clone,
769 * The block layer might modify the bvec array, so always
770 * copy the required bvecs because we need the original
771 * one in order to decrypt the whole bio data *afterwards*.
773 clone = bio_alloc(GFP_NOIO, bio_segments(bio));
776 clone->bi_vcnt = bio_segments(bio);
777 clone->bi_size = bio->bi_size;
778 memcpy(clone->bi_io_vec, bio_iovec(bio),
779 sizeof(struct bio_vec) * clone->bi_vcnt);
786 clone->bi_private = io;
787 clone->bi_end_io = crypt_endio;
788 clone->bi_bdev = cc->dev->bdev;
789 clone->bi_sector = cc->start + sector;
790 clone->bi_rw = bio->bi_rw;
795 static int crypt_map(struct dm_target *ti, struct bio *bio,
796 union map_info *map_context)
798 struct crypt_config *cc = (struct crypt_config *) ti->private;
799 struct crypt_io *io = mempool_alloc(cc->io_pool, GFP_NOIO);
800 struct convert_context ctx;
802 unsigned int remaining = bio->bi_size;
803 sector_t sector = bio->bi_sector - ti->begin;
804 unsigned int bvec_idx = 0;
808 io->first_clone = NULL;
810 atomic_set(&io->pending, 1); /* hold a reference */
812 if (bio_data_dir(bio) == WRITE)
813 crypt_convert_init(cc, &ctx, NULL, bio, sector, 1);
816 * The allocated buffers can be smaller than the whole bio,
817 * so repeat the whole process until all the data can be handled.
820 clone = crypt_clone(cc, io, bio, sector, &bvec_idx, &ctx);
824 if (!io->first_clone) {
826 * hold a reference to the first clone, because it
827 * holds the bio_vec array and that can't be freed
828 * before all other clones are released
831 io->first_clone = clone;
833 atomic_inc(&io->pending);
835 remaining -= clone->bi_size;
836 sector += bio_sectors(clone);
838 generic_make_request(clone);
840 /* out of memory -> run queues */
842 blk_congestion_wait(bio_data_dir(clone), HZ/100);
845 /* drop reference, clones could have returned before we reach this */
850 if (io->first_clone) {
851 dec_pending(io, -ENOMEM);
855 /* if no bio has been dispatched yet, we can directly return the error */
856 mempool_free(io, cc->io_pool);
860 static int crypt_status(struct dm_target *ti, status_type_t type,
861 char *result, unsigned int maxlen)
863 struct crypt_config *cc = (struct crypt_config *) ti->private;
865 const char *chainmode = NULL;
869 case STATUSTYPE_INFO:
873 case STATUSTYPE_TABLE:
874 cipher = crypto_tfm_alg_name(cc->tfm);
876 switch(cc->tfm->crt_cipher.cit_mode) {
877 case CRYPTO_TFM_MODE_CBC:
880 case CRYPTO_TFM_MODE_ECB:
888 DMEMIT("%s-%s-%s ", cipher, chainmode, cc->iv_mode);
890 DMEMIT("%s-%s ", cipher, chainmode);
892 if (cc->key_size > 0) {
893 if ((maxlen - sz) < ((cc->key_size << 1) + 1))
896 crypt_encode_key(result + sz, cc->key, cc->key_size);
897 sz += cc->key_size << 1;
904 DMEMIT(" " SECTOR_FORMAT " %s " SECTOR_FORMAT,
905 cc->iv_offset, cc->dev->name, cc->start);
911 static struct target_type crypt_target = {
914 .module = THIS_MODULE,
918 .status = crypt_status,
921 static int __init dm_crypt_init(void)
925 _crypt_io_pool = kmem_cache_create("dm-crypt_io",
926 sizeof(struct crypt_io),
931 _kcryptd_workqueue = create_workqueue("kcryptd");
932 if (!_kcryptd_workqueue) {
934 DMERR(PFX "couldn't create kcryptd");
938 r = dm_register_target(&crypt_target);
940 DMERR(PFX "register failed %d", r);
947 destroy_workqueue(_kcryptd_workqueue);
949 kmem_cache_destroy(_crypt_io_pool);
953 static void __exit dm_crypt_exit(void)
955 int r = dm_unregister_target(&crypt_target);
958 DMERR(PFX "unregister failed %d", r);
960 destroy_workqueue(_kcryptd_workqueue);
961 kmem_cache_destroy(_crypt_io_pool);
964 module_init(dm_crypt_init);
965 module_exit(dm_crypt_exit);
967 MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
968 MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
969 MODULE_LICENSE("GPL");