blob: 81b0fa66045204604a979fdc929e723f6c914a5c [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;
42 unsigned int offset_in;
43 unsigned int offset_out;
44 unsigned int idx_in;
45 unsigned int idx_out;
Mikulas Patockac66029f2012-07-27 15:08:05 +010046 sector_t cc_sector;
Mikulas Patocka40b62292012-07-27 15:08:04 +010047 atomic_t cc_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Milan Broz53017032008-02-08 02:10:38 +000050/*
51 * per bio private data
52 */
53struct dm_crypt_io {
Alasdair G Kergon49a8a922012-07-27 15:08:05 +010054 struct crypt_config *cc;
Milan Broz53017032008-02-08 02:10:38 +000055 struct bio *base_bio;
56 struct work_struct work;
57
58 struct convert_context ctx;
59
Mikulas Patocka40b62292012-07-27 15:08:04 +010060 atomic_t io_pending;
Milan Broz53017032008-02-08 02:10:38 +000061 int error;
Milan Broz0c395b02008-02-08 02:10:54 +000062 sector_t sector;
Milan Broz393b47e2008-10-21 17:45:02 +010063 struct dm_crypt_io *base_io;
Milan Broz53017032008-02-08 02:10:38 +000064};
65
Milan Broz01482b72008-02-08 02:11:04 +000066struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000067 struct convert_context *ctx;
Milan Broz01482b72008-02-08 02:11:04 +000068 struct scatterlist sg_in;
69 struct scatterlist sg_out;
Milan Broz2dc53272011-01-13 19:59:54 +000070 sector_t iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000071};
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073struct crypt_config;
74
75struct crypt_iv_operations {
76 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010077 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000079 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000080 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc53272011-01-13 19:59:54 +000081 int (*generator)(struct crypt_config *cc, u8 *iv,
82 struct dm_crypt_request *dmreq);
83 int (*post)(struct crypt_config *cc, u8 *iv,
84 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085};
86
Milan Broz60473592009-12-10 23:51:55 +000087struct iv_essiv_private {
Milan Brozb95bf2d2009-12-10 23:51:56 +000088 struct crypto_hash *hash_tfm;
89 u8 *salt;
Milan Broz60473592009-12-10 23:51:55 +000090};
91
92struct iv_benbi_private {
93 int shift;
94};
95
Milan Broz34745782011-01-13 19:59:55 +000096#define LMK_SEED_SIZE 64 /* hash + 0 */
97struct iv_lmk_private {
98 struct crypto_shash *hash_tfm;
99 u8 *seed;
100};
101
Milan Brozed04d982013-10-28 23:21:04 +0100102#define TCW_WHITENING_SIZE 16
103struct iv_tcw_private {
104 struct crypto_shash *crc32_tfm;
105 u8 *iv_seed;
106 u8 *whitening;
107};
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/*
110 * Crypt: maps a linear range of a block device
111 * and encrypts / decrypts at the same time.
112 */
Milan Broze48d4bb2006-10-03 01:15:37 -0700113enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
Andi Kleenc0297722011-01-13 19:59:53 +0000114
115/*
116 * Duplicated per-CPU state for cipher.
117 */
118struct crypt_cpu {
119 struct ablkcipher_request *req;
Andi Kleenc0297722011-01-13 19:59:53 +0000120};
121
122/*
123 * The fields in here must be read only after initialization,
124 * changing state should be in crypt_cpu.
125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126struct crypt_config {
127 struct dm_dev *dev;
128 sector_t start;
129
130 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000131 * pool for per bio private data, crypto requests and
132 * encryption requeusts/buffer pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
134 mempool_t *io_pool;
Milan Brozddd42ed2008-02-08 02:11:07 +0000135 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -0700137 struct bio_set *bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Milan Brozcabf08e2007-10-19 22:38:58 +0100139 struct workqueue_struct *io_queue;
140 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700141
Milan Broz5ebaee62010-08-12 04:14:07 +0100142 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000143 char *cipher_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800146 union {
Milan Broz60473592009-12-10 23:51:55 +0000147 struct iv_essiv_private essiv;
148 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000149 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100150 struct iv_tcw_private tcw;
Herbert Xu79066ad2006-12-05 13:41:52 -0800151 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 sector_t iv_offset;
153 unsigned int iv_size;
154
Milan Brozddd42ed2008-02-08 02:11:07 +0000155 /*
Andi Kleenc0297722011-01-13 19:59:53 +0000156 * Duplicated per cpu state. Access through
157 * per_cpu_ptr() only.
158 */
159 struct crypt_cpu __percpu *cpu;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100160
161 /* ESSIV: struct crypto_cipher *essiv_tfm */
162 void *iv_private;
163 struct crypto_ablkcipher **tfms;
Milan Brozd1f96422011-01-13 19:59:54 +0000164 unsigned tfms_count;
Andi Kleenc0297722011-01-13 19:59:53 +0000165
166 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000167 * Layout of each crypto request:
168 *
169 * struct ablkcipher_request
170 * context
171 * padding
172 * struct dm_crypt_request
173 * padding
174 * IV
175 *
176 * The padding is added so that dm_crypt_request and the IV are
177 * correctly aligned.
178 */
179 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000180
Milan Broze48d4bb2006-10-03 01:15:37 -0700181 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100183 unsigned int key_parts; /* independent parts in key buffer */
184 unsigned int key_extra_size; /* additional keys length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 u8 key[0];
186};
187
Milan Broz6a24c712006-10-03 01:15:40 -0700188#define MIN_IOS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#define MIN_POOL_PAGES 32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Christoph Lametere18b8902006-12-06 20:33:20 -0800191static struct kmem_cache *_crypt_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100193static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000194static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Broz2dc53272011-01-13 19:59:54 +0000195static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
Olaf Kirch027581f2007-05-09 02:32:52 -0700196
Andi Kleenc0297722011-01-13 19:59:53 +0000197static struct crypt_cpu *this_crypt_config(struct crypt_config *cc)
198{
199 return this_cpu_ptr(cc->cpu);
200}
201
202/*
203 * Use this to access cipher attributes that are the same for each CPU.
204 */
205static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc)
206{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100207 return cc->tfms[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * Different IV generation algorithms:
212 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000213 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200214 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 *
Milan Broz61afef62009-12-10 23:52:25 +0000216 * plain64: the initial vector is the 64-bit little-endian version of the sector
217 * number, padded with zeros if necessary.
218 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000219 * essiv: "encrypted sector|salt initial vector", the sector number is
220 * encrypted with the bulk cipher using a salt as key. The salt
221 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *
Rik Snel48527fa2006-09-03 08:56:39 +1000223 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
224 * (needed for LRW-32-AES and possible other narrow block modes)
225 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700226 * null: the initial vector is always zero. Provides compatibility with
227 * obsolete loop_fish2 devices. Do not use for new devices.
228 *
Milan Broz34745782011-01-13 19:59:55 +0000229 * lmk: Compatible implementation of the block chaining mode used
230 * by the Loop-AES block device encryption system
231 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
232 * It operates on full 512 byte sectors and uses CBC
233 * with an IV derived from the sector number, the data and
234 * optionally extra IV seed.
235 * This means that after decryption the first block
236 * of sector must be tweaked according to decrypted data.
237 * Loop-AES can use three encryption schemes:
238 * version 1: is plain aes-cbc mode
239 * version 2: uses 64 multikey scheme with lmk IV generator
240 * version 3: the same as version 2 with additional IV seed
241 * (it uses 65 keys, last key is used as IV seed)
242 *
Milan Brozed04d982013-10-28 23:21:04 +0100243 * tcw: Compatible implementation of the block chaining mode used
244 * by the TrueCrypt device encryption system (prior to version 4.1).
245 * For more info see: http://www.truecrypt.org
246 * It operates on full 512 byte sectors and uses CBC
247 * with an IV derived from initial key and the sector number.
248 * In addition, whitening value is applied on every sector, whitening
249 * is calculated from initial key, sector number and mixed using CRC32.
250 * Note that this encryption scheme is vulnerable to watermarking attacks
251 * and should be used for old compatible containers access only.
252 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * plumb: unimplemented, see:
254 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
255 */
256
Milan Broz2dc53272011-01-13 19:59:54 +0000257static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
258 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100261 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 return 0;
264}
265
Milan Broz61afef62009-12-10 23:52:25 +0000266static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000267 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000268{
269 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100270 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000271
272 return 0;
273}
274
Milan Brozb95bf2d2009-12-10 23:51:56 +0000275/* Initialise ESSIV - compute salt but no local memory allocations */
276static int crypt_iv_essiv_init(struct crypt_config *cc)
277{
278 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
279 struct hash_desc desc;
280 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000281 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100282 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000283
284 sg_init_one(&sg, cc->key, cc->key_size);
285 desc.tfm = essiv->hash_tfm;
286 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
287
288 err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt);
289 if (err)
290 return err;
291
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100292 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000293
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100294 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
295 crypto_hash_digestsize(essiv->hash_tfm));
296 if (err)
297 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000298
299 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000300}
301
Milan Broz542da312009-12-10 23:51:57 +0000302/* Wipe salt and reset key derived from volume key */
303static int crypt_iv_essiv_wipe(struct crypt_config *cc)
304{
305 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
306 unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000307 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100308 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000309
310 memset(essiv->salt, 0, salt_size);
311
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100312 essiv_tfm = cc->iv_private;
313 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
314 if (r)
315 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000316
317 return err;
318}
319
320/* Set up per cpu cipher state */
321static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
322 struct dm_target *ti,
323 u8 *salt, unsigned saltsize)
324{
325 struct crypto_cipher *essiv_tfm;
326 int err;
327
328 /* Setup the essiv_tfm with the given salt */
329 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
330 if (IS_ERR(essiv_tfm)) {
331 ti->error = "Error allocating crypto tfm for ESSIV";
332 return essiv_tfm;
333 }
334
335 if (crypto_cipher_blocksize(essiv_tfm) !=
336 crypto_ablkcipher_ivsize(any_tfm(cc))) {
337 ti->error = "Block size of ESSIV cipher does "
338 "not match IV size of block cipher";
339 crypto_free_cipher(essiv_tfm);
340 return ERR_PTR(-EINVAL);
341 }
342
343 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
344 if (err) {
345 ti->error = "Failed to set key for ESSIV cipher";
346 crypto_free_cipher(essiv_tfm);
347 return ERR_PTR(err);
348 }
349
350 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000351}
352
Milan Broz60473592009-12-10 23:51:55 +0000353static void crypt_iv_essiv_dtr(struct crypt_config *cc)
354{
Andi Kleenc0297722011-01-13 19:59:53 +0000355 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000356 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
357
Milan Brozb95bf2d2009-12-10 23:51:56 +0000358 crypto_free_hash(essiv->hash_tfm);
359 essiv->hash_tfm = NULL;
360
361 kzfree(essiv->salt);
362 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000363
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100364 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000365
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100366 if (essiv_tfm)
367 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000368
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100369 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100373 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Milan Broz5861f1b2009-12-10 23:51:56 +0000375 struct crypto_cipher *essiv_tfm = NULL;
376 struct crypto_hash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000377 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100378 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Milan Broz5861f1b2009-12-10 23:51:56 +0000380 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700381 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return -EINVAL;
383 }
384
Milan Brozb95bf2d2009-12-10 23:51:56 +0000385 /* Allocate hash algorithm */
Herbert Xu35058682006-08-24 19:10:20 +1000386 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
387 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700388 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000389 err = PTR_ERR(hash_tfm);
390 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
Milan Brozb95bf2d2009-12-10 23:51:56 +0000393 salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000394 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700395 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000396 err = -ENOMEM;
397 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
Milan Brozb95bf2d2009-12-10 23:51:56 +0000400 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000401 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
402
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100403 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
404 crypto_hash_digestsize(hash_tfm));
405 if (IS_ERR(essiv_tfm)) {
406 crypt_iv_essiv_dtr(cc);
407 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000408 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100409 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000412
413bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000414 if (hash_tfm && !IS_ERR(hash_tfm))
415 crypto_free_hash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000416 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000417 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Milan Broz2dc53272011-01-13 19:59:54 +0000420static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
421 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100423 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100426 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000427 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return 0;
430}
431
Rik Snel48527fa2006-09-03 08:56:39 +1000432static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
433 const char *opts)
434{
Andi Kleenc0297722011-01-13 19:59:53 +0000435 unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800436 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000437
438 /* we need to calculate how far we must shift the sector count
439 * to get the cipher block count, we use this shift in _gen */
440
441 if (1 << log != bs) {
442 ti->error = "cypher blocksize is not a power of 2";
443 return -EINVAL;
444 }
445
446 if (log > 9) {
447 ti->error = "cypher blocksize is > 512";
448 return -EINVAL;
449 }
450
Milan Broz60473592009-12-10 23:51:55 +0000451 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000452
453 return 0;
454}
455
456static void crypt_iv_benbi_dtr(struct crypt_config *cc)
457{
Rik Snel48527fa2006-09-03 08:56:39 +1000458}
459
Milan Broz2dc53272011-01-13 19:59:54 +0000460static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
461 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000462{
Herbert Xu79066ad2006-12-05 13:41:52 -0800463 __be64 val;
464
Rik Snel48527fa2006-09-03 08:56:39 +1000465 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800466
Milan Broz2dc53272011-01-13 19:59:54 +0000467 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800468 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return 0;
471}
472
Milan Broz2dc53272011-01-13 19:59:54 +0000473static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
474 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700475{
476 memset(iv, 0, cc->iv_size);
477
478 return 0;
479}
480
Milan Broz34745782011-01-13 19:59:55 +0000481static void crypt_iv_lmk_dtr(struct crypt_config *cc)
482{
483 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
484
485 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
486 crypto_free_shash(lmk->hash_tfm);
487 lmk->hash_tfm = NULL;
488
489 kzfree(lmk->seed);
490 lmk->seed = NULL;
491}
492
493static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
494 const char *opts)
495{
496 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
497
498 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
499 if (IS_ERR(lmk->hash_tfm)) {
500 ti->error = "Error initializing LMK hash";
501 return PTR_ERR(lmk->hash_tfm);
502 }
503
504 /* No seed in LMK version 2 */
505 if (cc->key_parts == cc->tfms_count) {
506 lmk->seed = NULL;
507 return 0;
508 }
509
510 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
511 if (!lmk->seed) {
512 crypt_iv_lmk_dtr(cc);
513 ti->error = "Error kmallocing seed storage in LMK";
514 return -ENOMEM;
515 }
516
517 return 0;
518}
519
520static int crypt_iv_lmk_init(struct crypt_config *cc)
521{
522 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
523 int subkey_size = cc->key_size / cc->key_parts;
524
525 /* LMK seed is on the position of LMK_KEYS + 1 key */
526 if (lmk->seed)
527 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
528 crypto_shash_digestsize(lmk->hash_tfm));
529
530 return 0;
531}
532
533static int crypt_iv_lmk_wipe(struct crypt_config *cc)
534{
535 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
536
537 if (lmk->seed)
538 memset(lmk->seed, 0, LMK_SEED_SIZE);
539
540 return 0;
541}
542
543static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
544 struct dm_crypt_request *dmreq,
545 u8 *data)
546{
547 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
548 struct {
549 struct shash_desc desc;
550 char ctx[crypto_shash_descsize(lmk->hash_tfm)];
551 } sdesc;
552 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100553 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000554 int i, r;
555
556 sdesc.desc.tfm = lmk->hash_tfm;
557 sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
558
559 r = crypto_shash_init(&sdesc.desc);
560 if (r)
561 return r;
562
563 if (lmk->seed) {
564 r = crypto_shash_update(&sdesc.desc, lmk->seed, LMK_SEED_SIZE);
565 if (r)
566 return r;
567 }
568
569 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
570 r = crypto_shash_update(&sdesc.desc, data + 16, 16 * 31);
571 if (r)
572 return r;
573
574 /* Sector is cropped to 56 bits here */
575 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
576 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
577 buf[2] = cpu_to_le32(4024);
578 buf[3] = 0;
579 r = crypto_shash_update(&sdesc.desc, (u8 *)buf, sizeof(buf));
580 if (r)
581 return r;
582
583 /* No MD5 padding here */
584 r = crypto_shash_export(&sdesc.desc, &md5state);
585 if (r)
586 return r;
587
588 for (i = 0; i < MD5_HASH_WORDS; i++)
589 __cpu_to_le32s(&md5state.hash[i]);
590 memcpy(iv, &md5state.hash, cc->iv_size);
591
592 return 0;
593}
594
595static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
596 struct dm_crypt_request *dmreq)
597{
598 u8 *src;
599 int r = 0;
600
601 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Cong Wangc2e022c2011-11-28 13:26:02 +0800602 src = kmap_atomic(sg_page(&dmreq->sg_in));
Milan Broz34745782011-01-13 19:59:55 +0000603 r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800604 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000605 } else
606 memset(iv, 0, cc->iv_size);
607
608 return r;
609}
610
611static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
612 struct dm_crypt_request *dmreq)
613{
614 u8 *dst;
615 int r;
616
617 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
618 return 0;
619
Cong Wangc2e022c2011-11-28 13:26:02 +0800620 dst = kmap_atomic(sg_page(&dmreq->sg_out));
Milan Broz34745782011-01-13 19:59:55 +0000621 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
622
623 /* Tweak the first block of plaintext sector */
624 if (!r)
625 crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
626
Cong Wangc2e022c2011-11-28 13:26:02 +0800627 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000628 return r;
629}
630
Milan Brozed04d982013-10-28 23:21:04 +0100631static void crypt_iv_tcw_dtr(struct crypt_config *cc)
632{
633 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
634
635 kzfree(tcw->iv_seed);
636 tcw->iv_seed = NULL;
637 kzfree(tcw->whitening);
638 tcw->whitening = NULL;
639
640 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
641 crypto_free_shash(tcw->crc32_tfm);
642 tcw->crc32_tfm = NULL;
643}
644
645static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
646 const char *opts)
647{
648 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
649
650 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
651 ti->error = "Wrong key size for TCW";
652 return -EINVAL;
653 }
654
655 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
656 if (IS_ERR(tcw->crc32_tfm)) {
657 ti->error = "Error initializing CRC32 in TCW";
658 return PTR_ERR(tcw->crc32_tfm);
659 }
660
661 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
662 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
663 if (!tcw->iv_seed || !tcw->whitening) {
664 crypt_iv_tcw_dtr(cc);
665 ti->error = "Error allocating seed storage in TCW";
666 return -ENOMEM;
667 }
668
669 return 0;
670}
671
672static int crypt_iv_tcw_init(struct crypt_config *cc)
673{
674 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
675 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
676
677 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
678 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
679 TCW_WHITENING_SIZE);
680
681 return 0;
682}
683
684static int crypt_iv_tcw_wipe(struct crypt_config *cc)
685{
686 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
687
688 memset(tcw->iv_seed, 0, cc->iv_size);
689 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
690
691 return 0;
692}
693
694static int crypt_iv_tcw_whitening(struct crypt_config *cc,
695 struct dm_crypt_request *dmreq,
696 u8 *data)
697{
698 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
699 u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
700 u8 buf[TCW_WHITENING_SIZE];
701 struct {
702 struct shash_desc desc;
703 char ctx[crypto_shash_descsize(tcw->crc32_tfm)];
704 } sdesc;
705 int i, r;
706
707 /* xor whitening with sector number */
708 memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
709 crypto_xor(buf, (u8 *)&sector, 8);
710 crypto_xor(&buf[8], (u8 *)&sector, 8);
711
712 /* calculate crc32 for every 32bit part and xor it */
713 sdesc.desc.tfm = tcw->crc32_tfm;
714 sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
715 for (i = 0; i < 4; i++) {
716 r = crypto_shash_init(&sdesc.desc);
717 if (r)
718 goto out;
719 r = crypto_shash_update(&sdesc.desc, &buf[i * 4], 4);
720 if (r)
721 goto out;
722 r = crypto_shash_final(&sdesc.desc, &buf[i * 4]);
723 if (r)
724 goto out;
725 }
726 crypto_xor(&buf[0], &buf[12], 4);
727 crypto_xor(&buf[4], &buf[8], 4);
728
729 /* apply whitening (8 bytes) to whole sector */
730 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
731 crypto_xor(data + i * 8, buf, 8);
732out:
733 memset(buf, 0, sizeof(buf));
734 return r;
735}
736
737static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
738 struct dm_crypt_request *dmreq)
739{
740 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
741 u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
742 u8 *src;
743 int r = 0;
744
745 /* Remove whitening from ciphertext */
746 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
747 src = kmap_atomic(sg_page(&dmreq->sg_in));
748 r = crypt_iv_tcw_whitening(cc, dmreq, src + dmreq->sg_in.offset);
749 kunmap_atomic(src);
750 }
751
752 /* Calculate IV */
753 memcpy(iv, tcw->iv_seed, cc->iv_size);
754 crypto_xor(iv, (u8 *)&sector, 8);
755 if (cc->iv_size > 8)
756 crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
757
758 return r;
759}
760
761static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
762 struct dm_crypt_request *dmreq)
763{
764 u8 *dst;
765 int r;
766
767 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
768 return 0;
769
770 /* Apply whitening on ciphertext */
771 dst = kmap_atomic(sg_page(&dmreq->sg_out));
772 r = crypt_iv_tcw_whitening(cc, dmreq, dst + dmreq->sg_out.offset);
773 kunmap_atomic(dst);
774
775 return r;
776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778static struct crypt_iv_operations crypt_iv_plain_ops = {
779 .generator = crypt_iv_plain_gen
780};
781
Milan Broz61afef62009-12-10 23:52:25 +0000782static struct crypt_iv_operations crypt_iv_plain64_ops = {
783 .generator = crypt_iv_plain64_gen
784};
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786static struct crypt_iv_operations crypt_iv_essiv_ops = {
787 .ctr = crypt_iv_essiv_ctr,
788 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000789 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000790 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 .generator = crypt_iv_essiv_gen
792};
793
Rik Snel48527fa2006-09-03 08:56:39 +1000794static struct crypt_iv_operations crypt_iv_benbi_ops = {
795 .ctr = crypt_iv_benbi_ctr,
796 .dtr = crypt_iv_benbi_dtr,
797 .generator = crypt_iv_benbi_gen
798};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Ludwig Nussel46b47732007-05-09 02:32:55 -0700800static struct crypt_iv_operations crypt_iv_null_ops = {
801 .generator = crypt_iv_null_gen
802};
803
Milan Broz34745782011-01-13 19:59:55 +0000804static struct crypt_iv_operations crypt_iv_lmk_ops = {
805 .ctr = crypt_iv_lmk_ctr,
806 .dtr = crypt_iv_lmk_dtr,
807 .init = crypt_iv_lmk_init,
808 .wipe = crypt_iv_lmk_wipe,
809 .generator = crypt_iv_lmk_gen,
810 .post = crypt_iv_lmk_post
811};
812
Milan Brozed04d982013-10-28 23:21:04 +0100813static struct crypt_iv_operations crypt_iv_tcw_ops = {
814 .ctr = crypt_iv_tcw_ctr,
815 .dtr = crypt_iv_tcw_dtr,
816 .init = crypt_iv_tcw_init,
817 .wipe = crypt_iv_tcw_wipe,
818 .generator = crypt_iv_tcw_gen,
819 .post = crypt_iv_tcw_post
820};
821
Milan Brozd469f842007-10-19 22:42:37 +0100822static void crypt_convert_init(struct crypt_config *cc,
823 struct convert_context *ctx,
824 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000825 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 ctx->bio_in = bio_in;
828 ctx->bio_out = bio_out;
829 ctx->offset_in = 0;
830 ctx->offset_out = 0;
831 ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
832 ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100833 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000834 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Huang Yingb2174ee2009-03-16 17:44:33 +0000837static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
838 struct ablkcipher_request *req)
839{
840 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
841}
842
843static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
844 struct dm_crypt_request *dmreq)
845{
846 return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
847}
848
Milan Broz2dc53272011-01-13 19:59:54 +0000849static u8 *iv_of_dmreq(struct crypt_config *cc,
850 struct dm_crypt_request *dmreq)
851{
852 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
853 crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
854}
855
Milan Broz01482b72008-02-08 02:11:04 +0000856static int crypt_convert_block(struct crypt_config *cc,
Milan Broz3a7f6c92008-02-08 02:11:14 +0000857 struct convert_context *ctx,
858 struct ablkcipher_request *req)
Milan Broz01482b72008-02-08 02:11:04 +0000859{
860 struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
861 struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000862 struct dm_crypt_request *dmreq;
863 u8 *iv;
Mikulas Patocka40b62292012-07-27 15:08:04 +0100864 int r;
Milan Broz01482b72008-02-08 02:11:04 +0000865
Huang Yingb2174ee2009-03-16 17:44:33 +0000866 dmreq = dmreq_of_req(cc, req);
Milan Broz2dc53272011-01-13 19:59:54 +0000867 iv = iv_of_dmreq(cc, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000868
Mikulas Patockac66029f2012-07-27 15:08:05 +0100869 dmreq->iv_sector = ctx->cc_sector;
Huang Yingb2174ee2009-03-16 17:44:33 +0000870 dmreq->ctx = ctx;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000871 sg_init_table(&dmreq->sg_in, 1);
872 sg_set_page(&dmreq->sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT,
Milan Broz01482b72008-02-08 02:11:04 +0000873 bv_in->bv_offset + ctx->offset_in);
874
Milan Broz3a7f6c92008-02-08 02:11:14 +0000875 sg_init_table(&dmreq->sg_out, 1);
876 sg_set_page(&dmreq->sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT,
Milan Broz01482b72008-02-08 02:11:04 +0000877 bv_out->bv_offset + ctx->offset_out);
878
879 ctx->offset_in += 1 << SECTOR_SHIFT;
880 if (ctx->offset_in >= bv_in->bv_len) {
881 ctx->offset_in = 0;
882 ctx->idx_in++;
883 }
884
885 ctx->offset_out += 1 << SECTOR_SHIFT;
886 if (ctx->offset_out >= bv_out->bv_len) {
887 ctx->offset_out = 0;
888 ctx->idx_out++;
889 }
890
Milan Broz3a7f6c92008-02-08 02:11:14 +0000891 if (cc->iv_gen_ops) {
Milan Broz2dc53272011-01-13 19:59:54 +0000892 r = cc->iv_gen_ops->generator(cc, iv, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000893 if (r < 0)
894 return r;
895 }
896
897 ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
898 1 << SECTOR_SHIFT, iv);
899
900 if (bio_data_dir(ctx->bio_in) == WRITE)
901 r = crypto_ablkcipher_encrypt(req);
902 else
903 r = crypto_ablkcipher_decrypt(req);
904
Milan Broz2dc53272011-01-13 19:59:54 +0000905 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
906 r = cc->iv_gen_ops->post(cc, iv, dmreq);
907
Milan Broz3a7f6c92008-02-08 02:11:14 +0000908 return r;
Milan Broz01482b72008-02-08 02:11:04 +0000909}
910
Milan Broz95497a92008-02-08 02:11:12 +0000911static void kcryptd_async_done(struct crypto_async_request *async_req,
912 int error);
Andi Kleenc0297722011-01-13 19:59:53 +0000913
Milan Brozddd42ed2008-02-08 02:11:07 +0000914static void crypt_alloc_req(struct crypt_config *cc,
915 struct convert_context *ctx)
916{
Andi Kleenc0297722011-01-13 19:59:53 +0000917 struct crypt_cpu *this_cc = this_crypt_config(cc);
Mikulas Patockac66029f2012-07-27 15:08:05 +0100918 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +0000919
920 if (!this_cc->req)
921 this_cc->req = mempool_alloc(cc->req_pool, GFP_NOIO);
922
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100923 ablkcipher_request_set_tfm(this_cc->req, cc->tfms[key_index]);
Andi Kleenc0297722011-01-13 19:59:53 +0000924 ablkcipher_request_set_callback(this_cc->req,
925 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
926 kcryptd_async_done, dmreq_of_req(cc, this_cc->req));
Milan Brozddd42ed2008-02-08 02:11:07 +0000927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929/*
930 * Encrypt / decrypt data from one bio to another one (can be the same one)
931 */
932static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +0100933 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Andi Kleenc0297722011-01-13 19:59:53 +0000935 struct crypt_cpu *this_cc = this_crypt_config(cc);
Milan Broz3f1e9072008-03-28 14:16:07 -0700936 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Mikulas Patocka40b62292012-07-27 15:08:04 +0100938 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +0100939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
941 ctx->idx_out < ctx->bio_out->bi_vcnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Milan Broz3a7f6c92008-02-08 02:11:14 +0000943 crypt_alloc_req(cc, ctx);
944
Mikulas Patocka40b62292012-07-27 15:08:04 +0100945 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700946
Andi Kleenc0297722011-01-13 19:59:53 +0000947 r = crypt_convert_block(cc, ctx, this_cc->req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000948
949 switch (r) {
Milan Broz3f1e9072008-03-28 14:16:07 -0700950 /* async */
Milan Broz3a7f6c92008-02-08 02:11:14 +0000951 case -EBUSY:
952 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -0800953 reinit_completion(&ctx->restart);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000954 /* fall through*/
955 case -EINPROGRESS:
Andi Kleenc0297722011-01-13 19:59:53 +0000956 this_cc->req = NULL;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100957 ctx->cc_sector++;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000958 continue;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000959
Milan Broz3f1e9072008-03-28 14:16:07 -0700960 /* sync */
961 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +0100962 atomic_dec(&ctx->cc_pending);
Mikulas Patockac66029f2012-07-27 15:08:05 +0100963 ctx->cc_sector++;
Milan Brozc7f1b202008-07-02 09:34:28 +0100964 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -0700965 continue;
966
967 /* error */
968 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +0100969 atomic_dec(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700970 return r;
971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973
Milan Broz3f1e9072008-03-28 14:16:07 -0700974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977/*
978 * Generate a new unfragmented bio with the given size
979 * This should never violate the device limitations
Milan Broz933f01d2008-10-10 13:37:08 +0100980 * May return a smaller bio when running out of pages, indicated by
981 * *out_of_pages set to 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 */
Milan Broz933f01d2008-10-10 13:37:08 +0100983static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
984 unsigned *out_of_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +0100986 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -0700987 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Virob4e3ca12005-10-21 03:22:34 -0400989 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
Milan Broz91e10622007-12-13 14:16:10 +0000990 unsigned i, len;
991 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Olaf Kirch2f9941b2007-05-09 02:32:53 -0700993 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -0700994 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Olaf Kirch027581f2007-05-09 02:32:52 -0700997 clone_init(io, clone);
Milan Broz933f01d2008-10-10 13:37:08 +0100998 *out_of_pages = 0;
Milan Broz6a24c712006-10-03 01:15:40 -0700999
Olaf Kirchf97380b2007-05-09 02:32:54 -07001000 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +00001001 page = mempool_alloc(cc->page_pool, gfp_mask);
Milan Broz933f01d2008-10-10 13:37:08 +01001002 if (!page) {
1003 *out_of_pages = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 break;
Milan Broz933f01d2008-10-10 13:37:08 +01001005 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 /*
Mikulas Patockaaeb2dea2012-03-28 18:41:22 +01001008 * If additional pages cannot be allocated without waiting,
1009 * return a partially-allocated bio. The caller will then try
1010 * to allocate more bios while submitting this partial bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 */
Mikulas Patockaaeb2dea2012-03-28 18:41:22 +01001012 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Milan Broz91e10622007-12-13 14:16:10 +00001014 len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Milan Broz91e10622007-12-13 14:16:10 +00001016 if (!bio_add_page(clone, page, len, 0)) {
1017 mempool_free(page, cc->page_pool);
1018 break;
1019 }
1020
1021 size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023
Milan Broz8b004452006-10-03 01:15:37 -07001024 if (!clone->bi_size) {
1025 bio_put(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return NULL;
1027 }
1028
Milan Broz8b004452006-10-03 01:15:37 -07001029 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
Neil Brown644bd2f2007-10-16 13:48:46 +02001032static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Neil Brown644bd2f2007-10-16 13:48:46 +02001034 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct bio_vec *bv;
1036
Kent Overstreetcb34e052012-09-05 15:22:02 -07001037 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 BUG_ON(!bv->bv_page);
1039 mempool_free(bv->bv_page, cc->page_pool);
1040 bv->bv_page = NULL;
1041 }
1042}
1043
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001044static struct dm_crypt_io *crypt_io_alloc(struct crypt_config *cc,
Milan Brozdc440d1e2008-10-10 13:37:03 +01001045 struct bio *bio, sector_t sector)
1046{
Milan Brozdc440d1e2008-10-10 13:37:03 +01001047 struct dm_crypt_io *io;
1048
1049 io = mempool_alloc(cc->io_pool, GFP_NOIO);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001050 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001051 io->base_bio = bio;
1052 io->sector = sector;
1053 io->error = 0;
Milan Broz393b47e2008-10-21 17:45:02 +01001054 io->base_io = NULL;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001055 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001056
1057 return io;
1058}
1059
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001060static void crypt_inc_pending(struct dm_crypt_io *io)
1061{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001062 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001063}
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/*
1066 * One of the bios was finished. Check for completion of
1067 * the whole request and correctly clean up the buffer.
Milan Broz393b47e2008-10-21 17:45:02 +01001068 * If base_io is set, wait for the last fragment to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Milan Broz5742fd72008-02-08 02:10:43 +00001070static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001072 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001073 struct bio *base_bio = io->base_bio;
1074 struct dm_crypt_io *base_io = io->base_io;
1075 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Mikulas Patocka40b62292012-07-27 15:08:04 +01001077 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return;
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 mempool_free(io, cc->io_pool);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001081
1082 if (likely(!base_io))
1083 bio_endio(base_bio, error);
1084 else {
1085 if (error && !base_io->error)
1086 base_io->error = error;
1087 crypt_dec_pending(base_io);
1088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001092 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 *
1094 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001095 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001096 *
1097 * kcryptd performs the actual encryption or decryption.
1098 *
1099 * kcryptd_io performs the IO submission.
1100 *
1101 * They must be separated as otherwise the final stages could be
1102 * starved by new requests which can block in the first stages due
1103 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001104 *
1105 * The work is done per CPU global for all dm-crypt instances.
1106 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 */
NeilBrown6712ecf2007-09-27 12:47:43 +02001108static void crypt_endio(struct bio *clone, int error)
Milan Broz8b004452006-10-03 01:15:37 -07001109{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001110 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001111 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001112 unsigned rw = bio_data_dir(clone);
Milan Broz8b004452006-10-03 01:15:37 -07001113
Milan Brozadfe4772007-12-13 14:15:51 +00001114 if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
1115 error = -EIO;
1116
Milan Broz8b004452006-10-03 01:15:37 -07001117 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001118 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001119 */
Milan Brozee7a4912008-02-08 02:10:46 +00001120 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001121 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001122
1123 bio_put(clone);
1124
1125 if (rw == READ && !error) {
1126 kcryptd_queue_crypt(io);
1127 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001128 }
Milan Broz8b004452006-10-03 01:15:37 -07001129
Milan Brozadfe4772007-12-13 14:15:51 +00001130 if (unlikely(error))
Milan Broz5742fd72008-02-08 02:10:43 +00001131 io->error = error;
1132
1133 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001134}
1135
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001136static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001137{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001138 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001139
1140 clone->bi_private = io;
1141 clone->bi_end_io = crypt_endio;
1142 clone->bi_bdev = cc->dev->bdev;
1143 clone->bi_rw = io->base_bio->bi_rw;
1144}
1145
Milan Broz20c82532011-01-13 19:59:53 +00001146static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001147{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001148 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001149 struct bio *base_bio = io->base_bio;
1150 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001151
Milan Broz8b004452006-10-03 01:15:37 -07001152 /*
1153 * The block layer might modify the bvec array, so always
1154 * copy the required bvecs because we need the original
1155 * one in order to decrypt the whole bio data *afterwards*.
1156 */
Kent Overstreetbf800ef2012-09-06 15:35:02 -07001157 clone = bio_clone_bioset(base_bio, gfp, cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001158 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001159 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001160
Milan Broz20c82532011-01-13 19:59:53 +00001161 crypt_inc_pending(io);
1162
Milan Broz8b004452006-10-03 01:15:37 -07001163 clone_init(io, clone);
Milan Broz0c395b02008-02-08 02:10:54 +00001164 clone->bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001165
Milan Broz93e605c2006-10-03 01:15:38 -07001166 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001167 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001168}
1169
Milan Broz4e4eef62008-02-08 02:10:49 +00001170static void kcryptd_io_write(struct dm_crypt_io *io)
1171{
Milan Broz95497a92008-02-08 02:11:12 +00001172 struct bio *clone = io->ctx.bio_out;
Milan Broz95497a92008-02-08 02:11:12 +00001173 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +00001174}
1175
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001176static void kcryptd_io(struct work_struct *work)
1177{
1178 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1179
Milan Broz20c82532011-01-13 19:59:53 +00001180 if (bio_data_dir(io->base_bio) == READ) {
1181 crypt_inc_pending(io);
1182 if (kcryptd_io_read(io, GFP_NOIO))
1183 io->error = -ENOMEM;
1184 crypt_dec_pending(io);
1185 } else
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001186 kcryptd_io_write(io);
1187}
1188
1189static void kcryptd_queue_io(struct dm_crypt_io *io)
1190{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001191 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001192
1193 INIT_WORK(&io->work, kcryptd_io);
1194 queue_work(cc->io_queue, &io->work);
1195}
1196
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001197static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001198{
Milan Brozdec1ced2008-02-08 02:10:57 +00001199 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001200 struct crypt_config *cc = io->cc;
Milan Brozdec1ced2008-02-08 02:10:57 +00001201
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001202 if (unlikely(io->error < 0)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001203 crypt_free_buffer_pages(cc, clone);
1204 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001205 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001206 return;
1207 }
1208
1209 /* crypt_convert should have filled the clone bio */
1210 BUG_ON(io->ctx.idx_out < clone->bi_vcnt);
1211
1212 clone->bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001213
Milan Broz95497a92008-02-08 02:11:12 +00001214 if (async)
1215 kcryptd_queue_io(io);
Alasdair G Kergon1e37bb82008-10-10 13:37:05 +01001216 else
Milan Broz95497a92008-02-08 02:11:12 +00001217 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +00001218}
1219
Milan Brozfc5a5e92008-10-10 13:37:04 +01001220static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001221{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001222 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001223 struct bio *clone;
Milan Broz393b47e2008-10-21 17:45:02 +01001224 struct dm_crypt_io *new_io;
Milan Brozc8081612008-10-10 13:37:08 +01001225 int crypt_finished;
Milan Broz933f01d2008-10-10 13:37:08 +01001226 unsigned out_of_pages = 0;
Milan Brozdec1ced2008-02-08 02:10:57 +00001227 unsigned remaining = io->base_bio->bi_size;
Milan Brozb635b002008-10-21 17:45:00 +01001228 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +00001229 int r;
Milan Broz8b004452006-10-03 01:15:37 -07001230
Milan Broz93e605c2006-10-03 01:15:38 -07001231 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001232 * Prevent io from disappearing until this function completes.
1233 */
1234 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001235 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001236
1237 /*
Milan Broz93e605c2006-10-03 01:15:38 -07001238 * The allocated buffers can be smaller than the whole bio,
1239 * so repeat the whole process until all the data can be handled.
1240 */
1241 while (remaining) {
Milan Broz933f01d2008-10-10 13:37:08 +01001242 clone = crypt_alloc_buffer(io, remaining, &out_of_pages);
Milan Broz23541d22006-10-03 01:15:39 -07001243 if (unlikely(!clone)) {
Milan Broz5742fd72008-02-08 02:10:43 +00001244 io->error = -ENOMEM;
Milan Brozfc5a5e92008-10-10 13:37:04 +01001245 break;
Milan Broz23541d22006-10-03 01:15:39 -07001246 }
Milan Broz93e605c2006-10-03 01:15:38 -07001247
Milan Broz53017032008-02-08 02:10:38 +00001248 io->ctx.bio_out = clone;
1249 io->ctx.idx_out = 0;
Milan Broz93e605c2006-10-03 01:15:38 -07001250
Milan Broz93e605c2006-10-03 01:15:38 -07001251 remaining -= clone->bi_size;
Milan Brozb635b002008-10-21 17:45:00 +01001252 sector += bio_sectors(clone);
Milan Brozdec1ced2008-02-08 02:10:57 +00001253
Milan Broz4e594092008-10-10 13:37:07 +01001254 crypt_inc_pending(io);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001255
Milan Brozdec1ced2008-02-08 02:10:57 +00001256 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001257 if (r < 0)
1258 io->error = -EIO;
1259
Mikulas Patocka40b62292012-07-27 15:08:04 +01001260 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
Milan Brozdec1ced2008-02-08 02:10:57 +00001261
Milan Brozc8081612008-10-10 13:37:08 +01001262 /* Encryption was already finished, submit io now */
1263 if (crypt_finished) {
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001264 kcryptd_crypt_write_io_submit(io, 0);
Milan Brozc8081612008-10-10 13:37:08 +01001265
1266 /*
1267 * If there was an error, do not try next fragments.
1268 * For async, error is processed in async handler.
1269 */
Milan Broz6c031f42008-10-10 13:37:06 +01001270 if (unlikely(r < 0))
Milan Brozfc5a5e92008-10-10 13:37:04 +01001271 break;
Milan Brozb635b002008-10-21 17:45:00 +01001272
1273 io->sector = sector;
Milan Broz4e594092008-10-10 13:37:07 +01001274 }
Milan Broz93e605c2006-10-03 01:15:38 -07001275
Milan Broz933f01d2008-10-10 13:37:08 +01001276 /*
1277 * Out of memory -> run queues
1278 * But don't wait if split was due to the io size restriction
1279 */
1280 if (unlikely(out_of_pages))
Jens Axboe8aa7e842009-07-09 14:52:32 +02001281 congestion_wait(BLK_RW_ASYNC, HZ/100);
Milan Broz933f01d2008-10-10 13:37:08 +01001282
Milan Broz393b47e2008-10-21 17:45:02 +01001283 /*
1284 * With async crypto it is unsafe to share the crypto context
1285 * between fragments, so switch to a new dm_crypt_io structure.
1286 */
1287 if (unlikely(!crypt_finished && remaining)) {
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001288 new_io = crypt_io_alloc(io->cc, io->base_bio,
Milan Broz393b47e2008-10-21 17:45:02 +01001289 sector);
1290 crypt_inc_pending(new_io);
1291 crypt_convert_init(cc, &new_io->ctx, NULL,
1292 io->base_bio, sector);
1293 new_io->ctx.idx_in = io->ctx.idx_in;
1294 new_io->ctx.offset_in = io->ctx.offset_in;
1295
1296 /*
1297 * Fragments after the first use the base_io
1298 * pending count.
1299 */
1300 if (!io->base_io)
1301 new_io->base_io = io;
1302 else {
1303 new_io->base_io = io->base_io;
1304 crypt_inc_pending(io->base_io);
1305 crypt_dec_pending(io);
1306 }
1307
1308 io = new_io;
1309 }
Milan Broz8b004452006-10-03 01:15:37 -07001310 }
Milan Broz899c95d2008-02-08 02:11:02 +00001311
1312 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001313}
1314
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001315static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001316{
Milan Broz5742fd72008-02-08 02:10:43 +00001317 crypt_dec_pending(io);
1318}
1319
Milan Broz4e4eef62008-02-08 02:10:49 +00001320static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001321{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001322 struct crypt_config *cc = io->cc;
Milan Broz5742fd72008-02-08 02:10:43 +00001323 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001324
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001325 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001326
Milan Broz53017032008-02-08 02:10:38 +00001327 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001328 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001329
Milan Broz5742fd72008-02-08 02:10:43 +00001330 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001331 if (r < 0)
1332 io->error = -EIO;
Milan Broz5742fd72008-02-08 02:10:43 +00001333
Mikulas Patocka40b62292012-07-27 15:08:04 +01001334 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001335 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001336
1337 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001338}
1339
Milan Broz95497a92008-02-08 02:11:12 +00001340static void kcryptd_async_done(struct crypto_async_request *async_req,
1341 int error)
1342{
Huang Yingb2174ee2009-03-16 17:44:33 +00001343 struct dm_crypt_request *dmreq = async_req->data;
1344 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001345 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001346 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001347
1348 if (error == -EINPROGRESS) {
1349 complete(&ctx->restart);
1350 return;
1351 }
1352
Milan Broz2dc53272011-01-13 19:59:54 +00001353 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
1354 error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
1355
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001356 if (error < 0)
1357 io->error = -EIO;
1358
Huang Yingb2174ee2009-03-16 17:44:33 +00001359 mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool);
Milan Broz95497a92008-02-08 02:11:12 +00001360
Mikulas Patocka40b62292012-07-27 15:08:04 +01001361 if (!atomic_dec_and_test(&ctx->cc_pending))
Milan Broz95497a92008-02-08 02:11:12 +00001362 return;
1363
1364 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001365 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001366 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001367 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001368}
1369
Milan Broz4e4eef62008-02-08 02:10:49 +00001370static void kcryptd_crypt(struct work_struct *work)
1371{
1372 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1373
1374 if (bio_data_dir(io->base_bio) == READ)
1375 kcryptd_crypt_read_convert(io);
1376 else
1377 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001378}
1379
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001380static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1381{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001382 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001383
1384 INIT_WORK(&io->work, kcryptd_crypt);
1385 queue_work(cc->crypt_queue, &io->work);
1386}
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388/*
1389 * Decode key from its hex representation
1390 */
1391static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1392{
1393 char buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 unsigned int i;
1395
1396 buffer[2] = '\0';
1397
Milan Broz8b004452006-10-03 01:15:37 -07001398 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 buffer[0] = *hex++;
1400 buffer[1] = *hex++;
1401
majianpeng1a66a082012-07-27 15:07:59 +01001402 if (kstrtou8(buffer, 16, &key[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 return -EINVAL;
1404 }
1405
1406 if (*hex != '\0')
1407 return -EINVAL;
1408
1409 return 0;
1410}
1411
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001412static void crypt_free_tfms(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001413{
Milan Brozd1f96422011-01-13 19:59:54 +00001414 unsigned i;
1415
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001416 if (!cc->tfms)
1417 return;
1418
Milan Brozd1f96422011-01-13 19:59:54 +00001419 for (i = 0; i < cc->tfms_count; i++)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001420 if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) {
1421 crypto_free_ablkcipher(cc->tfms[i]);
1422 cc->tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001423 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001424
1425 kfree(cc->tfms);
1426 cc->tfms = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001427}
1428
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001429static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001430{
Milan Brozd1f96422011-01-13 19:59:54 +00001431 unsigned i;
1432 int err;
1433
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001434 cc->tfms = kmalloc(cc->tfms_count * sizeof(struct crypto_ablkcipher *),
1435 GFP_KERNEL);
1436 if (!cc->tfms)
1437 return -ENOMEM;
1438
Milan Brozd1f96422011-01-13 19:59:54 +00001439 for (i = 0; i < cc->tfms_count; i++) {
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001440 cc->tfms[i] = crypto_alloc_ablkcipher(ciphermode, 0, 0);
1441 if (IS_ERR(cc->tfms[i])) {
1442 err = PTR_ERR(cc->tfms[i]);
1443 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001444 return err;
1445 }
1446 }
1447
1448 return 0;
1449}
1450
Andi Kleenc0297722011-01-13 19:59:53 +00001451static int crypt_setkey_allcpus(struct crypt_config *cc)
1452{
Milan Brozda31a072013-10-28 23:21:03 +01001453 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001454 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001455
Milan Brozda31a072013-10-28 23:21:03 +01001456 /* Ignore extra keys (which are used for IV etc) */
1457 subkey_size = (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1458
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001459 for (i = 0; i < cc->tfms_count; i++) {
1460 r = crypto_ablkcipher_setkey(cc->tfms[i],
1461 cc->key + (i * subkey_size),
1462 subkey_size);
1463 if (r)
1464 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001465 }
1466
1467 return err;
1468}
1469
Milan Broze48d4bb2006-10-03 01:15:37 -07001470static int crypt_set_key(struct crypt_config *cc, char *key)
1471{
Milan Brozde8be5a2011-03-24 13:54:27 +00001472 int r = -EINVAL;
1473 int key_string_len = strlen(key);
1474
Milan Broz69a8cfc2011-01-13 19:59:49 +00001475 /* The key size may not be changed. */
Milan Brozde8be5a2011-03-24 13:54:27 +00001476 if (cc->key_size != (key_string_len >> 1))
1477 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001478
Milan Broz69a8cfc2011-01-13 19:59:49 +00001479 /* Hyphen (which gives a key_size of zero) means there is no key. */
1480 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00001481 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001482
Milan Broz69a8cfc2011-01-13 19:59:49 +00001483 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00001484 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001485
1486 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1487
Milan Brozde8be5a2011-03-24 13:54:27 +00001488 r = crypt_setkey_allcpus(cc);
1489
1490out:
1491 /* Hex key string not needed after here, so wipe it. */
1492 memset(key, '0', key_string_len);
1493
1494 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07001495}
1496
1497static int crypt_wipe_key(struct crypt_config *cc)
1498{
1499 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1500 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00001501
1502 return crypt_setkey_allcpus(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07001503}
1504
Milan Broz28513fc2010-08-12 04:14:06 +01001505static void crypt_dtr(struct dm_target *ti)
1506{
1507 struct crypt_config *cc = ti->private;
Andi Kleenc0297722011-01-13 19:59:53 +00001508 struct crypt_cpu *cpu_cc;
1509 int cpu;
Milan Broz28513fc2010-08-12 04:14:06 +01001510
1511 ti->private = NULL;
1512
1513 if (!cc)
1514 return;
1515
1516 if (cc->io_queue)
1517 destroy_workqueue(cc->io_queue);
1518 if (cc->crypt_queue)
1519 destroy_workqueue(cc->crypt_queue);
1520
Andi Kleenc0297722011-01-13 19:59:53 +00001521 if (cc->cpu)
1522 for_each_possible_cpu(cpu) {
1523 cpu_cc = per_cpu_ptr(cc->cpu, cpu);
1524 if (cpu_cc->req)
1525 mempool_free(cpu_cc->req, cc->req_pool);
Andi Kleenc0297722011-01-13 19:59:53 +00001526 }
1527
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001528 crypt_free_tfms(cc);
1529
Milan Broz28513fc2010-08-12 04:14:06 +01001530 if (cc->bs)
1531 bioset_free(cc->bs);
1532
1533 if (cc->page_pool)
1534 mempool_destroy(cc->page_pool);
1535 if (cc->req_pool)
1536 mempool_destroy(cc->req_pool);
1537 if (cc->io_pool)
1538 mempool_destroy(cc->io_pool);
1539
1540 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1541 cc->iv_gen_ops->dtr(cc);
1542
Milan Broz28513fc2010-08-12 04:14:06 +01001543 if (cc->dev)
1544 dm_put_device(ti, cc->dev);
1545
Andi Kleenc0297722011-01-13 19:59:53 +00001546 if (cc->cpu)
1547 free_percpu(cc->cpu);
1548
Milan Broz5ebaee62010-08-12 04:14:07 +01001549 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00001550 kzfree(cc->cipher_string);
Milan Broz28513fc2010-08-12 04:14:06 +01001551
1552 /* Must zero key material before freeing */
1553 kzfree(cc);
1554}
1555
Milan Broz5ebaee62010-08-12 04:14:07 +01001556static int crypt_ctr_cipher(struct dm_target *ti,
1557 char *cipher_in, char *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Milan Broz5ebaee62010-08-12 04:14:07 +01001559 struct crypt_config *cc = ti->private;
Milan Brozd1f96422011-01-13 19:59:54 +00001560 char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01001561 char *cipher_api = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001562 int ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001563 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Milan Broz5ebaee62010-08-12 04:14:07 +01001565 /* Convert to crypto api definition? */
1566 if (strchr(cipher_in, '(')) {
1567 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return -EINVAL;
1569 }
1570
Milan Broz7dbcd132011-01-13 19:59:52 +00001571 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
1572 if (!cc->cipher_string)
1573 goto bad_mem;
1574
Milan Broz5ebaee62010-08-12 04:14:07 +01001575 /*
1576 * Legacy dm-crypt cipher specification
Milan Brozd1f96422011-01-13 19:59:54 +00001577 * cipher[:keycount]-mode-iv:ivopts
Milan Broz5ebaee62010-08-12 04:14:07 +01001578 */
1579 tmp = cipher_in;
Milan Brozd1f96422011-01-13 19:59:54 +00001580 keycount = strsep(&tmp, "-");
1581 cipher = strsep(&keycount, ":");
1582
1583 if (!keycount)
1584 cc->tfms_count = 1;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001585 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
Milan Brozd1f96422011-01-13 19:59:54 +00001586 !is_power_of_2(cc->tfms_count)) {
1587 ti->error = "Bad cipher key count specification";
1588 return -EINVAL;
1589 }
1590 cc->key_parts = cc->tfms_count;
Milan Brozda31a072013-10-28 23:21:03 +01001591 cc->key_extra_size = 0;
Milan Broz5ebaee62010-08-12 04:14:07 +01001592
1593 cc->cipher = kstrdup(cipher, GFP_KERNEL);
1594 if (!cc->cipher)
1595 goto bad_mem;
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 chainmode = strsep(&tmp, "-");
1598 ivopts = strsep(&tmp, "-");
1599 ivmode = strsep(&ivopts, ":");
1600
1601 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01001602 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001604 cc->cpu = __alloc_percpu(sizeof(*(cc->cpu)),
Milan Brozd1f96422011-01-13 19:59:54 +00001605 __alignof__(struct crypt_cpu));
Andi Kleenc0297722011-01-13 19:59:53 +00001606 if (!cc->cpu) {
1607 ti->error = "Cannot allocate per cpu state";
1608 goto bad_mem;
1609 }
1610
Milan Broz7dbcd132011-01-13 19:59:52 +00001611 /*
1612 * For compatibility with the original dm-crypt mapping format, if
1613 * only the cipher name is supplied, use cbc-plain.
1614 */
Milan Broz5ebaee62010-08-12 04:14:07 +01001615 if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 chainmode = "cbc";
1617 ivmode = "plain";
1618 }
1619
Herbert Xud1806f62006-08-22 20:29:17 +10001620 if (strcmp(chainmode, "ecb") && !ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001621 ti->error = "IV mechanism required";
1622 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624
Milan Broz5ebaee62010-08-12 04:14:07 +01001625 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
1626 if (!cipher_api)
1627 goto bad_mem;
1628
1629 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
1630 "%s(%s)", chainmode, cipher);
1631 if (ret < 0) {
1632 kfree(cipher_api);
1633 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10001634 }
1635
Milan Broz5ebaee62010-08-12 04:14:07 +01001636 /* Allocate cipher */
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001637 ret = crypt_alloc_tfms(cc, cipher_api);
1638 if (ret < 0) {
1639 ti->error = "Error allocating crypto tfm";
1640 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Milan Broz5ebaee62010-08-12 04:14:07 +01001643 /* Initialize IV */
Andi Kleenc0297722011-01-13 19:59:53 +00001644 cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
Milan Broz5ebaee62010-08-12 04:14:07 +01001645 if (cc->iv_size)
1646 /* at least a 64 bit sector number should fit in our buffer */
1647 cc->iv_size = max(cc->iv_size,
1648 (unsigned int)(sizeof(u64) / sizeof(u8)));
1649 else if (ivmode) {
1650 DMWARN("Selected cipher does not support IVs");
1651 ivmode = NULL;
1652 }
1653
1654 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (ivmode == NULL)
1656 cc->iv_gen_ops = NULL;
1657 else if (strcmp(ivmode, "plain") == 0)
1658 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00001659 else if (strcmp(ivmode, "plain64") == 0)
1660 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 else if (strcmp(ivmode, "essiv") == 0)
1662 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10001663 else if (strcmp(ivmode, "benbi") == 0)
1664 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07001665 else if (strcmp(ivmode, "null") == 0)
1666 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00001667 else if (strcmp(ivmode, "lmk") == 0) {
1668 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01001669 /*
1670 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00001671 * to length of provided multi-key string.
1672 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01001673 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00001674 */
Milan Brozda31a072013-10-28 23:21:03 +01001675 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00001676 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01001677 cc->key_extra_size = cc->key_size / cc->key_parts;
1678 }
Milan Brozed04d982013-10-28 23:21:04 +01001679 } else if (strcmp(ivmode, "tcw") == 0) {
1680 cc->iv_gen_ops = &crypt_iv_tcw_ops;
1681 cc->key_parts += 2; /* IV + whitening */
1682 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broz34745782011-01-13 19:59:55 +00001683 } else {
Milan Broz5ebaee62010-08-12 04:14:07 +01001684 ret = -EINVAL;
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001685 ti->error = "Invalid IV mode";
Milan Broz28513fc2010-08-12 04:14:06 +01001686 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688
Milan Brozda31a072013-10-28 23:21:03 +01001689 /* Initialize and set key */
1690 ret = crypt_set_key(cc, key);
1691 if (ret < 0) {
1692 ti->error = "Error decoding and setting key";
1693 goto bad;
1694 }
1695
Milan Broz28513fc2010-08-12 04:14:06 +01001696 /* Allocate IV */
1697 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
1698 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
1699 if (ret < 0) {
1700 ti->error = "Error creating IV";
1701 goto bad;
1702 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00001703 }
1704
Milan Broz28513fc2010-08-12 04:14:06 +01001705 /* Initialize IV (set keys for ESSIV etc) */
1706 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
1707 ret = cc->iv_gen_ops->init(cc);
1708 if (ret < 0) {
1709 ti->error = "Error initialising IV";
1710 goto bad;
1711 }
1712 }
1713
Milan Broz5ebaee62010-08-12 04:14:07 +01001714 ret = 0;
1715bad:
1716 kfree(cipher_api);
1717 return ret;
1718
1719bad_mem:
1720 ti->error = "Cannot allocate cipher strings";
1721 return -ENOMEM;
1722}
1723
1724/*
1725 * Construct an encryption mapping:
1726 * <cipher> <key> <iv_offset> <dev_path> <start>
1727 */
1728static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1729{
1730 struct crypt_config *cc;
Milan Broz772ae5f2011-08-02 12:32:08 +01001731 unsigned int key_size, opt_params;
Milan Broz5ebaee62010-08-12 04:14:07 +01001732 unsigned long long tmpll;
1733 int ret;
Milan Broz772ae5f2011-08-02 12:32:08 +01001734 struct dm_arg_set as;
1735 const char *opt_string;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001736 char dummy;
Milan Broz5ebaee62010-08-12 04:14:07 +01001737
Milan Broz772ae5f2011-08-02 12:32:08 +01001738 static struct dm_arg _args[] = {
1739 {0, 1, "Invalid number of feature args"},
1740 };
1741
1742 if (argc < 5) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001743 ti->error = "Not enough arguments";
1744 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746
Milan Broz5ebaee62010-08-12 04:14:07 +01001747 key_size = strlen(argv[1]) >> 1;
1748
1749 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1750 if (!cc) {
1751 ti->error = "Cannot allocate encryption context";
1752 return -ENOMEM;
1753 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00001754 cc->key_size = key_size;
Milan Broz5ebaee62010-08-12 04:14:07 +01001755
1756 ti->private = cc;
1757 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
1758 if (ret < 0)
1759 goto bad;
1760
Milan Broz28513fc2010-08-12 04:14:06 +01001761 ret = -ENOMEM;
Matthew Dobson93d23412006-03-26 01:37:50 -08001762 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 if (!cc->io_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001764 ti->error = "Cannot allocate crypt io mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001765 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
1767
Milan Brozddd42ed2008-02-08 02:11:07 +00001768 cc->dmreq_start = sizeof(struct ablkcipher_request);
Andi Kleenc0297722011-01-13 19:59:53 +00001769 cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
Milan Brozddd42ed2008-02-08 02:11:07 +00001770 cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
Andi Kleenc0297722011-01-13 19:59:53 +00001771 cc->dmreq_start += crypto_ablkcipher_alignmask(any_tfm(cc)) &
Milan Broz3a7f6c92008-02-08 02:11:14 +00001772 ~(crypto_tfm_ctx_alignment() - 1);
Milan Brozddd42ed2008-02-08 02:11:07 +00001773
1774 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
1775 sizeof(struct dm_crypt_request) + cc->iv_size);
1776 if (!cc->req_pool) {
1777 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001778 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00001779 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001780
Matthew Dobsona19b27c2006-03-26 01:37:45 -08001781 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001783 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001784 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 }
1786
Jens Axboebb799ca2008-12-10 15:35:05 +01001787 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07001788 if (!cc->bs) {
1789 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01001790 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07001791 }
1792
Milan Broz28513fc2010-08-12 04:14:06 +01001793 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001794 if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001795 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001796 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001798 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Milan Broz28513fc2010-08-12 04:14:06 +01001800 if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
1801 ti->error = "Device lookup failed";
1802 goto bad;
1803 }
1804
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001805 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001806 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001807 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001809 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Milan Broz772ae5f2011-08-02 12:32:08 +01001811 argv += 5;
1812 argc -= 5;
1813
1814 /* Optional parameters */
1815 if (argc) {
1816 as.argc = argc;
1817 as.argv = argv;
1818
1819 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
1820 if (ret)
1821 goto bad;
1822
1823 opt_string = dm_shift_arg(&as);
1824
1825 if (opt_params == 1 && opt_string &&
1826 !strcasecmp(opt_string, "allow_discards"))
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001827 ti->num_discard_bios = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01001828 else if (opt_params) {
1829 ret = -EINVAL;
1830 ti->error = "Invalid feature arguments";
1831 goto bad;
1832 }
1833 }
1834
Milan Broz28513fc2010-08-12 04:14:06 +01001835 ret = -ENOMEM;
Tejun Heo670368a2013-07-30 08:40:21 -04001836 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_MEM_RECLAIM, 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001837 if (!cc->io_queue) {
1838 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001839 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01001840 }
1841
Andi Kleenc0297722011-01-13 19:59:53 +00001842 cc->crypt_queue = alloc_workqueue("kcryptd",
Tejun Heo670368a2013-07-30 08:40:21 -04001843 WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001844 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01001845 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001846 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01001847 }
1848
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001849 ti->num_flush_bios = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01001850 ti->discard_zeroes_data_unsupported = true;
Milan Broz983c7db2011-09-25 23:26:21 +01001851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 return 0;
1853
Milan Broz28513fc2010-08-12 04:14:06 +01001854bad:
1855 crypt_dtr(ti);
1856 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001859static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001861 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001862 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001863
Milan Broz772ae5f2011-08-02 12:32:08 +01001864 /*
1865 * If bio is REQ_FLUSH or REQ_DISCARD, just bypass crypt queues.
1866 * - for REQ_FLUSH device-mapper core ensures that no IO is in-flight
1867 * - for REQ_DISCARD caller must use flush if IO ordering matters
1868 */
1869 if (unlikely(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD))) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001870 bio->bi_bdev = cc->dev->bdev;
Milan Broz772ae5f2011-08-02 12:32:08 +01001871 if (bio_sectors(bio))
1872 bio->bi_sector = cc->start + dm_target_offset(ti, bio->bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001873 return DM_MAPIO_REMAPPED;
1874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001876 io = crypt_io_alloc(cc, bio, dm_target_offset(ti, bio->bi_sector));
Milan Brozcabf08e2007-10-19 22:38:58 +01001877
Milan Broz20c82532011-01-13 19:59:53 +00001878 if (bio_data_dir(io->base_bio) == READ) {
1879 if (kcryptd_io_read(io, GFP_NOWAIT))
1880 kcryptd_queue_io(io);
1881 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01001882 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001884 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001887static void crypt_status(struct dm_target *ti, status_type_t type,
1888 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Milan Broz5ebaee62010-08-12 04:14:07 +01001890 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001891 unsigned i, sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 switch (type) {
1894 case STATUSTYPE_INFO:
1895 result[0] = '\0';
1896 break;
1897
1898 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00001899 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001901 if (cc->key_size > 0)
1902 for (i = 0; i < cc->key_size; i++)
1903 DMEMIT("%02x", cc->key[i]);
1904 else
1905 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Andrew Morton4ee218c2006-03-27 01:17:48 -08001907 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1908 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01001909
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001910 if (ti->num_discard_bios)
Milan Broz772ae5f2011-08-02 12:32:08 +01001911 DMEMIT(" 1 allow_discards");
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 break;
1914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915}
1916
Milan Broze48d4bb2006-10-03 01:15:37 -07001917static void crypt_postsuspend(struct dm_target *ti)
1918{
1919 struct crypt_config *cc = ti->private;
1920
1921 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1922}
1923
1924static int crypt_preresume(struct dm_target *ti)
1925{
1926 struct crypt_config *cc = ti->private;
1927
1928 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1929 DMERR("aborting resume - crypt key is not set.");
1930 return -EAGAIN;
1931 }
1932
1933 return 0;
1934}
1935
1936static void crypt_resume(struct dm_target *ti)
1937{
1938 struct crypt_config *cc = ti->private;
1939
1940 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1941}
1942
1943/* Message interface
1944 * key set <key>
1945 * key wipe
1946 */
1947static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1948{
1949 struct crypt_config *cc = ti->private;
Milan Broz542da312009-12-10 23:51:57 +00001950 int ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07001951
1952 if (argc < 2)
1953 goto error;
1954
Mike Snitzer498f0102011-08-02 12:32:04 +01001955 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07001956 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1957 DMWARN("not suspended during key manipulation.");
1958 return -EINVAL;
1959 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001960 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Milan Broz542da312009-12-10 23:51:57 +00001961 ret = crypt_set_key(cc, argv[2]);
1962 if (ret)
1963 return ret;
1964 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
1965 ret = cc->iv_gen_ops->init(cc);
1966 return ret;
1967 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001968 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00001969 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
1970 ret = cc->iv_gen_ops->wipe(cc);
1971 if (ret)
1972 return ret;
1973 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001974 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00001975 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001976 }
1977
1978error:
1979 DMWARN("unrecognised message received.");
1980 return -EINVAL;
1981}
1982
Milan Brozd41e26b2008-07-21 12:00:40 +01001983static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
1984 struct bio_vec *biovec, int max_size)
1985{
1986 struct crypt_config *cc = ti->private;
1987 struct request_queue *q = bdev_get_queue(cc->dev->bdev);
1988
1989 if (!q->merge_bvec_fn)
1990 return max_size;
1991
1992 bvm->bi_bdev = cc->dev->bdev;
Alasdair G Kergonb441a2622010-08-12 04:14:11 +01001993 bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
Milan Brozd41e26b2008-07-21 12:00:40 +01001994
1995 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
1996}
1997
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001998static int crypt_iterate_devices(struct dm_target *ti,
1999 iterate_devices_callout_fn fn, void *data)
2000{
2001 struct crypt_config *cc = ti->private;
2002
Mike Snitzer5dea2712009-07-23 20:30:42 +01002003 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002004}
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006static struct target_type crypt_target = {
2007 .name = "crypt",
Milan Brozed04d982013-10-28 23:21:04 +01002008 .version = {1, 13, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 .module = THIS_MODULE,
2010 .ctr = crypt_ctr,
2011 .dtr = crypt_dtr,
2012 .map = crypt_map,
2013 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07002014 .postsuspend = crypt_postsuspend,
2015 .preresume = crypt_preresume,
2016 .resume = crypt_resume,
2017 .message = crypt_message,
Milan Brozd41e26b2008-07-21 12:00:40 +01002018 .merge = crypt_merge,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002019 .iterate_devices = crypt_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020};
2021
2022static int __init dm_crypt_init(void)
2023{
2024 int r;
2025
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002026 _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (!_crypt_io_pool)
2028 return -ENOMEM;
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 r = dm_register_target(&crypt_target);
2031 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002032 DMERR("register failed %d", r);
Milan Broz9934a8b2007-10-19 22:38:57 +01002033 kmem_cache_destroy(_crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return r;
2037}
2038
2039static void __exit dm_crypt_exit(void)
2040{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002041 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 kmem_cache_destroy(_crypt_io_pool);
2043}
2044
2045module_init(dm_crypt_init);
2046module_exit(dm_crypt_exit);
2047
2048MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
2049MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
2050MODULE_LICENSE("GPL");