2 * Copyright (C) 2003 Jana Saout <jana@saout.de>
3 * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
4 * Copyright (C) 2006-2017 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2013-2017 Milan Broz <gmazyland@gmail.com>
7 * This file is released under the GPL.
10 #include <linux/completion.h>
11 #include <linux/err.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/key.h>
16 #include <linux/bio.h>
17 #include <linux/blkdev.h>
18 #include <linux/mempool.h>
19 #include <linux/slab.h>
20 #include <linux/crypto.h>
21 #include <linux/workqueue.h>
22 #include <linux/kthread.h>
23 #include <linux/backing-dev.h>
24 #include <linux/atomic.h>
25 #include <linux/scatterlist.h>
26 #include <linux/rbtree.h>
27 #include <linux/ctype.h>
29 #include <asm/unaligned.h>
30 #include <crypto/hash.h>
31 #include <crypto/md5.h>
32 #include <crypto/algapi.h>
33 #include <crypto/skcipher.h>
34 #include <crypto/aead.h>
35 #include <crypto/authenc.h>
36 #include <linux/rtnetlink.h> /* for struct rtattr and RTA macros only */
37 #include <keys/user-type.h>
39 #include <linux/device-mapper.h>
41 #define DM_MSG_PREFIX "crypt"
44 * context holding the current state of a multi-part conversion
46 struct convert_context {
47 struct completion restart;
50 struct bvec_iter iter_in;
51 struct bvec_iter iter_out;
55 struct skcipher_request *req;
56 struct aead_request *req_aead;
62 * per bio private data
65 struct crypt_config *cc;
67 u8 *integrity_metadata;
68 bool integrity_metadata_from_pool;
69 struct work_struct work;
71 struct convert_context ctx;
77 struct rb_node rb_node;
78 } CRYPTO_MINALIGN_ATTR;
80 struct dm_crypt_request {
81 struct convert_context *ctx;
82 struct scatterlist sg_in[4];
83 struct scatterlist sg_out[4];
89 struct crypt_iv_operations {
90 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
92 void (*dtr)(struct crypt_config *cc);
93 int (*init)(struct crypt_config *cc);
94 int (*wipe)(struct crypt_config *cc);
95 int (*generator)(struct crypt_config *cc, u8 *iv,
96 struct dm_crypt_request *dmreq);
97 int (*post)(struct crypt_config *cc, u8 *iv,
98 struct dm_crypt_request *dmreq);
101 struct iv_essiv_private {
102 struct crypto_shash *hash_tfm;
106 struct iv_benbi_private {
110 #define LMK_SEED_SIZE 64 /* hash + 0 */
111 struct iv_lmk_private {
112 struct crypto_shash *hash_tfm;
116 #define TCW_WHITENING_SIZE 16
117 struct iv_tcw_private {
118 struct crypto_shash *crc32_tfm;
123 struct iv_eboiv_private {
124 struct crypto_cipher *tfm;
128 * Crypt: maps a linear range of a block device
129 * and encrypts / decrypts at the same time.
131 enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID,
132 DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD };
135 CRYPT_MODE_INTEGRITY_AEAD, /* Use authenticated mode for cihper */
136 CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */
140 * The fields in here must be read only after initialization.
142 struct crypt_config {
146 struct percpu_counter n_allocated_pages;
148 struct workqueue_struct *io_queue;
149 struct workqueue_struct *crypt_queue;
151 spinlock_t write_thread_lock;
152 struct task_struct *write_thread;
153 struct rb_root write_tree;
160 const struct crypt_iv_operations *iv_gen_ops;
162 struct iv_essiv_private essiv;
163 struct iv_benbi_private benbi;
164 struct iv_lmk_private lmk;
165 struct iv_tcw_private tcw;
166 struct iv_eboiv_private eboiv;
169 unsigned int iv_size;
170 unsigned short int sector_size;
171 unsigned char sector_shift;
173 /* ESSIV: struct crypto_cipher *essiv_tfm */
176 struct crypto_skcipher **tfms;
177 struct crypto_aead **tfms_aead;
180 unsigned long cipher_flags;
183 * Layout of each crypto request:
185 * struct skcipher_request
188 * struct dm_crypt_request
192 * The padding is added so that dm_crypt_request and the IV are
195 unsigned int dmreq_start;
197 unsigned int per_bio_data_size;
200 unsigned int key_size;
201 unsigned int key_parts; /* independent parts in key buffer */
202 unsigned int key_extra_size; /* additional keys length */
203 unsigned int key_mac_size; /* MAC key size for authenc(...) */
205 unsigned int integrity_tag_size;
206 unsigned int integrity_iv_size;
207 unsigned int on_disk_tag_size;
210 * pool for per bio private data, crypto requests,
211 * encryption requeusts/buffer pages and integrity tags
213 unsigned tag_pool_max_sectors;
219 struct mutex bio_alloc_lock;
221 u8 *authenc_key; /* space for keys in authenc() format (if used) */
226 #define MAX_TAG_SIZE 480
227 #define POOL_ENTRY_SIZE 512
229 static DEFINE_SPINLOCK(dm_crypt_clients_lock);
230 static unsigned dm_crypt_clients_n = 0;
231 static volatile unsigned long dm_crypt_pages_per_client;
232 #define DM_CRYPT_MEMORY_PERCENT 2
233 #define DM_CRYPT_MIN_PAGES_PER_CLIENT (BIO_MAX_PAGES * 16)
235 static void clone_init(struct dm_crypt_io *, struct bio *);
236 static void kcryptd_queue_crypt(struct dm_crypt_io *io);
237 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
238 struct scatterlist *sg);
241 * Use this to access cipher attributes that are independent of the key.
243 static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
245 return cc->cipher_tfm.tfms[0];
248 static struct crypto_aead *any_tfm_aead(struct crypt_config *cc)
250 return cc->cipher_tfm.tfms_aead[0];
254 * Different IV generation algorithms:
256 * plain: the initial vector is the 32-bit little-endian version of the sector
257 * number, padded with zeros if necessary.
259 * plain64: the initial vector is the 64-bit little-endian version of the sector
260 * number, padded with zeros if necessary.
262 * plain64be: the initial vector is the 64-bit big-endian version of the sector
263 * number, padded with zeros if necessary.
265 * essiv: "encrypted sector|salt initial vector", the sector number is
266 * encrypted with the bulk cipher using a salt as key. The salt
267 * should be derived from the bulk cipher's key via hashing.
269 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
270 * (needed for LRW-32-AES and possible other narrow block modes)
272 * null: the initial vector is always zero. Provides compatibility with
273 * obsolete loop_fish2 devices. Do not use for new devices.
275 * lmk: Compatible implementation of the block chaining mode used
276 * by the Loop-AES block device encryption system
277 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
278 * It operates on full 512 byte sectors and uses CBC
279 * with an IV derived from the sector number, the data and
280 * optionally extra IV seed.
281 * This means that after decryption the first block
282 * of sector must be tweaked according to decrypted data.
283 * Loop-AES can use three encryption schemes:
284 * version 1: is plain aes-cbc mode
285 * version 2: uses 64 multikey scheme with lmk IV generator
286 * version 3: the same as version 2 with additional IV seed
287 * (it uses 65 keys, last key is used as IV seed)
289 * tcw: Compatible implementation of the block chaining mode used
290 * by the TrueCrypt device encryption system (prior to version 4.1).
291 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
292 * It operates on full 512 byte sectors and uses CBC
293 * with an IV derived from initial key and the sector number.
294 * In addition, whitening value is applied on every sector, whitening
295 * is calculated from initial key, sector number and mixed using CRC32.
296 * Note that this encryption scheme is vulnerable to watermarking attacks
297 * and should be used for old compatible containers access only.
299 * eboiv: Encrypted byte-offset IV (used in Bitlocker in CBC mode)
300 * The IV is encrypted little-endian byte-offset (with the same key
301 * and cipher as the volume).
304 static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
305 struct dm_crypt_request *dmreq)
307 memset(iv, 0, cc->iv_size);
308 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
313 static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
314 struct dm_crypt_request *dmreq)
316 memset(iv, 0, cc->iv_size);
317 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
322 static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv,
323 struct dm_crypt_request *dmreq)
325 memset(iv, 0, cc->iv_size);
326 /* iv_size is at least of size u64; usually it is 16 bytes */
327 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector);
332 /* Initialise ESSIV - compute salt but no local memory allocations */
333 static int crypt_iv_essiv_init(struct crypt_config *cc)
335 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
336 SHASH_DESC_ON_STACK(desc, essiv->hash_tfm);
337 struct crypto_cipher *essiv_tfm;
340 desc->tfm = essiv->hash_tfm;
342 err = crypto_shash_digest(desc, cc->key, cc->key_size, essiv->salt);
343 shash_desc_zero(desc);
347 essiv_tfm = cc->iv_private;
349 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
350 crypto_shash_digestsize(essiv->hash_tfm));
357 /* Wipe salt and reset key derived from volume key */
358 static int crypt_iv_essiv_wipe(struct crypt_config *cc)
360 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
361 unsigned salt_size = crypto_shash_digestsize(essiv->hash_tfm);
362 struct crypto_cipher *essiv_tfm;
365 memset(essiv->salt, 0, salt_size);
367 essiv_tfm = cc->iv_private;
368 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
375 /* Allocate the cipher for ESSIV */
376 static struct crypto_cipher *alloc_essiv_cipher(struct crypt_config *cc,
377 struct dm_target *ti,
379 unsigned int saltsize)
381 struct crypto_cipher *essiv_tfm;
384 /* Setup the essiv_tfm with the given salt */
385 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, 0);
386 if (IS_ERR(essiv_tfm)) {
387 ti->error = "Error allocating crypto tfm for ESSIV";
391 if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) {
392 ti->error = "Block size of ESSIV cipher does "
393 "not match IV size of block cipher";
394 crypto_free_cipher(essiv_tfm);
395 return ERR_PTR(-EINVAL);
398 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
400 ti->error = "Failed to set key for ESSIV cipher";
401 crypto_free_cipher(essiv_tfm);
408 static void crypt_iv_essiv_dtr(struct crypt_config *cc)
410 struct crypto_cipher *essiv_tfm;
411 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
413 crypto_free_shash(essiv->hash_tfm);
414 essiv->hash_tfm = NULL;
419 essiv_tfm = cc->iv_private;
422 crypto_free_cipher(essiv_tfm);
424 cc->iv_private = NULL;
427 static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
430 struct crypto_cipher *essiv_tfm = NULL;
431 struct crypto_shash *hash_tfm = NULL;
436 ti->error = "Digest algorithm missing for ESSIV mode";
440 /* Allocate hash algorithm */
441 hash_tfm = crypto_alloc_shash(opts, 0, 0);
442 if (IS_ERR(hash_tfm)) {
443 ti->error = "Error initializing ESSIV hash";
444 err = PTR_ERR(hash_tfm);
448 salt = kzalloc(crypto_shash_digestsize(hash_tfm), GFP_KERNEL);
450 ti->error = "Error kmallocing salt storage in ESSIV";
455 cc->iv_gen_private.essiv.salt = salt;
456 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
458 essiv_tfm = alloc_essiv_cipher(cc, ti, salt,
459 crypto_shash_digestsize(hash_tfm));
460 if (IS_ERR(essiv_tfm)) {
461 crypt_iv_essiv_dtr(cc);
462 return PTR_ERR(essiv_tfm);
464 cc->iv_private = essiv_tfm;
469 if (hash_tfm && !IS_ERR(hash_tfm))
470 crypto_free_shash(hash_tfm);
475 static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
476 struct dm_crypt_request *dmreq)
478 struct crypto_cipher *essiv_tfm = cc->iv_private;
480 memset(iv, 0, cc->iv_size);
481 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
482 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
487 static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
490 unsigned bs = crypto_skcipher_blocksize(any_tfm(cc));
493 /* we need to calculate how far we must shift the sector count
494 * to get the cipher block count, we use this shift in _gen */
496 if (1 << log != bs) {
497 ti->error = "cypher blocksize is not a power of 2";
502 ti->error = "cypher blocksize is > 512";
506 cc->iv_gen_private.benbi.shift = 9 - log;
511 static void crypt_iv_benbi_dtr(struct crypt_config *cc)
515 static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
516 struct dm_crypt_request *dmreq)
520 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
522 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
523 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
528 static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
529 struct dm_crypt_request *dmreq)
531 memset(iv, 0, cc->iv_size);
536 static void crypt_iv_lmk_dtr(struct crypt_config *cc)
538 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
540 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
541 crypto_free_shash(lmk->hash_tfm);
542 lmk->hash_tfm = NULL;
548 static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
551 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
553 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
554 ti->error = "Unsupported sector size for LMK";
558 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
559 if (IS_ERR(lmk->hash_tfm)) {
560 ti->error = "Error initializing LMK hash";
561 return PTR_ERR(lmk->hash_tfm);
564 /* No seed in LMK version 2 */
565 if (cc->key_parts == cc->tfms_count) {
570 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
572 crypt_iv_lmk_dtr(cc);
573 ti->error = "Error kmallocing seed storage in LMK";
580 static int crypt_iv_lmk_init(struct crypt_config *cc)
582 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
583 int subkey_size = cc->key_size / cc->key_parts;
585 /* LMK seed is on the position of LMK_KEYS + 1 key */
587 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
588 crypto_shash_digestsize(lmk->hash_tfm));
593 static int crypt_iv_lmk_wipe(struct crypt_config *cc)
595 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
598 memset(lmk->seed, 0, LMK_SEED_SIZE);
603 static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
604 struct dm_crypt_request *dmreq,
607 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
608 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
609 struct md5_state md5state;
613 desc->tfm = lmk->hash_tfm;
615 r = crypto_shash_init(desc);
620 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
625 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
626 r = crypto_shash_update(desc, data + 16, 16 * 31);
630 /* Sector is cropped to 56 bits here */
631 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
632 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
633 buf[2] = cpu_to_le32(4024);
635 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
639 /* No MD5 padding here */
640 r = crypto_shash_export(desc, &md5state);
644 for (i = 0; i < MD5_HASH_WORDS; i++)
645 __cpu_to_le32s(&md5state.hash[i]);
646 memcpy(iv, &md5state.hash, cc->iv_size);
651 static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
652 struct dm_crypt_request *dmreq)
654 struct scatterlist *sg;
658 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
659 sg = crypt_get_sg_data(cc, dmreq->sg_in);
660 src = kmap_atomic(sg_page(sg));
661 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset);
664 memset(iv, 0, cc->iv_size);
669 static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
670 struct dm_crypt_request *dmreq)
672 struct scatterlist *sg;
676 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
679 sg = crypt_get_sg_data(cc, dmreq->sg_out);
680 dst = kmap_atomic(sg_page(sg));
681 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset);
683 /* Tweak the first block of plaintext sector */
685 crypto_xor(dst + sg->offset, iv, cc->iv_size);
691 static void crypt_iv_tcw_dtr(struct crypt_config *cc)
693 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
695 kzfree(tcw->iv_seed);
697 kzfree(tcw->whitening);
698 tcw->whitening = NULL;
700 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
701 crypto_free_shash(tcw->crc32_tfm);
702 tcw->crc32_tfm = NULL;
705 static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
708 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
710 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
711 ti->error = "Unsupported sector size for TCW";
715 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
716 ti->error = "Wrong key size for TCW";
720 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
721 if (IS_ERR(tcw->crc32_tfm)) {
722 ti->error = "Error initializing CRC32 in TCW";
723 return PTR_ERR(tcw->crc32_tfm);
726 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
727 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
728 if (!tcw->iv_seed || !tcw->whitening) {
729 crypt_iv_tcw_dtr(cc);
730 ti->error = "Error allocating seed storage in TCW";
737 static int crypt_iv_tcw_init(struct crypt_config *cc)
739 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
740 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
742 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
743 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
749 static int crypt_iv_tcw_wipe(struct crypt_config *cc)
751 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
753 memset(tcw->iv_seed, 0, cc->iv_size);
754 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
759 static int crypt_iv_tcw_whitening(struct crypt_config *cc,
760 struct dm_crypt_request *dmreq,
763 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
764 __le64 sector = cpu_to_le64(dmreq->iv_sector);
765 u8 buf[TCW_WHITENING_SIZE];
766 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
769 /* xor whitening with sector number */
770 crypto_xor_cpy(buf, tcw->whitening, (u8 *)§or, 8);
771 crypto_xor_cpy(&buf[8], tcw->whitening + 8, (u8 *)§or, 8);
773 /* calculate crc32 for every 32bit part and xor it */
774 desc->tfm = tcw->crc32_tfm;
775 for (i = 0; i < 4; i++) {
776 r = crypto_shash_init(desc);
779 r = crypto_shash_update(desc, &buf[i * 4], 4);
782 r = crypto_shash_final(desc, &buf[i * 4]);
786 crypto_xor(&buf[0], &buf[12], 4);
787 crypto_xor(&buf[4], &buf[8], 4);
789 /* apply whitening (8 bytes) to whole sector */
790 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
791 crypto_xor(data + i * 8, buf, 8);
793 memzero_explicit(buf, sizeof(buf));
797 static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
798 struct dm_crypt_request *dmreq)
800 struct scatterlist *sg;
801 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
802 __le64 sector = cpu_to_le64(dmreq->iv_sector);
806 /* Remove whitening from ciphertext */
807 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
808 sg = crypt_get_sg_data(cc, dmreq->sg_in);
809 src = kmap_atomic(sg_page(sg));
810 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset);
815 crypto_xor_cpy(iv, tcw->iv_seed, (u8 *)§or, 8);
817 crypto_xor_cpy(&iv[8], tcw->iv_seed + 8, (u8 *)§or,
823 static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
824 struct dm_crypt_request *dmreq)
826 struct scatterlist *sg;
830 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
833 /* Apply whitening on ciphertext */
834 sg = crypt_get_sg_data(cc, dmreq->sg_out);
835 dst = kmap_atomic(sg_page(sg));
836 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset);
842 static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv,
843 struct dm_crypt_request *dmreq)
845 /* Used only for writes, there must be an additional space to store IV */
846 get_random_bytes(iv, cc->iv_size);
850 static void crypt_iv_eboiv_dtr(struct crypt_config *cc)
852 struct iv_eboiv_private *eboiv = &cc->iv_gen_private.eboiv;
854 crypto_free_cipher(eboiv->tfm);
858 static int crypt_iv_eboiv_ctr(struct crypt_config *cc, struct dm_target *ti,
861 struct iv_eboiv_private *eboiv = &cc->iv_gen_private.eboiv;
862 struct crypto_cipher *tfm;
864 tfm = crypto_alloc_cipher(cc->cipher, 0, 0);
866 ti->error = "Error allocating crypto tfm for EBOIV";
870 if (crypto_cipher_blocksize(tfm) != cc->iv_size) {
871 ti->error = "Block size of EBOIV cipher does "
872 "not match IV size of block cipher";
873 crypto_free_cipher(tfm);
881 static int crypt_iv_eboiv_init(struct crypt_config *cc)
883 struct iv_eboiv_private *eboiv = &cc->iv_gen_private.eboiv;
886 err = crypto_cipher_setkey(eboiv->tfm, cc->key, cc->key_size);
893 static int crypt_iv_eboiv_wipe(struct crypt_config *cc)
895 /* Called after cc->key is set to random key in crypt_wipe() */
896 return crypt_iv_eboiv_init(cc);
899 static int crypt_iv_eboiv_gen(struct crypt_config *cc, u8 *iv,
900 struct dm_crypt_request *dmreq)
902 struct iv_eboiv_private *eboiv = &cc->iv_gen_private.eboiv;
904 memset(iv, 0, cc->iv_size);
905 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector * cc->sector_size);
906 crypto_cipher_encrypt_one(eboiv->tfm, iv, iv);
911 static const struct crypt_iv_operations crypt_iv_plain_ops = {
912 .generator = crypt_iv_plain_gen
915 static const struct crypt_iv_operations crypt_iv_plain64_ops = {
916 .generator = crypt_iv_plain64_gen
919 static const struct crypt_iv_operations crypt_iv_plain64be_ops = {
920 .generator = crypt_iv_plain64be_gen
923 static const struct crypt_iv_operations crypt_iv_essiv_ops = {
924 .ctr = crypt_iv_essiv_ctr,
925 .dtr = crypt_iv_essiv_dtr,
926 .init = crypt_iv_essiv_init,
927 .wipe = crypt_iv_essiv_wipe,
928 .generator = crypt_iv_essiv_gen
931 static const struct crypt_iv_operations crypt_iv_benbi_ops = {
932 .ctr = crypt_iv_benbi_ctr,
933 .dtr = crypt_iv_benbi_dtr,
934 .generator = crypt_iv_benbi_gen
937 static const struct crypt_iv_operations crypt_iv_null_ops = {
938 .generator = crypt_iv_null_gen
941 static const struct crypt_iv_operations crypt_iv_lmk_ops = {
942 .ctr = crypt_iv_lmk_ctr,
943 .dtr = crypt_iv_lmk_dtr,
944 .init = crypt_iv_lmk_init,
945 .wipe = crypt_iv_lmk_wipe,
946 .generator = crypt_iv_lmk_gen,
947 .post = crypt_iv_lmk_post
950 static const struct crypt_iv_operations crypt_iv_tcw_ops = {
951 .ctr = crypt_iv_tcw_ctr,
952 .dtr = crypt_iv_tcw_dtr,
953 .init = crypt_iv_tcw_init,
954 .wipe = crypt_iv_tcw_wipe,
955 .generator = crypt_iv_tcw_gen,
956 .post = crypt_iv_tcw_post
959 static struct crypt_iv_operations crypt_iv_random_ops = {
960 .generator = crypt_iv_random_gen
963 static struct crypt_iv_operations crypt_iv_eboiv_ops = {
964 .ctr = crypt_iv_eboiv_ctr,
965 .dtr = crypt_iv_eboiv_dtr,
966 .init = crypt_iv_eboiv_init,
967 .wipe = crypt_iv_eboiv_wipe,
968 .generator = crypt_iv_eboiv_gen
972 * Integrity extensions
974 static bool crypt_integrity_aead(struct crypt_config *cc)
976 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
979 static bool crypt_integrity_hmac(struct crypt_config *cc)
981 return crypt_integrity_aead(cc) && cc->key_mac_size;
984 /* Get sg containing data */
985 static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
986 struct scatterlist *sg)
988 if (unlikely(crypt_integrity_aead(cc)))
994 static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
996 struct bio_integrity_payload *bip;
997 unsigned int tag_len;
1000 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size)
1003 bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
1005 return PTR_ERR(bip);
1007 tag_len = io->cc->on_disk_tag_size * (bio_sectors(bio) >> io->cc->sector_shift);
1009 bip->bip_iter.bi_size = tag_len;
1010 bip->bip_iter.bi_sector = io->cc->start + io->sector;
1012 ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
1013 tag_len, offset_in_page(io->integrity_metadata));
1014 if (unlikely(ret != tag_len))
1020 static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
1022 #ifdef CONFIG_BLK_DEV_INTEGRITY
1023 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk);
1024 struct mapped_device *md = dm_table_get_md(ti->table);
1026 /* From now we require underlying device with our integrity profile */
1027 if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) {
1028 ti->error = "Integrity profile not supported.";
1032 if (bi->tag_size != cc->on_disk_tag_size ||
1033 bi->tuple_size != cc->on_disk_tag_size) {
1034 ti->error = "Integrity profile tag size mismatch.";
1037 if (1 << bi->interval_exp != cc->sector_size) {
1038 ti->error = "Integrity profile sector size mismatch.";
1042 if (crypt_integrity_aead(cc)) {
1043 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size;
1044 DMDEBUG("%s: Integrity AEAD, tag size %u, IV size %u.", dm_device_name(md),
1045 cc->integrity_tag_size, cc->integrity_iv_size);
1047 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) {
1048 ti->error = "Integrity AEAD auth tag size is not supported.";
1051 } else if (cc->integrity_iv_size)
1052 DMDEBUG("%s: Additional per-sector space %u bytes for IV.", dm_device_name(md),
1053 cc->integrity_iv_size);
1055 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) {
1056 ti->error = "Not enough space for integrity tag in the profile.";
1062 ti->error = "Integrity profile not supported.";
1067 static void crypt_convert_init(struct crypt_config *cc,
1068 struct convert_context *ctx,
1069 struct bio *bio_out, struct bio *bio_in,
1072 ctx->bio_in = bio_in;
1073 ctx->bio_out = bio_out;
1075 ctx->iter_in = bio_in->bi_iter;
1077 ctx->iter_out = bio_out->bi_iter;
1078 ctx->cc_sector = sector + cc->iv_offset;
1079 init_completion(&ctx->restart);
1082 static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
1085 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
1088 static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq)
1090 return (void *)((char *)dmreq - cc->dmreq_start);
1093 static u8 *iv_of_dmreq(struct crypt_config *cc,
1094 struct dm_crypt_request *dmreq)
1096 if (crypt_integrity_aead(cc))
1097 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1098 crypto_aead_alignmask(any_tfm_aead(cc)) + 1);
1100 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1101 crypto_skcipher_alignmask(any_tfm(cc)) + 1);
1104 static u8 *org_iv_of_dmreq(struct crypt_config *cc,
1105 struct dm_crypt_request *dmreq)
1107 return iv_of_dmreq(cc, dmreq) + cc->iv_size;
1110 static __le64 *org_sector_of_dmreq(struct crypt_config *cc,
1111 struct dm_crypt_request *dmreq)
1113 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size;
1114 return (__le64 *) ptr;
1117 static unsigned int *org_tag_of_dmreq(struct crypt_config *cc,
1118 struct dm_crypt_request *dmreq)
1120 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size +
1121 cc->iv_size + sizeof(uint64_t);
1122 return (unsigned int*)ptr;
1125 static void *tag_from_dmreq(struct crypt_config *cc,
1126 struct dm_crypt_request *dmreq)
1128 struct convert_context *ctx = dmreq->ctx;
1129 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1131 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) *
1132 cc->on_disk_tag_size];
1135 static void *iv_tag_from_dmreq(struct crypt_config *cc,
1136 struct dm_crypt_request *dmreq)
1138 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size;
1141 static int crypt_convert_block_aead(struct crypt_config *cc,
1142 struct convert_context *ctx,
1143 struct aead_request *req,
1144 unsigned int tag_offset)
1146 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1147 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
1148 struct dm_crypt_request *dmreq;
1149 u8 *iv, *org_iv, *tag_iv, *tag;
1153 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size);
1155 /* Reject unexpected unaligned bio. */
1156 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
1159 dmreq = dmreq_of_req(cc, req);
1160 dmreq->iv_sector = ctx->cc_sector;
1161 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
1162 dmreq->iv_sector >>= cc->sector_shift;
1165 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
1167 sector = org_sector_of_dmreq(cc, dmreq);
1168 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1170 iv = iv_of_dmreq(cc, dmreq);
1171 org_iv = org_iv_of_dmreq(cc, dmreq);
1172 tag = tag_from_dmreq(cc, dmreq);
1173 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1176 * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
1177 * | (authenticated) | (auth+encryption) | |
1178 * | sector_LE | IV | sector in/out | tag in/out |
1180 sg_init_table(dmreq->sg_in, 4);
1181 sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t));
1182 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size);
1183 sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
1184 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size);
1186 sg_init_table(dmreq->sg_out, 4);
1187 sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t));
1188 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size);
1189 sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
1190 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size);
1192 if (cc->iv_gen_ops) {
1193 /* For READs use IV stored in integrity metadata */
1194 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1195 memcpy(org_iv, tag_iv, cc->iv_size);
1197 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1200 /* Store generated IV in integrity metadata */
1201 if (cc->integrity_iv_size)
1202 memcpy(tag_iv, org_iv, cc->iv_size);
1204 /* Working copy of IV, to be modified in crypto API */
1205 memcpy(iv, org_iv, cc->iv_size);
1208 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size);
1209 if (bio_data_dir(ctx->bio_in) == WRITE) {
1210 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
1211 cc->sector_size, iv);
1212 r = crypto_aead_encrypt(req);
1213 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size)
1214 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0,
1215 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size));
1217 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
1218 cc->sector_size + cc->integrity_tag_size, iv);
1219 r = crypto_aead_decrypt(req);
1222 if (r == -EBADMSG) {
1223 char b[BDEVNAME_SIZE];
1224 DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu", bio_devname(ctx->bio_in, b),
1225 (unsigned long long)le64_to_cpu(*sector));
1228 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
1229 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1231 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1232 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
1237 static int crypt_convert_block_skcipher(struct crypt_config *cc,
1238 struct convert_context *ctx,
1239 struct skcipher_request *req,
1240 unsigned int tag_offset)
1242 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1243 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
1244 struct scatterlist *sg_in, *sg_out;
1245 struct dm_crypt_request *dmreq;
1246 u8 *iv, *org_iv, *tag_iv;
1250 /* Reject unexpected unaligned bio. */
1251 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
1254 dmreq = dmreq_of_req(cc, req);
1255 dmreq->iv_sector = ctx->cc_sector;
1256 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
1257 dmreq->iv_sector >>= cc->sector_shift;
1260 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
1262 iv = iv_of_dmreq(cc, dmreq);
1263 org_iv = org_iv_of_dmreq(cc, dmreq);
1264 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1266 sector = org_sector_of_dmreq(cc, dmreq);
1267 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1269 /* For skcipher we use only the first sg item */
1270 sg_in = &dmreq->sg_in[0];
1271 sg_out = &dmreq->sg_out[0];
1273 sg_init_table(sg_in, 1);
1274 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
1276 sg_init_table(sg_out, 1);
1277 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
1279 if (cc->iv_gen_ops) {
1280 /* For READs use IV stored in integrity metadata */
1281 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1282 memcpy(org_iv, tag_iv, cc->integrity_iv_size);
1284 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1287 /* Store generated IV in integrity metadata */
1288 if (cc->integrity_iv_size)
1289 memcpy(tag_iv, org_iv, cc->integrity_iv_size);
1291 /* Working copy of IV, to be modified in crypto API */
1292 memcpy(iv, org_iv, cc->iv_size);
1295 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv);
1297 if (bio_data_dir(ctx->bio_in) == WRITE)
1298 r = crypto_skcipher_encrypt(req);
1300 r = crypto_skcipher_decrypt(req);
1302 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
1303 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1305 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1306 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
1311 static void kcryptd_async_done(struct crypto_async_request *async_req,
1314 static void crypt_alloc_req_skcipher(struct crypt_config *cc,
1315 struct convert_context *ctx)
1317 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
1320 ctx->r.req = mempool_alloc(&cc->req_pool, GFP_NOIO);
1322 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]);
1325 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1326 * requests if driver request queue is full.
1328 skcipher_request_set_callback(ctx->r.req,
1329 CRYPTO_TFM_REQ_MAY_BACKLOG,
1330 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req));
1333 static void crypt_alloc_req_aead(struct crypt_config *cc,
1334 struct convert_context *ctx)
1336 if (!ctx->r.req_aead)
1337 ctx->r.req_aead = mempool_alloc(&cc->req_pool, GFP_NOIO);
1339 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]);
1342 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1343 * requests if driver request queue is full.
1345 aead_request_set_callback(ctx->r.req_aead,
1346 CRYPTO_TFM_REQ_MAY_BACKLOG,
1347 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead));
1350 static void crypt_alloc_req(struct crypt_config *cc,
1351 struct convert_context *ctx)
1353 if (crypt_integrity_aead(cc))
1354 crypt_alloc_req_aead(cc, ctx);
1356 crypt_alloc_req_skcipher(cc, ctx);
1359 static void crypt_free_req_skcipher(struct crypt_config *cc,
1360 struct skcipher_request *req, struct bio *base_bio)
1362 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1364 if ((struct skcipher_request *)(io + 1) != req)
1365 mempool_free(req, &cc->req_pool);
1368 static void crypt_free_req_aead(struct crypt_config *cc,
1369 struct aead_request *req, struct bio *base_bio)
1371 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1373 if ((struct aead_request *)(io + 1) != req)
1374 mempool_free(req, &cc->req_pool);
1377 static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio)
1379 if (crypt_integrity_aead(cc))
1380 crypt_free_req_aead(cc, req, base_bio);
1382 crypt_free_req_skcipher(cc, req, base_bio);
1386 * Encrypt / decrypt data from one bio to another one (can be the same one)
1388 static blk_status_t crypt_convert(struct crypt_config *cc,
1389 struct convert_context *ctx)
1391 unsigned int tag_offset = 0;
1392 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT;
1395 atomic_set(&ctx->cc_pending, 1);
1397 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
1399 crypt_alloc_req(cc, ctx);
1400 atomic_inc(&ctx->cc_pending);
1402 if (crypt_integrity_aead(cc))
1403 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset);
1405 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset);
1409 * The request was queued by a crypto driver
1410 * but the driver request queue is full, let's wait.
1413 wait_for_completion(&ctx->restart);
1414 reinit_completion(&ctx->restart);
1417 * The request is queued and processed asynchronously,
1418 * completion function kcryptd_async_done() will be called.
1422 ctx->cc_sector += sector_step;
1426 * The request was already processed (synchronously).
1429 atomic_dec(&ctx->cc_pending);
1430 ctx->cc_sector += sector_step;
1435 * There was a data integrity error.
1438 atomic_dec(&ctx->cc_pending);
1439 return BLK_STS_PROTECTION;
1441 * There was an error while processing the request.
1444 atomic_dec(&ctx->cc_pending);
1445 return BLK_STS_IOERR;
1452 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1455 * Generate a new unfragmented bio with the given size
1456 * This should never violate the device limitations (but only because
1457 * max_segment_size is being constrained to PAGE_SIZE).
1459 * This function may be called concurrently. If we allocate from the mempool
1460 * concurrently, there is a possibility of deadlock. For example, if we have
1461 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1462 * the mempool concurrently, it may deadlock in a situation where both processes
1463 * have allocated 128 pages and the mempool is exhausted.
1465 * In order to avoid this scenario we allocate the pages under a mutex.
1467 * In order to not degrade performance with excessive locking, we try
1468 * non-blocking allocations without a mutex first but on failure we fallback
1469 * to blocking allocations with a mutex.
1471 static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
1473 struct crypt_config *cc = io->cc;
1475 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1476 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
1477 unsigned i, len, remaining_size;
1481 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
1482 mutex_lock(&cc->bio_alloc_lock);
1484 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, &cc->bs);
1488 clone_init(io, clone);
1490 remaining_size = size;
1492 for (i = 0; i < nr_iovecs; i++) {
1493 page = mempool_alloc(&cc->page_pool, gfp_mask);
1495 crypt_free_buffer_pages(cc, clone);
1497 gfp_mask |= __GFP_DIRECT_RECLAIM;
1501 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
1503 bio_add_page(clone, page, len, 0);
1505 remaining_size -= len;
1508 /* Allocate space for integrity tags */
1509 if (dm_crypt_integrity_io_alloc(io, clone)) {
1510 crypt_free_buffer_pages(cc, clone);
1515 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
1516 mutex_unlock(&cc->bio_alloc_lock);
1521 static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
1524 struct bvec_iter_all iter_all;
1526 bio_for_each_segment_all(bv, clone, iter_all) {
1527 BUG_ON(!bv->bv_page);
1528 mempool_free(bv->bv_page, &cc->page_pool);
1532 static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1533 struct bio *bio, sector_t sector)
1537 io->sector = sector;
1539 io->ctx.r.req = NULL;
1540 io->integrity_metadata = NULL;
1541 io->integrity_metadata_from_pool = false;
1542 atomic_set(&io->io_pending, 0);
1545 static void crypt_inc_pending(struct dm_crypt_io *io)
1547 atomic_inc(&io->io_pending);
1551 * One of the bios was finished. Check for completion of
1552 * the whole request and correctly clean up the buffer.
1554 static void crypt_dec_pending(struct dm_crypt_io *io)
1556 struct crypt_config *cc = io->cc;
1557 struct bio *base_bio = io->base_bio;
1558 blk_status_t error = io->error;
1560 if (!atomic_dec_and_test(&io->io_pending))
1564 crypt_free_req(cc, io->ctx.r.req, base_bio);
1566 if (unlikely(io->integrity_metadata_from_pool))
1567 mempool_free(io->integrity_metadata, &io->cc->tag_pool);
1569 kfree(io->integrity_metadata);
1571 base_bio->bi_status = error;
1572 bio_endio(base_bio);
1576 * kcryptd/kcryptd_io:
1578 * Needed because it would be very unwise to do decryption in an
1579 * interrupt context.
1581 * kcryptd performs the actual encryption or decryption.
1583 * kcryptd_io performs the IO submission.
1585 * They must be separated as otherwise the final stages could be
1586 * starved by new requests which can block in the first stages due
1587 * to memory allocation.
1589 * The work is done per CPU global for all dm-crypt instances.
1590 * They should not depend on each other and do not block.
1592 static void crypt_endio(struct bio *clone)
1594 struct dm_crypt_io *io = clone->bi_private;
1595 struct crypt_config *cc = io->cc;
1596 unsigned rw = bio_data_dir(clone);
1600 * free the processed pages
1603 crypt_free_buffer_pages(cc, clone);
1605 error = clone->bi_status;
1608 if (rw == READ && !error) {
1609 kcryptd_queue_crypt(io);
1613 if (unlikely(error))
1616 crypt_dec_pending(io);
1619 static void clone_init(struct dm_crypt_io *io, struct bio *clone)
1621 struct crypt_config *cc = io->cc;
1623 clone->bi_private = io;
1624 clone->bi_end_io = crypt_endio;
1625 bio_set_dev(clone, cc->dev->bdev);
1626 clone->bi_opf = io->base_bio->bi_opf;
1629 static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
1631 struct crypt_config *cc = io->cc;
1635 * We need the original biovec array in order to decrypt
1636 * the whole bio data *afterwards* -- thanks to immutable
1637 * biovecs we don't need to worry about the block layer
1638 * modifying the biovec array; so leverage bio_clone_fast().
1640 clone = bio_clone_fast(io->base_bio, gfp, &cc->bs);
1644 crypt_inc_pending(io);
1646 clone_init(io, clone);
1647 clone->bi_iter.bi_sector = cc->start + io->sector;
1649 if (dm_crypt_integrity_io_alloc(io, clone)) {
1650 crypt_dec_pending(io);
1655 generic_make_request(clone);
1659 static void kcryptd_io_read_work(struct work_struct *work)
1661 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1663 crypt_inc_pending(io);
1664 if (kcryptd_io_read(io, GFP_NOIO))
1665 io->error = BLK_STS_RESOURCE;
1666 crypt_dec_pending(io);
1669 static void kcryptd_queue_read(struct dm_crypt_io *io)
1671 struct crypt_config *cc = io->cc;
1673 INIT_WORK(&io->work, kcryptd_io_read_work);
1674 queue_work(cc->io_queue, &io->work);
1677 static void kcryptd_io_write(struct dm_crypt_io *io)
1679 struct bio *clone = io->ctx.bio_out;
1681 generic_make_request(clone);
1684 #define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1686 static int dmcrypt_write(void *data)
1688 struct crypt_config *cc = data;
1689 struct dm_crypt_io *io;
1692 struct rb_root write_tree;
1693 struct blk_plug plug;
1695 spin_lock_irq(&cc->write_thread_lock);
1698 if (!RB_EMPTY_ROOT(&cc->write_tree))
1701 set_current_state(TASK_INTERRUPTIBLE);
1703 spin_unlock_irq(&cc->write_thread_lock);
1705 if (unlikely(kthread_should_stop())) {
1706 set_current_state(TASK_RUNNING);
1712 set_current_state(TASK_RUNNING);
1713 spin_lock_irq(&cc->write_thread_lock);
1714 goto continue_locked;
1717 write_tree = cc->write_tree;
1718 cc->write_tree = RB_ROOT;
1719 spin_unlock_irq(&cc->write_thread_lock);
1721 BUG_ON(rb_parent(write_tree.rb_node));
1724 * Note: we cannot walk the tree here with rb_next because
1725 * the structures may be freed when kcryptd_io_write is called.
1727 blk_start_plug(&plug);
1729 io = crypt_io_from_node(rb_first(&write_tree));
1730 rb_erase(&io->rb_node, &write_tree);
1731 kcryptd_io_write(io);
1732 } while (!RB_EMPTY_ROOT(&write_tree));
1733 blk_finish_plug(&plug);
1738 static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
1740 struct bio *clone = io->ctx.bio_out;
1741 struct crypt_config *cc = io->cc;
1742 unsigned long flags;
1744 struct rb_node **rbp, *parent;
1746 if (unlikely(io->error)) {
1747 crypt_free_buffer_pages(cc, clone);
1749 crypt_dec_pending(io);
1753 /* crypt_convert should have filled the clone bio */
1754 BUG_ON(io->ctx.iter_out.bi_size);
1756 clone->bi_iter.bi_sector = cc->start + io->sector;
1758 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1759 generic_make_request(clone);
1763 spin_lock_irqsave(&cc->write_thread_lock, flags);
1764 if (RB_EMPTY_ROOT(&cc->write_tree))
1765 wake_up_process(cc->write_thread);
1766 rbp = &cc->write_tree.rb_node;
1768 sector = io->sector;
1771 if (sector < crypt_io_from_node(parent)->sector)
1772 rbp = &(*rbp)->rb_left;
1774 rbp = &(*rbp)->rb_right;
1776 rb_link_node(&io->rb_node, parent, rbp);
1777 rb_insert_color(&io->rb_node, &cc->write_tree);
1778 spin_unlock_irqrestore(&cc->write_thread_lock, flags);
1781 static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
1783 struct crypt_config *cc = io->cc;
1786 sector_t sector = io->sector;
1790 * Prevent io from disappearing until this function completes.
1792 crypt_inc_pending(io);
1793 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
1795 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1796 if (unlikely(!clone)) {
1797 io->error = BLK_STS_IOERR;
1801 io->ctx.bio_out = clone;
1802 io->ctx.iter_out = clone->bi_iter;
1804 sector += bio_sectors(clone);
1806 crypt_inc_pending(io);
1807 r = crypt_convert(cc, &io->ctx);
1810 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1812 /* Encryption was already finished, submit io now */
1813 if (crypt_finished) {
1814 kcryptd_crypt_write_io_submit(io, 0);
1815 io->sector = sector;
1819 crypt_dec_pending(io);
1822 static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
1824 crypt_dec_pending(io);
1827 static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
1829 struct crypt_config *cc = io->cc;
1832 crypt_inc_pending(io);
1834 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
1837 r = crypt_convert(cc, &io->ctx);
1841 if (atomic_dec_and_test(&io->ctx.cc_pending))
1842 kcryptd_crypt_read_done(io);
1844 crypt_dec_pending(io);
1847 static void kcryptd_async_done(struct crypto_async_request *async_req,
1850 struct dm_crypt_request *dmreq = async_req->data;
1851 struct convert_context *ctx = dmreq->ctx;
1852 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1853 struct crypt_config *cc = io->cc;
1856 * A request from crypto driver backlog is going to be processed now,
1857 * finish the completion and continue in crypt_convert().
1858 * (Callback will be called for the second time for this request.)
1860 if (error == -EINPROGRESS) {
1861 complete(&ctx->restart);
1865 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
1866 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq);
1868 if (error == -EBADMSG) {
1869 char b[BDEVNAME_SIZE];
1870 DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu", bio_devname(ctx->bio_in, b),
1871 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
1872 io->error = BLK_STS_PROTECTION;
1873 } else if (error < 0)
1874 io->error = BLK_STS_IOERR;
1876 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
1878 if (!atomic_dec_and_test(&ctx->cc_pending))
1881 if (bio_data_dir(io->base_bio) == READ)
1882 kcryptd_crypt_read_done(io);
1884 kcryptd_crypt_write_io_submit(io, 1);
1887 static void kcryptd_crypt(struct work_struct *work)
1889 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1891 if (bio_data_dir(io->base_bio) == READ)
1892 kcryptd_crypt_read_convert(io);
1894 kcryptd_crypt_write_convert(io);
1897 static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1899 struct crypt_config *cc = io->cc;
1901 INIT_WORK(&io->work, kcryptd_crypt);
1902 queue_work(cc->crypt_queue, &io->work);
1905 static void crypt_free_tfms_aead(struct crypt_config *cc)
1907 if (!cc->cipher_tfm.tfms_aead)
1910 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1911 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]);
1912 cc->cipher_tfm.tfms_aead[0] = NULL;
1915 kfree(cc->cipher_tfm.tfms_aead);
1916 cc->cipher_tfm.tfms_aead = NULL;
1919 static void crypt_free_tfms_skcipher(struct crypt_config *cc)
1923 if (!cc->cipher_tfm.tfms)
1926 for (i = 0; i < cc->tfms_count; i++)
1927 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) {
1928 crypto_free_skcipher(cc->cipher_tfm.tfms[i]);
1929 cc->cipher_tfm.tfms[i] = NULL;
1932 kfree(cc->cipher_tfm.tfms);
1933 cc->cipher_tfm.tfms = NULL;
1936 static void crypt_free_tfms(struct crypt_config *cc)
1938 if (crypt_integrity_aead(cc))
1939 crypt_free_tfms_aead(cc);
1941 crypt_free_tfms_skcipher(cc);
1944 static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode)
1949 cc->cipher_tfm.tfms = kcalloc(cc->tfms_count,
1950 sizeof(struct crypto_skcipher *),
1952 if (!cc->cipher_tfm.tfms)
1955 for (i = 0; i < cc->tfms_count; i++) {
1956 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
1957 if (IS_ERR(cc->cipher_tfm.tfms[i])) {
1958 err = PTR_ERR(cc->cipher_tfm.tfms[i]);
1959 crypt_free_tfms(cc);
1965 * dm-crypt performance can vary greatly depending on which crypto
1966 * algorithm implementation is used. Help people debug performance
1967 * problems by logging the ->cra_driver_name.
1969 DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode,
1970 crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name);
1974 static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
1978 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
1979 if (!cc->cipher_tfm.tfms)
1982 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
1983 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1984 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
1985 crypt_free_tfms(cc);
1989 DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode,
1990 crypto_aead_alg(any_tfm_aead(cc))->base.cra_driver_name);
1994 static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
1996 if (crypt_integrity_aead(cc))
1997 return crypt_alloc_tfms_aead(cc, ciphermode);
1999 return crypt_alloc_tfms_skcipher(cc, ciphermode);
2002 static unsigned crypt_subkey_size(struct crypt_config *cc)
2004 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
2007 static unsigned crypt_authenckey_size(struct crypt_config *cc)
2009 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
2013 * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
2014 * the key must be for some reason in special format.
2015 * This funcion converts cc->key to this special format.
2017 static void crypt_copy_authenckey(char *p, const void *key,
2018 unsigned enckeylen, unsigned authkeylen)
2020 struct crypto_authenc_key_param *param;
2023 rta = (struct rtattr *)p;
2024 param = RTA_DATA(rta);
2025 param->enckeylen = cpu_to_be32(enckeylen);
2026 rta->rta_len = RTA_LENGTH(sizeof(*param));
2027 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
2028 p += RTA_SPACE(sizeof(*param));
2029 memcpy(p, key + enckeylen, authkeylen);
2031 memcpy(p, key, enckeylen);
2034 static int crypt_setkey(struct crypt_config *cc)
2036 unsigned subkey_size;
2039 /* Ignore extra keys (which are used for IV etc) */
2040 subkey_size = crypt_subkey_size(cc);
2042 if (crypt_integrity_hmac(cc)) {
2043 if (subkey_size < cc->key_mac_size)
2046 crypt_copy_authenckey(cc->authenc_key, cc->key,
2047 subkey_size - cc->key_mac_size,
2051 for (i = 0; i < cc->tfms_count; i++) {
2052 if (crypt_integrity_hmac(cc))
2053 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
2054 cc->authenc_key, crypt_authenckey_size(cc));
2055 else if (crypt_integrity_aead(cc))
2056 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
2057 cc->key + (i * subkey_size),
2060 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i],
2061 cc->key + (i * subkey_size),
2067 if (crypt_integrity_hmac(cc))
2068 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc));
2075 static bool contains_whitespace(const char *str)
2078 if (isspace(*str++))
2083 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2085 char *new_key_string, *key_desc;
2088 const struct user_key_payload *ukp;
2091 * Reject key_string with whitespace. dm core currently lacks code for
2092 * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
2094 if (contains_whitespace(key_string)) {
2095 DMERR("whitespace chars not allowed in key string");
2099 /* look for next ':' separating key_type from key_description */
2100 key_desc = strpbrk(key_string, ":");
2101 if (!key_desc || key_desc == key_string || !strlen(key_desc + 1))
2104 if (strncmp(key_string, "logon:", key_desc - key_string + 1) &&
2105 strncmp(key_string, "user:", key_desc - key_string + 1))
2108 new_key_string = kstrdup(key_string, GFP_KERNEL);
2109 if (!new_key_string)
2112 key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user,
2113 key_desc + 1, NULL);
2115 kzfree(new_key_string);
2116 return PTR_ERR(key);
2119 down_read(&key->sem);
2121 ukp = user_key_payload_locked(key);
2125 kzfree(new_key_string);
2126 return -EKEYREVOKED;
2129 if (cc->key_size != ukp->datalen) {
2132 kzfree(new_key_string);
2136 memcpy(cc->key, ukp->data, cc->key_size);
2141 /* clear the flag since following operations may invalidate previously valid key */
2142 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2144 ret = crypt_setkey(cc);
2147 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2148 kzfree(cc->key_string);
2149 cc->key_string = new_key_string;
2151 kzfree(new_key_string);
2156 static int get_key_size(char **key_string)
2161 if (*key_string[0] != ':')
2162 return strlen(*key_string) >> 1;
2164 /* look for next ':' in key string */
2165 colon = strpbrk(*key_string + 1, ":");
2169 if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':')
2172 *key_string = colon;
2174 /* remaining key string should be :<logon|user>:<key_desc> */
2181 static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2186 static int get_key_size(char **key_string)
2188 return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1;
2193 static int crypt_set_key(struct crypt_config *cc, char *key)
2196 int key_string_len = strlen(key);
2198 /* Hyphen (which gives a key_size of zero) means there is no key. */
2199 if (!cc->key_size && strcmp(key, "-"))
2202 /* ':' means the key is in kernel keyring, short-circuit normal key processing */
2203 if (key[0] == ':') {
2204 r = crypt_set_keyring_key(cc, key + 1);
2208 /* clear the flag since following operations may invalidate previously valid key */
2209 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2211 /* wipe references to any kernel keyring key */
2212 kzfree(cc->key_string);
2213 cc->key_string = NULL;
2215 /* Decode key from its hex representation. */
2216 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0)
2219 r = crypt_setkey(cc);
2221 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2224 /* Hex key string not needed after here, so wipe it. */
2225 memset(key, '0', key_string_len);
2230 static int crypt_wipe_key(struct crypt_config *cc)
2234 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2235 get_random_bytes(&cc->key, cc->key_size);
2237 /* Wipe IV private keys */
2238 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
2239 r = cc->iv_gen_ops->wipe(cc);
2244 kzfree(cc->key_string);
2245 cc->key_string = NULL;
2246 r = crypt_setkey(cc);
2247 memset(&cc->key, 0, cc->key_size * sizeof(u8));
2252 static void crypt_calculate_pages_per_client(void)
2254 unsigned long pages = (totalram_pages() - totalhigh_pages()) * DM_CRYPT_MEMORY_PERCENT / 100;
2256 if (!dm_crypt_clients_n)
2259 pages /= dm_crypt_clients_n;
2260 if (pages < DM_CRYPT_MIN_PAGES_PER_CLIENT)
2261 pages = DM_CRYPT_MIN_PAGES_PER_CLIENT;
2262 dm_crypt_pages_per_client = pages;
2265 static void *crypt_page_alloc(gfp_t gfp_mask, void *pool_data)
2267 struct crypt_config *cc = pool_data;
2270 if (unlikely(percpu_counter_compare(&cc->n_allocated_pages, dm_crypt_pages_per_client) >= 0) &&
2271 likely(gfp_mask & __GFP_NORETRY))
2274 page = alloc_page(gfp_mask);
2275 if (likely(page != NULL))
2276 percpu_counter_add(&cc->n_allocated_pages, 1);
2281 static void crypt_page_free(void *page, void *pool_data)
2283 struct crypt_config *cc = pool_data;
2286 percpu_counter_sub(&cc->n_allocated_pages, 1);
2289 static void crypt_dtr(struct dm_target *ti)
2291 struct crypt_config *cc = ti->private;
2298 if (cc->write_thread)
2299 kthread_stop(cc->write_thread);
2302 destroy_workqueue(cc->io_queue);
2303 if (cc->crypt_queue)
2304 destroy_workqueue(cc->crypt_queue);
2306 crypt_free_tfms(cc);
2308 bioset_exit(&cc->bs);
2310 mempool_exit(&cc->page_pool);
2311 mempool_exit(&cc->req_pool);
2312 mempool_exit(&cc->tag_pool);
2314 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0);
2315 percpu_counter_destroy(&cc->n_allocated_pages);
2317 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
2318 cc->iv_gen_ops->dtr(cc);
2321 dm_put_device(ti, cc->dev);
2324 kzfree(cc->cipher_string);
2325 kzfree(cc->key_string);
2326 kzfree(cc->cipher_auth);
2327 kzfree(cc->authenc_key);
2329 mutex_destroy(&cc->bio_alloc_lock);
2331 /* Must zero key material before freeing */
2334 spin_lock(&dm_crypt_clients_lock);
2335 WARN_ON(!dm_crypt_clients_n);
2336 dm_crypt_clients_n--;
2337 crypt_calculate_pages_per_client();
2338 spin_unlock(&dm_crypt_clients_lock);
2341 static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
2343 struct crypt_config *cc = ti->private;
2345 if (crypt_integrity_aead(cc))
2346 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2348 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2351 /* at least a 64 bit sector number should fit in our buffer */
2352 cc->iv_size = max(cc->iv_size,
2353 (unsigned int)(sizeof(u64) / sizeof(u8)));
2355 DMWARN("Selected cipher does not support IVs");
2359 /* Choose ivmode, see comments at iv code. */
2361 cc->iv_gen_ops = NULL;
2362 else if (strcmp(ivmode, "plain") == 0)
2363 cc->iv_gen_ops = &crypt_iv_plain_ops;
2364 else if (strcmp(ivmode, "plain64") == 0)
2365 cc->iv_gen_ops = &crypt_iv_plain64_ops;
2366 else if (strcmp(ivmode, "plain64be") == 0)
2367 cc->iv_gen_ops = &crypt_iv_plain64be_ops;
2368 else if (strcmp(ivmode, "essiv") == 0)
2369 cc->iv_gen_ops = &crypt_iv_essiv_ops;
2370 else if (strcmp(ivmode, "benbi") == 0)
2371 cc->iv_gen_ops = &crypt_iv_benbi_ops;
2372 else if (strcmp(ivmode, "null") == 0)
2373 cc->iv_gen_ops = &crypt_iv_null_ops;
2374 else if (strcmp(ivmode, "eboiv") == 0)
2375 cc->iv_gen_ops = &crypt_iv_eboiv_ops;
2376 else if (strcmp(ivmode, "lmk") == 0) {
2377 cc->iv_gen_ops = &crypt_iv_lmk_ops;
2379 * Version 2 and 3 is recognised according
2380 * to length of provided multi-key string.
2381 * If present (version 3), last key is used as IV seed.
2382 * All keys (including IV seed) are always the same size.
2384 if (cc->key_size % cc->key_parts) {
2386 cc->key_extra_size = cc->key_size / cc->key_parts;
2388 } else if (strcmp(ivmode, "tcw") == 0) {
2389 cc->iv_gen_ops = &crypt_iv_tcw_ops;
2390 cc->key_parts += 2; /* IV + whitening */
2391 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
2392 } else if (strcmp(ivmode, "random") == 0) {
2393 cc->iv_gen_ops = &crypt_iv_random_ops;
2394 /* Need storage space in integrity fields. */
2395 cc->integrity_iv_size = cc->iv_size;
2397 ti->error = "Invalid IV mode";
2405 * Workaround to parse cipher algorithm from crypto API spec.
2406 * The cc->cipher is currently used only in ESSIV.
2407 * This should be probably done by crypto-api calls (once available...)
2409 static int crypt_ctr_blkdev_cipher(struct crypt_config *cc)
2411 const char *alg_name = NULL;
2414 if (crypt_integrity_aead(cc)) {
2415 alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc)));
2418 if (crypt_integrity_hmac(cc)) {
2419 alg_name = strchr(alg_name, ',');
2425 alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc)));
2430 start = strchr(alg_name, '(');
2431 end = strchr(alg_name, ')');
2433 if (!start && !end) {
2434 cc->cipher = kstrdup(alg_name, GFP_KERNEL);
2435 return cc->cipher ? 0 : -ENOMEM;
2438 if (!start || !end || ++start >= end)
2441 cc->cipher = kzalloc(end - start + 1, GFP_KERNEL);
2445 strncpy(cc->cipher, start, end - start);
2451 * Workaround to parse HMAC algorithm from AEAD crypto API spec.
2452 * The HMAC is needed to calculate tag size (HMAC digest size).
2453 * This should be probably done by crypto-api calls (once available...)
2455 static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
2457 char *start, *end, *mac_alg = NULL;
2458 struct crypto_ahash *mac;
2460 if (!strstarts(cipher_api, "authenc("))
2463 start = strchr(cipher_api, '(');
2464 end = strchr(cipher_api, ',');
2465 if (!start || !end || ++start > end)
2468 mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
2471 strncpy(mac_alg, start, end - start);
2473 mac = crypto_alloc_ahash(mac_alg, 0, 0);
2477 return PTR_ERR(mac);
2479 cc->key_mac_size = crypto_ahash_digestsize(mac);
2480 crypto_free_ahash(mac);
2482 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
2483 if (!cc->authenc_key)
2489 static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key,
2490 char **ivmode, char **ivopts)
2492 struct crypt_config *cc = ti->private;
2493 char *tmp, *cipher_api;
2499 * New format (capi: prefix)
2500 * capi:cipher_api_spec-iv:ivopts
2502 tmp = &cipher_in[strlen("capi:")];
2504 /* Separate IV options if present, it can contain another '-' in hash name */
2505 *ivopts = strrchr(tmp, ':');
2511 *ivmode = strrchr(tmp, '-');
2516 /* The rest is crypto API spec */
2519 if (*ivmode && !strcmp(*ivmode, "lmk"))
2520 cc->tfms_count = 64;
2522 cc->key_parts = cc->tfms_count;
2524 /* Allocate cipher */
2525 ret = crypt_alloc_tfms(cc, cipher_api);
2527 ti->error = "Error allocating crypto tfm";
2531 /* Alloc AEAD, can be used only in new format. */
2532 if (crypt_integrity_aead(cc)) {
2533 ret = crypt_ctr_auth_cipher(cc, cipher_api);
2535 ti->error = "Invalid AEAD cipher spec";
2538 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2540 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2542 ret = crypt_ctr_blkdev_cipher(cc);
2544 ti->error = "Cannot allocate cipher string";
2551 static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
2552 char **ivmode, char **ivopts)
2554 struct crypt_config *cc = ti->private;
2555 char *tmp, *cipher, *chainmode, *keycount;
2556 char *cipher_api = NULL;
2560 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) {
2561 ti->error = "Bad cipher specification";
2566 * Legacy dm-crypt cipher specification
2567 * cipher[:keycount]-mode-iv:ivopts
2570 keycount = strsep(&tmp, "-");
2571 cipher = strsep(&keycount, ":");
2575 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
2576 !is_power_of_2(cc->tfms_count)) {
2577 ti->error = "Bad cipher key count specification";
2580 cc->key_parts = cc->tfms_count;
2582 cc->cipher = kstrdup(cipher, GFP_KERNEL);
2586 chainmode = strsep(&tmp, "-");
2587 *ivmode = strsep(&tmp, ":");
2591 * For compatibility with the original dm-crypt mapping format, if
2592 * only the cipher name is supplied, use cbc-plain.
2594 if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) {
2599 if (strcmp(chainmode, "ecb") && !*ivmode) {
2600 ti->error = "IV mechanism required";
2604 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
2608 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
2609 "%s(%s)", chainmode, cipher);
2615 /* Allocate cipher */
2616 ret = crypt_alloc_tfms(cc, cipher_api);
2618 ti->error = "Error allocating crypto tfm";
2626 ti->error = "Cannot allocate cipher strings";
2630 static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key)
2632 struct crypt_config *cc = ti->private;
2633 char *ivmode = NULL, *ivopts = NULL;
2636 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
2637 if (!cc->cipher_string) {
2638 ti->error = "Cannot allocate cipher strings";
2642 if (strstarts(cipher_in, "capi:"))
2643 ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts);
2645 ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts);
2650 ret = crypt_ctr_ivmode(ti, ivmode);
2654 /* Initialize and set key */
2655 ret = crypt_set_key(cc, key);
2657 ti->error = "Error decoding and setting key";
2662 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
2663 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
2665 ti->error = "Error creating IV";
2670 /* Initialize IV (set keys for ESSIV etc) */
2671 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
2672 ret = cc->iv_gen_ops->init(cc);
2674 ti->error = "Error initialising IV";
2679 /* wipe the kernel key payload copy */
2681 memset(cc->key, 0, cc->key_size * sizeof(u8));
2686 static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
2688 struct crypt_config *cc = ti->private;
2689 struct dm_arg_set as;
2690 static const struct dm_arg _args[] = {
2691 {0, 6, "Invalid number of feature args"},
2693 unsigned int opt_params, val;
2694 const char *opt_string, *sval;
2698 /* Optional parameters */
2702 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2706 while (opt_params--) {
2707 opt_string = dm_shift_arg(&as);
2709 ti->error = "Not enough feature arguments";
2713 if (!strcasecmp(opt_string, "allow_discards"))
2714 ti->num_discard_bios = 1;
2716 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
2717 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
2719 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
2720 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
2721 else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
2722 if (val == 0 || val > MAX_TAG_SIZE) {
2723 ti->error = "Invalid integrity arguments";
2726 cc->on_disk_tag_size = val;
2727 sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
2728 if (!strcasecmp(sval, "aead")) {
2729 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
2730 } else if (strcasecmp(sval, "none")) {
2731 ti->error = "Unknown integrity profile";
2735 cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
2736 if (!cc->cipher_auth)
2738 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) {
2739 if (cc->sector_size < (1 << SECTOR_SHIFT) ||
2740 cc->sector_size > 4096 ||
2741 (cc->sector_size & (cc->sector_size - 1))) {
2742 ti->error = "Invalid feature value for sector_size";
2745 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) {
2746 ti->error = "Device size is not multiple of sector_size feature";
2749 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT;
2750 } else if (!strcasecmp(opt_string, "iv_large_sectors"))
2751 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
2753 ti->error = "Invalid feature arguments";
2762 * Construct an encryption mapping:
2763 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
2765 static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2767 struct crypt_config *cc;
2768 const char *devname = dm_table_device_name(ti->table);
2770 unsigned int align_mask;
2771 unsigned long long tmpll;
2773 size_t iv_size_padding, additional_req_size;
2777 ti->error = "Not enough arguments";
2781 key_size = get_key_size(&argv[1]);
2783 ti->error = "Cannot parse key size";
2787 cc = kzalloc(struct_size(cc, key, key_size), GFP_KERNEL);
2789 ti->error = "Cannot allocate encryption context";
2792 cc->key_size = key_size;
2793 cc->sector_size = (1 << SECTOR_SHIFT);
2794 cc->sector_shift = 0;
2798 spin_lock(&dm_crypt_clients_lock);
2799 dm_crypt_clients_n++;
2800 crypt_calculate_pages_per_client();
2801 spin_unlock(&dm_crypt_clients_lock);
2803 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL);
2807 /* Optional parameters need to be read before cipher constructor */
2809 ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
2814 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
2818 if (crypt_integrity_aead(cc)) {
2819 cc->dmreq_start = sizeof(struct aead_request);
2820 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
2821 align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
2823 cc->dmreq_start = sizeof(struct skcipher_request);
2824 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
2825 align_mask = crypto_skcipher_alignmask(any_tfm(cc));
2827 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
2829 if (align_mask < CRYPTO_MINALIGN) {
2830 /* Allocate the padding exactly */
2831 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
2835 * If the cipher requires greater alignment than kmalloc
2836 * alignment, we don't know the exact position of the
2837 * initialization vector. We must assume worst case.
2839 iv_size_padding = align_mask;
2842 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
2843 additional_req_size = sizeof(struct dm_crypt_request) +
2844 iv_size_padding + cc->iv_size +
2847 sizeof(unsigned int);
2849 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size);
2851 ti->error = "Cannot allocate crypt request mempool";
2855 cc->per_bio_data_size = ti->per_io_data_size =
2856 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
2857 ARCH_KMALLOC_MINALIGN);
2859 ret = mempool_init(&cc->page_pool, BIO_MAX_PAGES, crypt_page_alloc, crypt_page_free, cc);
2861 ti->error = "Cannot allocate page mempool";
2865 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS);
2867 ti->error = "Cannot allocate crypt bioset";
2871 mutex_init(&cc->bio_alloc_lock);
2874 if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
2875 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
2876 ti->error = "Invalid iv_offset sector";
2879 cc->iv_offset = tmpll;
2881 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
2883 ti->error = "Device lookup failed";
2888 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
2889 ti->error = "Invalid device sector";
2894 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
2895 ret = crypt_integrity_ctr(cc, ti);
2899 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
2900 if (!cc->tag_pool_max_sectors)
2901 cc->tag_pool_max_sectors = 1;
2903 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS,
2904 cc->tag_pool_max_sectors * cc->on_disk_tag_size);
2906 ti->error = "Cannot allocate integrity tags mempool";
2910 cc->tag_pool_max_sectors <<= cc->sector_shift;
2914 cc->io_queue = alloc_workqueue("kcryptd_io/%s",
2915 WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM,
2917 if (!cc->io_queue) {
2918 ti->error = "Couldn't create kcryptd io queue";
2922 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2923 cc->crypt_queue = alloc_workqueue("kcryptd/%s",
2924 WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM,
2927 cc->crypt_queue = alloc_workqueue("kcryptd/%s",
2928 WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
2929 num_online_cpus(), devname);
2930 if (!cc->crypt_queue) {
2931 ti->error = "Couldn't create kcryptd queue";
2935 spin_lock_init(&cc->write_thread_lock);
2936 cc->write_tree = RB_ROOT;
2938 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write/%s", devname);
2939 if (IS_ERR(cc->write_thread)) {
2940 ret = PTR_ERR(cc->write_thread);
2941 cc->write_thread = NULL;
2942 ti->error = "Couldn't spawn write thread";
2945 wake_up_process(cc->write_thread);
2947 ti->num_flush_bios = 1;
2956 static int crypt_map(struct dm_target *ti, struct bio *bio)
2958 struct dm_crypt_io *io;
2959 struct crypt_config *cc = ti->private;
2962 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
2963 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
2964 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
2966 if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
2967 bio_op(bio) == REQ_OP_DISCARD)) {
2968 bio_set_dev(bio, cc->dev->bdev);
2969 if (bio_sectors(bio))
2970 bio->bi_iter.bi_sector = cc->start +
2971 dm_target_offset(ti, bio->bi_iter.bi_sector);
2972 return DM_MAPIO_REMAPPED;
2976 * Check if bio is too large, split as needed.
2978 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
2979 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
2980 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
2983 * Ensure that bio is a multiple of internal sector encryption size
2984 * and is aligned to this size as defined in IO hints.
2986 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
2987 return DM_MAPIO_KILL;
2989 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
2990 return DM_MAPIO_KILL;
2992 io = dm_per_bio_data(bio, cc->per_bio_data_size);
2993 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
2995 if (cc->on_disk_tag_size) {
2996 unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift);
2998 if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
2999 unlikely(!(io->integrity_metadata = kmalloc(tag_len,
3000 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
3001 if (bio_sectors(bio) > cc->tag_pool_max_sectors)
3002 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
3003 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO);
3004 io->integrity_metadata_from_pool = true;
3008 if (crypt_integrity_aead(cc))
3009 io->ctx.r.req_aead = (struct aead_request *)(io + 1);
3011 io->ctx.r.req = (struct skcipher_request *)(io + 1);
3013 if (bio_data_dir(io->base_bio) == READ) {
3014 if (kcryptd_io_read(io, GFP_NOWAIT))
3015 kcryptd_queue_read(io);
3017 kcryptd_queue_crypt(io);
3019 return DM_MAPIO_SUBMITTED;
3022 static void crypt_status(struct dm_target *ti, status_type_t type,
3023 unsigned status_flags, char *result, unsigned maxlen)
3025 struct crypt_config *cc = ti->private;
3027 int num_feature_args = 0;
3030 case STATUSTYPE_INFO:
3034 case STATUSTYPE_TABLE:
3035 DMEMIT("%s ", cc->cipher_string);
3037 if (cc->key_size > 0) {
3039 DMEMIT(":%u:%s", cc->key_size, cc->key_string);
3041 for (i = 0; i < cc->key_size; i++)
3042 DMEMIT("%02x", cc->key[i]);
3046 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
3047 cc->dev->name, (unsigned long long)cc->start);
3049 num_feature_args += !!ti->num_discard_bios;
3050 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
3051 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
3052 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT);
3053 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
3054 if (cc->on_disk_tag_size)
3056 if (num_feature_args) {
3057 DMEMIT(" %d", num_feature_args);
3058 if (ti->num_discard_bios)
3059 DMEMIT(" allow_discards");
3060 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
3061 DMEMIT(" same_cpu_crypt");
3062 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
3063 DMEMIT(" submit_from_crypt_cpus");
3064 if (cc->on_disk_tag_size)
3065 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
3066 if (cc->sector_size != (1 << SECTOR_SHIFT))
3067 DMEMIT(" sector_size:%d", cc->sector_size);
3068 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
3069 DMEMIT(" iv_large_sectors");
3076 static void crypt_postsuspend(struct dm_target *ti)
3078 struct crypt_config *cc = ti->private;
3080 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
3083 static int crypt_preresume(struct dm_target *ti)
3085 struct crypt_config *cc = ti->private;
3087 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
3088 DMERR("aborting resume - crypt key is not set.");
3095 static void crypt_resume(struct dm_target *ti)
3097 struct crypt_config *cc = ti->private;
3099 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
3102 /* Message interface
3106 static int crypt_message(struct dm_target *ti, unsigned argc, char **argv,
3107 char *result, unsigned maxlen)
3109 struct crypt_config *cc = ti->private;
3110 int key_size, ret = -EINVAL;
3115 if (!strcasecmp(argv[0], "key")) {
3116 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
3117 DMWARN("not suspended during key manipulation.");
3120 if (argc == 3 && !strcasecmp(argv[1], "set")) {
3121 /* The key size may not be changed. */
3122 key_size = get_key_size(&argv[2]);
3123 if (key_size < 0 || cc->key_size != key_size) {
3124 memset(argv[2], '0', strlen(argv[2]));
3128 ret = crypt_set_key(cc, argv[2]);
3131 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
3132 ret = cc->iv_gen_ops->init(cc);
3133 /* wipe the kernel key payload copy */
3135 memset(cc->key, 0, cc->key_size * sizeof(u8));
3138 if (argc == 2 && !strcasecmp(argv[1], "wipe"))
3139 return crypt_wipe_key(cc);
3143 DMWARN("unrecognised message received.");
3147 static int crypt_iterate_devices(struct dm_target *ti,
3148 iterate_devices_callout_fn fn, void *data)
3150 struct crypt_config *cc = ti->private;
3152 return fn(ti, cc->dev, cc->start, ti->len, data);
3155 static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
3157 struct crypt_config *cc = ti->private;
3160 * Unfortunate constraint that is required to avoid the potential
3161 * for exceeding underlying device's max_segments limits -- due to
3162 * crypt_alloc_buffer() possibly allocating pages for the encryption
3163 * bio that are not as physically contiguous as the original bio.
3165 limits->max_segment_size = PAGE_SIZE;
3167 limits->logical_block_size =
3168 max_t(unsigned short, limits->logical_block_size, cc->sector_size);
3169 limits->physical_block_size =
3170 max_t(unsigned, limits->physical_block_size, cc->sector_size);
3171 limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size);
3174 static struct target_type crypt_target = {
3176 .version = {1, 19, 0},
3177 .module = THIS_MODULE,
3181 .status = crypt_status,
3182 .postsuspend = crypt_postsuspend,
3183 .preresume = crypt_preresume,
3184 .resume = crypt_resume,
3185 .message = crypt_message,
3186 .iterate_devices = crypt_iterate_devices,
3187 .io_hints = crypt_io_hints,
3190 static int __init dm_crypt_init(void)
3194 r = dm_register_target(&crypt_target);
3196 DMERR("register failed %d", r);
3201 static void __exit dm_crypt_exit(void)
3203 dm_unregister_target(&crypt_target);
3206 module_init(dm_crypt_init);
3207 module_exit(dm_crypt_exit);
3209 MODULE_AUTHOR("Jana Saout <jana@saout.de>");
3210 MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
3211 MODULE_LICENSE("GPL");