blob: f2ec882f96be4e63415e10f4ab6b3716d6774656 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jana Saoutbf142992014-06-24 14:27:04 -04002 * Copyright (C) 2003 Jana Saout <jana@saout.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
Milan Brozef43aa32017-01-04 20:23:54 +01004 * Copyright (C) 2006-2017 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2013-2017 Milan Broz <gmazyland@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This file is released under the GPL.
8 */
9
Milan Broz43d69032008-02-08 02:11:09 +000010#include <linux/completion.h>
Herbert Xud1806f62006-08-22 20:29:17 +100011#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
Ondrej Kozinac538f6e2016-11-21 15:58:51 +010015#include <linux/key.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/bio.h>
17#include <linux/blkdev.h>
18#include <linux/mempool.h>
19#include <linux/slab.h>
20#include <linux/crypto.h>
21#include <linux/workqueue.h>
Mikulas Patockadc267622015-02-13 08:25:59 -050022#include <linux/kthread.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070023#include <linux/backing-dev.h>
Arun Sharma600634972011-07-26 16:09:06 -070024#include <linux/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100025#include <linux/scatterlist.h>
Mikulas Patockab3c5fd32015-02-13 08:27:41 -050026#include <linux/rbtree.h>
Ondrej Kozina027c4312016-12-01 18:20:52 +010027#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100029#include <asm/unaligned.h>
Milan Broz34745782011-01-13 19:59:55 +000030#include <crypto/hash.h>
31#include <crypto/md5.h>
32#include <crypto/algapi.h>
Herbert Xubbdb23b2016-01-24 21:16:36 +080033#include <crypto/skcipher.h>
Milan Brozef43aa32017-01-04 20:23:54 +010034#include <crypto/aead.h>
35#include <crypto/authenc.h>
36#include <linux/rtnetlink.h> /* for struct rtattr and RTA macros only */
Ondrej Kozinac538f6e2016-11-21 15:58:51 +010037#include <keys/user-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Mikulas Patocka586e80e2008-10-21 17:44:59 +010039#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Alasdair G Kergon72d94862006-06-26 00:27:35 -070041#define DM_MSG_PREFIX "crypt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * context holding the current state of a multi-part conversion
45 */
46struct convert_context {
Milan Broz43d69032008-02-08 02:11:09 +000047 struct completion restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct bio *bio_in;
49 struct bio *bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -070050 struct bvec_iter iter_in;
51 struct bvec_iter iter_out;
AliOS system security4e26ee32018-11-05 15:31:42 +080052 u64 cc_sector;
Mikulas Patocka40b62292012-07-27 15:08:04 +010053 atomic_t cc_pending;
Milan Brozef43aa32017-01-04 20:23:54 +010054 union {
55 struct skcipher_request *req;
56 struct aead_request *req_aead;
57 } r;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Milan Broz53017032008-02-08 02:10:38 +000061/*
62 * per bio private data
63 */
64struct dm_crypt_io {
Alasdair G Kergon49a8a922012-07-27 15:08:05 +010065 struct crypt_config *cc;
Milan Broz53017032008-02-08 02:10:38 +000066 struct bio *base_bio;
Milan Brozef43aa32017-01-04 20:23:54 +010067 u8 *integrity_metadata;
68 bool integrity_metadata_from_pool;
Milan Broz53017032008-02-08 02:10:38 +000069 struct work_struct work;
70
71 struct convert_context ctx;
72
Mikulas Patocka40b62292012-07-27 15:08:04 +010073 atomic_t io_pending;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020074 blk_status_t error;
Milan Broz0c395b02008-02-08 02:10:54 +000075 sector_t sector;
Mikulas Patockadc267622015-02-13 08:25:59 -050076
Mikulas Patockab3c5fd32015-02-13 08:27:41 -050077 struct rb_node rb_node;
Mikulas Patocka298a9fa2014-03-28 15:51:55 -040078} CRYPTO_MINALIGN_ATTR;
Milan Broz53017032008-02-08 02:10:38 +000079
Milan Broz01482b72008-02-08 02:11:04 +000080struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000081 struct convert_context *ctx;
Milan Brozef43aa32017-01-04 20:23:54 +010082 struct scatterlist sg_in[4];
83 struct scatterlist sg_out[4];
AliOS system security4e26ee32018-11-05 15:31:42 +080084 u64 iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000085};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct crypt_config;
88
89struct crypt_iv_operations {
90 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010091 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000093 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000094 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc53272011-01-13 19:59:54 +000095 int (*generator)(struct crypt_config *cc, u8 *iv,
96 struct dm_crypt_request *dmreq);
97 int (*post)(struct crypt_config *cc, u8 *iv,
98 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Milan Broz60473592009-12-10 23:51:55 +0000101struct iv_essiv_private {
Kees Cookc07c88f2018-07-15 20:59:12 -0700102 struct crypto_shash *hash_tfm;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000103 u8 *salt;
Milan Broz60473592009-12-10 23:51:55 +0000104};
105
106struct iv_benbi_private {
107 int shift;
108};
109
Milan Broz34745782011-01-13 19:59:55 +0000110#define LMK_SEED_SIZE 64 /* hash + 0 */
111struct iv_lmk_private {
112 struct crypto_shash *hash_tfm;
113 u8 *seed;
114};
115
Milan Brozed04d982013-10-28 23:21:04 +0100116#define TCW_WHITENING_SIZE 16
117struct iv_tcw_private {
118 struct crypto_shash *crc32_tfm;
119 u8 *iv_seed;
120 u8 *whitening;
121};
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/*
124 * Crypt: maps a linear range of a block device
125 * and encrypts / decrypts at the same time.
126 */
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -0500127enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID,
Rabin Vincentf659b102016-09-21 16:22:29 +0200128 DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD };
Andi Kleenc0297722011-01-13 19:59:53 +0000129
Milan Brozef43aa32017-01-04 20:23:54 +0100130enum cipher_flags {
131 CRYPT_MODE_INTEGRITY_AEAD, /* Use authenticated mode for cihper */
Milan Broz8f0009a2017-03-16 15:39:44 +0100132 CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */
Milan Brozef43aa32017-01-04 20:23:54 +0100133};
134
Andi Kleenc0297722011-01-13 19:59:53 +0000135/*
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500136 * The fields in here must be read only after initialization.
Andi Kleenc0297722011-01-13 19:59:53 +0000137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138struct crypt_config {
139 struct dm_dev *dev;
140 sector_t start;
141
Mikulas Patocka50593532017-08-13 22:45:08 -0400142 struct percpu_counter n_allocated_pages;
143
Milan Brozcabf08e2007-10-19 22:38:58 +0100144 struct workqueue_struct *io_queue;
145 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700146
Mikulas Patockac7329ef2018-07-11 12:10:51 -0400147 spinlock_t write_thread_lock;
Mike Snitzer72d711c2018-05-22 18:26:20 -0400148 struct task_struct *write_thread;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -0500149 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -0500150
Milan Broz5ebaee62010-08-12 04:14:07 +0100151 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000152 char *cipher_string;
Milan Brozef43aa32017-01-04 20:23:54 +0100153 char *cipher_auth;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +0100154 char *key_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100155
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100156 const struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800157 union {
Milan Broz60473592009-12-10 23:51:55 +0000158 struct iv_essiv_private essiv;
159 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000160 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100161 struct iv_tcw_private tcw;
Herbert Xu79066ad2006-12-05 13:41:52 -0800162 } iv_gen_private;
AliOS system security4e26ee32018-11-05 15:31:42 +0800163 u64 iv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 unsigned int iv_size;
Mikulas Patockaff3af922017-03-23 10:23:14 -0400165 unsigned short int sector_size;
166 unsigned char sector_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100168 /* ESSIV: struct crypto_cipher *essiv_tfm */
169 void *iv_private;
Milan Brozef43aa32017-01-04 20:23:54 +0100170 union {
171 struct crypto_skcipher **tfms;
172 struct crypto_aead **tfms_aead;
173 } cipher_tfm;
Milan Brozd1f96422011-01-13 19:59:54 +0000174 unsigned tfms_count;
Milan Brozef43aa32017-01-04 20:23:54 +0100175 unsigned long cipher_flags;
Andi Kleenc0297722011-01-13 19:59:53 +0000176
177 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000178 * Layout of each crypto request:
179 *
Herbert Xubbdb23b2016-01-24 21:16:36 +0800180 * struct skcipher_request
Milan Brozddd42ed2008-02-08 02:11:07 +0000181 * context
182 * padding
183 * struct dm_crypt_request
184 * padding
185 * IV
186 *
187 * The padding is added so that dm_crypt_request and the IV are
188 * correctly aligned.
189 */
190 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000191
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400192 unsigned int per_bio_data_size;
193
Milan Broze48d4bb2006-10-03 01:15:37 -0700194 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100196 unsigned int key_parts; /* independent parts in key buffer */
197 unsigned int key_extra_size; /* additional keys length */
Milan Brozef43aa32017-01-04 20:23:54 +0100198 unsigned int key_mac_size; /* MAC key size for authenc(...) */
199
200 unsigned int integrity_tag_size;
201 unsigned int integrity_iv_size;
202 unsigned int on_disk_tag_size;
203
Mike Snitzer72d711c2018-05-22 18:26:20 -0400204 /*
205 * pool for per bio private data, crypto requests,
206 * encryption requeusts/buffer pages and integrity tags
207 */
208 unsigned tag_pool_max_sectors;
209 mempool_t tag_pool;
210 mempool_t req_pool;
211 mempool_t page_pool;
212
213 struct bio_set bs;
214 struct mutex bio_alloc_lock;
215
Milan Brozef43aa32017-01-04 20:23:54 +0100216 u8 *authenc_key; /* space for keys in authenc() format (if used) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 u8 key[0];
218};
219
Milan Brozef43aa32017-01-04 20:23:54 +0100220#define MIN_IOS 64
221#define MAX_TAG_SIZE 480
222#define POOL_ENTRY_SIZE 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Mikulas Patocka50593532017-08-13 22:45:08 -0400224static DEFINE_SPINLOCK(dm_crypt_clients_lock);
225static unsigned dm_crypt_clients_n = 0;
226static volatile unsigned long dm_crypt_pages_per_client;
227#define DM_CRYPT_MEMORY_PERCENT 2
228#define DM_CRYPT_MIN_PAGES_PER_CLIENT (BIO_MAX_PAGES * 16)
229
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100230static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000231static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Brozef43aa32017-01-04 20:23:54 +0100232static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
233 struct scatterlist *sg);
Olaf Kirch027581f2007-05-09 02:32:52 -0700234
Andi Kleenc0297722011-01-13 19:59:53 +0000235/*
Eric Biggers86f917a2017-03-30 22:18:48 -0700236 * Use this to access cipher attributes that are independent of the key.
Andi Kleenc0297722011-01-13 19:59:53 +0000237 */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800238static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +0000239{
Milan Brozef43aa32017-01-04 20:23:54 +0100240 return cc->cipher_tfm.tfms[0];
241}
242
243static struct crypto_aead *any_tfm_aead(struct crypt_config *cc)
244{
245 return cc->cipher_tfm.tfms_aead[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000246}
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 * Different IV generation algorithms:
250 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000251 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200252 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *
Milan Broz61afef62009-12-10 23:52:25 +0000254 * plain64: the initial vector is the 64-bit little-endian version of the sector
255 * number, padded with zeros if necessary.
256 *
Milan Broz7e3fd852017-06-06 09:07:01 +0200257 * plain64be: the initial vector is the 64-bit big-endian version of the sector
258 * number, padded with zeros if necessary.
259 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000260 * essiv: "encrypted sector|salt initial vector", the sector number is
261 * encrypted with the bulk cipher using a salt as key. The salt
262 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 *
Rik Snel48527fa2006-09-03 08:56:39 +1000264 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
265 * (needed for LRW-32-AES and possible other narrow block modes)
266 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700267 * null: the initial vector is always zero. Provides compatibility with
268 * obsolete loop_fish2 devices. Do not use for new devices.
269 *
Milan Broz34745782011-01-13 19:59:55 +0000270 * lmk: Compatible implementation of the block chaining mode used
271 * by the Loop-AES block device encryption system
272 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
273 * It operates on full 512 byte sectors and uses CBC
274 * with an IV derived from the sector number, the data and
275 * optionally extra IV seed.
276 * This means that after decryption the first block
277 * of sector must be tweaked according to decrypted data.
278 * Loop-AES can use three encryption schemes:
279 * version 1: is plain aes-cbc mode
280 * version 2: uses 64 multikey scheme with lmk IV generator
281 * version 3: the same as version 2 with additional IV seed
282 * (it uses 65 keys, last key is used as IV seed)
283 *
Milan Brozed04d982013-10-28 23:21:04 +0100284 * tcw: Compatible implementation of the block chaining mode used
285 * by the TrueCrypt device encryption system (prior to version 4.1).
Milan Broze44f23b2015-04-05 18:03:10 +0200286 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
Milan Brozed04d982013-10-28 23:21:04 +0100287 * It operates on full 512 byte sectors and uses CBC
288 * with an IV derived from initial key and the sector number.
289 * In addition, whitening value is applied on every sector, whitening
290 * is calculated from initial key, sector number and mixed using CRC32.
291 * Note that this encryption scheme is vulnerable to watermarking attacks
292 * and should be used for old compatible containers access only.
293 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * plumb: unimplemented, see:
295 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
296 */
297
Milan Broz2dc53272011-01-13 19:59:54 +0000298static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
299 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
301 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100302 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 return 0;
305}
306
Milan Broz61afef62009-12-10 23:52:25 +0000307static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000308 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000309{
310 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100311 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000312
313 return 0;
314}
315
Milan Broz7e3fd852017-06-06 09:07:01 +0200316static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv,
317 struct dm_crypt_request *dmreq)
318{
319 memset(iv, 0, cc->iv_size);
320 /* iv_size is at least of size u64; usually it is 16 bytes */
321 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector);
322
323 return 0;
324}
325
Milan Brozb95bf2d2009-12-10 23:51:56 +0000326/* Initialise ESSIV - compute salt but no local memory allocations */
327static int crypt_iv_essiv_init(struct crypt_config *cc)
328{
329 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Kees Cookc07c88f2018-07-15 20:59:12 -0700330 SHASH_DESC_ON_STACK(desc, essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000331 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100332 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000333
Kees Cookc07c88f2018-07-15 20:59:12 -0700334 desc->tfm = essiv->hash_tfm;
Mikulas Patocka432061b2018-09-05 09:17:45 -0400335 desc->flags = 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000336
Kees Cookc07c88f2018-07-15 20:59:12 -0700337 err = crypto_shash_digest(desc, cc->key, cc->key_size, essiv->salt);
338 shash_desc_zero(desc);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000339 if (err)
340 return err;
341
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100342 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000343
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100344 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
Kees Cookc07c88f2018-07-15 20:59:12 -0700345 crypto_shash_digestsize(essiv->hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100346 if (err)
347 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000348
349 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000350}
351
Milan Broz542da312009-12-10 23:51:57 +0000352/* Wipe salt and reset key derived from volume key */
353static int crypt_iv_essiv_wipe(struct crypt_config *cc)
354{
355 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Kees Cookc07c88f2018-07-15 20:59:12 -0700356 unsigned salt_size = crypto_shash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000357 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100358 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000359
360 memset(essiv->salt, 0, salt_size);
361
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100362 essiv_tfm = cc->iv_private;
363 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
364 if (r)
365 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000366
367 return err;
368}
369
Eric Biggers86f917a2017-03-30 22:18:48 -0700370/* Allocate the cipher for ESSIV */
371static struct crypto_cipher *alloc_essiv_cipher(struct crypt_config *cc,
372 struct dm_target *ti,
373 const u8 *salt,
374 unsigned int saltsize)
Andi Kleenc0297722011-01-13 19:59:53 +0000375{
376 struct crypto_cipher *essiv_tfm;
377 int err;
378
379 /* Setup the essiv_tfm with the given salt */
380 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
381 if (IS_ERR(essiv_tfm)) {
382 ti->error = "Error allocating crypto tfm for ESSIV";
383 return essiv_tfm;
384 }
385
Milan Brozef43aa32017-01-04 20:23:54 +0100386 if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) {
Andi Kleenc0297722011-01-13 19:59:53 +0000387 ti->error = "Block size of ESSIV cipher does "
388 "not match IV size of block cipher";
389 crypto_free_cipher(essiv_tfm);
390 return ERR_PTR(-EINVAL);
391 }
392
393 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
394 if (err) {
395 ti->error = "Failed to set key for ESSIV cipher";
396 crypto_free_cipher(essiv_tfm);
397 return ERR_PTR(err);
398 }
399
400 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000401}
402
Milan Broz60473592009-12-10 23:51:55 +0000403static void crypt_iv_essiv_dtr(struct crypt_config *cc)
404{
Andi Kleenc0297722011-01-13 19:59:53 +0000405 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000406 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
407
Kees Cookc07c88f2018-07-15 20:59:12 -0700408 crypto_free_shash(essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000409 essiv->hash_tfm = NULL;
410
411 kzfree(essiv->salt);
412 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000413
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100414 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000415
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100416 if (essiv_tfm)
417 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000418
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100419 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100423 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
Milan Broz5861f1b2009-12-10 23:51:56 +0000425 struct crypto_cipher *essiv_tfm = NULL;
Kees Cookc07c88f2018-07-15 20:59:12 -0700426 struct crypto_shash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000427 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100428 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Milan Broz5861f1b2009-12-10 23:51:56 +0000430 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700431 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return -EINVAL;
433 }
434
Milan Brozb95bf2d2009-12-10 23:51:56 +0000435 /* Allocate hash algorithm */
Kees Cookc07c88f2018-07-15 20:59:12 -0700436 hash_tfm = crypto_alloc_shash(opts, 0, 0);
Herbert Xu35058682006-08-24 19:10:20 +1000437 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700438 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000439 err = PTR_ERR(hash_tfm);
440 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
442
Kees Cookc07c88f2018-07-15 20:59:12 -0700443 salt = kzalloc(crypto_shash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000444 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700445 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000446 err = -ENOMEM;
447 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
449
Milan Brozb95bf2d2009-12-10 23:51:56 +0000450 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000451 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
452
Eric Biggers86f917a2017-03-30 22:18:48 -0700453 essiv_tfm = alloc_essiv_cipher(cc, ti, salt,
Kees Cookc07c88f2018-07-15 20:59:12 -0700454 crypto_shash_digestsize(hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100455 if (IS_ERR(essiv_tfm)) {
456 crypt_iv_essiv_dtr(cc);
457 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000458 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100459 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000462
463bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000464 if (hash_tfm && !IS_ERR(hash_tfm))
Kees Cookc07c88f2018-07-15 20:59:12 -0700465 crypto_free_shash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000466 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000467 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Milan Broz2dc53272011-01-13 19:59:54 +0000470static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
471 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100473 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100476 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000477 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 return 0;
480}
481
Rik Snel48527fa2006-09-03 08:56:39 +1000482static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
483 const char *opts)
484{
Herbert Xubbdb23b2016-01-24 21:16:36 +0800485 unsigned bs = crypto_skcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800486 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000487
488 /* we need to calculate how far we must shift the sector count
489 * to get the cipher block count, we use this shift in _gen */
490
491 if (1 << log != bs) {
492 ti->error = "cypher blocksize is not a power of 2";
493 return -EINVAL;
494 }
495
496 if (log > 9) {
497 ti->error = "cypher blocksize is > 512";
498 return -EINVAL;
499 }
500
Milan Broz60473592009-12-10 23:51:55 +0000501 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000502
503 return 0;
504}
505
506static void crypt_iv_benbi_dtr(struct crypt_config *cc)
507{
Rik Snel48527fa2006-09-03 08:56:39 +1000508}
509
Milan Broz2dc53272011-01-13 19:59:54 +0000510static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
511 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000512{
Herbert Xu79066ad2006-12-05 13:41:52 -0800513 __be64 val;
514
Rik Snel48527fa2006-09-03 08:56:39 +1000515 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800516
Milan Broz2dc53272011-01-13 19:59:54 +0000517 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800518 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return 0;
521}
522
Milan Broz2dc53272011-01-13 19:59:54 +0000523static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
524 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700525{
526 memset(iv, 0, cc->iv_size);
527
528 return 0;
529}
530
Milan Broz34745782011-01-13 19:59:55 +0000531static void crypt_iv_lmk_dtr(struct crypt_config *cc)
532{
533 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
534
535 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
536 crypto_free_shash(lmk->hash_tfm);
537 lmk->hash_tfm = NULL;
538
539 kzfree(lmk->seed);
540 lmk->seed = NULL;
541}
542
543static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
544 const char *opts)
545{
546 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
547
Milan Broz8f0009a2017-03-16 15:39:44 +0100548 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
549 ti->error = "Unsupported sector size for LMK";
550 return -EINVAL;
551 }
552
Milan Broz34745782011-01-13 19:59:55 +0000553 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
554 if (IS_ERR(lmk->hash_tfm)) {
555 ti->error = "Error initializing LMK hash";
556 return PTR_ERR(lmk->hash_tfm);
557 }
558
559 /* No seed in LMK version 2 */
560 if (cc->key_parts == cc->tfms_count) {
561 lmk->seed = NULL;
562 return 0;
563 }
564
565 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
566 if (!lmk->seed) {
567 crypt_iv_lmk_dtr(cc);
568 ti->error = "Error kmallocing seed storage in LMK";
569 return -ENOMEM;
570 }
571
572 return 0;
573}
574
575static int crypt_iv_lmk_init(struct crypt_config *cc)
576{
577 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
578 int subkey_size = cc->key_size / cc->key_parts;
579
580 /* LMK seed is on the position of LMK_KEYS + 1 key */
581 if (lmk->seed)
582 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
583 crypto_shash_digestsize(lmk->hash_tfm));
584
585 return 0;
586}
587
588static int crypt_iv_lmk_wipe(struct crypt_config *cc)
589{
590 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
591
592 if (lmk->seed)
593 memset(lmk->seed, 0, LMK_SEED_SIZE);
594
595 return 0;
596}
597
598static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
599 struct dm_crypt_request *dmreq,
600 u8 *data)
601{
602 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200603 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000604 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100605 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000606 int i, r;
607
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200608 desc->tfm = lmk->hash_tfm;
Mikulas Patocka432061b2018-09-05 09:17:45 -0400609 desc->flags = 0;
Milan Broz34745782011-01-13 19:59:55 +0000610
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200611 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000612 if (r)
613 return r;
614
615 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200616 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000617 if (r)
618 return r;
619 }
620
621 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200622 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000623 if (r)
624 return r;
625
626 /* Sector is cropped to 56 bits here */
627 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
628 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
629 buf[2] = cpu_to_le32(4024);
630 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200631 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000632 if (r)
633 return r;
634
635 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200636 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000637 if (r)
638 return r;
639
640 for (i = 0; i < MD5_HASH_WORDS; i++)
641 __cpu_to_le32s(&md5state.hash[i]);
642 memcpy(iv, &md5state.hash, cc->iv_size);
643
644 return 0;
645}
646
647static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
648 struct dm_crypt_request *dmreq)
649{
Milan Brozef43aa32017-01-04 20:23:54 +0100650 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000651 u8 *src;
652 int r = 0;
653
654 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100655 sg = crypt_get_sg_data(cc, dmreq->sg_in);
656 src = kmap_atomic(sg_page(sg));
657 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800658 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000659 } else
660 memset(iv, 0, cc->iv_size);
661
662 return r;
663}
664
665static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
666 struct dm_crypt_request *dmreq)
667{
Milan Brozef43aa32017-01-04 20:23:54 +0100668 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000669 u8 *dst;
670 int r;
671
672 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
673 return 0;
674
Milan Brozef43aa32017-01-04 20:23:54 +0100675 sg = crypt_get_sg_data(cc, dmreq->sg_out);
676 dst = kmap_atomic(sg_page(sg));
677 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset);
Milan Broz34745782011-01-13 19:59:55 +0000678
679 /* Tweak the first block of plaintext sector */
680 if (!r)
Milan Brozef43aa32017-01-04 20:23:54 +0100681 crypto_xor(dst + sg->offset, iv, cc->iv_size);
Milan Broz34745782011-01-13 19:59:55 +0000682
Cong Wangc2e022c2011-11-28 13:26:02 +0800683 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000684 return r;
685}
686
Milan Brozed04d982013-10-28 23:21:04 +0100687static void crypt_iv_tcw_dtr(struct crypt_config *cc)
688{
689 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
690
691 kzfree(tcw->iv_seed);
692 tcw->iv_seed = NULL;
693 kzfree(tcw->whitening);
694 tcw->whitening = NULL;
695
696 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
697 crypto_free_shash(tcw->crc32_tfm);
698 tcw->crc32_tfm = NULL;
699}
700
701static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
702 const char *opts)
703{
704 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
705
Milan Broz8f0009a2017-03-16 15:39:44 +0100706 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
707 ti->error = "Unsupported sector size for TCW";
708 return -EINVAL;
709 }
710
Milan Brozed04d982013-10-28 23:21:04 +0100711 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
712 ti->error = "Wrong key size for TCW";
713 return -EINVAL;
714 }
715
716 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
717 if (IS_ERR(tcw->crc32_tfm)) {
718 ti->error = "Error initializing CRC32 in TCW";
719 return PTR_ERR(tcw->crc32_tfm);
720 }
721
722 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
723 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
724 if (!tcw->iv_seed || !tcw->whitening) {
725 crypt_iv_tcw_dtr(cc);
726 ti->error = "Error allocating seed storage in TCW";
727 return -ENOMEM;
728 }
729
730 return 0;
731}
732
733static int crypt_iv_tcw_init(struct crypt_config *cc)
734{
735 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
736 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
737
738 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
739 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
740 TCW_WHITENING_SIZE);
741
742 return 0;
743}
744
745static int crypt_iv_tcw_wipe(struct crypt_config *cc)
746{
747 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
748
749 memset(tcw->iv_seed, 0, cc->iv_size);
750 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
751
752 return 0;
753}
754
755static int crypt_iv_tcw_whitening(struct crypt_config *cc,
756 struct dm_crypt_request *dmreq,
757 u8 *data)
758{
759 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200760 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100761 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200762 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100763 int i, r;
764
765 /* xor whitening with sector number */
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100766 crypto_xor_cpy(buf, tcw->whitening, (u8 *)&sector, 8);
767 crypto_xor_cpy(&buf[8], tcw->whitening + 8, (u8 *)&sector, 8);
Milan Brozed04d982013-10-28 23:21:04 +0100768
769 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200770 desc->tfm = tcw->crc32_tfm;
Mikulas Patocka432061b2018-09-05 09:17:45 -0400771 desc->flags = 0;
Milan Brozed04d982013-10-28 23:21:04 +0100772 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200773 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100774 if (r)
775 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200776 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100777 if (r)
778 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200779 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100780 if (r)
781 goto out;
782 }
783 crypto_xor(&buf[0], &buf[12], 4);
784 crypto_xor(&buf[4], &buf[8], 4);
785
786 /* apply whitening (8 bytes) to whole sector */
787 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
788 crypto_xor(data + i * 8, buf, 8);
789out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100790 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100791 return r;
792}
793
794static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
795 struct dm_crypt_request *dmreq)
796{
Milan Brozef43aa32017-01-04 20:23:54 +0100797 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100798 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200799 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100800 u8 *src;
801 int r = 0;
802
803 /* Remove whitening from ciphertext */
804 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100805 sg = crypt_get_sg_data(cc, dmreq->sg_in);
806 src = kmap_atomic(sg_page(sg));
807 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100808 kunmap_atomic(src);
809 }
810
811 /* Calculate IV */
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100812 crypto_xor_cpy(iv, tcw->iv_seed, (u8 *)&sector, 8);
Milan Brozed04d982013-10-28 23:21:04 +0100813 if (cc->iv_size > 8)
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100814 crypto_xor_cpy(&iv[8], tcw->iv_seed + 8, (u8 *)&sector,
815 cc->iv_size - 8);
Milan Brozed04d982013-10-28 23:21:04 +0100816
817 return r;
818}
819
820static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
821 struct dm_crypt_request *dmreq)
822{
Milan Brozef43aa32017-01-04 20:23:54 +0100823 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100824 u8 *dst;
825 int r;
826
827 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
828 return 0;
829
830 /* Apply whitening on ciphertext */
Milan Brozef43aa32017-01-04 20:23:54 +0100831 sg = crypt_get_sg_data(cc, dmreq->sg_out);
832 dst = kmap_atomic(sg_page(sg));
833 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100834 kunmap_atomic(dst);
835
836 return r;
837}
838
Milan Brozef43aa32017-01-04 20:23:54 +0100839static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv,
840 struct dm_crypt_request *dmreq)
841{
842 /* Used only for writes, there must be an additional space to store IV */
843 get_random_bytes(iv, cc->iv_size);
844 return 0;
845}
846
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100847static const struct crypt_iv_operations crypt_iv_plain_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 .generator = crypt_iv_plain_gen
849};
850
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100851static const struct crypt_iv_operations crypt_iv_plain64_ops = {
Milan Broz61afef62009-12-10 23:52:25 +0000852 .generator = crypt_iv_plain64_gen
853};
854
Milan Broz7e3fd852017-06-06 09:07:01 +0200855static const struct crypt_iv_operations crypt_iv_plain64be_ops = {
856 .generator = crypt_iv_plain64be_gen
857};
858
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100859static const struct crypt_iv_operations crypt_iv_essiv_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 .ctr = crypt_iv_essiv_ctr,
861 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000862 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000863 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 .generator = crypt_iv_essiv_gen
865};
866
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100867static const struct crypt_iv_operations crypt_iv_benbi_ops = {
Rik Snel48527fa2006-09-03 08:56:39 +1000868 .ctr = crypt_iv_benbi_ctr,
869 .dtr = crypt_iv_benbi_dtr,
870 .generator = crypt_iv_benbi_gen
871};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100873static const struct crypt_iv_operations crypt_iv_null_ops = {
Ludwig Nussel46b47732007-05-09 02:32:55 -0700874 .generator = crypt_iv_null_gen
875};
876
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100877static const struct crypt_iv_operations crypt_iv_lmk_ops = {
Milan Broz34745782011-01-13 19:59:55 +0000878 .ctr = crypt_iv_lmk_ctr,
879 .dtr = crypt_iv_lmk_dtr,
880 .init = crypt_iv_lmk_init,
881 .wipe = crypt_iv_lmk_wipe,
882 .generator = crypt_iv_lmk_gen,
883 .post = crypt_iv_lmk_post
884};
885
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100886static const struct crypt_iv_operations crypt_iv_tcw_ops = {
Milan Brozed04d982013-10-28 23:21:04 +0100887 .ctr = crypt_iv_tcw_ctr,
888 .dtr = crypt_iv_tcw_dtr,
889 .init = crypt_iv_tcw_init,
890 .wipe = crypt_iv_tcw_wipe,
891 .generator = crypt_iv_tcw_gen,
892 .post = crypt_iv_tcw_post
893};
894
Milan Brozef43aa32017-01-04 20:23:54 +0100895static struct crypt_iv_operations crypt_iv_random_ops = {
896 .generator = crypt_iv_random_gen
897};
898
899/*
900 * Integrity extensions
901 */
902static bool crypt_integrity_aead(struct crypt_config *cc)
903{
904 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
905}
906
907static bool crypt_integrity_hmac(struct crypt_config *cc)
908{
Milan Broz33d2f092017-03-16 15:39:40 +0100909 return crypt_integrity_aead(cc) && cc->key_mac_size;
Milan Brozef43aa32017-01-04 20:23:54 +0100910}
911
912/* Get sg containing data */
913static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
914 struct scatterlist *sg)
915{
Milan Broz33d2f092017-03-16 15:39:40 +0100916 if (unlikely(crypt_integrity_aead(cc)))
Milan Brozef43aa32017-01-04 20:23:54 +0100917 return &sg[2];
918
919 return sg;
920}
921
922static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
923{
924 struct bio_integrity_payload *bip;
925 unsigned int tag_len;
926 int ret;
927
928 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size)
929 return 0;
930
931 bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
932 if (IS_ERR(bip))
933 return PTR_ERR(bip);
934
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;
1450
Kent Overstreetcb34e052012-09-05 15:22:02 -07001451 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 BUG_ON(!bv->bv_page);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001453 mempool_free(bv->bv_page, &cc->page_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455}
1456
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001457static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1458 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001459{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001460 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001461 io->base_bio = bio;
1462 io->sector = sector;
1463 io->error = 0;
Milan Brozef43aa32017-01-04 20:23:54 +01001464 io->ctx.r.req = NULL;
1465 io->integrity_metadata = NULL;
1466 io->integrity_metadata_from_pool = false;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001467 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001468}
1469
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001470static void crypt_inc_pending(struct dm_crypt_io *io)
1471{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001472 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001473}
1474
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475/*
1476 * One of the bios was finished. Check for completion of
1477 * the whole request and correctly clean up the buffer.
1478 */
Milan Broz5742fd72008-02-08 02:10:43 +00001479static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001481 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001482 struct bio *base_bio = io->base_bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001483 blk_status_t error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Mikulas Patocka40b62292012-07-27 15:08:04 +01001485 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return;
1487
Milan Brozef43aa32017-01-04 20:23:54 +01001488 if (io->ctx.r.req)
1489 crypt_free_req(cc, io->ctx.r.req, base_bio);
1490
1491 if (unlikely(io->integrity_metadata_from_pool))
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001492 mempool_free(io->integrity_metadata, &io->cc->tag_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01001493 else
1494 kfree(io->integrity_metadata);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001495
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001496 base_bio->bi_status = error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001497 bio_endio(base_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499
1500/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001501 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 *
1503 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001504 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001505 *
1506 * kcryptd performs the actual encryption or decryption.
1507 *
1508 * kcryptd_io performs the IO submission.
1509 *
1510 * They must be separated as otherwise the final stages could be
1511 * starved by new requests which can block in the first stages due
1512 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001513 *
1514 * The work is done per CPU global for all dm-crypt instances.
1515 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001517static void crypt_endio(struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001518{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001519 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001520 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001521 unsigned rw = bio_data_dir(clone);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001522 blk_status_t error;
Milan Broz8b004452006-10-03 01:15:37 -07001523
1524 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001525 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001526 */
Milan Brozee7a4912008-02-08 02:10:46 +00001527 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001528 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001529
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001530 error = clone->bi_status;
Milan Brozee7a4912008-02-08 02:10:46 +00001531 bio_put(clone);
1532
Sasha Levin9b81c842015-08-10 19:05:18 -04001533 if (rw == READ && !error) {
Milan Brozee7a4912008-02-08 02:10:46 +00001534 kcryptd_queue_crypt(io);
1535 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001536 }
Milan Broz8b004452006-10-03 01:15:37 -07001537
Sasha Levin9b81c842015-08-10 19:05:18 -04001538 if (unlikely(error))
1539 io->error = error;
Milan Broz5742fd72008-02-08 02:10:43 +00001540
1541 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001542}
1543
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001544static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001545{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001546 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001547
1548 clone->bi_private = io;
1549 clone->bi_end_io = crypt_endio;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001550 bio_set_dev(clone, cc->dev->bdev);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001551 clone->bi_opf = io->base_bio->bi_opf;
Milan Broz8b004452006-10-03 01:15:37 -07001552}
1553
Milan Broz20c82532011-01-13 19:59:53 +00001554static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001555{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001556 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001557 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001558
Milan Broz8b004452006-10-03 01:15:37 -07001559 /*
Mike Snitzer59779072015-04-09 16:53:24 -04001560 * We need the original biovec array in order to decrypt
1561 * the whole bio data *afterwards* -- thanks to immutable
1562 * biovecs we don't need to worry about the block layer
1563 * modifying the biovec array; so leverage bio_clone_fast().
Milan Broz8b004452006-10-03 01:15:37 -07001564 */
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001565 clone = bio_clone_fast(io->base_bio, gfp, &cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001566 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001567 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001568
Milan Broz20c82532011-01-13 19:59:53 +00001569 crypt_inc_pending(io);
1570
Milan Broz8b004452006-10-03 01:15:37 -07001571 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001572 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001573
Milan Brozef43aa32017-01-04 20:23:54 +01001574 if (dm_crypt_integrity_io_alloc(io, clone)) {
1575 crypt_dec_pending(io);
1576 bio_put(clone);
1577 return 1;
1578 }
1579
Milan Broz93e605c2006-10-03 01:15:38 -07001580 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001581 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001582}
1583
Mikulas Patockadc267622015-02-13 08:25:59 -05001584static void kcryptd_io_read_work(struct work_struct *work)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001585{
1586 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1587
Mikulas Patockadc267622015-02-13 08:25:59 -05001588 crypt_inc_pending(io);
1589 if (kcryptd_io_read(io, GFP_NOIO))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001590 io->error = BLK_STS_RESOURCE;
Mikulas Patockadc267622015-02-13 08:25:59 -05001591 crypt_dec_pending(io);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001592}
1593
Mikulas Patockadc267622015-02-13 08:25:59 -05001594static void kcryptd_queue_read(struct dm_crypt_io *io)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001595{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001596 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001597
Mikulas Patockadc267622015-02-13 08:25:59 -05001598 INIT_WORK(&io->work, kcryptd_io_read_work);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001599 queue_work(cc->io_queue, &io->work);
1600}
1601
Mikulas Patockadc267622015-02-13 08:25:59 -05001602static void kcryptd_io_write(struct dm_crypt_io *io)
1603{
1604 struct bio *clone = io->ctx.bio_out;
1605
1606 generic_make_request(clone);
1607}
1608
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001609#define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1610
Mikulas Patockadc267622015-02-13 08:25:59 -05001611static int dmcrypt_write(void *data)
1612{
1613 struct crypt_config *cc = data;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001614 struct dm_crypt_io *io;
1615
Mikulas Patockadc267622015-02-13 08:25:59 -05001616 while (1) {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001617 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -05001618 struct blk_plug plug;
1619
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001620 spin_lock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001621continue_locked:
1622
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001623 if (!RB_EMPTY_ROOT(&cc->write_tree))
Mikulas Patockadc267622015-02-13 08:25:59 -05001624 goto pop_from_list;
1625
Rabin Vincentf659b102016-09-21 16:22:29 +02001626 set_current_state(TASK_INTERRUPTIBLE);
Mikulas Patockadc267622015-02-13 08:25:59 -05001627
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001628 spin_unlock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001629
Rabin Vincentf659b102016-09-21 16:22:29 +02001630 if (unlikely(kthread_should_stop())) {
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001631 set_current_state(TASK_RUNNING);
Rabin Vincentf659b102016-09-21 16:22:29 +02001632 break;
1633 }
1634
Mikulas Patockadc267622015-02-13 08:25:59 -05001635 schedule();
1636
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001637 set_current_state(TASK_RUNNING);
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001638 spin_lock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001639 goto continue_locked;
1640
1641pop_from_list:
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001642 write_tree = cc->write_tree;
1643 cc->write_tree = RB_ROOT;
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001644 spin_unlock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001645
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001646 BUG_ON(rb_parent(write_tree.rb_node));
1647
1648 /*
1649 * Note: we cannot walk the tree here with rb_next because
1650 * the structures may be freed when kcryptd_io_write is called.
1651 */
Mikulas Patockadc267622015-02-13 08:25:59 -05001652 blk_start_plug(&plug);
1653 do {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001654 io = crypt_io_from_node(rb_first(&write_tree));
1655 rb_erase(&io->rb_node, &write_tree);
Mikulas Patockadc267622015-02-13 08:25:59 -05001656 kcryptd_io_write(io);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001657 } while (!RB_EMPTY_ROOT(&write_tree));
Mikulas Patockadc267622015-02-13 08:25:59 -05001658 blk_finish_plug(&plug);
1659 }
1660 return 0;
1661}
1662
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001663static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001664{
Milan Brozdec1ced2008-02-08 02:10:57 +00001665 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001666 struct crypt_config *cc = io->cc;
Mikulas Patockadc267622015-02-13 08:25:59 -05001667 unsigned long flags;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001668 sector_t sector;
1669 struct rb_node **rbp, *parent;
Milan Brozdec1ced2008-02-08 02:10:57 +00001670
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001671 if (unlikely(io->error)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001672 crypt_free_buffer_pages(cc, clone);
1673 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001674 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001675 return;
1676 }
1677
1678 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001679 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001680
Kent Overstreet4f024f32013-10-11 15:44:27 -07001681 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001682
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001683 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1684 generic_make_request(clone);
1685 return;
1686 }
1687
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001688 spin_lock_irqsave(&cc->write_thread_lock, flags);
1689 if (RB_EMPTY_ROOT(&cc->write_tree))
1690 wake_up_process(cc->write_thread);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001691 rbp = &cc->write_tree.rb_node;
1692 parent = NULL;
1693 sector = io->sector;
1694 while (*rbp) {
1695 parent = *rbp;
1696 if (sector < crypt_io_from_node(parent)->sector)
1697 rbp = &(*rbp)->rb_left;
1698 else
1699 rbp = &(*rbp)->rb_right;
1700 }
1701 rb_link_node(&io->rb_node, parent, rbp);
1702 rb_insert_color(&io->rb_node, &cc->write_tree);
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001703 spin_unlock_irqrestore(&cc->write_thread_lock, flags);
Milan Broz4e4eef62008-02-08 02:10:49 +00001704}
1705
Milan Brozfc5a5e92008-10-10 13:37:04 +01001706static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001707{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001708 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001709 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001710 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001711 sector_t sector = io->sector;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001712 blk_status_t r;
Milan Broz8b004452006-10-03 01:15:37 -07001713
Milan Broz93e605c2006-10-03 01:15:38 -07001714 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001715 * Prevent io from disappearing until this function completes.
1716 */
1717 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001718 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001719
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001720 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1721 if (unlikely(!clone)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001722 io->error = BLK_STS_IOERR;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001723 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001724 }
Milan Broz899c95d2008-02-08 02:11:02 +00001725
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001726 io->ctx.bio_out = clone;
1727 io->ctx.iter_out = clone->bi_iter;
1728
1729 sector += bio_sectors(clone);
1730
1731 crypt_inc_pending(io);
1732 r = crypt_convert(cc, &io->ctx);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001733 if (r)
Milan Brozef43aa32017-01-04 20:23:54 +01001734 io->error = r;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001735 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1736
1737 /* Encryption was already finished, submit io now */
1738 if (crypt_finished) {
1739 kcryptd_crypt_write_io_submit(io, 0);
1740 io->sector = sector;
1741 }
1742
1743dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001744 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001745}
1746
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001747static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001748{
Milan Broz5742fd72008-02-08 02:10:43 +00001749 crypt_dec_pending(io);
1750}
1751
Milan Broz4e4eef62008-02-08 02:10:49 +00001752static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001753{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001754 struct crypt_config *cc = io->cc;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001755 blk_status_t r;
Milan Broz8b004452006-10-03 01:15:37 -07001756
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001757 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001758
Milan Broz53017032008-02-08 02:10:38 +00001759 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001760 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001761
Milan Broz5742fd72008-02-08 02:10:43 +00001762 r = crypt_convert(cc, &io->ctx);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001763 if (r)
Milan Brozef43aa32017-01-04 20:23:54 +01001764 io->error = r;
Milan Broz5742fd72008-02-08 02:10:43 +00001765
Mikulas Patocka40b62292012-07-27 15:08:04 +01001766 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001767 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001768
1769 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001770}
1771
Milan Broz95497a92008-02-08 02:11:12 +00001772static void kcryptd_async_done(struct crypto_async_request *async_req,
1773 int error)
1774{
Huang Yingb2174ee2009-03-16 17:44:33 +00001775 struct dm_crypt_request *dmreq = async_req->data;
1776 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001777 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001778 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001779
Milan Broz54cea3f2015-05-15 17:00:25 +02001780 /*
1781 * A request from crypto driver backlog is going to be processed now,
1782 * finish the completion and continue in crypt_convert().
1783 * (Callback will be called for the second time for this request.)
1784 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001785 if (error == -EINPROGRESS) {
1786 complete(&ctx->restart);
Milan Broz95497a92008-02-08 02:11:12 +00001787 return;
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001788 }
Milan Broz95497a92008-02-08 02:11:12 +00001789
Milan Broz2dc53272011-01-13 19:59:54 +00001790 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001791 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq);
Milan Broz2dc53272011-01-13 19:59:54 +00001792
Milan Brozef43aa32017-01-04 20:23:54 +01001793 if (error == -EBADMSG) {
1794 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1795 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001796 io->error = BLK_STS_PROTECTION;
Milan Brozef43aa32017-01-04 20:23:54 +01001797 } else if (error < 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001798 io->error = BLK_STS_IOERR;
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001799
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001800 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00001801
Mikulas Patocka40b62292012-07-27 15:08:04 +01001802 if (!atomic_dec_and_test(&ctx->cc_pending))
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001803 return;
Milan Broz95497a92008-02-08 02:11:12 +00001804
1805 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001806 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001807 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001808 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001809}
1810
Milan Broz4e4eef62008-02-08 02:10:49 +00001811static void kcryptd_crypt(struct work_struct *work)
1812{
1813 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1814
1815 if (bio_data_dir(io->base_bio) == READ)
1816 kcryptd_crypt_read_convert(io);
1817 else
1818 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001819}
1820
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001821static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1822{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001823 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001824
1825 INIT_WORK(&io->work, kcryptd_crypt);
1826 queue_work(cc->crypt_queue, &io->work);
1827}
1828
Milan Brozef43aa32017-01-04 20:23:54 +01001829static void crypt_free_tfms_aead(struct crypt_config *cc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Milan Brozef43aa32017-01-04 20:23:54 +01001831 if (!cc->cipher_tfm.tfms_aead)
1832 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Milan Brozef43aa32017-01-04 20:23:54 +01001834 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1835 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]);
1836 cc->cipher_tfm.tfms_aead[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 }
1838
Milan Brozef43aa32017-01-04 20:23:54 +01001839 kfree(cc->cipher_tfm.tfms_aead);
1840 cc->cipher_tfm.tfms_aead = NULL;
1841}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Milan Brozef43aa32017-01-04 20:23:54 +01001843static void crypt_free_tfms_skcipher(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001844{
Milan Brozd1f96422011-01-13 19:59:54 +00001845 unsigned i;
1846
Milan Brozef43aa32017-01-04 20:23:54 +01001847 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001848 return;
1849
Milan Brozd1f96422011-01-13 19:59:54 +00001850 for (i = 0; i < cc->tfms_count; i++)
Milan Brozef43aa32017-01-04 20:23:54 +01001851 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) {
1852 crypto_free_skcipher(cc->cipher_tfm.tfms[i]);
1853 cc->cipher_tfm.tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001854 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001855
Milan Brozef43aa32017-01-04 20:23:54 +01001856 kfree(cc->cipher_tfm.tfms);
1857 cc->cipher_tfm.tfms = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
1860static void crypt_free_tfms(struct crypt_config *cc)
1861{
Milan Broz33d2f092017-03-16 15:39:40 +01001862 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001863 crypt_free_tfms_aead(cc);
1864 else
1865 crypt_free_tfms_skcipher(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001866}
1867
Milan Brozef43aa32017-01-04 20:23:54 +01001868static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001869{
Milan Brozd1f96422011-01-13 19:59:54 +00001870 unsigned i;
1871 int err;
1872
Kees Cook6396bb22018-06-12 14:03:40 -07001873 cc->cipher_tfm.tfms = kcalloc(cc->tfms_count,
1874 sizeof(struct crypto_skcipher *),
1875 GFP_KERNEL);
Milan Brozef43aa32017-01-04 20:23:54 +01001876 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001877 return -ENOMEM;
1878
Milan Brozd1f96422011-01-13 19:59:54 +00001879 for (i = 0; i < cc->tfms_count; i++) {
Milan Brozef43aa32017-01-04 20:23:54 +01001880 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
1881 if (IS_ERR(cc->cipher_tfm.tfms[i])) {
1882 err = PTR_ERR(cc->cipher_tfm.tfms[i]);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001883 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001884 return err;
1885 }
1886 }
1887
1888 return 0;
1889}
1890
Milan Brozef43aa32017-01-04 20:23:54 +01001891static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
1892{
Milan Brozef43aa32017-01-04 20:23:54 +01001893 int err;
1894
1895 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
1896 if (!cc->cipher_tfm.tfms)
1897 return -ENOMEM;
1898
Milan Brozef43aa32017-01-04 20:23:54 +01001899 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
1900 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1901 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
1902 crypt_free_tfms(cc);
1903 return err;
1904 }
1905
Milan Brozef43aa32017-01-04 20:23:54 +01001906 return 0;
1907}
1908
1909static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
1910{
Milan Broz33d2f092017-03-16 15:39:40 +01001911 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001912 return crypt_alloc_tfms_aead(cc, ciphermode);
1913 else
1914 return crypt_alloc_tfms_skcipher(cc, ciphermode);
1915}
1916
1917static unsigned crypt_subkey_size(struct crypt_config *cc)
1918{
1919 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1920}
1921
1922static unsigned crypt_authenckey_size(struct crypt_config *cc)
1923{
1924 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
1925}
1926
1927/*
1928 * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
1929 * the key must be for some reason in special format.
1930 * This funcion converts cc->key to this special format.
1931 */
1932static void crypt_copy_authenckey(char *p, const void *key,
1933 unsigned enckeylen, unsigned authkeylen)
1934{
1935 struct crypto_authenc_key_param *param;
1936 struct rtattr *rta;
1937
1938 rta = (struct rtattr *)p;
1939 param = RTA_DATA(rta);
1940 param->enckeylen = cpu_to_be32(enckeylen);
1941 rta->rta_len = RTA_LENGTH(sizeof(*param));
1942 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
1943 p += RTA_SPACE(sizeof(*param));
1944 memcpy(p, key + enckeylen, authkeylen);
1945 p += authkeylen;
1946 memcpy(p, key, enckeylen);
1947}
1948
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04001949static int crypt_setkey(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +00001950{
Milan Brozda31a072013-10-28 23:21:03 +01001951 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001952 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001953
Milan Brozda31a072013-10-28 23:21:03 +01001954 /* Ignore extra keys (which are used for IV etc) */
Milan Brozef43aa32017-01-04 20:23:54 +01001955 subkey_size = crypt_subkey_size(cc);
Milan Brozda31a072013-10-28 23:21:03 +01001956
Milan Broz27c70032018-01-03 22:48:59 +01001957 if (crypt_integrity_hmac(cc)) {
1958 if (subkey_size < cc->key_mac_size)
1959 return -EINVAL;
1960
Milan Brozef43aa32017-01-04 20:23:54 +01001961 crypt_copy_authenckey(cc->authenc_key, cc->key,
1962 subkey_size - cc->key_mac_size,
1963 cc->key_mac_size);
Milan Broz27c70032018-01-03 22:48:59 +01001964 }
1965
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001966 for (i = 0; i < cc->tfms_count; i++) {
Milan Broz33d2f092017-03-16 15:39:40 +01001967 if (crypt_integrity_hmac(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001968 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1969 cc->authenc_key, crypt_authenckey_size(cc));
Milan Broz33d2f092017-03-16 15:39:40 +01001970 else if (crypt_integrity_aead(cc))
1971 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1972 cc->key + (i * subkey_size),
1973 subkey_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001974 else
1975 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i],
1976 cc->key + (i * subkey_size),
1977 subkey_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001978 if (r)
1979 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001980 }
1981
Milan Brozef43aa32017-01-04 20:23:54 +01001982 if (crypt_integrity_hmac(cc))
1983 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc));
1984
Andi Kleenc0297722011-01-13 19:59:53 +00001985 return err;
1986}
1987
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001988#ifdef CONFIG_KEYS
1989
Ondrej Kozina027c4312016-12-01 18:20:52 +01001990static bool contains_whitespace(const char *str)
1991{
1992 while (*str)
1993 if (isspace(*str++))
1994 return true;
1995 return false;
1996}
1997
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001998static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
1999{
2000 char *new_key_string, *key_desc;
2001 int ret;
2002 struct key *key;
2003 const struct user_key_payload *ukp;
2004
Ondrej Kozina027c4312016-12-01 18:20:52 +01002005 /*
2006 * Reject key_string with whitespace. dm core currently lacks code for
2007 * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
2008 */
2009 if (contains_whitespace(key_string)) {
2010 DMERR("whitespace chars not allowed in key string");
2011 return -EINVAL;
2012 }
2013
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002014 /* look for next ':' separating key_type from key_description */
2015 key_desc = strpbrk(key_string, ":");
2016 if (!key_desc || key_desc == key_string || !strlen(key_desc + 1))
2017 return -EINVAL;
2018
2019 if (strncmp(key_string, "logon:", key_desc - key_string + 1) &&
2020 strncmp(key_string, "user:", key_desc - key_string + 1))
2021 return -EINVAL;
2022
2023 new_key_string = kstrdup(key_string, GFP_KERNEL);
2024 if (!new_key_string)
2025 return -ENOMEM;
2026
2027 key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user,
2028 key_desc + 1, NULL);
2029 if (IS_ERR(key)) {
2030 kzfree(new_key_string);
2031 return PTR_ERR(key);
2032 }
2033
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002034 down_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002035
David Howells0837e492017-03-01 15:11:23 +00002036 ukp = user_key_payload_locked(key);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002037 if (!ukp) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002038 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002039 key_put(key);
2040 kzfree(new_key_string);
2041 return -EKEYREVOKED;
2042 }
2043
2044 if (cc->key_size != ukp->datalen) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002045 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002046 key_put(key);
2047 kzfree(new_key_string);
2048 return -EINVAL;
2049 }
2050
2051 memcpy(cc->key, ukp->data, cc->key_size);
2052
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002053 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002054 key_put(key);
2055
2056 /* clear the flag since following operations may invalidate previously valid key */
2057 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2058
2059 ret = crypt_setkey(cc);
2060
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002061 if (!ret) {
2062 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2063 kzfree(cc->key_string);
2064 cc->key_string = new_key_string;
2065 } else
2066 kzfree(new_key_string);
2067
2068 return ret;
2069}
2070
2071static int get_key_size(char **key_string)
2072{
2073 char *colon, dummy;
2074 int ret;
2075
2076 if (*key_string[0] != ':')
2077 return strlen(*key_string) >> 1;
2078
2079 /* look for next ':' in key string */
2080 colon = strpbrk(*key_string + 1, ":");
2081 if (!colon)
2082 return -EINVAL;
2083
2084 if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':')
2085 return -EINVAL;
2086
2087 *key_string = colon;
2088
2089 /* remaining key string should be :<logon|user>:<key_desc> */
2090
2091 return ret;
2092}
2093
2094#else
2095
2096static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2097{
2098 return -EINVAL;
2099}
2100
2101static int get_key_size(char **key_string)
2102{
2103 return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1;
2104}
2105
2106#endif
2107
Milan Broze48d4bb2006-10-03 01:15:37 -07002108static int crypt_set_key(struct crypt_config *cc, char *key)
2109{
Milan Brozde8be5a2011-03-24 13:54:27 +00002110 int r = -EINVAL;
2111 int key_string_len = strlen(key);
2112
Milan Broz69a8cfc2011-01-13 19:59:49 +00002113 /* Hyphen (which gives a key_size of zero) means there is no key. */
2114 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00002115 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002116
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002117 /* ':' means the key is in kernel keyring, short-circuit normal key processing */
2118 if (key[0] == ':') {
2119 r = crypt_set_keyring_key(cc, key + 1);
2120 goto out;
2121 }
2122
Ondrej Kozina265e9092016-11-02 15:02:08 +01002123 /* clear the flag since following operations may invalidate previously valid key */
2124 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2125
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002126 /* wipe references to any kernel keyring key */
2127 kzfree(cc->key_string);
2128 cc->key_string = NULL;
2129
Andy Shevchenkoe944e032017-04-27 16:52:04 +03002130 /* Decode key from its hex representation. */
2131 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00002132 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002133
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002134 r = crypt_setkey(cc);
Ondrej Kozina265e9092016-11-02 15:02:08 +01002135 if (!r)
2136 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Milan Brozde8be5a2011-03-24 13:54:27 +00002137
2138out:
2139 /* Hex key string not needed after here, so wipe it. */
2140 memset(key, '0', key_string_len);
2141
2142 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002143}
2144
2145static int crypt_wipe_key(struct crypt_config *cc)
2146{
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002147 int r;
2148
Milan Broze48d4bb2006-10-03 01:15:37 -07002149 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002150 get_random_bytes(&cc->key, cc->key_size);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002151 kzfree(cc->key_string);
2152 cc->key_string = NULL;
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002153 r = crypt_setkey(cc);
2154 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00002155
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002156 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002157}
2158
Mikulas Patocka50593532017-08-13 22:45:08 -04002159static void crypt_calculate_pages_per_client(void)
2160{
2161 unsigned long pages = (totalram_pages - totalhigh_pages) * DM_CRYPT_MEMORY_PERCENT / 100;
2162
2163 if (!dm_crypt_clients_n)
2164 return;
2165
2166 pages /= dm_crypt_clients_n;
2167 if (pages < DM_CRYPT_MIN_PAGES_PER_CLIENT)
2168 pages = DM_CRYPT_MIN_PAGES_PER_CLIENT;
2169 dm_crypt_pages_per_client = pages;
2170}
2171
2172static void *crypt_page_alloc(gfp_t gfp_mask, void *pool_data)
2173{
2174 struct crypt_config *cc = pool_data;
2175 struct page *page;
2176
2177 if (unlikely(percpu_counter_compare(&cc->n_allocated_pages, dm_crypt_pages_per_client) >= 0) &&
2178 likely(gfp_mask & __GFP_NORETRY))
2179 return NULL;
2180
2181 page = alloc_page(gfp_mask);
2182 if (likely(page != NULL))
2183 percpu_counter_add(&cc->n_allocated_pages, 1);
2184
2185 return page;
2186}
2187
2188static void crypt_page_free(void *page, void *pool_data)
2189{
2190 struct crypt_config *cc = pool_data;
2191
2192 __free_page(page);
2193 percpu_counter_sub(&cc->n_allocated_pages, 1);
2194}
2195
Milan Broz28513fc2010-08-12 04:14:06 +01002196static void crypt_dtr(struct dm_target *ti)
2197{
2198 struct crypt_config *cc = ti->private;
2199
2200 ti->private = NULL;
2201
2202 if (!cc)
2203 return;
2204
Rabin Vincentf659b102016-09-21 16:22:29 +02002205 if (cc->write_thread)
Mikulas Patockadc267622015-02-13 08:25:59 -05002206 kthread_stop(cc->write_thread);
2207
Milan Broz28513fc2010-08-12 04:14:06 +01002208 if (cc->io_queue)
2209 destroy_workqueue(cc->io_queue);
2210 if (cc->crypt_queue)
2211 destroy_workqueue(cc->crypt_queue);
2212
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002213 crypt_free_tfms(cc);
2214
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002215 bioset_exit(&cc->bs);
Milan Broz28513fc2010-08-12 04:14:06 +01002216
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002217 mempool_exit(&cc->page_pool);
2218 mempool_exit(&cc->req_pool);
2219 mempool_exit(&cc->tag_pool);
2220
Kent Overstreetd00a11d2018-06-02 13:45:04 -04002221 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0);
2222 percpu_counter_destroy(&cc->n_allocated_pages);
2223
Milan Broz28513fc2010-08-12 04:14:06 +01002224 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
2225 cc->iv_gen_ops->dtr(cc);
2226
Milan Broz28513fc2010-08-12 04:14:06 +01002227 if (cc->dev)
2228 dm_put_device(ti, cc->dev);
2229
Milan Broz5ebaee62010-08-12 04:14:07 +01002230 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00002231 kzfree(cc->cipher_string);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002232 kzfree(cc->key_string);
Milan Brozef43aa32017-01-04 20:23:54 +01002233 kzfree(cc->cipher_auth);
2234 kzfree(cc->authenc_key);
Milan Broz28513fc2010-08-12 04:14:06 +01002235
Mike Snitzerd5ffebd2018-01-05 21:17:20 -05002236 mutex_destroy(&cc->bio_alloc_lock);
2237
Milan Broz28513fc2010-08-12 04:14:06 +01002238 /* Must zero key material before freeing */
2239 kzfree(cc);
Mikulas Patocka50593532017-08-13 22:45:08 -04002240
2241 spin_lock(&dm_crypt_clients_lock);
2242 WARN_ON(!dm_crypt_clients_n);
2243 dm_crypt_clients_n--;
2244 crypt_calculate_pages_per_client();
2245 spin_unlock(&dm_crypt_clients_lock);
Milan Broz28513fc2010-08-12 04:14:06 +01002246}
2247
Milan Broze889f972017-03-16 15:39:39 +01002248static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249{
Milan Broz5ebaee62010-08-12 04:14:07 +01002250 struct crypt_config *cc = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Milan Broz33d2f092017-03-16 15:39:40 +01002252 if (crypt_integrity_aead(cc))
Milan Broze889f972017-03-16 15:39:39 +01002253 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2254 else
2255 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Milan Broz5ebaee62010-08-12 04:14:07 +01002257 if (cc->iv_size)
2258 /* at least a 64 bit sector number should fit in our buffer */
2259 cc->iv_size = max(cc->iv_size,
2260 (unsigned int)(sizeof(u64) / sizeof(u8)));
2261 else if (ivmode) {
2262 DMWARN("Selected cipher does not support IVs");
2263 ivmode = NULL;
2264 }
2265
2266 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 if (ivmode == NULL)
2268 cc->iv_gen_ops = NULL;
2269 else if (strcmp(ivmode, "plain") == 0)
2270 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00002271 else if (strcmp(ivmode, "plain64") == 0)
2272 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Milan Broz7e3fd852017-06-06 09:07:01 +02002273 else if (strcmp(ivmode, "plain64be") == 0)
2274 cc->iv_gen_ops = &crypt_iv_plain64be_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 else if (strcmp(ivmode, "essiv") == 0)
2276 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10002277 else if (strcmp(ivmode, "benbi") == 0)
2278 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07002279 else if (strcmp(ivmode, "null") == 0)
2280 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Broz34745782011-01-13 19:59:55 +00002281 else if (strcmp(ivmode, "lmk") == 0) {
2282 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01002283 /*
2284 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00002285 * to length of provided multi-key string.
2286 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01002287 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00002288 */
Milan Brozda31a072013-10-28 23:21:03 +01002289 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00002290 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01002291 cc->key_extra_size = cc->key_size / cc->key_parts;
2292 }
Milan Brozed04d982013-10-28 23:21:04 +01002293 } else if (strcmp(ivmode, "tcw") == 0) {
2294 cc->iv_gen_ops = &crypt_iv_tcw_ops;
2295 cc->key_parts += 2; /* IV + whitening */
2296 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broze889f972017-03-16 15:39:39 +01002297 } else if (strcmp(ivmode, "random") == 0) {
2298 cc->iv_gen_ops = &crypt_iv_random_ops;
2299 /* Need storage space in integrity fields. */
2300 cc->integrity_iv_size = cc->iv_size;
Milan Broz34745782011-01-13 19:59:55 +00002301 } else {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002302 ti->error = "Invalid IV mode";
Milan Broze889f972017-03-16 15:39:39 +01002303 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
2305
Milan Broze889f972017-03-16 15:39:39 +01002306 return 0;
2307}
2308
Milan Broz33d2f092017-03-16 15:39:40 +01002309/*
2310 * Workaround to parse cipher algorithm from crypto API spec.
2311 * The cc->cipher is currently used only in ESSIV.
2312 * This should be probably done by crypto-api calls (once available...)
2313 */
2314static int crypt_ctr_blkdev_cipher(struct crypt_config *cc)
2315{
2316 const char *alg_name = NULL;
2317 char *start, *end;
2318
2319 if (crypt_integrity_aead(cc)) {
2320 alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc)));
2321 if (!alg_name)
2322 return -EINVAL;
2323 if (crypt_integrity_hmac(cc)) {
2324 alg_name = strchr(alg_name, ',');
2325 if (!alg_name)
2326 return -EINVAL;
2327 }
2328 alg_name++;
2329 } else {
2330 alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc)));
2331 if (!alg_name)
2332 return -EINVAL;
2333 }
2334
2335 start = strchr(alg_name, '(');
2336 end = strchr(alg_name, ')');
2337
2338 if (!start && !end) {
2339 cc->cipher = kstrdup(alg_name, GFP_KERNEL);
2340 return cc->cipher ? 0 : -ENOMEM;
2341 }
2342
2343 if (!start || !end || ++start >= end)
2344 return -EINVAL;
2345
2346 cc->cipher = kzalloc(end - start + 1, GFP_KERNEL);
2347 if (!cc->cipher)
2348 return -ENOMEM;
2349
2350 strncpy(cc->cipher, start, end - start);
2351
2352 return 0;
2353}
2354
2355/*
2356 * Workaround to parse HMAC algorithm from AEAD crypto API spec.
2357 * The HMAC is needed to calculate tag size (HMAC digest size).
2358 * This should be probably done by crypto-api calls (once available...)
2359 */
2360static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
2361{
2362 char *start, *end, *mac_alg = NULL;
2363 struct crypto_ahash *mac;
2364
2365 if (!strstarts(cipher_api, "authenc("))
2366 return 0;
2367
2368 start = strchr(cipher_api, '(');
2369 end = strchr(cipher_api, ',');
2370 if (!start || !end || ++start > end)
2371 return -EINVAL;
2372
2373 mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
2374 if (!mac_alg)
2375 return -ENOMEM;
2376 strncpy(mac_alg, start, end - start);
2377
2378 mac = crypto_alloc_ahash(mac_alg, 0, 0);
2379 kfree(mac_alg);
2380
2381 if (IS_ERR(mac))
2382 return PTR_ERR(mac);
2383
2384 cc->key_mac_size = crypto_ahash_digestsize(mac);
2385 crypto_free_ahash(mac);
2386
2387 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
2388 if (!cc->authenc_key)
2389 return -ENOMEM;
2390
2391 return 0;
2392}
2393
2394static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key,
2395 char **ivmode, char **ivopts)
Milan Broz5ebaee62010-08-12 04:14:07 +01002396{
2397 struct crypt_config *cc = ti->private;
Milan Broz33d2f092017-03-16 15:39:40 +01002398 char *tmp, *cipher_api;
2399 int ret = -EINVAL;
2400
2401 cc->tfms_count = 1;
2402
2403 /*
2404 * New format (capi: prefix)
2405 * capi:cipher_api_spec-iv:ivopts
2406 */
2407 tmp = &cipher_in[strlen("capi:")];
2408 cipher_api = strsep(&tmp, "-");
2409 *ivmode = strsep(&tmp, ":");
2410 *ivopts = tmp;
2411
2412 if (*ivmode && !strcmp(*ivmode, "lmk"))
2413 cc->tfms_count = 64;
2414
2415 cc->key_parts = cc->tfms_count;
2416
2417 /* Allocate cipher */
2418 ret = crypt_alloc_tfms(cc, cipher_api);
2419 if (ret < 0) {
2420 ti->error = "Error allocating crypto tfm";
2421 return ret;
2422 }
2423
2424 /* Alloc AEAD, can be used only in new format. */
2425 if (crypt_integrity_aead(cc)) {
2426 ret = crypt_ctr_auth_cipher(cc, cipher_api);
2427 if (ret < 0) {
2428 ti->error = "Invalid AEAD cipher spec";
2429 return -ENOMEM;
2430 }
2431 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2432 } else
2433 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2434
2435 ret = crypt_ctr_blkdev_cipher(cc);
2436 if (ret < 0) {
2437 ti->error = "Cannot allocate cipher string";
2438 return -ENOMEM;
2439 }
2440
2441 return 0;
2442}
2443
2444static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
2445 char **ivmode, char **ivopts)
2446{
2447 struct crypt_config *cc = ti->private;
2448 char *tmp, *cipher, *chainmode, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01002449 char *cipher_api = NULL;
2450 int ret = -EINVAL;
2451 char dummy;
2452
Milan Broz33d2f092017-03-16 15:39:40 +01002453 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002454 ti->error = "Bad cipher specification";
2455 return -EINVAL;
2456 }
2457
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 /*
Milan Broz5ebaee62010-08-12 04:14:07 +01002459 * Legacy dm-crypt cipher specification
2460 * cipher[:keycount]-mode-iv:ivopts
2461 */
2462 tmp = cipher_in;
2463 keycount = strsep(&tmp, "-");
2464 cipher = strsep(&keycount, ":");
2465
Milan Broz69a8cfc2011-01-13 19:59:49 +00002466 if (!keycount)
Milan Broz5ebaee62010-08-12 04:14:07 +01002467 cc->tfms_count = 1;
2468 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
2469 !is_power_of_2(cc->tfms_count)) {
2470 ti->error = "Bad cipher key count specification";
2471 return -EINVAL;
2472 }
Milan Broz28513fc2010-08-12 04:14:06 +01002473 cc->key_parts = cc->tfms_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002475 cc->cipher = kstrdup(cipher, GFP_KERNEL);
Milan Broz28513fc2010-08-12 04:14:06 +01002476 if (!cc->cipher)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 goto bad_mem;
2478
Milan Brozddd42ed2008-02-08 02:11:07 +00002479 chainmode = strsep(&tmp, "-");
Milan Broz33d2f092017-03-16 15:39:40 +01002480 *ivopts = strsep(&tmp, "-");
2481 *ivmode = strsep(&*ivopts, ":");
Andi Kleenc0297722011-01-13 19:59:53 +00002482
Milan Broz3a7f6c92008-02-08 02:11:14 +00002483 if (tmp)
Milan Brozddd42ed2008-02-08 02:11:07 +00002484 DMWARN("Ignoring unexpected additional cipher options");
2485
2486 /*
2487 * For compatibility with the original dm-crypt mapping format, if
2488 * only the cipher name is supplied, use cbc-plain.
Milan Broz28513fc2010-08-12 04:14:06 +01002489 */
Milan Broz33d2f092017-03-16 15:39:40 +01002490 if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002491 chainmode = "cbc";
Milan Broz33d2f092017-03-16 15:39:40 +01002492 *ivmode = "plain";
Milan Brozcabf08e2007-10-19 22:38:58 +01002493 }
2494
Milan Broz33d2f092017-03-16 15:39:40 +01002495 if (strcmp(chainmode, "ecb") && !*ivmode) {
Andi Kleenc0297722011-01-13 19:59:53 +00002496 ti->error = "IV mechanism required";
2497 return -EINVAL;
2498 }
2499
Milan Brozcabf08e2007-10-19 22:38:58 +01002500 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
Milan Broz9934a8b2007-10-19 22:38:57 +01002501 if (!cipher_api)
Milan Broz28513fc2010-08-12 04:14:06 +01002502 goto bad_mem;
Milan Broz9934a8b2007-10-19 22:38:57 +01002503
2504 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002505 "%s(%s)", chainmode, cipher);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 if (ret < 0) {
2507 kfree(cipher_api);
Milan Broz28513fc2010-08-12 04:14:06 +01002508 goto bad_mem;
2509 }
2510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 /* Allocate cipher */
2512 ret = crypt_alloc_tfms(cc, cipher_api);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (ret < 0) {
2514 ti->error = "Error allocating crypto tfm";
Milan Broz33d2f092017-03-16 15:39:40 +01002515 kfree(cipher_api);
2516 return ret;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002517 }
Jeffy Chenbd86e322017-09-27 20:28:57 +08002518 kfree(cipher_api);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002519
Milan Broz33d2f092017-03-16 15:39:40 +01002520 return 0;
2521bad_mem:
2522 ti->error = "Cannot allocate cipher strings";
2523 return -ENOMEM;
2524}
2525
2526static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key)
2527{
2528 struct crypt_config *cc = ti->private;
2529 char *ivmode = NULL, *ivopts = NULL;
2530 int ret;
2531
2532 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
2533 if (!cc->cipher_string) {
2534 ti->error = "Cannot allocate cipher strings";
2535 return -ENOMEM;
2536 }
2537
2538 if (strstarts(cipher_in, "capi:"))
2539 ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts);
2540 else
2541 ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts);
2542 if (ret)
2543 return ret;
2544
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002545 /* Initialize IV */
Milan Broze889f972017-03-16 15:39:39 +01002546 ret = crypt_ctr_ivmode(ti, ivmode);
2547 if (ret < 0)
Milan Broz33d2f092017-03-16 15:39:40 +01002548 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Milan Brozda31a072013-10-28 23:21:03 +01002550 /* Initialize and set key */
2551 ret = crypt_set_key(cc, key);
2552 if (ret < 0) {
2553 ti->error = "Error decoding and setting key";
Milan Broz33d2f092017-03-16 15:39:40 +01002554 return ret;
Milan Brozda31a072013-10-28 23:21:03 +01002555 }
2556
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 /* Allocate IV */
2558 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
2559 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
2560 if (ret < 0) {
2561 ti->error = "Error creating IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002562 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
2564 }
2565
2566 /* Initialize IV (set keys for ESSIV etc) */
2567 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
2568 ret = cc->iv_gen_ops->init(cc);
2569 if (ret < 0) {
2570 ti->error = "Error initialising IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002571 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
2573 }
2574
Ondrej Kozinadc949022018-01-12 16:30:32 +01002575 /* wipe the kernel key payload copy */
2576 if (cc->key_string)
2577 memset(cc->key, 0, cc->key_size * sizeof(u8));
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Milan Brozef43aa32017-01-04 20:23:54 +01002582static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
2583{
2584 struct crypt_config *cc = ti->private;
2585 struct dm_arg_set as;
Eric Biggers5916a222017-06-22 11:32:45 -07002586 static const struct dm_arg _args[] = {
Milan Broz8f0009a2017-03-16 15:39:44 +01002587 {0, 6, "Invalid number of feature args"},
Milan Brozef43aa32017-01-04 20:23:54 +01002588 };
2589 unsigned int opt_params, val;
2590 const char *opt_string, *sval;
Milan Broz8f0009a2017-03-16 15:39:44 +01002591 char dummy;
Milan Brozef43aa32017-01-04 20:23:54 +01002592 int ret;
2593
2594 /* Optional parameters */
2595 as.argc = argc;
2596 as.argv = argv;
2597
2598 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2599 if (ret)
2600 return ret;
2601
2602 while (opt_params--) {
2603 opt_string = dm_shift_arg(&as);
2604 if (!opt_string) {
2605 ti->error = "Not enough feature arguments";
2606 return -EINVAL;
2607 }
2608
2609 if (!strcasecmp(opt_string, "allow_discards"))
2610 ti->num_discard_bios = 1;
2611
2612 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
2613 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
2614
2615 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
2616 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
2617 else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
2618 if (val == 0 || val > MAX_TAG_SIZE) {
2619 ti->error = "Invalid integrity arguments";
2620 return -EINVAL;
2621 }
2622 cc->on_disk_tag_size = val;
2623 sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
2624 if (!strcasecmp(sval, "aead")) {
2625 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002626 } else if (strcasecmp(sval, "none")) {
2627 ti->error = "Unknown integrity profile";
2628 return -EINVAL;
2629 }
2630
2631 cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
2632 if (!cc->cipher_auth)
2633 return -ENOMEM;
Mikulas Patockaff3af922017-03-23 10:23:14 -04002634 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002635 if (cc->sector_size < (1 << SECTOR_SHIFT) ||
2636 cc->sector_size > 4096 ||
Mikulas Patockaff3af922017-03-23 10:23:14 -04002637 (cc->sector_size & (cc->sector_size - 1))) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002638 ti->error = "Invalid feature value for sector_size";
2639 return -EINVAL;
2640 }
Milan Broz783874b2017-09-13 15:45:56 +02002641 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) {
2642 ti->error = "Device size is not multiple of sector_size feature";
2643 return -EINVAL;
2644 }
Mikulas Patockaff3af922017-03-23 10:23:14 -04002645 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT;
Milan Broz8f0009a2017-03-16 15:39:44 +01002646 } else if (!strcasecmp(opt_string, "iv_large_sectors"))
2647 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
2648 else {
Milan Brozef43aa32017-01-04 20:23:54 +01002649 ti->error = "Invalid feature arguments";
2650 return -EINVAL;
2651 }
2652 }
2653
2654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655}
2656
2657/*
2658 * Construct an encryption mapping:
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002659 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 */
2661static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2662{
2663 struct crypt_config *cc;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002664 int key_size;
Milan Brozef43aa32017-01-04 20:23:54 +01002665 unsigned int align_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 unsigned long long tmpll;
2667 int ret;
Milan Brozef43aa32017-01-04 20:23:54 +01002668 size_t iv_size_padding, additional_req_size;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002669 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Milan Broz772ae5f2011-08-02 12:32:08 +01002671 if (argc < 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 ti->error = "Not enough arguments";
2673 return -EINVAL;
2674 }
2675
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002676 key_size = get_key_size(&argv[1]);
2677 if (key_size < 0) {
2678 ti->error = "Cannot parse key size";
2679 return -EINVAL;
2680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
2683 if (!cc) {
2684 ti->error = "Cannot allocate encryption context";
2685 return -ENOMEM;
2686 }
2687 cc->key_size = key_size;
Milan Broz8f0009a2017-03-16 15:39:44 +01002688 cc->sector_size = (1 << SECTOR_SHIFT);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002689 cc->sector_shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
2691 ti->private = cc;
Milan Brozef43aa32017-01-04 20:23:54 +01002692
Mikulas Patocka50593532017-08-13 22:45:08 -04002693 spin_lock(&dm_crypt_clients_lock);
2694 dm_crypt_clients_n++;
2695 crypt_calculate_pages_per_client();
2696 spin_unlock(&dm_crypt_clients_lock);
2697
2698 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL);
2699 if (ret < 0)
2700 goto bad;
2701
Milan Brozef43aa32017-01-04 20:23:54 +01002702 /* Optional parameters need to be read before cipher constructor */
2703 if (argc > 5) {
2704 ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
2705 if (ret)
2706 goto bad;
2707 }
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
2710 if (ret < 0)
2711 goto bad;
2712
Milan Broz33d2f092017-03-16 15:39:40 +01002713 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +01002714 cc->dmreq_start = sizeof(struct aead_request);
2715 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
2716 align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
2717 } else {
2718 cc->dmreq_start = sizeof(struct skcipher_request);
2719 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
2720 align_mask = crypto_skcipher_alignmask(any_tfm(cc));
2721 }
Mikulas Patockad49ec522014-08-28 11:09:31 -04002722 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
2723
Milan Brozef43aa32017-01-04 20:23:54 +01002724 if (align_mask < CRYPTO_MINALIGN) {
Mikulas Patockad49ec522014-08-28 11:09:31 -04002725 /* Allocate the padding exactly */
2726 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
Milan Brozef43aa32017-01-04 20:23:54 +01002727 & align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002728 } else {
2729 /*
2730 * If the cipher requires greater alignment than kmalloc
2731 * alignment, we don't know the exact position of the
2732 * initialization vector. We must assume worst case.
2733 */
Milan Brozef43aa32017-01-04 20:23:54 +01002734 iv_size_padding = align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Milan Brozef43aa32017-01-04 20:23:54 +01002737 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
2738 additional_req_size = sizeof(struct dm_crypt_request) +
2739 iv_size_padding + cc->iv_size +
2740 cc->iv_size +
2741 sizeof(uint64_t) +
2742 sizeof(unsigned int);
2743
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002744 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size);
2745 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 ti->error = "Cannot allocate crypt request mempool";
2747 goto bad;
2748 }
2749
Mike Snitzer30187e12016-01-31 13:28:26 -05002750 cc->per_bio_data_size = ti->per_io_data_size =
Milan Brozef43aa32017-01-04 20:23:54 +01002751 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
Mikulas Patockad49ec522014-08-28 11:09:31 -04002752 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002753
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002754 ret = mempool_init(&cc->page_pool, BIO_MAX_PAGES, crypt_page_alloc, crypt_page_free, cc);
2755 if (ret) {
Milan Broz8b004452006-10-03 01:15:37 -07002756 ti->error = "Cannot allocate page mempool";
Milan Broze48d4bb2006-10-03 01:15:37 -07002757 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002759
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002760 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS);
2761 if (ret) {
Milan Broz0c395b02008-02-08 02:10:54 +00002762 ti->error = "Cannot allocate crypt bioset";
Milan Brozcabf08e2007-10-19 22:38:58 +01002763 goto bad;
Milan Broz93e605c2006-10-03 01:15:38 -07002764 }
Milan Brozcabf08e2007-10-19 22:38:58 +01002765
Mikulas Patocka7145c242015-02-13 08:24:41 -05002766 mutex_init(&cc->bio_alloc_lock);
2767
Milan Brozcabf08e2007-10-19 22:38:58 +01002768 ret = -EINVAL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002769 if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
2770 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002771 ti->error = "Invalid iv_offset sector";
2772 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002774 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Vivek Goyale80d1c82015-07-31 09:20:36 -04002776 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
2777 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 ti->error = "Device lookup failed";
2779 goto bad;
2780 }
2781
Vivek Goyale80d1c82015-07-31 09:20:36 -04002782 ret = -EINVAL;
Milan Broz887b1c92018-11-07 22:24:55 +01002783 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 ti->error = "Invalid device sector";
2785 goto bad;
2786 }
2787 cc->start = tmpll;
2788
Milan Broz33d2f092017-03-16 15:39:40 +01002789 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
Milan Brozef43aa32017-01-04 20:23:54 +01002790 ret = crypt_integrity_ctr(cc, ti);
Milan Broz772ae5f2011-08-02 12:32:08 +01002791 if (ret)
2792 goto bad;
2793
Milan Brozef43aa32017-01-04 20:23:54 +01002794 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
2795 if (!cc->tag_pool_max_sectors)
2796 cc->tag_pool_max_sectors = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01002797
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002798 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS,
Milan Brozef43aa32017-01-04 20:23:54 +01002799 cc->tag_pool_max_sectors * cc->on_disk_tag_size);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002800 if (ret) {
Milan Brozef43aa32017-01-04 20:23:54 +01002801 ti->error = "Cannot allocate integrity tags mempool";
2802 goto bad;
Milan Broz772ae5f2011-08-02 12:32:08 +01002803 }
Mikulas Patocka583fe742017-04-18 16:51:54 -04002804
2805 cc->tag_pool_max_sectors <<= cc->sector_shift;
Milan Broz772ae5f2011-08-02 12:32:08 +01002806 }
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 ret = -ENOMEM;
Tim Murraya1b89132017-04-21 11:11:36 +02002809 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (!cc->io_queue) {
2811 ti->error = "Couldn't create kcryptd io queue";
2812 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 }
Christophe Saout37af6562006-10-30 20:39:08 +01002814
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002815 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
Tim Murraya1b89132017-04-21 11:11:36 +02002816 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002817 else
Tim Murraya1b89132017-04-21 11:11:36 +02002818 cc->crypt_queue = alloc_workqueue("kcryptd",
2819 WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002820 num_online_cpus());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 if (!cc->crypt_queue) {
2822 ti->error = "Couldn't create kcryptd queue";
2823 goto bad;
2824 }
2825
Mikulas Patockac7329ef2018-07-11 12:10:51 -04002826 spin_lock_init(&cc->write_thread_lock);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05002827 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05002828
2829 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write");
2830 if (IS_ERR(cc->write_thread)) {
2831 ret = PTR_ERR(cc->write_thread);
2832 cc->write_thread = NULL;
2833 ti->error = "Couldn't spawn write thread";
2834 goto bad;
2835 }
2836 wake_up_process(cc->write_thread);
2837
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002838 ti->num_flush_bios = 1;
Milan Broz983c7db2011-09-25 23:26:21 +01002839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return 0;
2841
2842bad:
2843 crypt_dtr(ti);
2844 return ret;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002845}
2846
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002847static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848{
2849 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01002850 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002851
Milan Broz772ae5f2011-08-02 12:32:08 +01002852 /*
Mike Christie28a8f0d2016-06-05 14:32:25 -05002853 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
2854 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
Mike Christiee6047142016-06-05 14:32:04 -05002855 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
Milan Broz772ae5f2011-08-02 12:32:08 +01002856 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06002857 if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
Mike Christie28a8f0d2016-06-05 14:32:25 -05002858 bio_op(bio) == REQ_OP_DISCARD)) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02002859 bio_set_dev(bio, cc->dev->bdev);
Milan Broz772ae5f2011-08-02 12:32:08 +01002860 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07002861 bio->bi_iter.bi_sector = cc->start +
2862 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002863 return DM_MAPIO_REMAPPED;
2864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002866 /*
2867 * Check if bio is too large, split as needed.
2868 */
2869 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
Milan Brozef43aa32017-01-04 20:23:54 +01002870 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002871 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
2872
Milan Broz8f0009a2017-03-16 15:39:44 +01002873 /*
2874 * Ensure that bio is a multiple of internal sector encryption size
2875 * and is aligned to this size as defined in IO hints.
2876 */
2877 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
Christoph Hellwig846785e2017-06-03 09:38:02 +02002878 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002879
2880 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
Christoph Hellwig846785e2017-06-03 09:38:02 +02002881 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002882
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002883 io = dm_per_bio_data(bio, cc->per_bio_data_size);
2884 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Milan Brozef43aa32017-01-04 20:23:54 +01002885
2886 if (cc->on_disk_tag_size) {
Mikulas Patocka583fe742017-04-18 16:51:54 -04002887 unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift);
Milan Brozef43aa32017-01-04 20:23:54 +01002888
2889 if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
Mikulas Patocka583fe742017-04-18 16:51:54 -04002890 unlikely(!(io->integrity_metadata = kmalloc(tag_len,
Milan Brozef43aa32017-01-04 20:23:54 +01002891 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
2892 if (bio_sectors(bio) > cc->tag_pool_max_sectors)
2893 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002894 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO);
Milan Brozef43aa32017-01-04 20:23:54 +01002895 io->integrity_metadata_from_pool = true;
2896 }
2897 }
2898
Milan Broz33d2f092017-03-16 15:39:40 +01002899 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002900 io->ctx.r.req_aead = (struct aead_request *)(io + 1);
2901 else
2902 io->ctx.r.req = (struct skcipher_request *)(io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
Milan Broz20c82532011-01-13 19:59:53 +00002904 if (bio_data_dir(io->base_bio) == READ) {
2905 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05002906 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00002907 } else
Andrew Morton4ee218c2006-03-27 01:17:48 -08002908 kcryptd_queue_crypt(io);
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 return DM_MAPIO_SUBMITTED;
2911}
2912
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002913static void crypt_status(struct dm_target *ti, status_type_t type,
2914 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
Milan Broz5ebaee62010-08-12 04:14:07 +01002916 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002917 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002918 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
2920 switch (type) {
2921 case STATUSTYPE_INFO:
2922 result[0] = '\0';
2923 break;
2924
2925 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00002926 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002928 if (cc->key_size > 0) {
2929 if (cc->key_string)
2930 DMEMIT(":%u:%s", cc->key_size, cc->key_string);
2931 else
2932 for (i = 0; i < cc->key_size; i++)
2933 DMEMIT("%02x", cc->key[i]);
2934 } else
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002935 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
2937 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
2938 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01002939
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002940 num_feature_args += !!ti->num_discard_bios;
2941 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002942 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002943 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT);
Milan Broz8f0009a2017-03-16 15:39:44 +01002944 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002945 if (cc->on_disk_tag_size)
2946 num_feature_args++;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002947 if (num_feature_args) {
2948 DMEMIT(" %d", num_feature_args);
2949 if (ti->num_discard_bios)
2950 DMEMIT(" allow_discards");
2951 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2952 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002953 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
2954 DMEMIT(" submit_from_crypt_cpus");
Milan Brozef43aa32017-01-04 20:23:54 +01002955 if (cc->on_disk_tag_size)
2956 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
Milan Broz8f0009a2017-03-16 15:39:44 +01002957 if (cc->sector_size != (1 << SECTOR_SHIFT))
2958 DMEMIT(" sector_size:%d", cc->sector_size);
2959 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
2960 DMEMIT(" iv_large_sectors");
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002961 }
Milan Broz772ae5f2011-08-02 12:32:08 +01002962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 break;
2964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
Milan Broze48d4bb2006-10-03 01:15:37 -07002967static void crypt_postsuspend(struct dm_target *ti)
2968{
2969 struct crypt_config *cc = ti->private;
2970
2971 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2972}
2973
2974static int crypt_preresume(struct dm_target *ti)
2975{
2976 struct crypt_config *cc = ti->private;
2977
2978 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
2979 DMERR("aborting resume - crypt key is not set.");
2980 return -EAGAIN;
2981 }
2982
2983 return 0;
2984}
2985
2986static void crypt_resume(struct dm_target *ti)
2987{
2988 struct crypt_config *cc = ti->private;
2989
2990 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2991}
2992
2993/* Message interface
2994 * key set <key>
2995 * key wipe
2996 */
Mike Snitzer1eb5fa82018-02-28 15:59:59 -05002997static int crypt_message(struct dm_target *ti, unsigned argc, char **argv,
2998 char *result, unsigned maxlen)
Milan Broze48d4bb2006-10-03 01:15:37 -07002999{
3000 struct crypt_config *cc = ti->private;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01003001 int key_size, ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07003002
3003 if (argc < 2)
3004 goto error;
3005
Mike Snitzer498f0102011-08-02 12:32:04 +01003006 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07003007 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
3008 DMWARN("not suspended during key manipulation.");
3009 return -EINVAL;
3010 }
Mike Snitzer498f0102011-08-02 12:32:04 +01003011 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01003012 /* The key size may not be changed. */
3013 key_size = get_key_size(&argv[2]);
3014 if (key_size < 0 || cc->key_size != key_size) {
3015 memset(argv[2], '0', strlen(argv[2]));
3016 return -EINVAL;
3017 }
3018
Milan Broz542da312009-12-10 23:51:57 +00003019 ret = crypt_set_key(cc, argv[2]);
3020 if (ret)
3021 return ret;
3022 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
3023 ret = cc->iv_gen_ops->init(cc);
Ondrej Kozinadc949022018-01-12 16:30:32 +01003024 /* wipe the kernel key payload copy */
3025 if (cc->key_string)
3026 memset(cc->key, 0, cc->key_size * sizeof(u8));
Milan Broz542da312009-12-10 23:51:57 +00003027 return ret;
3028 }
Mike Snitzer498f0102011-08-02 12:32:04 +01003029 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00003030 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
3031 ret = cc->iv_gen_ops->wipe(cc);
3032 if (ret)
3033 return ret;
3034 }
Milan Broze48d4bb2006-10-03 01:15:37 -07003035 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00003036 }
Milan Broze48d4bb2006-10-03 01:15:37 -07003037 }
3038
3039error:
3040 DMWARN("unrecognised message received.");
3041 return -EINVAL;
3042}
3043
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003044static int crypt_iterate_devices(struct dm_target *ti,
3045 iterate_devices_callout_fn fn, void *data)
3046{
3047 struct crypt_config *cc = ti->private;
3048
Mike Snitzer5dea2712009-07-23 20:30:42 +01003049 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003050}
3051
Mike Snitzer586b2862015-09-09 21:34:51 -04003052static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
3053{
Milan Broz8f0009a2017-03-16 15:39:44 +01003054 struct crypt_config *cc = ti->private;
3055
Mike Snitzer586b2862015-09-09 21:34:51 -04003056 /*
3057 * Unfortunate constraint that is required to avoid the potential
3058 * for exceeding underlying device's max_segments limits -- due to
3059 * crypt_alloc_buffer() possibly allocating pages for the encryption
3060 * bio that are not as physically contiguous as the original bio.
3061 */
3062 limits->max_segment_size = PAGE_SIZE;
Milan Broz8f0009a2017-03-16 15:39:44 +01003063
Mikulas Patockabc9e9cf2018-08-10 11:23:56 -04003064 limits->logical_block_size =
3065 max_t(unsigned short, limits->logical_block_size, cc->sector_size);
3066 limits->physical_block_size =
3067 max_t(unsigned, limits->physical_block_size, cc->sector_size);
3068 limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size);
Mike Snitzer586b2862015-09-09 21:34:51 -04003069}
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071static struct target_type crypt_target = {
3072 .name = "crypt",
Ondrej Kozinadc949022018-01-12 16:30:32 +01003073 .version = {1, 18, 1},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 .module = THIS_MODULE,
3075 .ctr = crypt_ctr,
3076 .dtr = crypt_dtr,
3077 .map = crypt_map,
3078 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07003079 .postsuspend = crypt_postsuspend,
3080 .preresume = crypt_preresume,
3081 .resume = crypt_resume,
3082 .message = crypt_message,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003083 .iterate_devices = crypt_iterate_devices,
Mike Snitzer586b2862015-09-09 21:34:51 -04003084 .io_hints = crypt_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085};
3086
3087static int __init dm_crypt_init(void)
3088{
3089 int r;
3090
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05003092 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07003093 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 return r;
3096}
3097
3098static void __exit dm_crypt_exit(void)
3099{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00003100 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
3103module_init(dm_crypt_init);
3104module_exit(dm_crypt_exit);
3105
Jana Saoutbf142992014-06-24 14:27:04 -04003106MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
3108MODULE_LICENSE("GPL");