blob: 784695d22fde1acaaf11acd78c7263438c04648e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Christophe Saout <christophe@saout.de>
3 * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
Milan Broz542da312009-12-10 23:51:57 +00004 * Copyright (C) 2006-2009 Red Hat, Inc. All rights reserved.
Milan Brozed04d982013-10-28 23:21:04 +01005 * Copyright (C) 2013 Milan Broz <gmazyland@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This file is released under the GPL.
8 */
9
Milan Broz43d69032008-02-08 02:11:09 +000010#include <linux/completion.h>
Herbert Xud1806f62006-08-22 20:29:17 +100011#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/bio.h>
16#include <linux/blkdev.h>
17#include <linux/mempool.h>
18#include <linux/slab.h>
19#include <linux/crypto.h>
20#include <linux/workqueue.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070021#include <linux/backing-dev.h>
Andi Kleenc0297722011-01-13 19:59:53 +000022#include <linux/percpu.h>
Arun Sharma600634972011-07-26 16:09:06 -070023#include <linux/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100024#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100026#include <asm/unaligned.h>
Milan Broz34745782011-01-13 19:59:55 +000027#include <crypto/hash.h>
28#include <crypto/md5.h>
29#include <crypto/algapi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Mikulas Patocka586e80e2008-10-21 17:44:59 +010031#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Alasdair G Kergon72d94862006-06-26 00:27:35 -070033#define DM_MSG_PREFIX "crypt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * context holding the current state of a multi-part conversion
37 */
38struct convert_context {
Milan Broz43d69032008-02-08 02:11:09 +000039 struct completion restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 struct bio *bio_in;
41 struct bio *bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -070042 struct bvec_iter iter_in;
43 struct bvec_iter iter_out;
Mikulas Patockac66029f2012-07-27 15:08:05 +010044 sector_t cc_sector;
Mikulas Patocka40b62292012-07-27 15:08:04 +010045 atomic_t cc_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Milan Broz53017032008-02-08 02:10:38 +000048/*
49 * per bio private data
50 */
51struct dm_crypt_io {
Alasdair G Kergon49a8a922012-07-27 15:08:05 +010052 struct crypt_config *cc;
Milan Broz53017032008-02-08 02:10:38 +000053 struct bio *base_bio;
54 struct work_struct work;
55
56 struct convert_context ctx;
57
Mikulas Patocka40b62292012-07-27 15:08:04 +010058 atomic_t io_pending;
Milan Broz53017032008-02-08 02:10:38 +000059 int error;
Milan Broz0c395b02008-02-08 02:10:54 +000060 sector_t sector;
Milan Broz393b47e2008-10-21 17:45:02 +010061 struct dm_crypt_io *base_io;
Milan Broz53017032008-02-08 02:10:38 +000062};
63
Milan Broz01482b72008-02-08 02:11:04 +000064struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000065 struct convert_context *ctx;
Milan Broz01482b72008-02-08 02:11:04 +000066 struct scatterlist sg_in;
67 struct scatterlist sg_out;
Milan Broz2dc53272011-01-13 19:59:54 +000068 sector_t iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000069};
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071struct crypt_config;
72
73struct crypt_iv_operations {
74 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010075 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000077 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000078 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc53272011-01-13 19:59:54 +000079 int (*generator)(struct crypt_config *cc, u8 *iv,
80 struct dm_crypt_request *dmreq);
81 int (*post)(struct crypt_config *cc, u8 *iv,
82 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Milan Broz60473592009-12-10 23:51:55 +000085struct iv_essiv_private {
Milan Brozb95bf2d2009-12-10 23:51:56 +000086 struct crypto_hash *hash_tfm;
87 u8 *salt;
Milan Broz60473592009-12-10 23:51:55 +000088};
89
90struct iv_benbi_private {
91 int shift;
92};
93
Milan Broz34745782011-01-13 19:59:55 +000094#define LMK_SEED_SIZE 64 /* hash + 0 */
95struct iv_lmk_private {
96 struct crypto_shash *hash_tfm;
97 u8 *seed;
98};
99
Milan Brozed04d982013-10-28 23:21:04 +0100100#define TCW_WHITENING_SIZE 16
101struct iv_tcw_private {
102 struct crypto_shash *crc32_tfm;
103 u8 *iv_seed;
104 u8 *whitening;
105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/*
108 * Crypt: maps a linear range of a block device
109 * and encrypts / decrypts at the same time.
110 */
Milan Broze48d4bb2006-10-03 01:15:37 -0700111enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
Andi Kleenc0297722011-01-13 19:59:53 +0000112
113/*
114 * Duplicated per-CPU state for cipher.
115 */
116struct crypt_cpu {
117 struct ablkcipher_request *req;
Andi Kleenc0297722011-01-13 19:59:53 +0000118};
119
120/*
121 * The fields in here must be read only after initialization,
122 * changing state should be in crypt_cpu.
123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124struct crypt_config {
125 struct dm_dev *dev;
126 sector_t start;
127
128 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000129 * pool for per bio private data, crypto requests and
130 * encryption requeusts/buffer pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 */
132 mempool_t *io_pool;
Milan Brozddd42ed2008-02-08 02:11:07 +0000133 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -0700135 struct bio_set *bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Milan Brozcabf08e2007-10-19 22:38:58 +0100137 struct workqueue_struct *io_queue;
138 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700139
Milan Broz5ebaee62010-08-12 04:14:07 +0100140 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000141 char *cipher_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800144 union {
Milan Broz60473592009-12-10 23:51:55 +0000145 struct iv_essiv_private essiv;
146 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000147 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100148 struct iv_tcw_private tcw;
Herbert Xu79066ad2006-12-05 13:41:52 -0800149 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 sector_t iv_offset;
151 unsigned int iv_size;
152
Milan Brozddd42ed2008-02-08 02:11:07 +0000153 /*
Andi Kleenc0297722011-01-13 19:59:53 +0000154 * Duplicated per cpu state. Access through
155 * per_cpu_ptr() only.
156 */
157 struct crypt_cpu __percpu *cpu;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100158
159 /* ESSIV: struct crypto_cipher *essiv_tfm */
160 void *iv_private;
161 struct crypto_ablkcipher **tfms;
Milan Brozd1f96422011-01-13 19:59:54 +0000162 unsigned tfms_count;
Andi Kleenc0297722011-01-13 19:59:53 +0000163
164 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000165 * Layout of each crypto request:
166 *
167 * struct ablkcipher_request
168 * context
169 * padding
170 * struct dm_crypt_request
171 * padding
172 * IV
173 *
174 * The padding is added so that dm_crypt_request and the IV are
175 * correctly aligned.
176 */
177 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000178
Milan Broze48d4bb2006-10-03 01:15:37 -0700179 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100181 unsigned int key_parts; /* independent parts in key buffer */
182 unsigned int key_extra_size; /* additional keys length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 u8 key[0];
184};
185
Milan Broz6a24c712006-10-03 01:15:40 -0700186#define MIN_IOS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#define MIN_POOL_PAGES 32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Christoph Lametere18b8902006-12-06 20:33:20 -0800189static struct kmem_cache *_crypt_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100191static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000192static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Broz2dc53272011-01-13 19:59:54 +0000193static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
Olaf Kirch027581f2007-05-09 02:32:52 -0700194
Andi Kleenc0297722011-01-13 19:59:53 +0000195static struct crypt_cpu *this_crypt_config(struct crypt_config *cc)
196{
197 return this_cpu_ptr(cc->cpu);
198}
199
200/*
201 * Use this to access cipher attributes that are the same for each CPU.
202 */
203static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc)
204{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100205 return cc->tfms[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 * Different IV generation algorithms:
210 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000211 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200212 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 *
Milan Broz61afef62009-12-10 23:52:25 +0000214 * plain64: the initial vector is the 64-bit little-endian version of the sector
215 * number, padded with zeros if necessary.
216 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000217 * essiv: "encrypted sector|salt initial vector", the sector number is
218 * encrypted with the bulk cipher using a salt as key. The salt
219 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 *
Rik Snel48527fa2006-09-03 08:56:39 +1000221 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
222 * (needed for LRW-32-AES and possible other narrow block modes)
223 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700224 * null: the initial vector is always zero. Provides compatibility with
225 * obsolete loop_fish2 devices. Do not use for new devices.
226 *
Milan Broz34745782011-01-13 19:59:55 +0000227 * lmk: Compatible implementation of the block chaining mode used
228 * by the Loop-AES block device encryption system
229 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
230 * It operates on full 512 byte sectors and uses CBC
231 * with an IV derived from the sector number, the data and
232 * optionally extra IV seed.
233 * This means that after decryption the first block
234 * of sector must be tweaked according to decrypted data.
235 * Loop-AES can use three encryption schemes:
236 * version 1: is plain aes-cbc mode
237 * version 2: uses 64 multikey scheme with lmk IV generator
238 * version 3: the same as version 2 with additional IV seed
239 * (it uses 65 keys, last key is used as IV seed)
240 *
Milan Brozed04d982013-10-28 23:21:04 +0100241 * tcw: Compatible implementation of the block chaining mode used
242 * by the TrueCrypt device encryption system (prior to version 4.1).
243 * For more info see: http://www.truecrypt.org
244 * It operates on full 512 byte sectors and uses CBC
245 * with an IV derived from initial key and the sector number.
246 * In addition, whitening value is applied on every sector, whitening
247 * is calculated from initial key, sector number and mixed using CRC32.
248 * Note that this encryption scheme is vulnerable to watermarking attacks
249 * and should be used for old compatible containers access only.
250 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * plumb: unimplemented, see:
252 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
253 */
254
Milan Broz2dc53272011-01-13 19:59:54 +0000255static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
256 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100259 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 return 0;
262}
263
Milan Broz61afef62009-12-10 23:52:25 +0000264static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000265 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000266{
267 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100268 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000269
270 return 0;
271}
272
Milan Brozb95bf2d2009-12-10 23:51:56 +0000273/* Initialise ESSIV - compute salt but no local memory allocations */
274static int crypt_iv_essiv_init(struct crypt_config *cc)
275{
276 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
277 struct hash_desc desc;
278 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000279 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100280 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000281
282 sg_init_one(&sg, cc->key, cc->key_size);
283 desc.tfm = essiv->hash_tfm;
284 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
285
286 err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt);
287 if (err)
288 return err;
289
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100290 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000291
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100292 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
293 crypto_hash_digestsize(essiv->hash_tfm));
294 if (err)
295 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000296
297 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000298}
299
Milan Broz542da312009-12-10 23:51:57 +0000300/* Wipe salt and reset key derived from volume key */
301static int crypt_iv_essiv_wipe(struct crypt_config *cc)
302{
303 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
304 unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000305 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100306 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000307
308 memset(essiv->salt, 0, salt_size);
309
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100310 essiv_tfm = cc->iv_private;
311 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
312 if (r)
313 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000314
315 return err;
316}
317
318/* Set up per cpu cipher state */
319static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
320 struct dm_target *ti,
321 u8 *salt, unsigned saltsize)
322{
323 struct crypto_cipher *essiv_tfm;
324 int err;
325
326 /* Setup the essiv_tfm with the given salt */
327 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
328 if (IS_ERR(essiv_tfm)) {
329 ti->error = "Error allocating crypto tfm for ESSIV";
330 return essiv_tfm;
331 }
332
333 if (crypto_cipher_blocksize(essiv_tfm) !=
334 crypto_ablkcipher_ivsize(any_tfm(cc))) {
335 ti->error = "Block size of ESSIV cipher does "
336 "not match IV size of block cipher";
337 crypto_free_cipher(essiv_tfm);
338 return ERR_PTR(-EINVAL);
339 }
340
341 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
342 if (err) {
343 ti->error = "Failed to set key for ESSIV cipher";
344 crypto_free_cipher(essiv_tfm);
345 return ERR_PTR(err);
346 }
347
348 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000349}
350
Milan Broz60473592009-12-10 23:51:55 +0000351static void crypt_iv_essiv_dtr(struct crypt_config *cc)
352{
Andi Kleenc0297722011-01-13 19:59:53 +0000353 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000354 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
355
Milan Brozb95bf2d2009-12-10 23:51:56 +0000356 crypto_free_hash(essiv->hash_tfm);
357 essiv->hash_tfm = NULL;
358
359 kzfree(essiv->salt);
360 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000361
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100362 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000363
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100364 if (essiv_tfm)
365 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000366
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100367 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000368}
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100371 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Milan Broz5861f1b2009-12-10 23:51:56 +0000373 struct crypto_cipher *essiv_tfm = NULL;
374 struct crypto_hash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000375 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100376 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Milan Broz5861f1b2009-12-10 23:51:56 +0000378 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700379 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 return -EINVAL;
381 }
382
Milan Brozb95bf2d2009-12-10 23:51:56 +0000383 /* Allocate hash algorithm */
Herbert Xu35058682006-08-24 19:10:20 +1000384 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
385 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700386 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000387 err = PTR_ERR(hash_tfm);
388 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
Milan Brozb95bf2d2009-12-10 23:51:56 +0000391 salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000392 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700393 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000394 err = -ENOMEM;
395 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
Milan Brozb95bf2d2009-12-10 23:51:56 +0000398 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000399 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
400
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100401 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
402 crypto_hash_digestsize(hash_tfm));
403 if (IS_ERR(essiv_tfm)) {
404 crypt_iv_essiv_dtr(cc);
405 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000406 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100407 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000410
411bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000412 if (hash_tfm && !IS_ERR(hash_tfm))
413 crypto_free_hash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000414 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000415 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Milan Broz2dc53272011-01-13 19:59:54 +0000418static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
419 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100421 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100424 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000425 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return 0;
428}
429
Rik Snel48527fa2006-09-03 08:56:39 +1000430static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
431 const char *opts)
432{
Andi Kleenc0297722011-01-13 19:59:53 +0000433 unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800434 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000435
436 /* we need to calculate how far we must shift the sector count
437 * to get the cipher block count, we use this shift in _gen */
438
439 if (1 << log != bs) {
440 ti->error = "cypher blocksize is not a power of 2";
441 return -EINVAL;
442 }
443
444 if (log > 9) {
445 ti->error = "cypher blocksize is > 512";
446 return -EINVAL;
447 }
448
Milan Broz60473592009-12-10 23:51:55 +0000449 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000450
451 return 0;
452}
453
454static void crypt_iv_benbi_dtr(struct crypt_config *cc)
455{
Rik Snel48527fa2006-09-03 08:56:39 +1000456}
457
Milan Broz2dc53272011-01-13 19:59:54 +0000458static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
459 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000460{
Herbert Xu79066ad2006-12-05 13:41:52 -0800461 __be64 val;
462
Rik Snel48527fa2006-09-03 08:56:39 +1000463 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800464
Milan Broz2dc53272011-01-13 19:59:54 +0000465 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800466 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return 0;
469}
470
Milan Broz2dc53272011-01-13 19:59:54 +0000471static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
472 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700473{
474 memset(iv, 0, cc->iv_size);
475
476 return 0;
477}
478
Milan Broz34745782011-01-13 19:59:55 +0000479static void crypt_iv_lmk_dtr(struct crypt_config *cc)
480{
481 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
482
483 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
484 crypto_free_shash(lmk->hash_tfm);
485 lmk->hash_tfm = NULL;
486
487 kzfree(lmk->seed);
488 lmk->seed = NULL;
489}
490
491static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
492 const char *opts)
493{
494 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
495
496 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
497 if (IS_ERR(lmk->hash_tfm)) {
498 ti->error = "Error initializing LMK hash";
499 return PTR_ERR(lmk->hash_tfm);
500 }
501
502 /* No seed in LMK version 2 */
503 if (cc->key_parts == cc->tfms_count) {
504 lmk->seed = NULL;
505 return 0;
506 }
507
508 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
509 if (!lmk->seed) {
510 crypt_iv_lmk_dtr(cc);
511 ti->error = "Error kmallocing seed storage in LMK";
512 return -ENOMEM;
513 }
514
515 return 0;
516}
517
518static int crypt_iv_lmk_init(struct crypt_config *cc)
519{
520 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
521 int subkey_size = cc->key_size / cc->key_parts;
522
523 /* LMK seed is on the position of LMK_KEYS + 1 key */
524 if (lmk->seed)
525 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
526 crypto_shash_digestsize(lmk->hash_tfm));
527
528 return 0;
529}
530
531static int crypt_iv_lmk_wipe(struct crypt_config *cc)
532{
533 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
534
535 if (lmk->seed)
536 memset(lmk->seed, 0, LMK_SEED_SIZE);
537
538 return 0;
539}
540
541static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
542 struct dm_crypt_request *dmreq,
543 u8 *data)
544{
545 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
546 struct {
547 struct shash_desc desc;
548 char ctx[crypto_shash_descsize(lmk->hash_tfm)];
549 } sdesc;
550 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100551 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000552 int i, r;
553
554 sdesc.desc.tfm = lmk->hash_tfm;
555 sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
556
557 r = crypto_shash_init(&sdesc.desc);
558 if (r)
559 return r;
560
561 if (lmk->seed) {
562 r = crypto_shash_update(&sdesc.desc, lmk->seed, LMK_SEED_SIZE);
563 if (r)
564 return r;
565 }
566
567 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
568 r = crypto_shash_update(&sdesc.desc, data + 16, 16 * 31);
569 if (r)
570 return r;
571
572 /* Sector is cropped to 56 bits here */
573 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
574 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
575 buf[2] = cpu_to_le32(4024);
576 buf[3] = 0;
577 r = crypto_shash_update(&sdesc.desc, (u8 *)buf, sizeof(buf));
578 if (r)
579 return r;
580
581 /* No MD5 padding here */
582 r = crypto_shash_export(&sdesc.desc, &md5state);
583 if (r)
584 return r;
585
586 for (i = 0; i < MD5_HASH_WORDS; i++)
587 __cpu_to_le32s(&md5state.hash[i]);
588 memcpy(iv, &md5state.hash, cc->iv_size);
589
590 return 0;
591}
592
593static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
594 struct dm_crypt_request *dmreq)
595{
596 u8 *src;
597 int r = 0;
598
599 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Cong Wangc2e022c2011-11-28 13:26:02 +0800600 src = kmap_atomic(sg_page(&dmreq->sg_in));
Milan Broz34745782011-01-13 19:59:55 +0000601 r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800602 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000603 } else
604 memset(iv, 0, cc->iv_size);
605
606 return r;
607}
608
609static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
610 struct dm_crypt_request *dmreq)
611{
612 u8 *dst;
613 int r;
614
615 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
616 return 0;
617
Cong Wangc2e022c2011-11-28 13:26:02 +0800618 dst = kmap_atomic(sg_page(&dmreq->sg_out));
Milan Broz34745782011-01-13 19:59:55 +0000619 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
620
621 /* Tweak the first block of plaintext sector */
622 if (!r)
623 crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
624
Cong Wangc2e022c2011-11-28 13:26:02 +0800625 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000626 return r;
627}
628
Milan Brozed04d982013-10-28 23:21:04 +0100629static void crypt_iv_tcw_dtr(struct crypt_config *cc)
630{
631 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
632
633 kzfree(tcw->iv_seed);
634 tcw->iv_seed = NULL;
635 kzfree(tcw->whitening);
636 tcw->whitening = NULL;
637
638 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
639 crypto_free_shash(tcw->crc32_tfm);
640 tcw->crc32_tfm = NULL;
641}
642
643static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
644 const char *opts)
645{
646 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
647
648 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
649 ti->error = "Wrong key size for TCW";
650 return -EINVAL;
651 }
652
653 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
654 if (IS_ERR(tcw->crc32_tfm)) {
655 ti->error = "Error initializing CRC32 in TCW";
656 return PTR_ERR(tcw->crc32_tfm);
657 }
658
659 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
660 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
661 if (!tcw->iv_seed || !tcw->whitening) {
662 crypt_iv_tcw_dtr(cc);
663 ti->error = "Error allocating seed storage in TCW";
664 return -ENOMEM;
665 }
666
667 return 0;
668}
669
670static int crypt_iv_tcw_init(struct crypt_config *cc)
671{
672 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
673 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
674
675 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
676 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
677 TCW_WHITENING_SIZE);
678
679 return 0;
680}
681
682static int crypt_iv_tcw_wipe(struct crypt_config *cc)
683{
684 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
685
686 memset(tcw->iv_seed, 0, cc->iv_size);
687 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
688
689 return 0;
690}
691
692static int crypt_iv_tcw_whitening(struct crypt_config *cc,
693 struct dm_crypt_request *dmreq,
694 u8 *data)
695{
696 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
697 u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
698 u8 buf[TCW_WHITENING_SIZE];
699 struct {
700 struct shash_desc desc;
701 char ctx[crypto_shash_descsize(tcw->crc32_tfm)];
702 } sdesc;
703 int i, r;
704
705 /* xor whitening with sector number */
706 memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
707 crypto_xor(buf, (u8 *)&sector, 8);
708 crypto_xor(&buf[8], (u8 *)&sector, 8);
709
710 /* calculate crc32 for every 32bit part and xor it */
711 sdesc.desc.tfm = tcw->crc32_tfm;
712 sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
713 for (i = 0; i < 4; i++) {
714 r = crypto_shash_init(&sdesc.desc);
715 if (r)
716 goto out;
717 r = crypto_shash_update(&sdesc.desc, &buf[i * 4], 4);
718 if (r)
719 goto out;
720 r = crypto_shash_final(&sdesc.desc, &buf[i * 4]);
721 if (r)
722 goto out;
723 }
724 crypto_xor(&buf[0], &buf[12], 4);
725 crypto_xor(&buf[4], &buf[8], 4);
726
727 /* apply whitening (8 bytes) to whole sector */
728 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
729 crypto_xor(data + i * 8, buf, 8);
730out:
731 memset(buf, 0, sizeof(buf));
732 return r;
733}
734
735static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
736 struct dm_crypt_request *dmreq)
737{
738 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
739 u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
740 u8 *src;
741 int r = 0;
742
743 /* Remove whitening from ciphertext */
744 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
745 src = kmap_atomic(sg_page(&dmreq->sg_in));
746 r = crypt_iv_tcw_whitening(cc, dmreq, src + dmreq->sg_in.offset);
747 kunmap_atomic(src);
748 }
749
750 /* Calculate IV */
751 memcpy(iv, tcw->iv_seed, cc->iv_size);
752 crypto_xor(iv, (u8 *)&sector, 8);
753 if (cc->iv_size > 8)
754 crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
755
756 return r;
757}
758
759static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
760 struct dm_crypt_request *dmreq)
761{
762 u8 *dst;
763 int r;
764
765 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
766 return 0;
767
768 /* Apply whitening on ciphertext */
769 dst = kmap_atomic(sg_page(&dmreq->sg_out));
770 r = crypt_iv_tcw_whitening(cc, dmreq, dst + dmreq->sg_out.offset);
771 kunmap_atomic(dst);
772
773 return r;
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776static struct crypt_iv_operations crypt_iv_plain_ops = {
777 .generator = crypt_iv_plain_gen
778};
779
Milan Broz61afef62009-12-10 23:52:25 +0000780static struct crypt_iv_operations crypt_iv_plain64_ops = {
781 .generator = crypt_iv_plain64_gen
782};
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784static struct crypt_iv_operations crypt_iv_essiv_ops = {
785 .ctr = crypt_iv_essiv_ctr,
786 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000787 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000788 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 .generator = crypt_iv_essiv_gen
790};
791
Rik Snel48527fa2006-09-03 08:56:39 +1000792static struct crypt_iv_operations crypt_iv_benbi_ops = {
793 .ctr = crypt_iv_benbi_ctr,
794 .dtr = crypt_iv_benbi_dtr,
795 .generator = crypt_iv_benbi_gen
796};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Ludwig Nussel46b47732007-05-09 02:32:55 -0700798static struct crypt_iv_operations crypt_iv_null_ops = {
799 .generator = crypt_iv_null_gen
800};
801
Milan Broz34745782011-01-13 19:59:55 +0000802static struct crypt_iv_operations crypt_iv_lmk_ops = {
803 .ctr = crypt_iv_lmk_ctr,
804 .dtr = crypt_iv_lmk_dtr,
805 .init = crypt_iv_lmk_init,
806 .wipe = crypt_iv_lmk_wipe,
807 .generator = crypt_iv_lmk_gen,
808 .post = crypt_iv_lmk_post
809};
810
Milan Brozed04d982013-10-28 23:21:04 +0100811static struct crypt_iv_operations crypt_iv_tcw_ops = {
812 .ctr = crypt_iv_tcw_ctr,
813 .dtr = crypt_iv_tcw_dtr,
814 .init = crypt_iv_tcw_init,
815 .wipe = crypt_iv_tcw_wipe,
816 .generator = crypt_iv_tcw_gen,
817 .post = crypt_iv_tcw_post
818};
819
Milan Brozd469f842007-10-19 22:42:37 +0100820static void crypt_convert_init(struct crypt_config *cc,
821 struct convert_context *ctx,
822 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000823 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 ctx->bio_in = bio_in;
826 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700827 if (bio_in)
828 ctx->iter_in = bio_in->bi_iter;
829 if (bio_out)
830 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100831 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000832 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Huang Yingb2174ee2009-03-16 17:44:33 +0000835static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
836 struct ablkcipher_request *req)
837{
838 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
839}
840
841static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
842 struct dm_crypt_request *dmreq)
843{
844 return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
845}
846
Milan Broz2dc53272011-01-13 19:59:54 +0000847static u8 *iv_of_dmreq(struct crypt_config *cc,
848 struct dm_crypt_request *dmreq)
849{
850 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
851 crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
852}
853
Milan Broz01482b72008-02-08 02:11:04 +0000854static int crypt_convert_block(struct crypt_config *cc,
Milan Broz3a7f6c92008-02-08 02:11:14 +0000855 struct convert_context *ctx,
856 struct ablkcipher_request *req)
Milan Broz01482b72008-02-08 02:11:04 +0000857{
Kent Overstreet003b5c52013-10-11 15:45:43 -0700858 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
859 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000860 struct dm_crypt_request *dmreq;
861 u8 *iv;
Mikulas Patocka40b62292012-07-27 15:08:04 +0100862 int r;
Milan Broz01482b72008-02-08 02:11:04 +0000863
Huang Yingb2174ee2009-03-16 17:44:33 +0000864 dmreq = dmreq_of_req(cc, req);
Milan Broz2dc53272011-01-13 19:59:54 +0000865 iv = iv_of_dmreq(cc, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000866
Mikulas Patockac66029f2012-07-27 15:08:05 +0100867 dmreq->iv_sector = ctx->cc_sector;
Huang Yingb2174ee2009-03-16 17:44:33 +0000868 dmreq->ctx = ctx;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000869 sg_init_table(&dmreq->sg_in, 1);
Kent Overstreet003b5c52013-10-11 15:45:43 -0700870 sg_set_page(&dmreq->sg_in, bv_in.bv_page, 1 << SECTOR_SHIFT,
871 bv_in.bv_offset);
Milan Broz01482b72008-02-08 02:11:04 +0000872
Milan Broz3a7f6c92008-02-08 02:11:14 +0000873 sg_init_table(&dmreq->sg_out, 1);
Kent Overstreet003b5c52013-10-11 15:45:43 -0700874 sg_set_page(&dmreq->sg_out, bv_out.bv_page, 1 << SECTOR_SHIFT,
875 bv_out.bv_offset);
Milan Broz01482b72008-02-08 02:11:04 +0000876
Kent Overstreet003b5c52013-10-11 15:45:43 -0700877 bio_advance_iter(ctx->bio_in, &ctx->iter_in, 1 << SECTOR_SHIFT);
878 bio_advance_iter(ctx->bio_out, &ctx->iter_out, 1 << SECTOR_SHIFT);
Milan Broz01482b72008-02-08 02:11:04 +0000879
Milan Broz3a7f6c92008-02-08 02:11:14 +0000880 if (cc->iv_gen_ops) {
Milan Broz2dc53272011-01-13 19:59:54 +0000881 r = cc->iv_gen_ops->generator(cc, iv, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000882 if (r < 0)
883 return r;
884 }
885
886 ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
887 1 << SECTOR_SHIFT, iv);
888
889 if (bio_data_dir(ctx->bio_in) == WRITE)
890 r = crypto_ablkcipher_encrypt(req);
891 else
892 r = crypto_ablkcipher_decrypt(req);
893
Milan Broz2dc53272011-01-13 19:59:54 +0000894 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
895 r = cc->iv_gen_ops->post(cc, iv, dmreq);
896
Milan Broz3a7f6c92008-02-08 02:11:14 +0000897 return r;
Milan Broz01482b72008-02-08 02:11:04 +0000898}
899
Milan Broz95497a92008-02-08 02:11:12 +0000900static void kcryptd_async_done(struct crypto_async_request *async_req,
901 int error);
Andi Kleenc0297722011-01-13 19:59:53 +0000902
Milan Brozddd42ed2008-02-08 02:11:07 +0000903static void crypt_alloc_req(struct crypt_config *cc,
904 struct convert_context *ctx)
905{
Andi Kleenc0297722011-01-13 19:59:53 +0000906 struct crypt_cpu *this_cc = this_crypt_config(cc);
Mikulas Patockac66029f2012-07-27 15:08:05 +0100907 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +0000908
909 if (!this_cc->req)
910 this_cc->req = mempool_alloc(cc->req_pool, GFP_NOIO);
911
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100912 ablkcipher_request_set_tfm(this_cc->req, cc->tfms[key_index]);
Andi Kleenc0297722011-01-13 19:59:53 +0000913 ablkcipher_request_set_callback(this_cc->req,
914 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
915 kcryptd_async_done, dmreq_of_req(cc, this_cc->req));
Milan Brozddd42ed2008-02-08 02:11:07 +0000916}
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918/*
919 * Encrypt / decrypt data from one bio to another one (can be the same one)
920 */
921static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +0100922 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
Andi Kleenc0297722011-01-13 19:59:53 +0000924 struct crypt_cpu *this_cc = this_crypt_config(cc);
Milan Broz3f1e9072008-03-28 14:16:07 -0700925 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Mikulas Patocka40b62292012-07-27 15:08:04 +0100927 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +0100928
Kent Overstreet003b5c52013-10-11 15:45:43 -0700929 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Milan Broz3a7f6c92008-02-08 02:11:14 +0000931 crypt_alloc_req(cc, ctx);
932
Mikulas Patocka40b62292012-07-27 15:08:04 +0100933 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700934
Andi Kleenc0297722011-01-13 19:59:53 +0000935 r = crypt_convert_block(cc, ctx, this_cc->req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000936
937 switch (r) {
Milan Broz3f1e9072008-03-28 14:16:07 -0700938 /* async */
Milan Broz3a7f6c92008-02-08 02:11:14 +0000939 case -EBUSY:
940 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -0800941 reinit_completion(&ctx->restart);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000942 /* fall through*/
943 case -EINPROGRESS:
Andi Kleenc0297722011-01-13 19:59:53 +0000944 this_cc->req = NULL;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100945 ctx->cc_sector++;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000946 continue;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000947
Milan Broz3f1e9072008-03-28 14:16:07 -0700948 /* sync */
949 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +0100950 atomic_dec(&ctx->cc_pending);
Mikulas Patockac66029f2012-07-27 15:08:05 +0100951 ctx->cc_sector++;
Milan Brozc7f1b202008-07-02 09:34:28 +0100952 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -0700953 continue;
954
955 /* error */
956 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +0100957 atomic_dec(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700958 return r;
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961
Milan Broz3f1e9072008-03-28 14:16:07 -0700962 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965/*
966 * Generate a new unfragmented bio with the given size
967 * This should never violate the device limitations
Milan Broz933f01d2008-10-10 13:37:08 +0100968 * May return a smaller bio when running out of pages, indicated by
969 * *out_of_pages set to 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 */
Milan Broz933f01d2008-10-10 13:37:08 +0100971static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
972 unsigned *out_of_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +0100974 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -0700975 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Virob4e3ca12005-10-21 03:22:34 -0400977 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
Milan Broz91e10622007-12-13 14:16:10 +0000978 unsigned i, len;
979 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Olaf Kirch2f9941b2007-05-09 02:32:53 -0700981 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -0700982 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Olaf Kirch027581f2007-05-09 02:32:52 -0700985 clone_init(io, clone);
Milan Broz933f01d2008-10-10 13:37:08 +0100986 *out_of_pages = 0;
Milan Broz6a24c712006-10-03 01:15:40 -0700987
Olaf Kirchf97380b2007-05-09 02:32:54 -0700988 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +0000989 page = mempool_alloc(cc->page_pool, gfp_mask);
Milan Broz933f01d2008-10-10 13:37:08 +0100990 if (!page) {
991 *out_of_pages = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 break;
Milan Broz933f01d2008-10-10 13:37:08 +0100993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /*
Mikulas Patockaaeb2dea2012-03-28 18:41:22 +0100996 * If additional pages cannot be allocated without waiting,
997 * return a partially-allocated bio. The caller will then try
998 * to allocate more bios while submitting this partial bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
Mikulas Patockaaeb2dea2012-03-28 18:41:22 +01001000 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Milan Broz91e10622007-12-13 14:16:10 +00001002 len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Milan Broz91e10622007-12-13 14:16:10 +00001004 if (!bio_add_page(clone, page, len, 0)) {
1005 mempool_free(page, cc->page_pool);
1006 break;
1007 }
1008
1009 size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011
Kent Overstreet4f024f32013-10-11 15:44:27 -07001012 if (!clone->bi_iter.bi_size) {
Milan Broz8b004452006-10-03 01:15:37 -07001013 bio_put(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return NULL;
1015 }
1016
Milan Broz8b004452006-10-03 01:15:37 -07001017 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
Neil Brown644bd2f2007-10-16 13:48:46 +02001020static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Neil Brown644bd2f2007-10-16 13:48:46 +02001022 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 struct bio_vec *bv;
1024
Kent Overstreetcb34e052012-09-05 15:22:02 -07001025 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 BUG_ON(!bv->bv_page);
1027 mempool_free(bv->bv_page, cc->page_pool);
1028 bv->bv_page = NULL;
1029 }
1030}
1031
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001032static struct dm_crypt_io *crypt_io_alloc(struct crypt_config *cc,
Milan Brozdc440d1e2008-10-10 13:37:03 +01001033 struct bio *bio, sector_t sector)
1034{
Milan Brozdc440d1e2008-10-10 13:37:03 +01001035 struct dm_crypt_io *io;
1036
1037 io = mempool_alloc(cc->io_pool, GFP_NOIO);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001038 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001039 io->base_bio = bio;
1040 io->sector = sector;
1041 io->error = 0;
Milan Broz393b47e2008-10-21 17:45:02 +01001042 io->base_io = NULL;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001043 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001044
1045 return io;
1046}
1047
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001048static void crypt_inc_pending(struct dm_crypt_io *io)
1049{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001050 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001051}
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/*
1054 * One of the bios was finished. Check for completion of
1055 * the whole request and correctly clean up the buffer.
Milan Broz393b47e2008-10-21 17:45:02 +01001056 * If base_io is set, wait for the last fragment to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
Milan Broz5742fd72008-02-08 02:10:43 +00001058static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001060 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001061 struct bio *base_bio = io->base_bio;
1062 struct dm_crypt_io *base_io = io->base_io;
1063 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Mikulas Patocka40b62292012-07-27 15:08:04 +01001065 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 mempool_free(io, cc->io_pool);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001069
1070 if (likely(!base_io))
1071 bio_endio(base_bio, error);
1072 else {
1073 if (error && !base_io->error)
1074 base_io->error = error;
1075 crypt_dec_pending(base_io);
1076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
1079/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001080 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 *
1082 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001083 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001084 *
1085 * kcryptd performs the actual encryption or decryption.
1086 *
1087 * kcryptd_io performs the IO submission.
1088 *
1089 * They must be separated as otherwise the final stages could be
1090 * starved by new requests which can block in the first stages due
1091 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001092 *
1093 * The work is done per CPU global for all dm-crypt instances.
1094 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 */
NeilBrown6712ecf2007-09-27 12:47:43 +02001096static void crypt_endio(struct bio *clone, int error)
Milan Broz8b004452006-10-03 01:15:37 -07001097{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001098 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001099 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001100 unsigned rw = bio_data_dir(clone);
Milan Broz8b004452006-10-03 01:15:37 -07001101
Milan Brozadfe4772007-12-13 14:15:51 +00001102 if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
1103 error = -EIO;
1104
Milan Broz8b004452006-10-03 01:15:37 -07001105 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001106 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001107 */
Milan Brozee7a4912008-02-08 02:10:46 +00001108 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001109 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001110
1111 bio_put(clone);
1112
1113 if (rw == READ && !error) {
1114 kcryptd_queue_crypt(io);
1115 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001116 }
Milan Broz8b004452006-10-03 01:15:37 -07001117
Milan Brozadfe4772007-12-13 14:15:51 +00001118 if (unlikely(error))
Milan Broz5742fd72008-02-08 02:10:43 +00001119 io->error = error;
1120
1121 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001122}
1123
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001124static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001125{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001126 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001127
1128 clone->bi_private = io;
1129 clone->bi_end_io = crypt_endio;
1130 clone->bi_bdev = cc->dev->bdev;
1131 clone->bi_rw = io->base_bio->bi_rw;
1132}
1133
Milan Broz20c82532011-01-13 19:59:53 +00001134static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001135{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001136 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001137 struct bio *base_bio = io->base_bio;
1138 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001139
Milan Broz8b004452006-10-03 01:15:37 -07001140 /*
1141 * The block layer might modify the bvec array, so always
1142 * copy the required bvecs because we need the original
1143 * one in order to decrypt the whole bio data *afterwards*.
1144 */
Kent Overstreetbf800ef2012-09-06 15:35:02 -07001145 clone = bio_clone_bioset(base_bio, gfp, cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001146 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001147 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001148
Milan Broz20c82532011-01-13 19:59:53 +00001149 crypt_inc_pending(io);
1150
Milan Broz8b004452006-10-03 01:15:37 -07001151 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001152 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001153
Milan Broz93e605c2006-10-03 01:15:38 -07001154 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001155 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001156}
1157
Milan Broz4e4eef62008-02-08 02:10:49 +00001158static void kcryptd_io_write(struct dm_crypt_io *io)
1159{
Milan Broz95497a92008-02-08 02:11:12 +00001160 struct bio *clone = io->ctx.bio_out;
Milan Broz95497a92008-02-08 02:11:12 +00001161 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +00001162}
1163
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001164static void kcryptd_io(struct work_struct *work)
1165{
1166 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1167
Milan Broz20c82532011-01-13 19:59:53 +00001168 if (bio_data_dir(io->base_bio) == READ) {
1169 crypt_inc_pending(io);
1170 if (kcryptd_io_read(io, GFP_NOIO))
1171 io->error = -ENOMEM;
1172 crypt_dec_pending(io);
1173 } else
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001174 kcryptd_io_write(io);
1175}
1176
1177static void kcryptd_queue_io(struct dm_crypt_io *io)
1178{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001179 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001180
1181 INIT_WORK(&io->work, kcryptd_io);
1182 queue_work(cc->io_queue, &io->work);
1183}
1184
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001185static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001186{
Milan Brozdec1ced2008-02-08 02:10:57 +00001187 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001188 struct crypt_config *cc = io->cc;
Milan Brozdec1ced2008-02-08 02:10:57 +00001189
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001190 if (unlikely(io->error < 0)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001191 crypt_free_buffer_pages(cc, clone);
1192 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001193 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001194 return;
1195 }
1196
1197 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001198 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001199
Kent Overstreet4f024f32013-10-11 15:44:27 -07001200 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001201
Milan Broz95497a92008-02-08 02:11:12 +00001202 if (async)
1203 kcryptd_queue_io(io);
Alasdair G Kergon1e37bb82008-10-10 13:37:05 +01001204 else
Milan Broz95497a92008-02-08 02:11:12 +00001205 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +00001206}
1207
Milan Brozfc5a5e92008-10-10 13:37:04 +01001208static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001209{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001210 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001211 struct bio *clone;
Milan Broz393b47e2008-10-21 17:45:02 +01001212 struct dm_crypt_io *new_io;
Milan Brozc8081612008-10-10 13:37:08 +01001213 int crypt_finished;
Milan Broz933f01d2008-10-10 13:37:08 +01001214 unsigned out_of_pages = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001215 unsigned remaining = io->base_bio->bi_iter.bi_size;
Milan Brozb635b002008-10-21 17:45:00 +01001216 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +00001217 int r;
Milan Broz8b004452006-10-03 01:15:37 -07001218
Milan Broz93e605c2006-10-03 01:15:38 -07001219 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001220 * Prevent io from disappearing until this function completes.
1221 */
1222 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001223 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001224
1225 /*
Milan Broz93e605c2006-10-03 01:15:38 -07001226 * The allocated buffers can be smaller than the whole bio,
1227 * so repeat the whole process until all the data can be handled.
1228 */
1229 while (remaining) {
Milan Broz933f01d2008-10-10 13:37:08 +01001230 clone = crypt_alloc_buffer(io, remaining, &out_of_pages);
Milan Broz23541d22006-10-03 01:15:39 -07001231 if (unlikely(!clone)) {
Milan Broz5742fd72008-02-08 02:10:43 +00001232 io->error = -ENOMEM;
Milan Brozfc5a5e92008-10-10 13:37:04 +01001233 break;
Milan Broz23541d22006-10-03 01:15:39 -07001234 }
Milan Broz93e605c2006-10-03 01:15:38 -07001235
Milan Broz53017032008-02-08 02:10:38 +00001236 io->ctx.bio_out = clone;
Kent Overstreet003b5c52013-10-11 15:45:43 -07001237 io->ctx.iter_out = clone->bi_iter;
Milan Broz93e605c2006-10-03 01:15:38 -07001238
Kent Overstreet4f024f32013-10-11 15:44:27 -07001239 remaining -= clone->bi_iter.bi_size;
Milan Brozb635b002008-10-21 17:45:00 +01001240 sector += bio_sectors(clone);
Milan Brozdec1ced2008-02-08 02:10:57 +00001241
Milan Broz4e594092008-10-10 13:37:07 +01001242 crypt_inc_pending(io);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001243
Milan Brozdec1ced2008-02-08 02:10:57 +00001244 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001245 if (r < 0)
1246 io->error = -EIO;
1247
Mikulas Patocka40b62292012-07-27 15:08:04 +01001248 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
Milan Brozdec1ced2008-02-08 02:10:57 +00001249
Milan Brozc8081612008-10-10 13:37:08 +01001250 /* Encryption was already finished, submit io now */
1251 if (crypt_finished) {
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001252 kcryptd_crypt_write_io_submit(io, 0);
Milan Brozc8081612008-10-10 13:37:08 +01001253
1254 /*
1255 * If there was an error, do not try next fragments.
1256 * For async, error is processed in async handler.
1257 */
Milan Broz6c031f42008-10-10 13:37:06 +01001258 if (unlikely(r < 0))
Milan Brozfc5a5e92008-10-10 13:37:04 +01001259 break;
Milan Brozb635b002008-10-21 17:45:00 +01001260
1261 io->sector = sector;
Milan Broz4e594092008-10-10 13:37:07 +01001262 }
Milan Broz93e605c2006-10-03 01:15:38 -07001263
Milan Broz933f01d2008-10-10 13:37:08 +01001264 /*
1265 * Out of memory -> run queues
1266 * But don't wait if split was due to the io size restriction
1267 */
1268 if (unlikely(out_of_pages))
Jens Axboe8aa7e842009-07-09 14:52:32 +02001269 congestion_wait(BLK_RW_ASYNC, HZ/100);
Milan Broz933f01d2008-10-10 13:37:08 +01001270
Milan Broz393b47e2008-10-21 17:45:02 +01001271 /*
1272 * With async crypto it is unsafe to share the crypto context
1273 * between fragments, so switch to a new dm_crypt_io structure.
1274 */
1275 if (unlikely(!crypt_finished && remaining)) {
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001276 new_io = crypt_io_alloc(io->cc, io->base_bio,
Milan Broz393b47e2008-10-21 17:45:02 +01001277 sector);
1278 crypt_inc_pending(new_io);
1279 crypt_convert_init(cc, &new_io->ctx, NULL,
1280 io->base_bio, sector);
Kent Overstreet003b5c52013-10-11 15:45:43 -07001281 new_io->ctx.iter_in = io->ctx.iter_in;
Milan Broz393b47e2008-10-21 17:45:02 +01001282
1283 /*
1284 * Fragments after the first use the base_io
1285 * pending count.
1286 */
1287 if (!io->base_io)
1288 new_io->base_io = io;
1289 else {
1290 new_io->base_io = io->base_io;
1291 crypt_inc_pending(io->base_io);
1292 crypt_dec_pending(io);
1293 }
1294
1295 io = new_io;
1296 }
Milan Broz8b004452006-10-03 01:15:37 -07001297 }
Milan Broz899c95d2008-02-08 02:11:02 +00001298
1299 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001300}
1301
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001302static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001303{
Milan Broz5742fd72008-02-08 02:10:43 +00001304 crypt_dec_pending(io);
1305}
1306
Milan Broz4e4eef62008-02-08 02:10:49 +00001307static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001308{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001309 struct crypt_config *cc = io->cc;
Milan Broz5742fd72008-02-08 02:10:43 +00001310 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001311
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001312 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001313
Milan Broz53017032008-02-08 02:10:38 +00001314 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001315 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001316
Milan Broz5742fd72008-02-08 02:10:43 +00001317 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001318 if (r < 0)
1319 io->error = -EIO;
Milan Broz5742fd72008-02-08 02:10:43 +00001320
Mikulas Patocka40b62292012-07-27 15:08:04 +01001321 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001322 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001323
1324 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001325}
1326
Milan Broz95497a92008-02-08 02:11:12 +00001327static void kcryptd_async_done(struct crypto_async_request *async_req,
1328 int error)
1329{
Huang Yingb2174ee2009-03-16 17:44:33 +00001330 struct dm_crypt_request *dmreq = async_req->data;
1331 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001332 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001333 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001334
1335 if (error == -EINPROGRESS) {
1336 complete(&ctx->restart);
1337 return;
1338 }
1339
Milan Broz2dc53272011-01-13 19:59:54 +00001340 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
1341 error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
1342
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001343 if (error < 0)
1344 io->error = -EIO;
1345
Huang Yingb2174ee2009-03-16 17:44:33 +00001346 mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool);
Milan Broz95497a92008-02-08 02:11:12 +00001347
Mikulas Patocka40b62292012-07-27 15:08:04 +01001348 if (!atomic_dec_and_test(&ctx->cc_pending))
Milan Broz95497a92008-02-08 02:11:12 +00001349 return;
1350
1351 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001352 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001353 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001354 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001355}
1356
Milan Broz4e4eef62008-02-08 02:10:49 +00001357static void kcryptd_crypt(struct work_struct *work)
1358{
1359 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1360
1361 if (bio_data_dir(io->base_bio) == READ)
1362 kcryptd_crypt_read_convert(io);
1363 else
1364 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001365}
1366
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001367static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1368{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001369 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001370
1371 INIT_WORK(&io->work, kcryptd_crypt);
1372 queue_work(cc->crypt_queue, &io->work);
1373}
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375/*
1376 * Decode key from its hex representation
1377 */
1378static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1379{
1380 char buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 unsigned int i;
1382
1383 buffer[2] = '\0';
1384
Milan Broz8b004452006-10-03 01:15:37 -07001385 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 buffer[0] = *hex++;
1387 buffer[1] = *hex++;
1388
majianpeng1a66a082012-07-27 15:07:59 +01001389 if (kstrtou8(buffer, 16, &key[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return -EINVAL;
1391 }
1392
1393 if (*hex != '\0')
1394 return -EINVAL;
1395
1396 return 0;
1397}
1398
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001399static void crypt_free_tfms(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001400{
Milan Brozd1f96422011-01-13 19:59:54 +00001401 unsigned i;
1402
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001403 if (!cc->tfms)
1404 return;
1405
Milan Brozd1f96422011-01-13 19:59:54 +00001406 for (i = 0; i < cc->tfms_count; i++)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001407 if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) {
1408 crypto_free_ablkcipher(cc->tfms[i]);
1409 cc->tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001410 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001411
1412 kfree(cc->tfms);
1413 cc->tfms = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001414}
1415
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001416static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001417{
Milan Brozd1f96422011-01-13 19:59:54 +00001418 unsigned i;
1419 int err;
1420
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001421 cc->tfms = kmalloc(cc->tfms_count * sizeof(struct crypto_ablkcipher *),
1422 GFP_KERNEL);
1423 if (!cc->tfms)
1424 return -ENOMEM;
1425
Milan Brozd1f96422011-01-13 19:59:54 +00001426 for (i = 0; i < cc->tfms_count; i++) {
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001427 cc->tfms[i] = crypto_alloc_ablkcipher(ciphermode, 0, 0);
1428 if (IS_ERR(cc->tfms[i])) {
1429 err = PTR_ERR(cc->tfms[i]);
1430 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001431 return err;
1432 }
1433 }
1434
1435 return 0;
1436}
1437
Andi Kleenc0297722011-01-13 19:59:53 +00001438static int crypt_setkey_allcpus(struct crypt_config *cc)
1439{
Milan Brozda31a072013-10-28 23:21:03 +01001440 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001441 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001442
Milan Brozda31a072013-10-28 23:21:03 +01001443 /* Ignore extra keys (which are used for IV etc) */
1444 subkey_size = (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1445
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001446 for (i = 0; i < cc->tfms_count; i++) {
1447 r = crypto_ablkcipher_setkey(cc->tfms[i],
1448 cc->key + (i * subkey_size),
1449 subkey_size);
1450 if (r)
1451 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001452 }
1453
1454 return err;
1455}
1456
Milan Broze48d4bb2006-10-03 01:15:37 -07001457static int crypt_set_key(struct crypt_config *cc, char *key)
1458{
Milan Brozde8be5a2011-03-24 13:54:27 +00001459 int r = -EINVAL;
1460 int key_string_len = strlen(key);
1461
Milan Broz69a8cfc2011-01-13 19:59:49 +00001462 /* The key size may not be changed. */
Milan Brozde8be5a2011-03-24 13:54:27 +00001463 if (cc->key_size != (key_string_len >> 1))
1464 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001465
Milan Broz69a8cfc2011-01-13 19:59:49 +00001466 /* Hyphen (which gives a key_size of zero) means there is no key. */
1467 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00001468 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001469
Milan Broz69a8cfc2011-01-13 19:59:49 +00001470 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00001471 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001472
1473 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1474
Milan Brozde8be5a2011-03-24 13:54:27 +00001475 r = crypt_setkey_allcpus(cc);
1476
1477out:
1478 /* Hex key string not needed after here, so wipe it. */
1479 memset(key, '0', key_string_len);
1480
1481 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07001482}
1483
1484static int crypt_wipe_key(struct crypt_config *cc)
1485{
1486 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1487 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00001488
1489 return crypt_setkey_allcpus(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07001490}
1491
Milan Broz28513fc2010-08-12 04:14:06 +01001492static void crypt_dtr(struct dm_target *ti)
1493{
1494 struct crypt_config *cc = ti->private;
Andi Kleenc0297722011-01-13 19:59:53 +00001495 struct crypt_cpu *cpu_cc;
1496 int cpu;
Milan Broz28513fc2010-08-12 04:14:06 +01001497
1498 ti->private = NULL;
1499
1500 if (!cc)
1501 return;
1502
1503 if (cc->io_queue)
1504 destroy_workqueue(cc->io_queue);
1505 if (cc->crypt_queue)
1506 destroy_workqueue(cc->crypt_queue);
1507
Andi Kleenc0297722011-01-13 19:59:53 +00001508 if (cc->cpu)
1509 for_each_possible_cpu(cpu) {
1510 cpu_cc = per_cpu_ptr(cc->cpu, cpu);
1511 if (cpu_cc->req)
1512 mempool_free(cpu_cc->req, cc->req_pool);
Andi Kleenc0297722011-01-13 19:59:53 +00001513 }
1514
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001515 crypt_free_tfms(cc);
1516
Milan Broz28513fc2010-08-12 04:14:06 +01001517 if (cc->bs)
1518 bioset_free(cc->bs);
1519
1520 if (cc->page_pool)
1521 mempool_destroy(cc->page_pool);
1522 if (cc->req_pool)
1523 mempool_destroy(cc->req_pool);
1524 if (cc->io_pool)
1525 mempool_destroy(cc->io_pool);
1526
1527 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1528 cc->iv_gen_ops->dtr(cc);
1529
Milan Broz28513fc2010-08-12 04:14:06 +01001530 if (cc->dev)
1531 dm_put_device(ti, cc->dev);
1532
Andi Kleenc0297722011-01-13 19:59:53 +00001533 if (cc->cpu)
1534 free_percpu(cc->cpu);
1535
Milan Broz5ebaee62010-08-12 04:14:07 +01001536 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00001537 kzfree(cc->cipher_string);
Milan Broz28513fc2010-08-12 04:14:06 +01001538
1539 /* Must zero key material before freeing */
1540 kzfree(cc);
1541}
1542
Milan Broz5ebaee62010-08-12 04:14:07 +01001543static int crypt_ctr_cipher(struct dm_target *ti,
1544 char *cipher_in, char *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Milan Broz5ebaee62010-08-12 04:14:07 +01001546 struct crypt_config *cc = ti->private;
Milan Brozd1f96422011-01-13 19:59:54 +00001547 char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01001548 char *cipher_api = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001549 int ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001550 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Milan Broz5ebaee62010-08-12 04:14:07 +01001552 /* Convert to crypto api definition? */
1553 if (strchr(cipher_in, '(')) {
1554 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return -EINVAL;
1556 }
1557
Milan Broz7dbcd132011-01-13 19:59:52 +00001558 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
1559 if (!cc->cipher_string)
1560 goto bad_mem;
1561
Milan Broz5ebaee62010-08-12 04:14:07 +01001562 /*
1563 * Legacy dm-crypt cipher specification
Milan Brozd1f96422011-01-13 19:59:54 +00001564 * cipher[:keycount]-mode-iv:ivopts
Milan Broz5ebaee62010-08-12 04:14:07 +01001565 */
1566 tmp = cipher_in;
Milan Brozd1f96422011-01-13 19:59:54 +00001567 keycount = strsep(&tmp, "-");
1568 cipher = strsep(&keycount, ":");
1569
1570 if (!keycount)
1571 cc->tfms_count = 1;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001572 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
Milan Brozd1f96422011-01-13 19:59:54 +00001573 !is_power_of_2(cc->tfms_count)) {
1574 ti->error = "Bad cipher key count specification";
1575 return -EINVAL;
1576 }
1577 cc->key_parts = cc->tfms_count;
Milan Brozda31a072013-10-28 23:21:03 +01001578 cc->key_extra_size = 0;
Milan Broz5ebaee62010-08-12 04:14:07 +01001579
1580 cc->cipher = kstrdup(cipher, GFP_KERNEL);
1581 if (!cc->cipher)
1582 goto bad_mem;
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 chainmode = strsep(&tmp, "-");
1585 ivopts = strsep(&tmp, "-");
1586 ivmode = strsep(&ivopts, ":");
1587
1588 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01001589 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001591 cc->cpu = __alloc_percpu(sizeof(*(cc->cpu)),
Milan Brozd1f96422011-01-13 19:59:54 +00001592 __alignof__(struct crypt_cpu));
Andi Kleenc0297722011-01-13 19:59:53 +00001593 if (!cc->cpu) {
1594 ti->error = "Cannot allocate per cpu state";
1595 goto bad_mem;
1596 }
1597
Milan Broz7dbcd132011-01-13 19:59:52 +00001598 /*
1599 * For compatibility with the original dm-crypt mapping format, if
1600 * only the cipher name is supplied, use cbc-plain.
1601 */
Milan Broz5ebaee62010-08-12 04:14:07 +01001602 if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 chainmode = "cbc";
1604 ivmode = "plain";
1605 }
1606
Herbert Xud1806f62006-08-22 20:29:17 +10001607 if (strcmp(chainmode, "ecb") && !ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001608 ti->error = "IV mechanism required";
1609 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
1611
Milan Broz5ebaee62010-08-12 04:14:07 +01001612 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
1613 if (!cipher_api)
1614 goto bad_mem;
1615
1616 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
1617 "%s(%s)", chainmode, cipher);
1618 if (ret < 0) {
1619 kfree(cipher_api);
1620 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10001621 }
1622
Milan Broz5ebaee62010-08-12 04:14:07 +01001623 /* Allocate cipher */
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001624 ret = crypt_alloc_tfms(cc, cipher_api);
1625 if (ret < 0) {
1626 ti->error = "Error allocating crypto tfm";
1627 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Milan Broz5ebaee62010-08-12 04:14:07 +01001630 /* Initialize IV */
Andi Kleenc0297722011-01-13 19:59:53 +00001631 cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
Milan Broz5ebaee62010-08-12 04:14:07 +01001632 if (cc->iv_size)
1633 /* at least a 64 bit sector number should fit in our buffer */
1634 cc->iv_size = max(cc->iv_size,
1635 (unsigned int)(sizeof(u64) / sizeof(u8)));
1636 else if (ivmode) {
1637 DMWARN("Selected cipher does not support IVs");
1638 ivmode = NULL;
1639 }
1640
1641 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (ivmode == NULL)
1643 cc->iv_gen_ops = NULL;
1644 else if (strcmp(ivmode, "plain") == 0)
1645 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00001646 else if (strcmp(ivmode, "plain64") == 0)
1647 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 else if (strcmp(ivmode, "essiv") == 0)
1649 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10001650 else if (strcmp(ivmode, "benbi") == 0)
1651 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07001652 else if (strcmp(ivmode, "null") == 0)
1653 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00001654 else if (strcmp(ivmode, "lmk") == 0) {
1655 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01001656 /*
1657 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00001658 * to length of provided multi-key string.
1659 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01001660 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00001661 */
Milan Brozda31a072013-10-28 23:21:03 +01001662 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00001663 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01001664 cc->key_extra_size = cc->key_size / cc->key_parts;
1665 }
Milan Brozed04d982013-10-28 23:21:04 +01001666 } else if (strcmp(ivmode, "tcw") == 0) {
1667 cc->iv_gen_ops = &crypt_iv_tcw_ops;
1668 cc->key_parts += 2; /* IV + whitening */
1669 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broz34745782011-01-13 19:59:55 +00001670 } else {
Milan Broz5ebaee62010-08-12 04:14:07 +01001671 ret = -EINVAL;
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001672 ti->error = "Invalid IV mode";
Milan Broz28513fc2010-08-12 04:14:06 +01001673 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 }
1675
Milan Brozda31a072013-10-28 23:21:03 +01001676 /* Initialize and set key */
1677 ret = crypt_set_key(cc, key);
1678 if (ret < 0) {
1679 ti->error = "Error decoding and setting key";
1680 goto bad;
1681 }
1682
Milan Broz28513fc2010-08-12 04:14:06 +01001683 /* Allocate IV */
1684 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
1685 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
1686 if (ret < 0) {
1687 ti->error = "Error creating IV";
1688 goto bad;
1689 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00001690 }
1691
Milan Broz28513fc2010-08-12 04:14:06 +01001692 /* Initialize IV (set keys for ESSIV etc) */
1693 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
1694 ret = cc->iv_gen_ops->init(cc);
1695 if (ret < 0) {
1696 ti->error = "Error initialising IV";
1697 goto bad;
1698 }
1699 }
1700
Milan Broz5ebaee62010-08-12 04:14:07 +01001701 ret = 0;
1702bad:
1703 kfree(cipher_api);
1704 return ret;
1705
1706bad_mem:
1707 ti->error = "Cannot allocate cipher strings";
1708 return -ENOMEM;
1709}
1710
1711/*
1712 * Construct an encryption mapping:
1713 * <cipher> <key> <iv_offset> <dev_path> <start>
1714 */
1715static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1716{
1717 struct crypt_config *cc;
Milan Broz772ae5f2011-08-02 12:32:08 +01001718 unsigned int key_size, opt_params;
Milan Broz5ebaee62010-08-12 04:14:07 +01001719 unsigned long long tmpll;
1720 int ret;
Milan Broz772ae5f2011-08-02 12:32:08 +01001721 struct dm_arg_set as;
1722 const char *opt_string;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001723 char dummy;
Milan Broz5ebaee62010-08-12 04:14:07 +01001724
Milan Broz772ae5f2011-08-02 12:32:08 +01001725 static struct dm_arg _args[] = {
1726 {0, 1, "Invalid number of feature args"},
1727 };
1728
1729 if (argc < 5) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001730 ti->error = "Not enough arguments";
1731 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
1733
Milan Broz5ebaee62010-08-12 04:14:07 +01001734 key_size = strlen(argv[1]) >> 1;
1735
1736 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1737 if (!cc) {
1738 ti->error = "Cannot allocate encryption context";
1739 return -ENOMEM;
1740 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00001741 cc->key_size = key_size;
Milan Broz5ebaee62010-08-12 04:14:07 +01001742
1743 ti->private = cc;
1744 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
1745 if (ret < 0)
1746 goto bad;
1747
Milan Broz28513fc2010-08-12 04:14:06 +01001748 ret = -ENOMEM;
Matthew Dobson93d23412006-03-26 01:37:50 -08001749 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (!cc->io_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001751 ti->error = "Cannot allocate crypt io mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001752 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
Milan Brozddd42ed2008-02-08 02:11:07 +00001755 cc->dmreq_start = sizeof(struct ablkcipher_request);
Andi Kleenc0297722011-01-13 19:59:53 +00001756 cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
Milan Brozddd42ed2008-02-08 02:11:07 +00001757 cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
Andi Kleenc0297722011-01-13 19:59:53 +00001758 cc->dmreq_start += crypto_ablkcipher_alignmask(any_tfm(cc)) &
Milan Broz3a7f6c92008-02-08 02:11:14 +00001759 ~(crypto_tfm_ctx_alignment() - 1);
Milan Brozddd42ed2008-02-08 02:11:07 +00001760
1761 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
1762 sizeof(struct dm_crypt_request) + cc->iv_size);
1763 if (!cc->req_pool) {
1764 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001765 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00001766 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001767
Matthew Dobsona19b27c2006-03-26 01:37:45 -08001768 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001770 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001771 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773
Jens Axboebb799ca2008-12-10 15:35:05 +01001774 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07001775 if (!cc->bs) {
1776 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01001777 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07001778 }
1779
Milan Broz28513fc2010-08-12 04:14:06 +01001780 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001781 if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001782 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001783 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001785 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Milan Broz28513fc2010-08-12 04:14:06 +01001787 if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
1788 ti->error = "Device lookup failed";
1789 goto bad;
1790 }
1791
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001792 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001793 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001794 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001796 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Milan Broz772ae5f2011-08-02 12:32:08 +01001798 argv += 5;
1799 argc -= 5;
1800
1801 /* Optional parameters */
1802 if (argc) {
1803 as.argc = argc;
1804 as.argv = argv;
1805
1806 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
1807 if (ret)
1808 goto bad;
1809
1810 opt_string = dm_shift_arg(&as);
1811
1812 if (opt_params == 1 && opt_string &&
1813 !strcasecmp(opt_string, "allow_discards"))
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001814 ti->num_discard_bios = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01001815 else if (opt_params) {
1816 ret = -EINVAL;
1817 ti->error = "Invalid feature arguments";
1818 goto bad;
1819 }
1820 }
1821
Milan Broz28513fc2010-08-12 04:14:06 +01001822 ret = -ENOMEM;
Tejun Heo670368a2013-07-30 08:40:21 -04001823 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_MEM_RECLAIM, 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001824 if (!cc->io_queue) {
1825 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001826 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01001827 }
1828
Andi Kleenc0297722011-01-13 19:59:53 +00001829 cc->crypt_queue = alloc_workqueue("kcryptd",
Tejun Heo670368a2013-07-30 08:40:21 -04001830 WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001831 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01001832 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001833 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01001834 }
1835
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001836 ti->num_flush_bios = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01001837 ti->discard_zeroes_data_unsupported = true;
Milan Broz983c7db2011-09-25 23:26:21 +01001838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 return 0;
1840
Milan Broz28513fc2010-08-12 04:14:06 +01001841bad:
1842 crypt_dtr(ti);
1843 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
1845
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001846static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001848 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001849 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001850
Milan Broz772ae5f2011-08-02 12:32:08 +01001851 /*
1852 * If bio is REQ_FLUSH or REQ_DISCARD, just bypass crypt queues.
1853 * - for REQ_FLUSH device-mapper core ensures that no IO is in-flight
1854 * - for REQ_DISCARD caller must use flush if IO ordering matters
1855 */
1856 if (unlikely(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD))) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001857 bio->bi_bdev = cc->dev->bdev;
Milan Broz772ae5f2011-08-02 12:32:08 +01001858 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001859 bio->bi_iter.bi_sector = cc->start +
1860 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001861 return DM_MAPIO_REMAPPED;
1862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Kent Overstreet4f024f32013-10-11 15:44:27 -07001864 io = crypt_io_alloc(cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Milan Brozcabf08e2007-10-19 22:38:58 +01001865
Milan Broz20c82532011-01-13 19:59:53 +00001866 if (bio_data_dir(io->base_bio) == READ) {
1867 if (kcryptd_io_read(io, GFP_NOWAIT))
1868 kcryptd_queue_io(io);
1869 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01001870 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001872 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
1874
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001875static void crypt_status(struct dm_target *ti, status_type_t type,
1876 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Milan Broz5ebaee62010-08-12 04:14:07 +01001878 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001879 unsigned i, sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 switch (type) {
1882 case STATUSTYPE_INFO:
1883 result[0] = '\0';
1884 break;
1885
1886 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00001887 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001889 if (cc->key_size > 0)
1890 for (i = 0; i < cc->key_size; i++)
1891 DMEMIT("%02x", cc->key[i]);
1892 else
1893 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Andrew Morton4ee218c2006-03-27 01:17:48 -08001895 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1896 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01001897
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001898 if (ti->num_discard_bios)
Milan Broz772ae5f2011-08-02 12:32:08 +01001899 DMEMIT(" 1 allow_discards");
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 break;
1902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904
Milan Broze48d4bb2006-10-03 01:15:37 -07001905static void crypt_postsuspend(struct dm_target *ti)
1906{
1907 struct crypt_config *cc = ti->private;
1908
1909 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1910}
1911
1912static int crypt_preresume(struct dm_target *ti)
1913{
1914 struct crypt_config *cc = ti->private;
1915
1916 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1917 DMERR("aborting resume - crypt key is not set.");
1918 return -EAGAIN;
1919 }
1920
1921 return 0;
1922}
1923
1924static void crypt_resume(struct dm_target *ti)
1925{
1926 struct crypt_config *cc = ti->private;
1927
1928 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1929}
1930
1931/* Message interface
1932 * key set <key>
1933 * key wipe
1934 */
1935static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1936{
1937 struct crypt_config *cc = ti->private;
Milan Broz542da312009-12-10 23:51:57 +00001938 int ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07001939
1940 if (argc < 2)
1941 goto error;
1942
Mike Snitzer498f0102011-08-02 12:32:04 +01001943 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07001944 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1945 DMWARN("not suspended during key manipulation.");
1946 return -EINVAL;
1947 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001948 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Milan Broz542da312009-12-10 23:51:57 +00001949 ret = crypt_set_key(cc, argv[2]);
1950 if (ret)
1951 return ret;
1952 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
1953 ret = cc->iv_gen_ops->init(cc);
1954 return ret;
1955 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001956 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00001957 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
1958 ret = cc->iv_gen_ops->wipe(cc);
1959 if (ret)
1960 return ret;
1961 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001962 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00001963 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001964 }
1965
1966error:
1967 DMWARN("unrecognised message received.");
1968 return -EINVAL;
1969}
1970
Milan Brozd41e26b2008-07-21 12:00:40 +01001971static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
1972 struct bio_vec *biovec, int max_size)
1973{
1974 struct crypt_config *cc = ti->private;
1975 struct request_queue *q = bdev_get_queue(cc->dev->bdev);
1976
1977 if (!q->merge_bvec_fn)
1978 return max_size;
1979
1980 bvm->bi_bdev = cc->dev->bdev;
Alasdair G Kergonb441a2622010-08-12 04:14:11 +01001981 bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
Milan Brozd41e26b2008-07-21 12:00:40 +01001982
1983 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
1984}
1985
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001986static int crypt_iterate_devices(struct dm_target *ti,
1987 iterate_devices_callout_fn fn, void *data)
1988{
1989 struct crypt_config *cc = ti->private;
1990
Mike Snitzer5dea2712009-07-23 20:30:42 +01001991 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001992}
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994static struct target_type crypt_target = {
1995 .name = "crypt",
Milan Brozed04d982013-10-28 23:21:04 +01001996 .version = {1, 13, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 .module = THIS_MODULE,
1998 .ctr = crypt_ctr,
1999 .dtr = crypt_dtr,
2000 .map = crypt_map,
2001 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07002002 .postsuspend = crypt_postsuspend,
2003 .preresume = crypt_preresume,
2004 .resume = crypt_resume,
2005 .message = crypt_message,
Milan Brozd41e26b2008-07-21 12:00:40 +01002006 .merge = crypt_merge,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002007 .iterate_devices = crypt_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008};
2009
2010static int __init dm_crypt_init(void)
2011{
2012 int r;
2013
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002014 _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (!_crypt_io_pool)
2016 return -ENOMEM;
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 r = dm_register_target(&crypt_target);
2019 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002020 DMERR("register failed %d", r);
Milan Broz9934a8b2007-10-19 22:38:57 +01002021 kmem_cache_destroy(_crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return r;
2025}
2026
2027static void __exit dm_crypt_exit(void)
2028{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002029 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 kmem_cache_destroy(_crypt_io_pool);
2031}
2032
2033module_init(dm_crypt_init);
2034module_exit(dm_crypt_exit);
2035
2036MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
2037MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
2038MODULE_LICENSE("GPL");