blob: 4939fbc34ff294d2b4bc6bc650306b2ae7837f63 [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 Brozef43aa32017-01-04 20:23:54 +01004 * Copyright (C) 2006-2017 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2013-2017 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>
Ondrej Kozinac538f6e2016-11-21 15:58:51 +010015#include <linux/key.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/bio.h>
17#include <linux/blkdev.h>
18#include <linux/mempool.h>
19#include <linux/slab.h>
20#include <linux/crypto.h>
21#include <linux/workqueue.h>
Mikulas Patockadc267622015-02-13 08:25:59 -050022#include <linux/kthread.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070023#include <linux/backing-dev.h>
Arun Sharma600634972011-07-26 16:09:06 -070024#include <linux/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100025#include <linux/scatterlist.h>
Mikulas Patockab3c5fd32015-02-13 08:27:41 -050026#include <linux/rbtree.h>
Ondrej Kozina027c4312016-12-01 18:20:52 +010027#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100029#include <asm/unaligned.h>
Milan Broz34745782011-01-13 19:59:55 +000030#include <crypto/hash.h>
31#include <crypto/md5.h>
32#include <crypto/algapi.h>
Herbert Xubbdb23b2016-01-24 21:16:36 +080033#include <crypto/skcipher.h>
Milan Brozef43aa32017-01-04 20:23:54 +010034#include <crypto/aead.h>
35#include <crypto/authenc.h>
36#include <linux/rtnetlink.h> /* for struct rtattr and RTA macros only */
Ondrej Kozinac538f6e2016-11-21 15:58:51 +010037#include <keys/user-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Mikulas Patocka586e80e2008-10-21 17:44:59 +010039#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Alasdair G Kergon72d94862006-06-26 00:27:35 -070041#define DM_MSG_PREFIX "crypt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * context holding the current state of a multi-part conversion
45 */
46struct convert_context {
Milan Broz43d69032008-02-08 02:11:09 +000047 struct completion restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct bio *bio_in;
49 struct bio *bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -070050 struct bvec_iter iter_in;
51 struct bvec_iter iter_out;
Mikulas Patockac66029f2012-07-27 15:08:05 +010052 sector_t cc_sector;
Mikulas Patocka40b62292012-07-27 15:08:04 +010053 atomic_t cc_pending;
Milan Brozef43aa32017-01-04 20:23:54 +010054 union {
55 struct skcipher_request *req;
56 struct aead_request *req_aead;
57 } r;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Milan Broz53017032008-02-08 02:10:38 +000061/*
62 * per bio private data
63 */
64struct dm_crypt_io {
Alasdair G Kergon49a8a922012-07-27 15:08:05 +010065 struct crypt_config *cc;
Milan Broz53017032008-02-08 02:10:38 +000066 struct bio *base_bio;
Milan Brozef43aa32017-01-04 20:23:54 +010067 u8 *integrity_metadata;
68 bool integrity_metadata_from_pool;
Milan Broz53017032008-02-08 02:10:38 +000069 struct work_struct work;
70
71 struct convert_context ctx;
72
Mikulas Patocka40b62292012-07-27 15:08:04 +010073 atomic_t io_pending;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020074 blk_status_t error;
Milan Broz0c395b02008-02-08 02:10:54 +000075 sector_t sector;
Mikulas Patockadc267622015-02-13 08:25:59 -050076
Mikulas Patockab3c5fd32015-02-13 08:27:41 -050077 struct rb_node rb_node;
Mikulas Patocka298a9fa2014-03-28 15:51:55 -040078} CRYPTO_MINALIGN_ATTR;
Milan Broz53017032008-02-08 02:10:38 +000079
Milan Broz01482b72008-02-08 02:11:04 +000080struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000081 struct convert_context *ctx;
Milan Brozef43aa32017-01-04 20:23:54 +010082 struct scatterlist sg_in[4];
83 struct scatterlist sg_out[4];
Milan Broz2dc53272011-01-13 19:59:54 +000084 sector_t iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000085};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct crypt_config;
88
89struct crypt_iv_operations {
90 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010091 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000093 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000094 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc53272011-01-13 19:59:54 +000095 int (*generator)(struct crypt_config *cc, u8 *iv,
96 struct dm_crypt_request *dmreq);
97 int (*post)(struct crypt_config *cc, u8 *iv,
98 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Milan Broz60473592009-12-10 23:51:55 +0000101struct iv_essiv_private {
Herbert Xubbdb23b2016-01-24 21:16:36 +0800102 struct crypto_ahash *hash_tfm;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000103 u8 *salt;
Milan Broz60473592009-12-10 23:51:55 +0000104};
105
106struct iv_benbi_private {
107 int shift;
108};
109
Milan Broz34745782011-01-13 19:59:55 +0000110#define LMK_SEED_SIZE 64 /* hash + 0 */
111struct iv_lmk_private {
112 struct crypto_shash *hash_tfm;
113 u8 *seed;
114};
115
Milan Brozed04d982013-10-28 23:21:04 +0100116#define TCW_WHITENING_SIZE 16
117struct iv_tcw_private {
118 struct crypto_shash *crc32_tfm;
119 u8 *iv_seed;
120 u8 *whitening;
121};
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/*
124 * Crypt: maps a linear range of a block device
125 * and encrypts / decrypts at the same time.
126 */
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -0500127enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID,
Rabin Vincentf659b102016-09-21 16:22:29 +0200128 DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD };
Andi Kleenc0297722011-01-13 19:59:53 +0000129
Milan Brozef43aa32017-01-04 20:23:54 +0100130enum cipher_flags {
131 CRYPT_MODE_INTEGRITY_AEAD, /* Use authenticated mode for cihper */
Milan Broz8f0009a2017-03-16 15:39:44 +0100132 CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */
Milan Brozef43aa32017-01-04 20:23:54 +0100133};
134
Andi Kleenc0297722011-01-13 19:59:53 +0000135/*
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500136 * The fields in here must be read only after initialization.
Andi Kleenc0297722011-01-13 19:59:53 +0000137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138struct crypt_config {
139 struct dm_dev *dev;
140 sector_t start;
141
Mikulas Patocka50593532017-08-13 22:45:08 -0400142 struct percpu_counter n_allocated_pages;
143
Milan Brozcabf08e2007-10-19 22:38:58 +0100144 struct workqueue_struct *io_queue;
145 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700146
Mikulas Patockadc267622015-02-13 08:25:59 -0500147 wait_queue_head_t write_thread_wait;
Mike Snitzer72d711c2018-05-22 18:26:20 -0400148 struct task_struct *write_thread;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -0500149 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -0500150
Milan Broz5ebaee62010-08-12 04:14:07 +0100151 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000152 char *cipher_string;
Milan Brozef43aa32017-01-04 20:23:54 +0100153 char *cipher_auth;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +0100154 char *key_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100155
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100156 const struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800157 union {
Milan Broz60473592009-12-10 23:51:55 +0000158 struct iv_essiv_private essiv;
159 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000160 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100161 struct iv_tcw_private tcw;
Herbert Xu79066ad2006-12-05 13:41:52 -0800162 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 sector_t iv_offset;
164 unsigned int iv_size;
Mikulas Patockaff3af922017-03-23 10:23:14 -0400165 unsigned short int sector_size;
166 unsigned char sector_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100168 /* ESSIV: struct crypto_cipher *essiv_tfm */
169 void *iv_private;
Milan Brozef43aa32017-01-04 20:23:54 +0100170 union {
171 struct crypto_skcipher **tfms;
172 struct crypto_aead **tfms_aead;
173 } cipher_tfm;
Milan Brozd1f96422011-01-13 19:59:54 +0000174 unsigned tfms_count;
Milan Brozef43aa32017-01-04 20:23:54 +0100175 unsigned long cipher_flags;
Andi Kleenc0297722011-01-13 19:59:53 +0000176
177 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000178 * Layout of each crypto request:
179 *
Herbert Xubbdb23b2016-01-24 21:16:36 +0800180 * struct skcipher_request
Milan Brozddd42ed2008-02-08 02:11:07 +0000181 * context
182 * padding
183 * struct dm_crypt_request
184 * padding
185 * IV
186 *
187 * The padding is added so that dm_crypt_request and the IV are
188 * correctly aligned.
189 */
190 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000191
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400192 unsigned int per_bio_data_size;
193
Milan Broze48d4bb2006-10-03 01:15:37 -0700194 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100196 unsigned int key_parts; /* independent parts in key buffer */
197 unsigned int key_extra_size; /* additional keys length */
Milan Brozef43aa32017-01-04 20:23:54 +0100198 unsigned int key_mac_size; /* MAC key size for authenc(...) */
199
200 unsigned int integrity_tag_size;
201 unsigned int integrity_iv_size;
202 unsigned int on_disk_tag_size;
203
Mike Snitzer72d711c2018-05-22 18:26:20 -0400204 /*
205 * pool for per bio private data, crypto requests,
206 * encryption requeusts/buffer pages and integrity tags
207 */
208 unsigned tag_pool_max_sectors;
209 mempool_t tag_pool;
210 mempool_t req_pool;
211 mempool_t page_pool;
212
213 struct bio_set bs;
214 struct mutex bio_alloc_lock;
215
Milan Brozef43aa32017-01-04 20:23:54 +0100216 u8 *authenc_key; /* space for keys in authenc() format (if used) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 u8 key[0];
218};
219
Milan Brozef43aa32017-01-04 20:23:54 +0100220#define MIN_IOS 64
221#define MAX_TAG_SIZE 480
222#define POOL_ENTRY_SIZE 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Mikulas Patocka50593532017-08-13 22:45:08 -0400224static DEFINE_SPINLOCK(dm_crypt_clients_lock);
225static unsigned dm_crypt_clients_n = 0;
226static volatile unsigned long dm_crypt_pages_per_client;
227#define DM_CRYPT_MEMORY_PERCENT 2
228#define DM_CRYPT_MIN_PAGES_PER_CLIENT (BIO_MAX_PAGES * 16)
229
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100230static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000231static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Brozef43aa32017-01-04 20:23:54 +0100232static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
233 struct scatterlist *sg);
Olaf Kirch027581f2007-05-09 02:32:52 -0700234
Andi Kleenc0297722011-01-13 19:59:53 +0000235/*
Eric Biggers86f917a2017-03-30 22:18:48 -0700236 * Use this to access cipher attributes that are independent of the key.
Andi Kleenc0297722011-01-13 19:59:53 +0000237 */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800238static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +0000239{
Milan Brozef43aa32017-01-04 20:23:54 +0100240 return cc->cipher_tfm.tfms[0];
241}
242
243static struct crypto_aead *any_tfm_aead(struct crypt_config *cc)
244{
245 return cc->cipher_tfm.tfms_aead[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 * Different IV generation algorithms:
250 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000251 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200252 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *
Milan Broz61afef62009-12-10 23:52:25 +0000254 * plain64: the initial vector is the 64-bit little-endian version of the sector
255 * number, padded with zeros if necessary.
256 *
Milan Broz7e3fd852017-06-06 09:07:01 +0200257 * plain64be: the initial vector is the 64-bit big-endian version of the sector
258 * number, padded with zeros if necessary.
259 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000260 * essiv: "encrypted sector|salt initial vector", the sector number is
261 * encrypted with the bulk cipher using a salt as key. The salt
262 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *
Rik Snel48527fa2006-09-03 08:56:39 +1000264 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
265 * (needed for LRW-32-AES and possible other narrow block modes)
266 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700267 * null: the initial vector is always zero. Provides compatibility with
268 * obsolete loop_fish2 devices. Do not use for new devices.
269 *
Milan Broz34745782011-01-13 19:59:55 +0000270 * lmk: Compatible implementation of the block chaining mode used
271 * by the Loop-AES block device encryption system
272 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
273 * It operates on full 512 byte sectors and uses CBC
274 * with an IV derived from the sector number, the data and
275 * optionally extra IV seed.
276 * This means that after decryption the first block
277 * of sector must be tweaked according to decrypted data.
278 * Loop-AES can use three encryption schemes:
279 * version 1: is plain aes-cbc mode
280 * version 2: uses 64 multikey scheme with lmk IV generator
281 * version 3: the same as version 2 with additional IV seed
282 * (it uses 65 keys, last key is used as IV seed)
283 *
Milan Brozed04d982013-10-28 23:21:04 +0100284 * tcw: Compatible implementation of the block chaining mode used
285 * by the TrueCrypt device encryption system (prior to version 4.1).
Milan Broze44f23b2015-04-05 18:03:10 +0200286 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
Milan Brozed04d982013-10-28 23:21:04 +0100287 * It operates on full 512 byte sectors and uses CBC
288 * with an IV derived from initial key and the sector number.
289 * In addition, whitening value is applied on every sector, whitening
290 * is calculated from initial key, sector number and mixed using CRC32.
291 * Note that this encryption scheme is vulnerable to watermarking attacks
292 * and should be used for old compatible containers access only.
293 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * plumb: unimplemented, see:
295 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
296 */
297
Milan Broz2dc53272011-01-13 19:59:54 +0000298static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
299 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100302 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 return 0;
305}
306
Milan Broz61afef62009-12-10 23:52:25 +0000307static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000308 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000309{
310 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100311 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000312
313 return 0;
314}
315
Milan Broz7e3fd852017-06-06 09:07:01 +0200316static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv,
317 struct dm_crypt_request *dmreq)
318{
319 memset(iv, 0, cc->iv_size);
320 /* iv_size is at least of size u64; usually it is 16 bytes */
321 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector);
322
323 return 0;
324}
325
Milan Brozb95bf2d2009-12-10 23:51:56 +0000326/* Initialise ESSIV - compute salt but no local memory allocations */
327static int crypt_iv_essiv_init(struct crypt_config *cc)
328{
329 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800330 AHASH_REQUEST_ON_STACK(req, essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000331 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000332 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100333 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000334
335 sg_init_one(&sg, cc->key, cc->key_size);
Herbert Xubbdb23b2016-01-24 21:16:36 +0800336 ahash_request_set_tfm(req, essiv->hash_tfm);
337 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
338 ahash_request_set_crypt(req, &sg, essiv->salt, cc->key_size);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000339
Herbert Xubbdb23b2016-01-24 21:16:36 +0800340 err = crypto_ahash_digest(req);
341 ahash_request_zero(req);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000342 if (err)
343 return err;
344
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100345 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000346
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100347 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800348 crypto_ahash_digestsize(essiv->hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100349 if (err)
350 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000351
352 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000353}
354
Milan Broz542da312009-12-10 23:51:57 +0000355/* Wipe salt and reset key derived from volume key */
356static int crypt_iv_essiv_wipe(struct crypt_config *cc)
357{
358 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800359 unsigned salt_size = crypto_ahash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000360 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100361 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000362
363 memset(essiv->salt, 0, salt_size);
364
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100365 essiv_tfm = cc->iv_private;
366 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
367 if (r)
368 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000369
370 return err;
371}
372
Eric Biggers86f917a2017-03-30 22:18:48 -0700373/* Allocate the cipher for ESSIV */
374static struct crypto_cipher *alloc_essiv_cipher(struct crypt_config *cc,
375 struct dm_target *ti,
376 const u8 *salt,
377 unsigned int saltsize)
Andi Kleenc0297722011-01-13 19:59:53 +0000378{
379 struct crypto_cipher *essiv_tfm;
380 int err;
381
382 /* Setup the essiv_tfm with the given salt */
383 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
384 if (IS_ERR(essiv_tfm)) {
385 ti->error = "Error allocating crypto tfm for ESSIV";
386 return essiv_tfm;
387 }
388
Milan Brozef43aa32017-01-04 20:23:54 +0100389 if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) {
Andi Kleenc0297722011-01-13 19:59:53 +0000390 ti->error = "Block size of ESSIV cipher does "
391 "not match IV size of block cipher";
392 crypto_free_cipher(essiv_tfm);
393 return ERR_PTR(-EINVAL);
394 }
395
396 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
397 if (err) {
398 ti->error = "Failed to set key for ESSIV cipher";
399 crypto_free_cipher(essiv_tfm);
400 return ERR_PTR(err);
401 }
402
403 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000404}
405
Milan Broz60473592009-12-10 23:51:55 +0000406static void crypt_iv_essiv_dtr(struct crypt_config *cc)
407{
Andi Kleenc0297722011-01-13 19:59:53 +0000408 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000409 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
410
Herbert Xubbdb23b2016-01-24 21:16:36 +0800411 crypto_free_ahash(essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000412 essiv->hash_tfm = NULL;
413
414 kzfree(essiv->salt);
415 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000416
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100417 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000418
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100419 if (essiv_tfm)
420 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000421
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100422 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000423}
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100426 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Milan Broz5861f1b2009-12-10 23:51:56 +0000428 struct crypto_cipher *essiv_tfm = NULL;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800429 struct crypto_ahash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000430 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100431 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Milan Broz5861f1b2009-12-10 23:51:56 +0000433 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700434 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return -EINVAL;
436 }
437
Milan Brozb95bf2d2009-12-10 23:51:56 +0000438 /* Allocate hash algorithm */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800439 hash_tfm = crypto_alloc_ahash(opts, 0, CRYPTO_ALG_ASYNC);
Herbert Xu35058682006-08-24 19:10:20 +1000440 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700441 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000442 err = PTR_ERR(hash_tfm);
443 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
Herbert Xubbdb23b2016-01-24 21:16:36 +0800446 salt = kzalloc(crypto_ahash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000447 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700448 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000449 err = -ENOMEM;
450 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452
Milan Brozb95bf2d2009-12-10 23:51:56 +0000453 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000454 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
455
Eric Biggers86f917a2017-03-30 22:18:48 -0700456 essiv_tfm = alloc_essiv_cipher(cc, ti, salt,
457 crypto_ahash_digestsize(hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100458 if (IS_ERR(essiv_tfm)) {
459 crypt_iv_essiv_dtr(cc);
460 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000461 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100462 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000465
466bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000467 if (hash_tfm && !IS_ERR(hash_tfm))
Herbert Xubbdb23b2016-01-24 21:16:36 +0800468 crypto_free_ahash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000469 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000470 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471}
472
Milan Broz2dc53272011-01-13 19:59:54 +0000473static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
474 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100476 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100479 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000480 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 0;
483}
484
Rik Snel48527fa2006-09-03 08:56:39 +1000485static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
486 const char *opts)
487{
Herbert Xubbdb23b2016-01-24 21:16:36 +0800488 unsigned bs = crypto_skcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800489 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000490
491 /* we need to calculate how far we must shift the sector count
492 * to get the cipher block count, we use this shift in _gen */
493
494 if (1 << log != bs) {
495 ti->error = "cypher blocksize is not a power of 2";
496 return -EINVAL;
497 }
498
499 if (log > 9) {
500 ti->error = "cypher blocksize is > 512";
501 return -EINVAL;
502 }
503
Milan Broz60473592009-12-10 23:51:55 +0000504 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000505
506 return 0;
507}
508
509static void crypt_iv_benbi_dtr(struct crypt_config *cc)
510{
Rik Snel48527fa2006-09-03 08:56:39 +1000511}
512
Milan Broz2dc53272011-01-13 19:59:54 +0000513static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
514 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000515{
Herbert Xu79066ad2006-12-05 13:41:52 -0800516 __be64 val;
517
Rik Snel48527fa2006-09-03 08:56:39 +1000518 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800519
Milan Broz2dc53272011-01-13 19:59:54 +0000520 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800521 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return 0;
524}
525
Milan Broz2dc53272011-01-13 19:59:54 +0000526static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
527 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700528{
529 memset(iv, 0, cc->iv_size);
530
531 return 0;
532}
533
Milan Broz34745782011-01-13 19:59:55 +0000534static void crypt_iv_lmk_dtr(struct crypt_config *cc)
535{
536 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
537
538 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
539 crypto_free_shash(lmk->hash_tfm);
540 lmk->hash_tfm = NULL;
541
542 kzfree(lmk->seed);
543 lmk->seed = NULL;
544}
545
546static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
547 const char *opts)
548{
549 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
550
Milan Broz8f0009a2017-03-16 15:39:44 +0100551 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
552 ti->error = "Unsupported sector size for LMK";
553 return -EINVAL;
554 }
555
Milan Broz34745782011-01-13 19:59:55 +0000556 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
557 if (IS_ERR(lmk->hash_tfm)) {
558 ti->error = "Error initializing LMK hash";
559 return PTR_ERR(lmk->hash_tfm);
560 }
561
562 /* No seed in LMK version 2 */
563 if (cc->key_parts == cc->tfms_count) {
564 lmk->seed = NULL;
565 return 0;
566 }
567
568 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
569 if (!lmk->seed) {
570 crypt_iv_lmk_dtr(cc);
571 ti->error = "Error kmallocing seed storage in LMK";
572 return -ENOMEM;
573 }
574
575 return 0;
576}
577
578static int crypt_iv_lmk_init(struct crypt_config *cc)
579{
580 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
581 int subkey_size = cc->key_size / cc->key_parts;
582
583 /* LMK seed is on the position of LMK_KEYS + 1 key */
584 if (lmk->seed)
585 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
586 crypto_shash_digestsize(lmk->hash_tfm));
587
588 return 0;
589}
590
591static int crypt_iv_lmk_wipe(struct crypt_config *cc)
592{
593 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
594
595 if (lmk->seed)
596 memset(lmk->seed, 0, LMK_SEED_SIZE);
597
598 return 0;
599}
600
601static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
602 struct dm_crypt_request *dmreq,
603 u8 *data)
604{
605 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200606 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000607 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100608 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000609 int i, r;
610
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200611 desc->tfm = lmk->hash_tfm;
612 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Broz34745782011-01-13 19:59:55 +0000613
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200614 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000615 if (r)
616 return r;
617
618 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200619 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000620 if (r)
621 return r;
622 }
623
624 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200625 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000626 if (r)
627 return r;
628
629 /* Sector is cropped to 56 bits here */
630 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
631 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
632 buf[2] = cpu_to_le32(4024);
633 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200634 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000635 if (r)
636 return r;
637
638 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200639 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000640 if (r)
641 return r;
642
643 for (i = 0; i < MD5_HASH_WORDS; i++)
644 __cpu_to_le32s(&md5state.hash[i]);
645 memcpy(iv, &md5state.hash, cc->iv_size);
646
647 return 0;
648}
649
650static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
651 struct dm_crypt_request *dmreq)
652{
Milan Brozef43aa32017-01-04 20:23:54 +0100653 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000654 u8 *src;
655 int r = 0;
656
657 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100658 sg = crypt_get_sg_data(cc, dmreq->sg_in);
659 src = kmap_atomic(sg_page(sg));
660 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800661 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000662 } else
663 memset(iv, 0, cc->iv_size);
664
665 return r;
666}
667
668static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
669 struct dm_crypt_request *dmreq)
670{
Milan Brozef43aa32017-01-04 20:23:54 +0100671 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000672 u8 *dst;
673 int r;
674
675 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
676 return 0;
677
Milan Brozef43aa32017-01-04 20:23:54 +0100678 sg = crypt_get_sg_data(cc, dmreq->sg_out);
679 dst = kmap_atomic(sg_page(sg));
680 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset);
Milan Broz34745782011-01-13 19:59:55 +0000681
682 /* Tweak the first block of plaintext sector */
683 if (!r)
Milan Brozef43aa32017-01-04 20:23:54 +0100684 crypto_xor(dst + sg->offset, iv, cc->iv_size);
Milan Broz34745782011-01-13 19:59:55 +0000685
Cong Wangc2e022c2011-11-28 13:26:02 +0800686 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000687 return r;
688}
689
Milan Brozed04d982013-10-28 23:21:04 +0100690static void crypt_iv_tcw_dtr(struct crypt_config *cc)
691{
692 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
693
694 kzfree(tcw->iv_seed);
695 tcw->iv_seed = NULL;
696 kzfree(tcw->whitening);
697 tcw->whitening = NULL;
698
699 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
700 crypto_free_shash(tcw->crc32_tfm);
701 tcw->crc32_tfm = NULL;
702}
703
704static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
705 const char *opts)
706{
707 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
708
Milan Broz8f0009a2017-03-16 15:39:44 +0100709 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
710 ti->error = "Unsupported sector size for TCW";
711 return -EINVAL;
712 }
713
Milan Brozed04d982013-10-28 23:21:04 +0100714 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
715 ti->error = "Wrong key size for TCW";
716 return -EINVAL;
717 }
718
719 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
720 if (IS_ERR(tcw->crc32_tfm)) {
721 ti->error = "Error initializing CRC32 in TCW";
722 return PTR_ERR(tcw->crc32_tfm);
723 }
724
725 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
726 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
727 if (!tcw->iv_seed || !tcw->whitening) {
728 crypt_iv_tcw_dtr(cc);
729 ti->error = "Error allocating seed storage in TCW";
730 return -ENOMEM;
731 }
732
733 return 0;
734}
735
736static int crypt_iv_tcw_init(struct crypt_config *cc)
737{
738 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
739 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
740
741 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
742 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
743 TCW_WHITENING_SIZE);
744
745 return 0;
746}
747
748static int crypt_iv_tcw_wipe(struct crypt_config *cc)
749{
750 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
751
752 memset(tcw->iv_seed, 0, cc->iv_size);
753 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
754
755 return 0;
756}
757
758static int crypt_iv_tcw_whitening(struct crypt_config *cc,
759 struct dm_crypt_request *dmreq,
760 u8 *data)
761{
762 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200763 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100764 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200765 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100766 int i, r;
767
768 /* xor whitening with sector number */
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100769 crypto_xor_cpy(buf, tcw->whitening, (u8 *)&sector, 8);
770 crypto_xor_cpy(&buf[8], tcw->whitening + 8, (u8 *)&sector, 8);
Milan Brozed04d982013-10-28 23:21:04 +0100771
772 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200773 desc->tfm = tcw->crc32_tfm;
774 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Brozed04d982013-10-28 23:21:04 +0100775 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200776 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100777 if (r)
778 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200779 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100780 if (r)
781 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200782 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100783 if (r)
784 goto out;
785 }
786 crypto_xor(&buf[0], &buf[12], 4);
787 crypto_xor(&buf[4], &buf[8], 4);
788
789 /* apply whitening (8 bytes) to whole sector */
790 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
791 crypto_xor(data + i * 8, buf, 8);
792out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100793 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100794 return r;
795}
796
797static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
798 struct dm_crypt_request *dmreq)
799{
Milan Brozef43aa32017-01-04 20:23:54 +0100800 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100801 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200802 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100803 u8 *src;
804 int r = 0;
805
806 /* Remove whitening from ciphertext */
807 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100808 sg = crypt_get_sg_data(cc, dmreq->sg_in);
809 src = kmap_atomic(sg_page(sg));
810 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100811 kunmap_atomic(src);
812 }
813
814 /* Calculate IV */
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100815 crypto_xor_cpy(iv, tcw->iv_seed, (u8 *)&sector, 8);
Milan Brozed04d982013-10-28 23:21:04 +0100816 if (cc->iv_size > 8)
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100817 crypto_xor_cpy(&iv[8], tcw->iv_seed + 8, (u8 *)&sector,
818 cc->iv_size - 8);
Milan Brozed04d982013-10-28 23:21:04 +0100819
820 return r;
821}
822
823static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
824 struct dm_crypt_request *dmreq)
825{
Milan Brozef43aa32017-01-04 20:23:54 +0100826 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100827 u8 *dst;
828 int r;
829
830 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
831 return 0;
832
833 /* Apply whitening on ciphertext */
Milan Brozef43aa32017-01-04 20:23:54 +0100834 sg = crypt_get_sg_data(cc, dmreq->sg_out);
835 dst = kmap_atomic(sg_page(sg));
836 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100837 kunmap_atomic(dst);
838
839 return r;
840}
841
Milan Brozef43aa32017-01-04 20:23:54 +0100842static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv,
843 struct dm_crypt_request *dmreq)
844{
845 /* Used only for writes, there must be an additional space to store IV */
846 get_random_bytes(iv, cc->iv_size);
847 return 0;
848}
849
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100850static const struct crypt_iv_operations crypt_iv_plain_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 .generator = crypt_iv_plain_gen
852};
853
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100854static const struct crypt_iv_operations crypt_iv_plain64_ops = {
Milan Broz61afef62009-12-10 23:52:25 +0000855 .generator = crypt_iv_plain64_gen
856};
857
Milan Broz7e3fd852017-06-06 09:07:01 +0200858static const struct crypt_iv_operations crypt_iv_plain64be_ops = {
859 .generator = crypt_iv_plain64be_gen
860};
861
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100862static const struct crypt_iv_operations crypt_iv_essiv_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 .ctr = crypt_iv_essiv_ctr,
864 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000865 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000866 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 .generator = crypt_iv_essiv_gen
868};
869
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100870static const struct crypt_iv_operations crypt_iv_benbi_ops = {
Rik Snel48527fa2006-09-03 08:56:39 +1000871 .ctr = crypt_iv_benbi_ctr,
872 .dtr = crypt_iv_benbi_dtr,
873 .generator = crypt_iv_benbi_gen
874};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100876static const struct crypt_iv_operations crypt_iv_null_ops = {
Ludwig Nussel46b47732007-05-09 02:32:55 -0700877 .generator = crypt_iv_null_gen
878};
879
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100880static const struct crypt_iv_operations crypt_iv_lmk_ops = {
Milan Broz34745782011-01-13 19:59:55 +0000881 .ctr = crypt_iv_lmk_ctr,
882 .dtr = crypt_iv_lmk_dtr,
883 .init = crypt_iv_lmk_init,
884 .wipe = crypt_iv_lmk_wipe,
885 .generator = crypt_iv_lmk_gen,
886 .post = crypt_iv_lmk_post
887};
888
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100889static const struct crypt_iv_operations crypt_iv_tcw_ops = {
Milan Brozed04d982013-10-28 23:21:04 +0100890 .ctr = crypt_iv_tcw_ctr,
891 .dtr = crypt_iv_tcw_dtr,
892 .init = crypt_iv_tcw_init,
893 .wipe = crypt_iv_tcw_wipe,
894 .generator = crypt_iv_tcw_gen,
895 .post = crypt_iv_tcw_post
896};
897
Milan Brozef43aa32017-01-04 20:23:54 +0100898static struct crypt_iv_operations crypt_iv_random_ops = {
899 .generator = crypt_iv_random_gen
900};
901
902/*
903 * Integrity extensions
904 */
905static bool crypt_integrity_aead(struct crypt_config *cc)
906{
907 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
908}
909
910static bool crypt_integrity_hmac(struct crypt_config *cc)
911{
Milan Broz33d2f092017-03-16 15:39:40 +0100912 return crypt_integrity_aead(cc) && cc->key_mac_size;
Milan Brozef43aa32017-01-04 20:23:54 +0100913}
914
915/* Get sg containing data */
916static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
917 struct scatterlist *sg)
918{
Milan Broz33d2f092017-03-16 15:39:40 +0100919 if (unlikely(crypt_integrity_aead(cc)))
Milan Brozef43aa32017-01-04 20:23:54 +0100920 return &sg[2];
921
922 return sg;
923}
924
925static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
926{
927 struct bio_integrity_payload *bip;
928 unsigned int tag_len;
929 int ret;
930
931 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size)
932 return 0;
933
934 bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
935 if (IS_ERR(bip))
936 return PTR_ERR(bip);
937
938 tag_len = io->cc->on_disk_tag_size * bio_sectors(bio);
939
940 bip->bip_iter.bi_size = tag_len;
941 bip->bip_iter.bi_sector = io->cc->start + io->sector;
942
Milan Brozef43aa32017-01-04 20:23:54 +0100943 ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
944 tag_len, offset_in_page(io->integrity_metadata));
945 if (unlikely(ret != tag_len))
946 return -ENOMEM;
947
948 return 0;
949}
950
951static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
952{
953#ifdef CONFIG_BLK_DEV_INTEGRITY
954 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk);
955
956 /* From now we require underlying device with our integrity profile */
957 if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) {
958 ti->error = "Integrity profile not supported.";
959 return -EINVAL;
960 }
961
Mikulas Patocka583fe742017-04-18 16:51:54 -0400962 if (bi->tag_size != cc->on_disk_tag_size ||
963 bi->tuple_size != cc->on_disk_tag_size) {
Milan Brozef43aa32017-01-04 20:23:54 +0100964 ti->error = "Integrity profile tag size mismatch.";
965 return -EINVAL;
966 }
Mikulas Patocka583fe742017-04-18 16:51:54 -0400967 if (1 << bi->interval_exp != cc->sector_size) {
968 ti->error = "Integrity profile sector size mismatch.";
969 return -EINVAL;
970 }
Milan Brozef43aa32017-01-04 20:23:54 +0100971
Milan Broz33d2f092017-03-16 15:39:40 +0100972 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +0100973 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size;
974 DMINFO("Integrity AEAD, tag size %u, IV size %u.",
975 cc->integrity_tag_size, cc->integrity_iv_size);
976
977 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) {
978 ti->error = "Integrity AEAD auth tag size is not supported.";
979 return -EINVAL;
980 }
981 } else if (cc->integrity_iv_size)
982 DMINFO("Additional per-sector space %u bytes for IV.",
983 cc->integrity_iv_size);
984
985 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) {
986 ti->error = "Not enough space for integrity tag in the profile.";
987 return -EINVAL;
988 }
989
990 return 0;
991#else
992 ti->error = "Integrity profile not supported.";
993 return -EINVAL;
994#endif
995}
996
Milan Brozd469f842007-10-19 22:42:37 +0100997static void crypt_convert_init(struct crypt_config *cc,
998 struct convert_context *ctx,
999 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +00001000 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
1002 ctx->bio_in = bio_in;
1003 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -07001004 if (bio_in)
1005 ctx->iter_in = bio_in->bi_iter;
1006 if (bio_out)
1007 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +01001008 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +00001009 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
Huang Yingb2174ee2009-03-16 17:44:33 +00001012static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
Milan Brozef43aa32017-01-04 20:23:54 +01001013 void *req)
Huang Yingb2174ee2009-03-16 17:44:33 +00001014{
1015 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
1016}
1017
Milan Brozef43aa32017-01-04 20:23:54 +01001018static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq)
Huang Yingb2174ee2009-03-16 17:44:33 +00001019{
Milan Brozef43aa32017-01-04 20:23:54 +01001020 return (void *)((char *)dmreq - cc->dmreq_start);
Huang Yingb2174ee2009-03-16 17:44:33 +00001021}
1022
Milan Broz2dc53272011-01-13 19:59:54 +00001023static u8 *iv_of_dmreq(struct crypt_config *cc,
1024 struct dm_crypt_request *dmreq)
1025{
Milan Broz33d2f092017-03-16 15:39:40 +01001026 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001027 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1028 crypto_aead_alignmask(any_tfm_aead(cc)) + 1);
1029 else
1030 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1031 crypto_skcipher_alignmask(any_tfm(cc)) + 1);
Milan Broz2dc53272011-01-13 19:59:54 +00001032}
1033
Milan Brozef43aa32017-01-04 20:23:54 +01001034static u8 *org_iv_of_dmreq(struct crypt_config *cc,
1035 struct dm_crypt_request *dmreq)
1036{
1037 return iv_of_dmreq(cc, dmreq) + cc->iv_size;
1038}
1039
1040static uint64_t *org_sector_of_dmreq(struct crypt_config *cc,
1041 struct dm_crypt_request *dmreq)
1042{
1043 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size;
1044 return (uint64_t*) ptr;
1045}
1046
1047static unsigned int *org_tag_of_dmreq(struct crypt_config *cc,
1048 struct dm_crypt_request *dmreq)
1049{
1050 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size +
1051 cc->iv_size + sizeof(uint64_t);
1052 return (unsigned int*)ptr;
1053}
1054
1055static void *tag_from_dmreq(struct crypt_config *cc,
1056 struct dm_crypt_request *dmreq)
1057{
1058 struct convert_context *ctx = dmreq->ctx;
1059 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1060
1061 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) *
1062 cc->on_disk_tag_size];
1063}
1064
1065static void *iv_tag_from_dmreq(struct crypt_config *cc,
1066 struct dm_crypt_request *dmreq)
1067{
1068 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size;
1069}
1070
1071static int crypt_convert_block_aead(struct crypt_config *cc,
1072 struct convert_context *ctx,
1073 struct aead_request *req,
1074 unsigned int tag_offset)
Milan Broz01482b72008-02-08 02:11:04 +00001075{
Kent Overstreet003b5c52013-10-11 15:45:43 -07001076 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1077 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001078 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001079 u8 *iv, *org_iv, *tag_iv, *tag;
1080 uint64_t *sector;
1081 int r = 0;
1082
1083 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size);
Milan Broz01482b72008-02-08 02:11:04 +00001084
Milan Broz8f0009a2017-03-16 15:39:44 +01001085 /* Reject unexpected unaligned bio. */
Mikulas Patocka0440d5c2017-11-07 10:35:57 -05001086 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Broz8f0009a2017-03-16 15:39:44 +01001087 return -EIO;
Milan Broz01482b72008-02-08 02:11:04 +00001088
Huang Yingb2174ee2009-03-16 17:44:33 +00001089 dmreq = dmreq_of_req(cc, req);
Mikulas Patockac66029f2012-07-27 15:08:05 +01001090 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001091 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001092 dmreq->iv_sector >>= cc->sector_shift;
Huang Yingb2174ee2009-03-16 17:44:33 +00001093 dmreq->ctx = ctx;
Milan Broz01482b72008-02-08 02:11:04 +00001094
Milan Brozef43aa32017-01-04 20:23:54 +01001095 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
Milan Broz01482b72008-02-08 02:11:04 +00001096
Milan Brozef43aa32017-01-04 20:23:54 +01001097 sector = org_sector_of_dmreq(cc, dmreq);
1098 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1099
1100 iv = iv_of_dmreq(cc, dmreq);
1101 org_iv = org_iv_of_dmreq(cc, dmreq);
1102 tag = tag_from_dmreq(cc, dmreq);
1103 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1104
1105 /* AEAD request:
1106 * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
1107 * | (authenticated) | (auth+encryption) | |
1108 * | sector_LE | IV | sector in/out | tag in/out |
1109 */
1110 sg_init_table(dmreq->sg_in, 4);
1111 sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t));
1112 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001113 sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001114 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size);
1115
1116 sg_init_table(dmreq->sg_out, 4);
1117 sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t));
1118 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001119 sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001120 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size);
Milan Broz01482b72008-02-08 02:11:04 +00001121
Milan Broz3a7f6c92008-02-08 02:11:14 +00001122 if (cc->iv_gen_ops) {
Milan Brozef43aa32017-01-04 20:23:54 +01001123 /* For READs use IV stored in integrity metadata */
1124 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1125 memcpy(org_iv, tag_iv, cc->iv_size);
1126 } else {
1127 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1128 if (r < 0)
1129 return r;
1130 /* Store generated IV in integrity metadata */
1131 if (cc->integrity_iv_size)
1132 memcpy(tag_iv, org_iv, cc->iv_size);
1133 }
1134 /* Working copy of IV, to be modified in crypto API */
1135 memcpy(iv, org_iv, cc->iv_size);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001136 }
1137
Milan Brozef43aa32017-01-04 20:23:54 +01001138 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size);
1139 if (bio_data_dir(ctx->bio_in) == WRITE) {
1140 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001141 cc->sector_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001142 r = crypto_aead_encrypt(req);
1143 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size)
1144 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0,
1145 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size));
1146 } else {
1147 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001148 cc->sector_size + cc->integrity_tag_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001149 r = crypto_aead_decrypt(req);
1150 }
1151
1152 if (r == -EBADMSG)
1153 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1154 (unsigned long long)le64_to_cpu(*sector));
1155
1156 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
1157 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1158
Milan Broz8f0009a2017-03-16 15:39:44 +01001159 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1160 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001161
1162 return r;
1163}
1164
1165static int crypt_convert_block_skcipher(struct crypt_config *cc,
1166 struct convert_context *ctx,
1167 struct skcipher_request *req,
1168 unsigned int tag_offset)
1169{
1170 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1171 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
1172 struct scatterlist *sg_in, *sg_out;
1173 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001174 u8 *iv, *org_iv, *tag_iv;
1175 uint64_t *sector;
1176 int r = 0;
1177
Milan Broz8f0009a2017-03-16 15:39:44 +01001178 /* Reject unexpected unaligned bio. */
Mikulas Patocka0440d5c2017-11-07 10:35:57 -05001179 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Broz8f0009a2017-03-16 15:39:44 +01001180 return -EIO;
1181
Milan Brozef43aa32017-01-04 20:23:54 +01001182 dmreq = dmreq_of_req(cc, req);
1183 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001184 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001185 dmreq->iv_sector >>= cc->sector_shift;
Milan Brozef43aa32017-01-04 20:23:54 +01001186 dmreq->ctx = ctx;
1187
1188 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
1189
1190 iv = iv_of_dmreq(cc, dmreq);
1191 org_iv = org_iv_of_dmreq(cc, dmreq);
1192 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1193
1194 sector = org_sector_of_dmreq(cc, dmreq);
1195 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1196
1197 /* For skcipher we use only the first sg item */
1198 sg_in = &dmreq->sg_in[0];
1199 sg_out = &dmreq->sg_out[0];
1200
1201 sg_init_table(sg_in, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001202 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001203
1204 sg_init_table(sg_out, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001205 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001206
1207 if (cc->iv_gen_ops) {
1208 /* For READs use IV stored in integrity metadata */
1209 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1210 memcpy(org_iv, tag_iv, cc->integrity_iv_size);
1211 } else {
1212 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1213 if (r < 0)
1214 return r;
1215 /* Store generated IV in integrity metadata */
1216 if (cc->integrity_iv_size)
1217 memcpy(tag_iv, org_iv, cc->integrity_iv_size);
1218 }
1219 /* Working copy of IV, to be modified in crypto API */
1220 memcpy(iv, org_iv, cc->iv_size);
1221 }
1222
Milan Broz8f0009a2017-03-16 15:39:44 +01001223 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001224
1225 if (bio_data_dir(ctx->bio_in) == WRITE)
Herbert Xubbdb23b2016-01-24 21:16:36 +08001226 r = crypto_skcipher_encrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001227 else
Herbert Xubbdb23b2016-01-24 21:16:36 +08001228 r = crypto_skcipher_decrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001229
Milan Broz2dc53272011-01-13 19:59:54 +00001230 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001231 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1232
Milan Broz8f0009a2017-03-16 15:39:44 +01001233 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1234 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Broz2dc53272011-01-13 19:59:54 +00001235
Milan Broz3a7f6c92008-02-08 02:11:14 +00001236 return r;
Milan Broz01482b72008-02-08 02:11:04 +00001237}
1238
Milan Broz95497a92008-02-08 02:11:12 +00001239static void kcryptd_async_done(struct crypto_async_request *async_req,
1240 int error);
Andi Kleenc0297722011-01-13 19:59:53 +00001241
Milan Brozef43aa32017-01-04 20:23:54 +01001242static void crypt_alloc_req_skcipher(struct crypt_config *cc,
1243 struct convert_context *ctx)
Milan Brozddd42ed2008-02-08 02:11:07 +00001244{
Mikulas Patockac66029f2012-07-27 15:08:05 +01001245 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +00001246
Milan Brozef43aa32017-01-04 20:23:54 +01001247 if (!ctx->r.req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001248 ctx->r.req = mempool_alloc(&cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +00001249
Milan Brozef43aa32017-01-04 20:23:54 +01001250 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]);
Milan Broz54cea3f2015-05-15 17:00:25 +02001251
1252 /*
1253 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1254 * requests if driver request queue is full.
1255 */
Milan Brozef43aa32017-01-04 20:23:54 +01001256 skcipher_request_set_callback(ctx->r.req,
Andi Kleenc0297722011-01-13 19:59:53 +00001257 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Milan Brozef43aa32017-01-04 20:23:54 +01001258 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req));
Milan Brozddd42ed2008-02-08 02:11:07 +00001259}
1260
Milan Brozef43aa32017-01-04 20:23:54 +01001261static void crypt_alloc_req_aead(struct crypt_config *cc,
1262 struct convert_context *ctx)
1263{
1264 if (!ctx->r.req_aead)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001265 ctx->r.req_aead = mempool_alloc(&cc->req_pool, GFP_NOIO);
Milan Brozef43aa32017-01-04 20:23:54 +01001266
1267 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]);
1268
1269 /*
1270 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1271 * requests if driver request queue is full.
1272 */
1273 aead_request_set_callback(ctx->r.req_aead,
1274 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
1275 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead));
1276}
1277
1278static void crypt_alloc_req(struct crypt_config *cc,
1279 struct convert_context *ctx)
1280{
Milan Broz33d2f092017-03-16 15:39:40 +01001281 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001282 crypt_alloc_req_aead(cc, ctx);
1283 else
1284 crypt_alloc_req_skcipher(cc, ctx);
1285}
1286
1287static void crypt_free_req_skcipher(struct crypt_config *cc,
1288 struct skcipher_request *req, struct bio *base_bio)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001289{
1290 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1291
Herbert Xubbdb23b2016-01-24 21:16:36 +08001292 if ((struct skcipher_request *)(io + 1) != req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001293 mempool_free(req, &cc->req_pool);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001294}
1295
Milan Brozef43aa32017-01-04 20:23:54 +01001296static void crypt_free_req_aead(struct crypt_config *cc,
1297 struct aead_request *req, struct bio *base_bio)
1298{
1299 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1300
1301 if ((struct aead_request *)(io + 1) != req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001302 mempool_free(req, &cc->req_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01001303}
1304
1305static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio)
1306{
Milan Broz33d2f092017-03-16 15:39:40 +01001307 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001308 crypt_free_req_aead(cc, req, base_bio);
1309 else
1310 crypt_free_req_skcipher(cc, req, base_bio);
1311}
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313/*
1314 * Encrypt / decrypt data from one bio to another one (can be the same one)
1315 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001316static blk_status_t crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +01001317 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Milan Brozef43aa32017-01-04 20:23:54 +01001319 unsigned int tag_offset = 0;
Mikulas Patockaff3af922017-03-23 10:23:14 -04001320 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT;
Milan Broz3f1e9072008-03-28 14:16:07 -07001321 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Mikulas Patocka40b62292012-07-27 15:08:04 +01001323 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +01001324
Kent Overstreet003b5c52013-10-11 15:45:43 -07001325 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Milan Broz3a7f6c92008-02-08 02:11:14 +00001327 crypt_alloc_req(cc, ctx);
Mikulas Patocka40b62292012-07-27 15:08:04 +01001328 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -07001329
Milan Broz33d2f092017-03-16 15:39:40 +01001330 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001331 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset);
1332 else
1333 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001334
1335 switch (r) {
Milan Broz54cea3f2015-05-15 17:00:25 +02001336 /*
1337 * The request was queued by a crypto driver
1338 * but the driver request queue is full, let's wait.
1339 */
Milan Broz3a7f6c92008-02-08 02:11:14 +00001340 case -EBUSY:
1341 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -08001342 reinit_completion(&ctx->restart);
Milan Broz54cea3f2015-05-15 17:00:25 +02001343 /* fall through */
1344 /*
1345 * The request is queued and processed asynchronously,
1346 * completion function kcryptd_async_done() will be called.
1347 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001348 case -EINPROGRESS:
Milan Brozef43aa32017-01-04 20:23:54 +01001349 ctx->r.req = NULL;
Milan Broz8f0009a2017-03-16 15:39:44 +01001350 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001351 tag_offset++;
Milan Broz3a7f6c92008-02-08 02:11:14 +00001352 continue;
Milan Broz54cea3f2015-05-15 17:00:25 +02001353 /*
1354 * The request was already processed (synchronously).
1355 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001356 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001357 atomic_dec(&ctx->cc_pending);
Milan Broz8f0009a2017-03-16 15:39:44 +01001358 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001359 tag_offset++;
Milan Brozc7f1b202008-07-02 09:34:28 +01001360 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -07001361 continue;
Milan Brozef43aa32017-01-04 20:23:54 +01001362 /*
1363 * There was a data integrity error.
1364 */
1365 case -EBADMSG:
1366 atomic_dec(&ctx->cc_pending);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001367 return BLK_STS_PROTECTION;
Milan Brozef43aa32017-01-04 20:23:54 +01001368 /*
1369 * There was an error while processing the request.
1370 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001371 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001372 atomic_dec(&ctx->cc_pending);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001373 return BLK_STS_IOERR;
Milan Broz3f1e9072008-03-28 14:16:07 -07001374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376
Milan Broz3f1e9072008-03-28 14:16:07 -07001377 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
1379
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001380static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382/*
1383 * Generate a new unfragmented bio with the given size
Mike Snitzer586b2862015-09-09 21:34:51 -04001384 * This should never violate the device limitations (but only because
1385 * max_segment_size is being constrained to PAGE_SIZE).
Mikulas Patocka7145c242015-02-13 08:24:41 -05001386 *
1387 * This function may be called concurrently. If we allocate from the mempool
1388 * concurrently, there is a possibility of deadlock. For example, if we have
1389 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1390 * the mempool concurrently, it may deadlock in a situation where both processes
1391 * have allocated 128 pages and the mempool is exhausted.
1392 *
1393 * In order to avoid this scenario we allocate the pages under a mutex.
1394 *
1395 * In order to not degrade performance with excessive locking, we try
1396 * non-blocking allocations without a mutex first but on failure we fallback
1397 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001399static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001401 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001402 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001404 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
1405 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +00001406 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Mikulas Patocka7145c242015-02-13 08:24:41 -05001408retry:
Mel Gormand0164ad2015-11-06 16:28:21 -08001409 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001410 mutex_lock(&cc->bio_alloc_lock);
1411
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001412 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, &cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -07001413 if (!clone)
Milan Brozef43aa32017-01-04 20:23:54 +01001414 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Olaf Kirch027581f2007-05-09 02:32:52 -07001416 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -07001417
Mikulas Patocka7145c242015-02-13 08:24:41 -05001418 remaining_size = size;
1419
Olaf Kirchf97380b2007-05-09 02:32:54 -07001420 for (i = 0; i < nr_iovecs; i++) {
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001421 page = mempool_alloc(&cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -05001422 if (!page) {
1423 crypt_free_buffer_pages(cc, clone);
1424 bio_put(clone);
Mel Gormand0164ad2015-11-06 16:28:21 -08001425 gfp_mask |= __GFP_DIRECT_RECLAIM;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001426 goto retry;
1427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Mikulas Patocka7145c242015-02-13 08:24:41 -05001429 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Ming Lei0dae7fe2016-10-29 16:08:06 +08001431 bio_add_page(clone, page, len, 0);
Milan Broz91e10622007-12-13 14:16:10 +00001432
Mikulas Patocka7145c242015-02-13 08:24:41 -05001433 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
1435
Milan Brozef43aa32017-01-04 20:23:54 +01001436 /* Allocate space for integrity tags */
1437 if (dm_crypt_integrity_io_alloc(io, clone)) {
1438 crypt_free_buffer_pages(cc, clone);
1439 bio_put(clone);
1440 clone = NULL;
1441 }
1442out:
Mel Gormand0164ad2015-11-06 16:28:21 -08001443 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001444 mutex_unlock(&cc->bio_alloc_lock);
1445
Milan Broz8b004452006-10-03 01:15:37 -07001446 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Neil Brown644bd2f2007-10-16 13:48:46 +02001449static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Neil Brown644bd2f2007-10-16 13:48:46 +02001451 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 struct bio_vec *bv;
1453
Kent Overstreetcb34e052012-09-05 15:22:02 -07001454 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 BUG_ON(!bv->bv_page);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001456 mempool_free(bv->bv_page, &cc->page_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
1458}
1459
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001460static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1461 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001462{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001463 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001464 io->base_bio = bio;
1465 io->sector = sector;
1466 io->error = 0;
Milan Brozef43aa32017-01-04 20:23:54 +01001467 io->ctx.r.req = NULL;
1468 io->integrity_metadata = NULL;
1469 io->integrity_metadata_from_pool = false;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001470 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001471}
1472
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001473static void crypt_inc_pending(struct dm_crypt_io *io)
1474{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001475 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001476}
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478/*
1479 * One of the bios was finished. Check for completion of
1480 * the whole request and correctly clean up the buffer.
1481 */
Milan Broz5742fd72008-02-08 02:10:43 +00001482static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001484 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001485 struct bio *base_bio = io->base_bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001486 blk_status_t error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Mikulas Patocka40b62292012-07-27 15:08:04 +01001488 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 return;
1490
Milan Brozef43aa32017-01-04 20:23:54 +01001491 if (io->ctx.r.req)
1492 crypt_free_req(cc, io->ctx.r.req, base_bio);
1493
1494 if (unlikely(io->integrity_metadata_from_pool))
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001495 mempool_free(io->integrity_metadata, &io->cc->tag_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01001496 else
1497 kfree(io->integrity_metadata);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001498
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001499 base_bio->bi_status = error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001500 bio_endio(base_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
1503/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001504 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 *
1506 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001507 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001508 *
1509 * kcryptd performs the actual encryption or decryption.
1510 *
1511 * kcryptd_io performs the IO submission.
1512 *
1513 * They must be separated as otherwise the final stages could be
1514 * starved by new requests which can block in the first stages due
1515 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001516 *
1517 * The work is done per CPU global for all dm-crypt instances.
1518 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001520static void crypt_endio(struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001521{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001522 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001523 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001524 unsigned rw = bio_data_dir(clone);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001525 blk_status_t error;
Milan Broz8b004452006-10-03 01:15:37 -07001526
1527 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001528 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001529 */
Milan Brozee7a4912008-02-08 02:10:46 +00001530 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001531 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001532
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001533 error = clone->bi_status;
Milan Brozee7a4912008-02-08 02:10:46 +00001534 bio_put(clone);
1535
Sasha Levin9b81c842015-08-10 19:05:18 -04001536 if (rw == READ && !error) {
Milan Brozee7a4912008-02-08 02:10:46 +00001537 kcryptd_queue_crypt(io);
1538 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001539 }
Milan Broz8b004452006-10-03 01:15:37 -07001540
Sasha Levin9b81c842015-08-10 19:05:18 -04001541 if (unlikely(error))
1542 io->error = error;
Milan Broz5742fd72008-02-08 02:10:43 +00001543
1544 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001545}
1546
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001547static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001548{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001549 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001550
1551 clone->bi_private = io;
1552 clone->bi_end_io = crypt_endio;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001553 bio_set_dev(clone, cc->dev->bdev);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001554 clone->bi_opf = io->base_bio->bi_opf;
Milan Broz8b004452006-10-03 01:15:37 -07001555}
1556
Milan Broz20c82532011-01-13 19:59:53 +00001557static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001558{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001559 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001560 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001561
Milan Broz8b004452006-10-03 01:15:37 -07001562 /*
Mike Snitzer59779072015-04-09 16:53:24 -04001563 * We need the original biovec array in order to decrypt
1564 * the whole bio data *afterwards* -- thanks to immutable
1565 * biovecs we don't need to worry about the block layer
1566 * modifying the biovec array; so leverage bio_clone_fast().
Milan Broz8b004452006-10-03 01:15:37 -07001567 */
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001568 clone = bio_clone_fast(io->base_bio, gfp, &cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001569 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001570 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001571
Milan Broz20c82532011-01-13 19:59:53 +00001572 crypt_inc_pending(io);
1573
Milan Broz8b004452006-10-03 01:15:37 -07001574 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001575 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001576
Milan Brozef43aa32017-01-04 20:23:54 +01001577 if (dm_crypt_integrity_io_alloc(io, clone)) {
1578 crypt_dec_pending(io);
1579 bio_put(clone);
1580 return 1;
1581 }
1582
Milan Broz93e605c2006-10-03 01:15:38 -07001583 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001584 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001585}
1586
Mikulas Patockadc267622015-02-13 08:25:59 -05001587static void kcryptd_io_read_work(struct work_struct *work)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001588{
1589 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1590
Mikulas Patockadc267622015-02-13 08:25:59 -05001591 crypt_inc_pending(io);
1592 if (kcryptd_io_read(io, GFP_NOIO))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001593 io->error = BLK_STS_RESOURCE;
Mikulas Patockadc267622015-02-13 08:25:59 -05001594 crypt_dec_pending(io);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001595}
1596
Mikulas Patockadc267622015-02-13 08:25:59 -05001597static void kcryptd_queue_read(struct dm_crypt_io *io)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001598{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001599 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001600
Mikulas Patockadc267622015-02-13 08:25:59 -05001601 INIT_WORK(&io->work, kcryptd_io_read_work);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001602 queue_work(cc->io_queue, &io->work);
1603}
1604
Mikulas Patockadc267622015-02-13 08:25:59 -05001605static void kcryptd_io_write(struct dm_crypt_io *io)
1606{
1607 struct bio *clone = io->ctx.bio_out;
1608
1609 generic_make_request(clone);
1610}
1611
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001612#define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1613
Mikulas Patockadc267622015-02-13 08:25:59 -05001614static int dmcrypt_write(void *data)
1615{
1616 struct crypt_config *cc = data;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001617 struct dm_crypt_io *io;
1618
Mikulas Patockadc267622015-02-13 08:25:59 -05001619 while (1) {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001620 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -05001621 struct blk_plug plug;
1622
1623 DECLARE_WAITQUEUE(wait, current);
1624
1625 spin_lock_irq(&cc->write_thread_wait.lock);
1626continue_locked:
1627
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001628 if (!RB_EMPTY_ROOT(&cc->write_tree))
Mikulas Patockadc267622015-02-13 08:25:59 -05001629 goto pop_from_list;
1630
Rabin Vincentf659b102016-09-21 16:22:29 +02001631 set_current_state(TASK_INTERRUPTIBLE);
Mikulas Patockadc267622015-02-13 08:25:59 -05001632 __add_wait_queue(&cc->write_thread_wait, &wait);
1633
1634 spin_unlock_irq(&cc->write_thread_wait.lock);
1635
Rabin Vincentf659b102016-09-21 16:22:29 +02001636 if (unlikely(kthread_should_stop())) {
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001637 set_current_state(TASK_RUNNING);
Rabin Vincentf659b102016-09-21 16:22:29 +02001638 remove_wait_queue(&cc->write_thread_wait, &wait);
1639 break;
1640 }
1641
Mikulas Patockadc267622015-02-13 08:25:59 -05001642 schedule();
1643
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001644 set_current_state(TASK_RUNNING);
Mikulas Patockadc267622015-02-13 08:25:59 -05001645 spin_lock_irq(&cc->write_thread_wait.lock);
1646 __remove_wait_queue(&cc->write_thread_wait, &wait);
1647 goto continue_locked;
1648
1649pop_from_list:
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001650 write_tree = cc->write_tree;
1651 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05001652 spin_unlock_irq(&cc->write_thread_wait.lock);
1653
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001654 BUG_ON(rb_parent(write_tree.rb_node));
1655
1656 /*
1657 * Note: we cannot walk the tree here with rb_next because
1658 * the structures may be freed when kcryptd_io_write is called.
1659 */
Mikulas Patockadc267622015-02-13 08:25:59 -05001660 blk_start_plug(&plug);
1661 do {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001662 io = crypt_io_from_node(rb_first(&write_tree));
1663 rb_erase(&io->rb_node, &write_tree);
Mikulas Patockadc267622015-02-13 08:25:59 -05001664 kcryptd_io_write(io);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001665 } while (!RB_EMPTY_ROOT(&write_tree));
Mikulas Patockadc267622015-02-13 08:25:59 -05001666 blk_finish_plug(&plug);
1667 }
1668 return 0;
1669}
1670
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001671static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001672{
Milan Brozdec1ced2008-02-08 02:10:57 +00001673 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001674 struct crypt_config *cc = io->cc;
Mikulas Patockadc267622015-02-13 08:25:59 -05001675 unsigned long flags;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001676 sector_t sector;
1677 struct rb_node **rbp, *parent;
Milan Brozdec1ced2008-02-08 02:10:57 +00001678
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001679 if (unlikely(io->error)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001680 crypt_free_buffer_pages(cc, clone);
1681 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001682 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001683 return;
1684 }
1685
1686 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001687 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001688
Kent Overstreet4f024f32013-10-11 15:44:27 -07001689 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001690
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001691 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1692 generic_make_request(clone);
1693 return;
1694 }
1695
Mikulas Patockadc267622015-02-13 08:25:59 -05001696 spin_lock_irqsave(&cc->write_thread_wait.lock, flags);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001697 rbp = &cc->write_tree.rb_node;
1698 parent = NULL;
1699 sector = io->sector;
1700 while (*rbp) {
1701 parent = *rbp;
1702 if (sector < crypt_io_from_node(parent)->sector)
1703 rbp = &(*rbp)->rb_left;
1704 else
1705 rbp = &(*rbp)->rb_right;
1706 }
1707 rb_link_node(&io->rb_node, parent, rbp);
1708 rb_insert_color(&io->rb_node, &cc->write_tree);
1709
Mikulas Patockadc267622015-02-13 08:25:59 -05001710 wake_up_locked(&cc->write_thread_wait);
1711 spin_unlock_irqrestore(&cc->write_thread_wait.lock, flags);
Milan Broz4e4eef62008-02-08 02:10:49 +00001712}
1713
Milan Brozfc5a5e92008-10-10 13:37:04 +01001714static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001715{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001716 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001717 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001718 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001719 sector_t sector = io->sector;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001720 blk_status_t r;
Milan Broz8b004452006-10-03 01:15:37 -07001721
Milan Broz93e605c2006-10-03 01:15:38 -07001722 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001723 * Prevent io from disappearing until this function completes.
1724 */
1725 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001726 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001727
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001728 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1729 if (unlikely(!clone)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001730 io->error = BLK_STS_IOERR;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001731 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001732 }
Milan Broz899c95d2008-02-08 02:11:02 +00001733
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001734 io->ctx.bio_out = clone;
1735 io->ctx.iter_out = clone->bi_iter;
1736
1737 sector += bio_sectors(clone);
1738
1739 crypt_inc_pending(io);
1740 r = crypt_convert(cc, &io->ctx);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001741 if (r)
Milan Brozef43aa32017-01-04 20:23:54 +01001742 io->error = r;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001743 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1744
1745 /* Encryption was already finished, submit io now */
1746 if (crypt_finished) {
1747 kcryptd_crypt_write_io_submit(io, 0);
1748 io->sector = sector;
1749 }
1750
1751dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001752 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001753}
1754
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001755static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001756{
Milan Broz5742fd72008-02-08 02:10:43 +00001757 crypt_dec_pending(io);
1758}
1759
Milan Broz4e4eef62008-02-08 02:10:49 +00001760static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001761{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001762 struct crypt_config *cc = io->cc;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001763 blk_status_t r;
Milan Broz8b004452006-10-03 01:15:37 -07001764
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001765 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001766
Milan Broz53017032008-02-08 02:10:38 +00001767 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001768 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001769
Milan Broz5742fd72008-02-08 02:10:43 +00001770 r = crypt_convert(cc, &io->ctx);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001771 if (r)
Milan Brozef43aa32017-01-04 20:23:54 +01001772 io->error = r;
Milan Broz5742fd72008-02-08 02:10:43 +00001773
Mikulas Patocka40b62292012-07-27 15:08:04 +01001774 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001775 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001776
1777 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001778}
1779
Milan Broz95497a92008-02-08 02:11:12 +00001780static void kcryptd_async_done(struct crypto_async_request *async_req,
1781 int error)
1782{
Huang Yingb2174ee2009-03-16 17:44:33 +00001783 struct dm_crypt_request *dmreq = async_req->data;
1784 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001785 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001786 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001787
Milan Broz54cea3f2015-05-15 17:00:25 +02001788 /*
1789 * A request from crypto driver backlog is going to be processed now,
1790 * finish the completion and continue in crypt_convert().
1791 * (Callback will be called for the second time for this request.)
1792 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001793 if (error == -EINPROGRESS) {
1794 complete(&ctx->restart);
Milan Broz95497a92008-02-08 02:11:12 +00001795 return;
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001796 }
Milan Broz95497a92008-02-08 02:11:12 +00001797
Milan Broz2dc53272011-01-13 19:59:54 +00001798 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001799 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq);
Milan Broz2dc53272011-01-13 19:59:54 +00001800
Milan Brozef43aa32017-01-04 20:23:54 +01001801 if (error == -EBADMSG) {
1802 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1803 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001804 io->error = BLK_STS_PROTECTION;
Milan Brozef43aa32017-01-04 20:23:54 +01001805 } else if (error < 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001806 io->error = BLK_STS_IOERR;
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001807
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001808 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00001809
Mikulas Patocka40b62292012-07-27 15:08:04 +01001810 if (!atomic_dec_and_test(&ctx->cc_pending))
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001811 return;
Milan Broz95497a92008-02-08 02:11:12 +00001812
1813 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001814 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001815 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001816 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001817}
1818
Milan Broz4e4eef62008-02-08 02:10:49 +00001819static void kcryptd_crypt(struct work_struct *work)
1820{
1821 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1822
1823 if (bio_data_dir(io->base_bio) == READ)
1824 kcryptd_crypt_read_convert(io);
1825 else
1826 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001827}
1828
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001829static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1830{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001831 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001832
1833 INIT_WORK(&io->work, kcryptd_crypt);
1834 queue_work(cc->crypt_queue, &io->work);
1835}
1836
Milan Brozef43aa32017-01-04 20:23:54 +01001837static void crypt_free_tfms_aead(struct crypt_config *cc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Milan Brozef43aa32017-01-04 20:23:54 +01001839 if (!cc->cipher_tfm.tfms_aead)
1840 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Milan Brozef43aa32017-01-04 20:23:54 +01001842 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1843 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]);
1844 cc->cipher_tfm.tfms_aead[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
1846
Milan Brozef43aa32017-01-04 20:23:54 +01001847 kfree(cc->cipher_tfm.tfms_aead);
1848 cc->cipher_tfm.tfms_aead = NULL;
1849}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Milan Brozef43aa32017-01-04 20:23:54 +01001851static void crypt_free_tfms_skcipher(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001852{
Milan Brozd1f96422011-01-13 19:59:54 +00001853 unsigned i;
1854
Milan Brozef43aa32017-01-04 20:23:54 +01001855 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001856 return;
1857
Milan Brozd1f96422011-01-13 19:59:54 +00001858 for (i = 0; i < cc->tfms_count; i++)
Milan Brozef43aa32017-01-04 20:23:54 +01001859 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) {
1860 crypto_free_skcipher(cc->cipher_tfm.tfms[i]);
1861 cc->cipher_tfm.tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001862 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001863
Milan Brozef43aa32017-01-04 20:23:54 +01001864 kfree(cc->cipher_tfm.tfms);
1865 cc->cipher_tfm.tfms = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
1867
1868static void crypt_free_tfms(struct crypt_config *cc)
1869{
Milan Broz33d2f092017-03-16 15:39:40 +01001870 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001871 crypt_free_tfms_aead(cc);
1872 else
1873 crypt_free_tfms_skcipher(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001874}
1875
Milan Brozef43aa32017-01-04 20:23:54 +01001876static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001877{
Milan Brozd1f96422011-01-13 19:59:54 +00001878 unsigned i;
1879 int err;
1880
Milan Brozef43aa32017-01-04 20:23:54 +01001881 cc->cipher_tfm.tfms = kzalloc(cc->tfms_count *
1882 sizeof(struct crypto_skcipher *), GFP_KERNEL);
1883 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001884 return -ENOMEM;
1885
Milan Brozd1f96422011-01-13 19:59:54 +00001886 for (i = 0; i < cc->tfms_count; i++) {
Milan Brozef43aa32017-01-04 20:23:54 +01001887 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
1888 if (IS_ERR(cc->cipher_tfm.tfms[i])) {
1889 err = PTR_ERR(cc->cipher_tfm.tfms[i]);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001890 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001891 return err;
1892 }
1893 }
1894
1895 return 0;
1896}
1897
Milan Brozef43aa32017-01-04 20:23:54 +01001898static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
1899{
Milan Brozef43aa32017-01-04 20:23:54 +01001900 int err;
1901
1902 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
1903 if (!cc->cipher_tfm.tfms)
1904 return -ENOMEM;
1905
Milan Brozef43aa32017-01-04 20:23:54 +01001906 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
1907 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1908 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
1909 crypt_free_tfms(cc);
1910 return err;
1911 }
1912
Milan Brozef43aa32017-01-04 20:23:54 +01001913 return 0;
1914}
1915
1916static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
1917{
Milan Broz33d2f092017-03-16 15:39:40 +01001918 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001919 return crypt_alloc_tfms_aead(cc, ciphermode);
1920 else
1921 return crypt_alloc_tfms_skcipher(cc, ciphermode);
1922}
1923
1924static unsigned crypt_subkey_size(struct crypt_config *cc)
1925{
1926 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1927}
1928
1929static unsigned crypt_authenckey_size(struct crypt_config *cc)
1930{
1931 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
1932}
1933
1934/*
1935 * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
1936 * the key must be for some reason in special format.
1937 * This funcion converts cc->key to this special format.
1938 */
1939static void crypt_copy_authenckey(char *p, const void *key,
1940 unsigned enckeylen, unsigned authkeylen)
1941{
1942 struct crypto_authenc_key_param *param;
1943 struct rtattr *rta;
1944
1945 rta = (struct rtattr *)p;
1946 param = RTA_DATA(rta);
1947 param->enckeylen = cpu_to_be32(enckeylen);
1948 rta->rta_len = RTA_LENGTH(sizeof(*param));
1949 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
1950 p += RTA_SPACE(sizeof(*param));
1951 memcpy(p, key + enckeylen, authkeylen);
1952 p += authkeylen;
1953 memcpy(p, key, enckeylen);
1954}
1955
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04001956static int crypt_setkey(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +00001957{
Milan Brozda31a072013-10-28 23:21:03 +01001958 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001959 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001960
Milan Brozda31a072013-10-28 23:21:03 +01001961 /* Ignore extra keys (which are used for IV etc) */
Milan Brozef43aa32017-01-04 20:23:54 +01001962 subkey_size = crypt_subkey_size(cc);
Milan Brozda31a072013-10-28 23:21:03 +01001963
Milan Broz27c70032018-01-03 22:48:59 +01001964 if (crypt_integrity_hmac(cc)) {
1965 if (subkey_size < cc->key_mac_size)
1966 return -EINVAL;
1967
Milan Brozef43aa32017-01-04 20:23:54 +01001968 crypt_copy_authenckey(cc->authenc_key, cc->key,
1969 subkey_size - cc->key_mac_size,
1970 cc->key_mac_size);
Milan Broz27c70032018-01-03 22:48:59 +01001971 }
1972
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001973 for (i = 0; i < cc->tfms_count; i++) {
Milan Broz33d2f092017-03-16 15:39:40 +01001974 if (crypt_integrity_hmac(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001975 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1976 cc->authenc_key, crypt_authenckey_size(cc));
Milan Broz33d2f092017-03-16 15:39:40 +01001977 else if (crypt_integrity_aead(cc))
1978 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1979 cc->key + (i * subkey_size),
1980 subkey_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001981 else
1982 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i],
1983 cc->key + (i * subkey_size),
1984 subkey_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001985 if (r)
1986 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001987 }
1988
Milan Brozef43aa32017-01-04 20:23:54 +01001989 if (crypt_integrity_hmac(cc))
1990 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc));
1991
Andi Kleenc0297722011-01-13 19:59:53 +00001992 return err;
1993}
1994
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001995#ifdef CONFIG_KEYS
1996
Ondrej Kozina027c4312016-12-01 18:20:52 +01001997static bool contains_whitespace(const char *str)
1998{
1999 while (*str)
2000 if (isspace(*str++))
2001 return true;
2002 return false;
2003}
2004
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002005static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2006{
2007 char *new_key_string, *key_desc;
2008 int ret;
2009 struct key *key;
2010 const struct user_key_payload *ukp;
2011
Ondrej Kozina027c4312016-12-01 18:20:52 +01002012 /*
2013 * Reject key_string with whitespace. dm core currently lacks code for
2014 * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
2015 */
2016 if (contains_whitespace(key_string)) {
2017 DMERR("whitespace chars not allowed in key string");
2018 return -EINVAL;
2019 }
2020
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002021 /* look for next ':' separating key_type from key_description */
2022 key_desc = strpbrk(key_string, ":");
2023 if (!key_desc || key_desc == key_string || !strlen(key_desc + 1))
2024 return -EINVAL;
2025
2026 if (strncmp(key_string, "logon:", key_desc - key_string + 1) &&
2027 strncmp(key_string, "user:", key_desc - key_string + 1))
2028 return -EINVAL;
2029
2030 new_key_string = kstrdup(key_string, GFP_KERNEL);
2031 if (!new_key_string)
2032 return -ENOMEM;
2033
2034 key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user,
2035 key_desc + 1, NULL);
2036 if (IS_ERR(key)) {
2037 kzfree(new_key_string);
2038 return PTR_ERR(key);
2039 }
2040
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002041 down_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002042
David Howells0837e492017-03-01 15:11:23 +00002043 ukp = user_key_payload_locked(key);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002044 if (!ukp) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002045 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002046 key_put(key);
2047 kzfree(new_key_string);
2048 return -EKEYREVOKED;
2049 }
2050
2051 if (cc->key_size != ukp->datalen) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002052 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002053 key_put(key);
2054 kzfree(new_key_string);
2055 return -EINVAL;
2056 }
2057
2058 memcpy(cc->key, ukp->data, cc->key_size);
2059
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002060 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002061 key_put(key);
2062
2063 /* clear the flag since following operations may invalidate previously valid key */
2064 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2065
2066 ret = crypt_setkey(cc);
2067
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002068 if (!ret) {
2069 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2070 kzfree(cc->key_string);
2071 cc->key_string = new_key_string;
2072 } else
2073 kzfree(new_key_string);
2074
2075 return ret;
2076}
2077
2078static int get_key_size(char **key_string)
2079{
2080 char *colon, dummy;
2081 int ret;
2082
2083 if (*key_string[0] != ':')
2084 return strlen(*key_string) >> 1;
2085
2086 /* look for next ':' in key string */
2087 colon = strpbrk(*key_string + 1, ":");
2088 if (!colon)
2089 return -EINVAL;
2090
2091 if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':')
2092 return -EINVAL;
2093
2094 *key_string = colon;
2095
2096 /* remaining key string should be :<logon|user>:<key_desc> */
2097
2098 return ret;
2099}
2100
2101#else
2102
2103static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2104{
2105 return -EINVAL;
2106}
2107
2108static int get_key_size(char **key_string)
2109{
2110 return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1;
2111}
2112
2113#endif
2114
Milan Broze48d4bb2006-10-03 01:15:37 -07002115static int crypt_set_key(struct crypt_config *cc, char *key)
2116{
Milan Brozde8be5a2011-03-24 13:54:27 +00002117 int r = -EINVAL;
2118 int key_string_len = strlen(key);
2119
Milan Broz69a8cfc2011-01-13 19:59:49 +00002120 /* Hyphen (which gives a key_size of zero) means there is no key. */
2121 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00002122 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002123
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002124 /* ':' means the key is in kernel keyring, short-circuit normal key processing */
2125 if (key[0] == ':') {
2126 r = crypt_set_keyring_key(cc, key + 1);
2127 goto out;
2128 }
2129
Ondrej Kozina265e9092016-11-02 15:02:08 +01002130 /* clear the flag since following operations may invalidate previously valid key */
2131 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2132
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002133 /* wipe references to any kernel keyring key */
2134 kzfree(cc->key_string);
2135 cc->key_string = NULL;
2136
Andy Shevchenkoe944e032017-04-27 16:52:04 +03002137 /* Decode key from its hex representation. */
2138 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00002139 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002140
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002141 r = crypt_setkey(cc);
Ondrej Kozina265e9092016-11-02 15:02:08 +01002142 if (!r)
2143 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Milan Brozde8be5a2011-03-24 13:54:27 +00002144
2145out:
2146 /* Hex key string not needed after here, so wipe it. */
2147 memset(key, '0', key_string_len);
2148
2149 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002150}
2151
2152static int crypt_wipe_key(struct crypt_config *cc)
2153{
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002154 int r;
2155
Milan Broze48d4bb2006-10-03 01:15:37 -07002156 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002157 get_random_bytes(&cc->key, cc->key_size);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002158 kzfree(cc->key_string);
2159 cc->key_string = NULL;
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002160 r = crypt_setkey(cc);
2161 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00002162
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002163 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002164}
2165
Mikulas Patocka50593532017-08-13 22:45:08 -04002166static void crypt_calculate_pages_per_client(void)
2167{
2168 unsigned long pages = (totalram_pages - totalhigh_pages) * DM_CRYPT_MEMORY_PERCENT / 100;
2169
2170 if (!dm_crypt_clients_n)
2171 return;
2172
2173 pages /= dm_crypt_clients_n;
2174 if (pages < DM_CRYPT_MIN_PAGES_PER_CLIENT)
2175 pages = DM_CRYPT_MIN_PAGES_PER_CLIENT;
2176 dm_crypt_pages_per_client = pages;
2177}
2178
2179static void *crypt_page_alloc(gfp_t gfp_mask, void *pool_data)
2180{
2181 struct crypt_config *cc = pool_data;
2182 struct page *page;
2183
2184 if (unlikely(percpu_counter_compare(&cc->n_allocated_pages, dm_crypt_pages_per_client) >= 0) &&
2185 likely(gfp_mask & __GFP_NORETRY))
2186 return NULL;
2187
2188 page = alloc_page(gfp_mask);
2189 if (likely(page != NULL))
2190 percpu_counter_add(&cc->n_allocated_pages, 1);
2191
2192 return page;
2193}
2194
2195static void crypt_page_free(void *page, void *pool_data)
2196{
2197 struct crypt_config *cc = pool_data;
2198
2199 __free_page(page);
2200 percpu_counter_sub(&cc->n_allocated_pages, 1);
2201}
2202
Milan Broz28513fc2010-08-12 04:14:06 +01002203static void crypt_dtr(struct dm_target *ti)
2204{
2205 struct crypt_config *cc = ti->private;
2206
2207 ti->private = NULL;
2208
2209 if (!cc)
2210 return;
2211
Rabin Vincentf659b102016-09-21 16:22:29 +02002212 if (cc->write_thread)
Mikulas Patockadc267622015-02-13 08:25:59 -05002213 kthread_stop(cc->write_thread);
2214
Milan Broz28513fc2010-08-12 04:14:06 +01002215 if (cc->io_queue)
2216 destroy_workqueue(cc->io_queue);
2217 if (cc->crypt_queue)
2218 destroy_workqueue(cc->crypt_queue);
2219
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002220 crypt_free_tfms(cc);
2221
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002222 bioset_exit(&cc->bs);
Milan Broz28513fc2010-08-12 04:14:06 +01002223
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002224 mempool_exit(&cc->page_pool);
2225 mempool_exit(&cc->req_pool);
2226 mempool_exit(&cc->tag_pool);
2227
Kent Overstreetd00a11d2018-06-02 13:45:04 -04002228 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0);
2229 percpu_counter_destroy(&cc->n_allocated_pages);
2230
Milan Broz28513fc2010-08-12 04:14:06 +01002231 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
2232 cc->iv_gen_ops->dtr(cc);
2233
Milan Broz28513fc2010-08-12 04:14:06 +01002234 if (cc->dev)
2235 dm_put_device(ti, cc->dev);
2236
Milan Broz5ebaee62010-08-12 04:14:07 +01002237 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00002238 kzfree(cc->cipher_string);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002239 kzfree(cc->key_string);
Milan Brozef43aa32017-01-04 20:23:54 +01002240 kzfree(cc->cipher_auth);
2241 kzfree(cc->authenc_key);
Milan Broz28513fc2010-08-12 04:14:06 +01002242
Mike Snitzerd5ffebd2018-01-05 21:17:20 -05002243 mutex_destroy(&cc->bio_alloc_lock);
2244
Milan Broz28513fc2010-08-12 04:14:06 +01002245 /* Must zero key material before freeing */
2246 kzfree(cc);
Mikulas Patocka50593532017-08-13 22:45:08 -04002247
2248 spin_lock(&dm_crypt_clients_lock);
2249 WARN_ON(!dm_crypt_clients_n);
2250 dm_crypt_clients_n--;
2251 crypt_calculate_pages_per_client();
2252 spin_unlock(&dm_crypt_clients_lock);
Milan Broz28513fc2010-08-12 04:14:06 +01002253}
2254
Milan Broze889f972017-03-16 15:39:39 +01002255static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256{
Milan Broz5ebaee62010-08-12 04:14:07 +01002257 struct crypt_config *cc = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Milan Broz33d2f092017-03-16 15:39:40 +01002259 if (crypt_integrity_aead(cc))
Milan Broze889f972017-03-16 15:39:39 +01002260 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2261 else
2262 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Milan Broz5ebaee62010-08-12 04:14:07 +01002264 if (cc->iv_size)
2265 /* at least a 64 bit sector number should fit in our buffer */
2266 cc->iv_size = max(cc->iv_size,
2267 (unsigned int)(sizeof(u64) / sizeof(u8)));
2268 else if (ivmode) {
2269 DMWARN("Selected cipher does not support IVs");
2270 ivmode = NULL;
2271 }
2272
2273 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (ivmode == NULL)
2275 cc->iv_gen_ops = NULL;
2276 else if (strcmp(ivmode, "plain") == 0)
2277 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00002278 else if (strcmp(ivmode, "plain64") == 0)
2279 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Milan Broz7e3fd852017-06-06 09:07:01 +02002280 else if (strcmp(ivmode, "plain64be") == 0)
2281 cc->iv_gen_ops = &crypt_iv_plain64be_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 else if (strcmp(ivmode, "essiv") == 0)
2283 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10002284 else if (strcmp(ivmode, "benbi") == 0)
2285 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07002286 else if (strcmp(ivmode, "null") == 0)
2287 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00002288 else if (strcmp(ivmode, "lmk") == 0) {
2289 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01002290 /*
2291 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00002292 * to length of provided multi-key string.
2293 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01002294 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00002295 */
Milan Brozda31a072013-10-28 23:21:03 +01002296 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00002297 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01002298 cc->key_extra_size = cc->key_size / cc->key_parts;
2299 }
Milan Brozed04d982013-10-28 23:21:04 +01002300 } else if (strcmp(ivmode, "tcw") == 0) {
2301 cc->iv_gen_ops = &crypt_iv_tcw_ops;
2302 cc->key_parts += 2; /* IV + whitening */
2303 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broze889f972017-03-16 15:39:39 +01002304 } else if (strcmp(ivmode, "random") == 0) {
2305 cc->iv_gen_ops = &crypt_iv_random_ops;
2306 /* Need storage space in integrity fields. */
2307 cc->integrity_iv_size = cc->iv_size;
Milan Broz34745782011-01-13 19:59:55 +00002308 } else {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002309 ti->error = "Invalid IV mode";
Milan Broze889f972017-03-16 15:39:39 +01002310 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312
Milan Broze889f972017-03-16 15:39:39 +01002313 return 0;
2314}
2315
Milan Broz33d2f092017-03-16 15:39:40 +01002316/*
2317 * Workaround to parse cipher algorithm from crypto API spec.
2318 * The cc->cipher is currently used only in ESSIV.
2319 * This should be probably done by crypto-api calls (once available...)
2320 */
2321static int crypt_ctr_blkdev_cipher(struct crypt_config *cc)
2322{
2323 const char *alg_name = NULL;
2324 char *start, *end;
2325
2326 if (crypt_integrity_aead(cc)) {
2327 alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc)));
2328 if (!alg_name)
2329 return -EINVAL;
2330 if (crypt_integrity_hmac(cc)) {
2331 alg_name = strchr(alg_name, ',');
2332 if (!alg_name)
2333 return -EINVAL;
2334 }
2335 alg_name++;
2336 } else {
2337 alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc)));
2338 if (!alg_name)
2339 return -EINVAL;
2340 }
2341
2342 start = strchr(alg_name, '(');
2343 end = strchr(alg_name, ')');
2344
2345 if (!start && !end) {
2346 cc->cipher = kstrdup(alg_name, GFP_KERNEL);
2347 return cc->cipher ? 0 : -ENOMEM;
2348 }
2349
2350 if (!start || !end || ++start >= end)
2351 return -EINVAL;
2352
2353 cc->cipher = kzalloc(end - start + 1, GFP_KERNEL);
2354 if (!cc->cipher)
2355 return -ENOMEM;
2356
2357 strncpy(cc->cipher, start, end - start);
2358
2359 return 0;
2360}
2361
2362/*
2363 * Workaround to parse HMAC algorithm from AEAD crypto API spec.
2364 * The HMAC is needed to calculate tag size (HMAC digest size).
2365 * This should be probably done by crypto-api calls (once available...)
2366 */
2367static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
2368{
2369 char *start, *end, *mac_alg = NULL;
2370 struct crypto_ahash *mac;
2371
2372 if (!strstarts(cipher_api, "authenc("))
2373 return 0;
2374
2375 start = strchr(cipher_api, '(');
2376 end = strchr(cipher_api, ',');
2377 if (!start || !end || ++start > end)
2378 return -EINVAL;
2379
2380 mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
2381 if (!mac_alg)
2382 return -ENOMEM;
2383 strncpy(mac_alg, start, end - start);
2384
2385 mac = crypto_alloc_ahash(mac_alg, 0, 0);
2386 kfree(mac_alg);
2387
2388 if (IS_ERR(mac))
2389 return PTR_ERR(mac);
2390
2391 cc->key_mac_size = crypto_ahash_digestsize(mac);
2392 crypto_free_ahash(mac);
2393
2394 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
2395 if (!cc->authenc_key)
2396 return -ENOMEM;
2397
2398 return 0;
2399}
2400
2401static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key,
2402 char **ivmode, char **ivopts)
Milan Broz5ebaee62010-08-12 04:14:07 +01002403{
2404 struct crypt_config *cc = ti->private;
Milan Broz33d2f092017-03-16 15:39:40 +01002405 char *tmp, *cipher_api;
2406 int ret = -EINVAL;
2407
2408 cc->tfms_count = 1;
2409
2410 /*
2411 * New format (capi: prefix)
2412 * capi:cipher_api_spec-iv:ivopts
2413 */
2414 tmp = &cipher_in[strlen("capi:")];
2415 cipher_api = strsep(&tmp, "-");
2416 *ivmode = strsep(&tmp, ":");
2417 *ivopts = tmp;
2418
2419 if (*ivmode && !strcmp(*ivmode, "lmk"))
2420 cc->tfms_count = 64;
2421
2422 cc->key_parts = cc->tfms_count;
2423
2424 /* Allocate cipher */
2425 ret = crypt_alloc_tfms(cc, cipher_api);
2426 if (ret < 0) {
2427 ti->error = "Error allocating crypto tfm";
2428 return ret;
2429 }
2430
2431 /* Alloc AEAD, can be used only in new format. */
2432 if (crypt_integrity_aead(cc)) {
2433 ret = crypt_ctr_auth_cipher(cc, cipher_api);
2434 if (ret < 0) {
2435 ti->error = "Invalid AEAD cipher spec";
2436 return -ENOMEM;
2437 }
2438 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2439 } else
2440 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2441
2442 ret = crypt_ctr_blkdev_cipher(cc);
2443 if (ret < 0) {
2444 ti->error = "Cannot allocate cipher string";
2445 return -ENOMEM;
2446 }
2447
2448 return 0;
2449}
2450
2451static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
2452 char **ivmode, char **ivopts)
2453{
2454 struct crypt_config *cc = ti->private;
2455 char *tmp, *cipher, *chainmode, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01002456 char *cipher_api = NULL;
2457 int ret = -EINVAL;
2458 char dummy;
2459
Milan Broz33d2f092017-03-16 15:39:40 +01002460 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002461 ti->error = "Bad cipher specification";
2462 return -EINVAL;
2463 }
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 /*
Milan Broz5ebaee62010-08-12 04:14:07 +01002466 * Legacy dm-crypt cipher specification
2467 * cipher[:keycount]-mode-iv:ivopts
2468 */
2469 tmp = cipher_in;
2470 keycount = strsep(&tmp, "-");
2471 cipher = strsep(&keycount, ":");
2472
Milan Broz69a8cfc2011-01-13 19:59:49 +00002473 if (!keycount)
Milan Broz5ebaee62010-08-12 04:14:07 +01002474 cc->tfms_count = 1;
2475 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
2476 !is_power_of_2(cc->tfms_count)) {
2477 ti->error = "Bad cipher key count specification";
2478 return -EINVAL;
2479 }
Milan Broz28513fc2010-08-12 04:14:06 +01002480 cc->key_parts = cc->tfms_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002482 cc->cipher = kstrdup(cipher, GFP_KERNEL);
Milan Broz28513fc2010-08-12 04:14:06 +01002483 if (!cc->cipher)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 goto bad_mem;
2485
Milan Brozddd42ed2008-02-08 02:11:07 +00002486 chainmode = strsep(&tmp, "-");
Milan Broz33d2f092017-03-16 15:39:40 +01002487 *ivopts = strsep(&tmp, "-");
2488 *ivmode = strsep(&*ivopts, ":");
Andi Kleenc0297722011-01-13 19:59:53 +00002489
Milan Broz3a7f6c92008-02-08 02:11:14 +00002490 if (tmp)
Milan Brozddd42ed2008-02-08 02:11:07 +00002491 DMWARN("Ignoring unexpected additional cipher options");
2492
2493 /*
2494 * For compatibility with the original dm-crypt mapping format, if
2495 * only the cipher name is supplied, use cbc-plain.
Milan Broz28513fc2010-08-12 04:14:06 +01002496 */
Milan Broz33d2f092017-03-16 15:39:40 +01002497 if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002498 chainmode = "cbc";
Milan Broz33d2f092017-03-16 15:39:40 +01002499 *ivmode = "plain";
Milan Brozcabf08e2007-10-19 22:38:58 +01002500 }
2501
Milan Broz33d2f092017-03-16 15:39:40 +01002502 if (strcmp(chainmode, "ecb") && !*ivmode) {
Andi Kleenc0297722011-01-13 19:59:53 +00002503 ti->error = "IV mechanism required";
2504 return -EINVAL;
2505 }
2506
Milan Brozcabf08e2007-10-19 22:38:58 +01002507 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
Milan Broz9934a8b2007-10-19 22:38:57 +01002508 if (!cipher_api)
Milan Broz28513fc2010-08-12 04:14:06 +01002509 goto bad_mem;
Milan Broz9934a8b2007-10-19 22:38:57 +01002510
2511 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002512 "%s(%s)", chainmode, cipher);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (ret < 0) {
2514 kfree(cipher_api);
Milan Broz28513fc2010-08-12 04:14:06 +01002515 goto bad_mem;
2516 }
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 /* Allocate cipher */
2519 ret = crypt_alloc_tfms(cc, cipher_api);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if (ret < 0) {
2521 ti->error = "Error allocating crypto tfm";
Milan Broz33d2f092017-03-16 15:39:40 +01002522 kfree(cipher_api);
2523 return ret;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002524 }
Jeffy Chenbd86e322017-09-27 20:28:57 +08002525 kfree(cipher_api);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002526
Milan Broz33d2f092017-03-16 15:39:40 +01002527 return 0;
2528bad_mem:
2529 ti->error = "Cannot allocate cipher strings";
2530 return -ENOMEM;
2531}
2532
2533static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key)
2534{
2535 struct crypt_config *cc = ti->private;
2536 char *ivmode = NULL, *ivopts = NULL;
2537 int ret;
2538
2539 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
2540 if (!cc->cipher_string) {
2541 ti->error = "Cannot allocate cipher strings";
2542 return -ENOMEM;
2543 }
2544
2545 if (strstarts(cipher_in, "capi:"))
2546 ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts);
2547 else
2548 ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts);
2549 if (ret)
2550 return ret;
2551
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002552 /* Initialize IV */
Milan Broze889f972017-03-16 15:39:39 +01002553 ret = crypt_ctr_ivmode(ti, ivmode);
2554 if (ret < 0)
Milan Broz33d2f092017-03-16 15:39:40 +01002555 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Milan Brozda31a072013-10-28 23:21:03 +01002557 /* Initialize and set key */
2558 ret = crypt_set_key(cc, key);
2559 if (ret < 0) {
2560 ti->error = "Error decoding and setting key";
Milan Broz33d2f092017-03-16 15:39:40 +01002561 return ret;
Milan Brozda31a072013-10-28 23:21:03 +01002562 }
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 /* Allocate IV */
2565 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
2566 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
2567 if (ret < 0) {
2568 ti->error = "Error creating IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002569 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
2571 }
2572
2573 /* Initialize IV (set keys for ESSIV etc) */
2574 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
2575 ret = cc->iv_gen_ops->init(cc);
2576 if (ret < 0) {
2577 ti->error = "Error initialising IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002578 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
2580 }
2581
Ondrej Kozinadc949022018-01-12 16:30:32 +01002582 /* wipe the kernel key payload copy */
2583 if (cc->key_string)
2584 memset(cc->key, 0, cc->key_size * sizeof(u8));
2585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Milan Brozef43aa32017-01-04 20:23:54 +01002589static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
2590{
2591 struct crypt_config *cc = ti->private;
2592 struct dm_arg_set as;
Eric Biggers5916a222017-06-22 11:32:45 -07002593 static const struct dm_arg _args[] = {
Milan Broz8f0009a2017-03-16 15:39:44 +01002594 {0, 6, "Invalid number of feature args"},
Milan Brozef43aa32017-01-04 20:23:54 +01002595 };
2596 unsigned int opt_params, val;
2597 const char *opt_string, *sval;
Milan Broz8f0009a2017-03-16 15:39:44 +01002598 char dummy;
Milan Brozef43aa32017-01-04 20:23:54 +01002599 int ret;
2600
2601 /* Optional parameters */
2602 as.argc = argc;
2603 as.argv = argv;
2604
2605 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2606 if (ret)
2607 return ret;
2608
2609 while (opt_params--) {
2610 opt_string = dm_shift_arg(&as);
2611 if (!opt_string) {
2612 ti->error = "Not enough feature arguments";
2613 return -EINVAL;
2614 }
2615
2616 if (!strcasecmp(opt_string, "allow_discards"))
2617 ti->num_discard_bios = 1;
2618
2619 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
2620 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
2621
2622 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
2623 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
2624 else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
2625 if (val == 0 || val > MAX_TAG_SIZE) {
2626 ti->error = "Invalid integrity arguments";
2627 return -EINVAL;
2628 }
2629 cc->on_disk_tag_size = val;
2630 sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
2631 if (!strcasecmp(sval, "aead")) {
2632 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002633 } else if (strcasecmp(sval, "none")) {
2634 ti->error = "Unknown integrity profile";
2635 return -EINVAL;
2636 }
2637
2638 cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
2639 if (!cc->cipher_auth)
2640 return -ENOMEM;
Mikulas Patockaff3af922017-03-23 10:23:14 -04002641 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002642 if (cc->sector_size < (1 << SECTOR_SHIFT) ||
2643 cc->sector_size > 4096 ||
Mikulas Patockaff3af922017-03-23 10:23:14 -04002644 (cc->sector_size & (cc->sector_size - 1))) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002645 ti->error = "Invalid feature value for sector_size";
2646 return -EINVAL;
2647 }
Milan Broz783874b2017-09-13 15:45:56 +02002648 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) {
2649 ti->error = "Device size is not multiple of sector_size feature";
2650 return -EINVAL;
2651 }
Mikulas Patockaff3af922017-03-23 10:23:14 -04002652 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT;
Milan Broz8f0009a2017-03-16 15:39:44 +01002653 } else if (!strcasecmp(opt_string, "iv_large_sectors"))
2654 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
2655 else {
Milan Brozef43aa32017-01-04 20:23:54 +01002656 ti->error = "Invalid feature arguments";
2657 return -EINVAL;
2658 }
2659 }
2660
2661 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662}
2663
2664/*
2665 * Construct an encryption mapping:
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002666 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 */
2668static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2669{
2670 struct crypt_config *cc;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002671 int key_size;
Milan Brozef43aa32017-01-04 20:23:54 +01002672 unsigned int align_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 unsigned long long tmpll;
2674 int ret;
Milan Brozef43aa32017-01-04 20:23:54 +01002675 size_t iv_size_padding, additional_req_size;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002676 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Milan Broz772ae5f2011-08-02 12:32:08 +01002678 if (argc < 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 ti->error = "Not enough arguments";
2680 return -EINVAL;
2681 }
2682
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002683 key_size = get_key_size(&argv[1]);
2684 if (key_size < 0) {
2685 ti->error = "Cannot parse key size";
2686 return -EINVAL;
2687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
2689 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
2690 if (!cc) {
2691 ti->error = "Cannot allocate encryption context";
2692 return -ENOMEM;
2693 }
2694 cc->key_size = key_size;
Milan Broz8f0009a2017-03-16 15:39:44 +01002695 cc->sector_size = (1 << SECTOR_SHIFT);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002696 cc->sector_shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698 ti->private = cc;
Milan Brozef43aa32017-01-04 20:23:54 +01002699
Mikulas Patocka50593532017-08-13 22:45:08 -04002700 spin_lock(&dm_crypt_clients_lock);
2701 dm_crypt_clients_n++;
2702 crypt_calculate_pages_per_client();
2703 spin_unlock(&dm_crypt_clients_lock);
2704
2705 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL);
2706 if (ret < 0)
2707 goto bad;
2708
Milan Brozef43aa32017-01-04 20:23:54 +01002709 /* Optional parameters need to be read before cipher constructor */
2710 if (argc > 5) {
2711 ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
2712 if (ret)
2713 goto bad;
2714 }
2715
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
2717 if (ret < 0)
2718 goto bad;
2719
Milan Broz33d2f092017-03-16 15:39:40 +01002720 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +01002721 cc->dmreq_start = sizeof(struct aead_request);
2722 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
2723 align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
2724 } else {
2725 cc->dmreq_start = sizeof(struct skcipher_request);
2726 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
2727 align_mask = crypto_skcipher_alignmask(any_tfm(cc));
2728 }
Mikulas Patockad49ec522014-08-28 11:09:31 -04002729 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
2730
Milan Brozef43aa32017-01-04 20:23:54 +01002731 if (align_mask < CRYPTO_MINALIGN) {
Mikulas Patockad49ec522014-08-28 11:09:31 -04002732 /* Allocate the padding exactly */
2733 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
Milan Brozef43aa32017-01-04 20:23:54 +01002734 & align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002735 } else {
2736 /*
2737 * If the cipher requires greater alignment than kmalloc
2738 * alignment, we don't know the exact position of the
2739 * initialization vector. We must assume worst case.
2740 */
Milan Brozef43aa32017-01-04 20:23:54 +01002741 iv_size_padding = align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Milan Brozef43aa32017-01-04 20:23:54 +01002744 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
2745 additional_req_size = sizeof(struct dm_crypt_request) +
2746 iv_size_padding + cc->iv_size +
2747 cc->iv_size +
2748 sizeof(uint64_t) +
2749 sizeof(unsigned int);
2750
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002751 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size);
2752 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 ti->error = "Cannot allocate crypt request mempool";
2754 goto bad;
2755 }
2756
Mike Snitzer30187e12016-01-31 13:28:26 -05002757 cc->per_bio_data_size = ti->per_io_data_size =
Milan Brozef43aa32017-01-04 20:23:54 +01002758 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
Mikulas Patockad49ec522014-08-28 11:09:31 -04002759 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002760
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002761 ret = mempool_init(&cc->page_pool, BIO_MAX_PAGES, crypt_page_alloc, crypt_page_free, cc);
2762 if (ret) {
Milan Broz8b004452006-10-03 01:15:37 -07002763 ti->error = "Cannot allocate page mempool";
Milan Broze48d4bb2006-10-03 01:15:37 -07002764 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002766
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002767 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS);
2768 if (ret) {
Milan Broz0c395b02008-02-08 02:10:54 +00002769 ti->error = "Cannot allocate crypt bioset";
Milan Brozcabf08e2007-10-19 22:38:58 +01002770 goto bad;
Milan Broz93e605c2006-10-03 01:15:38 -07002771 }
Milan Brozcabf08e2007-10-19 22:38:58 +01002772
Mikulas Patocka7145c242015-02-13 08:24:41 -05002773 mutex_init(&cc->bio_alloc_lock);
2774
Milan Brozcabf08e2007-10-19 22:38:58 +01002775 ret = -EINVAL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002776 if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
2777 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002778 ti->error = "Invalid iv_offset sector";
2779 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002781 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Vivek Goyale80d1c82015-07-31 09:20:36 -04002783 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
2784 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 ti->error = "Device lookup failed";
2786 goto bad;
2787 }
2788
Vivek Goyale80d1c82015-07-31 09:20:36 -04002789 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002790 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 ti->error = "Invalid device sector";
2792 goto bad;
2793 }
2794 cc->start = tmpll;
2795
Milan Broz33d2f092017-03-16 15:39:40 +01002796 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
Milan Brozef43aa32017-01-04 20:23:54 +01002797 ret = crypt_integrity_ctr(cc, ti);
Milan Broz772ae5f2011-08-02 12:32:08 +01002798 if (ret)
2799 goto bad;
2800
Milan Brozef43aa32017-01-04 20:23:54 +01002801 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
2802 if (!cc->tag_pool_max_sectors)
2803 cc->tag_pool_max_sectors = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01002804
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002805 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS,
Milan Brozef43aa32017-01-04 20:23:54 +01002806 cc->tag_pool_max_sectors * cc->on_disk_tag_size);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002807 if (ret) {
Milan Brozef43aa32017-01-04 20:23:54 +01002808 ti->error = "Cannot allocate integrity tags mempool";
2809 goto bad;
Milan Broz772ae5f2011-08-02 12:32:08 +01002810 }
Mikulas Patocka583fe742017-04-18 16:51:54 -04002811
2812 cc->tag_pool_max_sectors <<= cc->sector_shift;
Milan Broz772ae5f2011-08-02 12:32:08 +01002813 }
2814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 ret = -ENOMEM;
Tim Murraya1b89132017-04-21 11:11:36 +02002816 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if (!cc->io_queue) {
2818 ti->error = "Couldn't create kcryptd io queue";
2819 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 }
Christophe Saout37af6562006-10-30 20:39:08 +01002821
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002822 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
Tim Murraya1b89132017-04-21 11:11:36 +02002823 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002824 else
Tim Murraya1b89132017-04-21 11:11:36 +02002825 cc->crypt_queue = alloc_workqueue("kcryptd",
2826 WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002827 num_online_cpus());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 if (!cc->crypt_queue) {
2829 ti->error = "Couldn't create kcryptd queue";
2830 goto bad;
2831 }
2832
Mikulas Patockadc267622015-02-13 08:25:59 -05002833 init_waitqueue_head(&cc->write_thread_wait);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05002834 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05002835
2836 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write");
2837 if (IS_ERR(cc->write_thread)) {
2838 ret = PTR_ERR(cc->write_thread);
2839 cc->write_thread = NULL;
2840 ti->error = "Couldn't spawn write thread";
2841 goto bad;
2842 }
2843 wake_up_process(cc->write_thread);
2844
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002845 ti->num_flush_bios = 1;
Milan Broz983c7db2011-09-25 23:26:21 +01002846
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return 0;
2848
2849bad:
2850 crypt_dtr(ti);
2851 return ret;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002852}
2853
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002854static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01002857 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002858
Milan Broz772ae5f2011-08-02 12:32:08 +01002859 /*
Mike Christie28a8f0d2016-06-05 14:32:25 -05002860 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
2861 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
Mike Christiee6047142016-06-05 14:32:04 -05002862 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
Milan Broz772ae5f2011-08-02 12:32:08 +01002863 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06002864 if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
Mike Christie28a8f0d2016-06-05 14:32:25 -05002865 bio_op(bio) == REQ_OP_DISCARD)) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02002866 bio_set_dev(bio, cc->dev->bdev);
Milan Broz772ae5f2011-08-02 12:32:08 +01002867 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07002868 bio->bi_iter.bi_sector = cc->start +
2869 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002870 return DM_MAPIO_REMAPPED;
2871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002873 /*
2874 * Check if bio is too large, split as needed.
2875 */
2876 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
Milan Brozef43aa32017-01-04 20:23:54 +01002877 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002878 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
2879
Milan Broz8f0009a2017-03-16 15:39:44 +01002880 /*
2881 * Ensure that bio is a multiple of internal sector encryption size
2882 * and is aligned to this size as defined in IO hints.
2883 */
2884 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
Christoph Hellwig846785e2017-06-03 09:38:02 +02002885 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002886
2887 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
Christoph Hellwig846785e2017-06-03 09:38:02 +02002888 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002889
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002890 io = dm_per_bio_data(bio, cc->per_bio_data_size);
2891 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Milan Brozef43aa32017-01-04 20:23:54 +01002892
2893 if (cc->on_disk_tag_size) {
Mikulas Patocka583fe742017-04-18 16:51:54 -04002894 unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift);
Milan Brozef43aa32017-01-04 20:23:54 +01002895
2896 if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
Mikulas Patocka583fe742017-04-18 16:51:54 -04002897 unlikely(!(io->integrity_metadata = kmalloc(tag_len,
Milan Brozef43aa32017-01-04 20:23:54 +01002898 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
2899 if (bio_sectors(bio) > cc->tag_pool_max_sectors)
2900 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002901 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO);
Milan Brozef43aa32017-01-04 20:23:54 +01002902 io->integrity_metadata_from_pool = true;
2903 }
2904 }
2905
Milan Broz33d2f092017-03-16 15:39:40 +01002906 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002907 io->ctx.r.req_aead = (struct aead_request *)(io + 1);
2908 else
2909 io->ctx.r.req = (struct skcipher_request *)(io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Milan Broz20c82532011-01-13 19:59:53 +00002911 if (bio_data_dir(io->base_bio) == READ) {
2912 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05002913 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00002914 } else
Andrew Morton4ee218c2006-03-27 01:17:48 -08002915 kcryptd_queue_crypt(io);
2916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 return DM_MAPIO_SUBMITTED;
2918}
2919
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002920static void crypt_status(struct dm_target *ti, status_type_t type,
2921 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
Milan Broz5ebaee62010-08-12 04:14:07 +01002923 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002924 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002925 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
2927 switch (type) {
2928 case STATUSTYPE_INFO:
2929 result[0] = '\0';
2930 break;
2931
2932 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00002933 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002935 if (cc->key_size > 0) {
2936 if (cc->key_string)
2937 DMEMIT(":%u:%s", cc->key_size, cc->key_string);
2938 else
2939 for (i = 0; i < cc->key_size; i++)
2940 DMEMIT("%02x", cc->key[i]);
2941 } else
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002942 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
2944 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
2945 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01002946
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002947 num_feature_args += !!ti->num_discard_bios;
2948 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002949 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002950 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT);
Milan Broz8f0009a2017-03-16 15:39:44 +01002951 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002952 if (cc->on_disk_tag_size)
2953 num_feature_args++;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002954 if (num_feature_args) {
2955 DMEMIT(" %d", num_feature_args);
2956 if (ti->num_discard_bios)
2957 DMEMIT(" allow_discards");
2958 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2959 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002960 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
2961 DMEMIT(" submit_from_crypt_cpus");
Milan Brozef43aa32017-01-04 20:23:54 +01002962 if (cc->on_disk_tag_size)
2963 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
Milan Broz8f0009a2017-03-16 15:39:44 +01002964 if (cc->sector_size != (1 << SECTOR_SHIFT))
2965 DMEMIT(" sector_size:%d", cc->sector_size);
2966 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
2967 DMEMIT(" iv_large_sectors");
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002968 }
Milan Broz772ae5f2011-08-02 12:32:08 +01002969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 break;
2971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
2973
Milan Broze48d4bb2006-10-03 01:15:37 -07002974static void crypt_postsuspend(struct dm_target *ti)
2975{
2976 struct crypt_config *cc = ti->private;
2977
2978 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2979}
2980
2981static int crypt_preresume(struct dm_target *ti)
2982{
2983 struct crypt_config *cc = ti->private;
2984
2985 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
2986 DMERR("aborting resume - crypt key is not set.");
2987 return -EAGAIN;
2988 }
2989
2990 return 0;
2991}
2992
2993static void crypt_resume(struct dm_target *ti)
2994{
2995 struct crypt_config *cc = ti->private;
2996
2997 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2998}
2999
3000/* Message interface
3001 * key set <key>
3002 * key wipe
3003 */
Mike Snitzer1eb5fa82018-02-28 15:59:59 -05003004static int crypt_message(struct dm_target *ti, unsigned argc, char **argv,
3005 char *result, unsigned maxlen)
Milan Broze48d4bb2006-10-03 01:15:37 -07003006{
3007 struct crypt_config *cc = ti->private;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01003008 int key_size, ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07003009
3010 if (argc < 2)
3011 goto error;
3012
Mike Snitzer498f0102011-08-02 12:32:04 +01003013 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07003014 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
3015 DMWARN("not suspended during key manipulation.");
3016 return -EINVAL;
3017 }
Mike Snitzer498f0102011-08-02 12:32:04 +01003018 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01003019 /* The key size may not be changed. */
3020 key_size = get_key_size(&argv[2]);
3021 if (key_size < 0 || cc->key_size != key_size) {
3022 memset(argv[2], '0', strlen(argv[2]));
3023 return -EINVAL;
3024 }
3025
Milan Broz542da312009-12-10 23:51:57 +00003026 ret = crypt_set_key(cc, argv[2]);
3027 if (ret)
3028 return ret;
3029 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
3030 ret = cc->iv_gen_ops->init(cc);
Ondrej Kozinadc949022018-01-12 16:30:32 +01003031 /* wipe the kernel key payload copy */
3032 if (cc->key_string)
3033 memset(cc->key, 0, cc->key_size * sizeof(u8));
Milan Broz542da312009-12-10 23:51:57 +00003034 return ret;
3035 }
Mike Snitzer498f0102011-08-02 12:32:04 +01003036 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00003037 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
3038 ret = cc->iv_gen_ops->wipe(cc);
3039 if (ret)
3040 return ret;
3041 }
Milan Broze48d4bb2006-10-03 01:15:37 -07003042 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00003043 }
Milan Broze48d4bb2006-10-03 01:15:37 -07003044 }
3045
3046error:
3047 DMWARN("unrecognised message received.");
3048 return -EINVAL;
3049}
3050
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003051static int crypt_iterate_devices(struct dm_target *ti,
3052 iterate_devices_callout_fn fn, void *data)
3053{
3054 struct crypt_config *cc = ti->private;
3055
Mike Snitzer5dea2712009-07-23 20:30:42 +01003056 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003057}
3058
Mike Snitzer586b2862015-09-09 21:34:51 -04003059static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
3060{
Milan Broz8f0009a2017-03-16 15:39:44 +01003061 struct crypt_config *cc = ti->private;
3062
Mike Snitzer586b2862015-09-09 21:34:51 -04003063 /*
3064 * Unfortunate constraint that is required to avoid the potential
3065 * for exceeding underlying device's max_segments limits -- due to
3066 * crypt_alloc_buffer() possibly allocating pages for the encryption
3067 * bio that are not as physically contiguous as the original bio.
3068 */
3069 limits->max_segment_size = PAGE_SIZE;
Milan Broz8f0009a2017-03-16 15:39:44 +01003070
3071 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
3072 limits->logical_block_size = cc->sector_size;
3073 limits->physical_block_size = cc->sector_size;
3074 blk_limits_io_min(limits, cc->sector_size);
3075 }
Mike Snitzer586b2862015-09-09 21:34:51 -04003076}
3077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078static struct target_type crypt_target = {
3079 .name = "crypt",
Ondrej Kozinadc949022018-01-12 16:30:32 +01003080 .version = {1, 18, 1},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 .module = THIS_MODULE,
3082 .ctr = crypt_ctr,
3083 .dtr = crypt_dtr,
3084 .map = crypt_map,
3085 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07003086 .postsuspend = crypt_postsuspend,
3087 .preresume = crypt_preresume,
3088 .resume = crypt_resume,
3089 .message = crypt_message,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003090 .iterate_devices = crypt_iterate_devices,
Mike Snitzer586b2862015-09-09 21:34:51 -04003091 .io_hints = crypt_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092};
3093
3094static int __init dm_crypt_init(void)
3095{
3096 int r;
3097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05003099 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07003100 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 return r;
3103}
3104
3105static void __exit dm_crypt_exit(void)
3106{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00003107 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
3110module_init(dm_crypt_init);
3111module_exit(dm_crypt_exit);
3112
Jana Saoutbf142992014-06-24 14:27:04 -04003113MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
3115MODULE_LICENSE("GPL");