blob: ba510a223f42e0682fdd683417852d0efe957439 [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This file is released under the GPL.
7 */
8
Milan Broz43d69032008-02-08 02:11:09 +00009#include <linux/completion.h>
Herbert Xud1806f62006-08-22 20:29:17 +100010#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/bio.h>
15#include <linux/blkdev.h>
16#include <linux/mempool.h>
17#include <linux/slab.h>
18#include <linux/crypto.h>
19#include <linux/workqueue.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070020#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100022#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100024#include <asm/unaligned.h>
Milan Broz34745782011-01-13 19:59:55 +000025#include <crypto/hash.h>
26#include <crypto/md5.h>
27#include <crypto/algapi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Mikulas Patocka586e80e2008-10-21 17:44:59 +010029#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Alasdair G Kergon72d94862006-06-26 00:27:35 -070031#define DM_MSG_PREFIX "crypt"
Milan Broze48d4bb2006-10-03 01:15:37 -070032#define MESG_STR(x) x, sizeof(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * context holding the current state of a multi-part conversion
36 */
37struct convert_context {
Milan Broz43d69032008-02-08 02:11:09 +000038 struct completion restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct bio *bio_in;
40 struct bio *bio_out;
41 unsigned int offset_in;
42 unsigned int offset_out;
43 unsigned int idx_in;
44 unsigned int idx_out;
45 sector_t sector;
Mikulas Patocka815bc972012-03-27 13:17:56 -070046 atomic_t cc_pending;
Mikulas Patocka30c273a2012-03-27 14:16:33 -070047 struct ablkcipher_request *req;
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 {
54 struct dm_target *target;
55 struct bio *base_bio;
56 struct work_struct work;
57
58 struct convert_context ctx;
59
Mikulas Patocka815bc972012-03-27 13:17:56 -070060 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/*
103 * Crypt: maps a linear range of a block device
104 * and encrypts / decrypts at the same time.
105 */
Milan Broze48d4bb2006-10-03 01:15:37 -0700106enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
Andi Kleenc0297722011-01-13 19:59:53 +0000107
108/*
Andi Kleenc0297722011-01-13 19:59:53 +0000109 * The fields in here must be read only after initialization,
Andi Kleenc0297722011-01-13 19:59:53 +0000110 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111struct crypt_config {
112 struct dm_dev *dev;
113 sector_t start;
114
115 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000116 * pool for per bio private data, crypto requests and
117 * encryption requeusts/buffer pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 */
119 mempool_t *io_pool;
Milan Brozddd42ed2008-02-08 02:11:07 +0000120 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -0700122 struct bio_set *bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Milan Brozcabf08e2007-10-19 22:38:58 +0100124 struct workqueue_struct *io_queue;
125 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700126
Milan Broz5ebaee62010-08-12 04:14:07 +0100127 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000128 char *cipher_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800131 union {
Milan Broz60473592009-12-10 23:51:55 +0000132 struct iv_essiv_private essiv;
133 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000134 struct iv_lmk_private lmk;
Herbert Xu79066ad2006-12-05 13:41:52 -0800135 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 sector_t iv_offset;
137 unsigned int iv_size;
138
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700139 /* ESSIV: struct crypto_cipher *essiv_tfm */
140 void *iv_private;
141 struct crypto_ablkcipher **tfms;
Milan Brozd1f96422011-01-13 19:59:54 +0000142 unsigned tfms_count;
Andi Kleenc0297722011-01-13 19:59:53 +0000143
144 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000145 * Layout of each crypto request:
146 *
147 * struct ablkcipher_request
148 * context
149 * padding
150 * struct dm_crypt_request
151 * padding
152 * IV
153 *
154 * The padding is added so that dm_crypt_request and the IV are
155 * correctly aligned.
156 */
157 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000158
Milan Broze48d4bb2006-10-03 01:15:37 -0700159 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 unsigned int key_size;
Milan Brozd1f96422011-01-13 19:59:54 +0000161 unsigned int key_parts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 u8 key[0];
163};
164
Milan Broz6a24c712006-10-03 01:15:40 -0700165#define MIN_IOS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#define MIN_POOL_PAGES 32
167#define MIN_BIO_PAGES 8
168
Christoph Lametere18b8902006-12-06 20:33:20 -0800169static struct kmem_cache *_crypt_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100171static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000172static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Broz2dc53272011-01-13 19:59:54 +0000173static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
Olaf Kirch027581f2007-05-09 02:32:52 -0700174
Andi Kleenc0297722011-01-13 19:59:53 +0000175/*
176 * Use this to access cipher attributes that are the same for each CPU.
177 */
178static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc)
179{
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700180 return cc->tfms[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * Different IV generation algorithms:
185 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000186 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200187 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 *
Milan Broz61afef62009-12-10 23:52:25 +0000189 * plain64: the initial vector is the 64-bit little-endian version of the sector
190 * number, padded with zeros if necessary.
191 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000192 * essiv: "encrypted sector|salt initial vector", the sector number is
193 * encrypted with the bulk cipher using a salt as key. The salt
194 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Rik Snel48527fa2006-09-03 08:56:39 +1000196 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
197 * (needed for LRW-32-AES and possible other narrow block modes)
198 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700199 * null: the initial vector is always zero. Provides compatibility with
200 * obsolete loop_fish2 devices. Do not use for new devices.
201 *
Milan Broz34745782011-01-13 19:59:55 +0000202 * lmk: Compatible implementation of the block chaining mode used
203 * by the Loop-AES block device encryption system
204 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
205 * It operates on full 512 byte sectors and uses CBC
206 * with an IV derived from the sector number, the data and
207 * optionally extra IV seed.
208 * This means that after decryption the first block
209 * of sector must be tweaked according to decrypted data.
210 * Loop-AES can use three encryption schemes:
211 * version 1: is plain aes-cbc mode
212 * version 2: uses 64 multikey scheme with lmk IV generator
213 * version 3: the same as version 2 with additional IV seed
214 * (it uses 65 keys, last key is used as IV seed)
215 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * plumb: unimplemented, see:
217 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
218 */
219
Milan Broz2dc53272011-01-13 19:59:54 +0000220static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
221 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 memset(iv, 0, cc->iv_size);
Milan Broz2dc53272011-01-13 19:59:54 +0000224 *(u32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 return 0;
227}
228
Milan Broz61afef62009-12-10 23:52:25 +0000229static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000230 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000231{
232 memset(iv, 0, cc->iv_size);
Milan Broz2dc53272011-01-13 19:59:54 +0000233 *(u64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000234
235 return 0;
236}
237
Milan Brozb95bf2d2009-12-10 23:51:56 +0000238/* Initialise ESSIV - compute salt but no local memory allocations */
239static int crypt_iv_essiv_init(struct crypt_config *cc)
240{
241 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
242 struct hash_desc desc;
243 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000244 struct crypto_cipher *essiv_tfm;
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700245 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000246
247 sg_init_one(&sg, cc->key, cc->key_size);
248 desc.tfm = essiv->hash_tfm;
249 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
250
251 err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt);
252 if (err)
253 return err;
254
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700255 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000256
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700257 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
258 crypto_hash_digestsize(essiv->hash_tfm));
259 if (err)
260 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000261
262 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000263}
264
Milan Broz542da312009-12-10 23:51:57 +0000265/* Wipe salt and reset key derived from volume key */
266static int crypt_iv_essiv_wipe(struct crypt_config *cc)
267{
268 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
269 unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000270 struct crypto_cipher *essiv_tfm;
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700271 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000272
273 memset(essiv->salt, 0, salt_size);
274
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700275 essiv_tfm = cc->iv_private;
276 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
277 if (r)
278 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000279
280 return err;
281}
282
283/* Set up per cpu cipher state */
284static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
285 struct dm_target *ti,
286 u8 *salt, unsigned saltsize)
287{
288 struct crypto_cipher *essiv_tfm;
289 int err;
290
291 /* Setup the essiv_tfm with the given salt */
292 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
293 if (IS_ERR(essiv_tfm)) {
294 ti->error = "Error allocating crypto tfm for ESSIV";
295 return essiv_tfm;
296 }
297
298 if (crypto_cipher_blocksize(essiv_tfm) !=
299 crypto_ablkcipher_ivsize(any_tfm(cc))) {
300 ti->error = "Block size of ESSIV cipher does "
301 "not match IV size of block cipher";
302 crypto_free_cipher(essiv_tfm);
303 return ERR_PTR(-EINVAL);
304 }
305
306 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
307 if (err) {
308 ti->error = "Failed to set key for ESSIV cipher";
309 crypto_free_cipher(essiv_tfm);
310 return ERR_PTR(err);
311 }
312
313 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000314}
315
Milan Broz60473592009-12-10 23:51:55 +0000316static void crypt_iv_essiv_dtr(struct crypt_config *cc)
317{
Andi Kleenc0297722011-01-13 19:59:53 +0000318 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000319 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
320
Milan Brozb95bf2d2009-12-10 23:51:56 +0000321 crypto_free_hash(essiv->hash_tfm);
322 essiv->hash_tfm = NULL;
323
324 kzfree(essiv->salt);
325 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000326
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700327 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000328
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700329 if (essiv_tfm)
330 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000331
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700332 cc->iv_private = NULL;
333
Milan Broz60473592009-12-10 23:51:55 +0000334}
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100337 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
Milan Broz5861f1b2009-12-10 23:51:56 +0000339 struct crypto_cipher *essiv_tfm = NULL;
340 struct crypto_hash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000341 u8 *salt = NULL;
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700342 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Milan Broz5861f1b2009-12-10 23:51:56 +0000344 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700345 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return -EINVAL;
347 }
348
Milan Brozb95bf2d2009-12-10 23:51:56 +0000349 /* Allocate hash algorithm */
Herbert Xu35058682006-08-24 19:10:20 +1000350 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
351 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700352 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000353 err = PTR_ERR(hash_tfm);
354 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356
Milan Brozb95bf2d2009-12-10 23:51:56 +0000357 salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000358 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700359 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000360 err = -ENOMEM;
361 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
Milan Brozb95bf2d2009-12-10 23:51:56 +0000364 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000365 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
366
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700367 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
368 crypto_hash_digestsize(hash_tfm));
369 if (IS_ERR(essiv_tfm)) {
370 crypt_iv_essiv_dtr(cc);
371 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000372 }
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700373 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000376
377bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000378 if (hash_tfm && !IS_ERR(hash_tfm))
379 crypto_free_hash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000380 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000381 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Milan Broz2dc53272011-01-13 19:59:54 +0000384static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
385 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Mikulas Patockafbd06f92012-03-27 14:00:17 -0700387
388 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 memset(iv, 0, cc->iv_size);
Milan Broz2dc53272011-01-13 19:59:54 +0000391 *(u64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000392 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 return 0;
395}
396
Rik Snel48527fa2006-09-03 08:56:39 +1000397static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
398 const char *opts)
399{
Andi Kleenc0297722011-01-13 19:59:53 +0000400 unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800401 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000402
403 /* we need to calculate how far we must shift the sector count
404 * to get the cipher block count, we use this shift in _gen */
405
406 if (1 << log != bs) {
407 ti->error = "cypher blocksize is not a power of 2";
408 return -EINVAL;
409 }
410
411 if (log > 9) {
412 ti->error = "cypher blocksize is > 512";
413 return -EINVAL;
414 }
415
Milan Broz60473592009-12-10 23:51:55 +0000416 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000417
418 return 0;
419}
420
421static void crypt_iv_benbi_dtr(struct crypt_config *cc)
422{
Rik Snel48527fa2006-09-03 08:56:39 +1000423}
424
Milan Broz2dc53272011-01-13 19:59:54 +0000425static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
426 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000427{
Herbert Xu79066ad2006-12-05 13:41:52 -0800428 __be64 val;
429
Rik Snel48527fa2006-09-03 08:56:39 +1000430 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800431
Milan Broz2dc53272011-01-13 19:59:54 +0000432 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800433 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return 0;
436}
437
Milan Broz2dc53272011-01-13 19:59:54 +0000438static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
439 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700440{
441 memset(iv, 0, cc->iv_size);
442
443 return 0;
444}
445
Milan Broz34745782011-01-13 19:59:55 +0000446static void crypt_iv_lmk_dtr(struct crypt_config *cc)
447{
448 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
449
450 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
451 crypto_free_shash(lmk->hash_tfm);
452 lmk->hash_tfm = NULL;
453
454 kzfree(lmk->seed);
455 lmk->seed = NULL;
456}
457
458static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
459 const char *opts)
460{
461 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
462
463 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
464 if (IS_ERR(lmk->hash_tfm)) {
465 ti->error = "Error initializing LMK hash";
466 return PTR_ERR(lmk->hash_tfm);
467 }
468
469 /* No seed in LMK version 2 */
470 if (cc->key_parts == cc->tfms_count) {
471 lmk->seed = NULL;
472 return 0;
473 }
474
475 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
476 if (!lmk->seed) {
477 crypt_iv_lmk_dtr(cc);
478 ti->error = "Error kmallocing seed storage in LMK";
479 return -ENOMEM;
480 }
481
482 return 0;
483}
484
485static int crypt_iv_lmk_init(struct crypt_config *cc)
486{
487 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
488 int subkey_size = cc->key_size / cc->key_parts;
489
490 /* LMK seed is on the position of LMK_KEYS + 1 key */
491 if (lmk->seed)
492 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
493 crypto_shash_digestsize(lmk->hash_tfm));
494
495 return 0;
496}
497
498static int crypt_iv_lmk_wipe(struct crypt_config *cc)
499{
500 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
501
502 if (lmk->seed)
503 memset(lmk->seed, 0, LMK_SEED_SIZE);
504
505 return 0;
506}
507
508static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
509 struct dm_crypt_request *dmreq,
510 u8 *data)
511{
512 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
513 struct {
514 struct shash_desc desc;
515 char ctx[crypto_shash_descsize(lmk->hash_tfm)];
516 } sdesc;
517 struct md5_state md5state;
518 u32 buf[4];
519 int i, r;
520
521 sdesc.desc.tfm = lmk->hash_tfm;
522 sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
523
524 r = crypto_shash_init(&sdesc.desc);
525 if (r)
526 return r;
527
528 if (lmk->seed) {
529 r = crypto_shash_update(&sdesc.desc, lmk->seed, LMK_SEED_SIZE);
530 if (r)
531 return r;
532 }
533
534 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
535 r = crypto_shash_update(&sdesc.desc, data + 16, 16 * 31);
536 if (r)
537 return r;
538
539 /* Sector is cropped to 56 bits here */
540 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
541 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
542 buf[2] = cpu_to_le32(4024);
543 buf[3] = 0;
544 r = crypto_shash_update(&sdesc.desc, (u8 *)buf, sizeof(buf));
545 if (r)
546 return r;
547
548 /* No MD5 padding here */
549 r = crypto_shash_export(&sdesc.desc, &md5state);
550 if (r)
551 return r;
552
553 for (i = 0; i < MD5_HASH_WORDS; i++)
554 __cpu_to_le32s(&md5state.hash[i]);
555 memcpy(iv, &md5state.hash, cc->iv_size);
556
557 return 0;
558}
559
560static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
561 struct dm_crypt_request *dmreq)
562{
563 u8 *src;
564 int r = 0;
565
566 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
567 src = kmap_atomic(sg_page(&dmreq->sg_in), KM_USER0);
568 r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
569 kunmap_atomic(src, KM_USER0);
570 } else
571 memset(iv, 0, cc->iv_size);
572
573 return r;
574}
575
576static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
577 struct dm_crypt_request *dmreq)
578{
579 u8 *dst;
580 int r;
581
582 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
583 return 0;
584
585 dst = kmap_atomic(sg_page(&dmreq->sg_out), KM_USER0);
586 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
587
588 /* Tweak the first block of plaintext sector */
589 if (!r)
590 crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
591
592 kunmap_atomic(dst, KM_USER0);
593 return r;
594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596static struct crypt_iv_operations crypt_iv_plain_ops = {
597 .generator = crypt_iv_plain_gen
598};
599
Milan Broz61afef62009-12-10 23:52:25 +0000600static struct crypt_iv_operations crypt_iv_plain64_ops = {
601 .generator = crypt_iv_plain64_gen
602};
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604static struct crypt_iv_operations crypt_iv_essiv_ops = {
605 .ctr = crypt_iv_essiv_ctr,
606 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000607 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000608 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 .generator = crypt_iv_essiv_gen
610};
611
Rik Snel48527fa2006-09-03 08:56:39 +1000612static struct crypt_iv_operations crypt_iv_benbi_ops = {
613 .ctr = crypt_iv_benbi_ctr,
614 .dtr = crypt_iv_benbi_dtr,
615 .generator = crypt_iv_benbi_gen
616};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Ludwig Nussel46b47732007-05-09 02:32:55 -0700618static struct crypt_iv_operations crypt_iv_null_ops = {
619 .generator = crypt_iv_null_gen
620};
621
Milan Broz34745782011-01-13 19:59:55 +0000622static struct crypt_iv_operations crypt_iv_lmk_ops = {
623 .ctr = crypt_iv_lmk_ctr,
624 .dtr = crypt_iv_lmk_dtr,
625 .init = crypt_iv_lmk_init,
626 .wipe = crypt_iv_lmk_wipe,
627 .generator = crypt_iv_lmk_gen,
628 .post = crypt_iv_lmk_post
629};
630
Milan Brozd469f842007-10-19 22:42:37 +0100631static void crypt_convert_init(struct crypt_config *cc,
632 struct convert_context *ctx,
633 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000634 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 ctx->bio_in = bio_in;
637 ctx->bio_out = bio_out;
638 ctx->offset_in = 0;
639 ctx->offset_out = 0;
640 ctx->idx_in = bio_in ? bio_in->bi_idx : 0;
641 ctx->idx_out = bio_out ? bio_out->bi_idx : 0;
642 ctx->sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000643 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Huang Yingb2174ee2009-03-16 17:44:33 +0000646static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
647 struct ablkcipher_request *req)
648{
649 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
650}
651
652static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
653 struct dm_crypt_request *dmreq)
654{
655 return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
656}
657
Milan Broz2dc53272011-01-13 19:59:54 +0000658static u8 *iv_of_dmreq(struct crypt_config *cc,
659 struct dm_crypt_request *dmreq)
660{
661 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
662 crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
663}
664
Milan Broz01482b72008-02-08 02:11:04 +0000665static int crypt_convert_block(struct crypt_config *cc,
Milan Broz3a7f6c92008-02-08 02:11:14 +0000666 struct convert_context *ctx,
667 struct ablkcipher_request *req)
Milan Broz01482b72008-02-08 02:11:04 +0000668{
669 struct bio_vec *bv_in = bio_iovec_idx(ctx->bio_in, ctx->idx_in);
670 struct bio_vec *bv_out = bio_iovec_idx(ctx->bio_out, ctx->idx_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000671 struct dm_crypt_request *dmreq;
672 u8 *iv;
673 int r = 0;
Milan Broz01482b72008-02-08 02:11:04 +0000674
Huang Yingb2174ee2009-03-16 17:44:33 +0000675 dmreq = dmreq_of_req(cc, req);
Milan Broz2dc53272011-01-13 19:59:54 +0000676 iv = iv_of_dmreq(cc, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000677
Milan Broz2dc53272011-01-13 19:59:54 +0000678 dmreq->iv_sector = ctx->sector;
Huang Yingb2174ee2009-03-16 17:44:33 +0000679 dmreq->ctx = ctx;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000680 sg_init_table(&dmreq->sg_in, 1);
681 sg_set_page(&dmreq->sg_in, bv_in->bv_page, 1 << SECTOR_SHIFT,
Milan Broz01482b72008-02-08 02:11:04 +0000682 bv_in->bv_offset + ctx->offset_in);
683
Milan Broz3a7f6c92008-02-08 02:11:14 +0000684 sg_init_table(&dmreq->sg_out, 1);
685 sg_set_page(&dmreq->sg_out, bv_out->bv_page, 1 << SECTOR_SHIFT,
Milan Broz01482b72008-02-08 02:11:04 +0000686 bv_out->bv_offset + ctx->offset_out);
687
688 ctx->offset_in += 1 << SECTOR_SHIFT;
689 if (ctx->offset_in >= bv_in->bv_len) {
690 ctx->offset_in = 0;
691 ctx->idx_in++;
692 }
693
694 ctx->offset_out += 1 << SECTOR_SHIFT;
695 if (ctx->offset_out >= bv_out->bv_len) {
696 ctx->offset_out = 0;
697 ctx->idx_out++;
698 }
699
Milan Broz3a7f6c92008-02-08 02:11:14 +0000700 if (cc->iv_gen_ops) {
Milan Broz2dc53272011-01-13 19:59:54 +0000701 r = cc->iv_gen_ops->generator(cc, iv, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000702 if (r < 0)
703 return r;
704 }
705
706 ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
707 1 << SECTOR_SHIFT, iv);
708
709 if (bio_data_dir(ctx->bio_in) == WRITE)
710 r = crypto_ablkcipher_encrypt(req);
711 else
712 r = crypto_ablkcipher_decrypt(req);
713
Milan Broz2dc53272011-01-13 19:59:54 +0000714 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
715 r = cc->iv_gen_ops->post(cc, iv, dmreq);
716
Milan Broz3a7f6c92008-02-08 02:11:14 +0000717 return r;
Milan Broz01482b72008-02-08 02:11:04 +0000718}
719
Milan Broz95497a92008-02-08 02:11:12 +0000720static void kcryptd_async_done(struct crypto_async_request *async_req,
721 int error);
Andi Kleenc0297722011-01-13 19:59:53 +0000722
Milan Brozddd42ed2008-02-08 02:11:07 +0000723static void crypt_alloc_req(struct crypt_config *cc,
724 struct convert_context *ctx)
725{
Milan Brozd1f96422011-01-13 19:59:54 +0000726 unsigned key_index = ctx->sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +0000727
Mikulas Patocka30c273a2012-03-27 14:16:33 -0700728 if (!ctx->req)
729 ctx->req = mempool_alloc(cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +0000730
Mikulas Patocka30c273a2012-03-27 14:16:33 -0700731 ablkcipher_request_set_tfm(ctx->req, cc->tfms[key_index]);
732 ablkcipher_request_set_callback(ctx->req,
Andi Kleenc0297722011-01-13 19:59:53 +0000733 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Mikulas Patocka30c273a2012-03-27 14:16:33 -0700734 kcryptd_async_done, dmreq_of_req(cc, ctx->req));
Milan Brozddd42ed2008-02-08 02:11:07 +0000735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/*
738 * Encrypt / decrypt data from one bio to another one (can be the same one)
739 */
740static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +0100741 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Milan Broz3f1e9072008-03-28 14:16:07 -0700743 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Mikulas Patocka815bc972012-03-27 13:17:56 -0700745 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +0100746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 while(ctx->idx_in < ctx->bio_in->bi_vcnt &&
748 ctx->idx_out < ctx->bio_out->bi_vcnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Milan Broz3a7f6c92008-02-08 02:11:14 +0000750 crypt_alloc_req(cc, ctx);
751
Mikulas Patocka815bc972012-03-27 13:17:56 -0700752 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700753
Mikulas Patocka30c273a2012-03-27 14:16:33 -0700754 r = crypt_convert_block(cc, ctx, ctx->req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000755
756 switch (r) {
Milan Broz3f1e9072008-03-28 14:16:07 -0700757 /* async */
Milan Broz3a7f6c92008-02-08 02:11:14 +0000758 case -EBUSY:
759 wait_for_completion(&ctx->restart);
760 INIT_COMPLETION(ctx->restart);
761 /* fall through*/
762 case -EINPROGRESS:
Mikulas Patocka30c273a2012-03-27 14:16:33 -0700763 ctx->req = NULL;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000764 ctx->sector++;
765 continue;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000766
Milan Broz3f1e9072008-03-28 14:16:07 -0700767 /* sync */
768 case 0:
Mikulas Patocka815bc972012-03-27 13:17:56 -0700769 atomic_dec(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700770 ctx->sector++;
Milan Brozc7f1b202008-07-02 09:34:28 +0100771 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -0700772 continue;
773
774 /* error */
775 default:
Mikulas Patocka815bc972012-03-27 13:17:56 -0700776 atomic_dec(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700777 return r;
778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
Milan Broz3f1e9072008-03-28 14:16:07 -0700781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Milan Brozd469f842007-10-19 22:42:37 +0100784static void dm_crypt_bio_destructor(struct bio *bio)
785{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100786 struct dm_crypt_io *io = bio->bi_private;
Milan Broz6a24c712006-10-03 01:15:40 -0700787 struct crypt_config *cc = io->target->private;
788
789 bio_free(bio, cc->bs);
Milan Brozd469f842007-10-19 22:42:37 +0100790}
Milan Broz6a24c712006-10-03 01:15:40 -0700791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792/*
793 * Generate a new unfragmented bio with the given size
794 * This should never violate the device limitations
Milan Broz933f01d2008-10-10 13:37:08 +0100795 * May return a smaller bio when running out of pages, indicated by
796 * *out_of_pages set to 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
Milan Broz933f01d2008-10-10 13:37:08 +0100798static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
799 unsigned *out_of_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Olaf Kirch027581f2007-05-09 02:32:52 -0700801 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -0700802 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Virob4e3ca12005-10-21 03:22:34 -0400804 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
Milan Broz91e10622007-12-13 14:16:10 +0000805 unsigned i, len;
806 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Olaf Kirch2f9941b2007-05-09 02:32:53 -0700808 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -0700809 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Olaf Kirch027581f2007-05-09 02:32:52 -0700812 clone_init(io, clone);
Milan Broz933f01d2008-10-10 13:37:08 +0100813 *out_of_pages = 0;
Milan Broz6a24c712006-10-03 01:15:40 -0700814
Olaf Kirchf97380b2007-05-09 02:32:54 -0700815 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +0000816 page = mempool_alloc(cc->page_pool, gfp_mask);
Milan Broz933f01d2008-10-10 13:37:08 +0100817 if (!page) {
818 *out_of_pages = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 break;
Milan Broz933f01d2008-10-10 13:37:08 +0100820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /*
823 * if additional pages cannot be allocated without waiting,
824 * return a partially allocated bio, the caller will then try
825 * to allocate additional bios while submitting this partial bio
826 */
Mikulas Patocka71b41c32012-03-27 13:15:32 -0700827 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Milan Broz91e10622007-12-13 14:16:10 +0000829 len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Milan Broz91e10622007-12-13 14:16:10 +0000831 if (!bio_add_page(clone, page, len, 0)) {
832 mempool_free(page, cc->page_pool);
833 break;
834 }
835
836 size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
Milan Broz8b004452006-10-03 01:15:37 -0700839 if (!clone->bi_size) {
840 bio_put(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return NULL;
842 }
843
Milan Broz8b004452006-10-03 01:15:37 -0700844 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Neil Brown644bd2f2007-10-16 13:48:46 +0200847static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Neil Brown644bd2f2007-10-16 13:48:46 +0200849 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 struct bio_vec *bv;
851
Neil Brown644bd2f2007-10-16 13:48:46 +0200852 for (i = 0; i < clone->bi_vcnt; i++) {
Milan Broz8b004452006-10-03 01:15:37 -0700853 bv = bio_iovec_idx(clone, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 BUG_ON(!bv->bv_page);
855 mempool_free(bv->bv_page, cc->page_pool);
856 bv->bv_page = NULL;
857 }
858}
859
Milan Brozdc440d1e2008-10-10 13:37:03 +0100860static struct dm_crypt_io *crypt_io_alloc(struct dm_target *ti,
861 struct bio *bio, sector_t sector)
862{
863 struct crypt_config *cc = ti->private;
864 struct dm_crypt_io *io;
865
866 io = mempool_alloc(cc->io_pool, GFP_NOIO);
867 io->target = ti;
868 io->base_bio = bio;
869 io->sector = sector;
870 io->error = 0;
Milan Broz393b47e2008-10-21 17:45:02 +0100871 io->base_io = NULL;
Mikulas Patocka30c273a2012-03-27 14:16:33 -0700872 io->ctx.req = NULL;
Mikulas Patocka815bc972012-03-27 13:17:56 -0700873 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +0100874
875 return io;
876}
877
Milan Broz3e1a8bd2008-10-10 13:37:02 +0100878static void crypt_inc_pending(struct dm_crypt_io *io)
879{
Mikulas Patocka815bc972012-03-27 13:17:56 -0700880 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +0100881}
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883/*
884 * One of the bios was finished. Check for completion of
885 * the whole request and correctly clean up the buffer.
Milan Broz393b47e2008-10-21 17:45:02 +0100886 * If base_io is set, wait for the last fragment to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
Milan Broz5742fd72008-02-08 02:10:43 +0000888static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
Milan Broz5742fd72008-02-08 02:10:43 +0000890 struct crypt_config *cc = io->target->private;
Milan Brozb35f8ca2009-03-16 17:44:36 +0000891 struct bio *base_bio = io->base_bio;
892 struct dm_crypt_io *base_io = io->base_io;
893 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Mikulas Patocka815bc972012-03-27 13:17:56 -0700895 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return;
897
Mikulas Patocka30c273a2012-03-27 14:16:33 -0700898 if (io->ctx.req)
899 mempool_free(io->ctx.req, cc->req_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 mempool_free(io, cc->io_pool);
Milan Brozb35f8ca2009-03-16 17:44:36 +0000901
902 if (likely(!base_io))
903 bio_endio(base_bio, error);
904 else {
905 if (error && !base_io->error)
906 base_io->error = error;
907 crypt_dec_pending(base_io);
908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911/*
Milan Brozcabf08e2007-10-19 22:38:58 +0100912 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 *
914 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -0700915 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +0100916 *
917 * kcryptd performs the actual encryption or decryption.
918 *
919 * kcryptd_io performs the IO submission.
920 *
921 * They must be separated as otherwise the final stages could be
922 * starved by new requests which can block in the first stages due
923 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +0000924 *
925 * The work is done per CPU global for all dm-crypt instances.
926 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 */
NeilBrown6712ecf2007-09-27 12:47:43 +0200928static void crypt_endio(struct bio *clone, int error)
Milan Broz8b004452006-10-03 01:15:37 -0700929{
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100930 struct dm_crypt_io *io = clone->bi_private;
Milan Broz8b004452006-10-03 01:15:37 -0700931 struct crypt_config *cc = io->target->private;
Milan Brozee7a4912008-02-08 02:10:46 +0000932 unsigned rw = bio_data_dir(clone);
Milan Broz8b004452006-10-03 01:15:37 -0700933
Milan Brozadfe4772007-12-13 14:15:51 +0000934 if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
935 error = -EIO;
936
Milan Broz8b004452006-10-03 01:15:37 -0700937 /*
NeilBrown6712ecf2007-09-27 12:47:43 +0200938 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -0700939 */
Milan Brozee7a4912008-02-08 02:10:46 +0000940 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +0200941 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +0000942
943 bio_put(clone);
944
945 if (rw == READ && !error) {
946 kcryptd_queue_crypt(io);
947 return;
NeilBrown6712ecf2007-09-27 12:47:43 +0200948 }
Milan Broz8b004452006-10-03 01:15:37 -0700949
Milan Brozadfe4772007-12-13 14:15:51 +0000950 if (unlikely(error))
Milan Broz5742fd72008-02-08 02:10:43 +0000951 io->error = error;
952
953 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -0700954}
955
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100956static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -0700957{
958 struct crypt_config *cc = io->target->private;
959
960 clone->bi_private = io;
961 clone->bi_end_io = crypt_endio;
962 clone->bi_bdev = cc->dev->bdev;
963 clone->bi_rw = io->base_bio->bi_rw;
Olaf Kirch027581f2007-05-09 02:32:52 -0700964 clone->bi_destructor = dm_crypt_bio_destructor;
Milan Broz8b004452006-10-03 01:15:37 -0700965}
966
Milan Broz20c82532011-01-13 19:59:53 +0000967static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -0700968{
969 struct crypt_config *cc = io->target->private;
970 struct bio *base_bio = io->base_bio;
971 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -0700972
Milan Broz8b004452006-10-03 01:15:37 -0700973 /*
974 * The block layer might modify the bvec array, so always
975 * copy the required bvecs because we need the original
976 * one in order to decrypt the whole bio data *afterwards*.
977 */
Milan Broz20c82532011-01-13 19:59:53 +0000978 clone = bio_alloc_bioset(gfp, bio_segments(base_bio), cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100979 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +0000980 return 1;
Milan Broz8b004452006-10-03 01:15:37 -0700981
Milan Broz20c82532011-01-13 19:59:53 +0000982 crypt_inc_pending(io);
983
Milan Broz8b004452006-10-03 01:15:37 -0700984 clone_init(io, clone);
985 clone->bi_idx = 0;
986 clone->bi_vcnt = bio_segments(base_bio);
987 clone->bi_size = base_bio->bi_size;
Milan Broz0c395b02008-02-08 02:10:54 +0000988 clone->bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -0700989 memcpy(clone->bi_io_vec, bio_iovec(base_bio),
990 sizeof(struct bio_vec) * clone->bi_vcnt);
Milan Broz8b004452006-10-03 01:15:37 -0700991
Milan Broz93e605c2006-10-03 01:15:38 -0700992 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +0000993 return 0;
Milan Broz8b004452006-10-03 01:15:37 -0700994}
995
Milan Broz4e4eef62008-02-08 02:10:49 +0000996static void kcryptd_io_write(struct dm_crypt_io *io)
997{
Milan Broz95497a92008-02-08 02:11:12 +0000998 struct bio *clone = io->ctx.bio_out;
Milan Broz95497a92008-02-08 02:11:12 +0000999 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +00001000}
1001
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001002static void kcryptd_io(struct work_struct *work)
1003{
1004 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1005
Milan Broz20c82532011-01-13 19:59:53 +00001006 if (bio_data_dir(io->base_bio) == READ) {
1007 crypt_inc_pending(io);
1008 if (kcryptd_io_read(io, GFP_NOIO))
1009 io->error = -ENOMEM;
1010 crypt_dec_pending(io);
1011 } else
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001012 kcryptd_io_write(io);
1013}
1014
1015static void kcryptd_queue_io(struct dm_crypt_io *io)
1016{
1017 struct crypt_config *cc = io->target->private;
1018
1019 INIT_WORK(&io->work, kcryptd_io);
1020 queue_work(cc->io_queue, &io->work);
1021}
1022
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001023static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001024{
Milan Brozdec1ced2008-02-08 02:10:57 +00001025 struct bio *clone = io->ctx.bio_out;
1026 struct crypt_config *cc = io->target->private;
1027
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001028 if (unlikely(io->error < 0)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001029 crypt_free_buffer_pages(cc, clone);
1030 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001031 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001032 return;
1033 }
1034
1035 /* crypt_convert should have filled the clone bio */
1036 BUG_ON(io->ctx.idx_out < clone->bi_vcnt);
1037
1038 clone->bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001039
Milan Broz95497a92008-02-08 02:11:12 +00001040 if (async)
1041 kcryptd_queue_io(io);
Alasdair G Kergon1e37bb82008-10-10 13:37:05 +01001042 else
Milan Broz95497a92008-02-08 02:11:12 +00001043 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +00001044}
1045
Milan Brozfc5a5e92008-10-10 13:37:04 +01001046static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001047{
1048 struct crypt_config *cc = io->target->private;
Milan Broz8b004452006-10-03 01:15:37 -07001049 struct bio *clone;
Milan Broz393b47e2008-10-21 17:45:02 +01001050 struct dm_crypt_io *new_io;
Milan Brozc8081612008-10-10 13:37:08 +01001051 int crypt_finished;
Milan Broz933f01d2008-10-10 13:37:08 +01001052 unsigned out_of_pages = 0;
Milan Brozdec1ced2008-02-08 02:10:57 +00001053 unsigned remaining = io->base_bio->bi_size;
Milan Brozb635b002008-10-21 17:45:00 +01001054 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +00001055 int r;
Milan Broz8b004452006-10-03 01:15:37 -07001056
Milan Broz93e605c2006-10-03 01:15:38 -07001057 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001058 * Prevent io from disappearing until this function completes.
1059 */
1060 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001061 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001062
1063 /*
Milan Broz93e605c2006-10-03 01:15:38 -07001064 * The allocated buffers can be smaller than the whole bio,
1065 * so repeat the whole process until all the data can be handled.
1066 */
1067 while (remaining) {
Milan Broz933f01d2008-10-10 13:37:08 +01001068 clone = crypt_alloc_buffer(io, remaining, &out_of_pages);
Milan Broz23541d22006-10-03 01:15:39 -07001069 if (unlikely(!clone)) {
Milan Broz5742fd72008-02-08 02:10:43 +00001070 io->error = -ENOMEM;
Milan Brozfc5a5e92008-10-10 13:37:04 +01001071 break;
Milan Broz23541d22006-10-03 01:15:39 -07001072 }
Milan Broz93e605c2006-10-03 01:15:38 -07001073
Milan Broz53017032008-02-08 02:10:38 +00001074 io->ctx.bio_out = clone;
1075 io->ctx.idx_out = 0;
Milan Broz93e605c2006-10-03 01:15:38 -07001076
Milan Broz93e605c2006-10-03 01:15:38 -07001077 remaining -= clone->bi_size;
Milan Brozb635b002008-10-21 17:45:00 +01001078 sector += bio_sectors(clone);
Milan Brozdec1ced2008-02-08 02:10:57 +00001079
Milan Broz4e594092008-10-10 13:37:07 +01001080 crypt_inc_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001081 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001082 if (r < 0)
1083 io->error = -EIO;
Mikulas Patocka815bc972012-03-27 13:17:56 -07001084 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
Milan Brozdec1ced2008-02-08 02:10:57 +00001085
Milan Brozc8081612008-10-10 13:37:08 +01001086 /* Encryption was already finished, submit io now */
1087 if (crypt_finished) {
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001088 kcryptd_crypt_write_io_submit(io, 0);
Milan Brozc8081612008-10-10 13:37:08 +01001089
1090 /*
1091 * If there was an error, do not try next fragments.
1092 * For async, error is processed in async handler.
1093 */
Milan Broz6c031f42008-10-10 13:37:06 +01001094 if (unlikely(r < 0))
Milan Brozfc5a5e92008-10-10 13:37:04 +01001095 break;
Milan Brozb635b002008-10-21 17:45:00 +01001096
1097 io->sector = sector;
Milan Broz4e594092008-10-10 13:37:07 +01001098 }
Milan Broz93e605c2006-10-03 01:15:38 -07001099
Milan Broz933f01d2008-10-10 13:37:08 +01001100 /*
1101 * Out of memory -> run queues
1102 * But don't wait if split was due to the io size restriction
1103 */
1104 if (unlikely(out_of_pages))
Jens Axboe8aa7e842009-07-09 14:52:32 +02001105 congestion_wait(BLK_RW_ASYNC, HZ/100);
Milan Broz933f01d2008-10-10 13:37:08 +01001106
Milan Broz393b47e2008-10-21 17:45:02 +01001107 /*
1108 * With async crypto it is unsafe to share the crypto context
1109 * between fragments, so switch to a new dm_crypt_io structure.
1110 */
1111 if (unlikely(!crypt_finished && remaining)) {
1112 new_io = crypt_io_alloc(io->target, io->base_bio,
1113 sector);
1114 crypt_inc_pending(new_io);
1115 crypt_convert_init(cc, &new_io->ctx, NULL,
1116 io->base_bio, sector);
1117 new_io->ctx.idx_in = io->ctx.idx_in;
1118 new_io->ctx.offset_in = io->ctx.offset_in;
1119
1120 /*
1121 * Fragments after the first use the base_io
1122 * pending count.
1123 */
1124 if (!io->base_io)
1125 new_io->base_io = io;
1126 else {
1127 new_io->base_io = io->base_io;
1128 crypt_inc_pending(io->base_io);
1129 crypt_dec_pending(io);
1130 }
1131
1132 io = new_io;
1133 }
Milan Broz8b004452006-10-03 01:15:37 -07001134 }
Milan Broz899c95d2008-02-08 02:11:02 +00001135
1136 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001137}
1138
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001139static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001140{
Milan Broz5742fd72008-02-08 02:10:43 +00001141 crypt_dec_pending(io);
1142}
1143
Milan Broz4e4eef62008-02-08 02:10:49 +00001144static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001145{
1146 struct crypt_config *cc = io->target->private;
Milan Broz5742fd72008-02-08 02:10:43 +00001147 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001148
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001149 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001150
Milan Broz53017032008-02-08 02:10:38 +00001151 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001152 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001153
Milan Broz5742fd72008-02-08 02:10:43 +00001154 r = crypt_convert(cc, &io->ctx);
1155
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001156 if (r < 0)
1157 io->error = -EIO;
1158
Mikulas Patocka815bc972012-03-27 13:17:56 -07001159 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001160 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001161
1162 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001163}
1164
Milan Broz95497a92008-02-08 02:11:12 +00001165static void kcryptd_async_done(struct crypto_async_request *async_req,
1166 int error)
1167{
Huang Yingb2174ee2009-03-16 17:44:33 +00001168 struct dm_crypt_request *dmreq = async_req->data;
1169 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001170 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1171 struct crypt_config *cc = io->target->private;
1172
1173 if (error == -EINPROGRESS) {
1174 complete(&ctx->restart);
1175 return;
1176 }
1177
Milan Broz2dc53272011-01-13 19:59:54 +00001178 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
1179 error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
1180
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001181 if (error < 0)
1182 io->error = -EIO;
1183
Huang Yingb2174ee2009-03-16 17:44:33 +00001184 mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool);
Milan Broz95497a92008-02-08 02:11:12 +00001185
Mikulas Patocka815bc972012-03-27 13:17:56 -07001186 if (!atomic_dec_and_test(&ctx->cc_pending))
Milan Broz95497a92008-02-08 02:11:12 +00001187 return;
1188
1189 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001190 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001191 else
Mikulas Patocka8bfc0152012-03-27 13:16:51 -07001192 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001193}
1194
Milan Broz4e4eef62008-02-08 02:10:49 +00001195static void kcryptd_crypt(struct work_struct *work)
1196{
1197 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1198
1199 if (bio_data_dir(io->base_bio) == READ)
1200 kcryptd_crypt_read_convert(io);
1201 else
1202 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001203}
1204
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001205static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1206{
1207 struct crypt_config *cc = io->target->private;
1208
1209 INIT_WORK(&io->work, kcryptd_crypt);
1210 queue_work(cc->crypt_queue, &io->work);
1211}
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213/*
1214 * Decode key from its hex representation
1215 */
1216static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1217{
1218 char buffer[3];
1219 char *endp;
1220 unsigned int i;
1221
1222 buffer[2] = '\0';
1223
Milan Broz8b004452006-10-03 01:15:37 -07001224 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 buffer[0] = *hex++;
1226 buffer[1] = *hex++;
1227
1228 key[i] = (u8)simple_strtoul(buffer, &endp, 16);
1229
1230 if (endp != &buffer[2])
1231 return -EINVAL;
1232 }
1233
1234 if (*hex != '\0')
1235 return -EINVAL;
1236
1237 return 0;
1238}
1239
1240/*
1241 * Encode key into its hex representation
1242 */
1243static void crypt_encode_key(char *hex, u8 *key, unsigned int size)
1244{
1245 unsigned int i;
1246
Milan Broz8b004452006-10-03 01:15:37 -07001247 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 sprintf(hex, "%02x", *key);
1249 hex += 2;
1250 key++;
1251 }
1252}
1253
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001254static void crypt_free_tfms(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001255{
Milan Brozd1f96422011-01-13 19:59:54 +00001256 unsigned i;
1257
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001258 if (!cc->tfms)
1259 return;
1260
Milan Brozd1f96422011-01-13 19:59:54 +00001261 for (i = 0; i < cc->tfms_count; i++)
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001262 if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) {
1263 crypto_free_ablkcipher(cc->tfms[i]);
1264 cc->tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001265 }
1266}
1267
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001268static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001269{
Milan Brozd1f96422011-01-13 19:59:54 +00001270 unsigned i;
1271 int err;
1272
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001273 cc->tfms = kmalloc(cc->tfms_count * sizeof(struct crypto_ablkcipher *),
1274 GFP_KERNEL);
1275 if (!cc->tfms)
1276 return -ENOMEM;
1277
Milan Brozd1f96422011-01-13 19:59:54 +00001278 for (i = 0; i < cc->tfms_count; i++) {
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001279 cc->tfms[i] = crypto_alloc_ablkcipher(ciphermode, 0, 0);
1280 if (IS_ERR(cc->tfms[i])) {
1281 err = PTR_ERR(cc->tfms[i]);
1282 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001283 return err;
1284 }
1285 }
1286
1287 return 0;
1288}
1289
Andi Kleenc0297722011-01-13 19:59:53 +00001290static int crypt_setkey_allcpus(struct crypt_config *cc)
1291{
Milan Brozd1f96422011-01-13 19:59:54 +00001292 unsigned subkey_size = cc->key_size >> ilog2(cc->tfms_count);
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001293 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001294
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001295 for (i = 0; i < cc->tfms_count; i++) {
1296 r = crypto_ablkcipher_setkey(cc->tfms[i],
1297 cc->key + (i * subkey_size),
1298 subkey_size);
1299 if (r)
1300 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001301 }
1302
1303 return err;
1304}
1305
Milan Broze48d4bb2006-10-03 01:15:37 -07001306static int crypt_set_key(struct crypt_config *cc, char *key)
1307{
Milan Brozde8be5a2011-03-24 13:54:27 +00001308 int r = -EINVAL;
1309 int key_string_len = strlen(key);
1310
Milan Broz69a8cfc2011-01-13 19:59:49 +00001311 /* The key size may not be changed. */
Milan Brozde8be5a2011-03-24 13:54:27 +00001312 if (cc->key_size != (key_string_len >> 1))
1313 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001314
Milan Broz69a8cfc2011-01-13 19:59:49 +00001315 /* Hyphen (which gives a key_size of zero) means there is no key. */
1316 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00001317 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001318
Milan Broz69a8cfc2011-01-13 19:59:49 +00001319 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00001320 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001321
1322 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1323
Milan Brozde8be5a2011-03-24 13:54:27 +00001324 r = crypt_setkey_allcpus(cc);
1325
1326out:
1327 /* Hex key string not needed after here, so wipe it. */
1328 memset(key, '0', key_string_len);
1329
1330 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07001331}
1332
1333static int crypt_wipe_key(struct crypt_config *cc)
1334{
1335 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1336 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00001337
1338 return crypt_setkey_allcpus(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07001339}
1340
Milan Broz28513fc2010-08-12 04:14:06 +01001341static void crypt_dtr(struct dm_target *ti)
1342{
1343 struct crypt_config *cc = ti->private;
1344
1345 ti->private = NULL;
1346
1347 if (!cc)
1348 return;
1349
1350 if (cc->io_queue)
1351 destroy_workqueue(cc->io_queue);
1352 if (cc->crypt_queue)
1353 destroy_workqueue(cc->crypt_queue);
1354
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001355 crypt_free_tfms(cc);
1356
Milan Broz28513fc2010-08-12 04:14:06 +01001357 if (cc->bs)
1358 bioset_free(cc->bs);
1359
1360 if (cc->page_pool)
1361 mempool_destroy(cc->page_pool);
1362 if (cc->req_pool)
1363 mempool_destroy(cc->req_pool);
1364 if (cc->io_pool)
1365 mempool_destroy(cc->io_pool);
1366
1367 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1368 cc->iv_gen_ops->dtr(cc);
1369
Milan Broz28513fc2010-08-12 04:14:06 +01001370 if (cc->dev)
1371 dm_put_device(ti, cc->dev);
1372
Milan Broz5ebaee62010-08-12 04:14:07 +01001373 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00001374 kzfree(cc->cipher_string);
Milan Broz28513fc2010-08-12 04:14:06 +01001375
1376 /* Must zero key material before freeing */
1377 kzfree(cc);
1378}
1379
Milan Broz5ebaee62010-08-12 04:14:07 +01001380static int crypt_ctr_cipher(struct dm_target *ti,
1381 char *cipher_in, char *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382{
Milan Broz5ebaee62010-08-12 04:14:07 +01001383 struct crypt_config *cc = ti->private;
Milan Brozd1f96422011-01-13 19:59:54 +00001384 char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01001385 char *cipher_api = NULL;
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001386 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Milan Broz5ebaee62010-08-12 04:14:07 +01001388 /* Convert to crypto api definition? */
1389 if (strchr(cipher_in, '(')) {
1390 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 return -EINVAL;
1392 }
1393
Milan Broz7dbcd132011-01-13 19:59:52 +00001394 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
1395 if (!cc->cipher_string)
1396 goto bad_mem;
1397
Milan Broz5ebaee62010-08-12 04:14:07 +01001398 /*
1399 * Legacy dm-crypt cipher specification
Milan Brozd1f96422011-01-13 19:59:54 +00001400 * cipher[:keycount]-mode-iv:ivopts
Milan Broz5ebaee62010-08-12 04:14:07 +01001401 */
1402 tmp = cipher_in;
Milan Brozd1f96422011-01-13 19:59:54 +00001403 keycount = strsep(&tmp, "-");
1404 cipher = strsep(&keycount, ":");
1405
1406 if (!keycount)
1407 cc->tfms_count = 1;
1408 else if (sscanf(keycount, "%u", &cc->tfms_count) != 1 ||
1409 !is_power_of_2(cc->tfms_count)) {
1410 ti->error = "Bad cipher key count specification";
1411 return -EINVAL;
1412 }
1413 cc->key_parts = cc->tfms_count;
Milan Broz5ebaee62010-08-12 04:14:07 +01001414
1415 cc->cipher = kstrdup(cipher, GFP_KERNEL);
1416 if (!cc->cipher)
1417 goto bad_mem;
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 chainmode = strsep(&tmp, "-");
1420 ivopts = strsep(&tmp, "-");
1421 ivmode = strsep(&ivopts, ":");
1422
1423 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01001424 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Milan Broz7dbcd132011-01-13 19:59:52 +00001426 /*
1427 * For compatibility with the original dm-crypt mapping format, if
1428 * only the cipher name is supplied, use cbc-plain.
1429 */
Milan Broz5ebaee62010-08-12 04:14:07 +01001430 if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 chainmode = "cbc";
1432 ivmode = "plain";
1433 }
1434
Herbert Xud1806f62006-08-22 20:29:17 +10001435 if (strcmp(chainmode, "ecb") && !ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001436 ti->error = "IV mechanism required";
1437 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439
Milan Broz5ebaee62010-08-12 04:14:07 +01001440 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
1441 if (!cipher_api)
1442 goto bad_mem;
1443
1444 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
1445 "%s(%s)", chainmode, cipher);
1446 if (ret < 0) {
1447 kfree(cipher_api);
1448 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10001449 }
1450
Milan Broz5ebaee62010-08-12 04:14:07 +01001451 /* Allocate cipher */
Mikulas Patockafbd06f92012-03-27 14:00:17 -07001452 ret = crypt_alloc_tfms(cc, cipher_api);
1453 if (ret < 0) {
1454 ti->error = "Error allocating crypto tfm";
1455 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Milan Broz5ebaee62010-08-12 04:14:07 +01001458 /* Initialize and set key */
1459 ret = crypt_set_key(cc, key);
Milan Broz28513fc2010-08-12 04:14:06 +01001460 if (ret < 0) {
Milan Broz0b430952009-12-10 23:51:55 +00001461 ti->error = "Error decoding and setting key";
Milan Broz28513fc2010-08-12 04:14:06 +01001462 goto bad;
Milan Broz0b430952009-12-10 23:51:55 +00001463 }
1464
Milan Broz5ebaee62010-08-12 04:14:07 +01001465 /* Initialize IV */
Andi Kleenc0297722011-01-13 19:59:53 +00001466 cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
Milan Broz5ebaee62010-08-12 04:14:07 +01001467 if (cc->iv_size)
1468 /* at least a 64 bit sector number should fit in our buffer */
1469 cc->iv_size = max(cc->iv_size,
1470 (unsigned int)(sizeof(u64) / sizeof(u8)));
1471 else if (ivmode) {
1472 DMWARN("Selected cipher does not support IVs");
1473 ivmode = NULL;
1474 }
1475
1476 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (ivmode == NULL)
1478 cc->iv_gen_ops = NULL;
1479 else if (strcmp(ivmode, "plain") == 0)
1480 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00001481 else if (strcmp(ivmode, "plain64") == 0)
1482 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 else if (strcmp(ivmode, "essiv") == 0)
1484 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10001485 else if (strcmp(ivmode, "benbi") == 0)
1486 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07001487 else if (strcmp(ivmode, "null") == 0)
1488 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00001489 else if (strcmp(ivmode, "lmk") == 0) {
1490 cc->iv_gen_ops = &crypt_iv_lmk_ops;
1491 /* Version 2 and 3 is recognised according
1492 * to length of provided multi-key string.
1493 * If present (version 3), last key is used as IV seed.
1494 */
1495 if (cc->key_size % cc->key_parts)
1496 cc->key_parts++;
1497 } else {
Milan Broz5ebaee62010-08-12 04:14:07 +01001498 ret = -EINVAL;
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001499 ti->error = "Invalid IV mode";
Milan Broz28513fc2010-08-12 04:14:06 +01001500 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
1502
Milan Broz28513fc2010-08-12 04:14:06 +01001503 /* Allocate IV */
1504 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
1505 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
1506 if (ret < 0) {
1507 ti->error = "Error creating IV";
1508 goto bad;
1509 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00001510 }
1511
Milan Broz28513fc2010-08-12 04:14:06 +01001512 /* Initialize IV (set keys for ESSIV etc) */
1513 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
1514 ret = cc->iv_gen_ops->init(cc);
1515 if (ret < 0) {
1516 ti->error = "Error initialising IV";
1517 goto bad;
1518 }
1519 }
1520
Milan Broz5ebaee62010-08-12 04:14:07 +01001521 ret = 0;
1522bad:
1523 kfree(cipher_api);
1524 return ret;
1525
1526bad_mem:
1527 ti->error = "Cannot allocate cipher strings";
1528 return -ENOMEM;
1529}
1530
1531/*
1532 * Construct an encryption mapping:
1533 * <cipher> <key> <iv_offset> <dev_path> <start>
1534 */
1535static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1536{
1537 struct crypt_config *cc;
1538 unsigned int key_size;
1539 unsigned long long tmpll;
1540 int ret;
1541
1542 if (argc != 5) {
1543 ti->error = "Not enough arguments";
1544 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546
Milan Broz5ebaee62010-08-12 04:14:07 +01001547 key_size = strlen(argv[1]) >> 1;
1548
1549 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1550 if (!cc) {
1551 ti->error = "Cannot allocate encryption context";
1552 return -ENOMEM;
1553 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00001554 cc->key_size = key_size;
Milan Broz5ebaee62010-08-12 04:14:07 +01001555
1556 ti->private = cc;
1557 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
1558 if (ret < 0)
1559 goto bad;
1560
Milan Broz28513fc2010-08-12 04:14:06 +01001561 ret = -ENOMEM;
Matthew Dobson93d23412006-03-26 01:37:50 -08001562 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 if (!cc->io_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001564 ti->error = "Cannot allocate crypt io mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001565 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567
Milan Brozddd42ed2008-02-08 02:11:07 +00001568 cc->dmreq_start = sizeof(struct ablkcipher_request);
Andi Kleenc0297722011-01-13 19:59:53 +00001569 cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
Milan Brozddd42ed2008-02-08 02:11:07 +00001570 cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
Andi Kleenc0297722011-01-13 19:59:53 +00001571 cc->dmreq_start += crypto_ablkcipher_alignmask(any_tfm(cc)) &
Milan Broz3a7f6c92008-02-08 02:11:14 +00001572 ~(crypto_tfm_ctx_alignment() - 1);
Milan Brozddd42ed2008-02-08 02:11:07 +00001573
1574 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
1575 sizeof(struct dm_crypt_request) + cc->iv_size);
1576 if (!cc->req_pool) {
1577 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001578 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00001579 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001580
Matthew Dobsona19b27c2006-03-26 01:37:45 -08001581 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001583 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001584 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 }
1586
Jens Axboebb799ca2008-12-10 15:35:05 +01001587 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07001588 if (!cc->bs) {
1589 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01001590 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07001591 }
1592
Milan Broz28513fc2010-08-12 04:14:06 +01001593 ret = -EINVAL;
Andrew Morton4ee218c2006-03-27 01:17:48 -08001594 if (sscanf(argv[2], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001595 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001596 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001598 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Milan Broz28513fc2010-08-12 04:14:06 +01001600 if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
1601 ti->error = "Device lookup failed";
1602 goto bad;
1603 }
1604
Andrew Morton4ee218c2006-03-27 01:17:48 -08001605 if (sscanf(argv[4], "%llu", &tmpll) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001606 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001607 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001609 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Milan Broz28513fc2010-08-12 04:14:06 +01001611 ret = -ENOMEM;
Andi Kleenc0297722011-01-13 19:59:53 +00001612 cc->io_queue = alloc_workqueue("kcryptd_io",
1613 WQ_NON_REENTRANT|
1614 WQ_MEM_RECLAIM,
1615 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001616 if (!cc->io_queue) {
1617 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001618 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01001619 }
1620
Andi Kleenc0297722011-01-13 19:59:53 +00001621 cc->crypt_queue = alloc_workqueue("kcryptd",
1622 WQ_NON_REENTRANT|
1623 WQ_CPU_INTENSIVE|
1624 WQ_MEM_RECLAIM,
1625 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001626 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01001627 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001628 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01001629 }
1630
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001631 ti->num_flush_requests = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return 0;
1633
Milan Broz28513fc2010-08-12 04:14:06 +01001634bad:
1635 crypt_dtr(ti);
1636 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639static int crypt_map(struct dm_target *ti, struct bio *bio,
1640 union map_info *map_context)
1641{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001642 struct dm_crypt_io *io;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001643 struct crypt_config *cc;
1644
Tejun Heod87f4c12010-09-03 11:56:19 +02001645 if (bio->bi_rw & REQ_FLUSH) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001646 cc = ti->private;
1647 bio->bi_bdev = cc->dev->bdev;
1648 return DM_MAPIO_REMAPPED;
1649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Alasdair G Kergonb441a2622010-08-12 04:14:11 +01001651 io = crypt_io_alloc(ti, bio, dm_target_offset(ti, bio->bi_sector));
Milan Brozcabf08e2007-10-19 22:38:58 +01001652
Milan Broz20c82532011-01-13 19:59:53 +00001653 if (bio_data_dir(io->base_bio) == READ) {
1654 if (kcryptd_io_read(io, GFP_NOWAIT))
1655 kcryptd_queue_io(io);
1656 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01001657 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001659 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
1662static int crypt_status(struct dm_target *ti, status_type_t type,
1663 char *result, unsigned int maxlen)
1664{
Milan Broz5ebaee62010-08-12 04:14:07 +01001665 struct crypt_config *cc = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 unsigned int sz = 0;
1667
1668 switch (type) {
1669 case STATUSTYPE_INFO:
1670 result[0] = '\0';
1671 break;
1672
1673 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00001674 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 if (cc->key_size > 0) {
1677 if ((maxlen - sz) < ((cc->key_size << 1) + 1))
1678 return -ENOMEM;
1679
1680 crypt_encode_key(result + sz, cc->key, cc->key_size);
1681 sz += cc->key_size << 1;
1682 } else {
1683 if (sz >= maxlen)
1684 return -ENOMEM;
1685 result[sz++] = '-';
1686 }
1687
Andrew Morton4ee218c2006-03-27 01:17:48 -08001688 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1689 cc->dev->name, (unsigned long long)cc->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 break;
1691 }
1692 return 0;
1693}
1694
Milan Broze48d4bb2006-10-03 01:15:37 -07001695static void crypt_postsuspend(struct dm_target *ti)
1696{
1697 struct crypt_config *cc = ti->private;
1698
1699 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1700}
1701
1702static int crypt_preresume(struct dm_target *ti)
1703{
1704 struct crypt_config *cc = ti->private;
1705
1706 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1707 DMERR("aborting resume - crypt key is not set.");
1708 return -EAGAIN;
1709 }
1710
1711 return 0;
1712}
1713
1714static void crypt_resume(struct dm_target *ti)
1715{
1716 struct crypt_config *cc = ti->private;
1717
1718 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1719}
1720
1721/* Message interface
1722 * key set <key>
1723 * key wipe
1724 */
1725static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1726{
1727 struct crypt_config *cc = ti->private;
Milan Broz542da312009-12-10 23:51:57 +00001728 int ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07001729
1730 if (argc < 2)
1731 goto error;
1732
1733 if (!strnicmp(argv[0], MESG_STR("key"))) {
1734 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1735 DMWARN("not suspended during key manipulation.");
1736 return -EINVAL;
1737 }
Milan Broz542da312009-12-10 23:51:57 +00001738 if (argc == 3 && !strnicmp(argv[1], MESG_STR("set"))) {
1739 ret = crypt_set_key(cc, argv[2]);
1740 if (ret)
1741 return ret;
1742 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
1743 ret = cc->iv_gen_ops->init(cc);
1744 return ret;
1745 }
1746 if (argc == 2 && !strnicmp(argv[1], MESG_STR("wipe"))) {
1747 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
1748 ret = cc->iv_gen_ops->wipe(cc);
1749 if (ret)
1750 return ret;
1751 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001752 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00001753 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001754 }
1755
1756error:
1757 DMWARN("unrecognised message received.");
1758 return -EINVAL;
1759}
1760
Milan Brozd41e26b2008-07-21 12:00:40 +01001761static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
1762 struct bio_vec *biovec, int max_size)
1763{
1764 struct crypt_config *cc = ti->private;
1765 struct request_queue *q = bdev_get_queue(cc->dev->bdev);
1766
1767 if (!q->merge_bvec_fn)
1768 return max_size;
1769
1770 bvm->bi_bdev = cc->dev->bdev;
Alasdair G Kergonb441a2622010-08-12 04:14:11 +01001771 bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
Milan Brozd41e26b2008-07-21 12:00:40 +01001772
1773 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
1774}
1775
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001776static int crypt_iterate_devices(struct dm_target *ti,
1777 iterate_devices_callout_fn fn, void *data)
1778{
1779 struct crypt_config *cc = ti->private;
1780
Mike Snitzer5dea2712009-07-23 20:30:42 +01001781 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001782}
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784static struct target_type crypt_target = {
1785 .name = "crypt",
Milan Brozd1f96422011-01-13 19:59:54 +00001786 .version = {1, 10, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 .module = THIS_MODULE,
1788 .ctr = crypt_ctr,
1789 .dtr = crypt_dtr,
1790 .map = crypt_map,
1791 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07001792 .postsuspend = crypt_postsuspend,
1793 .preresume = crypt_preresume,
1794 .resume = crypt_resume,
1795 .message = crypt_message,
Milan Brozd41e26b2008-07-21 12:00:40 +01001796 .merge = crypt_merge,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001797 .iterate_devices = crypt_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798};
1799
1800static int __init dm_crypt_init(void)
1801{
1802 int r;
1803
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001804 _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (!_crypt_io_pool)
1806 return -ENOMEM;
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 r = dm_register_target(&crypt_target);
1809 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001810 DMERR("register failed %d", r);
Milan Broz9934a8b2007-10-19 22:38:57 +01001811 kmem_cache_destroy(_crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return r;
1815}
1816
1817static void __exit dm_crypt_exit(void)
1818{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001819 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 kmem_cache_destroy(_crypt_io_pool);
1821}
1822
1823module_init(dm_crypt_init);
1824module_exit(dm_crypt_exit);
1825
1826MODULE_AUTHOR("Christophe Saout <christophe@saout.de>");
1827MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
1828MODULE_LICENSE("GPL");