blob: 6a1d5ab0931751b1e6c07e192f4df5894c39fc2e [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,
Michael Halcrow9832c012017-10-11 16:19:57 -0700116 DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD,
117 DM_CRYPT_ENCRYPT_OVERRIDE };
Andi Kleenc0297722011-01-13 19:59:53 +0000118
Milan Brozc083ebe2017-03-16 15:39:44 +0100119enum cipher_flags {
120 CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */
121};
122
Andi Kleenc0297722011-01-13 19:59:53 +0000123/*
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500124 * The fields in here must be read only after initialization.
Andi Kleenc0297722011-01-13 19:59:53 +0000125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126struct crypt_config {
127 struct dm_dev *dev;
128 sector_t start;
129
130 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000131 * pool for per bio private data, crypto requests and
132 * encryption requeusts/buffer pages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
Milan Brozddd42ed2008-02-08 02:11:07 +0000134 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 mempool_t *page_pool;
Milan Broz6a24c712006-10-03 01:15:40 -0700136 struct bio_set *bs;
Mikulas Patocka7145c242015-02-13 08:24:41 -0500137 struct mutex bio_alloc_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Milan Brozcabf08e2007-10-19 22:38:58 +0100139 struct workqueue_struct *io_queue;
140 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700141
Mikulas Patockadc267622015-02-13 08:25:59 -0500142 struct task_struct *write_thread;
143 wait_queue_head_t write_thread_wait;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -0500144 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -0500145
Milan Broz5ebaee62010-08-12 04:14:07 +0100146 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000147 char *cipher_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800150 union {
Milan Broz60473592009-12-10 23:51:55 +0000151 struct iv_essiv_private essiv;
152 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000153 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100154 struct iv_tcw_private tcw;
Herbert Xu79066ad2006-12-05 13:41:52 -0800155 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 sector_t iv_offset;
157 unsigned int iv_size;
Mikulas Patocka52ad0c42017-03-23 10:23:14 -0400158 unsigned short int sector_size;
159 unsigned char sector_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100161 /* ESSIV: struct crypto_cipher *essiv_tfm */
162 void *iv_private;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800163 struct crypto_skcipher **tfms;
Milan Brozd1f96422011-01-13 19:59:54 +0000164 unsigned tfms_count;
Milan Brozc083ebe2017-03-16 15:39:44 +0100165 unsigned long cipher_flags;
Andi Kleenc0297722011-01-13 19:59:53 +0000166
167 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000168 * Layout of each crypto request:
169 *
Herbert Xubbdb23b2016-01-24 21:16:36 +0800170 * struct skcipher_request
Milan Brozddd42ed2008-02-08 02:11:07 +0000171 * context
172 * padding
173 * struct dm_crypt_request
174 * padding
175 * IV
176 *
177 * The padding is added so that dm_crypt_request and the IV are
178 * correctly aligned.
179 */
180 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000181
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400182 unsigned int per_bio_data_size;
183
Milan Broze48d4bb2006-10-03 01:15:37 -0700184 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100186 unsigned int key_parts; /* independent parts in key buffer */
187 unsigned int key_extra_size; /* additional keys length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 u8 key[0];
189};
190
Mikulas Patocka0a83df62016-07-15 17:30:20 -0400191#define MIN_IOS 64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100193static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000194static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Broz2dc53272011-01-13 19:59:54 +0000195static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq);
Olaf Kirch027581f2007-05-09 02:32:52 -0700196
Andi Kleenc0297722011-01-13 19:59:53 +0000197/*
198 * Use this to access cipher attributes that are the same for each CPU.
199 */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800200static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +0000201{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100202 return cc->tfms[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * Different IV generation algorithms:
207 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000208 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200209 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 *
Milan Broz61afef62009-12-10 23:52:25 +0000211 * plain64: the initial vector is the 64-bit little-endian version of the sector
212 * number, padded with zeros if necessary.
213 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000214 * essiv: "encrypted sector|salt initial vector", the sector number is
215 * encrypted with the bulk cipher using a salt as key. The salt
216 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 *
Rik Snel48527fa2006-09-03 08:56:39 +1000218 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
219 * (needed for LRW-32-AES and possible other narrow block modes)
220 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700221 * null: the initial vector is always zero. Provides compatibility with
222 * obsolete loop_fish2 devices. Do not use for new devices.
223 *
Milan Broz34745782011-01-13 19:59:55 +0000224 * lmk: Compatible implementation of the block chaining mode used
225 * by the Loop-AES block device encryption system
226 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
227 * It operates on full 512 byte sectors and uses CBC
228 * with an IV derived from the sector number, the data and
229 * optionally extra IV seed.
230 * This means that after decryption the first block
231 * of sector must be tweaked according to decrypted data.
232 * Loop-AES can use three encryption schemes:
233 * version 1: is plain aes-cbc mode
234 * version 2: uses 64 multikey scheme with lmk IV generator
235 * version 3: the same as version 2 with additional IV seed
236 * (it uses 65 keys, last key is used as IV seed)
237 *
Milan Brozed04d982013-10-28 23:21:04 +0100238 * tcw: Compatible implementation of the block chaining mode used
239 * by the TrueCrypt device encryption system (prior to version 4.1).
Milan Broze44f23b2015-04-05 18:03:10 +0200240 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
Milan Brozed04d982013-10-28 23:21:04 +0100241 * It operates on full 512 byte sectors and uses CBC
242 * with an IV derived from initial key and the sector number.
243 * In addition, whitening value is applied on every sector, whitening
244 * is calculated from initial key, sector number and mixed using CRC32.
245 * Note that this encryption scheme is vulnerable to watermarking attacks
246 * and should be used for old compatible containers access only.
247 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 * plumb: unimplemented, see:
249 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
250 */
251
Milan Broz2dc53272011-01-13 19:59:54 +0000252static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
253 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100256 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 return 0;
259}
260
Milan Broz61afef62009-12-10 23:52:25 +0000261static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000262 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000263{
264 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100265 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000266
267 return 0;
268}
269
Milan Brozb95bf2d2009-12-10 23:51:56 +0000270/* Initialise ESSIV - compute salt but no local memory allocations */
271static int crypt_iv_essiv_init(struct crypt_config *cc)
272{
273 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800274 AHASH_REQUEST_ON_STACK(req, essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000275 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000276 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100277 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000278
279 sg_init_one(&sg, cc->key, cc->key_size);
Herbert Xubbdb23b2016-01-24 21:16:36 +0800280 ahash_request_set_tfm(req, essiv->hash_tfm);
281 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
282 ahash_request_set_crypt(req, &sg, essiv->salt, cc->key_size);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000283
Herbert Xubbdb23b2016-01-24 21:16:36 +0800284 err = crypto_ahash_digest(req);
285 ahash_request_zero(req);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000286 if (err)
287 return err;
288
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100289 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000290
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100291 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800292 crypto_ahash_digestsize(essiv->hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100293 if (err)
294 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000295
296 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000297}
298
Milan Broz542da312009-12-10 23:51:57 +0000299/* Wipe salt and reset key derived from volume key */
300static int crypt_iv_essiv_wipe(struct crypt_config *cc)
301{
302 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800303 unsigned salt_size = crypto_ahash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000304 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100305 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000306
307 memset(essiv->salt, 0, salt_size);
308
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100309 essiv_tfm = cc->iv_private;
310 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
311 if (r)
312 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000313
314 return err;
315}
316
317/* Set up per cpu cipher state */
318static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
319 struct dm_target *ti,
320 u8 *salt, unsigned saltsize)
321{
322 struct crypto_cipher *essiv_tfm;
323 int err;
324
325 /* Setup the essiv_tfm with the given salt */
326 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
327 if (IS_ERR(essiv_tfm)) {
328 ti->error = "Error allocating crypto tfm for ESSIV";
329 return essiv_tfm;
330 }
331
332 if (crypto_cipher_blocksize(essiv_tfm) !=
Herbert Xubbdb23b2016-01-24 21:16:36 +0800333 crypto_skcipher_ivsize(any_tfm(cc))) {
Andi Kleenc0297722011-01-13 19:59:53 +0000334 ti->error = "Block size of ESSIV cipher does "
335 "not match IV size of block cipher";
336 crypto_free_cipher(essiv_tfm);
337 return ERR_PTR(-EINVAL);
338 }
339
340 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
341 if (err) {
342 ti->error = "Failed to set key for ESSIV cipher";
343 crypto_free_cipher(essiv_tfm);
344 return ERR_PTR(err);
345 }
346
347 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000348}
349
Milan Broz60473592009-12-10 23:51:55 +0000350static void crypt_iv_essiv_dtr(struct crypt_config *cc)
351{
Andi Kleenc0297722011-01-13 19:59:53 +0000352 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000353 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
354
Herbert Xubbdb23b2016-01-24 21:16:36 +0800355 crypto_free_ahash(essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000356 essiv->hash_tfm = NULL;
357
358 kzfree(essiv->salt);
359 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000360
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100361 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000362
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100363 if (essiv_tfm)
364 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000365
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100366 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100370 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Milan Broz5861f1b2009-12-10 23:51:56 +0000372 struct crypto_cipher *essiv_tfm = NULL;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800373 struct crypto_ahash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000374 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100375 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Milan Broz5861f1b2009-12-10 23:51:56 +0000377 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700378 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return -EINVAL;
380 }
381
Milan Brozb95bf2d2009-12-10 23:51:56 +0000382 /* Allocate hash algorithm */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800383 hash_tfm = crypto_alloc_ahash(opts, 0, CRYPTO_ALG_ASYNC);
Herbert Xu35058682006-08-24 19:10:20 +1000384 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700385 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000386 err = PTR_ERR(hash_tfm);
387 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Herbert Xubbdb23b2016-01-24 21:16:36 +0800390 salt = kzalloc(crypto_ahash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000391 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700392 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000393 err = -ENOMEM;
394 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
Milan Brozb95bf2d2009-12-10 23:51:56 +0000397 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000398 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
399
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100400 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800401 crypto_ahash_digestsize(hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100402 if (IS_ERR(essiv_tfm)) {
403 crypt_iv_essiv_dtr(cc);
404 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000405 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100406 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000409
410bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000411 if (hash_tfm && !IS_ERR(hash_tfm))
Herbert Xubbdb23b2016-01-24 21:16:36 +0800412 crypto_free_ahash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000413 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000414 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Milan Broz2dc53272011-01-13 19:59:54 +0000417static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
418 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100420 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100423 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000424 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 0;
427}
428
Rik Snel48527fa2006-09-03 08:56:39 +1000429static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
430 const char *opts)
431{
Herbert Xubbdb23b2016-01-24 21:16:36 +0800432 unsigned bs = crypto_skcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800433 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000434
435 /* we need to calculate how far we must shift the sector count
436 * to get the cipher block count, we use this shift in _gen */
437
438 if (1 << log != bs) {
439 ti->error = "cypher blocksize is not a power of 2";
440 return -EINVAL;
441 }
442
443 if (log > 9) {
444 ti->error = "cypher blocksize is > 512";
445 return -EINVAL;
446 }
447
Milan Broz60473592009-12-10 23:51:55 +0000448 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000449
450 return 0;
451}
452
453static void crypt_iv_benbi_dtr(struct crypt_config *cc)
454{
Rik Snel48527fa2006-09-03 08:56:39 +1000455}
456
Milan Broz2dc53272011-01-13 19:59:54 +0000457static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
458 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000459{
Herbert Xu79066ad2006-12-05 13:41:52 -0800460 __be64 val;
461
Rik Snel48527fa2006-09-03 08:56:39 +1000462 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800463
Milan Broz2dc53272011-01-13 19:59:54 +0000464 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800465 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return 0;
468}
469
Milan Broz2dc53272011-01-13 19:59:54 +0000470static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
471 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700472{
473 memset(iv, 0, cc->iv_size);
474
475 return 0;
476}
477
Milan Broz34745782011-01-13 19:59:55 +0000478static void crypt_iv_lmk_dtr(struct crypt_config *cc)
479{
480 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
481
482 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
483 crypto_free_shash(lmk->hash_tfm);
484 lmk->hash_tfm = NULL;
485
486 kzfree(lmk->seed);
487 lmk->seed = NULL;
488}
489
490static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
491 const char *opts)
492{
493 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
494
Milan Brozc083ebe2017-03-16 15:39:44 +0100495 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
496 ti->error = "Unsupported sector size for LMK";
497 return -EINVAL;
498 }
499
Milan Broz34745782011-01-13 19:59:55 +0000500 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
501 if (IS_ERR(lmk->hash_tfm)) {
502 ti->error = "Error initializing LMK hash";
503 return PTR_ERR(lmk->hash_tfm);
504 }
505
506 /* No seed in LMK version 2 */
507 if (cc->key_parts == cc->tfms_count) {
508 lmk->seed = NULL;
509 return 0;
510 }
511
512 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
513 if (!lmk->seed) {
514 crypt_iv_lmk_dtr(cc);
515 ti->error = "Error kmallocing seed storage in LMK";
516 return -ENOMEM;
517 }
518
519 return 0;
520}
521
522static int crypt_iv_lmk_init(struct crypt_config *cc)
523{
524 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
525 int subkey_size = cc->key_size / cc->key_parts;
526
527 /* LMK seed is on the position of LMK_KEYS + 1 key */
528 if (lmk->seed)
529 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
530 crypto_shash_digestsize(lmk->hash_tfm));
531
532 return 0;
533}
534
535static int crypt_iv_lmk_wipe(struct crypt_config *cc)
536{
537 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
538
539 if (lmk->seed)
540 memset(lmk->seed, 0, LMK_SEED_SIZE);
541
542 return 0;
543}
544
545static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
546 struct dm_crypt_request *dmreq,
547 u8 *data)
548{
549 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200550 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000551 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100552 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000553 int i, r;
554
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200555 desc->tfm = lmk->hash_tfm;
556 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Broz34745782011-01-13 19:59:55 +0000557
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200558 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000559 if (r)
560 return r;
561
562 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200563 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000564 if (r)
565 return r;
566 }
567
568 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200569 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000570 if (r)
571 return r;
572
573 /* Sector is cropped to 56 bits here */
574 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
575 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
576 buf[2] = cpu_to_le32(4024);
577 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200578 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000579 if (r)
580 return r;
581
582 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200583 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000584 if (r)
585 return r;
586
587 for (i = 0; i < MD5_HASH_WORDS; i++)
588 __cpu_to_le32s(&md5state.hash[i]);
589 memcpy(iv, &md5state.hash, cc->iv_size);
590
591 return 0;
592}
593
594static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
595 struct dm_crypt_request *dmreq)
596{
597 u8 *src;
598 int r = 0;
599
600 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Cong Wangc2e022c2011-11-28 13:26:02 +0800601 src = kmap_atomic(sg_page(&dmreq->sg_in));
Milan Broz34745782011-01-13 19:59:55 +0000602 r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800603 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000604 } else
605 memset(iv, 0, cc->iv_size);
606
607 return r;
608}
609
610static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
611 struct dm_crypt_request *dmreq)
612{
613 u8 *dst;
614 int r;
615
616 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
617 return 0;
618
Cong Wangc2e022c2011-11-28 13:26:02 +0800619 dst = kmap_atomic(sg_page(&dmreq->sg_out));
Milan Broz34745782011-01-13 19:59:55 +0000620 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset);
621
622 /* Tweak the first block of plaintext sector */
623 if (!r)
624 crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size);
625
Cong Wangc2e022c2011-11-28 13:26:02 +0800626 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000627 return r;
628}
629
Milan Brozed04d982013-10-28 23:21:04 +0100630static void crypt_iv_tcw_dtr(struct crypt_config *cc)
631{
632 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
633
634 kzfree(tcw->iv_seed);
635 tcw->iv_seed = NULL;
636 kzfree(tcw->whitening);
637 tcw->whitening = NULL;
638
639 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
640 crypto_free_shash(tcw->crc32_tfm);
641 tcw->crc32_tfm = NULL;
642}
643
644static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
645 const char *opts)
646{
647 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
648
Milan Brozc083ebe2017-03-16 15:39:44 +0100649 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
650 ti->error = "Unsupported sector size for TCW";
651 return -EINVAL;
652 }
653
Milan Brozed04d982013-10-28 23:21:04 +0100654 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
655 ti->error = "Wrong key size for TCW";
656 return -EINVAL;
657 }
658
659 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
660 if (IS_ERR(tcw->crc32_tfm)) {
661 ti->error = "Error initializing CRC32 in TCW";
662 return PTR_ERR(tcw->crc32_tfm);
663 }
664
665 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
666 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
667 if (!tcw->iv_seed || !tcw->whitening) {
668 crypt_iv_tcw_dtr(cc);
669 ti->error = "Error allocating seed storage in TCW";
670 return -ENOMEM;
671 }
672
673 return 0;
674}
675
676static int crypt_iv_tcw_init(struct crypt_config *cc)
677{
678 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
679 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
680
681 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
682 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
683 TCW_WHITENING_SIZE);
684
685 return 0;
686}
687
688static int crypt_iv_tcw_wipe(struct crypt_config *cc)
689{
690 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
691
692 memset(tcw->iv_seed, 0, cc->iv_size);
693 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
694
695 return 0;
696}
697
698static int crypt_iv_tcw_whitening(struct crypt_config *cc,
699 struct dm_crypt_request *dmreq,
700 u8 *data)
701{
702 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200703 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100704 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200705 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100706 int i, r;
707
708 /* xor whitening with sector number */
709 memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
710 crypto_xor(buf, (u8 *)&sector, 8);
711 crypto_xor(&buf[8], (u8 *)&sector, 8);
712
713 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200714 desc->tfm = tcw->crc32_tfm;
715 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Brozed04d982013-10-28 23:21:04 +0100716 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200717 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100718 if (r)
719 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200720 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100721 if (r)
722 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200723 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100724 if (r)
725 goto out;
726 }
727 crypto_xor(&buf[0], &buf[12], 4);
728 crypto_xor(&buf[4], &buf[8], 4);
729
730 /* apply whitening (8 bytes) to whole sector */
731 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
732 crypto_xor(data + i * 8, buf, 8);
733out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100734 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100735 return r;
736}
737
738static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
739 struct dm_crypt_request *dmreq)
740{
741 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200742 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100743 u8 *src;
744 int r = 0;
745
746 /* Remove whitening from ciphertext */
747 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
748 src = kmap_atomic(sg_page(&dmreq->sg_in));
749 r = crypt_iv_tcw_whitening(cc, dmreq, src + dmreq->sg_in.offset);
750 kunmap_atomic(src);
751 }
752
753 /* Calculate IV */
754 memcpy(iv, tcw->iv_seed, cc->iv_size);
755 crypto_xor(iv, (u8 *)&sector, 8);
756 if (cc->iv_size > 8)
757 crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
758
759 return r;
760}
761
762static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
763 struct dm_crypt_request *dmreq)
764{
765 u8 *dst;
766 int r;
767
768 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
769 return 0;
770
771 /* Apply whitening on ciphertext */
772 dst = kmap_atomic(sg_page(&dmreq->sg_out));
773 r = crypt_iv_tcw_whitening(cc, dmreq, dst + dmreq->sg_out.offset);
774 kunmap_atomic(dst);
775
776 return r;
777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779static struct crypt_iv_operations crypt_iv_plain_ops = {
780 .generator = crypt_iv_plain_gen
781};
782
Milan Broz61afef62009-12-10 23:52:25 +0000783static struct crypt_iv_operations crypt_iv_plain64_ops = {
784 .generator = crypt_iv_plain64_gen
785};
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787static struct crypt_iv_operations crypt_iv_essiv_ops = {
788 .ctr = crypt_iv_essiv_ctr,
789 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000790 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000791 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 .generator = crypt_iv_essiv_gen
793};
794
Rik Snel48527fa2006-09-03 08:56:39 +1000795static struct crypt_iv_operations crypt_iv_benbi_ops = {
796 .ctr = crypt_iv_benbi_ctr,
797 .dtr = crypt_iv_benbi_dtr,
798 .generator = crypt_iv_benbi_gen
799};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Ludwig Nussel46b47732007-05-09 02:32:55 -0700801static struct crypt_iv_operations crypt_iv_null_ops = {
802 .generator = crypt_iv_null_gen
803};
804
Milan Broz34745782011-01-13 19:59:55 +0000805static struct crypt_iv_operations crypt_iv_lmk_ops = {
806 .ctr = crypt_iv_lmk_ctr,
807 .dtr = crypt_iv_lmk_dtr,
808 .init = crypt_iv_lmk_init,
809 .wipe = crypt_iv_lmk_wipe,
810 .generator = crypt_iv_lmk_gen,
811 .post = crypt_iv_lmk_post
812};
813
Milan Brozed04d982013-10-28 23:21:04 +0100814static struct crypt_iv_operations crypt_iv_tcw_ops = {
815 .ctr = crypt_iv_tcw_ctr,
816 .dtr = crypt_iv_tcw_dtr,
817 .init = crypt_iv_tcw_init,
818 .wipe = crypt_iv_tcw_wipe,
819 .generator = crypt_iv_tcw_gen,
820 .post = crypt_iv_tcw_post
821};
822
Milan Brozd469f842007-10-19 22:42:37 +0100823static void crypt_convert_init(struct crypt_config *cc,
824 struct convert_context *ctx,
825 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000826 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
828 ctx->bio_in = bio_in;
829 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700830 if (bio_in)
831 ctx->iter_in = bio_in->bi_iter;
832 if (bio_out)
833 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100834 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000835 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Huang Yingb2174ee2009-03-16 17:44:33 +0000838static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800839 struct skcipher_request *req)
Huang Yingb2174ee2009-03-16 17:44:33 +0000840{
841 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
842}
843
Herbert Xubbdb23b2016-01-24 21:16:36 +0800844static struct skcipher_request *req_of_dmreq(struct crypt_config *cc,
Huang Yingb2174ee2009-03-16 17:44:33 +0000845 struct dm_crypt_request *dmreq)
846{
Herbert Xubbdb23b2016-01-24 21:16:36 +0800847 return (struct skcipher_request *)((char *)dmreq - cc->dmreq_start);
Huang Yingb2174ee2009-03-16 17:44:33 +0000848}
849
Milan Broz2dc53272011-01-13 19:59:54 +0000850static u8 *iv_of_dmreq(struct crypt_config *cc,
851 struct dm_crypt_request *dmreq)
852{
853 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
Herbert Xubbdb23b2016-01-24 21:16:36 +0800854 crypto_skcipher_alignmask(any_tfm(cc)) + 1);
Milan Broz2dc53272011-01-13 19:59:54 +0000855}
856
Milan Broz01482b72008-02-08 02:11:04 +0000857static int crypt_convert_block(struct crypt_config *cc,
Milan Broz3a7f6c92008-02-08 02:11:14 +0000858 struct convert_context *ctx,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800859 struct skcipher_request *req)
Milan Broz01482b72008-02-08 02:11:04 +0000860{
Kent Overstreet003b5c52013-10-11 15:45:43 -0700861 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
862 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000863 struct dm_crypt_request *dmreq;
864 u8 *iv;
Mikulas Patocka40b62292012-07-27 15:08:04 +0100865 int r;
Milan Broz01482b72008-02-08 02:11:04 +0000866
Milan Brozc083ebe2017-03-16 15:39:44 +0100867 /* Reject unexpected unaligned bio. */
Mikulas Patocka946db242017-11-07 10:35:57 -0500868 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Brozc083ebe2017-03-16 15:39:44 +0100869 return -EIO;
870
Huang Yingb2174ee2009-03-16 17:44:33 +0000871 dmreq = dmreq_of_req(cc, req);
Milan Broz2dc53272011-01-13 19:59:54 +0000872 iv = iv_of_dmreq(cc, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000873
Mikulas Patockac66029f2012-07-27 15:08:05 +0100874 dmreq->iv_sector = ctx->cc_sector;
Milan Brozc083ebe2017-03-16 15:39:44 +0100875 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patocka52ad0c42017-03-23 10:23:14 -0400876 dmreq->iv_sector >>= cc->sector_shift;
Huang Yingb2174ee2009-03-16 17:44:33 +0000877 dmreq->ctx = ctx;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000878 sg_init_table(&dmreq->sg_in, 1);
Milan Brozc083ebe2017-03-16 15:39:44 +0100879 sg_set_page(&dmreq->sg_in, bv_in.bv_page, cc->sector_size,
Kent Overstreet003b5c52013-10-11 15:45:43 -0700880 bv_in.bv_offset);
Milan Broz01482b72008-02-08 02:11:04 +0000881
Milan Broz3a7f6c92008-02-08 02:11:14 +0000882 sg_init_table(&dmreq->sg_out, 1);
Milan Brozc083ebe2017-03-16 15:39:44 +0100883 sg_set_page(&dmreq->sg_out, bv_out.bv_page, cc->sector_size,
Kent Overstreet003b5c52013-10-11 15:45:43 -0700884 bv_out.bv_offset);
Milan Broz01482b72008-02-08 02:11:04 +0000885
Milan Brozc083ebe2017-03-16 15:39:44 +0100886 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
887 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Broz01482b72008-02-08 02:11:04 +0000888
Milan Broz3a7f6c92008-02-08 02:11:14 +0000889 if (cc->iv_gen_ops) {
Milan Broz2dc53272011-01-13 19:59:54 +0000890 r = cc->iv_gen_ops->generator(cc, iv, dmreq);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000891 if (r < 0)
892 return r;
893 }
894
Herbert Xubbdb23b2016-01-24 21:16:36 +0800895 skcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out,
Milan Brozc083ebe2017-03-16 15:39:44 +0100896 cc->sector_size, iv);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000897
898 if (bio_data_dir(ctx->bio_in) == WRITE)
Herbert Xubbdb23b2016-01-24 21:16:36 +0800899 r = crypto_skcipher_encrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000900 else
Herbert Xubbdb23b2016-01-24 21:16:36 +0800901 r = crypto_skcipher_decrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000902
Milan Broz2dc53272011-01-13 19:59:54 +0000903 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
904 r = cc->iv_gen_ops->post(cc, iv, dmreq);
905
Milan Broz3a7f6c92008-02-08 02:11:14 +0000906 return r;
Milan Broz01482b72008-02-08 02:11:04 +0000907}
908
Milan Broz95497a92008-02-08 02:11:12 +0000909static void kcryptd_async_done(struct crypto_async_request *async_req,
910 int error);
Andi Kleenc0297722011-01-13 19:59:53 +0000911
Milan Brozddd42ed2008-02-08 02:11:07 +0000912static void crypt_alloc_req(struct crypt_config *cc,
913 struct convert_context *ctx)
914{
Mikulas Patockac66029f2012-07-27 15:08:05 +0100915 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +0000916
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500917 if (!ctx->req)
918 ctx->req = mempool_alloc(cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +0000919
Herbert Xubbdb23b2016-01-24 21:16:36 +0800920 skcipher_request_set_tfm(ctx->req, cc->tfms[key_index]);
Milan Broz54cea3f2015-05-15 17:00:25 +0200921
922 /*
923 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
924 * requests if driver request queue is full.
925 */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800926 skcipher_request_set_callback(ctx->req,
Andi Kleenc0297722011-01-13 19:59:53 +0000927 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500928 kcryptd_async_done, dmreq_of_req(cc, ctx->req));
Milan Brozddd42ed2008-02-08 02:11:07 +0000929}
930
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400931static void crypt_free_req(struct crypt_config *cc,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800932 struct skcipher_request *req, struct bio *base_bio)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400933{
934 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
935
Herbert Xubbdb23b2016-01-24 21:16:36 +0800936 if ((struct skcipher_request *)(io + 1) != req)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400937 mempool_free(req, cc->req_pool);
938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940/*
941 * Encrypt / decrypt data from one bio to another one (can be the same one)
942 */
943static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +0100944 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Mikulas Patocka52ad0c42017-03-23 10:23:14 -0400946 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT;
Milan Broz3f1e9072008-03-28 14:16:07 -0700947 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Mikulas Patocka40b62292012-07-27 15:08:04 +0100949 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +0100950
Kent Overstreet003b5c52013-10-11 15:45:43 -0700951 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Milan Broz3a7f6c92008-02-08 02:11:14 +0000953 crypt_alloc_req(cc, ctx);
Mikulas Patocka40b62292012-07-27 15:08:04 +0100954 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700955
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500956 r = crypt_convert_block(cc, ctx, ctx->req);
Milan Broz3a7f6c92008-02-08 02:11:14 +0000957
958 switch (r) {
Milan Broz54cea3f2015-05-15 17:00:25 +0200959 /*
960 * The request was queued by a crypto driver
961 * but the driver request queue is full, let's wait.
962 */
Milan Broz3a7f6c92008-02-08 02:11:14 +0000963 case -EBUSY:
964 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -0800965 reinit_completion(&ctx->restart);
Milan Broz54cea3f2015-05-15 17:00:25 +0200966 /* fall through */
967 /*
968 * The request is queued and processed asynchronously,
969 * completion function kcryptd_async_done() will be called.
970 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +0200971 case -EINPROGRESS:
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500972 ctx->req = NULL;
Milan Brozc083ebe2017-03-16 15:39:44 +0100973 ctx->cc_sector += sector_step;
Milan Broz3a7f6c92008-02-08 02:11:14 +0000974 continue;
Milan Broz54cea3f2015-05-15 17:00:25 +0200975 /*
976 * The request was already processed (synchronously).
977 */
Milan Broz3f1e9072008-03-28 14:16:07 -0700978 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +0100979 atomic_dec(&ctx->cc_pending);
Milan Brozc083ebe2017-03-16 15:39:44 +0100980 ctx->cc_sector += sector_step;
Milan Brozc7f1b202008-07-02 09:34:28 +0100981 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -0700982 continue;
983
Milan Broz54cea3f2015-05-15 17:00:25 +0200984 /* There was an error while processing the request. */
Milan Broz3f1e9072008-03-28 14:16:07 -0700985 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +0100986 atomic_dec(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -0700987 return r;
988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990
Milan Broz3f1e9072008-03-28 14:16:07 -0700991 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
993
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -0500994static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
995
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996/*
997 * Generate a new unfragmented bio with the given size
Mike Snitzer586b2862015-09-09 21:34:51 -0400998 * This should never violate the device limitations (but only because
999 * max_segment_size is being constrained to PAGE_SIZE).
Mikulas Patocka7145c242015-02-13 08:24:41 -05001000 *
1001 * This function may be called concurrently. If we allocate from the mempool
1002 * concurrently, there is a possibility of deadlock. For example, if we have
1003 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1004 * the mempool concurrently, it may deadlock in a situation where both processes
1005 * have allocated 128 pages and the mempool is exhausted.
1006 *
1007 * In order to avoid this scenario we allocate the pages under a mutex.
1008 *
1009 * In order to not degrade performance with excessive locking, we try
1010 * non-blocking allocations without a mutex first but on failure we fallback
1011 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001013static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001015 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001016 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001018 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
1019 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +00001020 struct page *page;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001021 struct bio_vec *bvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Mikulas Patocka7145c242015-02-13 08:24:41 -05001023retry:
Mel Gormand0164ad2015-11-06 16:28:21 -08001024 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001025 mutex_lock(&cc->bio_alloc_lock);
1026
Olaf Kirch2f9941b2007-05-09 02:32:53 -07001027 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -07001028 if (!clone)
Mikulas Patocka7145c242015-02-13 08:24:41 -05001029 goto return_clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Olaf Kirch027581f2007-05-09 02:32:52 -07001031 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -07001032
Mikulas Patocka7145c242015-02-13 08:24:41 -05001033 remaining_size = size;
1034
Olaf Kirchf97380b2007-05-09 02:32:54 -07001035 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +00001036 page = mempool_alloc(cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -05001037 if (!page) {
1038 crypt_free_buffer_pages(cc, clone);
1039 bio_put(clone);
Mel Gormand0164ad2015-11-06 16:28:21 -08001040 gfp_mask |= __GFP_DIRECT_RECLAIM;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001041 goto retry;
1042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Mikulas Patocka7145c242015-02-13 08:24:41 -05001044 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001046 bvec = &clone->bi_io_vec[clone->bi_vcnt++];
1047 bvec->bv_page = page;
1048 bvec->bv_len = len;
1049 bvec->bv_offset = 0;
1050
1051 clone->bi_iter.bi_size += len;
Milan Broz91e10622007-12-13 14:16:10 +00001052
Mikulas Patocka7145c242015-02-13 08:24:41 -05001053 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
Mikulas Patocka7145c242015-02-13 08:24:41 -05001056return_clone:
Mel Gormand0164ad2015-11-06 16:28:21 -08001057 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001058 mutex_unlock(&cc->bio_alloc_lock);
1059
Milan Broz8b004452006-10-03 01:15:37 -07001060 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
Neil Brown644bd2f2007-10-16 13:48:46 +02001063static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Neil Brown644bd2f2007-10-16 13:48:46 +02001065 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct bio_vec *bv;
1067
Kent Overstreetcb34e052012-09-05 15:22:02 -07001068 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 BUG_ON(!bv->bv_page);
1070 mempool_free(bv->bv_page, cc->page_pool);
1071 bv->bv_page = NULL;
1072 }
1073}
1074
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001075static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1076 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001077{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001078 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001079 io->base_bio = bio;
1080 io->sector = sector;
1081 io->error = 0;
Mikulas Patocka610f2de2014-02-20 18:01:01 -05001082 io->ctx.req = NULL;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001083 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001084}
1085
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001086static void crypt_inc_pending(struct dm_crypt_io *io)
1087{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001088 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001089}
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091/*
1092 * One of the bios was finished. Check for completion of
1093 * the whole request and correctly clean up the buffer.
1094 */
Milan Broz5742fd72008-02-08 02:10:43 +00001095static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001097 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001098 struct bio *base_bio = io->base_bio;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001099 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Mikulas Patocka40b62292012-07-27 15:08:04 +01001101 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 return;
1103
Mikulas Patocka610f2de2014-02-20 18:01:01 -05001104 if (io->ctx.req)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001105 crypt_free_req(cc, io->ctx.req, base_bio);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001106
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001107 base_bio->bi_error = error;
1108 bio_endio(base_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
1111/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001112 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 *
1114 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001115 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001116 *
1117 * kcryptd performs the actual encryption or decryption.
1118 *
1119 * kcryptd_io performs the IO submission.
1120 *
1121 * They must be separated as otherwise the final stages could be
1122 * starved by new requests which can block in the first stages due
1123 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001124 *
1125 * The work is done per CPU global for all dm-crypt instances.
1126 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001128static void crypt_endio(struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001129{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001130 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001131 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001132 unsigned rw = bio_data_dir(clone);
Sasha Levin9b81c842015-08-10 19:05:18 -04001133 int error;
Milan Broz8b004452006-10-03 01:15:37 -07001134
1135 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001136 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001137 */
Milan Brozee7a4912008-02-08 02:10:46 +00001138 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001139 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001140
Sasha Levin9b81c842015-08-10 19:05:18 -04001141 error = clone->bi_error;
Milan Brozee7a4912008-02-08 02:10:46 +00001142 bio_put(clone);
1143
Sasha Levin9b81c842015-08-10 19:05:18 -04001144 if (rw == READ && !error) {
Milan Brozee7a4912008-02-08 02:10:46 +00001145 kcryptd_queue_crypt(io);
1146 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001147 }
Milan Broz8b004452006-10-03 01:15:37 -07001148
Sasha Levin9b81c842015-08-10 19:05:18 -04001149 if (unlikely(error))
1150 io->error = error;
Milan Broz5742fd72008-02-08 02:10:43 +00001151
1152 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001153}
1154
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001155static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001156{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001157 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001158
1159 clone->bi_private = io;
1160 clone->bi_end_io = crypt_endio;
1161 clone->bi_bdev = cc->dev->bdev;
Bart Van Assche4382e332016-09-14 10:45:36 +02001162 bio_set_op_attrs(clone, bio_op(io->base_bio), bio_flags(io->base_bio));
Milan Broz8b004452006-10-03 01:15:37 -07001163}
1164
Milan Broz20c82532011-01-13 19:59:53 +00001165static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001166{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001167 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001168 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001169
Milan Broz8b004452006-10-03 01:15:37 -07001170 /*
Mike Snitzer59779072015-04-09 16:53:24 -04001171 * We need the original biovec array in order to decrypt
1172 * the whole bio data *afterwards* -- thanks to immutable
1173 * biovecs we don't need to worry about the block layer
1174 * modifying the biovec array; so leverage bio_clone_fast().
Milan Broz8b004452006-10-03 01:15:37 -07001175 */
Mike Snitzer59779072015-04-09 16:53:24 -04001176 clone = bio_clone_fast(io->base_bio, gfp, cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001177 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001178 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001179
Milan Broz20c82532011-01-13 19:59:53 +00001180 crypt_inc_pending(io);
1181
Milan Broz8b004452006-10-03 01:15:37 -07001182 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001183 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001184
Milan Broz93e605c2006-10-03 01:15:38 -07001185 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001186 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001187}
1188
Mikulas Patockadc267622015-02-13 08:25:59 -05001189static void kcryptd_io_read_work(struct work_struct *work)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001190{
1191 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1192
Mikulas Patockadc267622015-02-13 08:25:59 -05001193 crypt_inc_pending(io);
1194 if (kcryptd_io_read(io, GFP_NOIO))
1195 io->error = -ENOMEM;
1196 crypt_dec_pending(io);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001197}
1198
Mikulas Patockadc267622015-02-13 08:25:59 -05001199static void kcryptd_queue_read(struct dm_crypt_io *io)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001200{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001201 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001202
Mikulas Patockadc267622015-02-13 08:25:59 -05001203 INIT_WORK(&io->work, kcryptd_io_read_work);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001204 queue_work(cc->io_queue, &io->work);
1205}
1206
Mikulas Patockadc267622015-02-13 08:25:59 -05001207static void kcryptd_io_write(struct dm_crypt_io *io)
1208{
1209 struct bio *clone = io->ctx.bio_out;
1210
1211 generic_make_request(clone);
1212}
1213
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001214#define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1215
Mikulas Patockadc267622015-02-13 08:25:59 -05001216static int dmcrypt_write(void *data)
1217{
1218 struct crypt_config *cc = data;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001219 struct dm_crypt_io *io;
1220
Mikulas Patockadc267622015-02-13 08:25:59 -05001221 while (1) {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001222 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -05001223 struct blk_plug plug;
1224
1225 DECLARE_WAITQUEUE(wait, current);
1226
1227 spin_lock_irq(&cc->write_thread_wait.lock);
1228continue_locked:
1229
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001230 if (!RB_EMPTY_ROOT(&cc->write_tree))
Mikulas Patockadc267622015-02-13 08:25:59 -05001231 goto pop_from_list;
1232
Rabin Vincentf659b102016-09-21 16:22:29 +02001233 set_current_state(TASK_INTERRUPTIBLE);
Mikulas Patockadc267622015-02-13 08:25:59 -05001234 __add_wait_queue(&cc->write_thread_wait, &wait);
1235
1236 spin_unlock_irq(&cc->write_thread_wait.lock);
1237
Rabin Vincentf659b102016-09-21 16:22:29 +02001238 if (unlikely(kthread_should_stop())) {
1239 set_task_state(current, TASK_RUNNING);
1240 remove_wait_queue(&cc->write_thread_wait, &wait);
1241 break;
1242 }
1243
Mikulas Patockadc267622015-02-13 08:25:59 -05001244 schedule();
1245
Rabin Vincentf659b102016-09-21 16:22:29 +02001246 set_task_state(current, TASK_RUNNING);
Mikulas Patockadc267622015-02-13 08:25:59 -05001247 spin_lock_irq(&cc->write_thread_wait.lock);
1248 __remove_wait_queue(&cc->write_thread_wait, &wait);
1249 goto continue_locked;
1250
1251pop_from_list:
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001252 write_tree = cc->write_tree;
1253 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05001254 spin_unlock_irq(&cc->write_thread_wait.lock);
1255
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001256 BUG_ON(rb_parent(write_tree.rb_node));
1257
1258 /*
1259 * Note: we cannot walk the tree here with rb_next because
1260 * the structures may be freed when kcryptd_io_write is called.
1261 */
Mikulas Patockadc267622015-02-13 08:25:59 -05001262 blk_start_plug(&plug);
1263 do {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001264 io = crypt_io_from_node(rb_first(&write_tree));
1265 rb_erase(&io->rb_node, &write_tree);
Mikulas Patockadc267622015-02-13 08:25:59 -05001266 kcryptd_io_write(io);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001267 } while (!RB_EMPTY_ROOT(&write_tree));
Mikulas Patockadc267622015-02-13 08:25:59 -05001268 blk_finish_plug(&plug);
1269 }
1270 return 0;
1271}
1272
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001273static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001274{
Milan Brozdec1ced2008-02-08 02:10:57 +00001275 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001276 struct crypt_config *cc = io->cc;
Mikulas Patockadc267622015-02-13 08:25:59 -05001277 unsigned long flags;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001278 sector_t sector;
1279 struct rb_node **rbp, *parent;
Milan Brozdec1ced2008-02-08 02:10:57 +00001280
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001281 if (unlikely(io->error < 0)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001282 crypt_free_buffer_pages(cc, clone);
1283 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001284 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001285 return;
1286 }
1287
1288 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001289 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001290
Kent Overstreet4f024f32013-10-11 15:44:27 -07001291 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001292
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001293 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1294 generic_make_request(clone);
1295 return;
1296 }
1297
Mikulas Patockadc267622015-02-13 08:25:59 -05001298 spin_lock_irqsave(&cc->write_thread_wait.lock, flags);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001299 rbp = &cc->write_tree.rb_node;
1300 parent = NULL;
1301 sector = io->sector;
1302 while (*rbp) {
1303 parent = *rbp;
1304 if (sector < crypt_io_from_node(parent)->sector)
1305 rbp = &(*rbp)->rb_left;
1306 else
1307 rbp = &(*rbp)->rb_right;
1308 }
1309 rb_link_node(&io->rb_node, parent, rbp);
1310 rb_insert_color(&io->rb_node, &cc->write_tree);
1311
Mikulas Patockadc267622015-02-13 08:25:59 -05001312 wake_up_locked(&cc->write_thread_wait);
1313 spin_unlock_irqrestore(&cc->write_thread_wait.lock, flags);
Milan Broz4e4eef62008-02-08 02:10:49 +00001314}
1315
Milan Brozfc5a5e92008-10-10 13:37:04 +01001316static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001317{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001318 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001319 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001320 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001321 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +00001322 int r;
Milan Broz8b004452006-10-03 01:15:37 -07001323
Milan Broz93e605c2006-10-03 01:15:38 -07001324 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001325 * Prevent io from disappearing until this function completes.
1326 */
1327 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001328 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001329
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001330 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1331 if (unlikely(!clone)) {
1332 io->error = -EIO;
1333 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001334 }
Milan Broz899c95d2008-02-08 02:11:02 +00001335
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001336 io->ctx.bio_out = clone;
1337 io->ctx.iter_out = clone->bi_iter;
1338
1339 sector += bio_sectors(clone);
1340
1341 crypt_inc_pending(io);
1342 r = crypt_convert(cc, &io->ctx);
1343 if (r)
1344 io->error = -EIO;
1345 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1346
1347 /* Encryption was already finished, submit io now */
1348 if (crypt_finished) {
1349 kcryptd_crypt_write_io_submit(io, 0);
1350 io->sector = sector;
1351 }
1352
1353dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001354 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001355}
1356
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001357static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001358{
Milan Broz5742fd72008-02-08 02:10:43 +00001359 crypt_dec_pending(io);
1360}
1361
Milan Broz4e4eef62008-02-08 02:10:49 +00001362static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001363{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001364 struct crypt_config *cc = io->cc;
Milan Broz5742fd72008-02-08 02:10:43 +00001365 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001366
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001367 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001368
Milan Broz53017032008-02-08 02:10:38 +00001369 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001370 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001371
Milan Broz5742fd72008-02-08 02:10:43 +00001372 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001373 if (r < 0)
1374 io->error = -EIO;
Milan Broz5742fd72008-02-08 02:10:43 +00001375
Mikulas Patocka40b62292012-07-27 15:08:04 +01001376 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001377 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001378
1379 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001380}
1381
Milan Broz95497a92008-02-08 02:11:12 +00001382static void kcryptd_async_done(struct crypto_async_request *async_req,
1383 int error)
1384{
Huang Yingb2174ee2009-03-16 17:44:33 +00001385 struct dm_crypt_request *dmreq = async_req->data;
1386 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001387 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001388 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001389
Milan Broz54cea3f2015-05-15 17:00:25 +02001390 /*
1391 * A request from crypto driver backlog is going to be processed now,
1392 * finish the completion and continue in crypt_convert().
1393 * (Callback will be called for the second time for this request.)
1394 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001395 if (error == -EINPROGRESS) {
1396 complete(&ctx->restart);
Milan Broz95497a92008-02-08 02:11:12 +00001397 return;
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001398 }
Milan Broz95497a92008-02-08 02:11:12 +00001399
Milan Broz2dc53272011-01-13 19:59:54 +00001400 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
1401 error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq);
1402
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001403 if (error < 0)
1404 io->error = -EIO;
1405
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001406 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00001407
Mikulas Patocka40b62292012-07-27 15:08:04 +01001408 if (!atomic_dec_and_test(&ctx->cc_pending))
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001409 return;
Milan Broz95497a92008-02-08 02:11:12 +00001410
1411 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001412 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001413 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001414 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001415}
1416
Milan Broz4e4eef62008-02-08 02:10:49 +00001417static void kcryptd_crypt(struct work_struct *work)
1418{
1419 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1420
1421 if (bio_data_dir(io->base_bio) == READ)
1422 kcryptd_crypt_read_convert(io);
1423 else
1424 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001425}
1426
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001427static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1428{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001429 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001430
1431 INIT_WORK(&io->work, kcryptd_crypt);
1432 queue_work(cc->crypt_queue, &io->work);
1433}
1434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435/*
1436 * Decode key from its hex representation
1437 */
1438static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1439{
1440 char buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 unsigned int i;
1442
1443 buffer[2] = '\0';
1444
Milan Broz8b004452006-10-03 01:15:37 -07001445 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 buffer[0] = *hex++;
1447 buffer[1] = *hex++;
1448
majianpeng1a66a082012-07-27 15:07:59 +01001449 if (kstrtou8(buffer, 16, &key[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return -EINVAL;
1451 }
1452
1453 if (*hex != '\0')
1454 return -EINVAL;
1455
1456 return 0;
1457}
1458
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001459static void crypt_free_tfms(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001460{
Milan Brozd1f96422011-01-13 19:59:54 +00001461 unsigned i;
1462
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001463 if (!cc->tfms)
1464 return;
1465
Milan Brozd1f96422011-01-13 19:59:54 +00001466 for (i = 0; i < cc->tfms_count; i++)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001467 if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) {
Herbert Xubbdb23b2016-01-24 21:16:36 +08001468 crypto_free_skcipher(cc->tfms[i]);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001469 cc->tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001470 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001471
1472 kfree(cc->tfms);
1473 cc->tfms = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001474}
1475
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001476static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001477{
Milan Brozd1f96422011-01-13 19:59:54 +00001478 unsigned i;
1479 int err;
1480
Eric Biggers5d0be842016-08-30 09:51:44 -07001481 cc->tfms = kzalloc(cc->tfms_count * sizeof(struct crypto_skcipher *),
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001482 GFP_KERNEL);
1483 if (!cc->tfms)
1484 return -ENOMEM;
1485
Milan Brozd1f96422011-01-13 19:59:54 +00001486 for (i = 0; i < cc->tfms_count; i++) {
Herbert Xubbdb23b2016-01-24 21:16:36 +08001487 cc->tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001488 if (IS_ERR(cc->tfms[i])) {
1489 err = PTR_ERR(cc->tfms[i]);
1490 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001491 return err;
1492 }
1493 }
1494
Eric Biggers1cae07e2018-12-05 20:53:00 -08001495 /*
1496 * dm-crypt performance can vary greatly depending on which crypto
1497 * algorithm implementation is used. Help people debug performance
1498 * problems by logging the ->cra_driver_name.
1499 */
1500 DMINFO("%s using implementation \"%s\"", ciphermode,
1501 crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name);
Milan Brozd1f96422011-01-13 19:59:54 +00001502 return 0;
1503}
1504
Andi Kleenc0297722011-01-13 19:59:53 +00001505static int crypt_setkey_allcpus(struct crypt_config *cc)
1506{
Milan Brozda31a072013-10-28 23:21:03 +01001507 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001508 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001509
Milan Brozda31a072013-10-28 23:21:03 +01001510 /* Ignore extra keys (which are used for IV etc) */
1511 subkey_size = (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1512
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001513 for (i = 0; i < cc->tfms_count; i++) {
Herbert Xubbdb23b2016-01-24 21:16:36 +08001514 r = crypto_skcipher_setkey(cc->tfms[i],
1515 cc->key + (i * subkey_size),
1516 subkey_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001517 if (r)
1518 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001519 }
1520
1521 return err;
1522}
1523
Milan Broze48d4bb2006-10-03 01:15:37 -07001524static int crypt_set_key(struct crypt_config *cc, char *key)
1525{
Milan Brozde8be5a2011-03-24 13:54:27 +00001526 int r = -EINVAL;
1527 int key_string_len = strlen(key);
1528
Milan Broz69a8cfc2011-01-13 19:59:49 +00001529 /* The key size may not be changed. */
Milan Brozde8be5a2011-03-24 13:54:27 +00001530 if (cc->key_size != (key_string_len >> 1))
1531 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001532
Milan Broz69a8cfc2011-01-13 19:59:49 +00001533 /* Hyphen (which gives a key_size of zero) means there is no key. */
1534 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00001535 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001536
Ondrej Kozina2c017f72016-11-02 15:02:08 +01001537 /* clear the flag since following operations may invalidate previously valid key */
1538 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1539
Milan Broz69a8cfc2011-01-13 19:59:49 +00001540 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00001541 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07001542
Milan Brozde8be5a2011-03-24 13:54:27 +00001543 r = crypt_setkey_allcpus(cc);
Ondrej Kozina2c017f72016-11-02 15:02:08 +01001544 if (!r)
1545 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Milan Brozde8be5a2011-03-24 13:54:27 +00001546
1547out:
1548 /* Hex key string not needed after here, so wipe it. */
1549 memset(key, '0', key_string_len);
1550
1551 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07001552}
1553
1554static int crypt_wipe_key(struct crypt_config *cc)
1555{
1556 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
1557 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00001558
1559 return crypt_setkey_allcpus(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07001560}
1561
Milan Broz28513fc2010-08-12 04:14:06 +01001562static void crypt_dtr(struct dm_target *ti)
1563{
1564 struct crypt_config *cc = ti->private;
1565
1566 ti->private = NULL;
1567
1568 if (!cc)
1569 return;
1570
Rabin Vincentf659b102016-09-21 16:22:29 +02001571 if (cc->write_thread)
Mikulas Patockadc267622015-02-13 08:25:59 -05001572 kthread_stop(cc->write_thread);
1573
Milan Broz28513fc2010-08-12 04:14:06 +01001574 if (cc->io_queue)
1575 destroy_workqueue(cc->io_queue);
1576 if (cc->crypt_queue)
1577 destroy_workqueue(cc->crypt_queue);
1578
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001579 crypt_free_tfms(cc);
1580
Milan Broz28513fc2010-08-12 04:14:06 +01001581 if (cc->bs)
1582 bioset_free(cc->bs);
1583
Julia Lawall6f659852015-09-13 14:15:05 +02001584 mempool_destroy(cc->page_pool);
1585 mempool_destroy(cc->req_pool);
Milan Broz28513fc2010-08-12 04:14:06 +01001586
1587 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
1588 cc->iv_gen_ops->dtr(cc);
1589
Milan Broz28513fc2010-08-12 04:14:06 +01001590 if (cc->dev)
1591 dm_put_device(ti, cc->dev);
1592
Milan Broz5ebaee62010-08-12 04:14:07 +01001593 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00001594 kzfree(cc->cipher_string);
Milan Broz28513fc2010-08-12 04:14:06 +01001595
1596 /* Must zero key material before freeing */
1597 kzfree(cc);
1598}
1599
Milan Broz5ebaee62010-08-12 04:14:07 +01001600static int crypt_ctr_cipher(struct dm_target *ti,
1601 char *cipher_in, char *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Milan Broz5ebaee62010-08-12 04:14:07 +01001603 struct crypt_config *cc = ti->private;
Milan Brozd1f96422011-01-13 19:59:54 +00001604 char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01001605 char *cipher_api = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001606 int ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001607 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Milan Broz5ebaee62010-08-12 04:14:07 +01001609 /* Convert to crypto api definition? */
1610 if (strchr(cipher_in, '(')) {
1611 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 return -EINVAL;
1613 }
1614
Milan Broz7dbcd132011-01-13 19:59:52 +00001615 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
1616 if (!cc->cipher_string)
1617 goto bad_mem;
1618
Milan Broz5ebaee62010-08-12 04:14:07 +01001619 /*
1620 * Legacy dm-crypt cipher specification
Milan Brozd1f96422011-01-13 19:59:54 +00001621 * cipher[:keycount]-mode-iv:ivopts
Milan Broz5ebaee62010-08-12 04:14:07 +01001622 */
1623 tmp = cipher_in;
Milan Brozd1f96422011-01-13 19:59:54 +00001624 keycount = strsep(&tmp, "-");
1625 cipher = strsep(&keycount, ":");
1626
1627 if (!keycount)
1628 cc->tfms_count = 1;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001629 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
Milan Brozd1f96422011-01-13 19:59:54 +00001630 !is_power_of_2(cc->tfms_count)) {
1631 ti->error = "Bad cipher key count specification";
1632 return -EINVAL;
1633 }
1634 cc->key_parts = cc->tfms_count;
Milan Brozda31a072013-10-28 23:21:03 +01001635 cc->key_extra_size = 0;
Milan Broz5ebaee62010-08-12 04:14:07 +01001636
1637 cc->cipher = kstrdup(cipher, GFP_KERNEL);
1638 if (!cc->cipher)
1639 goto bad_mem;
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 chainmode = strsep(&tmp, "-");
1642 ivopts = strsep(&tmp, "-");
1643 ivmode = strsep(&ivopts, ":");
1644
1645 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01001646 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Milan Broz7dbcd132011-01-13 19:59:52 +00001648 /*
1649 * For compatibility with the original dm-crypt mapping format, if
1650 * only the cipher name is supplied, use cbc-plain.
1651 */
Milan Broz5ebaee62010-08-12 04:14:07 +01001652 if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 chainmode = "cbc";
1654 ivmode = "plain";
1655 }
1656
Herbert Xud1806f62006-08-22 20:29:17 +10001657 if (strcmp(chainmode, "ecb") && !ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001658 ti->error = "IV mechanism required";
1659 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 }
1661
Milan Broz5ebaee62010-08-12 04:14:07 +01001662 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
1663 if (!cipher_api)
1664 goto bad_mem;
1665
1666 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
1667 "%s(%s)", chainmode, cipher);
1668 if (ret < 0) {
1669 kfree(cipher_api);
1670 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10001671 }
1672
Milan Broz5ebaee62010-08-12 04:14:07 +01001673 /* Allocate cipher */
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001674 ret = crypt_alloc_tfms(cc, cipher_api);
1675 if (ret < 0) {
1676 ti->error = "Error allocating crypto tfm";
1677 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Milan Broz5ebaee62010-08-12 04:14:07 +01001680 /* Initialize IV */
Herbert Xubbdb23b2016-01-24 21:16:36 +08001681 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
Milan Broz5ebaee62010-08-12 04:14:07 +01001682 if (cc->iv_size)
1683 /* at least a 64 bit sector number should fit in our buffer */
1684 cc->iv_size = max(cc->iv_size,
1685 (unsigned int)(sizeof(u64) / sizeof(u8)));
1686 else if (ivmode) {
1687 DMWARN("Selected cipher does not support IVs");
1688 ivmode = NULL;
1689 }
1690
1691 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (ivmode == NULL)
1693 cc->iv_gen_ops = NULL;
1694 else if (strcmp(ivmode, "plain") == 0)
1695 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00001696 else if (strcmp(ivmode, "plain64") == 0)
1697 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 else if (strcmp(ivmode, "essiv") == 0)
1699 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10001700 else if (strcmp(ivmode, "benbi") == 0)
1701 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07001702 else if (strcmp(ivmode, "null") == 0)
1703 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00001704 else if (strcmp(ivmode, "lmk") == 0) {
1705 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01001706 /*
1707 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00001708 * to length of provided multi-key string.
1709 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01001710 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00001711 */
Milan Brozda31a072013-10-28 23:21:03 +01001712 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00001713 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01001714 cc->key_extra_size = cc->key_size / cc->key_parts;
1715 }
Milan Brozed04d982013-10-28 23:21:04 +01001716 } else if (strcmp(ivmode, "tcw") == 0) {
1717 cc->iv_gen_ops = &crypt_iv_tcw_ops;
1718 cc->key_parts += 2; /* IV + whitening */
1719 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broz34745782011-01-13 19:59:55 +00001720 } else {
Milan Broz5ebaee62010-08-12 04:14:07 +01001721 ret = -EINVAL;
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001722 ti->error = "Invalid IV mode";
Milan Broz28513fc2010-08-12 04:14:06 +01001723 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 }
1725
Milan Brozda31a072013-10-28 23:21:03 +01001726 /* Initialize and set key */
1727 ret = crypt_set_key(cc, key);
1728 if (ret < 0) {
1729 ti->error = "Error decoding and setting key";
1730 goto bad;
1731 }
1732
Milan Broz28513fc2010-08-12 04:14:06 +01001733 /* Allocate IV */
1734 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
1735 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
1736 if (ret < 0) {
1737 ti->error = "Error creating IV";
1738 goto bad;
1739 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00001740 }
1741
Milan Broz28513fc2010-08-12 04:14:06 +01001742 /* Initialize IV (set keys for ESSIV etc) */
1743 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
1744 ret = cc->iv_gen_ops->init(cc);
1745 if (ret < 0) {
1746 ti->error = "Error initialising IV";
1747 goto bad;
1748 }
1749 }
1750
Milan Broz5ebaee62010-08-12 04:14:07 +01001751 ret = 0;
1752bad:
1753 kfree(cipher_api);
1754 return ret;
1755
1756bad_mem:
1757 ti->error = "Cannot allocate cipher strings";
1758 return -ENOMEM;
1759}
1760
1761/*
1762 * Construct an encryption mapping:
1763 * <cipher> <key> <iv_offset> <dev_path> <start>
1764 */
1765static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
1766{
1767 struct crypt_config *cc;
Milan Broz772ae5f2011-08-02 12:32:08 +01001768 unsigned int key_size, opt_params;
Milan Broz5ebaee62010-08-12 04:14:07 +01001769 unsigned long long tmpll;
1770 int ret;
Mikulas Patockad49ec522014-08-28 11:09:31 -04001771 size_t iv_size_padding;
Milan Broz772ae5f2011-08-02 12:32:08 +01001772 struct dm_arg_set as;
1773 const char *opt_string;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001774 char dummy;
Milan Broz5ebaee62010-08-12 04:14:07 +01001775
Milan Broz772ae5f2011-08-02 12:32:08 +01001776 static struct dm_arg _args[] = {
Milan Brozc083ebe2017-03-16 15:39:44 +01001777 {0, 5, "Invalid number of feature args"},
Milan Broz772ae5f2011-08-02 12:32:08 +01001778 };
1779
1780 if (argc < 5) {
Milan Broz5ebaee62010-08-12 04:14:07 +01001781 ti->error = "Not enough arguments";
1782 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784
Milan Broz5ebaee62010-08-12 04:14:07 +01001785 key_size = strlen(argv[1]) >> 1;
1786
1787 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
1788 if (!cc) {
1789 ti->error = "Cannot allocate encryption context";
1790 return -ENOMEM;
1791 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00001792 cc->key_size = key_size;
Milan Brozc083ebe2017-03-16 15:39:44 +01001793 cc->sector_size = (1 << SECTOR_SHIFT);
Mikulas Patocka52ad0c42017-03-23 10:23:14 -04001794 cc->sector_shift = 0;
Milan Broz5ebaee62010-08-12 04:14:07 +01001795
1796 ti->private = cc;
1797 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
1798 if (ret < 0)
1799 goto bad;
1800
Herbert Xubbdb23b2016-01-24 21:16:36 +08001801 cc->dmreq_start = sizeof(struct skcipher_request);
1802 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
Mikulas Patockad49ec522014-08-28 11:09:31 -04001803 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
1804
Herbert Xubbdb23b2016-01-24 21:16:36 +08001805 if (crypto_skcipher_alignmask(any_tfm(cc)) < CRYPTO_MINALIGN) {
Mikulas Patockad49ec522014-08-28 11:09:31 -04001806 /* Allocate the padding exactly */
1807 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
Herbert Xubbdb23b2016-01-24 21:16:36 +08001808 & crypto_skcipher_alignmask(any_tfm(cc));
Mikulas Patockad49ec522014-08-28 11:09:31 -04001809 } else {
1810 /*
1811 * If the cipher requires greater alignment than kmalloc
1812 * alignment, we don't know the exact position of the
1813 * initialization vector. We must assume worst case.
1814 */
Herbert Xubbdb23b2016-01-24 21:16:36 +08001815 iv_size_padding = crypto_skcipher_alignmask(any_tfm(cc));
Mikulas Patockad49ec522014-08-28 11:09:31 -04001816 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001817
Mikulas Patocka94f5e022015-02-13 08:25:26 -05001818 ret = -ENOMEM;
Milan Brozddd42ed2008-02-08 02:11:07 +00001819 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start +
Mikulas Patockad49ec522014-08-28 11:09:31 -04001820 sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size);
Milan Brozddd42ed2008-02-08 02:11:07 +00001821 if (!cc->req_pool) {
1822 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001823 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00001824 }
Milan Brozddd42ed2008-02-08 02:11:07 +00001825
Mike Snitzer30187e12016-01-31 13:28:26 -05001826 cc->per_bio_data_size = ti->per_io_data_size =
Mikulas Patockad49ec522014-08-28 11:09:31 -04001827 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start +
1828 sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size,
1829 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001830
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001831 cc->page_pool = mempool_create_page_pool(BIO_MAX_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001833 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01001834 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
1836
Jens Axboebb799ca2008-12-10 15:35:05 +01001837 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07001838 if (!cc->bs) {
1839 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01001840 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07001841 }
1842
Mikulas Patocka7145c242015-02-13 08:24:41 -05001843 mutex_init(&cc->bio_alloc_lock);
1844
Milan Broz28513fc2010-08-12 04:14:06 +01001845 ret = -EINVAL;
Milan Brozc083ebe2017-03-16 15:39:44 +01001846 if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
1847 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001848 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001849 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001851 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Vivek Goyale80d1c82015-07-31 09:20:36 -04001853 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
1854 if (ret) {
Milan Broz28513fc2010-08-12 04:14:06 +01001855 ti->error = "Device lookup failed";
1856 goto bad;
1857 }
1858
Vivek Goyale80d1c82015-07-31 09:20:36 -04001859 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001860 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001861 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01001862 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08001864 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Milan Broz772ae5f2011-08-02 12:32:08 +01001866 argv += 5;
1867 argc -= 5;
1868
1869 /* Optional parameters */
1870 if (argc) {
1871 as.argc = argc;
1872 as.argv = argv;
1873
1874 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
1875 if (ret)
1876 goto bad;
1877
Wei Yongjun44c144f2015-04-16 22:00:50 -04001878 ret = -EINVAL;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001879 while (opt_params--) {
1880 opt_string = dm_shift_arg(&as);
1881 if (!opt_string) {
1882 ti->error = "Not enough feature arguments";
1883 goto bad;
1884 }
Milan Broz772ae5f2011-08-02 12:32:08 +01001885
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001886 if (!strcasecmp(opt_string, "allow_discards"))
1887 ti->num_discard_bios = 1;
1888
1889 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
1890 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
1891
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001892 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
1893 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
1894
Michael Halcrow9832c012017-10-11 16:19:57 -07001895 else if (!strcasecmp(opt_string, "allow_encrypt_override"))
1896 set_bit(DM_CRYPT_ENCRYPT_OVERRIDE, &cc->flags);
1897
Mikulas Patocka52ad0c42017-03-23 10:23:14 -04001898 else if (sscanf(opt_string, "sector_size:%hu%c",
Milan Brozc083ebe2017-03-16 15:39:44 +01001899 &cc->sector_size, &dummy) == 1) {
1900 if (cc->sector_size < (1 << SECTOR_SHIFT) ||
1901 cc->sector_size > 4096 ||
Mikulas Patocka52ad0c42017-03-23 10:23:14 -04001902 (cc->sector_size & (cc->sector_size - 1))) {
Milan Brozc083ebe2017-03-16 15:39:44 +01001903 ti->error = "Invalid feature value for sector_size";
1904 goto bad;
1905 }
Milan Broz87e52a42017-09-13 15:45:56 +02001906 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) {
1907 ti->error = "Device size is not multiple of sector_size feature";
1908 goto bad;
1909 }
Mikulas Patocka52ad0c42017-03-23 10:23:14 -04001910 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT;
Milan Brozc083ebe2017-03-16 15:39:44 +01001911 } else if (!strcasecmp(opt_string, "iv_large_sectors"))
1912 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
jianzhoub62aeea2019-01-16 16:11:43 +08001913
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001914 else {
1915 ti->error = "Invalid feature arguments";
1916 goto bad;
1917 }
Milan Broz772ae5f2011-08-02 12:32:08 +01001918 }
1919 }
1920
Milan Broz28513fc2010-08-12 04:14:06 +01001921 ret = -ENOMEM;
Tim Murraye97c4ed2016-01-19 16:33:27 -08001922 cc->io_queue = alloc_workqueue("kcryptd_io",
1923 WQ_HIGHPRI |
1924 WQ_MEM_RECLAIM,
1925 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01001926 if (!cc->io_queue) {
1927 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001928 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01001929 }
1930
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001931 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
Tim Murraye97c4ed2016-01-19 16:33:27 -08001932 cc->crypt_queue = alloc_workqueue("kcryptd",
1933 WQ_HIGHPRI |
1934 WQ_MEM_RECLAIM, 1);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001935 else
Tim Murraye97c4ed2016-01-19 16:33:27 -08001936 cc->crypt_queue = alloc_workqueue("kcryptd",
1937 WQ_HIGHPRI |
1938 WQ_MEM_RECLAIM |
1939 WQ_UNBOUND,
Mikulas Patockaf3396c582015-02-13 08:23:09 -05001940 num_online_cpus());
Milan Brozcabf08e2007-10-19 22:38:58 +01001941 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01001942 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01001943 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01001944 }
1945
Mikulas Patockadc267622015-02-13 08:25:59 -05001946 init_waitqueue_head(&cc->write_thread_wait);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001947 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05001948
1949 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write");
1950 if (IS_ERR(cc->write_thread)) {
1951 ret = PTR_ERR(cc->write_thread);
1952 cc->write_thread = NULL;
1953 ti->error = "Couldn't spawn write thread";
1954 goto bad;
1955 }
1956 wake_up_process(cc->write_thread);
1957
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00001958 ti->num_flush_bios = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01001959 ti->discard_zeroes_data_unsupported = true;
Milan Broz983c7db2011-09-25 23:26:21 +01001960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 return 0;
1962
Milan Broz28513fc2010-08-12 04:14:06 +01001963bad:
1964 crypt_dtr(ti);
1965 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00001968static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001970 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001971 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001972
Milan Broz772ae5f2011-08-02 12:32:08 +01001973 /*
Michael Halcrow9832c012017-10-11 16:19:57 -07001974 * If bio is REQ_PREFLUSH, REQ_NOENCRYPT, or REQ_OP_DISCARD,
1975 * just bypass crypt queues.
Mike Christie28a8f0d2016-06-05 14:32:25 -05001976 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
Mike Christiee6047142016-06-05 14:32:04 -05001977 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
Milan Broz772ae5f2011-08-02 12:32:08 +01001978 */
Michael Halcrow9832c012017-10-11 16:19:57 -07001979 if (unlikely(bio->bi_opf & REQ_PREFLUSH) ||
1980 (unlikely(bio->bi_opf & REQ_NOENCRYPT) &&
1981 test_bit(DM_CRYPT_ENCRYPT_OVERRIDE, &cc->flags)) ||
1982 bio_op(bio) == REQ_OP_DISCARD) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001983 bio->bi_bdev = cc->dev->bdev;
Milan Broz772ae5f2011-08-02 12:32:08 +01001984 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001985 bio->bi_iter.bi_sector = cc->start +
1986 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01001987 return DM_MAPIO_REMAPPED;
1988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Mikulas Patocka4e870e92016-08-30 16:38:42 -04001990 /*
1991 * Check if bio is too large, split as needed.
1992 */
1993 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
1994 bio_data_dir(bio) == WRITE)
1995 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
1996
Milan Brozc083ebe2017-03-16 15:39:44 +01001997 /*
1998 * Ensure that bio is a multiple of internal sector encryption size
1999 * and is aligned to this size as defined in IO hints.
2000 */
2001 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
2002 return -EIO;
2003
2004 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
2005 return -EIO;
2006
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002007 io = dm_per_bio_data(bio, cc->per_bio_data_size);
2008 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Herbert Xubbdb23b2016-01-24 21:16:36 +08002009 io->ctx.req = (struct skcipher_request *)(io + 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01002010
Milan Broz20c82532011-01-13 19:59:53 +00002011 if (bio_data_dir(io->base_bio) == READ) {
2012 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05002013 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00002014 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01002015 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002017 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
2019
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002020static void crypt_status(struct dm_target *ti, status_type_t type,
2021 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Milan Broz5ebaee62010-08-12 04:14:07 +01002023 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002024 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002025 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027 switch (type) {
2028 case STATUSTYPE_INFO:
2029 result[0] = '\0';
2030 break;
2031
2032 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00002033 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002035 if (cc->key_size > 0)
2036 for (i = 0; i < cc->key_size; i++)
2037 DMEMIT("%02x", cc->key[i]);
2038 else
2039 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Andrew Morton4ee218c2006-03-27 01:17:48 -08002041 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
2042 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01002043
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002044 num_feature_args += !!ti->num_discard_bios;
2045 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002046 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Michael Halcrow9832c012017-10-11 16:19:57 -07002047 num_feature_args += test_bit(DM_CRYPT_ENCRYPT_OVERRIDE, &cc->flags);
Mikulas Patocka52ad0c42017-03-23 10:23:14 -04002048 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT);
Milan Brozc083ebe2017-03-16 15:39:44 +01002049 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002050 if (num_feature_args) {
2051 DMEMIT(" %d", num_feature_args);
2052 if (ti->num_discard_bios)
2053 DMEMIT(" allow_discards");
2054 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2055 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002056 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
2057 DMEMIT(" submit_from_crypt_cpus");
Michael Halcrow9832c012017-10-11 16:19:57 -07002058 if (test_bit(DM_CRYPT_ENCRYPT_OVERRIDE, &cc->flags))
2059 DMEMIT(" allow_encrypt_override");
Milan Brozc083ebe2017-03-16 15:39:44 +01002060 if (cc->sector_size != (1 << SECTOR_SHIFT))
2061 DMEMIT(" sector_size:%d", cc->sector_size);
2062 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
2063 DMEMIT(" iv_large_sectors");
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002064 }
Milan Broz772ae5f2011-08-02 12:32:08 +01002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 break;
2067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
Milan Broze48d4bb2006-10-03 01:15:37 -07002070static void crypt_postsuspend(struct dm_target *ti)
2071{
2072 struct crypt_config *cc = ti->private;
2073
2074 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2075}
2076
2077static int crypt_preresume(struct dm_target *ti)
2078{
2079 struct crypt_config *cc = ti->private;
2080
2081 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
2082 DMERR("aborting resume - crypt key is not set.");
2083 return -EAGAIN;
2084 }
2085
2086 return 0;
2087}
2088
2089static void crypt_resume(struct dm_target *ti)
2090{
2091 struct crypt_config *cc = ti->private;
2092
2093 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2094}
2095
2096/* Message interface
2097 * key set <key>
2098 * key wipe
2099 */
2100static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
2101{
2102 struct crypt_config *cc = ti->private;
Milan Broz542da312009-12-10 23:51:57 +00002103 int ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07002104
2105 if (argc < 2)
2106 goto error;
2107
Mike Snitzer498f0102011-08-02 12:32:04 +01002108 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07002109 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
2110 DMWARN("not suspended during key manipulation.");
2111 return -EINVAL;
2112 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002113 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Milan Broz542da312009-12-10 23:51:57 +00002114 ret = crypt_set_key(cc, argv[2]);
2115 if (ret)
2116 return ret;
2117 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
2118 ret = cc->iv_gen_ops->init(cc);
2119 return ret;
2120 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002121 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00002122 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
2123 ret = cc->iv_gen_ops->wipe(cc);
2124 if (ret)
2125 return ret;
2126 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002127 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00002128 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002129 }
2130
2131error:
2132 DMWARN("unrecognised message received.");
2133 return -EINVAL;
2134}
2135
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002136static int crypt_iterate_devices(struct dm_target *ti,
2137 iterate_devices_callout_fn fn, void *data)
2138{
2139 struct crypt_config *cc = ti->private;
2140
Mike Snitzer5dea2712009-07-23 20:30:42 +01002141 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002142}
2143
Mike Snitzer586b2862015-09-09 21:34:51 -04002144static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
2145{
Milan Brozc083ebe2017-03-16 15:39:44 +01002146 struct crypt_config *cc = ti->private;
2147
Mike Snitzer586b2862015-09-09 21:34:51 -04002148 /*
2149 * Unfortunate constraint that is required to avoid the potential
2150 * for exceeding underlying device's max_segments limits -- due to
2151 * crypt_alloc_buffer() possibly allocating pages for the encryption
2152 * bio that are not as physically contiguous as the original bio.
2153 */
2154 limits->max_segment_size = PAGE_SIZE;
Milan Brozc083ebe2017-03-16 15:39:44 +01002155
Mikulas Patockaf3e3edc2018-08-10 11:23:56 -04002156 limits->logical_block_size =
2157 max_t(unsigned short, limits->logical_block_size, cc->sector_size);
2158 limits->physical_block_size =
2159 max_t(unsigned, limits->physical_block_size, cc->sector_size);
2160 limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size);
Mike Snitzer586b2862015-09-09 21:34:51 -04002161}
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163static struct target_type crypt_target = {
2164 .name = "crypt",
Milan Brozc083ebe2017-03-16 15:39:44 +01002165 .version = {1, 17, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 .module = THIS_MODULE,
2167 .ctr = crypt_ctr,
2168 .dtr = crypt_dtr,
2169 .map = crypt_map,
2170 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07002171 .postsuspend = crypt_postsuspend,
2172 .preresume = crypt_preresume,
2173 .resume = crypt_resume,
2174 .message = crypt_message,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002175 .iterate_devices = crypt_iterate_devices,
Mike Snitzer586b2862015-09-09 21:34:51 -04002176 .io_hints = crypt_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177};
2178
2179static int __init dm_crypt_init(void)
2180{
2181 int r;
2182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05002184 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002185 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return r;
2188}
2189
2190static void __exit dm_crypt_exit(void)
2191{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002192 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
2195module_init(dm_crypt_init);
2196module_exit(dm_crypt_exit);
2197
Jana Saoutbf142992014-06-24 14:27:04 -04002198MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
2200MODULE_LICENSE("GPL");