blob: 34f5de13a93d160c0058a45946ef905c67657d6d [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;
AliOS system security4e26ee32018-11-05 15:31:42 +080052 u64 cc_sector;
Mikulas Patocka40b62292012-07-27 15:08:04 +010053 atomic_t cc_pending;
Milan Brozef43aa32017-01-04 20:23:54 +010054 union {
55 struct skcipher_request *req;
56 struct aead_request *req_aead;
57 } r;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Milan Broz53017032008-02-08 02:10:38 +000061/*
62 * per bio private data
63 */
64struct dm_crypt_io {
Alasdair G Kergon49a8a922012-07-27 15:08:05 +010065 struct crypt_config *cc;
Milan Broz53017032008-02-08 02:10:38 +000066 struct bio *base_bio;
Milan Brozef43aa32017-01-04 20:23:54 +010067 u8 *integrity_metadata;
68 bool integrity_metadata_from_pool;
Milan Broz53017032008-02-08 02:10:38 +000069 struct work_struct work;
70
71 struct convert_context ctx;
72
Mikulas Patocka40b62292012-07-27 15:08:04 +010073 atomic_t io_pending;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020074 blk_status_t error;
Milan Broz0c395b02008-02-08 02:10:54 +000075 sector_t sector;
Mikulas Patockadc267622015-02-13 08:25:59 -050076
Mikulas Patockab3c5fd32015-02-13 08:27:41 -050077 struct rb_node rb_node;
Mikulas Patocka298a9fa2014-03-28 15:51:55 -040078} CRYPTO_MINALIGN_ATTR;
Milan Broz53017032008-02-08 02:10:38 +000079
Milan Broz01482b72008-02-08 02:11:04 +000080struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000081 struct convert_context *ctx;
Milan Brozef43aa32017-01-04 20:23:54 +010082 struct scatterlist sg_in[4];
83 struct scatterlist sg_out[4];
AliOS system security4e26ee32018-11-05 15:31:42 +080084 u64 iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000085};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct crypt_config;
88
89struct crypt_iv_operations {
90 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010091 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000093 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000094 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc53272011-01-13 19:59:54 +000095 int (*generator)(struct crypt_config *cc, u8 *iv,
96 struct dm_crypt_request *dmreq);
97 int (*post)(struct crypt_config *cc, u8 *iv,
98 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Milan Broz60473592009-12-10 23:51:55 +0000101struct iv_essiv_private {
Kees Cookc07c88f2018-07-15 20:59:12 -0700102 struct crypto_shash *hash_tfm;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000103 u8 *salt;
Milan Broz60473592009-12-10 23:51:55 +0000104};
105
106struct iv_benbi_private {
107 int shift;
108};
109
Milan Broz34745782011-01-13 19:59:55 +0000110#define LMK_SEED_SIZE 64 /* hash + 0 */
111struct iv_lmk_private {
112 struct crypto_shash *hash_tfm;
113 u8 *seed;
114};
115
Milan Brozed04d982013-10-28 23:21:04 +0100116#define TCW_WHITENING_SIZE 16
117struct iv_tcw_private {
118 struct crypto_shash *crc32_tfm;
119 u8 *iv_seed;
120 u8 *whitening;
121};
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/*
124 * Crypt: maps a linear range of a block device
125 * and encrypts / decrypts at the same time.
126 */
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -0500127enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID,
Rabin Vincentf659b102016-09-21 16:22:29 +0200128 DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD };
Andi Kleenc0297722011-01-13 19:59:53 +0000129
Milan Brozef43aa32017-01-04 20:23:54 +0100130enum cipher_flags {
131 CRYPT_MODE_INTEGRITY_AEAD, /* Use authenticated mode for cihper */
Milan Broz8f0009a2017-03-16 15:39:44 +0100132 CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */
Milan Brozef43aa32017-01-04 20:23:54 +0100133};
134
Andi Kleenc0297722011-01-13 19:59:53 +0000135/*
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500136 * The fields in here must be read only after initialization.
Andi Kleenc0297722011-01-13 19:59:53 +0000137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138struct crypt_config {
139 struct dm_dev *dev;
140 sector_t start;
141
Mikulas Patocka50593532017-08-13 22:45:08 -0400142 struct percpu_counter n_allocated_pages;
143
Milan Brozcabf08e2007-10-19 22:38:58 +0100144 struct workqueue_struct *io_queue;
145 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700146
Mikulas Patockac7329ef2018-07-11 12:10:51 -0400147 spinlock_t write_thread_lock;
Mike Snitzer72d711c2018-05-22 18:26:20 -0400148 struct task_struct *write_thread;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -0500149 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -0500150
Milan Broz5ebaee62010-08-12 04:14:07 +0100151 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000152 char *cipher_string;
Milan Brozef43aa32017-01-04 20:23:54 +0100153 char *cipher_auth;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +0100154 char *key_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100155
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100156 const struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800157 union {
Milan Broz60473592009-12-10 23:51:55 +0000158 struct iv_essiv_private essiv;
159 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000160 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100161 struct iv_tcw_private tcw;
Herbert Xu79066ad2006-12-05 13:41:52 -0800162 } iv_gen_private;
AliOS system security4e26ee32018-11-05 15:31:42 +0800163 u64 iv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 unsigned int iv_size;
Mikulas Patockaff3af922017-03-23 10:23:14 -0400165 unsigned short int sector_size;
166 unsigned char sector_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100168 /* ESSIV: struct crypto_cipher *essiv_tfm */
169 void *iv_private;
Milan Brozef43aa32017-01-04 20:23:54 +0100170 union {
171 struct crypto_skcipher **tfms;
172 struct crypto_aead **tfms_aead;
173 } cipher_tfm;
Milan Brozd1f96422011-01-13 19:59:54 +0000174 unsigned tfms_count;
Milan Brozef43aa32017-01-04 20:23:54 +0100175 unsigned long cipher_flags;
Andi Kleenc0297722011-01-13 19:59:53 +0000176
177 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000178 * Layout of each crypto request:
179 *
Herbert Xubbdb23b2016-01-24 21:16:36 +0800180 * struct skcipher_request
Milan Brozddd42ed2008-02-08 02:11:07 +0000181 * context
182 * padding
183 * struct dm_crypt_request
184 * padding
185 * IV
186 *
187 * The padding is added so that dm_crypt_request and the IV are
188 * correctly aligned.
189 */
190 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000191
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400192 unsigned int per_bio_data_size;
193
Milan Broze48d4bb2006-10-03 01:15:37 -0700194 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100196 unsigned int key_parts; /* independent parts in key buffer */
197 unsigned int key_extra_size; /* additional keys length */
Milan Brozef43aa32017-01-04 20:23:54 +0100198 unsigned int key_mac_size; /* MAC key size for authenc(...) */
199
200 unsigned int integrity_tag_size;
201 unsigned int integrity_iv_size;
202 unsigned int on_disk_tag_size;
203
Mike Snitzer72d711c2018-05-22 18:26:20 -0400204 /*
205 * pool for per bio private data, crypto requests,
206 * encryption requeusts/buffer pages and integrity tags
207 */
208 unsigned tag_pool_max_sectors;
209 mempool_t tag_pool;
210 mempool_t req_pool;
211 mempool_t page_pool;
212
213 struct bio_set bs;
214 struct mutex bio_alloc_lock;
215
Milan Brozef43aa32017-01-04 20:23:54 +0100216 u8 *authenc_key; /* space for keys in authenc() format (if used) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 u8 key[0];
218};
219
Milan Brozef43aa32017-01-04 20:23:54 +0100220#define MIN_IOS 64
221#define MAX_TAG_SIZE 480
222#define POOL_ENTRY_SIZE 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Mikulas Patocka50593532017-08-13 22:45:08 -0400224static DEFINE_SPINLOCK(dm_crypt_clients_lock);
225static unsigned dm_crypt_clients_n = 0;
226static volatile unsigned long dm_crypt_pages_per_client;
227#define DM_CRYPT_MEMORY_PERCENT 2
228#define DM_CRYPT_MIN_PAGES_PER_CLIENT (BIO_MAX_PAGES * 16)
229
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100230static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000231static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Brozef43aa32017-01-04 20:23:54 +0100232static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
233 struct scatterlist *sg);
Olaf Kirch027581f2007-05-09 02:32:52 -0700234
Andi Kleenc0297722011-01-13 19:59:53 +0000235/*
Eric Biggers86f917a2017-03-30 22:18:48 -0700236 * Use this to access cipher attributes that are independent of the key.
Andi Kleenc0297722011-01-13 19:59:53 +0000237 */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800238static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +0000239{
Milan Brozef43aa32017-01-04 20:23:54 +0100240 return cc->cipher_tfm.tfms[0];
241}
242
243static struct crypto_aead *any_tfm_aead(struct crypt_config *cc)
244{
245 return cc->cipher_tfm.tfms_aead[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 * Different IV generation algorithms:
250 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000251 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200252 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *
Milan Broz61afef62009-12-10 23:52:25 +0000254 * plain64: the initial vector is the 64-bit little-endian version of the sector
255 * number, padded with zeros if necessary.
256 *
Milan Broz7e3fd852017-06-06 09:07:01 +0200257 * plain64be: the initial vector is the 64-bit big-endian version of the sector
258 * number, padded with zeros if necessary.
259 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000260 * essiv: "encrypted sector|salt initial vector", the sector number is
261 * encrypted with the bulk cipher using a salt as key. The salt
262 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *
Rik Snel48527fa2006-09-03 08:56:39 +1000264 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
265 * (needed for LRW-32-AES and possible other narrow block modes)
266 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700267 * null: the initial vector is always zero. Provides compatibility with
268 * obsolete loop_fish2 devices. Do not use for new devices.
269 *
Milan Broz34745782011-01-13 19:59:55 +0000270 * lmk: Compatible implementation of the block chaining mode used
271 * by the Loop-AES block device encryption system
272 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
273 * It operates on full 512 byte sectors and uses CBC
274 * with an IV derived from the sector number, the data and
275 * optionally extra IV seed.
276 * This means that after decryption the first block
277 * of sector must be tweaked according to decrypted data.
278 * Loop-AES can use three encryption schemes:
279 * version 1: is plain aes-cbc mode
280 * version 2: uses 64 multikey scheme with lmk IV generator
281 * version 3: the same as version 2 with additional IV seed
282 * (it uses 65 keys, last key is used as IV seed)
283 *
Milan Brozed04d982013-10-28 23:21:04 +0100284 * tcw: Compatible implementation of the block chaining mode used
285 * by the TrueCrypt device encryption system (prior to version 4.1).
Milan Broze44f23b2015-04-05 18:03:10 +0200286 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
Milan Brozed04d982013-10-28 23:21:04 +0100287 * It operates on full 512 byte sectors and uses CBC
288 * with an IV derived from initial key and the sector number.
289 * In addition, whitening value is applied on every sector, whitening
290 * is calculated from initial key, sector number and mixed using CRC32.
291 * Note that this encryption scheme is vulnerable to watermarking attacks
292 * and should be used for old compatible containers access only.
293 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * plumb: unimplemented, see:
295 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
296 */
297
Milan Broz2dc53272011-01-13 19:59:54 +0000298static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
299 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100302 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 return 0;
305}
306
Milan Broz61afef62009-12-10 23:52:25 +0000307static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000308 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000309{
310 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100311 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000312
313 return 0;
314}
315
Milan Broz7e3fd852017-06-06 09:07:01 +0200316static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv,
317 struct dm_crypt_request *dmreq)
318{
319 memset(iv, 0, cc->iv_size);
320 /* iv_size is at least of size u64; usually it is 16 bytes */
321 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector);
322
323 return 0;
324}
325
Milan Brozb95bf2d2009-12-10 23:51:56 +0000326/* Initialise ESSIV - compute salt but no local memory allocations */
327static int crypt_iv_essiv_init(struct crypt_config *cc)
328{
329 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Kees Cookc07c88f2018-07-15 20:59:12 -0700330 SHASH_DESC_ON_STACK(desc, essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000331 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100332 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000333
Kees Cookc07c88f2018-07-15 20:59:12 -0700334 desc->tfm = essiv->hash_tfm;
Mikulas Patocka432061b2018-09-05 09:17:45 -0400335 desc->flags = 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000336
Kees Cookc07c88f2018-07-15 20:59:12 -0700337 err = crypto_shash_digest(desc, cc->key, cc->key_size, essiv->salt);
338 shash_desc_zero(desc);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000339 if (err)
340 return err;
341
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100342 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000343
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100344 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
Kees Cookc07c88f2018-07-15 20:59:12 -0700345 crypto_shash_digestsize(essiv->hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100346 if (err)
347 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000348
349 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000350}
351
Milan Broz542da312009-12-10 23:51:57 +0000352/* Wipe salt and reset key derived from volume key */
353static int crypt_iv_essiv_wipe(struct crypt_config *cc)
354{
355 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Kees Cookc07c88f2018-07-15 20:59:12 -0700356 unsigned salt_size = crypto_shash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000357 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100358 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000359
360 memset(essiv->salt, 0, salt_size);
361
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100362 essiv_tfm = cc->iv_private;
363 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
364 if (r)
365 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000366
367 return err;
368}
369
Eric Biggers86f917a2017-03-30 22:18:48 -0700370/* Allocate the cipher for ESSIV */
371static struct crypto_cipher *alloc_essiv_cipher(struct crypt_config *cc,
372 struct dm_target *ti,
373 const u8 *salt,
374 unsigned int saltsize)
Andi Kleenc0297722011-01-13 19:59:53 +0000375{
376 struct crypto_cipher *essiv_tfm;
377 int err;
378
379 /* Setup the essiv_tfm with the given salt */
380 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
381 if (IS_ERR(essiv_tfm)) {
382 ti->error = "Error allocating crypto tfm for ESSIV";
383 return essiv_tfm;
384 }
385
Milan Brozef43aa32017-01-04 20:23:54 +0100386 if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) {
Andi Kleenc0297722011-01-13 19:59:53 +0000387 ti->error = "Block size of ESSIV cipher does "
388 "not match IV size of block cipher";
389 crypto_free_cipher(essiv_tfm);
390 return ERR_PTR(-EINVAL);
391 }
392
393 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
394 if (err) {
395 ti->error = "Failed to set key for ESSIV cipher";
396 crypto_free_cipher(essiv_tfm);
397 return ERR_PTR(err);
398 }
399
400 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000401}
402
Milan Broz60473592009-12-10 23:51:55 +0000403static void crypt_iv_essiv_dtr(struct crypt_config *cc)
404{
Andi Kleenc0297722011-01-13 19:59:53 +0000405 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000406 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
407
Kees Cookc07c88f2018-07-15 20:59:12 -0700408 crypto_free_shash(essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000409 essiv->hash_tfm = NULL;
410
411 kzfree(essiv->salt);
412 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000413
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100414 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000415
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100416 if (essiv_tfm)
417 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000418
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100419 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100423 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Milan Broz5861f1b2009-12-10 23:51:56 +0000425 struct crypto_cipher *essiv_tfm = NULL;
Kees Cookc07c88f2018-07-15 20:59:12 -0700426 struct crypto_shash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000427 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100428 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Milan Broz5861f1b2009-12-10 23:51:56 +0000430 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700431 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return -EINVAL;
433 }
434
Milan Brozb95bf2d2009-12-10 23:51:56 +0000435 /* Allocate hash algorithm */
Kees Cookc07c88f2018-07-15 20:59:12 -0700436 hash_tfm = crypto_alloc_shash(opts, 0, 0);
Herbert Xu35058682006-08-24 19:10:20 +1000437 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700438 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000439 err = PTR_ERR(hash_tfm);
440 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
Kees Cookc07c88f2018-07-15 20:59:12 -0700443 salt = kzalloc(crypto_shash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000444 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700445 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000446 err = -ENOMEM;
447 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
Milan Brozb95bf2d2009-12-10 23:51:56 +0000450 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000451 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
452
Eric Biggers86f917a2017-03-30 22:18:48 -0700453 essiv_tfm = alloc_essiv_cipher(cc, ti, salt,
Kees Cookc07c88f2018-07-15 20:59:12 -0700454 crypto_shash_digestsize(hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100455 if (IS_ERR(essiv_tfm)) {
456 crypt_iv_essiv_dtr(cc);
457 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000458 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100459 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000462
463bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000464 if (hash_tfm && !IS_ERR(hash_tfm))
Kees Cookc07c88f2018-07-15 20:59:12 -0700465 crypto_free_shash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000466 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000467 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Milan Broz2dc53272011-01-13 19:59:54 +0000470static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
471 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100473 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100476 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000477 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
480}
481
Rik Snel48527fa2006-09-03 08:56:39 +1000482static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
483 const char *opts)
484{
Herbert Xubbdb23b2016-01-24 21:16:36 +0800485 unsigned bs = crypto_skcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800486 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000487
488 /* we need to calculate how far we must shift the sector count
489 * to get the cipher block count, we use this shift in _gen */
490
491 if (1 << log != bs) {
492 ti->error = "cypher blocksize is not a power of 2";
493 return -EINVAL;
494 }
495
496 if (log > 9) {
497 ti->error = "cypher blocksize is > 512";
498 return -EINVAL;
499 }
500
Milan Broz60473592009-12-10 23:51:55 +0000501 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000502
503 return 0;
504}
505
506static void crypt_iv_benbi_dtr(struct crypt_config *cc)
507{
Rik Snel48527fa2006-09-03 08:56:39 +1000508}
509
Milan Broz2dc53272011-01-13 19:59:54 +0000510static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
511 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000512{
Herbert Xu79066ad2006-12-05 13:41:52 -0800513 __be64 val;
514
Rik Snel48527fa2006-09-03 08:56:39 +1000515 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800516
Milan Broz2dc53272011-01-13 19:59:54 +0000517 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800518 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return 0;
521}
522
Milan Broz2dc53272011-01-13 19:59:54 +0000523static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
524 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700525{
526 memset(iv, 0, cc->iv_size);
527
528 return 0;
529}
530
Milan Broz34745782011-01-13 19:59:55 +0000531static void crypt_iv_lmk_dtr(struct crypt_config *cc)
532{
533 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
534
535 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
536 crypto_free_shash(lmk->hash_tfm);
537 lmk->hash_tfm = NULL;
538
539 kzfree(lmk->seed);
540 lmk->seed = NULL;
541}
542
543static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
544 const char *opts)
545{
546 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
547
Milan Broz8f0009a2017-03-16 15:39:44 +0100548 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
549 ti->error = "Unsupported sector size for LMK";
550 return -EINVAL;
551 }
552
Milan Broz34745782011-01-13 19:59:55 +0000553 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
554 if (IS_ERR(lmk->hash_tfm)) {
555 ti->error = "Error initializing LMK hash";
556 return PTR_ERR(lmk->hash_tfm);
557 }
558
559 /* No seed in LMK version 2 */
560 if (cc->key_parts == cc->tfms_count) {
561 lmk->seed = NULL;
562 return 0;
563 }
564
565 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
566 if (!lmk->seed) {
567 crypt_iv_lmk_dtr(cc);
568 ti->error = "Error kmallocing seed storage in LMK";
569 return -ENOMEM;
570 }
571
572 return 0;
573}
574
575static int crypt_iv_lmk_init(struct crypt_config *cc)
576{
577 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
578 int subkey_size = cc->key_size / cc->key_parts;
579
580 /* LMK seed is on the position of LMK_KEYS + 1 key */
581 if (lmk->seed)
582 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
583 crypto_shash_digestsize(lmk->hash_tfm));
584
585 return 0;
586}
587
588static int crypt_iv_lmk_wipe(struct crypt_config *cc)
589{
590 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
591
592 if (lmk->seed)
593 memset(lmk->seed, 0, LMK_SEED_SIZE);
594
595 return 0;
596}
597
598static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
599 struct dm_crypt_request *dmreq,
600 u8 *data)
601{
602 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200603 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000604 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100605 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000606 int i, r;
607
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200608 desc->tfm = lmk->hash_tfm;
Mikulas Patocka432061b2018-09-05 09:17:45 -0400609 desc->flags = 0;
Milan Broz34745782011-01-13 19:59:55 +0000610
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200611 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000612 if (r)
613 return r;
614
615 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200616 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000617 if (r)
618 return r;
619 }
620
621 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200622 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000623 if (r)
624 return r;
625
626 /* Sector is cropped to 56 bits here */
627 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
628 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
629 buf[2] = cpu_to_le32(4024);
630 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200631 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000632 if (r)
633 return r;
634
635 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200636 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000637 if (r)
638 return r;
639
640 for (i = 0; i < MD5_HASH_WORDS; i++)
641 __cpu_to_le32s(&md5state.hash[i]);
642 memcpy(iv, &md5state.hash, cc->iv_size);
643
644 return 0;
645}
646
647static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
648 struct dm_crypt_request *dmreq)
649{
Milan Brozef43aa32017-01-04 20:23:54 +0100650 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000651 u8 *src;
652 int r = 0;
653
654 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100655 sg = crypt_get_sg_data(cc, dmreq->sg_in);
656 src = kmap_atomic(sg_page(sg));
657 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800658 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000659 } else
660 memset(iv, 0, cc->iv_size);
661
662 return r;
663}
664
665static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
666 struct dm_crypt_request *dmreq)
667{
Milan Brozef43aa32017-01-04 20:23:54 +0100668 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000669 u8 *dst;
670 int r;
671
672 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
673 return 0;
674
Milan Brozef43aa32017-01-04 20:23:54 +0100675 sg = crypt_get_sg_data(cc, dmreq->sg_out);
676 dst = kmap_atomic(sg_page(sg));
677 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset);
Milan Broz34745782011-01-13 19:59:55 +0000678
679 /* Tweak the first block of plaintext sector */
680 if (!r)
Milan Brozef43aa32017-01-04 20:23:54 +0100681 crypto_xor(dst + sg->offset, iv, cc->iv_size);
Milan Broz34745782011-01-13 19:59:55 +0000682
Cong Wangc2e022c2011-11-28 13:26:02 +0800683 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000684 return r;
685}
686
Milan Brozed04d982013-10-28 23:21:04 +0100687static void crypt_iv_tcw_dtr(struct crypt_config *cc)
688{
689 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
690
691 kzfree(tcw->iv_seed);
692 tcw->iv_seed = NULL;
693 kzfree(tcw->whitening);
694 tcw->whitening = NULL;
695
696 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
697 crypto_free_shash(tcw->crc32_tfm);
698 tcw->crc32_tfm = NULL;
699}
700
701static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
702 const char *opts)
703{
704 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
705
Milan Broz8f0009a2017-03-16 15:39:44 +0100706 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
707 ti->error = "Unsupported sector size for TCW";
708 return -EINVAL;
709 }
710
Milan Brozed04d982013-10-28 23:21:04 +0100711 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
712 ti->error = "Wrong key size for TCW";
713 return -EINVAL;
714 }
715
716 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
717 if (IS_ERR(tcw->crc32_tfm)) {
718 ti->error = "Error initializing CRC32 in TCW";
719 return PTR_ERR(tcw->crc32_tfm);
720 }
721
722 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
723 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
724 if (!tcw->iv_seed || !tcw->whitening) {
725 crypt_iv_tcw_dtr(cc);
726 ti->error = "Error allocating seed storage in TCW";
727 return -ENOMEM;
728 }
729
730 return 0;
731}
732
733static int crypt_iv_tcw_init(struct crypt_config *cc)
734{
735 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
736 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
737
738 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
739 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
740 TCW_WHITENING_SIZE);
741
742 return 0;
743}
744
745static int crypt_iv_tcw_wipe(struct crypt_config *cc)
746{
747 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
748
749 memset(tcw->iv_seed, 0, cc->iv_size);
750 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
751
752 return 0;
753}
754
755static int crypt_iv_tcw_whitening(struct crypt_config *cc,
756 struct dm_crypt_request *dmreq,
757 u8 *data)
758{
759 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200760 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100761 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200762 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100763 int i, r;
764
765 /* xor whitening with sector number */
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100766 crypto_xor_cpy(buf, tcw->whitening, (u8 *)&sector, 8);
767 crypto_xor_cpy(&buf[8], tcw->whitening + 8, (u8 *)&sector, 8);
Milan Brozed04d982013-10-28 23:21:04 +0100768
769 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200770 desc->tfm = tcw->crc32_tfm;
Mikulas Patocka432061b2018-09-05 09:17:45 -0400771 desc->flags = 0;
Milan Brozed04d982013-10-28 23:21:04 +0100772 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200773 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100774 if (r)
775 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200776 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100777 if (r)
778 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200779 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100780 if (r)
781 goto out;
782 }
783 crypto_xor(&buf[0], &buf[12], 4);
784 crypto_xor(&buf[4], &buf[8], 4);
785
786 /* apply whitening (8 bytes) to whole sector */
787 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
788 crypto_xor(data + i * 8, buf, 8);
789out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100790 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100791 return r;
792}
793
794static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
795 struct dm_crypt_request *dmreq)
796{
Milan Brozef43aa32017-01-04 20:23:54 +0100797 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100798 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200799 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100800 u8 *src;
801 int r = 0;
802
803 /* Remove whitening from ciphertext */
804 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100805 sg = crypt_get_sg_data(cc, dmreq->sg_in);
806 src = kmap_atomic(sg_page(sg));
807 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100808 kunmap_atomic(src);
809 }
810
811 /* Calculate IV */
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100812 crypto_xor_cpy(iv, tcw->iv_seed, (u8 *)&sector, 8);
Milan Brozed04d982013-10-28 23:21:04 +0100813 if (cc->iv_size > 8)
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100814 crypto_xor_cpy(&iv[8], tcw->iv_seed + 8, (u8 *)&sector,
815 cc->iv_size - 8);
Milan Brozed04d982013-10-28 23:21:04 +0100816
817 return r;
818}
819
820static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
821 struct dm_crypt_request *dmreq)
822{
Milan Brozef43aa32017-01-04 20:23:54 +0100823 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100824 u8 *dst;
825 int r;
826
827 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
828 return 0;
829
830 /* Apply whitening on ciphertext */
Milan Brozef43aa32017-01-04 20:23:54 +0100831 sg = crypt_get_sg_data(cc, dmreq->sg_out);
832 dst = kmap_atomic(sg_page(sg));
833 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100834 kunmap_atomic(dst);
835
836 return r;
837}
838
Milan Brozef43aa32017-01-04 20:23:54 +0100839static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv,
840 struct dm_crypt_request *dmreq)
841{
842 /* Used only for writes, there must be an additional space to store IV */
843 get_random_bytes(iv, cc->iv_size);
844 return 0;
845}
846
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100847static const struct crypt_iv_operations crypt_iv_plain_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 .generator = crypt_iv_plain_gen
849};
850
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100851static const struct crypt_iv_operations crypt_iv_plain64_ops = {
Milan Broz61afef62009-12-10 23:52:25 +0000852 .generator = crypt_iv_plain64_gen
853};
854
Milan Broz7e3fd852017-06-06 09:07:01 +0200855static const struct crypt_iv_operations crypt_iv_plain64be_ops = {
856 .generator = crypt_iv_plain64be_gen
857};
858
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100859static const struct crypt_iv_operations crypt_iv_essiv_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 .ctr = crypt_iv_essiv_ctr,
861 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000862 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000863 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 .generator = crypt_iv_essiv_gen
865};
866
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100867static const struct crypt_iv_operations crypt_iv_benbi_ops = {
Rik Snel48527fa2006-09-03 08:56:39 +1000868 .ctr = crypt_iv_benbi_ctr,
869 .dtr = crypt_iv_benbi_dtr,
870 .generator = crypt_iv_benbi_gen
871};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100873static const struct crypt_iv_operations crypt_iv_null_ops = {
Ludwig Nussel46b47732007-05-09 02:32:55 -0700874 .generator = crypt_iv_null_gen
875};
876
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100877static const struct crypt_iv_operations crypt_iv_lmk_ops = {
Milan Broz34745782011-01-13 19:59:55 +0000878 .ctr = crypt_iv_lmk_ctr,
879 .dtr = crypt_iv_lmk_dtr,
880 .init = crypt_iv_lmk_init,
881 .wipe = crypt_iv_lmk_wipe,
882 .generator = crypt_iv_lmk_gen,
883 .post = crypt_iv_lmk_post
884};
885
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100886static const struct crypt_iv_operations crypt_iv_tcw_ops = {
Milan Brozed04d982013-10-28 23:21:04 +0100887 .ctr = crypt_iv_tcw_ctr,
888 .dtr = crypt_iv_tcw_dtr,
889 .init = crypt_iv_tcw_init,
890 .wipe = crypt_iv_tcw_wipe,
891 .generator = crypt_iv_tcw_gen,
892 .post = crypt_iv_tcw_post
893};
894
Milan Brozef43aa32017-01-04 20:23:54 +0100895static struct crypt_iv_operations crypt_iv_random_ops = {
896 .generator = crypt_iv_random_gen
897};
898
899/*
900 * Integrity extensions
901 */
902static bool crypt_integrity_aead(struct crypt_config *cc)
903{
904 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
905}
906
907static bool crypt_integrity_hmac(struct crypt_config *cc)
908{
Milan Broz33d2f092017-03-16 15:39:40 +0100909 return crypt_integrity_aead(cc) && cc->key_mac_size;
Milan Brozef43aa32017-01-04 20:23:54 +0100910}
911
912/* Get sg containing data */
913static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
914 struct scatterlist *sg)
915{
Milan Broz33d2f092017-03-16 15:39:40 +0100916 if (unlikely(crypt_integrity_aead(cc)))
Milan Brozef43aa32017-01-04 20:23:54 +0100917 return &sg[2];
918
919 return sg;
920}
921
922static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
923{
924 struct bio_integrity_payload *bip;
925 unsigned int tag_len;
926 int ret;
927
928 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size)
929 return 0;
930
931 bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
932 if (IS_ERR(bip))
933 return PTR_ERR(bip);
934
Mikulas Patocka3ec93eb2019-02-08 10:52:07 -0500935 tag_len = io->cc->on_disk_tag_size * (bio_sectors(bio) >> io->cc->sector_shift);
Milan Brozef43aa32017-01-04 20:23:54 +0100936
937 bip->bip_iter.bi_size = tag_len;
938 bip->bip_iter.bi_sector = io->cc->start + io->sector;
939
Milan Brozef43aa32017-01-04 20:23:54 +0100940 ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
941 tag_len, offset_in_page(io->integrity_metadata));
942 if (unlikely(ret != tag_len))
943 return -ENOMEM;
944
945 return 0;
946}
947
948static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
949{
950#ifdef CONFIG_BLK_DEV_INTEGRITY
951 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk);
Milan Brozfcb2f1e2019-05-15 16:23:43 +0200952 struct mapped_device *md = dm_table_get_md(ti->table);
Milan Brozef43aa32017-01-04 20:23:54 +0100953
954 /* From now we require underlying device with our integrity profile */
955 if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) {
956 ti->error = "Integrity profile not supported.";
957 return -EINVAL;
958 }
959
Mikulas Patocka583fe742017-04-18 16:51:54 -0400960 if (bi->tag_size != cc->on_disk_tag_size ||
961 bi->tuple_size != cc->on_disk_tag_size) {
Milan Brozef43aa32017-01-04 20:23:54 +0100962 ti->error = "Integrity profile tag size mismatch.";
963 return -EINVAL;
964 }
Mikulas Patocka583fe742017-04-18 16:51:54 -0400965 if (1 << bi->interval_exp != cc->sector_size) {
966 ti->error = "Integrity profile sector size mismatch.";
967 return -EINVAL;
968 }
Milan Brozef43aa32017-01-04 20:23:54 +0100969
Milan Broz33d2f092017-03-16 15:39:40 +0100970 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +0100971 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size;
Milan Brozfcb2f1e2019-05-15 16:23:43 +0200972 DMDEBUG("%s: Integrity AEAD, tag size %u, IV size %u.", dm_device_name(md),
Milan Brozef43aa32017-01-04 20:23:54 +0100973 cc->integrity_tag_size, cc->integrity_iv_size);
974
975 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) {
976 ti->error = "Integrity AEAD auth tag size is not supported.";
977 return -EINVAL;
978 }
979 } else if (cc->integrity_iv_size)
Milan Brozfcb2f1e2019-05-15 16:23:43 +0200980 DMDEBUG("%s: Additional per-sector space %u bytes for IV.", dm_device_name(md),
Milan Brozef43aa32017-01-04 20:23:54 +0100981 cc->integrity_iv_size);
982
983 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) {
984 ti->error = "Not enough space for integrity tag in the profile.";
985 return -EINVAL;
986 }
987
988 return 0;
989#else
990 ti->error = "Integrity profile not supported.";
991 return -EINVAL;
992#endif
993}
994
Milan Brozd469f842007-10-19 22:42:37 +0100995static void crypt_convert_init(struct crypt_config *cc,
996 struct convert_context *ctx,
997 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000998 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 ctx->bio_in = bio_in;
1001 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -07001002 if (bio_in)
1003 ctx->iter_in = bio_in->bi_iter;
1004 if (bio_out)
1005 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +01001006 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +00001007 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Huang Yingb2174ee2009-03-16 17:44:33 +00001010static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
Milan Brozef43aa32017-01-04 20:23:54 +01001011 void *req)
Huang Yingb2174ee2009-03-16 17:44:33 +00001012{
1013 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
1014}
1015
Milan Brozef43aa32017-01-04 20:23:54 +01001016static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq)
Huang Yingb2174ee2009-03-16 17:44:33 +00001017{
Milan Brozef43aa32017-01-04 20:23:54 +01001018 return (void *)((char *)dmreq - cc->dmreq_start);
Huang Yingb2174ee2009-03-16 17:44:33 +00001019}
1020
Milan Broz2dc53272011-01-13 19:59:54 +00001021static u8 *iv_of_dmreq(struct crypt_config *cc,
1022 struct dm_crypt_request *dmreq)
1023{
Milan Broz33d2f092017-03-16 15:39:40 +01001024 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001025 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1026 crypto_aead_alignmask(any_tfm_aead(cc)) + 1);
1027 else
1028 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1029 crypto_skcipher_alignmask(any_tfm(cc)) + 1);
Milan Broz2dc53272011-01-13 19:59:54 +00001030}
1031
Milan Brozef43aa32017-01-04 20:23:54 +01001032static u8 *org_iv_of_dmreq(struct crypt_config *cc,
1033 struct dm_crypt_request *dmreq)
1034{
1035 return iv_of_dmreq(cc, dmreq) + cc->iv_size;
1036}
1037
1038static uint64_t *org_sector_of_dmreq(struct crypt_config *cc,
1039 struct dm_crypt_request *dmreq)
1040{
1041 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size;
1042 return (uint64_t*) ptr;
1043}
1044
1045static unsigned int *org_tag_of_dmreq(struct crypt_config *cc,
1046 struct dm_crypt_request *dmreq)
1047{
1048 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size +
1049 cc->iv_size + sizeof(uint64_t);
1050 return (unsigned int*)ptr;
1051}
1052
1053static void *tag_from_dmreq(struct crypt_config *cc,
1054 struct dm_crypt_request *dmreq)
1055{
1056 struct convert_context *ctx = dmreq->ctx;
1057 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1058
1059 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) *
1060 cc->on_disk_tag_size];
1061}
1062
1063static void *iv_tag_from_dmreq(struct crypt_config *cc,
1064 struct dm_crypt_request *dmreq)
1065{
1066 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size;
1067}
1068
1069static int crypt_convert_block_aead(struct crypt_config *cc,
1070 struct convert_context *ctx,
1071 struct aead_request *req,
1072 unsigned int tag_offset)
Milan Broz01482b72008-02-08 02:11:04 +00001073{
Kent Overstreet003b5c52013-10-11 15:45:43 -07001074 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1075 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001076 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001077 u8 *iv, *org_iv, *tag_iv, *tag;
1078 uint64_t *sector;
1079 int r = 0;
1080
1081 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size);
Milan Broz01482b72008-02-08 02:11:04 +00001082
Milan Broz8f0009a2017-03-16 15:39:44 +01001083 /* Reject unexpected unaligned bio. */
Mikulas Patocka0440d5c2017-11-07 10:35:57 -05001084 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Broz8f0009a2017-03-16 15:39:44 +01001085 return -EIO;
Milan Broz01482b72008-02-08 02:11:04 +00001086
Huang Yingb2174ee2009-03-16 17:44:33 +00001087 dmreq = dmreq_of_req(cc, req);
Mikulas Patockac66029f2012-07-27 15:08:05 +01001088 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001089 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001090 dmreq->iv_sector >>= cc->sector_shift;
Huang Yingb2174ee2009-03-16 17:44:33 +00001091 dmreq->ctx = ctx;
Milan Broz01482b72008-02-08 02:11:04 +00001092
Milan Brozef43aa32017-01-04 20:23:54 +01001093 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
Milan Broz01482b72008-02-08 02:11:04 +00001094
Milan Brozef43aa32017-01-04 20:23:54 +01001095 sector = org_sector_of_dmreq(cc, dmreq);
1096 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1097
1098 iv = iv_of_dmreq(cc, dmreq);
1099 org_iv = org_iv_of_dmreq(cc, dmreq);
1100 tag = tag_from_dmreq(cc, dmreq);
1101 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1102
1103 /* AEAD request:
1104 * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
1105 * | (authenticated) | (auth+encryption) | |
1106 * | sector_LE | IV | sector in/out | tag in/out |
1107 */
1108 sg_init_table(dmreq->sg_in, 4);
1109 sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t));
1110 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001111 sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001112 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size);
1113
1114 sg_init_table(dmreq->sg_out, 4);
1115 sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t));
1116 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001117 sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001118 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size);
Milan Broz01482b72008-02-08 02:11:04 +00001119
Milan Broz3a7f6c92008-02-08 02:11:14 +00001120 if (cc->iv_gen_ops) {
Milan Brozef43aa32017-01-04 20:23:54 +01001121 /* For READs use IV stored in integrity metadata */
1122 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1123 memcpy(org_iv, tag_iv, cc->iv_size);
1124 } else {
1125 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1126 if (r < 0)
1127 return r;
1128 /* Store generated IV in integrity metadata */
1129 if (cc->integrity_iv_size)
1130 memcpy(tag_iv, org_iv, cc->iv_size);
1131 }
1132 /* Working copy of IV, to be modified in crypto API */
1133 memcpy(iv, org_iv, cc->iv_size);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001134 }
1135
Milan Brozef43aa32017-01-04 20:23:54 +01001136 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size);
1137 if (bio_data_dir(ctx->bio_in) == WRITE) {
1138 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001139 cc->sector_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001140 r = crypto_aead_encrypt(req);
1141 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size)
1142 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0,
1143 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size));
1144 } else {
1145 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001146 cc->sector_size + cc->integrity_tag_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001147 r = crypto_aead_decrypt(req);
1148 }
1149
1150 if (r == -EBADMSG)
1151 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1152 (unsigned long long)le64_to_cpu(*sector));
1153
1154 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
1155 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1156
Milan Broz8f0009a2017-03-16 15:39:44 +01001157 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1158 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001159
1160 return r;
1161}
1162
1163static int crypt_convert_block_skcipher(struct crypt_config *cc,
1164 struct convert_context *ctx,
1165 struct skcipher_request *req,
1166 unsigned int tag_offset)
1167{
1168 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1169 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
1170 struct scatterlist *sg_in, *sg_out;
1171 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001172 u8 *iv, *org_iv, *tag_iv;
1173 uint64_t *sector;
1174 int r = 0;
1175
Milan Broz8f0009a2017-03-16 15:39:44 +01001176 /* Reject unexpected unaligned bio. */
Mikulas Patocka0440d5c2017-11-07 10:35:57 -05001177 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Broz8f0009a2017-03-16 15:39:44 +01001178 return -EIO;
1179
Milan Brozef43aa32017-01-04 20:23:54 +01001180 dmreq = dmreq_of_req(cc, req);
1181 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001182 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001183 dmreq->iv_sector >>= cc->sector_shift;
Milan Brozef43aa32017-01-04 20:23:54 +01001184 dmreq->ctx = ctx;
1185
1186 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
1187
1188 iv = iv_of_dmreq(cc, dmreq);
1189 org_iv = org_iv_of_dmreq(cc, dmreq);
1190 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1191
1192 sector = org_sector_of_dmreq(cc, dmreq);
1193 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1194
1195 /* For skcipher we use only the first sg item */
1196 sg_in = &dmreq->sg_in[0];
1197 sg_out = &dmreq->sg_out[0];
1198
1199 sg_init_table(sg_in, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001200 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001201
1202 sg_init_table(sg_out, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001203 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001204
1205 if (cc->iv_gen_ops) {
1206 /* For READs use IV stored in integrity metadata */
1207 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1208 memcpy(org_iv, tag_iv, cc->integrity_iv_size);
1209 } else {
1210 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1211 if (r < 0)
1212 return r;
1213 /* Store generated IV in integrity metadata */
1214 if (cc->integrity_iv_size)
1215 memcpy(tag_iv, org_iv, cc->integrity_iv_size);
1216 }
1217 /* Working copy of IV, to be modified in crypto API */
1218 memcpy(iv, org_iv, cc->iv_size);
1219 }
1220
Milan Broz8f0009a2017-03-16 15:39:44 +01001221 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001222
1223 if (bio_data_dir(ctx->bio_in) == WRITE)
Herbert Xubbdb23b2016-01-24 21:16:36 +08001224 r = crypto_skcipher_encrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001225 else
Herbert Xubbdb23b2016-01-24 21:16:36 +08001226 r = crypto_skcipher_decrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001227
Milan Broz2dc53272011-01-13 19:59:54 +00001228 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001229 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1230
Milan Broz8f0009a2017-03-16 15:39:44 +01001231 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1232 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Broz2dc53272011-01-13 19:59:54 +00001233
Milan Broz3a7f6c92008-02-08 02:11:14 +00001234 return r;
Milan Broz01482b72008-02-08 02:11:04 +00001235}
1236
Milan Broz95497a92008-02-08 02:11:12 +00001237static void kcryptd_async_done(struct crypto_async_request *async_req,
1238 int error);
Andi Kleenc0297722011-01-13 19:59:53 +00001239
Milan Brozef43aa32017-01-04 20:23:54 +01001240static void crypt_alloc_req_skcipher(struct crypt_config *cc,
1241 struct convert_context *ctx)
Milan Brozddd42ed2008-02-08 02:11:07 +00001242{
Mikulas Patockac66029f2012-07-27 15:08:05 +01001243 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +00001244
Milan Brozef43aa32017-01-04 20:23:54 +01001245 if (!ctx->r.req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001246 ctx->r.req = mempool_alloc(&cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +00001247
Milan Brozef43aa32017-01-04 20:23:54 +01001248 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]);
Milan Broz54cea3f2015-05-15 17:00:25 +02001249
1250 /*
1251 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1252 * requests if driver request queue is full.
1253 */
Milan Brozef43aa32017-01-04 20:23:54 +01001254 skcipher_request_set_callback(ctx->r.req,
Mikulas Patocka432061b2018-09-05 09:17:45 -04001255 CRYPTO_TFM_REQ_MAY_BACKLOG,
Milan Brozef43aa32017-01-04 20:23:54 +01001256 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req));
Milan Brozddd42ed2008-02-08 02:11:07 +00001257}
1258
Milan Brozef43aa32017-01-04 20:23:54 +01001259static void crypt_alloc_req_aead(struct crypt_config *cc,
1260 struct convert_context *ctx)
1261{
1262 if (!ctx->r.req_aead)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001263 ctx->r.req_aead = mempool_alloc(&cc->req_pool, GFP_NOIO);
Milan Brozef43aa32017-01-04 20:23:54 +01001264
1265 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]);
1266
1267 /*
1268 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1269 * requests if driver request queue is full.
1270 */
1271 aead_request_set_callback(ctx->r.req_aead,
Mikulas Patocka432061b2018-09-05 09:17:45 -04001272 CRYPTO_TFM_REQ_MAY_BACKLOG,
Milan Brozef43aa32017-01-04 20:23:54 +01001273 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead));
1274}
1275
1276static void crypt_alloc_req(struct crypt_config *cc,
1277 struct convert_context *ctx)
1278{
Milan Broz33d2f092017-03-16 15:39:40 +01001279 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001280 crypt_alloc_req_aead(cc, ctx);
1281 else
1282 crypt_alloc_req_skcipher(cc, ctx);
1283}
1284
1285static void crypt_free_req_skcipher(struct crypt_config *cc,
1286 struct skcipher_request *req, struct bio *base_bio)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001287{
1288 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1289
Herbert Xubbdb23b2016-01-24 21:16:36 +08001290 if ((struct skcipher_request *)(io + 1) != req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001291 mempool_free(req, &cc->req_pool);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001292}
1293
Milan Brozef43aa32017-01-04 20:23:54 +01001294static void crypt_free_req_aead(struct crypt_config *cc,
1295 struct aead_request *req, struct bio *base_bio)
1296{
1297 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1298
1299 if ((struct aead_request *)(io + 1) != req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001300 mempool_free(req, &cc->req_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01001301}
1302
1303static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio)
1304{
Milan Broz33d2f092017-03-16 15:39:40 +01001305 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001306 crypt_free_req_aead(cc, req, base_bio);
1307 else
1308 crypt_free_req_skcipher(cc, req, base_bio);
1309}
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311/*
1312 * Encrypt / decrypt data from one bio to another one (can be the same one)
1313 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001314static blk_status_t crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +01001315 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
Milan Brozef43aa32017-01-04 20:23:54 +01001317 unsigned int tag_offset = 0;
Mikulas Patockaff3af922017-03-23 10:23:14 -04001318 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT;
Milan Broz3f1e9072008-03-28 14:16:07 -07001319 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Mikulas Patocka40b62292012-07-27 15:08:04 +01001321 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +01001322
Kent Overstreet003b5c52013-10-11 15:45:43 -07001323 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Milan Broz3a7f6c92008-02-08 02:11:14 +00001325 crypt_alloc_req(cc, ctx);
Mikulas Patocka40b62292012-07-27 15:08:04 +01001326 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -07001327
Milan Broz33d2f092017-03-16 15:39:40 +01001328 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001329 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset);
1330 else
1331 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001332
1333 switch (r) {
Milan Broz54cea3f2015-05-15 17:00:25 +02001334 /*
1335 * The request was queued by a crypto driver
1336 * but the driver request queue is full, let's wait.
1337 */
Milan Broz3a7f6c92008-02-08 02:11:14 +00001338 case -EBUSY:
1339 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -08001340 reinit_completion(&ctx->restart);
Milan Broz54cea3f2015-05-15 17:00:25 +02001341 /* fall through */
1342 /*
1343 * The request is queued and processed asynchronously,
1344 * completion function kcryptd_async_done() will be called.
1345 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001346 case -EINPROGRESS:
Milan Brozef43aa32017-01-04 20:23:54 +01001347 ctx->r.req = NULL;
Milan Broz8f0009a2017-03-16 15:39:44 +01001348 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001349 tag_offset++;
Milan Broz3a7f6c92008-02-08 02:11:14 +00001350 continue;
Milan Broz54cea3f2015-05-15 17:00:25 +02001351 /*
1352 * The request was already processed (synchronously).
1353 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001354 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001355 atomic_dec(&ctx->cc_pending);
Milan Broz8f0009a2017-03-16 15:39:44 +01001356 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001357 tag_offset++;
Milan Brozc7f1b202008-07-02 09:34:28 +01001358 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -07001359 continue;
Milan Brozef43aa32017-01-04 20:23:54 +01001360 /*
1361 * There was a data integrity error.
1362 */
1363 case -EBADMSG:
1364 atomic_dec(&ctx->cc_pending);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001365 return BLK_STS_PROTECTION;
Milan Brozef43aa32017-01-04 20:23:54 +01001366 /*
1367 * There was an error while processing the request.
1368 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001369 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001370 atomic_dec(&ctx->cc_pending);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001371 return BLK_STS_IOERR;
Milan Broz3f1e9072008-03-28 14:16:07 -07001372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374
Milan Broz3f1e9072008-03-28 14:16:07 -07001375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001378static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380/*
1381 * Generate a new unfragmented bio with the given size
Mike Snitzer586b2862015-09-09 21:34:51 -04001382 * This should never violate the device limitations (but only because
1383 * max_segment_size is being constrained to PAGE_SIZE).
Mikulas Patocka7145c242015-02-13 08:24:41 -05001384 *
1385 * This function may be called concurrently. If we allocate from the mempool
1386 * concurrently, there is a possibility of deadlock. For example, if we have
1387 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1388 * the mempool concurrently, it may deadlock in a situation where both processes
1389 * have allocated 128 pages and the mempool is exhausted.
1390 *
1391 * In order to avoid this scenario we allocate the pages under a mutex.
1392 *
1393 * In order to not degrade performance with excessive locking, we try
1394 * non-blocking allocations without a mutex first but on failure we fallback
1395 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001397static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001399 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001400 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001402 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
1403 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +00001404 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Mikulas Patocka7145c242015-02-13 08:24:41 -05001406retry:
Mel Gormand0164ad2015-11-06 16:28:21 -08001407 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001408 mutex_lock(&cc->bio_alloc_lock);
1409
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001410 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, &cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -07001411 if (!clone)
Milan Brozef43aa32017-01-04 20:23:54 +01001412 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Olaf Kirch027581f2007-05-09 02:32:52 -07001414 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -07001415
Mikulas Patocka7145c242015-02-13 08:24:41 -05001416 remaining_size = size;
1417
Olaf Kirchf97380b2007-05-09 02:32:54 -07001418 for (i = 0; i < nr_iovecs; i++) {
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001419 page = mempool_alloc(&cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -05001420 if (!page) {
1421 crypt_free_buffer_pages(cc, clone);
1422 bio_put(clone);
Mel Gormand0164ad2015-11-06 16:28:21 -08001423 gfp_mask |= __GFP_DIRECT_RECLAIM;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001424 goto retry;
1425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Mikulas Patocka7145c242015-02-13 08:24:41 -05001427 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Ming Lei0dae7fe2016-10-29 16:08:06 +08001429 bio_add_page(clone, page, len, 0);
Milan Broz91e10622007-12-13 14:16:10 +00001430
Mikulas Patocka7145c242015-02-13 08:24:41 -05001431 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433
Milan Brozef43aa32017-01-04 20:23:54 +01001434 /* Allocate space for integrity tags */
1435 if (dm_crypt_integrity_io_alloc(io, clone)) {
1436 crypt_free_buffer_pages(cc, clone);
1437 bio_put(clone);
1438 clone = NULL;
1439 }
1440out:
Mel Gormand0164ad2015-11-06 16:28:21 -08001441 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001442 mutex_unlock(&cc->bio_alloc_lock);
1443
Milan Broz8b004452006-10-03 01:15:37 -07001444 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Neil Brown644bd2f2007-10-16 13:48:46 +02001447static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Neil Brown644bd2f2007-10-16 13:48:46 +02001449 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 struct bio_vec *bv;
1451
Kent Overstreetcb34e052012-09-05 15:22:02 -07001452 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 BUG_ON(!bv->bv_page);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001454 mempool_free(bv->bv_page, &cc->page_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
1456}
1457
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001458static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1459 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001460{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001461 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001462 io->base_bio = bio;
1463 io->sector = sector;
1464 io->error = 0;
Milan Brozef43aa32017-01-04 20:23:54 +01001465 io->ctx.r.req = NULL;
1466 io->integrity_metadata = NULL;
1467 io->integrity_metadata_from_pool = false;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001468 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001469}
1470
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001471static void crypt_inc_pending(struct dm_crypt_io *io)
1472{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001473 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001474}
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476/*
1477 * One of the bios was finished. Check for completion of
1478 * the whole request and correctly clean up the buffer.
1479 */
Milan Broz5742fd72008-02-08 02:10:43 +00001480static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001482 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001483 struct bio *base_bio = io->base_bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001484 blk_status_t error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Mikulas Patocka40b62292012-07-27 15:08:04 +01001486 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 return;
1488
Milan Brozef43aa32017-01-04 20:23:54 +01001489 if (io->ctx.r.req)
1490 crypt_free_req(cc, io->ctx.r.req, base_bio);
1491
1492 if (unlikely(io->integrity_metadata_from_pool))
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001493 mempool_free(io->integrity_metadata, &io->cc->tag_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01001494 else
1495 kfree(io->integrity_metadata);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001496
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001497 base_bio->bi_status = error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001498 bio_endio(base_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
1501/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001502 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 *
1504 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001505 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001506 *
1507 * kcryptd performs the actual encryption or decryption.
1508 *
1509 * kcryptd_io performs the IO submission.
1510 *
1511 * They must be separated as otherwise the final stages could be
1512 * starved by new requests which can block in the first stages due
1513 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001514 *
1515 * The work is done per CPU global for all dm-crypt instances.
1516 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001518static void crypt_endio(struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001519{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001520 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001521 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001522 unsigned rw = bio_data_dir(clone);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001523 blk_status_t error;
Milan Broz8b004452006-10-03 01:15:37 -07001524
1525 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001526 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001527 */
Milan Brozee7a4912008-02-08 02:10:46 +00001528 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001529 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001530
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001531 error = clone->bi_status;
Milan Brozee7a4912008-02-08 02:10:46 +00001532 bio_put(clone);
1533
Sasha Levin9b81c842015-08-10 19:05:18 -04001534 if (rw == READ && !error) {
Milan Brozee7a4912008-02-08 02:10:46 +00001535 kcryptd_queue_crypt(io);
1536 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001537 }
Milan Broz8b004452006-10-03 01:15:37 -07001538
Sasha Levin9b81c842015-08-10 19:05:18 -04001539 if (unlikely(error))
1540 io->error = error;
Milan Broz5742fd72008-02-08 02:10:43 +00001541
1542 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001543}
1544
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001545static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001546{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001547 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001548
1549 clone->bi_private = io;
1550 clone->bi_end_io = crypt_endio;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001551 bio_set_dev(clone, cc->dev->bdev);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001552 clone->bi_opf = io->base_bio->bi_opf;
Milan Broz8b004452006-10-03 01:15:37 -07001553}
1554
Milan Broz20c82532011-01-13 19:59:53 +00001555static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001556{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001557 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001558 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001559
Milan Broz8b004452006-10-03 01:15:37 -07001560 /*
Mike Snitzer59779072015-04-09 16:53:24 -04001561 * We need the original biovec array in order to decrypt
1562 * the whole bio data *afterwards* -- thanks to immutable
1563 * biovecs we don't need to worry about the block layer
1564 * modifying the biovec array; so leverage bio_clone_fast().
Milan Broz8b004452006-10-03 01:15:37 -07001565 */
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001566 clone = bio_clone_fast(io->base_bio, gfp, &cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001567 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001568 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001569
Milan Broz20c82532011-01-13 19:59:53 +00001570 crypt_inc_pending(io);
1571
Milan Broz8b004452006-10-03 01:15:37 -07001572 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001573 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001574
Milan Brozef43aa32017-01-04 20:23:54 +01001575 if (dm_crypt_integrity_io_alloc(io, clone)) {
1576 crypt_dec_pending(io);
1577 bio_put(clone);
1578 return 1;
1579 }
1580
Milan Broz93e605c2006-10-03 01:15:38 -07001581 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001582 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001583}
1584
Mikulas Patockadc267622015-02-13 08:25:59 -05001585static void kcryptd_io_read_work(struct work_struct *work)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001586{
1587 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1588
Mikulas Patockadc267622015-02-13 08:25:59 -05001589 crypt_inc_pending(io);
1590 if (kcryptd_io_read(io, GFP_NOIO))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001591 io->error = BLK_STS_RESOURCE;
Mikulas Patockadc267622015-02-13 08:25:59 -05001592 crypt_dec_pending(io);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001593}
1594
Mikulas Patockadc267622015-02-13 08:25:59 -05001595static void kcryptd_queue_read(struct dm_crypt_io *io)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001596{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001597 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001598
Mikulas Patockadc267622015-02-13 08:25:59 -05001599 INIT_WORK(&io->work, kcryptd_io_read_work);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001600 queue_work(cc->io_queue, &io->work);
1601}
1602
Mikulas Patockadc267622015-02-13 08:25:59 -05001603static void kcryptd_io_write(struct dm_crypt_io *io)
1604{
1605 struct bio *clone = io->ctx.bio_out;
1606
1607 generic_make_request(clone);
1608}
1609
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001610#define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1611
Mikulas Patockadc267622015-02-13 08:25:59 -05001612static int dmcrypt_write(void *data)
1613{
1614 struct crypt_config *cc = data;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001615 struct dm_crypt_io *io;
1616
Mikulas Patockadc267622015-02-13 08:25:59 -05001617 while (1) {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001618 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -05001619 struct blk_plug plug;
1620
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001621 spin_lock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001622continue_locked:
1623
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001624 if (!RB_EMPTY_ROOT(&cc->write_tree))
Mikulas Patockadc267622015-02-13 08:25:59 -05001625 goto pop_from_list;
1626
Rabin Vincentf659b102016-09-21 16:22:29 +02001627 set_current_state(TASK_INTERRUPTIBLE);
Mikulas Patockadc267622015-02-13 08:25:59 -05001628
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001629 spin_unlock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001630
Rabin Vincentf659b102016-09-21 16:22:29 +02001631 if (unlikely(kthread_should_stop())) {
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001632 set_current_state(TASK_RUNNING);
Rabin Vincentf659b102016-09-21 16:22:29 +02001633 break;
1634 }
1635
Mikulas Patockadc267622015-02-13 08:25:59 -05001636 schedule();
1637
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001638 set_current_state(TASK_RUNNING);
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001639 spin_lock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001640 goto continue_locked;
1641
1642pop_from_list:
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001643 write_tree = cc->write_tree;
1644 cc->write_tree = RB_ROOT;
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001645 spin_unlock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001646
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001647 BUG_ON(rb_parent(write_tree.rb_node));
1648
1649 /*
1650 * Note: we cannot walk the tree here with rb_next because
1651 * the structures may be freed when kcryptd_io_write is called.
1652 */
Mikulas Patockadc267622015-02-13 08:25:59 -05001653 blk_start_plug(&plug);
1654 do {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001655 io = crypt_io_from_node(rb_first(&write_tree));
1656 rb_erase(&io->rb_node, &write_tree);
Mikulas Patockadc267622015-02-13 08:25:59 -05001657 kcryptd_io_write(io);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001658 } while (!RB_EMPTY_ROOT(&write_tree));
Mikulas Patockadc267622015-02-13 08:25:59 -05001659 blk_finish_plug(&plug);
1660 }
1661 return 0;
1662}
1663
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001664static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001665{
Milan Brozdec1ced2008-02-08 02:10:57 +00001666 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001667 struct crypt_config *cc = io->cc;
Mikulas Patockadc267622015-02-13 08:25:59 -05001668 unsigned long flags;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001669 sector_t sector;
1670 struct rb_node **rbp, *parent;
Milan Brozdec1ced2008-02-08 02:10:57 +00001671
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001672 if (unlikely(io->error)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001673 crypt_free_buffer_pages(cc, clone);
1674 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001675 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001676 return;
1677 }
1678
1679 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001680 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001681
Kent Overstreet4f024f32013-10-11 15:44:27 -07001682 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001683
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001684 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1685 generic_make_request(clone);
1686 return;
1687 }
1688
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001689 spin_lock_irqsave(&cc->write_thread_lock, flags);
1690 if (RB_EMPTY_ROOT(&cc->write_tree))
1691 wake_up_process(cc->write_thread);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001692 rbp = &cc->write_tree.rb_node;
1693 parent = NULL;
1694 sector = io->sector;
1695 while (*rbp) {
1696 parent = *rbp;
1697 if (sector < crypt_io_from_node(parent)->sector)
1698 rbp = &(*rbp)->rb_left;
1699 else
1700 rbp = &(*rbp)->rb_right;
1701 }
1702 rb_link_node(&io->rb_node, parent, rbp);
1703 rb_insert_color(&io->rb_node, &cc->write_tree);
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001704 spin_unlock_irqrestore(&cc->write_thread_lock, flags);
Milan Broz4e4eef62008-02-08 02:10:49 +00001705}
1706
Milan Brozfc5a5e92008-10-10 13:37:04 +01001707static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001708{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001709 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001710 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001711 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001712 sector_t sector = io->sector;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001713 blk_status_t r;
Milan Broz8b004452006-10-03 01:15:37 -07001714
Milan Broz93e605c2006-10-03 01:15:38 -07001715 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001716 * Prevent io from disappearing until this function completes.
1717 */
1718 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001719 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001720
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001721 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1722 if (unlikely(!clone)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001723 io->error = BLK_STS_IOERR;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001724 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001725 }
Milan Broz899c95d2008-02-08 02:11:02 +00001726
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001727 io->ctx.bio_out = clone;
1728 io->ctx.iter_out = clone->bi_iter;
1729
1730 sector += bio_sectors(clone);
1731
1732 crypt_inc_pending(io);
1733 r = crypt_convert(cc, &io->ctx);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001734 if (r)
Milan Brozef43aa32017-01-04 20:23:54 +01001735 io->error = r;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001736 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1737
1738 /* Encryption was already finished, submit io now */
1739 if (crypt_finished) {
1740 kcryptd_crypt_write_io_submit(io, 0);
1741 io->sector = sector;
1742 }
1743
1744dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001745 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001746}
1747
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001748static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001749{
Milan Broz5742fd72008-02-08 02:10:43 +00001750 crypt_dec_pending(io);
1751}
1752
Milan Broz4e4eef62008-02-08 02:10:49 +00001753static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001754{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001755 struct crypt_config *cc = io->cc;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001756 blk_status_t r;
Milan Broz8b004452006-10-03 01:15:37 -07001757
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001758 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001759
Milan Broz53017032008-02-08 02:10:38 +00001760 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001761 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001762
Milan Broz5742fd72008-02-08 02:10:43 +00001763 r = crypt_convert(cc, &io->ctx);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001764 if (r)
Milan Brozef43aa32017-01-04 20:23:54 +01001765 io->error = r;
Milan Broz5742fd72008-02-08 02:10:43 +00001766
Mikulas Patocka40b62292012-07-27 15:08:04 +01001767 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001768 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001769
1770 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001771}
1772
Milan Broz95497a92008-02-08 02:11:12 +00001773static void kcryptd_async_done(struct crypto_async_request *async_req,
1774 int error)
1775{
Huang Yingb2174ee2009-03-16 17:44:33 +00001776 struct dm_crypt_request *dmreq = async_req->data;
1777 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001778 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001779 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001780
Milan Broz54cea3f2015-05-15 17:00:25 +02001781 /*
1782 * A request from crypto driver backlog is going to be processed now,
1783 * finish the completion and continue in crypt_convert().
1784 * (Callback will be called for the second time for this request.)
1785 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001786 if (error == -EINPROGRESS) {
1787 complete(&ctx->restart);
Milan Broz95497a92008-02-08 02:11:12 +00001788 return;
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001789 }
Milan Broz95497a92008-02-08 02:11:12 +00001790
Milan Broz2dc53272011-01-13 19:59:54 +00001791 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001792 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq);
Milan Broz2dc53272011-01-13 19:59:54 +00001793
Milan Brozef43aa32017-01-04 20:23:54 +01001794 if (error == -EBADMSG) {
1795 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1796 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001797 io->error = BLK_STS_PROTECTION;
Milan Brozef43aa32017-01-04 20:23:54 +01001798 } else if (error < 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001799 io->error = BLK_STS_IOERR;
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001800
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001801 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00001802
Mikulas Patocka40b62292012-07-27 15:08:04 +01001803 if (!atomic_dec_and_test(&ctx->cc_pending))
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001804 return;
Milan Broz95497a92008-02-08 02:11:12 +00001805
1806 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001807 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001808 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001809 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001810}
1811
Milan Broz4e4eef62008-02-08 02:10:49 +00001812static void kcryptd_crypt(struct work_struct *work)
1813{
1814 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1815
1816 if (bio_data_dir(io->base_bio) == READ)
1817 kcryptd_crypt_read_convert(io);
1818 else
1819 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001820}
1821
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001822static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1823{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001824 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001825
1826 INIT_WORK(&io->work, kcryptd_crypt);
1827 queue_work(cc->crypt_queue, &io->work);
1828}
1829
Milan Brozef43aa32017-01-04 20:23:54 +01001830static void crypt_free_tfms_aead(struct crypt_config *cc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Milan Brozef43aa32017-01-04 20:23:54 +01001832 if (!cc->cipher_tfm.tfms_aead)
1833 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Milan Brozef43aa32017-01-04 20:23:54 +01001835 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1836 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]);
1837 cc->cipher_tfm.tfms_aead[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 }
1839
Milan Brozef43aa32017-01-04 20:23:54 +01001840 kfree(cc->cipher_tfm.tfms_aead);
1841 cc->cipher_tfm.tfms_aead = NULL;
1842}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Milan Brozef43aa32017-01-04 20:23:54 +01001844static void crypt_free_tfms_skcipher(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001845{
Milan Brozd1f96422011-01-13 19:59:54 +00001846 unsigned i;
1847
Milan Brozef43aa32017-01-04 20:23:54 +01001848 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001849 return;
1850
Milan Brozd1f96422011-01-13 19:59:54 +00001851 for (i = 0; i < cc->tfms_count; i++)
Milan Brozef43aa32017-01-04 20:23:54 +01001852 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) {
1853 crypto_free_skcipher(cc->cipher_tfm.tfms[i]);
1854 cc->cipher_tfm.tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001855 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001856
Milan Brozef43aa32017-01-04 20:23:54 +01001857 kfree(cc->cipher_tfm.tfms);
1858 cc->cipher_tfm.tfms = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
1861static void crypt_free_tfms(struct crypt_config *cc)
1862{
Milan Broz33d2f092017-03-16 15:39:40 +01001863 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001864 crypt_free_tfms_aead(cc);
1865 else
1866 crypt_free_tfms_skcipher(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001867}
1868
Milan Brozef43aa32017-01-04 20:23:54 +01001869static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001870{
Milan Brozd1f96422011-01-13 19:59:54 +00001871 unsigned i;
1872 int err;
1873
Kees Cook6396bb22018-06-12 14:03:40 -07001874 cc->cipher_tfm.tfms = kcalloc(cc->tfms_count,
1875 sizeof(struct crypto_skcipher *),
1876 GFP_KERNEL);
Milan Brozef43aa32017-01-04 20:23:54 +01001877 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001878 return -ENOMEM;
1879
Milan Brozd1f96422011-01-13 19:59:54 +00001880 for (i = 0; i < cc->tfms_count; i++) {
Milan Brozef43aa32017-01-04 20:23:54 +01001881 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
1882 if (IS_ERR(cc->cipher_tfm.tfms[i])) {
1883 err = PTR_ERR(cc->cipher_tfm.tfms[i]);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001884 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001885 return err;
1886 }
1887 }
1888
1889 return 0;
1890}
1891
Milan Brozef43aa32017-01-04 20:23:54 +01001892static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
1893{
Milan Brozef43aa32017-01-04 20:23:54 +01001894 int err;
1895
1896 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
1897 if (!cc->cipher_tfm.tfms)
1898 return -ENOMEM;
1899
Milan Brozef43aa32017-01-04 20:23:54 +01001900 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
1901 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1902 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
1903 crypt_free_tfms(cc);
1904 return err;
1905 }
1906
Milan Brozef43aa32017-01-04 20:23:54 +01001907 return 0;
1908}
1909
1910static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
1911{
Milan Broz33d2f092017-03-16 15:39:40 +01001912 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001913 return crypt_alloc_tfms_aead(cc, ciphermode);
1914 else
1915 return crypt_alloc_tfms_skcipher(cc, ciphermode);
1916}
1917
1918static unsigned crypt_subkey_size(struct crypt_config *cc)
1919{
1920 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1921}
1922
1923static unsigned crypt_authenckey_size(struct crypt_config *cc)
1924{
1925 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
1926}
1927
1928/*
1929 * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
1930 * the key must be for some reason in special format.
1931 * This funcion converts cc->key to this special format.
1932 */
1933static void crypt_copy_authenckey(char *p, const void *key,
1934 unsigned enckeylen, unsigned authkeylen)
1935{
1936 struct crypto_authenc_key_param *param;
1937 struct rtattr *rta;
1938
1939 rta = (struct rtattr *)p;
1940 param = RTA_DATA(rta);
1941 param->enckeylen = cpu_to_be32(enckeylen);
1942 rta->rta_len = RTA_LENGTH(sizeof(*param));
1943 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
1944 p += RTA_SPACE(sizeof(*param));
1945 memcpy(p, key + enckeylen, authkeylen);
1946 p += authkeylen;
1947 memcpy(p, key, enckeylen);
1948}
1949
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04001950static int crypt_setkey(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +00001951{
Milan Brozda31a072013-10-28 23:21:03 +01001952 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001953 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001954
Milan Brozda31a072013-10-28 23:21:03 +01001955 /* Ignore extra keys (which are used for IV etc) */
Milan Brozef43aa32017-01-04 20:23:54 +01001956 subkey_size = crypt_subkey_size(cc);
Milan Brozda31a072013-10-28 23:21:03 +01001957
Milan Broz27c70032018-01-03 22:48:59 +01001958 if (crypt_integrity_hmac(cc)) {
1959 if (subkey_size < cc->key_mac_size)
1960 return -EINVAL;
1961
Milan Brozef43aa32017-01-04 20:23:54 +01001962 crypt_copy_authenckey(cc->authenc_key, cc->key,
1963 subkey_size - cc->key_mac_size,
1964 cc->key_mac_size);
Milan Broz27c70032018-01-03 22:48:59 +01001965 }
1966
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001967 for (i = 0; i < cc->tfms_count; i++) {
Milan Broz33d2f092017-03-16 15:39:40 +01001968 if (crypt_integrity_hmac(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001969 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1970 cc->authenc_key, crypt_authenckey_size(cc));
Milan Broz33d2f092017-03-16 15:39:40 +01001971 else if (crypt_integrity_aead(cc))
1972 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1973 cc->key + (i * subkey_size),
1974 subkey_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001975 else
1976 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i],
1977 cc->key + (i * subkey_size),
1978 subkey_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001979 if (r)
1980 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001981 }
1982
Milan Brozef43aa32017-01-04 20:23:54 +01001983 if (crypt_integrity_hmac(cc))
1984 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc));
1985
Andi Kleenc0297722011-01-13 19:59:53 +00001986 return err;
1987}
1988
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001989#ifdef CONFIG_KEYS
1990
Ondrej Kozina027c4312016-12-01 18:20:52 +01001991static bool contains_whitespace(const char *str)
1992{
1993 while (*str)
1994 if (isspace(*str++))
1995 return true;
1996 return false;
1997}
1998
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001999static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2000{
2001 char *new_key_string, *key_desc;
2002 int ret;
2003 struct key *key;
2004 const struct user_key_payload *ukp;
2005
Ondrej Kozina027c4312016-12-01 18:20:52 +01002006 /*
2007 * Reject key_string with whitespace. dm core currently lacks code for
2008 * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
2009 */
2010 if (contains_whitespace(key_string)) {
2011 DMERR("whitespace chars not allowed in key string");
2012 return -EINVAL;
2013 }
2014
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002015 /* look for next ':' separating key_type from key_description */
2016 key_desc = strpbrk(key_string, ":");
2017 if (!key_desc || key_desc == key_string || !strlen(key_desc + 1))
2018 return -EINVAL;
2019
2020 if (strncmp(key_string, "logon:", key_desc - key_string + 1) &&
2021 strncmp(key_string, "user:", key_desc - key_string + 1))
2022 return -EINVAL;
2023
2024 new_key_string = kstrdup(key_string, GFP_KERNEL);
2025 if (!new_key_string)
2026 return -ENOMEM;
2027
2028 key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user,
2029 key_desc + 1, NULL);
2030 if (IS_ERR(key)) {
2031 kzfree(new_key_string);
2032 return PTR_ERR(key);
2033 }
2034
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002035 down_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002036
David Howells0837e492017-03-01 15:11:23 +00002037 ukp = user_key_payload_locked(key);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002038 if (!ukp) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002039 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002040 key_put(key);
2041 kzfree(new_key_string);
2042 return -EKEYREVOKED;
2043 }
2044
2045 if (cc->key_size != ukp->datalen) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002046 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002047 key_put(key);
2048 kzfree(new_key_string);
2049 return -EINVAL;
2050 }
2051
2052 memcpy(cc->key, ukp->data, cc->key_size);
2053
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002054 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002055 key_put(key);
2056
2057 /* clear the flag since following operations may invalidate previously valid key */
2058 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2059
2060 ret = crypt_setkey(cc);
2061
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002062 if (!ret) {
2063 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2064 kzfree(cc->key_string);
2065 cc->key_string = new_key_string;
2066 } else
2067 kzfree(new_key_string);
2068
2069 return ret;
2070}
2071
2072static int get_key_size(char **key_string)
2073{
2074 char *colon, dummy;
2075 int ret;
2076
2077 if (*key_string[0] != ':')
2078 return strlen(*key_string) >> 1;
2079
2080 /* look for next ':' in key string */
2081 colon = strpbrk(*key_string + 1, ":");
2082 if (!colon)
2083 return -EINVAL;
2084
2085 if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':')
2086 return -EINVAL;
2087
2088 *key_string = colon;
2089
2090 /* remaining key string should be :<logon|user>:<key_desc> */
2091
2092 return ret;
2093}
2094
2095#else
2096
2097static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2098{
2099 return -EINVAL;
2100}
2101
2102static int get_key_size(char **key_string)
2103{
2104 return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1;
2105}
2106
2107#endif
2108
Milan Broze48d4bb2006-10-03 01:15:37 -07002109static int crypt_set_key(struct crypt_config *cc, char *key)
2110{
Milan Brozde8be5a2011-03-24 13:54:27 +00002111 int r = -EINVAL;
2112 int key_string_len = strlen(key);
2113
Milan Broz69a8cfc2011-01-13 19:59:49 +00002114 /* Hyphen (which gives a key_size of zero) means there is no key. */
2115 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00002116 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002117
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002118 /* ':' means the key is in kernel keyring, short-circuit normal key processing */
2119 if (key[0] == ':') {
2120 r = crypt_set_keyring_key(cc, key + 1);
2121 goto out;
2122 }
2123
Ondrej Kozina265e9092016-11-02 15:02:08 +01002124 /* clear the flag since following operations may invalidate previously valid key */
2125 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2126
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002127 /* wipe references to any kernel keyring key */
2128 kzfree(cc->key_string);
2129 cc->key_string = NULL;
2130
Andy Shevchenkoe944e032017-04-27 16:52:04 +03002131 /* Decode key from its hex representation. */
2132 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00002133 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002134
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002135 r = crypt_setkey(cc);
Ondrej Kozina265e9092016-11-02 15:02:08 +01002136 if (!r)
2137 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Milan Brozde8be5a2011-03-24 13:54:27 +00002138
2139out:
2140 /* Hex key string not needed after here, so wipe it. */
2141 memset(key, '0', key_string_len);
2142
2143 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002144}
2145
2146static int crypt_wipe_key(struct crypt_config *cc)
2147{
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002148 int r;
2149
Milan Broze48d4bb2006-10-03 01:15:37 -07002150 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002151 get_random_bytes(&cc->key, cc->key_size);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002152 kzfree(cc->key_string);
2153 cc->key_string = NULL;
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002154 r = crypt_setkey(cc);
2155 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00002156
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002157 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002158}
2159
Mikulas Patocka50593532017-08-13 22:45:08 -04002160static void crypt_calculate_pages_per_client(void)
2161{
2162 unsigned long pages = (totalram_pages - totalhigh_pages) * DM_CRYPT_MEMORY_PERCENT / 100;
2163
2164 if (!dm_crypt_clients_n)
2165 return;
2166
2167 pages /= dm_crypt_clients_n;
2168 if (pages < DM_CRYPT_MIN_PAGES_PER_CLIENT)
2169 pages = DM_CRYPT_MIN_PAGES_PER_CLIENT;
2170 dm_crypt_pages_per_client = pages;
2171}
2172
2173static void *crypt_page_alloc(gfp_t gfp_mask, void *pool_data)
2174{
2175 struct crypt_config *cc = pool_data;
2176 struct page *page;
2177
2178 if (unlikely(percpu_counter_compare(&cc->n_allocated_pages, dm_crypt_pages_per_client) >= 0) &&
2179 likely(gfp_mask & __GFP_NORETRY))
2180 return NULL;
2181
2182 page = alloc_page(gfp_mask);
2183 if (likely(page != NULL))
2184 percpu_counter_add(&cc->n_allocated_pages, 1);
2185
2186 return page;
2187}
2188
2189static void crypt_page_free(void *page, void *pool_data)
2190{
2191 struct crypt_config *cc = pool_data;
2192
2193 __free_page(page);
2194 percpu_counter_sub(&cc->n_allocated_pages, 1);
2195}
2196
Milan Broz28513fc2010-08-12 04:14:06 +01002197static void crypt_dtr(struct dm_target *ti)
2198{
2199 struct crypt_config *cc = ti->private;
2200
2201 ti->private = NULL;
2202
2203 if (!cc)
2204 return;
2205
Rabin Vincentf659b102016-09-21 16:22:29 +02002206 if (cc->write_thread)
Mikulas Patockadc267622015-02-13 08:25:59 -05002207 kthread_stop(cc->write_thread);
2208
Milan Broz28513fc2010-08-12 04:14:06 +01002209 if (cc->io_queue)
2210 destroy_workqueue(cc->io_queue);
2211 if (cc->crypt_queue)
2212 destroy_workqueue(cc->crypt_queue);
2213
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002214 crypt_free_tfms(cc);
2215
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002216 bioset_exit(&cc->bs);
Milan Broz28513fc2010-08-12 04:14:06 +01002217
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002218 mempool_exit(&cc->page_pool);
2219 mempool_exit(&cc->req_pool);
2220 mempool_exit(&cc->tag_pool);
2221
Kent Overstreetd00a11d2018-06-02 13:45:04 -04002222 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0);
2223 percpu_counter_destroy(&cc->n_allocated_pages);
2224
Milan Broz28513fc2010-08-12 04:14:06 +01002225 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
2226 cc->iv_gen_ops->dtr(cc);
2227
Milan Broz28513fc2010-08-12 04:14:06 +01002228 if (cc->dev)
2229 dm_put_device(ti, cc->dev);
2230
Milan Broz5ebaee62010-08-12 04:14:07 +01002231 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00002232 kzfree(cc->cipher_string);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002233 kzfree(cc->key_string);
Milan Brozef43aa32017-01-04 20:23:54 +01002234 kzfree(cc->cipher_auth);
2235 kzfree(cc->authenc_key);
Milan Broz28513fc2010-08-12 04:14:06 +01002236
Mike Snitzerd5ffebd2018-01-05 21:17:20 -05002237 mutex_destroy(&cc->bio_alloc_lock);
2238
Milan Broz28513fc2010-08-12 04:14:06 +01002239 /* Must zero key material before freeing */
2240 kzfree(cc);
Mikulas Patocka50593532017-08-13 22:45:08 -04002241
2242 spin_lock(&dm_crypt_clients_lock);
2243 WARN_ON(!dm_crypt_clients_n);
2244 dm_crypt_clients_n--;
2245 crypt_calculate_pages_per_client();
2246 spin_unlock(&dm_crypt_clients_lock);
Milan Broz28513fc2010-08-12 04:14:06 +01002247}
2248
Milan Broze889f972017-03-16 15:39:39 +01002249static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
Milan Broz5ebaee62010-08-12 04:14:07 +01002251 struct crypt_config *cc = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Milan Broz33d2f092017-03-16 15:39:40 +01002253 if (crypt_integrity_aead(cc))
Milan Broze889f972017-03-16 15:39:39 +01002254 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2255 else
2256 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Milan Broz5ebaee62010-08-12 04:14:07 +01002258 if (cc->iv_size)
2259 /* at least a 64 bit sector number should fit in our buffer */
2260 cc->iv_size = max(cc->iv_size,
2261 (unsigned int)(sizeof(u64) / sizeof(u8)));
2262 else if (ivmode) {
2263 DMWARN("Selected cipher does not support IVs");
2264 ivmode = NULL;
2265 }
2266
2267 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (ivmode == NULL)
2269 cc->iv_gen_ops = NULL;
2270 else if (strcmp(ivmode, "plain") == 0)
2271 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00002272 else if (strcmp(ivmode, "plain64") == 0)
2273 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Milan Broz7e3fd852017-06-06 09:07:01 +02002274 else if (strcmp(ivmode, "plain64be") == 0)
2275 cc->iv_gen_ops = &crypt_iv_plain64be_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 else if (strcmp(ivmode, "essiv") == 0)
2277 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10002278 else if (strcmp(ivmode, "benbi") == 0)
2279 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07002280 else if (strcmp(ivmode, "null") == 0)
2281 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00002282 else if (strcmp(ivmode, "lmk") == 0) {
2283 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01002284 /*
2285 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00002286 * to length of provided multi-key string.
2287 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01002288 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00002289 */
Milan Brozda31a072013-10-28 23:21:03 +01002290 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00002291 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01002292 cc->key_extra_size = cc->key_size / cc->key_parts;
2293 }
Milan Brozed04d982013-10-28 23:21:04 +01002294 } else if (strcmp(ivmode, "tcw") == 0) {
2295 cc->iv_gen_ops = &crypt_iv_tcw_ops;
2296 cc->key_parts += 2; /* IV + whitening */
2297 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broze889f972017-03-16 15:39:39 +01002298 } else if (strcmp(ivmode, "random") == 0) {
2299 cc->iv_gen_ops = &crypt_iv_random_ops;
2300 /* Need storage space in integrity fields. */
2301 cc->integrity_iv_size = cc->iv_size;
Milan Broz34745782011-01-13 19:59:55 +00002302 } else {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002303 ti->error = "Invalid IV mode";
Milan Broze889f972017-03-16 15:39:39 +01002304 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 }
2306
Milan Broze889f972017-03-16 15:39:39 +01002307 return 0;
2308}
2309
Milan Broz33d2f092017-03-16 15:39:40 +01002310/*
2311 * Workaround to parse cipher algorithm from crypto API spec.
2312 * The cc->cipher is currently used only in ESSIV.
2313 * This should be probably done by crypto-api calls (once available...)
2314 */
2315static int crypt_ctr_blkdev_cipher(struct crypt_config *cc)
2316{
2317 const char *alg_name = NULL;
2318 char *start, *end;
2319
2320 if (crypt_integrity_aead(cc)) {
2321 alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc)));
2322 if (!alg_name)
2323 return -EINVAL;
2324 if (crypt_integrity_hmac(cc)) {
2325 alg_name = strchr(alg_name, ',');
2326 if (!alg_name)
2327 return -EINVAL;
2328 }
2329 alg_name++;
2330 } else {
2331 alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc)));
2332 if (!alg_name)
2333 return -EINVAL;
2334 }
2335
2336 start = strchr(alg_name, '(');
2337 end = strchr(alg_name, ')');
2338
2339 if (!start && !end) {
2340 cc->cipher = kstrdup(alg_name, GFP_KERNEL);
2341 return cc->cipher ? 0 : -ENOMEM;
2342 }
2343
2344 if (!start || !end || ++start >= end)
2345 return -EINVAL;
2346
2347 cc->cipher = kzalloc(end - start + 1, GFP_KERNEL);
2348 if (!cc->cipher)
2349 return -ENOMEM;
2350
2351 strncpy(cc->cipher, start, end - start);
2352
2353 return 0;
2354}
2355
2356/*
2357 * Workaround to parse HMAC algorithm from AEAD crypto API spec.
2358 * The HMAC is needed to calculate tag size (HMAC digest size).
2359 * This should be probably done by crypto-api calls (once available...)
2360 */
2361static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
2362{
2363 char *start, *end, *mac_alg = NULL;
2364 struct crypto_ahash *mac;
2365
2366 if (!strstarts(cipher_api, "authenc("))
2367 return 0;
2368
2369 start = strchr(cipher_api, '(');
2370 end = strchr(cipher_api, ',');
2371 if (!start || !end || ++start > end)
2372 return -EINVAL;
2373
2374 mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
2375 if (!mac_alg)
2376 return -ENOMEM;
2377 strncpy(mac_alg, start, end - start);
2378
2379 mac = crypto_alloc_ahash(mac_alg, 0, 0);
2380 kfree(mac_alg);
2381
2382 if (IS_ERR(mac))
2383 return PTR_ERR(mac);
2384
2385 cc->key_mac_size = crypto_ahash_digestsize(mac);
2386 crypto_free_ahash(mac);
2387
2388 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
2389 if (!cc->authenc_key)
2390 return -ENOMEM;
2391
2392 return 0;
2393}
2394
2395static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key,
2396 char **ivmode, char **ivopts)
Milan Broz5ebaee62010-08-12 04:14:07 +01002397{
2398 struct crypt_config *cc = ti->private;
Milan Broz33d2f092017-03-16 15:39:40 +01002399 char *tmp, *cipher_api;
2400 int ret = -EINVAL;
2401
2402 cc->tfms_count = 1;
2403
2404 /*
2405 * New format (capi: prefix)
2406 * capi:cipher_api_spec-iv:ivopts
2407 */
2408 tmp = &cipher_in[strlen("capi:")];
Milan Brozb911f1d2019-01-09 11:57:14 +01002409
2410 /* Separate IV options if present, it can contain another '-' in hash name */
2411 *ivopts = strrchr(tmp, ':');
2412 if (*ivopts) {
2413 **ivopts = '\0';
2414 (*ivopts)++;
2415 }
2416 /* Parse IV mode */
2417 *ivmode = strrchr(tmp, '-');
2418 if (*ivmode) {
2419 **ivmode = '\0';
2420 (*ivmode)++;
2421 }
2422 /* The rest is crypto API spec */
2423 cipher_api = tmp;
Milan Broz33d2f092017-03-16 15:39:40 +01002424
2425 if (*ivmode && !strcmp(*ivmode, "lmk"))
2426 cc->tfms_count = 64;
2427
2428 cc->key_parts = cc->tfms_count;
2429
2430 /* Allocate cipher */
2431 ret = crypt_alloc_tfms(cc, cipher_api);
2432 if (ret < 0) {
2433 ti->error = "Error allocating crypto tfm";
2434 return ret;
2435 }
2436
2437 /* Alloc AEAD, can be used only in new format. */
2438 if (crypt_integrity_aead(cc)) {
2439 ret = crypt_ctr_auth_cipher(cc, cipher_api);
2440 if (ret < 0) {
2441 ti->error = "Invalid AEAD cipher spec";
2442 return -ENOMEM;
2443 }
2444 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2445 } else
2446 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2447
2448 ret = crypt_ctr_blkdev_cipher(cc);
2449 if (ret < 0) {
2450 ti->error = "Cannot allocate cipher string";
2451 return -ENOMEM;
2452 }
2453
2454 return 0;
2455}
2456
2457static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
2458 char **ivmode, char **ivopts)
2459{
2460 struct crypt_config *cc = ti->private;
2461 char *tmp, *cipher, *chainmode, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01002462 char *cipher_api = NULL;
2463 int ret = -EINVAL;
2464 char dummy;
2465
Milan Broz33d2f092017-03-16 15:39:40 +01002466 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002467 ti->error = "Bad cipher specification";
2468 return -EINVAL;
2469 }
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 /*
Milan Broz5ebaee62010-08-12 04:14:07 +01002472 * Legacy dm-crypt cipher specification
2473 * cipher[:keycount]-mode-iv:ivopts
2474 */
2475 tmp = cipher_in;
2476 keycount = strsep(&tmp, "-");
2477 cipher = strsep(&keycount, ":");
2478
Milan Broz69a8cfc2011-01-13 19:59:49 +00002479 if (!keycount)
Milan Broz5ebaee62010-08-12 04:14:07 +01002480 cc->tfms_count = 1;
2481 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
2482 !is_power_of_2(cc->tfms_count)) {
2483 ti->error = "Bad cipher key count specification";
2484 return -EINVAL;
2485 }
Milan Broz28513fc2010-08-12 04:14:06 +01002486 cc->key_parts = cc->tfms_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002488 cc->cipher = kstrdup(cipher, GFP_KERNEL);
Milan Broz28513fc2010-08-12 04:14:06 +01002489 if (!cc->cipher)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 goto bad_mem;
2491
Milan Brozddd42ed2008-02-08 02:11:07 +00002492 chainmode = strsep(&tmp, "-");
Milan Brozb911f1d2019-01-09 11:57:14 +01002493 *ivmode = strsep(&tmp, ":");
2494 *ivopts = tmp;
Milan Brozddd42ed2008-02-08 02:11:07 +00002495
2496 /*
2497 * For compatibility with the original dm-crypt mapping format, if
2498 * only the cipher name is supplied, use cbc-plain.
Milan Broz28513fc2010-08-12 04:14:06 +01002499 */
Milan Broz33d2f092017-03-16 15:39:40 +01002500 if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002501 chainmode = "cbc";
Milan Broz33d2f092017-03-16 15:39:40 +01002502 *ivmode = "plain";
Milan Brozcabf08e2007-10-19 22:38:58 +01002503 }
2504
Milan Broz33d2f092017-03-16 15:39:40 +01002505 if (strcmp(chainmode, "ecb") && !*ivmode) {
Andi Kleenc0297722011-01-13 19:59:53 +00002506 ti->error = "IV mechanism required";
2507 return -EINVAL;
2508 }
2509
Milan Brozcabf08e2007-10-19 22:38:58 +01002510 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
Milan Broz9934a8b2007-10-19 22:38:57 +01002511 if (!cipher_api)
Milan Broz28513fc2010-08-12 04:14:06 +01002512 goto bad_mem;
Milan Broz9934a8b2007-10-19 22:38:57 +01002513
2514 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002515 "%s(%s)", chainmode, cipher);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (ret < 0) {
2517 kfree(cipher_api);
Milan Broz28513fc2010-08-12 04:14:06 +01002518 goto bad_mem;
2519 }
2520
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 /* Allocate cipher */
2522 ret = crypt_alloc_tfms(cc, cipher_api);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (ret < 0) {
2524 ti->error = "Error allocating crypto tfm";
Milan Broz33d2f092017-03-16 15:39:40 +01002525 kfree(cipher_api);
2526 return ret;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002527 }
Jeffy Chenbd86e322017-09-27 20:28:57 +08002528 kfree(cipher_api);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002529
Milan Broz33d2f092017-03-16 15:39:40 +01002530 return 0;
2531bad_mem:
2532 ti->error = "Cannot allocate cipher strings";
2533 return -ENOMEM;
2534}
2535
2536static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key)
2537{
2538 struct crypt_config *cc = ti->private;
2539 char *ivmode = NULL, *ivopts = NULL;
2540 int ret;
2541
2542 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
2543 if (!cc->cipher_string) {
2544 ti->error = "Cannot allocate cipher strings";
2545 return -ENOMEM;
2546 }
2547
2548 if (strstarts(cipher_in, "capi:"))
2549 ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts);
2550 else
2551 ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts);
2552 if (ret)
2553 return ret;
2554
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002555 /* Initialize IV */
Milan Broze889f972017-03-16 15:39:39 +01002556 ret = crypt_ctr_ivmode(ti, ivmode);
2557 if (ret < 0)
Milan Broz33d2f092017-03-16 15:39:40 +01002558 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Milan Brozda31a072013-10-28 23:21:03 +01002560 /* Initialize and set key */
2561 ret = crypt_set_key(cc, key);
2562 if (ret < 0) {
2563 ti->error = "Error decoding and setting key";
Milan Broz33d2f092017-03-16 15:39:40 +01002564 return ret;
Milan Brozda31a072013-10-28 23:21:03 +01002565 }
2566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 /* Allocate IV */
2568 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
2569 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
2570 if (ret < 0) {
2571 ti->error = "Error creating IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002572 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 }
2574 }
2575
2576 /* Initialize IV (set keys for ESSIV etc) */
2577 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
2578 ret = cc->iv_gen_ops->init(cc);
2579 if (ret < 0) {
2580 ti->error = "Error initialising IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002581 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 }
2583 }
2584
Ondrej Kozinadc949022018-01-12 16:30:32 +01002585 /* wipe the kernel key payload copy */
2586 if (cc->key_string)
2587 memset(cc->key, 0, cc->key_size * sizeof(u8));
2588
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Milan Brozef43aa32017-01-04 20:23:54 +01002592static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
2593{
2594 struct crypt_config *cc = ti->private;
2595 struct dm_arg_set as;
Eric Biggers5916a222017-06-22 11:32:45 -07002596 static const struct dm_arg _args[] = {
Milan Broz8f0009a2017-03-16 15:39:44 +01002597 {0, 6, "Invalid number of feature args"},
Milan Brozef43aa32017-01-04 20:23:54 +01002598 };
2599 unsigned int opt_params, val;
2600 const char *opt_string, *sval;
Milan Broz8f0009a2017-03-16 15:39:44 +01002601 char dummy;
Milan Brozef43aa32017-01-04 20:23:54 +01002602 int ret;
2603
2604 /* Optional parameters */
2605 as.argc = argc;
2606 as.argv = argv;
2607
2608 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2609 if (ret)
2610 return ret;
2611
2612 while (opt_params--) {
2613 opt_string = dm_shift_arg(&as);
2614 if (!opt_string) {
2615 ti->error = "Not enough feature arguments";
2616 return -EINVAL;
2617 }
2618
2619 if (!strcasecmp(opt_string, "allow_discards"))
2620 ti->num_discard_bios = 1;
2621
2622 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
2623 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
2624
2625 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
2626 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
2627 else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
2628 if (val == 0 || val > MAX_TAG_SIZE) {
2629 ti->error = "Invalid integrity arguments";
2630 return -EINVAL;
2631 }
2632 cc->on_disk_tag_size = val;
2633 sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
2634 if (!strcasecmp(sval, "aead")) {
2635 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002636 } else if (strcasecmp(sval, "none")) {
2637 ti->error = "Unknown integrity profile";
2638 return -EINVAL;
2639 }
2640
2641 cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
2642 if (!cc->cipher_auth)
2643 return -ENOMEM;
Mikulas Patockaff3af922017-03-23 10:23:14 -04002644 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002645 if (cc->sector_size < (1 << SECTOR_SHIFT) ||
2646 cc->sector_size > 4096 ||
Mikulas Patockaff3af922017-03-23 10:23:14 -04002647 (cc->sector_size & (cc->sector_size - 1))) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002648 ti->error = "Invalid feature value for sector_size";
2649 return -EINVAL;
2650 }
Milan Broz783874b2017-09-13 15:45:56 +02002651 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) {
2652 ti->error = "Device size is not multiple of sector_size feature";
2653 return -EINVAL;
2654 }
Mikulas Patockaff3af922017-03-23 10:23:14 -04002655 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT;
Milan Broz8f0009a2017-03-16 15:39:44 +01002656 } else if (!strcasecmp(opt_string, "iv_large_sectors"))
2657 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
2658 else {
Milan Brozef43aa32017-01-04 20:23:54 +01002659 ti->error = "Invalid feature arguments";
2660 return -EINVAL;
2661 }
2662 }
2663
2664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666
2667/*
2668 * Construct an encryption mapping:
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002669 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 */
2671static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2672{
2673 struct crypt_config *cc;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002674 int key_size;
Milan Brozef43aa32017-01-04 20:23:54 +01002675 unsigned int align_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 unsigned long long tmpll;
2677 int ret;
Milan Brozef43aa32017-01-04 20:23:54 +01002678 size_t iv_size_padding, additional_req_size;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002679 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Milan Broz772ae5f2011-08-02 12:32:08 +01002681 if (argc < 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 ti->error = "Not enough arguments";
2683 return -EINVAL;
2684 }
2685
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002686 key_size = get_key_size(&argv[1]);
2687 if (key_size < 0) {
2688 ti->error = "Cannot parse key size";
2689 return -EINVAL;
2690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
2692 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
2693 if (!cc) {
2694 ti->error = "Cannot allocate encryption context";
2695 return -ENOMEM;
2696 }
2697 cc->key_size = key_size;
Milan Broz8f0009a2017-03-16 15:39:44 +01002698 cc->sector_size = (1 << SECTOR_SHIFT);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002699 cc->sector_shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
2701 ti->private = cc;
Milan Brozef43aa32017-01-04 20:23:54 +01002702
Mikulas Patocka50593532017-08-13 22:45:08 -04002703 spin_lock(&dm_crypt_clients_lock);
2704 dm_crypt_clients_n++;
2705 crypt_calculate_pages_per_client();
2706 spin_unlock(&dm_crypt_clients_lock);
2707
2708 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL);
2709 if (ret < 0)
2710 goto bad;
2711
Milan Brozef43aa32017-01-04 20:23:54 +01002712 /* Optional parameters need to be read before cipher constructor */
2713 if (argc > 5) {
2714 ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
2715 if (ret)
2716 goto bad;
2717 }
2718
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
2720 if (ret < 0)
2721 goto bad;
2722
Milan Broz33d2f092017-03-16 15:39:40 +01002723 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +01002724 cc->dmreq_start = sizeof(struct aead_request);
2725 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
2726 align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
2727 } else {
2728 cc->dmreq_start = sizeof(struct skcipher_request);
2729 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
2730 align_mask = crypto_skcipher_alignmask(any_tfm(cc));
2731 }
Mikulas Patockad49ec522014-08-28 11:09:31 -04002732 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
2733
Milan Brozef43aa32017-01-04 20:23:54 +01002734 if (align_mask < CRYPTO_MINALIGN) {
Mikulas Patockad49ec522014-08-28 11:09:31 -04002735 /* Allocate the padding exactly */
2736 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
Milan Brozef43aa32017-01-04 20:23:54 +01002737 & align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002738 } else {
2739 /*
2740 * If the cipher requires greater alignment than kmalloc
2741 * alignment, we don't know the exact position of the
2742 * initialization vector. We must assume worst case.
2743 */
Milan Brozef43aa32017-01-04 20:23:54 +01002744 iv_size_padding = align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Milan Brozef43aa32017-01-04 20:23:54 +01002747 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
2748 additional_req_size = sizeof(struct dm_crypt_request) +
2749 iv_size_padding + cc->iv_size +
2750 cc->iv_size +
2751 sizeof(uint64_t) +
2752 sizeof(unsigned int);
2753
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002754 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size);
2755 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 ti->error = "Cannot allocate crypt request mempool";
2757 goto bad;
2758 }
2759
Mike Snitzer30187e12016-01-31 13:28:26 -05002760 cc->per_bio_data_size = ti->per_io_data_size =
Milan Brozef43aa32017-01-04 20:23:54 +01002761 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
Mikulas Patockad49ec522014-08-28 11:09:31 -04002762 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002763
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002764 ret = mempool_init(&cc->page_pool, BIO_MAX_PAGES, crypt_page_alloc, crypt_page_free, cc);
2765 if (ret) {
Milan Broz8b004452006-10-03 01:15:37 -07002766 ti->error = "Cannot allocate page mempool";
Milan Broze48d4bb2006-10-03 01:15:37 -07002767 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002769
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002770 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS);
2771 if (ret) {
Milan Broz0c395b02008-02-08 02:10:54 +00002772 ti->error = "Cannot allocate crypt bioset";
Milan Brozcabf08e2007-10-19 22:38:58 +01002773 goto bad;
Milan Broz93e605c2006-10-03 01:15:38 -07002774 }
Milan Brozcabf08e2007-10-19 22:38:58 +01002775
Mikulas Patocka7145c242015-02-13 08:24:41 -05002776 mutex_init(&cc->bio_alloc_lock);
2777
Milan Brozcabf08e2007-10-19 22:38:58 +01002778 ret = -EINVAL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002779 if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
2780 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002781 ti->error = "Invalid iv_offset sector";
2782 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002784 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Vivek Goyale80d1c82015-07-31 09:20:36 -04002786 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
2787 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 ti->error = "Device lookup failed";
2789 goto bad;
2790 }
2791
Vivek Goyale80d1c82015-07-31 09:20:36 -04002792 ret = -EINVAL;
Milan Broz887b1c92018-11-07 22:24:55 +01002793 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 ti->error = "Invalid device sector";
2795 goto bad;
2796 }
2797 cc->start = tmpll;
2798
Milan Broz33d2f092017-03-16 15:39:40 +01002799 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
Milan Brozef43aa32017-01-04 20:23:54 +01002800 ret = crypt_integrity_ctr(cc, ti);
Milan Broz772ae5f2011-08-02 12:32:08 +01002801 if (ret)
2802 goto bad;
2803
Milan Brozef43aa32017-01-04 20:23:54 +01002804 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
2805 if (!cc->tag_pool_max_sectors)
2806 cc->tag_pool_max_sectors = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01002807
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002808 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS,
Milan Brozef43aa32017-01-04 20:23:54 +01002809 cc->tag_pool_max_sectors * cc->on_disk_tag_size);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002810 if (ret) {
Milan Brozef43aa32017-01-04 20:23:54 +01002811 ti->error = "Cannot allocate integrity tags mempool";
2812 goto bad;
Milan Broz772ae5f2011-08-02 12:32:08 +01002813 }
Mikulas Patocka583fe742017-04-18 16:51:54 -04002814
2815 cc->tag_pool_max_sectors <<= cc->sector_shift;
Milan Broz772ae5f2011-08-02 12:32:08 +01002816 }
2817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 ret = -ENOMEM;
Tim Murraya1b89132017-04-21 11:11:36 +02002819 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 if (!cc->io_queue) {
2821 ti->error = "Couldn't create kcryptd io queue";
2822 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 }
Christophe Saout37af6562006-10-30 20:39:08 +01002824
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002825 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
Tim Murraya1b89132017-04-21 11:11:36 +02002826 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002827 else
Tim Murraya1b89132017-04-21 11:11:36 +02002828 cc->crypt_queue = alloc_workqueue("kcryptd",
2829 WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002830 num_online_cpus());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 if (!cc->crypt_queue) {
2832 ti->error = "Couldn't create kcryptd queue";
2833 goto bad;
2834 }
2835
Mikulas Patockac7329ef2018-07-11 12:10:51 -04002836 spin_lock_init(&cc->write_thread_lock);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05002837 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05002838
2839 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write");
2840 if (IS_ERR(cc->write_thread)) {
2841 ret = PTR_ERR(cc->write_thread);
2842 cc->write_thread = NULL;
2843 ti->error = "Couldn't spawn write thread";
2844 goto bad;
2845 }
2846 wake_up_process(cc->write_thread);
2847
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002848 ti->num_flush_bios = 1;
Milan Broz983c7db2011-09-25 23:26:21 +01002849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return 0;
2851
2852bad:
2853 crypt_dtr(ti);
2854 return ret;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002855}
2856
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002857static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
2859 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01002860 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002861
Milan Broz772ae5f2011-08-02 12:32:08 +01002862 /*
Mike Christie28a8f0d2016-06-05 14:32:25 -05002863 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
2864 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
Mike Christiee6047142016-06-05 14:32:04 -05002865 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
Milan Broz772ae5f2011-08-02 12:32:08 +01002866 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06002867 if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
Mike Christie28a8f0d2016-06-05 14:32:25 -05002868 bio_op(bio) == REQ_OP_DISCARD)) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02002869 bio_set_dev(bio, cc->dev->bdev);
Milan Broz772ae5f2011-08-02 12:32:08 +01002870 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07002871 bio->bi_iter.bi_sector = cc->start +
2872 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002873 return DM_MAPIO_REMAPPED;
2874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002876 /*
2877 * Check if bio is too large, split as needed.
2878 */
2879 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
Milan Brozef43aa32017-01-04 20:23:54 +01002880 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002881 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
2882
Milan Broz8f0009a2017-03-16 15:39:44 +01002883 /*
2884 * Ensure that bio is a multiple of internal sector encryption size
2885 * and is aligned to this size as defined in IO hints.
2886 */
2887 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
Christoph Hellwig846785e2017-06-03 09:38:02 +02002888 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002889
2890 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
Christoph Hellwig846785e2017-06-03 09:38:02 +02002891 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002892
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002893 io = dm_per_bio_data(bio, cc->per_bio_data_size);
2894 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Milan Brozef43aa32017-01-04 20:23:54 +01002895
2896 if (cc->on_disk_tag_size) {
Mikulas Patocka583fe742017-04-18 16:51:54 -04002897 unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift);
Milan Brozef43aa32017-01-04 20:23:54 +01002898
2899 if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
Mikulas Patocka583fe742017-04-18 16:51:54 -04002900 unlikely(!(io->integrity_metadata = kmalloc(tag_len,
Milan Brozef43aa32017-01-04 20:23:54 +01002901 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
2902 if (bio_sectors(bio) > cc->tag_pool_max_sectors)
2903 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002904 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO);
Milan Brozef43aa32017-01-04 20:23:54 +01002905 io->integrity_metadata_from_pool = true;
2906 }
2907 }
2908
Milan Broz33d2f092017-03-16 15:39:40 +01002909 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002910 io->ctx.r.req_aead = (struct aead_request *)(io + 1);
2911 else
2912 io->ctx.r.req = (struct skcipher_request *)(io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Milan Broz20c82532011-01-13 19:59:53 +00002914 if (bio_data_dir(io->base_bio) == READ) {
2915 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05002916 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00002917 } else
Andrew Morton4ee218c2006-03-27 01:17:48 -08002918 kcryptd_queue_crypt(io);
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return DM_MAPIO_SUBMITTED;
2921}
2922
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002923static void crypt_status(struct dm_target *ti, status_type_t type,
2924 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925{
Milan Broz5ebaee62010-08-12 04:14:07 +01002926 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002927 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002928 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 switch (type) {
2931 case STATUSTYPE_INFO:
2932 result[0] = '\0';
2933 break;
2934
2935 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00002936 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002938 if (cc->key_size > 0) {
2939 if (cc->key_string)
2940 DMEMIT(":%u:%s", cc->key_size, cc->key_string);
2941 else
2942 for (i = 0; i < cc->key_size; i++)
2943 DMEMIT("%02x", cc->key[i]);
2944 } else
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002945 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
2947 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
2948 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01002949
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002950 num_feature_args += !!ti->num_discard_bios;
2951 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002952 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002953 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT);
Milan Broz8f0009a2017-03-16 15:39:44 +01002954 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002955 if (cc->on_disk_tag_size)
2956 num_feature_args++;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002957 if (num_feature_args) {
2958 DMEMIT(" %d", num_feature_args);
2959 if (ti->num_discard_bios)
2960 DMEMIT(" allow_discards");
2961 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2962 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002963 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
2964 DMEMIT(" submit_from_crypt_cpus");
Milan Brozef43aa32017-01-04 20:23:54 +01002965 if (cc->on_disk_tag_size)
2966 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
Milan Broz8f0009a2017-03-16 15:39:44 +01002967 if (cc->sector_size != (1 << SECTOR_SHIFT))
2968 DMEMIT(" sector_size:%d", cc->sector_size);
2969 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
2970 DMEMIT(" iv_large_sectors");
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002971 }
Milan Broz772ae5f2011-08-02 12:32:08 +01002972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 break;
2974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975}
2976
Milan Broze48d4bb2006-10-03 01:15:37 -07002977static void crypt_postsuspend(struct dm_target *ti)
2978{
2979 struct crypt_config *cc = ti->private;
2980
2981 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2982}
2983
2984static int crypt_preresume(struct dm_target *ti)
2985{
2986 struct crypt_config *cc = ti->private;
2987
2988 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
2989 DMERR("aborting resume - crypt key is not set.");
2990 return -EAGAIN;
2991 }
2992
2993 return 0;
2994}
2995
2996static void crypt_resume(struct dm_target *ti)
2997{
2998 struct crypt_config *cc = ti->private;
2999
3000 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
3001}
3002
3003/* Message interface
3004 * key set <key>
3005 * key wipe
3006 */
Mike Snitzer1eb5fa82018-02-28 15:59:59 -05003007static int crypt_message(struct dm_target *ti, unsigned argc, char **argv,
3008 char *result, unsigned maxlen)
Milan Broze48d4bb2006-10-03 01:15:37 -07003009{
3010 struct crypt_config *cc = ti->private;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01003011 int key_size, ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07003012
3013 if (argc < 2)
3014 goto error;
3015
Mike Snitzer498f0102011-08-02 12:32:04 +01003016 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07003017 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
3018 DMWARN("not suspended during key manipulation.");
3019 return -EINVAL;
3020 }
Mike Snitzer498f0102011-08-02 12:32:04 +01003021 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01003022 /* The key size may not be changed. */
3023 key_size = get_key_size(&argv[2]);
3024 if (key_size < 0 || cc->key_size != key_size) {
3025 memset(argv[2], '0', strlen(argv[2]));
3026 return -EINVAL;
3027 }
3028
Milan Broz542da312009-12-10 23:51:57 +00003029 ret = crypt_set_key(cc, argv[2]);
3030 if (ret)
3031 return ret;
3032 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
3033 ret = cc->iv_gen_ops->init(cc);
Ondrej Kozinadc949022018-01-12 16:30:32 +01003034 /* wipe the kernel key payload copy */
3035 if (cc->key_string)
3036 memset(cc->key, 0, cc->key_size * sizeof(u8));
Milan Broz542da312009-12-10 23:51:57 +00003037 return ret;
3038 }
Mike Snitzer498f0102011-08-02 12:32:04 +01003039 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00003040 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
3041 ret = cc->iv_gen_ops->wipe(cc);
3042 if (ret)
3043 return ret;
3044 }
Milan Broze48d4bb2006-10-03 01:15:37 -07003045 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00003046 }
Milan Broze48d4bb2006-10-03 01:15:37 -07003047 }
3048
3049error:
3050 DMWARN("unrecognised message received.");
3051 return -EINVAL;
3052}
3053
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003054static int crypt_iterate_devices(struct dm_target *ti,
3055 iterate_devices_callout_fn fn, void *data)
3056{
3057 struct crypt_config *cc = ti->private;
3058
Mike Snitzer5dea2712009-07-23 20:30:42 +01003059 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003060}
3061
Mike Snitzer586b2862015-09-09 21:34:51 -04003062static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
3063{
Milan Broz8f0009a2017-03-16 15:39:44 +01003064 struct crypt_config *cc = ti->private;
3065
Mike Snitzer586b2862015-09-09 21:34:51 -04003066 /*
3067 * Unfortunate constraint that is required to avoid the potential
3068 * for exceeding underlying device's max_segments limits -- due to
3069 * crypt_alloc_buffer() possibly allocating pages for the encryption
3070 * bio that are not as physically contiguous as the original bio.
3071 */
3072 limits->max_segment_size = PAGE_SIZE;
Milan Broz8f0009a2017-03-16 15:39:44 +01003073
Mikulas Patockabc9e9cf2018-08-10 11:23:56 -04003074 limits->logical_block_size =
3075 max_t(unsigned short, limits->logical_block_size, cc->sector_size);
3076 limits->physical_block_size =
3077 max_t(unsigned, limits->physical_block_size, cc->sector_size);
3078 limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size);
Mike Snitzer586b2862015-09-09 21:34:51 -04003079}
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081static struct target_type crypt_target = {
3082 .name = "crypt",
Ondrej Kozinadc949022018-01-12 16:30:32 +01003083 .version = {1, 18, 1},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 .module = THIS_MODULE,
3085 .ctr = crypt_ctr,
3086 .dtr = crypt_dtr,
3087 .map = crypt_map,
3088 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07003089 .postsuspend = crypt_postsuspend,
3090 .preresume = crypt_preresume,
3091 .resume = crypt_resume,
3092 .message = crypt_message,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003093 .iterate_devices = crypt_iterate_devices,
Mike Snitzer586b2862015-09-09 21:34:51 -04003094 .io_hints = crypt_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095};
3096
3097static int __init dm_crypt_init(void)
3098{
3099 int r;
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05003102 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07003103 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 return r;
3106}
3107
3108static void __exit dm_crypt_exit(void)
3109{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00003110 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
3113module_init(dm_crypt_init);
3114module_exit(dm_crypt_exit);
3115
Jana Saoutbf142992014-06-24 14:27:04 -04003116MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
3118MODULE_LICENSE("GPL");