blob: 2785007e0e462597d3b8e74839a8bfe25aa90b9c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jana Saoutbf142992014-06-24 14:27:04 -04002 * Copyright (C) 2003 Jana Saout <jana@saout.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * 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>
Arun Sharma600634972011-07-26 16:09:06 -070022#include <linux/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100023#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100025#include <asm/unaligned.h>
Milan Broz34745782011-01-13 19:59:55 +000026#include <crypto/hash.h>
27#include <crypto/md5.h>
28#include <crypto/algapi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Mikulas Patocka586e80e2008-10-21 17:44:59 +010030#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Alasdair G Kergon72d94862006-06-26 00:27:35 -070032#define DM_MSG_PREFIX "crypt"
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;
Kent Overstreet003b5c52013-10-11 15:45:43 -070041 struct bvec_iter iter_in;
42 struct bvec_iter iter_out;
Mikulas Patockac66029f2012-07-27 15:08:05 +010043 sector_t cc_sector;
Mikulas Patocka40b62292012-07-27 15:08:04 +010044 atomic_t cc_pending;
Mikulas Patocka610f2de2014-02-20 18:01:01 -050045 struct ablkcipher_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Milan Broz53017032008-02-08 02:10:38 +000048/*
49 * per bio private data
50 */
51struct dm_crypt_io {
Alasdair G Kergon49a8a922012-07-27 15:08:05 +010052 struct crypt_config *cc;
Milan Broz53017032008-02-08 02:10:38 +000053 struct bio *base_bio;
54 struct work_struct work;
55
56 struct convert_context ctx;
57
Mikulas Patocka40b62292012-07-27 15:08:04 +010058 atomic_t io_pending;
Milan Broz53017032008-02-08 02:10:38 +000059 int error;
Milan Broz0c395b02008-02-08 02:10:54 +000060 sector_t sector;
Milan Broz393b47e2008-10-21 17:45:02 +010061 struct dm_crypt_io *base_io;
Mikulas Patocka298a9fa2014-03-28 15:51:55 -040062} CRYPTO_MINALIGN_ATTR;
Milan Broz53017032008-02-08 02:10:38 +000063
Milan Broz01482b72008-02-08 02:11:04 +000064struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000065 struct convert_context *ctx;
Milan Broz01482b72008-02-08 02:11:04 +000066 struct scatterlist sg_in;
67 struct scatterlist sg_out;
Milan Broz2dc53272011-01-13 19:59:54 +000068 sector_t iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000069};
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071struct crypt_config;
72
73struct crypt_iv_operations {
74 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010075 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000077 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000078 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc53272011-01-13 19:59:54 +000079 int (*generator)(struct crypt_config *cc, u8 *iv,
80 struct dm_crypt_request *dmreq);
81 int (*post)(struct crypt_config *cc, u8 *iv,
82 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Milan Broz60473592009-12-10 23:51:55 +000085struct iv_essiv_private {
Milan Brozb95bf2d2009-12-10 23:51:56 +000086 struct crypto_hash *hash_tfm;
87 u8 *salt;
Milan Broz60473592009-12-10 23:51:55 +000088};
89
90struct iv_benbi_private {
91 int shift;
92};
93
Milan Broz34745782011-01-13 19:59:55 +000094#define LMK_SEED_SIZE 64 /* hash + 0 */
95struct iv_lmk_private {
96 struct crypto_shash *hash_tfm;
97 u8 *seed;
98};
99
Milan Brozed04d982013-10-28 23:21:04 +0100100#define TCW_WHITENING_SIZE 16
101struct iv_tcw_private {
102 struct crypto_shash *crc32_tfm;
103 u8 *iv_seed;
104 u8 *whitening;
105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/*
108 * Crypt: maps a linear range of a block device
109 * and encrypts / decrypts at the same time.
110 */
Milan Broze48d4bb2006-10-03 01:15:37 -0700111enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID };
Andi Kleenc0297722011-01-13 19:59:53 +0000112
113/*
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500114 * The fields in here must be read only after initialization.
Andi Kleenc0297722011-01-13 19:59:53 +0000115 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116struct crypt_config {
117 struct dm_dev *dev;
118 sector_t start;
119
120 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000121 * pool for per bio private data, crypto requests and
122 * encryption requeusts/buffer pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 */
124 mempool_t *io_pool;
Milan Brozddd42ed2008-02-08 02:11:07 +0000125 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -0700127 struct bio_set *bs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Milan Brozcabf08e2007-10-19 22:38:58 +0100129 struct workqueue_struct *io_queue;
130 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700131
Milan Broz5ebaee62010-08-12 04:14:07 +0100132 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000133 char *cipher_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800136 union {
Milan Broz60473592009-12-10 23:51:55 +0000137 struct iv_essiv_private essiv;
138 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000139 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100140 struct iv_tcw_private tcw;
Herbert Xu79066ad2006-12-05 13:41:52 -0800141 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 sector_t iv_offset;
143 unsigned int iv_size;
144
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100145 /* ESSIV: struct crypto_cipher *essiv_tfm */
146 void *iv_private;
147 struct crypto_ablkcipher **tfms;
Milan Brozd1f96422011-01-13 19:59:54 +0000148 unsigned tfms_count;
Andi Kleenc0297722011-01-13 19:59:53 +0000149
150 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000151 * Layout of each crypto request:
152 *
153 * struct ablkcipher_request
154 * context
155 * padding
156 * struct dm_crypt_request
157 * padding
158 * IV
159 *
160 * The padding is added so that dm_crypt_request and the IV are
161 * correctly aligned.
162 */
163 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000164
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400165 unsigned int per_bio_data_size;
166
Milan Broze48d4bb2006-10-03 01:15:37 -0700167 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100169 unsigned int key_parts; /* independent parts in key buffer */
170 unsigned int key_extra_size; /* additional keys length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 u8 key[0];
172};
173
Milan Broz6a24c712006-10-03 01:15:40 -0700174#define MIN_IOS 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#define MIN_POOL_PAGES 32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Christoph Lametere18b8902006-12-06 20:33:20 -0800177static struct kmem_cache *_crypt_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100179static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000180static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Broz2dc53272011-01-13 19:59:54 +0000181static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
Olaf Kirch027581f2007-05-09 02:32:52 -0700182
Andi Kleenc0297722011-01-13 19:59:53 +0000183/*
184 * Use this to access cipher attributes that are the same for each CPU.
185 */
186static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc)
187{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100188 return cc->tfms[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * Different IV generation algorithms:
193 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000194 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200195 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
Milan Broz61afef62009-12-10 23:52:25 +0000197 * plain64: the initial vector is the 64-bit little-endian version of the sector
198 * number, padded with zeros if necessary.
199 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000200 * essiv: "encrypted sector|salt initial vector", the sector number is
201 * encrypted with the bulk cipher using a salt as key. The salt
202 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 *
Rik Snel48527fa2006-09-03 08:56:39 +1000204 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
205 * (needed for LRW-32-AES and possible other narrow block modes)
206 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700207 * null: the initial vector is always zero. Provides compatibility with
208 * obsolete loop_fish2 devices. Do not use for new devices.
209 *
Milan Broz34745782011-01-13 19:59:55 +0000210 * lmk: Compatible implementation of the block chaining mode used
211 * by the Loop-AES block device encryption system
212 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
213 * It operates on full 512 byte sectors and uses CBC
214 * with an IV derived from the sector number, the data and
215 * optionally extra IV seed.
216 * This means that after decryption the first block
217 * of sector must be tweaked according to decrypted data.
218 * Loop-AES can use three encryption schemes:
219 * version 1: is plain aes-cbc mode
220 * version 2: uses 64 multikey scheme with lmk IV generator
221 * version 3: the same as version 2 with additional IV seed
222 * (it uses 65 keys, last key is used as IV seed)
223 *
Milan Brozed04d982013-10-28 23:21:04 +0100224 * tcw: Compatible implementation of the block chaining mode used
225 * by the TrueCrypt device encryption system (prior to version 4.1).
226 * For more info see: http://www.truecrypt.org
227 * It operates on full 512 byte sectors and uses CBC
228 * with an IV derived from initial key and the sector number.
229 * In addition, whitening value is applied on every sector, whitening
230 * is calculated from initial key, sector number and mixed using CRC32.
231 * Note that this encryption scheme is vulnerable to watermarking attacks
232 * and should be used for old compatible containers access only.
233 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * plumb: unimplemented, see:
235 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
236 */
237
Milan Broz2dc53272011-01-13 19:59:54 +0000238static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
239 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100242 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 return 0;
245}
246
Milan Broz61afef62009-12-10 23:52:25 +0000247static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000248 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000249{
250 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100251 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000252
253 return 0;
254}
255
Milan Brozb95bf2d2009-12-10 23:51:56 +0000256/* Initialise ESSIV - compute salt but no local memory allocations */
257static int crypt_iv_essiv_init(struct crypt_config *cc)
258{
259 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
260 struct hash_desc desc;
261 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000262 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100263 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000264
265 sg_init_one(&sg, cc->key, cc->key_size);
266 desc.tfm = essiv->hash_tfm;
267 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
268
269 err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt);
270 if (err)
271 return err;
272
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100273 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000274
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100275 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
276 crypto_hash_digestsize(essiv->hash_tfm));
277 if (err)
278 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000279
280 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000281}
282
Milan Broz542da312009-12-10 23:51:57 +0000283/* Wipe salt and reset key derived from volume key */
284static int crypt_iv_essiv_wipe(struct crypt_config *cc)
285{
286 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
287 unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000288 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100289 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000290
291 memset(essiv->salt, 0, salt_size);
292
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100293 essiv_tfm = cc->iv_private;
294 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
295 if (r)
296 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000297
298 return err;
299}
300
301/* Set up per cpu cipher state */
302static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
303 struct dm_target *ti,
304 u8 *salt, unsigned saltsize)
305{
306 struct crypto_cipher *essiv_tfm;
307 int err;
308
309 /* Setup the essiv_tfm with the given salt */
310 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
311 if (IS_ERR(essiv_tfm)) {
312 ti->error = "Error allocating crypto tfm for ESSIV";
313 return essiv_tfm;
314 }
315
316 if (crypto_cipher_blocksize(essiv_tfm) !=
317 crypto_ablkcipher_ivsize(any_tfm(cc))) {
318 ti->error = "Block size of ESSIV cipher does "
319 "not match IV size of block cipher";
320 crypto_free_cipher(essiv_tfm);
321 return ERR_PTR(-EINVAL);
322 }
323
324 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
325 if (err) {
326 ti->error = "Failed to set key for ESSIV cipher";
327 crypto_free_cipher(essiv_tfm);
328 return ERR_PTR(err);
329 }
330
331 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000332}
333
Milan Broz60473592009-12-10 23:51:55 +0000334static void crypt_iv_essiv_dtr(struct crypt_config *cc)
335{
Andi Kleenc0297722011-01-13 19:59:53 +0000336 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000337 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
338
Milan Brozb95bf2d2009-12-10 23:51:56 +0000339 crypto_free_hash(essiv->hash_tfm);
340 essiv->hash_tfm = NULL;
341
342 kzfree(essiv->salt);
343 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000344
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100345 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000346
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100347 if (essiv_tfm)
348 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000349
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100350 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100354 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Milan Broz5861f1b2009-12-10 23:51:56 +0000356 struct crypto_cipher *essiv_tfm = NULL;
357 struct crypto_hash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000358 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100359 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Milan Broz5861f1b2009-12-10 23:51:56 +0000361 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700362 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return -EINVAL;
364 }
365
Milan Brozb95bf2d2009-12-10 23:51:56 +0000366 /* Allocate hash algorithm */
Herbert Xu35058682006-08-24 19:10:20 +1000367 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
368 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700369 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000370 err = PTR_ERR(hash_tfm);
371 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
Milan Brozb95bf2d2009-12-10 23:51:56 +0000374 salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000375 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700376 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000377 err = -ENOMEM;
378 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
380
Milan Brozb95bf2d2009-12-10 23:51:56 +0000381 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000382 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
383
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100384 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
385 crypto_hash_digestsize(hash_tfm));
386 if (IS_ERR(essiv_tfm)) {
387 crypt_iv_essiv_dtr(cc);
388 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000389 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100390 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000393
394bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000395 if (hash_tfm && !IS_ERR(hash_tfm))
396 crypto_free_hash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000397 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000398 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Milan Broz2dc53272011-01-13 19:59:54 +0000401static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
402 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100404 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100407 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000408 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411}
412
Rik Snel48527fa2006-09-03 08:56:39 +1000413static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
414 const char *opts)
415{
Andi Kleenc0297722011-01-13 19:59:53 +0000416 unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800417 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000418
419 /* we need to calculate how far we must shift the sector count
420 * to get the cipher block count, we use this shift in _gen */
421
422 if (1 << log != bs) {
423 ti->error = "cypher blocksize is not a power of 2";
424 return -EINVAL;
425 }
426
427 if (log > 9) {
428 ti->error = "cypher blocksize is > 512";
429 return -EINVAL;
430 }
431
Milan Broz60473592009-12-10 23:51:55 +0000432 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000433
434 return 0;
435}
436
437static void crypt_iv_benbi_dtr(struct crypt_config *cc)
438{
Rik Snel48527fa2006-09-03 08:56:39 +1000439}
440
Milan Broz2dc53272011-01-13 19:59:54 +0000441static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
442 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000443{
Herbert Xu79066ad2006-12-05 13:41:52 -0800444 __be64 val;
445
Rik Snel48527fa2006-09-03 08:56:39 +1000446 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800447
Milan Broz2dc53272011-01-13 19:59:54 +0000448 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800449 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 0;
452}
453
Milan Broz2dc53272011-01-13 19:59:54 +0000454static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
455 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700456{
457 memset(iv, 0, cc->iv_size);
458
459 return 0;
460}
461
Milan Broz34745782011-01-13 19:59:55 +0000462static void crypt_iv_lmk_dtr(struct crypt_config *cc)
463{
464 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
465
466 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
467 crypto_free_shash(lmk->hash_tfm);
468 lmk->hash_tfm = NULL;
469
470 kzfree(lmk->seed);
471 lmk->seed = NULL;
472}
473
474static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
475 const char *opts)
476{
477 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
478
479 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
480 if (IS_ERR(lmk->hash_tfm)) {
481 ti->error = "Error initializing LMK hash";
482 return PTR_ERR(lmk->hash_tfm);
483 }
484
485 /* No seed in LMK version 2 */
486 if (cc->key_parts == cc->tfms_count) {
487 lmk->seed = NULL;
488 return 0;
489 }
490
491 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
492 if (!lmk->seed) {
493 crypt_iv_lmk_dtr(cc);
494 ti->error = "Error kmallocing seed storage in LMK";
495 return -ENOMEM;
496 }
497
498 return 0;
499}
500
501static int crypt_iv_lmk_init(struct crypt_config *cc)
502{
503 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
504 int subkey_size = cc->key_size / cc->key_parts;
505
506 /* LMK seed is on the position of LMK_KEYS + 1 key */
507 if (lmk->seed)
508 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
509 crypto_shash_digestsize(lmk->hash_tfm));
510
511 return 0;
512}
513
514static int crypt_iv_lmk_wipe(struct crypt_config *cc)
515{
516 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
517
518 if (lmk->seed)
519 memset(lmk->seed, 0, LMK_SEED_SIZE);
520
521 return 0;
522}
523
524static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
525 struct dm_crypt_request *dmreq,
526 u8 *data)
527{
528 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
529 struct {
530 struct shash_desc desc;
531 char ctx[crypto_shash_descsize(lmk->hash_tfm)];
532 } sdesc;
533 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100534 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000535 int i, r;
536
537 sdesc.desc.tfm = lmk->hash_tfm;
538 sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
539
540 r = crypto_shash_init(&sdesc.desc);
541 if (r)
542 return r;
543
544 if (lmk->seed) {
545 r = crypto_shash_update(&sdesc.desc, lmk->seed, LMK_SEED_SIZE);
546 if (r)
547 return r;
548 }
549
550 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
551 r = crypto_shash_update(&sdesc.desc, data + 16, 16 * 31);
552 if (r)
553 return r;
554
555 /* Sector is cropped to 56 bits here */
556 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
557 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
558 buf[2] = cpu_to_le32(4024);
559 buf[3] = 0;
560 r = crypto_shash_update(&sdesc.desc, (u8 *)buf, sizeof(buf));
561 if (r)
562 return r;
563
564 /* No MD5 padding here */
565 r = crypto_shash_export(&sdesc.desc, &md5state);
566 if (r)
567 return r;
568
569 for (i = 0; i < MD5_HASH_WORDS; i++)
570 __cpu_to_le32s(&md5state.hash[i]);
571 memcpy(iv, &md5state.hash, cc->iv_size);
572
573 return 0;
574}
575
576static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
577 struct dm_crypt_request *dmreq)
578{
579 u8 *src;
580 int r = 0;
581
582 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Cong Wangc2e022c2011-11-28 13:26:02 +0800583 src = kmap_atomic(sg_page(&dmreq->sg_in));
Milan Broz34745782011-01-13 19:59:55 +0000584 r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800585 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000586 } else
587 memset(iv, 0, cc->iv_size);
588
589 return r;
590}
591
592static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
593 struct dm_crypt_request *dmreq)
594{
595 u8 *dst;
596 int r;
597
598 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
599 return 0;
600
Cong Wangc2e022c2011-11-28 13:26:02 +0800601 dst = kmap_atomic(sg_page(&dmreq->sg_out));
Milan Broz34745782011-01-13 19:59:55 +0000602 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
603
604 /* Tweak the first block of plaintext sector */
605 if (!r)
606 crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
607
Cong Wangc2e022c2011-11-28 13:26:02 +0800608 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000609 return r;
610}
611
Milan Brozed04d982013-10-28 23:21:04 +0100612static void crypt_iv_tcw_dtr(struct crypt_config *cc)
613{
614 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
615
616 kzfree(tcw->iv_seed);
617 tcw->iv_seed = NULL;
618 kzfree(tcw->whitening);
619 tcw->whitening = NULL;
620
621 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
622 crypto_free_shash(tcw->crc32_tfm);
623 tcw->crc32_tfm = NULL;
624}
625
626static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
627 const char *opts)
628{
629 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
630
631 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
632 ti->error = "Wrong key size for TCW";
633 return -EINVAL;
634 }
635
636 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
637 if (IS_ERR(tcw->crc32_tfm)) {
638 ti->error = "Error initializing CRC32 in TCW";
639 return PTR_ERR(tcw->crc32_tfm);
640 }
641
642 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
643 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
644 if (!tcw->iv_seed || !tcw->whitening) {
645 crypt_iv_tcw_dtr(cc);
646 ti->error = "Error allocating seed storage in TCW";
647 return -ENOMEM;
648 }
649
650 return 0;
651}
652
653static int crypt_iv_tcw_init(struct crypt_config *cc)
654{
655 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
656 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
657
658 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
659 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
660 TCW_WHITENING_SIZE);
661
662 return 0;
663}
664
665static int crypt_iv_tcw_wipe(struct crypt_config *cc)
666{
667 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
668
669 memset(tcw->iv_seed, 0, cc->iv_size);
670 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
671
672 return 0;
673}
674
675static int crypt_iv_tcw_whitening(struct crypt_config *cc,
676 struct dm_crypt_request *dmreq,
677 u8 *data)
678{
679 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
680 u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
681 u8 buf[TCW_WHITENING_SIZE];
682 struct {
683 struct shash_desc desc;
684 char ctx[crypto_shash_descsize(tcw->crc32_tfm)];
685 } sdesc;
686 int i, r;
687
688 /* xor whitening with sector number */
689 memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
690 crypto_xor(buf, (u8 *)&sector, 8);
691 crypto_xor(&buf[8], (u8 *)&sector, 8);
692
693 /* calculate crc32 for every 32bit part and xor it */
694 sdesc.desc.tfm = tcw->crc32_tfm;
695 sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
696 for (i = 0; i < 4; i++) {
697 r = crypto_shash_init(&sdesc.desc);
698 if (r)
699 goto out;
700 r = crypto_shash_update(&sdesc.desc, &buf[i * 4], 4);
701 if (r)
702 goto out;
703 r = crypto_shash_final(&sdesc.desc, &buf[i * 4]);
704 if (r)
705 goto out;
706 }
707 crypto_xor(&buf[0], &buf[12], 4);
708 crypto_xor(&buf[4], &buf[8], 4);
709
710 /* apply whitening (8 bytes) to whole sector */
711 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
712 crypto_xor(data + i * 8, buf, 8);
713out:
714 memset(buf, 0, sizeof(buf));
715 return r;
716}
717
718static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
719 struct dm_crypt_request *dmreq)
720{
721 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
722 u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
723 u8 *src;
724 int r = 0;
725
726 /* Remove whitening from ciphertext */
727 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
728 src = kmap_atomic(sg_page(&dmreq->sg_in));
729 r = crypt_iv_tcw_whitening(cc, dmreq, src + dmreq->sg_in.offset);
730 kunmap_atomic(src);
731 }
732
733 /* Calculate IV */
734 memcpy(iv, tcw->iv_seed, cc->iv_size);
735 crypto_xor(iv, (u8 *)&sector, 8);
736 if (cc->iv_size > 8)
737 crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
738
739 return r;
740}
741
742static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
743 struct dm_crypt_request *dmreq)
744{
745 u8 *dst;
746 int r;
747
748 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
749 return 0;
750
751 /* Apply whitening on ciphertext */
752 dst = kmap_atomic(sg_page(&dmreq->sg_out));
753 r = crypt_iv_tcw_whitening(cc, dmreq, dst + dmreq->sg_out.offset);
754 kunmap_atomic(dst);
755
756 return r;
757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759static struct crypt_iv_operations crypt_iv_plain_ops = {
760 .generator = crypt_iv_plain_gen
761};
762
Milan Broz61afef62009-12-10 23:52:25 +0000763static struct crypt_iv_operations crypt_iv_plain64_ops = {
764 .generator = crypt_iv_plain64_gen
765};
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767static struct crypt_iv_operations crypt_iv_essiv_ops = {
768 .ctr = crypt_iv_essiv_ctr,
769 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000770 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000771 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 .generator = crypt_iv_essiv_gen
773};
774
Rik Snel48527fa2006-09-03 08:56:39 +1000775static struct crypt_iv_operations crypt_iv_benbi_ops = {
776 .ctr = crypt_iv_benbi_ctr,
777 .dtr = crypt_iv_benbi_dtr,
778 .generator = crypt_iv_benbi_gen
779};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Ludwig Nussel46b47732007-05-09 02:32:55 -0700781static struct crypt_iv_operations crypt_iv_null_ops = {
782 .generator = crypt_iv_null_gen
783};
784
Milan Broz34745782011-01-13 19:59:55 +0000785static struct crypt_iv_operations crypt_iv_lmk_ops = {
786 .ctr = crypt_iv_lmk_ctr,
787 .dtr = crypt_iv_lmk_dtr,
788 .init = crypt_iv_lmk_init,
789 .wipe = crypt_iv_lmk_wipe,
790 .generator = crypt_iv_lmk_gen,
791 .post = crypt_iv_lmk_post
792};
793
Milan Brozed04d982013-10-28 23:21:04 +0100794static struct crypt_iv_operations crypt_iv_tcw_ops = {
795 .ctr = crypt_iv_tcw_ctr,
796 .dtr = crypt_iv_tcw_dtr,
797 .init = crypt_iv_tcw_init,
798 .wipe = crypt_iv_tcw_wipe,
799 .generator = crypt_iv_tcw_gen,
800 .post = crypt_iv_tcw_post
801};
802
Milan Brozd469f842007-10-19 22:42:37 +0100803static void crypt_convert_init(struct crypt_config *cc,
804 struct convert_context *ctx,
805 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000806 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 ctx->bio_in = bio_in;
809 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700810 if (bio_in)
811 ctx->iter_in = bio_in->bi_iter;
812 if (bio_out)
813 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100814 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000815 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Huang Yingb2174ee2009-03-16 17:44:33 +0000818static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
819 struct ablkcipher_request *req)
820{
821 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
822}
823
824static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
825 struct dm_crypt_request *dmreq)
826{
827 return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
828}
829
Milan Broz2dc53272011-01-13 19:59:54 +0000830static u8 *iv_of_dmreq(struct crypt_config *cc,
831 struct dm_crypt_request *dmreq)
832{
833 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
834 crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
835}
836
Milan Broz01482b72008-02-08 02:11:04 +0000837static int crypt_convert_block(struct crypt_config *cc,
Milan Broz3a7f6c92008-02-08 02:11:14 +0000838 struct convert_context *ctx,
839 struct ablkcipher_request *req)
Milan Broz01482b72008-02-08 02:11:04 +0000840{
Kent Overstreet003b5c52013-10-11 15:45:43 -0700841 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
842 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000843 struct dm_crypt_request *dmreq;
844 u8 *iv;
Mikulas Patocka40b62292012-07-27 15:08:04 +0100845 int r;
Milan Broz01482b72008-02-08 02:11:04 +0000846
Huang Yingb2174ee2009-03-16 17:44:33 +0000847 dmreq = dmreq_of_req(cc, req);
Milan Broz2dc53272011-01-13 19:59:54 +0000848 iv = iv_of_dmreq(cc, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000849
Mikulas Patockac66029f2012-07-27 15:08:05 +0100850 dmreq->iv_sector = ctx->cc_sector;
Huang Yingb2174ee2009-03-16 17:44:33 +0000851 dmreq->ctx = ctx;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000852 sg_init_table(&dmreq->sg_in, 1);
Kent Overstreet003b5c52013-10-11 15:45:43 -0700853 sg_set_page(&dmreq->sg_in, bv_in.bv_page, 1 << SECTOR_SHIFT,
854 bv_in.bv_offset);
Milan Broz01482b72008-02-08 02:11:04 +0000855
Milan Broz3a7f6c92008-02-08 02:11:14 +0000856 sg_init_table(&dmreq->sg_out, 1);
Kent Overstreet003b5c52013-10-11 15:45:43 -0700857 sg_set_page(&dmreq->sg_out, bv_out.bv_page, 1 << SECTOR_SHIFT,
858 bv_out.bv_offset);
Milan Broz01482b72008-02-08 02:11:04 +0000859
Kent Overstreet003b5c52013-10-11 15:45:43 -0700860 bio_advance_iter(ctx->bio_in, &ctx->iter_in, 1 << SECTOR_SHIFT);
861 bio_advance_iter(ctx->bio_out, &ctx->iter_out, 1 << SECTOR_SHIFT);
Milan Broz01482b72008-02-08 02:11:04 +0000862
Milan Broz3a7f6c92008-02-08 02:11:14 +0000863 if (cc->iv_gen_ops) {
Milan Broz2dc53272011-01-13 19:59:54 +0000864 r = cc->iv_gen_ops->generator(cc, iv, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000865 if (r < 0)
866 return r;
867 }
868
869 ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
870 1 << SECTOR_SHIFT, iv);
871
872 if (bio_data_dir(ctx->bio_in) == WRITE)
873 r = crypto_ablkcipher_encrypt(req);
874 else
875 r = crypto_ablkcipher_decrypt(req);
876
Milan Broz2dc53272011-01-13 19:59:54 +0000877 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
878 r = cc->iv_gen_ops->post(cc, iv, dmreq);
879
Milan Broz3a7f6c92008-02-08 02:11:14 +0000880 return r;
Milan Broz01482b72008-02-08 02:11:04 +0000881}
882
Milan Broz95497a92008-02-08 02:11:12 +0000883static void kcryptd_async_done(struct crypto_async_request *async_req,
884 int error);
Andi Kleenc0297722011-01-13 19:59:53 +0000885
Milan Brozddd42ed2008-02-08 02:11:07 +0000886static void crypt_alloc_req(struct crypt_config *cc,
887 struct convert_context *ctx)
888{
Mikulas Patockac66029f2012-07-27 15:08:05 +0100889 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +0000890
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500891 if (!ctx->req)
892 ctx->req = mempool_alloc(cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +0000893
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500894 ablkcipher_request_set_tfm(ctx->req, cc->tfms[key_index]);
895 ablkcipher_request_set_callback(ctx->req,
Andi Kleenc0297722011-01-13 19:59:53 +0000896 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500897 kcryptd_async_done, dmreq_of_req(cc, ctx->req));
Milan Brozddd42ed2008-02-08 02:11:07 +0000898}
899
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400900static void crypt_free_req(struct crypt_config *cc,
901 struct ablkcipher_request *req, struct bio *base_bio)
902{
903 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
904
905 if ((struct ablkcipher_request *)(io + 1) != req)
906 mempool_free(req, cc->req_pool);
907}
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909/*
910 * Encrypt / decrypt data from one bio to another one (can be the same one)
911 */
912static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +0100913 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Milan Broz3f1e9072008-03-28 14:16:07 -0700915 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Mikulas Patocka40b62292012-07-27 15:08:04 +0100917 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +0100918
Kent Overstreet003b5c52013-10-11 15:45:43 -0700919 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Milan Broz3a7f6c92008-02-08 02:11:14 +0000921 crypt_alloc_req(cc, ctx);
922
Mikulas Patocka40b62292012-07-27 15:08:04 +0100923 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700924
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500925 r = crypt_convert_block(cc, ctx, ctx->req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000926
927 switch (r) {
Milan Broz3f1e9072008-03-28 14:16:07 -0700928 /* async */
Milan Broz3a7f6c92008-02-08 02:11:14 +0000929 case -EBUSY:
930 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -0800931 reinit_completion(&ctx->restart);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000932 /* fall through*/
933 case -EINPROGRESS:
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500934 ctx->req = NULL;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100935 ctx->cc_sector++;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000936 continue;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000937
Milan Broz3f1e9072008-03-28 14:16:07 -0700938 /* sync */
939 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +0100940 atomic_dec(&ctx->cc_pending);
Mikulas Patockac66029f2012-07-27 15:08:05 +0100941 ctx->cc_sector++;
Milan Brozc7f1b202008-07-02 09:34:28 +0100942 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -0700943 continue;
944
945 /* error */
946 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +0100947 atomic_dec(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700948 return r;
949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951
Milan Broz3f1e9072008-03-28 14:16:07 -0700952 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955/*
956 * Generate a new unfragmented bio with the given size
957 * This should never violate the device limitations
Milan Broz933f01d2008-10-10 13:37:08 +0100958 * May return a smaller bio when running out of pages, indicated by
959 * *out_of_pages set to 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 */
Milan Broz933f01d2008-10-10 13:37:08 +0100961static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size,
962 unsigned *out_of_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +0100964 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -0700965 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Al Virob4e3ca12005-10-21 03:22:34 -0400967 gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM;
Milan Broz91e10622007-12-13 14:16:10 +0000968 unsigned i, len;
969 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Olaf Kirch2f9941b2007-05-09 02:32:53 -0700971 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -0700972 if (!clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Olaf Kirch027581f2007-05-09 02:32:52 -0700975 clone_init(io, clone);
Milan Broz933f01d2008-10-10 13:37:08 +0100976 *out_of_pages = 0;
Milan Broz6a24c712006-10-03 01:15:40 -0700977
Olaf Kirchf97380b2007-05-09 02:32:54 -0700978 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +0000979 page = mempool_alloc(cc->page_pool, gfp_mask);
Milan Broz933f01d2008-10-10 13:37:08 +0100980 if (!page) {
981 *out_of_pages = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 break;
Milan Broz933f01d2008-10-10 13:37:08 +0100983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 /*
Mikulas Patockaaeb2dea2012-03-28 18:41:22 +0100986 * If additional pages cannot be allocated without waiting,
987 * return a partially-allocated bio. The caller will then try
988 * to allocate more bios while submitting this partial bio.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 */
Mikulas Patockaaeb2dea2012-03-28 18:41:22 +0100990 gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Milan Broz91e10622007-12-13 14:16:10 +0000992 len = (size > PAGE_SIZE) ? PAGE_SIZE : size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Milan Broz91e10622007-12-13 14:16:10 +0000994 if (!bio_add_page(clone, page, len, 0)) {
995 mempool_free(page, cc->page_pool);
996 break;
997 }
998
999 size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
Kent Overstreet4f024f32013-10-11 15:44:27 -07001002 if (!clone->bi_iter.bi_size) {
Milan Broz8b004452006-10-03 01:15:37 -07001003 bio_put(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return NULL;
1005 }
1006
Milan Broz8b004452006-10-03 01:15:37 -07001007 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Neil Brown644bd2f2007-10-16 13:48:46 +02001010static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Neil Brown644bd2f2007-10-16 13:48:46 +02001012 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 struct bio_vec *bv;
1014
Kent Overstreetcb34e052012-09-05 15:22:02 -07001015 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 BUG_ON(!bv->bv_page);
1017 mempool_free(bv->bv_page, cc->page_pool);
1018 bv->bv_page = NULL;
1019 }
1020}
1021
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001022static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1023 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001024{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001025 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001026 io->base_bio = bio;
1027 io->sector = sector;
1028 io->error = 0;
Milan Broz393b47e2008-10-21 17:45:02 +01001029 io->base_io = NULL;
Mikulas Patocka610f2de2014-02-20 18:01:01 -05001030 io->ctx.req = NULL;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001031 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001032}
1033
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001034static void crypt_inc_pending(struct dm_crypt_io *io)
1035{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001036 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001037}
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039/*
1040 * One of the bios was finished. Check for completion of
1041 * the whole request and correctly clean up the buffer.
Milan Broz393b47e2008-10-21 17:45:02 +01001042 * If base_io is set, wait for the last fragment to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
Milan Broz5742fd72008-02-08 02:10:43 +00001044static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001046 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001047 struct bio *base_bio = io->base_bio;
1048 struct dm_crypt_io *base_io = io->base_io;
1049 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Mikulas Patocka40b62292012-07-27 15:08:04 +01001051 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 return;
1053
Mikulas Patocka610f2de2014-02-20 18:01:01 -05001054 if (io->ctx.req)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001055 crypt_free_req(cc, io->ctx.req, base_bio);
1056 if (io != dm_per_bio_data(base_bio, cc->per_bio_data_size))
1057 mempool_free(io, cc->io_pool);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001058
1059 if (likely(!base_io))
1060 bio_endio(base_bio, error);
1061 else {
1062 if (error && !base_io->error)
1063 base_io->error = error;
1064 crypt_dec_pending(base_io);
1065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
1068/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001069 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 *
1071 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001072 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001073 *
1074 * kcryptd performs the actual encryption or decryption.
1075 *
1076 * kcryptd_io performs the IO submission.
1077 *
1078 * They must be separated as otherwise the final stages could be
1079 * starved by new requests which can block in the first stages due
1080 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001081 *
1082 * The work is done per CPU global for all dm-crypt instances.
1083 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 */
NeilBrown6712ecf2007-09-27 12:47:43 +02001085static void crypt_endio(struct bio *clone, int error)
Milan Broz8b004452006-10-03 01:15:37 -07001086{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001087 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001088 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001089 unsigned rw = bio_data_dir(clone);
Milan Broz8b004452006-10-03 01:15:37 -07001090
Milan Brozadfe4772007-12-13 14:15:51 +00001091 if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
1092 error = -EIO;
1093
Milan Broz8b004452006-10-03 01:15:37 -07001094 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001095 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001096 */
Milan Brozee7a4912008-02-08 02:10:46 +00001097 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001098 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001099
1100 bio_put(clone);
1101
1102 if (rw == READ && !error) {
1103 kcryptd_queue_crypt(io);
1104 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001105 }
Milan Broz8b004452006-10-03 01:15:37 -07001106
Milan Brozadfe4772007-12-13 14:15:51 +00001107 if (unlikely(error))
Milan Broz5742fd72008-02-08 02:10:43 +00001108 io->error = error;
1109
1110 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001111}
1112
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001113static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001114{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001115 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001116
1117 clone->bi_private = io;
1118 clone->bi_end_io = crypt_endio;
1119 clone->bi_bdev = cc->dev->bdev;
1120 clone->bi_rw = io->base_bio->bi_rw;
1121}
1122
Milan Broz20c82532011-01-13 19:59:53 +00001123static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001124{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001125 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001126 struct bio *base_bio = io->base_bio;
1127 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001128
Milan Broz8b004452006-10-03 01:15:37 -07001129 /*
1130 * The block layer might modify the bvec array, so always
1131 * copy the required bvecs because we need the original
1132 * one in order to decrypt the whole bio data *afterwards*.
1133 */
Kent Overstreetbf800ef2012-09-06 15:35:02 -07001134 clone = bio_clone_bioset(base_bio, gfp, cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001135 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001136 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001137
Milan Broz20c82532011-01-13 19:59:53 +00001138 crypt_inc_pending(io);
1139
Milan Broz8b004452006-10-03 01:15:37 -07001140 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001141 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001142
Milan Broz93e605c2006-10-03 01:15:38 -07001143 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001144 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001145}
1146
Milan Broz4e4eef62008-02-08 02:10:49 +00001147static void kcryptd_io_write(struct dm_crypt_io *io)
1148{
Milan Broz95497a92008-02-08 02:11:12 +00001149 struct bio *clone = io->ctx.bio_out;
Milan Broz95497a92008-02-08 02:11:12 +00001150 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +00001151}
1152
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001153static void kcryptd_io(struct work_struct *work)
1154{
1155 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1156
Milan Broz20c82532011-01-13 19:59:53 +00001157 if (bio_data_dir(io->base_bio) == READ) {
1158 crypt_inc_pending(io);
1159 if (kcryptd_io_read(io, GFP_NOIO))
1160 io->error = -ENOMEM;
1161 crypt_dec_pending(io);
1162 } else
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001163 kcryptd_io_write(io);
1164}
1165
1166static void kcryptd_queue_io(struct dm_crypt_io *io)
1167{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001168 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001169
1170 INIT_WORK(&io->work, kcryptd_io);
1171 queue_work(cc->io_queue, &io->work);
1172}
1173
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001174static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001175{
Milan Brozdec1ced2008-02-08 02:10:57 +00001176 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001177 struct crypt_config *cc = io->cc;
Milan Brozdec1ced2008-02-08 02:10:57 +00001178
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001179 if (unlikely(io->error < 0)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001180 crypt_free_buffer_pages(cc, clone);
1181 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001182 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001183 return;
1184 }
1185
1186 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001187 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001188
Kent Overstreet4f024f32013-10-11 15:44:27 -07001189 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001190
Milan Broz95497a92008-02-08 02:11:12 +00001191 if (async)
1192 kcryptd_queue_io(io);
Alasdair G Kergon1e37bb82008-10-10 13:37:05 +01001193 else
Milan Broz95497a92008-02-08 02:11:12 +00001194 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +00001195}
1196
Milan Brozfc5a5e92008-10-10 13:37:04 +01001197static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001198{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001199 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001200 struct bio *clone;
Milan Broz393b47e2008-10-21 17:45:02 +01001201 struct dm_crypt_io *new_io;
Milan Brozc8081612008-10-10 13:37:08 +01001202 int crypt_finished;
Milan Broz933f01d2008-10-10 13:37:08 +01001203 unsigned out_of_pages = 0;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001204 unsigned remaining = io->base_bio->bi_iter.bi_size;
Milan Brozb635b002008-10-21 17:45:00 +01001205 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +00001206 int r;
Milan Broz8b004452006-10-03 01:15:37 -07001207
Milan Broz93e605c2006-10-03 01:15:38 -07001208 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001209 * Prevent io from disappearing until this function completes.
1210 */
1211 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001212 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001213
1214 /*
Milan Broz93e605c2006-10-03 01:15:38 -07001215 * The allocated buffers can be smaller than the whole bio,
1216 * so repeat the whole process until all the data can be handled.
1217 */
1218 while (remaining) {
Milan Broz933f01d2008-10-10 13:37:08 +01001219 clone = crypt_alloc_buffer(io, remaining, &out_of_pages);
Milan Broz23541d22006-10-03 01:15:39 -07001220 if (unlikely(!clone)) {
Milan Broz5742fd72008-02-08 02:10:43 +00001221 io->error = -ENOMEM;
Milan Brozfc5a5e92008-10-10 13:37:04 +01001222 break;
Milan Broz23541d22006-10-03 01:15:39 -07001223 }
Milan Broz93e605c2006-10-03 01:15:38 -07001224
Milan Broz53017032008-02-08 02:10:38 +00001225 io->ctx.bio_out = clone;
Kent Overstreet003b5c52013-10-11 15:45:43 -07001226 io->ctx.iter_out = clone->bi_iter;
Milan Broz93e605c2006-10-03 01:15:38 -07001227
Kent Overstreet4f024f32013-10-11 15:44:27 -07001228 remaining -= clone->bi_iter.bi_size;
Milan Brozb635b002008-10-21 17:45:00 +01001229 sector += bio_sectors(clone);
Milan Brozdec1ced2008-02-08 02:10:57 +00001230
Milan Broz4e594092008-10-10 13:37:07 +01001231 crypt_inc_pending(io);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001232
Milan Brozdec1ced2008-02-08 02:10:57 +00001233 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001234 if (r < 0)
1235 io->error = -EIO;
1236
Mikulas Patocka40b62292012-07-27 15:08:04 +01001237 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
Milan Brozdec1ced2008-02-08 02:10:57 +00001238
Milan Brozc8081612008-10-10 13:37:08 +01001239 /* Encryption was already finished, submit io now */
1240 if (crypt_finished) {
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001241 kcryptd_crypt_write_io_submit(io, 0);
Milan Brozc8081612008-10-10 13:37:08 +01001242
1243 /*
1244 * If there was an error, do not try next fragments.
1245 * For async, error is processed in async handler.
1246 */
Milan Broz6c031f42008-10-10 13:37:06 +01001247 if (unlikely(r < 0))
Milan Brozfc5a5e92008-10-10 13:37:04 +01001248 break;
Milan Brozb635b002008-10-21 17:45:00 +01001249
1250 io->sector = sector;
Milan Broz4e594092008-10-10 13:37:07 +01001251 }
Milan Broz93e605c2006-10-03 01:15:38 -07001252
Milan Broz933f01d2008-10-10 13:37:08 +01001253 /*
1254 * Out of memory -> run queues
1255 * But don't wait if split was due to the io size restriction
1256 */
1257 if (unlikely(out_of_pages))
Jens Axboe8aa7e842009-07-09 14:52:32 +02001258 congestion_wait(BLK_RW_ASYNC, HZ/100);
Milan Broz933f01d2008-10-10 13:37:08 +01001259
Milan Broz393b47e2008-10-21 17:45:02 +01001260 /*
1261 * With async crypto it is unsafe to share the crypto context
1262 * between fragments, so switch to a new dm_crypt_io structure.
1263 */
1264 if (unlikely(!crypt_finished && remaining)) {
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001265 new_io = mempool_alloc(cc->io_pool, GFP_NOIO);
1266 crypt_io_init(new_io, io->cc, io->base_bio, sector);
Milan Broz393b47e2008-10-21 17:45:02 +01001267 crypt_inc_pending(new_io);
1268 crypt_convert_init(cc, &new_io->ctx, NULL,
1269 io->base_bio, sector);
Kent Overstreet003b5c52013-10-11 15:45:43 -07001270 new_io->ctx.iter_in = io->ctx.iter_in;
Milan Broz393b47e2008-10-21 17:45:02 +01001271
1272 /*
1273 * Fragments after the first use the base_io
1274 * pending count.
1275 */
1276 if (!io->base_io)
1277 new_io->base_io = io;
1278 else {
1279 new_io->base_io = io->base_io;
1280 crypt_inc_pending(io->base_io);
1281 crypt_dec_pending(io);
1282 }
1283
1284 io = new_io;
1285 }
Milan Broz8b004452006-10-03 01:15:37 -07001286 }
Milan Broz899c95d2008-02-08 02:11:02 +00001287
1288 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001289}
1290
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001291static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001292{
Milan Broz5742fd72008-02-08 02:10:43 +00001293 crypt_dec_pending(io);
1294}
1295
Milan Broz4e4eef62008-02-08 02:10:49 +00001296static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001297{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001298 struct crypt_config *cc = io->cc;
Milan Broz5742fd72008-02-08 02:10:43 +00001299 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001300
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001301 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001302
Milan Broz53017032008-02-08 02:10:38 +00001303 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001304 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001305
Milan Broz5742fd72008-02-08 02:10:43 +00001306 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001307 if (r < 0)
1308 io->error = -EIO;
Milan Broz5742fd72008-02-08 02:10:43 +00001309
Mikulas Patocka40b62292012-07-27 15:08:04 +01001310 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001311 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001312
1313 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001314}
1315
Milan Broz95497a92008-02-08 02:11:12 +00001316static void kcryptd_async_done(struct crypto_async_request *async_req,
1317 int error)
1318{
Huang Yingb2174ee2009-03-16 17:44:33 +00001319 struct dm_crypt_request *dmreq = async_req->data;
1320 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001321 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001322 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001323
1324 if (error == -EINPROGRESS) {
1325 complete(&ctx->restart);
1326 return;
1327 }
1328
Milan Broz2dc53272011-01-13 19:59:54 +00001329 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
1330 error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
1331
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001332 if (error < 0)
1333 io->error = -EIO;
1334
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001335 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00001336
Mikulas Patocka40b62292012-07-27 15:08:04 +01001337 if (!atomic_dec_and_test(&ctx->cc_pending))
Milan Broz95497a92008-02-08 02:11:12 +00001338 return;
1339
1340 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001341 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001342 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001343 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001344}
1345
Milan Broz4e4eef62008-02-08 02:10:49 +00001346static void kcryptd_crypt(struct work_struct *work)
1347{
1348 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1349
1350 if (bio_data_dir(io->base_bio) == READ)
1351 kcryptd_crypt_read_convert(io);
1352 else
1353 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001354}
1355
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001356static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1357{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001358 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001359
1360 INIT_WORK(&io->work, kcryptd_crypt);
1361 queue_work(cc->crypt_queue, &io->work);
1362}
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364/*
1365 * Decode key from its hex representation
1366 */
1367static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1368{
1369 char buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 unsigned int i;
1371
1372 buffer[2] = '\0';
1373
Milan Broz8b004452006-10-03 01:15:37 -07001374 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 buffer[0] = *hex++;
1376 buffer[1] = *hex++;
1377
majianpeng1a66a082012-07-27 15:07:59 +01001378 if (kstrtou8(buffer, 16, &key[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return -EINVAL;
1380 }
1381
1382 if (*hex != '\0')
1383 return -EINVAL;
1384
1385 return 0;
1386}
1387
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001388static void crypt_free_tfms(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001389{
Milan Brozd1f96422011-01-13 19:59:54 +00001390 unsigned i;
1391
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001392 if (!cc->tfms)
1393 return;
1394
Milan Brozd1f96422011-01-13 19:59:54 +00001395 for (i = 0; i < cc->tfms_count; i++)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001396 if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) {
1397 crypto_free_ablkcipher(cc->tfms[i]);
1398 cc->tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001399 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001400
1401 kfree(cc->tfms);
1402 cc->tfms = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001403}
1404
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001405static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001406{
Milan Brozd1f96422011-01-13 19:59:54 +00001407 unsigned i;
1408 int err;
1409
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001410 cc->tfms = kmalloc(cc->tfms_count * sizeof(struct crypto_ablkcipher *),
1411 GFP_KERNEL);
1412 if (!cc->tfms)
1413 return -ENOMEM;
1414
Milan Brozd1f96422011-01-13 19:59:54 +00001415 for (i = 0; i < cc->tfms_count; i++) {
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001416 cc->tfms[i] = crypto_alloc_ablkcipher(ciphermode, 0, 0);
1417 if (IS_ERR(cc->tfms[i])) {
1418 err = PTR_ERR(cc->tfms[i]);
1419 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001420 return err;
1421 }
1422 }
1423
1424 return 0;
1425}
1426
Andi Kleenc0297722011-01-13 19:59:53 +00001427static int crypt_setkey_allcpus(struct crypt_config *cc)
1428{
Milan Brozda31a072013-10-28 23:21:03 +01001429 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001430 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001431
Milan Brozda31a072013-10-28 23:21:03 +01001432 /* Ignore extra keys (which are used for IV etc) */
1433 subkey_size = (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1434
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001435 for (i = 0; i < cc->tfms_count; i++) {
1436 r = crypto_ablkcipher_setkey(cc->tfms[i],
1437 cc->key + (i * subkey_size),
1438 subkey_size);
1439 if (r)
1440 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001441 }
1442
1443 return err;
1444}
1445
Milan Broze48d4bb2006-10-03 01:15:37 -07001446static int crypt_set_key(struct crypt_config *cc, char *key)
1447{
Milan Brozde8be5a2011-03-24 13:54:27 +00001448 int r = -EINVAL;
1449 int key_string_len = strlen(key);
1450
Milan Broz69a8cfc2011-01-13 19:59:49 +00001451 /* The key size may not be changed. */
Milan Brozde8be5a2011-03-24 13:54:27 +00001452 if (cc->key_size != (key_string_len >> 1))
1453 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001454
Milan Broz69a8cfc2011-01-13 19:59:49 +00001455 /* Hyphen (which gives a key_size of zero) means there is no key. */
1456 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00001457 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001458
Milan Broz69a8cfc2011-01-13 19:59:49 +00001459 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00001460 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001461
1462 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1463
Milan Brozde8be5a2011-03-24 13:54:27 +00001464 r = crypt_setkey_allcpus(cc);
1465
1466out:
1467 /* Hex key string not needed after here, so wipe it. */
1468 memset(key, '0', key_string_len);
1469
1470 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07001471}
1472
1473static int crypt_wipe_key(struct crypt_config *cc)
1474{
1475 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1476 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00001477
1478 return crypt_setkey_allcpus(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07001479}
1480
Milan Broz28513fc2010-08-12 04:14:06 +01001481static void crypt_dtr(struct dm_target *ti)
1482{
1483 struct crypt_config *cc = ti->private;
1484
1485 ti->private = NULL;
1486
1487 if (!cc)
1488 return;
1489
1490 if (cc->io_queue)
1491 destroy_workqueue(cc->io_queue);
1492 if (cc->crypt_queue)
1493 destroy_workqueue(cc->crypt_queue);
1494
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001495 crypt_free_tfms(cc);
1496
Milan Broz28513fc2010-08-12 04:14:06 +01001497 if (cc->bs)
1498 bioset_free(cc->bs);
1499
1500 if (cc->page_pool)
1501 mempool_destroy(cc->page_pool);
1502 if (cc->req_pool)
1503 mempool_destroy(cc->req_pool);
1504 if (cc->io_pool)
1505 mempool_destroy(cc->io_pool);
1506
1507 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1508 cc->iv_gen_ops->dtr(cc);
1509
Milan Broz28513fc2010-08-12 04:14:06 +01001510 if (cc->dev)
1511 dm_put_device(ti, cc->dev);
1512
Milan Broz5ebaee62010-08-12 04:14:07 +01001513 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00001514 kzfree(cc->cipher_string);
Milan Broz28513fc2010-08-12 04:14:06 +01001515
1516 /* Must zero key material before freeing */
1517 kzfree(cc);
1518}
1519
Milan Broz5ebaee62010-08-12 04:14:07 +01001520static int crypt_ctr_cipher(struct dm_target *ti,
1521 char *cipher_in, char *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522{
Milan Broz5ebaee62010-08-12 04:14:07 +01001523 struct crypt_config *cc = ti->private;
Milan Brozd1f96422011-01-13 19:59:54 +00001524 char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01001525 char *cipher_api = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001526 int ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001527 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Milan Broz5ebaee62010-08-12 04:14:07 +01001529 /* Convert to crypto api definition? */
1530 if (strchr(cipher_in, '(')) {
1531 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return -EINVAL;
1533 }
1534
Milan Broz7dbcd132011-01-13 19:59:52 +00001535 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
1536 if (!cc->cipher_string)
1537 goto bad_mem;
1538
Milan Broz5ebaee62010-08-12 04:14:07 +01001539 /*
1540 * Legacy dm-crypt cipher specification
Milan Brozd1f96422011-01-13 19:59:54 +00001541 * cipher[:keycount]-mode-iv:ivopts
Milan Broz5ebaee62010-08-12 04:14:07 +01001542 */
1543 tmp = cipher_in;
Milan Brozd1f96422011-01-13 19:59:54 +00001544 keycount = strsep(&tmp, "-");
1545 cipher = strsep(&keycount, ":");
1546
1547 if (!keycount)
1548 cc->tfms_count = 1;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001549 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
Milan Brozd1f96422011-01-13 19:59:54 +00001550 !is_power_of_2(cc->tfms_count)) {
1551 ti->error = "Bad cipher key count specification";
1552 return -EINVAL;
1553 }
1554 cc->key_parts = cc->tfms_count;
Milan Brozda31a072013-10-28 23:21:03 +01001555 cc->key_extra_size = 0;
Milan Broz5ebaee62010-08-12 04:14:07 +01001556
1557 cc->cipher = kstrdup(cipher, GFP_KERNEL);
1558 if (!cc->cipher)
1559 goto bad_mem;
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 chainmode = strsep(&tmp, "-");
1562 ivopts = strsep(&tmp, "-");
1563 ivmode = strsep(&ivopts, ":");
1564
1565 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01001566 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Milan Broz7dbcd132011-01-13 19:59:52 +00001568 /*
1569 * For compatibility with the original dm-crypt mapping format, if
1570 * only the cipher name is supplied, use cbc-plain.
1571 */
Milan Broz5ebaee62010-08-12 04:14:07 +01001572 if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 chainmode = "cbc";
1574 ivmode = "plain";
1575 }
1576
Herbert Xud1806f62006-08-22 20:29:17 +10001577 if (strcmp(chainmode, "ecb") && !ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001578 ti->error = "IV mechanism required";
1579 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581
Milan Broz5ebaee62010-08-12 04:14:07 +01001582 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
1583 if (!cipher_api)
1584 goto bad_mem;
1585
1586 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
1587 "%s(%s)", chainmode, cipher);
1588 if (ret < 0) {
1589 kfree(cipher_api);
1590 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10001591 }
1592
Milan Broz5ebaee62010-08-12 04:14:07 +01001593 /* Allocate cipher */
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001594 ret = crypt_alloc_tfms(cc, cipher_api);
1595 if (ret < 0) {
1596 ti->error = "Error allocating crypto tfm";
1597 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Milan Broz5ebaee62010-08-12 04:14:07 +01001600 /* Initialize IV */
Andi Kleenc0297722011-01-13 19:59:53 +00001601 cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
Milan Broz5ebaee62010-08-12 04:14:07 +01001602 if (cc->iv_size)
1603 /* at least a 64 bit sector number should fit in our buffer */
1604 cc->iv_size = max(cc->iv_size,
1605 (unsigned int)(sizeof(u64) / sizeof(u8)));
1606 else if (ivmode) {
1607 DMWARN("Selected cipher does not support IVs");
1608 ivmode = NULL;
1609 }
1610
1611 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 if (ivmode == NULL)
1613 cc->iv_gen_ops = NULL;
1614 else if (strcmp(ivmode, "plain") == 0)
1615 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00001616 else if (strcmp(ivmode, "plain64") == 0)
1617 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 else if (strcmp(ivmode, "essiv") == 0)
1619 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10001620 else if (strcmp(ivmode, "benbi") == 0)
1621 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07001622 else if (strcmp(ivmode, "null") == 0)
1623 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00001624 else if (strcmp(ivmode, "lmk") == 0) {
1625 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01001626 /*
1627 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00001628 * to length of provided multi-key string.
1629 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01001630 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00001631 */
Milan Brozda31a072013-10-28 23:21:03 +01001632 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00001633 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01001634 cc->key_extra_size = cc->key_size / cc->key_parts;
1635 }
Milan Brozed04d982013-10-28 23:21:04 +01001636 } else if (strcmp(ivmode, "tcw") == 0) {
1637 cc->iv_gen_ops = &crypt_iv_tcw_ops;
1638 cc->key_parts += 2; /* IV + whitening */
1639 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broz34745782011-01-13 19:59:55 +00001640 } else {
Milan Broz5ebaee62010-08-12 04:14:07 +01001641 ret = -EINVAL;
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001642 ti->error = "Invalid IV mode";
Milan Broz28513fc2010-08-12 04:14:06 +01001643 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645
Milan Brozda31a072013-10-28 23:21:03 +01001646 /* Initialize and set key */
1647 ret = crypt_set_key(cc, key);
1648 if (ret < 0) {
1649 ti->error = "Error decoding and setting key";
1650 goto bad;
1651 }
1652
Milan Broz28513fc2010-08-12 04:14:06 +01001653 /* Allocate IV */
1654 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
1655 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
1656 if (ret < 0) {
1657 ti->error = "Error creating IV";
1658 goto bad;
1659 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00001660 }
1661
Milan Broz28513fc2010-08-12 04:14:06 +01001662 /* Initialize IV (set keys for ESSIV etc) */
1663 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
1664 ret = cc->iv_gen_ops->init(cc);
1665 if (ret < 0) {
1666 ti->error = "Error initialising IV";
1667 goto bad;
1668 }
1669 }
1670
Milan Broz5ebaee62010-08-12 04:14:07 +01001671 ret = 0;
1672bad:
1673 kfree(cipher_api);
1674 return ret;
1675
1676bad_mem:
1677 ti->error = "Cannot allocate cipher strings";
1678 return -ENOMEM;
1679}
1680
1681/*
1682 * Construct an encryption mapping:
1683 * <cipher> <key> <iv_offset> <dev_path> <start>
1684 */
1685static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1686{
1687 struct crypt_config *cc;
Milan Broz772ae5f2011-08-02 12:32:08 +01001688 unsigned int key_size, opt_params;
Milan Broz5ebaee62010-08-12 04:14:07 +01001689 unsigned long long tmpll;
1690 int ret;
Milan Broz772ae5f2011-08-02 12:32:08 +01001691 struct dm_arg_set as;
1692 const char *opt_string;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001693 char dummy;
Milan Broz5ebaee62010-08-12 04:14:07 +01001694
Milan Broz772ae5f2011-08-02 12:32:08 +01001695 static struct dm_arg _args[] = {
1696 {0, 1, "Invalid number of feature args"},
1697 };
1698
1699 if (argc < 5) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001700 ti->error = "Not enough arguments";
1701 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703
Milan Broz5ebaee62010-08-12 04:14:07 +01001704 key_size = strlen(argv[1]) >> 1;
1705
1706 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1707 if (!cc) {
1708 ti->error = "Cannot allocate encryption context";
1709 return -ENOMEM;
1710 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00001711 cc->key_size = key_size;
Milan Broz5ebaee62010-08-12 04:14:07 +01001712
1713 ti->private = cc;
1714 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
1715 if (ret < 0)
1716 goto bad;
1717
Milan Broz28513fc2010-08-12 04:14:06 +01001718 ret = -ENOMEM;
Matthew Dobson93d23412006-03-26 01:37:50 -08001719 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (!cc->io_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001721 ti->error = "Cannot allocate crypt io mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001722 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724
Milan Brozddd42ed2008-02-08 02:11:07 +00001725 cc->dmreq_start = sizeof(struct ablkcipher_request);
Andi Kleenc0297722011-01-13 19:59:53 +00001726 cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
Milan Brozddd42ed2008-02-08 02:11:07 +00001727 cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment());
Andi Kleenc0297722011-01-13 19:59:53 +00001728 cc->dmreq_start += crypto_ablkcipher_alignmask(any_tfm(cc)) &
Milan Broz3a7f6c92008-02-08 02:11:14 +00001729 ~(crypto_tfm_ctx_alignment() - 1);
Milan Brozddd42ed2008-02-08 02:11:07 +00001730
1731 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
1732 sizeof(struct dm_crypt_request) + cc->iv_size);
1733 if (!cc->req_pool) {
1734 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001735 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00001736 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001737
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001738 cc->per_bio_data_size = ti->per_bio_data_size =
1739 sizeof(struct dm_crypt_io) + cc->dmreq_start +
1740 sizeof(struct dm_crypt_request) + cc->iv_size;
1741
Matthew Dobsona19b27c2006-03-26 01:37:45 -08001742 cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001744 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001745 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747
Jens Axboebb799ca2008-12-10 15:35:05 +01001748 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07001749 if (!cc->bs) {
1750 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01001751 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07001752 }
1753
Milan Broz28513fc2010-08-12 04:14:06 +01001754 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001755 if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001756 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001757 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001759 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Milan Broz28513fc2010-08-12 04:14:06 +01001761 if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
1762 ti->error = "Device lookup failed";
1763 goto bad;
1764 }
1765
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001766 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001767 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001768 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001770 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Milan Broz772ae5f2011-08-02 12:32:08 +01001772 argv += 5;
1773 argc -= 5;
1774
1775 /* Optional parameters */
1776 if (argc) {
1777 as.argc = argc;
1778 as.argv = argv;
1779
1780 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
1781 if (ret)
1782 goto bad;
1783
1784 opt_string = dm_shift_arg(&as);
1785
1786 if (opt_params == 1 && opt_string &&
1787 !strcasecmp(opt_string, "allow_discards"))
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001788 ti->num_discard_bios = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01001789 else if (opt_params) {
1790 ret = -EINVAL;
1791 ti->error = "Invalid feature arguments";
1792 goto bad;
1793 }
1794 }
1795
Milan Broz28513fc2010-08-12 04:14:06 +01001796 ret = -ENOMEM;
Tejun Heo670368a2013-07-30 08:40:21 -04001797 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_MEM_RECLAIM, 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001798 if (!cc->io_queue) {
1799 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001800 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01001801 }
1802
Andi Kleenc0297722011-01-13 19:59:53 +00001803 cc->crypt_queue = alloc_workqueue("kcryptd",
Tejun Heo670368a2013-07-30 08:40:21 -04001804 WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001805 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01001806 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001807 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01001808 }
1809
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001810 ti->num_flush_bios = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01001811 ti->discard_zeroes_data_unsupported = true;
Milan Broz983c7db2011-09-25 23:26:21 +01001812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return 0;
1814
Milan Broz28513fc2010-08-12 04:14:06 +01001815bad:
1816 crypt_dtr(ti);
1817 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001820static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001822 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001823 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001824
Milan Broz772ae5f2011-08-02 12:32:08 +01001825 /*
1826 * If bio is REQ_FLUSH or REQ_DISCARD, just bypass crypt queues.
1827 * - for REQ_FLUSH device-mapper core ensures that no IO is in-flight
1828 * - for REQ_DISCARD caller must use flush if IO ordering matters
1829 */
1830 if (unlikely(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD))) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001831 bio->bi_bdev = cc->dev->bdev;
Milan Broz772ae5f2011-08-02 12:32:08 +01001832 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001833 bio->bi_iter.bi_sector = cc->start +
1834 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001835 return DM_MAPIO_REMAPPED;
1836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001838 io = dm_per_bio_data(bio, cc->per_bio_data_size);
1839 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
1840 io->ctx.req = (struct ablkcipher_request *)(io + 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001841
Milan Broz20c82532011-01-13 19:59:53 +00001842 if (bio_data_dir(io->base_bio) == READ) {
1843 if (kcryptd_io_read(io, GFP_NOWAIT))
1844 kcryptd_queue_io(io);
1845 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01001846 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001848 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849}
1850
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001851static void crypt_status(struct dm_target *ti, status_type_t type,
1852 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Milan Broz5ebaee62010-08-12 04:14:07 +01001854 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001855 unsigned i, sz = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 switch (type) {
1858 case STATUSTYPE_INFO:
1859 result[0] = '\0';
1860 break;
1861
1862 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00001863 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001865 if (cc->key_size > 0)
1866 for (i = 0; i < cc->key_size; i++)
1867 DMEMIT("%02x", cc->key[i]);
1868 else
1869 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Andrew Morton4ee218c2006-03-27 01:17:48 -08001871 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1872 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01001873
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001874 if (ti->num_discard_bios)
Milan Broz772ae5f2011-08-02 12:32:08 +01001875 DMEMIT(" 1 allow_discards");
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 break;
1878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
Milan Broze48d4bb2006-10-03 01:15:37 -07001881static void crypt_postsuspend(struct dm_target *ti)
1882{
1883 struct crypt_config *cc = ti->private;
1884
1885 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1886}
1887
1888static int crypt_preresume(struct dm_target *ti)
1889{
1890 struct crypt_config *cc = ti->private;
1891
1892 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1893 DMERR("aborting resume - crypt key is not set.");
1894 return -EAGAIN;
1895 }
1896
1897 return 0;
1898}
1899
1900static void crypt_resume(struct dm_target *ti)
1901{
1902 struct crypt_config *cc = ti->private;
1903
1904 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1905}
1906
1907/* Message interface
1908 * key set <key>
1909 * key wipe
1910 */
1911static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1912{
1913 struct crypt_config *cc = ti->private;
Milan Broz542da312009-12-10 23:51:57 +00001914 int ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07001915
1916 if (argc < 2)
1917 goto error;
1918
Mike Snitzer498f0102011-08-02 12:32:04 +01001919 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07001920 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1921 DMWARN("not suspended during key manipulation.");
1922 return -EINVAL;
1923 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001924 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Milan Broz542da312009-12-10 23:51:57 +00001925 ret = crypt_set_key(cc, argv[2]);
1926 if (ret)
1927 return ret;
1928 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
1929 ret = cc->iv_gen_ops->init(cc);
1930 return ret;
1931 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001932 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00001933 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
1934 ret = cc->iv_gen_ops->wipe(cc);
1935 if (ret)
1936 return ret;
1937 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001938 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00001939 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001940 }
1941
1942error:
1943 DMWARN("unrecognised message received.");
1944 return -EINVAL;
1945}
1946
Milan Brozd41e26b2008-07-21 12:00:40 +01001947static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
1948 struct bio_vec *biovec, int max_size)
1949{
1950 struct crypt_config *cc = ti->private;
1951 struct request_queue *q = bdev_get_queue(cc->dev->bdev);
1952
1953 if (!q->merge_bvec_fn)
1954 return max_size;
1955
1956 bvm->bi_bdev = cc->dev->bdev;
Alasdair G Kergonb441a2622010-08-12 04:14:11 +01001957 bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
Milan Brozd41e26b2008-07-21 12:00:40 +01001958
1959 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
1960}
1961
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001962static int crypt_iterate_devices(struct dm_target *ti,
1963 iterate_devices_callout_fn fn, void *data)
1964{
1965 struct crypt_config *cc = ti->private;
1966
Mike Snitzer5dea2712009-07-23 20:30:42 +01001967 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001968}
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970static struct target_type crypt_target = {
1971 .name = "crypt",
Milan Brozed04d982013-10-28 23:21:04 +01001972 .version = {1, 13, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 .module = THIS_MODULE,
1974 .ctr = crypt_ctr,
1975 .dtr = crypt_dtr,
1976 .map = crypt_map,
1977 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07001978 .postsuspend = crypt_postsuspend,
1979 .preresume = crypt_preresume,
1980 .resume = crypt_resume,
1981 .message = crypt_message,
Milan Brozd41e26b2008-07-21 12:00:40 +01001982 .merge = crypt_merge,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001983 .iterate_devices = crypt_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984};
1985
1986static int __init dm_crypt_init(void)
1987{
1988 int r;
1989
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001990 _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 if (!_crypt_io_pool)
1992 return -ENOMEM;
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 r = dm_register_target(&crypt_target);
1995 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001996 DMERR("register failed %d", r);
Milan Broz9934a8b2007-10-19 22:38:57 +01001997 kmem_cache_destroy(_crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return r;
2001}
2002
2003static void __exit dm_crypt_exit(void)
2004{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002005 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 kmem_cache_destroy(_crypt_io_pool);
2007}
2008
2009module_init(dm_crypt_init);
2010module_exit(dm_crypt_exit);
2011
Jana Saoutbf142992014-06-24 14:27:04 -04002012MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
2014MODULE_LICENSE("GPL");