blob: 3df90daba89eed000b563a84fd1aee09f69587ea [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 Brozbbb16582020-01-03 09:20:22 +01004 * Copyright (C) 2006-2020 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2013-2020 Milan Broz <gmazyland@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This file is released under the GPL.
8 */
9
Milan Broz43d69032008-02-08 02:11:09 +000010#include <linux/completion.h>
Herbert Xud1806f62006-08-22 20:29:17 +100011#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
Ondrej Kozinac538f6e2016-11-21 15:58:51 +010015#include <linux/key.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/bio.h>
17#include <linux/blkdev.h>
18#include <linux/mempool.h>
19#include <linux/slab.h>
20#include <linux/crypto.h>
21#include <linux/workqueue.h>
Mikulas Patockadc267622015-02-13 08:25:59 -050022#include <linux/kthread.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070023#include <linux/backing-dev.h>
Arun Sharma600634972011-07-26 16:09:06 -070024#include <linux/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100025#include <linux/scatterlist.h>
Mikulas Patockab3c5fd32015-02-13 08:27:41 -050026#include <linux/rbtree.h>
Ondrej Kozina027c4312016-12-01 18:20:52 +010027#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100029#include <asm/unaligned.h>
Milan Broz34745782011-01-13 19:59:55 +000030#include <crypto/hash.h>
31#include <crypto/md5.h>
32#include <crypto/algapi.h>
Herbert Xubbdb23b2016-01-24 21:16:36 +080033#include <crypto/skcipher.h>
Milan Brozef43aa32017-01-04 20:23:54 +010034#include <crypto/aead.h>
35#include <crypto/authenc.h>
36#include <linux/rtnetlink.h> /* for struct rtattr and RTA macros only */
Ondrej Kozinac538f6e2016-11-21 15:58:51 +010037#include <keys/user-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Mikulas Patocka586e80e2008-10-21 17:44:59 +010039#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Alasdair G Kergon72d94862006-06-26 00:27:35 -070041#define DM_MSG_PREFIX "crypt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * context holding the current state of a multi-part conversion
45 */
46struct convert_context {
Milan Broz43d69032008-02-08 02:11:09 +000047 struct completion restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct bio *bio_in;
49 struct bio *bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -070050 struct bvec_iter iter_in;
51 struct bvec_iter iter_out;
AliOS system security8d683dc2018-11-05 15:31:42 +080052 u64 cc_sector;
Mikulas Patocka40b62292012-07-27 15:08:04 +010053 atomic_t cc_pending;
Milan Brozef43aa32017-01-04 20:23:54 +010054 union {
55 struct skcipher_request *req;
56 struct aead_request *req_aead;
57 } r;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Milan Broz53017032008-02-08 02:10:38 +000061/*
62 * per bio private data
63 */
64struct dm_crypt_io {
Alasdair G Kergon49a8a922012-07-27 15:08:05 +010065 struct crypt_config *cc;
Milan Broz53017032008-02-08 02:10:38 +000066 struct bio *base_bio;
Milan Brozef43aa32017-01-04 20:23:54 +010067 u8 *integrity_metadata;
68 bool integrity_metadata_from_pool;
Milan Broz53017032008-02-08 02:10:38 +000069 struct work_struct work;
70
71 struct convert_context ctx;
72
Mikulas Patocka40b62292012-07-27 15:08:04 +010073 atomic_t io_pending;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020074 blk_status_t error;
Milan Broz0c395b02008-02-08 02:10:54 +000075 sector_t sector;
Mikulas Patockadc267622015-02-13 08:25:59 -050076
Mikulas Patockab3c5fd32015-02-13 08:27:41 -050077 struct rb_node rb_node;
Mikulas Patocka298a9fa2014-03-28 15:51:55 -040078} CRYPTO_MINALIGN_ATTR;
Milan Broz53017032008-02-08 02:10:38 +000079
Milan Broz01482b72008-02-08 02:11:04 +000080struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000081 struct convert_context *ctx;
Milan Brozef43aa32017-01-04 20:23:54 +010082 struct scatterlist sg_in[4];
83 struct scatterlist sg_out[4];
AliOS system security8d683dc2018-11-05 15:31:42 +080084 u64 iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000085};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct crypt_config;
88
89struct crypt_iv_operations {
90 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010091 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000093 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000094 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc53272011-01-13 19:59:54 +000095 int (*generator)(struct crypt_config *cc, u8 *iv,
96 struct dm_crypt_request *dmreq);
97 int (*post)(struct crypt_config *cc, u8 *iv,
98 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Milan Broz60473592009-12-10 23:51:55 +0000101struct iv_benbi_private {
102 int shift;
103};
104
Milan Broz34745782011-01-13 19:59:55 +0000105#define LMK_SEED_SIZE 64 /* hash + 0 */
106struct iv_lmk_private {
107 struct crypto_shash *hash_tfm;
108 u8 *seed;
109};
110
Milan Brozed04d982013-10-28 23:21:04 +0100111#define TCW_WHITENING_SIZE 16
112struct iv_tcw_private {
113 struct crypto_shash *crc32_tfm;
114 u8 *iv_seed;
115 u8 *whitening;
116};
117
Milan Brozbbb16582020-01-03 09:20:22 +0100118#define ELEPHANT_MAX_KEY_SIZE 32
119struct iv_elephant_private {
120 struct crypto_skcipher *tfm;
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 Brozbbb16582020-01-03 09:20:22 +0100133 CRYPT_ENCRYPT_PREPROCESS, /* Must preprocess data for encryption (elephant) */
Milan Brozef43aa32017-01-04 20:23:54 +0100134};
135
Andi Kleenc0297722011-01-13 19:59:53 +0000136/*
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500137 * The fields in here must be read only after initialization.
Andi Kleenc0297722011-01-13 19:59:53 +0000138 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139struct crypt_config {
140 struct dm_dev *dev;
141 sector_t start;
142
Mikulas Patocka50593532017-08-13 22:45:08 -0400143 struct percpu_counter n_allocated_pages;
144
Milan Brozcabf08e2007-10-19 22:38:58 +0100145 struct workqueue_struct *io_queue;
146 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700147
Mikulas Patockac7329ef2018-07-11 12:10:51 -0400148 spinlock_t write_thread_lock;
Mike Snitzer72d711c2018-05-22 18:26:20 -0400149 struct task_struct *write_thread;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -0500150 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -0500151
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_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000159 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100160 struct iv_tcw_private tcw;
Milan Brozbbb16582020-01-03 09:20:22 +0100161 struct iv_elephant_private elephant;
Herbert Xu79066ad2006-12-05 13:41:52 -0800162 } iv_gen_private;
AliOS system security8d683dc2018-11-05 15:31:42 +0800163 u64 iv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 unsigned int iv_size;
Mikulas Patockaff3af922017-03-23 10:23:14 -0400165 unsigned short int sector_size;
166 unsigned char sector_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Milan Brozef43aa32017-01-04 20:23:54 +0100168 union {
169 struct crypto_skcipher **tfms;
170 struct crypto_aead **tfms_aead;
171 } cipher_tfm;
Milan Brozd1f96422011-01-13 19:59:54 +0000172 unsigned tfms_count;
Milan Brozef43aa32017-01-04 20:23:54 +0100173 unsigned long cipher_flags;
Andi Kleenc0297722011-01-13 19:59:53 +0000174
175 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000176 * Layout of each crypto request:
177 *
Herbert Xubbdb23b2016-01-24 21:16:36 +0800178 * struct skcipher_request
Milan Brozddd42ed2008-02-08 02:11:07 +0000179 * context
180 * padding
181 * struct dm_crypt_request
182 * padding
183 * IV
184 *
185 * The padding is added so that dm_crypt_request and the IV are
186 * correctly aligned.
187 */
188 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000189
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400190 unsigned int per_bio_data_size;
191
Milan Broze48d4bb2006-10-03 01:15:37 -0700192 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100194 unsigned int key_parts; /* independent parts in key buffer */
195 unsigned int key_extra_size; /* additional keys length */
Milan Brozef43aa32017-01-04 20:23:54 +0100196 unsigned int key_mac_size; /* MAC key size for authenc(...) */
197
198 unsigned int integrity_tag_size;
199 unsigned int integrity_iv_size;
200 unsigned int on_disk_tag_size;
201
Mike Snitzer72d711c2018-05-22 18:26:20 -0400202 /*
203 * pool for per bio private data, crypto requests,
204 * encryption requeusts/buffer pages and integrity tags
205 */
206 unsigned tag_pool_max_sectors;
207 mempool_t tag_pool;
208 mempool_t req_pool;
209 mempool_t page_pool;
210
211 struct bio_set bs;
212 struct mutex bio_alloc_lock;
213
Milan Brozef43aa32017-01-04 20:23:54 +0100214 u8 *authenc_key; /* space for keys in authenc() format (if used) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 u8 key[0];
216};
217
Milan Brozef43aa32017-01-04 20:23:54 +0100218#define MIN_IOS 64
219#define MAX_TAG_SIZE 480
220#define POOL_ENTRY_SIZE 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Mikulas Patocka50593532017-08-13 22:45:08 -0400222static DEFINE_SPINLOCK(dm_crypt_clients_lock);
223static unsigned dm_crypt_clients_n = 0;
224static volatile unsigned long dm_crypt_pages_per_client;
225#define DM_CRYPT_MEMORY_PERCENT 2
226#define DM_CRYPT_MIN_PAGES_PER_CLIENT (BIO_MAX_PAGES * 16)
227
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100228static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000229static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Brozef43aa32017-01-04 20:23:54 +0100230static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
231 struct scatterlist *sg);
Olaf Kirch027581f2007-05-09 02:32:52 -0700232
Yang Yingliang3fd53532020-02-13 12:11:26 +0800233static bool crypt_integrity_aead(struct crypt_config *cc);
234
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.
Milan Brozb9411d72019-07-09 15:22:14 +0200293 *
294 * eboiv: Encrypted byte-offset IV (used in Bitlocker in CBC mode)
295 * The IV is encrypted little-endian byte-offset (with the same key
296 * and cipher as the volume).
Milan Brozbbb16582020-01-03 09:20:22 +0100297 *
298 * elephant: The extended version of eboiv with additional Elephant diffuser
299 * used with Bitlocker CBC mode.
300 * This mode was used in older Windows systems
301 * http://download.microsoft.com/download/0/2/3/0238acaf-d3bf-4a6d-b3d6-0a0be4bbb36e/bitlockercipher200608.pdf
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 */
303
Milan Broz2dc53272011-01-13 19:59:54 +0000304static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
305 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100308 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 return 0;
311}
312
Milan Broz61afef62009-12-10 23:52:25 +0000313static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000314 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000315{
316 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100317 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000318
319 return 0;
320}
321
Milan Broz7e3fd852017-06-06 09:07:01 +0200322static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv,
323 struct dm_crypt_request *dmreq)
324{
325 memset(iv, 0, cc->iv_size);
326 /* iv_size is at least of size u64; usually it is 16 bytes */
327 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector);
328
329 return 0;
330}
331
Milan Broz2dc53272011-01-13 19:59:54 +0000332static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
333 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Ard Biesheuvela1a262b2019-08-19 17:17:37 +0300335 /*
336 * ESSIV encryption of the IV is now handled by the crypto API,
337 * so just pass the plain sector number here.
338 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100340 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return 0;
343}
344
Rik Snel48527fa2006-09-03 08:56:39 +1000345static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
346 const char *opts)
347{
Milan Broz4ea94712020-01-06 10:11:47 +0100348 unsigned bs;
349 int log;
350
Yang Yingliang3fd53532020-02-13 12:11:26 +0800351 if (crypt_integrity_aead(cc))
Milan Broz4ea94712020-01-06 10:11:47 +0100352 bs = crypto_aead_blocksize(any_tfm_aead(cc));
353 else
354 bs = crypto_skcipher_blocksize(any_tfm(cc));
355 log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000356
357 /* we need to calculate how far we must shift the sector count
358 * to get the cipher block count, we use this shift in _gen */
359
360 if (1 << log != bs) {
361 ti->error = "cypher blocksize is not a power of 2";
362 return -EINVAL;
363 }
364
365 if (log > 9) {
366 ti->error = "cypher blocksize is > 512";
367 return -EINVAL;
368 }
369
Milan Broz60473592009-12-10 23:51:55 +0000370 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000371
372 return 0;
373}
374
375static void crypt_iv_benbi_dtr(struct crypt_config *cc)
376{
Rik Snel48527fa2006-09-03 08:56:39 +1000377}
378
Milan Broz2dc53272011-01-13 19:59:54 +0000379static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
380 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000381{
Herbert Xu79066ad2006-12-05 13:41:52 -0800382 __be64 val;
383
Rik Snel48527fa2006-09-03 08:56:39 +1000384 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800385
Milan Broz2dc53272011-01-13 19:59:54 +0000386 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800387 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 0;
390}
391
Milan Broz2dc53272011-01-13 19:59:54 +0000392static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
393 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700394{
395 memset(iv, 0, cc->iv_size);
396
397 return 0;
398}
399
Milan Broz34745782011-01-13 19:59:55 +0000400static void crypt_iv_lmk_dtr(struct crypt_config *cc)
401{
402 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
403
404 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
405 crypto_free_shash(lmk->hash_tfm);
406 lmk->hash_tfm = NULL;
407
408 kzfree(lmk->seed);
409 lmk->seed = NULL;
410}
411
412static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
413 const char *opts)
414{
415 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
416
Milan Broz8f0009a2017-03-16 15:39:44 +0100417 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
418 ti->error = "Unsupported sector size for LMK";
419 return -EINVAL;
420 }
421
Milan Broz34745782011-01-13 19:59:55 +0000422 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
423 if (IS_ERR(lmk->hash_tfm)) {
424 ti->error = "Error initializing LMK hash";
425 return PTR_ERR(lmk->hash_tfm);
426 }
427
428 /* No seed in LMK version 2 */
429 if (cc->key_parts == cc->tfms_count) {
430 lmk->seed = NULL;
431 return 0;
432 }
433
434 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
435 if (!lmk->seed) {
436 crypt_iv_lmk_dtr(cc);
437 ti->error = "Error kmallocing seed storage in LMK";
438 return -ENOMEM;
439 }
440
441 return 0;
442}
443
444static int crypt_iv_lmk_init(struct crypt_config *cc)
445{
446 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
447 int subkey_size = cc->key_size / cc->key_parts;
448
449 /* LMK seed is on the position of LMK_KEYS + 1 key */
450 if (lmk->seed)
451 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
452 crypto_shash_digestsize(lmk->hash_tfm));
453
454 return 0;
455}
456
457static int crypt_iv_lmk_wipe(struct crypt_config *cc)
458{
459 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
460
461 if (lmk->seed)
462 memset(lmk->seed, 0, LMK_SEED_SIZE);
463
464 return 0;
465}
466
467static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
468 struct dm_crypt_request *dmreq,
469 u8 *data)
470{
471 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200472 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000473 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100474 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000475 int i, r;
476
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200477 desc->tfm = lmk->hash_tfm;
Milan Broz34745782011-01-13 19:59:55 +0000478
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200479 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000480 if (r)
481 return r;
482
483 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200484 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000485 if (r)
486 return r;
487 }
488
489 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200490 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000491 if (r)
492 return r;
493
494 /* Sector is cropped to 56 bits here */
495 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
496 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
497 buf[2] = cpu_to_le32(4024);
498 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200499 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000500 if (r)
501 return r;
502
503 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200504 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000505 if (r)
506 return r;
507
508 for (i = 0; i < MD5_HASH_WORDS; i++)
509 __cpu_to_le32s(&md5state.hash[i]);
510 memcpy(iv, &md5state.hash, cc->iv_size);
511
512 return 0;
513}
514
515static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
516 struct dm_crypt_request *dmreq)
517{
Milan Brozef43aa32017-01-04 20:23:54 +0100518 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000519 u8 *src;
520 int r = 0;
521
522 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100523 sg = crypt_get_sg_data(cc, dmreq->sg_in);
524 src = kmap_atomic(sg_page(sg));
525 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800526 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000527 } else
528 memset(iv, 0, cc->iv_size);
529
530 return r;
531}
532
533static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
534 struct dm_crypt_request *dmreq)
535{
Milan Brozef43aa32017-01-04 20:23:54 +0100536 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000537 u8 *dst;
538 int r;
539
540 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
541 return 0;
542
Milan Brozef43aa32017-01-04 20:23:54 +0100543 sg = crypt_get_sg_data(cc, dmreq->sg_out);
544 dst = kmap_atomic(sg_page(sg));
545 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset);
Milan Broz34745782011-01-13 19:59:55 +0000546
547 /* Tweak the first block of plaintext sector */
548 if (!r)
Milan Brozef43aa32017-01-04 20:23:54 +0100549 crypto_xor(dst + sg->offset, iv, cc->iv_size);
Milan Broz34745782011-01-13 19:59:55 +0000550
Cong Wangc2e022c2011-11-28 13:26:02 +0800551 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000552 return r;
553}
554
Milan Brozed04d982013-10-28 23:21:04 +0100555static void crypt_iv_tcw_dtr(struct crypt_config *cc)
556{
557 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
558
559 kzfree(tcw->iv_seed);
560 tcw->iv_seed = NULL;
561 kzfree(tcw->whitening);
562 tcw->whitening = NULL;
563
564 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
565 crypto_free_shash(tcw->crc32_tfm);
566 tcw->crc32_tfm = NULL;
567}
568
569static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
570 const char *opts)
571{
572 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
573
Milan Broz8f0009a2017-03-16 15:39:44 +0100574 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
575 ti->error = "Unsupported sector size for TCW";
576 return -EINVAL;
577 }
578
Milan Brozed04d982013-10-28 23:21:04 +0100579 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
580 ti->error = "Wrong key size for TCW";
581 return -EINVAL;
582 }
583
584 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
585 if (IS_ERR(tcw->crc32_tfm)) {
586 ti->error = "Error initializing CRC32 in TCW";
587 return PTR_ERR(tcw->crc32_tfm);
588 }
589
590 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
591 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
592 if (!tcw->iv_seed || !tcw->whitening) {
593 crypt_iv_tcw_dtr(cc);
594 ti->error = "Error allocating seed storage in TCW";
595 return -ENOMEM;
596 }
597
598 return 0;
599}
600
601static int crypt_iv_tcw_init(struct crypt_config *cc)
602{
603 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
604 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
605
606 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
607 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
608 TCW_WHITENING_SIZE);
609
610 return 0;
611}
612
613static int crypt_iv_tcw_wipe(struct crypt_config *cc)
614{
615 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
616
617 memset(tcw->iv_seed, 0, cc->iv_size);
618 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
619
620 return 0;
621}
622
623static int crypt_iv_tcw_whitening(struct crypt_config *cc,
624 struct dm_crypt_request *dmreq,
625 u8 *data)
626{
627 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200628 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100629 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200630 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100631 int i, r;
632
633 /* xor whitening with sector number */
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100634 crypto_xor_cpy(buf, tcw->whitening, (u8 *)&sector, 8);
635 crypto_xor_cpy(&buf[8], tcw->whitening + 8, (u8 *)&sector, 8);
Milan Brozed04d982013-10-28 23:21:04 +0100636
637 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200638 desc->tfm = tcw->crc32_tfm;
Milan Brozed04d982013-10-28 23:21:04 +0100639 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200640 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100641 if (r)
642 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200643 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100644 if (r)
645 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200646 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100647 if (r)
648 goto out;
649 }
650 crypto_xor(&buf[0], &buf[12], 4);
651 crypto_xor(&buf[4], &buf[8], 4);
652
653 /* apply whitening (8 bytes) to whole sector */
654 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
655 crypto_xor(data + i * 8, buf, 8);
656out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100657 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100658 return r;
659}
660
661static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
662 struct dm_crypt_request *dmreq)
663{
Milan Brozef43aa32017-01-04 20:23:54 +0100664 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100665 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200666 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100667 u8 *src;
668 int r = 0;
669
670 /* Remove whitening from ciphertext */
671 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100672 sg = crypt_get_sg_data(cc, dmreq->sg_in);
673 src = kmap_atomic(sg_page(sg));
674 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100675 kunmap_atomic(src);
676 }
677
678 /* Calculate IV */
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100679 crypto_xor_cpy(iv, tcw->iv_seed, (u8 *)&sector, 8);
Milan Brozed04d982013-10-28 23:21:04 +0100680 if (cc->iv_size > 8)
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100681 crypto_xor_cpy(&iv[8], tcw->iv_seed + 8, (u8 *)&sector,
682 cc->iv_size - 8);
Milan Brozed04d982013-10-28 23:21:04 +0100683
684 return r;
685}
686
687static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
688 struct dm_crypt_request *dmreq)
689{
Milan Brozef43aa32017-01-04 20:23:54 +0100690 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100691 u8 *dst;
692 int r;
693
694 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
695 return 0;
696
697 /* Apply whitening on ciphertext */
Milan Brozef43aa32017-01-04 20:23:54 +0100698 sg = crypt_get_sg_data(cc, dmreq->sg_out);
699 dst = kmap_atomic(sg_page(sg));
700 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100701 kunmap_atomic(dst);
702
703 return r;
704}
705
Milan Brozef43aa32017-01-04 20:23:54 +0100706static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv,
707 struct dm_crypt_request *dmreq)
708{
709 /* Used only for writes, there must be an additional space to store IV */
710 get_random_bytes(iv, cc->iv_size);
711 return 0;
712}
713
Milan Brozb9411d72019-07-09 15:22:14 +0200714static int crypt_iv_eboiv_ctr(struct crypt_config *cc, struct dm_target *ti,
715 const char *opts)
716{
Yang Yingliang3fd53532020-02-13 12:11:26 +0800717 if (crypt_integrity_aead(cc)) {
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300718 ti->error = "AEAD transforms not supported for EBOIV";
Milan Brozb9411d72019-07-09 15:22:14 +0200719 return -EINVAL;
720 }
721
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300722 if (crypto_skcipher_blocksize(any_tfm(cc)) != cc->iv_size) {
723 ti->error = "Block size of EBOIV cipher does "
724 "not match IV size of block cipher";
725 return -EINVAL;
726 }
Milan Brozb9411d72019-07-09 15:22:14 +0200727
728 return 0;
729}
730
Milan Brozb9411d72019-07-09 15:22:14 +0200731static int crypt_iv_eboiv_gen(struct crypt_config *cc, u8 *iv,
732 struct dm_crypt_request *dmreq)
733{
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300734 u8 buf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(__le64));
735 struct skcipher_request *req;
736 struct scatterlist src, dst;
737 struct crypto_wait wait;
738 int err;
Milan Brozb9411d72019-07-09 15:22:14 +0200739
Mikulas Patocka9402e952020-01-02 08:23:32 -0500740 req = skcipher_request_alloc(any_tfm(cc), GFP_NOIO);
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300741 if (!req)
742 return -ENOMEM;
Milan Brozb9411d72019-07-09 15:22:14 +0200743
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300744 memset(buf, 0, cc->iv_size);
745 *(__le64 *)buf = cpu_to_le64(dmreq->iv_sector * cc->sector_size);
746
747 sg_init_one(&src, page_address(ZERO_PAGE(0)), cc->iv_size);
748 sg_init_one(&dst, iv, cc->iv_size);
749 skcipher_request_set_crypt(req, &src, &dst, cc->iv_size, buf);
750 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
751 err = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
752 skcipher_request_free(req);
753
754 return err;
Milan Brozb9411d72019-07-09 15:22:14 +0200755}
756
Milan Brozbbb16582020-01-03 09:20:22 +0100757static void crypt_iv_elephant_dtr(struct crypt_config *cc)
758{
759 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant;
760
761 crypto_free_skcipher(elephant->tfm);
762 elephant->tfm = NULL;
763}
764
765static int crypt_iv_elephant_ctr(struct crypt_config *cc, struct dm_target *ti,
766 const char *opts)
767{
768 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant;
769 int r;
770
771 elephant->tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
772 if (IS_ERR(elephant->tfm)) {
773 r = PTR_ERR(elephant->tfm);
774 elephant->tfm = NULL;
775 return r;
776 }
777
778 r = crypt_iv_eboiv_ctr(cc, ti, NULL);
779 if (r)
780 crypt_iv_elephant_dtr(cc);
781 return r;
782}
783
784static void diffuser_disk_to_cpu(u32 *d, size_t n)
785{
786#ifndef __LITTLE_ENDIAN
787 int i;
788
789 for (i = 0; i < n; i++)
790 d[i] = le32_to_cpu((__le32)d[i]);
791#endif
792}
793
794static void diffuser_cpu_to_disk(__le32 *d, size_t n)
795{
796#ifndef __LITTLE_ENDIAN
797 int i;
798
799 for (i = 0; i < n; i++)
800 d[i] = cpu_to_le32((u32)d[i]);
801#endif
802}
803
804static void diffuser_a_decrypt(u32 *d, size_t n)
805{
806 int i, i1, i2, i3;
807
808 for (i = 0; i < 5; i++) {
809 i1 = 0;
810 i2 = n - 2;
811 i3 = n - 5;
812
813 while (i1 < (n - 1)) {
814 d[i1] += d[i2] ^ (d[i3] << 9 | d[i3] >> 23);
815 i1++; i2++; i3++;
816
817 if (i3 >= n)
818 i3 -= n;
819
820 d[i1] += d[i2] ^ d[i3];
821 i1++; i2++; i3++;
822
823 if (i2 >= n)
824 i2 -= n;
825
826 d[i1] += d[i2] ^ (d[i3] << 13 | d[i3] >> 19);
827 i1++; i2++; i3++;
828
829 d[i1] += d[i2] ^ d[i3];
830 i1++; i2++; i3++;
831 }
832 }
833}
834
835static void diffuser_a_encrypt(u32 *d, size_t n)
836{
837 int i, i1, i2, i3;
838
839 for (i = 0; i < 5; i++) {
840 i1 = n - 1;
841 i2 = n - 2 - 1;
842 i3 = n - 5 - 1;
843
844 while (i1 > 0) {
845 d[i1] -= d[i2] ^ d[i3];
846 i1--; i2--; i3--;
847
848 d[i1] -= d[i2] ^ (d[i3] << 13 | d[i3] >> 19);
849 i1--; i2--; i3--;
850
851 if (i2 < 0)
852 i2 += n;
853
854 d[i1] -= d[i2] ^ d[i3];
855 i1--; i2--; i3--;
856
857 if (i3 < 0)
858 i3 += n;
859
860 d[i1] -= d[i2] ^ (d[i3] << 9 | d[i3] >> 23);
861 i1--; i2--; i3--;
862 }
863 }
864}
865
866static void diffuser_b_decrypt(u32 *d, size_t n)
867{
868 int i, i1, i2, i3;
869
870 for (i = 0; i < 3; i++) {
871 i1 = 0;
872 i2 = 2;
873 i3 = 5;
874
875 while (i1 < (n - 1)) {
876 d[i1] += d[i2] ^ d[i3];
877 i1++; i2++; i3++;
878
879 d[i1] += d[i2] ^ (d[i3] << 10 | d[i3] >> 22);
880 i1++; i2++; i3++;
881
882 if (i2 >= n)
883 i2 -= n;
884
885 d[i1] += d[i2] ^ d[i3];
886 i1++; i2++; i3++;
887
888 if (i3 >= n)
889 i3 -= n;
890
891 d[i1] += d[i2] ^ (d[i3] << 25 | d[i3] >> 7);
892 i1++; i2++; i3++;
893 }
894 }
895}
896
897static void diffuser_b_encrypt(u32 *d, size_t n)
898{
899 int i, i1, i2, i3;
900
901 for (i = 0; i < 3; i++) {
902 i1 = n - 1;
903 i2 = 2 - 1;
904 i3 = 5 - 1;
905
906 while (i1 > 0) {
907 d[i1] -= d[i2] ^ (d[i3] << 25 | d[i3] >> 7);
908 i1--; i2--; i3--;
909
910 if (i3 < 0)
911 i3 += n;
912
913 d[i1] -= d[i2] ^ d[i3];
914 i1--; i2--; i3--;
915
916 if (i2 < 0)
917 i2 += n;
918
919 d[i1] -= d[i2] ^ (d[i3] << 10 | d[i3] >> 22);
920 i1--; i2--; i3--;
921
922 d[i1] -= d[i2] ^ d[i3];
923 i1--; i2--; i3--;
924 }
925 }
926}
927
928static int crypt_iv_elephant(struct crypt_config *cc, struct dm_crypt_request *dmreq)
929{
930 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant;
931 u8 *es, *ks, *data, *data2, *data_offset;
932 struct skcipher_request *req;
933 struct scatterlist *sg, *sg2, src, dst;
934 struct crypto_wait wait;
935 int i, r;
936
937 req = skcipher_request_alloc(elephant->tfm, GFP_NOIO);
938 es = kzalloc(16, GFP_NOIO); /* Key for AES */
939 ks = kzalloc(32, GFP_NOIO); /* Elephant sector key */
940
941 if (!req || !es || !ks) {
942 r = -ENOMEM;
943 goto out;
944 }
945
946 *(__le64 *)es = cpu_to_le64(dmreq->iv_sector * cc->sector_size);
947
948 /* E(Ks, e(s)) */
949 sg_init_one(&src, es, 16);
950 sg_init_one(&dst, ks, 16);
951 skcipher_request_set_crypt(req, &src, &dst, 16, NULL);
952 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
953 r = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
954 if (r)
955 goto out;
956
957 /* E(Ks, e'(s)) */
958 es[15] = 0x80;
959 sg_init_one(&dst, &ks[16], 16);
960 r = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
961 if (r)
962 goto out;
963
964 sg = crypt_get_sg_data(cc, dmreq->sg_out);
965 data = kmap_atomic(sg_page(sg));
966 data_offset = data + sg->offset;
967
968 /* Cannot modify original bio, copy to sg_out and apply Elephant to it */
969 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
970 sg2 = crypt_get_sg_data(cc, dmreq->sg_in);
971 data2 = kmap_atomic(sg_page(sg2));
972 memcpy(data_offset, data2 + sg2->offset, cc->sector_size);
973 kunmap_atomic(data2);
974 }
975
976 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
977 diffuser_disk_to_cpu((u32*)data_offset, cc->sector_size / sizeof(u32));
978 diffuser_b_decrypt((u32*)data_offset, cc->sector_size / sizeof(u32));
979 diffuser_a_decrypt((u32*)data_offset, cc->sector_size / sizeof(u32));
980 diffuser_cpu_to_disk((__le32*)data_offset, cc->sector_size / sizeof(u32));
981 }
982
983 for (i = 0; i < (cc->sector_size / 32); i++)
984 crypto_xor(data_offset + i * 32, ks, 32);
985
986 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
987 diffuser_disk_to_cpu((u32*)data_offset, cc->sector_size / sizeof(u32));
988 diffuser_a_encrypt((u32*)data_offset, cc->sector_size / sizeof(u32));
989 diffuser_b_encrypt((u32*)data_offset, cc->sector_size / sizeof(u32));
990 diffuser_cpu_to_disk((__le32*)data_offset, cc->sector_size / sizeof(u32));
991 }
992
993 kunmap_atomic(data);
994out:
995 kzfree(ks);
996 kzfree(es);
997 skcipher_request_free(req);
998 return r;
999}
1000
1001static int crypt_iv_elephant_gen(struct crypt_config *cc, u8 *iv,
1002 struct dm_crypt_request *dmreq)
1003{
1004 int r;
1005
1006 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
1007 r = crypt_iv_elephant(cc, dmreq);
1008 if (r)
1009 return r;
1010 }
1011
1012 return crypt_iv_eboiv_gen(cc, iv, dmreq);
1013}
1014
1015static int crypt_iv_elephant_post(struct crypt_config *cc, u8 *iv,
1016 struct dm_crypt_request *dmreq)
1017{
1018 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
1019 return crypt_iv_elephant(cc, dmreq);
1020
1021 return 0;
1022}
1023
1024static int crypt_iv_elephant_init(struct crypt_config *cc)
1025{
1026 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant;
1027 int key_offset = cc->key_size - cc->key_extra_size;
1028
1029 return crypto_skcipher_setkey(elephant->tfm, &cc->key[key_offset], cc->key_extra_size);
1030}
1031
1032static int crypt_iv_elephant_wipe(struct crypt_config *cc)
1033{
1034 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant;
1035 u8 key[ELEPHANT_MAX_KEY_SIZE];
1036
1037 memset(key, 0, cc->key_extra_size);
1038 return crypto_skcipher_setkey(elephant->tfm, key, cc->key_extra_size);
1039}
1040
Julia Lawall1b1b58f2015-11-29 14:09:19 +01001041static const struct crypt_iv_operations crypt_iv_plain_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 .generator = crypt_iv_plain_gen
1043};
1044
Julia Lawall1b1b58f2015-11-29 14:09:19 +01001045static const struct crypt_iv_operations crypt_iv_plain64_ops = {
Milan Broz61afef62009-12-10 23:52:25 +00001046 .generator = crypt_iv_plain64_gen
1047};
1048
Milan Broz7e3fd852017-06-06 09:07:01 +02001049static const struct crypt_iv_operations crypt_iv_plain64be_ops = {
1050 .generator = crypt_iv_plain64be_gen
1051};
1052
Julia Lawall1b1b58f2015-11-29 14:09:19 +01001053static const struct crypt_iv_operations crypt_iv_essiv_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 .generator = crypt_iv_essiv_gen
1055};
1056
Julia Lawall1b1b58f2015-11-29 14:09:19 +01001057static const struct crypt_iv_operations crypt_iv_benbi_ops = {
Rik Snel48527fa2006-09-03 08:56:39 +10001058 .ctr = crypt_iv_benbi_ctr,
1059 .dtr = crypt_iv_benbi_dtr,
1060 .generator = crypt_iv_benbi_gen
1061};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Julia Lawall1b1b58f2015-11-29 14:09:19 +01001063static const struct crypt_iv_operations crypt_iv_null_ops = {
Ludwig Nussel46b47732007-05-09 02:32:55 -07001064 .generator = crypt_iv_null_gen
1065};
1066
Julia Lawall1b1b58f2015-11-29 14:09:19 +01001067static const struct crypt_iv_operations crypt_iv_lmk_ops = {
Milan Broz34745782011-01-13 19:59:55 +00001068 .ctr = crypt_iv_lmk_ctr,
1069 .dtr = crypt_iv_lmk_dtr,
1070 .init = crypt_iv_lmk_init,
1071 .wipe = crypt_iv_lmk_wipe,
1072 .generator = crypt_iv_lmk_gen,
1073 .post = crypt_iv_lmk_post
1074};
1075
Julia Lawall1b1b58f2015-11-29 14:09:19 +01001076static const struct crypt_iv_operations crypt_iv_tcw_ops = {
Milan Brozed04d982013-10-28 23:21:04 +01001077 .ctr = crypt_iv_tcw_ctr,
1078 .dtr = crypt_iv_tcw_dtr,
1079 .init = crypt_iv_tcw_init,
1080 .wipe = crypt_iv_tcw_wipe,
1081 .generator = crypt_iv_tcw_gen,
1082 .post = crypt_iv_tcw_post
1083};
1084
Milan Brozef43aa32017-01-04 20:23:54 +01001085static struct crypt_iv_operations crypt_iv_random_ops = {
1086 .generator = crypt_iv_random_gen
1087};
1088
Milan Brozb9411d72019-07-09 15:22:14 +02001089static struct crypt_iv_operations crypt_iv_eboiv_ops = {
1090 .ctr = crypt_iv_eboiv_ctr,
Milan Brozb9411d72019-07-09 15:22:14 +02001091 .generator = crypt_iv_eboiv_gen
1092};
1093
Milan Brozbbb16582020-01-03 09:20:22 +01001094static struct crypt_iv_operations crypt_iv_elephant_ops = {
1095 .ctr = crypt_iv_elephant_ctr,
1096 .dtr = crypt_iv_elephant_dtr,
1097 .init = crypt_iv_elephant_init,
1098 .wipe = crypt_iv_elephant_wipe,
1099 .generator = crypt_iv_elephant_gen,
1100 .post = crypt_iv_elephant_post
1101};
1102
Milan Brozef43aa32017-01-04 20:23:54 +01001103/*
1104 * Integrity extensions
1105 */
1106static bool crypt_integrity_aead(struct crypt_config *cc)
1107{
1108 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
1109}
1110
1111static bool crypt_integrity_hmac(struct crypt_config *cc)
1112{
Milan Broz33d2f092017-03-16 15:39:40 +01001113 return crypt_integrity_aead(cc) && cc->key_mac_size;
Milan Brozef43aa32017-01-04 20:23:54 +01001114}
1115
1116/* Get sg containing data */
1117static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
1118 struct scatterlist *sg)
1119{
Milan Broz33d2f092017-03-16 15:39:40 +01001120 if (unlikely(crypt_integrity_aead(cc)))
Milan Brozef43aa32017-01-04 20:23:54 +01001121 return &sg[2];
1122
1123 return sg;
1124}
1125
1126static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
1127{
1128 struct bio_integrity_payload *bip;
1129 unsigned int tag_len;
1130 int ret;
1131
1132 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size)
1133 return 0;
1134
1135 bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
1136 if (IS_ERR(bip))
1137 return PTR_ERR(bip);
1138
Mikulas Patockaff0c1292019-02-08 10:52:07 -05001139 tag_len = io->cc->on_disk_tag_size * (bio_sectors(bio) >> io->cc->sector_shift);
Milan Brozef43aa32017-01-04 20:23:54 +01001140
1141 bip->bip_iter.bi_size = tag_len;
1142 bip->bip_iter.bi_sector = io->cc->start + io->sector;
1143
Milan Brozef43aa32017-01-04 20:23:54 +01001144 ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
1145 tag_len, offset_in_page(io->integrity_metadata));
1146 if (unlikely(ret != tag_len))
1147 return -ENOMEM;
1148
1149 return 0;
1150}
1151
1152static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
1153{
1154#ifdef CONFIG_BLK_DEV_INTEGRITY
1155 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk);
Milan Broz7a1cd722019-05-15 16:23:43 +02001156 struct mapped_device *md = dm_table_get_md(ti->table);
Milan Brozef43aa32017-01-04 20:23:54 +01001157
1158 /* From now we require underlying device with our integrity profile */
1159 if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) {
1160 ti->error = "Integrity profile not supported.";
1161 return -EINVAL;
1162 }
1163
Mikulas Patocka583fe742017-04-18 16:51:54 -04001164 if (bi->tag_size != cc->on_disk_tag_size ||
1165 bi->tuple_size != cc->on_disk_tag_size) {
Milan Brozef43aa32017-01-04 20:23:54 +01001166 ti->error = "Integrity profile tag size mismatch.";
1167 return -EINVAL;
1168 }
Mikulas Patocka583fe742017-04-18 16:51:54 -04001169 if (1 << bi->interval_exp != cc->sector_size) {
1170 ti->error = "Integrity profile sector size mismatch.";
1171 return -EINVAL;
1172 }
Milan Brozef43aa32017-01-04 20:23:54 +01001173
Milan Broz33d2f092017-03-16 15:39:40 +01001174 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +01001175 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size;
Milan Broz7a1cd722019-05-15 16:23:43 +02001176 DMDEBUG("%s: Integrity AEAD, tag size %u, IV size %u.", dm_device_name(md),
Milan Brozef43aa32017-01-04 20:23:54 +01001177 cc->integrity_tag_size, cc->integrity_iv_size);
1178
1179 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) {
1180 ti->error = "Integrity AEAD auth tag size is not supported.";
1181 return -EINVAL;
1182 }
1183 } else if (cc->integrity_iv_size)
Milan Broz7a1cd722019-05-15 16:23:43 +02001184 DMDEBUG("%s: Additional per-sector space %u bytes for IV.", dm_device_name(md),
Milan Brozef43aa32017-01-04 20:23:54 +01001185 cc->integrity_iv_size);
1186
1187 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) {
1188 ti->error = "Not enough space for integrity tag in the profile.";
1189 return -EINVAL;
1190 }
1191
1192 return 0;
1193#else
1194 ti->error = "Integrity profile not supported.";
1195 return -EINVAL;
1196#endif
1197}
1198
Milan Brozd469f842007-10-19 22:42:37 +01001199static void crypt_convert_init(struct crypt_config *cc,
1200 struct convert_context *ctx,
1201 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +00001202 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 ctx->bio_in = bio_in;
1205 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -07001206 if (bio_in)
1207 ctx->iter_in = bio_in->bi_iter;
1208 if (bio_out)
1209 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +01001210 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +00001211 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212}
1213
Huang Yingb2174ee2009-03-16 17:44:33 +00001214static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
Milan Brozef43aa32017-01-04 20:23:54 +01001215 void *req)
Huang Yingb2174ee2009-03-16 17:44:33 +00001216{
1217 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
1218}
1219
Milan Brozef43aa32017-01-04 20:23:54 +01001220static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq)
Huang Yingb2174ee2009-03-16 17:44:33 +00001221{
Milan Brozef43aa32017-01-04 20:23:54 +01001222 return (void *)((char *)dmreq - cc->dmreq_start);
Huang Yingb2174ee2009-03-16 17:44:33 +00001223}
1224
Milan Broz2dc53272011-01-13 19:59:54 +00001225static u8 *iv_of_dmreq(struct crypt_config *cc,
1226 struct dm_crypt_request *dmreq)
1227{
Milan Broz33d2f092017-03-16 15:39:40 +01001228 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001229 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1230 crypto_aead_alignmask(any_tfm_aead(cc)) + 1);
1231 else
1232 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1233 crypto_skcipher_alignmask(any_tfm(cc)) + 1);
Milan Broz2dc53272011-01-13 19:59:54 +00001234}
1235
Milan Brozef43aa32017-01-04 20:23:54 +01001236static u8 *org_iv_of_dmreq(struct crypt_config *cc,
1237 struct dm_crypt_request *dmreq)
1238{
1239 return iv_of_dmreq(cc, dmreq) + cc->iv_size;
1240}
1241
Christoph Hellwigc13b5482019-04-04 18:33:34 +02001242static __le64 *org_sector_of_dmreq(struct crypt_config *cc,
Milan Brozef43aa32017-01-04 20:23:54 +01001243 struct dm_crypt_request *dmreq)
1244{
1245 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size;
Christoph Hellwigc13b5482019-04-04 18:33:34 +02001246 return (__le64 *) ptr;
Milan Brozef43aa32017-01-04 20:23:54 +01001247}
1248
1249static unsigned int *org_tag_of_dmreq(struct crypt_config *cc,
1250 struct dm_crypt_request *dmreq)
1251{
1252 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size +
1253 cc->iv_size + sizeof(uint64_t);
1254 return (unsigned int*)ptr;
1255}
1256
1257static void *tag_from_dmreq(struct crypt_config *cc,
1258 struct dm_crypt_request *dmreq)
1259{
1260 struct convert_context *ctx = dmreq->ctx;
1261 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1262
1263 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) *
1264 cc->on_disk_tag_size];
1265}
1266
1267static void *iv_tag_from_dmreq(struct crypt_config *cc,
1268 struct dm_crypt_request *dmreq)
1269{
1270 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size;
1271}
1272
1273static int crypt_convert_block_aead(struct crypt_config *cc,
1274 struct convert_context *ctx,
1275 struct aead_request *req,
1276 unsigned int tag_offset)
Milan Broz01482b72008-02-08 02:11:04 +00001277{
Kent Overstreet003b5c52013-10-11 15:45:43 -07001278 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1279 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001280 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001281 u8 *iv, *org_iv, *tag_iv, *tag;
Christoph Hellwigc13b5482019-04-04 18:33:34 +02001282 __le64 *sector;
Milan Brozef43aa32017-01-04 20:23:54 +01001283 int r = 0;
1284
1285 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size);
Milan Broz01482b72008-02-08 02:11:04 +00001286
Milan Broz8f0009a2017-03-16 15:39:44 +01001287 /* Reject unexpected unaligned bio. */
Mikulas Patocka0440d5c2017-11-07 10:35:57 -05001288 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Broz8f0009a2017-03-16 15:39:44 +01001289 return -EIO;
Milan Broz01482b72008-02-08 02:11:04 +00001290
Huang Yingb2174ee2009-03-16 17:44:33 +00001291 dmreq = dmreq_of_req(cc, req);
Mikulas Patockac66029f2012-07-27 15:08:05 +01001292 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001293 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001294 dmreq->iv_sector >>= cc->sector_shift;
Huang Yingb2174ee2009-03-16 17:44:33 +00001295 dmreq->ctx = ctx;
Milan Broz01482b72008-02-08 02:11:04 +00001296
Milan Brozef43aa32017-01-04 20:23:54 +01001297 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
Milan Broz01482b72008-02-08 02:11:04 +00001298
Milan Brozef43aa32017-01-04 20:23:54 +01001299 sector = org_sector_of_dmreq(cc, dmreq);
1300 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1301
1302 iv = iv_of_dmreq(cc, dmreq);
1303 org_iv = org_iv_of_dmreq(cc, dmreq);
1304 tag = tag_from_dmreq(cc, dmreq);
1305 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1306
1307 /* AEAD request:
1308 * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
1309 * | (authenticated) | (auth+encryption) | |
1310 * | sector_LE | IV | sector in/out | tag in/out |
1311 */
1312 sg_init_table(dmreq->sg_in, 4);
1313 sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t));
1314 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001315 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 +01001316 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size);
1317
1318 sg_init_table(dmreq->sg_out, 4);
1319 sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t));
1320 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001321 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 +01001322 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size);
Milan Broz01482b72008-02-08 02:11:04 +00001323
Milan Broz3a7f6c92008-02-08 02:11:14 +00001324 if (cc->iv_gen_ops) {
Milan Brozef43aa32017-01-04 20:23:54 +01001325 /* For READs use IV stored in integrity metadata */
1326 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1327 memcpy(org_iv, tag_iv, cc->iv_size);
1328 } else {
1329 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1330 if (r < 0)
1331 return r;
1332 /* Store generated IV in integrity metadata */
1333 if (cc->integrity_iv_size)
1334 memcpy(tag_iv, org_iv, cc->iv_size);
1335 }
1336 /* Working copy of IV, to be modified in crypto API */
1337 memcpy(iv, org_iv, cc->iv_size);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001338 }
1339
Milan Brozef43aa32017-01-04 20:23:54 +01001340 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size);
1341 if (bio_data_dir(ctx->bio_in) == WRITE) {
1342 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001343 cc->sector_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001344 r = crypto_aead_encrypt(req);
1345 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size)
1346 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0,
1347 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size));
1348 } else {
1349 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001350 cc->sector_size + cc->integrity_tag_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001351 r = crypto_aead_decrypt(req);
1352 }
1353
Milan Brozf7101262019-05-15 16:22:30 +02001354 if (r == -EBADMSG) {
1355 char b[BDEVNAME_SIZE];
1356 DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu", bio_devname(ctx->bio_in, b),
Milan Brozef43aa32017-01-04 20:23:54 +01001357 (unsigned long long)le64_to_cpu(*sector));
Milan Brozf7101262019-05-15 16:22:30 +02001358 }
Milan Brozef43aa32017-01-04 20:23:54 +01001359
1360 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
1361 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1362
Milan Broz8f0009a2017-03-16 15:39:44 +01001363 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1364 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001365
1366 return r;
1367}
1368
1369static int crypt_convert_block_skcipher(struct crypt_config *cc,
1370 struct convert_context *ctx,
1371 struct skcipher_request *req,
1372 unsigned int tag_offset)
1373{
1374 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1375 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
1376 struct scatterlist *sg_in, *sg_out;
1377 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001378 u8 *iv, *org_iv, *tag_iv;
Christoph Hellwigc13b5482019-04-04 18:33:34 +02001379 __le64 *sector;
Milan Brozef43aa32017-01-04 20:23:54 +01001380 int r = 0;
1381
Milan Broz8f0009a2017-03-16 15:39:44 +01001382 /* Reject unexpected unaligned bio. */
Mikulas Patocka0440d5c2017-11-07 10:35:57 -05001383 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Broz8f0009a2017-03-16 15:39:44 +01001384 return -EIO;
1385
Milan Brozef43aa32017-01-04 20:23:54 +01001386 dmreq = dmreq_of_req(cc, req);
1387 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001388 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001389 dmreq->iv_sector >>= cc->sector_shift;
Milan Brozef43aa32017-01-04 20:23:54 +01001390 dmreq->ctx = ctx;
1391
1392 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
1393
1394 iv = iv_of_dmreq(cc, dmreq);
1395 org_iv = org_iv_of_dmreq(cc, dmreq);
1396 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1397
1398 sector = org_sector_of_dmreq(cc, dmreq);
1399 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1400
1401 /* For skcipher we use only the first sg item */
1402 sg_in = &dmreq->sg_in[0];
1403 sg_out = &dmreq->sg_out[0];
1404
1405 sg_init_table(sg_in, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001406 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001407
1408 sg_init_table(sg_out, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001409 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001410
1411 if (cc->iv_gen_ops) {
1412 /* For READs use IV stored in integrity metadata */
1413 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1414 memcpy(org_iv, tag_iv, cc->integrity_iv_size);
1415 } else {
1416 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1417 if (r < 0)
1418 return r;
Milan Brozbbb16582020-01-03 09:20:22 +01001419 /* Data can be already preprocessed in generator */
1420 if (test_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags))
1421 sg_in = sg_out;
Milan Brozef43aa32017-01-04 20:23:54 +01001422 /* Store generated IV in integrity metadata */
1423 if (cc->integrity_iv_size)
1424 memcpy(tag_iv, org_iv, cc->integrity_iv_size);
1425 }
1426 /* Working copy of IV, to be modified in crypto API */
1427 memcpy(iv, org_iv, cc->iv_size);
1428 }
1429
Milan Broz8f0009a2017-03-16 15:39:44 +01001430 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001431
1432 if (bio_data_dir(ctx->bio_in) == WRITE)
Herbert Xubbdb23b2016-01-24 21:16:36 +08001433 r = crypto_skcipher_encrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001434 else
Herbert Xubbdb23b2016-01-24 21:16:36 +08001435 r = crypto_skcipher_decrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001436
Milan Broz2dc53272011-01-13 19:59:54 +00001437 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001438 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1439
Milan Broz8f0009a2017-03-16 15:39:44 +01001440 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1441 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Broz2dc53272011-01-13 19:59:54 +00001442
Milan Broz3a7f6c92008-02-08 02:11:14 +00001443 return r;
Milan Broz01482b72008-02-08 02:11:04 +00001444}
1445
Milan Broz95497a92008-02-08 02:11:12 +00001446static void kcryptd_async_done(struct crypto_async_request *async_req,
1447 int error);
Andi Kleenc0297722011-01-13 19:59:53 +00001448
Milan Brozef43aa32017-01-04 20:23:54 +01001449static void crypt_alloc_req_skcipher(struct crypt_config *cc,
1450 struct convert_context *ctx)
Milan Brozddd42ed2008-02-08 02:11:07 +00001451{
Mikulas Patockac66029f2012-07-27 15:08:05 +01001452 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +00001453
Milan Brozef43aa32017-01-04 20:23:54 +01001454 if (!ctx->r.req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001455 ctx->r.req = mempool_alloc(&cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +00001456
Milan Brozef43aa32017-01-04 20:23:54 +01001457 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]);
Milan Broz54cea3f2015-05-15 17:00:25 +02001458
1459 /*
1460 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1461 * requests if driver request queue is full.
1462 */
Milan Brozef43aa32017-01-04 20:23:54 +01001463 skcipher_request_set_callback(ctx->r.req,
Mikulas Patocka432061b2018-09-05 09:17:45 -04001464 CRYPTO_TFM_REQ_MAY_BACKLOG,
Milan Brozef43aa32017-01-04 20:23:54 +01001465 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req));
Milan Brozddd42ed2008-02-08 02:11:07 +00001466}
1467
Milan Brozef43aa32017-01-04 20:23:54 +01001468static void crypt_alloc_req_aead(struct crypt_config *cc,
1469 struct convert_context *ctx)
1470{
1471 if (!ctx->r.req_aead)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001472 ctx->r.req_aead = mempool_alloc(&cc->req_pool, GFP_NOIO);
Milan Brozef43aa32017-01-04 20:23:54 +01001473
1474 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]);
1475
1476 /*
1477 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1478 * requests if driver request queue is full.
1479 */
1480 aead_request_set_callback(ctx->r.req_aead,
Mikulas Patocka432061b2018-09-05 09:17:45 -04001481 CRYPTO_TFM_REQ_MAY_BACKLOG,
Milan Brozef43aa32017-01-04 20:23:54 +01001482 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead));
1483}
1484
1485static void crypt_alloc_req(struct crypt_config *cc,
1486 struct convert_context *ctx)
1487{
Milan Broz33d2f092017-03-16 15:39:40 +01001488 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001489 crypt_alloc_req_aead(cc, ctx);
1490 else
1491 crypt_alloc_req_skcipher(cc, ctx);
1492}
1493
1494static void crypt_free_req_skcipher(struct crypt_config *cc,
1495 struct skcipher_request *req, struct bio *base_bio)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001496{
1497 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1498
Herbert Xubbdb23b2016-01-24 21:16:36 +08001499 if ((struct skcipher_request *)(io + 1) != req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001500 mempool_free(req, &cc->req_pool);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001501}
1502
Milan Brozef43aa32017-01-04 20:23:54 +01001503static void crypt_free_req_aead(struct crypt_config *cc,
1504 struct aead_request *req, struct bio *base_bio)
1505{
1506 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1507
1508 if ((struct aead_request *)(io + 1) != req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001509 mempool_free(req, &cc->req_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01001510}
1511
1512static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio)
1513{
Milan Broz33d2f092017-03-16 15:39:40 +01001514 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001515 crypt_free_req_aead(cc, req, base_bio);
1516 else
1517 crypt_free_req_skcipher(cc, req, base_bio);
1518}
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520/*
1521 * Encrypt / decrypt data from one bio to another one (can be the same one)
1522 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001523static blk_status_t crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +01001524 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Milan Brozef43aa32017-01-04 20:23:54 +01001526 unsigned int tag_offset = 0;
Mikulas Patockaff3af922017-03-23 10:23:14 -04001527 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT;
Milan Broz3f1e9072008-03-28 14:16:07 -07001528 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Mikulas Patocka40b62292012-07-27 15:08:04 +01001530 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +01001531
Kent Overstreet003b5c52013-10-11 15:45:43 -07001532 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Milan Broz3a7f6c92008-02-08 02:11:14 +00001534 crypt_alloc_req(cc, ctx);
Mikulas Patocka40b62292012-07-27 15:08:04 +01001535 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -07001536
Milan Broz33d2f092017-03-16 15:39:40 +01001537 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001538 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset);
1539 else
1540 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001541
1542 switch (r) {
Milan Broz54cea3f2015-05-15 17:00:25 +02001543 /*
1544 * The request was queued by a crypto driver
1545 * but the driver request queue is full, let's wait.
1546 */
Milan Broz3a7f6c92008-02-08 02:11:14 +00001547 case -EBUSY:
1548 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -08001549 reinit_completion(&ctx->restart);
Milan Broz54cea3f2015-05-15 17:00:25 +02001550 /* fall through */
1551 /*
1552 * The request is queued and processed asynchronously,
1553 * completion function kcryptd_async_done() will be called.
1554 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001555 case -EINPROGRESS:
Milan Brozef43aa32017-01-04 20:23:54 +01001556 ctx->r.req = NULL;
Milan Broz8f0009a2017-03-16 15:39:44 +01001557 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001558 tag_offset++;
Milan Broz3a7f6c92008-02-08 02:11:14 +00001559 continue;
Milan Broz54cea3f2015-05-15 17:00:25 +02001560 /*
1561 * The request was already processed (synchronously).
1562 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001563 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001564 atomic_dec(&ctx->cc_pending);
Milan Broz8f0009a2017-03-16 15:39:44 +01001565 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001566 tag_offset++;
Milan Brozc7f1b202008-07-02 09:34:28 +01001567 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -07001568 continue;
Milan Brozef43aa32017-01-04 20:23:54 +01001569 /*
1570 * There was a data integrity error.
1571 */
1572 case -EBADMSG:
1573 atomic_dec(&ctx->cc_pending);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001574 return BLK_STS_PROTECTION;
Milan Brozef43aa32017-01-04 20:23:54 +01001575 /*
1576 * There was an error while processing the request.
1577 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001578 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001579 atomic_dec(&ctx->cc_pending);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001580 return BLK_STS_IOERR;
Milan Broz3f1e9072008-03-28 14:16:07 -07001581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583
Milan Broz3f1e9072008-03-28 14:16:07 -07001584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001587static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589/*
1590 * Generate a new unfragmented bio with the given size
Mike Snitzer586b2862015-09-09 21:34:51 -04001591 * This should never violate the device limitations (but only because
1592 * max_segment_size is being constrained to PAGE_SIZE).
Mikulas Patocka7145c242015-02-13 08:24:41 -05001593 *
1594 * This function may be called concurrently. If we allocate from the mempool
1595 * concurrently, there is a possibility of deadlock. For example, if we have
1596 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1597 * the mempool concurrently, it may deadlock in a situation where both processes
1598 * have allocated 128 pages and the mempool is exhausted.
1599 *
1600 * In order to avoid this scenario we allocate the pages under a mutex.
1601 *
1602 * In order to not degrade performance with excessive locking, we try
1603 * non-blocking allocations without a mutex first but on failure we fallback
1604 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001606static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001608 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001609 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001611 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
1612 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +00001613 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Mikulas Patocka7145c242015-02-13 08:24:41 -05001615retry:
Mel Gormand0164ad2015-11-06 16:28:21 -08001616 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001617 mutex_lock(&cc->bio_alloc_lock);
1618
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001619 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, &cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -07001620 if (!clone)
Milan Brozef43aa32017-01-04 20:23:54 +01001621 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Olaf Kirch027581f2007-05-09 02:32:52 -07001623 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -07001624
Mikulas Patocka7145c242015-02-13 08:24:41 -05001625 remaining_size = size;
1626
Olaf Kirchf97380b2007-05-09 02:32:54 -07001627 for (i = 0; i < nr_iovecs; i++) {
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001628 page = mempool_alloc(&cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -05001629 if (!page) {
1630 crypt_free_buffer_pages(cc, clone);
1631 bio_put(clone);
Mel Gormand0164ad2015-11-06 16:28:21 -08001632 gfp_mask |= __GFP_DIRECT_RECLAIM;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001633 goto retry;
1634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Mikulas Patocka7145c242015-02-13 08:24:41 -05001636 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Ming Lei0dae7fe2016-10-29 16:08:06 +08001638 bio_add_page(clone, page, len, 0);
Milan Broz91e10622007-12-13 14:16:10 +00001639
Mikulas Patocka7145c242015-02-13 08:24:41 -05001640 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642
Milan Brozef43aa32017-01-04 20:23:54 +01001643 /* Allocate space for integrity tags */
1644 if (dm_crypt_integrity_io_alloc(io, clone)) {
1645 crypt_free_buffer_pages(cc, clone);
1646 bio_put(clone);
1647 clone = NULL;
1648 }
1649out:
Mel Gormand0164ad2015-11-06 16:28:21 -08001650 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001651 mutex_unlock(&cc->bio_alloc_lock);
1652
Milan Broz8b004452006-10-03 01:15:37 -07001653 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
Neil Brown644bd2f2007-10-16 13:48:46 +02001656static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 struct bio_vec *bv;
Ming Lei6dc4f102019-02-15 19:13:19 +08001659 struct bvec_iter_all iter_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02001661 bio_for_each_segment_all(bv, clone, iter_all) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 BUG_ON(!bv->bv_page);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001663 mempool_free(bv->bv_page, &cc->page_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665}
1666
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001667static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1668 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001669{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001670 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001671 io->base_bio = bio;
1672 io->sector = sector;
1673 io->error = 0;
Milan Brozef43aa32017-01-04 20:23:54 +01001674 io->ctx.r.req = NULL;
1675 io->integrity_metadata = NULL;
1676 io->integrity_metadata_from_pool = false;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001677 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001678}
1679
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001680static void crypt_inc_pending(struct dm_crypt_io *io)
1681{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001682 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001683}
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685/*
1686 * One of the bios was finished. Check for completion of
1687 * the whole request and correctly clean up the buffer.
1688 */
Milan Broz5742fd72008-02-08 02:10:43 +00001689static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001691 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001692 struct bio *base_bio = io->base_bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001693 blk_status_t error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Mikulas Patocka40b62292012-07-27 15:08:04 +01001695 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return;
1697
Milan Brozef43aa32017-01-04 20:23:54 +01001698 if (io->ctx.r.req)
1699 crypt_free_req(cc, io->ctx.r.req, base_bio);
1700
1701 if (unlikely(io->integrity_metadata_from_pool))
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001702 mempool_free(io->integrity_metadata, &io->cc->tag_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01001703 else
1704 kfree(io->integrity_metadata);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001705
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001706 base_bio->bi_status = error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001707 bio_endio(base_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
1710/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001711 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 *
1713 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001714 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001715 *
1716 * kcryptd performs the actual encryption or decryption.
1717 *
1718 * kcryptd_io performs the IO submission.
1719 *
1720 * They must be separated as otherwise the final stages could be
1721 * starved by new requests which can block in the first stages due
1722 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001723 *
1724 * The work is done per CPU global for all dm-crypt instances.
1725 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001727static void crypt_endio(struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001728{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001729 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001730 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001731 unsigned rw = bio_data_dir(clone);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001732 blk_status_t error;
Milan Broz8b004452006-10-03 01:15:37 -07001733
1734 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001735 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001736 */
Milan Brozee7a4912008-02-08 02:10:46 +00001737 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001738 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001739
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001740 error = clone->bi_status;
Milan Brozee7a4912008-02-08 02:10:46 +00001741 bio_put(clone);
1742
Sasha Levin9b81c842015-08-10 19:05:18 -04001743 if (rw == READ && !error) {
Milan Brozee7a4912008-02-08 02:10:46 +00001744 kcryptd_queue_crypt(io);
1745 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001746 }
Milan Broz8b004452006-10-03 01:15:37 -07001747
Sasha Levin9b81c842015-08-10 19:05:18 -04001748 if (unlikely(error))
1749 io->error = error;
Milan Broz5742fd72008-02-08 02:10:43 +00001750
1751 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001752}
1753
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001754static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001755{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001756 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001757
1758 clone->bi_private = io;
1759 clone->bi_end_io = crypt_endio;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001760 bio_set_dev(clone, cc->dev->bdev);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001761 clone->bi_opf = io->base_bio->bi_opf;
Milan Broz8b004452006-10-03 01:15:37 -07001762}
1763
Milan Broz20c82532011-01-13 19:59:53 +00001764static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001765{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001766 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001767 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001768
Milan Broz8b004452006-10-03 01:15:37 -07001769 /*
Mike Snitzer59779072015-04-09 16:53:24 -04001770 * We need the original biovec array in order to decrypt
1771 * the whole bio data *afterwards* -- thanks to immutable
1772 * biovecs we don't need to worry about the block layer
1773 * modifying the biovec array; so leverage bio_clone_fast().
Milan Broz8b004452006-10-03 01:15:37 -07001774 */
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001775 clone = bio_clone_fast(io->base_bio, gfp, &cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001776 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001777 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001778
Milan Broz20c82532011-01-13 19:59:53 +00001779 crypt_inc_pending(io);
1780
Milan Broz8b004452006-10-03 01:15:37 -07001781 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001782 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001783
Milan Brozef43aa32017-01-04 20:23:54 +01001784 if (dm_crypt_integrity_io_alloc(io, clone)) {
1785 crypt_dec_pending(io);
1786 bio_put(clone);
1787 return 1;
1788 }
1789
Milan Broz93e605c2006-10-03 01:15:38 -07001790 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001791 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001792}
1793
Mikulas Patockadc267622015-02-13 08:25:59 -05001794static void kcryptd_io_read_work(struct work_struct *work)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001795{
1796 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1797
Mikulas Patockadc267622015-02-13 08:25:59 -05001798 crypt_inc_pending(io);
1799 if (kcryptd_io_read(io, GFP_NOIO))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001800 io->error = BLK_STS_RESOURCE;
Mikulas Patockadc267622015-02-13 08:25:59 -05001801 crypt_dec_pending(io);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001802}
1803
Mikulas Patockadc267622015-02-13 08:25:59 -05001804static void kcryptd_queue_read(struct dm_crypt_io *io)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001805{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001806 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001807
Mikulas Patockadc267622015-02-13 08:25:59 -05001808 INIT_WORK(&io->work, kcryptd_io_read_work);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001809 queue_work(cc->io_queue, &io->work);
1810}
1811
Mikulas Patockadc267622015-02-13 08:25:59 -05001812static void kcryptd_io_write(struct dm_crypt_io *io)
1813{
1814 struct bio *clone = io->ctx.bio_out;
1815
1816 generic_make_request(clone);
1817}
1818
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001819#define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1820
Mikulas Patockadc267622015-02-13 08:25:59 -05001821static int dmcrypt_write(void *data)
1822{
1823 struct crypt_config *cc = data;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001824 struct dm_crypt_io *io;
1825
Mikulas Patockadc267622015-02-13 08:25:59 -05001826 while (1) {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001827 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -05001828 struct blk_plug plug;
1829
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001830 spin_lock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001831continue_locked:
1832
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001833 if (!RB_EMPTY_ROOT(&cc->write_tree))
Mikulas Patockadc267622015-02-13 08:25:59 -05001834 goto pop_from_list;
1835
Rabin Vincentf659b102016-09-21 16:22:29 +02001836 set_current_state(TASK_INTERRUPTIBLE);
Mikulas Patockadc267622015-02-13 08:25:59 -05001837
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001838 spin_unlock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001839
Rabin Vincentf659b102016-09-21 16:22:29 +02001840 if (unlikely(kthread_should_stop())) {
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001841 set_current_state(TASK_RUNNING);
Rabin Vincentf659b102016-09-21 16:22:29 +02001842 break;
1843 }
1844
Mikulas Patockadc267622015-02-13 08:25:59 -05001845 schedule();
1846
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001847 set_current_state(TASK_RUNNING);
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001848 spin_lock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001849 goto continue_locked;
1850
1851pop_from_list:
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001852 write_tree = cc->write_tree;
1853 cc->write_tree = RB_ROOT;
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001854 spin_unlock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001855
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001856 BUG_ON(rb_parent(write_tree.rb_node));
1857
1858 /*
1859 * Note: we cannot walk the tree here with rb_next because
1860 * the structures may be freed when kcryptd_io_write is called.
1861 */
Mikulas Patockadc267622015-02-13 08:25:59 -05001862 blk_start_plug(&plug);
1863 do {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001864 io = crypt_io_from_node(rb_first(&write_tree));
1865 rb_erase(&io->rb_node, &write_tree);
Mikulas Patockadc267622015-02-13 08:25:59 -05001866 kcryptd_io_write(io);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001867 } while (!RB_EMPTY_ROOT(&write_tree));
Mikulas Patockadc267622015-02-13 08:25:59 -05001868 blk_finish_plug(&plug);
1869 }
1870 return 0;
1871}
1872
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001873static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001874{
Milan Brozdec1ced2008-02-08 02:10:57 +00001875 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001876 struct crypt_config *cc = io->cc;
Mikulas Patockadc267622015-02-13 08:25:59 -05001877 unsigned long flags;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001878 sector_t sector;
1879 struct rb_node **rbp, *parent;
Milan Brozdec1ced2008-02-08 02:10:57 +00001880
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001881 if (unlikely(io->error)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001882 crypt_free_buffer_pages(cc, clone);
1883 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001884 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001885 return;
1886 }
1887
1888 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001889 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001890
Kent Overstreet4f024f32013-10-11 15:44:27 -07001891 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001892
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001893 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1894 generic_make_request(clone);
1895 return;
1896 }
1897
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001898 spin_lock_irqsave(&cc->write_thread_lock, flags);
1899 if (RB_EMPTY_ROOT(&cc->write_tree))
1900 wake_up_process(cc->write_thread);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001901 rbp = &cc->write_tree.rb_node;
1902 parent = NULL;
1903 sector = io->sector;
1904 while (*rbp) {
1905 parent = *rbp;
1906 if (sector < crypt_io_from_node(parent)->sector)
1907 rbp = &(*rbp)->rb_left;
1908 else
1909 rbp = &(*rbp)->rb_right;
1910 }
1911 rb_link_node(&io->rb_node, parent, rbp);
1912 rb_insert_color(&io->rb_node, &cc->write_tree);
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001913 spin_unlock_irqrestore(&cc->write_thread_lock, flags);
Milan Broz4e4eef62008-02-08 02:10:49 +00001914}
1915
Milan Brozfc5a5e92008-10-10 13:37:04 +01001916static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001917{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001918 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001919 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001920 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001921 sector_t sector = io->sector;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001922 blk_status_t r;
Milan Broz8b004452006-10-03 01:15:37 -07001923
Milan Broz93e605c2006-10-03 01:15:38 -07001924 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001925 * Prevent io from disappearing until this function completes.
1926 */
1927 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001928 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001929
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001930 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1931 if (unlikely(!clone)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001932 io->error = BLK_STS_IOERR;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001933 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001934 }
Milan Broz899c95d2008-02-08 02:11:02 +00001935
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001936 io->ctx.bio_out = clone;
1937 io->ctx.iter_out = clone->bi_iter;
1938
1939 sector += bio_sectors(clone);
1940
1941 crypt_inc_pending(io);
1942 r = crypt_convert(cc, &io->ctx);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001943 if (r)
Milan Brozef43aa32017-01-04 20:23:54 +01001944 io->error = r;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001945 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1946
1947 /* Encryption was already finished, submit io now */
1948 if (crypt_finished) {
1949 kcryptd_crypt_write_io_submit(io, 0);
1950 io->sector = sector;
1951 }
1952
1953dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001954 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001955}
1956
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001957static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001958{
Milan Broz5742fd72008-02-08 02:10:43 +00001959 crypt_dec_pending(io);
1960}
1961
Milan Broz4e4eef62008-02-08 02:10:49 +00001962static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001963{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001964 struct crypt_config *cc = io->cc;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001965 blk_status_t r;
Milan Broz8b004452006-10-03 01:15:37 -07001966
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001967 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001968
Milan Broz53017032008-02-08 02:10:38 +00001969 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001970 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001971
Milan Broz5742fd72008-02-08 02:10:43 +00001972 r = crypt_convert(cc, &io->ctx);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001973 if (r)
Milan Brozef43aa32017-01-04 20:23:54 +01001974 io->error = r;
Milan Broz5742fd72008-02-08 02:10:43 +00001975
Mikulas Patocka40b62292012-07-27 15:08:04 +01001976 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001977 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001978
1979 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001980}
1981
Milan Broz95497a92008-02-08 02:11:12 +00001982static void kcryptd_async_done(struct crypto_async_request *async_req,
1983 int error)
1984{
Huang Yingb2174ee2009-03-16 17:44:33 +00001985 struct dm_crypt_request *dmreq = async_req->data;
1986 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001987 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001988 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001989
Milan Broz54cea3f2015-05-15 17:00:25 +02001990 /*
1991 * A request from crypto driver backlog is going to be processed now,
1992 * finish the completion and continue in crypt_convert().
1993 * (Callback will be called for the second time for this request.)
1994 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001995 if (error == -EINPROGRESS) {
1996 complete(&ctx->restart);
Milan Broz95497a92008-02-08 02:11:12 +00001997 return;
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001998 }
Milan Broz95497a92008-02-08 02:11:12 +00001999
Milan Broz2dc53272011-01-13 19:59:54 +00002000 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01002001 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq);
Milan Broz2dc53272011-01-13 19:59:54 +00002002
Milan Brozef43aa32017-01-04 20:23:54 +01002003 if (error == -EBADMSG) {
Milan Brozf7101262019-05-15 16:22:30 +02002004 char b[BDEVNAME_SIZE];
2005 DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu", bio_devname(ctx->bio_in, b),
Milan Brozef43aa32017-01-04 20:23:54 +01002006 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002007 io->error = BLK_STS_PROTECTION;
Milan Brozef43aa32017-01-04 20:23:54 +01002008 } else if (error < 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002009 io->error = BLK_STS_IOERR;
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01002010
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002011 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00002012
Mikulas Patocka40b62292012-07-27 15:08:04 +01002013 if (!atomic_dec_and_test(&ctx->cc_pending))
Rabin Vincentc0403ec2015-05-05 15:15:56 +02002014 return;
Milan Broz95497a92008-02-08 02:11:12 +00002015
2016 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01002017 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00002018 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01002019 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00002020}
2021
Milan Broz4e4eef62008-02-08 02:10:49 +00002022static void kcryptd_crypt(struct work_struct *work)
2023{
2024 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
2025
2026 if (bio_data_dir(io->base_bio) == READ)
2027 kcryptd_crypt_read_convert(io);
2028 else
2029 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07002030}
2031
Alasdair G Kergon395b1672008-02-08 02:10:52 +00002032static void kcryptd_queue_crypt(struct dm_crypt_io *io)
2033{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01002034 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00002035
2036 INIT_WORK(&io->work, kcryptd_crypt);
2037 queue_work(cc->crypt_queue, &io->work);
2038}
2039
Milan Brozef43aa32017-01-04 20:23:54 +01002040static void crypt_free_tfms_aead(struct crypt_config *cc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Milan Brozef43aa32017-01-04 20:23:54 +01002042 if (!cc->cipher_tfm.tfms_aead)
2043 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Milan Brozef43aa32017-01-04 20:23:54 +01002045 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
2046 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]);
2047 cc->cipher_tfm.tfms_aead[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
2049
Milan Brozef43aa32017-01-04 20:23:54 +01002050 kfree(cc->cipher_tfm.tfms_aead);
2051 cc->cipher_tfm.tfms_aead = NULL;
2052}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Milan Brozef43aa32017-01-04 20:23:54 +01002054static void crypt_free_tfms_skcipher(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00002055{
Milan Brozd1f96422011-01-13 19:59:54 +00002056 unsigned i;
2057
Milan Brozef43aa32017-01-04 20:23:54 +01002058 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002059 return;
2060
Milan Brozd1f96422011-01-13 19:59:54 +00002061 for (i = 0; i < cc->tfms_count; i++)
Milan Brozef43aa32017-01-04 20:23:54 +01002062 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) {
2063 crypto_free_skcipher(cc->cipher_tfm.tfms[i]);
2064 cc->cipher_tfm.tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00002065 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002066
Milan Brozef43aa32017-01-04 20:23:54 +01002067 kfree(cc->cipher_tfm.tfms);
2068 cc->cipher_tfm.tfms = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
2071static void crypt_free_tfms(struct crypt_config *cc)
2072{
Milan Broz33d2f092017-03-16 15:39:40 +01002073 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002074 crypt_free_tfms_aead(cc);
2075 else
2076 crypt_free_tfms_skcipher(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00002077}
2078
Milan Brozef43aa32017-01-04 20:23:54 +01002079static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00002080{
Milan Brozd1f96422011-01-13 19:59:54 +00002081 unsigned i;
2082 int err;
2083
Kees Cook6396bb22018-06-12 14:03:40 -07002084 cc->cipher_tfm.tfms = kcalloc(cc->tfms_count,
2085 sizeof(struct crypto_skcipher *),
2086 GFP_KERNEL);
Milan Brozef43aa32017-01-04 20:23:54 +01002087 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002088 return -ENOMEM;
2089
Milan Brozd1f96422011-01-13 19:59:54 +00002090 for (i = 0; i < cc->tfms_count; i++) {
Milan Brozef43aa32017-01-04 20:23:54 +01002091 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
2092 if (IS_ERR(cc->cipher_tfm.tfms[i])) {
2093 err = PTR_ERR(cc->cipher_tfm.tfms[i]);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002094 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00002095 return err;
2096 }
2097 }
2098
Eric Biggersaf331eb2018-12-05 20:53:00 -08002099 /*
2100 * dm-crypt performance can vary greatly depending on which crypto
2101 * algorithm implementation is used. Help people debug performance
2102 * problems by logging the ->cra_driver_name.
2103 */
Milan Broz7a1cd722019-05-15 16:23:43 +02002104 DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode,
Eric Biggersaf331eb2018-12-05 20:53:00 -08002105 crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name);
Milan Brozd1f96422011-01-13 19:59:54 +00002106 return 0;
2107}
2108
Milan Brozef43aa32017-01-04 20:23:54 +01002109static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
2110{
Milan Brozef43aa32017-01-04 20:23:54 +01002111 int err;
2112
2113 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
2114 if (!cc->cipher_tfm.tfms)
2115 return -ENOMEM;
2116
Milan Brozef43aa32017-01-04 20:23:54 +01002117 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
2118 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
2119 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
2120 crypt_free_tfms(cc);
2121 return err;
2122 }
2123
Milan Broz7a1cd722019-05-15 16:23:43 +02002124 DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode,
Eric Biggersaf331eb2018-12-05 20:53:00 -08002125 crypto_aead_alg(any_tfm_aead(cc))->base.cra_driver_name);
Milan Brozef43aa32017-01-04 20:23:54 +01002126 return 0;
2127}
2128
2129static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
2130{
Milan Broz33d2f092017-03-16 15:39:40 +01002131 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002132 return crypt_alloc_tfms_aead(cc, ciphermode);
2133 else
2134 return crypt_alloc_tfms_skcipher(cc, ciphermode);
2135}
2136
2137static unsigned crypt_subkey_size(struct crypt_config *cc)
2138{
2139 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
2140}
2141
2142static unsigned crypt_authenckey_size(struct crypt_config *cc)
2143{
2144 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
2145}
2146
2147/*
2148 * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
2149 * the key must be for some reason in special format.
2150 * This funcion converts cc->key to this special format.
2151 */
2152static void crypt_copy_authenckey(char *p, const void *key,
2153 unsigned enckeylen, unsigned authkeylen)
2154{
2155 struct crypto_authenc_key_param *param;
2156 struct rtattr *rta;
2157
2158 rta = (struct rtattr *)p;
2159 param = RTA_DATA(rta);
2160 param->enckeylen = cpu_to_be32(enckeylen);
2161 rta->rta_len = RTA_LENGTH(sizeof(*param));
2162 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
2163 p += RTA_SPACE(sizeof(*param));
2164 memcpy(p, key + enckeylen, authkeylen);
2165 p += authkeylen;
2166 memcpy(p, key, enckeylen);
2167}
2168
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002169static int crypt_setkey(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +00002170{
Milan Brozda31a072013-10-28 23:21:03 +01002171 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002172 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00002173
Milan Brozda31a072013-10-28 23:21:03 +01002174 /* Ignore extra keys (which are used for IV etc) */
Milan Brozef43aa32017-01-04 20:23:54 +01002175 subkey_size = crypt_subkey_size(cc);
Milan Brozda31a072013-10-28 23:21:03 +01002176
Milan Broz27c70032018-01-03 22:48:59 +01002177 if (crypt_integrity_hmac(cc)) {
2178 if (subkey_size < cc->key_mac_size)
2179 return -EINVAL;
2180
Milan Brozef43aa32017-01-04 20:23:54 +01002181 crypt_copy_authenckey(cc->authenc_key, cc->key,
2182 subkey_size - cc->key_mac_size,
2183 cc->key_mac_size);
Milan Broz27c70032018-01-03 22:48:59 +01002184 }
2185
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002186 for (i = 0; i < cc->tfms_count; i++) {
Milan Broz33d2f092017-03-16 15:39:40 +01002187 if (crypt_integrity_hmac(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002188 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
2189 cc->authenc_key, crypt_authenckey_size(cc));
Milan Broz33d2f092017-03-16 15:39:40 +01002190 else if (crypt_integrity_aead(cc))
2191 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
2192 cc->key + (i * subkey_size),
2193 subkey_size);
Milan Brozef43aa32017-01-04 20:23:54 +01002194 else
2195 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i],
2196 cc->key + (i * subkey_size),
2197 subkey_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002198 if (r)
2199 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00002200 }
2201
Milan Brozef43aa32017-01-04 20:23:54 +01002202 if (crypt_integrity_hmac(cc))
2203 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc));
2204
Andi Kleenc0297722011-01-13 19:59:53 +00002205 return err;
2206}
2207
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002208#ifdef CONFIG_KEYS
2209
Ondrej Kozina027c4312016-12-01 18:20:52 +01002210static bool contains_whitespace(const char *str)
2211{
2212 while (*str)
2213 if (isspace(*str++))
2214 return true;
2215 return false;
2216}
2217
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002218static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2219{
2220 char *new_key_string, *key_desc;
2221 int ret;
2222 struct key *key;
2223 const struct user_key_payload *ukp;
2224
Ondrej Kozina027c4312016-12-01 18:20:52 +01002225 /*
2226 * Reject key_string with whitespace. dm core currently lacks code for
2227 * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
2228 */
2229 if (contains_whitespace(key_string)) {
2230 DMERR("whitespace chars not allowed in key string");
2231 return -EINVAL;
2232 }
2233
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002234 /* look for next ':' separating key_type from key_description */
2235 key_desc = strpbrk(key_string, ":");
2236 if (!key_desc || key_desc == key_string || !strlen(key_desc + 1))
2237 return -EINVAL;
2238
2239 if (strncmp(key_string, "logon:", key_desc - key_string + 1) &&
2240 strncmp(key_string, "user:", key_desc - key_string + 1))
2241 return -EINVAL;
2242
2243 new_key_string = kstrdup(key_string, GFP_KERNEL);
2244 if (!new_key_string)
2245 return -ENOMEM;
2246
2247 key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user,
2248 key_desc + 1, NULL);
2249 if (IS_ERR(key)) {
2250 kzfree(new_key_string);
2251 return PTR_ERR(key);
2252 }
2253
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002254 down_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002255
David Howells0837e492017-03-01 15:11:23 +00002256 ukp = user_key_payload_locked(key);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002257 if (!ukp) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002258 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002259 key_put(key);
2260 kzfree(new_key_string);
2261 return -EKEYREVOKED;
2262 }
2263
2264 if (cc->key_size != ukp->datalen) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002265 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002266 key_put(key);
2267 kzfree(new_key_string);
2268 return -EINVAL;
2269 }
2270
2271 memcpy(cc->key, ukp->data, cc->key_size);
2272
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002273 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002274 key_put(key);
2275
2276 /* clear the flag since following operations may invalidate previously valid key */
2277 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2278
2279 ret = crypt_setkey(cc);
2280
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002281 if (!ret) {
2282 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2283 kzfree(cc->key_string);
2284 cc->key_string = new_key_string;
2285 } else
2286 kzfree(new_key_string);
2287
2288 return ret;
2289}
2290
2291static int get_key_size(char **key_string)
2292{
2293 char *colon, dummy;
2294 int ret;
2295
2296 if (*key_string[0] != ':')
2297 return strlen(*key_string) >> 1;
2298
2299 /* look for next ':' in key string */
2300 colon = strpbrk(*key_string + 1, ":");
2301 if (!colon)
2302 return -EINVAL;
2303
2304 if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':')
2305 return -EINVAL;
2306
2307 *key_string = colon;
2308
2309 /* remaining key string should be :<logon|user>:<key_desc> */
2310
2311 return ret;
2312}
2313
2314#else
2315
2316static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2317{
2318 return -EINVAL;
2319}
2320
2321static int get_key_size(char **key_string)
2322{
2323 return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1;
2324}
2325
2326#endif
2327
Milan Broze48d4bb2006-10-03 01:15:37 -07002328static int crypt_set_key(struct crypt_config *cc, char *key)
2329{
Milan Brozde8be5a2011-03-24 13:54:27 +00002330 int r = -EINVAL;
2331 int key_string_len = strlen(key);
2332
Milan Broz69a8cfc2011-01-13 19:59:49 +00002333 /* Hyphen (which gives a key_size of zero) means there is no key. */
2334 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00002335 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002336
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002337 /* ':' means the key is in kernel keyring, short-circuit normal key processing */
2338 if (key[0] == ':') {
2339 r = crypt_set_keyring_key(cc, key + 1);
2340 goto out;
2341 }
2342
Ondrej Kozina265e9092016-11-02 15:02:08 +01002343 /* clear the flag since following operations may invalidate previously valid key */
2344 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2345
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002346 /* wipe references to any kernel keyring key */
2347 kzfree(cc->key_string);
2348 cc->key_string = NULL;
2349
Andy Shevchenkoe944e032017-04-27 16:52:04 +03002350 /* Decode key from its hex representation. */
2351 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00002352 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002353
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002354 r = crypt_setkey(cc);
Ondrej Kozina265e9092016-11-02 15:02:08 +01002355 if (!r)
2356 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Milan Brozde8be5a2011-03-24 13:54:27 +00002357
2358out:
2359 /* Hex key string not needed after here, so wipe it. */
2360 memset(key, '0', key_string_len);
2361
2362 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002363}
2364
2365static int crypt_wipe_key(struct crypt_config *cc)
2366{
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002367 int r;
2368
Milan Broze48d4bb2006-10-03 01:15:37 -07002369 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002370 get_random_bytes(&cc->key, cc->key_size);
Milan Broz4a52ffc2019-07-09 15:22:12 +02002371
2372 /* Wipe IV private keys */
2373 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
2374 r = cc->iv_gen_ops->wipe(cc);
2375 if (r)
2376 return r;
2377 }
2378
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002379 kzfree(cc->key_string);
2380 cc->key_string = NULL;
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002381 r = crypt_setkey(cc);
2382 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00002383
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002384 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002385}
2386
Mikulas Patocka50593532017-08-13 22:45:08 -04002387static void crypt_calculate_pages_per_client(void)
2388{
Arun KSca79b0c2018-12-28 00:34:29 -08002389 unsigned long pages = (totalram_pages() - totalhigh_pages()) * DM_CRYPT_MEMORY_PERCENT / 100;
Mikulas Patocka50593532017-08-13 22:45:08 -04002390
2391 if (!dm_crypt_clients_n)
2392 return;
2393
2394 pages /= dm_crypt_clients_n;
2395 if (pages < DM_CRYPT_MIN_PAGES_PER_CLIENT)
2396 pages = DM_CRYPT_MIN_PAGES_PER_CLIENT;
2397 dm_crypt_pages_per_client = pages;
2398}
2399
2400static void *crypt_page_alloc(gfp_t gfp_mask, void *pool_data)
2401{
2402 struct crypt_config *cc = pool_data;
2403 struct page *page;
2404
2405 if (unlikely(percpu_counter_compare(&cc->n_allocated_pages, dm_crypt_pages_per_client) >= 0) &&
2406 likely(gfp_mask & __GFP_NORETRY))
2407 return NULL;
2408
2409 page = alloc_page(gfp_mask);
2410 if (likely(page != NULL))
2411 percpu_counter_add(&cc->n_allocated_pages, 1);
2412
2413 return page;
2414}
2415
2416static void crypt_page_free(void *page, void *pool_data)
2417{
2418 struct crypt_config *cc = pool_data;
2419
2420 __free_page(page);
2421 percpu_counter_sub(&cc->n_allocated_pages, 1);
2422}
2423
Milan Broz28513fc2010-08-12 04:14:06 +01002424static void crypt_dtr(struct dm_target *ti)
2425{
2426 struct crypt_config *cc = ti->private;
2427
2428 ti->private = NULL;
2429
2430 if (!cc)
2431 return;
2432
Rabin Vincentf659b102016-09-21 16:22:29 +02002433 if (cc->write_thread)
Mikulas Patockadc267622015-02-13 08:25:59 -05002434 kthread_stop(cc->write_thread);
2435
Milan Broz28513fc2010-08-12 04:14:06 +01002436 if (cc->io_queue)
2437 destroy_workqueue(cc->io_queue);
2438 if (cc->crypt_queue)
2439 destroy_workqueue(cc->crypt_queue);
2440
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002441 crypt_free_tfms(cc);
2442
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002443 bioset_exit(&cc->bs);
Milan Broz28513fc2010-08-12 04:14:06 +01002444
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002445 mempool_exit(&cc->page_pool);
2446 mempool_exit(&cc->req_pool);
2447 mempool_exit(&cc->tag_pool);
2448
Kent Overstreetd00a11d2018-06-02 13:45:04 -04002449 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0);
2450 percpu_counter_destroy(&cc->n_allocated_pages);
2451
Milan Broz28513fc2010-08-12 04:14:06 +01002452 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
2453 cc->iv_gen_ops->dtr(cc);
2454
Milan Broz28513fc2010-08-12 04:14:06 +01002455 if (cc->dev)
2456 dm_put_device(ti, cc->dev);
2457
Milan Broz7dbcd132011-01-13 19:59:52 +00002458 kzfree(cc->cipher_string);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002459 kzfree(cc->key_string);
Milan Brozef43aa32017-01-04 20:23:54 +01002460 kzfree(cc->cipher_auth);
2461 kzfree(cc->authenc_key);
Milan Broz28513fc2010-08-12 04:14:06 +01002462
Mike Snitzerd5ffebd2018-01-05 21:17:20 -05002463 mutex_destroy(&cc->bio_alloc_lock);
2464
Milan Broz28513fc2010-08-12 04:14:06 +01002465 /* Must zero key material before freeing */
2466 kzfree(cc);
Mikulas Patocka50593532017-08-13 22:45:08 -04002467
2468 spin_lock(&dm_crypt_clients_lock);
2469 WARN_ON(!dm_crypt_clients_n);
2470 dm_crypt_clients_n--;
2471 crypt_calculate_pages_per_client();
2472 spin_unlock(&dm_crypt_clients_lock);
Milan Broz28513fc2010-08-12 04:14:06 +01002473}
2474
Milan Broze889f972017-03-16 15:39:39 +01002475static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
Milan Broz5ebaee62010-08-12 04:14:07 +01002477 struct crypt_config *cc = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Milan Broz33d2f092017-03-16 15:39:40 +01002479 if (crypt_integrity_aead(cc))
Milan Broze889f972017-03-16 15:39:39 +01002480 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2481 else
2482 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Milan Broz5ebaee62010-08-12 04:14:07 +01002484 if (cc->iv_size)
2485 /* at least a 64 bit sector number should fit in our buffer */
2486 cc->iv_size = max(cc->iv_size,
2487 (unsigned int)(sizeof(u64) / sizeof(u8)));
2488 else if (ivmode) {
2489 DMWARN("Selected cipher does not support IVs");
2490 ivmode = NULL;
2491 }
2492
2493 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 if (ivmode == NULL)
2495 cc->iv_gen_ops = NULL;
2496 else if (strcmp(ivmode, "plain") == 0)
2497 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00002498 else if (strcmp(ivmode, "plain64") == 0)
2499 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Milan Broz7e3fd852017-06-06 09:07:01 +02002500 else if (strcmp(ivmode, "plain64be") == 0)
2501 cc->iv_gen_ops = &crypt_iv_plain64be_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 else if (strcmp(ivmode, "essiv") == 0)
2503 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10002504 else if (strcmp(ivmode, "benbi") == 0)
2505 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07002506 else if (strcmp(ivmode, "null") == 0)
2507 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Brozb9411d72019-07-09 15:22:14 +02002508 else if (strcmp(ivmode, "eboiv") == 0)
2509 cc->iv_gen_ops = &crypt_iv_eboiv_ops;
Milan Brozbbb16582020-01-03 09:20:22 +01002510 else if (strcmp(ivmode, "elephant") == 0) {
2511 cc->iv_gen_ops = &crypt_iv_elephant_ops;
2512 cc->key_parts = 2;
2513 cc->key_extra_size = cc->key_size / 2;
2514 if (cc->key_extra_size > ELEPHANT_MAX_KEY_SIZE)
2515 return -EINVAL;
2516 set_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags);
2517 } else if (strcmp(ivmode, "lmk") == 0) {
Milan Broz34745782011-01-13 19:59:55 +00002518 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01002519 /*
2520 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00002521 * to length of provided multi-key string.
2522 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01002523 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00002524 */
Milan Brozda31a072013-10-28 23:21:03 +01002525 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00002526 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01002527 cc->key_extra_size = cc->key_size / cc->key_parts;
2528 }
Milan Brozed04d982013-10-28 23:21:04 +01002529 } else if (strcmp(ivmode, "tcw") == 0) {
2530 cc->iv_gen_ops = &crypt_iv_tcw_ops;
2531 cc->key_parts += 2; /* IV + whitening */
2532 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broze889f972017-03-16 15:39:39 +01002533 } else if (strcmp(ivmode, "random") == 0) {
2534 cc->iv_gen_ops = &crypt_iv_random_ops;
2535 /* Need storage space in integrity fields. */
2536 cc->integrity_iv_size = cc->iv_size;
Milan Broz34745782011-01-13 19:59:55 +00002537 } else {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002538 ti->error = "Invalid IV mode";
Milan Broze889f972017-03-16 15:39:39 +01002539 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
2541
Milan Broze889f972017-03-16 15:39:39 +01002542 return 0;
2543}
2544
Milan Broz33d2f092017-03-16 15:39:40 +01002545/*
Milan Broz33d2f092017-03-16 15:39:40 +01002546 * Workaround to parse HMAC algorithm from AEAD crypto API spec.
2547 * The HMAC is needed to calculate tag size (HMAC digest size).
2548 * This should be probably done by crypto-api calls (once available...)
2549 */
2550static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
2551{
2552 char *start, *end, *mac_alg = NULL;
2553 struct crypto_ahash *mac;
2554
2555 if (!strstarts(cipher_api, "authenc("))
2556 return 0;
2557
2558 start = strchr(cipher_api, '(');
2559 end = strchr(cipher_api, ',');
2560 if (!start || !end || ++start > end)
2561 return -EINVAL;
2562
2563 mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
2564 if (!mac_alg)
2565 return -ENOMEM;
2566 strncpy(mac_alg, start, end - start);
2567
2568 mac = crypto_alloc_ahash(mac_alg, 0, 0);
2569 kfree(mac_alg);
2570
2571 if (IS_ERR(mac))
2572 return PTR_ERR(mac);
2573
2574 cc->key_mac_size = crypto_ahash_digestsize(mac);
2575 crypto_free_ahash(mac);
2576
2577 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
2578 if (!cc->authenc_key)
2579 return -ENOMEM;
2580
2581 return 0;
2582}
2583
2584static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key,
2585 char **ivmode, char **ivopts)
Milan Broz5ebaee62010-08-12 04:14:07 +01002586{
2587 struct crypt_config *cc = ti->private;
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002588 char *tmp, *cipher_api, buf[CRYPTO_MAX_ALG_NAME];
Milan Broz33d2f092017-03-16 15:39:40 +01002589 int ret = -EINVAL;
2590
2591 cc->tfms_count = 1;
2592
2593 /*
2594 * New format (capi: prefix)
2595 * capi:cipher_api_spec-iv:ivopts
2596 */
2597 tmp = &cipher_in[strlen("capi:")];
Milan Broz1856b9f2019-01-09 11:57:14 +01002598
2599 /* Separate IV options if present, it can contain another '-' in hash name */
2600 *ivopts = strrchr(tmp, ':');
2601 if (*ivopts) {
2602 **ivopts = '\0';
2603 (*ivopts)++;
2604 }
2605 /* Parse IV mode */
2606 *ivmode = strrchr(tmp, '-');
2607 if (*ivmode) {
2608 **ivmode = '\0';
2609 (*ivmode)++;
2610 }
2611 /* The rest is crypto API spec */
2612 cipher_api = tmp;
Milan Broz33d2f092017-03-16 15:39:40 +01002613
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002614 /* Alloc AEAD, can be used only in new format. */
2615 if (crypt_integrity_aead(cc)) {
2616 ret = crypt_ctr_auth_cipher(cc, cipher_api);
2617 if (ret < 0) {
2618 ti->error = "Invalid AEAD cipher spec";
2619 return -ENOMEM;
2620 }
2621 }
2622
Milan Broz33d2f092017-03-16 15:39:40 +01002623 if (*ivmode && !strcmp(*ivmode, "lmk"))
2624 cc->tfms_count = 64;
2625
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002626 if (*ivmode && !strcmp(*ivmode, "essiv")) {
2627 if (!*ivopts) {
2628 ti->error = "Digest algorithm missing for ESSIV mode";
2629 return -EINVAL;
2630 }
2631 ret = snprintf(buf, CRYPTO_MAX_ALG_NAME, "essiv(%s,%s)",
2632 cipher_api, *ivopts);
2633 if (ret < 0 || ret >= CRYPTO_MAX_ALG_NAME) {
2634 ti->error = "Cannot allocate cipher string";
2635 return -ENOMEM;
2636 }
2637 cipher_api = buf;
2638 }
2639
Milan Broz33d2f092017-03-16 15:39:40 +01002640 cc->key_parts = cc->tfms_count;
2641
2642 /* Allocate cipher */
2643 ret = crypt_alloc_tfms(cc, cipher_api);
2644 if (ret < 0) {
2645 ti->error = "Error allocating crypto tfm";
2646 return ret;
2647 }
2648
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002649 if (crypt_integrity_aead(cc))
Milan Broz33d2f092017-03-16 15:39:40 +01002650 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002651 else
Milan Broz33d2f092017-03-16 15:39:40 +01002652 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2653
Milan Broz33d2f092017-03-16 15:39:40 +01002654 return 0;
2655}
2656
2657static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
2658 char **ivmode, char **ivopts)
2659{
2660 struct crypt_config *cc = ti->private;
2661 char *tmp, *cipher, *chainmode, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01002662 char *cipher_api = NULL;
2663 int ret = -EINVAL;
2664 char dummy;
2665
Milan Broz33d2f092017-03-16 15:39:40 +01002666 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002667 ti->error = "Bad cipher specification";
2668 return -EINVAL;
2669 }
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 /*
Milan Broz5ebaee62010-08-12 04:14:07 +01002672 * Legacy dm-crypt cipher specification
2673 * cipher[:keycount]-mode-iv:ivopts
2674 */
2675 tmp = cipher_in;
2676 keycount = strsep(&tmp, "-");
2677 cipher = strsep(&keycount, ":");
2678
Milan Broz69a8cfc2011-01-13 19:59:49 +00002679 if (!keycount)
Milan Broz5ebaee62010-08-12 04:14:07 +01002680 cc->tfms_count = 1;
2681 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
2682 !is_power_of_2(cc->tfms_count)) {
2683 ti->error = "Bad cipher key count specification";
2684 return -EINVAL;
2685 }
Milan Broz28513fc2010-08-12 04:14:06 +01002686 cc->key_parts = cc->tfms_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Milan Brozddd42ed2008-02-08 02:11:07 +00002688 chainmode = strsep(&tmp, "-");
Milan Broz1856b9f2019-01-09 11:57:14 +01002689 *ivmode = strsep(&tmp, ":");
2690 *ivopts = tmp;
Milan Brozddd42ed2008-02-08 02:11:07 +00002691
2692 /*
2693 * For compatibility with the original dm-crypt mapping format, if
2694 * only the cipher name is supplied, use cbc-plain.
Milan Broz28513fc2010-08-12 04:14:06 +01002695 */
Milan Broz33d2f092017-03-16 15:39:40 +01002696 if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002697 chainmode = "cbc";
Milan Broz33d2f092017-03-16 15:39:40 +01002698 *ivmode = "plain";
Milan Brozcabf08e2007-10-19 22:38:58 +01002699 }
2700
Milan Broz33d2f092017-03-16 15:39:40 +01002701 if (strcmp(chainmode, "ecb") && !*ivmode) {
Andi Kleenc0297722011-01-13 19:59:53 +00002702 ti->error = "IV mechanism required";
2703 return -EINVAL;
2704 }
2705
Milan Brozcabf08e2007-10-19 22:38:58 +01002706 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
Milan Broz9934a8b2007-10-19 22:38:57 +01002707 if (!cipher_api)
Milan Broz28513fc2010-08-12 04:14:06 +01002708 goto bad_mem;
Milan Broz9934a8b2007-10-19 22:38:57 +01002709
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002710 if (*ivmode && !strcmp(*ivmode, "essiv")) {
2711 if (!*ivopts) {
2712 ti->error = "Digest algorithm missing for ESSIV mode";
2713 kfree(cipher_api);
2714 return -EINVAL;
2715 }
2716 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
2717 "essiv(%s(%s),%s)", chainmode, cipher, *ivopts);
2718 } else {
2719 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
2720 "%s(%s)", chainmode, cipher);
2721 }
2722 if (ret < 0 || ret >= CRYPTO_MAX_ALG_NAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 kfree(cipher_api);
Milan Broz28513fc2010-08-12 04:14:06 +01002724 goto bad_mem;
2725 }
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /* Allocate cipher */
2728 ret = crypt_alloc_tfms(cc, cipher_api);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 if (ret < 0) {
2730 ti->error = "Error allocating crypto tfm";
Milan Broz33d2f092017-03-16 15:39:40 +01002731 kfree(cipher_api);
2732 return ret;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002733 }
Jeffy Chenbd86e322017-09-27 20:28:57 +08002734 kfree(cipher_api);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002735
Milan Broz33d2f092017-03-16 15:39:40 +01002736 return 0;
2737bad_mem:
2738 ti->error = "Cannot allocate cipher strings";
2739 return -ENOMEM;
2740}
2741
2742static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key)
2743{
2744 struct crypt_config *cc = ti->private;
2745 char *ivmode = NULL, *ivopts = NULL;
2746 int ret;
2747
2748 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
2749 if (!cc->cipher_string) {
2750 ti->error = "Cannot allocate cipher strings";
2751 return -ENOMEM;
2752 }
2753
2754 if (strstarts(cipher_in, "capi:"))
2755 ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts);
2756 else
2757 ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts);
2758 if (ret)
2759 return ret;
2760
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002761 /* Initialize IV */
Milan Broze889f972017-03-16 15:39:39 +01002762 ret = crypt_ctr_ivmode(ti, ivmode);
2763 if (ret < 0)
Milan Broz33d2f092017-03-16 15:39:40 +01002764 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Milan Brozda31a072013-10-28 23:21:03 +01002766 /* Initialize and set key */
2767 ret = crypt_set_key(cc, key);
2768 if (ret < 0) {
2769 ti->error = "Error decoding and setting key";
Milan Broz33d2f092017-03-16 15:39:40 +01002770 return ret;
Milan Brozda31a072013-10-28 23:21:03 +01002771 }
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 /* Allocate IV */
2774 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
2775 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
2776 if (ret < 0) {
2777 ti->error = "Error creating IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002778 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
2780 }
2781
2782 /* Initialize IV (set keys for ESSIV etc) */
2783 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
2784 ret = cc->iv_gen_ops->init(cc);
2785 if (ret < 0) {
2786 ti->error = "Error initialising IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002787 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 }
2789 }
2790
Ondrej Kozinadc949022018-01-12 16:30:32 +01002791 /* wipe the kernel key payload copy */
2792 if (cc->key_string)
2793 memset(cc->key, 0, cc->key_size * sizeof(u8));
2794
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Milan Brozef43aa32017-01-04 20:23:54 +01002798static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
2799{
2800 struct crypt_config *cc = ti->private;
2801 struct dm_arg_set as;
Eric Biggers5916a222017-06-22 11:32:45 -07002802 static const struct dm_arg _args[] = {
Milan Broz8f0009a2017-03-16 15:39:44 +01002803 {0, 6, "Invalid number of feature args"},
Milan Brozef43aa32017-01-04 20:23:54 +01002804 };
2805 unsigned int opt_params, val;
2806 const char *opt_string, *sval;
Milan Broz8f0009a2017-03-16 15:39:44 +01002807 char dummy;
Milan Brozef43aa32017-01-04 20:23:54 +01002808 int ret;
2809
2810 /* Optional parameters */
2811 as.argc = argc;
2812 as.argv = argv;
2813
2814 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2815 if (ret)
2816 return ret;
2817
2818 while (opt_params--) {
2819 opt_string = dm_shift_arg(&as);
2820 if (!opt_string) {
2821 ti->error = "Not enough feature arguments";
2822 return -EINVAL;
2823 }
2824
2825 if (!strcasecmp(opt_string, "allow_discards"))
2826 ti->num_discard_bios = 1;
2827
2828 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
2829 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
2830
2831 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
2832 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
2833 else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
2834 if (val == 0 || val > MAX_TAG_SIZE) {
2835 ti->error = "Invalid integrity arguments";
2836 return -EINVAL;
2837 }
2838 cc->on_disk_tag_size = val;
2839 sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
2840 if (!strcasecmp(sval, "aead")) {
2841 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002842 } else if (strcasecmp(sval, "none")) {
2843 ti->error = "Unknown integrity profile";
2844 return -EINVAL;
2845 }
2846
2847 cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
2848 if (!cc->cipher_auth)
2849 return -ENOMEM;
Mikulas Patockaff3af922017-03-23 10:23:14 -04002850 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002851 if (cc->sector_size < (1 << SECTOR_SHIFT) ||
2852 cc->sector_size > 4096 ||
Mikulas Patockaff3af922017-03-23 10:23:14 -04002853 (cc->sector_size & (cc->sector_size - 1))) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002854 ti->error = "Invalid feature value for sector_size";
2855 return -EINVAL;
2856 }
Milan Broz783874b2017-09-13 15:45:56 +02002857 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) {
2858 ti->error = "Device size is not multiple of sector_size feature";
2859 return -EINVAL;
2860 }
Mikulas Patockaff3af922017-03-23 10:23:14 -04002861 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT;
Milan Broz8f0009a2017-03-16 15:39:44 +01002862 } else if (!strcasecmp(opt_string, "iv_large_sectors"))
2863 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
2864 else {
Milan Brozef43aa32017-01-04 20:23:54 +01002865 ti->error = "Invalid feature arguments";
2866 return -EINVAL;
2867 }
2868 }
2869
2870 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
2873/*
2874 * Construct an encryption mapping:
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002875 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 */
2877static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2878{
2879 struct crypt_config *cc;
Michał Mirosławed0302e2018-10-09 22:13:43 +02002880 const char *devname = dm_table_device_name(ti->table);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002881 int key_size;
Milan Brozef43aa32017-01-04 20:23:54 +01002882 unsigned int align_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 unsigned long long tmpll;
2884 int ret;
Milan Brozef43aa32017-01-04 20:23:54 +01002885 size_t iv_size_padding, additional_req_size;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002886 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Milan Broz772ae5f2011-08-02 12:32:08 +01002888 if (argc < 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 ti->error = "Not enough arguments";
2890 return -EINVAL;
2891 }
2892
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002893 key_size = get_key_size(&argv[1]);
2894 if (key_size < 0) {
2895 ti->error = "Cannot parse key size";
2896 return -EINVAL;
2897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Zhengyuan Liu9c81c992019-06-12 14:14:45 +08002899 cc = kzalloc(struct_size(cc, key, key_size), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 if (!cc) {
2901 ti->error = "Cannot allocate encryption context";
2902 return -ENOMEM;
2903 }
2904 cc->key_size = key_size;
Milan Broz8f0009a2017-03-16 15:39:44 +01002905 cc->sector_size = (1 << SECTOR_SHIFT);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002906 cc->sector_shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 ti->private = cc;
Milan Brozef43aa32017-01-04 20:23:54 +01002909
Mikulas Patocka50593532017-08-13 22:45:08 -04002910 spin_lock(&dm_crypt_clients_lock);
2911 dm_crypt_clients_n++;
2912 crypt_calculate_pages_per_client();
2913 spin_unlock(&dm_crypt_clients_lock);
2914
2915 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL);
2916 if (ret < 0)
2917 goto bad;
2918
Milan Brozef43aa32017-01-04 20:23:54 +01002919 /* Optional parameters need to be read before cipher constructor */
2920 if (argc > 5) {
2921 ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
2922 if (ret)
2923 goto bad;
2924 }
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
2927 if (ret < 0)
2928 goto bad;
2929
Milan Broz33d2f092017-03-16 15:39:40 +01002930 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +01002931 cc->dmreq_start = sizeof(struct aead_request);
2932 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
2933 align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
2934 } else {
2935 cc->dmreq_start = sizeof(struct skcipher_request);
2936 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
2937 align_mask = crypto_skcipher_alignmask(any_tfm(cc));
2938 }
Mikulas Patockad49ec522014-08-28 11:09:31 -04002939 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
2940
Milan Brozef43aa32017-01-04 20:23:54 +01002941 if (align_mask < CRYPTO_MINALIGN) {
Mikulas Patockad49ec522014-08-28 11:09:31 -04002942 /* Allocate the padding exactly */
2943 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
Milan Brozef43aa32017-01-04 20:23:54 +01002944 & align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002945 } else {
2946 /*
2947 * If the cipher requires greater alignment than kmalloc
2948 * alignment, we don't know the exact position of the
2949 * initialization vector. We must assume worst case.
2950 */
Milan Brozef43aa32017-01-04 20:23:54 +01002951 iv_size_padding = align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Milan Brozef43aa32017-01-04 20:23:54 +01002954 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
2955 additional_req_size = sizeof(struct dm_crypt_request) +
2956 iv_size_padding + cc->iv_size +
2957 cc->iv_size +
2958 sizeof(uint64_t) +
2959 sizeof(unsigned int);
2960
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002961 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size);
2962 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 ti->error = "Cannot allocate crypt request mempool";
2964 goto bad;
2965 }
2966
Mike Snitzer30187e12016-01-31 13:28:26 -05002967 cc->per_bio_data_size = ti->per_io_data_size =
Milan Brozef43aa32017-01-04 20:23:54 +01002968 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
Mikulas Patockad49ec522014-08-28 11:09:31 -04002969 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002970
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002971 ret = mempool_init(&cc->page_pool, BIO_MAX_PAGES, crypt_page_alloc, crypt_page_free, cc);
2972 if (ret) {
Milan Broz8b004452006-10-03 01:15:37 -07002973 ti->error = "Cannot allocate page mempool";
Milan Broze48d4bb2006-10-03 01:15:37 -07002974 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002976
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002977 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS);
2978 if (ret) {
Milan Broz0c395b02008-02-08 02:10:54 +00002979 ti->error = "Cannot allocate crypt bioset";
Milan Brozcabf08e2007-10-19 22:38:58 +01002980 goto bad;
Milan Broz93e605c2006-10-03 01:15:38 -07002981 }
Milan Brozcabf08e2007-10-19 22:38:58 +01002982
Mikulas Patocka7145c242015-02-13 08:24:41 -05002983 mutex_init(&cc->bio_alloc_lock);
2984
Milan Brozcabf08e2007-10-19 22:38:58 +01002985 ret = -EINVAL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002986 if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
2987 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002988 ti->error = "Invalid iv_offset sector";
2989 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002991 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Vivek Goyale80d1c82015-07-31 09:20:36 -04002993 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
2994 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 ti->error = "Device lookup failed";
2996 goto bad;
2997 }
2998
Vivek Goyale80d1c82015-07-31 09:20:36 -04002999 ret = -EINVAL;
Milan Brozef87bfc2018-11-07 22:24:55 +01003000 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 ti->error = "Invalid device sector";
3002 goto bad;
3003 }
3004 cc->start = tmpll;
3005
Milan Broz33d2f092017-03-16 15:39:40 +01003006 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
Milan Brozef43aa32017-01-04 20:23:54 +01003007 ret = crypt_integrity_ctr(cc, ti);
Milan Broz772ae5f2011-08-02 12:32:08 +01003008 if (ret)
3009 goto bad;
3010
Milan Brozef43aa32017-01-04 20:23:54 +01003011 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
3012 if (!cc->tag_pool_max_sectors)
3013 cc->tag_pool_max_sectors = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01003014
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003015 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS,
Milan Brozef43aa32017-01-04 20:23:54 +01003016 cc->tag_pool_max_sectors * cc->on_disk_tag_size);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003017 if (ret) {
Milan Brozef43aa32017-01-04 20:23:54 +01003018 ti->error = "Cannot allocate integrity tags mempool";
3019 goto bad;
Milan Broz772ae5f2011-08-02 12:32:08 +01003020 }
Mikulas Patocka583fe742017-04-18 16:51:54 -04003021
3022 cc->tag_pool_max_sectors <<= cc->sector_shift;
Milan Broz772ae5f2011-08-02 12:32:08 +01003023 }
3024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 ret = -ENOMEM;
Mike Snitzerf612b212019-11-20 17:27:39 -05003026 cc->io_queue = alloc_workqueue("kcryptd_io/%s", WQ_MEM_RECLAIM, 1, devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 if (!cc->io_queue) {
3028 ti->error = "Couldn't create kcryptd io queue";
3029 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
Christophe Saout37af6562006-10-30 20:39:08 +01003031
Mikulas Patockaf3396c582015-02-13 08:23:09 -05003032 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
Mike Snitzerf612b212019-11-20 17:27:39 -05003033 cc->crypt_queue = alloc_workqueue("kcryptd/%s", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM,
Michał Mirosławed0302e2018-10-09 22:13:43 +02003034 1, devname);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05003035 else
Michał Mirosławed0302e2018-10-09 22:13:43 +02003036 cc->crypt_queue = alloc_workqueue("kcryptd/%s",
Mike Snitzerf612b212019-11-20 17:27:39 -05003037 WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
Michał Mirosławed0302e2018-10-09 22:13:43 +02003038 num_online_cpus(), devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 if (!cc->crypt_queue) {
3040 ti->error = "Couldn't create kcryptd queue";
3041 goto bad;
3042 }
3043
Mikulas Patockac7329ef2018-07-11 12:10:51 -04003044 spin_lock_init(&cc->write_thread_lock);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05003045 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05003046
Michał Mirosławed0302e2018-10-09 22:13:43 +02003047 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write/%s", devname);
Mikulas Patockadc267622015-02-13 08:25:59 -05003048 if (IS_ERR(cc->write_thread)) {
3049 ret = PTR_ERR(cc->write_thread);
3050 cc->write_thread = NULL;
3051 ti->error = "Couldn't spawn write thread";
3052 goto bad;
3053 }
3054 wake_up_process(cc->write_thread);
3055
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003056 ti->num_flush_bios = 1;
Milan Broz983c7db2011-09-25 23:26:21 +01003057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 return 0;
3059
3060bad:
3061 crypt_dtr(ti);
3062 return ret;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01003063}
3064
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003065static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
3067 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01003068 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01003069
Milan Broz772ae5f2011-08-02 12:32:08 +01003070 /*
Mike Christie28a8f0d2016-06-05 14:32:25 -05003071 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
3072 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
Mike Christiee6047142016-06-05 14:32:04 -05003073 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
Milan Broz772ae5f2011-08-02 12:32:08 +01003074 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06003075 if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
Mike Christie28a8f0d2016-06-05 14:32:25 -05003076 bio_op(bio) == REQ_OP_DISCARD)) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02003077 bio_set_dev(bio, cc->dev->bdev);
Milan Broz772ae5f2011-08-02 12:32:08 +01003078 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07003079 bio->bi_iter.bi_sector = cc->start +
3080 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01003081 return DM_MAPIO_REMAPPED;
3082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Mikulas Patocka4e870e92016-08-30 16:38:42 -04003084 /*
3085 * Check if bio is too large, split as needed.
3086 */
3087 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
Milan Brozef43aa32017-01-04 20:23:54 +01003088 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
Mikulas Patocka4e870e92016-08-30 16:38:42 -04003089 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
3090
Milan Broz8f0009a2017-03-16 15:39:44 +01003091 /*
3092 * Ensure that bio is a multiple of internal sector encryption size
3093 * and is aligned to this size as defined in IO hints.
3094 */
3095 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
Christoph Hellwig846785e2017-06-03 09:38:02 +02003096 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01003097
3098 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
Christoph Hellwig846785e2017-06-03 09:38:02 +02003099 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01003100
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04003101 io = dm_per_bio_data(bio, cc->per_bio_data_size);
3102 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Milan Brozef43aa32017-01-04 20:23:54 +01003103
3104 if (cc->on_disk_tag_size) {
Mikulas Patocka583fe742017-04-18 16:51:54 -04003105 unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift);
Milan Brozef43aa32017-01-04 20:23:54 +01003106
3107 if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
Mikulas Patocka583fe742017-04-18 16:51:54 -04003108 unlikely(!(io->integrity_metadata = kmalloc(tag_len,
Milan Brozef43aa32017-01-04 20:23:54 +01003109 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
3110 if (bio_sectors(bio) > cc->tag_pool_max_sectors)
3111 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003112 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO);
Milan Brozef43aa32017-01-04 20:23:54 +01003113 io->integrity_metadata_from_pool = true;
3114 }
3115 }
3116
Milan Broz33d2f092017-03-16 15:39:40 +01003117 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01003118 io->ctx.r.req_aead = (struct aead_request *)(io + 1);
3119 else
3120 io->ctx.r.req = (struct skcipher_request *)(io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Milan Broz20c82532011-01-13 19:59:53 +00003122 if (bio_data_dir(io->base_bio) == READ) {
3123 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05003124 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00003125 } else
Andrew Morton4ee218c2006-03-27 01:17:48 -08003126 kcryptd_queue_crypt(io);
3127
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 return DM_MAPIO_SUBMITTED;
3129}
3130
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003131static void crypt_status(struct dm_target *ti, status_type_t type,
3132 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
Milan Broz5ebaee62010-08-12 04:14:07 +01003134 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003135 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05003136 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
3138 switch (type) {
3139 case STATUSTYPE_INFO:
3140 result[0] = '\0';
3141 break;
3142
3143 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00003144 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01003146 if (cc->key_size > 0) {
3147 if (cc->key_string)
3148 DMEMIT(":%u:%s", cc->key_size, cc->key_string);
3149 else
3150 for (i = 0; i < cc->key_size; i++)
3151 DMEMIT("%02x", cc->key[i]);
3152 } else
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003153 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
3155 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
3156 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01003157
Mikulas Patockaf3396c582015-02-13 08:23:09 -05003158 num_feature_args += !!ti->num_discard_bios;
3159 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05003160 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Mikulas Patockaff3af922017-03-23 10:23:14 -04003161 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT);
Milan Broz8f0009a2017-03-16 15:39:44 +01003162 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01003163 if (cc->on_disk_tag_size)
3164 num_feature_args++;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05003165 if (num_feature_args) {
3166 DMEMIT(" %d", num_feature_args);
3167 if (ti->num_discard_bios)
3168 DMEMIT(" allow_discards");
3169 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
3170 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05003171 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
3172 DMEMIT(" submit_from_crypt_cpus");
Milan Brozef43aa32017-01-04 20:23:54 +01003173 if (cc->on_disk_tag_size)
3174 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
Milan Broz8f0009a2017-03-16 15:39:44 +01003175 if (cc->sector_size != (1 << SECTOR_SHIFT))
3176 DMEMIT(" sector_size:%d", cc->sector_size);
3177 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
3178 DMEMIT(" iv_large_sectors");
Mikulas Patockaf3396c582015-02-13 08:23:09 -05003179 }
Milan Broz772ae5f2011-08-02 12:32:08 +01003180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 break;
3182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
3184
Milan Broze48d4bb2006-10-03 01:15:37 -07003185static void crypt_postsuspend(struct dm_target *ti)
3186{
3187 struct crypt_config *cc = ti->private;
3188
3189 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
3190}
3191
3192static int crypt_preresume(struct dm_target *ti)
3193{
3194 struct crypt_config *cc = ti->private;
3195
3196 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
3197 DMERR("aborting resume - crypt key is not set.");
3198 return -EAGAIN;
3199 }
3200
3201 return 0;
3202}
3203
3204static void crypt_resume(struct dm_target *ti)
3205{
3206 struct crypt_config *cc = ti->private;
3207
3208 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
3209}
3210
3211/* Message interface
3212 * key set <key>
3213 * key wipe
3214 */
Mike Snitzer1eb5fa82018-02-28 15:59:59 -05003215static int crypt_message(struct dm_target *ti, unsigned argc, char **argv,
3216 char *result, unsigned maxlen)
Milan Broze48d4bb2006-10-03 01:15:37 -07003217{
3218 struct crypt_config *cc = ti->private;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01003219 int key_size, ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07003220
3221 if (argc < 2)
3222 goto error;
3223
Mike Snitzer498f0102011-08-02 12:32:04 +01003224 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07003225 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
3226 DMWARN("not suspended during key manipulation.");
3227 return -EINVAL;
3228 }
Mike Snitzer498f0102011-08-02 12:32:04 +01003229 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01003230 /* The key size may not be changed. */
3231 key_size = get_key_size(&argv[2]);
3232 if (key_size < 0 || cc->key_size != key_size) {
3233 memset(argv[2], '0', strlen(argv[2]));
3234 return -EINVAL;
3235 }
3236
Milan Broz542da312009-12-10 23:51:57 +00003237 ret = crypt_set_key(cc, argv[2]);
3238 if (ret)
3239 return ret;
3240 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
3241 ret = cc->iv_gen_ops->init(cc);
Ondrej Kozinadc949022018-01-12 16:30:32 +01003242 /* wipe the kernel key payload copy */
3243 if (cc->key_string)
3244 memset(cc->key, 0, cc->key_size * sizeof(u8));
Milan Broz542da312009-12-10 23:51:57 +00003245 return ret;
3246 }
Milan Broz4a52ffc2019-07-09 15:22:12 +02003247 if (argc == 2 && !strcasecmp(argv[1], "wipe"))
Milan Broze48d4bb2006-10-03 01:15:37 -07003248 return crypt_wipe_key(cc);
3249 }
3250
3251error:
3252 DMWARN("unrecognised message received.");
3253 return -EINVAL;
3254}
3255
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003256static int crypt_iterate_devices(struct dm_target *ti,
3257 iterate_devices_callout_fn fn, void *data)
3258{
3259 struct crypt_config *cc = ti->private;
3260
Mike Snitzer5dea2712009-07-23 20:30:42 +01003261 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003262}
3263
Mike Snitzer586b2862015-09-09 21:34:51 -04003264static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
3265{
Milan Broz8f0009a2017-03-16 15:39:44 +01003266 struct crypt_config *cc = ti->private;
3267
Mike Snitzer586b2862015-09-09 21:34:51 -04003268 /*
3269 * Unfortunate constraint that is required to avoid the potential
3270 * for exceeding underlying device's max_segments limits -- due to
3271 * crypt_alloc_buffer() possibly allocating pages for the encryption
3272 * bio that are not as physically contiguous as the original bio.
3273 */
3274 limits->max_segment_size = PAGE_SIZE;
Milan Broz8f0009a2017-03-16 15:39:44 +01003275
Mikulas Patockabc9e9cf2018-08-10 11:23:56 -04003276 limits->logical_block_size =
3277 max_t(unsigned short, limits->logical_block_size, cc->sector_size);
3278 limits->physical_block_size =
3279 max_t(unsigned, limits->physical_block_size, cc->sector_size);
3280 limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size);
Mike Snitzer586b2862015-09-09 21:34:51 -04003281}
3282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283static struct target_type crypt_target = {
3284 .name = "crypt",
Milan Brozbbb16582020-01-03 09:20:22 +01003285 .version = {1, 20, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 .module = THIS_MODULE,
3287 .ctr = crypt_ctr,
3288 .dtr = crypt_dtr,
3289 .map = crypt_map,
3290 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07003291 .postsuspend = crypt_postsuspend,
3292 .preresume = crypt_preresume,
3293 .resume = crypt_resume,
3294 .message = crypt_message,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003295 .iterate_devices = crypt_iterate_devices,
Mike Snitzer586b2862015-09-09 21:34:51 -04003296 .io_hints = crypt_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297};
3298
3299static int __init dm_crypt_init(void)
3300{
3301 int r;
3302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05003304 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07003305 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 return r;
3308}
3309
3310static void __exit dm_crypt_exit(void)
3311{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00003312 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313}
3314
3315module_init(dm_crypt_init);
3316module_exit(dm_crypt_exit);
3317
Jana Saoutbf142992014-06-24 14:27:04 -04003318MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
3320MODULE_LICENSE("GPL");