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