blob: 9a29037f56158f4664c28fa90ef3a733edaf533a [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 security8d683dc2018-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 security8d683dc2018-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 security8d683dc2018-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 */
Eric Biggers1ad0f162018-11-14 12:19:39 -0800380 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, 0);
Andi Kleenc0297722011-01-13 19:59:53 +0000381 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
935 tag_len = io->cc->on_disk_tag_size * bio_sectors(bio);
936
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);
952
953 /* From now we require underlying device with our integrity profile */
954 if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) {
955 ti->error = "Integrity profile not supported.";
956 return -EINVAL;
957 }
958
Mikulas Patocka583fe742017-04-18 16:51:54 -0400959 if (bi->tag_size != cc->on_disk_tag_size ||
960 bi->tuple_size != cc->on_disk_tag_size) {
Milan Brozef43aa32017-01-04 20:23:54 +0100961 ti->error = "Integrity profile tag size mismatch.";
962 return -EINVAL;
963 }
Mikulas Patocka583fe742017-04-18 16:51:54 -0400964 if (1 << bi->interval_exp != cc->sector_size) {
965 ti->error = "Integrity profile sector size mismatch.";
966 return -EINVAL;
967 }
Milan Brozef43aa32017-01-04 20:23:54 +0100968
Milan Broz33d2f092017-03-16 15:39:40 +0100969 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +0100970 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size;
971 DMINFO("Integrity AEAD, tag size %u, IV size %u.",
972 cc->integrity_tag_size, cc->integrity_iv_size);
973
974 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) {
975 ti->error = "Integrity AEAD auth tag size is not supported.";
976 return -EINVAL;
977 }
978 } else if (cc->integrity_iv_size)
979 DMINFO("Additional per-sector space %u bytes for IV.",
980 cc->integrity_iv_size);
981
982 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) {
983 ti->error = "Not enough space for integrity tag in the profile.";
984 return -EINVAL;
985 }
986
987 return 0;
988#else
989 ti->error = "Integrity profile not supported.";
990 return -EINVAL;
991#endif
992}
993
Milan Brozd469f842007-10-19 22:42:37 +0100994static void crypt_convert_init(struct crypt_config *cc,
995 struct convert_context *ctx,
996 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000997 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
999 ctx->bio_in = bio_in;
1000 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -07001001 if (bio_in)
1002 ctx->iter_in = bio_in->bi_iter;
1003 if (bio_out)
1004 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +01001005 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +00001006 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007}
1008
Huang Yingb2174ee2009-03-16 17:44:33 +00001009static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
Milan Brozef43aa32017-01-04 20:23:54 +01001010 void *req)
Huang Yingb2174ee2009-03-16 17:44:33 +00001011{
1012 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
1013}
1014
Milan Brozef43aa32017-01-04 20:23:54 +01001015static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq)
Huang Yingb2174ee2009-03-16 17:44:33 +00001016{
Milan Brozef43aa32017-01-04 20:23:54 +01001017 return (void *)((char *)dmreq - cc->dmreq_start);
Huang Yingb2174ee2009-03-16 17:44:33 +00001018}
1019
Milan Broz2dc53272011-01-13 19:59:54 +00001020static u8 *iv_of_dmreq(struct crypt_config *cc,
1021 struct dm_crypt_request *dmreq)
1022{
Milan Broz33d2f092017-03-16 15:39:40 +01001023 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001024 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1025 crypto_aead_alignmask(any_tfm_aead(cc)) + 1);
1026 else
1027 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1028 crypto_skcipher_alignmask(any_tfm(cc)) + 1);
Milan Broz2dc53272011-01-13 19:59:54 +00001029}
1030
Milan Brozef43aa32017-01-04 20:23:54 +01001031static u8 *org_iv_of_dmreq(struct crypt_config *cc,
1032 struct dm_crypt_request *dmreq)
1033{
1034 return iv_of_dmreq(cc, dmreq) + cc->iv_size;
1035}
1036
1037static uint64_t *org_sector_of_dmreq(struct crypt_config *cc,
1038 struct dm_crypt_request *dmreq)
1039{
1040 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size;
1041 return (uint64_t*) ptr;
1042}
1043
1044static unsigned int *org_tag_of_dmreq(struct crypt_config *cc,
1045 struct dm_crypt_request *dmreq)
1046{
1047 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size +
1048 cc->iv_size + sizeof(uint64_t);
1049 return (unsigned int*)ptr;
1050}
1051
1052static void *tag_from_dmreq(struct crypt_config *cc,
1053 struct dm_crypt_request *dmreq)
1054{
1055 struct convert_context *ctx = dmreq->ctx;
1056 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1057
1058 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) *
1059 cc->on_disk_tag_size];
1060}
1061
1062static void *iv_tag_from_dmreq(struct crypt_config *cc,
1063 struct dm_crypt_request *dmreq)
1064{
1065 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size;
1066}
1067
1068static int crypt_convert_block_aead(struct crypt_config *cc,
1069 struct convert_context *ctx,
1070 struct aead_request *req,
1071 unsigned int tag_offset)
Milan Broz01482b72008-02-08 02:11:04 +00001072{
Kent Overstreet003b5c52013-10-11 15:45:43 -07001073 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1074 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001075 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001076 u8 *iv, *org_iv, *tag_iv, *tag;
1077 uint64_t *sector;
1078 int r = 0;
1079
1080 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size);
Milan Broz01482b72008-02-08 02:11:04 +00001081
Milan Broz8f0009a2017-03-16 15:39:44 +01001082 /* Reject unexpected unaligned bio. */
Mikulas Patocka0440d5c2017-11-07 10:35:57 -05001083 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Broz8f0009a2017-03-16 15:39:44 +01001084 return -EIO;
Milan Broz01482b72008-02-08 02:11:04 +00001085
Huang Yingb2174ee2009-03-16 17:44:33 +00001086 dmreq = dmreq_of_req(cc, req);
Mikulas Patockac66029f2012-07-27 15:08:05 +01001087 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001088 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001089 dmreq->iv_sector >>= cc->sector_shift;
Huang Yingb2174ee2009-03-16 17:44:33 +00001090 dmreq->ctx = ctx;
Milan Broz01482b72008-02-08 02:11:04 +00001091
Milan Brozef43aa32017-01-04 20:23:54 +01001092 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
Milan Broz01482b72008-02-08 02:11:04 +00001093
Milan Brozef43aa32017-01-04 20:23:54 +01001094 sector = org_sector_of_dmreq(cc, dmreq);
1095 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1096
1097 iv = iv_of_dmreq(cc, dmreq);
1098 org_iv = org_iv_of_dmreq(cc, dmreq);
1099 tag = tag_from_dmreq(cc, dmreq);
1100 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1101
1102 /* AEAD request:
1103 * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
1104 * | (authenticated) | (auth+encryption) | |
1105 * | sector_LE | IV | sector in/out | tag in/out |
1106 */
1107 sg_init_table(dmreq->sg_in, 4);
1108 sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t));
1109 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001110 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 +01001111 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size);
1112
1113 sg_init_table(dmreq->sg_out, 4);
1114 sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t));
1115 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001116 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 +01001117 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size);
Milan Broz01482b72008-02-08 02:11:04 +00001118
Milan Broz3a7f6c92008-02-08 02:11:14 +00001119 if (cc->iv_gen_ops) {
Milan Brozef43aa32017-01-04 20:23:54 +01001120 /* For READs use IV stored in integrity metadata */
1121 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1122 memcpy(org_iv, tag_iv, cc->iv_size);
1123 } else {
1124 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1125 if (r < 0)
1126 return r;
1127 /* Store generated IV in integrity metadata */
1128 if (cc->integrity_iv_size)
1129 memcpy(tag_iv, org_iv, cc->iv_size);
1130 }
1131 /* Working copy of IV, to be modified in crypto API */
1132 memcpy(iv, org_iv, cc->iv_size);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001133 }
1134
Milan Brozef43aa32017-01-04 20:23:54 +01001135 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size);
1136 if (bio_data_dir(ctx->bio_in) == WRITE) {
1137 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001138 cc->sector_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001139 r = crypto_aead_encrypt(req);
1140 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size)
1141 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0,
1142 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size));
1143 } else {
1144 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001145 cc->sector_size + cc->integrity_tag_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001146 r = crypto_aead_decrypt(req);
1147 }
1148
1149 if (r == -EBADMSG)
1150 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1151 (unsigned long long)le64_to_cpu(*sector));
1152
1153 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
1154 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1155
Milan Broz8f0009a2017-03-16 15:39:44 +01001156 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1157 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001158
1159 return r;
1160}
1161
1162static int crypt_convert_block_skcipher(struct crypt_config *cc,
1163 struct convert_context *ctx,
1164 struct skcipher_request *req,
1165 unsigned int tag_offset)
1166{
1167 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1168 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
1169 struct scatterlist *sg_in, *sg_out;
1170 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001171 u8 *iv, *org_iv, *tag_iv;
1172 uint64_t *sector;
1173 int r = 0;
1174
Milan Broz8f0009a2017-03-16 15:39:44 +01001175 /* Reject unexpected unaligned bio. */
Mikulas Patocka0440d5c2017-11-07 10:35:57 -05001176 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Broz8f0009a2017-03-16 15:39:44 +01001177 return -EIO;
1178
Milan Brozef43aa32017-01-04 20:23:54 +01001179 dmreq = dmreq_of_req(cc, req);
1180 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001181 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001182 dmreq->iv_sector >>= cc->sector_shift;
Milan Brozef43aa32017-01-04 20:23:54 +01001183 dmreq->ctx = ctx;
1184
1185 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
1186
1187 iv = iv_of_dmreq(cc, dmreq);
1188 org_iv = org_iv_of_dmreq(cc, dmreq);
1189 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1190
1191 sector = org_sector_of_dmreq(cc, dmreq);
1192 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1193
1194 /* For skcipher we use only the first sg item */
1195 sg_in = &dmreq->sg_in[0];
1196 sg_out = &dmreq->sg_out[0];
1197
1198 sg_init_table(sg_in, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001199 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001200
1201 sg_init_table(sg_out, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001202 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001203
1204 if (cc->iv_gen_ops) {
1205 /* For READs use IV stored in integrity metadata */
1206 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1207 memcpy(org_iv, tag_iv, cc->integrity_iv_size);
1208 } else {
1209 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1210 if (r < 0)
1211 return r;
1212 /* Store generated IV in integrity metadata */
1213 if (cc->integrity_iv_size)
1214 memcpy(tag_iv, org_iv, cc->integrity_iv_size);
1215 }
1216 /* Working copy of IV, to be modified in crypto API */
1217 memcpy(iv, org_iv, cc->iv_size);
1218 }
1219
Milan Broz8f0009a2017-03-16 15:39:44 +01001220 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001221
1222 if (bio_data_dir(ctx->bio_in) == WRITE)
Herbert Xubbdb23b2016-01-24 21:16:36 +08001223 r = crypto_skcipher_encrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001224 else
Herbert Xubbdb23b2016-01-24 21:16:36 +08001225 r = crypto_skcipher_decrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001226
Milan Broz2dc53272011-01-13 19:59:54 +00001227 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001228 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1229
Milan Broz8f0009a2017-03-16 15:39:44 +01001230 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1231 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Broz2dc53272011-01-13 19:59:54 +00001232
Milan Broz3a7f6c92008-02-08 02:11:14 +00001233 return r;
Milan Broz01482b72008-02-08 02:11:04 +00001234}
1235
Milan Broz95497a92008-02-08 02:11:12 +00001236static void kcryptd_async_done(struct crypto_async_request *async_req,
1237 int error);
Andi Kleenc0297722011-01-13 19:59:53 +00001238
Milan Brozef43aa32017-01-04 20:23:54 +01001239static void crypt_alloc_req_skcipher(struct crypt_config *cc,
1240 struct convert_context *ctx)
Milan Brozddd42ed2008-02-08 02:11:07 +00001241{
Mikulas Patockac66029f2012-07-27 15:08:05 +01001242 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +00001243
Milan Brozef43aa32017-01-04 20:23:54 +01001244 if (!ctx->r.req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001245 ctx->r.req = mempool_alloc(&cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +00001246
Milan Brozef43aa32017-01-04 20:23:54 +01001247 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]);
Milan Broz54cea3f2015-05-15 17:00:25 +02001248
1249 /*
1250 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1251 * requests if driver request queue is full.
1252 */
Milan Brozef43aa32017-01-04 20:23:54 +01001253 skcipher_request_set_callback(ctx->r.req,
Mikulas Patocka432061b2018-09-05 09:17:45 -04001254 CRYPTO_TFM_REQ_MAY_BACKLOG,
Milan Brozef43aa32017-01-04 20:23:54 +01001255 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req));
Milan Brozddd42ed2008-02-08 02:11:07 +00001256}
1257
Milan Brozef43aa32017-01-04 20:23:54 +01001258static void crypt_alloc_req_aead(struct crypt_config *cc,
1259 struct convert_context *ctx)
1260{
1261 if (!ctx->r.req_aead)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001262 ctx->r.req_aead = mempool_alloc(&cc->req_pool, GFP_NOIO);
Milan Brozef43aa32017-01-04 20:23:54 +01001263
1264 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]);
1265
1266 /*
1267 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1268 * requests if driver request queue is full.
1269 */
1270 aead_request_set_callback(ctx->r.req_aead,
Mikulas Patocka432061b2018-09-05 09:17:45 -04001271 CRYPTO_TFM_REQ_MAY_BACKLOG,
Milan Brozef43aa32017-01-04 20:23:54 +01001272 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead));
1273}
1274
1275static void crypt_alloc_req(struct crypt_config *cc,
1276 struct convert_context *ctx)
1277{
Milan Broz33d2f092017-03-16 15:39:40 +01001278 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001279 crypt_alloc_req_aead(cc, ctx);
1280 else
1281 crypt_alloc_req_skcipher(cc, ctx);
1282}
1283
1284static void crypt_free_req_skcipher(struct crypt_config *cc,
1285 struct skcipher_request *req, struct bio *base_bio)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001286{
1287 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1288
Herbert Xubbdb23b2016-01-24 21:16:36 +08001289 if ((struct skcipher_request *)(io + 1) != req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001290 mempool_free(req, &cc->req_pool);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001291}
1292
Milan Brozef43aa32017-01-04 20:23:54 +01001293static void crypt_free_req_aead(struct crypt_config *cc,
1294 struct aead_request *req, struct bio *base_bio)
1295{
1296 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1297
1298 if ((struct aead_request *)(io + 1) != req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001299 mempool_free(req, &cc->req_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01001300}
1301
1302static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio)
1303{
Milan Broz33d2f092017-03-16 15:39:40 +01001304 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001305 crypt_free_req_aead(cc, req, base_bio);
1306 else
1307 crypt_free_req_skcipher(cc, req, base_bio);
1308}
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/*
1311 * Encrypt / decrypt data from one bio to another one (can be the same one)
1312 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001313static blk_status_t crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +01001314 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Milan Brozef43aa32017-01-04 20:23:54 +01001316 unsigned int tag_offset = 0;
Mikulas Patockaff3af922017-03-23 10:23:14 -04001317 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT;
Milan Broz3f1e9072008-03-28 14:16:07 -07001318 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Mikulas Patocka40b62292012-07-27 15:08:04 +01001320 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +01001321
Kent Overstreet003b5c52013-10-11 15:45:43 -07001322 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Milan Broz3a7f6c92008-02-08 02:11:14 +00001324 crypt_alloc_req(cc, ctx);
Mikulas Patocka40b62292012-07-27 15:08:04 +01001325 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -07001326
Milan Broz33d2f092017-03-16 15:39:40 +01001327 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001328 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset);
1329 else
1330 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001331
1332 switch (r) {
Milan Broz54cea3f2015-05-15 17:00:25 +02001333 /*
1334 * The request was queued by a crypto driver
1335 * but the driver request queue is full, let's wait.
1336 */
Milan Broz3a7f6c92008-02-08 02:11:14 +00001337 case -EBUSY:
1338 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -08001339 reinit_completion(&ctx->restart);
Milan Broz54cea3f2015-05-15 17:00:25 +02001340 /* fall through */
1341 /*
1342 * The request is queued and processed asynchronously,
1343 * completion function kcryptd_async_done() will be called.
1344 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001345 case -EINPROGRESS:
Milan Brozef43aa32017-01-04 20:23:54 +01001346 ctx->r.req = NULL;
Milan Broz8f0009a2017-03-16 15:39:44 +01001347 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001348 tag_offset++;
Milan Broz3a7f6c92008-02-08 02:11:14 +00001349 continue;
Milan Broz54cea3f2015-05-15 17:00:25 +02001350 /*
1351 * The request was already processed (synchronously).
1352 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001353 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001354 atomic_dec(&ctx->cc_pending);
Milan Broz8f0009a2017-03-16 15:39:44 +01001355 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001356 tag_offset++;
Milan Brozc7f1b202008-07-02 09:34:28 +01001357 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -07001358 continue;
Milan Brozef43aa32017-01-04 20:23:54 +01001359 /*
1360 * There was a data integrity error.
1361 */
1362 case -EBADMSG:
1363 atomic_dec(&ctx->cc_pending);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001364 return BLK_STS_PROTECTION;
Milan Brozef43aa32017-01-04 20:23:54 +01001365 /*
1366 * There was an error while processing the request.
1367 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001368 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001369 atomic_dec(&ctx->cc_pending);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001370 return BLK_STS_IOERR;
Milan Broz3f1e9072008-03-28 14:16:07 -07001371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 }
1373
Milan Broz3f1e9072008-03-28 14:16:07 -07001374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001377static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379/*
1380 * Generate a new unfragmented bio with the given size
Mike Snitzer586b2862015-09-09 21:34:51 -04001381 * This should never violate the device limitations (but only because
1382 * max_segment_size is being constrained to PAGE_SIZE).
Mikulas Patocka7145c242015-02-13 08:24:41 -05001383 *
1384 * This function may be called concurrently. If we allocate from the mempool
1385 * concurrently, there is a possibility of deadlock. For example, if we have
1386 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1387 * the mempool concurrently, it may deadlock in a situation where both processes
1388 * have allocated 128 pages and the mempool is exhausted.
1389 *
1390 * In order to avoid this scenario we allocate the pages under a mutex.
1391 *
1392 * In order to not degrade performance with excessive locking, we try
1393 * non-blocking allocations without a mutex first but on failure we fallback
1394 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001396static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001398 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001399 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001401 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
1402 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +00001403 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Mikulas Patocka7145c242015-02-13 08:24:41 -05001405retry:
Mel Gormand0164ad2015-11-06 16:28:21 -08001406 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001407 mutex_lock(&cc->bio_alloc_lock);
1408
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001409 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, &cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -07001410 if (!clone)
Milan Brozef43aa32017-01-04 20:23:54 +01001411 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Olaf Kirch027581f2007-05-09 02:32:52 -07001413 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -07001414
Mikulas Patocka7145c242015-02-13 08:24:41 -05001415 remaining_size = size;
1416
Olaf Kirchf97380b2007-05-09 02:32:54 -07001417 for (i = 0; i < nr_iovecs; i++) {
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001418 page = mempool_alloc(&cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -05001419 if (!page) {
1420 crypt_free_buffer_pages(cc, clone);
1421 bio_put(clone);
Mel Gormand0164ad2015-11-06 16:28:21 -08001422 gfp_mask |= __GFP_DIRECT_RECLAIM;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001423 goto retry;
1424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Mikulas Patocka7145c242015-02-13 08:24:41 -05001426 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Ming Lei0dae7fe2016-10-29 16:08:06 +08001428 bio_add_page(clone, page, len, 0);
Milan Broz91e10622007-12-13 14:16:10 +00001429
Mikulas Patocka7145c242015-02-13 08:24:41 -05001430 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
1432
Milan Brozef43aa32017-01-04 20:23:54 +01001433 /* Allocate space for integrity tags */
1434 if (dm_crypt_integrity_io_alloc(io, clone)) {
1435 crypt_free_buffer_pages(cc, clone);
1436 bio_put(clone);
1437 clone = NULL;
1438 }
1439out:
Mel Gormand0164ad2015-11-06 16:28:21 -08001440 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001441 mutex_unlock(&cc->bio_alloc_lock);
1442
Milan Broz8b004452006-10-03 01:15:37 -07001443 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
Neil Brown644bd2f2007-10-16 13:48:46 +02001446static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Neil Brown644bd2f2007-10-16 13:48:46 +02001448 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 struct bio_vec *bv;
Ming Lei6dc4f102019-02-15 19:13:19 +08001450 struct bvec_iter_all iter_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Ming Lei6dc4f102019-02-15 19:13:19 +08001452 bio_for_each_segment_all(bv, clone, i, iter_all) {
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
Eric Biggersaf331eb2018-12-05 20:53:00 -08001889 /*
1890 * dm-crypt performance can vary greatly depending on which crypto
1891 * algorithm implementation is used. Help people debug performance
1892 * problems by logging the ->cra_driver_name.
1893 */
1894 DMINFO("%s using implementation \"%s\"", ciphermode,
1895 crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name);
Milan Brozd1f96422011-01-13 19:59:54 +00001896 return 0;
1897}
1898
Milan Brozef43aa32017-01-04 20:23:54 +01001899static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
1900{
Milan Brozef43aa32017-01-04 20:23:54 +01001901 int err;
1902
1903 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
1904 if (!cc->cipher_tfm.tfms)
1905 return -ENOMEM;
1906
Milan Brozef43aa32017-01-04 20:23:54 +01001907 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
1908 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1909 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
1910 crypt_free_tfms(cc);
1911 return err;
1912 }
1913
Eric Biggersaf331eb2018-12-05 20:53:00 -08001914 DMINFO("%s using implementation \"%s\"", ciphermode,
1915 crypto_aead_alg(any_tfm_aead(cc))->base.cra_driver_name);
Milan Brozef43aa32017-01-04 20:23:54 +01001916 return 0;
1917}
1918
1919static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
1920{
Milan Broz33d2f092017-03-16 15:39:40 +01001921 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001922 return crypt_alloc_tfms_aead(cc, ciphermode);
1923 else
1924 return crypt_alloc_tfms_skcipher(cc, ciphermode);
1925}
1926
1927static unsigned crypt_subkey_size(struct crypt_config *cc)
1928{
1929 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1930}
1931
1932static unsigned crypt_authenckey_size(struct crypt_config *cc)
1933{
1934 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
1935}
1936
1937/*
1938 * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
1939 * the key must be for some reason in special format.
1940 * This funcion converts cc->key to this special format.
1941 */
1942static void crypt_copy_authenckey(char *p, const void *key,
1943 unsigned enckeylen, unsigned authkeylen)
1944{
1945 struct crypto_authenc_key_param *param;
1946 struct rtattr *rta;
1947
1948 rta = (struct rtattr *)p;
1949 param = RTA_DATA(rta);
1950 param->enckeylen = cpu_to_be32(enckeylen);
1951 rta->rta_len = RTA_LENGTH(sizeof(*param));
1952 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
1953 p += RTA_SPACE(sizeof(*param));
1954 memcpy(p, key + enckeylen, authkeylen);
1955 p += authkeylen;
1956 memcpy(p, key, enckeylen);
1957}
1958
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04001959static int crypt_setkey(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +00001960{
Milan Brozda31a072013-10-28 23:21:03 +01001961 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001962 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001963
Milan Brozda31a072013-10-28 23:21:03 +01001964 /* Ignore extra keys (which are used for IV etc) */
Milan Brozef43aa32017-01-04 20:23:54 +01001965 subkey_size = crypt_subkey_size(cc);
Milan Brozda31a072013-10-28 23:21:03 +01001966
Milan Broz27c70032018-01-03 22:48:59 +01001967 if (crypt_integrity_hmac(cc)) {
1968 if (subkey_size < cc->key_mac_size)
1969 return -EINVAL;
1970
Milan Brozef43aa32017-01-04 20:23:54 +01001971 crypt_copy_authenckey(cc->authenc_key, cc->key,
1972 subkey_size - cc->key_mac_size,
1973 cc->key_mac_size);
Milan Broz27c70032018-01-03 22:48:59 +01001974 }
1975
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001976 for (i = 0; i < cc->tfms_count; i++) {
Milan Broz33d2f092017-03-16 15:39:40 +01001977 if (crypt_integrity_hmac(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001978 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1979 cc->authenc_key, crypt_authenckey_size(cc));
Milan Broz33d2f092017-03-16 15:39:40 +01001980 else if (crypt_integrity_aead(cc))
1981 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1982 cc->key + (i * subkey_size),
1983 subkey_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001984 else
1985 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i],
1986 cc->key + (i * subkey_size),
1987 subkey_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001988 if (r)
1989 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001990 }
1991
Milan Brozef43aa32017-01-04 20:23:54 +01001992 if (crypt_integrity_hmac(cc))
1993 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc));
1994
Andi Kleenc0297722011-01-13 19:59:53 +00001995 return err;
1996}
1997
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001998#ifdef CONFIG_KEYS
1999
Ondrej Kozina027c4312016-12-01 18:20:52 +01002000static bool contains_whitespace(const char *str)
2001{
2002 while (*str)
2003 if (isspace(*str++))
2004 return true;
2005 return false;
2006}
2007
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002008static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2009{
2010 char *new_key_string, *key_desc;
2011 int ret;
2012 struct key *key;
2013 const struct user_key_payload *ukp;
2014
Ondrej Kozina027c4312016-12-01 18:20:52 +01002015 /*
2016 * Reject key_string with whitespace. dm core currently lacks code for
2017 * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
2018 */
2019 if (contains_whitespace(key_string)) {
2020 DMERR("whitespace chars not allowed in key string");
2021 return -EINVAL;
2022 }
2023
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002024 /* look for next ':' separating key_type from key_description */
2025 key_desc = strpbrk(key_string, ":");
2026 if (!key_desc || key_desc == key_string || !strlen(key_desc + 1))
2027 return -EINVAL;
2028
2029 if (strncmp(key_string, "logon:", key_desc - key_string + 1) &&
2030 strncmp(key_string, "user:", key_desc - key_string + 1))
2031 return -EINVAL;
2032
2033 new_key_string = kstrdup(key_string, GFP_KERNEL);
2034 if (!new_key_string)
2035 return -ENOMEM;
2036
2037 key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user,
2038 key_desc + 1, NULL);
2039 if (IS_ERR(key)) {
2040 kzfree(new_key_string);
2041 return PTR_ERR(key);
2042 }
2043
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002044 down_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002045
David Howells0837e492017-03-01 15:11:23 +00002046 ukp = user_key_payload_locked(key);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002047 if (!ukp) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002048 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002049 key_put(key);
2050 kzfree(new_key_string);
2051 return -EKEYREVOKED;
2052 }
2053
2054 if (cc->key_size != ukp->datalen) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002055 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002056 key_put(key);
2057 kzfree(new_key_string);
2058 return -EINVAL;
2059 }
2060
2061 memcpy(cc->key, ukp->data, cc->key_size);
2062
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002063 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002064 key_put(key);
2065
2066 /* clear the flag since following operations may invalidate previously valid key */
2067 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2068
2069 ret = crypt_setkey(cc);
2070
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002071 if (!ret) {
2072 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2073 kzfree(cc->key_string);
2074 cc->key_string = new_key_string;
2075 } else
2076 kzfree(new_key_string);
2077
2078 return ret;
2079}
2080
2081static int get_key_size(char **key_string)
2082{
2083 char *colon, dummy;
2084 int ret;
2085
2086 if (*key_string[0] != ':')
2087 return strlen(*key_string) >> 1;
2088
2089 /* look for next ':' in key string */
2090 colon = strpbrk(*key_string + 1, ":");
2091 if (!colon)
2092 return -EINVAL;
2093
2094 if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':')
2095 return -EINVAL;
2096
2097 *key_string = colon;
2098
2099 /* remaining key string should be :<logon|user>:<key_desc> */
2100
2101 return ret;
2102}
2103
2104#else
2105
2106static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2107{
2108 return -EINVAL;
2109}
2110
2111static int get_key_size(char **key_string)
2112{
2113 return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1;
2114}
2115
2116#endif
2117
Milan Broze48d4bb2006-10-03 01:15:37 -07002118static int crypt_set_key(struct crypt_config *cc, char *key)
2119{
Milan Brozde8be5a2011-03-24 13:54:27 +00002120 int r = -EINVAL;
2121 int key_string_len = strlen(key);
2122
Milan Broz69a8cfc2011-01-13 19:59:49 +00002123 /* Hyphen (which gives a key_size of zero) means there is no key. */
2124 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00002125 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002126
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002127 /* ':' means the key is in kernel keyring, short-circuit normal key processing */
2128 if (key[0] == ':') {
2129 r = crypt_set_keyring_key(cc, key + 1);
2130 goto out;
2131 }
2132
Ondrej Kozina265e9092016-11-02 15:02:08 +01002133 /* clear the flag since following operations may invalidate previously valid key */
2134 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2135
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002136 /* wipe references to any kernel keyring key */
2137 kzfree(cc->key_string);
2138 cc->key_string = NULL;
2139
Andy Shevchenkoe944e032017-04-27 16:52:04 +03002140 /* Decode key from its hex representation. */
2141 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00002142 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002143
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002144 r = crypt_setkey(cc);
Ondrej Kozina265e9092016-11-02 15:02:08 +01002145 if (!r)
2146 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Milan Brozde8be5a2011-03-24 13:54:27 +00002147
2148out:
2149 /* Hex key string not needed after here, so wipe it. */
2150 memset(key, '0', key_string_len);
2151
2152 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002153}
2154
2155static int crypt_wipe_key(struct crypt_config *cc)
2156{
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002157 int r;
2158
Milan Broze48d4bb2006-10-03 01:15:37 -07002159 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002160 get_random_bytes(&cc->key, cc->key_size);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002161 kzfree(cc->key_string);
2162 cc->key_string = NULL;
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002163 r = crypt_setkey(cc);
2164 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00002165
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002166 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002167}
2168
Mikulas Patocka50593532017-08-13 22:45:08 -04002169static void crypt_calculate_pages_per_client(void)
2170{
Arun KSca79b0c2018-12-28 00:34:29 -08002171 unsigned long pages = (totalram_pages() - totalhigh_pages()) * DM_CRYPT_MEMORY_PERCENT / 100;
Mikulas Patocka50593532017-08-13 22:45:08 -04002172
2173 if (!dm_crypt_clients_n)
2174 return;
2175
2176 pages /= dm_crypt_clients_n;
2177 if (pages < DM_CRYPT_MIN_PAGES_PER_CLIENT)
2178 pages = DM_CRYPT_MIN_PAGES_PER_CLIENT;
2179 dm_crypt_pages_per_client = pages;
2180}
2181
2182static void *crypt_page_alloc(gfp_t gfp_mask, void *pool_data)
2183{
2184 struct crypt_config *cc = pool_data;
2185 struct page *page;
2186
2187 if (unlikely(percpu_counter_compare(&cc->n_allocated_pages, dm_crypt_pages_per_client) >= 0) &&
2188 likely(gfp_mask & __GFP_NORETRY))
2189 return NULL;
2190
2191 page = alloc_page(gfp_mask);
2192 if (likely(page != NULL))
2193 percpu_counter_add(&cc->n_allocated_pages, 1);
2194
2195 return page;
2196}
2197
2198static void crypt_page_free(void *page, void *pool_data)
2199{
2200 struct crypt_config *cc = pool_data;
2201
2202 __free_page(page);
2203 percpu_counter_sub(&cc->n_allocated_pages, 1);
2204}
2205
Milan Broz28513fc2010-08-12 04:14:06 +01002206static void crypt_dtr(struct dm_target *ti)
2207{
2208 struct crypt_config *cc = ti->private;
2209
2210 ti->private = NULL;
2211
2212 if (!cc)
2213 return;
2214
Rabin Vincentf659b102016-09-21 16:22:29 +02002215 if (cc->write_thread)
Mikulas Patockadc267622015-02-13 08:25:59 -05002216 kthread_stop(cc->write_thread);
2217
Milan Broz28513fc2010-08-12 04:14:06 +01002218 if (cc->io_queue)
2219 destroy_workqueue(cc->io_queue);
2220 if (cc->crypt_queue)
2221 destroy_workqueue(cc->crypt_queue);
2222
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002223 crypt_free_tfms(cc);
2224
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002225 bioset_exit(&cc->bs);
Milan Broz28513fc2010-08-12 04:14:06 +01002226
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002227 mempool_exit(&cc->page_pool);
2228 mempool_exit(&cc->req_pool);
2229 mempool_exit(&cc->tag_pool);
2230
Kent Overstreetd00a11d2018-06-02 13:45:04 -04002231 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0);
2232 percpu_counter_destroy(&cc->n_allocated_pages);
2233
Milan Broz28513fc2010-08-12 04:14:06 +01002234 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
2235 cc->iv_gen_ops->dtr(cc);
2236
Milan Broz28513fc2010-08-12 04:14:06 +01002237 if (cc->dev)
2238 dm_put_device(ti, cc->dev);
2239
Milan Broz5ebaee62010-08-12 04:14:07 +01002240 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00002241 kzfree(cc->cipher_string);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002242 kzfree(cc->key_string);
Milan Brozef43aa32017-01-04 20:23:54 +01002243 kzfree(cc->cipher_auth);
2244 kzfree(cc->authenc_key);
Milan Broz28513fc2010-08-12 04:14:06 +01002245
Mike Snitzerd5ffebd2018-01-05 21:17:20 -05002246 mutex_destroy(&cc->bio_alloc_lock);
2247
Milan Broz28513fc2010-08-12 04:14:06 +01002248 /* Must zero key material before freeing */
2249 kzfree(cc);
Mikulas Patocka50593532017-08-13 22:45:08 -04002250
2251 spin_lock(&dm_crypt_clients_lock);
2252 WARN_ON(!dm_crypt_clients_n);
2253 dm_crypt_clients_n--;
2254 crypt_calculate_pages_per_client();
2255 spin_unlock(&dm_crypt_clients_lock);
Milan Broz28513fc2010-08-12 04:14:06 +01002256}
2257
Milan Broze889f972017-03-16 15:39:39 +01002258static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259{
Milan Broz5ebaee62010-08-12 04:14:07 +01002260 struct crypt_config *cc = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Milan Broz33d2f092017-03-16 15:39:40 +01002262 if (crypt_integrity_aead(cc))
Milan Broze889f972017-03-16 15:39:39 +01002263 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2264 else
2265 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Milan Broz5ebaee62010-08-12 04:14:07 +01002267 if (cc->iv_size)
2268 /* at least a 64 bit sector number should fit in our buffer */
2269 cc->iv_size = max(cc->iv_size,
2270 (unsigned int)(sizeof(u64) / sizeof(u8)));
2271 else if (ivmode) {
2272 DMWARN("Selected cipher does not support IVs");
2273 ivmode = NULL;
2274 }
2275
2276 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (ivmode == NULL)
2278 cc->iv_gen_ops = NULL;
2279 else if (strcmp(ivmode, "plain") == 0)
2280 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00002281 else if (strcmp(ivmode, "plain64") == 0)
2282 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Milan Broz7e3fd852017-06-06 09:07:01 +02002283 else if (strcmp(ivmode, "plain64be") == 0)
2284 cc->iv_gen_ops = &crypt_iv_plain64be_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 else if (strcmp(ivmode, "essiv") == 0)
2286 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10002287 else if (strcmp(ivmode, "benbi") == 0)
2288 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07002289 else if (strcmp(ivmode, "null") == 0)
2290 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00002291 else if (strcmp(ivmode, "lmk") == 0) {
2292 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01002293 /*
2294 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00002295 * to length of provided multi-key string.
2296 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01002297 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00002298 */
Milan Brozda31a072013-10-28 23:21:03 +01002299 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00002300 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01002301 cc->key_extra_size = cc->key_size / cc->key_parts;
2302 }
Milan Brozed04d982013-10-28 23:21:04 +01002303 } else if (strcmp(ivmode, "tcw") == 0) {
2304 cc->iv_gen_ops = &crypt_iv_tcw_ops;
2305 cc->key_parts += 2; /* IV + whitening */
2306 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broze889f972017-03-16 15:39:39 +01002307 } else if (strcmp(ivmode, "random") == 0) {
2308 cc->iv_gen_ops = &crypt_iv_random_ops;
2309 /* Need storage space in integrity fields. */
2310 cc->integrity_iv_size = cc->iv_size;
Milan Broz34745782011-01-13 19:59:55 +00002311 } else {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002312 ti->error = "Invalid IV mode";
Milan Broze889f972017-03-16 15:39:39 +01002313 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
2315
Milan Broze889f972017-03-16 15:39:39 +01002316 return 0;
2317}
2318
Milan Broz33d2f092017-03-16 15:39:40 +01002319/*
2320 * Workaround to parse cipher algorithm from crypto API spec.
2321 * The cc->cipher is currently used only in ESSIV.
2322 * This should be probably done by crypto-api calls (once available...)
2323 */
2324static int crypt_ctr_blkdev_cipher(struct crypt_config *cc)
2325{
2326 const char *alg_name = NULL;
2327 char *start, *end;
2328
2329 if (crypt_integrity_aead(cc)) {
2330 alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc)));
2331 if (!alg_name)
2332 return -EINVAL;
2333 if (crypt_integrity_hmac(cc)) {
2334 alg_name = strchr(alg_name, ',');
2335 if (!alg_name)
2336 return -EINVAL;
2337 }
2338 alg_name++;
2339 } else {
2340 alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc)));
2341 if (!alg_name)
2342 return -EINVAL;
2343 }
2344
2345 start = strchr(alg_name, '(');
2346 end = strchr(alg_name, ')');
2347
2348 if (!start && !end) {
2349 cc->cipher = kstrdup(alg_name, GFP_KERNEL);
2350 return cc->cipher ? 0 : -ENOMEM;
2351 }
2352
2353 if (!start || !end || ++start >= end)
2354 return -EINVAL;
2355
2356 cc->cipher = kzalloc(end - start + 1, GFP_KERNEL);
2357 if (!cc->cipher)
2358 return -ENOMEM;
2359
2360 strncpy(cc->cipher, start, end - start);
2361
2362 return 0;
2363}
2364
2365/*
2366 * Workaround to parse HMAC algorithm from AEAD crypto API spec.
2367 * The HMAC is needed to calculate tag size (HMAC digest size).
2368 * This should be probably done by crypto-api calls (once available...)
2369 */
2370static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
2371{
2372 char *start, *end, *mac_alg = NULL;
2373 struct crypto_ahash *mac;
2374
2375 if (!strstarts(cipher_api, "authenc("))
2376 return 0;
2377
2378 start = strchr(cipher_api, '(');
2379 end = strchr(cipher_api, ',');
2380 if (!start || !end || ++start > end)
2381 return -EINVAL;
2382
2383 mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
2384 if (!mac_alg)
2385 return -ENOMEM;
2386 strncpy(mac_alg, start, end - start);
2387
2388 mac = crypto_alloc_ahash(mac_alg, 0, 0);
2389 kfree(mac_alg);
2390
2391 if (IS_ERR(mac))
2392 return PTR_ERR(mac);
2393
2394 cc->key_mac_size = crypto_ahash_digestsize(mac);
2395 crypto_free_ahash(mac);
2396
2397 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
2398 if (!cc->authenc_key)
2399 return -ENOMEM;
2400
2401 return 0;
2402}
2403
2404static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key,
2405 char **ivmode, char **ivopts)
Milan Broz5ebaee62010-08-12 04:14:07 +01002406{
2407 struct crypt_config *cc = ti->private;
Milan Broz33d2f092017-03-16 15:39:40 +01002408 char *tmp, *cipher_api;
2409 int ret = -EINVAL;
2410
2411 cc->tfms_count = 1;
2412
2413 /*
2414 * New format (capi: prefix)
2415 * capi:cipher_api_spec-iv:ivopts
2416 */
2417 tmp = &cipher_in[strlen("capi:")];
Milan Broz1856b9f2019-01-09 11:57:14 +01002418
2419 /* Separate IV options if present, it can contain another '-' in hash name */
2420 *ivopts = strrchr(tmp, ':');
2421 if (*ivopts) {
2422 **ivopts = '\0';
2423 (*ivopts)++;
2424 }
2425 /* Parse IV mode */
2426 *ivmode = strrchr(tmp, '-');
2427 if (*ivmode) {
2428 **ivmode = '\0';
2429 (*ivmode)++;
2430 }
2431 /* The rest is crypto API spec */
2432 cipher_api = tmp;
Milan Broz33d2f092017-03-16 15:39:40 +01002433
2434 if (*ivmode && !strcmp(*ivmode, "lmk"))
2435 cc->tfms_count = 64;
2436
2437 cc->key_parts = cc->tfms_count;
2438
2439 /* Allocate cipher */
2440 ret = crypt_alloc_tfms(cc, cipher_api);
2441 if (ret < 0) {
2442 ti->error = "Error allocating crypto tfm";
2443 return ret;
2444 }
2445
2446 /* Alloc AEAD, can be used only in new format. */
2447 if (crypt_integrity_aead(cc)) {
2448 ret = crypt_ctr_auth_cipher(cc, cipher_api);
2449 if (ret < 0) {
2450 ti->error = "Invalid AEAD cipher spec";
2451 return -ENOMEM;
2452 }
2453 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2454 } else
2455 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2456
2457 ret = crypt_ctr_blkdev_cipher(cc);
2458 if (ret < 0) {
2459 ti->error = "Cannot allocate cipher string";
2460 return -ENOMEM;
2461 }
2462
2463 return 0;
2464}
2465
2466static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
2467 char **ivmode, char **ivopts)
2468{
2469 struct crypt_config *cc = ti->private;
2470 char *tmp, *cipher, *chainmode, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01002471 char *cipher_api = NULL;
2472 int ret = -EINVAL;
2473 char dummy;
2474
Milan Broz33d2f092017-03-16 15:39:40 +01002475 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002476 ti->error = "Bad cipher specification";
2477 return -EINVAL;
2478 }
2479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 /*
Milan Broz5ebaee62010-08-12 04:14:07 +01002481 * Legacy dm-crypt cipher specification
2482 * cipher[:keycount]-mode-iv:ivopts
2483 */
2484 tmp = cipher_in;
2485 keycount = strsep(&tmp, "-");
2486 cipher = strsep(&keycount, ":");
2487
Milan Broz69a8cfc2011-01-13 19:59:49 +00002488 if (!keycount)
Milan Broz5ebaee62010-08-12 04:14:07 +01002489 cc->tfms_count = 1;
2490 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
2491 !is_power_of_2(cc->tfms_count)) {
2492 ti->error = "Bad cipher key count specification";
2493 return -EINVAL;
2494 }
Milan Broz28513fc2010-08-12 04:14:06 +01002495 cc->key_parts = cc->tfms_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002497 cc->cipher = kstrdup(cipher, GFP_KERNEL);
Milan Broz28513fc2010-08-12 04:14:06 +01002498 if (!cc->cipher)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 goto bad_mem;
2500
Milan Brozddd42ed2008-02-08 02:11:07 +00002501 chainmode = strsep(&tmp, "-");
Milan Broz1856b9f2019-01-09 11:57:14 +01002502 *ivmode = strsep(&tmp, ":");
2503 *ivopts = tmp;
Milan Brozddd42ed2008-02-08 02:11:07 +00002504
2505 /*
2506 * For compatibility with the original dm-crypt mapping format, if
2507 * only the cipher name is supplied, use cbc-plain.
Milan Broz28513fc2010-08-12 04:14:06 +01002508 */
Milan Broz33d2f092017-03-16 15:39:40 +01002509 if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002510 chainmode = "cbc";
Milan Broz33d2f092017-03-16 15:39:40 +01002511 *ivmode = "plain";
Milan Brozcabf08e2007-10-19 22:38:58 +01002512 }
2513
Milan Broz33d2f092017-03-16 15:39:40 +01002514 if (strcmp(chainmode, "ecb") && !*ivmode) {
Andi Kleenc0297722011-01-13 19:59:53 +00002515 ti->error = "IV mechanism required";
2516 return -EINVAL;
2517 }
2518
Milan Brozcabf08e2007-10-19 22:38:58 +01002519 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
Milan Broz9934a8b2007-10-19 22:38:57 +01002520 if (!cipher_api)
Milan Broz28513fc2010-08-12 04:14:06 +01002521 goto bad_mem;
Milan Broz9934a8b2007-10-19 22:38:57 +01002522
2523 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002524 "%s(%s)", chainmode, cipher);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (ret < 0) {
2526 kfree(cipher_api);
Milan Broz28513fc2010-08-12 04:14:06 +01002527 goto bad_mem;
2528 }
2529
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 /* Allocate cipher */
2531 ret = crypt_alloc_tfms(cc, cipher_api);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (ret < 0) {
2533 ti->error = "Error allocating crypto tfm";
Milan Broz33d2f092017-03-16 15:39:40 +01002534 kfree(cipher_api);
2535 return ret;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002536 }
Jeffy Chenbd86e322017-09-27 20:28:57 +08002537 kfree(cipher_api);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002538
Milan Broz33d2f092017-03-16 15:39:40 +01002539 return 0;
2540bad_mem:
2541 ti->error = "Cannot allocate cipher strings";
2542 return -ENOMEM;
2543}
2544
2545static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key)
2546{
2547 struct crypt_config *cc = ti->private;
2548 char *ivmode = NULL, *ivopts = NULL;
2549 int ret;
2550
2551 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
2552 if (!cc->cipher_string) {
2553 ti->error = "Cannot allocate cipher strings";
2554 return -ENOMEM;
2555 }
2556
2557 if (strstarts(cipher_in, "capi:"))
2558 ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts);
2559 else
2560 ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts);
2561 if (ret)
2562 return ret;
2563
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002564 /* Initialize IV */
Milan Broze889f972017-03-16 15:39:39 +01002565 ret = crypt_ctr_ivmode(ti, ivmode);
2566 if (ret < 0)
Milan Broz33d2f092017-03-16 15:39:40 +01002567 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568
Milan Brozda31a072013-10-28 23:21:03 +01002569 /* Initialize and set key */
2570 ret = crypt_set_key(cc, key);
2571 if (ret < 0) {
2572 ti->error = "Error decoding and setting key";
Milan Broz33d2f092017-03-16 15:39:40 +01002573 return ret;
Milan Brozda31a072013-10-28 23:21:03 +01002574 }
2575
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 /* Allocate IV */
2577 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
2578 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
2579 if (ret < 0) {
2580 ti->error = "Error creating IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002581 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 }
2583 }
2584
2585 /* Initialize IV (set keys for ESSIV etc) */
2586 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
2587 ret = cc->iv_gen_ops->init(cc);
2588 if (ret < 0) {
2589 ti->error = "Error initialising IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002590 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 }
2592 }
2593
Ondrej Kozinadc949022018-01-12 16:30:32 +01002594 /* wipe the kernel key payload copy */
2595 if (cc->key_string)
2596 memset(cc->key, 0, cc->key_size * sizeof(u8));
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Milan Brozef43aa32017-01-04 20:23:54 +01002601static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
2602{
2603 struct crypt_config *cc = ti->private;
2604 struct dm_arg_set as;
Eric Biggers5916a222017-06-22 11:32:45 -07002605 static const struct dm_arg _args[] = {
Milan Broz8f0009a2017-03-16 15:39:44 +01002606 {0, 6, "Invalid number of feature args"},
Milan Brozef43aa32017-01-04 20:23:54 +01002607 };
2608 unsigned int opt_params, val;
2609 const char *opt_string, *sval;
Milan Broz8f0009a2017-03-16 15:39:44 +01002610 char dummy;
Milan Brozef43aa32017-01-04 20:23:54 +01002611 int ret;
2612
2613 /* Optional parameters */
2614 as.argc = argc;
2615 as.argv = argv;
2616
2617 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2618 if (ret)
2619 return ret;
2620
2621 while (opt_params--) {
2622 opt_string = dm_shift_arg(&as);
2623 if (!opt_string) {
2624 ti->error = "Not enough feature arguments";
2625 return -EINVAL;
2626 }
2627
2628 if (!strcasecmp(opt_string, "allow_discards"))
2629 ti->num_discard_bios = 1;
2630
2631 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
2632 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
2633
2634 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
2635 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
2636 else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
2637 if (val == 0 || val > MAX_TAG_SIZE) {
2638 ti->error = "Invalid integrity arguments";
2639 return -EINVAL;
2640 }
2641 cc->on_disk_tag_size = val;
2642 sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
2643 if (!strcasecmp(sval, "aead")) {
2644 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002645 } else if (strcasecmp(sval, "none")) {
2646 ti->error = "Unknown integrity profile";
2647 return -EINVAL;
2648 }
2649
2650 cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
2651 if (!cc->cipher_auth)
2652 return -ENOMEM;
Mikulas Patockaff3af922017-03-23 10:23:14 -04002653 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002654 if (cc->sector_size < (1 << SECTOR_SHIFT) ||
2655 cc->sector_size > 4096 ||
Mikulas Patockaff3af922017-03-23 10:23:14 -04002656 (cc->sector_size & (cc->sector_size - 1))) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002657 ti->error = "Invalid feature value for sector_size";
2658 return -EINVAL;
2659 }
Milan Broz783874b2017-09-13 15:45:56 +02002660 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) {
2661 ti->error = "Device size is not multiple of sector_size feature";
2662 return -EINVAL;
2663 }
Mikulas Patockaff3af922017-03-23 10:23:14 -04002664 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT;
Milan Broz8f0009a2017-03-16 15:39:44 +01002665 } else if (!strcasecmp(opt_string, "iv_large_sectors"))
2666 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
2667 else {
Milan Brozef43aa32017-01-04 20:23:54 +01002668 ti->error = "Invalid feature arguments";
2669 return -EINVAL;
2670 }
2671 }
2672
2673 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
2675
2676/*
2677 * Construct an encryption mapping:
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002678 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 */
2680static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2681{
2682 struct crypt_config *cc;
Michał Mirosławed0302e2018-10-09 22:13:43 +02002683 const char *devname = dm_table_device_name(ti->table);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002684 int key_size;
Milan Brozef43aa32017-01-04 20:23:54 +01002685 unsigned int align_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 unsigned long long tmpll;
2687 int ret;
Milan Brozef43aa32017-01-04 20:23:54 +01002688 size_t iv_size_padding, additional_req_size;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002689 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Milan Broz772ae5f2011-08-02 12:32:08 +01002691 if (argc < 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 ti->error = "Not enough arguments";
2693 return -EINVAL;
2694 }
2695
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002696 key_size = get_key_size(&argv[1]);
2697 if (key_size < 0) {
2698 ti->error = "Cannot parse key size";
2699 return -EINVAL;
2700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
2702 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
2703 if (!cc) {
2704 ti->error = "Cannot allocate encryption context";
2705 return -ENOMEM;
2706 }
2707 cc->key_size = key_size;
Milan Broz8f0009a2017-03-16 15:39:44 +01002708 cc->sector_size = (1 << SECTOR_SHIFT);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002709 cc->sector_shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711 ti->private = cc;
Milan Brozef43aa32017-01-04 20:23:54 +01002712
Mikulas Patocka50593532017-08-13 22:45:08 -04002713 spin_lock(&dm_crypt_clients_lock);
2714 dm_crypt_clients_n++;
2715 crypt_calculate_pages_per_client();
2716 spin_unlock(&dm_crypt_clients_lock);
2717
2718 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL);
2719 if (ret < 0)
2720 goto bad;
2721
Milan Brozef43aa32017-01-04 20:23:54 +01002722 /* Optional parameters need to be read before cipher constructor */
2723 if (argc > 5) {
2724 ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
2725 if (ret)
2726 goto bad;
2727 }
2728
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
2730 if (ret < 0)
2731 goto bad;
2732
Milan Broz33d2f092017-03-16 15:39:40 +01002733 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +01002734 cc->dmreq_start = sizeof(struct aead_request);
2735 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
2736 align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
2737 } else {
2738 cc->dmreq_start = sizeof(struct skcipher_request);
2739 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
2740 align_mask = crypto_skcipher_alignmask(any_tfm(cc));
2741 }
Mikulas Patockad49ec522014-08-28 11:09:31 -04002742 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
2743
Milan Brozef43aa32017-01-04 20:23:54 +01002744 if (align_mask < CRYPTO_MINALIGN) {
Mikulas Patockad49ec522014-08-28 11:09:31 -04002745 /* Allocate the padding exactly */
2746 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
Milan Brozef43aa32017-01-04 20:23:54 +01002747 & align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002748 } else {
2749 /*
2750 * If the cipher requires greater alignment than kmalloc
2751 * alignment, we don't know the exact position of the
2752 * initialization vector. We must assume worst case.
2753 */
Milan Brozef43aa32017-01-04 20:23:54 +01002754 iv_size_padding = align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Milan Brozef43aa32017-01-04 20:23:54 +01002757 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
2758 additional_req_size = sizeof(struct dm_crypt_request) +
2759 iv_size_padding + cc->iv_size +
2760 cc->iv_size +
2761 sizeof(uint64_t) +
2762 sizeof(unsigned int);
2763
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002764 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size);
2765 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 ti->error = "Cannot allocate crypt request mempool";
2767 goto bad;
2768 }
2769
Mike Snitzer30187e12016-01-31 13:28:26 -05002770 cc->per_bio_data_size = ti->per_io_data_size =
Milan Brozef43aa32017-01-04 20:23:54 +01002771 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
Mikulas Patockad49ec522014-08-28 11:09:31 -04002772 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002773
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002774 ret = mempool_init(&cc->page_pool, BIO_MAX_PAGES, crypt_page_alloc, crypt_page_free, cc);
2775 if (ret) {
Milan Broz8b004452006-10-03 01:15:37 -07002776 ti->error = "Cannot allocate page mempool";
Milan Broze48d4bb2006-10-03 01:15:37 -07002777 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002779
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002780 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS);
2781 if (ret) {
Milan Broz0c395b02008-02-08 02:10:54 +00002782 ti->error = "Cannot allocate crypt bioset";
Milan Brozcabf08e2007-10-19 22:38:58 +01002783 goto bad;
Milan Broz93e605c2006-10-03 01:15:38 -07002784 }
Milan Brozcabf08e2007-10-19 22:38:58 +01002785
Mikulas Patocka7145c242015-02-13 08:24:41 -05002786 mutex_init(&cc->bio_alloc_lock);
2787
Milan Brozcabf08e2007-10-19 22:38:58 +01002788 ret = -EINVAL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002789 if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
2790 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002791 ti->error = "Invalid iv_offset sector";
2792 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002794 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Vivek Goyale80d1c82015-07-31 09:20:36 -04002796 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
2797 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 ti->error = "Device lookup failed";
2799 goto bad;
2800 }
2801
Vivek Goyale80d1c82015-07-31 09:20:36 -04002802 ret = -EINVAL;
Milan Brozef87bfc2018-11-07 22:24:55 +01002803 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 ti->error = "Invalid device sector";
2805 goto bad;
2806 }
2807 cc->start = tmpll;
2808
Milan Broz33d2f092017-03-16 15:39:40 +01002809 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
Milan Brozef43aa32017-01-04 20:23:54 +01002810 ret = crypt_integrity_ctr(cc, ti);
Milan Broz772ae5f2011-08-02 12:32:08 +01002811 if (ret)
2812 goto bad;
2813
Milan Brozef43aa32017-01-04 20:23:54 +01002814 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
2815 if (!cc->tag_pool_max_sectors)
2816 cc->tag_pool_max_sectors = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01002817
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002818 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS,
Milan Brozef43aa32017-01-04 20:23:54 +01002819 cc->tag_pool_max_sectors * cc->on_disk_tag_size);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002820 if (ret) {
Milan Brozef43aa32017-01-04 20:23:54 +01002821 ti->error = "Cannot allocate integrity tags mempool";
2822 goto bad;
Milan Broz772ae5f2011-08-02 12:32:08 +01002823 }
Mikulas Patocka583fe742017-04-18 16:51:54 -04002824
2825 cc->tag_pool_max_sectors <<= cc->sector_shift;
Milan Broz772ae5f2011-08-02 12:32:08 +01002826 }
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 ret = -ENOMEM;
Michał Mirosławed0302e2018-10-09 22:13:43 +02002829 cc->io_queue = alloc_workqueue("kcryptd_io/%s",
2830 WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM,
2831 1, devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 if (!cc->io_queue) {
2833 ti->error = "Couldn't create kcryptd io queue";
2834 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
Christophe Saout37af6562006-10-30 20:39:08 +01002836
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002837 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
Michał Mirosławed0302e2018-10-09 22:13:43 +02002838 cc->crypt_queue = alloc_workqueue("kcryptd/%s",
2839 WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM,
2840 1, devname);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002841 else
Michał Mirosławed0302e2018-10-09 22:13:43 +02002842 cc->crypt_queue = alloc_workqueue("kcryptd/%s",
Tim Murraya1b89132017-04-21 11:11:36 +02002843 WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
Michał Mirosławed0302e2018-10-09 22:13:43 +02002844 num_online_cpus(), devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 if (!cc->crypt_queue) {
2846 ti->error = "Couldn't create kcryptd queue";
2847 goto bad;
2848 }
2849
Mikulas Patockac7329ef2018-07-11 12:10:51 -04002850 spin_lock_init(&cc->write_thread_lock);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05002851 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05002852
Michał Mirosławed0302e2018-10-09 22:13:43 +02002853 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write/%s", devname);
Mikulas Patockadc267622015-02-13 08:25:59 -05002854 if (IS_ERR(cc->write_thread)) {
2855 ret = PTR_ERR(cc->write_thread);
2856 cc->write_thread = NULL;
2857 ti->error = "Couldn't spawn write thread";
2858 goto bad;
2859 }
2860 wake_up_process(cc->write_thread);
2861
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002862 ti->num_flush_bios = 1;
Milan Broz983c7db2011-09-25 23:26:21 +01002863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 return 0;
2865
2866bad:
2867 crypt_dtr(ti);
2868 return ret;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002869}
2870
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002871static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
2873 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01002874 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002875
Milan Broz772ae5f2011-08-02 12:32:08 +01002876 /*
Mike Christie28a8f0d2016-06-05 14:32:25 -05002877 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
2878 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
Mike Christiee6047142016-06-05 14:32:04 -05002879 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
Milan Broz772ae5f2011-08-02 12:32:08 +01002880 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06002881 if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
Mike Christie28a8f0d2016-06-05 14:32:25 -05002882 bio_op(bio) == REQ_OP_DISCARD)) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02002883 bio_set_dev(bio, cc->dev->bdev);
Milan Broz772ae5f2011-08-02 12:32:08 +01002884 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07002885 bio->bi_iter.bi_sector = cc->start +
2886 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002887 return DM_MAPIO_REMAPPED;
2888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002890 /*
2891 * Check if bio is too large, split as needed.
2892 */
2893 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
Milan Brozef43aa32017-01-04 20:23:54 +01002894 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002895 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
2896
Milan Broz8f0009a2017-03-16 15:39:44 +01002897 /*
2898 * Ensure that bio is a multiple of internal sector encryption size
2899 * and is aligned to this size as defined in IO hints.
2900 */
2901 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
Christoph Hellwig846785e2017-06-03 09:38:02 +02002902 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002903
2904 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
Christoph Hellwig846785e2017-06-03 09:38:02 +02002905 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002906
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002907 io = dm_per_bio_data(bio, cc->per_bio_data_size);
2908 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Milan Brozef43aa32017-01-04 20:23:54 +01002909
2910 if (cc->on_disk_tag_size) {
Mikulas Patocka583fe742017-04-18 16:51:54 -04002911 unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift);
Milan Brozef43aa32017-01-04 20:23:54 +01002912
2913 if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
Mikulas Patocka583fe742017-04-18 16:51:54 -04002914 unlikely(!(io->integrity_metadata = kmalloc(tag_len,
Milan Brozef43aa32017-01-04 20:23:54 +01002915 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
2916 if (bio_sectors(bio) > cc->tag_pool_max_sectors)
2917 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002918 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO);
Milan Brozef43aa32017-01-04 20:23:54 +01002919 io->integrity_metadata_from_pool = true;
2920 }
2921 }
2922
Milan Broz33d2f092017-03-16 15:39:40 +01002923 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002924 io->ctx.r.req_aead = (struct aead_request *)(io + 1);
2925 else
2926 io->ctx.r.req = (struct skcipher_request *)(io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Milan Broz20c82532011-01-13 19:59:53 +00002928 if (bio_data_dir(io->base_bio) == READ) {
2929 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05002930 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00002931 } else
Andrew Morton4ee218c2006-03-27 01:17:48 -08002932 kcryptd_queue_crypt(io);
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 return DM_MAPIO_SUBMITTED;
2935}
2936
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002937static void crypt_status(struct dm_target *ti, status_type_t type,
2938 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
Milan Broz5ebaee62010-08-12 04:14:07 +01002940 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002941 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002942 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
2944 switch (type) {
2945 case STATUSTYPE_INFO:
2946 result[0] = '\0';
2947 break;
2948
2949 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00002950 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002952 if (cc->key_size > 0) {
2953 if (cc->key_string)
2954 DMEMIT(":%u:%s", cc->key_size, cc->key_string);
2955 else
2956 for (i = 0; i < cc->key_size; i++)
2957 DMEMIT("%02x", cc->key[i]);
2958 } else
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002959 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
2961 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
2962 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01002963
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002964 num_feature_args += !!ti->num_discard_bios;
2965 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002966 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002967 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT);
Milan Broz8f0009a2017-03-16 15:39:44 +01002968 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002969 if (cc->on_disk_tag_size)
2970 num_feature_args++;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002971 if (num_feature_args) {
2972 DMEMIT(" %d", num_feature_args);
2973 if (ti->num_discard_bios)
2974 DMEMIT(" allow_discards");
2975 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2976 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002977 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
2978 DMEMIT(" submit_from_crypt_cpus");
Milan Brozef43aa32017-01-04 20:23:54 +01002979 if (cc->on_disk_tag_size)
2980 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
Milan Broz8f0009a2017-03-16 15:39:44 +01002981 if (cc->sector_size != (1 << SECTOR_SHIFT))
2982 DMEMIT(" sector_size:%d", cc->sector_size);
2983 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
2984 DMEMIT(" iv_large_sectors");
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002985 }
Milan Broz772ae5f2011-08-02 12:32:08 +01002986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 break;
2988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
Milan Broze48d4bb2006-10-03 01:15:37 -07002991static void crypt_postsuspend(struct dm_target *ti)
2992{
2993 struct crypt_config *cc = ti->private;
2994
2995 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2996}
2997
2998static int crypt_preresume(struct dm_target *ti)
2999{
3000 struct crypt_config *cc = ti->private;
3001
3002 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
3003 DMERR("aborting resume - crypt key is not set.");
3004 return -EAGAIN;
3005 }
3006
3007 return 0;
3008}
3009
3010static void crypt_resume(struct dm_target *ti)
3011{
3012 struct crypt_config *cc = ti->private;
3013
3014 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
3015}
3016
3017/* Message interface
3018 * key set <key>
3019 * key wipe
3020 */
Mike Snitzer1eb5fa82018-02-28 15:59:59 -05003021static int crypt_message(struct dm_target *ti, unsigned argc, char **argv,
3022 char *result, unsigned maxlen)
Milan Broze48d4bb2006-10-03 01:15:37 -07003023{
3024 struct crypt_config *cc = ti->private;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01003025 int key_size, ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07003026
3027 if (argc < 2)
3028 goto error;
3029
Mike Snitzer498f0102011-08-02 12:32:04 +01003030 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07003031 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
3032 DMWARN("not suspended during key manipulation.");
3033 return -EINVAL;
3034 }
Mike Snitzer498f0102011-08-02 12:32:04 +01003035 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01003036 /* The key size may not be changed. */
3037 key_size = get_key_size(&argv[2]);
3038 if (key_size < 0 || cc->key_size != key_size) {
3039 memset(argv[2], '0', strlen(argv[2]));
3040 return -EINVAL;
3041 }
3042
Milan Broz542da312009-12-10 23:51:57 +00003043 ret = crypt_set_key(cc, argv[2]);
3044 if (ret)
3045 return ret;
3046 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
3047 ret = cc->iv_gen_ops->init(cc);
Ondrej Kozinadc949022018-01-12 16:30:32 +01003048 /* wipe the kernel key payload copy */
3049 if (cc->key_string)
3050 memset(cc->key, 0, cc->key_size * sizeof(u8));
Milan Broz542da312009-12-10 23:51:57 +00003051 return ret;
3052 }
Mike Snitzer498f0102011-08-02 12:32:04 +01003053 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00003054 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
3055 ret = cc->iv_gen_ops->wipe(cc);
3056 if (ret)
3057 return ret;
3058 }
Milan Broze48d4bb2006-10-03 01:15:37 -07003059 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00003060 }
Milan Broze48d4bb2006-10-03 01:15:37 -07003061 }
3062
3063error:
3064 DMWARN("unrecognised message received.");
3065 return -EINVAL;
3066}
3067
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003068static int crypt_iterate_devices(struct dm_target *ti,
3069 iterate_devices_callout_fn fn, void *data)
3070{
3071 struct crypt_config *cc = ti->private;
3072
Mike Snitzer5dea2712009-07-23 20:30:42 +01003073 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003074}
3075
Mike Snitzer586b2862015-09-09 21:34:51 -04003076static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
3077{
Milan Broz8f0009a2017-03-16 15:39:44 +01003078 struct crypt_config *cc = ti->private;
3079
Mike Snitzer586b2862015-09-09 21:34:51 -04003080 /*
3081 * Unfortunate constraint that is required to avoid the potential
3082 * for exceeding underlying device's max_segments limits -- due to
3083 * crypt_alloc_buffer() possibly allocating pages for the encryption
3084 * bio that are not as physically contiguous as the original bio.
3085 */
3086 limits->max_segment_size = PAGE_SIZE;
Milan Broz8f0009a2017-03-16 15:39:44 +01003087
Mikulas Patockabc9e9cf2018-08-10 11:23:56 -04003088 limits->logical_block_size =
3089 max_t(unsigned short, limits->logical_block_size, cc->sector_size);
3090 limits->physical_block_size =
3091 max_t(unsigned, limits->physical_block_size, cc->sector_size);
3092 limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size);
Mike Snitzer586b2862015-09-09 21:34:51 -04003093}
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095static struct target_type crypt_target = {
3096 .name = "crypt",
Ondrej Kozinadc949022018-01-12 16:30:32 +01003097 .version = {1, 18, 1},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 .module = THIS_MODULE,
3099 .ctr = crypt_ctr,
3100 .dtr = crypt_dtr,
3101 .map = crypt_map,
3102 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07003103 .postsuspend = crypt_postsuspend,
3104 .preresume = crypt_preresume,
3105 .resume = crypt_resume,
3106 .message = crypt_message,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003107 .iterate_devices = crypt_iterate_devices,
Mike Snitzer586b2862015-09-09 21:34:51 -04003108 .io_hints = crypt_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109};
3110
3111static int __init dm_crypt_init(void)
3112{
3113 int r;
3114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05003116 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07003117 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 return r;
3120}
3121
3122static void __exit dm_crypt_exit(void)
3123{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00003124 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
3127module_init(dm_crypt_init);
3128module_exit(dm_crypt_exit);
3129
Jana Saoutbf142992014-06-24 14:27:04 -04003130MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
3132MODULE_LICENSE("GPL");