blob: 000ddfab5ba05887e0bfda13baac9327fd0776f1 [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 */
Dmitry Baryshkov27f54112020-04-20 16:46:59 +030037#include <linux/key-type.h>
Ondrej Kozinac538f6e2016-11-21 15:58:51 +010038#include <keys/user-type.h>
Dmitry Baryshkov27f54112020-04-20 16:46:59 +030039#include <keys/encrypted-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Mikulas Patocka586e80e2008-10-21 17:44:59 +010041#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Alasdair G Kergon72d94862006-06-26 00:27:35 -070043#define DM_MSG_PREFIX "crypt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * context holding the current state of a multi-part conversion
47 */
48struct convert_context {
Milan Broz43d69032008-02-08 02:11:09 +000049 struct completion restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 struct bio *bio_in;
51 struct bio *bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -070052 struct bvec_iter iter_in;
53 struct bvec_iter iter_out;
AliOS system security8d683dc2018-11-05 15:31:42 +080054 u64 cc_sector;
Mikulas Patocka40b62292012-07-27 15:08:04 +010055 atomic_t cc_pending;
Milan Brozef43aa32017-01-04 20:23:54 +010056 union {
57 struct skcipher_request *req;
58 struct aead_request *req_aead;
59 } r;
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
Milan Broz53017032008-02-08 02:10:38 +000063/*
64 * per bio private data
65 */
66struct dm_crypt_io {
Alasdair G Kergon49a8a922012-07-27 15:08:05 +010067 struct crypt_config *cc;
Milan Broz53017032008-02-08 02:10:38 +000068 struct bio *base_bio;
Milan Brozef43aa32017-01-04 20:23:54 +010069 u8 *integrity_metadata;
70 bool integrity_metadata_from_pool;
Milan Broz53017032008-02-08 02:10:38 +000071 struct work_struct work;
72
73 struct convert_context ctx;
74
Mikulas Patocka40b62292012-07-27 15:08:04 +010075 atomic_t io_pending;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020076 blk_status_t error;
Milan Broz0c395b02008-02-08 02:10:54 +000077 sector_t sector;
Mikulas Patockadc267622015-02-13 08:25:59 -050078
Mikulas Patockab3c5fd32015-02-13 08:27:41 -050079 struct rb_node rb_node;
Mikulas Patocka298a9fa2014-03-28 15:51:55 -040080} CRYPTO_MINALIGN_ATTR;
Milan Broz53017032008-02-08 02:10:38 +000081
Milan Broz01482b72008-02-08 02:11:04 +000082struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000083 struct convert_context *ctx;
Milan Brozef43aa32017-01-04 20:23:54 +010084 struct scatterlist sg_in[4];
85 struct scatterlist sg_out[4];
AliOS system security8d683dc2018-11-05 15:31:42 +080086 u64 iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000087};
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089struct crypt_config;
90
91struct crypt_iv_operations {
92 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010093 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000095 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000096 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc53272011-01-13 19:59:54 +000097 int (*generator)(struct crypt_config *cc, u8 *iv,
98 struct dm_crypt_request *dmreq);
99 int (*post)(struct crypt_config *cc, u8 *iv,
100 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Milan Broz60473592009-12-10 23:51:55 +0000103struct iv_benbi_private {
104 int shift;
105};
106
Milan Broz34745782011-01-13 19:59:55 +0000107#define LMK_SEED_SIZE 64 /* hash + 0 */
108struct iv_lmk_private {
109 struct crypto_shash *hash_tfm;
110 u8 *seed;
111};
112
Milan Brozed04d982013-10-28 23:21:04 +0100113#define TCW_WHITENING_SIZE 16
114struct iv_tcw_private {
115 struct crypto_shash *crc32_tfm;
116 u8 *iv_seed;
117 u8 *whitening;
118};
119
Milan Brozbbb16582020-01-03 09:20:22 +0100120#define ELEPHANT_MAX_KEY_SIZE 32
121struct iv_elephant_private {
122 struct crypto_skcipher *tfm;
123};
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
126 * Crypt: maps a linear range of a block device
127 * and encrypts / decrypts at the same time.
128 */
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -0500129enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID,
Rabin Vincentf659b102016-09-21 16:22:29 +0200130 DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD };
Andi Kleenc0297722011-01-13 19:59:53 +0000131
Milan Brozef43aa32017-01-04 20:23:54 +0100132enum cipher_flags {
133 CRYPT_MODE_INTEGRITY_AEAD, /* Use authenticated mode for cihper */
Milan Broz8f0009a2017-03-16 15:39:44 +0100134 CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */
Milan Brozbbb16582020-01-03 09:20:22 +0100135 CRYPT_ENCRYPT_PREPROCESS, /* Must preprocess data for encryption (elephant) */
Milan Brozef43aa32017-01-04 20:23:54 +0100136};
137
Andi Kleenc0297722011-01-13 19:59:53 +0000138/*
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500139 * The fields in here must be read only after initialization.
Andi Kleenc0297722011-01-13 19:59:53 +0000140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141struct crypt_config {
142 struct dm_dev *dev;
143 sector_t start;
144
Mikulas Patocka50593532017-08-13 22:45:08 -0400145 struct percpu_counter n_allocated_pages;
146
Milan Brozcabf08e2007-10-19 22:38:58 +0100147 struct workqueue_struct *io_queue;
148 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700149
Mikulas Patockac7329ef2018-07-11 12:10:51 -0400150 spinlock_t write_thread_lock;
Mike Snitzer72d711c2018-05-22 18:26:20 -0400151 struct task_struct *write_thread;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -0500152 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -0500153
Milan Broz7dbcd132011-01-13 19:59:52 +0000154 char *cipher_string;
Milan Brozef43aa32017-01-04 20:23:54 +0100155 char *cipher_auth;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +0100156 char *key_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100157
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100158 const struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800159 union {
Milan Broz60473592009-12-10 23:51:55 +0000160 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000161 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100162 struct iv_tcw_private tcw;
Milan Brozbbb16582020-01-03 09:20:22 +0100163 struct iv_elephant_private elephant;
Herbert Xu79066ad2006-12-05 13:41:52 -0800164 } iv_gen_private;
AliOS system security8d683dc2018-11-05 15:31:42 +0800165 u64 iv_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 unsigned int iv_size;
Mikulas Patockaff3af922017-03-23 10:23:14 -0400167 unsigned short int sector_size;
168 unsigned char sector_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Milan Brozef43aa32017-01-04 20:23:54 +0100170 union {
171 struct crypto_skcipher **tfms;
172 struct crypto_aead **tfms_aead;
173 } cipher_tfm;
Milan Brozd1f96422011-01-13 19:59:54 +0000174 unsigned tfms_count;
Milan Brozef43aa32017-01-04 20:23:54 +0100175 unsigned long cipher_flags;
Andi Kleenc0297722011-01-13 19:59:53 +0000176
177 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000178 * Layout of each crypto request:
179 *
Herbert Xubbdb23b2016-01-24 21:16:36 +0800180 * struct skcipher_request
Milan Brozddd42ed2008-02-08 02:11:07 +0000181 * context
182 * padding
183 * struct dm_crypt_request
184 * padding
185 * IV
186 *
187 * The padding is added so that dm_crypt_request and the IV are
188 * correctly aligned.
189 */
190 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000191
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400192 unsigned int per_bio_data_size;
193
Milan Broze48d4bb2006-10-03 01:15:37 -0700194 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100196 unsigned int key_parts; /* independent parts in key buffer */
197 unsigned int key_extra_size; /* additional keys length */
Milan Brozef43aa32017-01-04 20:23:54 +0100198 unsigned int key_mac_size; /* MAC key size for authenc(...) */
199
200 unsigned int integrity_tag_size;
201 unsigned int integrity_iv_size;
202 unsigned int on_disk_tag_size;
203
Mike Snitzer72d711c2018-05-22 18:26:20 -0400204 /*
205 * pool for per bio private data, crypto requests,
206 * encryption requeusts/buffer pages and integrity tags
207 */
208 unsigned tag_pool_max_sectors;
209 mempool_t tag_pool;
210 mempool_t req_pool;
211 mempool_t page_pool;
212
213 struct bio_set bs;
214 struct mutex bio_alloc_lock;
215
Milan Brozef43aa32017-01-04 20:23:54 +0100216 u8 *authenc_key; /* space for keys in authenc() format (if used) */
Gustavo A. R. Silvab18ae8d2020-05-07 13:51:58 -0500217 u8 key[];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219
Milan Brozef43aa32017-01-04 20:23:54 +0100220#define MIN_IOS 64
221#define MAX_TAG_SIZE 480
222#define POOL_ENTRY_SIZE 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Mikulas Patocka50593532017-08-13 22:45:08 -0400224static DEFINE_SPINLOCK(dm_crypt_clients_lock);
225static unsigned dm_crypt_clients_n = 0;
226static volatile unsigned long dm_crypt_pages_per_client;
227#define DM_CRYPT_MEMORY_PERCENT 2
228#define DM_CRYPT_MIN_PAGES_PER_CLIENT (BIO_MAX_PAGES * 16)
229
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100230static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000231static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Brozef43aa32017-01-04 20:23:54 +0100232static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
233 struct scatterlist *sg);
Olaf Kirch027581f2007-05-09 02:32:52 -0700234
Yang Yingliang3fd53532020-02-13 12:11:26 +0800235static bool crypt_integrity_aead(struct crypt_config *cc);
236
Andi Kleenc0297722011-01-13 19:59:53 +0000237/*
Eric Biggers86f917a2017-03-30 22:18:48 -0700238 * Use this to access cipher attributes that are independent of the key.
Andi Kleenc0297722011-01-13 19:59:53 +0000239 */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800240static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +0000241{
Milan Brozef43aa32017-01-04 20:23:54 +0100242 return cc->cipher_tfm.tfms[0];
243}
244
245static struct crypto_aead *any_tfm_aead(struct crypt_config *cc)
246{
247 return cc->cipher_tfm.tfms_aead[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * Different IV generation algorithms:
252 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000253 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200254 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *
Milan Broz61afef62009-12-10 23:52:25 +0000256 * plain64: the initial vector is the 64-bit little-endian version of the sector
257 * number, padded with zeros if necessary.
258 *
Milan Broz7e3fd852017-06-06 09:07:01 +0200259 * plain64be: the initial vector is the 64-bit big-endian version of the sector
260 * number, padded with zeros if necessary.
261 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000262 * essiv: "encrypted sector|salt initial vector", the sector number is
263 * encrypted with the bulk cipher using a salt as key. The salt
264 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 *
Rik Snel48527fa2006-09-03 08:56:39 +1000266 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
267 * (needed for LRW-32-AES and possible other narrow block modes)
268 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700269 * null: the initial vector is always zero. Provides compatibility with
270 * obsolete loop_fish2 devices. Do not use for new devices.
271 *
Milan Broz34745782011-01-13 19:59:55 +0000272 * lmk: Compatible implementation of the block chaining mode used
273 * by the Loop-AES block device encryption system
274 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
275 * It operates on full 512 byte sectors and uses CBC
276 * with an IV derived from the sector number, the data and
277 * optionally extra IV seed.
278 * This means that after decryption the first block
279 * of sector must be tweaked according to decrypted data.
280 * Loop-AES can use three encryption schemes:
281 * version 1: is plain aes-cbc mode
282 * version 2: uses 64 multikey scheme with lmk IV generator
283 * version 3: the same as version 2 with additional IV seed
284 * (it uses 65 keys, last key is used as IV seed)
285 *
Milan Brozed04d982013-10-28 23:21:04 +0100286 * tcw: Compatible implementation of the block chaining mode used
287 * by the TrueCrypt device encryption system (prior to version 4.1).
Milan Broze44f23b2015-04-05 18:03:10 +0200288 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
Milan Brozed04d982013-10-28 23:21:04 +0100289 * It operates on full 512 byte sectors and uses CBC
290 * with an IV derived from initial key and the sector number.
291 * In addition, whitening value is applied on every sector, whitening
292 * is calculated from initial key, sector number and mixed using CRC32.
293 * Note that this encryption scheme is vulnerable to watermarking attacks
294 * and should be used for old compatible containers access only.
Milan Brozb9411d72019-07-09 15:22:14 +0200295 *
296 * eboiv: Encrypted byte-offset IV (used in Bitlocker in CBC mode)
297 * The IV is encrypted little-endian byte-offset (with the same key
298 * and cipher as the volume).
Milan Brozbbb16582020-01-03 09:20:22 +0100299 *
300 * elephant: The extended version of eboiv with additional Elephant diffuser
301 * used with Bitlocker CBC mode.
302 * This mode was used in older Windows systems
303 * http://download.microsoft.com/download/0/2/3/0238acaf-d3bf-4a6d-b3d6-0a0be4bbb36e/bitlockercipher200608.pdf
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 */
305
Milan Broz2dc53272011-01-13 19:59:54 +0000306static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
307 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100310 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 return 0;
313}
314
Milan Broz61afef62009-12-10 23:52:25 +0000315static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc53272011-01-13 19:59:54 +0000316 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000317{
318 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100319 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000320
321 return 0;
322}
323
Milan Broz7e3fd852017-06-06 09:07:01 +0200324static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv,
325 struct dm_crypt_request *dmreq)
326{
327 memset(iv, 0, cc->iv_size);
328 /* iv_size is at least of size u64; usually it is 16 bytes */
329 *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector);
330
331 return 0;
332}
333
Milan Broz2dc53272011-01-13 19:59:54 +0000334static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
335 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Ard Biesheuvela1a262b2019-08-19 17:17:37 +0300337 /*
338 * ESSIV encryption of the IV is now handled by the crypto API,
339 * so just pass the plain sector number here.
340 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100342 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return 0;
345}
346
Rik Snel48527fa2006-09-03 08:56:39 +1000347static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
348 const char *opts)
349{
Milan Broz4ea94712020-01-06 10:11:47 +0100350 unsigned bs;
351 int log;
352
Yang Yingliang3fd53532020-02-13 12:11:26 +0800353 if (crypt_integrity_aead(cc))
Milan Broz4ea94712020-01-06 10:11:47 +0100354 bs = crypto_aead_blocksize(any_tfm_aead(cc));
355 else
356 bs = crypto_skcipher_blocksize(any_tfm(cc));
357 log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000358
359 /* we need to calculate how far we must shift the sector count
360 * to get the cipher block count, we use this shift in _gen */
361
362 if (1 << log != bs) {
363 ti->error = "cypher blocksize is not a power of 2";
364 return -EINVAL;
365 }
366
367 if (log > 9) {
368 ti->error = "cypher blocksize is > 512";
369 return -EINVAL;
370 }
371
Milan Broz60473592009-12-10 23:51:55 +0000372 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000373
374 return 0;
375}
376
377static void crypt_iv_benbi_dtr(struct crypt_config *cc)
378{
Rik Snel48527fa2006-09-03 08:56:39 +1000379}
380
Milan Broz2dc53272011-01-13 19:59:54 +0000381static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
382 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000383{
Herbert Xu79066ad2006-12-05 13:41:52 -0800384 __be64 val;
385
Rik Snel48527fa2006-09-03 08:56:39 +1000386 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800387
Milan Broz2dc53272011-01-13 19:59:54 +0000388 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800389 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return 0;
392}
393
Milan Broz2dc53272011-01-13 19:59:54 +0000394static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
395 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700396{
397 memset(iv, 0, cc->iv_size);
398
399 return 0;
400}
401
Milan Broz34745782011-01-13 19:59:55 +0000402static void crypt_iv_lmk_dtr(struct crypt_config *cc)
403{
404 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
405
406 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
407 crypto_free_shash(lmk->hash_tfm);
408 lmk->hash_tfm = NULL;
409
410 kzfree(lmk->seed);
411 lmk->seed = NULL;
412}
413
414static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
415 const char *opts)
416{
417 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
418
Milan Broz8f0009a2017-03-16 15:39:44 +0100419 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
420 ti->error = "Unsupported sector size for LMK";
421 return -EINVAL;
422 }
423
Milan Broz34745782011-01-13 19:59:55 +0000424 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
425 if (IS_ERR(lmk->hash_tfm)) {
426 ti->error = "Error initializing LMK hash";
427 return PTR_ERR(lmk->hash_tfm);
428 }
429
430 /* No seed in LMK version 2 */
431 if (cc->key_parts == cc->tfms_count) {
432 lmk->seed = NULL;
433 return 0;
434 }
435
436 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
437 if (!lmk->seed) {
438 crypt_iv_lmk_dtr(cc);
439 ti->error = "Error kmallocing seed storage in LMK";
440 return -ENOMEM;
441 }
442
443 return 0;
444}
445
446static int crypt_iv_lmk_init(struct crypt_config *cc)
447{
448 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
449 int subkey_size = cc->key_size / cc->key_parts;
450
451 /* LMK seed is on the position of LMK_KEYS + 1 key */
452 if (lmk->seed)
453 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
454 crypto_shash_digestsize(lmk->hash_tfm));
455
456 return 0;
457}
458
459static int crypt_iv_lmk_wipe(struct crypt_config *cc)
460{
461 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
462
463 if (lmk->seed)
464 memset(lmk->seed, 0, LMK_SEED_SIZE);
465
466 return 0;
467}
468
469static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
470 struct dm_crypt_request *dmreq,
471 u8 *data)
472{
473 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200474 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000475 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100476 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000477 int i, r;
478
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200479 desc->tfm = lmk->hash_tfm;
Milan Broz34745782011-01-13 19:59:55 +0000480
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200481 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000482 if (r)
483 return r;
484
485 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200486 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000487 if (r)
488 return r;
489 }
490
491 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200492 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000493 if (r)
494 return r;
495
496 /* Sector is cropped to 56 bits here */
497 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
498 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
499 buf[2] = cpu_to_le32(4024);
500 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200501 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000502 if (r)
503 return r;
504
505 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200506 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000507 if (r)
508 return r;
509
510 for (i = 0; i < MD5_HASH_WORDS; i++)
511 __cpu_to_le32s(&md5state.hash[i]);
512 memcpy(iv, &md5state.hash, cc->iv_size);
513
514 return 0;
515}
516
517static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
518 struct dm_crypt_request *dmreq)
519{
Milan Brozef43aa32017-01-04 20:23:54 +0100520 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000521 u8 *src;
522 int r = 0;
523
524 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100525 sg = crypt_get_sg_data(cc, dmreq->sg_in);
526 src = kmap_atomic(sg_page(sg));
527 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800528 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000529 } else
530 memset(iv, 0, cc->iv_size);
531
532 return r;
533}
534
535static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
536 struct dm_crypt_request *dmreq)
537{
Milan Brozef43aa32017-01-04 20:23:54 +0100538 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000539 u8 *dst;
540 int r;
541
542 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
543 return 0;
544
Milan Brozef43aa32017-01-04 20:23:54 +0100545 sg = crypt_get_sg_data(cc, dmreq->sg_out);
546 dst = kmap_atomic(sg_page(sg));
547 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset);
Milan Broz34745782011-01-13 19:59:55 +0000548
549 /* Tweak the first block of plaintext sector */
550 if (!r)
Milan Brozef43aa32017-01-04 20:23:54 +0100551 crypto_xor(dst + sg->offset, iv, cc->iv_size);
Milan Broz34745782011-01-13 19:59:55 +0000552
Cong Wangc2e022c2011-11-28 13:26:02 +0800553 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000554 return r;
555}
556
Milan Brozed04d982013-10-28 23:21:04 +0100557static void crypt_iv_tcw_dtr(struct crypt_config *cc)
558{
559 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
560
561 kzfree(tcw->iv_seed);
562 tcw->iv_seed = NULL;
563 kzfree(tcw->whitening);
564 tcw->whitening = NULL;
565
566 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
567 crypto_free_shash(tcw->crc32_tfm);
568 tcw->crc32_tfm = NULL;
569}
570
571static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
572 const char *opts)
573{
574 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
575
Milan Broz8f0009a2017-03-16 15:39:44 +0100576 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
577 ti->error = "Unsupported sector size for TCW";
578 return -EINVAL;
579 }
580
Milan Brozed04d982013-10-28 23:21:04 +0100581 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
582 ti->error = "Wrong key size for TCW";
583 return -EINVAL;
584 }
585
586 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
587 if (IS_ERR(tcw->crc32_tfm)) {
588 ti->error = "Error initializing CRC32 in TCW";
589 return PTR_ERR(tcw->crc32_tfm);
590 }
591
592 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
593 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
594 if (!tcw->iv_seed || !tcw->whitening) {
595 crypt_iv_tcw_dtr(cc);
596 ti->error = "Error allocating seed storage in TCW";
597 return -ENOMEM;
598 }
599
600 return 0;
601}
602
603static int crypt_iv_tcw_init(struct crypt_config *cc)
604{
605 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
606 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
607
608 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
609 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
610 TCW_WHITENING_SIZE);
611
612 return 0;
613}
614
615static int crypt_iv_tcw_wipe(struct crypt_config *cc)
616{
617 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
618
619 memset(tcw->iv_seed, 0, cc->iv_size);
620 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
621
622 return 0;
623}
624
625static int crypt_iv_tcw_whitening(struct crypt_config *cc,
626 struct dm_crypt_request *dmreq,
627 u8 *data)
628{
629 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200630 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100631 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200632 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100633 int i, r;
634
635 /* xor whitening with sector number */
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100636 crypto_xor_cpy(buf, tcw->whitening, (u8 *)&sector, 8);
637 crypto_xor_cpy(&buf[8], tcw->whitening + 8, (u8 *)&sector, 8);
Milan Brozed04d982013-10-28 23:21:04 +0100638
639 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200640 desc->tfm = tcw->crc32_tfm;
Milan Brozed04d982013-10-28 23:21:04 +0100641 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200642 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100643 if (r)
644 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200645 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100646 if (r)
647 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200648 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100649 if (r)
650 goto out;
651 }
652 crypto_xor(&buf[0], &buf[12], 4);
653 crypto_xor(&buf[4], &buf[8], 4);
654
655 /* apply whitening (8 bytes) to whole sector */
656 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
657 crypto_xor(data + i * 8, buf, 8);
658out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100659 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100660 return r;
661}
662
663static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
664 struct dm_crypt_request *dmreq)
665{
Milan Brozef43aa32017-01-04 20:23:54 +0100666 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100667 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200668 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100669 u8 *src;
670 int r = 0;
671
672 /* Remove whitening from ciphertext */
673 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100674 sg = crypt_get_sg_data(cc, dmreq->sg_in);
675 src = kmap_atomic(sg_page(sg));
676 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100677 kunmap_atomic(src);
678 }
679
680 /* Calculate IV */
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100681 crypto_xor_cpy(iv, tcw->iv_seed, (u8 *)&sector, 8);
Milan Brozed04d982013-10-28 23:21:04 +0100682 if (cc->iv_size > 8)
Ard Biesheuvel45fe93d2017-07-24 11:28:04 +0100683 crypto_xor_cpy(&iv[8], tcw->iv_seed + 8, (u8 *)&sector,
684 cc->iv_size - 8);
Milan Brozed04d982013-10-28 23:21:04 +0100685
686 return r;
687}
688
689static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
690 struct dm_crypt_request *dmreq)
691{
Milan Brozef43aa32017-01-04 20:23:54 +0100692 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100693 u8 *dst;
694 int r;
695
696 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
697 return 0;
698
699 /* Apply whitening on ciphertext */
Milan Brozef43aa32017-01-04 20:23:54 +0100700 sg = crypt_get_sg_data(cc, dmreq->sg_out);
701 dst = kmap_atomic(sg_page(sg));
702 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100703 kunmap_atomic(dst);
704
705 return r;
706}
707
Milan Brozef43aa32017-01-04 20:23:54 +0100708static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv,
709 struct dm_crypt_request *dmreq)
710{
711 /* Used only for writes, there must be an additional space to store IV */
712 get_random_bytes(iv, cc->iv_size);
713 return 0;
714}
715
Milan Brozb9411d72019-07-09 15:22:14 +0200716static int crypt_iv_eboiv_ctr(struct crypt_config *cc, struct dm_target *ti,
717 const char *opts)
718{
Yang Yingliang3fd53532020-02-13 12:11:26 +0800719 if (crypt_integrity_aead(cc)) {
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300720 ti->error = "AEAD transforms not supported for EBOIV";
Milan Brozb9411d72019-07-09 15:22:14 +0200721 return -EINVAL;
722 }
723
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300724 if (crypto_skcipher_blocksize(any_tfm(cc)) != cc->iv_size) {
725 ti->error = "Block size of EBOIV cipher does "
726 "not match IV size of block cipher";
727 return -EINVAL;
728 }
Milan Brozb9411d72019-07-09 15:22:14 +0200729
730 return 0;
731}
732
Milan Brozb9411d72019-07-09 15:22:14 +0200733static int crypt_iv_eboiv_gen(struct crypt_config *cc, u8 *iv,
734 struct dm_crypt_request *dmreq)
735{
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300736 u8 buf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(__le64));
737 struct skcipher_request *req;
738 struct scatterlist src, dst;
739 struct crypto_wait wait;
740 int err;
Milan Brozb9411d72019-07-09 15:22:14 +0200741
Mikulas Patocka9402e952020-01-02 08:23:32 -0500742 req = skcipher_request_alloc(any_tfm(cc), GFP_NOIO);
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300743 if (!req)
744 return -ENOMEM;
Milan Brozb9411d72019-07-09 15:22:14 +0200745
Ard Biesheuvel39d13a12019-08-07 08:50:22 +0300746 memset(buf, 0, cc->iv_size);
747 *(__le64 *)buf = cpu_to_le64(dmreq->iv_sector * cc->sector_size);
748
749 sg_init_one(&src, page_address(ZERO_PAGE(0)), cc->iv_size);
750 sg_init_one(&dst, iv, cc->iv_size);
751 skcipher_request_set_crypt(req, &src, &dst, cc->iv_size, buf);
752 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
753 err = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
754 skcipher_request_free(req);
755
756 return err;
Milan Brozb9411d72019-07-09 15:22:14 +0200757}
758
Milan Brozbbb16582020-01-03 09:20:22 +0100759static void crypt_iv_elephant_dtr(struct crypt_config *cc)
760{
761 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant;
762
763 crypto_free_skcipher(elephant->tfm);
764 elephant->tfm = NULL;
765}
766
767static int crypt_iv_elephant_ctr(struct crypt_config *cc, struct dm_target *ti,
768 const char *opts)
769{
770 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant;
771 int r;
772
773 elephant->tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
774 if (IS_ERR(elephant->tfm)) {
775 r = PTR_ERR(elephant->tfm);
776 elephant->tfm = NULL;
777 return r;
778 }
779
780 r = crypt_iv_eboiv_ctr(cc, ti, NULL);
781 if (r)
782 crypt_iv_elephant_dtr(cc);
783 return r;
784}
785
786static void diffuser_disk_to_cpu(u32 *d, size_t n)
787{
788#ifndef __LITTLE_ENDIAN
789 int i;
790
791 for (i = 0; i < n; i++)
792 d[i] = le32_to_cpu((__le32)d[i]);
793#endif
794}
795
796static void diffuser_cpu_to_disk(__le32 *d, size_t n)
797{
798#ifndef __LITTLE_ENDIAN
799 int i;
800
801 for (i = 0; i < n; i++)
802 d[i] = cpu_to_le32((u32)d[i]);
803#endif
804}
805
806static void diffuser_a_decrypt(u32 *d, size_t n)
807{
808 int i, i1, i2, i3;
809
810 for (i = 0; i < 5; i++) {
811 i1 = 0;
812 i2 = n - 2;
813 i3 = n - 5;
814
815 while (i1 < (n - 1)) {
816 d[i1] += d[i2] ^ (d[i3] << 9 | d[i3] >> 23);
817 i1++; i2++; i3++;
818
819 if (i3 >= n)
820 i3 -= n;
821
822 d[i1] += d[i2] ^ d[i3];
823 i1++; i2++; i3++;
824
825 if (i2 >= n)
826 i2 -= n;
827
828 d[i1] += d[i2] ^ (d[i3] << 13 | d[i3] >> 19);
829 i1++; i2++; i3++;
830
831 d[i1] += d[i2] ^ d[i3];
832 i1++; i2++; i3++;
833 }
834 }
835}
836
837static void diffuser_a_encrypt(u32 *d, size_t n)
838{
839 int i, i1, i2, i3;
840
841 for (i = 0; i < 5; i++) {
842 i1 = n - 1;
843 i2 = n - 2 - 1;
844 i3 = n - 5 - 1;
845
846 while (i1 > 0) {
847 d[i1] -= d[i2] ^ d[i3];
848 i1--; i2--; i3--;
849
850 d[i1] -= d[i2] ^ (d[i3] << 13 | d[i3] >> 19);
851 i1--; i2--; i3--;
852
853 if (i2 < 0)
854 i2 += n;
855
856 d[i1] -= d[i2] ^ d[i3];
857 i1--; i2--; i3--;
858
859 if (i3 < 0)
860 i3 += n;
861
862 d[i1] -= d[i2] ^ (d[i3] << 9 | d[i3] >> 23);
863 i1--; i2--; i3--;
864 }
865 }
866}
867
868static void diffuser_b_decrypt(u32 *d, size_t n)
869{
870 int i, i1, i2, i3;
871
872 for (i = 0; i < 3; i++) {
873 i1 = 0;
874 i2 = 2;
875 i3 = 5;
876
877 while (i1 < (n - 1)) {
878 d[i1] += d[i2] ^ d[i3];
879 i1++; i2++; i3++;
880
881 d[i1] += d[i2] ^ (d[i3] << 10 | d[i3] >> 22);
882 i1++; i2++; i3++;
883
884 if (i2 >= n)
885 i2 -= n;
886
887 d[i1] += d[i2] ^ d[i3];
888 i1++; i2++; i3++;
889
890 if (i3 >= n)
891 i3 -= n;
892
893 d[i1] += d[i2] ^ (d[i3] << 25 | d[i3] >> 7);
894 i1++; i2++; i3++;
895 }
896 }
897}
898
899static void diffuser_b_encrypt(u32 *d, size_t n)
900{
901 int i, i1, i2, i3;
902
903 for (i = 0; i < 3; i++) {
904 i1 = n - 1;
905 i2 = 2 - 1;
906 i3 = 5 - 1;
907
908 while (i1 > 0) {
909 d[i1] -= d[i2] ^ (d[i3] << 25 | d[i3] >> 7);
910 i1--; i2--; i3--;
911
912 if (i3 < 0)
913 i3 += n;
914
915 d[i1] -= d[i2] ^ d[i3];
916 i1--; i2--; i3--;
917
918 if (i2 < 0)
919 i2 += n;
920
921 d[i1] -= d[i2] ^ (d[i3] << 10 | d[i3] >> 22);
922 i1--; i2--; i3--;
923
924 d[i1] -= d[i2] ^ d[i3];
925 i1--; i2--; i3--;
926 }
927 }
928}
929
930static int crypt_iv_elephant(struct crypt_config *cc, struct dm_crypt_request *dmreq)
931{
932 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant;
933 u8 *es, *ks, *data, *data2, *data_offset;
934 struct skcipher_request *req;
935 struct scatterlist *sg, *sg2, src, dst;
936 struct crypto_wait wait;
937 int i, r;
938
939 req = skcipher_request_alloc(elephant->tfm, GFP_NOIO);
940 es = kzalloc(16, GFP_NOIO); /* Key for AES */
941 ks = kzalloc(32, GFP_NOIO); /* Elephant sector key */
942
943 if (!req || !es || !ks) {
944 r = -ENOMEM;
945 goto out;
946 }
947
948 *(__le64 *)es = cpu_to_le64(dmreq->iv_sector * cc->sector_size);
949
950 /* E(Ks, e(s)) */
951 sg_init_one(&src, es, 16);
952 sg_init_one(&dst, ks, 16);
953 skcipher_request_set_crypt(req, &src, &dst, 16, NULL);
954 skcipher_request_set_callback(req, 0, crypto_req_done, &wait);
955 r = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
956 if (r)
957 goto out;
958
959 /* E(Ks, e'(s)) */
960 es[15] = 0x80;
961 sg_init_one(&dst, &ks[16], 16);
962 r = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
963 if (r)
964 goto out;
965
966 sg = crypt_get_sg_data(cc, dmreq->sg_out);
967 data = kmap_atomic(sg_page(sg));
968 data_offset = data + sg->offset;
969
970 /* Cannot modify original bio, copy to sg_out and apply Elephant to it */
971 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
972 sg2 = crypt_get_sg_data(cc, dmreq->sg_in);
973 data2 = kmap_atomic(sg_page(sg2));
974 memcpy(data_offset, data2 + sg2->offset, cc->sector_size);
975 kunmap_atomic(data2);
976 }
977
978 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
979 diffuser_disk_to_cpu((u32*)data_offset, cc->sector_size / sizeof(u32));
980 diffuser_b_decrypt((u32*)data_offset, cc->sector_size / sizeof(u32));
981 diffuser_a_decrypt((u32*)data_offset, cc->sector_size / sizeof(u32));
982 diffuser_cpu_to_disk((__le32*)data_offset, cc->sector_size / sizeof(u32));
983 }
984
985 for (i = 0; i < (cc->sector_size / 32); i++)
986 crypto_xor(data_offset + i * 32, ks, 32);
987
988 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
989 diffuser_disk_to_cpu((u32*)data_offset, cc->sector_size / sizeof(u32));
990 diffuser_a_encrypt((u32*)data_offset, cc->sector_size / sizeof(u32));
991 diffuser_b_encrypt((u32*)data_offset, cc->sector_size / sizeof(u32));
992 diffuser_cpu_to_disk((__le32*)data_offset, cc->sector_size / sizeof(u32));
993 }
994
995 kunmap_atomic(data);
996out:
997 kzfree(ks);
998 kzfree(es);
999 skcipher_request_free(req);
1000 return r;
1001}
1002
1003static int crypt_iv_elephant_gen(struct crypt_config *cc, u8 *iv,
1004 struct dm_crypt_request *dmreq)
1005{
1006 int r;
1007
1008 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
1009 r = crypt_iv_elephant(cc, dmreq);
1010 if (r)
1011 return r;
1012 }
1013
1014 return crypt_iv_eboiv_gen(cc, iv, dmreq);
1015}
1016
1017static int crypt_iv_elephant_post(struct crypt_config *cc, u8 *iv,
1018 struct dm_crypt_request *dmreq)
1019{
1020 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
1021 return crypt_iv_elephant(cc, dmreq);
1022
1023 return 0;
1024}
1025
1026static int crypt_iv_elephant_init(struct crypt_config *cc)
1027{
1028 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant;
1029 int key_offset = cc->key_size - cc->key_extra_size;
1030
1031 return crypto_skcipher_setkey(elephant->tfm, &cc->key[key_offset], cc->key_extra_size);
1032}
1033
1034static int crypt_iv_elephant_wipe(struct crypt_config *cc)
1035{
1036 struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant;
1037 u8 key[ELEPHANT_MAX_KEY_SIZE];
1038
1039 memset(key, 0, cc->key_extra_size);
1040 return crypto_skcipher_setkey(elephant->tfm, key, cc->key_extra_size);
1041}
1042
Julia Lawall1b1b58f2015-11-29 14:09:19 +01001043static const struct crypt_iv_operations crypt_iv_plain_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 .generator = crypt_iv_plain_gen
1045};
1046
Julia Lawall1b1b58f2015-11-29 14:09:19 +01001047static const struct crypt_iv_operations crypt_iv_plain64_ops = {
Milan Broz61afef62009-12-10 23:52:25 +00001048 .generator = crypt_iv_plain64_gen
1049};
1050
Milan Broz7e3fd852017-06-06 09:07:01 +02001051static const struct crypt_iv_operations crypt_iv_plain64be_ops = {
1052 .generator = crypt_iv_plain64be_gen
1053};
1054
Julia Lawall1b1b58f2015-11-29 14:09:19 +01001055static const struct crypt_iv_operations crypt_iv_essiv_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 .generator = crypt_iv_essiv_gen
1057};
1058
Julia Lawall1b1b58f2015-11-29 14:09:19 +01001059static const struct crypt_iv_operations crypt_iv_benbi_ops = {
Rik Snel48527fa2006-09-03 08:56:39 +10001060 .ctr = crypt_iv_benbi_ctr,
1061 .dtr = crypt_iv_benbi_dtr,
1062 .generator = crypt_iv_benbi_gen
1063};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Julia Lawall1b1b58f2015-11-29 14:09:19 +01001065static const struct crypt_iv_operations crypt_iv_null_ops = {
Ludwig Nussel46b47732007-05-09 02:32:55 -07001066 .generator = crypt_iv_null_gen
1067};
1068
Julia Lawall1b1b58f2015-11-29 14:09:19 +01001069static const struct crypt_iv_operations crypt_iv_lmk_ops = {
Milan Broz34745782011-01-13 19:59:55 +00001070 .ctr = crypt_iv_lmk_ctr,
1071 .dtr = crypt_iv_lmk_dtr,
1072 .init = crypt_iv_lmk_init,
1073 .wipe = crypt_iv_lmk_wipe,
1074 .generator = crypt_iv_lmk_gen,
1075 .post = crypt_iv_lmk_post
1076};
1077
Julia Lawall1b1b58f2015-11-29 14:09:19 +01001078static const struct crypt_iv_operations crypt_iv_tcw_ops = {
Milan Brozed04d982013-10-28 23:21:04 +01001079 .ctr = crypt_iv_tcw_ctr,
1080 .dtr = crypt_iv_tcw_dtr,
1081 .init = crypt_iv_tcw_init,
1082 .wipe = crypt_iv_tcw_wipe,
1083 .generator = crypt_iv_tcw_gen,
1084 .post = crypt_iv_tcw_post
1085};
1086
Milan Brozef43aa32017-01-04 20:23:54 +01001087static struct crypt_iv_operations crypt_iv_random_ops = {
1088 .generator = crypt_iv_random_gen
1089};
1090
Milan Brozb9411d72019-07-09 15:22:14 +02001091static struct crypt_iv_operations crypt_iv_eboiv_ops = {
1092 .ctr = crypt_iv_eboiv_ctr,
Milan Brozb9411d72019-07-09 15:22:14 +02001093 .generator = crypt_iv_eboiv_gen
1094};
1095
Milan Brozbbb16582020-01-03 09:20:22 +01001096static struct crypt_iv_operations crypt_iv_elephant_ops = {
1097 .ctr = crypt_iv_elephant_ctr,
1098 .dtr = crypt_iv_elephant_dtr,
1099 .init = crypt_iv_elephant_init,
1100 .wipe = crypt_iv_elephant_wipe,
1101 .generator = crypt_iv_elephant_gen,
1102 .post = crypt_iv_elephant_post
1103};
1104
Milan Brozef43aa32017-01-04 20:23:54 +01001105/*
1106 * Integrity extensions
1107 */
1108static bool crypt_integrity_aead(struct crypt_config *cc)
1109{
1110 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
1111}
1112
1113static bool crypt_integrity_hmac(struct crypt_config *cc)
1114{
Milan Broz33d2f092017-03-16 15:39:40 +01001115 return crypt_integrity_aead(cc) && cc->key_mac_size;
Milan Brozef43aa32017-01-04 20:23:54 +01001116}
1117
1118/* Get sg containing data */
1119static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
1120 struct scatterlist *sg)
1121{
Milan Broz33d2f092017-03-16 15:39:40 +01001122 if (unlikely(crypt_integrity_aead(cc)))
Milan Brozef43aa32017-01-04 20:23:54 +01001123 return &sg[2];
1124
1125 return sg;
1126}
1127
1128static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
1129{
1130 struct bio_integrity_payload *bip;
1131 unsigned int tag_len;
1132 int ret;
1133
1134 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size)
1135 return 0;
1136
1137 bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
1138 if (IS_ERR(bip))
1139 return PTR_ERR(bip);
1140
Mikulas Patockaff0c1292019-02-08 10:52:07 -05001141 tag_len = io->cc->on_disk_tag_size * (bio_sectors(bio) >> io->cc->sector_shift);
Milan Brozef43aa32017-01-04 20:23:54 +01001142
1143 bip->bip_iter.bi_size = tag_len;
1144 bip->bip_iter.bi_sector = io->cc->start + io->sector;
1145
Milan Brozef43aa32017-01-04 20:23:54 +01001146 ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
1147 tag_len, offset_in_page(io->integrity_metadata));
1148 if (unlikely(ret != tag_len))
1149 return -ENOMEM;
1150
1151 return 0;
1152}
1153
1154static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
1155{
1156#ifdef CONFIG_BLK_DEV_INTEGRITY
1157 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk);
Milan Broz7a1cd722019-05-15 16:23:43 +02001158 struct mapped_device *md = dm_table_get_md(ti->table);
Milan Brozef43aa32017-01-04 20:23:54 +01001159
1160 /* From now we require underlying device with our integrity profile */
1161 if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) {
1162 ti->error = "Integrity profile not supported.";
1163 return -EINVAL;
1164 }
1165
Mikulas Patocka583fe742017-04-18 16:51:54 -04001166 if (bi->tag_size != cc->on_disk_tag_size ||
1167 bi->tuple_size != cc->on_disk_tag_size) {
Milan Brozef43aa32017-01-04 20:23:54 +01001168 ti->error = "Integrity profile tag size mismatch.";
1169 return -EINVAL;
1170 }
Mikulas Patocka583fe742017-04-18 16:51:54 -04001171 if (1 << bi->interval_exp != cc->sector_size) {
1172 ti->error = "Integrity profile sector size mismatch.";
1173 return -EINVAL;
1174 }
Milan Brozef43aa32017-01-04 20:23:54 +01001175
Milan Broz33d2f092017-03-16 15:39:40 +01001176 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +01001177 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size;
Milan Broz7a1cd722019-05-15 16:23:43 +02001178 DMDEBUG("%s: Integrity AEAD, tag size %u, IV size %u.", dm_device_name(md),
Milan Brozef43aa32017-01-04 20:23:54 +01001179 cc->integrity_tag_size, cc->integrity_iv_size);
1180
1181 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) {
1182 ti->error = "Integrity AEAD auth tag size is not supported.";
1183 return -EINVAL;
1184 }
1185 } else if (cc->integrity_iv_size)
Milan Broz7a1cd722019-05-15 16:23:43 +02001186 DMDEBUG("%s: Additional per-sector space %u bytes for IV.", dm_device_name(md),
Milan Brozef43aa32017-01-04 20:23:54 +01001187 cc->integrity_iv_size);
1188
1189 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) {
1190 ti->error = "Not enough space for integrity tag in the profile.";
1191 return -EINVAL;
1192 }
1193
1194 return 0;
1195#else
1196 ti->error = "Integrity profile not supported.";
1197 return -EINVAL;
1198#endif
1199}
1200
Milan Brozd469f842007-10-19 22:42:37 +01001201static void crypt_convert_init(struct crypt_config *cc,
1202 struct convert_context *ctx,
1203 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +00001204 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
1206 ctx->bio_in = bio_in;
1207 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -07001208 if (bio_in)
1209 ctx->iter_in = bio_in->bi_iter;
1210 if (bio_out)
1211 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +01001212 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +00001213 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Huang Yingb2174ee2009-03-16 17:44:33 +00001216static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
Milan Brozef43aa32017-01-04 20:23:54 +01001217 void *req)
Huang Yingb2174ee2009-03-16 17:44:33 +00001218{
1219 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
1220}
1221
Milan Brozef43aa32017-01-04 20:23:54 +01001222static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq)
Huang Yingb2174ee2009-03-16 17:44:33 +00001223{
Milan Brozef43aa32017-01-04 20:23:54 +01001224 return (void *)((char *)dmreq - cc->dmreq_start);
Huang Yingb2174ee2009-03-16 17:44:33 +00001225}
1226
Milan Broz2dc53272011-01-13 19:59:54 +00001227static u8 *iv_of_dmreq(struct crypt_config *cc,
1228 struct dm_crypt_request *dmreq)
1229{
Milan Broz33d2f092017-03-16 15:39:40 +01001230 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001231 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1232 crypto_aead_alignmask(any_tfm_aead(cc)) + 1);
1233 else
1234 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1235 crypto_skcipher_alignmask(any_tfm(cc)) + 1);
Milan Broz2dc53272011-01-13 19:59:54 +00001236}
1237
Milan Brozef43aa32017-01-04 20:23:54 +01001238static u8 *org_iv_of_dmreq(struct crypt_config *cc,
1239 struct dm_crypt_request *dmreq)
1240{
1241 return iv_of_dmreq(cc, dmreq) + cc->iv_size;
1242}
1243
Christoph Hellwigc13b5482019-04-04 18:33:34 +02001244static __le64 *org_sector_of_dmreq(struct crypt_config *cc,
Milan Brozef43aa32017-01-04 20:23:54 +01001245 struct dm_crypt_request *dmreq)
1246{
1247 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size;
Christoph Hellwigc13b5482019-04-04 18:33:34 +02001248 return (__le64 *) ptr;
Milan Brozef43aa32017-01-04 20:23:54 +01001249}
1250
1251static unsigned int *org_tag_of_dmreq(struct crypt_config *cc,
1252 struct dm_crypt_request *dmreq)
1253{
1254 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size +
1255 cc->iv_size + sizeof(uint64_t);
1256 return (unsigned int*)ptr;
1257}
1258
1259static void *tag_from_dmreq(struct crypt_config *cc,
1260 struct dm_crypt_request *dmreq)
1261{
1262 struct convert_context *ctx = dmreq->ctx;
1263 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1264
1265 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) *
1266 cc->on_disk_tag_size];
1267}
1268
1269static void *iv_tag_from_dmreq(struct crypt_config *cc,
1270 struct dm_crypt_request *dmreq)
1271{
1272 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size;
1273}
1274
1275static int crypt_convert_block_aead(struct crypt_config *cc,
1276 struct convert_context *ctx,
1277 struct aead_request *req,
1278 unsigned int tag_offset)
Milan Broz01482b72008-02-08 02:11:04 +00001279{
Kent Overstreet003b5c52013-10-11 15:45:43 -07001280 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1281 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001282 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001283 u8 *iv, *org_iv, *tag_iv, *tag;
Christoph Hellwigc13b5482019-04-04 18:33:34 +02001284 __le64 *sector;
Milan Brozef43aa32017-01-04 20:23:54 +01001285 int r = 0;
1286
1287 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size);
Milan Broz01482b72008-02-08 02:11:04 +00001288
Milan Broz8f0009a2017-03-16 15:39:44 +01001289 /* Reject unexpected unaligned bio. */
Mikulas Patocka0440d5c2017-11-07 10:35:57 -05001290 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Broz8f0009a2017-03-16 15:39:44 +01001291 return -EIO;
Milan Broz01482b72008-02-08 02:11:04 +00001292
Huang Yingb2174ee2009-03-16 17:44:33 +00001293 dmreq = dmreq_of_req(cc, req);
Mikulas Patockac66029f2012-07-27 15:08:05 +01001294 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001295 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001296 dmreq->iv_sector >>= cc->sector_shift;
Huang Yingb2174ee2009-03-16 17:44:33 +00001297 dmreq->ctx = ctx;
Milan Broz01482b72008-02-08 02:11:04 +00001298
Milan Brozef43aa32017-01-04 20:23:54 +01001299 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
Milan Broz01482b72008-02-08 02:11:04 +00001300
Milan Brozef43aa32017-01-04 20:23:54 +01001301 sector = org_sector_of_dmreq(cc, dmreq);
1302 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1303
1304 iv = iv_of_dmreq(cc, dmreq);
1305 org_iv = org_iv_of_dmreq(cc, dmreq);
1306 tag = tag_from_dmreq(cc, dmreq);
1307 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1308
1309 /* AEAD request:
1310 * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
1311 * | (authenticated) | (auth+encryption) | |
1312 * | sector_LE | IV | sector in/out | tag in/out |
1313 */
1314 sg_init_table(dmreq->sg_in, 4);
1315 sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t));
1316 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001317 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 +01001318 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size);
1319
1320 sg_init_table(dmreq->sg_out, 4);
1321 sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t));
1322 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001323 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 +01001324 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size);
Milan Broz01482b72008-02-08 02:11:04 +00001325
Milan Broz3a7f6c92008-02-08 02:11:14 +00001326 if (cc->iv_gen_ops) {
Milan Brozef43aa32017-01-04 20:23:54 +01001327 /* For READs use IV stored in integrity metadata */
1328 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1329 memcpy(org_iv, tag_iv, cc->iv_size);
1330 } else {
1331 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1332 if (r < 0)
1333 return r;
1334 /* Store generated IV in integrity metadata */
1335 if (cc->integrity_iv_size)
1336 memcpy(tag_iv, org_iv, cc->iv_size);
1337 }
1338 /* Working copy of IV, to be modified in crypto API */
1339 memcpy(iv, org_iv, cc->iv_size);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001340 }
1341
Milan Brozef43aa32017-01-04 20:23:54 +01001342 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size);
1343 if (bio_data_dir(ctx->bio_in) == WRITE) {
1344 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001345 cc->sector_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001346 r = crypto_aead_encrypt(req);
1347 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size)
1348 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0,
1349 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size));
1350 } else {
1351 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001352 cc->sector_size + cc->integrity_tag_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001353 r = crypto_aead_decrypt(req);
1354 }
1355
Milan Brozf7101262019-05-15 16:22:30 +02001356 if (r == -EBADMSG) {
1357 char b[BDEVNAME_SIZE];
1358 DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu", bio_devname(ctx->bio_in, b),
Milan Brozef43aa32017-01-04 20:23:54 +01001359 (unsigned long long)le64_to_cpu(*sector));
Milan Brozf7101262019-05-15 16:22:30 +02001360 }
Milan Brozef43aa32017-01-04 20:23:54 +01001361
1362 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
1363 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1364
Milan Broz8f0009a2017-03-16 15:39:44 +01001365 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1366 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001367
1368 return r;
1369}
1370
1371static int crypt_convert_block_skcipher(struct crypt_config *cc,
1372 struct convert_context *ctx,
1373 struct skcipher_request *req,
1374 unsigned int tag_offset)
1375{
1376 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1377 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
1378 struct scatterlist *sg_in, *sg_out;
1379 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001380 u8 *iv, *org_iv, *tag_iv;
Christoph Hellwigc13b5482019-04-04 18:33:34 +02001381 __le64 *sector;
Milan Brozef43aa32017-01-04 20:23:54 +01001382 int r = 0;
1383
Milan Broz8f0009a2017-03-16 15:39:44 +01001384 /* Reject unexpected unaligned bio. */
Mikulas Patocka0440d5c2017-11-07 10:35:57 -05001385 if (unlikely(bv_in.bv_len & (cc->sector_size - 1)))
Milan Broz8f0009a2017-03-16 15:39:44 +01001386 return -EIO;
1387
Milan Brozef43aa32017-01-04 20:23:54 +01001388 dmreq = dmreq_of_req(cc, req);
1389 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001390 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001391 dmreq->iv_sector >>= cc->sector_shift;
Milan Brozef43aa32017-01-04 20:23:54 +01001392 dmreq->ctx = ctx;
1393
1394 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
1395
1396 iv = iv_of_dmreq(cc, dmreq);
1397 org_iv = org_iv_of_dmreq(cc, dmreq);
1398 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1399
1400 sector = org_sector_of_dmreq(cc, dmreq);
1401 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1402
1403 /* For skcipher we use only the first sg item */
1404 sg_in = &dmreq->sg_in[0];
1405 sg_out = &dmreq->sg_out[0];
1406
1407 sg_init_table(sg_in, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001408 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001409
1410 sg_init_table(sg_out, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001411 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001412
1413 if (cc->iv_gen_ops) {
1414 /* For READs use IV stored in integrity metadata */
1415 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1416 memcpy(org_iv, tag_iv, cc->integrity_iv_size);
1417 } else {
1418 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1419 if (r < 0)
1420 return r;
Milan Brozbbb16582020-01-03 09:20:22 +01001421 /* Data can be already preprocessed in generator */
1422 if (test_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags))
1423 sg_in = sg_out;
Milan Brozef43aa32017-01-04 20:23:54 +01001424 /* Store generated IV in integrity metadata */
1425 if (cc->integrity_iv_size)
1426 memcpy(tag_iv, org_iv, cc->integrity_iv_size);
1427 }
1428 /* Working copy of IV, to be modified in crypto API */
1429 memcpy(iv, org_iv, cc->iv_size);
1430 }
1431
Milan Broz8f0009a2017-03-16 15:39:44 +01001432 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001433
1434 if (bio_data_dir(ctx->bio_in) == WRITE)
Herbert Xubbdb23b2016-01-24 21:16:36 +08001435 r = crypto_skcipher_encrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001436 else
Herbert Xubbdb23b2016-01-24 21:16:36 +08001437 r = crypto_skcipher_decrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001438
Milan Broz2dc53272011-01-13 19:59:54 +00001439 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001440 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1441
Milan Broz8f0009a2017-03-16 15:39:44 +01001442 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1443 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Broz2dc53272011-01-13 19:59:54 +00001444
Milan Broz3a7f6c92008-02-08 02:11:14 +00001445 return r;
Milan Broz01482b72008-02-08 02:11:04 +00001446}
1447
Milan Broz95497a92008-02-08 02:11:12 +00001448static void kcryptd_async_done(struct crypto_async_request *async_req,
1449 int error);
Andi Kleenc0297722011-01-13 19:59:53 +00001450
Milan Brozef43aa32017-01-04 20:23:54 +01001451static void crypt_alloc_req_skcipher(struct crypt_config *cc,
1452 struct convert_context *ctx)
Milan Brozddd42ed2008-02-08 02:11:07 +00001453{
Mikulas Patockac66029f2012-07-27 15:08:05 +01001454 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +00001455
Milan Brozef43aa32017-01-04 20:23:54 +01001456 if (!ctx->r.req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001457 ctx->r.req = mempool_alloc(&cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +00001458
Milan Brozef43aa32017-01-04 20:23:54 +01001459 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]);
Milan Broz54cea3f2015-05-15 17:00:25 +02001460
1461 /*
1462 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1463 * requests if driver request queue is full.
1464 */
Milan Brozef43aa32017-01-04 20:23:54 +01001465 skcipher_request_set_callback(ctx->r.req,
Mikulas Patocka432061b2018-09-05 09:17:45 -04001466 CRYPTO_TFM_REQ_MAY_BACKLOG,
Milan Brozef43aa32017-01-04 20:23:54 +01001467 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req));
Milan Brozddd42ed2008-02-08 02:11:07 +00001468}
1469
Milan Brozef43aa32017-01-04 20:23:54 +01001470static void crypt_alloc_req_aead(struct crypt_config *cc,
1471 struct convert_context *ctx)
1472{
1473 if (!ctx->r.req_aead)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001474 ctx->r.req_aead = mempool_alloc(&cc->req_pool, GFP_NOIO);
Milan Brozef43aa32017-01-04 20:23:54 +01001475
1476 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]);
1477
1478 /*
1479 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1480 * requests if driver request queue is full.
1481 */
1482 aead_request_set_callback(ctx->r.req_aead,
Mikulas Patocka432061b2018-09-05 09:17:45 -04001483 CRYPTO_TFM_REQ_MAY_BACKLOG,
Milan Brozef43aa32017-01-04 20:23:54 +01001484 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead));
1485}
1486
1487static void crypt_alloc_req(struct crypt_config *cc,
1488 struct convert_context *ctx)
1489{
Milan Broz33d2f092017-03-16 15:39:40 +01001490 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001491 crypt_alloc_req_aead(cc, ctx);
1492 else
1493 crypt_alloc_req_skcipher(cc, ctx);
1494}
1495
1496static void crypt_free_req_skcipher(struct crypt_config *cc,
1497 struct skcipher_request *req, struct bio *base_bio)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001498{
1499 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1500
Herbert Xubbdb23b2016-01-24 21:16:36 +08001501 if ((struct skcipher_request *)(io + 1) != req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001502 mempool_free(req, &cc->req_pool);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001503}
1504
Milan Brozef43aa32017-01-04 20:23:54 +01001505static void crypt_free_req_aead(struct crypt_config *cc,
1506 struct aead_request *req, struct bio *base_bio)
1507{
1508 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1509
1510 if ((struct aead_request *)(io + 1) != req)
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001511 mempool_free(req, &cc->req_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01001512}
1513
1514static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio)
1515{
Milan Broz33d2f092017-03-16 15:39:40 +01001516 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001517 crypt_free_req_aead(cc, req, base_bio);
1518 else
1519 crypt_free_req_skcipher(cc, req, base_bio);
1520}
1521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522/*
1523 * Encrypt / decrypt data from one bio to another one (can be the same one)
1524 */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001525static blk_status_t crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +01001526 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Milan Brozef43aa32017-01-04 20:23:54 +01001528 unsigned int tag_offset = 0;
Mikulas Patockaff3af922017-03-23 10:23:14 -04001529 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT;
Milan Broz3f1e9072008-03-28 14:16:07 -07001530 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Mikulas Patocka40b62292012-07-27 15:08:04 +01001532 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +01001533
Kent Overstreet003b5c52013-10-11 15:45:43 -07001534 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Milan Broz3a7f6c92008-02-08 02:11:14 +00001536 crypt_alloc_req(cc, ctx);
Mikulas Patocka40b62292012-07-27 15:08:04 +01001537 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -07001538
Milan Broz33d2f092017-03-16 15:39:40 +01001539 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001540 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset);
1541 else
1542 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001543
1544 switch (r) {
Milan Broz54cea3f2015-05-15 17:00:25 +02001545 /*
1546 * The request was queued by a crypto driver
1547 * but the driver request queue is full, let's wait.
1548 */
Milan Broz3a7f6c92008-02-08 02:11:14 +00001549 case -EBUSY:
1550 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -08001551 reinit_completion(&ctx->restart);
Milan Broz54cea3f2015-05-15 17:00:25 +02001552 /* fall through */
1553 /*
1554 * The request is queued and processed asynchronously,
1555 * completion function kcryptd_async_done() will be called.
1556 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001557 case -EINPROGRESS:
Milan Brozef43aa32017-01-04 20:23:54 +01001558 ctx->r.req = NULL;
Milan Broz8f0009a2017-03-16 15:39:44 +01001559 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001560 tag_offset++;
Milan Broz3a7f6c92008-02-08 02:11:14 +00001561 continue;
Milan Broz54cea3f2015-05-15 17:00:25 +02001562 /*
1563 * The request was already processed (synchronously).
1564 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001565 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001566 atomic_dec(&ctx->cc_pending);
Milan Broz8f0009a2017-03-16 15:39:44 +01001567 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001568 tag_offset++;
Milan Brozc7f1b202008-07-02 09:34:28 +01001569 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -07001570 continue;
Milan Brozef43aa32017-01-04 20:23:54 +01001571 /*
1572 * There was a data integrity error.
1573 */
1574 case -EBADMSG:
1575 atomic_dec(&ctx->cc_pending);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001576 return BLK_STS_PROTECTION;
Milan Brozef43aa32017-01-04 20:23:54 +01001577 /*
1578 * There was an error while processing the request.
1579 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001580 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001581 atomic_dec(&ctx->cc_pending);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001582 return BLK_STS_IOERR;
Milan Broz3f1e9072008-03-28 14:16:07 -07001583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585
Milan Broz3f1e9072008-03-28 14:16:07 -07001586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001589static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591/*
1592 * Generate a new unfragmented bio with the given size
Mike Snitzer586b2862015-09-09 21:34:51 -04001593 * This should never violate the device limitations (but only because
1594 * max_segment_size is being constrained to PAGE_SIZE).
Mikulas Patocka7145c242015-02-13 08:24:41 -05001595 *
1596 * This function may be called concurrently. If we allocate from the mempool
1597 * concurrently, there is a possibility of deadlock. For example, if we have
1598 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1599 * the mempool concurrently, it may deadlock in a situation where both processes
1600 * have allocated 128 pages and the mempool is exhausted.
1601 *
1602 * In order to avoid this scenario we allocate the pages under a mutex.
1603 *
1604 * In order to not degrade performance with excessive locking, we try
1605 * non-blocking allocations without a mutex first but on failure we fallback
1606 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001608static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001610 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001611 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001613 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
1614 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +00001615 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Mikulas Patocka7145c242015-02-13 08:24:41 -05001617retry:
Mel Gormand0164ad2015-11-06 16:28:21 -08001618 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001619 mutex_lock(&cc->bio_alloc_lock);
1620
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001621 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, &cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -07001622 if (!clone)
Milan Brozef43aa32017-01-04 20:23:54 +01001623 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Olaf Kirch027581f2007-05-09 02:32:52 -07001625 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -07001626
Mikulas Patocka7145c242015-02-13 08:24:41 -05001627 remaining_size = size;
1628
Olaf Kirchf97380b2007-05-09 02:32:54 -07001629 for (i = 0; i < nr_iovecs; i++) {
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001630 page = mempool_alloc(&cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -05001631 if (!page) {
1632 crypt_free_buffer_pages(cc, clone);
1633 bio_put(clone);
Mel Gormand0164ad2015-11-06 16:28:21 -08001634 gfp_mask |= __GFP_DIRECT_RECLAIM;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001635 goto retry;
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Mikulas Patocka7145c242015-02-13 08:24:41 -05001638 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Ming Lei0dae7fe2016-10-29 16:08:06 +08001640 bio_add_page(clone, page, len, 0);
Milan Broz91e10622007-12-13 14:16:10 +00001641
Mikulas Patocka7145c242015-02-13 08:24:41 -05001642 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644
Milan Brozef43aa32017-01-04 20:23:54 +01001645 /* Allocate space for integrity tags */
1646 if (dm_crypt_integrity_io_alloc(io, clone)) {
1647 crypt_free_buffer_pages(cc, clone);
1648 bio_put(clone);
1649 clone = NULL;
1650 }
1651out:
Mel Gormand0164ad2015-11-06 16:28:21 -08001652 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001653 mutex_unlock(&cc->bio_alloc_lock);
1654
Milan Broz8b004452006-10-03 01:15:37 -07001655 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Neil Brown644bd2f2007-10-16 13:48:46 +02001658static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 struct bio_vec *bv;
Ming Lei6dc4f102019-02-15 19:13:19 +08001661 struct bvec_iter_all iter_all;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02001663 bio_for_each_segment_all(bv, clone, iter_all) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 BUG_ON(!bv->bv_page);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001665 mempool_free(bv->bv_page, &cc->page_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667}
1668
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001669static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1670 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001671{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001672 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001673 io->base_bio = bio;
1674 io->sector = sector;
1675 io->error = 0;
Milan Brozef43aa32017-01-04 20:23:54 +01001676 io->ctx.r.req = NULL;
1677 io->integrity_metadata = NULL;
1678 io->integrity_metadata_from_pool = false;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001679 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001680}
1681
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001682static void crypt_inc_pending(struct dm_crypt_io *io)
1683{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001684 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001685}
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687/*
1688 * One of the bios was finished. Check for completion of
1689 * the whole request and correctly clean up the buffer.
1690 */
Milan Broz5742fd72008-02-08 02:10:43 +00001691static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001693 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001694 struct bio *base_bio = io->base_bio;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001695 blk_status_t error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Mikulas Patocka40b62292012-07-27 15:08:04 +01001697 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return;
1699
Milan Brozef43aa32017-01-04 20:23:54 +01001700 if (io->ctx.r.req)
1701 crypt_free_req(cc, io->ctx.r.req, base_bio);
1702
1703 if (unlikely(io->integrity_metadata_from_pool))
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001704 mempool_free(io->integrity_metadata, &io->cc->tag_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01001705 else
1706 kfree(io->integrity_metadata);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001707
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001708 base_bio->bi_status = error;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001709 bio_endio(base_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
1711
1712/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001713 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 *
1715 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001716 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001717 *
1718 * kcryptd performs the actual encryption or decryption.
1719 *
1720 * kcryptd_io performs the IO submission.
1721 *
1722 * They must be separated as otherwise the final stages could be
1723 * starved by new requests which can block in the first stages due
1724 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001725 *
1726 * The work is done per CPU global for all dm-crypt instances.
1727 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001729static void crypt_endio(struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001730{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001731 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001732 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001733 unsigned rw = bio_data_dir(clone);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001734 blk_status_t error;
Milan Broz8b004452006-10-03 01:15:37 -07001735
1736 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001737 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001738 */
Milan Brozee7a4912008-02-08 02:10:46 +00001739 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001740 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001741
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001742 error = clone->bi_status;
Milan Brozee7a4912008-02-08 02:10:46 +00001743 bio_put(clone);
1744
Sasha Levin9b81c842015-08-10 19:05:18 -04001745 if (rw == READ && !error) {
Milan Brozee7a4912008-02-08 02:10:46 +00001746 kcryptd_queue_crypt(io);
1747 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001748 }
Milan Broz8b004452006-10-03 01:15:37 -07001749
Sasha Levin9b81c842015-08-10 19:05:18 -04001750 if (unlikely(error))
1751 io->error = error;
Milan Broz5742fd72008-02-08 02:10:43 +00001752
1753 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001754}
1755
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001756static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001757{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001758 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001759
1760 clone->bi_private = io;
1761 clone->bi_end_io = crypt_endio;
Christoph Hellwig74d46992017-08-23 19:10:32 +02001762 bio_set_dev(clone, cc->dev->bdev);
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001763 clone->bi_opf = io->base_bio->bi_opf;
Milan Broz8b004452006-10-03 01:15:37 -07001764}
1765
Milan Broz20c82532011-01-13 19:59:53 +00001766static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001767{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001768 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001769 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001770
Milan Broz8b004452006-10-03 01:15:37 -07001771 /*
Mike Snitzer59779072015-04-09 16:53:24 -04001772 * We need the original biovec array in order to decrypt
1773 * the whole bio data *afterwards* -- thanks to immutable
1774 * biovecs we don't need to worry about the block layer
1775 * modifying the biovec array; so leverage bio_clone_fast().
Milan Broz8b004452006-10-03 01:15:37 -07001776 */
Kent Overstreet6f1c8192018-05-20 18:25:53 -04001777 clone = bio_clone_fast(io->base_bio, gfp, &cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001778 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001779 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001780
Milan Broz20c82532011-01-13 19:59:53 +00001781 crypt_inc_pending(io);
1782
Milan Broz8b004452006-10-03 01:15:37 -07001783 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001784 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001785
Milan Brozef43aa32017-01-04 20:23:54 +01001786 if (dm_crypt_integrity_io_alloc(io, clone)) {
1787 crypt_dec_pending(io);
1788 bio_put(clone);
1789 return 1;
1790 }
1791
Milan Broz93e605c2006-10-03 01:15:38 -07001792 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001793 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001794}
1795
Mikulas Patockadc267622015-02-13 08:25:59 -05001796static void kcryptd_io_read_work(struct work_struct *work)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001797{
1798 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1799
Mikulas Patockadc267622015-02-13 08:25:59 -05001800 crypt_inc_pending(io);
1801 if (kcryptd_io_read(io, GFP_NOIO))
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001802 io->error = BLK_STS_RESOURCE;
Mikulas Patockadc267622015-02-13 08:25:59 -05001803 crypt_dec_pending(io);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001804}
1805
Mikulas Patockadc267622015-02-13 08:25:59 -05001806static void kcryptd_queue_read(struct dm_crypt_io *io)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001807{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001808 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001809
Mikulas Patockadc267622015-02-13 08:25:59 -05001810 INIT_WORK(&io->work, kcryptd_io_read_work);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001811 queue_work(cc->io_queue, &io->work);
1812}
1813
Mikulas Patockadc267622015-02-13 08:25:59 -05001814static void kcryptd_io_write(struct dm_crypt_io *io)
1815{
1816 struct bio *clone = io->ctx.bio_out;
1817
1818 generic_make_request(clone);
1819}
1820
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001821#define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1822
Mikulas Patockadc267622015-02-13 08:25:59 -05001823static int dmcrypt_write(void *data)
1824{
1825 struct crypt_config *cc = data;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001826 struct dm_crypt_io *io;
1827
Mikulas Patockadc267622015-02-13 08:25:59 -05001828 while (1) {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001829 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -05001830 struct blk_plug plug;
1831
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001832 spin_lock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001833continue_locked:
1834
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001835 if (!RB_EMPTY_ROOT(&cc->write_tree))
Mikulas Patockadc267622015-02-13 08:25:59 -05001836 goto pop_from_list;
1837
Rabin Vincentf659b102016-09-21 16:22:29 +02001838 set_current_state(TASK_INTERRUPTIBLE);
Mikulas Patockadc267622015-02-13 08:25:59 -05001839
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001840 spin_unlock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001841
Rabin Vincentf659b102016-09-21 16:22:29 +02001842 if (unlikely(kthread_should_stop())) {
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001843 set_current_state(TASK_RUNNING);
Rabin Vincentf659b102016-09-21 16:22:29 +02001844 break;
1845 }
1846
Mikulas Patockadc267622015-02-13 08:25:59 -05001847 schedule();
1848
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001849 set_current_state(TASK_RUNNING);
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001850 spin_lock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001851 goto continue_locked;
1852
1853pop_from_list:
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001854 write_tree = cc->write_tree;
1855 cc->write_tree = RB_ROOT;
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001856 spin_unlock_irq(&cc->write_thread_lock);
Mikulas Patockadc267622015-02-13 08:25:59 -05001857
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001858 BUG_ON(rb_parent(write_tree.rb_node));
1859
1860 /*
1861 * Note: we cannot walk the tree here with rb_next because
1862 * the structures may be freed when kcryptd_io_write is called.
1863 */
Mikulas Patockadc267622015-02-13 08:25:59 -05001864 blk_start_plug(&plug);
1865 do {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001866 io = crypt_io_from_node(rb_first(&write_tree));
1867 rb_erase(&io->rb_node, &write_tree);
Mikulas Patockadc267622015-02-13 08:25:59 -05001868 kcryptd_io_write(io);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001869 } while (!RB_EMPTY_ROOT(&write_tree));
Mikulas Patockadc267622015-02-13 08:25:59 -05001870 blk_finish_plug(&plug);
1871 }
1872 return 0;
1873}
1874
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001875static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001876{
Milan Brozdec1ced2008-02-08 02:10:57 +00001877 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001878 struct crypt_config *cc = io->cc;
Mikulas Patockadc267622015-02-13 08:25:59 -05001879 unsigned long flags;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001880 sector_t sector;
1881 struct rb_node **rbp, *parent;
Milan Brozdec1ced2008-02-08 02:10:57 +00001882
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001883 if (unlikely(io->error)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001884 crypt_free_buffer_pages(cc, clone);
1885 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001886 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001887 return;
1888 }
1889
1890 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001891 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001892
Kent Overstreet4f024f32013-10-11 15:44:27 -07001893 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001894
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001895 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1896 generic_make_request(clone);
1897 return;
1898 }
1899
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001900 spin_lock_irqsave(&cc->write_thread_lock, flags);
1901 if (RB_EMPTY_ROOT(&cc->write_tree))
1902 wake_up_process(cc->write_thread);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001903 rbp = &cc->write_tree.rb_node;
1904 parent = NULL;
1905 sector = io->sector;
1906 while (*rbp) {
1907 parent = *rbp;
1908 if (sector < crypt_io_from_node(parent)->sector)
1909 rbp = &(*rbp)->rb_left;
1910 else
1911 rbp = &(*rbp)->rb_right;
1912 }
1913 rb_link_node(&io->rb_node, parent, rbp);
1914 rb_insert_color(&io->rb_node, &cc->write_tree);
Mikulas Patockac7329ef2018-07-11 12:10:51 -04001915 spin_unlock_irqrestore(&cc->write_thread_lock, flags);
Milan Broz4e4eef62008-02-08 02:10:49 +00001916}
1917
Milan Brozfc5a5e92008-10-10 13:37:04 +01001918static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001919{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001920 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001921 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001922 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001923 sector_t sector = io->sector;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001924 blk_status_t r;
Milan Broz8b004452006-10-03 01:15:37 -07001925
Milan Broz93e605c2006-10-03 01:15:38 -07001926 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001927 * Prevent io from disappearing until this function completes.
1928 */
1929 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001930 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001931
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001932 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1933 if (unlikely(!clone)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001934 io->error = BLK_STS_IOERR;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001935 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001936 }
Milan Broz899c95d2008-02-08 02:11:02 +00001937
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001938 io->ctx.bio_out = clone;
1939 io->ctx.iter_out = clone->bi_iter;
1940
1941 sector += bio_sectors(clone);
1942
1943 crypt_inc_pending(io);
1944 r = crypt_convert(cc, &io->ctx);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001945 if (r)
Milan Brozef43aa32017-01-04 20:23:54 +01001946 io->error = r;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001947 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1948
1949 /* Encryption was already finished, submit io now */
1950 if (crypt_finished) {
1951 kcryptd_crypt_write_io_submit(io, 0);
1952 io->sector = sector;
1953 }
1954
1955dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001956 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001957}
1958
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001959static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001960{
Milan Broz5742fd72008-02-08 02:10:43 +00001961 crypt_dec_pending(io);
1962}
1963
Milan Broz4e4eef62008-02-08 02:10:49 +00001964static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001965{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001966 struct crypt_config *cc = io->cc;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001967 blk_status_t r;
Milan Broz8b004452006-10-03 01:15:37 -07001968
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001969 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001970
Milan Broz53017032008-02-08 02:10:38 +00001971 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001972 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001973
Milan Broz5742fd72008-02-08 02:10:43 +00001974 r = crypt_convert(cc, &io->ctx);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02001975 if (r)
Milan Brozef43aa32017-01-04 20:23:54 +01001976 io->error = r;
Milan Broz5742fd72008-02-08 02:10:43 +00001977
Mikulas Patocka40b62292012-07-27 15:08:04 +01001978 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001979 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001980
1981 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001982}
1983
Milan Broz95497a92008-02-08 02:11:12 +00001984static void kcryptd_async_done(struct crypto_async_request *async_req,
1985 int error)
1986{
Huang Yingb2174ee2009-03-16 17:44:33 +00001987 struct dm_crypt_request *dmreq = async_req->data;
1988 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001989 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001990 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001991
Milan Broz54cea3f2015-05-15 17:00:25 +02001992 /*
1993 * A request from crypto driver backlog is going to be processed now,
1994 * finish the completion and continue in crypt_convert().
1995 * (Callback will be called for the second time for this request.)
1996 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001997 if (error == -EINPROGRESS) {
1998 complete(&ctx->restart);
Milan Broz95497a92008-02-08 02:11:12 +00001999 return;
Rabin Vincentc0403ec2015-05-05 15:15:56 +02002000 }
Milan Broz95497a92008-02-08 02:11:12 +00002001
Milan Broz2dc53272011-01-13 19:59:54 +00002002 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01002003 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq);
Milan Broz2dc53272011-01-13 19:59:54 +00002004
Milan Brozef43aa32017-01-04 20:23:54 +01002005 if (error == -EBADMSG) {
Milan Brozf7101262019-05-15 16:22:30 +02002006 char b[BDEVNAME_SIZE];
2007 DMERR_LIMIT("%s: INTEGRITY AEAD ERROR, sector %llu", bio_devname(ctx->bio_in, b),
Milan Brozef43aa32017-01-04 20:23:54 +01002008 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002009 io->error = BLK_STS_PROTECTION;
Milan Brozef43aa32017-01-04 20:23:54 +01002010 } else if (error < 0)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002011 io->error = BLK_STS_IOERR;
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01002012
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002013 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00002014
Mikulas Patocka40b62292012-07-27 15:08:04 +01002015 if (!atomic_dec_and_test(&ctx->cc_pending))
Rabin Vincentc0403ec2015-05-05 15:15:56 +02002016 return;
Milan Broz95497a92008-02-08 02:11:12 +00002017
2018 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01002019 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00002020 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01002021 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00002022}
2023
Milan Broz4e4eef62008-02-08 02:10:49 +00002024static void kcryptd_crypt(struct work_struct *work)
2025{
2026 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
2027
2028 if (bio_data_dir(io->base_bio) == READ)
2029 kcryptd_crypt_read_convert(io);
2030 else
2031 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07002032}
2033
Alasdair G Kergon395b1672008-02-08 02:10:52 +00002034static void kcryptd_queue_crypt(struct dm_crypt_io *io)
2035{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01002036 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00002037
2038 INIT_WORK(&io->work, kcryptd_crypt);
2039 queue_work(cc->crypt_queue, &io->work);
2040}
2041
Milan Brozef43aa32017-01-04 20:23:54 +01002042static void crypt_free_tfms_aead(struct crypt_config *cc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Milan Brozef43aa32017-01-04 20:23:54 +01002044 if (!cc->cipher_tfm.tfms_aead)
2045 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Milan Brozef43aa32017-01-04 20:23:54 +01002047 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
2048 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]);
2049 cc->cipher_tfm.tfms_aead[0] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051
Milan Brozef43aa32017-01-04 20:23:54 +01002052 kfree(cc->cipher_tfm.tfms_aead);
2053 cc->cipher_tfm.tfms_aead = NULL;
2054}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Milan Brozef43aa32017-01-04 20:23:54 +01002056static void crypt_free_tfms_skcipher(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00002057{
Milan Brozd1f96422011-01-13 19:59:54 +00002058 unsigned i;
2059
Milan Brozef43aa32017-01-04 20:23:54 +01002060 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002061 return;
2062
Milan Brozd1f96422011-01-13 19:59:54 +00002063 for (i = 0; i < cc->tfms_count; i++)
Milan Brozef43aa32017-01-04 20:23:54 +01002064 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) {
2065 crypto_free_skcipher(cc->cipher_tfm.tfms[i]);
2066 cc->cipher_tfm.tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00002067 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002068
Milan Brozef43aa32017-01-04 20:23:54 +01002069 kfree(cc->cipher_tfm.tfms);
2070 cc->cipher_tfm.tfms = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
2073static void crypt_free_tfms(struct crypt_config *cc)
2074{
Milan Broz33d2f092017-03-16 15:39:40 +01002075 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002076 crypt_free_tfms_aead(cc);
2077 else
2078 crypt_free_tfms_skcipher(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00002079}
2080
Milan Brozef43aa32017-01-04 20:23:54 +01002081static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00002082{
Milan Brozd1f96422011-01-13 19:59:54 +00002083 unsigned i;
2084 int err;
2085
Kees Cook6396bb22018-06-12 14:03:40 -07002086 cc->cipher_tfm.tfms = kcalloc(cc->tfms_count,
2087 sizeof(struct crypto_skcipher *),
2088 GFP_KERNEL);
Milan Brozef43aa32017-01-04 20:23:54 +01002089 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002090 return -ENOMEM;
2091
Milan Brozd1f96422011-01-13 19:59:54 +00002092 for (i = 0; i < cc->tfms_count; i++) {
Milan Brozef43aa32017-01-04 20:23:54 +01002093 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
2094 if (IS_ERR(cc->cipher_tfm.tfms[i])) {
2095 err = PTR_ERR(cc->cipher_tfm.tfms[i]);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002096 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00002097 return err;
2098 }
2099 }
2100
Eric Biggersaf331eb2018-12-05 20:53:00 -08002101 /*
2102 * dm-crypt performance can vary greatly depending on which crypto
2103 * algorithm implementation is used. Help people debug performance
2104 * problems by logging the ->cra_driver_name.
2105 */
Milan Broz7a1cd722019-05-15 16:23:43 +02002106 DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode,
Eric Biggersaf331eb2018-12-05 20:53:00 -08002107 crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name);
Milan Brozd1f96422011-01-13 19:59:54 +00002108 return 0;
2109}
2110
Milan Brozef43aa32017-01-04 20:23:54 +01002111static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
2112{
Milan Brozef43aa32017-01-04 20:23:54 +01002113 int err;
2114
2115 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
2116 if (!cc->cipher_tfm.tfms)
2117 return -ENOMEM;
2118
Milan Brozef43aa32017-01-04 20:23:54 +01002119 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
2120 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
2121 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
2122 crypt_free_tfms(cc);
2123 return err;
2124 }
2125
Milan Broz7a1cd722019-05-15 16:23:43 +02002126 DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode,
Eric Biggersaf331eb2018-12-05 20:53:00 -08002127 crypto_aead_alg(any_tfm_aead(cc))->base.cra_driver_name);
Milan Brozef43aa32017-01-04 20:23:54 +01002128 return 0;
2129}
2130
2131static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
2132{
Milan Broz33d2f092017-03-16 15:39:40 +01002133 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002134 return crypt_alloc_tfms_aead(cc, ciphermode);
2135 else
2136 return crypt_alloc_tfms_skcipher(cc, ciphermode);
2137}
2138
2139static unsigned crypt_subkey_size(struct crypt_config *cc)
2140{
2141 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
2142}
2143
2144static unsigned crypt_authenckey_size(struct crypt_config *cc)
2145{
2146 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
2147}
2148
2149/*
2150 * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
2151 * the key must be for some reason in special format.
2152 * This funcion converts cc->key to this special format.
2153 */
2154static void crypt_copy_authenckey(char *p, const void *key,
2155 unsigned enckeylen, unsigned authkeylen)
2156{
2157 struct crypto_authenc_key_param *param;
2158 struct rtattr *rta;
2159
2160 rta = (struct rtattr *)p;
2161 param = RTA_DATA(rta);
2162 param->enckeylen = cpu_to_be32(enckeylen);
2163 rta->rta_len = RTA_LENGTH(sizeof(*param));
2164 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
2165 p += RTA_SPACE(sizeof(*param));
2166 memcpy(p, key + enckeylen, authkeylen);
2167 p += authkeylen;
2168 memcpy(p, key, enckeylen);
2169}
2170
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002171static int crypt_setkey(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +00002172{
Milan Brozda31a072013-10-28 23:21:03 +01002173 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002174 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00002175
Milan Brozda31a072013-10-28 23:21:03 +01002176 /* Ignore extra keys (which are used for IV etc) */
Milan Brozef43aa32017-01-04 20:23:54 +01002177 subkey_size = crypt_subkey_size(cc);
Milan Brozda31a072013-10-28 23:21:03 +01002178
Milan Broz27c70032018-01-03 22:48:59 +01002179 if (crypt_integrity_hmac(cc)) {
2180 if (subkey_size < cc->key_mac_size)
2181 return -EINVAL;
2182
Milan Brozef43aa32017-01-04 20:23:54 +01002183 crypt_copy_authenckey(cc->authenc_key, cc->key,
2184 subkey_size - cc->key_mac_size,
2185 cc->key_mac_size);
Milan Broz27c70032018-01-03 22:48:59 +01002186 }
2187
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002188 for (i = 0; i < cc->tfms_count; i++) {
Milan Broz33d2f092017-03-16 15:39:40 +01002189 if (crypt_integrity_hmac(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002190 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
2191 cc->authenc_key, crypt_authenckey_size(cc));
Milan Broz33d2f092017-03-16 15:39:40 +01002192 else if (crypt_integrity_aead(cc))
2193 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
2194 cc->key + (i * subkey_size),
2195 subkey_size);
Milan Brozef43aa32017-01-04 20:23:54 +01002196 else
2197 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i],
2198 cc->key + (i * subkey_size),
2199 subkey_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002200 if (r)
2201 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00002202 }
2203
Milan Brozef43aa32017-01-04 20:23:54 +01002204 if (crypt_integrity_hmac(cc))
2205 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc));
2206
Andi Kleenc0297722011-01-13 19:59:53 +00002207 return err;
2208}
2209
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002210#ifdef CONFIG_KEYS
2211
Ondrej Kozina027c4312016-12-01 18:20:52 +01002212static bool contains_whitespace(const char *str)
2213{
2214 while (*str)
2215 if (isspace(*str++))
2216 return true;
2217 return false;
2218}
2219
Dmitry Baryshkov27f54112020-04-20 16:46:59 +03002220static int set_key_user(struct crypt_config *cc, struct key *key)
2221{
2222 const struct user_key_payload *ukp;
2223
2224 ukp = user_key_payload_locked(key);
2225 if (!ukp)
2226 return -EKEYREVOKED;
2227
2228 if (cc->key_size != ukp->datalen)
2229 return -EINVAL;
2230
2231 memcpy(cc->key, ukp->data, cc->key_size);
2232
2233 return 0;
2234}
2235
2236#if defined(CONFIG_ENCRYPTED_KEYS) || defined(CONFIG_ENCRYPTED_KEYS_MODULE)
2237static int set_key_encrypted(struct crypt_config *cc, struct key *key)
2238{
2239 const struct encrypted_key_payload *ekp;
2240
2241 ekp = key->payload.data[0];
2242 if (!ekp)
2243 return -EKEYREVOKED;
2244
2245 if (cc->key_size != ekp->decrypted_datalen)
2246 return -EINVAL;
2247
2248 memcpy(cc->key, ekp->decrypted_data, cc->key_size);
2249
2250 return 0;
2251}
2252#endif /* CONFIG_ENCRYPTED_KEYS */
2253
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002254static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2255{
2256 char *new_key_string, *key_desc;
2257 int ret;
Dmitry Baryshkov27f54112020-04-20 16:46:59 +03002258 struct key_type *type;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002259 struct key *key;
Dmitry Baryshkov27f54112020-04-20 16:46:59 +03002260 int (*set_key)(struct crypt_config *cc, struct key *key);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002261
Ondrej Kozina027c4312016-12-01 18:20:52 +01002262 /*
2263 * Reject key_string with whitespace. dm core currently lacks code for
2264 * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
2265 */
2266 if (contains_whitespace(key_string)) {
2267 DMERR("whitespace chars not allowed in key string");
2268 return -EINVAL;
2269 }
2270
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002271 /* look for next ':' separating key_type from key_description */
2272 key_desc = strpbrk(key_string, ":");
2273 if (!key_desc || key_desc == key_string || !strlen(key_desc + 1))
2274 return -EINVAL;
2275
Dmitry Baryshkov27f54112020-04-20 16:46:59 +03002276 if (!strncmp(key_string, "logon:", key_desc - key_string + 1)) {
2277 type = &key_type_logon;
2278 set_key = set_key_user;
2279 } else if (!strncmp(key_string, "user:", key_desc - key_string + 1)) {
2280 type = &key_type_user;
2281 set_key = set_key_user;
2282#if defined(CONFIG_ENCRYPTED_KEYS) || defined(CONFIG_ENCRYPTED_KEYS_MODULE)
2283 } else if (!strncmp(key_string, "encrypted:", key_desc - key_string + 1)) {
2284 type = &key_type_encrypted;
2285 set_key = set_key_encrypted;
2286#endif
2287 } else {
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002288 return -EINVAL;
Dmitry Baryshkov27f54112020-04-20 16:46:59 +03002289 }
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002290
2291 new_key_string = kstrdup(key_string, GFP_KERNEL);
2292 if (!new_key_string)
2293 return -ENOMEM;
2294
Dmitry Baryshkov27f54112020-04-20 16:46:59 +03002295 key = request_key(type, key_desc + 1, NULL);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002296 if (IS_ERR(key)) {
2297 kzfree(new_key_string);
2298 return PTR_ERR(key);
2299 }
2300
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002301 down_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002302
Dmitry Baryshkov27f54112020-04-20 16:46:59 +03002303 ret = set_key(cc, key);
2304 if (ret < 0) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002305 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002306 key_put(key);
2307 kzfree(new_key_string);
Dmitry Baryshkov27f54112020-04-20 16:46:59 +03002308 return ret;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002309 }
2310
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002311 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002312 key_put(key);
2313
2314 /* clear the flag since following operations may invalidate previously valid key */
2315 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2316
2317 ret = crypt_setkey(cc);
2318
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002319 if (!ret) {
2320 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2321 kzfree(cc->key_string);
2322 cc->key_string = new_key_string;
2323 } else
2324 kzfree(new_key_string);
2325
2326 return ret;
2327}
2328
2329static int get_key_size(char **key_string)
2330{
2331 char *colon, dummy;
2332 int ret;
2333
2334 if (*key_string[0] != ':')
2335 return strlen(*key_string) >> 1;
2336
2337 /* look for next ':' in key string */
2338 colon = strpbrk(*key_string + 1, ":");
2339 if (!colon)
2340 return -EINVAL;
2341
2342 if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':')
2343 return -EINVAL;
2344
2345 *key_string = colon;
2346
2347 /* remaining key string should be :<logon|user>:<key_desc> */
2348
2349 return ret;
2350}
2351
2352#else
2353
2354static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2355{
2356 return -EINVAL;
2357}
2358
2359static int get_key_size(char **key_string)
2360{
2361 return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1;
2362}
2363
Dmitry Baryshkov27f54112020-04-20 16:46:59 +03002364#endif /* CONFIG_KEYS */
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002365
Milan Broze48d4bb2006-10-03 01:15:37 -07002366static int crypt_set_key(struct crypt_config *cc, char *key)
2367{
Milan Brozde8be5a2011-03-24 13:54:27 +00002368 int r = -EINVAL;
2369 int key_string_len = strlen(key);
2370
Milan Broz69a8cfc2011-01-13 19:59:49 +00002371 /* Hyphen (which gives a key_size of zero) means there is no key. */
2372 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00002373 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002374
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002375 /* ':' means the key is in kernel keyring, short-circuit normal key processing */
2376 if (key[0] == ':') {
2377 r = crypt_set_keyring_key(cc, key + 1);
2378 goto out;
2379 }
2380
Ondrej Kozina265e9092016-11-02 15:02:08 +01002381 /* clear the flag since following operations may invalidate previously valid key */
2382 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2383
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002384 /* wipe references to any kernel keyring key */
2385 kzfree(cc->key_string);
2386 cc->key_string = NULL;
2387
Andy Shevchenkoe944e032017-04-27 16:52:04 +03002388 /* Decode key from its hex representation. */
2389 if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00002390 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002391
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002392 r = crypt_setkey(cc);
Ondrej Kozina265e9092016-11-02 15:02:08 +01002393 if (!r)
2394 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Milan Brozde8be5a2011-03-24 13:54:27 +00002395
2396out:
2397 /* Hex key string not needed after here, so wipe it. */
2398 memset(key, '0', key_string_len);
2399
2400 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002401}
2402
2403static int crypt_wipe_key(struct crypt_config *cc)
2404{
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002405 int r;
2406
Milan Broze48d4bb2006-10-03 01:15:37 -07002407 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002408 get_random_bytes(&cc->key, cc->key_size);
Milan Broz4a52ffc2019-07-09 15:22:12 +02002409
2410 /* Wipe IV private keys */
2411 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
2412 r = cc->iv_gen_ops->wipe(cc);
2413 if (r)
2414 return r;
2415 }
2416
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002417 kzfree(cc->key_string);
2418 cc->key_string = NULL;
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002419 r = crypt_setkey(cc);
2420 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Andi Kleenc0297722011-01-13 19:59:53 +00002421
Ondrej Kozinac82feee2017-04-24 14:21:53 +02002422 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002423}
2424
Mikulas Patocka50593532017-08-13 22:45:08 -04002425static void crypt_calculate_pages_per_client(void)
2426{
Arun KSca79b0c2018-12-28 00:34:29 -08002427 unsigned long pages = (totalram_pages() - totalhigh_pages()) * DM_CRYPT_MEMORY_PERCENT / 100;
Mikulas Patocka50593532017-08-13 22:45:08 -04002428
2429 if (!dm_crypt_clients_n)
2430 return;
2431
2432 pages /= dm_crypt_clients_n;
2433 if (pages < DM_CRYPT_MIN_PAGES_PER_CLIENT)
2434 pages = DM_CRYPT_MIN_PAGES_PER_CLIENT;
2435 dm_crypt_pages_per_client = pages;
2436}
2437
2438static void *crypt_page_alloc(gfp_t gfp_mask, void *pool_data)
2439{
2440 struct crypt_config *cc = pool_data;
2441 struct page *page;
2442
2443 if (unlikely(percpu_counter_compare(&cc->n_allocated_pages, dm_crypt_pages_per_client) >= 0) &&
2444 likely(gfp_mask & __GFP_NORETRY))
2445 return NULL;
2446
2447 page = alloc_page(gfp_mask);
2448 if (likely(page != NULL))
2449 percpu_counter_add(&cc->n_allocated_pages, 1);
2450
2451 return page;
2452}
2453
2454static void crypt_page_free(void *page, void *pool_data)
2455{
2456 struct crypt_config *cc = pool_data;
2457
2458 __free_page(page);
2459 percpu_counter_sub(&cc->n_allocated_pages, 1);
2460}
2461
Milan Broz28513fc2010-08-12 04:14:06 +01002462static void crypt_dtr(struct dm_target *ti)
2463{
2464 struct crypt_config *cc = ti->private;
2465
2466 ti->private = NULL;
2467
2468 if (!cc)
2469 return;
2470
Rabin Vincentf659b102016-09-21 16:22:29 +02002471 if (cc->write_thread)
Mikulas Patockadc267622015-02-13 08:25:59 -05002472 kthread_stop(cc->write_thread);
2473
Milan Broz28513fc2010-08-12 04:14:06 +01002474 if (cc->io_queue)
2475 destroy_workqueue(cc->io_queue);
2476 if (cc->crypt_queue)
2477 destroy_workqueue(cc->crypt_queue);
2478
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002479 crypt_free_tfms(cc);
2480
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002481 bioset_exit(&cc->bs);
Milan Broz28513fc2010-08-12 04:14:06 +01002482
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002483 mempool_exit(&cc->page_pool);
2484 mempool_exit(&cc->req_pool);
2485 mempool_exit(&cc->tag_pool);
2486
Kent Overstreetd00a11d2018-06-02 13:45:04 -04002487 WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0);
2488 percpu_counter_destroy(&cc->n_allocated_pages);
2489
Milan Broz28513fc2010-08-12 04:14:06 +01002490 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
2491 cc->iv_gen_ops->dtr(cc);
2492
Milan Broz28513fc2010-08-12 04:14:06 +01002493 if (cc->dev)
2494 dm_put_device(ti, cc->dev);
2495
Milan Broz7dbcd132011-01-13 19:59:52 +00002496 kzfree(cc->cipher_string);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002497 kzfree(cc->key_string);
Milan Brozef43aa32017-01-04 20:23:54 +01002498 kzfree(cc->cipher_auth);
2499 kzfree(cc->authenc_key);
Milan Broz28513fc2010-08-12 04:14:06 +01002500
Mike Snitzerd5ffebd2018-01-05 21:17:20 -05002501 mutex_destroy(&cc->bio_alloc_lock);
2502
Milan Broz28513fc2010-08-12 04:14:06 +01002503 /* Must zero key material before freeing */
2504 kzfree(cc);
Mikulas Patocka50593532017-08-13 22:45:08 -04002505
2506 spin_lock(&dm_crypt_clients_lock);
2507 WARN_ON(!dm_crypt_clients_n);
2508 dm_crypt_clients_n--;
2509 crypt_calculate_pages_per_client();
2510 spin_unlock(&dm_crypt_clients_lock);
Milan Broz28513fc2010-08-12 04:14:06 +01002511}
2512
Milan Broze889f972017-03-16 15:39:39 +01002513static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
Milan Broz5ebaee62010-08-12 04:14:07 +01002515 struct crypt_config *cc = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
Milan Broz33d2f092017-03-16 15:39:40 +01002517 if (crypt_integrity_aead(cc))
Milan Broze889f972017-03-16 15:39:39 +01002518 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2519 else
2520 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Milan Broz5ebaee62010-08-12 04:14:07 +01002522 if (cc->iv_size)
2523 /* at least a 64 bit sector number should fit in our buffer */
2524 cc->iv_size = max(cc->iv_size,
2525 (unsigned int)(sizeof(u64) / sizeof(u8)));
2526 else if (ivmode) {
2527 DMWARN("Selected cipher does not support IVs");
2528 ivmode = NULL;
2529 }
2530
2531 /* Choose ivmode, see comments at iv code. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (ivmode == NULL)
2533 cc->iv_gen_ops = NULL;
2534 else if (strcmp(ivmode, "plain") == 0)
2535 cc->iv_gen_ops = &crypt_iv_plain_ops;
Milan Broz61afef62009-12-10 23:52:25 +00002536 else if (strcmp(ivmode, "plain64") == 0)
2537 cc->iv_gen_ops = &crypt_iv_plain64_ops;
Milan Broz7e3fd852017-06-06 09:07:01 +02002538 else if (strcmp(ivmode, "plain64be") == 0)
2539 cc->iv_gen_ops = &crypt_iv_plain64be_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 else if (strcmp(ivmode, "essiv") == 0)
2541 cc->iv_gen_ops = &crypt_iv_essiv_ops;
Rik Snel48527fa2006-09-03 08:56:39 +10002542 else if (strcmp(ivmode, "benbi") == 0)
2543 cc->iv_gen_ops = &crypt_iv_benbi_ops;
Ludwig Nussel46b47732007-05-09 02:32:55 -07002544 else if (strcmp(ivmode, "null") == 0)
2545 cc->iv_gen_ops = &crypt_iv_null_ops;
Milan Brozb9411d72019-07-09 15:22:14 +02002546 else if (strcmp(ivmode, "eboiv") == 0)
2547 cc->iv_gen_ops = &crypt_iv_eboiv_ops;
Milan Brozbbb16582020-01-03 09:20:22 +01002548 else if (strcmp(ivmode, "elephant") == 0) {
2549 cc->iv_gen_ops = &crypt_iv_elephant_ops;
2550 cc->key_parts = 2;
2551 cc->key_extra_size = cc->key_size / 2;
2552 if (cc->key_extra_size > ELEPHANT_MAX_KEY_SIZE)
2553 return -EINVAL;
2554 set_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags);
2555 } else if (strcmp(ivmode, "lmk") == 0) {
Milan Broz34745782011-01-13 19:59:55 +00002556 cc->iv_gen_ops = &crypt_iv_lmk_ops;
Milan Brozed04d982013-10-28 23:21:04 +01002557 /*
2558 * Version 2 and 3 is recognised according
Milan Broz34745782011-01-13 19:59:55 +00002559 * to length of provided multi-key string.
2560 * If present (version 3), last key is used as IV seed.
Milan Brozed04d982013-10-28 23:21:04 +01002561 * All keys (including IV seed) are always the same size.
Milan Broz34745782011-01-13 19:59:55 +00002562 */
Milan Brozda31a072013-10-28 23:21:03 +01002563 if (cc->key_size % cc->key_parts) {
Milan Broz34745782011-01-13 19:59:55 +00002564 cc->key_parts++;
Milan Brozda31a072013-10-28 23:21:03 +01002565 cc->key_extra_size = cc->key_size / cc->key_parts;
2566 }
Milan Brozed04d982013-10-28 23:21:04 +01002567 } else if (strcmp(ivmode, "tcw") == 0) {
2568 cc->iv_gen_ops = &crypt_iv_tcw_ops;
2569 cc->key_parts += 2; /* IV + whitening */
2570 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
Milan Broze889f972017-03-16 15:39:39 +01002571 } else if (strcmp(ivmode, "random") == 0) {
2572 cc->iv_gen_ops = &crypt_iv_random_ops;
2573 /* Need storage space in integrity fields. */
2574 cc->integrity_iv_size = cc->iv_size;
Milan Broz34745782011-01-13 19:59:55 +00002575 } else {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002576 ti->error = "Invalid IV mode";
Milan Broze889f972017-03-16 15:39:39 +01002577 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 }
2579
Milan Broze889f972017-03-16 15:39:39 +01002580 return 0;
2581}
2582
Milan Broz33d2f092017-03-16 15:39:40 +01002583/*
Milan Broz33d2f092017-03-16 15:39:40 +01002584 * Workaround to parse HMAC algorithm from AEAD crypto API spec.
2585 * The HMAC is needed to calculate tag size (HMAC digest size).
2586 * This should be probably done by crypto-api calls (once available...)
2587 */
2588static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
2589{
2590 char *start, *end, *mac_alg = NULL;
2591 struct crypto_ahash *mac;
2592
2593 if (!strstarts(cipher_api, "authenc("))
2594 return 0;
2595
2596 start = strchr(cipher_api, '(');
2597 end = strchr(cipher_api, ',');
2598 if (!start || !end || ++start > end)
2599 return -EINVAL;
2600
2601 mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
2602 if (!mac_alg)
2603 return -ENOMEM;
2604 strncpy(mac_alg, start, end - start);
2605
2606 mac = crypto_alloc_ahash(mac_alg, 0, 0);
2607 kfree(mac_alg);
2608
2609 if (IS_ERR(mac))
2610 return PTR_ERR(mac);
2611
2612 cc->key_mac_size = crypto_ahash_digestsize(mac);
2613 crypto_free_ahash(mac);
2614
2615 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
2616 if (!cc->authenc_key)
2617 return -ENOMEM;
2618
2619 return 0;
2620}
2621
2622static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key,
2623 char **ivmode, char **ivopts)
Milan Broz5ebaee62010-08-12 04:14:07 +01002624{
2625 struct crypt_config *cc = ti->private;
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002626 char *tmp, *cipher_api, buf[CRYPTO_MAX_ALG_NAME];
Milan Broz33d2f092017-03-16 15:39:40 +01002627 int ret = -EINVAL;
2628
2629 cc->tfms_count = 1;
2630
2631 /*
2632 * New format (capi: prefix)
2633 * capi:cipher_api_spec-iv:ivopts
2634 */
2635 tmp = &cipher_in[strlen("capi:")];
Milan Broz1856b9f2019-01-09 11:57:14 +01002636
2637 /* Separate IV options if present, it can contain another '-' in hash name */
2638 *ivopts = strrchr(tmp, ':');
2639 if (*ivopts) {
2640 **ivopts = '\0';
2641 (*ivopts)++;
2642 }
2643 /* Parse IV mode */
2644 *ivmode = strrchr(tmp, '-');
2645 if (*ivmode) {
2646 **ivmode = '\0';
2647 (*ivmode)++;
2648 }
2649 /* The rest is crypto API spec */
2650 cipher_api = tmp;
Milan Broz33d2f092017-03-16 15:39:40 +01002651
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002652 /* Alloc AEAD, can be used only in new format. */
2653 if (crypt_integrity_aead(cc)) {
2654 ret = crypt_ctr_auth_cipher(cc, cipher_api);
2655 if (ret < 0) {
2656 ti->error = "Invalid AEAD cipher spec";
2657 return -ENOMEM;
2658 }
2659 }
2660
Milan Broz33d2f092017-03-16 15:39:40 +01002661 if (*ivmode && !strcmp(*ivmode, "lmk"))
2662 cc->tfms_count = 64;
2663
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002664 if (*ivmode && !strcmp(*ivmode, "essiv")) {
2665 if (!*ivopts) {
2666 ti->error = "Digest algorithm missing for ESSIV mode";
2667 return -EINVAL;
2668 }
2669 ret = snprintf(buf, CRYPTO_MAX_ALG_NAME, "essiv(%s,%s)",
2670 cipher_api, *ivopts);
2671 if (ret < 0 || ret >= CRYPTO_MAX_ALG_NAME) {
2672 ti->error = "Cannot allocate cipher string";
2673 return -ENOMEM;
2674 }
2675 cipher_api = buf;
2676 }
2677
Milan Broz33d2f092017-03-16 15:39:40 +01002678 cc->key_parts = cc->tfms_count;
2679
2680 /* Allocate cipher */
2681 ret = crypt_alloc_tfms(cc, cipher_api);
2682 if (ret < 0) {
2683 ti->error = "Error allocating crypto tfm";
2684 return ret;
2685 }
2686
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002687 if (crypt_integrity_aead(cc))
Milan Broz33d2f092017-03-16 15:39:40 +01002688 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002689 else
Milan Broz33d2f092017-03-16 15:39:40 +01002690 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2691
Milan Broz33d2f092017-03-16 15:39:40 +01002692 return 0;
2693}
2694
2695static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
2696 char **ivmode, char **ivopts)
2697{
2698 struct crypt_config *cc = ti->private;
2699 char *tmp, *cipher, *chainmode, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01002700 char *cipher_api = NULL;
2701 int ret = -EINVAL;
2702 char dummy;
2703
Milan Broz33d2f092017-03-16 15:39:40 +01002704 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002705 ti->error = "Bad cipher specification";
2706 return -EINVAL;
2707 }
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 /*
Milan Broz5ebaee62010-08-12 04:14:07 +01002710 * Legacy dm-crypt cipher specification
2711 * cipher[:keycount]-mode-iv:ivopts
2712 */
2713 tmp = cipher_in;
2714 keycount = strsep(&tmp, "-");
2715 cipher = strsep(&keycount, ":");
2716
Milan Broz69a8cfc2011-01-13 19:59:49 +00002717 if (!keycount)
Milan Broz5ebaee62010-08-12 04:14:07 +01002718 cc->tfms_count = 1;
2719 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
2720 !is_power_of_2(cc->tfms_count)) {
2721 ti->error = "Bad cipher key count specification";
2722 return -EINVAL;
2723 }
Milan Broz28513fc2010-08-12 04:14:06 +01002724 cc->key_parts = cc->tfms_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Milan Brozddd42ed2008-02-08 02:11:07 +00002726 chainmode = strsep(&tmp, "-");
Milan Broz1856b9f2019-01-09 11:57:14 +01002727 *ivmode = strsep(&tmp, ":");
2728 *ivopts = tmp;
Milan Brozddd42ed2008-02-08 02:11:07 +00002729
2730 /*
2731 * For compatibility with the original dm-crypt mapping format, if
2732 * only the cipher name is supplied, use cbc-plain.
Milan Broz28513fc2010-08-12 04:14:06 +01002733 */
Milan Broz33d2f092017-03-16 15:39:40 +01002734 if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) {
Milan Brozcabf08e2007-10-19 22:38:58 +01002735 chainmode = "cbc";
Milan Broz33d2f092017-03-16 15:39:40 +01002736 *ivmode = "plain";
Milan Brozcabf08e2007-10-19 22:38:58 +01002737 }
2738
Milan Broz33d2f092017-03-16 15:39:40 +01002739 if (strcmp(chainmode, "ecb") && !*ivmode) {
Andi Kleenc0297722011-01-13 19:59:53 +00002740 ti->error = "IV mechanism required";
2741 return -EINVAL;
2742 }
2743
Milan Brozcabf08e2007-10-19 22:38:58 +01002744 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
Milan Broz9934a8b2007-10-19 22:38:57 +01002745 if (!cipher_api)
Milan Broz28513fc2010-08-12 04:14:06 +01002746 goto bad_mem;
Milan Broz9934a8b2007-10-19 22:38:57 +01002747
Ard Biesheuvela1a262b2019-08-19 17:17:37 +03002748 if (*ivmode && !strcmp(*ivmode, "essiv")) {
2749 if (!*ivopts) {
2750 ti->error = "Digest algorithm missing for ESSIV mode";
2751 kfree(cipher_api);
2752 return -EINVAL;
2753 }
2754 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
2755 "essiv(%s(%s),%s)", chainmode, cipher, *ivopts);
2756 } else {
2757 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
2758 "%s(%s)", chainmode, cipher);
2759 }
2760 if (ret < 0 || ret >= CRYPTO_MAX_ALG_NAME) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 kfree(cipher_api);
Milan Broz28513fc2010-08-12 04:14:06 +01002762 goto bad_mem;
2763 }
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 /* Allocate cipher */
2766 ret = crypt_alloc_tfms(cc, cipher_api);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 if (ret < 0) {
2768 ti->error = "Error allocating crypto tfm";
Milan Broz33d2f092017-03-16 15:39:40 +01002769 kfree(cipher_api);
2770 return ret;
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002771 }
Jeffy Chenbd86e322017-09-27 20:28:57 +08002772 kfree(cipher_api);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002773
Milan Broz33d2f092017-03-16 15:39:40 +01002774 return 0;
2775bad_mem:
2776 ti->error = "Cannot allocate cipher strings";
2777 return -ENOMEM;
2778}
2779
2780static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key)
2781{
2782 struct crypt_config *cc = ti->private;
2783 char *ivmode = NULL, *ivopts = NULL;
2784 int ret;
2785
2786 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
2787 if (!cc->cipher_string) {
2788 ti->error = "Cannot allocate cipher strings";
2789 return -ENOMEM;
2790 }
2791
2792 if (strstarts(cipher_in, "capi:"))
2793 ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts);
2794 else
2795 ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts);
2796 if (ret)
2797 return ret;
2798
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002799 /* Initialize IV */
Milan Broze889f972017-03-16 15:39:39 +01002800 ret = crypt_ctr_ivmode(ti, ivmode);
2801 if (ret < 0)
Milan Broz33d2f092017-03-16 15:39:40 +01002802 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Milan Brozda31a072013-10-28 23:21:03 +01002804 /* Initialize and set key */
2805 ret = crypt_set_key(cc, key);
2806 if (ret < 0) {
2807 ti->error = "Error decoding and setting key";
Milan Broz33d2f092017-03-16 15:39:40 +01002808 return ret;
Milan Brozda31a072013-10-28 23:21:03 +01002809 }
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 /* Allocate IV */
2812 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
2813 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
2814 if (ret < 0) {
2815 ti->error = "Error creating IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002816 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 }
2818 }
2819
2820 /* Initialize IV (set keys for ESSIV etc) */
2821 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
2822 ret = cc->iv_gen_ops->init(cc);
2823 if (ret < 0) {
2824 ti->error = "Error initialising IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002825 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
2827 }
2828
Ondrej Kozinadc949022018-01-12 16:30:32 +01002829 /* wipe the kernel key payload copy */
2830 if (cc->key_string)
2831 memset(cc->key, 0, cc->key_size * sizeof(u8));
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Milan Brozef43aa32017-01-04 20:23:54 +01002836static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
2837{
2838 struct crypt_config *cc = ti->private;
2839 struct dm_arg_set as;
Eric Biggers5916a222017-06-22 11:32:45 -07002840 static const struct dm_arg _args[] = {
Milan Broz8f0009a2017-03-16 15:39:44 +01002841 {0, 6, "Invalid number of feature args"},
Milan Brozef43aa32017-01-04 20:23:54 +01002842 };
2843 unsigned int opt_params, val;
2844 const char *opt_string, *sval;
Milan Broz8f0009a2017-03-16 15:39:44 +01002845 char dummy;
Milan Brozef43aa32017-01-04 20:23:54 +01002846 int ret;
2847
2848 /* Optional parameters */
2849 as.argc = argc;
2850 as.argv = argv;
2851
2852 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2853 if (ret)
2854 return ret;
2855
2856 while (opt_params--) {
2857 opt_string = dm_shift_arg(&as);
2858 if (!opt_string) {
2859 ti->error = "Not enough feature arguments";
2860 return -EINVAL;
2861 }
2862
2863 if (!strcasecmp(opt_string, "allow_discards"))
2864 ti->num_discard_bios = 1;
2865
2866 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
2867 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
2868
2869 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
2870 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
2871 else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
2872 if (val == 0 || val > MAX_TAG_SIZE) {
2873 ti->error = "Invalid integrity arguments";
2874 return -EINVAL;
2875 }
2876 cc->on_disk_tag_size = val;
2877 sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
2878 if (!strcasecmp(sval, "aead")) {
2879 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002880 } else if (strcasecmp(sval, "none")) {
2881 ti->error = "Unknown integrity profile";
2882 return -EINVAL;
2883 }
2884
2885 cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
2886 if (!cc->cipher_auth)
2887 return -ENOMEM;
Mikulas Patockaff3af922017-03-23 10:23:14 -04002888 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002889 if (cc->sector_size < (1 << SECTOR_SHIFT) ||
2890 cc->sector_size > 4096 ||
Mikulas Patockaff3af922017-03-23 10:23:14 -04002891 (cc->sector_size & (cc->sector_size - 1))) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002892 ti->error = "Invalid feature value for sector_size";
2893 return -EINVAL;
2894 }
Milan Broz783874b2017-09-13 15:45:56 +02002895 if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) {
2896 ti->error = "Device size is not multiple of sector_size feature";
2897 return -EINVAL;
2898 }
Mikulas Patockaff3af922017-03-23 10:23:14 -04002899 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT;
Milan Broz8f0009a2017-03-16 15:39:44 +01002900 } else if (!strcasecmp(opt_string, "iv_large_sectors"))
2901 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
2902 else {
Milan Brozef43aa32017-01-04 20:23:54 +01002903 ti->error = "Invalid feature arguments";
2904 return -EINVAL;
2905 }
2906 }
2907
2908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
2910
2911/*
2912 * Construct an encryption mapping:
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002913 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 */
2915static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2916{
2917 struct crypt_config *cc;
Michał Mirosławed0302e2018-10-09 22:13:43 +02002918 const char *devname = dm_table_device_name(ti->table);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002919 int key_size;
Milan Brozef43aa32017-01-04 20:23:54 +01002920 unsigned int align_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 unsigned long long tmpll;
2922 int ret;
Milan Brozef43aa32017-01-04 20:23:54 +01002923 size_t iv_size_padding, additional_req_size;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002924 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Milan Broz772ae5f2011-08-02 12:32:08 +01002926 if (argc < 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 ti->error = "Not enough arguments";
2928 return -EINVAL;
2929 }
2930
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002931 key_size = get_key_size(&argv[1]);
2932 if (key_size < 0) {
2933 ti->error = "Cannot parse key size";
2934 return -EINVAL;
2935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Zhengyuan Liu9c81c992019-06-12 14:14:45 +08002937 cc = kzalloc(struct_size(cc, key, key_size), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 if (!cc) {
2939 ti->error = "Cannot allocate encryption context";
2940 return -ENOMEM;
2941 }
2942 cc->key_size = key_size;
Milan Broz8f0009a2017-03-16 15:39:44 +01002943 cc->sector_size = (1 << SECTOR_SHIFT);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002944 cc->sector_shift = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
2946 ti->private = cc;
Milan Brozef43aa32017-01-04 20:23:54 +01002947
Mikulas Patocka50593532017-08-13 22:45:08 -04002948 spin_lock(&dm_crypt_clients_lock);
2949 dm_crypt_clients_n++;
2950 crypt_calculate_pages_per_client();
2951 spin_unlock(&dm_crypt_clients_lock);
2952
2953 ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL);
2954 if (ret < 0)
2955 goto bad;
2956
Milan Brozef43aa32017-01-04 20:23:54 +01002957 /* Optional parameters need to be read before cipher constructor */
2958 if (argc > 5) {
2959 ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
2960 if (ret)
2961 goto bad;
2962 }
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
2965 if (ret < 0)
2966 goto bad;
2967
Milan Broz33d2f092017-03-16 15:39:40 +01002968 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +01002969 cc->dmreq_start = sizeof(struct aead_request);
2970 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
2971 align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
2972 } else {
2973 cc->dmreq_start = sizeof(struct skcipher_request);
2974 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
2975 align_mask = crypto_skcipher_alignmask(any_tfm(cc));
2976 }
Mikulas Patockad49ec522014-08-28 11:09:31 -04002977 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
2978
Milan Brozef43aa32017-01-04 20:23:54 +01002979 if (align_mask < CRYPTO_MINALIGN) {
Mikulas Patockad49ec522014-08-28 11:09:31 -04002980 /* Allocate the padding exactly */
2981 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
Milan Brozef43aa32017-01-04 20:23:54 +01002982 & align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002983 } else {
2984 /*
2985 * If the cipher requires greater alignment than kmalloc
2986 * alignment, we don't know the exact position of the
2987 * initialization vector. We must assume worst case.
2988 */
Milan Brozef43aa32017-01-04 20:23:54 +01002989 iv_size_padding = align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Milan Brozef43aa32017-01-04 20:23:54 +01002992 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
2993 additional_req_size = sizeof(struct dm_crypt_request) +
2994 iv_size_padding + cc->iv_size +
2995 cc->iv_size +
2996 sizeof(uint64_t) +
2997 sizeof(unsigned int);
2998
Kent Overstreet6f1c8192018-05-20 18:25:53 -04002999 ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size);
3000 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 ti->error = "Cannot allocate crypt request mempool";
3002 goto bad;
3003 }
3004
Mike Snitzer30187e12016-01-31 13:28:26 -05003005 cc->per_bio_data_size = ti->per_io_data_size =
Milan Brozef43aa32017-01-04 20:23:54 +01003006 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
Mikulas Patockad49ec522014-08-28 11:09:31 -04003007 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04003008
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003009 ret = mempool_init(&cc->page_pool, BIO_MAX_PAGES, crypt_page_alloc, crypt_page_free, cc);
3010 if (ret) {
Milan Broz8b004452006-10-03 01:15:37 -07003011 ti->error = "Cannot allocate page mempool";
Milan Broze48d4bb2006-10-03 01:15:37 -07003012 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 }
Milan Broze48d4bb2006-10-03 01:15:37 -07003014
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003015 ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS);
3016 if (ret) {
Milan Broz0c395b02008-02-08 02:10:54 +00003017 ti->error = "Cannot allocate crypt bioset";
Milan Brozcabf08e2007-10-19 22:38:58 +01003018 goto bad;
Milan Broz93e605c2006-10-03 01:15:38 -07003019 }
Milan Brozcabf08e2007-10-19 22:38:58 +01003020
Mikulas Patocka7145c242015-02-13 08:24:41 -05003021 mutex_init(&cc->bio_alloc_lock);
3022
Milan Brozcabf08e2007-10-19 22:38:58 +01003023 ret = -EINVAL;
Milan Broz8f0009a2017-03-16 15:39:44 +01003024 if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
3025 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
Milan Brozcabf08e2007-10-19 22:38:58 +01003026 ti->error = "Invalid iv_offset sector";
3027 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08003029 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Vivek Goyale80d1c82015-07-31 09:20:36 -04003031 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
3032 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 ti->error = "Device lookup failed";
3034 goto bad;
3035 }
3036
Vivek Goyale80d1c82015-07-31 09:20:36 -04003037 ret = -EINVAL;
Milan Brozef87bfc2018-11-07 22:24:55 +01003038 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 ti->error = "Invalid device sector";
3040 goto bad;
3041 }
3042 cc->start = tmpll;
3043
Milan Broz33d2f092017-03-16 15:39:40 +01003044 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
Milan Brozef43aa32017-01-04 20:23:54 +01003045 ret = crypt_integrity_ctr(cc, ti);
Milan Broz772ae5f2011-08-02 12:32:08 +01003046 if (ret)
3047 goto bad;
3048
Milan Brozef43aa32017-01-04 20:23:54 +01003049 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
3050 if (!cc->tag_pool_max_sectors)
3051 cc->tag_pool_max_sectors = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01003052
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003053 ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS,
Milan Brozef43aa32017-01-04 20:23:54 +01003054 cc->tag_pool_max_sectors * cc->on_disk_tag_size);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003055 if (ret) {
Milan Brozef43aa32017-01-04 20:23:54 +01003056 ti->error = "Cannot allocate integrity tags mempool";
3057 goto bad;
Milan Broz772ae5f2011-08-02 12:32:08 +01003058 }
Mikulas Patocka583fe742017-04-18 16:51:54 -04003059
3060 cc->tag_pool_max_sectors <<= cc->sector_shift;
Milan Broz772ae5f2011-08-02 12:32:08 +01003061 }
3062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 ret = -ENOMEM;
Mike Snitzerf612b212019-11-20 17:27:39 -05003064 cc->io_queue = alloc_workqueue("kcryptd_io/%s", WQ_MEM_RECLAIM, 1, devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 if (!cc->io_queue) {
3066 ti->error = "Couldn't create kcryptd io queue";
3067 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
Christophe Saout37af6562006-10-30 20:39:08 +01003069
Mikulas Patockaf3396c582015-02-13 08:23:09 -05003070 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
Mike Snitzerf612b212019-11-20 17:27:39 -05003071 cc->crypt_queue = alloc_workqueue("kcryptd/%s", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM,
Michał Mirosławed0302e2018-10-09 22:13:43 +02003072 1, devname);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05003073 else
Michał Mirosławed0302e2018-10-09 22:13:43 +02003074 cc->crypt_queue = alloc_workqueue("kcryptd/%s",
Mike Snitzerf612b212019-11-20 17:27:39 -05003075 WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
Michał Mirosławed0302e2018-10-09 22:13:43 +02003076 num_online_cpus(), devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 if (!cc->crypt_queue) {
3078 ti->error = "Couldn't create kcryptd queue";
3079 goto bad;
3080 }
3081
Mikulas Patockac7329ef2018-07-11 12:10:51 -04003082 spin_lock_init(&cc->write_thread_lock);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05003083 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05003084
Michał Mirosławed0302e2018-10-09 22:13:43 +02003085 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write/%s", devname);
Mikulas Patockadc267622015-02-13 08:25:59 -05003086 if (IS_ERR(cc->write_thread)) {
3087 ret = PTR_ERR(cc->write_thread);
3088 cc->write_thread = NULL;
3089 ti->error = "Couldn't spawn write thread";
3090 goto bad;
3091 }
3092 wake_up_process(cc->write_thread);
3093
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00003094 ti->num_flush_bios = 1;
Milan Broz983c7db2011-09-25 23:26:21 +01003095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return 0;
3097
3098bad:
3099 crypt_dtr(ti);
3100 return ret;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01003101}
3102
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00003103static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
3105 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01003106 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01003107
Milan Broz772ae5f2011-08-02 12:32:08 +01003108 /*
Mike Christie28a8f0d2016-06-05 14:32:25 -05003109 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
3110 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
Mike Christiee6047142016-06-05 14:32:04 -05003111 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
Milan Broz772ae5f2011-08-02 12:32:08 +01003112 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06003113 if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
Mike Christie28a8f0d2016-06-05 14:32:25 -05003114 bio_op(bio) == REQ_OP_DISCARD)) {
Christoph Hellwig74d46992017-08-23 19:10:32 +02003115 bio_set_dev(bio, cc->dev->bdev);
Milan Broz772ae5f2011-08-02 12:32:08 +01003116 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07003117 bio->bi_iter.bi_sector = cc->start +
3118 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01003119 return DM_MAPIO_REMAPPED;
3120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
Mikulas Patocka4e870e92016-08-30 16:38:42 -04003122 /*
3123 * Check if bio is too large, split as needed.
3124 */
3125 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
Milan Brozef43aa32017-01-04 20:23:54 +01003126 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
Mikulas Patocka4e870e92016-08-30 16:38:42 -04003127 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
3128
Milan Broz8f0009a2017-03-16 15:39:44 +01003129 /*
3130 * Ensure that bio is a multiple of internal sector encryption size
3131 * and is aligned to this size as defined in IO hints.
3132 */
3133 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
Christoph Hellwig846785e2017-06-03 09:38:02 +02003134 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01003135
3136 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
Christoph Hellwig846785e2017-06-03 09:38:02 +02003137 return DM_MAPIO_KILL;
Milan Broz8f0009a2017-03-16 15:39:44 +01003138
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04003139 io = dm_per_bio_data(bio, cc->per_bio_data_size);
3140 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Milan Brozef43aa32017-01-04 20:23:54 +01003141
3142 if (cc->on_disk_tag_size) {
Mikulas Patocka583fe742017-04-18 16:51:54 -04003143 unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift);
Milan Brozef43aa32017-01-04 20:23:54 +01003144
3145 if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
Mikulas Patocka583fe742017-04-18 16:51:54 -04003146 unlikely(!(io->integrity_metadata = kmalloc(tag_len,
Milan Brozef43aa32017-01-04 20:23:54 +01003147 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
3148 if (bio_sectors(bio) > cc->tag_pool_max_sectors)
3149 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
Kent Overstreet6f1c8192018-05-20 18:25:53 -04003150 io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO);
Milan Brozef43aa32017-01-04 20:23:54 +01003151 io->integrity_metadata_from_pool = true;
3152 }
3153 }
3154
Milan Broz33d2f092017-03-16 15:39:40 +01003155 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01003156 io->ctx.r.req_aead = (struct aead_request *)(io + 1);
3157 else
3158 io->ctx.r.req = (struct skcipher_request *)(io + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Milan Broz20c82532011-01-13 19:59:53 +00003160 if (bio_data_dir(io->base_bio) == READ) {
3161 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05003162 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00003163 } else
Andrew Morton4ee218c2006-03-27 01:17:48 -08003164 kcryptd_queue_crypt(io);
3165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return DM_MAPIO_SUBMITTED;
3167}
3168
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003169static void crypt_status(struct dm_target *ti, status_type_t type,
3170 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
Milan Broz5ebaee62010-08-12 04:14:07 +01003172 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003173 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05003174 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
3176 switch (type) {
3177 case STATUSTYPE_INFO:
3178 result[0] = '\0';
3179 break;
3180
3181 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00003182 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01003184 if (cc->key_size > 0) {
3185 if (cc->key_string)
3186 DMEMIT(":%u:%s", cc->key_size, cc->key_string);
3187 else
3188 for (i = 0; i < cc->key_size; i++)
3189 DMEMIT("%02x", cc->key[i]);
3190 } else
Mikulas Patockafd7c0922013-03-01 22:45:44 +00003191 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
3193 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
3194 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01003195
Mikulas Patockaf3396c582015-02-13 08:23:09 -05003196 num_feature_args += !!ti->num_discard_bios;
3197 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05003198 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Mikulas Patockaff3af922017-03-23 10:23:14 -04003199 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT);
Milan Broz8f0009a2017-03-16 15:39:44 +01003200 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01003201 if (cc->on_disk_tag_size)
3202 num_feature_args++;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05003203 if (num_feature_args) {
3204 DMEMIT(" %d", num_feature_args);
3205 if (ti->num_discard_bios)
3206 DMEMIT(" allow_discards");
3207 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
3208 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05003209 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
3210 DMEMIT(" submit_from_crypt_cpus");
Milan Brozef43aa32017-01-04 20:23:54 +01003211 if (cc->on_disk_tag_size)
3212 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
Milan Broz8f0009a2017-03-16 15:39:44 +01003213 if (cc->sector_size != (1 << SECTOR_SHIFT))
3214 DMEMIT(" sector_size:%d", cc->sector_size);
3215 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
3216 DMEMIT(" iv_large_sectors");
Mikulas Patockaf3396c582015-02-13 08:23:09 -05003217 }
Milan Broz772ae5f2011-08-02 12:32:08 +01003218
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 break;
3220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Milan Broze48d4bb2006-10-03 01:15:37 -07003223static void crypt_postsuspend(struct dm_target *ti)
3224{
3225 struct crypt_config *cc = ti->private;
3226
3227 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
3228}
3229
3230static int crypt_preresume(struct dm_target *ti)
3231{
3232 struct crypt_config *cc = ti->private;
3233
3234 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
3235 DMERR("aborting resume - crypt key is not set.");
3236 return -EAGAIN;
3237 }
3238
3239 return 0;
3240}
3241
3242static void crypt_resume(struct dm_target *ti)
3243{
3244 struct crypt_config *cc = ti->private;
3245
3246 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
3247}
3248
3249/* Message interface
3250 * key set <key>
3251 * key wipe
3252 */
Mike Snitzer1eb5fa82018-02-28 15:59:59 -05003253static int crypt_message(struct dm_target *ti, unsigned argc, char **argv,
3254 char *result, unsigned maxlen)
Milan Broze48d4bb2006-10-03 01:15:37 -07003255{
3256 struct crypt_config *cc = ti->private;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01003257 int key_size, ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07003258
3259 if (argc < 2)
3260 goto error;
3261
Mike Snitzer498f0102011-08-02 12:32:04 +01003262 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07003263 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
3264 DMWARN("not suspended during key manipulation.");
3265 return -EINVAL;
3266 }
Mike Snitzer498f0102011-08-02 12:32:04 +01003267 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01003268 /* The key size may not be changed. */
3269 key_size = get_key_size(&argv[2]);
3270 if (key_size < 0 || cc->key_size != key_size) {
3271 memset(argv[2], '0', strlen(argv[2]));
3272 return -EINVAL;
3273 }
3274
Milan Broz542da312009-12-10 23:51:57 +00003275 ret = crypt_set_key(cc, argv[2]);
3276 if (ret)
3277 return ret;
3278 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
3279 ret = cc->iv_gen_ops->init(cc);
Ondrej Kozinadc949022018-01-12 16:30:32 +01003280 /* wipe the kernel key payload copy */
3281 if (cc->key_string)
3282 memset(cc->key, 0, cc->key_size * sizeof(u8));
Milan Broz542da312009-12-10 23:51:57 +00003283 return ret;
3284 }
Milan Broz4a52ffc2019-07-09 15:22:12 +02003285 if (argc == 2 && !strcasecmp(argv[1], "wipe"))
Milan Broze48d4bb2006-10-03 01:15:37 -07003286 return crypt_wipe_key(cc);
3287 }
3288
3289error:
3290 DMWARN("unrecognised message received.");
3291 return -EINVAL;
3292}
3293
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003294static int crypt_iterate_devices(struct dm_target *ti,
3295 iterate_devices_callout_fn fn, void *data)
3296{
3297 struct crypt_config *cc = ti->private;
3298
Mike Snitzer5dea2712009-07-23 20:30:42 +01003299 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003300}
3301
Mike Snitzer586b2862015-09-09 21:34:51 -04003302static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
3303{
Milan Broz8f0009a2017-03-16 15:39:44 +01003304 struct crypt_config *cc = ti->private;
3305
Mike Snitzer586b2862015-09-09 21:34:51 -04003306 /*
3307 * Unfortunate constraint that is required to avoid the potential
3308 * for exceeding underlying device's max_segments limits -- due to
3309 * crypt_alloc_buffer() possibly allocating pages for the encryption
3310 * bio that are not as physically contiguous as the original bio.
3311 */
3312 limits->max_segment_size = PAGE_SIZE;
Milan Broz8f0009a2017-03-16 15:39:44 +01003313
Mikulas Patockabc9e9cf2018-08-10 11:23:56 -04003314 limits->logical_block_size =
Eric Biggers64611a152020-06-04 12:01:26 -07003315 max_t(unsigned, limits->logical_block_size, cc->sector_size);
Mikulas Patockabc9e9cf2018-08-10 11:23:56 -04003316 limits->physical_block_size =
3317 max_t(unsigned, limits->physical_block_size, cc->sector_size);
3318 limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size);
Mike Snitzer586b2862015-09-09 21:34:51 -04003319}
3320
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321static struct target_type crypt_target = {
3322 .name = "crypt",
Dmitry Baryshkov27f54112020-04-20 16:46:59 +03003323 .version = {1, 21, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 .module = THIS_MODULE,
3325 .ctr = crypt_ctr,
3326 .dtr = crypt_dtr,
3327 .map = crypt_map,
3328 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07003329 .postsuspend = crypt_postsuspend,
3330 .preresume = crypt_preresume,
3331 .resume = crypt_resume,
3332 .message = crypt_message,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003333 .iterate_devices = crypt_iterate_devices,
Mike Snitzer586b2862015-09-09 21:34:51 -04003334 .io_hints = crypt_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335};
3336
3337static int __init dm_crypt_init(void)
3338{
3339 int r;
3340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05003342 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07003343 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 return r;
3346}
3347
3348static void __exit dm_crypt_exit(void)
3349{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00003350 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351}
3352
3353module_init(dm_crypt_init);
3354module_exit(dm_crypt_exit);
3355
Jana Saoutbf142992014-06-24 14:27:04 -04003356MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
3358MODULE_LICENSE("GPL");