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
;
124 * Crypt: maps a linear range of a block device
125 * and encrypts / decrypts at the same time.
127 enum flags
{ DM_CRYPT_SUSPENDED
, DM_CRYPT_KEY_VALID
,
128 DM_CRYPT_SAME_CPU
, DM_CRYPT_NO_OFFLOAD
};
131 CRYPT_MODE_INTEGRITY_AEAD
, /* Use authenticated mode for cihper */
132 CRYPT_IV_LARGE_SECTORS
, /* Calculate IV from sector_size, not 512B sectors */
136 * The fields in here must be read only after initialization.
138 struct crypt_config
{
142 struct percpu_counter n_allocated_pages
;
144 struct workqueue_struct
*io_queue
;
145 struct workqueue_struct
*crypt_queue
;
147 spinlock_t write_thread_lock
;
148 struct task_struct
*write_thread
;
149 struct rb_root write_tree
;
156 const struct crypt_iv_operations
*iv_gen_ops
;
158 struct iv_essiv_private essiv
;
159 struct iv_benbi_private benbi
;
160 struct iv_lmk_private lmk
;
161 struct iv_tcw_private tcw
;
164 unsigned int iv_size
;
165 unsigned short int sector_size
;
166 unsigned char sector_shift
;
168 /* ESSIV: struct crypto_cipher *essiv_tfm */
171 struct crypto_skcipher
**tfms
;
172 struct crypto_aead
**tfms_aead
;
175 unsigned long cipher_flags
;
178 * Layout of each crypto request:
180 * struct skcipher_request
183 * struct dm_crypt_request
187 * The padding is added so that dm_crypt_request and the IV are
190 unsigned int dmreq_start
;
192 unsigned int per_bio_data_size
;
195 unsigned int key_size
;
196 unsigned int key_parts
; /* independent parts in key buffer */
197 unsigned int key_extra_size
; /* additional keys length */
198 unsigned int key_mac_size
; /* MAC key size for authenc(...) */
200 unsigned int integrity_tag_size
;
201 unsigned int integrity_iv_size
;
202 unsigned int on_disk_tag_size
;
205 * pool for per bio private data, crypto requests,
206 * encryption requeusts/buffer pages and integrity tags
208 unsigned tag_pool_max_sectors
;
214 struct mutex bio_alloc_lock
;
216 u8
*authenc_key
; /* space for keys in authenc() format (if used) */
221 #define MAX_TAG_SIZE 480
222 #define POOL_ENTRY_SIZE 512
224 static DEFINE_SPINLOCK(dm_crypt_clients_lock
);
225 static unsigned dm_crypt_clients_n
= 0;
226 static volatile unsigned long dm_crypt_pages_per_client
;
227 #define DM_CRYPT_MEMORY_PERCENT 2
228 #define DM_CRYPT_MIN_PAGES_PER_CLIENT (BIO_MAX_PAGES * 16)
230 static void clone_init(struct dm_crypt_io
*, struct bio
*);
231 static void kcryptd_queue_crypt(struct dm_crypt_io
*io
);
232 static struct scatterlist
*crypt_get_sg_data(struct crypt_config
*cc
,
233 struct scatterlist
*sg
);
236 * Use this to access cipher attributes that are independent of the key.
238 static struct crypto_skcipher
*any_tfm(struct crypt_config
*cc
)
240 return cc
->cipher_tfm
.tfms
[0];
243 static struct crypto_aead
*any_tfm_aead(struct crypt_config
*cc
)
245 return cc
->cipher_tfm
.tfms_aead
[0];
249 * Different IV generation algorithms:
251 * plain: the initial vector is the 32-bit little-endian version of the sector
252 * number, padded with zeros if necessary.
254 * plain64: the initial vector is the 64-bit little-endian version of the sector
255 * number, padded with zeros if necessary.
257 * plain64be: the initial vector is the 64-bit big-endian version of the sector
258 * number, padded with zeros if necessary.
260 * essiv: "encrypted sector|salt initial vector", the sector number is
261 * encrypted with the bulk cipher using a salt as key. The salt
262 * should be derived from the bulk cipher's key via hashing.
264 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
265 * (needed for LRW-32-AES and possible other narrow block modes)
267 * null: the initial vector is always zero. Provides compatibility with
268 * obsolete loop_fish2 devices. Do not use for new devices.
270 * lmk: Compatible implementation of the block chaining mode used
271 * by the Loop-AES block device encryption system
272 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
273 * It operates on full 512 byte sectors and uses CBC
274 * with an IV derived from the sector number, the data and
275 * optionally extra IV seed.
276 * This means that after decryption the first block
277 * of sector must be tweaked according to decrypted data.
278 * Loop-AES can use three encryption schemes:
279 * version 1: is plain aes-cbc mode
280 * version 2: uses 64 multikey scheme with lmk IV generator
281 * version 3: the same as version 2 with additional IV seed
282 * (it uses 65 keys, last key is used as IV seed)
284 * tcw: Compatible implementation of the block chaining mode used
285 * by the TrueCrypt device encryption system (prior to version 4.1).
286 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
287 * It operates on full 512 byte sectors and uses CBC
288 * with an IV derived from initial key and the sector number.
289 * In addition, whitening value is applied on every sector, whitening
290 * is calculated from initial key, sector number and mixed using CRC32.
291 * Note that this encryption scheme is vulnerable to watermarking attacks
292 * and should be used for old compatible containers access only.
294 * plumb: unimplemented, see:
295 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
298 static int crypt_iv_plain_gen(struct crypt_config
*cc
, u8
*iv
,
299 struct dm_crypt_request
*dmreq
)
301 memset(iv
, 0, cc
->iv_size
);
302 *(__le32
*)iv
= cpu_to_le32(dmreq
->iv_sector
& 0xffffffff);
307 static int crypt_iv_plain64_gen(struct crypt_config
*cc
, u8
*iv
,
308 struct dm_crypt_request
*dmreq
)
310 memset(iv
, 0, cc
->iv_size
);
311 *(__le64
*)iv
= cpu_to_le64(dmreq
->iv_sector
);
316 static int crypt_iv_plain64be_gen(struct crypt_config
*cc
, u8
*iv
,
317 struct dm_crypt_request
*dmreq
)
319 memset(iv
, 0, cc
->iv_size
);
320 /* iv_size is at least of size u64; usually it is 16 bytes */
321 *(__be64
*)&iv
[cc
->iv_size
- sizeof(u64
)] = cpu_to_be64(dmreq
->iv_sector
);
326 /* Initialise ESSIV - compute salt but no local memory allocations */
327 static int crypt_iv_essiv_init(struct crypt_config
*cc
)
329 struct iv_essiv_private
*essiv
= &cc
->iv_gen_private
.essiv
;
330 SHASH_DESC_ON_STACK(desc
, essiv
->hash_tfm
);
331 struct crypto_cipher
*essiv_tfm
;
334 desc
->tfm
= essiv
->hash_tfm
;
335 desc
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
337 err
= crypto_shash_digest(desc
, cc
->key
, cc
->key_size
, essiv
->salt
);
338 shash_desc_zero(desc
);
342 essiv_tfm
= cc
->iv_private
;
344 err
= crypto_cipher_setkey(essiv_tfm
, essiv
->salt
,
345 crypto_shash_digestsize(essiv
->hash_tfm
));
352 /* Wipe salt and reset key derived from volume key */
353 static int crypt_iv_essiv_wipe(struct crypt_config
*cc
)
355 struct iv_essiv_private
*essiv
= &cc
->iv_gen_private
.essiv
;
356 unsigned salt_size
= crypto_shash_digestsize(essiv
->hash_tfm
);
357 struct crypto_cipher
*essiv_tfm
;
360 memset(essiv
->salt
, 0, salt_size
);
362 essiv_tfm
= cc
->iv_private
;
363 r
= crypto_cipher_setkey(essiv_tfm
, essiv
->salt
, salt_size
);
370 /* Allocate the cipher for ESSIV */
371 static struct crypto_cipher
*alloc_essiv_cipher(struct crypt_config
*cc
,
372 struct dm_target
*ti
,
374 unsigned int saltsize
)
376 struct crypto_cipher
*essiv_tfm
;
379 /* Setup the essiv_tfm with the given salt */
380 essiv_tfm
= crypto_alloc_cipher(cc
->cipher
, 0, CRYPTO_ALG_ASYNC
);
381 if (IS_ERR(essiv_tfm
)) {
382 ti
->error
= "Error allocating crypto tfm for ESSIV";
386 if (crypto_cipher_blocksize(essiv_tfm
) != cc
->iv_size
) {
387 ti
->error
= "Block size of ESSIV cipher does "
388 "not match IV size of block cipher";
389 crypto_free_cipher(essiv_tfm
);
390 return ERR_PTR(-EINVAL
);
393 err
= crypto_cipher_setkey(essiv_tfm
, salt
, saltsize
);
395 ti
->error
= "Failed to set key for ESSIV cipher";
396 crypto_free_cipher(essiv_tfm
);
403 static void crypt_iv_essiv_dtr(struct crypt_config
*cc
)
405 struct crypto_cipher
*essiv_tfm
;
406 struct iv_essiv_private
*essiv
= &cc
->iv_gen_private
.essiv
;
408 crypto_free_shash(essiv
->hash_tfm
);
409 essiv
->hash_tfm
= NULL
;
414 essiv_tfm
= cc
->iv_private
;
417 crypto_free_cipher(essiv_tfm
);
419 cc
->iv_private
= NULL
;
422 static int crypt_iv_essiv_ctr(struct crypt_config
*cc
, struct dm_target
*ti
,
425 struct crypto_cipher
*essiv_tfm
= NULL
;
426 struct crypto_shash
*hash_tfm
= NULL
;
431 ti
->error
= "Digest algorithm missing for ESSIV mode";
435 /* Allocate hash algorithm */
436 hash_tfm
= crypto_alloc_shash(opts
, 0, 0);
437 if (IS_ERR(hash_tfm
)) {
438 ti
->error
= "Error initializing ESSIV hash";
439 err
= PTR_ERR(hash_tfm
);
443 salt
= kzalloc(crypto_shash_digestsize(hash_tfm
), GFP_KERNEL
);
445 ti
->error
= "Error kmallocing salt storage in ESSIV";
450 cc
->iv_gen_private
.essiv
.salt
= salt
;
451 cc
->iv_gen_private
.essiv
.hash_tfm
= hash_tfm
;
453 essiv_tfm
= alloc_essiv_cipher(cc
, ti
, salt
,
454 crypto_shash_digestsize(hash_tfm
));
455 if (IS_ERR(essiv_tfm
)) {
456 crypt_iv_essiv_dtr(cc
);
457 return PTR_ERR(essiv_tfm
);
459 cc
->iv_private
= essiv_tfm
;
464 if (hash_tfm
&& !IS_ERR(hash_tfm
))
465 crypto_free_shash(hash_tfm
);
470 static int crypt_iv_essiv_gen(struct crypt_config
*cc
, u8
*iv
,
471 struct dm_crypt_request
*dmreq
)
473 struct crypto_cipher
*essiv_tfm
= cc
->iv_private
;
475 memset(iv
, 0, cc
->iv_size
);
476 *(__le64
*)iv
= cpu_to_le64(dmreq
->iv_sector
);
477 crypto_cipher_encrypt_one(essiv_tfm
, iv
, iv
);
482 static int crypt_iv_benbi_ctr(struct crypt_config
*cc
, struct dm_target
*ti
,
485 unsigned bs
= crypto_skcipher_blocksize(any_tfm(cc
));
488 /* we need to calculate how far we must shift the sector count
489 * to get the cipher block count, we use this shift in _gen */
491 if (1 << log
!= bs
) {
492 ti
->error
= "cypher blocksize is not a power of 2";
497 ti
->error
= "cypher blocksize is > 512";
501 cc
->iv_gen_private
.benbi
.shift
= 9 - log
;
506 static void crypt_iv_benbi_dtr(struct crypt_config
*cc
)
510 static int crypt_iv_benbi_gen(struct crypt_config
*cc
, u8
*iv
,
511 struct dm_crypt_request
*dmreq
)
515 memset(iv
, 0, cc
->iv_size
- sizeof(u64
)); /* rest is cleared below */
517 val
= cpu_to_be64(((u64
)dmreq
->iv_sector
<< cc
->iv_gen_private
.benbi
.shift
) + 1);
518 put_unaligned(val
, (__be64
*)(iv
+ cc
->iv_size
- sizeof(u64
)));
523 static int crypt_iv_null_gen(struct crypt_config
*cc
, u8
*iv
,
524 struct dm_crypt_request
*dmreq
)
526 memset(iv
, 0, cc
->iv_size
);
531 static void crypt_iv_lmk_dtr(struct crypt_config
*cc
)
533 struct iv_lmk_private
*lmk
= &cc
->iv_gen_private
.lmk
;
535 if (lmk
->hash_tfm
&& !IS_ERR(lmk
->hash_tfm
))
536 crypto_free_shash(lmk
->hash_tfm
);
537 lmk
->hash_tfm
= NULL
;
543 static int crypt_iv_lmk_ctr(struct crypt_config
*cc
, struct dm_target
*ti
,
546 struct iv_lmk_private
*lmk
= &cc
->iv_gen_private
.lmk
;
548 if (cc
->sector_size
!= (1 << SECTOR_SHIFT
)) {
549 ti
->error
= "Unsupported sector size for LMK";
553 lmk
->hash_tfm
= crypto_alloc_shash("md5", 0, 0);
554 if (IS_ERR(lmk
->hash_tfm
)) {
555 ti
->error
= "Error initializing LMK hash";
556 return PTR_ERR(lmk
->hash_tfm
);
559 /* No seed in LMK version 2 */
560 if (cc
->key_parts
== cc
->tfms_count
) {
565 lmk
->seed
= kzalloc(LMK_SEED_SIZE
, GFP_KERNEL
);
567 crypt_iv_lmk_dtr(cc
);
568 ti
->error
= "Error kmallocing seed storage in LMK";
575 static int crypt_iv_lmk_init(struct crypt_config
*cc
)
577 struct iv_lmk_private
*lmk
= &cc
->iv_gen_private
.lmk
;
578 int subkey_size
= cc
->key_size
/ cc
->key_parts
;
580 /* LMK seed is on the position of LMK_KEYS + 1 key */
582 memcpy(lmk
->seed
, cc
->key
+ (cc
->tfms_count
* subkey_size
),
583 crypto_shash_digestsize(lmk
->hash_tfm
));
588 static int crypt_iv_lmk_wipe(struct crypt_config
*cc
)
590 struct iv_lmk_private
*lmk
= &cc
->iv_gen_private
.lmk
;
593 memset(lmk
->seed
, 0, LMK_SEED_SIZE
);
598 static int crypt_iv_lmk_one(struct crypt_config
*cc
, u8
*iv
,
599 struct dm_crypt_request
*dmreq
,
602 struct iv_lmk_private
*lmk
= &cc
->iv_gen_private
.lmk
;
603 SHASH_DESC_ON_STACK(desc
, lmk
->hash_tfm
);
604 struct md5_state md5state
;
608 desc
->tfm
= lmk
->hash_tfm
;
609 desc
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
611 r
= crypto_shash_init(desc
);
616 r
= crypto_shash_update(desc
, lmk
->seed
, LMK_SEED_SIZE
);
621 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
622 r
= crypto_shash_update(desc
, data
+ 16, 16 * 31);
626 /* Sector is cropped to 56 bits here */
627 buf
[0] = cpu_to_le32(dmreq
->iv_sector
& 0xFFFFFFFF);
628 buf
[1] = cpu_to_le32((((u64
)dmreq
->iv_sector
>> 32) & 0x00FFFFFF) | 0x80000000);
629 buf
[2] = cpu_to_le32(4024);
631 r
= crypto_shash_update(desc
, (u8
*)buf
, sizeof(buf
));
635 /* No MD5 padding here */
636 r
= crypto_shash_export(desc
, &md5state
);
640 for (i
= 0; i
< MD5_HASH_WORDS
; i
++)
641 __cpu_to_le32s(&md5state
.hash
[i
]);
642 memcpy(iv
, &md5state
.hash
, cc
->iv_size
);
647 static int crypt_iv_lmk_gen(struct crypt_config
*cc
, u8
*iv
,
648 struct dm_crypt_request
*dmreq
)
650 struct scatterlist
*sg
;
654 if (bio_data_dir(dmreq
->ctx
->bio_in
) == WRITE
) {
655 sg
= crypt_get_sg_data(cc
, dmreq
->sg_in
);
656 src
= kmap_atomic(sg_page(sg
));
657 r
= crypt_iv_lmk_one(cc
, iv
, dmreq
, src
+ sg
->offset
);
660 memset(iv
, 0, cc
->iv_size
);
665 static int crypt_iv_lmk_post(struct crypt_config
*cc
, u8
*iv
,
666 struct dm_crypt_request
*dmreq
)
668 struct scatterlist
*sg
;
672 if (bio_data_dir(dmreq
->ctx
->bio_in
) == WRITE
)
675 sg
= crypt_get_sg_data(cc
, dmreq
->sg_out
);
676 dst
= kmap_atomic(sg_page(sg
));
677 r
= crypt_iv_lmk_one(cc
, iv
, dmreq
, dst
+ sg
->offset
);
679 /* Tweak the first block of plaintext sector */
681 crypto_xor(dst
+ sg
->offset
, iv
, cc
->iv_size
);
687 static void crypt_iv_tcw_dtr(struct crypt_config
*cc
)
689 struct iv_tcw_private
*tcw
= &cc
->iv_gen_private
.tcw
;
691 kzfree(tcw
->iv_seed
);
693 kzfree(tcw
->whitening
);
694 tcw
->whitening
= NULL
;
696 if (tcw
->crc32_tfm
&& !IS_ERR(tcw
->crc32_tfm
))
697 crypto_free_shash(tcw
->crc32_tfm
);
698 tcw
->crc32_tfm
= NULL
;
701 static int crypt_iv_tcw_ctr(struct crypt_config
*cc
, struct dm_target
*ti
,
704 struct iv_tcw_private
*tcw
= &cc
->iv_gen_private
.tcw
;
706 if (cc
->sector_size
!= (1 << SECTOR_SHIFT
)) {
707 ti
->error
= "Unsupported sector size for TCW";
711 if (cc
->key_size
<= (cc
->iv_size
+ TCW_WHITENING_SIZE
)) {
712 ti
->error
= "Wrong key size for TCW";
716 tcw
->crc32_tfm
= crypto_alloc_shash("crc32", 0, 0);
717 if (IS_ERR(tcw
->crc32_tfm
)) {
718 ti
->error
= "Error initializing CRC32 in TCW";
719 return PTR_ERR(tcw
->crc32_tfm
);
722 tcw
->iv_seed
= kzalloc(cc
->iv_size
, GFP_KERNEL
);
723 tcw
->whitening
= kzalloc(TCW_WHITENING_SIZE
, GFP_KERNEL
);
724 if (!tcw
->iv_seed
|| !tcw
->whitening
) {
725 crypt_iv_tcw_dtr(cc
);
726 ti
->error
= "Error allocating seed storage in TCW";
733 static int crypt_iv_tcw_init(struct crypt_config
*cc
)
735 struct iv_tcw_private
*tcw
= &cc
->iv_gen_private
.tcw
;
736 int key_offset
= cc
->key_size
- cc
->iv_size
- TCW_WHITENING_SIZE
;
738 memcpy(tcw
->iv_seed
, &cc
->key
[key_offset
], cc
->iv_size
);
739 memcpy(tcw
->whitening
, &cc
->key
[key_offset
+ cc
->iv_size
],
745 static int crypt_iv_tcw_wipe(struct crypt_config
*cc
)
747 struct iv_tcw_private
*tcw
= &cc
->iv_gen_private
.tcw
;
749 memset(tcw
->iv_seed
, 0, cc
->iv_size
);
750 memset(tcw
->whitening
, 0, TCW_WHITENING_SIZE
);
755 static int crypt_iv_tcw_whitening(struct crypt_config
*cc
,
756 struct dm_crypt_request
*dmreq
,
759 struct iv_tcw_private
*tcw
= &cc
->iv_gen_private
.tcw
;
760 __le64 sector
= cpu_to_le64(dmreq
->iv_sector
);
761 u8 buf
[TCW_WHITENING_SIZE
];
762 SHASH_DESC_ON_STACK(desc
, tcw
->crc32_tfm
);
765 /* xor whitening with sector number */
766 crypto_xor_cpy(buf
, tcw
->whitening
, (u8
*)§or
, 8);
767 crypto_xor_cpy(&buf
[8], tcw
->whitening
+ 8, (u8
*)§or
, 8);
769 /* calculate crc32 for every 32bit part and xor it */
770 desc
->tfm
= tcw
->crc32_tfm
;
771 desc
->flags
= CRYPTO_TFM_REQ_MAY_SLEEP
;
772 for (i
= 0; i
< 4; i
++) {
773 r
= crypto_shash_init(desc
);
776 r
= crypto_shash_update(desc
, &buf
[i
* 4], 4);
779 r
= crypto_shash_final(desc
, &buf
[i
* 4]);
783 crypto_xor(&buf
[0], &buf
[12], 4);
784 crypto_xor(&buf
[4], &buf
[8], 4);
786 /* apply whitening (8 bytes) to whole sector */
787 for (i
= 0; i
< ((1 << SECTOR_SHIFT
) / 8); i
++)
788 crypto_xor(data
+ i
* 8, buf
, 8);
790 memzero_explicit(buf
, sizeof(buf
));
794 static int crypt_iv_tcw_gen(struct crypt_config
*cc
, u8
*iv
,
795 struct dm_crypt_request
*dmreq
)
797 struct scatterlist
*sg
;
798 struct iv_tcw_private
*tcw
= &cc
->iv_gen_private
.tcw
;
799 __le64 sector
= cpu_to_le64(dmreq
->iv_sector
);
803 /* Remove whitening from ciphertext */
804 if (bio_data_dir(dmreq
->ctx
->bio_in
) != WRITE
) {
805 sg
= crypt_get_sg_data(cc
, dmreq
->sg_in
);
806 src
= kmap_atomic(sg_page(sg
));
807 r
= crypt_iv_tcw_whitening(cc
, dmreq
, src
+ sg
->offset
);
812 crypto_xor_cpy(iv
, tcw
->iv_seed
, (u8
*)§or
, 8);
814 crypto_xor_cpy(&iv
[8], tcw
->iv_seed
+ 8, (u8
*)§or
,
820 static int crypt_iv_tcw_post(struct crypt_config
*cc
, u8
*iv
,
821 struct dm_crypt_request
*dmreq
)
823 struct scatterlist
*sg
;
827 if (bio_data_dir(dmreq
->ctx
->bio_in
) != WRITE
)
830 /* Apply whitening on ciphertext */
831 sg
= crypt_get_sg_data(cc
, dmreq
->sg_out
);
832 dst
= kmap_atomic(sg_page(sg
));
833 r
= crypt_iv_tcw_whitening(cc
, dmreq
, dst
+ sg
->offset
);
839 static int crypt_iv_random_gen(struct crypt_config
*cc
, u8
*iv
,
840 struct dm_crypt_request
*dmreq
)
842 /* Used only for writes, there must be an additional space to store IV */
843 get_random_bytes(iv
, cc
->iv_size
);
847 static const struct crypt_iv_operations crypt_iv_plain_ops
= {
848 .generator
= crypt_iv_plain_gen
851 static const struct crypt_iv_operations crypt_iv_plain64_ops
= {
852 .generator
= crypt_iv_plain64_gen
855 static const struct crypt_iv_operations crypt_iv_plain64be_ops
= {
856 .generator
= crypt_iv_plain64be_gen
859 static const struct crypt_iv_operations crypt_iv_essiv_ops
= {
860 .ctr
= crypt_iv_essiv_ctr
,
861 .dtr
= crypt_iv_essiv_dtr
,
862 .init
= crypt_iv_essiv_init
,
863 .wipe
= crypt_iv_essiv_wipe
,
864 .generator
= crypt_iv_essiv_gen
867 static const struct crypt_iv_operations crypt_iv_benbi_ops
= {
868 .ctr
= crypt_iv_benbi_ctr
,
869 .dtr
= crypt_iv_benbi_dtr
,
870 .generator
= crypt_iv_benbi_gen
873 static const struct crypt_iv_operations crypt_iv_null_ops
= {
874 .generator
= crypt_iv_null_gen
877 static const struct crypt_iv_operations crypt_iv_lmk_ops
= {
878 .ctr
= crypt_iv_lmk_ctr
,
879 .dtr
= crypt_iv_lmk_dtr
,
880 .init
= crypt_iv_lmk_init
,
881 .wipe
= crypt_iv_lmk_wipe
,
882 .generator
= crypt_iv_lmk_gen
,
883 .post
= crypt_iv_lmk_post
886 static const struct crypt_iv_operations crypt_iv_tcw_ops
= {
887 .ctr
= crypt_iv_tcw_ctr
,
888 .dtr
= crypt_iv_tcw_dtr
,
889 .init
= crypt_iv_tcw_init
,
890 .wipe
= crypt_iv_tcw_wipe
,
891 .generator
= crypt_iv_tcw_gen
,
892 .post
= crypt_iv_tcw_post
895 static struct crypt_iv_operations crypt_iv_random_ops
= {
896 .generator
= crypt_iv_random_gen
900 * Integrity extensions
902 static bool crypt_integrity_aead(struct crypt_config
*cc
)
904 return test_bit(CRYPT_MODE_INTEGRITY_AEAD
, &cc
->cipher_flags
);
907 static bool crypt_integrity_hmac(struct crypt_config
*cc
)
909 return crypt_integrity_aead(cc
) && cc
->key_mac_size
;
912 /* Get sg containing data */
913 static struct scatterlist
*crypt_get_sg_data(struct crypt_config
*cc
,
914 struct scatterlist
*sg
)
916 if (unlikely(crypt_integrity_aead(cc
)))
922 static int dm_crypt_integrity_io_alloc(struct dm_crypt_io
*io
, struct bio
*bio
)
924 struct bio_integrity_payload
*bip
;
925 unsigned int tag_len
;
928 if (!bio_sectors(bio
) || !io
->cc
->on_disk_tag_size
)
931 bip
= bio_integrity_alloc(bio
, GFP_NOIO
, 1);
935 tag_len
= io
->cc
->on_disk_tag_size
* bio_sectors(bio
);
937 bip
->bip_iter
.bi_size
= tag_len
;
938 bip
->bip_iter
.bi_sector
= io
->cc
->start
+ io
->sector
;
940 ret
= bio_integrity_add_page(bio
, virt_to_page(io
->integrity_metadata
),
941 tag_len
, offset_in_page(io
->integrity_metadata
));
942 if (unlikely(ret
!= tag_len
))
948 static int crypt_integrity_ctr(struct crypt_config
*cc
, struct dm_target
*ti
)
950 #ifdef CONFIG_BLK_DEV_INTEGRITY
951 struct blk_integrity
*bi
= blk_get_integrity(cc
->dev
->bdev
->bd_disk
);
953 /* From now we require underlying device with our integrity profile */
954 if (!bi
|| strcasecmp(bi
->profile
->name
, "DM-DIF-EXT-TAG")) {
955 ti
->error
= "Integrity profile not supported.";
959 if (bi
->tag_size
!= cc
->on_disk_tag_size
||
960 bi
->tuple_size
!= cc
->on_disk_tag_size
) {
961 ti
->error
= "Integrity profile tag size mismatch.";
964 if (1 << bi
->interval_exp
!= cc
->sector_size
) {
965 ti
->error
= "Integrity profile sector size mismatch.";
969 if (crypt_integrity_aead(cc
)) {
970 cc
->integrity_tag_size
= cc
->on_disk_tag_size
- cc
->integrity_iv_size
;
971 DMINFO("Integrity AEAD, tag size %u, IV size %u.",
972 cc
->integrity_tag_size
, cc
->integrity_iv_size
);
974 if (crypto_aead_setauthsize(any_tfm_aead(cc
), cc
->integrity_tag_size
)) {
975 ti
->error
= "Integrity AEAD auth tag size is not supported.";
978 } else if (cc
->integrity_iv_size
)
979 DMINFO("Additional per-sector space %u bytes for IV.",
980 cc
->integrity_iv_size
);
982 if ((cc
->integrity_tag_size
+ cc
->integrity_iv_size
) != bi
->tag_size
) {
983 ti
->error
= "Not enough space for integrity tag in the profile.";
989 ti
->error
= "Integrity profile not supported.";
994 static void crypt_convert_init(struct crypt_config
*cc
,
995 struct convert_context
*ctx
,
996 struct bio
*bio_out
, struct bio
*bio_in
,
999 ctx
->bio_in
= bio_in
;
1000 ctx
->bio_out
= bio_out
;
1002 ctx
->iter_in
= bio_in
->bi_iter
;
1004 ctx
->iter_out
= bio_out
->bi_iter
;
1005 ctx
->cc_sector
= sector
+ cc
->iv_offset
;
1006 init_completion(&ctx
->restart
);
1009 static struct dm_crypt_request
*dmreq_of_req(struct crypt_config
*cc
,
1012 return (struct dm_crypt_request
*)((char *)req
+ cc
->dmreq_start
);
1015 static void *req_of_dmreq(struct crypt_config
*cc
, struct dm_crypt_request
*dmreq
)
1017 return (void *)((char *)dmreq
- cc
->dmreq_start
);
1020 static u8
*iv_of_dmreq(struct crypt_config
*cc
,
1021 struct dm_crypt_request
*dmreq
)
1023 if (crypt_integrity_aead(cc
))
1024 return (u8
*)ALIGN((unsigned long)(dmreq
+ 1),
1025 crypto_aead_alignmask(any_tfm_aead(cc
)) + 1);
1027 return (u8
*)ALIGN((unsigned long)(dmreq
+ 1),
1028 crypto_skcipher_alignmask(any_tfm(cc
)) + 1);
1031 static u8
*org_iv_of_dmreq(struct crypt_config
*cc
,
1032 struct dm_crypt_request
*dmreq
)
1034 return iv_of_dmreq(cc
, dmreq
) + cc
->iv_size
;
1037 static uint64_t *org_sector_of_dmreq(struct crypt_config
*cc
,
1038 struct dm_crypt_request
*dmreq
)
1040 u8
*ptr
= iv_of_dmreq(cc
, dmreq
) + cc
->iv_size
+ cc
->iv_size
;
1041 return (uint64_t*) ptr
;
1044 static unsigned int *org_tag_of_dmreq(struct crypt_config
*cc
,
1045 struct dm_crypt_request
*dmreq
)
1047 u8
*ptr
= iv_of_dmreq(cc
, dmreq
) + cc
->iv_size
+
1048 cc
->iv_size
+ sizeof(uint64_t);
1049 return (unsigned int*)ptr
;
1052 static void *tag_from_dmreq(struct crypt_config
*cc
,
1053 struct dm_crypt_request
*dmreq
)
1055 struct convert_context
*ctx
= dmreq
->ctx
;
1056 struct dm_crypt_io
*io
= container_of(ctx
, struct dm_crypt_io
, ctx
);
1058 return &io
->integrity_metadata
[*org_tag_of_dmreq(cc
, dmreq
) *
1059 cc
->on_disk_tag_size
];
1062 static void *iv_tag_from_dmreq(struct crypt_config
*cc
,
1063 struct dm_crypt_request
*dmreq
)
1065 return tag_from_dmreq(cc
, dmreq
) + cc
->integrity_tag_size
;
1068 static int crypt_convert_block_aead(struct crypt_config
*cc
,
1069 struct convert_context
*ctx
,
1070 struct aead_request
*req
,
1071 unsigned int tag_offset
)
1073 struct bio_vec bv_in
= bio_iter_iovec(ctx
->bio_in
, ctx
->iter_in
);
1074 struct bio_vec bv_out
= bio_iter_iovec(ctx
->bio_out
, ctx
->iter_out
);
1075 struct dm_crypt_request
*dmreq
;
1076 u8
*iv
, *org_iv
, *tag_iv
, *tag
;
1080 BUG_ON(cc
->integrity_iv_size
&& cc
->integrity_iv_size
!= cc
->iv_size
);
1082 /* Reject unexpected unaligned bio. */
1083 if (unlikely(bv_in
.bv_len
& (cc
->sector_size
- 1)))
1086 dmreq
= dmreq_of_req(cc
, req
);
1087 dmreq
->iv_sector
= ctx
->cc_sector
;
1088 if (test_bit(CRYPT_IV_LARGE_SECTORS
, &cc
->cipher_flags
))
1089 dmreq
->iv_sector
>>= cc
->sector_shift
;
1092 *org_tag_of_dmreq(cc
, dmreq
) = tag_offset
;
1094 sector
= org_sector_of_dmreq(cc
, dmreq
);
1095 *sector
= cpu_to_le64(ctx
->cc_sector
- cc
->iv_offset
);
1097 iv
= iv_of_dmreq(cc
, dmreq
);
1098 org_iv
= org_iv_of_dmreq(cc
, dmreq
);
1099 tag
= tag_from_dmreq(cc
, dmreq
);
1100 tag_iv
= iv_tag_from_dmreq(cc
, dmreq
);
1103 * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
1104 * | (authenticated) | (auth+encryption) | |
1105 * | sector_LE | IV | sector in/out | tag in/out |
1107 sg_init_table(dmreq
->sg_in
, 4);
1108 sg_set_buf(&dmreq
->sg_in
[0], sector
, sizeof(uint64_t));
1109 sg_set_buf(&dmreq
->sg_in
[1], org_iv
, cc
->iv_size
);
1110 sg_set_page(&dmreq
->sg_in
[2], bv_in
.bv_page
, cc
->sector_size
, bv_in
.bv_offset
);
1111 sg_set_buf(&dmreq
->sg_in
[3], tag
, cc
->integrity_tag_size
);
1113 sg_init_table(dmreq
->sg_out
, 4);
1114 sg_set_buf(&dmreq
->sg_out
[0], sector
, sizeof(uint64_t));
1115 sg_set_buf(&dmreq
->sg_out
[1], org_iv
, cc
->iv_size
);
1116 sg_set_page(&dmreq
->sg_out
[2], bv_out
.bv_page
, cc
->sector_size
, bv_out
.bv_offset
);
1117 sg_set_buf(&dmreq
->sg_out
[3], tag
, cc
->integrity_tag_size
);
1119 if (cc
->iv_gen_ops
) {
1120 /* For READs use IV stored in integrity metadata */
1121 if (cc
->integrity_iv_size
&& bio_data_dir(ctx
->bio_in
) != WRITE
) {
1122 memcpy(org_iv
, tag_iv
, cc
->iv_size
);
1124 r
= cc
->iv_gen_ops
->generator(cc
, org_iv
, dmreq
);
1127 /* Store generated IV in integrity metadata */
1128 if (cc
->integrity_iv_size
)
1129 memcpy(tag_iv
, org_iv
, cc
->iv_size
);
1131 /* Working copy of IV, to be modified in crypto API */
1132 memcpy(iv
, org_iv
, cc
->iv_size
);
1135 aead_request_set_ad(req
, sizeof(uint64_t) + cc
->iv_size
);
1136 if (bio_data_dir(ctx
->bio_in
) == WRITE
) {
1137 aead_request_set_crypt(req
, dmreq
->sg_in
, dmreq
->sg_out
,
1138 cc
->sector_size
, iv
);
1139 r
= crypto_aead_encrypt(req
);
1140 if (cc
->integrity_tag_size
+ cc
->integrity_iv_size
!= cc
->on_disk_tag_size
)
1141 memset(tag
+ cc
->integrity_tag_size
+ cc
->integrity_iv_size
, 0,
1142 cc
->on_disk_tag_size
- (cc
->integrity_tag_size
+ cc
->integrity_iv_size
));
1144 aead_request_set_crypt(req
, dmreq
->sg_in
, dmreq
->sg_out
,
1145 cc
->sector_size
+ cc
->integrity_tag_size
, iv
);
1146 r
= crypto_aead_decrypt(req
);
1150 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1151 (unsigned long long)le64_to_cpu(*sector
));
1153 if (!r
&& cc
->iv_gen_ops
&& cc
->iv_gen_ops
->post
)
1154 r
= cc
->iv_gen_ops
->post(cc
, org_iv
, dmreq
);
1156 bio_advance_iter(ctx
->bio_in
, &ctx
->iter_in
, cc
->sector_size
);
1157 bio_advance_iter(ctx
->bio_out
, &ctx
->iter_out
, cc
->sector_size
);
1162 static int crypt_convert_block_skcipher(struct crypt_config
*cc
,
1163 struct convert_context
*ctx
,
1164 struct skcipher_request
*req
,
1165 unsigned int tag_offset
)
1167 struct bio_vec bv_in
= bio_iter_iovec(ctx
->bio_in
, ctx
->iter_in
);
1168 struct bio_vec bv_out
= bio_iter_iovec(ctx
->bio_out
, ctx
->iter_out
);
1169 struct scatterlist
*sg_in
, *sg_out
;
1170 struct dm_crypt_request
*dmreq
;
1171 u8
*iv
, *org_iv
, *tag_iv
;
1175 /* Reject unexpected unaligned bio. */
1176 if (unlikely(bv_in
.bv_len
& (cc
->sector_size
- 1)))
1179 dmreq
= dmreq_of_req(cc
, req
);
1180 dmreq
->iv_sector
= ctx
->cc_sector
;
1181 if (test_bit(CRYPT_IV_LARGE_SECTORS
, &cc
->cipher_flags
))
1182 dmreq
->iv_sector
>>= cc
->sector_shift
;
1185 *org_tag_of_dmreq(cc
, dmreq
) = tag_offset
;
1187 iv
= iv_of_dmreq(cc
, dmreq
);
1188 org_iv
= org_iv_of_dmreq(cc
, dmreq
);
1189 tag_iv
= iv_tag_from_dmreq(cc
, dmreq
);
1191 sector
= org_sector_of_dmreq(cc
, dmreq
);
1192 *sector
= cpu_to_le64(ctx
->cc_sector
- cc
->iv_offset
);
1194 /* For skcipher we use only the first sg item */
1195 sg_in
= &dmreq
->sg_in
[0];
1196 sg_out
= &dmreq
->sg_out
[0];
1198 sg_init_table(sg_in
, 1);
1199 sg_set_page(sg_in
, bv_in
.bv_page
, cc
->sector_size
, bv_in
.bv_offset
);
1201 sg_init_table(sg_out
, 1);
1202 sg_set_page(sg_out
, bv_out
.bv_page
, cc
->sector_size
, bv_out
.bv_offset
);
1204 if (cc
->iv_gen_ops
) {
1205 /* For READs use IV stored in integrity metadata */
1206 if (cc
->integrity_iv_size
&& bio_data_dir(ctx
->bio_in
) != WRITE
) {
1207 memcpy(org_iv
, tag_iv
, cc
->integrity_iv_size
);
1209 r
= cc
->iv_gen_ops
->generator(cc
, org_iv
, dmreq
);
1212 /* Store generated IV in integrity metadata */
1213 if (cc
->integrity_iv_size
)
1214 memcpy(tag_iv
, org_iv
, cc
->integrity_iv_size
);
1216 /* Working copy of IV, to be modified in crypto API */
1217 memcpy(iv
, org_iv
, cc
->iv_size
);
1220 skcipher_request_set_crypt(req
, sg_in
, sg_out
, cc
->sector_size
, iv
);
1222 if (bio_data_dir(ctx
->bio_in
) == WRITE
)
1223 r
= crypto_skcipher_encrypt(req
);
1225 r
= crypto_skcipher_decrypt(req
);
1227 if (!r
&& cc
->iv_gen_ops
&& cc
->iv_gen_ops
->post
)
1228 r
= cc
->iv_gen_ops
->post(cc
, org_iv
, dmreq
);
1230 bio_advance_iter(ctx
->bio_in
, &ctx
->iter_in
, cc
->sector_size
);
1231 bio_advance_iter(ctx
->bio_out
, &ctx
->iter_out
, cc
->sector_size
);
1236 static void kcryptd_async_done(struct crypto_async_request
*async_req
,
1239 static void crypt_alloc_req_skcipher(struct crypt_config
*cc
,
1240 struct convert_context
*ctx
)
1242 unsigned key_index
= ctx
->cc_sector
& (cc
->tfms_count
- 1);
1245 ctx
->r
.req
= mempool_alloc(&cc
->req_pool
, GFP_NOIO
);
1247 skcipher_request_set_tfm(ctx
->r
.req
, cc
->cipher_tfm
.tfms
[key_index
]);
1250 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1251 * requests if driver request queue is full.
1253 skcipher_request_set_callback(ctx
->r
.req
,
1254 CRYPTO_TFM_REQ_MAY_BACKLOG
| CRYPTO_TFM_REQ_MAY_SLEEP
,
1255 kcryptd_async_done
, dmreq_of_req(cc
, ctx
->r
.req
));
1258 static void crypt_alloc_req_aead(struct crypt_config
*cc
,
1259 struct convert_context
*ctx
)
1261 if (!ctx
->r
.req_aead
)
1262 ctx
->r
.req_aead
= mempool_alloc(&cc
->req_pool
, GFP_NOIO
);
1264 aead_request_set_tfm(ctx
->r
.req_aead
, cc
->cipher_tfm
.tfms_aead
[0]);
1267 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1268 * requests if driver request queue is full.
1270 aead_request_set_callback(ctx
->r
.req_aead
,
1271 CRYPTO_TFM_REQ_MAY_BACKLOG
| CRYPTO_TFM_REQ_MAY_SLEEP
,
1272 kcryptd_async_done
, dmreq_of_req(cc
, ctx
->r
.req_aead
));
1275 static void crypt_alloc_req(struct crypt_config
*cc
,
1276 struct convert_context
*ctx
)
1278 if (crypt_integrity_aead(cc
))
1279 crypt_alloc_req_aead(cc
, ctx
);
1281 crypt_alloc_req_skcipher(cc
, ctx
);
1284 static void crypt_free_req_skcipher(struct crypt_config
*cc
,
1285 struct skcipher_request
*req
, struct bio
*base_bio
)
1287 struct dm_crypt_io
*io
= dm_per_bio_data(base_bio
, cc
->per_bio_data_size
);
1289 if ((struct skcipher_request
*)(io
+ 1) != req
)
1290 mempool_free(req
, &cc
->req_pool
);
1293 static void crypt_free_req_aead(struct crypt_config
*cc
,
1294 struct aead_request
*req
, struct bio
*base_bio
)
1296 struct dm_crypt_io
*io
= dm_per_bio_data(base_bio
, cc
->per_bio_data_size
);
1298 if ((struct aead_request
*)(io
+ 1) != req
)
1299 mempool_free(req
, &cc
->req_pool
);
1302 static void crypt_free_req(struct crypt_config
*cc
, void *req
, struct bio
*base_bio
)
1304 if (crypt_integrity_aead(cc
))
1305 crypt_free_req_aead(cc
, req
, base_bio
);
1307 crypt_free_req_skcipher(cc
, req
, base_bio
);
1311 * Encrypt / decrypt data from one bio to another one (can be the same one)
1313 static blk_status_t
crypt_convert(struct crypt_config
*cc
,
1314 struct convert_context
*ctx
)
1316 unsigned int tag_offset
= 0;
1317 unsigned int sector_step
= cc
->sector_size
>> SECTOR_SHIFT
;
1320 atomic_set(&ctx
->cc_pending
, 1);
1322 while (ctx
->iter_in
.bi_size
&& ctx
->iter_out
.bi_size
) {
1324 crypt_alloc_req(cc
, ctx
);
1325 atomic_inc(&ctx
->cc_pending
);
1327 if (crypt_integrity_aead(cc
))
1328 r
= crypt_convert_block_aead(cc
, ctx
, ctx
->r
.req_aead
, tag_offset
);
1330 r
= crypt_convert_block_skcipher(cc
, ctx
, ctx
->r
.req
, tag_offset
);
1334 * The request was queued by a crypto driver
1335 * but the driver request queue is full, let's wait.
1338 wait_for_completion(&ctx
->restart
);
1339 reinit_completion(&ctx
->restart
);
1342 * The request is queued and processed asynchronously,
1343 * completion function kcryptd_async_done() will be called.
1347 ctx
->cc_sector
+= sector_step
;
1351 * The request was already processed (synchronously).
1354 atomic_dec(&ctx
->cc_pending
);
1355 ctx
->cc_sector
+= sector_step
;
1360 * There was a data integrity error.
1363 atomic_dec(&ctx
->cc_pending
);
1364 return BLK_STS_PROTECTION
;
1366 * There was an error while processing the request.
1369 atomic_dec(&ctx
->cc_pending
);
1370 return BLK_STS_IOERR
;
1377 static void crypt_free_buffer_pages(struct crypt_config
*cc
, struct bio
*clone
);
1380 * Generate a new unfragmented bio with the given size
1381 * This should never violate the device limitations (but only because
1382 * max_segment_size is being constrained to PAGE_SIZE).
1384 * This function may be called concurrently. If we allocate from the mempool
1385 * concurrently, there is a possibility of deadlock. For example, if we have
1386 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1387 * the mempool concurrently, it may deadlock in a situation where both processes
1388 * have allocated 128 pages and the mempool is exhausted.
1390 * In order to avoid this scenario we allocate the pages under a mutex.
1392 * In order to not degrade performance with excessive locking, we try
1393 * non-blocking allocations without a mutex first but on failure we fallback
1394 * to blocking allocations with a mutex.
1396 static struct bio
*crypt_alloc_buffer(struct dm_crypt_io
*io
, unsigned size
)
1398 struct crypt_config
*cc
= io
->cc
;
1400 unsigned int nr_iovecs
= (size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1401 gfp_t gfp_mask
= GFP_NOWAIT
| __GFP_HIGHMEM
;
1402 unsigned i
, len
, remaining_size
;
1406 if (unlikely(gfp_mask
& __GFP_DIRECT_RECLAIM
))
1407 mutex_lock(&cc
->bio_alloc_lock
);
1409 clone
= bio_alloc_bioset(GFP_NOIO
, nr_iovecs
, &cc
->bs
);
1413 clone_init(io
, clone
);
1415 remaining_size
= size
;
1417 for (i
= 0; i
< nr_iovecs
; i
++) {
1418 page
= mempool_alloc(&cc
->page_pool
, gfp_mask
);
1420 crypt_free_buffer_pages(cc
, clone
);
1422 gfp_mask
|= __GFP_DIRECT_RECLAIM
;
1426 len
= (remaining_size
> PAGE_SIZE
) ? PAGE_SIZE
: remaining_size
;
1428 bio_add_page(clone
, page
, len
, 0);
1430 remaining_size
-= len
;
1433 /* Allocate space for integrity tags */
1434 if (dm_crypt_integrity_io_alloc(io
, clone
)) {
1435 crypt_free_buffer_pages(cc
, clone
);
1440 if (unlikely(gfp_mask
& __GFP_DIRECT_RECLAIM
))
1441 mutex_unlock(&cc
->bio_alloc_lock
);
1446 static void crypt_free_buffer_pages(struct crypt_config
*cc
, struct bio
*clone
)
1451 bio_for_each_segment_all(bv
, clone
, i
) {
1452 BUG_ON(!bv
->bv_page
);
1453 mempool_free(bv
->bv_page
, &cc
->page_pool
);
1457 static void crypt_io_init(struct dm_crypt_io
*io
, struct crypt_config
*cc
,
1458 struct bio
*bio
, sector_t sector
)
1462 io
->sector
= sector
;
1464 io
->ctx
.r
.req
= NULL
;
1465 io
->integrity_metadata
= NULL
;
1466 io
->integrity_metadata_from_pool
= false;
1467 atomic_set(&io
->io_pending
, 0);
1470 static void crypt_inc_pending(struct dm_crypt_io
*io
)
1472 atomic_inc(&io
->io_pending
);
1476 * One of the bios was finished. Check for completion of
1477 * the whole request and correctly clean up the buffer.
1479 static void crypt_dec_pending(struct dm_crypt_io
*io
)
1481 struct crypt_config
*cc
= io
->cc
;
1482 struct bio
*base_bio
= io
->base_bio
;
1483 blk_status_t error
= io
->error
;
1485 if (!atomic_dec_and_test(&io
->io_pending
))
1489 crypt_free_req(cc
, io
->ctx
.r
.req
, base_bio
);
1491 if (unlikely(io
->integrity_metadata_from_pool
))
1492 mempool_free(io
->integrity_metadata
, &io
->cc
->tag_pool
);
1494 kfree(io
->integrity_metadata
);
1496 base_bio
->bi_status
= error
;
1497 bio_endio(base_bio
);
1501 * kcryptd/kcryptd_io:
1503 * Needed because it would be very unwise to do decryption in an
1504 * interrupt context.
1506 * kcryptd performs the actual encryption or decryption.
1508 * kcryptd_io performs the IO submission.
1510 * They must be separated as otherwise the final stages could be
1511 * starved by new requests which can block in the first stages due
1512 * to memory allocation.
1514 * The work is done per CPU global for all dm-crypt instances.
1515 * They should not depend on each other and do not block.
1517 static void crypt_endio(struct bio
*clone
)
1519 struct dm_crypt_io
*io
= clone
->bi_private
;
1520 struct crypt_config
*cc
= io
->cc
;
1521 unsigned rw
= bio_data_dir(clone
);
1525 * free the processed pages
1528 crypt_free_buffer_pages(cc
, clone
);
1530 error
= clone
->bi_status
;
1533 if (rw
== READ
&& !error
) {
1534 kcryptd_queue_crypt(io
);
1538 if (unlikely(error
))
1541 crypt_dec_pending(io
);
1544 static void clone_init(struct dm_crypt_io
*io
, struct bio
*clone
)
1546 struct crypt_config
*cc
= io
->cc
;
1548 clone
->bi_private
= io
;
1549 clone
->bi_end_io
= crypt_endio
;
1550 bio_set_dev(clone
, cc
->dev
->bdev
);
1551 clone
->bi_opf
= io
->base_bio
->bi_opf
;
1554 static int kcryptd_io_read(struct dm_crypt_io
*io
, gfp_t gfp
)
1556 struct crypt_config
*cc
= io
->cc
;
1560 * We need the original biovec array in order to decrypt
1561 * the whole bio data *afterwards* -- thanks to immutable
1562 * biovecs we don't need to worry about the block layer
1563 * modifying the biovec array; so leverage bio_clone_fast().
1565 clone
= bio_clone_fast(io
->base_bio
, gfp
, &cc
->bs
);
1569 crypt_inc_pending(io
);
1571 clone_init(io
, clone
);
1572 clone
->bi_iter
.bi_sector
= cc
->start
+ io
->sector
;
1574 if (dm_crypt_integrity_io_alloc(io
, clone
)) {
1575 crypt_dec_pending(io
);
1580 generic_make_request(clone
);
1584 static void kcryptd_io_read_work(struct work_struct
*work
)
1586 struct dm_crypt_io
*io
= container_of(work
, struct dm_crypt_io
, work
);
1588 crypt_inc_pending(io
);
1589 if (kcryptd_io_read(io
, GFP_NOIO
))
1590 io
->error
= BLK_STS_RESOURCE
;
1591 crypt_dec_pending(io
);
1594 static void kcryptd_queue_read(struct dm_crypt_io
*io
)
1596 struct crypt_config
*cc
= io
->cc
;
1598 INIT_WORK(&io
->work
, kcryptd_io_read_work
);
1599 queue_work(cc
->io_queue
, &io
->work
);
1602 static void kcryptd_io_write(struct dm_crypt_io
*io
)
1604 struct bio
*clone
= io
->ctx
.bio_out
;
1606 generic_make_request(clone
);
1609 #define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1611 static int dmcrypt_write(void *data
)
1613 struct crypt_config
*cc
= data
;
1614 struct dm_crypt_io
*io
;
1617 struct rb_root write_tree
;
1618 struct blk_plug plug
;
1620 spin_lock_irq(&cc
->write_thread_lock
);
1623 if (!RB_EMPTY_ROOT(&cc
->write_tree
))
1626 set_current_state(TASK_INTERRUPTIBLE
);
1628 spin_unlock_irq(&cc
->write_thread_lock
);
1630 if (unlikely(kthread_should_stop())) {
1631 set_current_state(TASK_RUNNING
);
1637 set_current_state(TASK_RUNNING
);
1638 spin_lock_irq(&cc
->write_thread_lock
);
1639 goto continue_locked
;
1642 write_tree
= cc
->write_tree
;
1643 cc
->write_tree
= RB_ROOT
;
1644 spin_unlock_irq(&cc
->write_thread_lock
);
1646 BUG_ON(rb_parent(write_tree
.rb_node
));
1649 * Note: we cannot walk the tree here with rb_next because
1650 * the structures may be freed when kcryptd_io_write is called.
1652 blk_start_plug(&plug
);
1654 io
= crypt_io_from_node(rb_first(&write_tree
));
1655 rb_erase(&io
->rb_node
, &write_tree
);
1656 kcryptd_io_write(io
);
1657 } while (!RB_EMPTY_ROOT(&write_tree
));
1658 blk_finish_plug(&plug
);
1663 static void kcryptd_crypt_write_io_submit(struct dm_crypt_io
*io
, int async
)
1665 struct bio
*clone
= io
->ctx
.bio_out
;
1666 struct crypt_config
*cc
= io
->cc
;
1667 unsigned long flags
;
1669 struct rb_node
**rbp
, *parent
;
1671 if (unlikely(io
->error
)) {
1672 crypt_free_buffer_pages(cc
, clone
);
1674 crypt_dec_pending(io
);
1678 /* crypt_convert should have filled the clone bio */
1679 BUG_ON(io
->ctx
.iter_out
.bi_size
);
1681 clone
->bi_iter
.bi_sector
= cc
->start
+ io
->sector
;
1683 if (likely(!async
) && test_bit(DM_CRYPT_NO_OFFLOAD
, &cc
->flags
)) {
1684 generic_make_request(clone
);
1688 spin_lock_irqsave(&cc
->write_thread_lock
, flags
);
1689 if (RB_EMPTY_ROOT(&cc
->write_tree
))
1690 wake_up_process(cc
->write_thread
);
1691 rbp
= &cc
->write_tree
.rb_node
;
1693 sector
= io
->sector
;
1696 if (sector
< crypt_io_from_node(parent
)->sector
)
1697 rbp
= &(*rbp
)->rb_left
;
1699 rbp
= &(*rbp
)->rb_right
;
1701 rb_link_node(&io
->rb_node
, parent
, rbp
);
1702 rb_insert_color(&io
->rb_node
, &cc
->write_tree
);
1703 spin_unlock_irqrestore(&cc
->write_thread_lock
, flags
);
1706 static void kcryptd_crypt_write_convert(struct dm_crypt_io
*io
)
1708 struct crypt_config
*cc
= io
->cc
;
1711 sector_t sector
= io
->sector
;
1715 * Prevent io from disappearing until this function completes.
1717 crypt_inc_pending(io
);
1718 crypt_convert_init(cc
, &io
->ctx
, NULL
, io
->base_bio
, sector
);
1720 clone
= crypt_alloc_buffer(io
, io
->base_bio
->bi_iter
.bi_size
);
1721 if (unlikely(!clone
)) {
1722 io
->error
= BLK_STS_IOERR
;
1726 io
->ctx
.bio_out
= clone
;
1727 io
->ctx
.iter_out
= clone
->bi_iter
;
1729 sector
+= bio_sectors(clone
);
1731 crypt_inc_pending(io
);
1732 r
= crypt_convert(cc
, &io
->ctx
);
1735 crypt_finished
= atomic_dec_and_test(&io
->ctx
.cc_pending
);
1737 /* Encryption was already finished, submit io now */
1738 if (crypt_finished
) {
1739 kcryptd_crypt_write_io_submit(io
, 0);
1740 io
->sector
= sector
;
1744 crypt_dec_pending(io
);
1747 static void kcryptd_crypt_read_done(struct dm_crypt_io
*io
)
1749 crypt_dec_pending(io
);
1752 static void kcryptd_crypt_read_convert(struct dm_crypt_io
*io
)
1754 struct crypt_config
*cc
= io
->cc
;
1757 crypt_inc_pending(io
);
1759 crypt_convert_init(cc
, &io
->ctx
, io
->base_bio
, io
->base_bio
,
1762 r
= crypt_convert(cc
, &io
->ctx
);
1766 if (atomic_dec_and_test(&io
->ctx
.cc_pending
))
1767 kcryptd_crypt_read_done(io
);
1769 crypt_dec_pending(io
);
1772 static void kcryptd_async_done(struct crypto_async_request
*async_req
,
1775 struct dm_crypt_request
*dmreq
= async_req
->data
;
1776 struct convert_context
*ctx
= dmreq
->ctx
;
1777 struct dm_crypt_io
*io
= container_of(ctx
, struct dm_crypt_io
, ctx
);
1778 struct crypt_config
*cc
= io
->cc
;
1781 * A request from crypto driver backlog is going to be processed now,
1782 * finish the completion and continue in crypt_convert().
1783 * (Callback will be called for the second time for this request.)
1785 if (error
== -EINPROGRESS
) {
1786 complete(&ctx
->restart
);
1790 if (!error
&& cc
->iv_gen_ops
&& cc
->iv_gen_ops
->post
)
1791 error
= cc
->iv_gen_ops
->post(cc
, org_iv_of_dmreq(cc
, dmreq
), dmreq
);
1793 if (error
== -EBADMSG
) {
1794 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1795 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc
, dmreq
)));
1796 io
->error
= BLK_STS_PROTECTION
;
1797 } else if (error
< 0)
1798 io
->error
= BLK_STS_IOERR
;
1800 crypt_free_req(cc
, req_of_dmreq(cc
, dmreq
), io
->base_bio
);
1802 if (!atomic_dec_and_test(&ctx
->cc_pending
))
1805 if (bio_data_dir(io
->base_bio
) == READ
)
1806 kcryptd_crypt_read_done(io
);
1808 kcryptd_crypt_write_io_submit(io
, 1);
1811 static void kcryptd_crypt(struct work_struct
*work
)
1813 struct dm_crypt_io
*io
= container_of(work
, struct dm_crypt_io
, work
);
1815 if (bio_data_dir(io
->base_bio
) == READ
)
1816 kcryptd_crypt_read_convert(io
);
1818 kcryptd_crypt_write_convert(io
);
1821 static void kcryptd_queue_crypt(struct dm_crypt_io
*io
)
1823 struct crypt_config
*cc
= io
->cc
;
1825 INIT_WORK(&io
->work
, kcryptd_crypt
);
1826 queue_work(cc
->crypt_queue
, &io
->work
);
1829 static void crypt_free_tfms_aead(struct crypt_config
*cc
)
1831 if (!cc
->cipher_tfm
.tfms_aead
)
1834 if (cc
->cipher_tfm
.tfms_aead
[0] && !IS_ERR(cc
->cipher_tfm
.tfms_aead
[0])) {
1835 crypto_free_aead(cc
->cipher_tfm
.tfms_aead
[0]);
1836 cc
->cipher_tfm
.tfms_aead
[0] = NULL
;
1839 kfree(cc
->cipher_tfm
.tfms_aead
);
1840 cc
->cipher_tfm
.tfms_aead
= NULL
;
1843 static void crypt_free_tfms_skcipher(struct crypt_config
*cc
)
1847 if (!cc
->cipher_tfm
.tfms
)
1850 for (i
= 0; i
< cc
->tfms_count
; i
++)
1851 if (cc
->cipher_tfm
.tfms
[i
] && !IS_ERR(cc
->cipher_tfm
.tfms
[i
])) {
1852 crypto_free_skcipher(cc
->cipher_tfm
.tfms
[i
]);
1853 cc
->cipher_tfm
.tfms
[i
] = NULL
;
1856 kfree(cc
->cipher_tfm
.tfms
);
1857 cc
->cipher_tfm
.tfms
= NULL
;
1860 static void crypt_free_tfms(struct crypt_config
*cc
)
1862 if (crypt_integrity_aead(cc
))
1863 crypt_free_tfms_aead(cc
);
1865 crypt_free_tfms_skcipher(cc
);
1868 static int crypt_alloc_tfms_skcipher(struct crypt_config
*cc
, char *ciphermode
)
1873 cc
->cipher_tfm
.tfms
= kcalloc(cc
->tfms_count
,
1874 sizeof(struct crypto_skcipher
*),
1876 if (!cc
->cipher_tfm
.tfms
)
1879 for (i
= 0; i
< cc
->tfms_count
; i
++) {
1880 cc
->cipher_tfm
.tfms
[i
] = crypto_alloc_skcipher(ciphermode
, 0, 0);
1881 if (IS_ERR(cc
->cipher_tfm
.tfms
[i
])) {
1882 err
= PTR_ERR(cc
->cipher_tfm
.tfms
[i
]);
1883 crypt_free_tfms(cc
);
1891 static int crypt_alloc_tfms_aead(struct crypt_config
*cc
, char *ciphermode
)
1895 cc
->cipher_tfm
.tfms
= kmalloc(sizeof(struct crypto_aead
*), GFP_KERNEL
);
1896 if (!cc
->cipher_tfm
.tfms
)
1899 cc
->cipher_tfm
.tfms_aead
[0] = crypto_alloc_aead(ciphermode
, 0, 0);
1900 if (IS_ERR(cc
->cipher_tfm
.tfms_aead
[0])) {
1901 err
= PTR_ERR(cc
->cipher_tfm
.tfms_aead
[0]);
1902 crypt_free_tfms(cc
);
1909 static int crypt_alloc_tfms(struct crypt_config
*cc
, char *ciphermode
)
1911 if (crypt_integrity_aead(cc
))
1912 return crypt_alloc_tfms_aead(cc
, ciphermode
);
1914 return crypt_alloc_tfms_skcipher(cc
, ciphermode
);
1917 static unsigned crypt_subkey_size(struct crypt_config
*cc
)
1919 return (cc
->key_size
- cc
->key_extra_size
) >> ilog2(cc
->tfms_count
);
1922 static unsigned crypt_authenckey_size(struct crypt_config
*cc
)
1924 return crypt_subkey_size(cc
) + RTA_SPACE(sizeof(struct crypto_authenc_key_param
));
1928 * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
1929 * the key must be for some reason in special format.
1930 * This funcion converts cc->key to this special format.
1932 static void crypt_copy_authenckey(char *p
, const void *key
,
1933 unsigned enckeylen
, unsigned authkeylen
)
1935 struct crypto_authenc_key_param
*param
;
1938 rta
= (struct rtattr
*)p
;
1939 param
= RTA_DATA(rta
);
1940 param
->enckeylen
= cpu_to_be32(enckeylen
);
1941 rta
->rta_len
= RTA_LENGTH(sizeof(*param
));
1942 rta
->rta_type
= CRYPTO_AUTHENC_KEYA_PARAM
;
1943 p
+= RTA_SPACE(sizeof(*param
));
1944 memcpy(p
, key
+ enckeylen
, authkeylen
);
1946 memcpy(p
, key
, enckeylen
);
1949 static int crypt_setkey(struct crypt_config
*cc
)
1951 unsigned subkey_size
;
1954 /* Ignore extra keys (which are used for IV etc) */
1955 subkey_size
= crypt_subkey_size(cc
);
1957 if (crypt_integrity_hmac(cc
)) {
1958 if (subkey_size
< cc
->key_mac_size
)
1961 crypt_copy_authenckey(cc
->authenc_key
, cc
->key
,
1962 subkey_size
- cc
->key_mac_size
,
1966 for (i
= 0; i
< cc
->tfms_count
; i
++) {
1967 if (crypt_integrity_hmac(cc
))
1968 r
= crypto_aead_setkey(cc
->cipher_tfm
.tfms_aead
[i
],
1969 cc
->authenc_key
, crypt_authenckey_size(cc
));
1970 else if (crypt_integrity_aead(cc
))
1971 r
= crypto_aead_setkey(cc
->cipher_tfm
.tfms_aead
[i
],
1972 cc
->key
+ (i
* subkey_size
),
1975 r
= crypto_skcipher_setkey(cc
->cipher_tfm
.tfms
[i
],
1976 cc
->key
+ (i
* subkey_size
),
1982 if (crypt_integrity_hmac(cc
))
1983 memzero_explicit(cc
->authenc_key
, crypt_authenckey_size(cc
));
1990 static bool contains_whitespace(const char *str
)
1993 if (isspace(*str
++))
1998 static int crypt_set_keyring_key(struct crypt_config
*cc
, const char *key_string
)
2000 char *new_key_string
, *key_desc
;
2003 const struct user_key_payload
*ukp
;
2006 * Reject key_string with whitespace. dm core currently lacks code for
2007 * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
2009 if (contains_whitespace(key_string
)) {
2010 DMERR("whitespace chars not allowed in key string");
2014 /* look for next ':' separating key_type from key_description */
2015 key_desc
= strpbrk(key_string
, ":");
2016 if (!key_desc
|| key_desc
== key_string
|| !strlen(key_desc
+ 1))
2019 if (strncmp(key_string
, "logon:", key_desc
- key_string
+ 1) &&
2020 strncmp(key_string
, "user:", key_desc
- key_string
+ 1))
2023 new_key_string
= kstrdup(key_string
, GFP_KERNEL
);
2024 if (!new_key_string
)
2027 key
= request_key(key_string
[0] == 'l' ? &key_type_logon
: &key_type_user
,
2028 key_desc
+ 1, NULL
);
2030 kzfree(new_key_string
);
2031 return PTR_ERR(key
);
2034 down_read(&key
->sem
);
2036 ukp
= user_key_payload_locked(key
);
2040 kzfree(new_key_string
);
2041 return -EKEYREVOKED
;
2044 if (cc
->key_size
!= ukp
->datalen
) {
2047 kzfree(new_key_string
);
2051 memcpy(cc
->key
, ukp
->data
, cc
->key_size
);
2056 /* clear the flag since following operations may invalidate previously valid key */
2057 clear_bit(DM_CRYPT_KEY_VALID
, &cc
->flags
);
2059 ret
= crypt_setkey(cc
);
2062 set_bit(DM_CRYPT_KEY_VALID
, &cc
->flags
);
2063 kzfree(cc
->key_string
);
2064 cc
->key_string
= new_key_string
;
2066 kzfree(new_key_string
);
2071 static int get_key_size(char **key_string
)
2076 if (*key_string
[0] != ':')
2077 return strlen(*key_string
) >> 1;
2079 /* look for next ':' in key string */
2080 colon
= strpbrk(*key_string
+ 1, ":");
2084 if (sscanf(*key_string
+ 1, "%u%c", &ret
, &dummy
) != 2 || dummy
!= ':')
2087 *key_string
= colon
;
2089 /* remaining key string should be :<logon|user>:<key_desc> */
2096 static int crypt_set_keyring_key(struct crypt_config
*cc
, const char *key_string
)
2101 static int get_key_size(char **key_string
)
2103 return (*key_string
[0] == ':') ? -EINVAL
: strlen(*key_string
) >> 1;
2108 static int crypt_set_key(struct crypt_config
*cc
, char *key
)
2111 int key_string_len
= strlen(key
);
2113 /* Hyphen (which gives a key_size of zero) means there is no key. */
2114 if (!cc
->key_size
&& strcmp(key
, "-"))
2117 /* ':' means the key is in kernel keyring, short-circuit normal key processing */
2118 if (key
[0] == ':') {
2119 r
= crypt_set_keyring_key(cc
, key
+ 1);
2123 /* clear the flag since following operations may invalidate previously valid key */
2124 clear_bit(DM_CRYPT_KEY_VALID
, &cc
->flags
);
2126 /* wipe references to any kernel keyring key */
2127 kzfree(cc
->key_string
);
2128 cc
->key_string
= NULL
;
2130 /* Decode key from its hex representation. */
2131 if (cc
->key_size
&& hex2bin(cc
->key
, key
, cc
->key_size
) < 0)
2134 r
= crypt_setkey(cc
);
2136 set_bit(DM_CRYPT_KEY_VALID
, &cc
->flags
);
2139 /* Hex key string not needed after here, so wipe it. */
2140 memset(key
, '0', key_string_len
);
2145 static int crypt_wipe_key(struct crypt_config
*cc
)
2149 clear_bit(DM_CRYPT_KEY_VALID
, &cc
->flags
);
2150 get_random_bytes(&cc
->key
, cc
->key_size
);
2151 kzfree(cc
->key_string
);
2152 cc
->key_string
= NULL
;
2153 r
= crypt_setkey(cc
);
2154 memset(&cc
->key
, 0, cc
->key_size
* sizeof(u8
));
2159 static void crypt_calculate_pages_per_client(void)
2161 unsigned long pages
= (totalram_pages
- totalhigh_pages
) * DM_CRYPT_MEMORY_PERCENT
/ 100;
2163 if (!dm_crypt_clients_n
)
2166 pages
/= dm_crypt_clients_n
;
2167 if (pages
< DM_CRYPT_MIN_PAGES_PER_CLIENT
)
2168 pages
= DM_CRYPT_MIN_PAGES_PER_CLIENT
;
2169 dm_crypt_pages_per_client
= pages
;
2172 static void *crypt_page_alloc(gfp_t gfp_mask
, void *pool_data
)
2174 struct crypt_config
*cc
= pool_data
;
2177 if (unlikely(percpu_counter_compare(&cc
->n_allocated_pages
, dm_crypt_pages_per_client
) >= 0) &&
2178 likely(gfp_mask
& __GFP_NORETRY
))
2181 page
= alloc_page(gfp_mask
);
2182 if (likely(page
!= NULL
))
2183 percpu_counter_add(&cc
->n_allocated_pages
, 1);
2188 static void crypt_page_free(void *page
, void *pool_data
)
2190 struct crypt_config
*cc
= pool_data
;
2193 percpu_counter_sub(&cc
->n_allocated_pages
, 1);
2196 static void crypt_dtr(struct dm_target
*ti
)
2198 struct crypt_config
*cc
= ti
->private;
2205 if (cc
->write_thread
)
2206 kthread_stop(cc
->write_thread
);
2209 destroy_workqueue(cc
->io_queue
);
2210 if (cc
->crypt_queue
)
2211 destroy_workqueue(cc
->crypt_queue
);
2213 crypt_free_tfms(cc
);
2215 bioset_exit(&cc
->bs
);
2217 mempool_exit(&cc
->page_pool
);
2218 mempool_exit(&cc
->req_pool
);
2219 mempool_exit(&cc
->tag_pool
);
2221 WARN_ON(percpu_counter_sum(&cc
->n_allocated_pages
) != 0);
2222 percpu_counter_destroy(&cc
->n_allocated_pages
);
2224 if (cc
->iv_gen_ops
&& cc
->iv_gen_ops
->dtr
)
2225 cc
->iv_gen_ops
->dtr(cc
);
2228 dm_put_device(ti
, cc
->dev
);
2231 kzfree(cc
->cipher_string
);
2232 kzfree(cc
->key_string
);
2233 kzfree(cc
->cipher_auth
);
2234 kzfree(cc
->authenc_key
);
2236 mutex_destroy(&cc
->bio_alloc_lock
);
2238 /* Must zero key material before freeing */
2241 spin_lock(&dm_crypt_clients_lock
);
2242 WARN_ON(!dm_crypt_clients_n
);
2243 dm_crypt_clients_n
--;
2244 crypt_calculate_pages_per_client();
2245 spin_unlock(&dm_crypt_clients_lock
);
2248 static int crypt_ctr_ivmode(struct dm_target
*ti
, const char *ivmode
)
2250 struct crypt_config
*cc
= ti
->private;
2252 if (crypt_integrity_aead(cc
))
2253 cc
->iv_size
= crypto_aead_ivsize(any_tfm_aead(cc
));
2255 cc
->iv_size
= crypto_skcipher_ivsize(any_tfm(cc
));
2258 /* at least a 64 bit sector number should fit in our buffer */
2259 cc
->iv_size
= max(cc
->iv_size
,
2260 (unsigned int)(sizeof(u64
) / sizeof(u8
)));
2262 DMWARN("Selected cipher does not support IVs");
2266 /* Choose ivmode, see comments at iv code. */
2268 cc
->iv_gen_ops
= NULL
;
2269 else if (strcmp(ivmode
, "plain") == 0)
2270 cc
->iv_gen_ops
= &crypt_iv_plain_ops
;
2271 else if (strcmp(ivmode
, "plain64") == 0)
2272 cc
->iv_gen_ops
= &crypt_iv_plain64_ops
;
2273 else if (strcmp(ivmode
, "plain64be") == 0)
2274 cc
->iv_gen_ops
= &crypt_iv_plain64be_ops
;
2275 else if (strcmp(ivmode
, "essiv") == 0)
2276 cc
->iv_gen_ops
= &crypt_iv_essiv_ops
;
2277 else if (strcmp(ivmode
, "benbi") == 0)
2278 cc
->iv_gen_ops
= &crypt_iv_benbi_ops
;
2279 else if (strcmp(ivmode
, "null") == 0)
2280 cc
->iv_gen_ops
= &crypt_iv_null_ops
;
2281 else if (strcmp(ivmode
, "lmk") == 0) {
2282 cc
->iv_gen_ops
= &crypt_iv_lmk_ops
;
2284 * Version 2 and 3 is recognised according
2285 * to length of provided multi-key string.
2286 * If present (version 3), last key is used as IV seed.
2287 * All keys (including IV seed) are always the same size.
2289 if (cc
->key_size
% cc
->key_parts
) {
2291 cc
->key_extra_size
= cc
->key_size
/ cc
->key_parts
;
2293 } else if (strcmp(ivmode
, "tcw") == 0) {
2294 cc
->iv_gen_ops
= &crypt_iv_tcw_ops
;
2295 cc
->key_parts
+= 2; /* IV + whitening */
2296 cc
->key_extra_size
= cc
->iv_size
+ TCW_WHITENING_SIZE
;
2297 } else if (strcmp(ivmode
, "random") == 0) {
2298 cc
->iv_gen_ops
= &crypt_iv_random_ops
;
2299 /* Need storage space in integrity fields. */
2300 cc
->integrity_iv_size
= cc
->iv_size
;
2302 ti
->error
= "Invalid IV mode";
2310 * Workaround to parse cipher algorithm from crypto API spec.
2311 * The cc->cipher is currently used only in ESSIV.
2312 * This should be probably done by crypto-api calls (once available...)
2314 static int crypt_ctr_blkdev_cipher(struct crypt_config
*cc
)
2316 const char *alg_name
= NULL
;
2319 if (crypt_integrity_aead(cc
)) {
2320 alg_name
= crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc
)));
2323 if (crypt_integrity_hmac(cc
)) {
2324 alg_name
= strchr(alg_name
, ',');
2330 alg_name
= crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc
)));
2335 start
= strchr(alg_name
, '(');
2336 end
= strchr(alg_name
, ')');
2338 if (!start
&& !end
) {
2339 cc
->cipher
= kstrdup(alg_name
, GFP_KERNEL
);
2340 return cc
->cipher
? 0 : -ENOMEM
;
2343 if (!start
|| !end
|| ++start
>= end
)
2346 cc
->cipher
= kzalloc(end
- start
+ 1, GFP_KERNEL
);
2350 strncpy(cc
->cipher
, start
, end
- start
);
2356 * Workaround to parse HMAC algorithm from AEAD crypto API spec.
2357 * The HMAC is needed to calculate tag size (HMAC digest size).
2358 * This should be probably done by crypto-api calls (once available...)
2360 static int crypt_ctr_auth_cipher(struct crypt_config
*cc
, char *cipher_api
)
2362 char *start
, *end
, *mac_alg
= NULL
;
2363 struct crypto_ahash
*mac
;
2365 if (!strstarts(cipher_api
, "authenc("))
2368 start
= strchr(cipher_api
, '(');
2369 end
= strchr(cipher_api
, ',');
2370 if (!start
|| !end
|| ++start
> end
)
2373 mac_alg
= kzalloc(end
- start
+ 1, GFP_KERNEL
);
2376 strncpy(mac_alg
, start
, end
- start
);
2378 mac
= crypto_alloc_ahash(mac_alg
, 0, 0);
2382 return PTR_ERR(mac
);
2384 cc
->key_mac_size
= crypto_ahash_digestsize(mac
);
2385 crypto_free_ahash(mac
);
2387 cc
->authenc_key
= kmalloc(crypt_authenckey_size(cc
), GFP_KERNEL
);
2388 if (!cc
->authenc_key
)
2394 static int crypt_ctr_cipher_new(struct dm_target
*ti
, char *cipher_in
, char *key
,
2395 char **ivmode
, char **ivopts
)
2397 struct crypt_config
*cc
= ti
->private;
2398 char *tmp
, *cipher_api
;
2404 * New format (capi: prefix)
2405 * capi:cipher_api_spec-iv:ivopts
2407 tmp
= &cipher_in
[strlen("capi:")];
2408 cipher_api
= strsep(&tmp
, "-");
2409 *ivmode
= strsep(&tmp
, ":");
2412 if (*ivmode
&& !strcmp(*ivmode
, "lmk"))
2413 cc
->tfms_count
= 64;
2415 cc
->key_parts
= cc
->tfms_count
;
2417 /* Allocate cipher */
2418 ret
= crypt_alloc_tfms(cc
, cipher_api
);
2420 ti
->error
= "Error allocating crypto tfm";
2424 /* Alloc AEAD, can be used only in new format. */
2425 if (crypt_integrity_aead(cc
)) {
2426 ret
= crypt_ctr_auth_cipher(cc
, cipher_api
);
2428 ti
->error
= "Invalid AEAD cipher spec";
2431 cc
->iv_size
= crypto_aead_ivsize(any_tfm_aead(cc
));
2433 cc
->iv_size
= crypto_skcipher_ivsize(any_tfm(cc
));
2435 ret
= crypt_ctr_blkdev_cipher(cc
);
2437 ti
->error
= "Cannot allocate cipher string";
2444 static int crypt_ctr_cipher_old(struct dm_target
*ti
, char *cipher_in
, char *key
,
2445 char **ivmode
, char **ivopts
)
2447 struct crypt_config
*cc
= ti
->private;
2448 char *tmp
, *cipher
, *chainmode
, *keycount
;
2449 char *cipher_api
= NULL
;
2453 if (strchr(cipher_in
, '(') || crypt_integrity_aead(cc
)) {
2454 ti
->error
= "Bad cipher specification";
2459 * Legacy dm-crypt cipher specification
2460 * cipher[:keycount]-mode-iv:ivopts
2463 keycount
= strsep(&tmp
, "-");
2464 cipher
= strsep(&keycount
, ":");
2468 else if (sscanf(keycount
, "%u%c", &cc
->tfms_count
, &dummy
) != 1 ||
2469 !is_power_of_2(cc
->tfms_count
)) {
2470 ti
->error
= "Bad cipher key count specification";
2473 cc
->key_parts
= cc
->tfms_count
;
2475 cc
->cipher
= kstrdup(cipher
, GFP_KERNEL
);
2479 chainmode
= strsep(&tmp
, "-");
2480 *ivopts
= strsep(&tmp
, "-");
2481 *ivmode
= strsep(&*ivopts
, ":");
2484 DMWARN("Ignoring unexpected additional cipher options");
2487 * For compatibility with the original dm-crypt mapping format, if
2488 * only the cipher name is supplied, use cbc-plain.
2490 if (!chainmode
|| (!strcmp(chainmode
, "plain") && !*ivmode
)) {
2495 if (strcmp(chainmode
, "ecb") && !*ivmode
) {
2496 ti
->error
= "IV mechanism required";
2500 cipher_api
= kmalloc(CRYPTO_MAX_ALG_NAME
, GFP_KERNEL
);
2504 ret
= snprintf(cipher_api
, CRYPTO_MAX_ALG_NAME
,
2505 "%s(%s)", chainmode
, cipher
);
2511 /* Allocate cipher */
2512 ret
= crypt_alloc_tfms(cc
, cipher_api
);
2514 ti
->error
= "Error allocating crypto tfm";
2522 ti
->error
= "Cannot allocate cipher strings";
2526 static int crypt_ctr_cipher(struct dm_target
*ti
, char *cipher_in
, char *key
)
2528 struct crypt_config
*cc
= ti
->private;
2529 char *ivmode
= NULL
, *ivopts
= NULL
;
2532 cc
->cipher_string
= kstrdup(cipher_in
, GFP_KERNEL
);
2533 if (!cc
->cipher_string
) {
2534 ti
->error
= "Cannot allocate cipher strings";
2538 if (strstarts(cipher_in
, "capi:"))
2539 ret
= crypt_ctr_cipher_new(ti
, cipher_in
, key
, &ivmode
, &ivopts
);
2541 ret
= crypt_ctr_cipher_old(ti
, cipher_in
, key
, &ivmode
, &ivopts
);
2546 ret
= crypt_ctr_ivmode(ti
, ivmode
);
2550 /* Initialize and set key */
2551 ret
= crypt_set_key(cc
, key
);
2553 ti
->error
= "Error decoding and setting key";
2558 if (cc
->iv_gen_ops
&& cc
->iv_gen_ops
->ctr
) {
2559 ret
= cc
->iv_gen_ops
->ctr(cc
, ti
, ivopts
);
2561 ti
->error
= "Error creating IV";
2566 /* Initialize IV (set keys for ESSIV etc) */
2567 if (cc
->iv_gen_ops
&& cc
->iv_gen_ops
->init
) {
2568 ret
= cc
->iv_gen_ops
->init(cc
);
2570 ti
->error
= "Error initialising IV";
2575 /* wipe the kernel key payload copy */
2577 memset(cc
->key
, 0, cc
->key_size
* sizeof(u8
));
2582 static int crypt_ctr_optional(struct dm_target
*ti
, unsigned int argc
, char **argv
)
2584 struct crypt_config
*cc
= ti
->private;
2585 struct dm_arg_set as
;
2586 static const struct dm_arg _args
[] = {
2587 {0, 6, "Invalid number of feature args"},
2589 unsigned int opt_params
, val
;
2590 const char *opt_string
, *sval
;
2594 /* Optional parameters */
2598 ret
= dm_read_arg_group(_args
, &as
, &opt_params
, &ti
->error
);
2602 while (opt_params
--) {
2603 opt_string
= dm_shift_arg(&as
);
2605 ti
->error
= "Not enough feature arguments";
2609 if (!strcasecmp(opt_string
, "allow_discards"))
2610 ti
->num_discard_bios
= 1;
2612 else if (!strcasecmp(opt_string
, "same_cpu_crypt"))
2613 set_bit(DM_CRYPT_SAME_CPU
, &cc
->flags
);
2615 else if (!strcasecmp(opt_string
, "submit_from_crypt_cpus"))
2616 set_bit(DM_CRYPT_NO_OFFLOAD
, &cc
->flags
);
2617 else if (sscanf(opt_string
, "integrity:%u:", &val
) == 1) {
2618 if (val
== 0 || val
> MAX_TAG_SIZE
) {
2619 ti
->error
= "Invalid integrity arguments";
2622 cc
->on_disk_tag_size
= val
;
2623 sval
= strchr(opt_string
+ strlen("integrity:"), ':') + 1;
2624 if (!strcasecmp(sval
, "aead")) {
2625 set_bit(CRYPT_MODE_INTEGRITY_AEAD
, &cc
->cipher_flags
);
2626 } else if (strcasecmp(sval
, "none")) {
2627 ti
->error
= "Unknown integrity profile";
2631 cc
->cipher_auth
= kstrdup(sval
, GFP_KERNEL
);
2632 if (!cc
->cipher_auth
)
2634 } else if (sscanf(opt_string
, "sector_size:%hu%c", &cc
->sector_size
, &dummy
) == 1) {
2635 if (cc
->sector_size
< (1 << SECTOR_SHIFT
) ||
2636 cc
->sector_size
> 4096 ||
2637 (cc
->sector_size
& (cc
->sector_size
- 1))) {
2638 ti
->error
= "Invalid feature value for sector_size";
2641 if (ti
->len
& ((cc
->sector_size
>> SECTOR_SHIFT
) - 1)) {
2642 ti
->error
= "Device size is not multiple of sector_size feature";
2645 cc
->sector_shift
= __ffs(cc
->sector_size
) - SECTOR_SHIFT
;
2646 } else if (!strcasecmp(opt_string
, "iv_large_sectors"))
2647 set_bit(CRYPT_IV_LARGE_SECTORS
, &cc
->cipher_flags
);
2649 ti
->error
= "Invalid feature arguments";
2658 * Construct an encryption mapping:
2659 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
2661 static int crypt_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
2663 struct crypt_config
*cc
;
2665 unsigned int align_mask
;
2666 unsigned long long tmpll
;
2668 size_t iv_size_padding
, additional_req_size
;
2672 ti
->error
= "Not enough arguments";
2676 key_size
= get_key_size(&argv
[1]);
2678 ti
->error
= "Cannot parse key size";
2682 cc
= kzalloc(sizeof(*cc
) + key_size
* sizeof(u8
), GFP_KERNEL
);
2684 ti
->error
= "Cannot allocate encryption context";
2687 cc
->key_size
= key_size
;
2688 cc
->sector_size
= (1 << SECTOR_SHIFT
);
2689 cc
->sector_shift
= 0;
2693 spin_lock(&dm_crypt_clients_lock
);
2694 dm_crypt_clients_n
++;
2695 crypt_calculate_pages_per_client();
2696 spin_unlock(&dm_crypt_clients_lock
);
2698 ret
= percpu_counter_init(&cc
->n_allocated_pages
, 0, GFP_KERNEL
);
2702 /* Optional parameters need to be read before cipher constructor */
2704 ret
= crypt_ctr_optional(ti
, argc
- 5, &argv
[5]);
2709 ret
= crypt_ctr_cipher(ti
, argv
[0], argv
[1]);
2713 if (crypt_integrity_aead(cc
)) {
2714 cc
->dmreq_start
= sizeof(struct aead_request
);
2715 cc
->dmreq_start
+= crypto_aead_reqsize(any_tfm_aead(cc
));
2716 align_mask
= crypto_aead_alignmask(any_tfm_aead(cc
));
2718 cc
->dmreq_start
= sizeof(struct skcipher_request
);
2719 cc
->dmreq_start
+= crypto_skcipher_reqsize(any_tfm(cc
));
2720 align_mask
= crypto_skcipher_alignmask(any_tfm(cc
));
2722 cc
->dmreq_start
= ALIGN(cc
->dmreq_start
, __alignof__(struct dm_crypt_request
));
2724 if (align_mask
< CRYPTO_MINALIGN
) {
2725 /* Allocate the padding exactly */
2726 iv_size_padding
= -(cc
->dmreq_start
+ sizeof(struct dm_crypt_request
))
2730 * If the cipher requires greater alignment than kmalloc
2731 * alignment, we don't know the exact position of the
2732 * initialization vector. We must assume worst case.
2734 iv_size_padding
= align_mask
;
2737 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
2738 additional_req_size
= sizeof(struct dm_crypt_request
) +
2739 iv_size_padding
+ cc
->iv_size
+
2742 sizeof(unsigned int);
2744 ret
= mempool_init_kmalloc_pool(&cc
->req_pool
, MIN_IOS
, cc
->dmreq_start
+ additional_req_size
);
2746 ti
->error
= "Cannot allocate crypt request mempool";
2750 cc
->per_bio_data_size
= ti
->per_io_data_size
=
2751 ALIGN(sizeof(struct dm_crypt_io
) + cc
->dmreq_start
+ additional_req_size
,
2752 ARCH_KMALLOC_MINALIGN
);
2754 ret
= mempool_init(&cc
->page_pool
, BIO_MAX_PAGES
, crypt_page_alloc
, crypt_page_free
, cc
);
2756 ti
->error
= "Cannot allocate page mempool";
2760 ret
= bioset_init(&cc
->bs
, MIN_IOS
, 0, BIOSET_NEED_BVECS
);
2762 ti
->error
= "Cannot allocate crypt bioset";
2766 mutex_init(&cc
->bio_alloc_lock
);
2769 if ((sscanf(argv
[2], "%llu%c", &tmpll
, &dummy
) != 1) ||
2770 (tmpll
& ((cc
->sector_size
>> SECTOR_SHIFT
) - 1))) {
2771 ti
->error
= "Invalid iv_offset sector";
2774 cc
->iv_offset
= tmpll
;
2776 ret
= dm_get_device(ti
, argv
[3], dm_table_get_mode(ti
->table
), &cc
->dev
);
2778 ti
->error
= "Device lookup failed";
2783 if (sscanf(argv
[4], "%llu%c", &tmpll
, &dummy
) != 1) {
2784 ti
->error
= "Invalid device sector";
2789 if (crypt_integrity_aead(cc
) || cc
->integrity_iv_size
) {
2790 ret
= crypt_integrity_ctr(cc
, ti
);
2794 cc
->tag_pool_max_sectors
= POOL_ENTRY_SIZE
/ cc
->on_disk_tag_size
;
2795 if (!cc
->tag_pool_max_sectors
)
2796 cc
->tag_pool_max_sectors
= 1;
2798 ret
= mempool_init_kmalloc_pool(&cc
->tag_pool
, MIN_IOS
,
2799 cc
->tag_pool_max_sectors
* cc
->on_disk_tag_size
);
2801 ti
->error
= "Cannot allocate integrity tags mempool";
2805 cc
->tag_pool_max_sectors
<<= cc
->sector_shift
;
2809 cc
->io_queue
= alloc_workqueue("kcryptd_io", WQ_HIGHPRI
| WQ_CPU_INTENSIVE
| WQ_MEM_RECLAIM
, 1);
2810 if (!cc
->io_queue
) {
2811 ti
->error
= "Couldn't create kcryptd io queue";
2815 if (test_bit(DM_CRYPT_SAME_CPU
, &cc
->flags
))
2816 cc
->crypt_queue
= alloc_workqueue("kcryptd", WQ_HIGHPRI
| WQ_CPU_INTENSIVE
| WQ_MEM_RECLAIM
, 1);
2818 cc
->crypt_queue
= alloc_workqueue("kcryptd",
2819 WQ_HIGHPRI
| WQ_CPU_INTENSIVE
| WQ_MEM_RECLAIM
| WQ_UNBOUND
,
2821 if (!cc
->crypt_queue
) {
2822 ti
->error
= "Couldn't create kcryptd queue";
2826 spin_lock_init(&cc
->write_thread_lock
);
2827 cc
->write_tree
= RB_ROOT
;
2829 cc
->write_thread
= kthread_create(dmcrypt_write
, cc
, "dmcrypt_write");
2830 if (IS_ERR(cc
->write_thread
)) {
2831 ret
= PTR_ERR(cc
->write_thread
);
2832 cc
->write_thread
= NULL
;
2833 ti
->error
= "Couldn't spawn write thread";
2836 wake_up_process(cc
->write_thread
);
2838 ti
->num_flush_bios
= 1;
2847 static int crypt_map(struct dm_target
*ti
, struct bio
*bio
)
2849 struct dm_crypt_io
*io
;
2850 struct crypt_config
*cc
= ti
->private;
2853 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
2854 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
2855 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
2857 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
||
2858 bio_op(bio
) == REQ_OP_DISCARD
)) {
2859 bio_set_dev(bio
, cc
->dev
->bdev
);
2860 if (bio_sectors(bio
))
2861 bio
->bi_iter
.bi_sector
= cc
->start
+
2862 dm_target_offset(ti
, bio
->bi_iter
.bi_sector
);
2863 return DM_MAPIO_REMAPPED
;
2867 * Check if bio is too large, split as needed.
2869 if (unlikely(bio
->bi_iter
.bi_size
> (BIO_MAX_PAGES
<< PAGE_SHIFT
)) &&
2870 (bio_data_dir(bio
) == WRITE
|| cc
->on_disk_tag_size
))
2871 dm_accept_partial_bio(bio
, ((BIO_MAX_PAGES
<< PAGE_SHIFT
) >> SECTOR_SHIFT
));
2874 * Ensure that bio is a multiple of internal sector encryption size
2875 * and is aligned to this size as defined in IO hints.
2877 if (unlikely((bio
->bi_iter
.bi_sector
& ((cc
->sector_size
>> SECTOR_SHIFT
) - 1)) != 0))
2878 return DM_MAPIO_KILL
;
2880 if (unlikely(bio
->bi_iter
.bi_size
& (cc
->sector_size
- 1)))
2881 return DM_MAPIO_KILL
;
2883 io
= dm_per_bio_data(bio
, cc
->per_bio_data_size
);
2884 crypt_io_init(io
, cc
, bio
, dm_target_offset(ti
, bio
->bi_iter
.bi_sector
));
2886 if (cc
->on_disk_tag_size
) {
2887 unsigned tag_len
= cc
->on_disk_tag_size
* (bio_sectors(bio
) >> cc
->sector_shift
);
2889 if (unlikely(tag_len
> KMALLOC_MAX_SIZE
) ||
2890 unlikely(!(io
->integrity_metadata
= kmalloc(tag_len
,
2891 GFP_NOIO
| __GFP_NORETRY
| __GFP_NOMEMALLOC
| __GFP_NOWARN
)))) {
2892 if (bio_sectors(bio
) > cc
->tag_pool_max_sectors
)
2893 dm_accept_partial_bio(bio
, cc
->tag_pool_max_sectors
);
2894 io
->integrity_metadata
= mempool_alloc(&cc
->tag_pool
, GFP_NOIO
);
2895 io
->integrity_metadata_from_pool
= true;
2899 if (crypt_integrity_aead(cc
))
2900 io
->ctx
.r
.req_aead
= (struct aead_request
*)(io
+ 1);
2902 io
->ctx
.r
.req
= (struct skcipher_request
*)(io
+ 1);
2904 if (bio_data_dir(io
->base_bio
) == READ
) {
2905 if (kcryptd_io_read(io
, GFP_NOWAIT
))
2906 kcryptd_queue_read(io
);
2908 kcryptd_queue_crypt(io
);
2910 return DM_MAPIO_SUBMITTED
;
2913 static void crypt_status(struct dm_target
*ti
, status_type_t type
,
2914 unsigned status_flags
, char *result
, unsigned maxlen
)
2916 struct crypt_config
*cc
= ti
->private;
2918 int num_feature_args
= 0;
2921 case STATUSTYPE_INFO
:
2925 case STATUSTYPE_TABLE
:
2926 DMEMIT("%s ", cc
->cipher_string
);
2928 if (cc
->key_size
> 0) {
2930 DMEMIT(":%u:%s", cc
->key_size
, cc
->key_string
);
2932 for (i
= 0; i
< cc
->key_size
; i
++)
2933 DMEMIT("%02x", cc
->key
[i
]);
2937 DMEMIT(" %llu %s %llu", (unsigned long long)cc
->iv_offset
,
2938 cc
->dev
->name
, (unsigned long long)cc
->start
);
2940 num_feature_args
+= !!ti
->num_discard_bios
;
2941 num_feature_args
+= test_bit(DM_CRYPT_SAME_CPU
, &cc
->flags
);
2942 num_feature_args
+= test_bit(DM_CRYPT_NO_OFFLOAD
, &cc
->flags
);
2943 num_feature_args
+= cc
->sector_size
!= (1 << SECTOR_SHIFT
);
2944 num_feature_args
+= test_bit(CRYPT_IV_LARGE_SECTORS
, &cc
->cipher_flags
);
2945 if (cc
->on_disk_tag_size
)
2947 if (num_feature_args
) {
2948 DMEMIT(" %d", num_feature_args
);
2949 if (ti
->num_discard_bios
)
2950 DMEMIT(" allow_discards");
2951 if (test_bit(DM_CRYPT_SAME_CPU
, &cc
->flags
))
2952 DMEMIT(" same_cpu_crypt");
2953 if (test_bit(DM_CRYPT_NO_OFFLOAD
, &cc
->flags
))
2954 DMEMIT(" submit_from_crypt_cpus");
2955 if (cc
->on_disk_tag_size
)
2956 DMEMIT(" integrity:%u:%s", cc
->on_disk_tag_size
, cc
->cipher_auth
);
2957 if (cc
->sector_size
!= (1 << SECTOR_SHIFT
))
2958 DMEMIT(" sector_size:%d", cc
->sector_size
);
2959 if (test_bit(CRYPT_IV_LARGE_SECTORS
, &cc
->cipher_flags
))
2960 DMEMIT(" iv_large_sectors");
2967 static void crypt_postsuspend(struct dm_target
*ti
)
2969 struct crypt_config
*cc
= ti
->private;
2971 set_bit(DM_CRYPT_SUSPENDED
, &cc
->flags
);
2974 static int crypt_preresume(struct dm_target
*ti
)
2976 struct crypt_config
*cc
= ti
->private;
2978 if (!test_bit(DM_CRYPT_KEY_VALID
, &cc
->flags
)) {
2979 DMERR("aborting resume - crypt key is not set.");
2986 static void crypt_resume(struct dm_target
*ti
)
2988 struct crypt_config
*cc
= ti
->private;
2990 clear_bit(DM_CRYPT_SUSPENDED
, &cc
->flags
);
2993 /* Message interface
2997 static int crypt_message(struct dm_target
*ti
, unsigned argc
, char **argv
,
2998 char *result
, unsigned maxlen
)
3000 struct crypt_config
*cc
= ti
->private;
3001 int key_size
, ret
= -EINVAL
;
3006 if (!strcasecmp(argv
[0], "key")) {
3007 if (!test_bit(DM_CRYPT_SUSPENDED
, &cc
->flags
)) {
3008 DMWARN("not suspended during key manipulation.");
3011 if (argc
== 3 && !strcasecmp(argv
[1], "set")) {
3012 /* The key size may not be changed. */
3013 key_size
= get_key_size(&argv
[2]);
3014 if (key_size
< 0 || cc
->key_size
!= key_size
) {
3015 memset(argv
[2], '0', strlen(argv
[2]));
3019 ret
= crypt_set_key(cc
, argv
[2]);
3022 if (cc
->iv_gen_ops
&& cc
->iv_gen_ops
->init
)
3023 ret
= cc
->iv_gen_ops
->init(cc
);
3024 /* wipe the kernel key payload copy */
3026 memset(cc
->key
, 0, cc
->key_size
* sizeof(u8
));
3029 if (argc
== 2 && !strcasecmp(argv
[1], "wipe")) {
3030 if (cc
->iv_gen_ops
&& cc
->iv_gen_ops
->wipe
) {
3031 ret
= cc
->iv_gen_ops
->wipe(cc
);
3035 return crypt_wipe_key(cc
);
3040 DMWARN("unrecognised message received.");
3044 static int crypt_iterate_devices(struct dm_target
*ti
,
3045 iterate_devices_callout_fn fn
, void *data
)
3047 struct crypt_config
*cc
= ti
->private;
3049 return fn(ti
, cc
->dev
, cc
->start
, ti
->len
, data
);
3052 static void crypt_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
3054 struct crypt_config
*cc
= ti
->private;
3057 * Unfortunate constraint that is required to avoid the potential
3058 * for exceeding underlying device's max_segments limits -- due to
3059 * crypt_alloc_buffer() possibly allocating pages for the encryption
3060 * bio that are not as physically contiguous as the original bio.
3062 limits
->max_segment_size
= PAGE_SIZE
;
3064 limits
->logical_block_size
=
3065 max_t(unsigned short, limits
->logical_block_size
, cc
->sector_size
);
3066 limits
->physical_block_size
=
3067 max_t(unsigned, limits
->physical_block_size
, cc
->sector_size
);
3068 limits
->io_min
= max_t(unsigned, limits
->io_min
, cc
->sector_size
);
3071 static struct target_type crypt_target
= {
3073 .version
= {1, 18, 1},
3074 .module
= THIS_MODULE
,
3078 .status
= crypt_status
,
3079 .postsuspend
= crypt_postsuspend
,
3080 .preresume
= crypt_preresume
,
3081 .resume
= crypt_resume
,
3082 .message
= crypt_message
,
3083 .iterate_devices
= crypt_iterate_devices
,
3084 .io_hints
= crypt_io_hints
,
3087 static int __init
dm_crypt_init(void)
3091 r
= dm_register_target(&crypt_target
);
3093 DMERR("register failed %d", r
);
3098 static void __exit
dm_crypt_exit(void)
3100 dm_unregister_target(&crypt_target
);
3103 module_init(dm_crypt_init
);
3104 module_exit(dm_crypt_exit
);
3106 MODULE_AUTHOR("Jana Saout <jana@saout.de>");
3107 MODULE_DESCRIPTION(DM_NAME
" target for transparent encryption / decryption");
3108 MODULE_LICENSE("GPL");