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