blob: e8e570d000f6246a0183e817f170a9373fe82a82 [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;
Milan Broz53017032008-02-08 02:10:38 +000074 int 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 */
132 CRYPT_MODE_INTEGRITY_HMAC, /* Compose authenticated mode from normal mode and HMAC */
133};
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;
175
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100176 /* ESSIV: struct crypto_cipher *essiv_tfm */
177 void *iv_private;
Milan Brozef43aa32017-01-04 20:23:54 +0100178 union {
179 struct crypto_skcipher **tfms;
180 struct crypto_aead **tfms_aead;
181 } cipher_tfm;
Milan Brozd1f96422011-01-13 19:59:54 +0000182 unsigned tfms_count;
Milan Brozef43aa32017-01-04 20:23:54 +0100183 unsigned long cipher_flags;
Andi Kleenc0297722011-01-13 19:59:53 +0000184
185 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000186 * Layout of each crypto request:
187 *
Herbert Xubbdb23b2016-01-24 21:16:36 +0800188 * struct skcipher_request
Milan Brozddd42ed2008-02-08 02:11:07 +0000189 * context
190 * padding
191 * struct dm_crypt_request
192 * padding
193 * IV
194 *
195 * The padding is added so that dm_crypt_request and the IV are
196 * correctly aligned.
197 */
198 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000199
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400200 unsigned int per_bio_data_size;
201
Milan Broze48d4bb2006-10-03 01:15:37 -0700202 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100204 unsigned int key_parts; /* independent parts in key buffer */
205 unsigned int key_extra_size; /* additional keys length */
Milan Brozef43aa32017-01-04 20:23:54 +0100206 unsigned int key_mac_size; /* MAC key size for authenc(...) */
207
208 unsigned int integrity_tag_size;
209 unsigned int integrity_iv_size;
210 unsigned int on_disk_tag_size;
211
212 u8 *authenc_key; /* space for keys in authenc() format (if used) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 u8 key[0];
214};
215
Milan Brozef43aa32017-01-04 20:23:54 +0100216#define MIN_IOS 64
217#define MAX_TAG_SIZE 480
218#define POOL_ENTRY_SIZE 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100220static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000221static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Brozef43aa32017-01-04 20:23:54 +0100222static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
223 struct scatterlist *sg);
Olaf Kirch027581f2007-05-09 02:32:52 -0700224
Andi Kleenc0297722011-01-13 19:59:53 +0000225/*
226 * Use this to access cipher attributes that are the same for each CPU.
227 */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800228static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +0000229{
Milan Brozef43aa32017-01-04 20:23:54 +0100230 return cc->cipher_tfm.tfms[0];
231}
232
233static struct crypto_aead *any_tfm_aead(struct crypt_config *cc)
234{
235 return cc->cipher_tfm.tfms_aead[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 * Different IV generation algorithms:
240 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000241 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200242 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 *
Milan Broz61afef62009-12-10 23:52:25 +0000244 * plain64: the initial vector is the 64-bit little-endian version of the sector
245 * number, padded with zeros if necessary.
246 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000247 * essiv: "encrypted sector|salt initial vector", the sector number is
248 * encrypted with the bulk cipher using a salt as key. The salt
249 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 *
Rik Snel48527fa2006-09-03 08:56:39 +1000251 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
252 * (needed for LRW-32-AES and possible other narrow block modes)
253 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700254 * null: the initial vector is always zero. Provides compatibility with
255 * obsolete loop_fish2 devices. Do not use for new devices.
256 *
Milan Broz34745782011-01-13 19:59:55 +0000257 * lmk: Compatible implementation of the block chaining mode used
258 * by the Loop-AES block device encryption system
259 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
260 * It operates on full 512 byte sectors and uses CBC
261 * with an IV derived from the sector number, the data and
262 * optionally extra IV seed.
263 * This means that after decryption the first block
264 * of sector must be tweaked according to decrypted data.
265 * Loop-AES can use three encryption schemes:
266 * version 1: is plain aes-cbc mode
267 * version 2: uses 64 multikey scheme with lmk IV generator
268 * version 3: the same as version 2 with additional IV seed
269 * (it uses 65 keys, last key is used as IV seed)
270 *
Milan Brozed04d982013-10-28 23:21:04 +0100271 * tcw: Compatible implementation of the block chaining mode used
272 * by the TrueCrypt device encryption system (prior to version 4.1).
Milan Broze44f23b2015-04-05 18:03:10 +0200273 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
Milan Brozed04d982013-10-28 23:21:04 +0100274 * It operates on full 512 byte sectors and uses CBC
275 * with an IV derived from initial key and the sector number.
276 * In addition, whitening value is applied on every sector, whitening
277 * is calculated from initial key, sector number and mixed using CRC32.
278 * Note that this encryption scheme is vulnerable to watermarking attacks
279 * and should be used for old compatible containers access only.
280 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * plumb: unimplemented, see:
282 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
283 */
284
Milan Broz2dc5327d32011-01-13 19:59:54 +0000285static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
286 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100289 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 return 0;
292}
293
Milan Broz61afef62009-12-10 23:52:25 +0000294static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc5327d32011-01-13 19:59:54 +0000295 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000296{
297 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100298 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000299
300 return 0;
301}
302
Milan Brozb95bf2d2009-12-10 23:51:56 +0000303/* Initialise ESSIV - compute salt but no local memory allocations */
304static int crypt_iv_essiv_init(struct crypt_config *cc)
305{
306 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800307 AHASH_REQUEST_ON_STACK(req, essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000308 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000309 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100310 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000311
312 sg_init_one(&sg, cc->key, cc->key_size);
Herbert Xubbdb23b2016-01-24 21:16:36 +0800313 ahash_request_set_tfm(req, essiv->hash_tfm);
314 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
315 ahash_request_set_crypt(req, &sg, essiv->salt, cc->key_size);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000316
Herbert Xubbdb23b2016-01-24 21:16:36 +0800317 err = crypto_ahash_digest(req);
318 ahash_request_zero(req);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000319 if (err)
320 return err;
321
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100322 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000323
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100324 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800325 crypto_ahash_digestsize(essiv->hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100326 if (err)
327 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000328
329 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000330}
331
Milan Broz542da312009-12-10 23:51:57 +0000332/* Wipe salt and reset key derived from volume key */
333static int crypt_iv_essiv_wipe(struct crypt_config *cc)
334{
335 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800336 unsigned salt_size = crypto_ahash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000337 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100338 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000339
340 memset(essiv->salt, 0, salt_size);
341
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100342 essiv_tfm = cc->iv_private;
343 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
344 if (r)
345 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000346
347 return err;
348}
349
350/* Set up per cpu cipher state */
351static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
352 struct dm_target *ti,
353 u8 *salt, unsigned saltsize)
354{
355 struct crypto_cipher *essiv_tfm;
356 int err;
357
358 /* Setup the essiv_tfm with the given salt */
359 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
360 if (IS_ERR(essiv_tfm)) {
361 ti->error = "Error allocating crypto tfm for ESSIV";
362 return essiv_tfm;
363 }
364
Milan Brozef43aa32017-01-04 20:23:54 +0100365 if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) {
Andi Kleenc0297722011-01-13 19:59:53 +0000366 ti->error = "Block size of ESSIV cipher does "
367 "not match IV size of block cipher";
368 crypto_free_cipher(essiv_tfm);
369 return ERR_PTR(-EINVAL);
370 }
371
372 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
373 if (err) {
374 ti->error = "Failed to set key for ESSIV cipher";
375 crypto_free_cipher(essiv_tfm);
376 return ERR_PTR(err);
377 }
378
379 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000380}
381
Milan Broz60473592009-12-10 23:51:55 +0000382static void crypt_iv_essiv_dtr(struct crypt_config *cc)
383{
Andi Kleenc0297722011-01-13 19:59:53 +0000384 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000385 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
386
Herbert Xubbdb23b2016-01-24 21:16:36 +0800387 crypto_free_ahash(essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000388 essiv->hash_tfm = NULL;
389
390 kzfree(essiv->salt);
391 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000392
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100393 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000394
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100395 if (essiv_tfm)
396 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000397
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100398 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100402 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Milan Broz5861f1b2009-12-10 23:51:56 +0000404 struct crypto_cipher *essiv_tfm = NULL;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800405 struct crypto_ahash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000406 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100407 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Milan Broz5861f1b2009-12-10 23:51:56 +0000409 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700410 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 return -EINVAL;
412 }
413
Milan Brozb95bf2d2009-12-10 23:51:56 +0000414 /* Allocate hash algorithm */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800415 hash_tfm = crypto_alloc_ahash(opts, 0, CRYPTO_ALG_ASYNC);
Herbert Xu35058682006-08-24 19:10:20 +1000416 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700417 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000418 err = PTR_ERR(hash_tfm);
419 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
Herbert Xubbdb23b2016-01-24 21:16:36 +0800422 salt = kzalloc(crypto_ahash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000423 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700424 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000425 err = -ENOMEM;
426 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
Milan Brozb95bf2d2009-12-10 23:51:56 +0000429 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000430 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
431
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100432 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800433 crypto_ahash_digestsize(hash_tfm));
Milan Brozef43aa32017-01-04 20:23:54 +0100434
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100435 if (IS_ERR(essiv_tfm)) {
436 crypt_iv_essiv_dtr(cc);
437 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000438 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100439 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000442
443bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000444 if (hash_tfm && !IS_ERR(hash_tfm))
Herbert Xubbdb23b2016-01-24 21:16:36 +0800445 crypto_free_ahash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000446 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000447 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Milan Broz2dc5327d32011-01-13 19:59:54 +0000450static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
451 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100453 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100456 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000457 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return 0;
460}
461
Rik Snel48527fa2006-09-03 08:56:39 +1000462static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
463 const char *opts)
464{
Herbert Xubbdb23b2016-01-24 21:16:36 +0800465 unsigned bs = crypto_skcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800466 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000467
468 /* we need to calculate how far we must shift the sector count
469 * to get the cipher block count, we use this shift in _gen */
470
471 if (1 << log != bs) {
472 ti->error = "cypher blocksize is not a power of 2";
473 return -EINVAL;
474 }
475
476 if (log > 9) {
477 ti->error = "cypher blocksize is > 512";
478 return -EINVAL;
479 }
480
Milan Broz60473592009-12-10 23:51:55 +0000481 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000482
483 return 0;
484}
485
486static void crypt_iv_benbi_dtr(struct crypt_config *cc)
487{
Rik Snel48527fa2006-09-03 08:56:39 +1000488}
489
Milan Broz2dc5327d32011-01-13 19:59:54 +0000490static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
491 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000492{
Herbert Xu79066ad2006-12-05 13:41:52 -0800493 __be64 val;
494
Rik Snel48527fa2006-09-03 08:56:39 +1000495 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800496
Milan Broz2dc5327d32011-01-13 19:59:54 +0000497 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800498 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
501}
502
Milan Broz2dc5327d32011-01-13 19:59:54 +0000503static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
504 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700505{
506 memset(iv, 0, cc->iv_size);
507
508 return 0;
509}
510
Milan Broz34745782011-01-13 19:59:55 +0000511static void crypt_iv_lmk_dtr(struct crypt_config *cc)
512{
513 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
514
515 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
516 crypto_free_shash(lmk->hash_tfm);
517 lmk->hash_tfm = NULL;
518
519 kzfree(lmk->seed);
520 lmk->seed = NULL;
521}
522
523static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
524 const char *opts)
525{
526 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
527
528 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
529 if (IS_ERR(lmk->hash_tfm)) {
530 ti->error = "Error initializing LMK hash";
531 return PTR_ERR(lmk->hash_tfm);
532 }
533
534 /* No seed in LMK version 2 */
535 if (cc->key_parts == cc->tfms_count) {
536 lmk->seed = NULL;
537 return 0;
538 }
539
540 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
541 if (!lmk->seed) {
542 crypt_iv_lmk_dtr(cc);
543 ti->error = "Error kmallocing seed storage in LMK";
544 return -ENOMEM;
545 }
546
547 return 0;
548}
549
550static int crypt_iv_lmk_init(struct crypt_config *cc)
551{
552 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
553 int subkey_size = cc->key_size / cc->key_parts;
554
555 /* LMK seed is on the position of LMK_KEYS + 1 key */
556 if (lmk->seed)
557 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
558 crypto_shash_digestsize(lmk->hash_tfm));
559
560 return 0;
561}
562
563static int crypt_iv_lmk_wipe(struct crypt_config *cc)
564{
565 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
566
567 if (lmk->seed)
568 memset(lmk->seed, 0, LMK_SEED_SIZE);
569
570 return 0;
571}
572
573static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
574 struct dm_crypt_request *dmreq,
575 u8 *data)
576{
577 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200578 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000579 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100580 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000581 int i, r;
582
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200583 desc->tfm = lmk->hash_tfm;
584 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Broz34745782011-01-13 19:59:55 +0000585
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200586 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000587 if (r)
588 return r;
589
590 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200591 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000592 if (r)
593 return r;
594 }
595
596 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200597 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000598 if (r)
599 return r;
600
601 /* Sector is cropped to 56 bits here */
602 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
603 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
604 buf[2] = cpu_to_le32(4024);
605 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200606 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000607 if (r)
608 return r;
609
610 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200611 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000612 if (r)
613 return r;
614
615 for (i = 0; i < MD5_HASH_WORDS; i++)
616 __cpu_to_le32s(&md5state.hash[i]);
617 memcpy(iv, &md5state.hash, cc->iv_size);
618
619 return 0;
620}
621
622static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
623 struct dm_crypt_request *dmreq)
624{
Milan Brozef43aa32017-01-04 20:23:54 +0100625 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000626 u8 *src;
627 int r = 0;
628
629 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100630 sg = crypt_get_sg_data(cc, dmreq->sg_in);
631 src = kmap_atomic(sg_page(sg));
632 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800633 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000634 } else
635 memset(iv, 0, cc->iv_size);
636
637 return r;
638}
639
640static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
641 struct dm_crypt_request *dmreq)
642{
Milan Brozef43aa32017-01-04 20:23:54 +0100643 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000644 u8 *dst;
645 int r;
646
647 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
648 return 0;
649
Milan Brozef43aa32017-01-04 20:23:54 +0100650 sg = crypt_get_sg_data(cc, dmreq->sg_out);
651 dst = kmap_atomic(sg_page(sg));
652 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset);
Milan Broz34745782011-01-13 19:59:55 +0000653
654 /* Tweak the first block of plaintext sector */
655 if (!r)
Milan Brozef43aa32017-01-04 20:23:54 +0100656 crypto_xor(dst + sg->offset, iv, cc->iv_size);
Milan Broz34745782011-01-13 19:59:55 +0000657
Cong Wangc2e022c2011-11-28 13:26:02 +0800658 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000659 return r;
660}
661
Milan Brozed04d982013-10-28 23:21:04 +0100662static void crypt_iv_tcw_dtr(struct crypt_config *cc)
663{
664 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
665
666 kzfree(tcw->iv_seed);
667 tcw->iv_seed = NULL;
668 kzfree(tcw->whitening);
669 tcw->whitening = NULL;
670
671 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
672 crypto_free_shash(tcw->crc32_tfm);
673 tcw->crc32_tfm = NULL;
674}
675
676static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
677 const char *opts)
678{
679 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
680
681 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
682 ti->error = "Wrong key size for TCW";
683 return -EINVAL;
684 }
685
686 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
687 if (IS_ERR(tcw->crc32_tfm)) {
688 ti->error = "Error initializing CRC32 in TCW";
689 return PTR_ERR(tcw->crc32_tfm);
690 }
691
692 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
693 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
694 if (!tcw->iv_seed || !tcw->whitening) {
695 crypt_iv_tcw_dtr(cc);
696 ti->error = "Error allocating seed storage in TCW";
697 return -ENOMEM;
698 }
699
700 return 0;
701}
702
703static int crypt_iv_tcw_init(struct crypt_config *cc)
704{
705 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
706 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
707
708 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
709 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
710 TCW_WHITENING_SIZE);
711
712 return 0;
713}
714
715static int crypt_iv_tcw_wipe(struct crypt_config *cc)
716{
717 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
718
719 memset(tcw->iv_seed, 0, cc->iv_size);
720 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
721
722 return 0;
723}
724
725static int crypt_iv_tcw_whitening(struct crypt_config *cc,
726 struct dm_crypt_request *dmreq,
727 u8 *data)
728{
729 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200730 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100731 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200732 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100733 int i, r;
734
735 /* xor whitening with sector number */
736 memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
737 crypto_xor(buf, (u8 *)&sector, 8);
738 crypto_xor(&buf[8], (u8 *)&sector, 8);
739
740 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200741 desc->tfm = tcw->crc32_tfm;
742 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Brozed04d982013-10-28 23:21:04 +0100743 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200744 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100745 if (r)
746 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200747 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100748 if (r)
749 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200750 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100751 if (r)
752 goto out;
753 }
754 crypto_xor(&buf[0], &buf[12], 4);
755 crypto_xor(&buf[4], &buf[8], 4);
756
757 /* apply whitening (8 bytes) to whole sector */
758 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
759 crypto_xor(data + i * 8, buf, 8);
760out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100761 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100762 return r;
763}
764
765static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
766 struct dm_crypt_request *dmreq)
767{
Milan Brozef43aa32017-01-04 20:23:54 +0100768 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100769 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200770 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100771 u8 *src;
772 int r = 0;
773
774 /* Remove whitening from ciphertext */
775 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100776 sg = crypt_get_sg_data(cc, dmreq->sg_in);
777 src = kmap_atomic(sg_page(sg));
778 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100779 kunmap_atomic(src);
780 }
781
782 /* Calculate IV */
783 memcpy(iv, tcw->iv_seed, cc->iv_size);
784 crypto_xor(iv, (u8 *)&sector, 8);
785 if (cc->iv_size > 8)
786 crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
787
788 return r;
789}
790
791static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
792 struct dm_crypt_request *dmreq)
793{
Milan Brozef43aa32017-01-04 20:23:54 +0100794 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100795 u8 *dst;
796 int r;
797
798 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
799 return 0;
800
801 /* Apply whitening on ciphertext */
Milan Brozef43aa32017-01-04 20:23:54 +0100802 sg = crypt_get_sg_data(cc, dmreq->sg_out);
803 dst = kmap_atomic(sg_page(sg));
804 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100805 kunmap_atomic(dst);
806
807 return r;
808}
809
Milan Brozef43aa32017-01-04 20:23:54 +0100810static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv,
811 struct dm_crypt_request *dmreq)
812{
813 /* Used only for writes, there must be an additional space to store IV */
814 get_random_bytes(iv, cc->iv_size);
815 return 0;
816}
817
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100818static const struct crypt_iv_operations crypt_iv_plain_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 .generator = crypt_iv_plain_gen
820};
821
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100822static const struct crypt_iv_operations crypt_iv_plain64_ops = {
Milan Broz61afef62009-12-10 23:52:25 +0000823 .generator = crypt_iv_plain64_gen
824};
825
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100826static const struct crypt_iv_operations crypt_iv_essiv_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 .ctr = crypt_iv_essiv_ctr,
828 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000829 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000830 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 .generator = crypt_iv_essiv_gen
832};
833
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100834static const struct crypt_iv_operations crypt_iv_benbi_ops = {
Rik Snel48527fa2006-09-03 08:56:39 +1000835 .ctr = crypt_iv_benbi_ctr,
836 .dtr = crypt_iv_benbi_dtr,
837 .generator = crypt_iv_benbi_gen
838};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100840static const struct crypt_iv_operations crypt_iv_null_ops = {
Ludwig Nussel46b47732007-05-09 02:32:55 -0700841 .generator = crypt_iv_null_gen
842};
843
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100844static const struct crypt_iv_operations crypt_iv_lmk_ops = {
Milan Broz34745782011-01-13 19:59:55 +0000845 .ctr = crypt_iv_lmk_ctr,
846 .dtr = crypt_iv_lmk_dtr,
847 .init = crypt_iv_lmk_init,
848 .wipe = crypt_iv_lmk_wipe,
849 .generator = crypt_iv_lmk_gen,
850 .post = crypt_iv_lmk_post
851};
852
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100853static const struct crypt_iv_operations crypt_iv_tcw_ops = {
Milan Brozed04d982013-10-28 23:21:04 +0100854 .ctr = crypt_iv_tcw_ctr,
855 .dtr = crypt_iv_tcw_dtr,
856 .init = crypt_iv_tcw_init,
857 .wipe = crypt_iv_tcw_wipe,
858 .generator = crypt_iv_tcw_gen,
859 .post = crypt_iv_tcw_post
860};
861
Milan Brozef43aa32017-01-04 20:23:54 +0100862static struct crypt_iv_operations crypt_iv_random_ops = {
863 .generator = crypt_iv_random_gen
864};
865
866/*
867 * Integrity extensions
868 */
869static bool crypt_integrity_aead(struct crypt_config *cc)
870{
871 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
872}
873
874static bool crypt_integrity_hmac(struct crypt_config *cc)
875{
876 return test_bit(CRYPT_MODE_INTEGRITY_HMAC, &cc->cipher_flags);
877}
878
879static bool crypt_integrity_mode(struct crypt_config *cc)
880{
881 return crypt_integrity_aead(cc) || crypt_integrity_hmac(cc);
882}
883
884/* Get sg containing data */
885static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
886 struct scatterlist *sg)
887{
888 if (unlikely(crypt_integrity_mode(cc)))
889 return &sg[2];
890
891 return sg;
892}
893
894static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
895{
896 struct bio_integrity_payload *bip;
897 unsigned int tag_len;
898 int ret;
899
900 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size)
901 return 0;
902
903 bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
904 if (IS_ERR(bip))
905 return PTR_ERR(bip);
906
907 tag_len = io->cc->on_disk_tag_size * bio_sectors(bio);
908
909 bip->bip_iter.bi_size = tag_len;
910 bip->bip_iter.bi_sector = io->cc->start + io->sector;
911
912 /* We own the metadata, do not let bio_free to release it */
913 bip->bip_flags &= ~BIP_BLOCK_INTEGRITY;
914
915 ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
916 tag_len, offset_in_page(io->integrity_metadata));
917 if (unlikely(ret != tag_len))
918 return -ENOMEM;
919
920 return 0;
921}
922
923static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
924{
925#ifdef CONFIG_BLK_DEV_INTEGRITY
926 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk);
927
928 /* From now we require underlying device with our integrity profile */
929 if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) {
930 ti->error = "Integrity profile not supported.";
931 return -EINVAL;
932 }
933
934 if (bi->tag_size != cc->on_disk_tag_size) {
935 ti->error = "Integrity profile tag size mismatch.";
936 return -EINVAL;
937 }
938
939 if (crypt_integrity_mode(cc)) {
940 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size;
941 DMINFO("Integrity AEAD, tag size %u, IV size %u.",
942 cc->integrity_tag_size, cc->integrity_iv_size);
943
944 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) {
945 ti->error = "Integrity AEAD auth tag size is not supported.";
946 return -EINVAL;
947 }
948 } else if (cc->integrity_iv_size)
949 DMINFO("Additional per-sector space %u bytes for IV.",
950 cc->integrity_iv_size);
951
952 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) {
953 ti->error = "Not enough space for integrity tag in the profile.";
954 return -EINVAL;
955 }
956
957 return 0;
958#else
959 ti->error = "Integrity profile not supported.";
960 return -EINVAL;
961#endif
962}
963
Milan Brozd469f842007-10-19 22:42:37 +0100964static void crypt_convert_init(struct crypt_config *cc,
965 struct convert_context *ctx,
966 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000967 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 ctx->bio_in = bio_in;
970 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700971 if (bio_in)
972 ctx->iter_in = bio_in->bi_iter;
973 if (bio_out)
974 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100975 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000976 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Huang Yingb2174ee2009-03-16 17:44:33 +0000979static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
Milan Brozef43aa32017-01-04 20:23:54 +0100980 void *req)
Huang Yingb2174ee2009-03-16 17:44:33 +0000981{
982 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
983}
984
Milan Brozef43aa32017-01-04 20:23:54 +0100985static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq)
Huang Yingb2174ee2009-03-16 17:44:33 +0000986{
Milan Brozef43aa32017-01-04 20:23:54 +0100987 return (void *)((char *)dmreq - cc->dmreq_start);
Huang Yingb2174ee2009-03-16 17:44:33 +0000988}
989
Milan Broz2dc5327d32011-01-13 19:59:54 +0000990static u8 *iv_of_dmreq(struct crypt_config *cc,
991 struct dm_crypt_request *dmreq)
992{
Milan Brozef43aa32017-01-04 20:23:54 +0100993 if (crypt_integrity_mode(cc))
994 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
995 crypto_aead_alignmask(any_tfm_aead(cc)) + 1);
996 else
997 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
998 crypto_skcipher_alignmask(any_tfm(cc)) + 1);
Milan Broz2dc5327d32011-01-13 19:59:54 +0000999}
1000
Milan Brozef43aa32017-01-04 20:23:54 +01001001static u8 *org_iv_of_dmreq(struct crypt_config *cc,
1002 struct dm_crypt_request *dmreq)
1003{
1004 return iv_of_dmreq(cc, dmreq) + cc->iv_size;
1005}
1006
1007static uint64_t *org_sector_of_dmreq(struct crypt_config *cc,
1008 struct dm_crypt_request *dmreq)
1009{
1010 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size;
1011 return (uint64_t*) ptr;
1012}
1013
1014static unsigned int *org_tag_of_dmreq(struct crypt_config *cc,
1015 struct dm_crypt_request *dmreq)
1016{
1017 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size +
1018 cc->iv_size + sizeof(uint64_t);
1019 return (unsigned int*)ptr;
1020}
1021
1022static void *tag_from_dmreq(struct crypt_config *cc,
1023 struct dm_crypt_request *dmreq)
1024{
1025 struct convert_context *ctx = dmreq->ctx;
1026 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1027
1028 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) *
1029 cc->on_disk_tag_size];
1030}
1031
1032static void *iv_tag_from_dmreq(struct crypt_config *cc,
1033 struct dm_crypt_request *dmreq)
1034{
1035 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size;
1036}
1037
1038static int crypt_convert_block_aead(struct crypt_config *cc,
1039 struct convert_context *ctx,
1040 struct aead_request *req,
1041 unsigned int tag_offset)
Milan Broz01482b72008-02-08 02:11:04 +00001042{
Kent Overstreet003b5c52013-10-11 15:45:43 -07001043 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1044 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001045 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001046 unsigned int data_len = 1 << SECTOR_SHIFT;
1047 u8 *iv, *org_iv, *tag_iv, *tag;
1048 uint64_t *sector;
1049 int r = 0;
1050
1051 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size);
Milan Broz01482b72008-02-08 02:11:04 +00001052
Huang Yingb2174ee2009-03-16 17:44:33 +00001053 dmreq = dmreq_of_req(cc, req);
Mikulas Patockac66029f2012-07-27 15:08:05 +01001054 dmreq->iv_sector = ctx->cc_sector;
Huang Yingb2174ee2009-03-16 17:44:33 +00001055 dmreq->ctx = ctx;
Milan Broz01482b72008-02-08 02:11:04 +00001056
Milan Brozef43aa32017-01-04 20:23:54 +01001057 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
Milan Broz01482b72008-02-08 02:11:04 +00001058
Milan Brozef43aa32017-01-04 20:23:54 +01001059 sector = org_sector_of_dmreq(cc, dmreq);
1060 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1061
1062 iv = iv_of_dmreq(cc, dmreq);
1063 org_iv = org_iv_of_dmreq(cc, dmreq);
1064 tag = tag_from_dmreq(cc, dmreq);
1065 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1066
1067 /* AEAD request:
1068 * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
1069 * | (authenticated) | (auth+encryption) | |
1070 * | sector_LE | IV | sector in/out | tag in/out |
1071 */
1072 sg_init_table(dmreq->sg_in, 4);
1073 sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t));
1074 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size);
1075 sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, data_len, bv_in.bv_offset);
1076 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size);
1077
1078 sg_init_table(dmreq->sg_out, 4);
1079 sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t));
1080 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size);
1081 sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, data_len, bv_out.bv_offset);
1082 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size);
Milan Broz01482b72008-02-08 02:11:04 +00001083
Milan Broz3a7f6c92008-02-08 02:11:14 +00001084 if (cc->iv_gen_ops) {
Milan Brozef43aa32017-01-04 20:23:54 +01001085 /* For READs use IV stored in integrity metadata */
1086 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1087 memcpy(org_iv, tag_iv, cc->iv_size);
1088 } else {
1089 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1090 if (r < 0)
1091 return r;
1092 /* Store generated IV in integrity metadata */
1093 if (cc->integrity_iv_size)
1094 memcpy(tag_iv, org_iv, cc->iv_size);
1095 }
1096 /* Working copy of IV, to be modified in crypto API */
1097 memcpy(iv, org_iv, cc->iv_size);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001098 }
1099
Milan Brozef43aa32017-01-04 20:23:54 +01001100 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size);
1101 if (bio_data_dir(ctx->bio_in) == WRITE) {
1102 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
1103 data_len, iv);
1104 r = crypto_aead_encrypt(req);
1105 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size)
1106 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0,
1107 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size));
1108 } else {
1109 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
1110 data_len + cc->integrity_tag_size, iv);
1111 r = crypto_aead_decrypt(req);
1112 }
1113
1114 if (r == -EBADMSG)
1115 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1116 (unsigned long long)le64_to_cpu(*sector));
1117
1118 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
1119 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1120
1121 bio_advance_iter(ctx->bio_in, &ctx->iter_in, data_len);
1122 bio_advance_iter(ctx->bio_out, &ctx->iter_out, data_len);
1123
1124 return r;
1125}
1126
1127static int crypt_convert_block_skcipher(struct crypt_config *cc,
1128 struct convert_context *ctx,
1129 struct skcipher_request *req,
1130 unsigned int tag_offset)
1131{
1132 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1133 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
1134 struct scatterlist *sg_in, *sg_out;
1135 struct dm_crypt_request *dmreq;
1136 unsigned int data_len = 1 << SECTOR_SHIFT;
1137 u8 *iv, *org_iv, *tag_iv;
1138 uint64_t *sector;
1139 int r = 0;
1140
1141 dmreq = dmreq_of_req(cc, req);
1142 dmreq->iv_sector = ctx->cc_sector;
1143 dmreq->ctx = ctx;
1144
1145 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
1146
1147 iv = iv_of_dmreq(cc, dmreq);
1148 org_iv = org_iv_of_dmreq(cc, dmreq);
1149 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1150
1151 sector = org_sector_of_dmreq(cc, dmreq);
1152 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1153
1154 /* For skcipher we use only the first sg item */
1155 sg_in = &dmreq->sg_in[0];
1156 sg_out = &dmreq->sg_out[0];
1157
1158 sg_init_table(sg_in, 1);
1159 sg_set_page(sg_in, bv_in.bv_page, data_len, bv_in.bv_offset);
1160
1161 sg_init_table(sg_out, 1);
1162 sg_set_page(sg_out, bv_out.bv_page, data_len, bv_out.bv_offset);
1163
1164 if (cc->iv_gen_ops) {
1165 /* For READs use IV stored in integrity metadata */
1166 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1167 memcpy(org_iv, tag_iv, cc->integrity_iv_size);
1168 } else {
1169 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1170 if (r < 0)
1171 return r;
1172 /* Store generated IV in integrity metadata */
1173 if (cc->integrity_iv_size)
1174 memcpy(tag_iv, org_iv, cc->integrity_iv_size);
1175 }
1176 /* Working copy of IV, to be modified in crypto API */
1177 memcpy(iv, org_iv, cc->iv_size);
1178 }
1179
1180 skcipher_request_set_crypt(req, sg_in, sg_out, data_len, iv);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001181
1182 if (bio_data_dir(ctx->bio_in) == WRITE)
Herbert Xubbdb23b2016-01-24 21:16:36 +08001183 r = crypto_skcipher_encrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001184 else
Herbert Xubbdb23b2016-01-24 21:16:36 +08001185 r = crypto_skcipher_decrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001186
Milan Broz2dc5327d32011-01-13 19:59:54 +00001187 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001188 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1189
1190 bio_advance_iter(ctx->bio_in, &ctx->iter_in, data_len);
1191 bio_advance_iter(ctx->bio_out, &ctx->iter_out, data_len);
Milan Broz2dc5327d32011-01-13 19:59:54 +00001192
Milan Broz3a7f6c92008-02-08 02:11:14 +00001193 return r;
Milan Broz01482b72008-02-08 02:11:04 +00001194}
1195
Milan Broz95497a92008-02-08 02:11:12 +00001196static void kcryptd_async_done(struct crypto_async_request *async_req,
1197 int error);
Andi Kleenc0297722011-01-13 19:59:53 +00001198
Milan Brozef43aa32017-01-04 20:23:54 +01001199static void crypt_alloc_req_skcipher(struct crypt_config *cc,
1200 struct convert_context *ctx)
Milan Brozddd42ed2008-02-08 02:11:07 +00001201{
Mikulas Patockac66029f2012-07-27 15:08:05 +01001202 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +00001203
Milan Brozef43aa32017-01-04 20:23:54 +01001204 if (!ctx->r.req)
1205 ctx->r.req = mempool_alloc(cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +00001206
Milan Brozef43aa32017-01-04 20:23:54 +01001207 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]);
Milan Broz54cea3f2015-05-15 17:00:25 +02001208
1209 /*
1210 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1211 * requests if driver request queue is full.
1212 */
Milan Brozef43aa32017-01-04 20:23:54 +01001213 skcipher_request_set_callback(ctx->r.req,
Andi Kleenc0297722011-01-13 19:59:53 +00001214 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Milan Brozef43aa32017-01-04 20:23:54 +01001215 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req));
Milan Brozddd42ed2008-02-08 02:11:07 +00001216}
1217
Milan Brozef43aa32017-01-04 20:23:54 +01001218static void crypt_alloc_req_aead(struct crypt_config *cc,
1219 struct convert_context *ctx)
1220{
1221 if (!ctx->r.req_aead)
1222 ctx->r.req_aead = mempool_alloc(cc->req_pool, GFP_NOIO);
1223
1224 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]);
1225
1226 /*
1227 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1228 * requests if driver request queue is full.
1229 */
1230 aead_request_set_callback(ctx->r.req_aead,
1231 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
1232 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead));
1233}
1234
1235static void crypt_alloc_req(struct crypt_config *cc,
1236 struct convert_context *ctx)
1237{
1238 if (crypt_integrity_mode(cc))
1239 crypt_alloc_req_aead(cc, ctx);
1240 else
1241 crypt_alloc_req_skcipher(cc, ctx);
1242}
1243
1244static void crypt_free_req_skcipher(struct crypt_config *cc,
1245 struct skcipher_request *req, struct bio *base_bio)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001246{
1247 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1248
Herbert Xubbdb23b2016-01-24 21:16:36 +08001249 if ((struct skcipher_request *)(io + 1) != req)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001250 mempool_free(req, cc->req_pool);
1251}
1252
Milan Brozef43aa32017-01-04 20:23:54 +01001253static void crypt_free_req_aead(struct crypt_config *cc,
1254 struct aead_request *req, struct bio *base_bio)
1255{
1256 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1257
1258 if ((struct aead_request *)(io + 1) != req)
1259 mempool_free(req, cc->req_pool);
1260}
1261
1262static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio)
1263{
1264 if (crypt_integrity_mode(cc))
1265 crypt_free_req_aead(cc, req, base_bio);
1266 else
1267 crypt_free_req_skcipher(cc, req, base_bio);
1268}
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270/*
1271 * Encrypt / decrypt data from one bio to another one (can be the same one)
1272 */
1273static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +01001274 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Milan Brozef43aa32017-01-04 20:23:54 +01001276 unsigned int tag_offset = 0;
Milan Broz3f1e9072008-03-28 14:16:07 -07001277 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Mikulas Patocka40b62292012-07-27 15:08:04 +01001279 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +01001280
Kent Overstreet003b5c52013-10-11 15:45:43 -07001281 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Milan Broz3a7f6c92008-02-08 02:11:14 +00001283 crypt_alloc_req(cc, ctx);
1284
Mikulas Patocka40b62292012-07-27 15:08:04 +01001285 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -07001286
Milan Brozef43aa32017-01-04 20:23:54 +01001287 if (crypt_integrity_mode(cc))
1288 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset);
1289 else
1290 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001291
1292 switch (r) {
Milan Broz54cea3f2015-05-15 17:00:25 +02001293 /*
1294 * The request was queued by a crypto driver
1295 * but the driver request queue is full, let's wait.
1296 */
Milan Broz3a7f6c92008-02-08 02:11:14 +00001297 case -EBUSY:
1298 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -08001299 reinit_completion(&ctx->restart);
Milan Broz54cea3f2015-05-15 17:00:25 +02001300 /* fall through */
1301 /*
1302 * The request is queued and processed asynchronously,
1303 * completion function kcryptd_async_done() will be called.
1304 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001305 case -EINPROGRESS:
Milan Brozef43aa32017-01-04 20:23:54 +01001306 ctx->r.req = NULL;
Mikulas Patockac66029f2012-07-27 15:08:05 +01001307 ctx->cc_sector++;
Milan Brozef43aa32017-01-04 20:23:54 +01001308 tag_offset++;
Milan Broz3a7f6c92008-02-08 02:11:14 +00001309 continue;
Milan Broz54cea3f2015-05-15 17:00:25 +02001310 /*
1311 * The request was already processed (synchronously).
1312 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001313 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001314 atomic_dec(&ctx->cc_pending);
Mikulas Patockac66029f2012-07-27 15:08:05 +01001315 ctx->cc_sector++;
Milan Brozef43aa32017-01-04 20:23:54 +01001316 tag_offset++;
Milan Brozc7f1b202008-07-02 09:34:28 +01001317 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -07001318 continue;
Milan Brozef43aa32017-01-04 20:23:54 +01001319 /*
1320 * There was a data integrity error.
1321 */
1322 case -EBADMSG:
1323 atomic_dec(&ctx->cc_pending);
1324 return -EILSEQ;
1325 /*
1326 * There was an error while processing the request.
1327 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001328 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001329 atomic_dec(&ctx->cc_pending);
Milan Brozef43aa32017-01-04 20:23:54 +01001330 return -EIO;
Milan Broz3f1e9072008-03-28 14:16:07 -07001331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333
Milan Broz3f1e9072008-03-28 14:16:07 -07001334 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001337static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339/*
1340 * Generate a new unfragmented bio with the given size
Mike Snitzer586b2862015-09-09 21:34:51 -04001341 * This should never violate the device limitations (but only because
1342 * max_segment_size is being constrained to PAGE_SIZE).
Mikulas Patocka7145c242015-02-13 08:24:41 -05001343 *
1344 * This function may be called concurrently. If we allocate from the mempool
1345 * concurrently, there is a possibility of deadlock. For example, if we have
1346 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1347 * the mempool concurrently, it may deadlock in a situation where both processes
1348 * have allocated 128 pages and the mempool is exhausted.
1349 *
1350 * In order to avoid this scenario we allocate the pages under a mutex.
1351 *
1352 * In order to not degrade performance with excessive locking, we try
1353 * non-blocking allocations without a mutex first but on failure we fallback
1354 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001356static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001358 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001359 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001361 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
1362 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +00001363 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Mikulas Patocka7145c242015-02-13 08:24:41 -05001365retry:
Mel Gormand0164ad2015-11-06 16:28:21 -08001366 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001367 mutex_lock(&cc->bio_alloc_lock);
1368
Olaf Kirch2f9941b2007-05-09 02:32:53 -07001369 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -07001370 if (!clone)
Milan Brozef43aa32017-01-04 20:23:54 +01001371 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Olaf Kirch027581f2007-05-09 02:32:52 -07001373 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -07001374
Mikulas Patocka7145c242015-02-13 08:24:41 -05001375 remaining_size = size;
1376
Olaf Kirchf97380b2007-05-09 02:32:54 -07001377 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +00001378 page = mempool_alloc(cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -05001379 if (!page) {
1380 crypt_free_buffer_pages(cc, clone);
1381 bio_put(clone);
Mel Gormand0164ad2015-11-06 16:28:21 -08001382 gfp_mask |= __GFP_DIRECT_RECLAIM;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001383 goto retry;
1384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Mikulas Patocka7145c242015-02-13 08:24:41 -05001386 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Ming Lei0dae7fe2016-10-29 16:08:06 +08001388 bio_add_page(clone, page, len, 0);
Milan Broz91e10622007-12-13 14:16:10 +00001389
Mikulas Patocka7145c242015-02-13 08:24:41 -05001390 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
Milan Brozef43aa32017-01-04 20:23:54 +01001393 /* Allocate space for integrity tags */
1394 if (dm_crypt_integrity_io_alloc(io, clone)) {
1395 crypt_free_buffer_pages(cc, clone);
1396 bio_put(clone);
1397 clone = NULL;
1398 }
1399out:
Mel Gormand0164ad2015-11-06 16:28:21 -08001400 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001401 mutex_unlock(&cc->bio_alloc_lock);
1402
Milan Broz8b004452006-10-03 01:15:37 -07001403 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Neil Brown644bd2f2007-10-16 13:48:46 +02001406static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
Neil Brown644bd2f2007-10-16 13:48:46 +02001408 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 struct bio_vec *bv;
1410
Kent Overstreetcb34e052012-09-05 15:22:02 -07001411 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 BUG_ON(!bv->bv_page);
1413 mempool_free(bv->bv_page, cc->page_pool);
1414 bv->bv_page = NULL;
1415 }
1416}
1417
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001418static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1419 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001420{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001421 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001422 io->base_bio = bio;
1423 io->sector = sector;
1424 io->error = 0;
Milan Brozef43aa32017-01-04 20:23:54 +01001425 io->ctx.r.req = NULL;
1426 io->integrity_metadata = NULL;
1427 io->integrity_metadata_from_pool = false;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001428 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001429}
1430
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001431static void crypt_inc_pending(struct dm_crypt_io *io)
1432{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001433 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001434}
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436/*
1437 * One of the bios was finished. Check for completion of
1438 * the whole request and correctly clean up the buffer.
1439 */
Milan Broz5742fd72008-02-08 02:10:43 +00001440static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001442 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001443 struct bio *base_bio = io->base_bio;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001444 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Mikulas Patocka40b62292012-07-27 15:08:04 +01001446 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 return;
1448
Milan Brozef43aa32017-01-04 20:23:54 +01001449 if (io->ctx.r.req)
1450 crypt_free_req(cc, io->ctx.r.req, base_bio);
1451
1452 if (unlikely(io->integrity_metadata_from_pool))
1453 mempool_free(io->integrity_metadata, io->cc->tag_pool);
1454 else
1455 kfree(io->integrity_metadata);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001456
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001457 base_bio->bi_error = error;
1458 bio_endio(base_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001462 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 *
1464 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001465 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001466 *
1467 * kcryptd performs the actual encryption or decryption.
1468 *
1469 * kcryptd_io performs the IO submission.
1470 *
1471 * They must be separated as otherwise the final stages could be
1472 * starved by new requests which can block in the first stages due
1473 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001474 *
1475 * The work is done per CPU global for all dm-crypt instances.
1476 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001478static void crypt_endio(struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001479{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001480 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001481 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001482 unsigned rw = bio_data_dir(clone);
Sasha Levin9b81c842015-08-10 19:05:18 -04001483 int error;
Milan Broz8b004452006-10-03 01:15:37 -07001484
1485 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001486 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001487 */
Milan Brozee7a4912008-02-08 02:10:46 +00001488 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001489 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001490
Sasha Levin9b81c842015-08-10 19:05:18 -04001491 error = clone->bi_error;
Milan Brozee7a4912008-02-08 02:10:46 +00001492 bio_put(clone);
1493
Sasha Levin9b81c842015-08-10 19:05:18 -04001494 if (rw == READ && !error) {
Milan Brozee7a4912008-02-08 02:10:46 +00001495 kcryptd_queue_crypt(io);
1496 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001497 }
Milan Broz8b004452006-10-03 01:15:37 -07001498
Sasha Levin9b81c842015-08-10 19:05:18 -04001499 if (unlikely(error))
1500 io->error = error;
Milan Broz5742fd72008-02-08 02:10:43 +00001501
1502 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001503}
1504
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001505static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001506{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001507 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001508
1509 clone->bi_private = io;
1510 clone->bi_end_io = crypt_endio;
1511 clone->bi_bdev = cc->dev->bdev;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001512 clone->bi_opf = io->base_bio->bi_opf;
Milan Broz8b004452006-10-03 01:15:37 -07001513}
1514
Milan Broz20c82532011-01-13 19:59:53 +00001515static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001516{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001517 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001518 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001519
Milan Broz8b004452006-10-03 01:15:37 -07001520 /*
Mike Snitzer59779072015-04-09 16:53:24 -04001521 * We need the original biovec array in order to decrypt
1522 * the whole bio data *afterwards* -- thanks to immutable
1523 * biovecs we don't need to worry about the block layer
1524 * modifying the biovec array; so leverage bio_clone_fast().
Milan Broz8b004452006-10-03 01:15:37 -07001525 */
Mike Snitzer59779072015-04-09 16:53:24 -04001526 clone = bio_clone_fast(io->base_bio, gfp, cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001527 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001528 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001529
Milan Broz20c82532011-01-13 19:59:53 +00001530 crypt_inc_pending(io);
1531
Milan Broz8b004452006-10-03 01:15:37 -07001532 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001533 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001534
Milan Brozef43aa32017-01-04 20:23:54 +01001535 if (dm_crypt_integrity_io_alloc(io, clone)) {
1536 crypt_dec_pending(io);
1537 bio_put(clone);
1538 return 1;
1539 }
1540
Milan Broz93e605c2006-10-03 01:15:38 -07001541 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001542 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001543}
1544
Mikulas Patockadc267622015-02-13 08:25:59 -05001545static void kcryptd_io_read_work(struct work_struct *work)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001546{
1547 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1548
Mikulas Patockadc267622015-02-13 08:25:59 -05001549 crypt_inc_pending(io);
1550 if (kcryptd_io_read(io, GFP_NOIO))
1551 io->error = -ENOMEM;
1552 crypt_dec_pending(io);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001553}
1554
Mikulas Patockadc267622015-02-13 08:25:59 -05001555static void kcryptd_queue_read(struct dm_crypt_io *io)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001556{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001557 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001558
Mikulas Patockadc267622015-02-13 08:25:59 -05001559 INIT_WORK(&io->work, kcryptd_io_read_work);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001560 queue_work(cc->io_queue, &io->work);
1561}
1562
Mikulas Patockadc267622015-02-13 08:25:59 -05001563static void kcryptd_io_write(struct dm_crypt_io *io)
1564{
1565 struct bio *clone = io->ctx.bio_out;
1566
1567 generic_make_request(clone);
1568}
1569
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001570#define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1571
Mikulas Patockadc267622015-02-13 08:25:59 -05001572static int dmcrypt_write(void *data)
1573{
1574 struct crypt_config *cc = data;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001575 struct dm_crypt_io *io;
1576
Mikulas Patockadc267622015-02-13 08:25:59 -05001577 while (1) {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001578 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -05001579 struct blk_plug plug;
1580
1581 DECLARE_WAITQUEUE(wait, current);
1582
1583 spin_lock_irq(&cc->write_thread_wait.lock);
1584continue_locked:
1585
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001586 if (!RB_EMPTY_ROOT(&cc->write_tree))
Mikulas Patockadc267622015-02-13 08:25:59 -05001587 goto pop_from_list;
1588
Rabin Vincentf659b102016-09-21 16:22:29 +02001589 set_current_state(TASK_INTERRUPTIBLE);
Mikulas Patockadc267622015-02-13 08:25:59 -05001590 __add_wait_queue(&cc->write_thread_wait, &wait);
1591
1592 spin_unlock_irq(&cc->write_thread_wait.lock);
1593
Rabin Vincentf659b102016-09-21 16:22:29 +02001594 if (unlikely(kthread_should_stop())) {
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001595 set_current_state(TASK_RUNNING);
Rabin Vincentf659b102016-09-21 16:22:29 +02001596 remove_wait_queue(&cc->write_thread_wait, &wait);
1597 break;
1598 }
1599
Mikulas Patockadc267622015-02-13 08:25:59 -05001600 schedule();
1601
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001602 set_current_state(TASK_RUNNING);
Mikulas Patockadc267622015-02-13 08:25:59 -05001603 spin_lock_irq(&cc->write_thread_wait.lock);
1604 __remove_wait_queue(&cc->write_thread_wait, &wait);
1605 goto continue_locked;
1606
1607pop_from_list:
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001608 write_tree = cc->write_tree;
1609 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05001610 spin_unlock_irq(&cc->write_thread_wait.lock);
1611
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001612 BUG_ON(rb_parent(write_tree.rb_node));
1613
1614 /*
1615 * Note: we cannot walk the tree here with rb_next because
1616 * the structures may be freed when kcryptd_io_write is called.
1617 */
Mikulas Patockadc267622015-02-13 08:25:59 -05001618 blk_start_plug(&plug);
1619 do {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001620 io = crypt_io_from_node(rb_first(&write_tree));
1621 rb_erase(&io->rb_node, &write_tree);
Mikulas Patockadc267622015-02-13 08:25:59 -05001622 kcryptd_io_write(io);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001623 } while (!RB_EMPTY_ROOT(&write_tree));
Mikulas Patockadc267622015-02-13 08:25:59 -05001624 blk_finish_plug(&plug);
1625 }
1626 return 0;
1627}
1628
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001629static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001630{
Milan Brozdec1ced2008-02-08 02:10:57 +00001631 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001632 struct crypt_config *cc = io->cc;
Mikulas Patockadc267622015-02-13 08:25:59 -05001633 unsigned long flags;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001634 sector_t sector;
1635 struct rb_node **rbp, *parent;
Milan Brozdec1ced2008-02-08 02:10:57 +00001636
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001637 if (unlikely(io->error < 0)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001638 crypt_free_buffer_pages(cc, clone);
1639 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001640 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001641 return;
1642 }
1643
1644 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001645 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001646
Kent Overstreet4f024f32013-10-11 15:44:27 -07001647 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001648
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001649 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1650 generic_make_request(clone);
1651 return;
1652 }
1653
Mikulas Patockadc267622015-02-13 08:25:59 -05001654 spin_lock_irqsave(&cc->write_thread_wait.lock, flags);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001655 rbp = &cc->write_tree.rb_node;
1656 parent = NULL;
1657 sector = io->sector;
1658 while (*rbp) {
1659 parent = *rbp;
1660 if (sector < crypt_io_from_node(parent)->sector)
1661 rbp = &(*rbp)->rb_left;
1662 else
1663 rbp = &(*rbp)->rb_right;
1664 }
1665 rb_link_node(&io->rb_node, parent, rbp);
1666 rb_insert_color(&io->rb_node, &cc->write_tree);
1667
Mikulas Patockadc267622015-02-13 08:25:59 -05001668 wake_up_locked(&cc->write_thread_wait);
1669 spin_unlock_irqrestore(&cc->write_thread_wait.lock, flags);
Milan Broz4e4eef62008-02-08 02:10:49 +00001670}
1671
Milan Brozfc5a5e92008-10-10 13:37:04 +01001672static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001673{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001674 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001675 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001676 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001677 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +00001678 int r;
Milan Broz8b004452006-10-03 01:15:37 -07001679
Milan Broz93e605c2006-10-03 01:15:38 -07001680 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001681 * Prevent io from disappearing until this function completes.
1682 */
1683 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001684 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001685
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001686 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1687 if (unlikely(!clone)) {
1688 io->error = -EIO;
1689 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001690 }
Milan Broz899c95d2008-02-08 02:11:02 +00001691
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001692 io->ctx.bio_out = clone;
1693 io->ctx.iter_out = clone->bi_iter;
1694
1695 sector += bio_sectors(clone);
1696
1697 crypt_inc_pending(io);
1698 r = crypt_convert(cc, &io->ctx);
Milan Brozef43aa32017-01-04 20:23:54 +01001699 if (r < 0)
1700 io->error = r;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001701 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1702
1703 /* Encryption was already finished, submit io now */
1704 if (crypt_finished) {
1705 kcryptd_crypt_write_io_submit(io, 0);
1706 io->sector = sector;
1707 }
1708
1709dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001710 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001711}
1712
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001713static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001714{
Milan Broz5742fd72008-02-08 02:10:43 +00001715 crypt_dec_pending(io);
1716}
1717
Milan Broz4e4eef62008-02-08 02:10:49 +00001718static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001719{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001720 struct crypt_config *cc = io->cc;
Milan Broz5742fd72008-02-08 02:10:43 +00001721 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001722
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001723 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001724
Milan Broz53017032008-02-08 02:10:38 +00001725 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001726 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001727
Milan Broz5742fd72008-02-08 02:10:43 +00001728 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001729 if (r < 0)
Milan Brozef43aa32017-01-04 20:23:54 +01001730 io->error = r;
Milan Broz5742fd72008-02-08 02:10:43 +00001731
Mikulas Patocka40b62292012-07-27 15:08:04 +01001732 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001733 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001734
1735 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001736}
1737
Milan Broz95497a92008-02-08 02:11:12 +00001738static void kcryptd_async_done(struct crypto_async_request *async_req,
1739 int error)
1740{
Huang Yingb2174ee2009-03-16 17:44:33 +00001741 struct dm_crypt_request *dmreq = async_req->data;
1742 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001743 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001744 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001745
Milan Broz54cea3f2015-05-15 17:00:25 +02001746 /*
1747 * A request from crypto driver backlog is going to be processed now,
1748 * finish the completion and continue in crypt_convert().
1749 * (Callback will be called for the second time for this request.)
1750 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001751 if (error == -EINPROGRESS) {
1752 complete(&ctx->restart);
Milan Broz95497a92008-02-08 02:11:12 +00001753 return;
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001754 }
Milan Broz95497a92008-02-08 02:11:12 +00001755
Milan Broz2dc5327d32011-01-13 19:59:54 +00001756 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001757 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq);
Milan Broz2dc5327d32011-01-13 19:59:54 +00001758
Milan Brozef43aa32017-01-04 20:23:54 +01001759 if (error == -EBADMSG) {
1760 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1761 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
1762 io->error = -EILSEQ;
1763 } else if (error < 0)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001764 io->error = -EIO;
1765
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001766 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00001767
Mikulas Patocka40b62292012-07-27 15:08:04 +01001768 if (!atomic_dec_and_test(&ctx->cc_pending))
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001769 return;
Milan Broz95497a92008-02-08 02:11:12 +00001770
1771 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001772 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001773 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001774 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001775}
1776
Milan Broz4e4eef62008-02-08 02:10:49 +00001777static void kcryptd_crypt(struct work_struct *work)
1778{
1779 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1780
1781 if (bio_data_dir(io->base_bio) == READ)
1782 kcryptd_crypt_read_convert(io);
1783 else
1784 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001785}
1786
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001787static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1788{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001789 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001790
1791 INIT_WORK(&io->work, kcryptd_crypt);
1792 queue_work(cc->crypt_queue, &io->work);
1793}
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795/*
1796 * Decode key from its hex representation
1797 */
1798static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1799{
1800 char buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 unsigned int i;
1802
1803 buffer[2] = '\0';
1804
Milan Broz8b004452006-10-03 01:15:37 -07001805 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 buffer[0] = *hex++;
1807 buffer[1] = *hex++;
1808
majianpeng1a66a082012-07-27 15:07:59 +01001809 if (kstrtou8(buffer, 16, &key[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return -EINVAL;
1811 }
1812
1813 if (*hex != '\0')
1814 return -EINVAL;
1815
1816 return 0;
1817}
1818
Milan Brozef43aa32017-01-04 20:23:54 +01001819static void crypt_free_tfms_aead(struct crypt_config *cc)
1820{
1821 if (!cc->cipher_tfm.tfms_aead)
1822 return;
1823
1824 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1825 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]);
1826 cc->cipher_tfm.tfms_aead[0] = NULL;
1827 }
1828
1829 kfree(cc->cipher_tfm.tfms_aead);
1830 cc->cipher_tfm.tfms_aead = NULL;
1831}
1832
1833static void crypt_free_tfms_skcipher(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001834{
Milan Brozd1f96422011-01-13 19:59:54 +00001835 unsigned i;
1836
Milan Brozef43aa32017-01-04 20:23:54 +01001837 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001838 return;
1839
Milan Brozd1f96422011-01-13 19:59:54 +00001840 for (i = 0; i < cc->tfms_count; i++)
Milan Brozef43aa32017-01-04 20:23:54 +01001841 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) {
1842 crypto_free_skcipher(cc->cipher_tfm.tfms[i]);
1843 cc->cipher_tfm.tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001844 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001845
Milan Brozef43aa32017-01-04 20:23:54 +01001846 kfree(cc->cipher_tfm.tfms);
1847 cc->cipher_tfm.tfms = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001848}
1849
Milan Brozef43aa32017-01-04 20:23:54 +01001850static void crypt_free_tfms(struct crypt_config *cc)
1851{
1852 if (crypt_integrity_mode(cc))
1853 crypt_free_tfms_aead(cc);
1854 else
1855 crypt_free_tfms_skcipher(cc);
1856}
1857
1858static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001859{
Milan Brozd1f96422011-01-13 19:59:54 +00001860 unsigned i;
1861 int err;
1862
Milan Brozef43aa32017-01-04 20:23:54 +01001863 cc->cipher_tfm.tfms = kzalloc(cc->tfms_count *
1864 sizeof(struct crypto_skcipher *), GFP_KERNEL);
1865 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001866 return -ENOMEM;
1867
Milan Brozd1f96422011-01-13 19:59:54 +00001868 for (i = 0; i < cc->tfms_count; i++) {
Milan Brozef43aa32017-01-04 20:23:54 +01001869 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
1870 if (IS_ERR(cc->cipher_tfm.tfms[i])) {
1871 err = PTR_ERR(cc->cipher_tfm.tfms[i]);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001872 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001873 return err;
1874 }
1875 }
1876
1877 return 0;
1878}
1879
Milan Brozef43aa32017-01-04 20:23:54 +01001880static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
1881{
1882 char *authenc = NULL;
1883 int err;
1884
1885 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
1886 if (!cc->cipher_tfm.tfms)
1887 return -ENOMEM;
1888
1889 /* Compose AEAD cipher with autenc(authenticator,cipher) structure */
1890 if (crypt_integrity_hmac(cc)) {
1891 authenc = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
1892 if (!authenc)
1893 return -ENOMEM;
1894 err = snprintf(authenc, CRYPTO_MAX_ALG_NAME,
1895 "authenc(%s,%s)", cc->cipher_auth, ciphermode);
1896 if (err < 0) {
1897 kzfree(authenc);
1898 return err;
1899 }
1900 ciphermode = authenc;
1901 }
1902
1903 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
1904 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1905 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
1906 crypt_free_tfms(cc);
1907 return err;
1908 }
1909
1910 kzfree(authenc);
1911 return 0;
1912}
1913
1914static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
1915{
1916 if (crypt_integrity_mode(cc))
1917 return crypt_alloc_tfms_aead(cc, ciphermode);
1918 else
1919 return crypt_alloc_tfms_skcipher(cc, ciphermode);
1920}
1921
1922static unsigned crypt_subkey_size(struct crypt_config *cc)
1923{
1924 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1925}
1926
1927static unsigned crypt_authenckey_size(struct crypt_config *cc)
1928{
1929 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
1930}
1931
1932/*
1933 * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
1934 * the key must be for some reason in special format.
1935 * This funcion converts cc->key to this special format.
1936 */
1937static void crypt_copy_authenckey(char *p, const void *key,
1938 unsigned enckeylen, unsigned authkeylen)
1939{
1940 struct crypto_authenc_key_param *param;
1941 struct rtattr *rta;
1942
1943 rta = (struct rtattr *)p;
1944 param = RTA_DATA(rta);
1945 param->enckeylen = cpu_to_be32(enckeylen);
1946 rta->rta_len = RTA_LENGTH(sizeof(*param));
1947 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
1948 p += RTA_SPACE(sizeof(*param));
1949 memcpy(p, key + enckeylen, authkeylen);
1950 p += authkeylen;
1951 memcpy(p, key, enckeylen);
1952}
1953
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04001954static int crypt_setkey(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +00001955{
Milan Brozda31a072013-10-28 23:21:03 +01001956 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001957 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001958
Milan Brozda31a072013-10-28 23:21:03 +01001959 /* Ignore extra keys (which are used for IV etc) */
Milan Brozef43aa32017-01-04 20:23:54 +01001960 subkey_size = crypt_subkey_size(cc);
Milan Brozda31a072013-10-28 23:21:03 +01001961
Milan Brozef43aa32017-01-04 20:23:54 +01001962 if (crypt_integrity_hmac(cc))
1963 crypt_copy_authenckey(cc->authenc_key, cc->key,
1964 subkey_size - cc->key_mac_size,
1965 cc->key_mac_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001966 for (i = 0; i < cc->tfms_count; i++) {
Milan Brozef43aa32017-01-04 20:23:54 +01001967 if (crypt_integrity_aead(cc))
1968 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1969 cc->key + (i * subkey_size),
1970 subkey_size);
1971 else if (crypt_integrity_hmac(cc))
1972 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1973 cc->authenc_key, crypt_authenckey_size(cc));
1974 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
Milan Broz69a8cfc2011-01-13 19:59:49 +00002133 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00002134 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002135
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002136 r = crypt_setkey(cc);
Ondrej Kozina265e9092016-11-02 15:02:08 +01002137 if (!r)
2138 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Milan Brozde8be5a2011-03-24 13:54:27 +00002139
2140out:
2141 /* Hex key string not needed after here, so wipe it. */
2142 memset(key, '0', key_string_len);
2143
2144 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002145}
2146
2147static int crypt_wipe_key(struct crypt_config *cc)
2148{
2149 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2150 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002151 kzfree(cc->key_string);
2152 cc->key_string = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +00002153
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002154 return crypt_setkey(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07002155}
2156
Milan Broz28513fc2010-08-12 04:14:06 +01002157static void crypt_dtr(struct dm_target *ti)
2158{
2159 struct crypt_config *cc = ti->private;
2160
2161 ti->private = NULL;
2162
2163 if (!cc)
2164 return;
2165
Rabin Vincentf659b102016-09-21 16:22:29 +02002166 if (cc->write_thread)
Mikulas Patockadc267622015-02-13 08:25:59 -05002167 kthread_stop(cc->write_thread);
2168
Milan Broz28513fc2010-08-12 04:14:06 +01002169 if (cc->io_queue)
2170 destroy_workqueue(cc->io_queue);
2171 if (cc->crypt_queue)
2172 destroy_workqueue(cc->crypt_queue);
2173
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002174 crypt_free_tfms(cc);
2175
Milan Broz28513fc2010-08-12 04:14:06 +01002176 if (cc->bs)
2177 bioset_free(cc->bs);
2178
Julia Lawall6f659852015-09-13 14:15:05 +02002179 mempool_destroy(cc->page_pool);
2180 mempool_destroy(cc->req_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01002181 mempool_destroy(cc->tag_pool);
Milan Broz28513fc2010-08-12 04:14:06 +01002182
2183 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
2184 cc->iv_gen_ops->dtr(cc);
2185
Milan Broz28513fc2010-08-12 04:14:06 +01002186 if (cc->dev)
2187 dm_put_device(ti, cc->dev);
2188
Milan Broz5ebaee62010-08-12 04:14:07 +01002189 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00002190 kzfree(cc->cipher_string);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002191 kzfree(cc->key_string);
Milan Brozef43aa32017-01-04 20:23:54 +01002192 kzfree(cc->cipher_auth);
2193 kzfree(cc->authenc_key);
Milan Broz28513fc2010-08-12 04:14:06 +01002194
2195 /* Must zero key material before freeing */
2196 kzfree(cc);
2197}
2198
Milan Broz5ebaee62010-08-12 04:14:07 +01002199static int crypt_ctr_cipher(struct dm_target *ti,
2200 char *cipher_in, char *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Milan Broz5ebaee62010-08-12 04:14:07 +01002202 struct crypt_config *cc = ti->private;
Milan Brozd1f96422011-01-13 19:59:54 +00002203 char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01002204 char *cipher_api = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002205 int ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002206 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Milan Broz5ebaee62010-08-12 04:14:07 +01002208 /* Convert to crypto api definition? */
2209 if (strchr(cipher_in, '(')) {
2210 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return -EINVAL;
2212 }
2213
Milan Broz7dbcd132011-01-13 19:59:52 +00002214 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
2215 if (!cc->cipher_string)
2216 goto bad_mem;
2217
Milan Broz5ebaee62010-08-12 04:14:07 +01002218 /*
2219 * Legacy dm-crypt cipher specification
Milan Brozd1f96422011-01-13 19:59:54 +00002220 * cipher[:keycount]-mode-iv:ivopts
Milan Broz5ebaee62010-08-12 04:14:07 +01002221 */
2222 tmp = cipher_in;
Milan Brozd1f96422011-01-13 19:59:54 +00002223 keycount = strsep(&tmp, "-");
2224 cipher = strsep(&keycount, ":");
2225
2226 if (!keycount)
2227 cc->tfms_count = 1;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002228 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
Milan Brozd1f96422011-01-13 19:59:54 +00002229 !is_power_of_2(cc->tfms_count)) {
2230 ti->error = "Bad cipher key count specification";
2231 return -EINVAL;
2232 }
2233 cc->key_parts = cc->tfms_count;
Milan Broz5ebaee62010-08-12 04:14:07 +01002234
2235 cc->cipher = kstrdup(cipher, GFP_KERNEL);
2236 if (!cc->cipher)
2237 goto bad_mem;
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 chainmode = strsep(&tmp, "-");
2240 ivopts = strsep(&tmp, "-");
2241 ivmode = strsep(&ivopts, ":");
2242
2243 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01002244 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Milan Broz7dbcd132011-01-13 19:59:52 +00002246 /*
2247 * For compatibility with the original dm-crypt mapping format, if
2248 * only the cipher name is supplied, use cbc-plain.
2249 */
Milan Broz5ebaee62010-08-12 04:14:07 +01002250 if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 chainmode = "cbc";
2252 ivmode = "plain";
2253 }
2254
Herbert Xud1806f62006-08-22 20:29:17 +10002255 if (strcmp(chainmode, "ecb") && !ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002256 ti->error = "IV mechanism required";
2257 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
2259
Milan Broz5ebaee62010-08-12 04:14:07 +01002260 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
2261 if (!cipher_api)
2262 goto bad_mem;
2263
2264 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
2265 "%s(%s)", chainmode, cipher);
2266 if (ret < 0) {
2267 kfree(cipher_api);
2268 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10002269 }
2270
Milan Broz5ebaee62010-08-12 04:14:07 +01002271 /* Allocate cipher */
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002272 ret = crypt_alloc_tfms(cc, cipher_api);
2273 if (ret < 0) {
2274 ti->error = "Error allocating crypto tfm";
2275 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Milan Broz5ebaee62010-08-12 04:14:07 +01002278 /* Initialize IV */
Milan Brozef43aa32017-01-04 20:23:54 +01002279 if (crypt_integrity_mode(cc))
2280 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2281 else
2282 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2283
2284 if (crypt_integrity_hmac(cc)) {
2285 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
2286 if (!cc->authenc_key) {
2287 ret = -ENOMEM;
2288 ti->error = "Error allocating authenc key space";
2289 goto bad;
2290 }
2291 }
2292
Milan Broz5ebaee62010-08-12 04:14:07 +01002293 if (cc->iv_size)
2294 /* at least a 64 bit sector number should fit in our buffer */
2295 cc->iv_size = max(cc->iv_size,
2296 (unsigned int)(sizeof(u64) / sizeof(u8)));
2297 else if (ivmode) {
2298 DMWARN("Selected cipher does not support IVs");
2299 ivmode = NULL;
2300 }
2301
2302 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if (ivmode == NULL)
2304 cc->iv_gen_ops = NULL;
2305 else if (strcmp(ivmode, "plain") == 0)
2306 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00002307 else if (strcmp(ivmode, "plain64") == 0)
2308 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 else if (strcmp(ivmode, "essiv") == 0)
2310 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10002311 else if (strcmp(ivmode, "benbi") == 0)
2312 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07002313 else if (strcmp(ivmode, "null") == 0)
2314 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00002315 else if (strcmp(ivmode, "lmk") == 0) {
2316 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01002317 /*
2318 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00002319 * to length of provided multi-key string.
2320 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01002321 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00002322 */
Milan Brozda31a072013-10-28 23:21:03 +01002323 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00002324 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01002325 cc->key_extra_size = cc->key_size / cc->key_parts;
2326 }
Milan Brozed04d982013-10-28 23:21:04 +01002327 } else if (strcmp(ivmode, "tcw") == 0) {
2328 cc->iv_gen_ops = &crypt_iv_tcw_ops;
2329 cc->key_parts += 2; /* IV + whitening */
2330 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Brozef43aa32017-01-04 20:23:54 +01002331 } else if (strcmp(ivmode, "random") == 0) {
2332 cc->iv_gen_ops = &crypt_iv_random_ops;
2333 /* Need storage space in integrity fields. */
2334 cc->integrity_iv_size = cc->iv_size;
Milan Broz34745782011-01-13 19:59:55 +00002335 } else {
Milan Broz5ebaee62010-08-12 04:14:07 +01002336 ret = -EINVAL;
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002337 ti->error = "Invalid IV mode";
Milan Broz28513fc2010-08-12 04:14:06 +01002338 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
2340
Milan Brozda31a072013-10-28 23:21:03 +01002341 /* Initialize and set key */
2342 ret = crypt_set_key(cc, key);
2343 if (ret < 0) {
2344 ti->error = "Error decoding and setting key";
2345 goto bad;
2346 }
2347
Milan Broz28513fc2010-08-12 04:14:06 +01002348 /* Allocate IV */
2349 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
2350 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
2351 if (ret < 0) {
2352 ti->error = "Error creating IV";
2353 goto bad;
2354 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00002355 }
2356
Milan Broz28513fc2010-08-12 04:14:06 +01002357 /* Initialize IV (set keys for ESSIV etc) */
2358 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
2359 ret = cc->iv_gen_ops->init(cc);
2360 if (ret < 0) {
2361 ti->error = "Error initialising IV";
2362 goto bad;
2363 }
2364 }
2365
Milan Broz5ebaee62010-08-12 04:14:07 +01002366 ret = 0;
2367bad:
2368 kfree(cipher_api);
2369 return ret;
2370
2371bad_mem:
2372 ti->error = "Cannot allocate cipher strings";
2373 return -ENOMEM;
2374}
2375
Milan Brozef43aa32017-01-04 20:23:54 +01002376static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
2377{
2378 struct crypt_config *cc = ti->private;
2379 struct dm_arg_set as;
2380 static struct dm_arg _args[] = {
2381 {0, 3, "Invalid number of feature args"},
2382 };
2383 unsigned int opt_params, val;
2384 const char *opt_string, *sval;
2385 int ret;
2386
2387 /* Optional parameters */
2388 as.argc = argc;
2389 as.argv = argv;
2390
2391 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2392 if (ret)
2393 return ret;
2394
2395 while (opt_params--) {
2396 opt_string = dm_shift_arg(&as);
2397 if (!opt_string) {
2398 ti->error = "Not enough feature arguments";
2399 return -EINVAL;
2400 }
2401
2402 if (!strcasecmp(opt_string, "allow_discards"))
2403 ti->num_discard_bios = 1;
2404
2405 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
2406 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
2407
2408 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
2409 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
2410 else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
2411 if (val == 0 || val > MAX_TAG_SIZE) {
2412 ti->error = "Invalid integrity arguments";
2413 return -EINVAL;
2414 }
2415 cc->on_disk_tag_size = val;
2416 sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
2417 if (!strcasecmp(sval, "aead")) {
2418 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
2419 } else if (!strncasecmp(sval, "hmac(", strlen("hmac("))) {
2420 struct crypto_ahash *hmac_tfm = crypto_alloc_ahash(sval, 0, 0);
2421 if (IS_ERR(hmac_tfm)) {
2422 ti->error = "Error initializing HMAC integrity hash.";
2423 return PTR_ERR(hmac_tfm);
2424 }
2425 cc->key_mac_size = crypto_ahash_digestsize(hmac_tfm);
2426 crypto_free_ahash(hmac_tfm);
2427 set_bit(CRYPT_MODE_INTEGRITY_HMAC, &cc->cipher_flags);
2428 } else if (strcasecmp(sval, "none")) {
2429 ti->error = "Unknown integrity profile";
2430 return -EINVAL;
2431 }
2432
2433 cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
2434 if (!cc->cipher_auth)
2435 return -ENOMEM;
2436 } else {
2437 ti->error = "Invalid feature arguments";
2438 return -EINVAL;
2439 }
2440 }
2441
2442 return 0;
2443}
2444
Milan Broz5ebaee62010-08-12 04:14:07 +01002445/*
2446 * Construct an encryption mapping:
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002447 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
Milan Broz5ebaee62010-08-12 04:14:07 +01002448 */
2449static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2450{
2451 struct crypt_config *cc;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002452 int key_size;
Milan Brozef43aa32017-01-04 20:23:54 +01002453 unsigned int align_mask;
Milan Broz5ebaee62010-08-12 04:14:07 +01002454 unsigned long long tmpll;
2455 int ret;
Milan Brozef43aa32017-01-04 20:23:54 +01002456 size_t iv_size_padding, additional_req_size;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002457 char dummy;
Milan Broz5ebaee62010-08-12 04:14:07 +01002458
Milan Broz772ae5f2011-08-02 12:32:08 +01002459 if (argc < 5) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002460 ti->error = "Not enough arguments";
2461 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
2463
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002464 key_size = get_key_size(&argv[1]);
2465 if (key_size < 0) {
2466 ti->error = "Cannot parse key size";
2467 return -EINVAL;
2468 }
Milan Broz5ebaee62010-08-12 04:14:07 +01002469
2470 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
2471 if (!cc) {
2472 ti->error = "Cannot allocate encryption context";
2473 return -ENOMEM;
2474 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00002475 cc->key_size = key_size;
Milan Broz5ebaee62010-08-12 04:14:07 +01002476
2477 ti->private = cc;
Milan Brozef43aa32017-01-04 20:23:54 +01002478
2479 /* Optional parameters need to be read before cipher constructor */
2480 if (argc > 5) {
2481 ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
2482 if (ret)
2483 goto bad;
2484 }
2485
Milan Broz5ebaee62010-08-12 04:14:07 +01002486 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
2487 if (ret < 0)
2488 goto bad;
2489
Milan Brozef43aa32017-01-04 20:23:54 +01002490 if (crypt_integrity_mode(cc)) {
2491 cc->dmreq_start = sizeof(struct aead_request);
2492 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
2493 align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
2494 } else {
2495 cc->dmreq_start = sizeof(struct skcipher_request);
2496 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
2497 align_mask = crypto_skcipher_alignmask(any_tfm(cc));
2498 }
Mikulas Patockad49ec522014-08-28 11:09:31 -04002499 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
2500
Milan Brozef43aa32017-01-04 20:23:54 +01002501 if (align_mask < CRYPTO_MINALIGN) {
Mikulas Patockad49ec522014-08-28 11:09:31 -04002502 /* Allocate the padding exactly */
2503 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
Milan Brozef43aa32017-01-04 20:23:54 +01002504 & align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002505 } else {
2506 /*
2507 * If the cipher requires greater alignment than kmalloc
2508 * alignment, we don't know the exact position of the
2509 * initialization vector. We must assume worst case.
2510 */
Milan Brozef43aa32017-01-04 20:23:54 +01002511 iv_size_padding = align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002512 }
Milan Brozddd42ed2008-02-08 02:11:07 +00002513
Mikulas Patocka94f5e022015-02-13 08:25:26 -05002514 ret = -ENOMEM;
Milan Brozef43aa32017-01-04 20:23:54 +01002515
2516 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
2517 additional_req_size = sizeof(struct dm_crypt_request) +
2518 iv_size_padding + cc->iv_size +
2519 cc->iv_size +
2520 sizeof(uint64_t) +
2521 sizeof(unsigned int);
2522
2523 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start + additional_req_size);
Milan Brozddd42ed2008-02-08 02:11:07 +00002524 if (!cc->req_pool) {
2525 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01002526 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00002527 }
Milan Brozddd42ed2008-02-08 02:11:07 +00002528
Mike Snitzer30187e12016-01-31 13:28:26 -05002529 cc->per_bio_data_size = ti->per_io_data_size =
Milan Brozef43aa32017-01-04 20:23:54 +01002530 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
Mikulas Patockad49ec522014-08-28 11:09:31 -04002531 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002532
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05002533 cc->page_pool = mempool_create_page_pool(BIO_MAX_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002535 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01002536 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 }
2538
Jens Axboebb799ca2008-12-10 15:35:05 +01002539 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07002540 if (!cc->bs) {
2541 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01002542 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07002543 }
2544
Mikulas Patocka7145c242015-02-13 08:24:41 -05002545 mutex_init(&cc->bio_alloc_lock);
2546
Milan Broz28513fc2010-08-12 04:14:06 +01002547 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002548 if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002549 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01002550 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08002552 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Vivek Goyale80d1c82015-07-31 09:20:36 -04002554 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
2555 if (ret) {
Milan Broz28513fc2010-08-12 04:14:06 +01002556 ti->error = "Device lookup failed";
2557 goto bad;
2558 }
2559
Vivek Goyale80d1c82015-07-31 09:20:36 -04002560 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002561 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002562 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01002563 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08002565 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Milan Brozef43aa32017-01-04 20:23:54 +01002567 if (crypt_integrity_mode(cc) || cc->integrity_iv_size) {
2568 ret = crypt_integrity_ctr(cc, ti);
Milan Broz772ae5f2011-08-02 12:32:08 +01002569 if (ret)
2570 goto bad;
2571
Milan Brozef43aa32017-01-04 20:23:54 +01002572 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
2573 if (!cc->tag_pool_max_sectors)
2574 cc->tag_pool_max_sectors = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01002575
Milan Brozef43aa32017-01-04 20:23:54 +01002576 cc->tag_pool = mempool_create_kmalloc_pool(MIN_IOS,
2577 cc->tag_pool_max_sectors * cc->on_disk_tag_size);
2578 if (!cc->tag_pool) {
2579 ti->error = "Cannot allocate integrity tags mempool";
2580 goto bad;
Milan Broz772ae5f2011-08-02 12:32:08 +01002581 }
2582 }
2583
Milan Broz28513fc2010-08-12 04:14:06 +01002584 ret = -ENOMEM;
Tejun Heo670368a2013-07-30 08:40:21 -04002585 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_MEM_RECLAIM, 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01002586 if (!cc->io_queue) {
2587 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01002588 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01002589 }
2590
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002591 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2592 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
2593 else
2594 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
2595 num_online_cpus());
Milan Brozcabf08e2007-10-19 22:38:58 +01002596 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01002597 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01002598 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01002599 }
2600
Mikulas Patockadc267622015-02-13 08:25:59 -05002601 init_waitqueue_head(&cc->write_thread_wait);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05002602 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05002603
2604 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write");
2605 if (IS_ERR(cc->write_thread)) {
2606 ret = PTR_ERR(cc->write_thread);
2607 cc->write_thread = NULL;
2608 ti->error = "Couldn't spawn write thread";
2609 goto bad;
2610 }
2611 wake_up_process(cc->write_thread);
2612
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002613 ti->num_flush_bios = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002614 ti->discard_zeroes_data_unsupported = true;
Milan Broz983c7db2011-09-25 23:26:21 +01002615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return 0;
2617
Milan Broz28513fc2010-08-12 04:14:06 +01002618bad:
2619 crypt_dtr(ti);
2620 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002623static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002625 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01002626 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002627
Milan Broz772ae5f2011-08-02 12:32:08 +01002628 /*
Mike Christie28a8f0d2016-06-05 14:32:25 -05002629 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
2630 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
Mike Christiee6047142016-06-05 14:32:04 -05002631 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
Milan Broz772ae5f2011-08-02 12:32:08 +01002632 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06002633 if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
Mike Christie28a8f0d2016-06-05 14:32:25 -05002634 bio_op(bio) == REQ_OP_DISCARD)) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002635 bio->bi_bdev = cc->dev->bdev;
Milan Broz772ae5f2011-08-02 12:32:08 +01002636 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07002637 bio->bi_iter.bi_sector = cc->start +
2638 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002639 return DM_MAPIO_REMAPPED;
2640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002642 /*
2643 * Check if bio is too large, split as needed.
2644 */
2645 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
Milan Brozef43aa32017-01-04 20:23:54 +01002646 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002647 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
2648
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002649 io = dm_per_bio_data(bio, cc->per_bio_data_size);
2650 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Milan Brozef43aa32017-01-04 20:23:54 +01002651
2652 if (cc->on_disk_tag_size) {
2653 unsigned tag_len = cc->on_disk_tag_size * bio_sectors(bio);
2654
2655 if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
2656 unlikely(!(io->integrity_metadata = kmalloc(tag_len,
2657 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
2658 if (bio_sectors(bio) > cc->tag_pool_max_sectors)
2659 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
2660 io->integrity_metadata = mempool_alloc(cc->tag_pool, GFP_NOIO);
2661 io->integrity_metadata_from_pool = true;
2662 }
2663 }
2664
2665 if (crypt_integrity_mode(cc))
2666 io->ctx.r.req_aead = (struct aead_request *)(io + 1);
2667 else
2668 io->ctx.r.req = (struct skcipher_request *)(io + 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01002669
Milan Broz20c82532011-01-13 19:59:53 +00002670 if (bio_data_dir(io->base_bio) == READ) {
2671 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05002672 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00002673 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01002674 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002676 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002679static void crypt_status(struct dm_target *ti, status_type_t type,
2680 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
Milan Broz5ebaee62010-08-12 04:14:07 +01002682 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002683 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002684 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
2686 switch (type) {
2687 case STATUSTYPE_INFO:
2688 result[0] = '\0';
2689 break;
2690
2691 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00002692 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002694 if (cc->key_size > 0) {
2695 if (cc->key_string)
2696 DMEMIT(":%u:%s", cc->key_size, cc->key_string);
2697 else
2698 for (i = 0; i < cc->key_size; i++)
2699 DMEMIT("%02x", cc->key[i]);
2700 } else
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002701 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Andrew Morton4ee218c2006-03-27 01:17:48 -08002703 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
2704 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01002705
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002706 num_feature_args += !!ti->num_discard_bios;
2707 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002708 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002709 if (cc->on_disk_tag_size)
2710 num_feature_args++;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002711 if (num_feature_args) {
2712 DMEMIT(" %d", num_feature_args);
2713 if (ti->num_discard_bios)
2714 DMEMIT(" allow_discards");
2715 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2716 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002717 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
2718 DMEMIT(" submit_from_crypt_cpus");
Milan Brozef43aa32017-01-04 20:23:54 +01002719 if (cc->on_disk_tag_size)
2720 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002721 }
Milan Broz772ae5f2011-08-02 12:32:08 +01002722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 break;
2724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725}
2726
Milan Broze48d4bb2006-10-03 01:15:37 -07002727static void crypt_postsuspend(struct dm_target *ti)
2728{
2729 struct crypt_config *cc = ti->private;
2730
2731 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2732}
2733
2734static int crypt_preresume(struct dm_target *ti)
2735{
2736 struct crypt_config *cc = ti->private;
2737
2738 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
2739 DMERR("aborting resume - crypt key is not set.");
2740 return -EAGAIN;
2741 }
2742
2743 return 0;
2744}
2745
2746static void crypt_resume(struct dm_target *ti)
2747{
2748 struct crypt_config *cc = ti->private;
2749
2750 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2751}
2752
2753/* Message interface
2754 * key set <key>
2755 * key wipe
2756 */
2757static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
2758{
2759 struct crypt_config *cc = ti->private;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002760 int key_size, ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07002761
2762 if (argc < 2)
2763 goto error;
2764
Mike Snitzer498f0102011-08-02 12:32:04 +01002765 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07002766 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
2767 DMWARN("not suspended during key manipulation.");
2768 return -EINVAL;
2769 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002770 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002771 /* The key size may not be changed. */
2772 key_size = get_key_size(&argv[2]);
2773 if (key_size < 0 || cc->key_size != key_size) {
2774 memset(argv[2], '0', strlen(argv[2]));
2775 return -EINVAL;
2776 }
2777
Milan Broz542da312009-12-10 23:51:57 +00002778 ret = crypt_set_key(cc, argv[2]);
2779 if (ret)
2780 return ret;
2781 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
2782 ret = cc->iv_gen_ops->init(cc);
2783 return ret;
2784 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002785 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00002786 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
2787 ret = cc->iv_gen_ops->wipe(cc);
2788 if (ret)
2789 return ret;
2790 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002791 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00002792 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002793 }
2794
2795error:
2796 DMWARN("unrecognised message received.");
2797 return -EINVAL;
2798}
2799
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002800static int crypt_iterate_devices(struct dm_target *ti,
2801 iterate_devices_callout_fn fn, void *data)
2802{
2803 struct crypt_config *cc = ti->private;
2804
Mike Snitzer5dea2712009-07-23 20:30:42 +01002805 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002806}
2807
Mike Snitzer586b2862015-09-09 21:34:51 -04002808static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
2809{
2810 /*
2811 * Unfortunate constraint that is required to avoid the potential
2812 * for exceeding underlying device's max_segments limits -- due to
2813 * crypt_alloc_buffer() possibly allocating pages for the encryption
2814 * bio that are not as physically contiguous as the original bio.
2815 */
2816 limits->max_segment_size = PAGE_SIZE;
2817}
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819static struct target_type crypt_target = {
2820 .name = "crypt",
Milan Brozef43aa32017-01-04 20:23:54 +01002821 .version = {1, 16, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 .module = THIS_MODULE,
2823 .ctr = crypt_ctr,
2824 .dtr = crypt_dtr,
2825 .map = crypt_map,
2826 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07002827 .postsuspend = crypt_postsuspend,
2828 .preresume = crypt_preresume,
2829 .resume = crypt_resume,
2830 .message = crypt_message,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002831 .iterate_devices = crypt_iterate_devices,
Mike Snitzer586b2862015-09-09 21:34:51 -04002832 .io_hints = crypt_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833};
2834
2835static int __init dm_crypt_init(void)
2836{
2837 int r;
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05002840 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002841 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 return r;
2844}
2845
2846static void __exit dm_crypt_exit(void)
2847{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002848 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
2851module_init(dm_crypt_init);
2852module_exit(dm_crypt_exit);
2853
Jana Saoutbf142992014-06-24 14:27:04 -04002854MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
2856MODULE_LICENSE("GPL");