blob: 18692aab889873ec14c5c07e6996f658dc0ab7ff [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 Broz54cea3f2015-05-15 17:00:25 +02004 * Copyright (C) 2006-2015 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>
Mikulas Patockadc267622015-02-13 08:25:59 -050021#include <linux/kthread.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070022#include <linux/backing-dev.h>
Arun Sharma600634972011-07-26 16:09:06 -070023#include <linux/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100024#include <linux/scatterlist.h>
Mikulas Patockab3c5fd32015-02-13 08:27:41 -050025#include <linux/rbtree.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100027#include <asm/unaligned.h>
Milan Broz34745782011-01-13 19:59:55 +000028#include <crypto/hash.h>
29#include <crypto/md5.h>
30#include <crypto/algapi.h>
Herbert Xubbdb23b2016-01-24 21:16:36 +080031#include <crypto/skcipher.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Mikulas Patocka586e80e2008-10-21 17:44:59 +010033#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Alasdair G Kergon72d94862006-06-26 00:27:35 -070035#define DM_MSG_PREFIX "crypt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * context holding the current state of a multi-part conversion
39 */
40struct convert_context {
Milan Broz43d69032008-02-08 02:11:09 +000041 struct completion restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct bio *bio_in;
43 struct bio *bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -070044 struct bvec_iter iter_in;
45 struct bvec_iter iter_out;
Mikulas Patockac66029f2012-07-27 15:08:05 +010046 sector_t cc_sector;
Mikulas Patocka40b62292012-07-27 15:08:04 +010047 atomic_t cc_pending;
Herbert Xubbdb23b2016-01-24 21:16:36 +080048 struct skcipher_request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Milan Broz53017032008-02-08 02:10:38 +000051/*
52 * per bio private data
53 */
54struct dm_crypt_io {
Alasdair G Kergon49a8a922012-07-27 15:08:05 +010055 struct crypt_config *cc;
Milan Broz53017032008-02-08 02:10:38 +000056 struct bio *base_bio;
57 struct work_struct work;
58
59 struct convert_context ctx;
60
Mikulas Patocka40b62292012-07-27 15:08:04 +010061 atomic_t io_pending;
Milan Broz53017032008-02-08 02:10:38 +000062 int error;
Milan Broz0c395b02008-02-08 02:10:54 +000063 sector_t sector;
Mikulas Patockadc267622015-02-13 08:25:59 -050064
Mikulas Patockab3c5fd32015-02-13 08:27:41 -050065 struct rb_node rb_node;
Mikulas Patocka298a9fa2014-03-28 15:51:55 -040066} CRYPTO_MINALIGN_ATTR;
Milan Broz53017032008-02-08 02:10:38 +000067
Milan Broz01482b72008-02-08 02:11:04 +000068struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000069 struct convert_context *ctx;
Milan Broz01482b72008-02-08 02:11:04 +000070 struct scatterlist sg_in;
71 struct scatterlist sg_out;
Milan Broz2dc53272011-01-13 19:59:54 +000072 sector_t iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000073};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075struct crypt_config;
76
77struct crypt_iv_operations {
78 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010079 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000081 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000082 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc53272011-01-13 19:59:54 +000083 int (*generator)(struct crypt_config *cc, u8 *iv,
84 struct dm_crypt_request *dmreq);
85 int (*post)(struct crypt_config *cc, u8 *iv,
86 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087};
88
Milan Broz60473592009-12-10 23:51:55 +000089struct iv_essiv_private {
Herbert Xubbdb23b2016-01-24 21:16:36 +080090 struct crypto_ahash *hash_tfm;
Milan Brozb95bf2d2009-12-10 23:51:56 +000091 u8 *salt;
Milan Broz60473592009-12-10 23:51:55 +000092};
93
94struct iv_benbi_private {
95 int shift;
96};
97
Milan Broz34745782011-01-13 19:59:55 +000098#define LMK_SEED_SIZE 64 /* hash + 0 */
99struct iv_lmk_private {
100 struct crypto_shash *hash_tfm;
101 u8 *seed;
102};
103
Milan Brozed04d982013-10-28 23:21:04 +0100104#define TCW_WHITENING_SIZE 16
105struct iv_tcw_private {
106 struct crypto_shash *crc32_tfm;
107 u8 *iv_seed;
108 u8 *whitening;
109};
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/*
112 * Crypt: maps a linear range of a block device
113 * and encrypts / decrypts at the same time.
114 */
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -0500115enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID,
Rabin Vincentf659b102016-09-21 16:22:29 +0200116 DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD };
Andi Kleenc0297722011-01-13 19:59:53 +0000117
Milan Brozc083ebe2017-03-16 15:39:44 +0100118enum cipher_flags {
119 CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */
120};
121
Andi Kleenc0297722011-01-13 19:59:53 +0000122/*
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500123 * The fields in here must be read only after initialization.
Andi Kleenc0297722011-01-13 19:59:53 +0000124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125struct crypt_config {
126 struct dm_dev *dev;
127 sector_t start;
128
129 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000130 * pool for per bio private data, crypto requests and
131 * encryption requeusts/buffer pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
Milan Brozddd42ed2008-02-08 02:11:07 +0000133 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -0700135 struct bio_set *bs;
Mikulas Patocka7145c242015-02-13 08:24:41 -0500136 struct mutex bio_alloc_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Milan Brozcabf08e2007-10-19 22:38:58 +0100138 struct workqueue_struct *io_queue;
139 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700140
Mikulas Patockadc267622015-02-13 08:25:59 -0500141 struct task_struct *write_thread;
142 wait_queue_head_t write_thread_wait;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -0500143 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -0500144
Milan Broz5ebaee62010-08-12 04:14:07 +0100145 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000146 char *cipher_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800149 union {
Milan Broz60473592009-12-10 23:51:55 +0000150 struct iv_essiv_private essiv;
151 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000152 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100153 struct iv_tcw_private tcw;
Herbert Xu79066ad2006-12-05 13:41:52 -0800154 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 sector_t iv_offset;
156 unsigned int iv_size;
Milan Brozc083ebe2017-03-16 15:39:44 +0100157 unsigned int sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100159 /* ESSIV: struct crypto_cipher *essiv_tfm */
160 void *iv_private;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800161 struct crypto_skcipher **tfms;
Milan Brozd1f96422011-01-13 19:59:54 +0000162 unsigned tfms_count;
Milan Brozc083ebe2017-03-16 15:39:44 +0100163 unsigned long cipher_flags;
Andi Kleenc0297722011-01-13 19:59:53 +0000164
165 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000166 * Layout of each crypto request:
167 *
Herbert Xubbdb23b2016-01-24 21:16:36 +0800168 * struct skcipher_request
Milan Brozddd42ed2008-02-08 02:11:07 +0000169 * context
170 * padding
171 * struct dm_crypt_request
172 * padding
173 * IV
174 *
175 * The padding is added so that dm_crypt_request and the IV are
176 * correctly aligned.
177 */
178 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000179
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400180 unsigned int per_bio_data_size;
181
Milan Broze48d4bb2006-10-03 01:15:37 -0700182 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100184 unsigned int key_parts; /* independent parts in key buffer */
185 unsigned int key_extra_size; /* additional keys length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 u8 key[0];
187};
188
Mikulas Patocka0a83df62016-07-15 17:30:20 -0400189#define MIN_IOS 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100191static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000192static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Broz2dc53272011-01-13 19:59:54 +0000193static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
Olaf Kirch027581f2007-05-09 02:32:52 -0700194
Andi Kleenc0297722011-01-13 19:59:53 +0000195/*
196 * Use this to access cipher attributes that are the same for each CPU.
197 */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800198static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +0000199{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100200 return cc->tfms[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000201}
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * Different IV generation algorithms:
205 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000206 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200207 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
Milan Broz61afef62009-12-10 23:52:25 +0000209 * plain64: the initial vector is the 64-bit little-endian version of the sector
210 * number, padded with zeros if necessary.
211 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000212 * essiv: "encrypted sector|salt initial vector", the sector number is
213 * encrypted with the bulk cipher using a salt as key. The salt
214 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 *
Rik Snel48527fa2006-09-03 08:56:39 +1000216 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
217 * (needed for LRW-32-AES and possible other narrow block modes)
218 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700219 * null: the initial vector is always zero. Provides compatibility with
220 * obsolete loop_fish2 devices. Do not use for new devices.
221 *
Milan Broz34745782011-01-13 19:59:55 +0000222 * lmk: Compatible implementation of the block chaining mode used
223 * by the Loop-AES block device encryption system
224 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
225 * It operates on full 512 byte sectors and uses CBC
226 * with an IV derived from the sector number, the data and
227 * optionally extra IV seed.
228 * This means that after decryption the first block
229 * of sector must be tweaked according to decrypted data.
230 * Loop-AES can use three encryption schemes:
231 * version 1: is plain aes-cbc mode
232 * version 2: uses 64 multikey scheme with lmk IV generator
233 * version 3: the same as version 2 with additional IV seed
234 * (it uses 65 keys, last key is used as IV seed)
235 *
Milan Brozed04d982013-10-28 23:21:04 +0100236 * tcw: Compatible implementation of the block chaining mode used
237 * by the TrueCrypt device encryption system (prior to version 4.1).
Milan Broze44f23b2015-04-05 18:03:10 +0200238 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
Milan Brozed04d982013-10-28 23:21:04 +0100239 * It operates on full 512 byte sectors and uses CBC
240 * with an IV derived from initial key and the sector number.
241 * In addition, whitening value is applied on every sector, whitening
242 * is calculated from initial key, sector number and mixed using CRC32.
243 * Note that this encryption scheme is vulnerable to watermarking attacks
244 * and should be used for old compatible containers access only.
245 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * plumb: unimplemented, see:
247 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
248 */
249
Milan Broz2dc53272011-01-13 19:59:54 +0000250static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
251 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100254 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 return 0;
257}
258
Milan Broz61afef62009-12-10 23:52:25 +0000259static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000260 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000261{
262 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100263 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000264
265 return 0;
266}
267
Milan Brozb95bf2d2009-12-10 23:51:56 +0000268/* Initialise ESSIV - compute salt but no local memory allocations */
269static int crypt_iv_essiv_init(struct crypt_config *cc)
270{
271 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800272 AHASH_REQUEST_ON_STACK(req, essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000273 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000274 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100275 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000276
277 sg_init_one(&sg, cc->key, cc->key_size);
Herbert Xubbdb23b2016-01-24 21:16:36 +0800278 ahash_request_set_tfm(req, essiv->hash_tfm);
279 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
280 ahash_request_set_crypt(req, &sg, essiv->salt, cc->key_size);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000281
Herbert Xubbdb23b2016-01-24 21:16:36 +0800282 err = crypto_ahash_digest(req);
283 ahash_request_zero(req);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000284 if (err)
285 return err;
286
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100287 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000288
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100289 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800290 crypto_ahash_digestsize(essiv->hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100291 if (err)
292 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000293
294 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000295}
296
Milan Broz542da312009-12-10 23:51:57 +0000297/* Wipe salt and reset key derived from volume key */
298static int crypt_iv_essiv_wipe(struct crypt_config *cc)
299{
300 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800301 unsigned salt_size = crypto_ahash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000302 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100303 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000304
305 memset(essiv->salt, 0, salt_size);
306
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100307 essiv_tfm = cc->iv_private;
308 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
309 if (r)
310 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000311
312 return err;
313}
314
315/* Set up per cpu cipher state */
316static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
317 struct dm_target *ti,
318 u8 *salt, unsigned saltsize)
319{
320 struct crypto_cipher *essiv_tfm;
321 int err;
322
323 /* Setup the essiv_tfm with the given salt */
324 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
325 if (IS_ERR(essiv_tfm)) {
326 ti->error = "Error allocating crypto tfm for ESSIV";
327 return essiv_tfm;
328 }
329
330 if (crypto_cipher_blocksize(essiv_tfm) !=
Herbert Xubbdb23b2016-01-24 21:16:36 +0800331 crypto_skcipher_ivsize(any_tfm(cc))) {
Andi Kleenc0297722011-01-13 19:59:53 +0000332 ti->error = "Block size of ESSIV cipher does "
333 "not match IV size of block cipher";
334 crypto_free_cipher(essiv_tfm);
335 return ERR_PTR(-EINVAL);
336 }
337
338 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
339 if (err) {
340 ti->error = "Failed to set key for ESSIV cipher";
341 crypto_free_cipher(essiv_tfm);
342 return ERR_PTR(err);
343 }
344
345 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000346}
347
Milan Broz60473592009-12-10 23:51:55 +0000348static void crypt_iv_essiv_dtr(struct crypt_config *cc)
349{
Andi Kleenc0297722011-01-13 19:59:53 +0000350 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000351 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
352
Herbert Xubbdb23b2016-01-24 21:16:36 +0800353 crypto_free_ahash(essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000354 essiv->hash_tfm = NULL;
355
356 kzfree(essiv->salt);
357 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000358
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100359 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000360
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100361 if (essiv_tfm)
362 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000363
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100364 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100368 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Milan Broz5861f1b2009-12-10 23:51:56 +0000370 struct crypto_cipher *essiv_tfm = NULL;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800371 struct crypto_ahash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000372 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100373 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Milan Broz5861f1b2009-12-10 23:51:56 +0000375 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700376 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return -EINVAL;
378 }
379
Milan Brozb95bf2d2009-12-10 23:51:56 +0000380 /* Allocate hash algorithm */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800381 hash_tfm = crypto_alloc_ahash(opts, 0, CRYPTO_ALG_ASYNC);
Herbert Xu35058682006-08-24 19:10:20 +1000382 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700383 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000384 err = PTR_ERR(hash_tfm);
385 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
Herbert Xubbdb23b2016-01-24 21:16:36 +0800388 salt = kzalloc(crypto_ahash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000389 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700390 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000391 err = -ENOMEM;
392 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
Milan Brozb95bf2d2009-12-10 23:51:56 +0000395 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000396 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
397
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100398 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800399 crypto_ahash_digestsize(hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100400 if (IS_ERR(essiv_tfm)) {
401 crypt_iv_essiv_dtr(cc);
402 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000403 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100404 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000407
408bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000409 if (hash_tfm && !IS_ERR(hash_tfm))
Herbert Xubbdb23b2016-01-24 21:16:36 +0800410 crypto_free_ahash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000411 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000412 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Milan Broz2dc53272011-01-13 19:59:54 +0000415static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
416 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100418 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100421 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000422 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return 0;
425}
426
Rik Snel48527fa2006-09-03 08:56:39 +1000427static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
428 const char *opts)
429{
Herbert Xubbdb23b2016-01-24 21:16:36 +0800430 unsigned bs = crypto_skcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800431 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000432
433 /* we need to calculate how far we must shift the sector count
434 * to get the cipher block count, we use this shift in _gen */
435
436 if (1 << log != bs) {
437 ti->error = "cypher blocksize is not a power of 2";
438 return -EINVAL;
439 }
440
441 if (log > 9) {
442 ti->error = "cypher blocksize is > 512";
443 return -EINVAL;
444 }
445
Milan Broz60473592009-12-10 23:51:55 +0000446 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000447
448 return 0;
449}
450
451static void crypt_iv_benbi_dtr(struct crypt_config *cc)
452{
Rik Snel48527fa2006-09-03 08:56:39 +1000453}
454
Milan Broz2dc53272011-01-13 19:59:54 +0000455static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
456 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000457{
Herbert Xu79066ad2006-12-05 13:41:52 -0800458 __be64 val;
459
Rik Snel48527fa2006-09-03 08:56:39 +1000460 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800461
Milan Broz2dc53272011-01-13 19:59:54 +0000462 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800463 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 0;
466}
467
Milan Broz2dc53272011-01-13 19:59:54 +0000468static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
469 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700470{
471 memset(iv, 0, cc->iv_size);
472
473 return 0;
474}
475
Milan Broz34745782011-01-13 19:59:55 +0000476static void crypt_iv_lmk_dtr(struct crypt_config *cc)
477{
478 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
479
480 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
481 crypto_free_shash(lmk->hash_tfm);
482 lmk->hash_tfm = NULL;
483
484 kzfree(lmk->seed);
485 lmk->seed = NULL;
486}
487
488static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
489 const char *opts)
490{
491 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
492
Milan Brozc083ebe2017-03-16 15:39:44 +0100493 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
494 ti->error = "Unsupported sector size for LMK";
495 return -EINVAL;
496 }
497
Milan Broz34745782011-01-13 19:59:55 +0000498 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
499 if (IS_ERR(lmk->hash_tfm)) {
500 ti->error = "Error initializing LMK hash";
501 return PTR_ERR(lmk->hash_tfm);
502 }
503
504 /* No seed in LMK version 2 */
505 if (cc->key_parts == cc->tfms_count) {
506 lmk->seed = NULL;
507 return 0;
508 }
509
510 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
511 if (!lmk->seed) {
512 crypt_iv_lmk_dtr(cc);
513 ti->error = "Error kmallocing seed storage in LMK";
514 return -ENOMEM;
515 }
516
517 return 0;
518}
519
520static int crypt_iv_lmk_init(struct crypt_config *cc)
521{
522 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
523 int subkey_size = cc->key_size / cc->key_parts;
524
525 /* LMK seed is on the position of LMK_KEYS + 1 key */
526 if (lmk->seed)
527 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
528 crypto_shash_digestsize(lmk->hash_tfm));
529
530 return 0;
531}
532
533static int crypt_iv_lmk_wipe(struct crypt_config *cc)
534{
535 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
536
537 if (lmk->seed)
538 memset(lmk->seed, 0, LMK_SEED_SIZE);
539
540 return 0;
541}
542
543static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
544 struct dm_crypt_request *dmreq,
545 u8 *data)
546{
547 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200548 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000549 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100550 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000551 int i, r;
552
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200553 desc->tfm = lmk->hash_tfm;
554 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Broz34745782011-01-13 19:59:55 +0000555
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200556 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000557 if (r)
558 return r;
559
560 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200561 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000562 if (r)
563 return r;
564 }
565
566 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200567 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000568 if (r)
569 return r;
570
571 /* Sector is cropped to 56 bits here */
572 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
573 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
574 buf[2] = cpu_to_le32(4024);
575 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200576 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000577 if (r)
578 return r;
579
580 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200581 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000582 if (r)
583 return r;
584
585 for (i = 0; i < MD5_HASH_WORDS; i++)
586 __cpu_to_le32s(&md5state.hash[i]);
587 memcpy(iv, &md5state.hash, cc->iv_size);
588
589 return 0;
590}
591
592static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
593 struct dm_crypt_request *dmreq)
594{
595 u8 *src;
596 int r = 0;
597
598 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Cong Wangc2e022c2011-11-28 13:26:02 +0800599 src = kmap_atomic(sg_page(&dmreq->sg_in));
Milan Broz34745782011-01-13 19:59:55 +0000600 r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800601 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000602 } else
603 memset(iv, 0, cc->iv_size);
604
605 return r;
606}
607
608static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
609 struct dm_crypt_request *dmreq)
610{
611 u8 *dst;
612 int r;
613
614 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
615 return 0;
616
Cong Wangc2e022c2011-11-28 13:26:02 +0800617 dst = kmap_atomic(sg_page(&dmreq->sg_out));
Milan Broz34745782011-01-13 19:59:55 +0000618 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
619
620 /* Tweak the first block of plaintext sector */
621 if (!r)
622 crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
623
Cong Wangc2e022c2011-11-28 13:26:02 +0800624 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000625 return r;
626}
627
Milan Brozed04d982013-10-28 23:21:04 +0100628static void crypt_iv_tcw_dtr(struct crypt_config *cc)
629{
630 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
631
632 kzfree(tcw->iv_seed);
633 tcw->iv_seed = NULL;
634 kzfree(tcw->whitening);
635 tcw->whitening = NULL;
636
637 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
638 crypto_free_shash(tcw->crc32_tfm);
639 tcw->crc32_tfm = NULL;
640}
641
642static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
643 const char *opts)
644{
645 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
646
Milan Brozc083ebe2017-03-16 15:39:44 +0100647 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
648 ti->error = "Unsupported sector size for TCW";
649 return -EINVAL;
650 }
651
Milan Brozed04d982013-10-28 23:21:04 +0100652 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
653 ti->error = "Wrong key size for TCW";
654 return -EINVAL;
655 }
656
657 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
658 if (IS_ERR(tcw->crc32_tfm)) {
659 ti->error = "Error initializing CRC32 in TCW";
660 return PTR_ERR(tcw->crc32_tfm);
661 }
662
663 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
664 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
665 if (!tcw->iv_seed || !tcw->whitening) {
666 crypt_iv_tcw_dtr(cc);
667 ti->error = "Error allocating seed storage in TCW";
668 return -ENOMEM;
669 }
670
671 return 0;
672}
673
674static int crypt_iv_tcw_init(struct crypt_config *cc)
675{
676 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
677 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
678
679 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
680 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
681 TCW_WHITENING_SIZE);
682
683 return 0;
684}
685
686static int crypt_iv_tcw_wipe(struct crypt_config *cc)
687{
688 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
689
690 memset(tcw->iv_seed, 0, cc->iv_size);
691 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
692
693 return 0;
694}
695
696static int crypt_iv_tcw_whitening(struct crypt_config *cc,
697 struct dm_crypt_request *dmreq,
698 u8 *data)
699{
700 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200701 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100702 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200703 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100704 int i, r;
705
706 /* xor whitening with sector number */
707 memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
708 crypto_xor(buf, (u8 *)&sector, 8);
709 crypto_xor(&buf[8], (u8 *)&sector, 8);
710
711 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200712 desc->tfm = tcw->crc32_tfm;
713 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Brozed04d982013-10-28 23:21:04 +0100714 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200715 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100716 if (r)
717 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200718 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100719 if (r)
720 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200721 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100722 if (r)
723 goto out;
724 }
725 crypto_xor(&buf[0], &buf[12], 4);
726 crypto_xor(&buf[4], &buf[8], 4);
727
728 /* apply whitening (8 bytes) to whole sector */
729 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
730 crypto_xor(data + i * 8, buf, 8);
731out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100732 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100733 return r;
734}
735
736static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
737 struct dm_crypt_request *dmreq)
738{
739 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200740 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100741 u8 *src;
742 int r = 0;
743
744 /* Remove whitening from ciphertext */
745 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
746 src = kmap_atomic(sg_page(&dmreq->sg_in));
747 r = crypt_iv_tcw_whitening(cc, dmreq, src + dmreq->sg_in.offset);
748 kunmap_atomic(src);
749 }
750
751 /* Calculate IV */
752 memcpy(iv, tcw->iv_seed, cc->iv_size);
753 crypto_xor(iv, (u8 *)&sector, 8);
754 if (cc->iv_size > 8)
755 crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
756
757 return r;
758}
759
760static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
761 struct dm_crypt_request *dmreq)
762{
763 u8 *dst;
764 int r;
765
766 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
767 return 0;
768
769 /* Apply whitening on ciphertext */
770 dst = kmap_atomic(sg_page(&dmreq->sg_out));
771 r = crypt_iv_tcw_whitening(cc, dmreq, dst + dmreq->sg_out.offset);
772 kunmap_atomic(dst);
773
774 return r;
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777static struct crypt_iv_operations crypt_iv_plain_ops = {
778 .generator = crypt_iv_plain_gen
779};
780
Milan Broz61afef62009-12-10 23:52:25 +0000781static struct crypt_iv_operations crypt_iv_plain64_ops = {
782 .generator = crypt_iv_plain64_gen
783};
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785static struct crypt_iv_operations crypt_iv_essiv_ops = {
786 .ctr = crypt_iv_essiv_ctr,
787 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000788 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000789 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 .generator = crypt_iv_essiv_gen
791};
792
Rik Snel48527fa2006-09-03 08:56:39 +1000793static struct crypt_iv_operations crypt_iv_benbi_ops = {
794 .ctr = crypt_iv_benbi_ctr,
795 .dtr = crypt_iv_benbi_dtr,
796 .generator = crypt_iv_benbi_gen
797};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Ludwig Nussel46b47732007-05-09 02:32:55 -0700799static struct crypt_iv_operations crypt_iv_null_ops = {
800 .generator = crypt_iv_null_gen
801};
802
Milan Broz34745782011-01-13 19:59:55 +0000803static struct crypt_iv_operations crypt_iv_lmk_ops = {
804 .ctr = crypt_iv_lmk_ctr,
805 .dtr = crypt_iv_lmk_dtr,
806 .init = crypt_iv_lmk_init,
807 .wipe = crypt_iv_lmk_wipe,
808 .generator = crypt_iv_lmk_gen,
809 .post = crypt_iv_lmk_post
810};
811
Milan Brozed04d982013-10-28 23:21:04 +0100812static struct crypt_iv_operations crypt_iv_tcw_ops = {
813 .ctr = crypt_iv_tcw_ctr,
814 .dtr = crypt_iv_tcw_dtr,
815 .init = crypt_iv_tcw_init,
816 .wipe = crypt_iv_tcw_wipe,
817 .generator = crypt_iv_tcw_gen,
818 .post = crypt_iv_tcw_post
819};
820
Milan Brozd469f842007-10-19 22:42:37 +0100821static void crypt_convert_init(struct crypt_config *cc,
822 struct convert_context *ctx,
823 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000824 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825{
826 ctx->bio_in = bio_in;
827 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700828 if (bio_in)
829 ctx->iter_in = bio_in->bi_iter;
830 if (bio_out)
831 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100832 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000833 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
Huang Yingb2174ee2009-03-16 17:44:33 +0000836static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800837 struct skcipher_request *req)
Huang Yingb2174ee2009-03-16 17:44:33 +0000838{
839 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
840}
841
Herbert Xubbdb23b2016-01-24 21:16:36 +0800842static struct skcipher_request *req_of_dmreq(struct crypt_config *cc,
Huang Yingb2174ee2009-03-16 17:44:33 +0000843 struct dm_crypt_request *dmreq)
844{
Herbert Xubbdb23b2016-01-24 21:16:36 +0800845 return (struct skcipher_request *)((char *)dmreq - cc->dmreq_start);
Huang Yingb2174ee2009-03-16 17:44:33 +0000846}
847
Milan Broz2dc53272011-01-13 19:59:54 +0000848static u8 *iv_of_dmreq(struct crypt_config *cc,
849 struct dm_crypt_request *dmreq)
850{
851 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
Herbert Xubbdb23b2016-01-24 21:16:36 +0800852 crypto_skcipher_alignmask(any_tfm(cc)) + 1);
Milan Broz2dc53272011-01-13 19:59:54 +0000853}
854
Milan Broz01482b72008-02-08 02:11:04 +0000855static int crypt_convert_block(struct crypt_config *cc,
Milan Broz3a7f6c92008-02-08 02:11:14 +0000856 struct convert_context *ctx,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800857 struct skcipher_request *req)
Milan Broz01482b72008-02-08 02:11:04 +0000858{
Kent Overstreet003b5c52013-10-11 15:45:43 -0700859 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
860 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000861 struct dm_crypt_request *dmreq;
862 u8 *iv;
Mikulas Patocka40b62292012-07-27 15:08:04 +0100863 int r;
Milan Broz01482b72008-02-08 02:11:04 +0000864
Milan Brozc083ebe2017-03-16 15:39:44 +0100865 /* Reject unexpected unaligned bio. */
866 if (unlikely(bv_in.bv_offset & (cc->sector_size - 1)))
867 return -EIO;
868
Huang Yingb2174ee2009-03-16 17:44:33 +0000869 dmreq = dmreq_of_req(cc, req);
Milan Broz2dc53272011-01-13 19:59:54 +0000870 iv = iv_of_dmreq(cc, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000871
Mikulas Patockac66029f2012-07-27 15:08:05 +0100872 dmreq->iv_sector = ctx->cc_sector;
Milan Brozc083ebe2017-03-16 15:39:44 +0100873 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
874 sector_div(dmreq->iv_sector, cc->sector_size >> SECTOR_SHIFT);
Huang Yingb2174ee2009-03-16 17:44:33 +0000875 dmreq->ctx = ctx;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000876 sg_init_table(&dmreq->sg_in, 1);
Milan Brozc083ebe2017-03-16 15:39:44 +0100877 sg_set_page(&dmreq->sg_in, bv_in.bv_page, cc->sector_size,
Kent Overstreet003b5c52013-10-11 15:45:43 -0700878 bv_in.bv_offset);
Milan Broz01482b72008-02-08 02:11:04 +0000879
Milan Broz3a7f6c92008-02-08 02:11:14 +0000880 sg_init_table(&dmreq->sg_out, 1);
Milan Brozc083ebe2017-03-16 15:39:44 +0100881 sg_set_page(&dmreq->sg_out, bv_out.bv_page, cc->sector_size,
Kent Overstreet003b5c52013-10-11 15:45:43 -0700882 bv_out.bv_offset);
Milan Broz01482b72008-02-08 02:11:04 +0000883
Milan Brozc083ebe2017-03-16 15:39:44 +0100884 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
885 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Broz01482b72008-02-08 02:11:04 +0000886
Milan Broz3a7f6c92008-02-08 02:11:14 +0000887 if (cc->iv_gen_ops) {
Milan Broz2dc53272011-01-13 19:59:54 +0000888 r = cc->iv_gen_ops->generator(cc, iv, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000889 if (r < 0)
890 return r;
891 }
892
Herbert Xubbdb23b2016-01-24 21:16:36 +0800893 skcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
Milan Brozc083ebe2017-03-16 15:39:44 +0100894 cc->sector_size, iv);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000895
896 if (bio_data_dir(ctx->bio_in) == WRITE)
Herbert Xubbdb23b2016-01-24 21:16:36 +0800897 r = crypto_skcipher_encrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000898 else
Herbert Xubbdb23b2016-01-24 21:16:36 +0800899 r = crypto_skcipher_decrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000900
Milan Broz2dc53272011-01-13 19:59:54 +0000901 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
902 r = cc->iv_gen_ops->post(cc, iv, dmreq);
903
Milan Broz3a7f6c92008-02-08 02:11:14 +0000904 return r;
Milan Broz01482b72008-02-08 02:11:04 +0000905}
906
Milan Broz95497a92008-02-08 02:11:12 +0000907static void kcryptd_async_done(struct crypto_async_request *async_req,
908 int error);
Andi Kleenc0297722011-01-13 19:59:53 +0000909
Milan Brozddd42ed2008-02-08 02:11:07 +0000910static void crypt_alloc_req(struct crypt_config *cc,
911 struct convert_context *ctx)
912{
Mikulas Patockac66029f2012-07-27 15:08:05 +0100913 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +0000914
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500915 if (!ctx->req)
916 ctx->req = mempool_alloc(cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +0000917
Herbert Xubbdb23b2016-01-24 21:16:36 +0800918 skcipher_request_set_tfm(ctx->req, cc->tfms[key_index]);
Milan Broz54cea3f2015-05-15 17:00:25 +0200919
920 /*
921 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
922 * requests if driver request queue is full.
923 */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800924 skcipher_request_set_callback(ctx->req,
Andi Kleenc0297722011-01-13 19:59:53 +0000925 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500926 kcryptd_async_done, dmreq_of_req(cc, ctx->req));
Milan Brozddd42ed2008-02-08 02:11:07 +0000927}
928
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400929static void crypt_free_req(struct crypt_config *cc,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800930 struct skcipher_request *req, struct bio *base_bio)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400931{
932 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
933
Herbert Xubbdb23b2016-01-24 21:16:36 +0800934 if ((struct skcipher_request *)(io + 1) != req)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400935 mempool_free(req, cc->req_pool);
936}
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/*
939 * Encrypt / decrypt data from one bio to another one (can be the same one)
940 */
941static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +0100942 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
Milan Brozc083ebe2017-03-16 15:39:44 +0100944 unsigned int sector_step = cc->sector_size / (1 << SECTOR_SHIFT);
Milan Broz3f1e9072008-03-28 14:16:07 -0700945 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Mikulas Patocka40b62292012-07-27 15:08:04 +0100947 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +0100948
Kent Overstreet003b5c52013-10-11 15:45:43 -0700949 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Milan Broz3a7f6c92008-02-08 02:11:14 +0000951 crypt_alloc_req(cc, ctx);
Mikulas Patocka40b62292012-07-27 15:08:04 +0100952 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700953
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500954 r = crypt_convert_block(cc, ctx, ctx->req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000955
956 switch (r) {
Milan Broz54cea3f2015-05-15 17:00:25 +0200957 /*
958 * The request was queued by a crypto driver
959 * but the driver request queue is full, let's wait.
960 */
Milan Broz3a7f6c92008-02-08 02:11:14 +0000961 case -EBUSY:
962 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -0800963 reinit_completion(&ctx->restart);
Milan Broz54cea3f2015-05-15 17:00:25 +0200964 /* fall through */
965 /*
966 * The request is queued and processed asynchronously,
967 * completion function kcryptd_async_done() will be called.
968 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +0200969 case -EINPROGRESS:
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500970 ctx->req = NULL;
Milan Brozc083ebe2017-03-16 15:39:44 +0100971 ctx->cc_sector += sector_step;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000972 continue;
Milan Broz54cea3f2015-05-15 17:00:25 +0200973 /*
974 * The request was already processed (synchronously).
975 */
Milan Broz3f1e9072008-03-28 14:16:07 -0700976 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +0100977 atomic_dec(&ctx->cc_pending);
Milan Brozc083ebe2017-03-16 15:39:44 +0100978 ctx->cc_sector += sector_step;
Milan Brozc7f1b202008-07-02 09:34:28 +0100979 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -0700980 continue;
981
Milan Broz54cea3f2015-05-15 17:00:25 +0200982 /* There was an error while processing the request. */
Milan Broz3f1e9072008-03-28 14:16:07 -0700983 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +0100984 atomic_dec(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700985 return r;
986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988
Milan Broz3f1e9072008-03-28 14:16:07 -0700989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
991
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -0500992static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994/*
995 * Generate a new unfragmented bio with the given size
Mike Snitzer586b2862015-09-09 21:34:51 -0400996 * This should never violate the device limitations (but only because
997 * max_segment_size is being constrained to PAGE_SIZE).
Mikulas Patocka7145c242015-02-13 08:24:41 -0500998 *
999 * This function may be called concurrently. If we allocate from the mempool
1000 * concurrently, there is a possibility of deadlock. For example, if we have
1001 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1002 * the mempool concurrently, it may deadlock in a situation where both processes
1003 * have allocated 128 pages and the mempool is exhausted.
1004 *
1005 * In order to avoid this scenario we allocate the pages under a mutex.
1006 *
1007 * In order to not degrade performance with excessive locking, we try
1008 * non-blocking allocations without a mutex first but on failure we fallback
1009 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001011static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001013 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001014 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001016 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
1017 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +00001018 struct page *page;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001019 struct bio_vec *bvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Mikulas Patocka7145c242015-02-13 08:24:41 -05001021retry:
Mel Gormand0164ad2015-11-06 16:28:21 -08001022 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001023 mutex_lock(&cc->bio_alloc_lock);
1024
Olaf Kirch2f9941b2007-05-09 02:32:53 -07001025 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -07001026 if (!clone)
Mikulas Patocka7145c242015-02-13 08:24:41 -05001027 goto return_clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Olaf Kirch027581f2007-05-09 02:32:52 -07001029 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -07001030
Mikulas Patocka7145c242015-02-13 08:24:41 -05001031 remaining_size = size;
1032
Olaf Kirchf97380b2007-05-09 02:32:54 -07001033 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +00001034 page = mempool_alloc(cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -05001035 if (!page) {
1036 crypt_free_buffer_pages(cc, clone);
1037 bio_put(clone);
Mel Gormand0164ad2015-11-06 16:28:21 -08001038 gfp_mask |= __GFP_DIRECT_RECLAIM;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001039 goto retry;
1040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Mikulas Patocka7145c242015-02-13 08:24:41 -05001042 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001044 bvec = &clone->bi_io_vec[clone->bi_vcnt++];
1045 bvec->bv_page = page;
1046 bvec->bv_len = len;
1047 bvec->bv_offset = 0;
1048
1049 clone->bi_iter.bi_size += len;
Milan Broz91e10622007-12-13 14:16:10 +00001050
Mikulas Patocka7145c242015-02-13 08:24:41 -05001051 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053
Mikulas Patocka7145c242015-02-13 08:24:41 -05001054return_clone:
Mel Gormand0164ad2015-11-06 16:28:21 -08001055 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001056 mutex_unlock(&cc->bio_alloc_lock);
1057
Milan Broz8b004452006-10-03 01:15:37 -07001058 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
Neil Brown644bd2f2007-10-16 13:48:46 +02001061static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Neil Brown644bd2f2007-10-16 13:48:46 +02001063 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 struct bio_vec *bv;
1065
Kent Overstreetcb34e052012-09-05 15:22:02 -07001066 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 BUG_ON(!bv->bv_page);
1068 mempool_free(bv->bv_page, cc->page_pool);
1069 bv->bv_page = NULL;
1070 }
1071}
1072
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001073static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1074 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001075{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001076 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001077 io->base_bio = bio;
1078 io->sector = sector;
1079 io->error = 0;
Mikulas Patocka610f2de2014-02-20 18:01:01 -05001080 io->ctx.req = NULL;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001081 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001082}
1083
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001084static void crypt_inc_pending(struct dm_crypt_io *io)
1085{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001086 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/*
1090 * One of the bios was finished. Check for completion of
1091 * the whole request and correctly clean up the buffer.
1092 */
Milan Broz5742fd72008-02-08 02:10:43 +00001093static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001095 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001096 struct bio *base_bio = io->base_bio;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001097 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Mikulas Patocka40b62292012-07-27 15:08:04 +01001099 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return;
1101
Mikulas Patocka610f2de2014-02-20 18:01:01 -05001102 if (io->ctx.req)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001103 crypt_free_req(cc, io->ctx.req, base_bio);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001104
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001105 base_bio->bi_error = error;
1106 bio_endio(base_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
1109/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001110 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 *
1112 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001113 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001114 *
1115 * kcryptd performs the actual encryption or decryption.
1116 *
1117 * kcryptd_io performs the IO submission.
1118 *
1119 * They must be separated as otherwise the final stages could be
1120 * starved by new requests which can block in the first stages due
1121 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001122 *
1123 * The work is done per CPU global for all dm-crypt instances.
1124 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001126static void crypt_endio(struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001127{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001128 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001129 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001130 unsigned rw = bio_data_dir(clone);
Sasha Levin9b81c842015-08-10 19:05:18 -04001131 int error;
Milan Broz8b004452006-10-03 01:15:37 -07001132
1133 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001134 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001135 */
Milan Brozee7a4912008-02-08 02:10:46 +00001136 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001137 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001138
Sasha Levin9b81c842015-08-10 19:05:18 -04001139 error = clone->bi_error;
Milan Brozee7a4912008-02-08 02:10:46 +00001140 bio_put(clone);
1141
Sasha Levin9b81c842015-08-10 19:05:18 -04001142 if (rw == READ && !error) {
Milan Brozee7a4912008-02-08 02:10:46 +00001143 kcryptd_queue_crypt(io);
1144 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001145 }
Milan Broz8b004452006-10-03 01:15:37 -07001146
Sasha Levin9b81c842015-08-10 19:05:18 -04001147 if (unlikely(error))
1148 io->error = error;
Milan Broz5742fd72008-02-08 02:10:43 +00001149
1150 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001151}
1152
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001153static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001154{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001155 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001156
1157 clone->bi_private = io;
1158 clone->bi_end_io = crypt_endio;
1159 clone->bi_bdev = cc->dev->bdev;
Bart Van Assche4382e332016-09-14 10:45:36 +02001160 bio_set_op_attrs(clone, bio_op(io->base_bio), bio_flags(io->base_bio));
Milan Broz8b004452006-10-03 01:15:37 -07001161}
1162
Milan Broz20c82532011-01-13 19:59:53 +00001163static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001164{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001165 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001166 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001167
Milan Broz8b004452006-10-03 01:15:37 -07001168 /*
Mike Snitzer59779072015-04-09 16:53:24 -04001169 * We need the original biovec array in order to decrypt
1170 * the whole bio data *afterwards* -- thanks to immutable
1171 * biovecs we don't need to worry about the block layer
1172 * modifying the biovec array; so leverage bio_clone_fast().
Milan Broz8b004452006-10-03 01:15:37 -07001173 */
Mike Snitzer59779072015-04-09 16:53:24 -04001174 clone = bio_clone_fast(io->base_bio, gfp, cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001175 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001176 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001177
Milan Broz20c82532011-01-13 19:59:53 +00001178 crypt_inc_pending(io);
1179
Milan Broz8b004452006-10-03 01:15:37 -07001180 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001181 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001182
Milan Broz93e605c2006-10-03 01:15:38 -07001183 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001184 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001185}
1186
Mikulas Patockadc267622015-02-13 08:25:59 -05001187static void kcryptd_io_read_work(struct work_struct *work)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001188{
1189 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1190
Mikulas Patockadc267622015-02-13 08:25:59 -05001191 crypt_inc_pending(io);
1192 if (kcryptd_io_read(io, GFP_NOIO))
1193 io->error = -ENOMEM;
1194 crypt_dec_pending(io);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001195}
1196
Mikulas Patockadc267622015-02-13 08:25:59 -05001197static void kcryptd_queue_read(struct dm_crypt_io *io)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001198{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001199 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001200
Mikulas Patockadc267622015-02-13 08:25:59 -05001201 INIT_WORK(&io->work, kcryptd_io_read_work);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001202 queue_work(cc->io_queue, &io->work);
1203}
1204
Mikulas Patockadc267622015-02-13 08:25:59 -05001205static void kcryptd_io_write(struct dm_crypt_io *io)
1206{
1207 struct bio *clone = io->ctx.bio_out;
1208
1209 generic_make_request(clone);
1210}
1211
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001212#define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1213
Mikulas Patockadc267622015-02-13 08:25:59 -05001214static int dmcrypt_write(void *data)
1215{
1216 struct crypt_config *cc = data;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001217 struct dm_crypt_io *io;
1218
Mikulas Patockadc267622015-02-13 08:25:59 -05001219 while (1) {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001220 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -05001221 struct blk_plug plug;
1222
1223 DECLARE_WAITQUEUE(wait, current);
1224
1225 spin_lock_irq(&cc->write_thread_wait.lock);
1226continue_locked:
1227
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001228 if (!RB_EMPTY_ROOT(&cc->write_tree))
Mikulas Patockadc267622015-02-13 08:25:59 -05001229 goto pop_from_list;
1230
Rabin Vincentf659b102016-09-21 16:22:29 +02001231 set_current_state(TASK_INTERRUPTIBLE);
Mikulas Patockadc267622015-02-13 08:25:59 -05001232 __add_wait_queue(&cc->write_thread_wait, &wait);
1233
1234 spin_unlock_irq(&cc->write_thread_wait.lock);
1235
Rabin Vincentf659b102016-09-21 16:22:29 +02001236 if (unlikely(kthread_should_stop())) {
1237 set_task_state(current, TASK_RUNNING);
1238 remove_wait_queue(&cc->write_thread_wait, &wait);
1239 break;
1240 }
1241
Mikulas Patockadc267622015-02-13 08:25:59 -05001242 schedule();
1243
Rabin Vincentf659b102016-09-21 16:22:29 +02001244 set_task_state(current, TASK_RUNNING);
Mikulas Patockadc267622015-02-13 08:25:59 -05001245 spin_lock_irq(&cc->write_thread_wait.lock);
1246 __remove_wait_queue(&cc->write_thread_wait, &wait);
1247 goto continue_locked;
1248
1249pop_from_list:
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001250 write_tree = cc->write_tree;
1251 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05001252 spin_unlock_irq(&cc->write_thread_wait.lock);
1253
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001254 BUG_ON(rb_parent(write_tree.rb_node));
1255
1256 /*
1257 * Note: we cannot walk the tree here with rb_next because
1258 * the structures may be freed when kcryptd_io_write is called.
1259 */
Mikulas Patockadc267622015-02-13 08:25:59 -05001260 blk_start_plug(&plug);
1261 do {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001262 io = crypt_io_from_node(rb_first(&write_tree));
1263 rb_erase(&io->rb_node, &write_tree);
Mikulas Patockadc267622015-02-13 08:25:59 -05001264 kcryptd_io_write(io);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001265 } while (!RB_EMPTY_ROOT(&write_tree));
Mikulas Patockadc267622015-02-13 08:25:59 -05001266 blk_finish_plug(&plug);
1267 }
1268 return 0;
1269}
1270
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001271static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001272{
Milan Brozdec1ced2008-02-08 02:10:57 +00001273 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001274 struct crypt_config *cc = io->cc;
Mikulas Patockadc267622015-02-13 08:25:59 -05001275 unsigned long flags;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001276 sector_t sector;
1277 struct rb_node **rbp, *parent;
Milan Brozdec1ced2008-02-08 02:10:57 +00001278
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001279 if (unlikely(io->error < 0)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001280 crypt_free_buffer_pages(cc, clone);
1281 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001282 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001283 return;
1284 }
1285
1286 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001287 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001288
Kent Overstreet4f024f32013-10-11 15:44:27 -07001289 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001290
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001291 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1292 generic_make_request(clone);
1293 return;
1294 }
1295
Mikulas Patockadc267622015-02-13 08:25:59 -05001296 spin_lock_irqsave(&cc->write_thread_wait.lock, flags);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001297 rbp = &cc->write_tree.rb_node;
1298 parent = NULL;
1299 sector = io->sector;
1300 while (*rbp) {
1301 parent = *rbp;
1302 if (sector < crypt_io_from_node(parent)->sector)
1303 rbp = &(*rbp)->rb_left;
1304 else
1305 rbp = &(*rbp)->rb_right;
1306 }
1307 rb_link_node(&io->rb_node, parent, rbp);
1308 rb_insert_color(&io->rb_node, &cc->write_tree);
1309
Mikulas Patockadc267622015-02-13 08:25:59 -05001310 wake_up_locked(&cc->write_thread_wait);
1311 spin_unlock_irqrestore(&cc->write_thread_wait.lock, flags);
Milan Broz4e4eef62008-02-08 02:10:49 +00001312}
1313
Milan Brozfc5a5e92008-10-10 13:37:04 +01001314static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001315{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001316 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001317 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001318 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001319 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +00001320 int r;
Milan Broz8b004452006-10-03 01:15:37 -07001321
Milan Broz93e605c2006-10-03 01:15:38 -07001322 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001323 * Prevent io from disappearing until this function completes.
1324 */
1325 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001326 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001327
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001328 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1329 if (unlikely(!clone)) {
1330 io->error = -EIO;
1331 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001332 }
Milan Broz899c95d2008-02-08 02:11:02 +00001333
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001334 io->ctx.bio_out = clone;
1335 io->ctx.iter_out = clone->bi_iter;
1336
1337 sector += bio_sectors(clone);
1338
1339 crypt_inc_pending(io);
1340 r = crypt_convert(cc, &io->ctx);
1341 if (r)
1342 io->error = -EIO;
1343 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1344
1345 /* Encryption was already finished, submit io now */
1346 if (crypt_finished) {
1347 kcryptd_crypt_write_io_submit(io, 0);
1348 io->sector = sector;
1349 }
1350
1351dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001352 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001353}
1354
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001355static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001356{
Milan Broz5742fd72008-02-08 02:10:43 +00001357 crypt_dec_pending(io);
1358}
1359
Milan Broz4e4eef62008-02-08 02:10:49 +00001360static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001361{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001362 struct crypt_config *cc = io->cc;
Milan Broz5742fd72008-02-08 02:10:43 +00001363 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001364
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001365 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001366
Milan Broz53017032008-02-08 02:10:38 +00001367 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001368 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001369
Milan Broz5742fd72008-02-08 02:10:43 +00001370 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001371 if (r < 0)
1372 io->error = -EIO;
Milan Broz5742fd72008-02-08 02:10:43 +00001373
Mikulas Patocka40b62292012-07-27 15:08:04 +01001374 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001375 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001376
1377 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001378}
1379
Milan Broz95497a92008-02-08 02:11:12 +00001380static void kcryptd_async_done(struct crypto_async_request *async_req,
1381 int error)
1382{
Huang Yingb2174ee2009-03-16 17:44:33 +00001383 struct dm_crypt_request *dmreq = async_req->data;
1384 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001385 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001386 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001387
Milan Broz54cea3f2015-05-15 17:00:25 +02001388 /*
1389 * A request from crypto driver backlog is going to be processed now,
1390 * finish the completion and continue in crypt_convert().
1391 * (Callback will be called for the second time for this request.)
1392 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001393 if (error == -EINPROGRESS) {
1394 complete(&ctx->restart);
Milan Broz95497a92008-02-08 02:11:12 +00001395 return;
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001396 }
Milan Broz95497a92008-02-08 02:11:12 +00001397
Milan Broz2dc53272011-01-13 19:59:54 +00001398 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
1399 error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
1400
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001401 if (error < 0)
1402 io->error = -EIO;
1403
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001404 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00001405
Mikulas Patocka40b62292012-07-27 15:08:04 +01001406 if (!atomic_dec_and_test(&ctx->cc_pending))
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001407 return;
Milan Broz95497a92008-02-08 02:11:12 +00001408
1409 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001410 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001411 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001412 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001413}
1414
Milan Broz4e4eef62008-02-08 02:10:49 +00001415static void kcryptd_crypt(struct work_struct *work)
1416{
1417 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1418
1419 if (bio_data_dir(io->base_bio) == READ)
1420 kcryptd_crypt_read_convert(io);
1421 else
1422 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001423}
1424
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001425static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1426{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001427 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001428
1429 INIT_WORK(&io->work, kcryptd_crypt);
1430 queue_work(cc->crypt_queue, &io->work);
1431}
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433/*
1434 * Decode key from its hex representation
1435 */
1436static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1437{
1438 char buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 unsigned int i;
1440
1441 buffer[2] = '\0';
1442
Milan Broz8b004452006-10-03 01:15:37 -07001443 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 buffer[0] = *hex++;
1445 buffer[1] = *hex++;
1446
majianpeng1a66a082012-07-27 15:07:59 +01001447 if (kstrtou8(buffer, 16, &key[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 return -EINVAL;
1449 }
1450
1451 if (*hex != '\0')
1452 return -EINVAL;
1453
1454 return 0;
1455}
1456
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001457static void crypt_free_tfms(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001458{
Milan Brozd1f96422011-01-13 19:59:54 +00001459 unsigned i;
1460
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001461 if (!cc->tfms)
1462 return;
1463
Milan Brozd1f96422011-01-13 19:59:54 +00001464 for (i = 0; i < cc->tfms_count; i++)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001465 if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) {
Herbert Xubbdb23b2016-01-24 21:16:36 +08001466 crypto_free_skcipher(cc->tfms[i]);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001467 cc->tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001468 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001469
1470 kfree(cc->tfms);
1471 cc->tfms = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001472}
1473
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001474static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001475{
Milan Brozd1f96422011-01-13 19:59:54 +00001476 unsigned i;
1477 int err;
1478
Eric Biggers5d0be842016-08-30 09:51:44 -07001479 cc->tfms = kzalloc(cc->tfms_count * sizeof(struct crypto_skcipher *),
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001480 GFP_KERNEL);
1481 if (!cc->tfms)
1482 return -ENOMEM;
1483
Milan Brozd1f96422011-01-13 19:59:54 +00001484 for (i = 0; i < cc->tfms_count; i++) {
Herbert Xubbdb23b2016-01-24 21:16:36 +08001485 cc->tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001486 if (IS_ERR(cc->tfms[i])) {
1487 err = PTR_ERR(cc->tfms[i]);
1488 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001489 return err;
1490 }
1491 }
1492
1493 return 0;
1494}
1495
Andi Kleenc0297722011-01-13 19:59:53 +00001496static int crypt_setkey_allcpus(struct crypt_config *cc)
1497{
Milan Brozda31a072013-10-28 23:21:03 +01001498 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001499 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001500
Milan Brozda31a072013-10-28 23:21:03 +01001501 /* Ignore extra keys (which are used for IV etc) */
1502 subkey_size = (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1503
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001504 for (i = 0; i < cc->tfms_count; i++) {
Herbert Xubbdb23b2016-01-24 21:16:36 +08001505 r = crypto_skcipher_setkey(cc->tfms[i],
1506 cc->key + (i * subkey_size),
1507 subkey_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001508 if (r)
1509 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001510 }
1511
1512 return err;
1513}
1514
Milan Broze48d4bb2006-10-03 01:15:37 -07001515static int crypt_set_key(struct crypt_config *cc, char *key)
1516{
Milan Brozde8be5a2011-03-24 13:54:27 +00001517 int r = -EINVAL;
1518 int key_string_len = strlen(key);
1519
Milan Broz69a8cfc2011-01-13 19:59:49 +00001520 /* The key size may not be changed. */
Milan Brozde8be5a2011-03-24 13:54:27 +00001521 if (cc->key_size != (key_string_len >> 1))
1522 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001523
Milan Broz69a8cfc2011-01-13 19:59:49 +00001524 /* Hyphen (which gives a key_size of zero) means there is no key. */
1525 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00001526 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001527
Ondrej Kozina2c017f72016-11-02 15:02:08 +01001528 /* clear the flag since following operations may invalidate previously valid key */
1529 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1530
Milan Broz69a8cfc2011-01-13 19:59:49 +00001531 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00001532 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001533
Milan Brozde8be5a2011-03-24 13:54:27 +00001534 r = crypt_setkey_allcpus(cc);
Ondrej Kozina2c017f72016-11-02 15:02:08 +01001535 if (!r)
1536 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Milan Brozde8be5a2011-03-24 13:54:27 +00001537
1538out:
1539 /* Hex key string not needed after here, so wipe it. */
1540 memset(key, '0', key_string_len);
1541
1542 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07001543}
1544
1545static int crypt_wipe_key(struct crypt_config *cc)
1546{
1547 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1548 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00001549
1550 return crypt_setkey_allcpus(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07001551}
1552
Milan Broz28513fc2010-08-12 04:14:06 +01001553static void crypt_dtr(struct dm_target *ti)
1554{
1555 struct crypt_config *cc = ti->private;
1556
1557 ti->private = NULL;
1558
1559 if (!cc)
1560 return;
1561
Rabin Vincentf659b102016-09-21 16:22:29 +02001562 if (cc->write_thread)
Mikulas Patockadc267622015-02-13 08:25:59 -05001563 kthread_stop(cc->write_thread);
1564
Milan Broz28513fc2010-08-12 04:14:06 +01001565 if (cc->io_queue)
1566 destroy_workqueue(cc->io_queue);
1567 if (cc->crypt_queue)
1568 destroy_workqueue(cc->crypt_queue);
1569
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001570 crypt_free_tfms(cc);
1571
Milan Broz28513fc2010-08-12 04:14:06 +01001572 if (cc->bs)
1573 bioset_free(cc->bs);
1574
Julia Lawall6f659852015-09-13 14:15:05 +02001575 mempool_destroy(cc->page_pool);
1576 mempool_destroy(cc->req_pool);
Milan Broz28513fc2010-08-12 04:14:06 +01001577
1578 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1579 cc->iv_gen_ops->dtr(cc);
1580
Milan Broz28513fc2010-08-12 04:14:06 +01001581 if (cc->dev)
1582 dm_put_device(ti, cc->dev);
1583
Milan Broz5ebaee62010-08-12 04:14:07 +01001584 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00001585 kzfree(cc->cipher_string);
Milan Broz28513fc2010-08-12 04:14:06 +01001586
1587 /* Must zero key material before freeing */
1588 kzfree(cc);
1589}
1590
Milan Broz5ebaee62010-08-12 04:14:07 +01001591static int crypt_ctr_cipher(struct dm_target *ti,
1592 char *cipher_in, char *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Milan Broz5ebaee62010-08-12 04:14:07 +01001594 struct crypt_config *cc = ti->private;
Milan Brozd1f96422011-01-13 19:59:54 +00001595 char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01001596 char *cipher_api = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001597 int ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001598 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Milan Broz5ebaee62010-08-12 04:14:07 +01001600 /* Convert to crypto api definition? */
1601 if (strchr(cipher_in, '(')) {
1602 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return -EINVAL;
1604 }
1605
Milan Broz7dbcd132011-01-13 19:59:52 +00001606 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
1607 if (!cc->cipher_string)
1608 goto bad_mem;
1609
Milan Broz5ebaee62010-08-12 04:14:07 +01001610 /*
1611 * Legacy dm-crypt cipher specification
Milan Brozd1f96422011-01-13 19:59:54 +00001612 * cipher[:keycount]-mode-iv:ivopts
Milan Broz5ebaee62010-08-12 04:14:07 +01001613 */
1614 tmp = cipher_in;
Milan Brozd1f96422011-01-13 19:59:54 +00001615 keycount = strsep(&tmp, "-");
1616 cipher = strsep(&keycount, ":");
1617
1618 if (!keycount)
1619 cc->tfms_count = 1;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001620 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
Milan Brozd1f96422011-01-13 19:59:54 +00001621 !is_power_of_2(cc->tfms_count)) {
1622 ti->error = "Bad cipher key count specification";
1623 return -EINVAL;
1624 }
1625 cc->key_parts = cc->tfms_count;
Milan Brozda31a072013-10-28 23:21:03 +01001626 cc->key_extra_size = 0;
Milan Broz5ebaee62010-08-12 04:14:07 +01001627
1628 cc->cipher = kstrdup(cipher, GFP_KERNEL);
1629 if (!cc->cipher)
1630 goto bad_mem;
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 chainmode = strsep(&tmp, "-");
1633 ivopts = strsep(&tmp, "-");
1634 ivmode = strsep(&ivopts, ":");
1635
1636 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01001637 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Milan Broz7dbcd132011-01-13 19:59:52 +00001639 /*
1640 * For compatibility with the original dm-crypt mapping format, if
1641 * only the cipher name is supplied, use cbc-plain.
1642 */
Milan Broz5ebaee62010-08-12 04:14:07 +01001643 if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 chainmode = "cbc";
1645 ivmode = "plain";
1646 }
1647
Herbert Xud1806f62006-08-22 20:29:17 +10001648 if (strcmp(chainmode, "ecb") && !ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001649 ti->error = "IV mechanism required";
1650 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652
Milan Broz5ebaee62010-08-12 04:14:07 +01001653 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
1654 if (!cipher_api)
1655 goto bad_mem;
1656
1657 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
1658 "%s(%s)", chainmode, cipher);
1659 if (ret < 0) {
1660 kfree(cipher_api);
1661 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10001662 }
1663
Milan Broz5ebaee62010-08-12 04:14:07 +01001664 /* Allocate cipher */
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001665 ret = crypt_alloc_tfms(cc, cipher_api);
1666 if (ret < 0) {
1667 ti->error = "Error allocating crypto tfm";
1668 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Milan Broz5ebaee62010-08-12 04:14:07 +01001671 /* Initialize IV */
Herbert Xubbdb23b2016-01-24 21:16:36 +08001672 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
Milan Broz5ebaee62010-08-12 04:14:07 +01001673 if (cc->iv_size)
1674 /* at least a 64 bit sector number should fit in our buffer */
1675 cc->iv_size = max(cc->iv_size,
1676 (unsigned int)(sizeof(u64) / sizeof(u8)));
1677 else if (ivmode) {
1678 DMWARN("Selected cipher does not support IVs");
1679 ivmode = NULL;
1680 }
1681
1682 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (ivmode == NULL)
1684 cc->iv_gen_ops = NULL;
1685 else if (strcmp(ivmode, "plain") == 0)
1686 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00001687 else if (strcmp(ivmode, "plain64") == 0)
1688 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 else if (strcmp(ivmode, "essiv") == 0)
1690 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10001691 else if (strcmp(ivmode, "benbi") == 0)
1692 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07001693 else if (strcmp(ivmode, "null") == 0)
1694 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00001695 else if (strcmp(ivmode, "lmk") == 0) {
1696 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01001697 /*
1698 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00001699 * to length of provided multi-key string.
1700 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01001701 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00001702 */
Milan Brozda31a072013-10-28 23:21:03 +01001703 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00001704 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01001705 cc->key_extra_size = cc->key_size / cc->key_parts;
1706 }
Milan Brozed04d982013-10-28 23:21:04 +01001707 } else if (strcmp(ivmode, "tcw") == 0) {
1708 cc->iv_gen_ops = &crypt_iv_tcw_ops;
1709 cc->key_parts += 2; /* IV + whitening */
1710 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broz34745782011-01-13 19:59:55 +00001711 } else {
Milan Broz5ebaee62010-08-12 04:14:07 +01001712 ret = -EINVAL;
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001713 ti->error = "Invalid IV mode";
Milan Broz28513fc2010-08-12 04:14:06 +01001714 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716
Milan Brozda31a072013-10-28 23:21:03 +01001717 /* Initialize and set key */
1718 ret = crypt_set_key(cc, key);
1719 if (ret < 0) {
1720 ti->error = "Error decoding and setting key";
1721 goto bad;
1722 }
1723
Milan Broz28513fc2010-08-12 04:14:06 +01001724 /* Allocate IV */
1725 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
1726 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
1727 if (ret < 0) {
1728 ti->error = "Error creating IV";
1729 goto bad;
1730 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00001731 }
1732
Milan Broz28513fc2010-08-12 04:14:06 +01001733 /* Initialize IV (set keys for ESSIV etc) */
1734 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
1735 ret = cc->iv_gen_ops->init(cc);
1736 if (ret < 0) {
1737 ti->error = "Error initialising IV";
1738 goto bad;
1739 }
1740 }
1741
Milan Broz5ebaee62010-08-12 04:14:07 +01001742 ret = 0;
1743bad:
1744 kfree(cipher_api);
1745 return ret;
1746
1747bad_mem:
1748 ti->error = "Cannot allocate cipher strings";
1749 return -ENOMEM;
1750}
1751
1752/*
1753 * Construct an encryption mapping:
1754 * <cipher> <key> <iv_offset> <dev_path> <start>
1755 */
1756static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1757{
1758 struct crypt_config *cc;
Milan Broz772ae5f2011-08-02 12:32:08 +01001759 unsigned int key_size, opt_params;
Milan Broz5ebaee62010-08-12 04:14:07 +01001760 unsigned long long tmpll;
1761 int ret;
Mikulas Patockad49ec522014-08-28 11:09:31 -04001762 size_t iv_size_padding;
Milan Broz772ae5f2011-08-02 12:32:08 +01001763 struct dm_arg_set as;
1764 const char *opt_string;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001765 char dummy;
Milan Broz5ebaee62010-08-12 04:14:07 +01001766
Milan Broz772ae5f2011-08-02 12:32:08 +01001767 static struct dm_arg _args[] = {
Milan Brozc083ebe2017-03-16 15:39:44 +01001768 {0, 5, "Invalid number of feature args"},
Milan Broz772ae5f2011-08-02 12:32:08 +01001769 };
1770
1771 if (argc < 5) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001772 ti->error = "Not enough arguments";
1773 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
1775
Milan Broz5ebaee62010-08-12 04:14:07 +01001776 key_size = strlen(argv[1]) >> 1;
1777
1778 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1779 if (!cc) {
1780 ti->error = "Cannot allocate encryption context";
1781 return -ENOMEM;
1782 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00001783 cc->key_size = key_size;
Milan Brozc083ebe2017-03-16 15:39:44 +01001784 cc->sector_size = (1 << SECTOR_SHIFT);
Milan Broz5ebaee62010-08-12 04:14:07 +01001785
1786 ti->private = cc;
1787 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
1788 if (ret < 0)
1789 goto bad;
1790
Herbert Xubbdb23b2016-01-24 21:16:36 +08001791 cc->dmreq_start = sizeof(struct skcipher_request);
1792 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
Mikulas Patockad49ec522014-08-28 11:09:31 -04001793 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
1794
Herbert Xubbdb23b2016-01-24 21:16:36 +08001795 if (crypto_skcipher_alignmask(any_tfm(cc)) < CRYPTO_MINALIGN) {
Mikulas Patockad49ec522014-08-28 11:09:31 -04001796 /* Allocate the padding exactly */
1797 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
Herbert Xubbdb23b2016-01-24 21:16:36 +08001798 & crypto_skcipher_alignmask(any_tfm(cc));
Mikulas Patockad49ec522014-08-28 11:09:31 -04001799 } else {
1800 /*
1801 * If the cipher requires greater alignment than kmalloc
1802 * alignment, we don't know the exact position of the
1803 * initialization vector. We must assume worst case.
1804 */
Herbert Xubbdb23b2016-01-24 21:16:36 +08001805 iv_size_padding = crypto_skcipher_alignmask(any_tfm(cc));
Mikulas Patockad49ec522014-08-28 11:09:31 -04001806 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001807
Mikulas Patocka94f5e022015-02-13 08:25:26 -05001808 ret = -ENOMEM;
Milan Brozddd42ed2008-02-08 02:11:07 +00001809 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
Mikulas Patockad49ec522014-08-28 11:09:31 -04001810 sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size);
Milan Brozddd42ed2008-02-08 02:11:07 +00001811 if (!cc->req_pool) {
1812 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001813 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00001814 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001815
Mike Snitzer30187e12016-01-31 13:28:26 -05001816 cc->per_bio_data_size = ti->per_io_data_size =
Mikulas Patockad49ec522014-08-28 11:09:31 -04001817 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start +
1818 sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size,
1819 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001820
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001821 cc->page_pool = mempool_create_page_pool(BIO_MAX_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001823 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001824 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 }
1826
Jens Axboebb799ca2008-12-10 15:35:05 +01001827 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07001828 if (!cc->bs) {
1829 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01001830 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07001831 }
1832
Mikulas Patocka7145c242015-02-13 08:24:41 -05001833 mutex_init(&cc->bio_alloc_lock);
1834
Milan Broz28513fc2010-08-12 04:14:06 +01001835 ret = -EINVAL;
Milan Brozc083ebe2017-03-16 15:39:44 +01001836 if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
1837 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001838 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001839 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001841 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Vivek Goyale80d1c82015-07-31 09:20:36 -04001843 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
1844 if (ret) {
Milan Broz28513fc2010-08-12 04:14:06 +01001845 ti->error = "Device lookup failed";
1846 goto bad;
1847 }
1848
Vivek Goyale80d1c82015-07-31 09:20:36 -04001849 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001850 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001851 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001852 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001854 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Milan Broz772ae5f2011-08-02 12:32:08 +01001856 argv += 5;
1857 argc -= 5;
1858
1859 /* Optional parameters */
1860 if (argc) {
1861 as.argc = argc;
1862 as.argv = argv;
1863
1864 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
1865 if (ret)
1866 goto bad;
1867
Wei Yongjun44c144f2015-04-16 22:00:50 -04001868 ret = -EINVAL;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001869 while (opt_params--) {
1870 opt_string = dm_shift_arg(&as);
1871 if (!opt_string) {
1872 ti->error = "Not enough feature arguments";
1873 goto bad;
1874 }
Milan Broz772ae5f2011-08-02 12:32:08 +01001875
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001876 if (!strcasecmp(opt_string, "allow_discards"))
1877 ti->num_discard_bios = 1;
1878
1879 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
1880 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
1881
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001882 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
1883 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
1884
Milan Brozc083ebe2017-03-16 15:39:44 +01001885 else if (sscanf(opt_string, "sector_size:%u%c",
1886 &cc->sector_size, &dummy) == 1) {
1887 if (cc->sector_size < (1 << SECTOR_SHIFT) ||
1888 cc->sector_size > 4096 ||
1889 (1 << ilog2(cc->sector_size) != cc->sector_size)) {
1890 ti->error = "Invalid feature value for sector_size";
1891 goto bad;
1892 }
1893 } else if (!strcasecmp(opt_string, "iv_large_sectors"))
1894 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001895 else {
1896 ti->error = "Invalid feature arguments";
1897 goto bad;
1898 }
Milan Broz772ae5f2011-08-02 12:32:08 +01001899 }
1900 }
1901
Milan Broz28513fc2010-08-12 04:14:06 +01001902 ret = -ENOMEM;
Tim Murraye97c4ed2016-01-19 16:33:27 -08001903 cc->io_queue = alloc_workqueue("kcryptd_io",
1904 WQ_HIGHPRI |
1905 WQ_MEM_RECLAIM,
1906 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001907 if (!cc->io_queue) {
1908 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001909 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01001910 }
1911
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001912 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
Tim Murraye97c4ed2016-01-19 16:33:27 -08001913 cc->crypt_queue = alloc_workqueue("kcryptd",
1914 WQ_HIGHPRI |
1915 WQ_MEM_RECLAIM, 1);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001916 else
Tim Murraye97c4ed2016-01-19 16:33:27 -08001917 cc->crypt_queue = alloc_workqueue("kcryptd",
1918 WQ_HIGHPRI |
1919 WQ_MEM_RECLAIM |
1920 WQ_UNBOUND,
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001921 num_online_cpus());
Milan Brozcabf08e2007-10-19 22:38:58 +01001922 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01001923 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001924 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01001925 }
1926
Mikulas Patockadc267622015-02-13 08:25:59 -05001927 init_waitqueue_head(&cc->write_thread_wait);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001928 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05001929
1930 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write");
1931 if (IS_ERR(cc->write_thread)) {
1932 ret = PTR_ERR(cc->write_thread);
1933 cc->write_thread = NULL;
1934 ti->error = "Couldn't spawn write thread";
1935 goto bad;
1936 }
1937 wake_up_process(cc->write_thread);
1938
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001939 ti->num_flush_bios = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01001940 ti->discard_zeroes_data_unsupported = true;
Milan Broz983c7db2011-09-25 23:26:21 +01001941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return 0;
1943
Milan Broz28513fc2010-08-12 04:14:06 +01001944bad:
1945 crypt_dtr(ti);
1946 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947}
1948
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001949static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001951 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001952 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001953
Milan Broz772ae5f2011-08-02 12:32:08 +01001954 /*
Mike Christie28a8f0d2016-06-05 14:32:25 -05001955 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
1956 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
Mike Christiee6047142016-06-05 14:32:04 -05001957 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
Milan Broz772ae5f2011-08-02 12:32:08 +01001958 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06001959 if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
Mike Christie28a8f0d2016-06-05 14:32:25 -05001960 bio_op(bio) == REQ_OP_DISCARD)) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001961 bio->bi_bdev = cc->dev->bdev;
Milan Broz772ae5f2011-08-02 12:32:08 +01001962 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001963 bio->bi_iter.bi_sector = cc->start +
1964 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001965 return DM_MAPIO_REMAPPED;
1966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Mikulas Patocka4e870e92016-08-30 16:38:42 -04001968 /*
1969 * Check if bio is too large, split as needed.
1970 */
1971 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
1972 bio_data_dir(bio) == WRITE)
1973 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
1974
Milan Brozc083ebe2017-03-16 15:39:44 +01001975 /*
1976 * Ensure that bio is a multiple of internal sector encryption size
1977 * and is aligned to this size as defined in IO hints.
1978 */
1979 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
1980 return -EIO;
1981
1982 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
1983 return -EIO;
1984
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001985 io = dm_per_bio_data(bio, cc->per_bio_data_size);
1986 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Herbert Xubbdb23b2016-01-24 21:16:36 +08001987 io->ctx.req = (struct skcipher_request *)(io + 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001988
Milan Broz20c82532011-01-13 19:59:53 +00001989 if (bio_data_dir(io->base_bio) == READ) {
1990 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05001991 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00001992 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01001993 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001995 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001998static void crypt_status(struct dm_target *ti, status_type_t type,
1999 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Milan Broz5ebaee62010-08-12 04:14:07 +01002001 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002002 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002003 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 switch (type) {
2006 case STATUSTYPE_INFO:
2007 result[0] = '\0';
2008 break;
2009
2010 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00002011 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002013 if (cc->key_size > 0)
2014 for (i = 0; i < cc->key_size; i++)
2015 DMEMIT("%02x", cc->key[i]);
2016 else
2017 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Andrew Morton4ee218c2006-03-27 01:17:48 -08002019 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
2020 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01002021
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002022 num_feature_args += !!ti->num_discard_bios;
2023 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002024 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Milan Brozc083ebe2017-03-16 15:39:44 +01002025 num_feature_args += (cc->sector_size != (1 << SECTOR_SHIFT)) ? 1 : 0;
2026 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002027 if (num_feature_args) {
2028 DMEMIT(" %d", num_feature_args);
2029 if (ti->num_discard_bios)
2030 DMEMIT(" allow_discards");
2031 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2032 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002033 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
2034 DMEMIT(" submit_from_crypt_cpus");
Milan Brozc083ebe2017-03-16 15:39:44 +01002035 if (cc->sector_size != (1 << SECTOR_SHIFT))
2036 DMEMIT(" sector_size:%d", cc->sector_size);
2037 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
2038 DMEMIT(" iv_large_sectors");
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002039 }
Milan Broz772ae5f2011-08-02 12:32:08 +01002040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 break;
2042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
Milan Broze48d4bb2006-10-03 01:15:37 -07002045static void crypt_postsuspend(struct dm_target *ti)
2046{
2047 struct crypt_config *cc = ti->private;
2048
2049 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2050}
2051
2052static int crypt_preresume(struct dm_target *ti)
2053{
2054 struct crypt_config *cc = ti->private;
2055
2056 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
2057 DMERR("aborting resume - crypt key is not set.");
2058 return -EAGAIN;
2059 }
2060
2061 return 0;
2062}
2063
2064static void crypt_resume(struct dm_target *ti)
2065{
2066 struct crypt_config *cc = ti->private;
2067
2068 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2069}
2070
2071/* Message interface
2072 * key set <key>
2073 * key wipe
2074 */
2075static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
2076{
2077 struct crypt_config *cc = ti->private;
Milan Broz542da312009-12-10 23:51:57 +00002078 int ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07002079
2080 if (argc < 2)
2081 goto error;
2082
Mike Snitzer498f0102011-08-02 12:32:04 +01002083 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07002084 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
2085 DMWARN("not suspended during key manipulation.");
2086 return -EINVAL;
2087 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002088 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Milan Broz542da312009-12-10 23:51:57 +00002089 ret = crypt_set_key(cc, argv[2]);
2090 if (ret)
2091 return ret;
2092 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
2093 ret = cc->iv_gen_ops->init(cc);
2094 return ret;
2095 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002096 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00002097 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
2098 ret = cc->iv_gen_ops->wipe(cc);
2099 if (ret)
2100 return ret;
2101 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002102 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00002103 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002104 }
2105
2106error:
2107 DMWARN("unrecognised message received.");
2108 return -EINVAL;
2109}
2110
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002111static int crypt_iterate_devices(struct dm_target *ti,
2112 iterate_devices_callout_fn fn, void *data)
2113{
2114 struct crypt_config *cc = ti->private;
2115
Mike Snitzer5dea2712009-07-23 20:30:42 +01002116 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002117}
2118
Mike Snitzer586b2862015-09-09 21:34:51 -04002119static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
2120{
Milan Brozc083ebe2017-03-16 15:39:44 +01002121 struct crypt_config *cc = ti->private;
2122
Mike Snitzer586b2862015-09-09 21:34:51 -04002123 /*
2124 * Unfortunate constraint that is required to avoid the potential
2125 * for exceeding underlying device's max_segments limits -- due to
2126 * crypt_alloc_buffer() possibly allocating pages for the encryption
2127 * bio that are not as physically contiguous as the original bio.
2128 */
2129 limits->max_segment_size = PAGE_SIZE;
Milan Brozc083ebe2017-03-16 15:39:44 +01002130
2131 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
2132 limits->logical_block_size = cc->sector_size;
2133 limits->physical_block_size = cc->sector_size;
2134 blk_limits_io_min(limits, cc->sector_size);
2135 }
Mike Snitzer586b2862015-09-09 21:34:51 -04002136}
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138static struct target_type crypt_target = {
2139 .name = "crypt",
Milan Brozc083ebe2017-03-16 15:39:44 +01002140 .version = {1, 17, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 .module = THIS_MODULE,
2142 .ctr = crypt_ctr,
2143 .dtr = crypt_dtr,
2144 .map = crypt_map,
2145 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07002146 .postsuspend = crypt_postsuspend,
2147 .preresume = crypt_preresume,
2148 .resume = crypt_resume,
2149 .message = crypt_message,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002150 .iterate_devices = crypt_iterate_devices,
Mike Snitzer586b2862015-09-09 21:34:51 -04002151 .io_hints = crypt_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152};
2153
2154static int __init dm_crypt_init(void)
2155{
2156 int r;
2157
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05002159 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002160 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return r;
2163}
2164
2165static void __exit dm_crypt_exit(void)
2166{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002167 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168}
2169
2170module_init(dm_crypt_init);
2171module_exit(dm_crypt_exit);
2172
Jana Saoutbf142992014-06-24 14:27:04 -04002173MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
2175MODULE_LICENSE("GPL");