Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
bf14299f | 2 | * Copyright (C) 2003 Jana Saout <jana@saout.de> |
1da177e4 | 3 | * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org> |
ef43aa38 MB |
4 | * Copyright (C) 2006-2017 Red Hat, Inc. All rights reserved. |
5 | * Copyright (C) 2013-2017 Milan Broz <gmazyland@gmail.com> | |
1da177e4 LT |
6 | * |
7 | * This file is released under the GPL. | |
8 | */ | |
9 | ||
43d69034 | 10 | #include <linux/completion.h> |
d1806f6a | 11 | #include <linux/err.h> |
1da177e4 LT |
12 | #include <linux/module.h> |
13 | #include <linux/init.h> | |
14 | #include <linux/kernel.h> | |
c538f6ec | 15 | #include <linux/key.h> |
1da177e4 LT |
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> | |
dc267621 | 22 | #include <linux/kthread.h> |
3fcfab16 | 23 | #include <linux/backing-dev.h> |
60063497 | 24 | #include <linux/atomic.h> |
378f058c | 25 | #include <linux/scatterlist.h> |
b3c5fd30 | 26 | #include <linux/rbtree.h> |
027c431c | 27 | #include <linux/ctype.h> |
1da177e4 | 28 | #include <asm/page.h> |
48527fa7 | 29 | #include <asm/unaligned.h> |
34745785 MB |
30 | #include <crypto/hash.h> |
31 | #include <crypto/md5.h> | |
32 | #include <crypto/algapi.h> | |
bbdb23b5 | 33 | #include <crypto/skcipher.h> |
ef43aa38 MB |
34 | #include <crypto/aead.h> |
35 | #include <crypto/authenc.h> | |
36 | #include <linux/rtnetlink.h> /* for struct rtattr and RTA macros only */ | |
c538f6ec | 37 | #include <keys/user-type.h> |
1da177e4 | 38 | |
586e80e6 | 39 | #include <linux/device-mapper.h> |
1da177e4 | 40 | |
72d94861 | 41 | #define DM_MSG_PREFIX "crypt" |
1da177e4 | 42 | |
1da177e4 LT |
43 | /* |
44 | * context holding the current state of a multi-part conversion | |
45 | */ | |
46 | struct convert_context { | |
43d69034 | 47 | struct completion restart; |
1da177e4 LT |
48 | struct bio *bio_in; |
49 | struct bio *bio_out; | |
003b5c57 KO |
50 | struct bvec_iter iter_in; |
51 | struct bvec_iter iter_out; | |
8d683dcd | 52 | u64 cc_sector; |
40b6229b | 53 | atomic_t cc_pending; |
ef43aa38 MB |
54 | union { |
55 | struct skcipher_request *req; | |
56 | struct aead_request *req_aead; | |
57 | } r; | |
58 | ||
1da177e4 LT |
59 | }; |
60 | ||
53017030 MB |
61 | /* |
62 | * per bio private data | |
63 | */ | |
64 | struct dm_crypt_io { | |
49a8a920 | 65 | struct crypt_config *cc; |
53017030 | 66 | struct bio *base_bio; |
ef43aa38 MB |
67 | u8 *integrity_metadata; |
68 | bool integrity_metadata_from_pool; | |
53017030 MB |
69 | struct work_struct work; |
70 | ||
71 | struct convert_context ctx; | |
72 | ||
40b6229b | 73 | atomic_t io_pending; |
4e4cbee9 | 74 | blk_status_t error; |
0c395b0f | 75 | sector_t sector; |
dc267621 | 76 | |
b3c5fd30 | 77 | struct rb_node rb_node; |
298a9fa0 | 78 | } CRYPTO_MINALIGN_ATTR; |
53017030 | 79 | |
01482b76 | 80 | struct dm_crypt_request { |
b2174eeb | 81 | struct convert_context *ctx; |
ef43aa38 MB |
82 | struct scatterlist sg_in[4]; |
83 | struct scatterlist sg_out[4]; | |
8d683dcd | 84 | u64 iv_sector; |
01482b76 MB |
85 | }; |
86 | ||
1da177e4 LT |
87 | struct crypt_config; |
88 | ||
89 | struct crypt_iv_operations { | |
90 | int (*ctr)(struct crypt_config *cc, struct dm_target *ti, | |
d469f841 | 91 | const char *opts); |
1da177e4 | 92 | void (*dtr)(struct crypt_config *cc); |
b95bf2d3 | 93 | int (*init)(struct crypt_config *cc); |
542da317 | 94 | int (*wipe)(struct crypt_config *cc); |
2dc5327d MB |
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); | |
1da177e4 LT |
99 | }; |
100 | ||
60473592 | 101 | struct iv_essiv_private { |
c07c88f5 | 102 | struct crypto_shash *hash_tfm; |
b95bf2d3 | 103 | u8 *salt; |
60473592 MB |
104 | }; |
105 | ||
106 | struct iv_benbi_private { | |
107 | int shift; | |
108 | }; | |
109 | ||
34745785 MB |
110 | #define LMK_SEED_SIZE 64 /* hash + 0 */ |
111 | struct iv_lmk_private { | |
112 | struct crypto_shash *hash_tfm; | |
113 | u8 *seed; | |
114 | }; | |
115 | ||
ed04d981 MB |
116 | #define TCW_WHITENING_SIZE 16 |
117 | struct iv_tcw_private { | |
118 | struct crypto_shash *crc32_tfm; | |
119 | u8 *iv_seed; | |
120 | u8 *whitening; | |
121 | }; | |
122 | ||
1da177e4 LT |
123 | /* |
124 | * Crypt: maps a linear range of a block device | |
125 | * and encrypts / decrypts at the same time. | |
126 | */ | |
0f5d8e6e | 127 | enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID, |
f659b100 | 128 | DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD }; |
c0297721 | 129 | |
ef43aa38 MB |
130 | enum cipher_flags { |
131 | CRYPT_MODE_INTEGRITY_AEAD, /* Use authenticated mode for cihper */ | |
8f0009a2 | 132 | CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */ |
ef43aa38 MB |
133 | }; |
134 | ||
c0297721 | 135 | /* |
610f2de3 | 136 | * The fields in here must be read only after initialization. |
c0297721 | 137 | */ |
1da177e4 LT |
138 | struct crypt_config { |
139 | struct dm_dev *dev; | |
140 | sector_t start; | |
141 | ||
5059353d MP |
142 | struct percpu_counter n_allocated_pages; |
143 | ||
cabf08e4 MB |
144 | struct workqueue_struct *io_queue; |
145 | struct workqueue_struct *crypt_queue; | |
3f1e9070 | 146 | |
c7329eff | 147 | spinlock_t write_thread_lock; |
72d711c8 | 148 | struct task_struct *write_thread; |
b3c5fd30 | 149 | struct rb_root write_tree; |
dc267621 | 150 | |
5ebaee6d | 151 | char *cipher; |
7dbcd137 | 152 | char *cipher_string; |
ef43aa38 | 153 | char *cipher_auth; |
c538f6ec | 154 | char *key_string; |
5ebaee6d | 155 | |
1b1b58f5 | 156 | const struct crypt_iv_operations *iv_gen_ops; |
79066ad3 | 157 | union { |
60473592 MB |
158 | struct iv_essiv_private essiv; |
159 | struct iv_benbi_private benbi; | |
34745785 | 160 | struct iv_lmk_private lmk; |
ed04d981 | 161 | struct iv_tcw_private tcw; |
79066ad3 | 162 | } iv_gen_private; |
8d683dcd | 163 | u64 iv_offset; |
1da177e4 | 164 | unsigned int iv_size; |
ff3af92b MP |
165 | unsigned short int sector_size; |
166 | unsigned char sector_shift; | |
1da177e4 | 167 | |
fd2d231f MP |
168 | /* ESSIV: struct crypto_cipher *essiv_tfm */ |
169 | void *iv_private; | |
ef43aa38 MB |
170 | union { |
171 | struct crypto_skcipher **tfms; | |
172 | struct crypto_aead **tfms_aead; | |
173 | } cipher_tfm; | |
d1f96423 | 174 | unsigned tfms_count; |
ef43aa38 | 175 | unsigned long cipher_flags; |
c0297721 | 176 | |
ddd42edf MB |
177 | /* |
178 | * Layout of each crypto request: | |
179 | * | |
bbdb23b5 | 180 | * struct skcipher_request |
ddd42edf MB |
181 | * context |
182 | * padding | |
183 | * struct dm_crypt_request | |
184 | * padding | |
185 | * IV | |
186 | * | |
187 | * The padding is added so that dm_crypt_request and the IV are | |
188 | * correctly aligned. | |
189 | */ | |
190 | unsigned int dmreq_start; | |
ddd42edf | 191 | |
298a9fa0 MP |
192 | unsigned int per_bio_data_size; |
193 | ||
e48d4bbf | 194 | unsigned long flags; |
1da177e4 | 195 | unsigned int key_size; |
da31a078 MB |
196 | unsigned int key_parts; /* independent parts in key buffer */ |
197 | unsigned int key_extra_size; /* additional keys length */ | |
ef43aa38 MB |
198 | unsigned int key_mac_size; /* MAC key size for authenc(...) */ |
199 | ||
200 | unsigned int integrity_tag_size; | |
201 | unsigned int integrity_iv_size; | |
202 | unsigned int on_disk_tag_size; | |
203 | ||
72d711c8 MS |
204 | /* |
205 | * pool for per bio private data, crypto requests, | |
206 | * encryption requeusts/buffer pages and integrity tags | |
207 | */ | |
208 | unsigned tag_pool_max_sectors; | |
209 | mempool_t tag_pool; | |
210 | mempool_t req_pool; | |
211 | mempool_t page_pool; | |
212 | ||
213 | struct bio_set bs; | |
214 | struct mutex bio_alloc_lock; | |
215 | ||
ef43aa38 | 216 | u8 *authenc_key; /* space for keys in authenc() format (if used) */ |
1da177e4 LT |
217 | u8 key[0]; |
218 | }; | |
219 | ||
ef43aa38 MB |
220 | #define MIN_IOS 64 |
221 | #define MAX_TAG_SIZE 480 | |
222 | #define POOL_ENTRY_SIZE 512 | |
1da177e4 | 223 | |
5059353d MP |
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) | |
229 | ||
028867ac | 230 | static void clone_init(struct dm_crypt_io *, struct bio *); |
395b167c | 231 | static void kcryptd_queue_crypt(struct dm_crypt_io *io); |
ef43aa38 MB |
232 | static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc, |
233 | struct scatterlist *sg); | |
027581f3 | 234 | |
c0297721 | 235 | /* |
86f917ad | 236 | * Use this to access cipher attributes that are independent of the key. |
c0297721 | 237 | */ |
bbdb23b5 | 238 | static struct crypto_skcipher *any_tfm(struct crypt_config *cc) |
c0297721 | 239 | { |
ef43aa38 MB |
240 | return cc->cipher_tfm.tfms[0]; |
241 | } | |
242 | ||
243 | static struct crypto_aead *any_tfm_aead(struct crypt_config *cc) | |
244 | { | |
245 | return cc->cipher_tfm.tfms_aead[0]; | |
c0297721 AK |
246 | } |
247 | ||
1da177e4 LT |
248 | /* |
249 | * Different IV generation algorithms: | |
250 | * | |
3c164bd8 | 251 | * plain: the initial vector is the 32-bit little-endian version of the sector |
3a4fa0a2 | 252 | * number, padded with zeros if necessary. |
1da177e4 | 253 | * |
61afef61 MB |
254 | * plain64: the initial vector is the 64-bit little-endian version of the sector |
255 | * number, padded with zeros if necessary. | |
256 | * | |
7e3fd855 MB |
257 | * plain64be: the initial vector is the 64-bit big-endian version of the sector |
258 | * number, padded with zeros if necessary. | |
259 | * | |
3c164bd8 RS |
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. | |
1da177e4 | 263 | * |
48527fa7 RS |
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) | |
266 | * | |
46b47730 LN |
267 | * null: the initial vector is always zero. Provides compatibility with |
268 | * obsolete loop_fish2 devices. Do not use for new devices. | |
269 | * | |
34745785 MB |
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) | |
283 | * | |
ed04d981 MB |
284 | * tcw: Compatible implementation of the block chaining mode used |
285 | * by the TrueCrypt device encryption system (prior to version 4.1). | |
e44f23b3 | 286 | * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat |
ed04d981 MB |
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. | |
293 | * | |
1da177e4 LT |
294 | * plumb: unimplemented, see: |
295 | * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454 | |
296 | */ | |
297 | ||
2dc5327d MB |
298 | static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, |
299 | struct dm_crypt_request *dmreq) | |
1da177e4 LT |
300 | { |
301 | memset(iv, 0, cc->iv_size); | |
283a8328 | 302 | *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff); |
1da177e4 LT |
303 | |
304 | return 0; | |
305 | } | |
306 | ||
61afef61 | 307 | static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv, |
2dc5327d | 308 | struct dm_crypt_request *dmreq) |
61afef61 MB |
309 | { |
310 | memset(iv, 0, cc->iv_size); | |
283a8328 | 311 | *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector); |
61afef61 MB |
312 | |
313 | return 0; | |
314 | } | |
315 | ||
7e3fd855 MB |
316 | static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv, |
317 | struct dm_crypt_request *dmreq) | |
318 | { | |
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); | |
322 | ||
323 | return 0; | |
324 | } | |
325 | ||
b95bf2d3 MB |
326 | /* Initialise ESSIV - compute salt but no local memory allocations */ |
327 | static int crypt_iv_essiv_init(struct crypt_config *cc) | |
328 | { | |
329 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; | |
c07c88f5 | 330 | SHASH_DESC_ON_STACK(desc, essiv->hash_tfm); |
c0297721 | 331 | struct crypto_cipher *essiv_tfm; |
fd2d231f | 332 | int err; |
b95bf2d3 | 333 | |
c07c88f5 | 334 | desc->tfm = essiv->hash_tfm; |
b95bf2d3 | 335 | |
c07c88f5 KC |
336 | err = crypto_shash_digest(desc, cc->key, cc->key_size, essiv->salt); |
337 | shash_desc_zero(desc); | |
b95bf2d3 MB |
338 | if (err) |
339 | return err; | |
340 | ||
fd2d231f | 341 | essiv_tfm = cc->iv_private; |
c0297721 | 342 | |
fd2d231f | 343 | err = crypto_cipher_setkey(essiv_tfm, essiv->salt, |
c07c88f5 | 344 | crypto_shash_digestsize(essiv->hash_tfm)); |
fd2d231f MP |
345 | if (err) |
346 | return err; | |
c0297721 AK |
347 | |
348 | return 0; | |
b95bf2d3 MB |
349 | } |
350 | ||
542da317 MB |
351 | /* Wipe salt and reset key derived from volume key */ |
352 | static int crypt_iv_essiv_wipe(struct crypt_config *cc) | |
353 | { | |
354 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; | |
c07c88f5 | 355 | unsigned salt_size = crypto_shash_digestsize(essiv->hash_tfm); |
c0297721 | 356 | struct crypto_cipher *essiv_tfm; |
fd2d231f | 357 | int r, err = 0; |
542da317 MB |
358 | |
359 | memset(essiv->salt, 0, salt_size); | |
360 | ||
fd2d231f MP |
361 | essiv_tfm = cc->iv_private; |
362 | r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size); | |
363 | if (r) | |
364 | err = r; | |
c0297721 AK |
365 | |
366 | return err; | |
367 | } | |
368 | ||
86f917ad EB |
369 | /* Allocate the cipher for ESSIV */ |
370 | static struct crypto_cipher *alloc_essiv_cipher(struct crypt_config *cc, | |
371 | struct dm_target *ti, | |
372 | const u8 *salt, | |
373 | unsigned int saltsize) | |
c0297721 AK |
374 | { |
375 | struct crypto_cipher *essiv_tfm; | |
376 | int err; | |
377 | ||
378 | /* Setup the essiv_tfm with the given salt */ | |
1ad0f160 | 379 | essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, 0); |
c0297721 AK |
380 | if (IS_ERR(essiv_tfm)) { |
381 | ti->error = "Error allocating crypto tfm for ESSIV"; | |
382 | return essiv_tfm; | |
383 | } | |
384 | ||
ef43aa38 | 385 | if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) { |
c0297721 AK |
386 | ti->error = "Block size of ESSIV cipher does " |
387 | "not match IV size of block cipher"; | |
388 | crypto_free_cipher(essiv_tfm); | |
389 | return ERR_PTR(-EINVAL); | |
390 | } | |
391 | ||
392 | err = crypto_cipher_setkey(essiv_tfm, salt, saltsize); | |
393 | if (err) { | |
394 | ti->error = "Failed to set key for ESSIV cipher"; | |
395 | crypto_free_cipher(essiv_tfm); | |
396 | return ERR_PTR(err); | |
397 | } | |
398 | ||
399 | return essiv_tfm; | |
542da317 MB |
400 | } |
401 | ||
60473592 MB |
402 | static void crypt_iv_essiv_dtr(struct crypt_config *cc) |
403 | { | |
c0297721 | 404 | struct crypto_cipher *essiv_tfm; |
60473592 MB |
405 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; |
406 | ||
c07c88f5 | 407 | crypto_free_shash(essiv->hash_tfm); |
b95bf2d3 MB |
408 | essiv->hash_tfm = NULL; |
409 | ||
410 | kzfree(essiv->salt); | |
411 | essiv->salt = NULL; | |
c0297721 | 412 | |
fd2d231f | 413 | essiv_tfm = cc->iv_private; |
c0297721 | 414 | |
fd2d231f MP |
415 | if (essiv_tfm) |
416 | crypto_free_cipher(essiv_tfm); | |
c0297721 | 417 | |
fd2d231f | 418 | cc->iv_private = NULL; |
60473592 MB |
419 | } |
420 | ||
1da177e4 | 421 | static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti, |
d469f841 | 422 | const char *opts) |
1da177e4 | 423 | { |
5861f1be | 424 | struct crypto_cipher *essiv_tfm = NULL; |
c07c88f5 | 425 | struct crypto_shash *hash_tfm = NULL; |
5861f1be | 426 | u8 *salt = NULL; |
fd2d231f | 427 | int err; |
1da177e4 | 428 | |
5861f1be | 429 | if (!opts) { |
72d94861 | 430 | ti->error = "Digest algorithm missing for ESSIV mode"; |
1da177e4 LT |
431 | return -EINVAL; |
432 | } | |
433 | ||
b95bf2d3 | 434 | /* Allocate hash algorithm */ |
c07c88f5 | 435 | hash_tfm = crypto_alloc_shash(opts, 0, 0); |
35058687 | 436 | if (IS_ERR(hash_tfm)) { |
72d94861 | 437 | ti->error = "Error initializing ESSIV hash"; |
5861f1be MB |
438 | err = PTR_ERR(hash_tfm); |
439 | goto bad; | |
1da177e4 LT |
440 | } |
441 | ||
c07c88f5 | 442 | salt = kzalloc(crypto_shash_digestsize(hash_tfm), GFP_KERNEL); |
5861f1be | 443 | if (!salt) { |
72d94861 | 444 | ti->error = "Error kmallocing salt storage in ESSIV"; |
5861f1be MB |
445 | err = -ENOMEM; |
446 | goto bad; | |
1da177e4 LT |
447 | } |
448 | ||
b95bf2d3 | 449 | cc->iv_gen_private.essiv.salt = salt; |
b95bf2d3 MB |
450 | cc->iv_gen_private.essiv.hash_tfm = hash_tfm; |
451 | ||
86f917ad | 452 | essiv_tfm = alloc_essiv_cipher(cc, ti, salt, |
c07c88f5 | 453 | crypto_shash_digestsize(hash_tfm)); |
fd2d231f MP |
454 | if (IS_ERR(essiv_tfm)) { |
455 | crypt_iv_essiv_dtr(cc); | |
456 | return PTR_ERR(essiv_tfm); | |
c0297721 | 457 | } |
fd2d231f | 458 | cc->iv_private = essiv_tfm; |
c0297721 | 459 | |
1da177e4 | 460 | return 0; |
5861f1be MB |
461 | |
462 | bad: | |
5861f1be | 463 | if (hash_tfm && !IS_ERR(hash_tfm)) |
c07c88f5 | 464 | crypto_free_shash(hash_tfm); |
b95bf2d3 | 465 | kfree(salt); |
5861f1be | 466 | return err; |
1da177e4 LT |
467 | } |
468 | ||
2dc5327d MB |
469 | static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, |
470 | struct dm_crypt_request *dmreq) | |
1da177e4 | 471 | { |
fd2d231f | 472 | struct crypto_cipher *essiv_tfm = cc->iv_private; |
c0297721 | 473 | |
1da177e4 | 474 | memset(iv, 0, cc->iv_size); |
283a8328 | 475 | *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector); |
c0297721 AK |
476 | crypto_cipher_encrypt_one(essiv_tfm, iv, iv); |
477 | ||
1da177e4 LT |
478 | return 0; |
479 | } | |
480 | ||
48527fa7 RS |
481 | static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, |
482 | const char *opts) | |
483 | { | |
bbdb23b5 | 484 | unsigned bs = crypto_skcipher_blocksize(any_tfm(cc)); |
f0d1b0b3 | 485 | int log = ilog2(bs); |
48527fa7 RS |
486 | |
487 | /* we need to calculate how far we must shift the sector count | |
488 | * to get the cipher block count, we use this shift in _gen */ | |
489 | ||
490 | if (1 << log != bs) { | |
491 | ti->error = "cypher blocksize is not a power of 2"; | |
492 | return -EINVAL; | |
493 | } | |
494 | ||
495 | if (log > 9) { | |
496 | ti->error = "cypher blocksize is > 512"; | |
497 | return -EINVAL; | |
498 | } | |
499 | ||
60473592 | 500 | cc->iv_gen_private.benbi.shift = 9 - log; |
48527fa7 RS |
501 | |
502 | return 0; | |
503 | } | |
504 | ||
505 | static void crypt_iv_benbi_dtr(struct crypt_config *cc) | |
506 | { | |
48527fa7 RS |
507 | } |
508 | ||
2dc5327d MB |
509 | static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, |
510 | struct dm_crypt_request *dmreq) | |
48527fa7 | 511 | { |
79066ad3 HX |
512 | __be64 val; |
513 | ||
48527fa7 | 514 | memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ |
79066ad3 | 515 | |
2dc5327d | 516 | val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1); |
79066ad3 | 517 | put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); |
48527fa7 | 518 | |
1da177e4 LT |
519 | return 0; |
520 | } | |
521 | ||
2dc5327d MB |
522 | static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, |
523 | struct dm_crypt_request *dmreq) | |
46b47730 LN |
524 | { |
525 | memset(iv, 0, cc->iv_size); | |
526 | ||
527 | return 0; | |
528 | } | |
529 | ||
34745785 MB |
530 | static void crypt_iv_lmk_dtr(struct crypt_config *cc) |
531 | { | |
532 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; | |
533 | ||
534 | if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm)) | |
535 | crypto_free_shash(lmk->hash_tfm); | |
536 | lmk->hash_tfm = NULL; | |
537 | ||
538 | kzfree(lmk->seed); | |
539 | lmk->seed = NULL; | |
540 | } | |
541 | ||
542 | static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti, | |
543 | const char *opts) | |
544 | { | |
545 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; | |
546 | ||
8f0009a2 MB |
547 | if (cc->sector_size != (1 << SECTOR_SHIFT)) { |
548 | ti->error = "Unsupported sector size for LMK"; | |
549 | return -EINVAL; | |
550 | } | |
551 | ||
34745785 MB |
552 | lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0); |
553 | if (IS_ERR(lmk->hash_tfm)) { | |
554 | ti->error = "Error initializing LMK hash"; | |
555 | return PTR_ERR(lmk->hash_tfm); | |
556 | } | |
557 | ||
558 | /* No seed in LMK version 2 */ | |
559 | if (cc->key_parts == cc->tfms_count) { | |
560 | lmk->seed = NULL; | |
561 | return 0; | |
562 | } | |
563 | ||
564 | lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL); | |
565 | if (!lmk->seed) { | |
566 | crypt_iv_lmk_dtr(cc); | |
567 | ti->error = "Error kmallocing seed storage in LMK"; | |
568 | return -ENOMEM; | |
569 | } | |
570 | ||
571 | return 0; | |
572 | } | |
573 | ||
574 | static int crypt_iv_lmk_init(struct crypt_config *cc) | |
575 | { | |
576 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; | |
577 | int subkey_size = cc->key_size / cc->key_parts; | |
578 | ||
579 | /* LMK seed is on the position of LMK_KEYS + 1 key */ | |
580 | if (lmk->seed) | |
581 | memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size), | |
582 | crypto_shash_digestsize(lmk->hash_tfm)); | |
583 | ||
584 | return 0; | |
585 | } | |
586 | ||
587 | static int crypt_iv_lmk_wipe(struct crypt_config *cc) | |
588 | { | |
589 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; | |
590 | ||
591 | if (lmk->seed) | |
592 | memset(lmk->seed, 0, LMK_SEED_SIZE); | |
593 | ||
594 | return 0; | |
595 | } | |
596 | ||
597 | static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv, | |
598 | struct dm_crypt_request *dmreq, | |
599 | u8 *data) | |
600 | { | |
601 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; | |
b6106265 | 602 | SHASH_DESC_ON_STACK(desc, lmk->hash_tfm); |
34745785 | 603 | struct md5_state md5state; |
da31a078 | 604 | __le32 buf[4]; |
34745785 MB |
605 | int i, r; |
606 | ||
b6106265 | 607 | desc->tfm = lmk->hash_tfm; |
34745785 | 608 | |
b6106265 | 609 | r = crypto_shash_init(desc); |
34745785 MB |
610 | if (r) |
611 | return r; | |
612 | ||
613 | if (lmk->seed) { | |
b6106265 | 614 | r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE); |
34745785 MB |
615 | if (r) |
616 | return r; | |
617 | } | |
618 | ||
619 | /* Sector is always 512B, block size 16, add data of blocks 1-31 */ | |
b6106265 | 620 | r = crypto_shash_update(desc, data + 16, 16 * 31); |
34745785 MB |
621 | if (r) |
622 | return r; | |
623 | ||
624 | /* Sector is cropped to 56 bits here */ | |
625 | buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF); | |
626 | buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000); | |
627 | buf[2] = cpu_to_le32(4024); | |
628 | buf[3] = 0; | |
b6106265 | 629 | r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf)); |
34745785 MB |
630 | if (r) |
631 | return r; | |
632 | ||
633 | /* No MD5 padding here */ | |
b6106265 | 634 | r = crypto_shash_export(desc, &md5state); |
34745785 MB |
635 | if (r) |
636 | return r; | |
637 | ||
638 | for (i = 0; i < MD5_HASH_WORDS; i++) | |
639 | __cpu_to_le32s(&md5state.hash[i]); | |
640 | memcpy(iv, &md5state.hash, cc->iv_size); | |
641 | ||
642 | return 0; | |
643 | } | |
644 | ||
645 | static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv, | |
646 | struct dm_crypt_request *dmreq) | |
647 | { | |
ef43aa38 | 648 | struct scatterlist *sg; |
34745785 MB |
649 | u8 *src; |
650 | int r = 0; | |
651 | ||
652 | if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) { | |
ef43aa38 MB |
653 | sg = crypt_get_sg_data(cc, dmreq->sg_in); |
654 | src = kmap_atomic(sg_page(sg)); | |
655 | r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset); | |
c2e022cb | 656 | kunmap_atomic(src); |
34745785 MB |
657 | } else |
658 | memset(iv, 0, cc->iv_size); | |
659 | ||
660 | return r; | |
661 | } | |
662 | ||
663 | static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv, | |
664 | struct dm_crypt_request *dmreq) | |
665 | { | |
ef43aa38 | 666 | struct scatterlist *sg; |
34745785 MB |
667 | u8 *dst; |
668 | int r; | |
669 | ||
670 | if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) | |
671 | return 0; | |
672 | ||
ef43aa38 MB |
673 | sg = crypt_get_sg_data(cc, dmreq->sg_out); |
674 | dst = kmap_atomic(sg_page(sg)); | |
675 | r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset); | |
34745785 MB |
676 | |
677 | /* Tweak the first block of plaintext sector */ | |
678 | if (!r) | |
ef43aa38 | 679 | crypto_xor(dst + sg->offset, iv, cc->iv_size); |
34745785 | 680 | |
c2e022cb | 681 | kunmap_atomic(dst); |
34745785 MB |
682 | return r; |
683 | } | |
684 | ||
ed04d981 MB |
685 | static void crypt_iv_tcw_dtr(struct crypt_config *cc) |
686 | { | |
687 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; | |
688 | ||
689 | kzfree(tcw->iv_seed); | |
690 | tcw->iv_seed = NULL; | |
691 | kzfree(tcw->whitening); | |
692 | tcw->whitening = NULL; | |
693 | ||
694 | if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm)) | |
695 | crypto_free_shash(tcw->crc32_tfm); | |
696 | tcw->crc32_tfm = NULL; | |
697 | } | |
698 | ||
699 | static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti, | |
700 | const char *opts) | |
701 | { | |
702 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; | |
703 | ||
8f0009a2 MB |
704 | if (cc->sector_size != (1 << SECTOR_SHIFT)) { |
705 | ti->error = "Unsupported sector size for TCW"; | |
706 | return -EINVAL; | |
707 | } | |
708 | ||
ed04d981 MB |
709 | if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) { |
710 | ti->error = "Wrong key size for TCW"; | |
711 | return -EINVAL; | |
712 | } | |
713 | ||
714 | tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0); | |
715 | if (IS_ERR(tcw->crc32_tfm)) { | |
716 | ti->error = "Error initializing CRC32 in TCW"; | |
717 | return PTR_ERR(tcw->crc32_tfm); | |
718 | } | |
719 | ||
720 | tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL); | |
721 | tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL); | |
722 | if (!tcw->iv_seed || !tcw->whitening) { | |
723 | crypt_iv_tcw_dtr(cc); | |
724 | ti->error = "Error allocating seed storage in TCW"; | |
725 | return -ENOMEM; | |
726 | } | |
727 | ||
728 | return 0; | |
729 | } | |
730 | ||
731 | static int crypt_iv_tcw_init(struct crypt_config *cc) | |
732 | { | |
733 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; | |
734 | int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE; | |
735 | ||
736 | memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size); | |
737 | memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size], | |
738 | TCW_WHITENING_SIZE); | |
739 | ||
740 | return 0; | |
741 | } | |
742 | ||
743 | static int crypt_iv_tcw_wipe(struct crypt_config *cc) | |
744 | { | |
745 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; | |
746 | ||
747 | memset(tcw->iv_seed, 0, cc->iv_size); | |
748 | memset(tcw->whitening, 0, TCW_WHITENING_SIZE); | |
749 | ||
750 | return 0; | |
751 | } | |
752 | ||
753 | static int crypt_iv_tcw_whitening(struct crypt_config *cc, | |
754 | struct dm_crypt_request *dmreq, | |
755 | u8 *data) | |
756 | { | |
757 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; | |
350b5393 | 758 | __le64 sector = cpu_to_le64(dmreq->iv_sector); |
ed04d981 | 759 | u8 buf[TCW_WHITENING_SIZE]; |
b6106265 | 760 | SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm); |
ed04d981 MB |
761 | int i, r; |
762 | ||
763 | /* xor whitening with sector number */ | |
45fe93df AB |
764 | crypto_xor_cpy(buf, tcw->whitening, (u8 *)§or, 8); |
765 | crypto_xor_cpy(&buf[8], tcw->whitening + 8, (u8 *)§or, 8); | |
ed04d981 MB |
766 | |
767 | /* calculate crc32 for every 32bit part and xor it */ | |
b6106265 | 768 | desc->tfm = tcw->crc32_tfm; |
ed04d981 | 769 | for (i = 0; i < 4; i++) { |
b6106265 | 770 | r = crypto_shash_init(desc); |
ed04d981 MB |
771 | if (r) |
772 | goto out; | |
b6106265 | 773 | r = crypto_shash_update(desc, &buf[i * 4], 4); |
ed04d981 MB |
774 | if (r) |
775 | goto out; | |
b6106265 | 776 | r = crypto_shash_final(desc, &buf[i * 4]); |
ed04d981 MB |
777 | if (r) |
778 | goto out; | |
779 | } | |
780 | crypto_xor(&buf[0], &buf[12], 4); | |
781 | crypto_xor(&buf[4], &buf[8], 4); | |
782 | ||
783 | /* apply whitening (8 bytes) to whole sector */ | |
784 | for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++) | |
785 | crypto_xor(data + i * 8, buf, 8); | |
786 | out: | |
1a71d6ff | 787 | memzero_explicit(buf, sizeof(buf)); |
ed04d981 MB |
788 | return r; |
789 | } | |
790 | ||
791 | static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv, | |
792 | struct dm_crypt_request *dmreq) | |
793 | { | |
ef43aa38 | 794 | struct scatterlist *sg; |
ed04d981 | 795 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
350b5393 | 796 | __le64 sector = cpu_to_le64(dmreq->iv_sector); |
ed04d981 MB |
797 | u8 *src; |
798 | int r = 0; | |
799 | ||
800 | /* Remove whitening from ciphertext */ | |
801 | if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) { | |
ef43aa38 MB |
802 | sg = crypt_get_sg_data(cc, dmreq->sg_in); |
803 | src = kmap_atomic(sg_page(sg)); | |
804 | r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset); | |
ed04d981 MB |
805 | kunmap_atomic(src); |
806 | } | |
807 | ||
808 | /* Calculate IV */ | |
45fe93df | 809 | crypto_xor_cpy(iv, tcw->iv_seed, (u8 *)§or, 8); |
ed04d981 | 810 | if (cc->iv_size > 8) |
45fe93df AB |
811 | crypto_xor_cpy(&iv[8], tcw->iv_seed + 8, (u8 *)§or, |
812 | cc->iv_size - 8); | |
ed04d981 MB |
813 | |
814 | return r; | |
815 | } | |
816 | ||
817 | static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv, | |
818 | struct dm_crypt_request *dmreq) | |
819 | { | |
ef43aa38 | 820 | struct scatterlist *sg; |
ed04d981 MB |
821 | u8 *dst; |
822 | int r; | |
823 | ||
824 | if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) | |
825 | return 0; | |
826 | ||
827 | /* Apply whitening on ciphertext */ | |
ef43aa38 MB |
828 | sg = crypt_get_sg_data(cc, dmreq->sg_out); |
829 | dst = kmap_atomic(sg_page(sg)); | |
830 | r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset); | |
ed04d981 MB |
831 | kunmap_atomic(dst); |
832 | ||
833 | return r; | |
834 | } | |
835 | ||
ef43aa38 MB |
836 | static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv, |
837 | struct dm_crypt_request *dmreq) | |
838 | { | |
839 | /* Used only for writes, there must be an additional space to store IV */ | |
840 | get_random_bytes(iv, cc->iv_size); | |
841 | return 0; | |
842 | } | |
843 | ||
1b1b58f5 | 844 | static const struct crypt_iv_operations crypt_iv_plain_ops = { |
1da177e4 LT |
845 | .generator = crypt_iv_plain_gen |
846 | }; | |
847 | ||
1b1b58f5 | 848 | static const struct crypt_iv_operations crypt_iv_plain64_ops = { |
61afef61 MB |
849 | .generator = crypt_iv_plain64_gen |
850 | }; | |
851 | ||
7e3fd855 MB |
852 | static const struct crypt_iv_operations crypt_iv_plain64be_ops = { |
853 | .generator = crypt_iv_plain64be_gen | |
854 | }; | |
855 | ||
1b1b58f5 | 856 | static const struct crypt_iv_operations crypt_iv_essiv_ops = { |
1da177e4 LT |
857 | .ctr = crypt_iv_essiv_ctr, |
858 | .dtr = crypt_iv_essiv_dtr, | |
b95bf2d3 | 859 | .init = crypt_iv_essiv_init, |
542da317 | 860 | .wipe = crypt_iv_essiv_wipe, |
1da177e4 LT |
861 | .generator = crypt_iv_essiv_gen |
862 | }; | |
863 | ||
1b1b58f5 | 864 | static const struct crypt_iv_operations crypt_iv_benbi_ops = { |
48527fa7 RS |
865 | .ctr = crypt_iv_benbi_ctr, |
866 | .dtr = crypt_iv_benbi_dtr, | |
867 | .generator = crypt_iv_benbi_gen | |
868 | }; | |
1da177e4 | 869 | |
1b1b58f5 | 870 | static const struct crypt_iv_operations crypt_iv_null_ops = { |
46b47730 LN |
871 | .generator = crypt_iv_null_gen |
872 | }; | |
873 | ||
1b1b58f5 | 874 | static const struct crypt_iv_operations crypt_iv_lmk_ops = { |
34745785 MB |
875 | .ctr = crypt_iv_lmk_ctr, |
876 | .dtr = crypt_iv_lmk_dtr, | |
877 | .init = crypt_iv_lmk_init, | |
878 | .wipe = crypt_iv_lmk_wipe, | |
879 | .generator = crypt_iv_lmk_gen, | |
880 | .post = crypt_iv_lmk_post | |
881 | }; | |
882 | ||
1b1b58f5 | 883 | static const struct crypt_iv_operations crypt_iv_tcw_ops = { |
ed04d981 MB |
884 | .ctr = crypt_iv_tcw_ctr, |
885 | .dtr = crypt_iv_tcw_dtr, | |
886 | .init = crypt_iv_tcw_init, | |
887 | .wipe = crypt_iv_tcw_wipe, | |
888 | .generator = crypt_iv_tcw_gen, | |
889 | .post = crypt_iv_tcw_post | |
890 | }; | |
891 | ||
ef43aa38 MB |
892 | static struct crypt_iv_operations crypt_iv_random_ops = { |
893 | .generator = crypt_iv_random_gen | |
894 | }; | |
895 | ||
896 | /* | |
897 | * Integrity extensions | |
898 | */ | |
899 | static bool crypt_integrity_aead(struct crypt_config *cc) | |
900 | { | |
901 | return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); | |
902 | } | |
903 | ||
904 | static bool crypt_integrity_hmac(struct crypt_config *cc) | |
905 | { | |
33d2f09f | 906 | return crypt_integrity_aead(cc) && cc->key_mac_size; |
ef43aa38 MB |
907 | } |
908 | ||
909 | /* Get sg containing data */ | |
910 | static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc, | |
911 | struct scatterlist *sg) | |
912 | { | |
33d2f09f | 913 | if (unlikely(crypt_integrity_aead(cc))) |
ef43aa38 MB |
914 | return &sg[2]; |
915 | ||
916 | return sg; | |
917 | } | |
918 | ||
919 | static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio) | |
920 | { | |
921 | struct bio_integrity_payload *bip; | |
922 | unsigned int tag_len; | |
923 | int ret; | |
924 | ||
925 | if (!bio_sectors(bio) || !io->cc->on_disk_tag_size) | |
926 | return 0; | |
927 | ||
928 | bip = bio_integrity_alloc(bio, GFP_NOIO, 1); | |
929 | if (IS_ERR(bip)) | |
930 | return PTR_ERR(bip); | |
931 | ||
ff0c129d | 932 | tag_len = io->cc->on_disk_tag_size * (bio_sectors(bio) >> io->cc->sector_shift); |
ef43aa38 MB |
933 | |
934 | bip->bip_iter.bi_size = tag_len; | |
935 | bip->bip_iter.bi_sector = io->cc->start + io->sector; | |
936 | ||
ef43aa38 MB |
937 | ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata), |
938 | tag_len, offset_in_page(io->integrity_metadata)); | |
939 | if (unlikely(ret != tag_len)) | |
940 | return -ENOMEM; | |
941 | ||
942 | return 0; | |
943 | } | |
944 | ||
945 | static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti) | |
946 | { | |
947 | #ifdef CONFIG_BLK_DEV_INTEGRITY | |
948 | struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk); | |
7a1cd723 | 949 | struct mapped_device *md = dm_table_get_md(ti->table); |
ef43aa38 MB |
950 | |
951 | /* From now we require underlying device with our integrity profile */ | |
952 | if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) { | |
953 | ti->error = "Integrity profile not supported."; | |
954 | return -EINVAL; | |
955 | } | |
956 | ||
583fe747 MP |
957 | if (bi->tag_size != cc->on_disk_tag_size || |
958 | bi->tuple_size != cc->on_disk_tag_size) { | |
ef43aa38 MB |
959 | ti->error = "Integrity profile tag size mismatch."; |
960 | return -EINVAL; | |
961 | } | |
583fe747 MP |
962 | if (1 << bi->interval_exp != cc->sector_size) { |
963 | ti->error = "Integrity profile sector size mismatch."; | |
964 | return -EINVAL; | |
965 | } | |
ef43aa38 | 966 | |
33d2f09f | 967 | if (crypt_integrity_aead(cc)) { |
ef43aa38 | 968 | cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size; |
7a1cd723 | 969 | DMDEBUG("%s: Integrity AEAD, tag size %u, IV size %u.", dm_device_name(md), |
ef43aa38 MB |
970 | cc->integrity_tag_size, cc->integrity_iv_size); |
971 | ||
972 | if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) { | |
973 | ti->error = "Integrity AEAD auth tag size is not supported."; | |
974 | return -EINVAL; | |
975 | } | |
976 | } else if (cc->integrity_iv_size) | |
7a1cd723 | 977 | DMDEBUG("%s: Additional per-sector space %u bytes for IV.", dm_device_name(md), |
ef43aa38 MB |
978 | cc->integrity_iv_size); |
979 | ||
980 | if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) { | |
981 | ti->error = "Not enough space for integrity tag in the profile."; | |
982 | return -EINVAL; | |
983 | } | |
984 | ||
985 | return 0; | |
986 | #else | |
987 | ti->error = "Integrity profile not supported."; | |
988 | return -EINVAL; | |
989 | #endif | |
990 | } | |
991 | ||
d469f841 MB |
992 | static void crypt_convert_init(struct crypt_config *cc, |
993 | struct convert_context *ctx, | |
994 | struct bio *bio_out, struct bio *bio_in, | |
fcd369da | 995 | sector_t sector) |
1da177e4 LT |
996 | { |
997 | ctx->bio_in = bio_in; | |
998 | ctx->bio_out = bio_out; | |
003b5c57 KO |
999 | if (bio_in) |
1000 | ctx->iter_in = bio_in->bi_iter; | |
1001 | if (bio_out) | |
1002 | ctx->iter_out = bio_out->bi_iter; | |
c66029f4 | 1003 | ctx->cc_sector = sector + cc->iv_offset; |
43d69034 | 1004 | init_completion(&ctx->restart); |
1da177e4 LT |
1005 | } |
1006 | ||
b2174eeb | 1007 | static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc, |
ef43aa38 | 1008 | void *req) |
b2174eeb HY |
1009 | { |
1010 | return (struct dm_crypt_request *)((char *)req + cc->dmreq_start); | |
1011 | } | |
1012 | ||
ef43aa38 | 1013 | static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq) |
b2174eeb | 1014 | { |
ef43aa38 | 1015 | return (void *)((char *)dmreq - cc->dmreq_start); |
b2174eeb HY |
1016 | } |
1017 | ||
2dc5327d MB |
1018 | static u8 *iv_of_dmreq(struct crypt_config *cc, |
1019 | struct dm_crypt_request *dmreq) | |
1020 | { | |
33d2f09f | 1021 | if (crypt_integrity_aead(cc)) |
ef43aa38 MB |
1022 | return (u8 *)ALIGN((unsigned long)(dmreq + 1), |
1023 | crypto_aead_alignmask(any_tfm_aead(cc)) + 1); | |
1024 | else | |
1025 | return (u8 *)ALIGN((unsigned long)(dmreq + 1), | |
1026 | crypto_skcipher_alignmask(any_tfm(cc)) + 1); | |
2dc5327d MB |
1027 | } |
1028 | ||
ef43aa38 MB |
1029 | static u8 *org_iv_of_dmreq(struct crypt_config *cc, |
1030 | struct dm_crypt_request *dmreq) | |
1031 | { | |
1032 | return iv_of_dmreq(cc, dmreq) + cc->iv_size; | |
1033 | } | |
1034 | ||
c13b5487 | 1035 | static __le64 *org_sector_of_dmreq(struct crypt_config *cc, |
ef43aa38 MB |
1036 | struct dm_crypt_request *dmreq) |
1037 | { | |
1038 | u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size; | |
c13b5487 | 1039 | return (__le64 *) ptr; |
ef43aa38 MB |
1040 | } |
1041 | ||
1042 | static unsigned int *org_tag_of_dmreq(struct crypt_config *cc, | |
1043 | struct dm_crypt_request *dmreq) | |
1044 | { | |
1045 | u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + | |
1046 | cc->iv_size + sizeof(uint64_t); | |
1047 | return (unsigned int*)ptr; | |
1048 | } | |
1049 | ||
1050 | static void *tag_from_dmreq(struct crypt_config *cc, | |
1051 | struct dm_crypt_request *dmreq) | |
1052 | { | |
1053 | struct convert_context *ctx = dmreq->ctx; | |
1054 | struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx); | |
1055 | ||
1056 | return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) * | |
1057 | cc->on_disk_tag_size]; | |
1058 | } | |
1059 | ||
1060 | static void *iv_tag_from_dmreq(struct crypt_config *cc, | |
1061 | struct dm_crypt_request *dmreq) | |
1062 | { | |
1063 | return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size; | |
1064 | } | |
1065 | ||
1066 | static int crypt_convert_block_aead(struct crypt_config *cc, | |
1067 | struct convert_context *ctx, | |
1068 | struct aead_request *req, | |
1069 | unsigned int tag_offset) | |
01482b76 | 1070 | { |
003b5c57 KO |
1071 | struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in); |
1072 | struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out); | |
3a7f6c99 | 1073 | struct dm_crypt_request *dmreq; |
ef43aa38 | 1074 | u8 *iv, *org_iv, *tag_iv, *tag; |
c13b5487 | 1075 | __le64 *sector; |
ef43aa38 MB |
1076 | int r = 0; |
1077 | ||
1078 | BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size); | |
3a7f6c99 | 1079 | |
8f0009a2 | 1080 | /* Reject unexpected unaligned bio. */ |
0440d5c0 | 1081 | if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) |
8f0009a2 | 1082 | return -EIO; |
3a7f6c99 | 1083 | |
b2174eeb | 1084 | dmreq = dmreq_of_req(cc, req); |
ef43aa38 | 1085 | dmreq->iv_sector = ctx->cc_sector; |
8f0009a2 | 1086 | if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) |
ff3af92b | 1087 | dmreq->iv_sector >>= cc->sector_shift; |
ef43aa38 MB |
1088 | dmreq->ctx = ctx; |
1089 | ||
1090 | *org_tag_of_dmreq(cc, dmreq) = tag_offset; | |
1091 | ||
1092 | sector = org_sector_of_dmreq(cc, dmreq); | |
1093 | *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); | |
1094 | ||
2dc5327d | 1095 | iv = iv_of_dmreq(cc, dmreq); |
ef43aa38 MB |
1096 | org_iv = org_iv_of_dmreq(cc, dmreq); |
1097 | tag = tag_from_dmreq(cc, dmreq); | |
1098 | tag_iv = iv_tag_from_dmreq(cc, dmreq); | |
1099 | ||
1100 | /* AEAD request: | |
1101 | * |----- AAD -------|------ DATA -------|-- AUTH TAG --| | |
1102 | * | (authenticated) | (auth+encryption) | | | |
1103 | * | sector_LE | IV | sector in/out | tag in/out | | |
1104 | */ | |
1105 | sg_init_table(dmreq->sg_in, 4); | |
1106 | sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t)); | |
1107 | sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size); | |
8f0009a2 | 1108 | sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset); |
ef43aa38 MB |
1109 | sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size); |
1110 | ||
1111 | sg_init_table(dmreq->sg_out, 4); | |
1112 | sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t)); | |
1113 | sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size); | |
8f0009a2 | 1114 | sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset); |
ef43aa38 MB |
1115 | sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size); |
1116 | ||
1117 | if (cc->iv_gen_ops) { | |
1118 | /* For READs use IV stored in integrity metadata */ | |
1119 | if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { | |
1120 | memcpy(org_iv, tag_iv, cc->iv_size); | |
1121 | } else { | |
1122 | r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); | |
1123 | if (r < 0) | |
1124 | return r; | |
1125 | /* Store generated IV in integrity metadata */ | |
1126 | if (cc->integrity_iv_size) | |
1127 | memcpy(tag_iv, org_iv, cc->iv_size); | |
1128 | } | |
1129 | /* Working copy of IV, to be modified in crypto API */ | |
1130 | memcpy(iv, org_iv, cc->iv_size); | |
1131 | } | |
1132 | ||
1133 | aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size); | |
1134 | if (bio_data_dir(ctx->bio_in) == WRITE) { | |
1135 | aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out, | |
8f0009a2 | 1136 | cc->sector_size, iv); |
ef43aa38 MB |
1137 | r = crypto_aead_encrypt(req); |
1138 | if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size) | |
1139 | memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0, | |
1140 | cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size)); | |
1141 | } else { | |
1142 | aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out, | |
8f0009a2 | 1143 | cc->sector_size + cc->integrity_tag_size, iv); |
ef43aa38 MB |
1144 | r = crypto_aead_decrypt(req); |
1145 | } | |
1146 | ||
f710126c MB |
1147 | if (r == -EBADMSG) { |
1148 | char b[BDEVNAME_SIZE]; | |
1149 | DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu", bio_devname(ctx->bio_in, b), | |
ef43aa38 | 1150 | (unsigned long long)le64_to_cpu(*sector)); |
f710126c | 1151 | } |
ef43aa38 MB |
1152 | |
1153 | if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) | |
1154 | r = cc->iv_gen_ops->post(cc, org_iv, dmreq); | |
1155 | ||
8f0009a2 MB |
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); | |
01482b76 | 1158 | |
ef43aa38 MB |
1159 | return r; |
1160 | } | |
1161 | ||
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) | |
1166 | { | |
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; | |
ef43aa38 | 1171 | u8 *iv, *org_iv, *tag_iv; |
c13b5487 | 1172 | __le64 *sector; |
ef43aa38 | 1173 | int r = 0; |
01482b76 | 1174 | |
8f0009a2 | 1175 | /* Reject unexpected unaligned bio. */ |
0440d5c0 | 1176 | if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) |
8f0009a2 MB |
1177 | return -EIO; |
1178 | ||
ef43aa38 | 1179 | dmreq = dmreq_of_req(cc, req); |
c66029f4 | 1180 | dmreq->iv_sector = ctx->cc_sector; |
8f0009a2 | 1181 | if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) |
ff3af92b | 1182 | dmreq->iv_sector >>= cc->sector_shift; |
b2174eeb | 1183 | dmreq->ctx = ctx; |
01482b76 | 1184 | |
ef43aa38 MB |
1185 | *org_tag_of_dmreq(cc, dmreq) = tag_offset; |
1186 | ||
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); | |
1190 | ||
1191 | sector = org_sector_of_dmreq(cc, dmreq); | |
1192 | *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); | |
1193 | ||
1194 | /* For skcipher we use only the first sg item */ | |
1195 | sg_in = &dmreq->sg_in[0]; | |
1196 | sg_out = &dmreq->sg_out[0]; | |
01482b76 | 1197 | |
ef43aa38 | 1198 | sg_init_table(sg_in, 1); |
8f0009a2 | 1199 | sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset); |
ef43aa38 MB |
1200 | |
1201 | sg_init_table(sg_out, 1); | |
8f0009a2 | 1202 | sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset); |
01482b76 | 1203 | |
3a7f6c99 | 1204 | if (cc->iv_gen_ops) { |
ef43aa38 MB |
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); | |
1208 | } else { | |
1209 | r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); | |
1210 | if (r < 0) | |
1211 | return r; | |
1212 | /* Store generated IV in integrity metadata */ | |
1213 | if (cc->integrity_iv_size) | |
1214 | memcpy(tag_iv, org_iv, cc->integrity_iv_size); | |
1215 | } | |
1216 | /* Working copy of IV, to be modified in crypto API */ | |
1217 | memcpy(iv, org_iv, cc->iv_size); | |
3a7f6c99 MB |
1218 | } |
1219 | ||
8f0009a2 | 1220 | skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv); |
3a7f6c99 MB |
1221 | |
1222 | if (bio_data_dir(ctx->bio_in) == WRITE) | |
bbdb23b5 | 1223 | r = crypto_skcipher_encrypt(req); |
3a7f6c99 | 1224 | else |
bbdb23b5 | 1225 | r = crypto_skcipher_decrypt(req); |
3a7f6c99 | 1226 | |
2dc5327d | 1227 | if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) |
ef43aa38 MB |
1228 | r = cc->iv_gen_ops->post(cc, org_iv, dmreq); |
1229 | ||
8f0009a2 MB |
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); | |
2dc5327d | 1232 | |
3a7f6c99 | 1233 | return r; |
01482b76 MB |
1234 | } |
1235 | ||
95497a96 MB |
1236 | static void kcryptd_async_done(struct crypto_async_request *async_req, |
1237 | int error); | |
c0297721 | 1238 | |
ef43aa38 MB |
1239 | static void crypt_alloc_req_skcipher(struct crypt_config *cc, |
1240 | struct convert_context *ctx) | |
ddd42edf | 1241 | { |
c66029f4 | 1242 | unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1); |
c0297721 | 1243 | |
ef43aa38 | 1244 | if (!ctx->r.req) |
6f1c819c | 1245 | ctx->r.req = mempool_alloc(&cc->req_pool, GFP_NOIO); |
c0297721 | 1246 | |
ef43aa38 | 1247 | skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]); |
54cea3f6 MB |
1248 | |
1249 | /* | |
1250 | * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs | |
1251 | * requests if driver request queue is full. | |
1252 | */ | |
ef43aa38 | 1253 | skcipher_request_set_callback(ctx->r.req, |
432061b3 | 1254 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
ef43aa38 | 1255 | kcryptd_async_done, dmreq_of_req(cc, ctx->r.req)); |
ddd42edf MB |
1256 | } |
1257 | ||
ef43aa38 MB |
1258 | static void crypt_alloc_req_aead(struct crypt_config *cc, |
1259 | struct convert_context *ctx) | |
1260 | { | |
1261 | if (!ctx->r.req_aead) | |
6f1c819c | 1262 | ctx->r.req_aead = mempool_alloc(&cc->req_pool, GFP_NOIO); |
c0297721 | 1263 | |
ef43aa38 | 1264 | aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]); |
54cea3f6 MB |
1265 | |
1266 | /* | |
1267 | * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs | |
1268 | * requests if driver request queue is full. | |
1269 | */ | |
ef43aa38 | 1270 | aead_request_set_callback(ctx->r.req_aead, |
432061b3 | 1271 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
ef43aa38 MB |
1272 | kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead)); |
1273 | } | |
1274 | ||
1275 | static void crypt_alloc_req(struct crypt_config *cc, | |
1276 | struct convert_context *ctx) | |
1277 | { | |
33d2f09f | 1278 | if (crypt_integrity_aead(cc)) |
ef43aa38 MB |
1279 | crypt_alloc_req_aead(cc, ctx); |
1280 | else | |
1281 | crypt_alloc_req_skcipher(cc, ctx); | |
ddd42edf MB |
1282 | } |
1283 | ||
ef43aa38 MB |
1284 | static void crypt_free_req_skcipher(struct crypt_config *cc, |
1285 | struct skcipher_request *req, struct bio *base_bio) | |
298a9fa0 MP |
1286 | { |
1287 | struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); | |
1288 | ||
bbdb23b5 | 1289 | if ((struct skcipher_request *)(io + 1) != req) |
6f1c819c | 1290 | mempool_free(req, &cc->req_pool); |
298a9fa0 MP |
1291 | } |
1292 | ||
ef43aa38 MB |
1293 | static void crypt_free_req_aead(struct crypt_config *cc, |
1294 | struct aead_request *req, struct bio *base_bio) | |
1295 | { | |
1296 | struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); | |
1297 | ||
1298 | if ((struct aead_request *)(io + 1) != req) | |
6f1c819c | 1299 | mempool_free(req, &cc->req_pool); |
ef43aa38 MB |
1300 | } |
1301 | ||
1302 | static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio) | |
1303 | { | |
33d2f09f | 1304 | if (crypt_integrity_aead(cc)) |
ef43aa38 MB |
1305 | crypt_free_req_aead(cc, req, base_bio); |
1306 | else | |
1307 | crypt_free_req_skcipher(cc, req, base_bio); | |
1308 | } | |
1309 | ||
1da177e4 LT |
1310 | /* |
1311 | * Encrypt / decrypt data from one bio to another one (can be the same one) | |
1312 | */ | |
4e4cbee9 | 1313 | static blk_status_t crypt_convert(struct crypt_config *cc, |
d469f841 | 1314 | struct convert_context *ctx) |
1da177e4 | 1315 | { |
ef43aa38 | 1316 | unsigned int tag_offset = 0; |
ff3af92b | 1317 | unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT; |
3f1e9070 | 1318 | int r; |
1da177e4 | 1319 | |
40b6229b | 1320 | atomic_set(&ctx->cc_pending, 1); |
c8081618 | 1321 | |
003b5c57 | 1322 | while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) { |
1da177e4 | 1323 | |
3a7f6c99 | 1324 | crypt_alloc_req(cc, ctx); |
40b6229b | 1325 | atomic_inc(&ctx->cc_pending); |
3f1e9070 | 1326 | |
33d2f09f | 1327 | if (crypt_integrity_aead(cc)) |
ef43aa38 MB |
1328 | r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset); |
1329 | else | |
1330 | r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset); | |
3a7f6c99 MB |
1331 | |
1332 | switch (r) { | |
54cea3f6 MB |
1333 | /* |
1334 | * The request was queued by a crypto driver | |
1335 | * but the driver request queue is full, let's wait. | |
1336 | */ | |
3a7f6c99 MB |
1337 | case -EBUSY: |
1338 | wait_for_completion(&ctx->restart); | |
16735d02 | 1339 | reinit_completion(&ctx->restart); |
54cea3f6 MB |
1340 | /* fall through */ |
1341 | /* | |
1342 | * The request is queued and processed asynchronously, | |
1343 | * completion function kcryptd_async_done() will be called. | |
1344 | */ | |
c0403ec0 | 1345 | case -EINPROGRESS: |
ef43aa38 | 1346 | ctx->r.req = NULL; |
8f0009a2 | 1347 | ctx->cc_sector += sector_step; |
583fe747 | 1348 | tag_offset++; |
3f1e9070 | 1349 | continue; |
54cea3f6 MB |
1350 | /* |
1351 | * The request was already processed (synchronously). | |
1352 | */ | |
3a7f6c99 | 1353 | case 0: |
40b6229b | 1354 | atomic_dec(&ctx->cc_pending); |
8f0009a2 | 1355 | ctx->cc_sector += sector_step; |
583fe747 | 1356 | tag_offset++; |
c7f1b204 | 1357 | cond_resched(); |
3a7f6c99 | 1358 | continue; |
ef43aa38 MB |
1359 | /* |
1360 | * There was a data integrity error. | |
1361 | */ | |
1362 | case -EBADMSG: | |
1363 | atomic_dec(&ctx->cc_pending); | |
4e4cbee9 | 1364 | return BLK_STS_PROTECTION; |
ef43aa38 MB |
1365 | /* |
1366 | * There was an error while processing the request. | |
1367 | */ | |
3f1e9070 | 1368 | default: |
40b6229b | 1369 | atomic_dec(&ctx->cc_pending); |
4e4cbee9 | 1370 | return BLK_STS_IOERR; |
3f1e9070 | 1371 | } |
1da177e4 LT |
1372 | } |
1373 | ||
3f1e9070 | 1374 | return 0; |
1da177e4 LT |
1375 | } |
1376 | ||
cf2f1abf MP |
1377 | static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone); |
1378 | ||
1da177e4 LT |
1379 | /* |
1380 | * Generate a new unfragmented bio with the given size | |
586b286b MS |
1381 | * This should never violate the device limitations (but only because |
1382 | * max_segment_size is being constrained to PAGE_SIZE). | |
7145c241 MP |
1383 | * |
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. | |
1389 | * | |
1390 | * In order to avoid this scenario we allocate the pages under a mutex. | |
1391 | * | |
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. | |
1da177e4 | 1395 | */ |
cf2f1abf | 1396 | static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size) |
1da177e4 | 1397 | { |
49a8a920 | 1398 | struct crypt_config *cc = io->cc; |
8b004457 | 1399 | struct bio *clone; |
1da177e4 | 1400 | unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
7145c241 MP |
1401 | gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM; |
1402 | unsigned i, len, remaining_size; | |
91e10625 | 1403 | struct page *page; |
1da177e4 | 1404 | |
7145c241 | 1405 | retry: |
d0164adc | 1406 | if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM)) |
7145c241 MP |
1407 | mutex_lock(&cc->bio_alloc_lock); |
1408 | ||
6f1c819c | 1409 | clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, &cc->bs); |
8b004457 | 1410 | if (!clone) |
ef43aa38 | 1411 | goto out; |
1da177e4 | 1412 | |
027581f3 | 1413 | clone_init(io, clone); |
6a24c718 | 1414 | |
7145c241 MP |
1415 | remaining_size = size; |
1416 | ||
f97380bc | 1417 | for (i = 0; i < nr_iovecs; i++) { |
6f1c819c | 1418 | page = mempool_alloc(&cc->page_pool, gfp_mask); |
7145c241 MP |
1419 | if (!page) { |
1420 | crypt_free_buffer_pages(cc, clone); | |
1421 | bio_put(clone); | |
d0164adc | 1422 | gfp_mask |= __GFP_DIRECT_RECLAIM; |
7145c241 MP |
1423 | goto retry; |
1424 | } | |
1da177e4 | 1425 | |
7145c241 | 1426 | len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size; |
91e10625 | 1427 | |
0dae7fe5 | 1428 | bio_add_page(clone, page, len, 0); |
1da177e4 | 1429 | |
7145c241 | 1430 | remaining_size -= len; |
1da177e4 LT |
1431 | } |
1432 | ||
ef43aa38 MB |
1433 | /* Allocate space for integrity tags */ |
1434 | if (dm_crypt_integrity_io_alloc(io, clone)) { | |
1435 | crypt_free_buffer_pages(cc, clone); | |
1436 | bio_put(clone); | |
1437 | clone = NULL; | |
1438 | } | |
1439 | out: | |
d0164adc | 1440 | if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM)) |
7145c241 MP |
1441 | mutex_unlock(&cc->bio_alloc_lock); |
1442 | ||
8b004457 | 1443 | return clone; |
1da177e4 LT |
1444 | } |
1445 | ||
644bd2f0 | 1446 | static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone) |
1da177e4 | 1447 | { |
1da177e4 | 1448 | struct bio_vec *bv; |
6dc4f100 | 1449 | struct bvec_iter_all iter_all; |
1da177e4 | 1450 | |
2b070cfe | 1451 | bio_for_each_segment_all(bv, clone, iter_all) { |
1da177e4 | 1452 | BUG_ON(!bv->bv_page); |
6f1c819c | 1453 | mempool_free(bv->bv_page, &cc->page_pool); |
1da177e4 LT |
1454 | } |
1455 | } | |
1456 | ||
298a9fa0 MP |
1457 | static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc, |
1458 | struct bio *bio, sector_t sector) | |
dc440d1e | 1459 | { |
49a8a920 | 1460 | io->cc = cc; |
dc440d1e MB |
1461 | io->base_bio = bio; |
1462 | io->sector = sector; | |
1463 | io->error = 0; | |
ef43aa38 MB |
1464 | io->ctx.r.req = NULL; |
1465 | io->integrity_metadata = NULL; | |
1466 | io->integrity_metadata_from_pool = false; | |
40b6229b | 1467 | atomic_set(&io->io_pending, 0); |
dc440d1e MB |
1468 | } |
1469 | ||
3e1a8bdd MB |
1470 | static void crypt_inc_pending(struct dm_crypt_io *io) |
1471 | { | |
40b6229b | 1472 | atomic_inc(&io->io_pending); |
3e1a8bdd MB |
1473 | } |
1474 | ||
1da177e4 LT |
1475 | /* |
1476 | * One of the bios was finished. Check for completion of | |
1477 | * the whole request and correctly clean up the buffer. | |
1478 | */ | |
5742fd77 | 1479 | static void crypt_dec_pending(struct dm_crypt_io *io) |
1da177e4 | 1480 | { |
49a8a920 | 1481 | struct crypt_config *cc = io->cc; |
b35f8caa | 1482 | struct bio *base_bio = io->base_bio; |
4e4cbee9 | 1483 | blk_status_t error = io->error; |
1da177e4 | 1484 | |
40b6229b | 1485 | if (!atomic_dec_and_test(&io->io_pending)) |
1da177e4 LT |
1486 | return; |
1487 | ||
ef43aa38 MB |
1488 | if (io->ctx.r.req) |
1489 | crypt_free_req(cc, io->ctx.r.req, base_bio); | |
1490 | ||
1491 | if (unlikely(io->integrity_metadata_from_pool)) | |
6f1c819c | 1492 | mempool_free(io->integrity_metadata, &io->cc->tag_pool); |
ef43aa38 MB |
1493 | else |
1494 | kfree(io->integrity_metadata); | |
b35f8caa | 1495 | |
4e4cbee9 | 1496 | base_bio->bi_status = error; |
4246a0b6 | 1497 | bio_endio(base_bio); |
1da177e4 LT |
1498 | } |
1499 | ||
1500 | /* | |
cabf08e4 | 1501 | * kcryptd/kcryptd_io: |
1da177e4 LT |
1502 | * |
1503 | * Needed because it would be very unwise to do decryption in an | |
23541d2d | 1504 | * interrupt context. |
cabf08e4 MB |
1505 | * |
1506 | * kcryptd performs the actual encryption or decryption. | |
1507 | * | |
1508 | * kcryptd_io performs the IO submission. | |
1509 | * | |
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. | |
c0297721 AK |
1513 | * |
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. | |
1da177e4 | 1516 | */ |
4246a0b6 | 1517 | static void crypt_endio(struct bio *clone) |
8b004457 | 1518 | { |
028867ac | 1519 | struct dm_crypt_io *io = clone->bi_private; |
49a8a920 | 1520 | struct crypt_config *cc = io->cc; |
ee7a491e | 1521 | unsigned rw = bio_data_dir(clone); |
4e4cbee9 | 1522 | blk_status_t error; |
8b004457 MB |
1523 | |
1524 | /* | |
6712ecf8 | 1525 | * free the processed pages |
8b004457 | 1526 | */ |
ee7a491e | 1527 | if (rw == WRITE) |
644bd2f0 | 1528 | crypt_free_buffer_pages(cc, clone); |
8b004457 | 1529 | |
4e4cbee9 | 1530 | error = clone->bi_status; |
8b004457 | 1531 | bio_put(clone); |
8b004457 | 1532 | |
9b81c842 | 1533 | if (rw == READ && !error) { |
ee7a491e MB |
1534 | kcryptd_queue_crypt(io); |
1535 | return; | |
1536 | } | |
5742fd77 | 1537 | |
9b81c842 SL |
1538 | if (unlikely(error)) |
1539 | io->error = error; | |
5742fd77 MB |
1540 | |
1541 | crypt_dec_pending(io); | |
8b004457 MB |
1542 | } |
1543 | ||
028867ac | 1544 | static void clone_init(struct dm_crypt_io *io, struct bio *clone) |
8b004457 | 1545 | { |
49a8a920 | 1546 | struct crypt_config *cc = io->cc; |
8b004457 MB |
1547 | |
1548 | clone->bi_private = io; | |
1549 | clone->bi_end_io = crypt_endio; | |
74d46992 | 1550 | bio_set_dev(clone, cc->dev->bdev); |
ef295ecf | 1551 | clone->bi_opf = io->base_bio->bi_opf; |
8b004457 MB |
1552 | } |
1553 | ||
20c82538 | 1554 | static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp) |
8b004457 | 1555 | { |
49a8a920 | 1556 | struct crypt_config *cc = io->cc; |
8b004457 | 1557 | struct bio *clone; |
93e605c2 | 1558 | |
8b004457 | 1559 | /* |
59779079 MS |
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(). | |
8b004457 | 1564 | */ |
6f1c819c | 1565 | clone = bio_clone_fast(io->base_bio, gfp, &cc->bs); |
7eaceacc | 1566 | if (!clone) |
20c82538 | 1567 | return 1; |
8b004457 | 1568 | |
20c82538 MB |
1569 | crypt_inc_pending(io); |
1570 | ||
8b004457 | 1571 | clone_init(io, clone); |
4f024f37 | 1572 | clone->bi_iter.bi_sector = cc->start + io->sector; |
8b004457 | 1573 | |
ef43aa38 MB |
1574 | if (dm_crypt_integrity_io_alloc(io, clone)) { |
1575 | crypt_dec_pending(io); | |
1576 | bio_put(clone); | |
1577 | return 1; | |
1578 | } | |
1579 | ||
93e605c2 | 1580 | generic_make_request(clone); |
20c82538 | 1581 | return 0; |
8b004457 MB |
1582 | } |
1583 | ||
dc267621 MP |
1584 | static void kcryptd_io_read_work(struct work_struct *work) |
1585 | { | |
1586 | struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); | |
1587 | ||
1588 | crypt_inc_pending(io); | |
1589 | if (kcryptd_io_read(io, GFP_NOIO)) | |
4e4cbee9 | 1590 | io->error = BLK_STS_RESOURCE; |
dc267621 MP |
1591 | crypt_dec_pending(io); |
1592 | } | |
1593 | ||
1594 | static void kcryptd_queue_read(struct dm_crypt_io *io) | |
1595 | { | |
1596 | struct crypt_config *cc = io->cc; | |
1597 | ||
1598 | INIT_WORK(&io->work, kcryptd_io_read_work); | |
1599 | queue_work(cc->io_queue, &io->work); | |
1600 | } | |
1601 | ||
4e4eef64 MB |
1602 | static void kcryptd_io_write(struct dm_crypt_io *io) |
1603 | { | |
95497a96 | 1604 | struct bio *clone = io->ctx.bio_out; |
dc267621 | 1605 | |
95497a96 | 1606 | generic_make_request(clone); |
4e4eef64 MB |
1607 | } |
1608 | ||
b3c5fd30 MP |
1609 | #define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node) |
1610 | ||
dc267621 | 1611 | static int dmcrypt_write(void *data) |
395b167c | 1612 | { |
dc267621 | 1613 | struct crypt_config *cc = data; |
b3c5fd30 MP |
1614 | struct dm_crypt_io *io; |
1615 | ||
dc267621 | 1616 | while (1) { |
b3c5fd30 | 1617 | struct rb_root write_tree; |
dc267621 | 1618 | struct blk_plug plug; |
395b167c | 1619 | |
c7329eff | 1620 | spin_lock_irq(&cc->write_thread_lock); |
dc267621 | 1621 | continue_locked: |
395b167c | 1622 | |
b3c5fd30 | 1623 | if (!RB_EMPTY_ROOT(&cc->write_tree)) |
dc267621 MP |
1624 | goto pop_from_list; |
1625 | ||
f659b100 | 1626 | set_current_state(TASK_INTERRUPTIBLE); |
dc267621 | 1627 | |
c7329eff | 1628 | spin_unlock_irq(&cc->write_thread_lock); |
dc267621 | 1629 | |
f659b100 | 1630 | if (unlikely(kthread_should_stop())) { |
642fa448 | 1631 | set_current_state(TASK_RUNNING); |
f659b100 RV |
1632 | break; |
1633 | } | |
1634 | ||
dc267621 MP |
1635 | schedule(); |
1636 | ||
642fa448 | 1637 | set_current_state(TASK_RUNNING); |
c7329eff | 1638 | spin_lock_irq(&cc->write_thread_lock); |
dc267621 MP |
1639 | goto continue_locked; |
1640 | ||
1641 | pop_from_list: | |
b3c5fd30 MP |
1642 | write_tree = cc->write_tree; |
1643 | cc->write_tree = RB_ROOT; | |
c7329eff | 1644 | spin_unlock_irq(&cc->write_thread_lock); |
dc267621 | 1645 | |
b3c5fd30 MP |
1646 | BUG_ON(rb_parent(write_tree.rb_node)); |
1647 | ||
1648 | /* | |
1649 | * Note: we cannot walk the tree here with rb_next because | |
1650 | * the structures may be freed when kcryptd_io_write is called. | |
1651 | */ | |
dc267621 MP |
1652 | blk_start_plug(&plug); |
1653 | do { | |
b3c5fd30 MP |
1654 | io = crypt_io_from_node(rb_first(&write_tree)); |
1655 | rb_erase(&io->rb_node, &write_tree); | |
dc267621 | 1656 | kcryptd_io_write(io); |
b3c5fd30 | 1657 | } while (!RB_EMPTY_ROOT(&write_tree)); |
dc267621 MP |
1658 | blk_finish_plug(&plug); |
1659 | } | |
1660 | return 0; | |
395b167c AK |
1661 | } |
1662 | ||
72c6e7af | 1663 | static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async) |
4e4eef64 | 1664 | { |
dec1cedf | 1665 | struct bio *clone = io->ctx.bio_out; |
49a8a920 | 1666 | struct crypt_config *cc = io->cc; |
dc267621 | 1667 | unsigned long flags; |
b3c5fd30 MP |
1668 | sector_t sector; |
1669 | struct rb_node **rbp, *parent; | |
dec1cedf | 1670 | |
4e4cbee9 | 1671 | if (unlikely(io->error)) { |
dec1cedf MB |
1672 | crypt_free_buffer_pages(cc, clone); |
1673 | bio_put(clone); | |
6c031f41 | 1674 | crypt_dec_pending(io); |
dec1cedf MB |
1675 | return; |
1676 | } | |
1677 | ||
1678 | /* crypt_convert should have filled the clone bio */ | |
003b5c57 | 1679 | BUG_ON(io->ctx.iter_out.bi_size); |
dec1cedf | 1680 | |
4f024f37 | 1681 | clone->bi_iter.bi_sector = cc->start + io->sector; |
899c95d3 | 1682 | |
0f5d8e6e MP |
1683 | if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) { |
1684 | generic_make_request(clone); | |
1685 | return; | |
1686 | } | |
1687 | ||
c7329eff MP |
1688 | spin_lock_irqsave(&cc->write_thread_lock, flags); |
1689 | if (RB_EMPTY_ROOT(&cc->write_tree)) | |
1690 | wake_up_process(cc->write_thread); | |
b3c5fd30 MP |
1691 | rbp = &cc->write_tree.rb_node; |
1692 | parent = NULL; | |
1693 | sector = io->sector; | |
1694 | while (*rbp) { | |
1695 | parent = *rbp; | |
1696 | if (sector < crypt_io_from_node(parent)->sector) | |
1697 | rbp = &(*rbp)->rb_left; | |
1698 | else | |
1699 | rbp = &(*rbp)->rb_right; | |
1700 | } | |
1701 | rb_link_node(&io->rb_node, parent, rbp); | |
1702 | rb_insert_color(&io->rb_node, &cc->write_tree); | |
c7329eff | 1703 | spin_unlock_irqrestore(&cc->write_thread_lock, flags); |
4e4eef64 MB |
1704 | } |
1705 | ||
fc5a5e9a | 1706 | static void kcryptd_crypt_write_convert(struct dm_crypt_io *io) |
8b004457 | 1707 | { |
49a8a920 | 1708 | struct crypt_config *cc = io->cc; |
8b004457 | 1709 | struct bio *clone; |
c8081618 | 1710 | int crypt_finished; |
b635b00e | 1711 | sector_t sector = io->sector; |
4e4cbee9 | 1712 | blk_status_t r; |
8b004457 | 1713 | |
fc5a5e9a MB |
1714 | /* |
1715 | * Prevent io from disappearing until this function completes. | |
1716 | */ | |
1717 | crypt_inc_pending(io); | |
b635b00e | 1718 | crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector); |
fc5a5e9a | 1719 | |
cf2f1abf MP |
1720 | clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size); |
1721 | if (unlikely(!clone)) { | |
4e4cbee9 | 1722 | io->error = BLK_STS_IOERR; |
cf2f1abf MP |
1723 | goto dec; |
1724 | } | |
c8081618 | 1725 | |
cf2f1abf MP |
1726 | io->ctx.bio_out = clone; |
1727 | io->ctx.iter_out = clone->bi_iter; | |
b635b00e | 1728 | |
cf2f1abf | 1729 | sector += bio_sectors(clone); |
93e605c2 | 1730 | |
cf2f1abf MP |
1731 | crypt_inc_pending(io); |
1732 | r = crypt_convert(cc, &io->ctx); | |
4e4cbee9 | 1733 | if (r) |
ef43aa38 | 1734 | io->error = r; |
cf2f1abf | 1735 | crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending); |
933f01d4 | 1736 | |
cf2f1abf MP |
1737 | /* Encryption was already finished, submit io now */ |
1738 | if (crypt_finished) { | |
1739 | kcryptd_crypt_write_io_submit(io, 0); | |
1740 | io->sector = sector; | |
93e605c2 | 1741 | } |
899c95d3 | 1742 | |
cf2f1abf | 1743 | dec: |
899c95d3 | 1744 | crypt_dec_pending(io); |
84131db6 MB |
1745 | } |
1746 | ||
72c6e7af | 1747 | static void kcryptd_crypt_read_done(struct dm_crypt_io *io) |
5742fd77 | 1748 | { |
5742fd77 MB |
1749 | crypt_dec_pending(io); |
1750 | } | |
1751 | ||
4e4eef64 | 1752 | static void kcryptd_crypt_read_convert(struct dm_crypt_io *io) |
8b004457 | 1753 | { |
49a8a920 | 1754 | struct crypt_config *cc = io->cc; |
4e4cbee9 | 1755 | blk_status_t r; |
1da177e4 | 1756 | |
3e1a8bdd | 1757 | crypt_inc_pending(io); |
3a7f6c99 | 1758 | |
53017030 | 1759 | crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio, |
0c395b0f | 1760 | io->sector); |
1da177e4 | 1761 | |
5742fd77 | 1762 | r = crypt_convert(cc, &io->ctx); |
4e4cbee9 | 1763 | if (r) |
ef43aa38 | 1764 | io->error = r; |
5742fd77 | 1765 | |
40b6229b | 1766 | if (atomic_dec_and_test(&io->ctx.cc_pending)) |
72c6e7af | 1767 | kcryptd_crypt_read_done(io); |
3a7f6c99 MB |
1768 | |
1769 | crypt_dec_pending(io); | |
1da177e4 LT |
1770 | } |
1771 | ||
95497a96 MB |
1772 | static void kcryptd_async_done(struct crypto_async_request *async_req, |
1773 | int error) | |
1774 | { | |
b2174eeb HY |
1775 | struct dm_crypt_request *dmreq = async_req->data; |
1776 | struct convert_context *ctx = dmreq->ctx; | |
95497a96 | 1777 | struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx); |
49a8a920 | 1778 | struct crypt_config *cc = io->cc; |
95497a96 | 1779 | |
54cea3f6 MB |
1780 | /* |
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.) | |
1784 | */ | |
c0403ec0 RV |
1785 | if (error == -EINPROGRESS) { |
1786 | complete(&ctx->restart); | |
95497a96 | 1787 | return; |
c0403ec0 | 1788 | } |
95497a96 | 1789 | |
2dc5327d | 1790 | if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post) |
ef43aa38 | 1791 | error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq); |
2dc5327d | 1792 | |
ef43aa38 | 1793 | if (error == -EBADMSG) { |
f710126c MB |
1794 | char b[BDEVNAME_SIZE]; |
1795 | DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu", bio_devname(ctx->bio_in, b), | |
ef43aa38 | 1796 | (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq))); |
4e4cbee9 | 1797 | io->error = BLK_STS_PROTECTION; |
ef43aa38 | 1798 | } else if (error < 0) |
4e4cbee9 | 1799 | io->error = BLK_STS_IOERR; |
72c6e7af | 1800 | |
298a9fa0 | 1801 | crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio); |
95497a96 | 1802 | |
40b6229b | 1803 | if (!atomic_dec_and_test(&ctx->cc_pending)) |
c0403ec0 | 1804 | return; |
95497a96 MB |
1805 | |
1806 | if (bio_data_dir(io->base_bio) == READ) | |
72c6e7af | 1807 | kcryptd_crypt_read_done(io); |
95497a96 | 1808 | else |
72c6e7af | 1809 | kcryptd_crypt_write_io_submit(io, 1); |
95497a96 MB |
1810 | } |
1811 | ||
395b167c | 1812 | static void kcryptd_crypt(struct work_struct *work) |
1da177e4 | 1813 | { |
028867ac | 1814 | struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); |
8b004457 | 1815 | |
cabf08e4 | 1816 | if (bio_data_dir(io->base_bio) == READ) |
395b167c | 1817 | kcryptd_crypt_read_convert(io); |
4e4eef64 | 1818 | else |
395b167c | 1819 | kcryptd_crypt_write_convert(io); |
cabf08e4 MB |
1820 | } |
1821 | ||
395b167c | 1822 | static void kcryptd_queue_crypt(struct dm_crypt_io *io) |
cabf08e4 | 1823 | { |
49a8a920 | 1824 | struct crypt_config *cc = io->cc; |
cabf08e4 | 1825 | |
395b167c AK |
1826 | INIT_WORK(&io->work, kcryptd_crypt); |
1827 | queue_work(cc->crypt_queue, &io->work); | |
1da177e4 LT |
1828 | } |
1829 | ||
ef43aa38 | 1830 | static void crypt_free_tfms_aead(struct crypt_config *cc) |
1da177e4 | 1831 | { |
ef43aa38 MB |
1832 | if (!cc->cipher_tfm.tfms_aead) |
1833 | return; | |
1da177e4 | 1834 | |
ef43aa38 MB |
1835 | if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) { |
1836 | crypto_free_aead(cc->cipher_tfm.tfms_aead[0]); | |
1837 | cc->cipher_tfm.tfms_aead[0] = NULL; | |
1da177e4 LT |
1838 | } |
1839 | ||
ef43aa38 MB |
1840 | kfree(cc->cipher_tfm.tfms_aead); |
1841 | cc->cipher_tfm.tfms_aead = NULL; | |
1da177e4 LT |
1842 | } |
1843 | ||
ef43aa38 | 1844 | static void crypt_free_tfms_skcipher(struct crypt_config *cc) |
d1f96423 | 1845 | { |
d1f96423 MB |
1846 | unsigned i; |
1847 | ||
ef43aa38 | 1848 | if (!cc->cipher_tfm.tfms) |
fd2d231f MP |
1849 | return; |
1850 | ||
d1f96423 | 1851 | for (i = 0; i < cc->tfms_count; i++) |
ef43aa38 MB |
1852 | if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) { |
1853 | crypto_free_skcipher(cc->cipher_tfm.tfms[i]); | |
1854 | cc->cipher_tfm.tfms[i] = NULL; | |
d1f96423 | 1855 | } |
fd2d231f | 1856 | |
ef43aa38 MB |
1857 | kfree(cc->cipher_tfm.tfms); |
1858 | cc->cipher_tfm.tfms = NULL; | |
d1f96423 MB |
1859 | } |
1860 | ||
ef43aa38 MB |
1861 | static void crypt_free_tfms(struct crypt_config *cc) |
1862 | { | |
33d2f09f | 1863 | if (crypt_integrity_aead(cc)) |
ef43aa38 MB |
1864 | crypt_free_tfms_aead(cc); |
1865 | else | |
1866 | crypt_free_tfms_skcipher(cc); | |
1867 | } | |
1868 | ||
1869 | static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode) | |
d1f96423 | 1870 | { |
d1f96423 MB |
1871 | unsigned i; |
1872 | int err; | |
1873 | ||
6396bb22 KC |
1874 | cc->cipher_tfm.tfms = kcalloc(cc->tfms_count, |
1875 | sizeof(struct crypto_skcipher *), | |
1876 | GFP_KERNEL); | |
ef43aa38 | 1877 | if (!cc->cipher_tfm.tfms) |
fd2d231f MP |
1878 | return -ENOMEM; |
1879 | ||
d1f96423 | 1880 | for (i = 0; i < cc->tfms_count; i++) { |
ef43aa38 MB |
1881 | cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0); |
1882 | if (IS_ERR(cc->cipher_tfm.tfms[i])) { | |
1883 | err = PTR_ERR(cc->cipher_tfm.tfms[i]); | |
fd2d231f | 1884 | crypt_free_tfms(cc); |
d1f96423 MB |
1885 | return err; |
1886 | } | |
1887 | } | |
1888 | ||
af331eba EB |
1889 | /* |
1890 | * dm-crypt performance can vary greatly depending on which crypto | |
1891 | * algorithm implementation is used. Help people debug performance | |
1892 | * problems by logging the ->cra_driver_name. | |
1893 | */ | |
7a1cd723 | 1894 | DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode, |
af331eba | 1895 | crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name); |
d1f96423 MB |
1896 | return 0; |
1897 | } | |
1898 | ||
ef43aa38 MB |
1899 | static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode) |
1900 | { | |
ef43aa38 MB |
1901 | int err; |
1902 | ||
1903 | cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL); | |
1904 | if (!cc->cipher_tfm.tfms) | |
1905 | return -ENOMEM; | |
1906 | ||
ef43aa38 MB |
1907 | cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0); |
1908 | if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) { | |
1909 | err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]); | |
1910 | crypt_free_tfms(cc); | |
1911 | return err; | |
1912 | } | |
1913 | ||
7a1cd723 | 1914 | DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode, |
af331eba | 1915 | crypto_aead_alg(any_tfm_aead(cc))->base.cra_driver_name); |
ef43aa38 MB |
1916 | return 0; |
1917 | } | |
1918 | ||
1919 | static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode) | |
1920 | { | |
33d2f09f | 1921 | if (crypt_integrity_aead(cc)) |
ef43aa38 MB |
1922 | return crypt_alloc_tfms_aead(cc, ciphermode); |
1923 | else | |
1924 | return crypt_alloc_tfms_skcipher(cc, ciphermode); | |
1925 | } | |
1926 | ||
1927 | static unsigned crypt_subkey_size(struct crypt_config *cc) | |
1928 | { | |
1929 | return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count); | |
1930 | } | |
1931 | ||
1932 | static unsigned crypt_authenckey_size(struct crypt_config *cc) | |
1933 | { | |
1934 | return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param)); | |
1935 | } | |
1936 | ||
1937 | /* | |
1938 | * If AEAD is composed like authenc(hmac(sha256),xts(aes)), | |
1939 | * the key must be for some reason in special format. | |
1940 | * This funcion converts cc->key to this special format. | |
1941 | */ | |
1942 | static void crypt_copy_authenckey(char *p, const void *key, | |
1943 | unsigned enckeylen, unsigned authkeylen) | |
1944 | { | |
1945 | struct crypto_authenc_key_param *param; | |
1946 | struct rtattr *rta; | |
1947 | ||
1948 | rta = (struct rtattr *)p; | |
1949 | param = RTA_DATA(rta); | |
1950 | param->enckeylen = cpu_to_be32(enckeylen); | |
1951 | rta->rta_len = RTA_LENGTH(sizeof(*param)); | |
1952 | rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM; | |
1953 | p += RTA_SPACE(sizeof(*param)); | |
1954 | memcpy(p, key + enckeylen, authkeylen); | |
1955 | p += authkeylen; | |
1956 | memcpy(p, key, enckeylen); | |
1957 | } | |
1958 | ||
671ea6b4 | 1959 | static int crypt_setkey(struct crypt_config *cc) |
c0297721 | 1960 | { |
da31a078 | 1961 | unsigned subkey_size; |
fd2d231f MP |
1962 | int err = 0, i, r; |
1963 | ||
da31a078 | 1964 | /* Ignore extra keys (which are used for IV etc) */ |
ef43aa38 | 1965 | subkey_size = crypt_subkey_size(cc); |
da31a078 | 1966 | |
27c70036 MB |
1967 | if (crypt_integrity_hmac(cc)) { |
1968 | if (subkey_size < cc->key_mac_size) | |
1969 | return -EINVAL; | |
1970 | ||
ef43aa38 MB |
1971 | crypt_copy_authenckey(cc->authenc_key, cc->key, |
1972 | subkey_size - cc->key_mac_size, | |
1973 | cc->key_mac_size); | |
27c70036 MB |
1974 | } |
1975 | ||
fd2d231f | 1976 | for (i = 0; i < cc->tfms_count; i++) { |
33d2f09f | 1977 | if (crypt_integrity_hmac(cc)) |
ef43aa38 MB |
1978 | r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], |
1979 | cc->authenc_key, crypt_authenckey_size(cc)); | |
33d2f09f MB |
1980 | else if (crypt_integrity_aead(cc)) |
1981 | r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], | |
1982 | cc->key + (i * subkey_size), | |
1983 | subkey_size); | |
ef43aa38 MB |
1984 | else |
1985 | r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i], | |
1986 | cc->key + (i * subkey_size), | |
1987 | subkey_size); | |
fd2d231f MP |
1988 | if (r) |
1989 | err = r; | |
c0297721 AK |
1990 | } |
1991 | ||
ef43aa38 MB |
1992 | if (crypt_integrity_hmac(cc)) |
1993 | memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc)); | |
1994 | ||
c0297721 AK |
1995 | return err; |
1996 | } | |
1997 | ||
c538f6ec OK |
1998 | #ifdef CONFIG_KEYS |
1999 | ||
027c431c OK |
2000 | static bool contains_whitespace(const char *str) |
2001 | { | |
2002 | while (*str) | |
2003 | if (isspace(*str++)) | |
2004 | return true; | |
2005 | return false; | |
2006 | } | |
2007 | ||
c538f6ec OK |
2008 | static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) |
2009 | { | |
2010 | char *new_key_string, *key_desc; | |
2011 | int ret; | |
2012 | struct key *key; | |
2013 | const struct user_key_payload *ukp; | |
2014 | ||
027c431c OK |
2015 | /* |
2016 | * Reject key_string with whitespace. dm core currently lacks code for | |
2017 | * proper whitespace escaping in arguments on DM_TABLE_STATUS path. | |
2018 | */ | |
2019 | if (contains_whitespace(key_string)) { | |
2020 | DMERR("whitespace chars not allowed in key string"); | |
2021 | return -EINVAL; | |
2022 | } | |
2023 | ||
c538f6ec OK |
2024 | /* look for next ':' separating key_type from key_description */ |
2025 | key_desc = strpbrk(key_string, ":"); | |
2026 | if (!key_desc || key_desc == key_string || !strlen(key_desc + 1)) | |
2027 | return -EINVAL; | |
2028 | ||
2029 | if (strncmp(key_string, "logon:", key_desc - key_string + 1) && | |
2030 | strncmp(key_string, "user:", key_desc - key_string + 1)) | |
2031 | return -EINVAL; | |
2032 | ||
2033 | new_key_string = kstrdup(key_string, GFP_KERNEL); | |
2034 | if (!new_key_string) | |
2035 | return -ENOMEM; | |
2036 | ||
2037 | key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user, | |
2038 | key_desc + 1, NULL); | |
2039 | if (IS_ERR(key)) { | |
2040 | kzfree(new_key_string); | |
2041 | return PTR_ERR(key); | |
2042 | } | |
2043 | ||
f5b0cba8 | 2044 | down_read(&key->sem); |
c538f6ec | 2045 | |
0837e49a | 2046 | ukp = user_key_payload_locked(key); |
c538f6ec | 2047 | if (!ukp) { |
f5b0cba8 | 2048 | up_read(&key->sem); |
c538f6ec OK |
2049 | key_put(key); |
2050 | kzfree(new_key_string); | |
2051 | return -EKEYREVOKED; | |
2052 | } | |
2053 | ||
2054 | if (cc->key_size != ukp->datalen) { | |
f5b0cba8 | 2055 | up_read(&key->sem); |
c538f6ec OK |
2056 | key_put(key); |
2057 | kzfree(new_key_string); | |
2058 | return -EINVAL; | |
2059 | } | |
2060 | ||
2061 | memcpy(cc->key, ukp->data, cc->key_size); | |
2062 | ||
f5b0cba8 | 2063 | up_read(&key->sem); |
c538f6ec OK |
2064 | key_put(key); |
2065 | ||
2066 | /* clear the flag since following operations may invalidate previously valid key */ | |
2067 | clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); | |
2068 | ||
2069 | ret = crypt_setkey(cc); | |
2070 | ||
c538f6ec OK |
2071 | if (!ret) { |
2072 | set_bit(DM_CRYPT_KEY_VALID, &cc->flags); | |
2073 | kzfree(cc->key_string); | |
2074 | cc->key_string = new_key_string; | |
2075 | } else | |
2076 | kzfree(new_key_string); | |
2077 | ||
2078 | return ret; | |
2079 | } | |
2080 | ||
2081 | static int get_key_size(char **key_string) | |
2082 | { | |
2083 | char *colon, dummy; | |
2084 | int ret; | |
2085 | ||
2086 | if (*key_string[0] != ':') | |
2087 | return strlen(*key_string) >> 1; | |
2088 | ||
2089 | /* look for next ':' in key string */ | |
2090 | colon = strpbrk(*key_string + 1, ":"); | |
2091 | if (!colon) | |
2092 | return -EINVAL; | |
2093 | ||
2094 | if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':') | |
2095 | return -EINVAL; | |
2096 | ||
2097 | *key_string = colon; | |
2098 | ||
2099 | /* remaining key string should be :<logon|user>:<key_desc> */ | |
2100 | ||
2101 | return ret; | |
2102 | } | |
2103 | ||
2104 | #else | |
2105 | ||
2106 | static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) | |
2107 | { | |
2108 | return -EINVAL; | |
2109 | } | |
2110 | ||
2111 | static int get_key_size(char **key_string) | |
2112 | { | |
2113 | return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1; | |
2114 | } | |
2115 | ||
2116 | #endif | |
2117 | ||
e48d4bbf MB |
2118 | static int crypt_set_key(struct crypt_config *cc, char *key) |
2119 | { | |
de8be5ac MB |
2120 | int r = -EINVAL; |
2121 | int key_string_len = strlen(key); | |
2122 | ||
69a8cfcd MB |
2123 | /* Hyphen (which gives a key_size of zero) means there is no key. */ |
2124 | if (!cc->key_size && strcmp(key, "-")) | |
de8be5ac | 2125 | goto out; |
e48d4bbf | 2126 | |
c538f6ec OK |
2127 | /* ':' means the key is in kernel keyring, short-circuit normal key processing */ |
2128 | if (key[0] == ':') { | |
2129 | r = crypt_set_keyring_key(cc, key + 1); | |
de8be5ac | 2130 | goto out; |
c538f6ec | 2131 | } |
e48d4bbf | 2132 | |
265e9098 OK |
2133 | /* clear the flag since following operations may invalidate previously valid key */ |
2134 | clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); | |
e48d4bbf | 2135 | |
c538f6ec OK |
2136 | /* wipe references to any kernel keyring key */ |
2137 | kzfree(cc->key_string); | |
2138 | cc->key_string = NULL; | |
2139 | ||
e944e03e AS |
2140 | /* Decode key from its hex representation. */ |
2141 | if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0) | |
de8be5ac | 2142 | goto out; |
e48d4bbf | 2143 | |
671ea6b4 | 2144 | r = crypt_setkey(cc); |
265e9098 OK |
2145 | if (!r) |
2146 | set_bit(DM_CRYPT_KEY_VALID, &cc->flags); | |
de8be5ac MB |
2147 | |
2148 | out: | |
2149 | /* Hex key string not needed after here, so wipe it. */ | |
2150 | memset(key, '0', key_string_len); | |
2151 | ||
2152 | return r; | |
e48d4bbf MB |
2153 | } |
2154 | ||
2155 | static int crypt_wipe_key(struct crypt_config *cc) | |
2156 | { | |
c82feeec OK |
2157 | int r; |
2158 | ||
e48d4bbf | 2159 | clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
c82feeec | 2160 | get_random_bytes(&cc->key, cc->key_size); |
c538f6ec OK |
2161 | kzfree(cc->key_string); |
2162 | cc->key_string = NULL; | |
c82feeec OK |
2163 | r = crypt_setkey(cc); |
2164 | memset(&cc->key, 0, cc->key_size * sizeof(u8)); | |
c0297721 | 2165 | |
c82feeec | 2166 | return r; |
e48d4bbf MB |
2167 | } |
2168 | ||
5059353d MP |
2169 | static void crypt_calculate_pages_per_client(void) |
2170 | { | |
ca79b0c2 | 2171 | unsigned long pages = (totalram_pages() - totalhigh_pages()) * DM_CRYPT_MEMORY_PERCENT / 100; |
5059353d MP |
2172 | |
2173 | if (!dm_crypt_clients_n) | |
2174 | return; | |
2175 | ||
2176 | pages /= dm_crypt_clients_n; | |
2177 | if (pages < DM_CRYPT_MIN_PAGES_PER_CLIENT) | |
2178 | pages = DM_CRYPT_MIN_PAGES_PER_CLIENT; | |
2179 | dm_crypt_pages_per_client = pages; | |
2180 | } | |
2181 | ||
2182 | static void *crypt_page_alloc(gfp_t gfp_mask, void *pool_data) | |
2183 | { | |
2184 | struct crypt_config *cc = pool_data; | |
2185 | struct page *page; | |
2186 | ||
2187 | if (unlikely(percpu_counter_compare(&cc->n_allocated_pages, dm_crypt_pages_per_client) >= 0) && | |
2188 | likely(gfp_mask & __GFP_NORETRY)) | |
2189 | return NULL; | |
2190 | ||
2191 | page = alloc_page(gfp_mask); | |
2192 | if (likely(page != NULL)) | |
2193 | percpu_counter_add(&cc->n_allocated_pages, 1); | |
2194 | ||
2195 | return page; | |
2196 | } | |
2197 | ||
2198 | static void crypt_page_free(void *page, void *pool_data) | |
2199 | { | |
2200 | struct crypt_config *cc = pool_data; | |
2201 | ||
2202 | __free_page(page); | |
2203 | percpu_counter_sub(&cc->n_allocated_pages, 1); | |
2204 | } | |
2205 | ||
28513fcc MB |
2206 | static void crypt_dtr(struct dm_target *ti) |
2207 | { | |
2208 | struct crypt_config *cc = ti->private; | |
2209 | ||
2210 | ti->private = NULL; | |
2211 | ||
2212 | if (!cc) | |
2213 | return; | |
2214 | ||
f659b100 | 2215 | if (cc->write_thread) |
dc267621 MP |
2216 | kthread_stop(cc->write_thread); |
2217 | ||
28513fcc MB |
2218 | if (cc->io_queue) |
2219 | destroy_workqueue(cc->io_queue); | |
2220 | if (cc->crypt_queue) | |
2221 | destroy_workqueue(cc->crypt_queue); | |
2222 | ||
fd2d231f MP |
2223 | crypt_free_tfms(cc); |
2224 | ||
6f1c819c | 2225 | bioset_exit(&cc->bs); |
28513fcc | 2226 | |
6f1c819c KO |
2227 | mempool_exit(&cc->page_pool); |
2228 | mempool_exit(&cc->req_pool); | |
2229 | mempool_exit(&cc->tag_pool); | |
2230 | ||
d00a11df KO |
2231 | WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0); |
2232 | percpu_counter_destroy(&cc->n_allocated_pages); | |
2233 | ||
28513fcc MB |
2234 | if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) |
2235 | cc->iv_gen_ops->dtr(cc); | |
2236 | ||
28513fcc MB |
2237 | if (cc->dev) |
2238 | dm_put_device(ti, cc->dev); | |
2239 | ||
5ebaee6d | 2240 | kzfree(cc->cipher); |
7dbcd137 | 2241 | kzfree(cc->cipher_string); |
c538f6ec | 2242 | kzfree(cc->key_string); |
ef43aa38 MB |
2243 | kzfree(cc->cipher_auth); |
2244 | kzfree(cc->authenc_key); | |
28513fcc | 2245 | |
d5ffebdd MS |
2246 | mutex_destroy(&cc->bio_alloc_lock); |
2247 | ||
28513fcc MB |
2248 | /* Must zero key material before freeing */ |
2249 | kzfree(cc); | |
5059353d MP |
2250 | |
2251 | spin_lock(&dm_crypt_clients_lock); | |
2252 | WARN_ON(!dm_crypt_clients_n); | |
2253 | dm_crypt_clients_n--; | |
2254 | crypt_calculate_pages_per_client(); | |
2255 | spin_unlock(&dm_crypt_clients_lock); | |
28513fcc MB |
2256 | } |
2257 | ||
e889f97a MB |
2258 | static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode) |
2259 | { | |
2260 | struct crypt_config *cc = ti->private; | |
2261 | ||
33d2f09f | 2262 | if (crypt_integrity_aead(cc)) |
e889f97a MB |
2263 | cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); |
2264 | else | |
2265 | cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); | |
2266 | ||
e889f97a MB |
2267 | if (cc->iv_size) |
2268 | /* at least a 64 bit sector number should fit in our buffer */ | |
2269 | cc->iv_size = max(cc->iv_size, | |
2270 | (unsigned int)(sizeof(u64) / sizeof(u8))); | |
2271 | else if (ivmode) { | |
2272 | DMWARN("Selected cipher does not support IVs"); | |
2273 | ivmode = NULL; | |
2274 | } | |
2275 | ||
2276 | /* Choose ivmode, see comments at iv code. */ | |
2277 | if (ivmode == NULL) | |
2278 | cc->iv_gen_ops = NULL; | |
2279 | else if (strcmp(ivmode, "plain") == 0) | |
2280 | cc->iv_gen_ops = &crypt_iv_plain_ops; | |
2281 | else if (strcmp(ivmode, "plain64") == 0) | |
2282 | cc->iv_gen_ops = &crypt_iv_plain64_ops; | |
7e3fd855 MB |
2283 | else if (strcmp(ivmode, "plain64be") == 0) |
2284 | cc->iv_gen_ops = &crypt_iv_plain64be_ops; | |
e889f97a MB |
2285 | else if (strcmp(ivmode, "essiv") == 0) |
2286 | cc->iv_gen_ops = &crypt_iv_essiv_ops; | |
2287 | else if (strcmp(ivmode, "benbi") == 0) | |
2288 | cc->iv_gen_ops = &crypt_iv_benbi_ops; | |
2289 | else if (strcmp(ivmode, "null") == 0) | |
2290 | cc->iv_gen_ops = &crypt_iv_null_ops; | |
2291 | else if (strcmp(ivmode, "lmk") == 0) { | |
2292 | cc->iv_gen_ops = &crypt_iv_lmk_ops; | |
2293 | /* | |
2294 | * Version 2 and 3 is recognised according | |
2295 | * to length of provided multi-key string. | |
2296 | * If present (version 3), last key is used as IV seed. | |
2297 | * All keys (including IV seed) are always the same size. | |
2298 | */ | |
2299 | if (cc->key_size % cc->key_parts) { | |
2300 | cc->key_parts++; | |
2301 | cc->key_extra_size = cc->key_size / cc->key_parts; | |
2302 | } | |
2303 | } else if (strcmp(ivmode, "tcw") == 0) { | |
2304 | cc->iv_gen_ops = &crypt_iv_tcw_ops; | |
2305 | cc->key_parts += 2; /* IV + whitening */ | |
2306 | cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE; | |
2307 | } else if (strcmp(ivmode, "random") == 0) { | |
2308 | cc->iv_gen_ops = &crypt_iv_random_ops; | |
2309 | /* Need storage space in integrity fields. */ | |
2310 | cc->integrity_iv_size = cc->iv_size; | |
2311 | } else { | |
2312 | ti->error = "Invalid IV mode"; | |
2313 | return -EINVAL; | |
2314 | } | |
2315 | ||
2316 | return 0; | |
2317 | } | |
2318 | ||
33d2f09f MB |
2319 | /* |
2320 | * Workaround to parse cipher algorithm from crypto API spec. | |
2321 | * The cc->cipher is currently used only in ESSIV. | |
2322 | * This should be probably done by crypto-api calls (once available...) | |
2323 | */ | |
2324 | static int crypt_ctr_blkdev_cipher(struct crypt_config *cc) | |
2325 | { | |
2326 | const char *alg_name = NULL; | |
2327 | char *start, *end; | |
2328 | ||
2329 | if (crypt_integrity_aead(cc)) { | |
2330 | alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc))); | |
2331 | if (!alg_name) | |
2332 | return -EINVAL; | |
2333 | if (crypt_integrity_hmac(cc)) { | |
2334 | alg_name = strchr(alg_name, ','); | |
2335 | if (!alg_name) | |
2336 | return -EINVAL; | |
2337 | } | |
2338 | alg_name++; | |
2339 | } else { | |
2340 | alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc))); | |
2341 | if (!alg_name) | |
2342 | return -EINVAL; | |
2343 | } | |
2344 | ||
2345 | start = strchr(alg_name, '('); | |
2346 | end = strchr(alg_name, ')'); | |
2347 | ||
2348 | if (!start && !end) { | |
2349 | cc->cipher = kstrdup(alg_name, GFP_KERNEL); | |
2350 | return cc->cipher ? 0 : -ENOMEM; | |
2351 | } | |
2352 | ||
2353 | if (!start || !end || ++start >= end) | |
2354 | return -EINVAL; | |
2355 | ||
2356 | cc->cipher = kzalloc(end - start + 1, GFP_KERNEL); | |
2357 | if (!cc->cipher) | |
2358 | return -ENOMEM; | |
2359 | ||
2360 | strncpy(cc->cipher, start, end - start); | |
2361 | ||
2362 | return 0; | |
2363 | } | |
2364 | ||
2365 | /* | |
2366 | * Workaround to parse HMAC algorithm from AEAD crypto API spec. | |
2367 | * The HMAC is needed to calculate tag size (HMAC digest size). | |
2368 | * This should be probably done by crypto-api calls (once available...) | |
2369 | */ | |
2370 | static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api) | |
2371 | { | |
2372 | char *start, *end, *mac_alg = NULL; | |
2373 | struct crypto_ahash *mac; | |
2374 | ||
2375 | if (!strstarts(cipher_api, "authenc(")) | |
2376 | return 0; | |
2377 | ||
2378 | start = strchr(cipher_api, '('); | |
2379 | end = strchr(cipher_api, ','); | |
2380 | if (!start || !end || ++start > end) | |
2381 | return -EINVAL; | |
2382 | ||
2383 | mac_alg = kzalloc(end - start + 1, GFP_KERNEL); | |
2384 | if (!mac_alg) | |
2385 | return -ENOMEM; | |
2386 | strncpy(mac_alg, start, end - start); | |
2387 | ||
2388 | mac = crypto_alloc_ahash(mac_alg, 0, 0); | |
2389 | kfree(mac_alg); | |
2390 | ||
2391 | if (IS_ERR(mac)) | |
2392 | return PTR_ERR(mac); | |
2393 | ||
2394 | cc->key_mac_size = crypto_ahash_digestsize(mac); | |
2395 | crypto_free_ahash(mac); | |
2396 | ||
2397 | cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL); | |
2398 | if (!cc->authenc_key) | |
2399 | return -ENOMEM; | |
2400 | ||
2401 | return 0; | |
2402 | } | |
2403 | ||
2404 | static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key, | |
2405 | char **ivmode, char **ivopts) | |
2406 | { | |
2407 | struct crypt_config *cc = ti->private; | |
2408 | char *tmp, *cipher_api; | |
2409 | int ret = -EINVAL; | |
2410 | ||
2411 | cc->tfms_count = 1; | |
2412 | ||
2413 | /* | |
2414 | * New format (capi: prefix) | |
2415 | * capi:cipher_api_spec-iv:ivopts | |
2416 | */ | |
2417 | tmp = &cipher_in[strlen("capi:")]; | |
1856b9f7 MB |
2418 | |
2419 | /* Separate IV options if present, it can contain another '-' in hash name */ | |
2420 | *ivopts = strrchr(tmp, ':'); | |
2421 | if (*ivopts) { | |
2422 | **ivopts = '\0'; | |
2423 | (*ivopts)++; | |
2424 | } | |
2425 | /* Parse IV mode */ | |
2426 | *ivmode = strrchr(tmp, '-'); | |
2427 | if (*ivmode) { | |
2428 | **ivmode = '\0'; | |
2429 | (*ivmode)++; | |
2430 | } | |
2431 | /* The rest is crypto API spec */ | |
2432 | cipher_api = tmp; | |
33d2f09f MB |
2433 | |
2434 | if (*ivmode && !strcmp(*ivmode, "lmk")) | |
2435 | cc->tfms_count = 64; | |
2436 | ||
2437 | cc->key_parts = cc->tfms_count; | |
2438 | ||
2439 | /* Allocate cipher */ | |
2440 | ret = crypt_alloc_tfms(cc, cipher_api); | |
2441 | if (ret < 0) { | |
2442 | ti->error = "Error allocating crypto tfm"; | |
2443 | return ret; | |
2444 | } | |
2445 | ||
2446 | /* Alloc AEAD, can be used only in new format. */ | |
2447 | if (crypt_integrity_aead(cc)) { | |
2448 | ret = crypt_ctr_auth_cipher(cc, cipher_api); | |
2449 | if (ret < 0) { | |
2450 | ti->error = "Invalid AEAD cipher spec"; | |
2451 | return -ENOMEM; | |
2452 | } | |
2453 | cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); | |
2454 | } else | |
2455 | cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); | |
2456 | ||
2457 | ret = crypt_ctr_blkdev_cipher(cc); | |
2458 | if (ret < 0) { | |
2459 | ti->error = "Cannot allocate cipher string"; | |
2460 | return -ENOMEM; | |
2461 | } | |
2462 | ||
2463 | return 0; | |
2464 | } | |
2465 | ||
2466 | static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key, | |
2467 | char **ivmode, char **ivopts) | |
1da177e4 | 2468 | { |
5ebaee6d | 2469 | struct crypt_config *cc = ti->private; |
33d2f09f | 2470 | char *tmp, *cipher, *chainmode, *keycount; |
5ebaee6d | 2471 | char *cipher_api = NULL; |
fd2d231f | 2472 | int ret = -EINVAL; |
31998ef1 | 2473 | char dummy; |
1da177e4 | 2474 | |
33d2f09f | 2475 | if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) { |
5ebaee6d | 2476 | ti->error = "Bad cipher specification"; |
1da177e4 LT |
2477 | return -EINVAL; |
2478 | } | |
2479 | ||
5ebaee6d MB |
2480 | /* |
2481 | * Legacy dm-crypt cipher specification | |
d1f96423 | 2482 | * cipher[:keycount]-mode-iv:ivopts |
5ebaee6d MB |
2483 | */ |
2484 | tmp = cipher_in; | |
d1f96423 MB |
2485 | keycount = strsep(&tmp, "-"); |
2486 | cipher = strsep(&keycount, ":"); | |
2487 | ||
2488 | if (!keycount) | |
2489 | cc->tfms_count = 1; | |
31998ef1 | 2490 | else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 || |
d1f96423 MB |
2491 | !is_power_of_2(cc->tfms_count)) { |
2492 | ti->error = "Bad cipher key count specification"; | |
2493 | return -EINVAL; | |
2494 | } | |
2495 | cc->key_parts = cc->tfms_count; | |
5ebaee6d MB |
2496 | |
2497 | cc->cipher = kstrdup(cipher, GFP_KERNEL); | |
2498 | if (!cc->cipher) | |
2499 | goto bad_mem; | |
2500 | ||
1da177e4 | 2501 | chainmode = strsep(&tmp, "-"); |
1856b9f7 MB |
2502 | *ivmode = strsep(&tmp, ":"); |
2503 | *ivopts = tmp; | |
1da177e4 | 2504 | |
7dbcd137 MB |
2505 | /* |
2506 | * For compatibility with the original dm-crypt mapping format, if | |
2507 | * only the cipher name is supplied, use cbc-plain. | |
2508 | */ | |
33d2f09f | 2509 | if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) { |
1da177e4 | 2510 | chainmode = "cbc"; |
33d2f09f | 2511 | *ivmode = "plain"; |
1da177e4 LT |
2512 | } |
2513 | ||
33d2f09f | 2514 | if (strcmp(chainmode, "ecb") && !*ivmode) { |
5ebaee6d MB |
2515 | ti->error = "IV mechanism required"; |
2516 | return -EINVAL; | |
1da177e4 LT |
2517 | } |
2518 | ||
5ebaee6d MB |
2519 | cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL); |
2520 | if (!cipher_api) | |
2521 | goto bad_mem; | |
2522 | ||
2523 | ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME, | |
2524 | "%s(%s)", chainmode, cipher); | |
2525 | if (ret < 0) { | |
2526 | kfree(cipher_api); | |
2527 | goto bad_mem; | |
1da177e4 LT |
2528 | } |
2529 | ||
5ebaee6d | 2530 | /* Allocate cipher */ |
fd2d231f MP |
2531 | ret = crypt_alloc_tfms(cc, cipher_api); |
2532 | if (ret < 0) { | |
2533 | ti->error = "Error allocating crypto tfm"; | |
33d2f09f MB |
2534 | kfree(cipher_api); |
2535 | return ret; | |
1da177e4 | 2536 | } |
bd86e320 | 2537 | kfree(cipher_api); |
1da177e4 | 2538 | |
33d2f09f MB |
2539 | return 0; |
2540 | bad_mem: | |
2541 | ti->error = "Cannot allocate cipher strings"; | |
2542 | return -ENOMEM; | |
2543 | } | |
5ebaee6d | 2544 | |
33d2f09f MB |
2545 | static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key) |
2546 | { | |
2547 | struct crypt_config *cc = ti->private; | |
2548 | char *ivmode = NULL, *ivopts = NULL; | |
2549 | int ret; | |
2550 | ||
2551 | cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL); | |
2552 | if (!cc->cipher_string) { | |
2553 | ti->error = "Cannot allocate cipher strings"; | |
2554 | return -ENOMEM; | |
1da177e4 LT |
2555 | } |
2556 | ||
33d2f09f MB |
2557 | if (strstarts(cipher_in, "capi:")) |
2558 | ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts); | |
2559 | else | |
2560 | ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts); | |
2561 | if (ret) | |
2562 | return ret; | |
2563 | ||
5ebaee6d | 2564 | /* Initialize IV */ |
e889f97a MB |
2565 | ret = crypt_ctr_ivmode(ti, ivmode); |
2566 | if (ret < 0) | |
33d2f09f | 2567 | return ret; |
1da177e4 | 2568 | |
da31a078 MB |
2569 | /* Initialize and set key */ |
2570 | ret = crypt_set_key(cc, key); | |
2571 | if (ret < 0) { | |
2572 | ti->error = "Error decoding and setting key"; | |
33d2f09f | 2573 | return ret; |
da31a078 MB |
2574 | } |
2575 | ||
28513fcc MB |
2576 | /* Allocate IV */ |
2577 | if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) { | |
2578 | ret = cc->iv_gen_ops->ctr(cc, ti, ivopts); | |
2579 | if (ret < 0) { | |
2580 | ti->error = "Error creating IV"; | |
33d2f09f | 2581 | return ret; |
28513fcc MB |
2582 | } |
2583 | } | |
1da177e4 | 2584 | |
28513fcc MB |
2585 | /* Initialize IV (set keys for ESSIV etc) */ |
2586 | if (cc->iv_gen_ops && cc->iv_gen_ops->init) { | |
2587 | ret = cc->iv_gen_ops->init(cc); | |
2588 | if (ret < 0) { | |
2589 | ti->error = "Error initialising IV"; | |
33d2f09f | 2590 | return ret; |
28513fcc | 2591 | } |
b95bf2d3 MB |
2592 | } |
2593 | ||
dc94902b OK |
2594 | /* wipe the kernel key payload copy */ |
2595 | if (cc->key_string) | |
2596 | memset(cc->key, 0, cc->key_size * sizeof(u8)); | |
2597 | ||
5ebaee6d | 2598 | return ret; |
5ebaee6d | 2599 | } |
5ebaee6d | 2600 | |
ef43aa38 MB |
2601 | static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv) |
2602 | { | |
2603 | struct crypt_config *cc = ti->private; | |
2604 | struct dm_arg_set as; | |
5916a22b | 2605 | static const struct dm_arg _args[] = { |
8f0009a2 | 2606 | {0, 6, "Invalid number of feature args"}, |
ef43aa38 MB |
2607 | }; |
2608 | unsigned int opt_params, val; | |
2609 | const char *opt_string, *sval; | |
8f0009a2 | 2610 | char dummy; |
ef43aa38 MB |
2611 | int ret; |
2612 | ||
2613 | /* Optional parameters */ | |
2614 | as.argc = argc; | |
2615 | as.argv = argv; | |
2616 | ||
2617 | ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error); | |
2618 | if (ret) | |
2619 | return ret; | |
2620 | ||
2621 | while (opt_params--) { | |
2622 | opt_string = dm_shift_arg(&as); | |
2623 | if (!opt_string) { | |
2624 | ti->error = "Not enough feature arguments"; | |
2625 | return -EINVAL; | |
2626 | } | |
2627 | ||
2628 | if (!strcasecmp(opt_string, "allow_discards")) | |
2629 | ti->num_discard_bios = 1; | |
2630 | ||
2631 | else if (!strcasecmp(opt_string, "same_cpu_crypt")) | |
2632 | set_bit(DM_CRYPT_SAME_CPU, &cc->flags); | |
2633 | ||
2634 | else if (!strcasecmp(opt_string, "submit_from_crypt_cpus")) | |
2635 | set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); | |
2636 | else if (sscanf(opt_string, "integrity:%u:", &val) == 1) { | |
2637 | if (val == 0 || val > MAX_TAG_SIZE) { | |
2638 | ti->error = "Invalid integrity arguments"; | |
2639 | return -EINVAL; | |
2640 | } | |
2641 | cc->on_disk_tag_size = val; | |
2642 | sval = strchr(opt_string + strlen("integrity:"), ':') + 1; | |
2643 | if (!strcasecmp(sval, "aead")) { | |
2644 | set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); | |
ef43aa38 MB |
2645 | } else if (strcasecmp(sval, "none")) { |
2646 | ti->error = "Unknown integrity profile"; | |
2647 | return -EINVAL; | |
2648 | } | |
2649 | ||
2650 | cc->cipher_auth = kstrdup(sval, GFP_KERNEL); | |
2651 | if (!cc->cipher_auth) | |
2652 | return -ENOMEM; | |
ff3af92b | 2653 | } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) { |
8f0009a2 MB |
2654 | if (cc->sector_size < (1 << SECTOR_SHIFT) || |
2655 | cc->sector_size > 4096 || | |
ff3af92b | 2656 | (cc->sector_size & (cc->sector_size - 1))) { |
8f0009a2 MB |
2657 | ti->error = "Invalid feature value for sector_size"; |
2658 | return -EINVAL; | |
2659 | } | |
783874b0 MB |
2660 | if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) { |
2661 | ti->error = "Device size is not multiple of sector_size feature"; | |
2662 | return -EINVAL; | |
2663 | } | |
ff3af92b | 2664 | cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT; |
8f0009a2 MB |
2665 | } else if (!strcasecmp(opt_string, "iv_large_sectors")) |
2666 | set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); | |
2667 | else { | |
ef43aa38 MB |
2668 | ti->error = "Invalid feature arguments"; |
2669 | return -EINVAL; | |
2670 | } | |
2671 | } | |
2672 | ||
2673 | return 0; | |
5ebaee6d MB |
2674 | } |
2675 | ||
2676 | /* | |
2677 | * Construct an encryption mapping: | |
c538f6ec | 2678 | * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start> |
5ebaee6d MB |
2679 | */ |
2680 | static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) | |
2681 | { | |
2682 | struct crypt_config *cc; | |
ed0302e8 | 2683 | const char *devname = dm_table_device_name(ti->table); |
c538f6ec | 2684 | int key_size; |
ef43aa38 | 2685 | unsigned int align_mask; |
5ebaee6d MB |
2686 | unsigned long long tmpll; |
2687 | int ret; | |
ef43aa38 | 2688 | size_t iv_size_padding, additional_req_size; |
31998ef1 | 2689 | char dummy; |
772ae5f5 | 2690 | |
772ae5f5 | 2691 | if (argc < 5) { |
5ebaee6d MB |
2692 | ti->error = "Not enough arguments"; |
2693 | return -EINVAL; | |
1da177e4 LT |
2694 | } |
2695 | ||
c538f6ec OK |
2696 | key_size = get_key_size(&argv[1]); |
2697 | if (key_size < 0) { | |
2698 | ti->error = "Cannot parse key size"; | |
2699 | return -EINVAL; | |
2700 | } | |
5ebaee6d | 2701 | |
9c81c99b | 2702 | cc = kzalloc(struct_size(cc, key, key_size), GFP_KERNEL); |
5ebaee6d MB |
2703 | if (!cc) { |
2704 | ti->error = "Cannot allocate encryption context"; | |
2705 | return -ENOMEM; | |
2706 | } | |
69a8cfcd | 2707 | cc->key_size = key_size; |
8f0009a2 | 2708 | cc->sector_size = (1 << SECTOR_SHIFT); |
ff3af92b | 2709 | cc->sector_shift = 0; |
5ebaee6d MB |
2710 | |
2711 | ti->private = cc; | |
ef43aa38 | 2712 | |
5059353d MP |
2713 | spin_lock(&dm_crypt_clients_lock); |
2714 | dm_crypt_clients_n++; | |
2715 | crypt_calculate_pages_per_client(); | |
2716 | spin_unlock(&dm_crypt_clients_lock); | |
2717 | ||
2718 | ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL); | |
2719 | if (ret < 0) | |
2720 | goto bad; | |
2721 | ||
ef43aa38 MB |
2722 | /* Optional parameters need to be read before cipher constructor */ |
2723 | if (argc > 5) { | |
2724 | ret = crypt_ctr_optional(ti, argc - 5, &argv[5]); | |
2725 | if (ret) | |
2726 | goto bad; | |
2727 | } | |
2728 | ||
5ebaee6d MB |
2729 | ret = crypt_ctr_cipher(ti, argv[0], argv[1]); |
2730 | if (ret < 0) | |
2731 | goto bad; | |
2732 | ||
33d2f09f | 2733 | if (crypt_integrity_aead(cc)) { |
ef43aa38 MB |
2734 | cc->dmreq_start = sizeof(struct aead_request); |
2735 | cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc)); | |
2736 | align_mask = crypto_aead_alignmask(any_tfm_aead(cc)); | |
2737 | } else { | |
2738 | cc->dmreq_start = sizeof(struct skcipher_request); | |
2739 | cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc)); | |
2740 | align_mask = crypto_skcipher_alignmask(any_tfm(cc)); | |
2741 | } | |
d49ec52f MP |
2742 | cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request)); |
2743 | ||
ef43aa38 | 2744 | if (align_mask < CRYPTO_MINALIGN) { |
d49ec52f MP |
2745 | /* Allocate the padding exactly */ |
2746 | iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request)) | |
ef43aa38 | 2747 | & align_mask; |
d49ec52f MP |
2748 | } else { |
2749 | /* | |
2750 | * If the cipher requires greater alignment than kmalloc | |
2751 | * alignment, we don't know the exact position of the | |
2752 | * initialization vector. We must assume worst case. | |
2753 | */ | |
ef43aa38 | 2754 | iv_size_padding = align_mask; |
d49ec52f | 2755 | } |
ddd42edf | 2756 | |
ef43aa38 MB |
2757 | /* ...| IV + padding | original IV | original sec. number | bio tag offset | */ |
2758 | additional_req_size = sizeof(struct dm_crypt_request) + | |
2759 | iv_size_padding + cc->iv_size + | |
2760 | cc->iv_size + | |
2761 | sizeof(uint64_t) + | |
2762 | sizeof(unsigned int); | |
2763 | ||
6f1c819c KO |
2764 | ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size); |
2765 | if (ret) { | |
ddd42edf | 2766 | ti->error = "Cannot allocate crypt request mempool"; |
28513fcc | 2767 | goto bad; |
ddd42edf | 2768 | } |
ddd42edf | 2769 | |
30187e1d | 2770 | cc->per_bio_data_size = ti->per_io_data_size = |
ef43aa38 | 2771 | ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size, |
d49ec52f | 2772 | ARCH_KMALLOC_MINALIGN); |
298a9fa0 | 2773 | |
6f1c819c KO |
2774 | ret = mempool_init(&cc->page_pool, BIO_MAX_PAGES, crypt_page_alloc, crypt_page_free, cc); |
2775 | if (ret) { | |
72d94861 | 2776 | ti->error = "Cannot allocate page mempool"; |
28513fcc | 2777 | goto bad; |
1da177e4 LT |
2778 | } |
2779 | ||
6f1c819c KO |
2780 | ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS); |
2781 | if (ret) { | |
6a24c718 | 2782 | ti->error = "Cannot allocate crypt bioset"; |
28513fcc | 2783 | goto bad; |
6a24c718 MB |
2784 | } |
2785 | ||
7145c241 MP |
2786 | mutex_init(&cc->bio_alloc_lock); |
2787 | ||
28513fcc | 2788 | ret = -EINVAL; |
8f0009a2 MB |
2789 | if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) || |
2790 | (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) { | |
72d94861 | 2791 | ti->error = "Invalid iv_offset sector"; |
28513fcc | 2792 | goto bad; |
1da177e4 | 2793 | } |
4ee218cd | 2794 | cc->iv_offset = tmpll; |
1da177e4 | 2795 | |
e80d1c80 VG |
2796 | ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev); |
2797 | if (ret) { | |
28513fcc MB |
2798 | ti->error = "Device lookup failed"; |
2799 | goto bad; | |
2800 | } | |
2801 | ||
e80d1c80 | 2802 | ret = -EINVAL; |
ef87bfc2 | 2803 | if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) { |
72d94861 | 2804 | ti->error = "Invalid device sector"; |
28513fcc | 2805 | goto bad; |
1da177e4 | 2806 | } |
4ee218cd | 2807 | cc->start = tmpll; |
1da177e4 | 2808 | |
33d2f09f | 2809 | if (crypt_integrity_aead(cc) || cc->integrity_iv_size) { |
ef43aa38 | 2810 | ret = crypt_integrity_ctr(cc, ti); |
772ae5f5 MB |
2811 | if (ret) |
2812 | goto bad; | |
2813 | ||
ef43aa38 MB |
2814 | cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size; |
2815 | if (!cc->tag_pool_max_sectors) | |
2816 | cc->tag_pool_max_sectors = 1; | |
f3396c58 | 2817 | |
6f1c819c | 2818 | ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS, |
ef43aa38 | 2819 | cc->tag_pool_max_sectors * cc->on_disk_tag_size); |
6f1c819c | 2820 | if (ret) { |
ef43aa38 MB |
2821 | ti->error = "Cannot allocate integrity tags mempool"; |
2822 | goto bad; | |
772ae5f5 | 2823 | } |
583fe747 MP |
2824 | |
2825 | cc->tag_pool_max_sectors <<= cc->sector_shift; | |
772ae5f5 MB |
2826 | } |
2827 | ||
28513fcc | 2828 | ret = -ENOMEM; |
ed0302e8 MM |
2829 | cc->io_queue = alloc_workqueue("kcryptd_io/%s", |
2830 | WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, | |
2831 | 1, devname); | |
cabf08e4 MB |
2832 | if (!cc->io_queue) { |
2833 | ti->error = "Couldn't create kcryptd io queue"; | |
28513fcc | 2834 | goto bad; |
cabf08e4 MB |
2835 | } |
2836 | ||
f3396c58 | 2837 | if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) |
ed0302e8 MM |
2838 | cc->crypt_queue = alloc_workqueue("kcryptd/%s", |
2839 | WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, | |
2840 | 1, devname); | |
f3396c58 | 2841 | else |
ed0302e8 | 2842 | cc->crypt_queue = alloc_workqueue("kcryptd/%s", |
a1b89132 | 2843 | WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, |
ed0302e8 | 2844 | num_online_cpus(), devname); |
cabf08e4 | 2845 | if (!cc->crypt_queue) { |
9934a8be | 2846 | ti->error = "Couldn't create kcryptd queue"; |
28513fcc | 2847 | goto bad; |
9934a8be MB |
2848 | } |
2849 | ||
c7329eff | 2850 | spin_lock_init(&cc->write_thread_lock); |
b3c5fd30 | 2851 | cc->write_tree = RB_ROOT; |
dc267621 | 2852 | |
ed0302e8 | 2853 | cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write/%s", devname); |
dc267621 MP |
2854 | if (IS_ERR(cc->write_thread)) { |
2855 | ret = PTR_ERR(cc->write_thread); | |
2856 | cc->write_thread = NULL; | |
2857 | ti->error = "Couldn't spawn write thread"; | |
2858 | goto bad; | |
2859 | } | |
2860 | wake_up_process(cc->write_thread); | |
2861 | ||
55a62eef | 2862 | ti->num_flush_bios = 1; |
983c7db3 | 2863 | |
1da177e4 LT |
2864 | return 0; |
2865 | ||
28513fcc MB |
2866 | bad: |
2867 | crypt_dtr(ti); | |
2868 | return ret; | |
1da177e4 LT |
2869 | } |
2870 | ||
7de3ee57 | 2871 | static int crypt_map(struct dm_target *ti, struct bio *bio) |
1da177e4 | 2872 | { |
028867ac | 2873 | struct dm_crypt_io *io; |
49a8a920 | 2874 | struct crypt_config *cc = ti->private; |
647c7db1 | 2875 | |
772ae5f5 | 2876 | /* |
28a8f0d3 MC |
2877 | * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues. |
2878 | * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight | |
e6047149 | 2879 | * - for REQ_OP_DISCARD caller must use flush if IO ordering matters |
772ae5f5 | 2880 | */ |
1eff9d32 | 2881 | if (unlikely(bio->bi_opf & REQ_PREFLUSH || |
28a8f0d3 | 2882 | bio_op(bio) == REQ_OP_DISCARD)) { |
74d46992 | 2883 | bio_set_dev(bio, cc->dev->bdev); |
772ae5f5 | 2884 | if (bio_sectors(bio)) |
4f024f37 KO |
2885 | bio->bi_iter.bi_sector = cc->start + |
2886 | dm_target_offset(ti, bio->bi_iter.bi_sector); | |
647c7db1 MP |
2887 | return DM_MAPIO_REMAPPED; |
2888 | } | |
1da177e4 | 2889 | |
4e870e94 MP |
2890 | /* |
2891 | * Check if bio is too large, split as needed. | |
2892 | */ | |
2893 | if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) && | |
ef43aa38 | 2894 | (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size)) |
4e870e94 MP |
2895 | dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT)); |
2896 | ||
8f0009a2 MB |
2897 | /* |
2898 | * Ensure that bio is a multiple of internal sector encryption size | |
2899 | * and is aligned to this size as defined in IO hints. | |
2900 | */ | |
2901 | if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0)) | |
846785e6 | 2902 | return DM_MAPIO_KILL; |
8f0009a2 MB |
2903 | |
2904 | if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1))) | |
846785e6 | 2905 | return DM_MAPIO_KILL; |
8f0009a2 | 2906 | |
298a9fa0 MP |
2907 | io = dm_per_bio_data(bio, cc->per_bio_data_size); |
2908 | crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector)); | |
ef43aa38 MB |
2909 | |
2910 | if (cc->on_disk_tag_size) { | |
583fe747 | 2911 | unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift); |
ef43aa38 MB |
2912 | |
2913 | if (unlikely(tag_len > KMALLOC_MAX_SIZE) || | |
583fe747 | 2914 | unlikely(!(io->integrity_metadata = kmalloc(tag_len, |
ef43aa38 MB |
2915 | GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) { |
2916 | if (bio_sectors(bio) > cc->tag_pool_max_sectors) | |
2917 | dm_accept_partial_bio(bio, cc->tag_pool_max_sectors); | |
6f1c819c | 2918 | io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO); |
ef43aa38 MB |
2919 | io->integrity_metadata_from_pool = true; |
2920 | } | |
2921 | } | |
2922 | ||
33d2f09f | 2923 | if (crypt_integrity_aead(cc)) |
ef43aa38 MB |
2924 | io->ctx.r.req_aead = (struct aead_request *)(io + 1); |
2925 | else | |
2926 | io->ctx.r.req = (struct skcipher_request *)(io + 1); | |
cabf08e4 | 2927 | |
20c82538 MB |
2928 | if (bio_data_dir(io->base_bio) == READ) { |
2929 | if (kcryptd_io_read(io, GFP_NOWAIT)) | |
dc267621 | 2930 | kcryptd_queue_read(io); |
20c82538 | 2931 | } else |
cabf08e4 | 2932 | kcryptd_queue_crypt(io); |
1da177e4 | 2933 | |
d2a7ad29 | 2934 | return DM_MAPIO_SUBMITTED; |
1da177e4 LT |
2935 | } |
2936 | ||
fd7c092e MP |
2937 | static void crypt_status(struct dm_target *ti, status_type_t type, |
2938 | unsigned status_flags, char *result, unsigned maxlen) | |
1da177e4 | 2939 | { |
5ebaee6d | 2940 | struct crypt_config *cc = ti->private; |
fd7c092e | 2941 | unsigned i, sz = 0; |
f3396c58 | 2942 | int num_feature_args = 0; |
1da177e4 LT |
2943 | |
2944 | switch (type) { | |
2945 | case STATUSTYPE_INFO: | |
2946 | result[0] = '\0'; | |
2947 | break; | |
2948 | ||
2949 | case STATUSTYPE_TABLE: | |
7dbcd137 | 2950 | DMEMIT("%s ", cc->cipher_string); |
1da177e4 | 2951 | |
c538f6ec OK |
2952 | if (cc->key_size > 0) { |
2953 | if (cc->key_string) | |
2954 | DMEMIT(":%u:%s", cc->key_size, cc->key_string); | |
2955 | else | |
2956 | for (i = 0; i < cc->key_size; i++) | |
2957 | DMEMIT("%02x", cc->key[i]); | |
2958 | } else | |
fd7c092e | 2959 | DMEMIT("-"); |
1da177e4 | 2960 | |
4ee218cd AM |
2961 | DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, |
2962 | cc->dev->name, (unsigned long long)cc->start); | |
772ae5f5 | 2963 | |
f3396c58 MP |
2964 | num_feature_args += !!ti->num_discard_bios; |
2965 | num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags); | |
0f5d8e6e | 2966 | num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); |
ff3af92b | 2967 | num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT); |
8f0009a2 | 2968 | num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); |
ef43aa38 MB |
2969 | if (cc->on_disk_tag_size) |
2970 | num_feature_args++; | |
f3396c58 MP |
2971 | if (num_feature_args) { |
2972 | DMEMIT(" %d", num_feature_args); | |
2973 | if (ti->num_discard_bios) | |
2974 | DMEMIT(" allow_discards"); | |
2975 | if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) | |
2976 | DMEMIT(" same_cpu_crypt"); | |
0f5d8e6e MP |
2977 | if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) |
2978 | DMEMIT(" submit_from_crypt_cpus"); | |
ef43aa38 MB |
2979 | if (cc->on_disk_tag_size) |
2980 | DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth); | |
8f0009a2 MB |
2981 | if (cc->sector_size != (1 << SECTOR_SHIFT)) |
2982 | DMEMIT(" sector_size:%d", cc->sector_size); | |
2983 | if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) | |
2984 | DMEMIT(" iv_large_sectors"); | |
f3396c58 | 2985 | } |
772ae5f5 | 2986 | |
1da177e4 LT |
2987 | break; |
2988 | } | |
1da177e4 LT |
2989 | } |
2990 | ||
e48d4bbf MB |
2991 | static void crypt_postsuspend(struct dm_target *ti) |
2992 | { | |
2993 | struct crypt_config *cc = ti->private; | |
2994 | ||
2995 | set_bit(DM_CRYPT_SUSPENDED, &cc->flags); | |
2996 | } | |
2997 | ||
2998 | static int crypt_preresume(struct dm_target *ti) | |
2999 | { | |
3000 | struct crypt_config *cc = ti->private; | |
3001 | ||
3002 | if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { | |
3003 | DMERR("aborting resume - crypt key is not set."); | |
3004 | return -EAGAIN; | |
3005 | } | |
3006 | ||
3007 | return 0; | |
3008 | } | |
3009 | ||
3010 | static void crypt_resume(struct dm_target *ti) | |
3011 | { | |
3012 | struct crypt_config *cc = ti->private; | |
3013 | ||
3014 | clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); | |
3015 | } | |
3016 | ||
3017 | /* Message interface | |
3018 | * key set <key> | |
3019 | * key wipe | |
3020 | */ | |
1eb5fa84 MS |
3021 | static int crypt_message(struct dm_target *ti, unsigned argc, char **argv, |
3022 | char *result, unsigned maxlen) | |
e48d4bbf MB |
3023 | { |
3024 | struct crypt_config *cc = ti->private; | |
c538f6ec | 3025 | int key_size, ret = -EINVAL; |
e48d4bbf MB |
3026 | |
3027 | if (argc < 2) | |
3028 | goto error; | |
3029 | ||
498f0103 | 3030 | if (!strcasecmp(argv[0], "key")) { |
e48d4bbf MB |
3031 | if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { |
3032 | DMWARN("not suspended during key manipulation."); | |
3033 | return -EINVAL; | |
3034 | } | |
498f0103 | 3035 | if (argc == 3 && !strcasecmp(argv[1], "set")) { |
c538f6ec OK |
3036 | /* The key size may not be changed. */ |
3037 | key_size = get_key_size(&argv[2]); | |
3038 | if (key_size < 0 || cc->key_size != key_size) { | |
3039 | memset(argv[2], '0', strlen(argv[2])); | |
3040 | return -EINVAL; | |
3041 | } | |
3042 | ||
542da317 MB |
3043 | ret = crypt_set_key(cc, argv[2]); |
3044 | if (ret) | |
3045 | return ret; | |
3046 | if (cc->iv_gen_ops && cc->iv_gen_ops->init) | |
3047 | ret = cc->iv_gen_ops->init(cc); | |
dc94902b OK |
3048 | /* wipe the kernel key payload copy */ |
3049 | if (cc->key_string) | |
3050 | memset(cc->key, 0, cc->key_size * sizeof(u8)); | |
542da317 MB |
3051 | return ret; |
3052 | } | |
498f0103 | 3053 | if (argc == 2 && !strcasecmp(argv[1], "wipe")) { |
542da317 MB |
3054 | if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) { |
3055 | ret = cc->iv_gen_ops->wipe(cc); | |
3056 | if (ret) | |
3057 | return ret; | |
3058 | } | |
e48d4bbf | 3059 | return crypt_wipe_key(cc); |
542da317 | 3060 | } |
e48d4bbf MB |
3061 | } |
3062 | ||
3063 | error: | |
3064 | DMWARN("unrecognised message received."); | |
3065 | return -EINVAL; | |
3066 | } | |
3067 | ||
af4874e0 MS |
3068 | static int crypt_iterate_devices(struct dm_target *ti, |
3069 | iterate_devices_callout_fn fn, void *data) | |
3070 | { | |
3071 | struct crypt_config *cc = ti->private; | |
3072 | ||
5dea271b | 3073 | return fn(ti, cc->dev, cc->start, ti->len, data); |
af4874e0 MS |
3074 | } |
3075 | ||
586b286b MS |
3076 | static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits) |
3077 | { | |
8f0009a2 MB |
3078 | struct crypt_config *cc = ti->private; |
3079 | ||
586b286b MS |
3080 | /* |
3081 | * Unfortunate constraint that is required to avoid the potential | |
3082 | * for exceeding underlying device's max_segments limits -- due to | |
3083 | * crypt_alloc_buffer() possibly allocating pages for the encryption | |
3084 | * bio that are not as physically contiguous as the original bio. | |
3085 | */ | |
3086 | limits->max_segment_size = PAGE_SIZE; | |
8f0009a2 | 3087 | |
bc9e9cf0 MP |
3088 | limits->logical_block_size = |
3089 | max_t(unsigned short, limits->logical_block_size, cc->sector_size); | |
3090 | limits->physical_block_size = | |
3091 | max_t(unsigned, limits->physical_block_size, cc->sector_size); | |
3092 | limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size); | |
586b286b MS |
3093 | } |
3094 | ||
1da177e4 LT |
3095 | static struct target_type crypt_target = { |
3096 | .name = "crypt", | |
dc94902b | 3097 | .version = {1, 18, 1}, |
1da177e4 LT |
3098 | .module = THIS_MODULE, |
3099 | .ctr = crypt_ctr, | |
3100 | .dtr = crypt_dtr, | |
3101 | .map = crypt_map, | |
3102 | .status = crypt_status, | |
e48d4bbf MB |
3103 | .postsuspend = crypt_postsuspend, |
3104 | .preresume = crypt_preresume, | |
3105 | .resume = crypt_resume, | |
3106 | .message = crypt_message, | |
af4874e0 | 3107 | .iterate_devices = crypt_iterate_devices, |
586b286b | 3108 | .io_hints = crypt_io_hints, |
1da177e4 LT |
3109 | }; |
3110 | ||
3111 | static int __init dm_crypt_init(void) | |
3112 | { | |
3113 | int r; | |
3114 | ||
1da177e4 | 3115 | r = dm_register_target(&crypt_target); |
94f5e024 | 3116 | if (r < 0) |
72d94861 | 3117 | DMERR("register failed %d", r); |
1da177e4 | 3118 | |
1da177e4 LT |
3119 | return r; |
3120 | } | |
3121 | ||
3122 | static void __exit dm_crypt_exit(void) | |
3123 | { | |
10d3bd09 | 3124 | dm_unregister_target(&crypt_target); |
1da177e4 LT |
3125 | } |
3126 | ||
3127 | module_init(dm_crypt_init); | |
3128 | module_exit(dm_crypt_exit); | |
3129 | ||
bf14299f | 3130 | MODULE_AUTHOR("Jana Saout <jana@saout.de>"); |
1da177e4 LT |
3131 | MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption"); |
3132 | MODULE_LICENSE("GPL"); |