blob: 4cc3809b2a3a414eba277b12e392bedcf2685b2e [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 Broz2dc5327d32011-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 Broz2dc5327d32011-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
142 /*
Milan Brozef43aa32017-01-04 20:23:54 +0100143 * pool for per bio private data, crypto requests,
144 * encryption requeusts/buffer pages and integrity tags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 */
Milan Brozddd42ed2008-02-08 02:11:07 +0000146 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 mempool_t *page_pool;
Milan Brozef43aa32017-01-04 20:23:54 +0100148 mempool_t *tag_pool;
149 unsigned tag_pool_max_sectors;
150
Milan Broz6a24c712006-10-03 01:15:40 -0700151 struct bio_set *bs;
Mikulas Patocka7145c242015-02-13 08:24:41 -0500152 struct mutex bio_alloc_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Milan Brozcabf08e2007-10-19 22:38:58 +0100154 struct workqueue_struct *io_queue;
155 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700156
Mikulas Patockadc267622015-02-13 08:25:59 -0500157 struct task_struct *write_thread;
158 wait_queue_head_t write_thread_wait;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -0500159 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -0500160
Milan Broz5ebaee62010-08-12 04:14:07 +0100161 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000162 char *cipher_string;
Milan Brozef43aa32017-01-04 20:23:54 +0100163 char *cipher_auth;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +0100164 char *key_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100165
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100166 const struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800167 union {
Milan Broz60473592009-12-10 23:51:55 +0000168 struct iv_essiv_private essiv;
169 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000170 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100171 struct iv_tcw_private tcw;
Herbert Xu79066ad2006-12-05 13:41:52 -0800172 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 sector_t iv_offset;
174 unsigned int iv_size;
Mikulas Patockaff3af922017-03-23 10:23:14 -0400175 unsigned short int sector_size;
176 unsigned char sector_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100178 /* ESSIV: struct crypto_cipher *essiv_tfm */
179 void *iv_private;
Milan Brozef43aa32017-01-04 20:23:54 +0100180 union {
181 struct crypto_skcipher **tfms;
182 struct crypto_aead **tfms_aead;
183 } cipher_tfm;
Milan Brozd1f96422011-01-13 19:59:54 +0000184 unsigned tfms_count;
Milan Brozef43aa32017-01-04 20:23:54 +0100185 unsigned long cipher_flags;
Andi Kleenc0297722011-01-13 19:59:53 +0000186
187 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000188 * Layout of each crypto request:
189 *
Herbert Xubbdb23b2016-01-24 21:16:36 +0800190 * struct skcipher_request
Milan Brozddd42ed2008-02-08 02:11:07 +0000191 * context
192 * padding
193 * struct dm_crypt_request
194 * padding
195 * IV
196 *
197 * The padding is added so that dm_crypt_request and the IV are
198 * correctly aligned.
199 */
200 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000201
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400202 unsigned int per_bio_data_size;
203
Milan Broze48d4bb2006-10-03 01:15:37 -0700204 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100206 unsigned int key_parts; /* independent parts in key buffer */
207 unsigned int key_extra_size; /* additional keys length */
Milan Brozef43aa32017-01-04 20:23:54 +0100208 unsigned int key_mac_size; /* MAC key size for authenc(...) */
209
210 unsigned int integrity_tag_size;
211 unsigned int integrity_iv_size;
212 unsigned int on_disk_tag_size;
213
214 u8 *authenc_key; /* space for keys in authenc() format (if used) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 u8 key[0];
216};
217
Milan Brozef43aa32017-01-04 20:23:54 +0100218#define MIN_IOS 64
219#define MAX_TAG_SIZE 480
220#define POOL_ENTRY_SIZE 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100222static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000223static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Brozef43aa32017-01-04 20:23:54 +0100224static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
225 struct scatterlist *sg);
Olaf Kirch027581f2007-05-09 02:32:52 -0700226
Andi Kleenc0297722011-01-13 19:59:53 +0000227/*
Eric Biggers86f917a2017-03-30 22:18:48 -0700228 * Use this to access cipher attributes that are independent of the key.
Andi Kleenc0297722011-01-13 19:59:53 +0000229 */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800230static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +0000231{
Milan Brozef43aa32017-01-04 20:23:54 +0100232 return cc->cipher_tfm.tfms[0];
233}
234
235static struct crypto_aead *any_tfm_aead(struct crypt_config *cc)
236{
237 return cc->cipher_tfm.tfms_aead[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * Different IV generation algorithms:
242 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000243 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200244 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 *
Milan Broz61afef62009-12-10 23:52:25 +0000246 * plain64: the initial vector is the 64-bit little-endian version of the sector
247 * number, padded with zeros if necessary.
248 *
Milan Broz7e3fd852017-06-06 09:07:01 +0200249 * plain64be: the initial vector is the 64-bit big-endian version of the sector
250 * number, padded with zeros if necessary.
251 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000252 * essiv: "encrypted sector|salt initial vector", the sector number is
253 * encrypted with the bulk cipher using a salt as key. The salt
254 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *
Rik Snel48527fa2006-09-03 08:56:39 +1000256 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
257 * (needed for LRW-32-AES and possible other narrow block modes)
258 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700259 * null: the initial vector is always zero. Provides compatibility with
260 * obsolete loop_fish2 devices. Do not use for new devices.
261 *
Milan Broz34745782011-01-13 19:59:55 +0000262 * lmk: Compatible implementation of the block chaining mode used
263 * by the Loop-AES block device encryption system
264 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
265 * It operates on full 512 byte sectors and uses CBC
266 * with an IV derived from the sector number, the data and
267 * optionally extra IV seed.
268 * This means that after decryption the first block
269 * of sector must be tweaked according to decrypted data.
270 * Loop-AES can use three encryption schemes:
271 * version 1: is plain aes-cbc mode
272 * version 2: uses 64 multikey scheme with lmk IV generator
273 * version 3: the same as version 2 with additional IV seed
274 * (it uses 65 keys, last key is used as IV seed)
275 *
Milan Brozed04d982013-10-28 23:21:04 +0100276 * tcw: Compatible implementation of the block chaining mode used
277 * by the TrueCrypt device encryption system (prior to version 4.1).
Milan Broze44f23b2015-04-05 18:03:10 +0200278 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
Milan Brozed04d982013-10-28 23:21:04 +0100279 * It operates on full 512 byte sectors and uses CBC
280 * with an IV derived from initial key and the sector number.
281 * In addition, whitening value is applied on every sector, whitening
282 * is calculated from initial key, sector number and mixed using CRC32.
283 * Note that this encryption scheme is vulnerable to watermarking attacks
284 * and should be used for old compatible containers access only.
285 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * plumb: unimplemented, see:
287 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
288 */
289
Milan Broz2dc5327d32011-01-13 19:59:54 +0000290static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
291 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100294 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 return 0;
297}
298
Milan Broz61afef62009-12-10 23:52:25 +0000299static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc5327d32011-01-13 19:59:54 +0000300 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000301{
302 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100303 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000304
305 return 0;
306}
307
Milan Broz7e3fd852017-06-06 09:07:01 +0200308static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv,
309 struct dm_crypt_request *dmreq)
310{
311 memset(iv, 0, cc->iv_size);
312 /* iv_size is at least of size u64; usually it is 16 bytes */
313 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector);
314
315 return 0;
316}
317
Milan Brozb95bf2d2009-12-10 23:51:56 +0000318/* Initialise ESSIV - compute salt but no local memory allocations */
319static int crypt_iv_essiv_init(struct crypt_config *cc)
320{
321 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800322 AHASH_REQUEST_ON_STACK(req, essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000323 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000324 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100325 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000326
327 sg_init_one(&sg, cc->key, cc->key_size);
Herbert Xubbdb23b2016-01-24 21:16:36 +0800328 ahash_request_set_tfm(req, essiv->hash_tfm);
329 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
330 ahash_request_set_crypt(req, &sg, essiv->salt, cc->key_size);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000331
Herbert Xubbdb23b2016-01-24 21:16:36 +0800332 err = crypto_ahash_digest(req);
333 ahash_request_zero(req);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000334 if (err)
335 return err;
336
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100337 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000338
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100339 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800340 crypto_ahash_digestsize(essiv->hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100341 if (err)
342 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000343
344 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000345}
346
Milan Broz542da312009-12-10 23:51:57 +0000347/* Wipe salt and reset key derived from volume key */
348static int crypt_iv_essiv_wipe(struct crypt_config *cc)
349{
350 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800351 unsigned salt_size = crypto_ahash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000352 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100353 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000354
355 memset(essiv->salt, 0, salt_size);
356
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100357 essiv_tfm = cc->iv_private;
358 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
359 if (r)
360 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000361
362 return err;
363}
364
Eric Biggers86f917a2017-03-30 22:18:48 -0700365/* Allocate the cipher for ESSIV */
366static struct crypto_cipher *alloc_essiv_cipher(struct crypt_config *cc,
367 struct dm_target *ti,
368 const u8 *salt,
369 unsigned int saltsize)
Andi Kleenc0297722011-01-13 19:59:53 +0000370{
371 struct crypto_cipher *essiv_tfm;
372 int err;
373
374 /* Setup the essiv_tfm with the given salt */
375 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
376 if (IS_ERR(essiv_tfm)) {
377 ti->error = "Error allocating crypto tfm for ESSIV";
378 return essiv_tfm;
379 }
380
Milan Brozef43aa32017-01-04 20:23:54 +0100381 if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) {
Andi Kleenc0297722011-01-13 19:59:53 +0000382 ti->error = "Block size of ESSIV cipher does "
383 "not match IV size of block cipher";
384 crypto_free_cipher(essiv_tfm);
385 return ERR_PTR(-EINVAL);
386 }
387
388 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
389 if (err) {
390 ti->error = "Failed to set key for ESSIV cipher";
391 crypto_free_cipher(essiv_tfm);
392 return ERR_PTR(err);
393 }
394
395 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000396}
397
Milan Broz60473592009-12-10 23:51:55 +0000398static void crypt_iv_essiv_dtr(struct crypt_config *cc)
399{
Andi Kleenc0297722011-01-13 19:59:53 +0000400 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000401 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
402
Herbert Xubbdb23b2016-01-24 21:16:36 +0800403 crypto_free_ahash(essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000404 essiv->hash_tfm = NULL;
405
406 kzfree(essiv->salt);
407 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000408
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100409 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000410
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100411 if (essiv_tfm)
412 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000413
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100414 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000415}
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100418 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Milan Broz5861f1b2009-12-10 23:51:56 +0000420 struct crypto_cipher *essiv_tfm = NULL;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800421 struct crypto_ahash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000422 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100423 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Milan Broz5861f1b2009-12-10 23:51:56 +0000425 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700426 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -EINVAL;
428 }
429
Milan Brozb95bf2d2009-12-10 23:51:56 +0000430 /* Allocate hash algorithm */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800431 hash_tfm = crypto_alloc_ahash(opts, 0, CRYPTO_ALG_ASYNC);
Herbert Xu35058682006-08-24 19:10:20 +1000432 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700433 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000434 err = PTR_ERR(hash_tfm);
435 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437
Herbert Xubbdb23b2016-01-24 21:16:36 +0800438 salt = kzalloc(crypto_ahash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000439 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700440 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000441 err = -ENOMEM;
442 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
Milan Brozb95bf2d2009-12-10 23:51:56 +0000445 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000446 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
447
Eric Biggers86f917a2017-03-30 22:18:48 -0700448 essiv_tfm = alloc_essiv_cipher(cc, ti, salt,
449 crypto_ahash_digestsize(hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100450 if (IS_ERR(essiv_tfm)) {
451 crypt_iv_essiv_dtr(cc);
452 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000453 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100454 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000457
458bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000459 if (hash_tfm && !IS_ERR(hash_tfm))
Herbert Xubbdb23b2016-01-24 21:16:36 +0800460 crypto_free_ahash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000461 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000462 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Milan Broz2dc5327d32011-01-13 19:59:54 +0000465static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
466 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100468 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100471 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000472 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return 0;
475}
476
Rik Snel48527fa2006-09-03 08:56:39 +1000477static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
478 const char *opts)
479{
Herbert Xubbdb23b2016-01-24 21:16:36 +0800480 unsigned bs = crypto_skcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800481 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000482
483 /* we need to calculate how far we must shift the sector count
484 * to get the cipher block count, we use this shift in _gen */
485
486 if (1 << log != bs) {
487 ti->error = "cypher blocksize is not a power of 2";
488 return -EINVAL;
489 }
490
491 if (log > 9) {
492 ti->error = "cypher blocksize is > 512";
493 return -EINVAL;
494 }
495
Milan Broz60473592009-12-10 23:51:55 +0000496 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000497
498 return 0;
499}
500
501static void crypt_iv_benbi_dtr(struct crypt_config *cc)
502{
Rik Snel48527fa2006-09-03 08:56:39 +1000503}
504
Milan Broz2dc5327d32011-01-13 19:59:54 +0000505static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
506 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000507{
Herbert Xu79066ad2006-12-05 13:41:52 -0800508 __be64 val;
509
Rik Snel48527fa2006-09-03 08:56:39 +1000510 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800511
Milan Broz2dc5327d32011-01-13 19:59:54 +0000512 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800513 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return 0;
516}
517
Milan Broz2dc5327d32011-01-13 19:59:54 +0000518static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
519 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700520{
521 memset(iv, 0, cc->iv_size);
522
523 return 0;
524}
525
Milan Broz34745782011-01-13 19:59:55 +0000526static void crypt_iv_lmk_dtr(struct crypt_config *cc)
527{
528 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
529
530 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
531 crypto_free_shash(lmk->hash_tfm);
532 lmk->hash_tfm = NULL;
533
534 kzfree(lmk->seed);
535 lmk->seed = NULL;
536}
537
538static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
539 const char *opts)
540{
541 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
542
Milan Broz8f0009a2017-03-16 15:39:44 +0100543 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
544 ti->error = "Unsupported sector size for LMK";
545 return -EINVAL;
546 }
547
Milan Broz34745782011-01-13 19:59:55 +0000548 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
549 if (IS_ERR(lmk->hash_tfm)) {
550 ti->error = "Error initializing LMK hash";
551 return PTR_ERR(lmk->hash_tfm);
552 }
553
554 /* No seed in LMK version 2 */
555 if (cc->key_parts == cc->tfms_count) {
556 lmk->seed = NULL;
557 return 0;
558 }
559
560 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
561 if (!lmk->seed) {
562 crypt_iv_lmk_dtr(cc);
563 ti->error = "Error kmallocing seed storage in LMK";
564 return -ENOMEM;
565 }
566
567 return 0;
568}
569
570static int crypt_iv_lmk_init(struct crypt_config *cc)
571{
572 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
573 int subkey_size = cc->key_size / cc->key_parts;
574
575 /* LMK seed is on the position of LMK_KEYS + 1 key */
576 if (lmk->seed)
577 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
578 crypto_shash_digestsize(lmk->hash_tfm));
579
580 return 0;
581}
582
583static int crypt_iv_lmk_wipe(struct crypt_config *cc)
584{
585 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
586
587 if (lmk->seed)
588 memset(lmk->seed, 0, LMK_SEED_SIZE);
589
590 return 0;
591}
592
593static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
594 struct dm_crypt_request *dmreq,
595 u8 *data)
596{
597 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200598 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000599 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100600 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000601 int i, r;
602
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200603 desc->tfm = lmk->hash_tfm;
604 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Broz34745782011-01-13 19:59:55 +0000605
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200606 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000607 if (r)
608 return r;
609
610 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200611 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000612 if (r)
613 return r;
614 }
615
616 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200617 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000618 if (r)
619 return r;
620
621 /* Sector is cropped to 56 bits here */
622 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
623 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
624 buf[2] = cpu_to_le32(4024);
625 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200626 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000627 if (r)
628 return r;
629
630 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200631 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000632 if (r)
633 return r;
634
635 for (i = 0; i < MD5_HASH_WORDS; i++)
636 __cpu_to_le32s(&md5state.hash[i]);
637 memcpy(iv, &md5state.hash, cc->iv_size);
638
639 return 0;
640}
641
642static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
643 struct dm_crypt_request *dmreq)
644{
Milan Brozef43aa32017-01-04 20:23:54 +0100645 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000646 u8 *src;
647 int r = 0;
648
649 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100650 sg = crypt_get_sg_data(cc, dmreq->sg_in);
651 src = kmap_atomic(sg_page(sg));
652 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800653 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000654 } else
655 memset(iv, 0, cc->iv_size);
656
657 return r;
658}
659
660static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
661 struct dm_crypt_request *dmreq)
662{
Milan Brozef43aa32017-01-04 20:23:54 +0100663 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000664 u8 *dst;
665 int r;
666
667 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
668 return 0;
669
Milan Brozef43aa32017-01-04 20:23:54 +0100670 sg = crypt_get_sg_data(cc, dmreq->sg_out);
671 dst = kmap_atomic(sg_page(sg));
672 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset);
Milan Broz34745782011-01-13 19:59:55 +0000673
674 /* Tweak the first block of plaintext sector */
675 if (!r)
Milan Brozef43aa32017-01-04 20:23:54 +0100676 crypto_xor(dst + sg->offset, iv, cc->iv_size);
Milan Broz34745782011-01-13 19:59:55 +0000677
Cong Wangc2e022c2011-11-28 13:26:02 +0800678 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000679 return r;
680}
681
Milan Brozed04d982013-10-28 23:21:04 +0100682static void crypt_iv_tcw_dtr(struct crypt_config *cc)
683{
684 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
685
686 kzfree(tcw->iv_seed);
687 tcw->iv_seed = NULL;
688 kzfree(tcw->whitening);
689 tcw->whitening = NULL;
690
691 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
692 crypto_free_shash(tcw->crc32_tfm);
693 tcw->crc32_tfm = NULL;
694}
695
696static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
697 const char *opts)
698{
699 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
700
Milan Broz8f0009a2017-03-16 15:39:44 +0100701 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
702 ti->error = "Unsupported sector size for TCW";
703 return -EINVAL;
704 }
705
Milan Brozed04d982013-10-28 23:21:04 +0100706 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
707 ti->error = "Wrong key size for TCW";
708 return -EINVAL;
709 }
710
711 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
712 if (IS_ERR(tcw->crc32_tfm)) {
713 ti->error = "Error initializing CRC32 in TCW";
714 return PTR_ERR(tcw->crc32_tfm);
715 }
716
717 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
718 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
719 if (!tcw->iv_seed || !tcw->whitening) {
720 crypt_iv_tcw_dtr(cc);
721 ti->error = "Error allocating seed storage in TCW";
722 return -ENOMEM;
723 }
724
725 return 0;
726}
727
728static int crypt_iv_tcw_init(struct crypt_config *cc)
729{
730 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
731 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
732
733 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
734 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
735 TCW_WHITENING_SIZE);
736
737 return 0;
738}
739
740static int crypt_iv_tcw_wipe(struct crypt_config *cc)
741{
742 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
743
744 memset(tcw->iv_seed, 0, cc->iv_size);
745 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
746
747 return 0;
748}
749
750static int crypt_iv_tcw_whitening(struct crypt_config *cc,
751 struct dm_crypt_request *dmreq,
752 u8 *data)
753{
754 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200755 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100756 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200757 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100758 int i, r;
759
760 /* xor whitening with sector number */
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100761 crypto_xor_cpy(buf, tcw->whitening, (u8 *)&sector, 8);
762 crypto_xor_cpy(&buf[8], tcw->whitening + 8, (u8 *)&sector, 8);
Milan Brozed04d982013-10-28 23:21:04 +0100763
764 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200765 desc->tfm = tcw->crc32_tfm;
766 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Brozed04d982013-10-28 23:21:04 +0100767 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200768 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100769 if (r)
770 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200771 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100772 if (r)
773 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200774 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100775 if (r)
776 goto out;
777 }
778 crypto_xor(&buf[0], &buf[12], 4);
779 crypto_xor(&buf[4], &buf[8], 4);
780
781 /* apply whitening (8 bytes) to whole sector */
782 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
783 crypto_xor(data + i * 8, buf, 8);
784out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100785 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100786 return r;
787}
788
789static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
790 struct dm_crypt_request *dmreq)
791{
Milan Brozef43aa32017-01-04 20:23:54 +0100792 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100793 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200794 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100795 u8 *src;
796 int r = 0;
797
798 /* Remove whitening from ciphertext */
799 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100800 sg = crypt_get_sg_data(cc, dmreq->sg_in);
801 src = kmap_atomic(sg_page(sg));
802 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100803 kunmap_atomic(src);
804 }
805
806 /* Calculate IV */
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100807 crypto_xor_cpy(iv, tcw->iv_seed, (u8 *)&sector, 8);
Milan Brozed04d982013-10-28 23:21:04 +0100808 if (cc->iv_size > 8)
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100809 crypto_xor_cpy(&iv[8], tcw->iv_seed + 8, (u8 *)&sector,
810 cc->iv_size - 8);
Milan Brozed04d982013-10-28 23:21:04 +0100811
812 return r;
813}
814
815static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
816 struct dm_crypt_request *dmreq)
817{
Milan Brozef43aa32017-01-04 20:23:54 +0100818 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100819 u8 *dst;
820 int r;
821
822 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
823 return 0;
824
825 /* Apply whitening on ciphertext */
Milan Brozef43aa32017-01-04 20:23:54 +0100826 sg = crypt_get_sg_data(cc, dmreq->sg_out);
827 dst = kmap_atomic(sg_page(sg));
828 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100829 kunmap_atomic(dst);
830
831 return r;
832}
833
Milan Brozef43aa32017-01-04 20:23:54 +0100834static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv,
835 struct dm_crypt_request *dmreq)
836{
837 /* Used only for writes, there must be an additional space to store IV */
838 get_random_bytes(iv, cc->iv_size);
839 return 0;
840}
841
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100842static const struct crypt_iv_operations crypt_iv_plain_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 .generator = crypt_iv_plain_gen
844};
845
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100846static const struct crypt_iv_operations crypt_iv_plain64_ops = {
Milan Broz61afef62009-12-10 23:52:25 +0000847 .generator = crypt_iv_plain64_gen
848};
849
Milan Broz7e3fd852017-06-06 09:07:01 +0200850static const struct crypt_iv_operations crypt_iv_plain64be_ops = {
851 .generator = crypt_iv_plain64be_gen
852};
853
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100854static const struct crypt_iv_operations crypt_iv_essiv_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 .ctr = crypt_iv_essiv_ctr,
856 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000857 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000858 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 .generator = crypt_iv_essiv_gen
860};
861
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100862static const struct crypt_iv_operations crypt_iv_benbi_ops = {
Rik Snel48527fa2006-09-03 08:56:39 +1000863 .ctr = crypt_iv_benbi_ctr,
864 .dtr = crypt_iv_benbi_dtr,
865 .generator = crypt_iv_benbi_gen
866};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100868static const struct crypt_iv_operations crypt_iv_null_ops = {
Ludwig Nussel46b47732007-05-09 02:32:55 -0700869 .generator = crypt_iv_null_gen
870};
871
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100872static const struct crypt_iv_operations crypt_iv_lmk_ops = {
Milan Broz34745782011-01-13 19:59:55 +0000873 .ctr = crypt_iv_lmk_ctr,
874 .dtr = crypt_iv_lmk_dtr,
875 .init = crypt_iv_lmk_init,
876 .wipe = crypt_iv_lmk_wipe,
877 .generator = crypt_iv_lmk_gen,
878 .post = crypt_iv_lmk_post
879};
880
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100881static const struct crypt_iv_operations crypt_iv_tcw_ops = {
Milan Brozed04d982013-10-28 23:21:04 +0100882 .ctr = crypt_iv_tcw_ctr,
883 .dtr = crypt_iv_tcw_dtr,
884 .init = crypt_iv_tcw_init,
885 .wipe = crypt_iv_tcw_wipe,
886 .generator = crypt_iv_tcw_gen,
887 .post = crypt_iv_tcw_post
888};
889
Milan Brozef43aa32017-01-04 20:23:54 +0100890static struct crypt_iv_operations crypt_iv_random_ops = {
891 .generator = crypt_iv_random_gen
892};
893
894/*
895 * Integrity extensions
896 */
897static bool crypt_integrity_aead(struct crypt_config *cc)
898{
899 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
900}
901
902static bool crypt_integrity_hmac(struct crypt_config *cc)
903{
Milan Broz33d2f092017-03-16 15:39:40 +0100904 return crypt_integrity_aead(cc) && cc->key_mac_size;
Milan Brozef43aa32017-01-04 20:23:54 +0100905}
906
907/* Get sg containing data */
908static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
909 struct scatterlist *sg)
910{
Milan Broz33d2f092017-03-16 15:39:40 +0100911 if (unlikely(crypt_integrity_aead(cc)))
Milan Brozef43aa32017-01-04 20:23:54 +0100912 return &sg[2];
913
914 return sg;
915}
916
917static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
918{
919 struct bio_integrity_payload *bip;
920 unsigned int tag_len;
921 int ret;
922
923 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size)
924 return 0;
925
926 bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
927 if (IS_ERR(bip))
928 return PTR_ERR(bip);
929
930 tag_len = io->cc->on_disk_tag_size * bio_sectors(bio);
931
932 bip->bip_iter.bi_size = tag_len;
933 bip->bip_iter.bi_sector = io->cc->start + io->sector;
934
Milan Brozef43aa32017-01-04 20:23:54 +0100935 ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
936 tag_len, offset_in_page(io->integrity_metadata));
937 if (unlikely(ret != tag_len))
938 return -ENOMEM;
939
940 return 0;
941}
942
943static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
944{
945#ifdef CONFIG_BLK_DEV_INTEGRITY
946 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk);
947
948 /* From now we require underlying device with our integrity profile */
949 if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) {
950 ti->error = "Integrity profile not supported.";
951 return -EINVAL;
952 }
953
Mikulas Patocka583fe742017-04-18 16:51:54 -0400954 if (bi->tag_size != cc->on_disk_tag_size ||
955 bi->tuple_size != cc->on_disk_tag_size) {
Milan Brozef43aa32017-01-04 20:23:54 +0100956 ti->error = "Integrity profile tag size mismatch.";
957 return -EINVAL;
958 }
Mikulas Patocka583fe742017-04-18 16:51:54 -0400959 if (1 << bi->interval_exp != cc->sector_size) {
960 ti->error = "Integrity profile sector size mismatch.";
961 return -EINVAL;
962 }
Milan Brozef43aa32017-01-04 20:23:54 +0100963
Milan Broz33d2f092017-03-16 15:39:40 +0100964 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +0100965 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size;
966 DMINFO("Integrity AEAD, tag size %u, IV size %u.",
967 cc->integrity_tag_size, cc->integrity_iv_size);
968
969 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) {
970 ti->error = "Integrity AEAD auth tag size is not supported.";
971 return -EINVAL;
972 }
973 } else if (cc->integrity_iv_size)
974 DMINFO("Additional per-sector space %u bytes for IV.",
975 cc->integrity_iv_size);
976
977 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) {
978 ti->error = "Not enough space for integrity tag in the profile.";
979 return -EINVAL;
980 }
981
982 return 0;
983#else
984 ti->error = "Integrity profile not supported.";
985 return -EINVAL;
986#endif
987}
988
Milan Brozd469f842007-10-19 22:42:37 +0100989static void crypt_convert_init(struct crypt_config *cc,
990 struct convert_context *ctx,
991 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000992 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 ctx->bio_in = bio_in;
995 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700996 if (bio_in)
997 ctx->iter_in = bio_in->bi_iter;
998 if (bio_out)
999 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +01001000 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +00001001 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
Huang Yingb2174ee2009-03-16 17:44:33 +00001004static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
Milan Brozef43aa32017-01-04 20:23:54 +01001005 void *req)
Huang Yingb2174ee2009-03-16 17:44:33 +00001006{
1007 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
1008}
1009
Milan Brozef43aa32017-01-04 20:23:54 +01001010static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq)
Huang Yingb2174ee2009-03-16 17:44:33 +00001011{
Milan Brozef43aa32017-01-04 20:23:54 +01001012 return (void *)((char *)dmreq - cc->dmreq_start);
Huang Yingb2174ee2009-03-16 17:44:33 +00001013}
1014
Milan Broz2dc5327d32011-01-13 19:59:54 +00001015static u8 *iv_of_dmreq(struct crypt_config *cc,
1016 struct dm_crypt_request *dmreq)
1017{
Milan Broz33d2f092017-03-16 15:39:40 +01001018 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001019 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1020 crypto_aead_alignmask(any_tfm_aead(cc)) + 1);
1021 else
1022 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1023 crypto_skcipher_alignmask(any_tfm(cc)) + 1);
Milan Broz2dc5327d32011-01-13 19:59:54 +00001024}
1025
Milan Brozef43aa32017-01-04 20:23:54 +01001026static u8 *org_iv_of_dmreq(struct crypt_config *cc,
1027 struct dm_crypt_request *dmreq)
1028{
1029 return iv_of_dmreq(cc, dmreq) + cc->iv_size;
1030}
1031
1032static uint64_t *org_sector_of_dmreq(struct crypt_config *cc,
1033 struct dm_crypt_request *dmreq)
1034{
1035 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size;
1036 return (uint64_t*) ptr;
1037}
1038
1039static unsigned int *org_tag_of_dmreq(struct crypt_config *cc,
1040 struct dm_crypt_request *dmreq)
1041{
1042 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size +
1043 cc->iv_size + sizeof(uint64_t);
1044 return (unsigned int*)ptr;
1045}
1046
1047static void *tag_from_dmreq(struct crypt_config *cc,
1048 struct dm_crypt_request *dmreq)
1049{
1050 struct convert_context *ctx = dmreq->ctx;
1051 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1052
1053 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) *
1054 cc->on_disk_tag_size];
1055}
1056
1057static void *iv_tag_from_dmreq(struct crypt_config *cc,
1058 struct dm_crypt_request *dmreq)
1059{
1060 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size;
1061}
1062
1063static int crypt_convert_block_aead(struct crypt_config *cc,
1064 struct convert_context *ctx,
1065 struct aead_request *req,
1066 unsigned int tag_offset)
Milan Broz01482b72008-02-08 02:11:04 +00001067{
Kent Overstreet003b5c52013-10-11 15:45:43 -07001068 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1069 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001070 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001071 u8 *iv, *org_iv, *tag_iv, *tag;
1072 uint64_t *sector;
1073 int r = 0;
1074
1075 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size);
Milan Broz01482b72008-02-08 02:11:04 +00001076
Milan Broz8f0009a2017-03-16 15:39:44 +01001077 /* Reject unexpected unaligned bio. */
Mikulas Patocka0440d5c2017-11-07 10:35:57 -05001078 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Broz8f0009a2017-03-16 15:39:44 +01001079 return -EIO;
Milan Broz01482b72008-02-08 02:11:04 +00001080
Huang Yingb2174ee2009-03-16 17:44:33 +00001081 dmreq = dmreq_of_req(cc, req);
Mikulas Patockac66029f2012-07-27 15:08:05 +01001082 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001083 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001084 dmreq->iv_sector >>= cc->sector_shift;
Huang Yingb2174ee2009-03-16 17:44:33 +00001085 dmreq->ctx = ctx;
Milan Broz01482b72008-02-08 02:11:04 +00001086
Milan Brozef43aa32017-01-04 20:23:54 +01001087 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
Milan Broz01482b72008-02-08 02:11:04 +00001088
Milan Brozef43aa32017-01-04 20:23:54 +01001089 sector = org_sector_of_dmreq(cc, dmreq);
1090 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1091
1092 iv = iv_of_dmreq(cc, dmreq);
1093 org_iv = org_iv_of_dmreq(cc, dmreq);
1094 tag = tag_from_dmreq(cc, dmreq);
1095 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1096
1097 /* AEAD request:
1098 * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
1099 * | (authenticated) | (auth+encryption) | |
1100 * | sector_LE | IV | sector in/out | tag in/out |
1101 */
1102 sg_init_table(dmreq->sg_in, 4);
1103 sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t));
1104 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001105 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 +01001106 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size);
1107
1108 sg_init_table(dmreq->sg_out, 4);
1109 sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t));
1110 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001111 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 +01001112 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size);
Milan Broz01482b72008-02-08 02:11:04 +00001113
Milan Broz3a7f6c92008-02-08 02:11:14 +00001114 if (cc->iv_gen_ops) {
Milan Brozef43aa32017-01-04 20:23:54 +01001115 /* For READs use IV stored in integrity metadata */
1116 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1117 memcpy(org_iv, tag_iv, cc->iv_size);
1118 } else {
1119 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1120 if (r < 0)
1121 return r;
1122 /* Store generated IV in integrity metadata */
1123 if (cc->integrity_iv_size)
1124 memcpy(tag_iv, org_iv, cc->iv_size);
1125 }
1126 /* Working copy of IV, to be modified in crypto API */
1127 memcpy(iv, org_iv, cc->iv_size);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001128 }
1129
Milan Brozef43aa32017-01-04 20:23:54 +01001130 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size);
1131 if (bio_data_dir(ctx->bio_in) == WRITE) {
1132 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001133 cc->sector_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001134 r = crypto_aead_encrypt(req);
1135 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size)
1136 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0,
1137 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size));
1138 } else {
1139 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001140 cc->sector_size + cc->integrity_tag_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001141 r = crypto_aead_decrypt(req);
1142 }
1143
1144 if (r == -EBADMSG)
1145 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1146 (unsigned long long)le64_to_cpu(*sector));
1147
1148 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
1149 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1150
Milan Broz8f0009a2017-03-16 15:39:44 +01001151 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1152 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001153
1154 return r;
1155}
1156
1157static int crypt_convert_block_skcipher(struct crypt_config *cc,
1158 struct convert_context *ctx,
1159 struct skcipher_request *req,
1160 unsigned int tag_offset)
1161{
1162 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1163 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
1164 struct scatterlist *sg_in, *sg_out;
1165 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001166 u8 *iv, *org_iv, *tag_iv;
1167 uint64_t *sector;
1168 int r = 0;
1169
Milan Broz8f0009a2017-03-16 15:39:44 +01001170 /* Reject unexpected unaligned bio. */
Mikulas Patocka0440d5c2017-11-07 10:35:57 -05001171 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Broz8f0009a2017-03-16 15:39:44 +01001172 return -EIO;
1173
Milan Brozef43aa32017-01-04 20:23:54 +01001174 dmreq = dmreq_of_req(cc, req);
1175 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001176 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001177 dmreq->iv_sector >>= cc->sector_shift;
Milan Brozef43aa32017-01-04 20:23:54 +01001178 dmreq->ctx = ctx;
1179
1180 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
1181
1182 iv = iv_of_dmreq(cc, dmreq);
1183 org_iv = org_iv_of_dmreq(cc, dmreq);
1184 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1185
1186 sector = org_sector_of_dmreq(cc, dmreq);
1187 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1188
1189 /* For skcipher we use only the first sg item */
1190 sg_in = &dmreq->sg_in[0];
1191 sg_out = &dmreq->sg_out[0];
1192
1193 sg_init_table(sg_in, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001194 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001195
1196 sg_init_table(sg_out, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001197 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001198
1199 if (cc->iv_gen_ops) {
1200 /* For READs use IV stored in integrity metadata */
1201 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1202 memcpy(org_iv, tag_iv, cc->integrity_iv_size);
1203 } else {
1204 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1205 if (r < 0)
1206 return r;
1207 /* Store generated IV in integrity metadata */
1208 if (cc->integrity_iv_size)
1209 memcpy(tag_iv, org_iv, cc->integrity_iv_size);
1210 }
1211 /* Working copy of IV, to be modified in crypto API */
1212 memcpy(iv, org_iv, cc->iv_size);
1213 }
1214
Milan Broz8f0009a2017-03-16 15:39:44 +01001215 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001216
1217 if (bio_data_dir(ctx->bio_in) == WRITE)
Herbert Xubbdb23b2016-01-24 21:16:36 +08001218 r = crypto_skcipher_encrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001219 else
Herbert Xubbdb23b2016-01-24 21:16:36 +08001220 r = crypto_skcipher_decrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001221
Milan Broz2dc5327d32011-01-13 19:59:54 +00001222 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001223 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1224
Milan Broz8f0009a2017-03-16 15:39:44 +01001225 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1226 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Broz2dc5327d32011-01-13 19:59:54 +00001227
Milan Broz3a7f6c92008-02-08 02:11:14 +00001228 return r;
Milan Broz01482b72008-02-08 02:11:04 +00001229}
1230
Milan Broz95497a92008-02-08 02:11:12 +00001231static void kcryptd_async_done(struct crypto_async_request *async_req,
1232 int error);
Andi Kleenc0297722011-01-13 19:59:53 +00001233
Milan Brozef43aa32017-01-04 20:23:54 +01001234static void crypt_alloc_req_skcipher(struct crypt_config *cc,
1235 struct convert_context *ctx)
Milan Brozddd42ed2008-02-08 02:11:07 +00001236{
Mikulas Patockac66029f2012-07-27 15:08:05 +01001237 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +00001238
Milan Brozef43aa32017-01-04 20:23:54 +01001239 if (!ctx->r.req)
1240 ctx->r.req = mempool_alloc(cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +00001241
Milan Brozef43aa32017-01-04 20:23:54 +01001242 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]);
Milan Broz54cea3f2015-05-15 17:00:25 +02001243
1244 /*
1245 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1246 * requests if driver request queue is full.
1247 */
Milan Brozef43aa32017-01-04 20:23:54 +01001248 skcipher_request_set_callback(ctx->r.req,
Andi Kleenc0297722011-01-13 19:59:53 +00001249 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Milan Brozef43aa32017-01-04 20:23:54 +01001250 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req));
Milan Brozddd42ed2008-02-08 02:11:07 +00001251}
1252
Milan Brozef43aa32017-01-04 20:23:54 +01001253static void crypt_alloc_req_aead(struct crypt_config *cc,
1254 struct convert_context *ctx)
1255{
1256 if (!ctx->r.req_aead)
1257 ctx->r.req_aead = mempool_alloc(cc->req_pool, GFP_NOIO);
1258
1259 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]);
1260
1261 /*
1262 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1263 * requests if driver request queue is full.
1264 */
1265 aead_request_set_callback(ctx->r.req_aead,
1266 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
1267 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead));
1268}
1269
1270static void crypt_alloc_req(struct crypt_config *cc,
1271 struct convert_context *ctx)
1272{
Milan Broz33d2f092017-03-16 15:39:40 +01001273 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001274 crypt_alloc_req_aead(cc, ctx);
1275 else
1276 crypt_alloc_req_skcipher(cc, ctx);
1277}
1278
1279static void crypt_free_req_skcipher(struct crypt_config *cc,
1280 struct skcipher_request *req, struct bio *base_bio)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001281{
1282 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1283
Herbert Xubbdb23b2016-01-24 21:16:36 +08001284 if ((struct skcipher_request *)(io + 1) != req)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001285 mempool_free(req, cc->req_pool);
1286}
1287
Milan Brozef43aa32017-01-04 20:23:54 +01001288static void crypt_free_req_aead(struct crypt_config *cc,
1289 struct aead_request *req, struct bio *base_bio)
1290{
1291 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1292
1293 if ((struct aead_request *)(io + 1) != req)
1294 mempool_free(req, cc->req_pool);
1295}
1296
1297static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio)
1298{
Milan Broz33d2f092017-03-16 15:39:40 +01001299 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001300 crypt_free_req_aead(cc, req, base_bio);
1301 else
1302 crypt_free_req_skcipher(cc, req, base_bio);
1303}
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305/*
1306 * Encrypt / decrypt data from one bio to another one (can be the same one)
1307 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001308static blk_status_t crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +01001309 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Milan Brozef43aa32017-01-04 20:23:54 +01001311 unsigned int tag_offset = 0;
Mikulas Patockaff3af922017-03-23 10:23:14 -04001312 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT;
Milan Broz3f1e9072008-03-28 14:16:07 -07001313 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Mikulas Patocka40b62292012-07-27 15:08:04 +01001315 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +01001316
Kent Overstreet003b5c52013-10-11 15:45:43 -07001317 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Milan Broz3a7f6c92008-02-08 02:11:14 +00001319 crypt_alloc_req(cc, ctx);
Mikulas Patocka40b62292012-07-27 15:08:04 +01001320 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -07001321
Milan Broz33d2f092017-03-16 15:39:40 +01001322 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001323 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset);
1324 else
1325 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001326
1327 switch (r) {
Milan Broz54cea3f2015-05-15 17:00:25 +02001328 /*
1329 * The request was queued by a crypto driver
1330 * but the driver request queue is full, let's wait.
1331 */
Milan Broz3a7f6c92008-02-08 02:11:14 +00001332 case -EBUSY:
1333 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -08001334 reinit_completion(&ctx->restart);
Milan Broz54cea3f2015-05-15 17:00:25 +02001335 /* fall through */
1336 /*
1337 * The request is queued and processed asynchronously,
1338 * completion function kcryptd_async_done() will be called.
1339 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001340 case -EINPROGRESS:
Milan Brozef43aa32017-01-04 20:23:54 +01001341 ctx->r.req = NULL;
Milan Broz8f0009a2017-03-16 15:39:44 +01001342 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001343 tag_offset++;
Milan Broz3a7f6c92008-02-08 02:11:14 +00001344 continue;
Milan Broz54cea3f2015-05-15 17:00:25 +02001345 /*
1346 * The request was already processed (synchronously).
1347 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001348 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001349 atomic_dec(&ctx->cc_pending);
Milan Broz8f0009a2017-03-16 15:39:44 +01001350 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001351 tag_offset++;
Milan Brozc7f1b202008-07-02 09:34:28 +01001352 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -07001353 continue;
Milan Brozef43aa32017-01-04 20:23:54 +01001354 /*
1355 * There was a data integrity error.
1356 */
1357 case -EBADMSG:
1358 atomic_dec(&ctx->cc_pending);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001359 return BLK_STS_PROTECTION;
Milan Brozef43aa32017-01-04 20:23:54 +01001360 /*
1361 * There was an error while processing the request.
1362 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001363 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001364 atomic_dec(&ctx->cc_pending);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001365 return BLK_STS_IOERR;
Milan Broz3f1e9072008-03-28 14:16:07 -07001366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368
Milan Broz3f1e9072008-03-28 14:16:07 -07001369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001372static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374/*
1375 * Generate a new unfragmented bio with the given size
Mike Snitzer586b2862015-09-09 21:34:51 -04001376 * This should never violate the device limitations (but only because
1377 * max_segment_size is being constrained to PAGE_SIZE).
Mikulas Patocka7145c242015-02-13 08:24:41 -05001378 *
1379 * This function may be called concurrently. If we allocate from the mempool
1380 * concurrently, there is a possibility of deadlock. For example, if we have
1381 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1382 * the mempool concurrently, it may deadlock in a situation where both processes
1383 * have allocated 128 pages and the mempool is exhausted.
1384 *
1385 * In order to avoid this scenario we allocate the pages under a mutex.
1386 *
1387 * In order to not degrade performance with excessive locking, we try
1388 * non-blocking allocations without a mutex first but on failure we fallback
1389 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001391static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001393 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001394 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001396 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
1397 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +00001398 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Mikulas Patocka7145c242015-02-13 08:24:41 -05001400retry:
Mel Gormand0164ad2015-11-06 16:28:21 -08001401 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001402 mutex_lock(&cc->bio_alloc_lock);
1403
Olaf Kirch2f9941b2007-05-09 02:32:53 -07001404 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -07001405 if (!clone)
Milan Brozef43aa32017-01-04 20:23:54 +01001406 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Olaf Kirch027581f2007-05-09 02:32:52 -07001408 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -07001409
Mikulas Patocka7145c242015-02-13 08:24:41 -05001410 remaining_size = size;
1411
Olaf Kirchf97380b2007-05-09 02:32:54 -07001412 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +00001413 page = mempool_alloc(cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -05001414 if (!page) {
1415 crypt_free_buffer_pages(cc, clone);
1416 bio_put(clone);
Mel Gormand0164ad2015-11-06 16:28:21 -08001417 gfp_mask |= __GFP_DIRECT_RECLAIM;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001418 goto retry;
1419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Mikulas Patocka7145c242015-02-13 08:24:41 -05001421 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Ming Lei0dae7fe2016-10-29 16:08:06 +08001423 bio_add_page(clone, page, len, 0);
Milan Broz91e10622007-12-13 14:16:10 +00001424
Mikulas Patocka7145c242015-02-13 08:24:41 -05001425 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 }
1427
Milan Brozef43aa32017-01-04 20:23:54 +01001428 /* Allocate space for integrity tags */
1429 if (dm_crypt_integrity_io_alloc(io, clone)) {
1430 crypt_free_buffer_pages(cc, clone);
1431 bio_put(clone);
1432 clone = NULL;
1433 }
1434out:
Mel Gormand0164ad2015-11-06 16:28:21 -08001435 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001436 mutex_unlock(&cc->bio_alloc_lock);
1437
Milan Broz8b004452006-10-03 01:15:37 -07001438 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Neil Brown644bd2f2007-10-16 13:48:46 +02001441static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Neil Brown644bd2f2007-10-16 13:48:46 +02001443 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 struct bio_vec *bv;
1445
Kent Overstreetcb34e052012-09-05 15:22:02 -07001446 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 BUG_ON(!bv->bv_page);
1448 mempool_free(bv->bv_page, cc->page_pool);
1449 bv->bv_page = NULL;
1450 }
1451}
1452
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001453static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1454 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001455{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001456 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001457 io->base_bio = bio;
1458 io->sector = sector;
1459 io->error = 0;
Milan Brozef43aa32017-01-04 20:23:54 +01001460 io->ctx.r.req = NULL;
1461 io->integrity_metadata = NULL;
1462 io->integrity_metadata_from_pool = false;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001463 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001464}
1465
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001466static void crypt_inc_pending(struct dm_crypt_io *io)
1467{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001468 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001469}
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471/*
1472 * One of the bios was finished. Check for completion of
1473 * the whole request and correctly clean up the buffer.
1474 */
Milan Broz5742fd72008-02-08 02:10:43 +00001475static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001477 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001478 struct bio *base_bio = io->base_bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001479 blk_status_t error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Mikulas Patocka40b62292012-07-27 15:08:04 +01001481 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return;
1483
Milan Brozef43aa32017-01-04 20:23:54 +01001484 if (io->ctx.r.req)
1485 crypt_free_req(cc, io->ctx.r.req, base_bio);
1486
1487 if (unlikely(io->integrity_metadata_from_pool))
1488 mempool_free(io->integrity_metadata, io->cc->tag_pool);
1489 else
1490 kfree(io->integrity_metadata);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001491
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001492 base_bio->bi_status = error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001493 bio_endio(base_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
1496/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001497 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 *
1499 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001500 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001501 *
1502 * kcryptd performs the actual encryption or decryption.
1503 *
1504 * kcryptd_io performs the IO submission.
1505 *
1506 * They must be separated as otherwise the final stages could be
1507 * starved by new requests which can block in the first stages due
1508 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001509 *
1510 * The work is done per CPU global for all dm-crypt instances.
1511 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001513static void crypt_endio(struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001514{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001515 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001516 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001517 unsigned rw = bio_data_dir(clone);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001518 blk_status_t error;
Milan Broz8b004452006-10-03 01:15:37 -07001519
1520 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001521 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001522 */
Milan Brozee7a4912008-02-08 02:10:46 +00001523 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001524 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001525
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001526 error = clone->bi_status;
Milan Brozee7a4912008-02-08 02:10:46 +00001527 bio_put(clone);
1528
Sasha Levin9b81c842015-08-10 19:05:18 -04001529 if (rw == READ && !error) {
Milan Brozee7a4912008-02-08 02:10:46 +00001530 kcryptd_queue_crypt(io);
1531 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001532 }
Milan Broz8b004452006-10-03 01:15:37 -07001533
Sasha Levin9b81c842015-08-10 19:05:18 -04001534 if (unlikely(error))
1535 io->error = error;
Milan Broz5742fd72008-02-08 02:10:43 +00001536
1537 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001538}
1539
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001540static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001541{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001542 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001543
1544 clone->bi_private = io;
1545 clone->bi_end_io = crypt_endio;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001546 bio_set_dev(clone, cc->dev->bdev);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001547 clone->bi_opf = io->base_bio->bi_opf;
Milan Broz8b004452006-10-03 01:15:37 -07001548}
1549
Milan Broz20c82532011-01-13 19:59:53 +00001550static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001551{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001552 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001553 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001554
Milan Broz8b004452006-10-03 01:15:37 -07001555 /*
Mike Snitzer59779072015-04-09 16:53:24 -04001556 * We need the original biovec array in order to decrypt
1557 * the whole bio data *afterwards* -- thanks to immutable
1558 * biovecs we don't need to worry about the block layer
1559 * modifying the biovec array; so leverage bio_clone_fast().
Milan Broz8b004452006-10-03 01:15:37 -07001560 */
Mike Snitzer59779072015-04-09 16:53:24 -04001561 clone = bio_clone_fast(io->base_bio, gfp, cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001562 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001563 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001564
Milan Broz20c82532011-01-13 19:59:53 +00001565 crypt_inc_pending(io);
1566
Milan Broz8b004452006-10-03 01:15:37 -07001567 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001568 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001569
Milan Brozef43aa32017-01-04 20:23:54 +01001570 if (dm_crypt_integrity_io_alloc(io, clone)) {
1571 crypt_dec_pending(io);
1572 bio_put(clone);
1573 return 1;
1574 }
1575
Milan Broz93e605c2006-10-03 01:15:38 -07001576 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001577 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001578}
1579
Mikulas Patockadc267622015-02-13 08:25:59 -05001580static void kcryptd_io_read_work(struct work_struct *work)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001581{
1582 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1583
Mikulas Patockadc267622015-02-13 08:25:59 -05001584 crypt_inc_pending(io);
1585 if (kcryptd_io_read(io, GFP_NOIO))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001586 io->error = BLK_STS_RESOURCE;
Mikulas Patockadc267622015-02-13 08:25:59 -05001587 crypt_dec_pending(io);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001588}
1589
Mikulas Patockadc267622015-02-13 08:25:59 -05001590static void kcryptd_queue_read(struct dm_crypt_io *io)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001591{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001592 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001593
Mikulas Patockadc267622015-02-13 08:25:59 -05001594 INIT_WORK(&io->work, kcryptd_io_read_work);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001595 queue_work(cc->io_queue, &io->work);
1596}
1597
Mikulas Patockadc267622015-02-13 08:25:59 -05001598static void kcryptd_io_write(struct dm_crypt_io *io)
1599{
1600 struct bio *clone = io->ctx.bio_out;
1601
1602 generic_make_request(clone);
1603}
1604
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001605#define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1606
Mikulas Patockadc267622015-02-13 08:25:59 -05001607static int dmcrypt_write(void *data)
1608{
1609 struct crypt_config *cc = data;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001610 struct dm_crypt_io *io;
1611
Mikulas Patockadc267622015-02-13 08:25:59 -05001612 while (1) {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001613 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -05001614 struct blk_plug plug;
1615
1616 DECLARE_WAITQUEUE(wait, current);
1617
1618 spin_lock_irq(&cc->write_thread_wait.lock);
1619continue_locked:
1620
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001621 if (!RB_EMPTY_ROOT(&cc->write_tree))
Mikulas Patockadc267622015-02-13 08:25:59 -05001622 goto pop_from_list;
1623
Rabin Vincentf659b102016-09-21 16:22:29 +02001624 set_current_state(TASK_INTERRUPTIBLE);
Mikulas Patockadc267622015-02-13 08:25:59 -05001625 __add_wait_queue(&cc->write_thread_wait, &wait);
1626
1627 spin_unlock_irq(&cc->write_thread_wait.lock);
1628
Rabin Vincentf659b102016-09-21 16:22:29 +02001629 if (unlikely(kthread_should_stop())) {
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001630 set_current_state(TASK_RUNNING);
Rabin Vincentf659b102016-09-21 16:22:29 +02001631 remove_wait_queue(&cc->write_thread_wait, &wait);
1632 break;
1633 }
1634
Mikulas Patockadc267622015-02-13 08:25:59 -05001635 schedule();
1636
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001637 set_current_state(TASK_RUNNING);
Mikulas Patockadc267622015-02-13 08:25:59 -05001638 spin_lock_irq(&cc->write_thread_wait.lock);
1639 __remove_wait_queue(&cc->write_thread_wait, &wait);
1640 goto continue_locked;
1641
1642pop_from_list:
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001643 write_tree = cc->write_tree;
1644 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05001645 spin_unlock_irq(&cc->write_thread_wait.lock);
1646
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001647 BUG_ON(rb_parent(write_tree.rb_node));
1648
1649 /*
1650 * Note: we cannot walk the tree here with rb_next because
1651 * the structures may be freed when kcryptd_io_write is called.
1652 */
Mikulas Patockadc267622015-02-13 08:25:59 -05001653 blk_start_plug(&plug);
1654 do {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001655 io = crypt_io_from_node(rb_first(&write_tree));
1656 rb_erase(&io->rb_node, &write_tree);
Mikulas Patockadc267622015-02-13 08:25:59 -05001657 kcryptd_io_write(io);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001658 } while (!RB_EMPTY_ROOT(&write_tree));
Mikulas Patockadc267622015-02-13 08:25:59 -05001659 blk_finish_plug(&plug);
1660 }
1661 return 0;
1662}
1663
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001664static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001665{
Milan Brozdec1ced2008-02-08 02:10:57 +00001666 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001667 struct crypt_config *cc = io->cc;
Mikulas Patockadc267622015-02-13 08:25:59 -05001668 unsigned long flags;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001669 sector_t sector;
1670 struct rb_node **rbp, *parent;
Milan Brozdec1ced2008-02-08 02:10:57 +00001671
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001672 if (unlikely(io->error)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001673 crypt_free_buffer_pages(cc, clone);
1674 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001675 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001676 return;
1677 }
1678
1679 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001680 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001681
Kent Overstreet4f024f32013-10-11 15:44:27 -07001682 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001683
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001684 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1685 generic_make_request(clone);
1686 return;
1687 }
1688
Mikulas Patockadc267622015-02-13 08:25:59 -05001689 spin_lock_irqsave(&cc->write_thread_wait.lock, flags);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001690 rbp = &cc->write_tree.rb_node;
1691 parent = NULL;
1692 sector = io->sector;
1693 while (*rbp) {
1694 parent = *rbp;
1695 if (sector < crypt_io_from_node(parent)->sector)
1696 rbp = &(*rbp)->rb_left;
1697 else
1698 rbp = &(*rbp)->rb_right;
1699 }
1700 rb_link_node(&io->rb_node, parent, rbp);
1701 rb_insert_color(&io->rb_node, &cc->write_tree);
1702
Mikulas Patockadc267622015-02-13 08:25:59 -05001703 wake_up_locked(&cc->write_thread_wait);
1704 spin_unlock_irqrestore(&cc->write_thread_wait.lock, flags);
Milan Broz4e4eef62008-02-08 02:10:49 +00001705}
1706
Milan Brozfc5a5e92008-10-10 13:37:04 +01001707static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001708{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001709 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001710 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001711 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001712 sector_t sector = io->sector;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001713 blk_status_t r;
Milan Broz8b004452006-10-03 01:15:37 -07001714
Milan Broz93e605c2006-10-03 01:15:38 -07001715 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001716 * Prevent io from disappearing until this function completes.
1717 */
1718 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001719 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001720
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001721 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1722 if (unlikely(!clone)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001723 io->error = BLK_STS_IOERR;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001724 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001725 }
Milan Broz899c95d2008-02-08 02:11:02 +00001726
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001727 io->ctx.bio_out = clone;
1728 io->ctx.iter_out = clone->bi_iter;
1729
1730 sector += bio_sectors(clone);
1731
1732 crypt_inc_pending(io);
1733 r = crypt_convert(cc, &io->ctx);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001734 if (r)
Milan Brozef43aa32017-01-04 20:23:54 +01001735 io->error = r;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001736 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1737
1738 /* Encryption was already finished, submit io now */
1739 if (crypt_finished) {
1740 kcryptd_crypt_write_io_submit(io, 0);
1741 io->sector = sector;
1742 }
1743
1744dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001745 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001746}
1747
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001748static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001749{
Milan Broz5742fd72008-02-08 02:10:43 +00001750 crypt_dec_pending(io);
1751}
1752
Milan Broz4e4eef62008-02-08 02:10:49 +00001753static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001754{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001755 struct crypt_config *cc = io->cc;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001756 blk_status_t r;
Milan Broz8b004452006-10-03 01:15:37 -07001757
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001758 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001759
Milan Broz53017032008-02-08 02:10:38 +00001760 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001761 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001762
Milan Broz5742fd72008-02-08 02:10:43 +00001763 r = crypt_convert(cc, &io->ctx);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001764 if (r)
Milan Brozef43aa32017-01-04 20:23:54 +01001765 io->error = r;
Milan Broz5742fd72008-02-08 02:10:43 +00001766
Mikulas Patocka40b62292012-07-27 15:08:04 +01001767 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001768 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001769
1770 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001771}
1772
Milan Broz95497a92008-02-08 02:11:12 +00001773static void kcryptd_async_done(struct crypto_async_request *async_req,
1774 int error)
1775{
Huang Yingb2174ee2009-03-16 17:44:33 +00001776 struct dm_crypt_request *dmreq = async_req->data;
1777 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001778 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001779 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001780
Milan Broz54cea3f2015-05-15 17:00:25 +02001781 /*
1782 * A request from crypto driver backlog is going to be processed now,
1783 * finish the completion and continue in crypt_convert().
1784 * (Callback will be called for the second time for this request.)
1785 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001786 if (error == -EINPROGRESS) {
1787 complete(&ctx->restart);
Milan Broz95497a92008-02-08 02:11:12 +00001788 return;
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001789 }
Milan Broz95497a92008-02-08 02:11:12 +00001790
Milan Broz2dc5327d32011-01-13 19:59:54 +00001791 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001792 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq);
Milan Broz2dc5327d32011-01-13 19:59:54 +00001793
Milan Brozef43aa32017-01-04 20:23:54 +01001794 if (error == -EBADMSG) {
1795 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1796 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001797 io->error = BLK_STS_PROTECTION;
Milan Brozef43aa32017-01-04 20:23:54 +01001798 } else if (error < 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001799 io->error = BLK_STS_IOERR;
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001800
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001801 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00001802
Mikulas Patocka40b62292012-07-27 15:08:04 +01001803 if (!atomic_dec_and_test(&ctx->cc_pending))
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001804 return;
Milan Broz95497a92008-02-08 02:11:12 +00001805
1806 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001807 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001808 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001809 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001810}
1811
Milan Broz4e4eef62008-02-08 02:10:49 +00001812static void kcryptd_crypt(struct work_struct *work)
1813{
1814 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1815
1816 if (bio_data_dir(io->base_bio) == READ)
1817 kcryptd_crypt_read_convert(io);
1818 else
1819 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001820}
1821
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001822static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1823{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001824 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001825
1826 INIT_WORK(&io->work, kcryptd_crypt);
1827 queue_work(cc->crypt_queue, &io->work);
1828}
1829
Milan Brozef43aa32017-01-04 20:23:54 +01001830static void crypt_free_tfms_aead(struct crypt_config *cc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Milan Brozef43aa32017-01-04 20:23:54 +01001832 if (!cc->cipher_tfm.tfms_aead)
1833 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Milan Brozef43aa32017-01-04 20:23:54 +01001835 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1836 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]);
1837 cc->cipher_tfm.tfms_aead[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839
Milan Brozef43aa32017-01-04 20:23:54 +01001840 kfree(cc->cipher_tfm.tfms_aead);
1841 cc->cipher_tfm.tfms_aead = NULL;
1842}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Milan Brozef43aa32017-01-04 20:23:54 +01001844static void crypt_free_tfms_skcipher(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001845{
Milan Brozd1f96422011-01-13 19:59:54 +00001846 unsigned i;
1847
Milan Brozef43aa32017-01-04 20:23:54 +01001848 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001849 return;
1850
Milan Brozd1f96422011-01-13 19:59:54 +00001851 for (i = 0; i < cc->tfms_count; i++)
Milan Brozef43aa32017-01-04 20:23:54 +01001852 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) {
1853 crypto_free_skcipher(cc->cipher_tfm.tfms[i]);
1854 cc->cipher_tfm.tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001855 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001856
Milan Brozef43aa32017-01-04 20:23:54 +01001857 kfree(cc->cipher_tfm.tfms);
1858 cc->cipher_tfm.tfms = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
1861static void crypt_free_tfms(struct crypt_config *cc)
1862{
Milan Broz33d2f092017-03-16 15:39:40 +01001863 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001864 crypt_free_tfms_aead(cc);
1865 else
1866 crypt_free_tfms_skcipher(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001867}
1868
Milan Brozef43aa32017-01-04 20:23:54 +01001869static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001870{
Milan Brozd1f96422011-01-13 19:59:54 +00001871 unsigned i;
1872 int err;
1873
Milan Brozef43aa32017-01-04 20:23:54 +01001874 cc->cipher_tfm.tfms = kzalloc(cc->tfms_count *
1875 sizeof(struct crypto_skcipher *), GFP_KERNEL);
1876 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001877 return -ENOMEM;
1878
Milan Brozd1f96422011-01-13 19:59:54 +00001879 for (i = 0; i < cc->tfms_count; i++) {
Milan Brozef43aa32017-01-04 20:23:54 +01001880 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
1881 if (IS_ERR(cc->cipher_tfm.tfms[i])) {
1882 err = PTR_ERR(cc->cipher_tfm.tfms[i]);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001883 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001884 return err;
1885 }
1886 }
1887
1888 return 0;
1889}
1890
Milan Brozef43aa32017-01-04 20:23:54 +01001891static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
1892{
Milan Brozef43aa32017-01-04 20:23:54 +01001893 int err;
1894
1895 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
1896 if (!cc->cipher_tfm.tfms)
1897 return -ENOMEM;
1898
Milan Brozef43aa32017-01-04 20:23:54 +01001899 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
1900 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1901 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
1902 crypt_free_tfms(cc);
1903 return err;
1904 }
1905
Milan Brozef43aa32017-01-04 20:23:54 +01001906 return 0;
1907}
1908
1909static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
1910{
Milan Broz33d2f092017-03-16 15:39:40 +01001911 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001912 return crypt_alloc_tfms_aead(cc, ciphermode);
1913 else
1914 return crypt_alloc_tfms_skcipher(cc, ciphermode);
1915}
1916
1917static unsigned crypt_subkey_size(struct crypt_config *cc)
1918{
1919 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1920}
1921
1922static unsigned crypt_authenckey_size(struct crypt_config *cc)
1923{
1924 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
1925}
1926
1927/*
1928 * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
1929 * the key must be for some reason in special format.
1930 * This funcion converts cc->key to this special format.
1931 */
1932static void crypt_copy_authenckey(char *p, const void *key,
1933 unsigned enckeylen, unsigned authkeylen)
1934{
1935 struct crypto_authenc_key_param *param;
1936 struct rtattr *rta;
1937
1938 rta = (struct rtattr *)p;
1939 param = RTA_DATA(rta);
1940 param->enckeylen = cpu_to_be32(enckeylen);
1941 rta->rta_len = RTA_LENGTH(sizeof(*param));
1942 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
1943 p += RTA_SPACE(sizeof(*param));
1944 memcpy(p, key + enckeylen, authkeylen);
1945 p += authkeylen;
1946 memcpy(p, key, enckeylen);
1947}
1948
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04001949static int crypt_setkey(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +00001950{
Milan Brozda31a072013-10-28 23:21:03 +01001951 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001952 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001953
Milan Brozda31a072013-10-28 23:21:03 +01001954 /* Ignore extra keys (which are used for IV etc) */
Milan Brozef43aa32017-01-04 20:23:54 +01001955 subkey_size = crypt_subkey_size(cc);
Milan Brozda31a072013-10-28 23:21:03 +01001956
Milan Broz27c70032018-01-03 22:48:59 +01001957 if (crypt_integrity_hmac(cc)) {
1958 if (subkey_size < cc->key_mac_size)
1959 return -EINVAL;
1960
Milan Brozef43aa32017-01-04 20:23:54 +01001961 crypt_copy_authenckey(cc->authenc_key, cc->key,
1962 subkey_size - cc->key_mac_size,
1963 cc->key_mac_size);
Milan Broz27c70032018-01-03 22:48:59 +01001964 }
1965
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001966 for (i = 0; i < cc->tfms_count; i++) {
Milan Broz33d2f092017-03-16 15:39:40 +01001967 if (crypt_integrity_hmac(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001968 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1969 cc->authenc_key, crypt_authenckey_size(cc));
Milan Broz33d2f092017-03-16 15:39:40 +01001970 else if (crypt_integrity_aead(cc))
1971 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1972 cc->key + (i * subkey_size),
1973 subkey_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001974 else
1975 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i],
1976 cc->key + (i * subkey_size),
1977 subkey_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001978 if (r)
1979 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001980 }
1981
Milan Brozef43aa32017-01-04 20:23:54 +01001982 if (crypt_integrity_hmac(cc))
1983 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc));
1984
Andi Kleenc0297722011-01-13 19:59:53 +00001985 return err;
1986}
1987
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001988#ifdef CONFIG_KEYS
1989
Ondrej Kozina027c4312016-12-01 18:20:52 +01001990static bool contains_whitespace(const char *str)
1991{
1992 while (*str)
1993 if (isspace(*str++))
1994 return true;
1995 return false;
1996}
1997
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001998static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
1999{
2000 char *new_key_string, *key_desc;
2001 int ret;
2002 struct key *key;
2003 const struct user_key_payload *ukp;
2004
Ondrej Kozina027c4312016-12-01 18:20:52 +01002005 /*
2006 * Reject key_string with whitespace. dm core currently lacks code for
2007 * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
2008 */
2009 if (contains_whitespace(key_string)) {
2010 DMERR("whitespace chars not allowed in key string");
2011 return -EINVAL;
2012 }
2013
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002014 /* look for next ':' separating key_type from key_description */
2015 key_desc = strpbrk(key_string, ":");
2016 if (!key_desc || key_desc == key_string || !strlen(key_desc + 1))
2017 return -EINVAL;
2018
2019 if (strncmp(key_string, "logon:", key_desc - key_string + 1) &&
2020 strncmp(key_string, "user:", key_desc - key_string + 1))
2021 return -EINVAL;
2022
2023 new_key_string = kstrdup(key_string, GFP_KERNEL);
2024 if (!new_key_string)
2025 return -ENOMEM;
2026
2027 key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user,
2028 key_desc + 1, NULL);
2029 if (IS_ERR(key)) {
2030 kzfree(new_key_string);
2031 return PTR_ERR(key);
2032 }
2033
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002034 down_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002035
David Howells0837e492017-03-01 15:11:23 +00002036 ukp = user_key_payload_locked(key);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002037 if (!ukp) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002038 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002039 key_put(key);
2040 kzfree(new_key_string);
2041 return -EKEYREVOKED;
2042 }
2043
2044 if (cc->key_size != ukp->datalen) {
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 -EINVAL;
2049 }
2050
2051 memcpy(cc->key, ukp->data, cc->key_size);
2052
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002053 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002054 key_put(key);
2055
2056 /* clear the flag since following operations may invalidate previously valid key */
2057 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2058
2059 ret = crypt_setkey(cc);
2060
2061 /* wipe the kernel key payload copy in each case */
2062 memset(cc->key, 0, cc->key_size * sizeof(u8));
2063
2064 if (!ret) {
2065 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2066 kzfree(cc->key_string);
2067 cc->key_string = new_key_string;
2068 } else
2069 kzfree(new_key_string);
2070
2071 return ret;
2072}
2073
2074static int get_key_size(char **key_string)
2075{
2076 char *colon, dummy;
2077 int ret;
2078
2079 if (*key_string[0] != ':')
2080 return strlen(*key_string) >> 1;
2081
2082 /* look for next ':' in key string */
2083 colon = strpbrk(*key_string + 1, ":");
2084 if (!colon)
2085 return -EINVAL;
2086
2087 if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':')
2088 return -EINVAL;
2089
2090 *key_string = colon;
2091
2092 /* remaining key string should be :<logon|user>:<key_desc> */
2093
2094 return ret;
2095}
2096
2097#else
2098
2099static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2100{
2101 return -EINVAL;
2102}
2103
2104static int get_key_size(char **key_string)
2105{
2106 return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1;
2107}
2108
2109#endif
2110
Milan Broze48d4bb2006-10-03 01:15:37 -07002111static int crypt_set_key(struct crypt_config *cc, char *key)
2112{
Milan Brozde8be5a2011-03-24 13:54:27 +00002113 int r = -EINVAL;
2114 int key_string_len = strlen(key);
2115
Milan Broz69a8cfc2011-01-13 19:59:49 +00002116 /* Hyphen (which gives a key_size of zero) means there is no key. */
2117 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00002118 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002119
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002120 /* ':' means the key is in kernel keyring, short-circuit normal key processing */
2121 if (key[0] == ':') {
2122 r = crypt_set_keyring_key(cc, key + 1);
2123 goto out;
2124 }
2125
Ondrej Kozina265e9092016-11-02 15:02:08 +01002126 /* clear the flag since following operations may invalidate previously valid key */
2127 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2128
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002129 /* wipe references to any kernel keyring key */
2130 kzfree(cc->key_string);
2131 cc->key_string = NULL;
2132
Andy Shevchenkoe944e032017-04-27 16:52:04 +03002133 /* Decode key from its hex representation. */
2134 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00002135 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002136
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002137 r = crypt_setkey(cc);
Ondrej Kozina265e9092016-11-02 15:02:08 +01002138 if (!r)
2139 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Milan Brozde8be5a2011-03-24 13:54:27 +00002140
2141out:
2142 /* Hex key string not needed after here, so wipe it. */
2143 memset(key, '0', key_string_len);
2144
2145 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002146}
2147
2148static int crypt_wipe_key(struct crypt_config *cc)
2149{
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002150 int r;
2151
Milan Broze48d4bb2006-10-03 01:15:37 -07002152 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002153 get_random_bytes(&cc->key, cc->key_size);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002154 kzfree(cc->key_string);
2155 cc->key_string = NULL;
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002156 r = crypt_setkey(cc);
2157 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00002158
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002159 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002160}
2161
Milan Broz28513fc2010-08-12 04:14:06 +01002162static void crypt_dtr(struct dm_target *ti)
2163{
2164 struct crypt_config *cc = ti->private;
2165
2166 ti->private = NULL;
2167
2168 if (!cc)
2169 return;
2170
Rabin Vincentf659b102016-09-21 16:22:29 +02002171 if (cc->write_thread)
Mikulas Patockadc267622015-02-13 08:25:59 -05002172 kthread_stop(cc->write_thread);
2173
Milan Broz28513fc2010-08-12 04:14:06 +01002174 if (cc->io_queue)
2175 destroy_workqueue(cc->io_queue);
2176 if (cc->crypt_queue)
2177 destroy_workqueue(cc->crypt_queue);
2178
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002179 crypt_free_tfms(cc);
2180
Milan Broz28513fc2010-08-12 04:14:06 +01002181 if (cc->bs)
2182 bioset_free(cc->bs);
2183
Julia Lawall6f659852015-09-13 14:15:05 +02002184 mempool_destroy(cc->page_pool);
2185 mempool_destroy(cc->req_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01002186 mempool_destroy(cc->tag_pool);
Milan Broz28513fc2010-08-12 04:14:06 +01002187
2188 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
2189 cc->iv_gen_ops->dtr(cc);
2190
Milan Broz28513fc2010-08-12 04:14:06 +01002191 if (cc->dev)
2192 dm_put_device(ti, cc->dev);
2193
Milan Broz5ebaee62010-08-12 04:14:07 +01002194 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00002195 kzfree(cc->cipher_string);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002196 kzfree(cc->key_string);
Milan Brozef43aa32017-01-04 20:23:54 +01002197 kzfree(cc->cipher_auth);
2198 kzfree(cc->authenc_key);
Milan Broz28513fc2010-08-12 04:14:06 +01002199
2200 /* Must zero key material before freeing */
2201 kzfree(cc);
2202}
2203
Milan Broze889f972017-03-16 15:39:39 +01002204static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Milan Broz5ebaee62010-08-12 04:14:07 +01002206 struct crypt_config *cc = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Milan Broz33d2f092017-03-16 15:39:40 +01002208 if (crypt_integrity_aead(cc))
Milan Broze889f972017-03-16 15:39:39 +01002209 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2210 else
2211 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Milan Broz5ebaee62010-08-12 04:14:07 +01002213 if (cc->iv_size)
2214 /* at least a 64 bit sector number should fit in our buffer */
2215 cc->iv_size = max(cc->iv_size,
2216 (unsigned int)(sizeof(u64) / sizeof(u8)));
2217 else if (ivmode) {
2218 DMWARN("Selected cipher does not support IVs");
2219 ivmode = NULL;
2220 }
2221
2222 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (ivmode == NULL)
2224 cc->iv_gen_ops = NULL;
2225 else if (strcmp(ivmode, "plain") == 0)
2226 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00002227 else if (strcmp(ivmode, "plain64") == 0)
2228 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Milan Broz7e3fd852017-06-06 09:07:01 +02002229 else if (strcmp(ivmode, "plain64be") == 0)
2230 cc->iv_gen_ops = &crypt_iv_plain64be_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 else if (strcmp(ivmode, "essiv") == 0)
2232 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10002233 else if (strcmp(ivmode, "benbi") == 0)
2234 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07002235 else if (strcmp(ivmode, "null") == 0)
2236 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00002237 else if (strcmp(ivmode, "lmk") == 0) {
2238 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01002239 /*
2240 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00002241 * to length of provided multi-key string.
2242 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01002243 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00002244 */
Milan Brozda31a072013-10-28 23:21:03 +01002245 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00002246 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01002247 cc->key_extra_size = cc->key_size / cc->key_parts;
2248 }
Milan Brozed04d982013-10-28 23:21:04 +01002249 } else if (strcmp(ivmode, "tcw") == 0) {
2250 cc->iv_gen_ops = &crypt_iv_tcw_ops;
2251 cc->key_parts += 2; /* IV + whitening */
2252 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broze889f972017-03-16 15:39:39 +01002253 } else if (strcmp(ivmode, "random") == 0) {
2254 cc->iv_gen_ops = &crypt_iv_random_ops;
2255 /* Need storage space in integrity fields. */
2256 cc->integrity_iv_size = cc->iv_size;
Milan Broz34745782011-01-13 19:59:55 +00002257 } else {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002258 ti->error = "Invalid IV mode";
Milan Broze889f972017-03-16 15:39:39 +01002259 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261
Milan Broze889f972017-03-16 15:39:39 +01002262 return 0;
2263}
2264
Milan Broz33d2f092017-03-16 15:39:40 +01002265/*
2266 * Workaround to parse cipher algorithm from crypto API spec.
2267 * The cc->cipher is currently used only in ESSIV.
2268 * This should be probably done by crypto-api calls (once available...)
2269 */
2270static int crypt_ctr_blkdev_cipher(struct crypt_config *cc)
2271{
2272 const char *alg_name = NULL;
2273 char *start, *end;
2274
2275 if (crypt_integrity_aead(cc)) {
2276 alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc)));
2277 if (!alg_name)
2278 return -EINVAL;
2279 if (crypt_integrity_hmac(cc)) {
2280 alg_name = strchr(alg_name, ',');
2281 if (!alg_name)
2282 return -EINVAL;
2283 }
2284 alg_name++;
2285 } else {
2286 alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc)));
2287 if (!alg_name)
2288 return -EINVAL;
2289 }
2290
2291 start = strchr(alg_name, '(');
2292 end = strchr(alg_name, ')');
2293
2294 if (!start && !end) {
2295 cc->cipher = kstrdup(alg_name, GFP_KERNEL);
2296 return cc->cipher ? 0 : -ENOMEM;
2297 }
2298
2299 if (!start || !end || ++start >= end)
2300 return -EINVAL;
2301
2302 cc->cipher = kzalloc(end - start + 1, GFP_KERNEL);
2303 if (!cc->cipher)
2304 return -ENOMEM;
2305
2306 strncpy(cc->cipher, start, end - start);
2307
2308 return 0;
2309}
2310
2311/*
2312 * Workaround to parse HMAC algorithm from AEAD crypto API spec.
2313 * The HMAC is needed to calculate tag size (HMAC digest size).
2314 * This should be probably done by crypto-api calls (once available...)
2315 */
2316static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
2317{
2318 char *start, *end, *mac_alg = NULL;
2319 struct crypto_ahash *mac;
2320
2321 if (!strstarts(cipher_api, "authenc("))
2322 return 0;
2323
2324 start = strchr(cipher_api, '(');
2325 end = strchr(cipher_api, ',');
2326 if (!start || !end || ++start > end)
2327 return -EINVAL;
2328
2329 mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
2330 if (!mac_alg)
2331 return -ENOMEM;
2332 strncpy(mac_alg, start, end - start);
2333
2334 mac = crypto_alloc_ahash(mac_alg, 0, 0);
2335 kfree(mac_alg);
2336
2337 if (IS_ERR(mac))
2338 return PTR_ERR(mac);
2339
2340 cc->key_mac_size = crypto_ahash_digestsize(mac);
2341 crypto_free_ahash(mac);
2342
2343 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
2344 if (!cc->authenc_key)
2345 return -ENOMEM;
2346
2347 return 0;
2348}
2349
2350static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key,
2351 char **ivmode, char **ivopts)
Milan Broz5ebaee62010-08-12 04:14:07 +01002352{
2353 struct crypt_config *cc = ti->private;
Milan Broz33d2f092017-03-16 15:39:40 +01002354 char *tmp, *cipher_api;
2355 int ret = -EINVAL;
2356
2357 cc->tfms_count = 1;
2358
2359 /*
2360 * New format (capi: prefix)
2361 * capi:cipher_api_spec-iv:ivopts
2362 */
2363 tmp = &cipher_in[strlen("capi:")];
2364 cipher_api = strsep(&tmp, "-");
2365 *ivmode = strsep(&tmp, ":");
2366 *ivopts = tmp;
2367
2368 if (*ivmode && !strcmp(*ivmode, "lmk"))
2369 cc->tfms_count = 64;
2370
2371 cc->key_parts = cc->tfms_count;
2372
2373 /* Allocate cipher */
2374 ret = crypt_alloc_tfms(cc, cipher_api);
2375 if (ret < 0) {
2376 ti->error = "Error allocating crypto tfm";
2377 return ret;
2378 }
2379
2380 /* Alloc AEAD, can be used only in new format. */
2381 if (crypt_integrity_aead(cc)) {
2382 ret = crypt_ctr_auth_cipher(cc, cipher_api);
2383 if (ret < 0) {
2384 ti->error = "Invalid AEAD cipher spec";
2385 return -ENOMEM;
2386 }
2387 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2388 } else
2389 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2390
2391 ret = crypt_ctr_blkdev_cipher(cc);
2392 if (ret < 0) {
2393 ti->error = "Cannot allocate cipher string";
2394 return -ENOMEM;
2395 }
2396
2397 return 0;
2398}
2399
2400static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
2401 char **ivmode, char **ivopts)
2402{
2403 struct crypt_config *cc = ti->private;
2404 char *tmp, *cipher, *chainmode, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01002405 char *cipher_api = NULL;
2406 int ret = -EINVAL;
2407 char dummy;
2408
Milan Broz33d2f092017-03-16 15:39:40 +01002409 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002410 ti->error = "Bad cipher specification";
2411 return -EINVAL;
2412 }
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 /*
Milan Broz5ebaee62010-08-12 04:14:07 +01002415 * Legacy dm-crypt cipher specification
2416 * cipher[:keycount]-mode-iv:ivopts
2417 */
2418 tmp = cipher_in;
2419 keycount = strsep(&tmp, "-");
2420 cipher = strsep(&keycount, ":");
2421
Milan Broz69a8cfc2011-01-13 19:59:49 +00002422 if (!keycount)
Milan Broz5ebaee62010-08-12 04:14:07 +01002423 cc->tfms_count = 1;
2424 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
2425 !is_power_of_2(cc->tfms_count)) {
2426 ti->error = "Bad cipher key count specification";
2427 return -EINVAL;
2428 }
Milan Broz28513fc2010-08-12 04:14:06 +01002429 cc->key_parts = cc->tfms_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002431 cc->cipher = kstrdup(cipher, GFP_KERNEL);
Milan Broz28513fc2010-08-12 04:14:06 +01002432 if (!cc->cipher)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 goto bad_mem;
2434
Milan Brozddd42ed2008-02-08 02:11:07 +00002435 chainmode = strsep(&tmp, "-");
Milan Broz33d2f092017-03-16 15:39:40 +01002436 *ivopts = strsep(&tmp, "-");
2437 *ivmode = strsep(&*ivopts, ":");
Andi Kleenc0297722011-01-13 19:59:53 +00002438
Milan Broz3a7f6c92008-02-08 02:11:14 +00002439 if (tmp)
Milan Brozddd42ed2008-02-08 02:11:07 +00002440 DMWARN("Ignoring unexpected additional cipher options");
2441
2442 /*
2443 * For compatibility with the original dm-crypt mapping format, if
2444 * only the cipher name is supplied, use cbc-plain.
Milan Broz28513fc2010-08-12 04:14:06 +01002445 */
Milan Broz33d2f092017-03-16 15:39:40 +01002446 if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002447 chainmode = "cbc";
Milan Broz33d2f092017-03-16 15:39:40 +01002448 *ivmode = "plain";
Milan Brozcabf08e2007-10-19 22:38:58 +01002449 }
2450
Milan Broz33d2f092017-03-16 15:39:40 +01002451 if (strcmp(chainmode, "ecb") && !*ivmode) {
Andi Kleenc0297722011-01-13 19:59:53 +00002452 ti->error = "IV mechanism required";
2453 return -EINVAL;
2454 }
2455
Milan Brozcabf08e2007-10-19 22:38:58 +01002456 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
Milan Broz9934a8b2007-10-19 22:38:57 +01002457 if (!cipher_api)
Milan Broz28513fc2010-08-12 04:14:06 +01002458 goto bad_mem;
Milan Broz9934a8b2007-10-19 22:38:57 +01002459
2460 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002461 "%s(%s)", chainmode, cipher);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (ret < 0) {
2463 kfree(cipher_api);
Milan Broz28513fc2010-08-12 04:14:06 +01002464 goto bad_mem;
2465 }
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 /* Allocate cipher */
2468 ret = crypt_alloc_tfms(cc, cipher_api);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if (ret < 0) {
2470 ti->error = "Error allocating crypto tfm";
Milan Broz33d2f092017-03-16 15:39:40 +01002471 kfree(cipher_api);
2472 return ret;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002473 }
Jeffy Chenbd86e322017-09-27 20:28:57 +08002474 kfree(cipher_api);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002475
Milan Broz33d2f092017-03-16 15:39:40 +01002476 return 0;
2477bad_mem:
2478 ti->error = "Cannot allocate cipher strings";
2479 return -ENOMEM;
2480}
2481
2482static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key)
2483{
2484 struct crypt_config *cc = ti->private;
2485 char *ivmode = NULL, *ivopts = NULL;
2486 int ret;
2487
2488 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
2489 if (!cc->cipher_string) {
2490 ti->error = "Cannot allocate cipher strings";
2491 return -ENOMEM;
2492 }
2493
2494 if (strstarts(cipher_in, "capi:"))
2495 ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts);
2496 else
2497 ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts);
2498 if (ret)
2499 return ret;
2500
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002501 /* Initialize IV */
Milan Broze889f972017-03-16 15:39:39 +01002502 ret = crypt_ctr_ivmode(ti, ivmode);
2503 if (ret < 0)
Milan Broz33d2f092017-03-16 15:39:40 +01002504 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505
Milan Brozda31a072013-10-28 23:21:03 +01002506 /* Initialize and set key */
2507 ret = crypt_set_key(cc, key);
2508 if (ret < 0) {
2509 ti->error = "Error decoding and setting key";
Milan Broz33d2f092017-03-16 15:39:40 +01002510 return ret;
Milan Brozda31a072013-10-28 23:21:03 +01002511 }
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 /* Allocate IV */
2514 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
2515 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
2516 if (ret < 0) {
2517 ti->error = "Error creating IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002518 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 }
2520 }
2521
2522 /* Initialize IV (set keys for ESSIV etc) */
2523 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
2524 ret = cc->iv_gen_ops->init(cc);
2525 if (ret < 0) {
2526 ti->error = "Error initialising IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002527 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 }
2529 }
2530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Milan Brozef43aa32017-01-04 20:23:54 +01002534static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
2535{
2536 struct crypt_config *cc = ti->private;
2537 struct dm_arg_set as;
Eric Biggers5916a222017-06-22 11:32:45 -07002538 static const struct dm_arg _args[] = {
Milan Broz8f0009a2017-03-16 15:39:44 +01002539 {0, 6, "Invalid number of feature args"},
Milan Brozef43aa32017-01-04 20:23:54 +01002540 };
2541 unsigned int opt_params, val;
2542 const char *opt_string, *sval;
Milan Broz8f0009a2017-03-16 15:39:44 +01002543 char dummy;
Milan Brozef43aa32017-01-04 20:23:54 +01002544 int ret;
2545
2546 /* Optional parameters */
2547 as.argc = argc;
2548 as.argv = argv;
2549
2550 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2551 if (ret)
2552 return ret;
2553
2554 while (opt_params--) {
2555 opt_string = dm_shift_arg(&as);
2556 if (!opt_string) {
2557 ti->error = "Not enough feature arguments";
2558 return -EINVAL;
2559 }
2560
2561 if (!strcasecmp(opt_string, "allow_discards"))
2562 ti->num_discard_bios = 1;
2563
2564 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
2565 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
2566
2567 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
2568 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
2569 else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
2570 if (val == 0 || val > MAX_TAG_SIZE) {
2571 ti->error = "Invalid integrity arguments";
2572 return -EINVAL;
2573 }
2574 cc->on_disk_tag_size = val;
2575 sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
2576 if (!strcasecmp(sval, "aead")) {
2577 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002578 } else if (strcasecmp(sval, "none")) {
2579 ti->error = "Unknown integrity profile";
2580 return -EINVAL;
2581 }
2582
2583 cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
2584 if (!cc->cipher_auth)
2585 return -ENOMEM;
Mikulas Patockaff3af922017-03-23 10:23:14 -04002586 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002587 if (cc->sector_size < (1 << SECTOR_SHIFT) ||
2588 cc->sector_size > 4096 ||
Mikulas Patockaff3af922017-03-23 10:23:14 -04002589 (cc->sector_size & (cc->sector_size - 1))) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002590 ti->error = "Invalid feature value for sector_size";
2591 return -EINVAL;
2592 }
Milan Broz783874b2017-09-13 15:45:56 +02002593 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) {
2594 ti->error = "Device size is not multiple of sector_size feature";
2595 return -EINVAL;
2596 }
Mikulas Patockaff3af922017-03-23 10:23:14 -04002597 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT;
Milan Broz8f0009a2017-03-16 15:39:44 +01002598 } else if (!strcasecmp(opt_string, "iv_large_sectors"))
2599 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
2600 else {
Milan Brozef43aa32017-01-04 20:23:54 +01002601 ti->error = "Invalid feature arguments";
2602 return -EINVAL;
2603 }
2604 }
2605
2606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607}
2608
2609/*
2610 * Construct an encryption mapping:
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002611 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 */
2613static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2614{
2615 struct crypt_config *cc;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002616 int key_size;
Milan Brozef43aa32017-01-04 20:23:54 +01002617 unsigned int align_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 unsigned long long tmpll;
2619 int ret;
Milan Brozef43aa32017-01-04 20:23:54 +01002620 size_t iv_size_padding, additional_req_size;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002621 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Milan Broz772ae5f2011-08-02 12:32:08 +01002623 if (argc < 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 ti->error = "Not enough arguments";
2625 return -EINVAL;
2626 }
2627
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002628 key_size = get_key_size(&argv[1]);
2629 if (key_size < 0) {
2630 ti->error = "Cannot parse key size";
2631 return -EINVAL;
2632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
2634 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
2635 if (!cc) {
2636 ti->error = "Cannot allocate encryption context";
2637 return -ENOMEM;
2638 }
2639 cc->key_size = key_size;
Milan Broz8f0009a2017-03-16 15:39:44 +01002640 cc->sector_size = (1 << SECTOR_SHIFT);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002641 cc->sector_shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 ti->private = cc;
Milan Brozef43aa32017-01-04 20:23:54 +01002644
2645 /* Optional parameters need to be read before cipher constructor */
2646 if (argc > 5) {
2647 ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
2648 if (ret)
2649 goto bad;
2650 }
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
2653 if (ret < 0)
2654 goto bad;
2655
Milan Broz33d2f092017-03-16 15:39:40 +01002656 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +01002657 cc->dmreq_start = sizeof(struct aead_request);
2658 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
2659 align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
2660 } else {
2661 cc->dmreq_start = sizeof(struct skcipher_request);
2662 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
2663 align_mask = crypto_skcipher_alignmask(any_tfm(cc));
2664 }
Mikulas Patockad49ec522014-08-28 11:09:31 -04002665 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
2666
Milan Brozef43aa32017-01-04 20:23:54 +01002667 if (align_mask < CRYPTO_MINALIGN) {
Mikulas Patockad49ec522014-08-28 11:09:31 -04002668 /* Allocate the padding exactly */
2669 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
Milan Brozef43aa32017-01-04 20:23:54 +01002670 & align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002671 } else {
2672 /*
2673 * If the cipher requires greater alignment than kmalloc
2674 * alignment, we don't know the exact position of the
2675 * initialization vector. We must assume worst case.
2676 */
Milan Brozef43aa32017-01-04 20:23:54 +01002677 iv_size_padding = align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Mikulas Patocka94f5e022015-02-13 08:25:26 -05002680 ret = -ENOMEM;
Milan Brozef43aa32017-01-04 20:23:54 +01002681
2682 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
2683 additional_req_size = sizeof(struct dm_crypt_request) +
2684 iv_size_padding + cc->iv_size +
2685 cc->iv_size +
2686 sizeof(uint64_t) +
2687 sizeof(unsigned int);
2688
2689 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start + additional_req_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 if (!cc->req_pool) {
2691 ti->error = "Cannot allocate crypt request mempool";
2692 goto bad;
2693 }
2694
Mike Snitzer30187e12016-01-31 13:28:26 -05002695 cc->per_bio_data_size = ti->per_io_data_size =
Milan Brozef43aa32017-01-04 20:23:54 +01002696 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
Mikulas Patockad49ec522014-08-28 11:09:31 -04002697 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002698
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05002699 cc->page_pool = mempool_create_page_pool(BIO_MAX_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 if (!cc->page_pool) {
Milan Broz8b004452006-10-03 01:15:37 -07002701 ti->error = "Cannot allocate page mempool";
Milan Broze48d4bb2006-10-03 01:15:37 -07002702 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002704
NeilBrown47e0fb42017-06-18 14:38:57 +10002705 cc->bs = bioset_create(MIN_IOS, 0, (BIOSET_NEED_BVECS |
2706 BIOSET_NEED_RESCUER));
Milan Broz8b004452006-10-03 01:15:37 -07002707 if (!cc->bs) {
Milan Broz0c395b02008-02-08 02:10:54 +00002708 ti->error = "Cannot allocate crypt bioset";
Milan Brozcabf08e2007-10-19 22:38:58 +01002709 goto bad;
Milan Broz93e605c2006-10-03 01:15:38 -07002710 }
Milan Brozcabf08e2007-10-19 22:38:58 +01002711
Mikulas Patocka7145c242015-02-13 08:24:41 -05002712 mutex_init(&cc->bio_alloc_lock);
2713
Milan Brozcabf08e2007-10-19 22:38:58 +01002714 ret = -EINVAL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002715 if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
2716 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002717 ti->error = "Invalid iv_offset sector";
2718 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002720 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Vivek Goyale80d1c82015-07-31 09:20:36 -04002722 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
2723 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 ti->error = "Device lookup failed";
2725 goto bad;
2726 }
2727
Vivek Goyale80d1c82015-07-31 09:20:36 -04002728 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002729 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 ti->error = "Invalid device sector";
2731 goto bad;
2732 }
2733 cc->start = tmpll;
2734
Milan Broz33d2f092017-03-16 15:39:40 +01002735 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
Milan Brozef43aa32017-01-04 20:23:54 +01002736 ret = crypt_integrity_ctr(cc, ti);
Milan Broz772ae5f2011-08-02 12:32:08 +01002737 if (ret)
2738 goto bad;
2739
Milan Brozef43aa32017-01-04 20:23:54 +01002740 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
2741 if (!cc->tag_pool_max_sectors)
2742 cc->tag_pool_max_sectors = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01002743
Milan Brozef43aa32017-01-04 20:23:54 +01002744 cc->tag_pool = mempool_create_kmalloc_pool(MIN_IOS,
2745 cc->tag_pool_max_sectors * cc->on_disk_tag_size);
2746 if (!cc->tag_pool) {
2747 ti->error = "Cannot allocate integrity tags mempool";
2748 goto bad;
Milan Broz772ae5f2011-08-02 12:32:08 +01002749 }
Mikulas Patocka583fe742017-04-18 16:51:54 -04002750
2751 cc->tag_pool_max_sectors <<= cc->sector_shift;
Milan Broz772ae5f2011-08-02 12:32:08 +01002752 }
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 ret = -ENOMEM;
Tim Murraya1b89132017-04-21 11:11:36 +02002755 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if (!cc->io_queue) {
2757 ti->error = "Couldn't create kcryptd io queue";
2758 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
Christophe Saout37af6562006-10-30 20:39:08 +01002760
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002761 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
Tim Murraya1b89132017-04-21 11:11:36 +02002762 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002763 else
Tim Murraya1b89132017-04-21 11:11:36 +02002764 cc->crypt_queue = alloc_workqueue("kcryptd",
2765 WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002766 num_online_cpus());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 if (!cc->crypt_queue) {
2768 ti->error = "Couldn't create kcryptd queue";
2769 goto bad;
2770 }
2771
Mikulas Patockadc267622015-02-13 08:25:59 -05002772 init_waitqueue_head(&cc->write_thread_wait);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05002773 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05002774
2775 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write");
2776 if (IS_ERR(cc->write_thread)) {
2777 ret = PTR_ERR(cc->write_thread);
2778 cc->write_thread = NULL;
2779 ti->error = "Couldn't spawn write thread";
2780 goto bad;
2781 }
2782 wake_up_process(cc->write_thread);
2783
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002784 ti->num_flush_bios = 1;
Milan Broz983c7db2011-09-25 23:26:21 +01002785
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return 0;
2787
2788bad:
2789 crypt_dtr(ti);
2790 return ret;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002791}
2792
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002793static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
2795 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01002796 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002797
Milan Broz772ae5f2011-08-02 12:32:08 +01002798 /*
Mike Christie28a8f0d2016-06-05 14:32:25 -05002799 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
2800 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
Mike Christiee6047142016-06-05 14:32:04 -05002801 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
Milan Broz772ae5f2011-08-02 12:32:08 +01002802 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06002803 if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
Mike Christie28a8f0d2016-06-05 14:32:25 -05002804 bio_op(bio) == REQ_OP_DISCARD)) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02002805 bio_set_dev(bio, cc->dev->bdev);
Milan Broz772ae5f2011-08-02 12:32:08 +01002806 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07002807 bio->bi_iter.bi_sector = cc->start +
2808 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002809 return DM_MAPIO_REMAPPED;
2810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002812 /*
2813 * Check if bio is too large, split as needed.
2814 */
2815 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
Milan Brozef43aa32017-01-04 20:23:54 +01002816 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002817 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
2818
Milan Broz8f0009a2017-03-16 15:39:44 +01002819 /*
2820 * Ensure that bio is a multiple of internal sector encryption size
2821 * and is aligned to this size as defined in IO hints.
2822 */
2823 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
Christoph Hellwig846785e2017-06-03 09:38:02 +02002824 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002825
2826 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
Christoph Hellwig846785e2017-06-03 09:38:02 +02002827 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002828
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002829 io = dm_per_bio_data(bio, cc->per_bio_data_size);
2830 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Milan Brozef43aa32017-01-04 20:23:54 +01002831
2832 if (cc->on_disk_tag_size) {
Mikulas Patocka583fe742017-04-18 16:51:54 -04002833 unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift);
Milan Brozef43aa32017-01-04 20:23:54 +01002834
2835 if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
Mikulas Patocka583fe742017-04-18 16:51:54 -04002836 unlikely(!(io->integrity_metadata = kmalloc(tag_len,
Milan Brozef43aa32017-01-04 20:23:54 +01002837 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
2838 if (bio_sectors(bio) > cc->tag_pool_max_sectors)
2839 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
2840 io->integrity_metadata = mempool_alloc(cc->tag_pool, GFP_NOIO);
2841 io->integrity_metadata_from_pool = true;
2842 }
2843 }
2844
Milan Broz33d2f092017-03-16 15:39:40 +01002845 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002846 io->ctx.r.req_aead = (struct aead_request *)(io + 1);
2847 else
2848 io->ctx.r.req = (struct skcipher_request *)(io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Milan Broz20c82532011-01-13 19:59:53 +00002850 if (bio_data_dir(io->base_bio) == READ) {
2851 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05002852 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00002853 } else
Andrew Morton4ee218c2006-03-27 01:17:48 -08002854 kcryptd_queue_crypt(io);
2855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 return DM_MAPIO_SUBMITTED;
2857}
2858
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002859static void crypt_status(struct dm_target *ti, status_type_t type,
2860 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
Milan Broz5ebaee62010-08-12 04:14:07 +01002862 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002863 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002864 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 switch (type) {
2867 case STATUSTYPE_INFO:
2868 result[0] = '\0';
2869 break;
2870
2871 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00002872 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002874 if (cc->key_size > 0) {
2875 if (cc->key_string)
2876 DMEMIT(":%u:%s", cc->key_size, cc->key_string);
2877 else
2878 for (i = 0; i < cc->key_size; i++)
2879 DMEMIT("%02x", cc->key[i]);
2880 } else
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002881 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
2884 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01002885
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002886 num_feature_args += !!ti->num_discard_bios;
2887 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002888 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002889 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT);
Milan Broz8f0009a2017-03-16 15:39:44 +01002890 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002891 if (cc->on_disk_tag_size)
2892 num_feature_args++;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002893 if (num_feature_args) {
2894 DMEMIT(" %d", num_feature_args);
2895 if (ti->num_discard_bios)
2896 DMEMIT(" allow_discards");
2897 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2898 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002899 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
2900 DMEMIT(" submit_from_crypt_cpus");
Milan Brozef43aa32017-01-04 20:23:54 +01002901 if (cc->on_disk_tag_size)
2902 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
Milan Broz8f0009a2017-03-16 15:39:44 +01002903 if (cc->sector_size != (1 << SECTOR_SHIFT))
2904 DMEMIT(" sector_size:%d", cc->sector_size);
2905 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
2906 DMEMIT(" iv_large_sectors");
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002907 }
Milan Broz772ae5f2011-08-02 12:32:08 +01002908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 break;
2910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911}
2912
Milan Broze48d4bb2006-10-03 01:15:37 -07002913static void crypt_postsuspend(struct dm_target *ti)
2914{
2915 struct crypt_config *cc = ti->private;
2916
2917 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2918}
2919
2920static int crypt_preresume(struct dm_target *ti)
2921{
2922 struct crypt_config *cc = ti->private;
2923
2924 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
2925 DMERR("aborting resume - crypt key is not set.");
2926 return -EAGAIN;
2927 }
2928
2929 return 0;
2930}
2931
2932static void crypt_resume(struct dm_target *ti)
2933{
2934 struct crypt_config *cc = ti->private;
2935
2936 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2937}
2938
2939/* Message interface
2940 * key set <key>
2941 * key wipe
2942 */
2943static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
2944{
2945 struct crypt_config *cc = ti->private;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002946 int key_size, ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07002947
2948 if (argc < 2)
2949 goto error;
2950
Mike Snitzer498f0102011-08-02 12:32:04 +01002951 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07002952 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
2953 DMWARN("not suspended during key manipulation.");
2954 return -EINVAL;
2955 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002956 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002957 /* The key size may not be changed. */
2958 key_size = get_key_size(&argv[2]);
2959 if (key_size < 0 || cc->key_size != key_size) {
2960 memset(argv[2], '0', strlen(argv[2]));
2961 return -EINVAL;
2962 }
2963
Milan Broz542da312009-12-10 23:51:57 +00002964 ret = crypt_set_key(cc, argv[2]);
2965 if (ret)
2966 return ret;
2967 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
2968 ret = cc->iv_gen_ops->init(cc);
2969 return ret;
2970 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002971 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00002972 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
2973 ret = cc->iv_gen_ops->wipe(cc);
2974 if (ret)
2975 return ret;
2976 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002977 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00002978 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002979 }
2980
2981error:
2982 DMWARN("unrecognised message received.");
2983 return -EINVAL;
2984}
2985
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002986static int crypt_iterate_devices(struct dm_target *ti,
2987 iterate_devices_callout_fn fn, void *data)
2988{
2989 struct crypt_config *cc = ti->private;
2990
Mike Snitzer5dea2712009-07-23 20:30:42 +01002991 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002992}
2993
Mike Snitzer586b2862015-09-09 21:34:51 -04002994static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
2995{
Milan Broz8f0009a2017-03-16 15:39:44 +01002996 struct crypt_config *cc = ti->private;
2997
Mike Snitzer586b2862015-09-09 21:34:51 -04002998 /*
2999 * Unfortunate constraint that is required to avoid the potential
3000 * for exceeding underlying device's max_segments limits -- due to
3001 * crypt_alloc_buffer() possibly allocating pages for the encryption
3002 * bio that are not as physically contiguous as the original bio.
3003 */
3004 limits->max_segment_size = PAGE_SIZE;
Milan Broz8f0009a2017-03-16 15:39:44 +01003005
3006 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
3007 limits->logical_block_size = cc->sector_size;
3008 limits->physical_block_size = cc->sector_size;
3009 blk_limits_io_min(limits, cc->sector_size);
3010 }
Mike Snitzer586b2862015-09-09 21:34:51 -04003011}
3012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013static struct target_type crypt_target = {
3014 .name = "crypt",
Milan Broz7e3fd852017-06-06 09:07:01 +02003015 .version = {1, 18, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 .module = THIS_MODULE,
3017 .ctr = crypt_ctr,
3018 .dtr = crypt_dtr,
3019 .map = crypt_map,
3020 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07003021 .postsuspend = crypt_postsuspend,
3022 .preresume = crypt_preresume,
3023 .resume = crypt_resume,
3024 .message = crypt_message,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003025 .iterate_devices = crypt_iterate_devices,
Mike Snitzer586b2862015-09-09 21:34:51 -04003026 .io_hints = crypt_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027};
3028
3029static int __init dm_crypt_init(void)
3030{
3031 int r;
3032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05003034 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07003035 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return r;
3038}
3039
3040static void __exit dm_crypt_exit(void)
3041{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00003042 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}
3044
3045module_init(dm_crypt_init);
3046module_exit(dm_crypt_exit);
3047
Jana Saoutbf142992014-06-24 14:27:04 -04003048MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
3050MODULE_LICENSE("GPL");