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