blob: fa1dba1d06f7f1fc99b99a52a8cbe2d47721484e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jana Saoutbf14299f2014-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;
Mikulas Patocka298a9fa2014-03-28 15:51:55 -040061} CRYPTO_MINALIGN_ATTR;
Milan Broz53017032008-02-08 02:10:38 +000062
Milan Broz01482b72008-02-08 02:11:04 +000063struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000064 struct convert_context *ctx;
Milan Broz01482b72008-02-08 02:11:04 +000065 struct scatterlist sg_in;
66 struct scatterlist sg_out;
Milan Broz2dc53272011-01-13 19:59:54 +000067 sector_t iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000068};
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070struct crypt_config;
71
72struct crypt_iv_operations {
73 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010074 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000076 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000077 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc53272011-01-13 19:59:54 +000078 int (*generator)(struct crypt_config *cc, u8 *iv,
79 struct dm_crypt_request *dmreq);
80 int (*post)(struct crypt_config *cc, u8 *iv,
81 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082};
83
Milan Broz60473592009-12-10 23:51:55 +000084struct iv_essiv_private {
Milan Brozb95bf2d2009-12-10 23:51:56 +000085 struct crypto_hash *hash_tfm;
86 u8 *salt;
Milan Broz60473592009-12-10 23:51:55 +000087};
88
89struct iv_benbi_private {
90 int shift;
91};
92
Milan Broz34745782011-01-13 19:59:55 +000093#define LMK_SEED_SIZE 64 /* hash + 0 */
94struct iv_lmk_private {
95 struct crypto_shash *hash_tfm;
96 u8 *seed;
97};
98
Milan Brozed04d982013-10-28 23:21:04 +010099#define TCW_WHITENING_SIZE 16
100struct iv_tcw_private {
101 struct crypto_shash *crc32_tfm;
102 u8 *iv_seed;
103 u8 *whitening;
104};
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/*
107 * Crypt: maps a linear range of a block device
108 * and encrypts / decrypts at the same time.
109 */
Mikulas Patockaf3396c582015-02-13 08:23:09 -0500110enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID, DM_CRYPT_SAME_CPU };
Andi Kleenc0297722011-01-13 19:59:53 +0000111
112/*
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500113 * The fields in here must be read only after initialization.
Andi Kleenc0297722011-01-13 19:59:53 +0000114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115struct crypt_config {
116 struct dm_dev *dev;
117 sector_t start;
118
119 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000120 * pool for per bio private data, crypto requests and
121 * encryption requeusts/buffer pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 */
123 mempool_t *io_pool;
Milan Brozddd42ed2008-02-08 02:11:07 +0000124 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -0700126 struct bio_set *bs;
Mikulas Patocka7145c242015-02-13 08:24:41 -0500127 struct mutex bio_alloc_lock;
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
Christoph Lametere18b8902006-12-06 20:33:20 -0800176static struct kmem_cache *_crypt_io_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100178static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000179static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Broz2dc53272011-01-13 19:59:54 +0000180static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
Olaf Kirch027581f2007-05-09 02:32:52 -0700181
Andi Kleenc0297722011-01-13 19:59:53 +0000182/*
183 * Use this to access cipher attributes that are the same for each CPU.
184 */
185static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc)
186{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100187 return cc->tfms[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * Different IV generation algorithms:
192 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000193 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200194 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Milan Broz61afef62009-12-10 23:52:25 +0000196 * plain64: the initial vector is the 64-bit little-endian version of the sector
197 * number, padded with zeros if necessary.
198 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000199 * essiv: "encrypted sector|salt initial vector", the sector number is
200 * encrypted with the bulk cipher using a salt as key. The salt
201 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 *
Rik Snel48527fa2006-09-03 08:56:39 +1000203 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
204 * (needed for LRW-32-AES and possible other narrow block modes)
205 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700206 * null: the initial vector is always zero. Provides compatibility with
207 * obsolete loop_fish2 devices. Do not use for new devices.
208 *
Milan Broz34745782011-01-13 19:59:55 +0000209 * lmk: Compatible implementation of the block chaining mode used
210 * by the Loop-AES block device encryption system
211 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
212 * It operates on full 512 byte sectors and uses CBC
213 * with an IV derived from the sector number, the data and
214 * optionally extra IV seed.
215 * This means that after decryption the first block
216 * of sector must be tweaked according to decrypted data.
217 * Loop-AES can use three encryption schemes:
218 * version 1: is plain aes-cbc mode
219 * version 2: uses 64 multikey scheme with lmk IV generator
220 * version 3: the same as version 2 with additional IV seed
221 * (it uses 65 keys, last key is used as IV seed)
222 *
Milan Brozed04d982013-10-28 23:21:04 +0100223 * tcw: Compatible implementation of the block chaining mode used
224 * by the TrueCrypt device encryption system (prior to version 4.1).
225 * For more info see: http://www.truecrypt.org
226 * It operates on full 512 byte sectors and uses CBC
227 * with an IV derived from initial key and the sector number.
228 * In addition, whitening value is applied on every sector, whitening
229 * is calculated from initial key, sector number and mixed using CRC32.
230 * Note that this encryption scheme is vulnerable to watermarking attacks
231 * and should be used for old compatible containers access only.
232 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * plumb: unimplemented, see:
234 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
235 */
236
Milan Broz2dc53272011-01-13 19:59:54 +0000237static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
238 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100241 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 return 0;
244}
245
Milan Broz61afef62009-12-10 23:52:25 +0000246static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000247 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000248{
249 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100250 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000251
252 return 0;
253}
254
Milan Brozb95bf2d2009-12-10 23:51:56 +0000255/* Initialise ESSIV - compute salt but no local memory allocations */
256static int crypt_iv_essiv_init(struct crypt_config *cc)
257{
258 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
259 struct hash_desc desc;
260 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000261 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100262 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000263
264 sg_init_one(&sg, cc->key, cc->key_size);
265 desc.tfm = essiv->hash_tfm;
266 desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP;
267
268 err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt);
269 if (err)
270 return err;
271
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100272 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000273
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100274 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
275 crypto_hash_digestsize(essiv->hash_tfm));
276 if (err)
277 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000278
279 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000280}
281
Milan Broz542da312009-12-10 23:51:57 +0000282/* Wipe salt and reset key derived from volume key */
283static int crypt_iv_essiv_wipe(struct crypt_config *cc)
284{
285 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
286 unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000287 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100288 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000289
290 memset(essiv->salt, 0, salt_size);
291
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100292 essiv_tfm = cc->iv_private;
293 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
294 if (r)
295 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000296
297 return err;
298}
299
300/* Set up per cpu cipher state */
301static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
302 struct dm_target *ti,
303 u8 *salt, unsigned saltsize)
304{
305 struct crypto_cipher *essiv_tfm;
306 int err;
307
308 /* Setup the essiv_tfm with the given salt */
309 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
310 if (IS_ERR(essiv_tfm)) {
311 ti->error = "Error allocating crypto tfm for ESSIV";
312 return essiv_tfm;
313 }
314
315 if (crypto_cipher_blocksize(essiv_tfm) !=
316 crypto_ablkcipher_ivsize(any_tfm(cc))) {
317 ti->error = "Block size of ESSIV cipher does "
318 "not match IV size of block cipher";
319 crypto_free_cipher(essiv_tfm);
320 return ERR_PTR(-EINVAL);
321 }
322
323 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
324 if (err) {
325 ti->error = "Failed to set key for ESSIV cipher";
326 crypto_free_cipher(essiv_tfm);
327 return ERR_PTR(err);
328 }
329
330 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000331}
332
Milan Broz60473592009-12-10 23:51:55 +0000333static void crypt_iv_essiv_dtr(struct crypt_config *cc)
334{
Andi Kleenc0297722011-01-13 19:59:53 +0000335 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000336 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
337
Milan Brozb95bf2d2009-12-10 23:51:56 +0000338 crypto_free_hash(essiv->hash_tfm);
339 essiv->hash_tfm = NULL;
340
341 kzfree(essiv->salt);
342 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000343
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100344 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000345
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100346 if (essiv_tfm)
347 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000348
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100349 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100353 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Milan Broz5861f1b2009-12-10 23:51:56 +0000355 struct crypto_cipher *essiv_tfm = NULL;
356 struct crypto_hash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000357 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100358 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Milan Broz5861f1b2009-12-10 23:51:56 +0000360 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700361 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return -EINVAL;
363 }
364
Milan Brozb95bf2d2009-12-10 23:51:56 +0000365 /* Allocate hash algorithm */
Herbert Xu35058682006-08-24 19:10:20 +1000366 hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC);
367 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700368 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000369 err = PTR_ERR(hash_tfm);
370 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
372
Milan Brozb95bf2d2009-12-10 23:51:56 +0000373 salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000374 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700375 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000376 err = -ENOMEM;
377 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
Milan Brozb95bf2d2009-12-10 23:51:56 +0000380 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000381 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
382
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100383 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
384 crypto_hash_digestsize(hash_tfm));
385 if (IS_ERR(essiv_tfm)) {
386 crypt_iv_essiv_dtr(cc);
387 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000388 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100389 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000392
393bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000394 if (hash_tfm && !IS_ERR(hash_tfm))
395 crypto_free_hash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000396 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000397 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Milan Broz2dc53272011-01-13 19:59:54 +0000400static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
401 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100403 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100406 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000407 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return 0;
410}
411
Rik Snel48527fa2006-09-03 08:56:39 +1000412static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
413 const char *opts)
414{
Andi Kleenc0297722011-01-13 19:59:53 +0000415 unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800416 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000417
418 /* we need to calculate how far we must shift the sector count
419 * to get the cipher block count, we use this shift in _gen */
420
421 if (1 << log != bs) {
422 ti->error = "cypher blocksize is not a power of 2";
423 return -EINVAL;
424 }
425
426 if (log > 9) {
427 ti->error = "cypher blocksize is > 512";
428 return -EINVAL;
429 }
430
Milan Broz60473592009-12-10 23:51:55 +0000431 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000432
433 return 0;
434}
435
436static void crypt_iv_benbi_dtr(struct crypt_config *cc)
437{
Rik Snel48527fa2006-09-03 08:56:39 +1000438}
439
Milan Broz2dc53272011-01-13 19:59:54 +0000440static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
441 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000442{
Herbert Xu79066ad2006-12-05 13:41:52 -0800443 __be64 val;
444
Rik Snel48527fa2006-09-03 08:56:39 +1000445 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800446
Milan Broz2dc53272011-01-13 19:59:54 +0000447 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800448 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0;
451}
452
Milan Broz2dc53272011-01-13 19:59:54 +0000453static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
454 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700455{
456 memset(iv, 0, cc->iv_size);
457
458 return 0;
459}
460
Milan Broz34745782011-01-13 19:59:55 +0000461static void crypt_iv_lmk_dtr(struct crypt_config *cc)
462{
463 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
464
465 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
466 crypto_free_shash(lmk->hash_tfm);
467 lmk->hash_tfm = NULL;
468
469 kzfree(lmk->seed);
470 lmk->seed = NULL;
471}
472
473static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
474 const char *opts)
475{
476 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
477
478 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
479 if (IS_ERR(lmk->hash_tfm)) {
480 ti->error = "Error initializing LMK hash";
481 return PTR_ERR(lmk->hash_tfm);
482 }
483
484 /* No seed in LMK version 2 */
485 if (cc->key_parts == cc->tfms_count) {
486 lmk->seed = NULL;
487 return 0;
488 }
489
490 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
491 if (!lmk->seed) {
492 crypt_iv_lmk_dtr(cc);
493 ti->error = "Error kmallocing seed storage in LMK";
494 return -ENOMEM;
495 }
496
497 return 0;
498}
499
500static int crypt_iv_lmk_init(struct crypt_config *cc)
501{
502 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
503 int subkey_size = cc->key_size / cc->key_parts;
504
505 /* LMK seed is on the position of LMK_KEYS + 1 key */
506 if (lmk->seed)
507 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
508 crypto_shash_digestsize(lmk->hash_tfm));
509
510 return 0;
511}
512
513static int crypt_iv_lmk_wipe(struct crypt_config *cc)
514{
515 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
516
517 if (lmk->seed)
518 memset(lmk->seed, 0, LMK_SEED_SIZE);
519
520 return 0;
521}
522
523static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
524 struct dm_crypt_request *dmreq,
525 u8 *data)
526{
527 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200528 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000529 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100530 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000531 int i, r;
532
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200533 desc->tfm = lmk->hash_tfm;
534 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Broz34745782011-01-13 19:59:55 +0000535
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200536 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000537 if (r)
538 return r;
539
540 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200541 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000542 if (r)
543 return r;
544 }
545
546 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200547 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000548 if (r)
549 return r;
550
551 /* Sector is cropped to 56 bits here */
552 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
553 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
554 buf[2] = cpu_to_le32(4024);
555 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200556 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000557 if (r)
558 return r;
559
560 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200561 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000562 if (r)
563 return r;
564
565 for (i = 0; i < MD5_HASH_WORDS; i++)
566 __cpu_to_le32s(&md5state.hash[i]);
567 memcpy(iv, &md5state.hash, cc->iv_size);
568
569 return 0;
570}
571
572static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
573 struct dm_crypt_request *dmreq)
574{
575 u8 *src;
576 int r = 0;
577
578 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Cong Wangc2e022c2011-11-28 13:26:02 +0800579 src = kmap_atomic(sg_page(&dmreq->sg_in));
Milan Broz34745782011-01-13 19:59:55 +0000580 r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800581 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000582 } else
583 memset(iv, 0, cc->iv_size);
584
585 return r;
586}
587
588static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
589 struct dm_crypt_request *dmreq)
590{
591 u8 *dst;
592 int r;
593
594 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
595 return 0;
596
Cong Wangc2e022c2011-11-28 13:26:02 +0800597 dst = kmap_atomic(sg_page(&dmreq->sg_out));
Milan Broz34745782011-01-13 19:59:55 +0000598 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
599
600 /* Tweak the first block of plaintext sector */
601 if (!r)
602 crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
603
Cong Wangc2e022c2011-11-28 13:26:02 +0800604 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000605 return r;
606}
607
Milan Brozed04d982013-10-28 23:21:04 +0100608static void crypt_iv_tcw_dtr(struct crypt_config *cc)
609{
610 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
611
612 kzfree(tcw->iv_seed);
613 tcw->iv_seed = NULL;
614 kzfree(tcw->whitening);
615 tcw->whitening = NULL;
616
617 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
618 crypto_free_shash(tcw->crc32_tfm);
619 tcw->crc32_tfm = NULL;
620}
621
622static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
623 const char *opts)
624{
625 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
626
627 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
628 ti->error = "Wrong key size for TCW";
629 return -EINVAL;
630 }
631
632 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
633 if (IS_ERR(tcw->crc32_tfm)) {
634 ti->error = "Error initializing CRC32 in TCW";
635 return PTR_ERR(tcw->crc32_tfm);
636 }
637
638 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
639 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
640 if (!tcw->iv_seed || !tcw->whitening) {
641 crypt_iv_tcw_dtr(cc);
642 ti->error = "Error allocating seed storage in TCW";
643 return -ENOMEM;
644 }
645
646 return 0;
647}
648
649static int crypt_iv_tcw_init(struct crypt_config *cc)
650{
651 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
652 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
653
654 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
655 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
656 TCW_WHITENING_SIZE);
657
658 return 0;
659}
660
661static int crypt_iv_tcw_wipe(struct crypt_config *cc)
662{
663 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
664
665 memset(tcw->iv_seed, 0, cc->iv_size);
666 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
667
668 return 0;
669}
670
671static int crypt_iv_tcw_whitening(struct crypt_config *cc,
672 struct dm_crypt_request *dmreq,
673 u8 *data)
674{
675 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
676 u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
677 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200678 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100679 int i, r;
680
681 /* xor whitening with sector number */
682 memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
683 crypto_xor(buf, (u8 *)&sector, 8);
684 crypto_xor(&buf[8], (u8 *)&sector, 8);
685
686 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200687 desc->tfm = tcw->crc32_tfm;
688 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Brozed04d982013-10-28 23:21:04 +0100689 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200690 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100691 if (r)
692 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200693 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100694 if (r)
695 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200696 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100697 if (r)
698 goto out;
699 }
700 crypto_xor(&buf[0], &buf[12], 4);
701 crypto_xor(&buf[4], &buf[8], 4);
702
703 /* apply whitening (8 bytes) to whole sector */
704 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
705 crypto_xor(data + i * 8, buf, 8);
706out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100707 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100708 return r;
709}
710
711static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
712 struct dm_crypt_request *dmreq)
713{
714 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
715 u64 sector = cpu_to_le64((u64)dmreq->iv_sector);
716 u8 *src;
717 int r = 0;
718
719 /* Remove whitening from ciphertext */
720 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
721 src = kmap_atomic(sg_page(&dmreq->sg_in));
722 r = crypt_iv_tcw_whitening(cc, dmreq, src + dmreq->sg_in.offset);
723 kunmap_atomic(src);
724 }
725
726 /* Calculate IV */
727 memcpy(iv, tcw->iv_seed, cc->iv_size);
728 crypto_xor(iv, (u8 *)&sector, 8);
729 if (cc->iv_size > 8)
730 crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
731
732 return r;
733}
734
735static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
736 struct dm_crypt_request *dmreq)
737{
738 u8 *dst;
739 int r;
740
741 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
742 return 0;
743
744 /* Apply whitening on ciphertext */
745 dst = kmap_atomic(sg_page(&dmreq->sg_out));
746 r = crypt_iv_tcw_whitening(cc, dmreq, dst + dmreq->sg_out.offset);
747 kunmap_atomic(dst);
748
749 return r;
750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752static struct crypt_iv_operations crypt_iv_plain_ops = {
753 .generator = crypt_iv_plain_gen
754};
755
Milan Broz61afef62009-12-10 23:52:25 +0000756static struct crypt_iv_operations crypt_iv_plain64_ops = {
757 .generator = crypt_iv_plain64_gen
758};
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760static struct crypt_iv_operations crypt_iv_essiv_ops = {
761 .ctr = crypt_iv_essiv_ctr,
762 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000763 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000764 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 .generator = crypt_iv_essiv_gen
766};
767
Rik Snel48527fa2006-09-03 08:56:39 +1000768static struct crypt_iv_operations crypt_iv_benbi_ops = {
769 .ctr = crypt_iv_benbi_ctr,
770 .dtr = crypt_iv_benbi_dtr,
771 .generator = crypt_iv_benbi_gen
772};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Ludwig Nussel46b47732007-05-09 02:32:55 -0700774static struct crypt_iv_operations crypt_iv_null_ops = {
775 .generator = crypt_iv_null_gen
776};
777
Milan Broz34745782011-01-13 19:59:55 +0000778static struct crypt_iv_operations crypt_iv_lmk_ops = {
779 .ctr = crypt_iv_lmk_ctr,
780 .dtr = crypt_iv_lmk_dtr,
781 .init = crypt_iv_lmk_init,
782 .wipe = crypt_iv_lmk_wipe,
783 .generator = crypt_iv_lmk_gen,
784 .post = crypt_iv_lmk_post
785};
786
Milan Brozed04d982013-10-28 23:21:04 +0100787static struct crypt_iv_operations crypt_iv_tcw_ops = {
788 .ctr = crypt_iv_tcw_ctr,
789 .dtr = crypt_iv_tcw_dtr,
790 .init = crypt_iv_tcw_init,
791 .wipe = crypt_iv_tcw_wipe,
792 .generator = crypt_iv_tcw_gen,
793 .post = crypt_iv_tcw_post
794};
795
Milan Brozd469f842007-10-19 22:42:37 +0100796static void crypt_convert_init(struct crypt_config *cc,
797 struct convert_context *ctx,
798 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000799 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 ctx->bio_in = bio_in;
802 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700803 if (bio_in)
804 ctx->iter_in = bio_in->bi_iter;
805 if (bio_out)
806 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100807 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000808 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Huang Yingb2174ee2009-03-16 17:44:33 +0000811static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
812 struct ablkcipher_request *req)
813{
814 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
815}
816
817static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc,
818 struct dm_crypt_request *dmreq)
819{
820 return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start);
821}
822
Milan Broz2dc53272011-01-13 19:59:54 +0000823static u8 *iv_of_dmreq(struct crypt_config *cc,
824 struct dm_crypt_request *dmreq)
825{
826 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
827 crypto_ablkcipher_alignmask(any_tfm(cc)) + 1);
828}
829
Milan Broz01482b72008-02-08 02:11:04 +0000830static int crypt_convert_block(struct crypt_config *cc,
Milan Broz3a7f6c92008-02-08 02:11:14 +0000831 struct convert_context *ctx,
832 struct ablkcipher_request *req)
Milan Broz01482b72008-02-08 02:11:04 +0000833{
Kent Overstreet003b5c52013-10-11 15:45:43 -0700834 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
835 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000836 struct dm_crypt_request *dmreq;
837 u8 *iv;
Mikulas Patocka40b62292012-07-27 15:08:04 +0100838 int r;
Milan Broz01482b72008-02-08 02:11:04 +0000839
Huang Yingb2174ee2009-03-16 17:44:33 +0000840 dmreq = dmreq_of_req(cc, req);
Milan Broz2dc53272011-01-13 19:59:54 +0000841 iv = iv_of_dmreq(cc, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000842
Mikulas Patockac66029f2012-07-27 15:08:05 +0100843 dmreq->iv_sector = ctx->cc_sector;
Huang Yingb2174ee2009-03-16 17:44:33 +0000844 dmreq->ctx = ctx;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000845 sg_init_table(&dmreq->sg_in, 1);
Kent Overstreet003b5c52013-10-11 15:45:43 -0700846 sg_set_page(&dmreq->sg_in, bv_in.bv_page, 1 << SECTOR_SHIFT,
847 bv_in.bv_offset);
Milan Broz01482b72008-02-08 02:11:04 +0000848
Milan Broz3a7f6c92008-02-08 02:11:14 +0000849 sg_init_table(&dmreq->sg_out, 1);
Kent Overstreet003b5c52013-10-11 15:45:43 -0700850 sg_set_page(&dmreq->sg_out, bv_out.bv_page, 1 << SECTOR_SHIFT,
851 bv_out.bv_offset);
Milan Broz01482b72008-02-08 02:11:04 +0000852
Kent Overstreet003b5c52013-10-11 15:45:43 -0700853 bio_advance_iter(ctx->bio_in, &ctx->iter_in, 1 << SECTOR_SHIFT);
854 bio_advance_iter(ctx->bio_out, &ctx->iter_out, 1 << SECTOR_SHIFT);
Milan Broz01482b72008-02-08 02:11:04 +0000855
Milan Broz3a7f6c92008-02-08 02:11:14 +0000856 if (cc->iv_gen_ops) {
Milan Broz2dc53272011-01-13 19:59:54 +0000857 r = cc->iv_gen_ops->generator(cc, iv, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000858 if (r < 0)
859 return r;
860 }
861
862 ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
863 1 << SECTOR_SHIFT, iv);
864
865 if (bio_data_dir(ctx->bio_in) == WRITE)
866 r = crypto_ablkcipher_encrypt(req);
867 else
868 r = crypto_ablkcipher_decrypt(req);
869
Milan Broz2dc53272011-01-13 19:59:54 +0000870 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
871 r = cc->iv_gen_ops->post(cc, iv, dmreq);
872
Milan Broz3a7f6c92008-02-08 02:11:14 +0000873 return r;
Milan Broz01482b72008-02-08 02:11:04 +0000874}
875
Milan Broz95497a92008-02-08 02:11:12 +0000876static void kcryptd_async_done(struct crypto_async_request *async_req,
877 int error);
Andi Kleenc0297722011-01-13 19:59:53 +0000878
Milan Brozddd42ed2008-02-08 02:11:07 +0000879static void crypt_alloc_req(struct crypt_config *cc,
880 struct convert_context *ctx)
881{
Mikulas Patockac66029f2012-07-27 15:08:05 +0100882 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +0000883
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500884 if (!ctx->req)
885 ctx->req = mempool_alloc(cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +0000886
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500887 ablkcipher_request_set_tfm(ctx->req, cc->tfms[key_index]);
888 ablkcipher_request_set_callback(ctx->req,
Andi Kleenc0297722011-01-13 19:59:53 +0000889 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500890 kcryptd_async_done, dmreq_of_req(cc, ctx->req));
Milan Brozddd42ed2008-02-08 02:11:07 +0000891}
892
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400893static void crypt_free_req(struct crypt_config *cc,
894 struct ablkcipher_request *req, struct bio *base_bio)
895{
896 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
897
898 if ((struct ablkcipher_request *)(io + 1) != req)
899 mempool_free(req, cc->req_pool);
900}
901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902/*
903 * Encrypt / decrypt data from one bio to another one (can be the same one)
904 */
905static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +0100906 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Milan Broz3f1e9072008-03-28 14:16:07 -0700908 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Mikulas Patocka40b62292012-07-27 15:08:04 +0100910 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +0100911
Kent Overstreet003b5c52013-10-11 15:45:43 -0700912 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Milan Broz3a7f6c92008-02-08 02:11:14 +0000914 crypt_alloc_req(cc, ctx);
915
Mikulas Patocka40b62292012-07-27 15:08:04 +0100916 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700917
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500918 r = crypt_convert_block(cc, ctx, ctx->req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000919
920 switch (r) {
Milan Broz3f1e9072008-03-28 14:16:07 -0700921 /* async */
Milan Broz3a7f6c92008-02-08 02:11:14 +0000922 case -EBUSY:
923 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -0800924 reinit_completion(&ctx->restart);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000925 /* fall through*/
926 case -EINPROGRESS:
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500927 ctx->req = NULL;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100928 ctx->cc_sector++;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000929 continue;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000930
Milan Broz3f1e9072008-03-28 14:16:07 -0700931 /* sync */
932 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +0100933 atomic_dec(&ctx->cc_pending);
Mikulas Patockac66029f2012-07-27 15:08:05 +0100934 ctx->cc_sector++;
Milan Brozc7f1b202008-07-02 09:34:28 +0100935 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -0700936 continue;
937
938 /* error */
939 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +0100940 atomic_dec(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700941 return r;
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944
Milan Broz3f1e9072008-03-28 14:16:07 -0700945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -0500948static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
949
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950/*
951 * Generate a new unfragmented bio with the given size
952 * This should never violate the device limitations
Mikulas Patocka7145c242015-02-13 08:24:41 -0500953 *
954 * This function may be called concurrently. If we allocate from the mempool
955 * concurrently, there is a possibility of deadlock. For example, if we have
956 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
957 * the mempool concurrently, it may deadlock in a situation where both processes
958 * have allocated 128 pages and the mempool is exhausted.
959 *
960 * In order to avoid this scenario we allocate the pages under a mutex.
961 *
962 * In order to not degrade performance with excessive locking, we try
963 * non-blocking allocations without a mutex first but on failure we fallback
964 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -0500966static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +0100968 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -0700969 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -0500971 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
972 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +0000973 struct page *page;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -0500974 struct bio_vec *bvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Mikulas Patocka7145c242015-02-13 08:24:41 -0500976retry:
977 if (unlikely(gfp_mask & __GFP_WAIT))
978 mutex_lock(&cc->bio_alloc_lock);
979
Olaf Kirch2f9941b2007-05-09 02:32:53 -0700980 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -0700981 if (!clone)
Mikulas Patocka7145c242015-02-13 08:24:41 -0500982 goto return_clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Olaf Kirch027581f2007-05-09 02:32:52 -0700984 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -0700985
Mikulas Patocka7145c242015-02-13 08:24:41 -0500986 remaining_size = size;
987
Olaf Kirchf97380b2007-05-09 02:32:54 -0700988 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +0000989 page = mempool_alloc(cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -0500990 if (!page) {
991 crypt_free_buffer_pages(cc, clone);
992 bio_put(clone);
993 gfp_mask |= __GFP_WAIT;
994 goto retry;
995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Mikulas Patocka7145c242015-02-13 08:24:41 -0500997 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -0500999 bvec = &clone->bi_io_vec[clone->bi_vcnt++];
1000 bvec->bv_page = page;
1001 bvec->bv_len = len;
1002 bvec->bv_offset = 0;
1003
1004 clone->bi_iter.bi_size += len;
Milan Broz91e10622007-12-13 14:16:10 +00001005
Mikulas Patocka7145c242015-02-13 08:24:41 -05001006 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008
Mikulas Patocka7145c242015-02-13 08:24:41 -05001009return_clone:
1010 if (unlikely(gfp_mask & __GFP_WAIT))
1011 mutex_unlock(&cc->bio_alloc_lock);
1012
Milan Broz8b004452006-10-03 01:15:37 -07001013 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014}
1015
Neil Brown644bd2f2007-10-16 13:48:46 +02001016static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Neil Brown644bd2f2007-10-16 13:48:46 +02001018 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 struct bio_vec *bv;
1020
Kent Overstreetcb34e052012-09-05 15:22:02 -07001021 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 BUG_ON(!bv->bv_page);
1023 mempool_free(bv->bv_page, cc->page_pool);
1024 bv->bv_page = NULL;
1025 }
1026}
1027
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001028static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1029 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001030{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001031 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001032 io->base_bio = bio;
1033 io->sector = sector;
1034 io->error = 0;
Mikulas Patocka610f2de2014-02-20 18:01:01 -05001035 io->ctx.req = NULL;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001036 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001037}
1038
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001039static void crypt_inc_pending(struct dm_crypt_io *io)
1040{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001041 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001042}
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044/*
1045 * One of the bios was finished. Check for completion of
1046 * the whole request and correctly clean up the buffer.
1047 */
Milan Broz5742fd72008-02-08 02:10:43 +00001048static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001050 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001051 struct bio *base_bio = io->base_bio;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001052 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Mikulas Patocka40b62292012-07-27 15:08:04 +01001054 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return;
1056
Mikulas Patocka610f2de2014-02-20 18:01:01 -05001057 if (io->ctx.req)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001058 crypt_free_req(cc, io->ctx.req, base_bio);
1059 if (io != dm_per_bio_data(base_bio, cc->per_bio_data_size))
1060 mempool_free(io, cc->io_pool);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001061
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001062 bio_endio(base_bio, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001066 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 *
1068 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001069 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001070 *
1071 * kcryptd performs the actual encryption or decryption.
1072 *
1073 * kcryptd_io performs the IO submission.
1074 *
1075 * They must be separated as otherwise the final stages could be
1076 * starved by new requests which can block in the first stages due
1077 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001078 *
1079 * The work is done per CPU global for all dm-crypt instances.
1080 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
NeilBrown6712ecf2007-09-27 12:47:43 +02001082static void crypt_endio(struct bio *clone, int error)
Milan Broz8b004452006-10-03 01:15:37 -07001083{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001084 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001085 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001086 unsigned rw = bio_data_dir(clone);
Milan Broz8b004452006-10-03 01:15:37 -07001087
Milan Brozadfe4772007-12-13 14:15:51 +00001088 if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error))
1089 error = -EIO;
1090
Milan Broz8b004452006-10-03 01:15:37 -07001091 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001092 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001093 */
Milan Brozee7a4912008-02-08 02:10:46 +00001094 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001095 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001096
1097 bio_put(clone);
1098
1099 if (rw == READ && !error) {
1100 kcryptd_queue_crypt(io);
1101 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001102 }
Milan Broz8b004452006-10-03 01:15:37 -07001103
Milan Brozadfe4772007-12-13 14:15:51 +00001104 if (unlikely(error))
Milan Broz5742fd72008-02-08 02:10:43 +00001105 io->error = error;
1106
1107 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001108}
1109
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001110static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001111{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001112 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001113
1114 clone->bi_private = io;
1115 clone->bi_end_io = crypt_endio;
1116 clone->bi_bdev = cc->dev->bdev;
1117 clone->bi_rw = io->base_bio->bi_rw;
1118}
1119
Milan Broz20c82532011-01-13 19:59:53 +00001120static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001121{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001122 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001123 struct bio *base_bio = io->base_bio;
1124 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001125
Milan Broz8b004452006-10-03 01:15:37 -07001126 /*
1127 * The block layer might modify the bvec array, so always
1128 * copy the required bvecs because we need the original
1129 * one in order to decrypt the whole bio data *afterwards*.
1130 */
Kent Overstreetbf800ef2012-09-06 15:35:02 -07001131 clone = bio_clone_bioset(base_bio, gfp, cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001132 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001133 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001134
Milan Broz20c82532011-01-13 19:59:53 +00001135 crypt_inc_pending(io);
1136
Milan Broz8b004452006-10-03 01:15:37 -07001137 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001138 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001139
Milan Broz93e605c2006-10-03 01:15:38 -07001140 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001141 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001142}
1143
Milan Broz4e4eef62008-02-08 02:10:49 +00001144static void kcryptd_io_write(struct dm_crypt_io *io)
1145{
Milan Broz95497a92008-02-08 02:11:12 +00001146 struct bio *clone = io->ctx.bio_out;
Milan Broz95497a92008-02-08 02:11:12 +00001147 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +00001148}
1149
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001150static void kcryptd_io(struct work_struct *work)
1151{
1152 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1153
Milan Broz20c82532011-01-13 19:59:53 +00001154 if (bio_data_dir(io->base_bio) == READ) {
1155 crypt_inc_pending(io);
1156 if (kcryptd_io_read(io, GFP_NOIO))
1157 io->error = -ENOMEM;
1158 crypt_dec_pending(io);
1159 } else
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001160 kcryptd_io_write(io);
1161}
1162
1163static void kcryptd_queue_io(struct dm_crypt_io *io)
1164{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001165 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001166
1167 INIT_WORK(&io->work, kcryptd_io);
1168 queue_work(cc->io_queue, &io->work);
1169}
1170
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001171static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001172{
Milan Brozdec1ced2008-02-08 02:10:57 +00001173 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001174 struct crypt_config *cc = io->cc;
Milan Brozdec1ced2008-02-08 02:10:57 +00001175
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001176 if (unlikely(io->error < 0)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001177 crypt_free_buffer_pages(cc, clone);
1178 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001179 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001180 return;
1181 }
1182
1183 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001184 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001185
Kent Overstreet4f024f32013-10-11 15:44:27 -07001186 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001187
Milan Broz95497a92008-02-08 02:11:12 +00001188 if (async)
1189 kcryptd_queue_io(io);
Alasdair G Kergon1e37bb82008-10-10 13:37:05 +01001190 else
Milan Broz95497a92008-02-08 02:11:12 +00001191 generic_make_request(clone);
Milan Broz4e4eef62008-02-08 02:10:49 +00001192}
1193
Milan Brozfc5a5e92008-10-10 13:37:04 +01001194static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001195{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001196 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001197 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001198 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001199 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +00001200 int r;
Milan Broz8b004452006-10-03 01:15:37 -07001201
Milan Broz93e605c2006-10-03 01:15:38 -07001202 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001203 * Prevent io from disappearing until this function completes.
1204 */
1205 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001206 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001207
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001208 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1209 if (unlikely(!clone)) {
1210 io->error = -EIO;
1211 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001212 }
Milan Broz899c95d2008-02-08 02:11:02 +00001213
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001214 io->ctx.bio_out = clone;
1215 io->ctx.iter_out = clone->bi_iter;
1216
1217 sector += bio_sectors(clone);
1218
1219 crypt_inc_pending(io);
1220 r = crypt_convert(cc, &io->ctx);
1221 if (r)
1222 io->error = -EIO;
1223 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1224
1225 /* Encryption was already finished, submit io now */
1226 if (crypt_finished) {
1227 kcryptd_crypt_write_io_submit(io, 0);
1228 io->sector = sector;
1229 }
1230
1231dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001232 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001233}
1234
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001235static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001236{
Milan Broz5742fd72008-02-08 02:10:43 +00001237 crypt_dec_pending(io);
1238}
1239
Milan Broz4e4eef62008-02-08 02:10:49 +00001240static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001241{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001242 struct crypt_config *cc = io->cc;
Milan Broz5742fd72008-02-08 02:10:43 +00001243 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001244
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001245 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001246
Milan Broz53017032008-02-08 02:10:38 +00001247 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001248 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001249
Milan Broz5742fd72008-02-08 02:10:43 +00001250 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001251 if (r < 0)
1252 io->error = -EIO;
Milan Broz5742fd72008-02-08 02:10:43 +00001253
Mikulas Patocka40b62292012-07-27 15:08:04 +01001254 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001255 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001256
1257 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001258}
1259
Milan Broz95497a92008-02-08 02:11:12 +00001260static void kcryptd_async_done(struct crypto_async_request *async_req,
1261 int error)
1262{
Huang Yingb2174ee2009-03-16 17:44:33 +00001263 struct dm_crypt_request *dmreq = async_req->data;
1264 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001265 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001266 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001267
1268 if (error == -EINPROGRESS) {
1269 complete(&ctx->restart);
1270 return;
1271 }
1272
Milan Broz2dc53272011-01-13 19:59:54 +00001273 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
1274 error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
1275
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001276 if (error < 0)
1277 io->error = -EIO;
1278
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001279 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00001280
Mikulas Patocka40b62292012-07-27 15:08:04 +01001281 if (!atomic_dec_and_test(&ctx->cc_pending))
Milan Broz95497a92008-02-08 02:11:12 +00001282 return;
1283
1284 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001285 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001286 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001287 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001288}
1289
Milan Broz4e4eef62008-02-08 02:10:49 +00001290static void kcryptd_crypt(struct work_struct *work)
1291{
1292 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1293
1294 if (bio_data_dir(io->base_bio) == READ)
1295 kcryptd_crypt_read_convert(io);
1296 else
1297 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001298}
1299
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001300static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1301{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001302 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001303
1304 INIT_WORK(&io->work, kcryptd_crypt);
1305 queue_work(cc->crypt_queue, &io->work);
1306}
1307
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308/*
1309 * Decode key from its hex representation
1310 */
1311static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1312{
1313 char buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 unsigned int i;
1315
1316 buffer[2] = '\0';
1317
Milan Broz8b004452006-10-03 01:15:37 -07001318 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 buffer[0] = *hex++;
1320 buffer[1] = *hex++;
1321
majianpeng1a66a082012-07-27 15:07:59 +01001322 if (kstrtou8(buffer, 16, &key[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return -EINVAL;
1324 }
1325
1326 if (*hex != '\0')
1327 return -EINVAL;
1328
1329 return 0;
1330}
1331
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001332static void crypt_free_tfms(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001333{
Milan Brozd1f96422011-01-13 19:59:54 +00001334 unsigned i;
1335
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001336 if (!cc->tfms)
1337 return;
1338
Milan Brozd1f96422011-01-13 19:59:54 +00001339 for (i = 0; i < cc->tfms_count; i++)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001340 if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) {
1341 crypto_free_ablkcipher(cc->tfms[i]);
1342 cc->tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001343 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001344
1345 kfree(cc->tfms);
1346 cc->tfms = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001347}
1348
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001349static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001350{
Milan Brozd1f96422011-01-13 19:59:54 +00001351 unsigned i;
1352 int err;
1353
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001354 cc->tfms = kmalloc(cc->tfms_count * sizeof(struct crypto_ablkcipher *),
1355 GFP_KERNEL);
1356 if (!cc->tfms)
1357 return -ENOMEM;
1358
Milan Brozd1f96422011-01-13 19:59:54 +00001359 for (i = 0; i < cc->tfms_count; i++) {
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001360 cc->tfms[i] = crypto_alloc_ablkcipher(ciphermode, 0, 0);
1361 if (IS_ERR(cc->tfms[i])) {
1362 err = PTR_ERR(cc->tfms[i]);
1363 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001364 return err;
1365 }
1366 }
1367
1368 return 0;
1369}
1370
Andi Kleenc0297722011-01-13 19:59:53 +00001371static int crypt_setkey_allcpus(struct crypt_config *cc)
1372{
Milan Brozda31a072013-10-28 23:21:03 +01001373 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001374 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001375
Milan Brozda31a072013-10-28 23:21:03 +01001376 /* Ignore extra keys (which are used for IV etc) */
1377 subkey_size = (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1378
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001379 for (i = 0; i < cc->tfms_count; i++) {
1380 r = crypto_ablkcipher_setkey(cc->tfms[i],
1381 cc->key + (i * subkey_size),
1382 subkey_size);
1383 if (r)
1384 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001385 }
1386
1387 return err;
1388}
1389
Milan Broze48d4bb2006-10-03 01:15:37 -07001390static int crypt_set_key(struct crypt_config *cc, char *key)
1391{
Milan Brozde8be5a2011-03-24 13:54:27 +00001392 int r = -EINVAL;
1393 int key_string_len = strlen(key);
1394
Milan Broz69a8cfc2011-01-13 19:59:49 +00001395 /* The key size may not be changed. */
Milan Brozde8be5a2011-03-24 13:54:27 +00001396 if (cc->key_size != (key_string_len >> 1))
1397 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001398
Milan Broz69a8cfc2011-01-13 19:59:49 +00001399 /* Hyphen (which gives a key_size of zero) means there is no key. */
1400 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00001401 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001402
Milan Broz69a8cfc2011-01-13 19:59:49 +00001403 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00001404 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001405
1406 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1407
Milan Brozde8be5a2011-03-24 13:54:27 +00001408 r = crypt_setkey_allcpus(cc);
1409
1410out:
1411 /* Hex key string not needed after here, so wipe it. */
1412 memset(key, '0', key_string_len);
1413
1414 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07001415}
1416
1417static int crypt_wipe_key(struct crypt_config *cc)
1418{
1419 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1420 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00001421
1422 return crypt_setkey_allcpus(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07001423}
1424
Milan Broz28513fc2010-08-12 04:14:06 +01001425static void crypt_dtr(struct dm_target *ti)
1426{
1427 struct crypt_config *cc = ti->private;
1428
1429 ti->private = NULL;
1430
1431 if (!cc)
1432 return;
1433
1434 if (cc->io_queue)
1435 destroy_workqueue(cc->io_queue);
1436 if (cc->crypt_queue)
1437 destroy_workqueue(cc->crypt_queue);
1438
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001439 crypt_free_tfms(cc);
1440
Milan Broz28513fc2010-08-12 04:14:06 +01001441 if (cc->bs)
1442 bioset_free(cc->bs);
1443
1444 if (cc->page_pool)
1445 mempool_destroy(cc->page_pool);
1446 if (cc->req_pool)
1447 mempool_destroy(cc->req_pool);
1448 if (cc->io_pool)
1449 mempool_destroy(cc->io_pool);
1450
1451 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1452 cc->iv_gen_ops->dtr(cc);
1453
Milan Broz28513fc2010-08-12 04:14:06 +01001454 if (cc->dev)
1455 dm_put_device(ti, cc->dev);
1456
Milan Broz5ebaee62010-08-12 04:14:07 +01001457 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00001458 kzfree(cc->cipher_string);
Milan Broz28513fc2010-08-12 04:14:06 +01001459
1460 /* Must zero key material before freeing */
1461 kzfree(cc);
1462}
1463
Milan Broz5ebaee62010-08-12 04:14:07 +01001464static int crypt_ctr_cipher(struct dm_target *ti,
1465 char *cipher_in, char *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Milan Broz5ebaee62010-08-12 04:14:07 +01001467 struct crypt_config *cc = ti->private;
Milan Brozd1f96422011-01-13 19:59:54 +00001468 char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01001469 char *cipher_api = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001470 int ret = -EINVAL;
Mikulas Patocka31998ef12012-03-28 18:41:26 +01001471 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
Milan Broz5ebaee62010-08-12 04:14:07 +01001473 /* Convert to crypto api definition? */
1474 if (strchr(cipher_in, '(')) {
1475 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 return -EINVAL;
1477 }
1478
Milan Broz7dbcd132011-01-13 19:59:52 +00001479 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
1480 if (!cc->cipher_string)
1481 goto bad_mem;
1482
Milan Broz5ebaee62010-08-12 04:14:07 +01001483 /*
1484 * Legacy dm-crypt cipher specification
Milan Brozd1f96422011-01-13 19:59:54 +00001485 * cipher[:keycount]-mode-iv:ivopts
Milan Broz5ebaee62010-08-12 04:14:07 +01001486 */
1487 tmp = cipher_in;
Milan Brozd1f96422011-01-13 19:59:54 +00001488 keycount = strsep(&tmp, "-");
1489 cipher = strsep(&keycount, ":");
1490
1491 if (!keycount)
1492 cc->tfms_count = 1;
Mikulas Patocka31998ef12012-03-28 18:41:26 +01001493 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
Milan Brozd1f96422011-01-13 19:59:54 +00001494 !is_power_of_2(cc->tfms_count)) {
1495 ti->error = "Bad cipher key count specification";
1496 return -EINVAL;
1497 }
1498 cc->key_parts = cc->tfms_count;
Milan Brozda31a072013-10-28 23:21:03 +01001499 cc->key_extra_size = 0;
Milan Broz5ebaee62010-08-12 04:14:07 +01001500
1501 cc->cipher = kstrdup(cipher, GFP_KERNEL);
1502 if (!cc->cipher)
1503 goto bad_mem;
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 chainmode = strsep(&tmp, "-");
1506 ivopts = strsep(&tmp, "-");
1507 ivmode = strsep(&ivopts, ":");
1508
1509 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01001510 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Milan Broz7dbcd132011-01-13 19:59:52 +00001512 /*
1513 * For compatibility with the original dm-crypt mapping format, if
1514 * only the cipher name is supplied, use cbc-plain.
1515 */
Milan Broz5ebaee62010-08-12 04:14:07 +01001516 if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 chainmode = "cbc";
1518 ivmode = "plain";
1519 }
1520
Herbert Xud1806f62006-08-22 20:29:17 +10001521 if (strcmp(chainmode, "ecb") && !ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001522 ti->error = "IV mechanism required";
1523 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
1525
Milan Broz5ebaee62010-08-12 04:14:07 +01001526 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
1527 if (!cipher_api)
1528 goto bad_mem;
1529
1530 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
1531 "%s(%s)", chainmode, cipher);
1532 if (ret < 0) {
1533 kfree(cipher_api);
1534 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10001535 }
1536
Milan Broz5ebaee62010-08-12 04:14:07 +01001537 /* Allocate cipher */
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001538 ret = crypt_alloc_tfms(cc, cipher_api);
1539 if (ret < 0) {
1540 ti->error = "Error allocating crypto tfm";
1541 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Milan Broz5ebaee62010-08-12 04:14:07 +01001544 /* Initialize IV */
Andi Kleenc0297722011-01-13 19:59:53 +00001545 cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc));
Milan Broz5ebaee62010-08-12 04:14:07 +01001546 if (cc->iv_size)
1547 /* at least a 64 bit sector number should fit in our buffer */
1548 cc->iv_size = max(cc->iv_size,
1549 (unsigned int)(sizeof(u64) / sizeof(u8)));
1550 else if (ivmode) {
1551 DMWARN("Selected cipher does not support IVs");
1552 ivmode = NULL;
1553 }
1554
1555 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (ivmode == NULL)
1557 cc->iv_gen_ops = NULL;
1558 else if (strcmp(ivmode, "plain") == 0)
1559 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00001560 else if (strcmp(ivmode, "plain64") == 0)
1561 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 else if (strcmp(ivmode, "essiv") == 0)
1563 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10001564 else if (strcmp(ivmode, "benbi") == 0)
1565 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07001566 else if (strcmp(ivmode, "null") == 0)
1567 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00001568 else if (strcmp(ivmode, "lmk") == 0) {
1569 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01001570 /*
1571 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00001572 * to length of provided multi-key string.
1573 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01001574 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00001575 */
Milan Brozda31a072013-10-28 23:21:03 +01001576 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00001577 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01001578 cc->key_extra_size = cc->key_size / cc->key_parts;
1579 }
Milan Brozed04d982013-10-28 23:21:04 +01001580 } else if (strcmp(ivmode, "tcw") == 0) {
1581 cc->iv_gen_ops = &crypt_iv_tcw_ops;
1582 cc->key_parts += 2; /* IV + whitening */
1583 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broz34745782011-01-13 19:59:55 +00001584 } else {
Milan Broz5ebaee62010-08-12 04:14:07 +01001585 ret = -EINVAL;
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001586 ti->error = "Invalid IV mode";
Milan Broz28513fc2010-08-12 04:14:06 +01001587 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
1589
Milan Brozda31a072013-10-28 23:21:03 +01001590 /* Initialize and set key */
1591 ret = crypt_set_key(cc, key);
1592 if (ret < 0) {
1593 ti->error = "Error decoding and setting key";
1594 goto bad;
1595 }
1596
Milan Broz28513fc2010-08-12 04:14:06 +01001597 /* Allocate IV */
1598 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
1599 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
1600 if (ret < 0) {
1601 ti->error = "Error creating IV";
1602 goto bad;
1603 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00001604 }
1605
Milan Broz28513fc2010-08-12 04:14:06 +01001606 /* Initialize IV (set keys for ESSIV etc) */
1607 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
1608 ret = cc->iv_gen_ops->init(cc);
1609 if (ret < 0) {
1610 ti->error = "Error initialising IV";
1611 goto bad;
1612 }
1613 }
1614
Milan Broz5ebaee62010-08-12 04:14:07 +01001615 ret = 0;
1616bad:
1617 kfree(cipher_api);
1618 return ret;
1619
1620bad_mem:
1621 ti->error = "Cannot allocate cipher strings";
1622 return -ENOMEM;
1623}
1624
1625/*
1626 * Construct an encryption mapping:
1627 * <cipher> <key> <iv_offset> <dev_path> <start>
1628 */
1629static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1630{
1631 struct crypt_config *cc;
Milan Broz772ae5f2011-08-02 12:32:08 +01001632 unsigned int key_size, opt_params;
Milan Broz5ebaee62010-08-12 04:14:07 +01001633 unsigned long long tmpll;
1634 int ret;
Mikulas Patockad49ec522014-08-28 11:09:31 -04001635 size_t iv_size_padding;
Milan Broz772ae5f2011-08-02 12:32:08 +01001636 struct dm_arg_set as;
1637 const char *opt_string;
Mikulas Patocka31998ef12012-03-28 18:41:26 +01001638 char dummy;
Milan Broz5ebaee62010-08-12 04:14:07 +01001639
Milan Broz772ae5f2011-08-02 12:32:08 +01001640 static struct dm_arg _args[] = {
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001641 {0, 2, "Invalid number of feature args"},
Milan Broz772ae5f2011-08-02 12:32:08 +01001642 };
1643
1644 if (argc < 5) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001645 ti->error = "Not enough arguments";
1646 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
1648
Milan Broz5ebaee62010-08-12 04:14:07 +01001649 key_size = strlen(argv[1]) >> 1;
1650
1651 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1652 if (!cc) {
1653 ti->error = "Cannot allocate encryption context";
1654 return -ENOMEM;
1655 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00001656 cc->key_size = key_size;
Milan Broz5ebaee62010-08-12 04:14:07 +01001657
1658 ti->private = cc;
1659 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
1660 if (ret < 0)
1661 goto bad;
1662
Milan Broz28513fc2010-08-12 04:14:06 +01001663 ret = -ENOMEM;
Matthew Dobson93d23412006-03-26 01:37:50 -08001664 cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 if (!cc->io_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001666 ti->error = "Cannot allocate crypt io mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001667 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 }
1669
Milan Brozddd42ed2008-02-08 02:11:07 +00001670 cc->dmreq_start = sizeof(struct ablkcipher_request);
Andi Kleenc0297722011-01-13 19:59:53 +00001671 cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc));
Mikulas Patockad49ec522014-08-28 11:09:31 -04001672 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
1673
1674 if (crypto_ablkcipher_alignmask(any_tfm(cc)) < CRYPTO_MINALIGN) {
1675 /* Allocate the padding exactly */
1676 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
1677 & crypto_ablkcipher_alignmask(any_tfm(cc));
1678 } else {
1679 /*
1680 * If the cipher requires greater alignment than kmalloc
1681 * alignment, we don't know the exact position of the
1682 * initialization vector. We must assume worst case.
1683 */
1684 iv_size_padding = crypto_ablkcipher_alignmask(any_tfm(cc));
1685 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001686
1687 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
Mikulas Patockad49ec522014-08-28 11:09:31 -04001688 sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size);
Milan Brozddd42ed2008-02-08 02:11:07 +00001689 if (!cc->req_pool) {
1690 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001691 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00001692 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001693
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001694 cc->per_bio_data_size = ti->per_bio_data_size =
Mikulas Patockad49ec522014-08-28 11:09:31 -04001695 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start +
1696 sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size,
1697 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001698
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001699 cc->page_pool = mempool_create_page_pool(BIO_MAX_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001701 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001702 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704
Jens Axboebb799ca2008-12-10 15:35:05 +01001705 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07001706 if (!cc->bs) {
1707 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01001708 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07001709 }
1710
Mikulas Patocka7145c242015-02-13 08:24:41 -05001711 mutex_init(&cc->bio_alloc_lock);
1712
Milan Broz28513fc2010-08-12 04:14:06 +01001713 ret = -EINVAL;
Mikulas Patocka31998ef12012-03-28 18:41:26 +01001714 if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001715 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001716 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001718 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Milan Broz28513fc2010-08-12 04:14:06 +01001720 if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) {
1721 ti->error = "Device lookup failed";
1722 goto bad;
1723 }
1724
Mikulas Patocka31998ef12012-03-28 18:41:26 +01001725 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001726 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001727 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001729 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Milan Broz772ae5f2011-08-02 12:32:08 +01001731 argv += 5;
1732 argc -= 5;
1733
1734 /* Optional parameters */
1735 if (argc) {
1736 as.argc = argc;
1737 as.argv = argv;
1738
1739 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
1740 if (ret)
1741 goto bad;
1742
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001743 while (opt_params--) {
1744 opt_string = dm_shift_arg(&as);
1745 if (!opt_string) {
1746 ti->error = "Not enough feature arguments";
1747 goto bad;
1748 }
Milan Broz772ae5f2011-08-02 12:32:08 +01001749
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001750 if (!strcasecmp(opt_string, "allow_discards"))
1751 ti->num_discard_bios = 1;
1752
1753 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
1754 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
1755
1756 else {
1757 ti->error = "Invalid feature arguments";
1758 goto bad;
1759 }
Milan Broz772ae5f2011-08-02 12:32:08 +01001760 }
1761 }
1762
Milan Broz28513fc2010-08-12 04:14:06 +01001763 ret = -ENOMEM;
Tejun Heo670368a2013-07-30 08:40:21 -04001764 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_MEM_RECLAIM, 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001765 if (!cc->io_queue) {
1766 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001767 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01001768 }
1769
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001770 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
1771 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
1772 else
1773 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
1774 num_online_cpus());
Milan Brozcabf08e2007-10-19 22:38:58 +01001775 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01001776 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001777 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01001778 }
1779
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001780 ti->num_flush_bios = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01001781 ti->discard_zeroes_data_unsupported = true;
Milan Broz983c7db2011-09-25 23:26:21 +01001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return 0;
1784
Milan Broz28513fc2010-08-12 04:14:06 +01001785bad:
1786 crypt_dtr(ti);
1787 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001790static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001792 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001793 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001794
Milan Broz772ae5f2011-08-02 12:32:08 +01001795 /*
1796 * If bio is REQ_FLUSH or REQ_DISCARD, just bypass crypt queues.
1797 * - for REQ_FLUSH device-mapper core ensures that no IO is in-flight
1798 * - for REQ_DISCARD caller must use flush if IO ordering matters
1799 */
1800 if (unlikely(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD))) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001801 bio->bi_bdev = cc->dev->bdev;
Milan Broz772ae5f2011-08-02 12:32:08 +01001802 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001803 bio->bi_iter.bi_sector = cc->start +
1804 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001805 return DM_MAPIO_REMAPPED;
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001808 io = dm_per_bio_data(bio, cc->per_bio_data_size);
1809 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
1810 io->ctx.req = (struct ablkcipher_request *)(io + 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001811
Milan Broz20c82532011-01-13 19:59:53 +00001812 if (bio_data_dir(io->base_bio) == READ) {
1813 if (kcryptd_io_read(io, GFP_NOWAIT))
1814 kcryptd_queue_io(io);
1815 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01001816 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001818 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819}
1820
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00001821static void crypt_status(struct dm_target *ti, status_type_t type,
1822 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Milan Broz5ebaee62010-08-12 04:14:07 +01001824 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00001825 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001826 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
1828 switch (type) {
1829 case STATUSTYPE_INFO:
1830 result[0] = '\0';
1831 break;
1832
1833 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00001834 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Mikulas Patockafd7c092e2013-03-01 22:45:44 +00001836 if (cc->key_size > 0)
1837 for (i = 0; i < cc->key_size; i++)
1838 DMEMIT("%02x", cc->key[i]);
1839 else
1840 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Andrew Morton4ee218c2006-03-27 01:17:48 -08001842 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
1843 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01001844
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001845 num_feature_args += !!ti->num_discard_bios;
1846 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
1847 if (num_feature_args) {
1848 DMEMIT(" %d", num_feature_args);
1849 if (ti->num_discard_bios)
1850 DMEMIT(" allow_discards");
1851 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
1852 DMEMIT(" same_cpu_crypt");
1853 }
Milan Broz772ae5f2011-08-02 12:32:08 +01001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 break;
1856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858
Milan Broze48d4bb2006-10-03 01:15:37 -07001859static void crypt_postsuspend(struct dm_target *ti)
1860{
1861 struct crypt_config *cc = ti->private;
1862
1863 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1864}
1865
1866static int crypt_preresume(struct dm_target *ti)
1867{
1868 struct crypt_config *cc = ti->private;
1869
1870 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
1871 DMERR("aborting resume - crypt key is not set.");
1872 return -EAGAIN;
1873 }
1874
1875 return 0;
1876}
1877
1878static void crypt_resume(struct dm_target *ti)
1879{
1880 struct crypt_config *cc = ti->private;
1881
1882 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
1883}
1884
1885/* Message interface
1886 * key set <key>
1887 * key wipe
1888 */
1889static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
1890{
1891 struct crypt_config *cc = ti->private;
Milan Broz542da312009-12-10 23:51:57 +00001892 int ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07001893
1894 if (argc < 2)
1895 goto error;
1896
Mike Snitzer498f0102011-08-02 12:32:04 +01001897 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07001898 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
1899 DMWARN("not suspended during key manipulation.");
1900 return -EINVAL;
1901 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001902 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Milan Broz542da312009-12-10 23:51:57 +00001903 ret = crypt_set_key(cc, argv[2]);
1904 if (ret)
1905 return ret;
1906 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
1907 ret = cc->iv_gen_ops->init(cc);
1908 return ret;
1909 }
Mike Snitzer498f0102011-08-02 12:32:04 +01001910 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00001911 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
1912 ret = cc->iv_gen_ops->wipe(cc);
1913 if (ret)
1914 return ret;
1915 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001916 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00001917 }
Milan Broze48d4bb2006-10-03 01:15:37 -07001918 }
1919
1920error:
1921 DMWARN("unrecognised message received.");
1922 return -EINVAL;
1923}
1924
Milan Brozd41e26b2008-07-21 12:00:40 +01001925static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm,
1926 struct bio_vec *biovec, int max_size)
1927{
1928 struct crypt_config *cc = ti->private;
1929 struct request_queue *q = bdev_get_queue(cc->dev->bdev);
1930
1931 if (!q->merge_bvec_fn)
1932 return max_size;
1933
1934 bvm->bi_bdev = cc->dev->bdev;
Alasdair G Kergonb441a2622010-08-12 04:14:11 +01001935 bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector);
Milan Brozd41e26b2008-07-21 12:00:40 +01001936
1937 return min(max_size, q->merge_bvec_fn(q, bvm, biovec));
1938}
1939
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001940static int crypt_iterate_devices(struct dm_target *ti,
1941 iterate_devices_callout_fn fn, void *data)
1942{
1943 struct crypt_config *cc = ti->private;
1944
Mike Snitzer5dea2712009-07-23 20:30:42 +01001945 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001946}
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948static struct target_type crypt_target = {
1949 .name = "crypt",
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001950 .version = {1, 14, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 .module = THIS_MODULE,
1952 .ctr = crypt_ctr,
1953 .dtr = crypt_dtr,
1954 .map = crypt_map,
1955 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07001956 .postsuspend = crypt_postsuspend,
1957 .preresume = crypt_preresume,
1958 .resume = crypt_resume,
1959 .message = crypt_message,
Milan Brozd41e26b2008-07-21 12:00:40 +01001960 .merge = crypt_merge,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001961 .iterate_devices = crypt_iterate_devices,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962};
1963
1964static int __init dm_crypt_init(void)
1965{
1966 int r;
1967
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001968 _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (!_crypt_io_pool)
1970 return -ENOMEM;
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 r = dm_register_target(&crypt_target);
1973 if (r < 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001974 DMERR("register failed %d", r);
Milan Broz9934a8b2007-10-19 22:38:57 +01001975 kmem_cache_destroy(_crypt_io_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 return r;
1979}
1980
1981static void __exit dm_crypt_exit(void)
1982{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001983 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 kmem_cache_destroy(_crypt_io_pool);
1985}
1986
1987module_init(dm_crypt_init);
1988module_exit(dm_crypt_exit);
1989
Jana Saoutbf14299f2014-06-24 14:27:04 -04001990MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
1992MODULE_LICENSE("GPL");