blob: 8bff6f7a4c6cb32291d0e541c6a639239f12c837 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jana Saoutbf142992014-06-24 14:27:04 -04002 * Copyright (C) 2003 Jana Saout <jana@saout.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org>
Milan Brozef43aa32017-01-04 20:23:54 +01004 * Copyright (C) 2006-2017 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2013-2017 Milan Broz <gmazyland@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This file is released under the GPL.
8 */
9
Milan Broz43d69032008-02-08 02:11:09 +000010#include <linux/completion.h>
Herbert Xud1806f62006-08-22 20:29:17 +100011#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
Ondrej Kozinac538f6e2016-11-21 15:58:51 +010015#include <linux/key.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/bio.h>
17#include <linux/blkdev.h>
18#include <linux/mempool.h>
19#include <linux/slab.h>
20#include <linux/crypto.h>
21#include <linux/workqueue.h>
Mikulas Patockadc267622015-02-13 08:25:59 -050022#include <linux/kthread.h>
Andrew Morton3fcfab12006-10-19 23:28:16 -070023#include <linux/backing-dev.h>
Arun Sharma600634972011-07-26 16:09:06 -070024#include <linux/atomic.h>
David Hardeman378f0582005-09-17 17:55:31 +100025#include <linux/scatterlist.h>
Mikulas Patockab3c5fd32015-02-13 08:27:41 -050026#include <linux/rbtree.h>
Ondrej Kozina027c4312016-12-01 18:20:52 +010027#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/page.h>
Rik Snel48527fa2006-09-03 08:56:39 +100029#include <asm/unaligned.h>
Milan Broz34745782011-01-13 19:59:55 +000030#include <crypto/hash.h>
31#include <crypto/md5.h>
32#include <crypto/algapi.h>
Herbert Xubbdb23b2016-01-24 21:16:36 +080033#include <crypto/skcipher.h>
Milan Brozef43aa32017-01-04 20:23:54 +010034#include <crypto/aead.h>
35#include <crypto/authenc.h>
36#include <linux/rtnetlink.h> /* for struct rtattr and RTA macros only */
Ondrej Kozinac538f6e2016-11-21 15:58:51 +010037#include <keys/user-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Mikulas Patocka586e80e2008-10-21 17:44:59 +010039#include <linux/device-mapper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Alasdair G Kergon72d94862006-06-26 00:27:35 -070041#define DM_MSG_PREFIX "crypt"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * context holding the current state of a multi-part conversion
45 */
46struct convert_context {
Milan Broz43d69032008-02-08 02:11:09 +000047 struct completion restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct bio *bio_in;
49 struct bio *bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -070050 struct bvec_iter iter_in;
51 struct bvec_iter iter_out;
Mikulas Patockac66029f2012-07-27 15:08:05 +010052 sector_t cc_sector;
Mikulas Patocka40b62292012-07-27 15:08:04 +010053 atomic_t cc_pending;
Milan Brozef43aa32017-01-04 20:23:54 +010054 union {
55 struct skcipher_request *req;
56 struct aead_request *req_aead;
57 } r;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Milan Broz53017032008-02-08 02:10:38 +000061/*
62 * per bio private data
63 */
64struct dm_crypt_io {
Alasdair G Kergon49a8a922012-07-27 15:08:05 +010065 struct crypt_config *cc;
Milan Broz53017032008-02-08 02:10:38 +000066 struct bio *base_bio;
Milan Brozef43aa32017-01-04 20:23:54 +010067 u8 *integrity_metadata;
68 bool integrity_metadata_from_pool;
Milan Broz53017032008-02-08 02:10:38 +000069 struct work_struct work;
70
71 struct convert_context ctx;
72
Mikulas Patocka40b62292012-07-27 15:08:04 +010073 atomic_t io_pending;
Milan Broz53017032008-02-08 02:10:38 +000074 int error;
Milan Broz0c395b02008-02-08 02:10:54 +000075 sector_t sector;
Mikulas Patockadc267622015-02-13 08:25:59 -050076
Mikulas Patockab3c5fd32015-02-13 08:27:41 -050077 struct rb_node rb_node;
Mikulas Patocka298a9fa2014-03-28 15:51:55 -040078} CRYPTO_MINALIGN_ATTR;
Milan Broz53017032008-02-08 02:10:38 +000079
Milan Broz01482b72008-02-08 02:11:04 +000080struct dm_crypt_request {
Huang Yingb2174ee2009-03-16 17:44:33 +000081 struct convert_context *ctx;
Milan Brozef43aa32017-01-04 20:23:54 +010082 struct scatterlist sg_in[4];
83 struct scatterlist sg_out[4];
Milan Broz2dc5327d32011-01-13 19:59:54 +000084 sector_t iv_sector;
Milan Broz01482b72008-02-08 02:11:04 +000085};
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087struct crypt_config;
88
89struct crypt_iv_operations {
90 int (*ctr)(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +010091 const char *opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 void (*dtr)(struct crypt_config *cc);
Milan Brozb95bf2d2009-12-10 23:51:56 +000093 int (*init)(struct crypt_config *cc);
Milan Broz542da312009-12-10 23:51:57 +000094 int (*wipe)(struct crypt_config *cc);
Milan Broz2dc5327d32011-01-13 19:59:54 +000095 int (*generator)(struct crypt_config *cc, u8 *iv,
96 struct dm_crypt_request *dmreq);
97 int (*post)(struct crypt_config *cc, u8 *iv,
98 struct dm_crypt_request *dmreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
Milan Broz60473592009-12-10 23:51:55 +0000101struct iv_essiv_private {
Herbert Xubbdb23b2016-01-24 21:16:36 +0800102 struct crypto_ahash *hash_tfm;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000103 u8 *salt;
Milan Broz60473592009-12-10 23:51:55 +0000104};
105
106struct iv_benbi_private {
107 int shift;
108};
109
Milan Broz34745782011-01-13 19:59:55 +0000110#define LMK_SEED_SIZE 64 /* hash + 0 */
111struct iv_lmk_private {
112 struct crypto_shash *hash_tfm;
113 u8 *seed;
114};
115
Milan Brozed04d982013-10-28 23:21:04 +0100116#define TCW_WHITENING_SIZE 16
117struct iv_tcw_private {
118 struct crypto_shash *crc32_tfm;
119 u8 *iv_seed;
120 u8 *whitening;
121};
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/*
124 * Crypt: maps a linear range of a block device
125 * and encrypts / decrypts at the same time.
126 */
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -0500127enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID,
Rabin Vincentf659b102016-09-21 16:22:29 +0200128 DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD };
Andi Kleenc0297722011-01-13 19:59:53 +0000129
Milan Brozef43aa32017-01-04 20:23:54 +0100130enum cipher_flags {
131 CRYPT_MODE_INTEGRITY_AEAD, /* Use authenticated mode for cihper */
Milan Broz8f0009a2017-03-16 15:39:44 +0100132 CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */
Milan Brozef43aa32017-01-04 20:23:54 +0100133};
134
Andi Kleenc0297722011-01-13 19:59:53 +0000135/*
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500136 * The fields in here must be read only after initialization.
Andi Kleenc0297722011-01-13 19:59:53 +0000137 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138struct crypt_config {
139 struct dm_dev *dev;
140 sector_t start;
141
142 /*
Milan Brozef43aa32017-01-04 20:23:54 +0100143 * pool for per bio private data, crypto requests,
144 * encryption requeusts/buffer pages and integrity tags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 */
Milan Brozddd42ed2008-02-08 02:11:07 +0000146 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 mempool_t *page_pool;
Milan Brozef43aa32017-01-04 20:23:54 +0100148 mempool_t *tag_pool;
149 unsigned tag_pool_max_sectors;
150
Milan Broz6a24c712006-10-03 01:15:40 -0700151 struct bio_set *bs;
Mikulas Patocka7145c242015-02-13 08:24:41 -0500152 struct mutex bio_alloc_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Milan Brozcabf08e2007-10-19 22:38:58 +0100154 struct workqueue_struct *io_queue;
155 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700156
Mikulas Patockadc267622015-02-13 08:25:59 -0500157 struct task_struct *write_thread;
158 wait_queue_head_t write_thread_wait;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -0500159 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -0500160
Milan Broz5ebaee62010-08-12 04:14:07 +0100161 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000162 char *cipher_string;
Milan Brozef43aa32017-01-04 20:23:54 +0100163 char *cipher_auth;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +0100164 char *key_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100165
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100166 const struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800167 union {
Milan Broz60473592009-12-10 23:51:55 +0000168 struct iv_essiv_private essiv;
169 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000170 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100171 struct iv_tcw_private tcw;
Herbert Xu79066ad2006-12-05 13:41:52 -0800172 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 sector_t iv_offset;
174 unsigned int iv_size;
Mikulas Patockaff3af922017-03-23 10:23:14 -0400175 unsigned short int sector_size;
176 unsigned char sector_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100178 /* ESSIV: struct crypto_cipher *essiv_tfm */
179 void *iv_private;
Milan Brozef43aa32017-01-04 20:23:54 +0100180 union {
181 struct crypto_skcipher **tfms;
182 struct crypto_aead **tfms_aead;
183 } cipher_tfm;
Milan Brozd1f96422011-01-13 19:59:54 +0000184 unsigned tfms_count;
Milan Brozef43aa32017-01-04 20:23:54 +0100185 unsigned long cipher_flags;
Andi Kleenc0297722011-01-13 19:59:53 +0000186
187 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000188 * Layout of each crypto request:
189 *
Herbert Xubbdb23b2016-01-24 21:16:36 +0800190 * struct skcipher_request
Milan Brozddd42ed2008-02-08 02:11:07 +0000191 * context
192 * padding
193 * struct dm_crypt_request
194 * padding
195 * IV
196 *
197 * The padding is added so that dm_crypt_request and the IV are
198 * correctly aligned.
199 */
200 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000201
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400202 unsigned int per_bio_data_size;
203
Milan Broze48d4bb2006-10-03 01:15:37 -0700204 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100206 unsigned int key_parts; /* independent parts in key buffer */
207 unsigned int key_extra_size; /* additional keys length */
Milan Brozef43aa32017-01-04 20:23:54 +0100208 unsigned int key_mac_size; /* MAC key size for authenc(...) */
209
210 unsigned int integrity_tag_size;
211 unsigned int integrity_iv_size;
212 unsigned int on_disk_tag_size;
213
214 u8 *authenc_key; /* space for keys in authenc() format (if used) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 u8 key[0];
216};
217
Milan Brozef43aa32017-01-04 20:23:54 +0100218#define MIN_IOS 64
219#define MAX_TAG_SIZE 480
220#define POOL_ENTRY_SIZE 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100222static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000223static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Brozef43aa32017-01-04 20:23:54 +0100224static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
225 struct scatterlist *sg);
Olaf Kirch027581f2007-05-09 02:32:52 -0700226
Andi Kleenc0297722011-01-13 19:59:53 +0000227/*
228 * Use this to access cipher attributes that are the same for each CPU.
229 */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800230static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +0000231{
Milan Brozef43aa32017-01-04 20:23:54 +0100232 return cc->cipher_tfm.tfms[0];
233}
234
235static struct crypto_aead *any_tfm_aead(struct crypt_config *cc)
236{
237 return cc->cipher_tfm.tfms_aead[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 * Different IV generation algorithms:
242 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000243 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200244 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 *
Milan Broz61afef62009-12-10 23:52:25 +0000246 * plain64: the initial vector is the 64-bit little-endian version of the sector
247 * number, padded with zeros if necessary.
248 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000249 * essiv: "encrypted sector|salt initial vector", the sector number is
250 * encrypted with the bulk cipher using a salt as key. The salt
251 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 *
Rik Snel48527fa2006-09-03 08:56:39 +1000253 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
254 * (needed for LRW-32-AES and possible other narrow block modes)
255 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700256 * null: the initial vector is always zero. Provides compatibility with
257 * obsolete loop_fish2 devices. Do not use for new devices.
258 *
Milan Broz34745782011-01-13 19:59:55 +0000259 * lmk: Compatible implementation of the block chaining mode used
260 * by the Loop-AES block device encryption system
261 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
262 * It operates on full 512 byte sectors and uses CBC
263 * with an IV derived from the sector number, the data and
264 * optionally extra IV seed.
265 * This means that after decryption the first block
266 * of sector must be tweaked according to decrypted data.
267 * Loop-AES can use three encryption schemes:
268 * version 1: is plain aes-cbc mode
269 * version 2: uses 64 multikey scheme with lmk IV generator
270 * version 3: the same as version 2 with additional IV seed
271 * (it uses 65 keys, last key is used as IV seed)
272 *
Milan Brozed04d982013-10-28 23:21:04 +0100273 * tcw: Compatible implementation of the block chaining mode used
274 * by the TrueCrypt device encryption system (prior to version 4.1).
Milan Broze44f23b2015-04-05 18:03:10 +0200275 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
Milan Brozed04d982013-10-28 23:21:04 +0100276 * It operates on full 512 byte sectors and uses CBC
277 * with an IV derived from initial key and the sector number.
278 * In addition, whitening value is applied on every sector, whitening
279 * is calculated from initial key, sector number and mixed using CRC32.
280 * Note that this encryption scheme is vulnerable to watermarking attacks
281 * and should be used for old compatible containers access only.
282 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 * plumb: unimplemented, see:
284 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
285 */
286
Milan Broz2dc5327d32011-01-13 19:59:54 +0000287static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
288 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100291 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 return 0;
294}
295
Milan Broz61afef62009-12-10 23:52:25 +0000296static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc5327d32011-01-13 19:59:54 +0000297 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000298{
299 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100300 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000301
302 return 0;
303}
304
Milan Brozb95bf2d2009-12-10 23:51:56 +0000305/* Initialise ESSIV - compute salt but no local memory allocations */
306static int crypt_iv_essiv_init(struct crypt_config *cc)
307{
308 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800309 AHASH_REQUEST_ON_STACK(req, essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000310 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000311 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100312 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000313
314 sg_init_one(&sg, cc->key, cc->key_size);
Herbert Xubbdb23b2016-01-24 21:16:36 +0800315 ahash_request_set_tfm(req, essiv->hash_tfm);
316 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
317 ahash_request_set_crypt(req, &sg, essiv->salt, cc->key_size);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000318
Herbert Xubbdb23b2016-01-24 21:16:36 +0800319 err = crypto_ahash_digest(req);
320 ahash_request_zero(req);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000321 if (err)
322 return err;
323
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100324 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000325
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100326 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800327 crypto_ahash_digestsize(essiv->hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100328 if (err)
329 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000330
331 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000332}
333
Milan Broz542da312009-12-10 23:51:57 +0000334/* Wipe salt and reset key derived from volume key */
335static int crypt_iv_essiv_wipe(struct crypt_config *cc)
336{
337 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800338 unsigned salt_size = crypto_ahash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000339 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100340 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000341
342 memset(essiv->salt, 0, salt_size);
343
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100344 essiv_tfm = cc->iv_private;
345 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
346 if (r)
347 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000348
349 return err;
350}
351
352/* Set up per cpu cipher state */
353static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
354 struct dm_target *ti,
355 u8 *salt, unsigned saltsize)
356{
357 struct crypto_cipher *essiv_tfm;
358 int err;
359
360 /* Setup the essiv_tfm with the given salt */
361 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
362 if (IS_ERR(essiv_tfm)) {
363 ti->error = "Error allocating crypto tfm for ESSIV";
364 return essiv_tfm;
365 }
366
Milan Brozef43aa32017-01-04 20:23:54 +0100367 if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) {
Andi Kleenc0297722011-01-13 19:59:53 +0000368 ti->error = "Block size of ESSIV cipher does "
369 "not match IV size of block cipher";
370 crypto_free_cipher(essiv_tfm);
371 return ERR_PTR(-EINVAL);
372 }
373
374 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
375 if (err) {
376 ti->error = "Failed to set key for ESSIV cipher";
377 crypto_free_cipher(essiv_tfm);
378 return ERR_PTR(err);
379 }
380
381 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000382}
383
Milan Broz60473592009-12-10 23:51:55 +0000384static void crypt_iv_essiv_dtr(struct crypt_config *cc)
385{
Andi Kleenc0297722011-01-13 19:59:53 +0000386 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000387 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
388
Herbert Xubbdb23b2016-01-24 21:16:36 +0800389 crypto_free_ahash(essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000390 essiv->hash_tfm = NULL;
391
392 kzfree(essiv->salt);
393 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000394
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100395 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000396
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100397 if (essiv_tfm)
398 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000399
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100400 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000401}
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100404 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Milan Broz5861f1b2009-12-10 23:51:56 +0000406 struct crypto_cipher *essiv_tfm = NULL;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800407 struct crypto_ahash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000408 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100409 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Milan Broz5861f1b2009-12-10 23:51:56 +0000411 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700412 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return -EINVAL;
414 }
415
Milan Brozb95bf2d2009-12-10 23:51:56 +0000416 /* Allocate hash algorithm */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800417 hash_tfm = crypto_alloc_ahash(opts, 0, CRYPTO_ALG_ASYNC);
Herbert Xu35058682006-08-24 19:10:20 +1000418 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700419 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000420 err = PTR_ERR(hash_tfm);
421 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
Herbert Xubbdb23b2016-01-24 21:16:36 +0800424 salt = kzalloc(crypto_ahash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000425 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700426 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000427 err = -ENOMEM;
428 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
Milan Brozb95bf2d2009-12-10 23:51:56 +0000431 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000432 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
433
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100434 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800435 crypto_ahash_digestsize(hash_tfm));
Milan Brozef43aa32017-01-04 20:23:54 +0100436
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100437 if (IS_ERR(essiv_tfm)) {
438 crypt_iv_essiv_dtr(cc);
439 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000440 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100441 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000444
445bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000446 if (hash_tfm && !IS_ERR(hash_tfm))
Herbert Xubbdb23b2016-01-24 21:16:36 +0800447 crypto_free_ahash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000448 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000449 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Milan Broz2dc5327d32011-01-13 19:59:54 +0000452static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
453 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100455 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100458 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000459 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 0;
462}
463
Rik Snel48527fa2006-09-03 08:56:39 +1000464static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
465 const char *opts)
466{
Herbert Xubbdb23b2016-01-24 21:16:36 +0800467 unsigned bs = crypto_skcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800468 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000469
470 /* we need to calculate how far we must shift the sector count
471 * to get the cipher block count, we use this shift in _gen */
472
473 if (1 << log != bs) {
474 ti->error = "cypher blocksize is not a power of 2";
475 return -EINVAL;
476 }
477
478 if (log > 9) {
479 ti->error = "cypher blocksize is > 512";
480 return -EINVAL;
481 }
482
Milan Broz60473592009-12-10 23:51:55 +0000483 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000484
485 return 0;
486}
487
488static void crypt_iv_benbi_dtr(struct crypt_config *cc)
489{
Rik Snel48527fa2006-09-03 08:56:39 +1000490}
491
Milan Broz2dc5327d32011-01-13 19:59:54 +0000492static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
493 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000494{
Herbert Xu79066ad2006-12-05 13:41:52 -0800495 __be64 val;
496
Rik Snel48527fa2006-09-03 08:56:39 +1000497 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800498
Milan Broz2dc5327d32011-01-13 19:59:54 +0000499 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800500 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return 0;
503}
504
Milan Broz2dc5327d32011-01-13 19:59:54 +0000505static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
506 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700507{
508 memset(iv, 0, cc->iv_size);
509
510 return 0;
511}
512
Milan Broz34745782011-01-13 19:59:55 +0000513static void crypt_iv_lmk_dtr(struct crypt_config *cc)
514{
515 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
516
517 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
518 crypto_free_shash(lmk->hash_tfm);
519 lmk->hash_tfm = NULL;
520
521 kzfree(lmk->seed);
522 lmk->seed = NULL;
523}
524
525static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
526 const char *opts)
527{
528 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
529
Milan Broz8f0009a2017-03-16 15:39:44 +0100530 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
531 ti->error = "Unsupported sector size for LMK";
532 return -EINVAL;
533 }
534
Milan Broz34745782011-01-13 19:59:55 +0000535 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
536 if (IS_ERR(lmk->hash_tfm)) {
537 ti->error = "Error initializing LMK hash";
538 return PTR_ERR(lmk->hash_tfm);
539 }
540
541 /* No seed in LMK version 2 */
542 if (cc->key_parts == cc->tfms_count) {
543 lmk->seed = NULL;
544 return 0;
545 }
546
547 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
548 if (!lmk->seed) {
549 crypt_iv_lmk_dtr(cc);
550 ti->error = "Error kmallocing seed storage in LMK";
551 return -ENOMEM;
552 }
553
554 return 0;
555}
556
557static int crypt_iv_lmk_init(struct crypt_config *cc)
558{
559 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
560 int subkey_size = cc->key_size / cc->key_parts;
561
562 /* LMK seed is on the position of LMK_KEYS + 1 key */
563 if (lmk->seed)
564 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
565 crypto_shash_digestsize(lmk->hash_tfm));
566
567 return 0;
568}
569
570static int crypt_iv_lmk_wipe(struct crypt_config *cc)
571{
572 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
573
574 if (lmk->seed)
575 memset(lmk->seed, 0, LMK_SEED_SIZE);
576
577 return 0;
578}
579
580static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
581 struct dm_crypt_request *dmreq,
582 u8 *data)
583{
584 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200585 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000586 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100587 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000588 int i, r;
589
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200590 desc->tfm = lmk->hash_tfm;
591 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Broz34745782011-01-13 19:59:55 +0000592
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200593 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000594 if (r)
595 return r;
596
597 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200598 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000599 if (r)
600 return r;
601 }
602
603 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200604 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000605 if (r)
606 return r;
607
608 /* Sector is cropped to 56 bits here */
609 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
610 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
611 buf[2] = cpu_to_le32(4024);
612 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200613 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000614 if (r)
615 return r;
616
617 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200618 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000619 if (r)
620 return r;
621
622 for (i = 0; i < MD5_HASH_WORDS; i++)
623 __cpu_to_le32s(&md5state.hash[i]);
624 memcpy(iv, &md5state.hash, cc->iv_size);
625
626 return 0;
627}
628
629static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
630 struct dm_crypt_request *dmreq)
631{
Milan Brozef43aa32017-01-04 20:23:54 +0100632 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000633 u8 *src;
634 int r = 0;
635
636 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100637 sg = crypt_get_sg_data(cc, dmreq->sg_in);
638 src = kmap_atomic(sg_page(sg));
639 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800640 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000641 } else
642 memset(iv, 0, cc->iv_size);
643
644 return r;
645}
646
647static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
648 struct dm_crypt_request *dmreq)
649{
Milan Brozef43aa32017-01-04 20:23:54 +0100650 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000651 u8 *dst;
652 int r;
653
654 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
655 return 0;
656
Milan Brozef43aa32017-01-04 20:23:54 +0100657 sg = crypt_get_sg_data(cc, dmreq->sg_out);
658 dst = kmap_atomic(sg_page(sg));
659 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset);
Milan Broz34745782011-01-13 19:59:55 +0000660
661 /* Tweak the first block of plaintext sector */
662 if (!r)
Milan Brozef43aa32017-01-04 20:23:54 +0100663 crypto_xor(dst + sg->offset, iv, cc->iv_size);
Milan Broz34745782011-01-13 19:59:55 +0000664
Cong Wangc2e022c2011-11-28 13:26:02 +0800665 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000666 return r;
667}
668
Milan Brozed04d982013-10-28 23:21:04 +0100669static void crypt_iv_tcw_dtr(struct crypt_config *cc)
670{
671 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
672
673 kzfree(tcw->iv_seed);
674 tcw->iv_seed = NULL;
675 kzfree(tcw->whitening);
676 tcw->whitening = NULL;
677
678 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
679 crypto_free_shash(tcw->crc32_tfm);
680 tcw->crc32_tfm = NULL;
681}
682
683static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
684 const char *opts)
685{
686 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
687
Milan Broz8f0009a2017-03-16 15:39:44 +0100688 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
689 ti->error = "Unsupported sector size for TCW";
690 return -EINVAL;
691 }
692
Milan Brozed04d982013-10-28 23:21:04 +0100693 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
694 ti->error = "Wrong key size for TCW";
695 return -EINVAL;
696 }
697
698 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
699 if (IS_ERR(tcw->crc32_tfm)) {
700 ti->error = "Error initializing CRC32 in TCW";
701 return PTR_ERR(tcw->crc32_tfm);
702 }
703
704 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
705 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
706 if (!tcw->iv_seed || !tcw->whitening) {
707 crypt_iv_tcw_dtr(cc);
708 ti->error = "Error allocating seed storage in TCW";
709 return -ENOMEM;
710 }
711
712 return 0;
713}
714
715static int crypt_iv_tcw_init(struct crypt_config *cc)
716{
717 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
718 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
719
720 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
721 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
722 TCW_WHITENING_SIZE);
723
724 return 0;
725}
726
727static int crypt_iv_tcw_wipe(struct crypt_config *cc)
728{
729 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
730
731 memset(tcw->iv_seed, 0, cc->iv_size);
732 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
733
734 return 0;
735}
736
737static int crypt_iv_tcw_whitening(struct crypt_config *cc,
738 struct dm_crypt_request *dmreq,
739 u8 *data)
740{
741 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200742 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100743 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200744 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100745 int i, r;
746
747 /* xor whitening with sector number */
748 memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
749 crypto_xor(buf, (u8 *)&sector, 8);
750 crypto_xor(&buf[8], (u8 *)&sector, 8);
751
752 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200753 desc->tfm = tcw->crc32_tfm;
754 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Brozed04d982013-10-28 23:21:04 +0100755 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200756 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100757 if (r)
758 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200759 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100760 if (r)
761 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200762 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100763 if (r)
764 goto out;
765 }
766 crypto_xor(&buf[0], &buf[12], 4);
767 crypto_xor(&buf[4], &buf[8], 4);
768
769 /* apply whitening (8 bytes) to whole sector */
770 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
771 crypto_xor(data + i * 8, buf, 8);
772out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100773 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100774 return r;
775}
776
777static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
778 struct dm_crypt_request *dmreq)
779{
Milan Brozef43aa32017-01-04 20:23:54 +0100780 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100781 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200782 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100783 u8 *src;
784 int r = 0;
785
786 /* Remove whitening from ciphertext */
787 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100788 sg = crypt_get_sg_data(cc, dmreq->sg_in);
789 src = kmap_atomic(sg_page(sg));
790 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100791 kunmap_atomic(src);
792 }
793
794 /* Calculate IV */
795 memcpy(iv, tcw->iv_seed, cc->iv_size);
796 crypto_xor(iv, (u8 *)&sector, 8);
797 if (cc->iv_size > 8)
798 crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
799
800 return r;
801}
802
803static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
804 struct dm_crypt_request *dmreq)
805{
Milan Brozef43aa32017-01-04 20:23:54 +0100806 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100807 u8 *dst;
808 int r;
809
810 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
811 return 0;
812
813 /* Apply whitening on ciphertext */
Milan Brozef43aa32017-01-04 20:23:54 +0100814 sg = crypt_get_sg_data(cc, dmreq->sg_out);
815 dst = kmap_atomic(sg_page(sg));
816 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100817 kunmap_atomic(dst);
818
819 return r;
820}
821
Milan Brozef43aa32017-01-04 20:23:54 +0100822static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv,
823 struct dm_crypt_request *dmreq)
824{
825 /* Used only for writes, there must be an additional space to store IV */
826 get_random_bytes(iv, cc->iv_size);
827 return 0;
828}
829
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100830static const struct crypt_iv_operations crypt_iv_plain_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 .generator = crypt_iv_plain_gen
832};
833
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100834static const struct crypt_iv_operations crypt_iv_plain64_ops = {
Milan Broz61afef62009-12-10 23:52:25 +0000835 .generator = crypt_iv_plain64_gen
836};
837
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100838static const struct crypt_iv_operations crypt_iv_essiv_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 .ctr = crypt_iv_essiv_ctr,
840 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000841 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000842 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 .generator = crypt_iv_essiv_gen
844};
845
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100846static const struct crypt_iv_operations crypt_iv_benbi_ops = {
Rik Snel48527fa2006-09-03 08:56:39 +1000847 .ctr = crypt_iv_benbi_ctr,
848 .dtr = crypt_iv_benbi_dtr,
849 .generator = crypt_iv_benbi_gen
850};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100852static const struct crypt_iv_operations crypt_iv_null_ops = {
Ludwig Nussel46b47732007-05-09 02:32:55 -0700853 .generator = crypt_iv_null_gen
854};
855
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100856static const struct crypt_iv_operations crypt_iv_lmk_ops = {
Milan Broz34745782011-01-13 19:59:55 +0000857 .ctr = crypt_iv_lmk_ctr,
858 .dtr = crypt_iv_lmk_dtr,
859 .init = crypt_iv_lmk_init,
860 .wipe = crypt_iv_lmk_wipe,
861 .generator = crypt_iv_lmk_gen,
862 .post = crypt_iv_lmk_post
863};
864
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100865static const struct crypt_iv_operations crypt_iv_tcw_ops = {
Milan Brozed04d982013-10-28 23:21:04 +0100866 .ctr = crypt_iv_tcw_ctr,
867 .dtr = crypt_iv_tcw_dtr,
868 .init = crypt_iv_tcw_init,
869 .wipe = crypt_iv_tcw_wipe,
870 .generator = crypt_iv_tcw_gen,
871 .post = crypt_iv_tcw_post
872};
873
Milan Brozef43aa32017-01-04 20:23:54 +0100874static struct crypt_iv_operations crypt_iv_random_ops = {
875 .generator = crypt_iv_random_gen
876};
877
878/*
879 * Integrity extensions
880 */
881static bool crypt_integrity_aead(struct crypt_config *cc)
882{
883 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
884}
885
886static bool crypt_integrity_hmac(struct crypt_config *cc)
887{
Milan Broz33d2f092017-03-16 15:39:40 +0100888 return crypt_integrity_aead(cc) && cc->key_mac_size;
Milan Brozef43aa32017-01-04 20:23:54 +0100889}
890
891/* Get sg containing data */
892static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
893 struct scatterlist *sg)
894{
Milan Broz33d2f092017-03-16 15:39:40 +0100895 if (unlikely(crypt_integrity_aead(cc)))
Milan Brozef43aa32017-01-04 20:23:54 +0100896 return &sg[2];
897
898 return sg;
899}
900
901static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
902{
903 struct bio_integrity_payload *bip;
904 unsigned int tag_len;
905 int ret;
906
907 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size)
908 return 0;
909
910 bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
911 if (IS_ERR(bip))
912 return PTR_ERR(bip);
913
914 tag_len = io->cc->on_disk_tag_size * bio_sectors(bio);
915
916 bip->bip_iter.bi_size = tag_len;
917 bip->bip_iter.bi_sector = io->cc->start + io->sector;
918
919 /* We own the metadata, do not let bio_free to release it */
920 bip->bip_flags &= ~BIP_BLOCK_INTEGRITY;
921
922 ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
923 tag_len, offset_in_page(io->integrity_metadata));
924 if (unlikely(ret != tag_len))
925 return -ENOMEM;
926
927 return 0;
928}
929
930static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
931{
932#ifdef CONFIG_BLK_DEV_INTEGRITY
933 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk);
934
935 /* From now we require underlying device with our integrity profile */
936 if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) {
937 ti->error = "Integrity profile not supported.";
938 return -EINVAL;
939 }
940
Mikulas Patocka583fe742017-04-18 16:51:54 -0400941 if (bi->tag_size != cc->on_disk_tag_size ||
942 bi->tuple_size != cc->on_disk_tag_size) {
Milan Brozef43aa32017-01-04 20:23:54 +0100943 ti->error = "Integrity profile tag size mismatch.";
944 return -EINVAL;
945 }
Mikulas Patocka583fe742017-04-18 16:51:54 -0400946 if (1 << bi->interval_exp != cc->sector_size) {
947 ti->error = "Integrity profile sector size mismatch.";
948 return -EINVAL;
949 }
Milan Brozef43aa32017-01-04 20:23:54 +0100950
Milan Broz33d2f092017-03-16 15:39:40 +0100951 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +0100952 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size;
953 DMINFO("Integrity AEAD, tag size %u, IV size %u.",
954 cc->integrity_tag_size, cc->integrity_iv_size);
955
956 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) {
957 ti->error = "Integrity AEAD auth tag size is not supported.";
958 return -EINVAL;
959 }
960 } else if (cc->integrity_iv_size)
961 DMINFO("Additional per-sector space %u bytes for IV.",
962 cc->integrity_iv_size);
963
964 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) {
965 ti->error = "Not enough space for integrity tag in the profile.";
966 return -EINVAL;
967 }
968
969 return 0;
970#else
971 ti->error = "Integrity profile not supported.";
972 return -EINVAL;
973#endif
974}
975
Milan Brozd469f842007-10-19 22:42:37 +0100976static void crypt_convert_init(struct crypt_config *cc,
977 struct convert_context *ctx,
978 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000979 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
981 ctx->bio_in = bio_in;
982 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700983 if (bio_in)
984 ctx->iter_in = bio_in->bi_iter;
985 if (bio_out)
986 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100987 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000988 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
Huang Yingb2174ee2009-03-16 17:44:33 +0000991static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
Milan Brozef43aa32017-01-04 20:23:54 +0100992 void *req)
Huang Yingb2174ee2009-03-16 17:44:33 +0000993{
994 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
995}
996
Milan Brozef43aa32017-01-04 20:23:54 +0100997static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq)
Huang Yingb2174ee2009-03-16 17:44:33 +0000998{
Milan Brozef43aa32017-01-04 20:23:54 +0100999 return (void *)((char *)dmreq - cc->dmreq_start);
Huang Yingb2174ee2009-03-16 17:44:33 +00001000}
1001
Milan Broz2dc5327d32011-01-13 19:59:54 +00001002static u8 *iv_of_dmreq(struct crypt_config *cc,
1003 struct dm_crypt_request *dmreq)
1004{
Milan Broz33d2f092017-03-16 15:39:40 +01001005 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001006 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1007 crypto_aead_alignmask(any_tfm_aead(cc)) + 1);
1008 else
1009 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1010 crypto_skcipher_alignmask(any_tfm(cc)) + 1);
Milan Broz2dc5327d32011-01-13 19:59:54 +00001011}
1012
Milan Brozef43aa32017-01-04 20:23:54 +01001013static u8 *org_iv_of_dmreq(struct crypt_config *cc,
1014 struct dm_crypt_request *dmreq)
1015{
1016 return iv_of_dmreq(cc, dmreq) + cc->iv_size;
1017}
1018
1019static uint64_t *org_sector_of_dmreq(struct crypt_config *cc,
1020 struct dm_crypt_request *dmreq)
1021{
1022 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size;
1023 return (uint64_t*) ptr;
1024}
1025
1026static unsigned int *org_tag_of_dmreq(struct crypt_config *cc,
1027 struct dm_crypt_request *dmreq)
1028{
1029 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size +
1030 cc->iv_size + sizeof(uint64_t);
1031 return (unsigned int*)ptr;
1032}
1033
1034static void *tag_from_dmreq(struct crypt_config *cc,
1035 struct dm_crypt_request *dmreq)
1036{
1037 struct convert_context *ctx = dmreq->ctx;
1038 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1039
1040 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) *
1041 cc->on_disk_tag_size];
1042}
1043
1044static void *iv_tag_from_dmreq(struct crypt_config *cc,
1045 struct dm_crypt_request *dmreq)
1046{
1047 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size;
1048}
1049
1050static int crypt_convert_block_aead(struct crypt_config *cc,
1051 struct convert_context *ctx,
1052 struct aead_request *req,
1053 unsigned int tag_offset)
Milan Broz01482b72008-02-08 02:11:04 +00001054{
Kent Overstreet003b5c52013-10-11 15:45:43 -07001055 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1056 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001057 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001058 u8 *iv, *org_iv, *tag_iv, *tag;
1059 uint64_t *sector;
1060 int r = 0;
1061
1062 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size);
Milan Broz01482b72008-02-08 02:11:04 +00001063
Milan Broz8f0009a2017-03-16 15:39:44 +01001064 /* Reject unexpected unaligned bio. */
1065 if (unlikely(bv_in.bv_offset & (cc->sector_size - 1)))
1066 return -EIO;
1067
Huang Yingb2174ee2009-03-16 17:44:33 +00001068 dmreq = dmreq_of_req(cc, req);
Mikulas Patockac66029f2012-07-27 15:08:05 +01001069 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001070 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001071 dmreq->iv_sector >>= cc->sector_shift;
Huang Yingb2174ee2009-03-16 17:44:33 +00001072 dmreq->ctx = ctx;
Milan Broz01482b72008-02-08 02:11:04 +00001073
Milan Brozef43aa32017-01-04 20:23:54 +01001074 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
Milan Broz01482b72008-02-08 02:11:04 +00001075
Milan Brozef43aa32017-01-04 20:23:54 +01001076 sector = org_sector_of_dmreq(cc, dmreq);
1077 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1078
1079 iv = iv_of_dmreq(cc, dmreq);
1080 org_iv = org_iv_of_dmreq(cc, dmreq);
1081 tag = tag_from_dmreq(cc, dmreq);
1082 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1083
1084 /* AEAD request:
1085 * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
1086 * | (authenticated) | (auth+encryption) | |
1087 * | sector_LE | IV | sector in/out | tag in/out |
1088 */
1089 sg_init_table(dmreq->sg_in, 4);
1090 sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t));
1091 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001092 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 +01001093 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size);
1094
1095 sg_init_table(dmreq->sg_out, 4);
1096 sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t));
1097 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001098 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 +01001099 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size);
Milan Broz01482b72008-02-08 02:11:04 +00001100
Milan Broz3a7f6c92008-02-08 02:11:14 +00001101 if (cc->iv_gen_ops) {
Milan Brozef43aa32017-01-04 20:23:54 +01001102 /* For READs use IV stored in integrity metadata */
1103 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1104 memcpy(org_iv, tag_iv, cc->iv_size);
1105 } else {
1106 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1107 if (r < 0)
1108 return r;
1109 /* Store generated IV in integrity metadata */
1110 if (cc->integrity_iv_size)
1111 memcpy(tag_iv, org_iv, cc->iv_size);
1112 }
1113 /* Working copy of IV, to be modified in crypto API */
1114 memcpy(iv, org_iv, cc->iv_size);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001115 }
1116
Milan Brozef43aa32017-01-04 20:23:54 +01001117 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size);
1118 if (bio_data_dir(ctx->bio_in) == WRITE) {
1119 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001120 cc->sector_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001121 r = crypto_aead_encrypt(req);
1122 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size)
1123 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0,
1124 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size));
1125 } else {
1126 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001127 cc->sector_size + cc->integrity_tag_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001128 r = crypto_aead_decrypt(req);
1129 }
1130
1131 if (r == -EBADMSG)
1132 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1133 (unsigned long long)le64_to_cpu(*sector));
1134
1135 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
1136 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1137
Milan Broz8f0009a2017-03-16 15:39:44 +01001138 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1139 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001140
1141 return r;
1142}
1143
1144static int crypt_convert_block_skcipher(struct crypt_config *cc,
1145 struct convert_context *ctx,
1146 struct skcipher_request *req,
1147 unsigned int tag_offset)
1148{
1149 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1150 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
1151 struct scatterlist *sg_in, *sg_out;
1152 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001153 u8 *iv, *org_iv, *tag_iv;
1154 uint64_t *sector;
1155 int r = 0;
1156
Milan Broz8f0009a2017-03-16 15:39:44 +01001157 /* Reject unexpected unaligned bio. */
1158 if (unlikely(bv_in.bv_offset & (cc->sector_size - 1)))
1159 return -EIO;
1160
Milan Brozef43aa32017-01-04 20:23:54 +01001161 dmreq = dmreq_of_req(cc, req);
1162 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001163 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
Mikulas Patockaff3af922017-03-23 10:23:14 -04001164 dmreq->iv_sector >>= cc->sector_shift;
Milan Brozef43aa32017-01-04 20:23:54 +01001165 dmreq->ctx = ctx;
1166
1167 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
1168
1169 iv = iv_of_dmreq(cc, dmreq);
1170 org_iv = org_iv_of_dmreq(cc, dmreq);
1171 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1172
1173 sector = org_sector_of_dmreq(cc, dmreq);
1174 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1175
1176 /* For skcipher we use only the first sg item */
1177 sg_in = &dmreq->sg_in[0];
1178 sg_out = &dmreq->sg_out[0];
1179
1180 sg_init_table(sg_in, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001181 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001182
1183 sg_init_table(sg_out, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001184 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001185
1186 if (cc->iv_gen_ops) {
1187 /* For READs use IV stored in integrity metadata */
1188 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1189 memcpy(org_iv, tag_iv, cc->integrity_iv_size);
1190 } else {
1191 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1192 if (r < 0)
1193 return r;
1194 /* Store generated IV in integrity metadata */
1195 if (cc->integrity_iv_size)
1196 memcpy(tag_iv, org_iv, cc->integrity_iv_size);
1197 }
1198 /* Working copy of IV, to be modified in crypto API */
1199 memcpy(iv, org_iv, cc->iv_size);
1200 }
1201
Milan Broz8f0009a2017-03-16 15:39:44 +01001202 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001203
1204 if (bio_data_dir(ctx->bio_in) == WRITE)
Herbert Xubbdb23b2016-01-24 21:16:36 +08001205 r = crypto_skcipher_encrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001206 else
Herbert Xubbdb23b2016-01-24 21:16:36 +08001207 r = crypto_skcipher_decrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001208
Milan Broz2dc5327d32011-01-13 19:59:54 +00001209 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001210 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1211
Milan Broz8f0009a2017-03-16 15:39:44 +01001212 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1213 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Broz2dc5327d32011-01-13 19:59:54 +00001214
Milan Broz3a7f6c92008-02-08 02:11:14 +00001215 return r;
Milan Broz01482b72008-02-08 02:11:04 +00001216}
1217
Milan Broz95497a92008-02-08 02:11:12 +00001218static void kcryptd_async_done(struct crypto_async_request *async_req,
1219 int error);
Andi Kleenc0297722011-01-13 19:59:53 +00001220
Milan Brozef43aa32017-01-04 20:23:54 +01001221static void crypt_alloc_req_skcipher(struct crypt_config *cc,
1222 struct convert_context *ctx)
Milan Brozddd42ed2008-02-08 02:11:07 +00001223{
Mikulas Patockac66029f2012-07-27 15:08:05 +01001224 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +00001225
Milan Brozef43aa32017-01-04 20:23:54 +01001226 if (!ctx->r.req)
1227 ctx->r.req = mempool_alloc(cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +00001228
Milan Brozef43aa32017-01-04 20:23:54 +01001229 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]);
Milan Broz54cea3f2015-05-15 17:00:25 +02001230
1231 /*
1232 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1233 * requests if driver request queue is full.
1234 */
Milan Brozef43aa32017-01-04 20:23:54 +01001235 skcipher_request_set_callback(ctx->r.req,
Andi Kleenc0297722011-01-13 19:59:53 +00001236 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Milan Brozef43aa32017-01-04 20:23:54 +01001237 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req));
Milan Brozddd42ed2008-02-08 02:11:07 +00001238}
1239
Milan Brozef43aa32017-01-04 20:23:54 +01001240static void crypt_alloc_req_aead(struct crypt_config *cc,
1241 struct convert_context *ctx)
1242{
1243 if (!ctx->r.req_aead)
1244 ctx->r.req_aead = mempool_alloc(cc->req_pool, GFP_NOIO);
1245
1246 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]);
1247
1248 /*
1249 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1250 * requests if driver request queue is full.
1251 */
1252 aead_request_set_callback(ctx->r.req_aead,
1253 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
1254 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead));
1255}
1256
1257static void crypt_alloc_req(struct crypt_config *cc,
1258 struct convert_context *ctx)
1259{
Milan Broz33d2f092017-03-16 15:39:40 +01001260 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001261 crypt_alloc_req_aead(cc, ctx);
1262 else
1263 crypt_alloc_req_skcipher(cc, ctx);
1264}
1265
1266static void crypt_free_req_skcipher(struct crypt_config *cc,
1267 struct skcipher_request *req, struct bio *base_bio)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001268{
1269 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1270
Herbert Xubbdb23b2016-01-24 21:16:36 +08001271 if ((struct skcipher_request *)(io + 1) != req)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001272 mempool_free(req, cc->req_pool);
1273}
1274
Milan Brozef43aa32017-01-04 20:23:54 +01001275static void crypt_free_req_aead(struct crypt_config *cc,
1276 struct aead_request *req, struct bio *base_bio)
1277{
1278 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1279
1280 if ((struct aead_request *)(io + 1) != req)
1281 mempool_free(req, cc->req_pool);
1282}
1283
1284static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio)
1285{
Milan Broz33d2f092017-03-16 15:39:40 +01001286 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001287 crypt_free_req_aead(cc, req, base_bio);
1288 else
1289 crypt_free_req_skcipher(cc, req, base_bio);
1290}
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292/*
1293 * Encrypt / decrypt data from one bio to another one (can be the same one)
1294 */
1295static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +01001296 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Milan Brozef43aa32017-01-04 20:23:54 +01001298 unsigned int tag_offset = 0;
Mikulas Patockaff3af922017-03-23 10:23:14 -04001299 unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT;
Milan Broz3f1e9072008-03-28 14:16:07 -07001300 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Mikulas Patocka40b62292012-07-27 15:08:04 +01001302 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +01001303
Kent Overstreet003b5c52013-10-11 15:45:43 -07001304 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Milan Broz3a7f6c92008-02-08 02:11:14 +00001306 crypt_alloc_req(cc, ctx);
Mikulas Patocka40b62292012-07-27 15:08:04 +01001307 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -07001308
Milan Broz33d2f092017-03-16 15:39:40 +01001309 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001310 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset);
1311 else
1312 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001313
1314 switch (r) {
Milan Broz54cea3f2015-05-15 17:00:25 +02001315 /*
1316 * The request was queued by a crypto driver
1317 * but the driver request queue is full, let's wait.
1318 */
Milan Broz3a7f6c92008-02-08 02:11:14 +00001319 case -EBUSY:
1320 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -08001321 reinit_completion(&ctx->restart);
Milan Broz54cea3f2015-05-15 17:00:25 +02001322 /* fall through */
1323 /*
1324 * The request is queued and processed asynchronously,
1325 * completion function kcryptd_async_done() will be called.
1326 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001327 case -EINPROGRESS:
Milan Brozef43aa32017-01-04 20:23:54 +01001328 ctx->r.req = NULL;
Milan Broz8f0009a2017-03-16 15:39:44 +01001329 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001330 tag_offset++;
Milan Broz3a7f6c92008-02-08 02:11:14 +00001331 continue;
Milan Broz54cea3f2015-05-15 17:00:25 +02001332 /*
1333 * The request was already processed (synchronously).
1334 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001335 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001336 atomic_dec(&ctx->cc_pending);
Milan Broz8f0009a2017-03-16 15:39:44 +01001337 ctx->cc_sector += sector_step;
Mikulas Patocka583fe742017-04-18 16:51:54 -04001338 tag_offset++;
Milan Brozc7f1b202008-07-02 09:34:28 +01001339 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -07001340 continue;
Milan Brozef43aa32017-01-04 20:23:54 +01001341 /*
1342 * There was a data integrity error.
1343 */
1344 case -EBADMSG:
1345 atomic_dec(&ctx->cc_pending);
1346 return -EILSEQ;
1347 /*
1348 * There was an error while processing the request.
1349 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001350 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001351 atomic_dec(&ctx->cc_pending);
Milan Brozef43aa32017-01-04 20:23:54 +01001352 return -EIO;
Milan Broz3f1e9072008-03-28 14:16:07 -07001353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355
Milan Broz3f1e9072008-03-28 14:16:07 -07001356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001359static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1360
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361/*
1362 * Generate a new unfragmented bio with the given size
Mike Snitzer586b2862015-09-09 21:34:51 -04001363 * This should never violate the device limitations (but only because
1364 * max_segment_size is being constrained to PAGE_SIZE).
Mikulas Patocka7145c242015-02-13 08:24:41 -05001365 *
1366 * This function may be called concurrently. If we allocate from the mempool
1367 * concurrently, there is a possibility of deadlock. For example, if we have
1368 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1369 * the mempool concurrently, it may deadlock in a situation where both processes
1370 * have allocated 128 pages and the mempool is exhausted.
1371 *
1372 * In order to avoid this scenario we allocate the pages under a mutex.
1373 *
1374 * In order to not degrade performance with excessive locking, we try
1375 * non-blocking allocations without a mutex first but on failure we fallback
1376 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001378static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001380 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001381 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001383 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
1384 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +00001385 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Mikulas Patocka7145c242015-02-13 08:24:41 -05001387retry:
Mel Gormand0164ad2015-11-06 16:28:21 -08001388 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001389 mutex_lock(&cc->bio_alloc_lock);
1390
Olaf Kirch2f9941b2007-05-09 02:32:53 -07001391 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -07001392 if (!clone)
Milan Brozef43aa32017-01-04 20:23:54 +01001393 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Olaf Kirch027581f2007-05-09 02:32:52 -07001395 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -07001396
Mikulas Patocka7145c242015-02-13 08:24:41 -05001397 remaining_size = size;
1398
Olaf Kirchf97380b2007-05-09 02:32:54 -07001399 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +00001400 page = mempool_alloc(cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -05001401 if (!page) {
1402 crypt_free_buffer_pages(cc, clone);
1403 bio_put(clone);
Mel Gormand0164ad2015-11-06 16:28:21 -08001404 gfp_mask |= __GFP_DIRECT_RECLAIM;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001405 goto retry;
1406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Mikulas Patocka7145c242015-02-13 08:24:41 -05001408 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Ming Lei0dae7fe2016-10-29 16:08:06 +08001410 bio_add_page(clone, page, len, 0);
Milan Broz91e10622007-12-13 14:16:10 +00001411
Mikulas Patocka7145c242015-02-13 08:24:41 -05001412 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
Milan Brozef43aa32017-01-04 20:23:54 +01001415 /* Allocate space for integrity tags */
1416 if (dm_crypt_integrity_io_alloc(io, clone)) {
1417 crypt_free_buffer_pages(cc, clone);
1418 bio_put(clone);
1419 clone = NULL;
1420 }
1421out:
Mel Gormand0164ad2015-11-06 16:28:21 -08001422 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001423 mutex_unlock(&cc->bio_alloc_lock);
1424
Milan Broz8b004452006-10-03 01:15:37 -07001425 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Neil Brown644bd2f2007-10-16 13:48:46 +02001428static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Neil Brown644bd2f2007-10-16 13:48:46 +02001430 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 struct bio_vec *bv;
1432
Kent Overstreetcb34e052012-09-05 15:22:02 -07001433 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 BUG_ON(!bv->bv_page);
1435 mempool_free(bv->bv_page, cc->page_pool);
1436 bv->bv_page = NULL;
1437 }
1438}
1439
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001440static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1441 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001442{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001443 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001444 io->base_bio = bio;
1445 io->sector = sector;
1446 io->error = 0;
Milan Brozef43aa32017-01-04 20:23:54 +01001447 io->ctx.r.req = NULL;
1448 io->integrity_metadata = NULL;
1449 io->integrity_metadata_from_pool = false;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001450 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001451}
1452
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001453static void crypt_inc_pending(struct dm_crypt_io *io)
1454{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001455 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001456}
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458/*
1459 * One of the bios was finished. Check for completion of
1460 * the whole request and correctly clean up the buffer.
1461 */
Milan Broz5742fd72008-02-08 02:10:43 +00001462static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001464 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001465 struct bio *base_bio = io->base_bio;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001466 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Mikulas Patocka40b62292012-07-27 15:08:04 +01001468 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return;
1470
Milan Brozef43aa32017-01-04 20:23:54 +01001471 if (io->ctx.r.req)
1472 crypt_free_req(cc, io->ctx.r.req, base_bio);
1473
1474 if (unlikely(io->integrity_metadata_from_pool))
1475 mempool_free(io->integrity_metadata, io->cc->tag_pool);
1476 else
1477 kfree(io->integrity_metadata);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001478
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001479 base_bio->bi_error = error;
1480 bio_endio(base_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
1483/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001484 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 *
1486 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001487 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001488 *
1489 * kcryptd performs the actual encryption or decryption.
1490 *
1491 * kcryptd_io performs the IO submission.
1492 *
1493 * They must be separated as otherwise the final stages could be
1494 * starved by new requests which can block in the first stages due
1495 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001496 *
1497 * The work is done per CPU global for all dm-crypt instances.
1498 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001500static void crypt_endio(struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001501{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001502 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001503 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001504 unsigned rw = bio_data_dir(clone);
Sasha Levin9b81c842015-08-10 19:05:18 -04001505 int error;
Milan Broz8b004452006-10-03 01:15:37 -07001506
1507 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001508 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001509 */
Milan Brozee7a4912008-02-08 02:10:46 +00001510 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001511 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001512
Sasha Levin9b81c842015-08-10 19:05:18 -04001513 error = clone->bi_error;
Milan Brozee7a4912008-02-08 02:10:46 +00001514 bio_put(clone);
1515
Sasha Levin9b81c842015-08-10 19:05:18 -04001516 if (rw == READ && !error) {
Milan Brozee7a4912008-02-08 02:10:46 +00001517 kcryptd_queue_crypt(io);
1518 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001519 }
Milan Broz8b004452006-10-03 01:15:37 -07001520
Sasha Levin9b81c842015-08-10 19:05:18 -04001521 if (unlikely(error))
1522 io->error = error;
Milan Broz5742fd72008-02-08 02:10:43 +00001523
1524 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001525}
1526
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001527static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001528{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001529 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001530
1531 clone->bi_private = io;
1532 clone->bi_end_io = crypt_endio;
1533 clone->bi_bdev = cc->dev->bdev;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001534 clone->bi_opf = io->base_bio->bi_opf;
Milan Broz8b004452006-10-03 01:15:37 -07001535}
1536
Milan Broz20c82532011-01-13 19:59:53 +00001537static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001538{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001539 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001540 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001541
Milan Broz8b004452006-10-03 01:15:37 -07001542 /*
Mike Snitzer59779072015-04-09 16:53:24 -04001543 * We need the original biovec array in order to decrypt
1544 * the whole bio data *afterwards* -- thanks to immutable
1545 * biovecs we don't need to worry about the block layer
1546 * modifying the biovec array; so leverage bio_clone_fast().
Milan Broz8b004452006-10-03 01:15:37 -07001547 */
Mike Snitzer59779072015-04-09 16:53:24 -04001548 clone = bio_clone_fast(io->base_bio, gfp, cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001549 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001550 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001551
Milan Broz20c82532011-01-13 19:59:53 +00001552 crypt_inc_pending(io);
1553
Milan Broz8b004452006-10-03 01:15:37 -07001554 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001555 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001556
Milan Brozef43aa32017-01-04 20:23:54 +01001557 if (dm_crypt_integrity_io_alloc(io, clone)) {
1558 crypt_dec_pending(io);
1559 bio_put(clone);
1560 return 1;
1561 }
1562
Milan Broz93e605c2006-10-03 01:15:38 -07001563 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001564 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001565}
1566
Mikulas Patockadc267622015-02-13 08:25:59 -05001567static void kcryptd_io_read_work(struct work_struct *work)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001568{
1569 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1570
Mikulas Patockadc267622015-02-13 08:25:59 -05001571 crypt_inc_pending(io);
1572 if (kcryptd_io_read(io, GFP_NOIO))
1573 io->error = -ENOMEM;
1574 crypt_dec_pending(io);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001575}
1576
Mikulas Patockadc267622015-02-13 08:25:59 -05001577static void kcryptd_queue_read(struct dm_crypt_io *io)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001578{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001579 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001580
Mikulas Patockadc267622015-02-13 08:25:59 -05001581 INIT_WORK(&io->work, kcryptd_io_read_work);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001582 queue_work(cc->io_queue, &io->work);
1583}
1584
Mikulas Patockadc267622015-02-13 08:25:59 -05001585static void kcryptd_io_write(struct dm_crypt_io *io)
1586{
1587 struct bio *clone = io->ctx.bio_out;
1588
1589 generic_make_request(clone);
1590}
1591
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001592#define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1593
Mikulas Patockadc267622015-02-13 08:25:59 -05001594static int dmcrypt_write(void *data)
1595{
1596 struct crypt_config *cc = data;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001597 struct dm_crypt_io *io;
1598
Mikulas Patockadc267622015-02-13 08:25:59 -05001599 while (1) {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001600 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -05001601 struct blk_plug plug;
1602
1603 DECLARE_WAITQUEUE(wait, current);
1604
1605 spin_lock_irq(&cc->write_thread_wait.lock);
1606continue_locked:
1607
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001608 if (!RB_EMPTY_ROOT(&cc->write_tree))
Mikulas Patockadc267622015-02-13 08:25:59 -05001609 goto pop_from_list;
1610
Rabin Vincentf659b102016-09-21 16:22:29 +02001611 set_current_state(TASK_INTERRUPTIBLE);
Mikulas Patockadc267622015-02-13 08:25:59 -05001612 __add_wait_queue(&cc->write_thread_wait, &wait);
1613
1614 spin_unlock_irq(&cc->write_thread_wait.lock);
1615
Rabin Vincentf659b102016-09-21 16:22:29 +02001616 if (unlikely(kthread_should_stop())) {
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001617 set_current_state(TASK_RUNNING);
Rabin Vincentf659b102016-09-21 16:22:29 +02001618 remove_wait_queue(&cc->write_thread_wait, &wait);
1619 break;
1620 }
1621
Mikulas Patockadc267622015-02-13 08:25:59 -05001622 schedule();
1623
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001624 set_current_state(TASK_RUNNING);
Mikulas Patockadc267622015-02-13 08:25:59 -05001625 spin_lock_irq(&cc->write_thread_wait.lock);
1626 __remove_wait_queue(&cc->write_thread_wait, &wait);
1627 goto continue_locked;
1628
1629pop_from_list:
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001630 write_tree = cc->write_tree;
1631 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05001632 spin_unlock_irq(&cc->write_thread_wait.lock);
1633
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001634 BUG_ON(rb_parent(write_tree.rb_node));
1635
1636 /*
1637 * Note: we cannot walk the tree here with rb_next because
1638 * the structures may be freed when kcryptd_io_write is called.
1639 */
Mikulas Patockadc267622015-02-13 08:25:59 -05001640 blk_start_plug(&plug);
1641 do {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001642 io = crypt_io_from_node(rb_first(&write_tree));
1643 rb_erase(&io->rb_node, &write_tree);
Mikulas Patockadc267622015-02-13 08:25:59 -05001644 kcryptd_io_write(io);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001645 } while (!RB_EMPTY_ROOT(&write_tree));
Mikulas Patockadc267622015-02-13 08:25:59 -05001646 blk_finish_plug(&plug);
1647 }
1648 return 0;
1649}
1650
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001651static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001652{
Milan Brozdec1ced2008-02-08 02:10:57 +00001653 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001654 struct crypt_config *cc = io->cc;
Mikulas Patockadc267622015-02-13 08:25:59 -05001655 unsigned long flags;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001656 sector_t sector;
1657 struct rb_node **rbp, *parent;
Milan Brozdec1ced2008-02-08 02:10:57 +00001658
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001659 if (unlikely(io->error < 0)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001660 crypt_free_buffer_pages(cc, clone);
1661 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001662 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001663 return;
1664 }
1665
1666 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001667 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001668
Kent Overstreet4f024f32013-10-11 15:44:27 -07001669 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001670
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001671 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1672 generic_make_request(clone);
1673 return;
1674 }
1675
Mikulas Patockadc267622015-02-13 08:25:59 -05001676 spin_lock_irqsave(&cc->write_thread_wait.lock, flags);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001677 rbp = &cc->write_tree.rb_node;
1678 parent = NULL;
1679 sector = io->sector;
1680 while (*rbp) {
1681 parent = *rbp;
1682 if (sector < crypt_io_from_node(parent)->sector)
1683 rbp = &(*rbp)->rb_left;
1684 else
1685 rbp = &(*rbp)->rb_right;
1686 }
1687 rb_link_node(&io->rb_node, parent, rbp);
1688 rb_insert_color(&io->rb_node, &cc->write_tree);
1689
Mikulas Patockadc267622015-02-13 08:25:59 -05001690 wake_up_locked(&cc->write_thread_wait);
1691 spin_unlock_irqrestore(&cc->write_thread_wait.lock, flags);
Milan Broz4e4eef62008-02-08 02:10:49 +00001692}
1693
Milan Brozfc5a5e92008-10-10 13:37:04 +01001694static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001695{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001696 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001697 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001698 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001699 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +00001700 int r;
Milan Broz8b004452006-10-03 01:15:37 -07001701
Milan Broz93e605c2006-10-03 01:15:38 -07001702 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001703 * Prevent io from disappearing until this function completes.
1704 */
1705 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001706 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001707
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001708 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1709 if (unlikely(!clone)) {
1710 io->error = -EIO;
1711 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001712 }
Milan Broz899c95d2008-02-08 02:11:02 +00001713
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001714 io->ctx.bio_out = clone;
1715 io->ctx.iter_out = clone->bi_iter;
1716
1717 sector += bio_sectors(clone);
1718
1719 crypt_inc_pending(io);
1720 r = crypt_convert(cc, &io->ctx);
Milan Brozef43aa32017-01-04 20:23:54 +01001721 if (r < 0)
1722 io->error = r;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001723 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1724
1725 /* Encryption was already finished, submit io now */
1726 if (crypt_finished) {
1727 kcryptd_crypt_write_io_submit(io, 0);
1728 io->sector = sector;
1729 }
1730
1731dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001732 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001733}
1734
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001735static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001736{
Milan Broz5742fd72008-02-08 02:10:43 +00001737 crypt_dec_pending(io);
1738}
1739
Milan Broz4e4eef62008-02-08 02:10:49 +00001740static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001741{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001742 struct crypt_config *cc = io->cc;
Milan Broz5742fd72008-02-08 02:10:43 +00001743 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001744
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001745 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001746
Milan Broz53017032008-02-08 02:10:38 +00001747 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001748 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001749
Milan Broz5742fd72008-02-08 02:10:43 +00001750 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001751 if (r < 0)
Milan Brozef43aa32017-01-04 20:23:54 +01001752 io->error = r;
Milan Broz5742fd72008-02-08 02:10:43 +00001753
Mikulas Patocka40b62292012-07-27 15:08:04 +01001754 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001755 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001756
1757 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001758}
1759
Milan Broz95497a92008-02-08 02:11:12 +00001760static void kcryptd_async_done(struct crypto_async_request *async_req,
1761 int error)
1762{
Huang Yingb2174ee2009-03-16 17:44:33 +00001763 struct dm_crypt_request *dmreq = async_req->data;
1764 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001765 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001766 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001767
Milan Broz54cea3f2015-05-15 17:00:25 +02001768 /*
1769 * A request from crypto driver backlog is going to be processed now,
1770 * finish the completion and continue in crypt_convert().
1771 * (Callback will be called for the second time for this request.)
1772 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001773 if (error == -EINPROGRESS) {
1774 complete(&ctx->restart);
Milan Broz95497a92008-02-08 02:11:12 +00001775 return;
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001776 }
Milan Broz95497a92008-02-08 02:11:12 +00001777
Milan Broz2dc5327d32011-01-13 19:59:54 +00001778 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001779 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq);
Milan Broz2dc5327d32011-01-13 19:59:54 +00001780
Milan Brozef43aa32017-01-04 20:23:54 +01001781 if (error == -EBADMSG) {
1782 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1783 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
1784 io->error = -EILSEQ;
1785 } else if (error < 0)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001786 io->error = -EIO;
1787
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001788 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00001789
Mikulas Patocka40b62292012-07-27 15:08:04 +01001790 if (!atomic_dec_and_test(&ctx->cc_pending))
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001791 return;
Milan Broz95497a92008-02-08 02:11:12 +00001792
1793 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001794 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001795 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001796 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001797}
1798
Milan Broz4e4eef62008-02-08 02:10:49 +00001799static void kcryptd_crypt(struct work_struct *work)
1800{
1801 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1802
1803 if (bio_data_dir(io->base_bio) == READ)
1804 kcryptd_crypt_read_convert(io);
1805 else
1806 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001807}
1808
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001809static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1810{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001811 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001812
1813 INIT_WORK(&io->work, kcryptd_crypt);
1814 queue_work(cc->crypt_queue, &io->work);
1815}
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817/*
1818 * Decode key from its hex representation
1819 */
1820static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1821{
1822 char buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 unsigned int i;
1824
1825 buffer[2] = '\0';
1826
Milan Broz8b004452006-10-03 01:15:37 -07001827 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 buffer[0] = *hex++;
1829 buffer[1] = *hex++;
1830
majianpeng1a66a082012-07-27 15:07:59 +01001831 if (kstrtou8(buffer, 16, &key[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 return -EINVAL;
1833 }
1834
1835 if (*hex != '\0')
1836 return -EINVAL;
1837
1838 return 0;
1839}
1840
Milan Brozef43aa32017-01-04 20:23:54 +01001841static void crypt_free_tfms_aead(struct crypt_config *cc)
1842{
1843 if (!cc->cipher_tfm.tfms_aead)
1844 return;
1845
1846 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1847 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]);
1848 cc->cipher_tfm.tfms_aead[0] = NULL;
1849 }
1850
1851 kfree(cc->cipher_tfm.tfms_aead);
1852 cc->cipher_tfm.tfms_aead = NULL;
1853}
1854
1855static void crypt_free_tfms_skcipher(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001856{
Milan Brozd1f96422011-01-13 19:59:54 +00001857 unsigned i;
1858
Milan Brozef43aa32017-01-04 20:23:54 +01001859 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001860 return;
1861
Milan Brozd1f96422011-01-13 19:59:54 +00001862 for (i = 0; i < cc->tfms_count; i++)
Milan Brozef43aa32017-01-04 20:23:54 +01001863 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) {
1864 crypto_free_skcipher(cc->cipher_tfm.tfms[i]);
1865 cc->cipher_tfm.tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001866 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001867
Milan Brozef43aa32017-01-04 20:23:54 +01001868 kfree(cc->cipher_tfm.tfms);
1869 cc->cipher_tfm.tfms = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001870}
1871
Milan Brozef43aa32017-01-04 20:23:54 +01001872static void crypt_free_tfms(struct crypt_config *cc)
1873{
Milan Broz33d2f092017-03-16 15:39:40 +01001874 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001875 crypt_free_tfms_aead(cc);
1876 else
1877 crypt_free_tfms_skcipher(cc);
1878}
1879
1880static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001881{
Milan Brozd1f96422011-01-13 19:59:54 +00001882 unsigned i;
1883 int err;
1884
Milan Brozef43aa32017-01-04 20:23:54 +01001885 cc->cipher_tfm.tfms = kzalloc(cc->tfms_count *
1886 sizeof(struct crypto_skcipher *), GFP_KERNEL);
1887 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001888 return -ENOMEM;
1889
Milan Brozd1f96422011-01-13 19:59:54 +00001890 for (i = 0; i < cc->tfms_count; i++) {
Milan Brozef43aa32017-01-04 20:23:54 +01001891 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
1892 if (IS_ERR(cc->cipher_tfm.tfms[i])) {
1893 err = PTR_ERR(cc->cipher_tfm.tfms[i]);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001894 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001895 return err;
1896 }
1897 }
1898
1899 return 0;
1900}
1901
Milan Brozef43aa32017-01-04 20:23:54 +01001902static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
1903{
Milan Brozef43aa32017-01-04 20:23:54 +01001904 int err;
1905
1906 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
1907 if (!cc->cipher_tfm.tfms)
1908 return -ENOMEM;
1909
Milan Brozef43aa32017-01-04 20:23:54 +01001910 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
1911 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1912 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
1913 crypt_free_tfms(cc);
1914 return err;
1915 }
1916
Milan Brozef43aa32017-01-04 20:23:54 +01001917 return 0;
1918}
1919
1920static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
1921{
Milan Broz33d2f092017-03-16 15:39:40 +01001922 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001923 return crypt_alloc_tfms_aead(cc, ciphermode);
1924 else
1925 return crypt_alloc_tfms_skcipher(cc, ciphermode);
1926}
1927
1928static unsigned crypt_subkey_size(struct crypt_config *cc)
1929{
1930 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1931}
1932
1933static unsigned crypt_authenckey_size(struct crypt_config *cc)
1934{
1935 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
1936}
1937
1938/*
1939 * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
1940 * the key must be for some reason in special format.
1941 * This funcion converts cc->key to this special format.
1942 */
1943static void crypt_copy_authenckey(char *p, const void *key,
1944 unsigned enckeylen, unsigned authkeylen)
1945{
1946 struct crypto_authenc_key_param *param;
1947 struct rtattr *rta;
1948
1949 rta = (struct rtattr *)p;
1950 param = RTA_DATA(rta);
1951 param->enckeylen = cpu_to_be32(enckeylen);
1952 rta->rta_len = RTA_LENGTH(sizeof(*param));
1953 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
1954 p += RTA_SPACE(sizeof(*param));
1955 memcpy(p, key + enckeylen, authkeylen);
1956 p += authkeylen;
1957 memcpy(p, key, enckeylen);
1958}
1959
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04001960static int crypt_setkey(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +00001961{
Milan Brozda31a072013-10-28 23:21:03 +01001962 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001963 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001964
Milan Brozda31a072013-10-28 23:21:03 +01001965 /* Ignore extra keys (which are used for IV etc) */
Milan Brozef43aa32017-01-04 20:23:54 +01001966 subkey_size = crypt_subkey_size(cc);
Milan Brozda31a072013-10-28 23:21:03 +01001967
Milan Brozef43aa32017-01-04 20:23:54 +01001968 if (crypt_integrity_hmac(cc))
1969 crypt_copy_authenckey(cc->authenc_key, cc->key,
1970 subkey_size - cc->key_mac_size,
1971 cc->key_mac_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001972 for (i = 0; i < cc->tfms_count; i++) {
Milan Broz33d2f092017-03-16 15:39:40 +01001973 if (crypt_integrity_hmac(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001974 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1975 cc->authenc_key, crypt_authenckey_size(cc));
Milan Broz33d2f092017-03-16 15:39:40 +01001976 else if (crypt_integrity_aead(cc))
1977 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1978 cc->key + (i * subkey_size),
1979 subkey_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001980 else
1981 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i],
1982 cc->key + (i * subkey_size),
1983 subkey_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001984 if (r)
1985 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001986 }
1987
Milan Brozef43aa32017-01-04 20:23:54 +01001988 if (crypt_integrity_hmac(cc))
1989 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc));
1990
Andi Kleenc0297722011-01-13 19:59:53 +00001991 return err;
1992}
1993
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001994#ifdef CONFIG_KEYS
1995
Ondrej Kozina027c4312016-12-01 18:20:52 +01001996static bool contains_whitespace(const char *str)
1997{
1998 while (*str)
1999 if (isspace(*str++))
2000 return true;
2001 return false;
2002}
2003
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002004static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2005{
2006 char *new_key_string, *key_desc;
2007 int ret;
2008 struct key *key;
2009 const struct user_key_payload *ukp;
2010
Ondrej Kozina027c4312016-12-01 18:20:52 +01002011 /*
2012 * Reject key_string with whitespace. dm core currently lacks code for
2013 * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
2014 */
2015 if (contains_whitespace(key_string)) {
2016 DMERR("whitespace chars not allowed in key string");
2017 return -EINVAL;
2018 }
2019
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002020 /* look for next ':' separating key_type from key_description */
2021 key_desc = strpbrk(key_string, ":");
2022 if (!key_desc || key_desc == key_string || !strlen(key_desc + 1))
2023 return -EINVAL;
2024
2025 if (strncmp(key_string, "logon:", key_desc - key_string + 1) &&
2026 strncmp(key_string, "user:", key_desc - key_string + 1))
2027 return -EINVAL;
2028
2029 new_key_string = kstrdup(key_string, GFP_KERNEL);
2030 if (!new_key_string)
2031 return -ENOMEM;
2032
2033 key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user,
2034 key_desc + 1, NULL);
2035 if (IS_ERR(key)) {
2036 kzfree(new_key_string);
2037 return PTR_ERR(key);
2038 }
2039
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002040 down_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002041
David Howells0837e492017-03-01 15:11:23 +00002042 ukp = user_key_payload_locked(key);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002043 if (!ukp) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002044 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002045 key_put(key);
2046 kzfree(new_key_string);
2047 return -EKEYREVOKED;
2048 }
2049
2050 if (cc->key_size != ukp->datalen) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002051 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002052 key_put(key);
2053 kzfree(new_key_string);
2054 return -EINVAL;
2055 }
2056
2057 memcpy(cc->key, ukp->data, cc->key_size);
2058
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002059 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002060 key_put(key);
2061
2062 /* clear the flag since following operations may invalidate previously valid key */
2063 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2064
2065 ret = crypt_setkey(cc);
2066
2067 /* wipe the kernel key payload copy in each case */
2068 memset(cc->key, 0, cc->key_size * sizeof(u8));
2069
2070 if (!ret) {
2071 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2072 kzfree(cc->key_string);
2073 cc->key_string = new_key_string;
2074 } else
2075 kzfree(new_key_string);
2076
2077 return ret;
2078}
2079
2080static int get_key_size(char **key_string)
2081{
2082 char *colon, dummy;
2083 int ret;
2084
2085 if (*key_string[0] != ':')
2086 return strlen(*key_string) >> 1;
2087
2088 /* look for next ':' in key string */
2089 colon = strpbrk(*key_string + 1, ":");
2090 if (!colon)
2091 return -EINVAL;
2092
2093 if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':')
2094 return -EINVAL;
2095
2096 *key_string = colon;
2097
2098 /* remaining key string should be :<logon|user>:<key_desc> */
2099
2100 return ret;
2101}
2102
2103#else
2104
2105static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2106{
2107 return -EINVAL;
2108}
2109
2110static int get_key_size(char **key_string)
2111{
2112 return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1;
2113}
2114
2115#endif
2116
Milan Broze48d4bb2006-10-03 01:15:37 -07002117static int crypt_set_key(struct crypt_config *cc, char *key)
2118{
Milan Brozde8be5a2011-03-24 13:54:27 +00002119 int r = -EINVAL;
2120 int key_string_len = strlen(key);
2121
Milan Broz69a8cfc2011-01-13 19:59:49 +00002122 /* Hyphen (which gives a key_size of zero) means there is no key. */
2123 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00002124 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002125
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002126 /* ':' means the key is in kernel keyring, short-circuit normal key processing */
2127 if (key[0] == ':') {
2128 r = crypt_set_keyring_key(cc, key + 1);
2129 goto out;
2130 }
2131
Ondrej Kozina265e9092016-11-02 15:02:08 +01002132 /* clear the flag since following operations may invalidate previously valid key */
2133 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2134
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002135 /* wipe references to any kernel keyring key */
2136 kzfree(cc->key_string);
2137 cc->key_string = NULL;
2138
Milan Broz69a8cfc2011-01-13 19:59:49 +00002139 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00002140 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002141
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002142 r = crypt_setkey(cc);
Ondrej Kozina265e9092016-11-02 15:02:08 +01002143 if (!r)
2144 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Milan Brozde8be5a2011-03-24 13:54:27 +00002145
2146out:
2147 /* Hex key string not needed after here, so wipe it. */
2148 memset(key, '0', key_string_len);
2149
2150 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002151}
2152
2153static int crypt_wipe_key(struct crypt_config *cc)
2154{
2155 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2156 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002157 kzfree(cc->key_string);
2158 cc->key_string = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +00002159
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002160 return crypt_setkey(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07002161}
2162
Milan Broz28513fc2010-08-12 04:14:06 +01002163static void crypt_dtr(struct dm_target *ti)
2164{
2165 struct crypt_config *cc = ti->private;
2166
2167 ti->private = NULL;
2168
2169 if (!cc)
2170 return;
2171
Rabin Vincentf659b102016-09-21 16:22:29 +02002172 if (cc->write_thread)
Mikulas Patockadc267622015-02-13 08:25:59 -05002173 kthread_stop(cc->write_thread);
2174
Milan Broz28513fc2010-08-12 04:14:06 +01002175 if (cc->io_queue)
2176 destroy_workqueue(cc->io_queue);
2177 if (cc->crypt_queue)
2178 destroy_workqueue(cc->crypt_queue);
2179
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002180 crypt_free_tfms(cc);
2181
Milan Broz28513fc2010-08-12 04:14:06 +01002182 if (cc->bs)
2183 bioset_free(cc->bs);
2184
Julia Lawall6f659852015-09-13 14:15:05 +02002185 mempool_destroy(cc->page_pool);
2186 mempool_destroy(cc->req_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01002187 mempool_destroy(cc->tag_pool);
Milan Broz28513fc2010-08-12 04:14:06 +01002188
2189 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
2190 cc->iv_gen_ops->dtr(cc);
2191
Milan Broz28513fc2010-08-12 04:14:06 +01002192 if (cc->dev)
2193 dm_put_device(ti, cc->dev);
2194
Milan Broz5ebaee62010-08-12 04:14:07 +01002195 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00002196 kzfree(cc->cipher_string);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002197 kzfree(cc->key_string);
Milan Brozef43aa32017-01-04 20:23:54 +01002198 kzfree(cc->cipher_auth);
2199 kzfree(cc->authenc_key);
Milan Broz28513fc2010-08-12 04:14:06 +01002200
2201 /* Must zero key material before freeing */
2202 kzfree(cc);
2203}
2204
Milan Broze889f972017-03-16 15:39:39 +01002205static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
2206{
2207 struct crypt_config *cc = ti->private;
2208
Milan Broz33d2f092017-03-16 15:39:40 +01002209 if (crypt_integrity_aead(cc))
Milan Broze889f972017-03-16 15:39:39 +01002210 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2211 else
2212 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2213
Milan Broze889f972017-03-16 15:39:39 +01002214 if (cc->iv_size)
2215 /* at least a 64 bit sector number should fit in our buffer */
2216 cc->iv_size = max(cc->iv_size,
2217 (unsigned int)(sizeof(u64) / sizeof(u8)));
2218 else if (ivmode) {
2219 DMWARN("Selected cipher does not support IVs");
2220 ivmode = NULL;
2221 }
2222
2223 /* Choose ivmode, see comments at iv code. */
2224 if (ivmode == NULL)
2225 cc->iv_gen_ops = NULL;
2226 else if (strcmp(ivmode, "plain") == 0)
2227 cc->iv_gen_ops = &crypt_iv_plain_ops;
2228 else if (strcmp(ivmode, "plain64") == 0)
2229 cc->iv_gen_ops = &crypt_iv_plain64_ops;
2230 else if (strcmp(ivmode, "essiv") == 0)
2231 cc->iv_gen_ops = &crypt_iv_essiv_ops;
2232 else if (strcmp(ivmode, "benbi") == 0)
2233 cc->iv_gen_ops = &crypt_iv_benbi_ops;
2234 else if (strcmp(ivmode, "null") == 0)
2235 cc->iv_gen_ops = &crypt_iv_null_ops;
2236 else if (strcmp(ivmode, "lmk") == 0) {
2237 cc->iv_gen_ops = &crypt_iv_lmk_ops;
2238 /*
2239 * Version 2 and 3 is recognised according
2240 * to length of provided multi-key string.
2241 * If present (version 3), last key is used as IV seed.
2242 * All keys (including IV seed) are always the same size.
2243 */
2244 if (cc->key_size % cc->key_parts) {
2245 cc->key_parts++;
2246 cc->key_extra_size = cc->key_size / cc->key_parts;
2247 }
2248 } else if (strcmp(ivmode, "tcw") == 0) {
2249 cc->iv_gen_ops = &crypt_iv_tcw_ops;
2250 cc->key_parts += 2; /* IV + whitening */
2251 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
2252 } else if (strcmp(ivmode, "random") == 0) {
2253 cc->iv_gen_ops = &crypt_iv_random_ops;
2254 /* Need storage space in integrity fields. */
2255 cc->integrity_iv_size = cc->iv_size;
2256 } else {
2257 ti->error = "Invalid IV mode";
2258 return -EINVAL;
2259 }
2260
2261 return 0;
2262}
2263
Milan Broz33d2f092017-03-16 15:39:40 +01002264/*
2265 * Workaround to parse cipher algorithm from crypto API spec.
2266 * The cc->cipher is currently used only in ESSIV.
2267 * This should be probably done by crypto-api calls (once available...)
2268 */
2269static int crypt_ctr_blkdev_cipher(struct crypt_config *cc)
2270{
2271 const char *alg_name = NULL;
2272 char *start, *end;
2273
2274 if (crypt_integrity_aead(cc)) {
2275 alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc)));
2276 if (!alg_name)
2277 return -EINVAL;
2278 if (crypt_integrity_hmac(cc)) {
2279 alg_name = strchr(alg_name, ',');
2280 if (!alg_name)
2281 return -EINVAL;
2282 }
2283 alg_name++;
2284 } else {
2285 alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc)));
2286 if (!alg_name)
2287 return -EINVAL;
2288 }
2289
2290 start = strchr(alg_name, '(');
2291 end = strchr(alg_name, ')');
2292
2293 if (!start && !end) {
2294 cc->cipher = kstrdup(alg_name, GFP_KERNEL);
2295 return cc->cipher ? 0 : -ENOMEM;
2296 }
2297
2298 if (!start || !end || ++start >= end)
2299 return -EINVAL;
2300
2301 cc->cipher = kzalloc(end - start + 1, GFP_KERNEL);
2302 if (!cc->cipher)
2303 return -ENOMEM;
2304
2305 strncpy(cc->cipher, start, end - start);
2306
2307 return 0;
2308}
2309
2310/*
2311 * Workaround to parse HMAC algorithm from AEAD crypto API spec.
2312 * The HMAC is needed to calculate tag size (HMAC digest size).
2313 * This should be probably done by crypto-api calls (once available...)
2314 */
2315static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
2316{
2317 char *start, *end, *mac_alg = NULL;
2318 struct crypto_ahash *mac;
2319
2320 if (!strstarts(cipher_api, "authenc("))
2321 return 0;
2322
2323 start = strchr(cipher_api, '(');
2324 end = strchr(cipher_api, ',');
2325 if (!start || !end || ++start > end)
2326 return -EINVAL;
2327
2328 mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
2329 if (!mac_alg)
2330 return -ENOMEM;
2331 strncpy(mac_alg, start, end - start);
2332
2333 mac = crypto_alloc_ahash(mac_alg, 0, 0);
2334 kfree(mac_alg);
2335
2336 if (IS_ERR(mac))
2337 return PTR_ERR(mac);
2338
2339 cc->key_mac_size = crypto_ahash_digestsize(mac);
2340 crypto_free_ahash(mac);
2341
2342 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
2343 if (!cc->authenc_key)
2344 return -ENOMEM;
2345
2346 return 0;
2347}
2348
2349static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key,
2350 char **ivmode, char **ivopts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351{
Milan Broz5ebaee62010-08-12 04:14:07 +01002352 struct crypt_config *cc = ti->private;
Milan Broz33d2f092017-03-16 15:39:40 +01002353 char *tmp, *cipher_api;
2354 int ret = -EINVAL;
2355
2356 cc->tfms_count = 1;
2357
2358 /*
2359 * New format (capi: prefix)
2360 * capi:cipher_api_spec-iv:ivopts
2361 */
2362 tmp = &cipher_in[strlen("capi:")];
2363 cipher_api = strsep(&tmp, "-");
2364 *ivmode = strsep(&tmp, ":");
2365 *ivopts = tmp;
2366
2367 if (*ivmode && !strcmp(*ivmode, "lmk"))
2368 cc->tfms_count = 64;
2369
2370 cc->key_parts = cc->tfms_count;
2371
2372 /* Allocate cipher */
2373 ret = crypt_alloc_tfms(cc, cipher_api);
2374 if (ret < 0) {
2375 ti->error = "Error allocating crypto tfm";
2376 return ret;
2377 }
2378
2379 /* Alloc AEAD, can be used only in new format. */
2380 if (crypt_integrity_aead(cc)) {
2381 ret = crypt_ctr_auth_cipher(cc, cipher_api);
2382 if (ret < 0) {
2383 ti->error = "Invalid AEAD cipher spec";
2384 return -ENOMEM;
2385 }
2386 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2387 } else
2388 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2389
2390 ret = crypt_ctr_blkdev_cipher(cc);
2391 if (ret < 0) {
2392 ti->error = "Cannot allocate cipher string";
2393 return -ENOMEM;
2394 }
2395
2396 return 0;
2397}
2398
2399static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
2400 char **ivmode, char **ivopts)
2401{
2402 struct crypt_config *cc = ti->private;
2403 char *tmp, *cipher, *chainmode, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01002404 char *cipher_api = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002405 int ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002406 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Milan Broz33d2f092017-03-16 15:39:40 +01002408 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002409 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 return -EINVAL;
2411 }
2412
Milan Broz5ebaee62010-08-12 04:14:07 +01002413 /*
2414 * Legacy dm-crypt cipher specification
Milan Brozd1f96422011-01-13 19:59:54 +00002415 * cipher[:keycount]-mode-iv:ivopts
Milan Broz5ebaee62010-08-12 04:14:07 +01002416 */
2417 tmp = cipher_in;
Milan Brozd1f96422011-01-13 19:59:54 +00002418 keycount = strsep(&tmp, "-");
2419 cipher = strsep(&keycount, ":");
2420
2421 if (!keycount)
2422 cc->tfms_count = 1;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002423 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
Milan Brozd1f96422011-01-13 19:59:54 +00002424 !is_power_of_2(cc->tfms_count)) {
2425 ti->error = "Bad cipher key count specification";
2426 return -EINVAL;
2427 }
2428 cc->key_parts = cc->tfms_count;
Milan Broz5ebaee62010-08-12 04:14:07 +01002429
2430 cc->cipher = kstrdup(cipher, GFP_KERNEL);
2431 if (!cc->cipher)
2432 goto bad_mem;
2433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 chainmode = strsep(&tmp, "-");
Milan Broz33d2f092017-03-16 15:39:40 +01002435 *ivopts = strsep(&tmp, "-");
2436 *ivmode = strsep(&*ivopts, ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
2438 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01002439 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Milan Broz7dbcd132011-01-13 19:59:52 +00002441 /*
2442 * For compatibility with the original dm-crypt mapping format, if
2443 * only the cipher name is supplied, use cbc-plain.
2444 */
Milan Broz33d2f092017-03-16 15:39:40 +01002445 if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 chainmode = "cbc";
Milan Broz33d2f092017-03-16 15:39:40 +01002447 *ivmode = "plain";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
2449
Milan Broz33d2f092017-03-16 15:39:40 +01002450 if (strcmp(chainmode, "ecb") && !*ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002451 ti->error = "IV mechanism required";
2452 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 }
2454
Milan Broz5ebaee62010-08-12 04:14:07 +01002455 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
2456 if (!cipher_api)
2457 goto bad_mem;
2458
2459 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
2460 "%s(%s)", chainmode, cipher);
2461 if (ret < 0) {
2462 kfree(cipher_api);
2463 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10002464 }
2465
Milan Broz5ebaee62010-08-12 04:14:07 +01002466 /* Allocate cipher */
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002467 ret = crypt_alloc_tfms(cc, cipher_api);
2468 if (ret < 0) {
2469 ti->error = "Error allocating crypto tfm";
Milan Broz33d2f092017-03-16 15:39:40 +01002470 kfree(cipher_api);
2471 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Milan Broz33d2f092017-03-16 15:39:40 +01002474 return 0;
2475bad_mem:
2476 ti->error = "Cannot allocate cipher strings";
2477 return -ENOMEM;
2478}
2479
2480static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key)
2481{
2482 struct crypt_config *cc = ti->private;
2483 char *ivmode = NULL, *ivopts = NULL;
2484 int ret;
2485
2486 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
2487 if (!cc->cipher_string) {
2488 ti->error = "Cannot allocate cipher strings";
2489 return -ENOMEM;
2490 }
2491
2492 if (strstarts(cipher_in, "capi:"))
2493 ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts);
2494 else
2495 ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts);
2496 if (ret)
2497 return ret;
2498
Milan Broz5ebaee62010-08-12 04:14:07 +01002499 /* Initialize IV */
Milan Broze889f972017-03-16 15:39:39 +01002500 ret = crypt_ctr_ivmode(ti, ivmode);
2501 if (ret < 0)
Milan Broz33d2f092017-03-16 15:39:40 +01002502 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Milan Brozda31a072013-10-28 23:21:03 +01002504 /* Initialize and set key */
2505 ret = crypt_set_key(cc, key);
2506 if (ret < 0) {
2507 ti->error = "Error decoding and setting key";
Milan Broz33d2f092017-03-16 15:39:40 +01002508 return ret;
Milan Brozda31a072013-10-28 23:21:03 +01002509 }
2510
Milan Broz28513fc2010-08-12 04:14:06 +01002511 /* Allocate IV */
2512 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
2513 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
2514 if (ret < 0) {
2515 ti->error = "Error creating IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002516 return ret;
Milan Broz28513fc2010-08-12 04:14:06 +01002517 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00002518 }
2519
Milan Broz28513fc2010-08-12 04:14:06 +01002520 /* Initialize IV (set keys for ESSIV etc) */
2521 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
2522 ret = cc->iv_gen_ops->init(cc);
2523 if (ret < 0) {
2524 ti->error = "Error initialising IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002525 return ret;
Milan Broz28513fc2010-08-12 04:14:06 +01002526 }
2527 }
2528
Milan Broz5ebaee62010-08-12 04:14:07 +01002529 return ret;
Milan Broz5ebaee62010-08-12 04:14:07 +01002530}
2531
Milan Brozef43aa32017-01-04 20:23:54 +01002532static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
2533{
2534 struct crypt_config *cc = ti->private;
2535 struct dm_arg_set as;
2536 static struct dm_arg _args[] = {
Milan Broz8f0009a2017-03-16 15:39:44 +01002537 {0, 6, "Invalid number of feature args"},
Milan Brozef43aa32017-01-04 20:23:54 +01002538 };
2539 unsigned int opt_params, val;
2540 const char *opt_string, *sval;
Milan Broz8f0009a2017-03-16 15:39:44 +01002541 char dummy;
Milan Brozef43aa32017-01-04 20:23:54 +01002542 int ret;
2543
2544 /* Optional parameters */
2545 as.argc = argc;
2546 as.argv = argv;
2547
2548 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2549 if (ret)
2550 return ret;
2551
2552 while (opt_params--) {
2553 opt_string = dm_shift_arg(&as);
2554 if (!opt_string) {
2555 ti->error = "Not enough feature arguments";
2556 return -EINVAL;
2557 }
2558
2559 if (!strcasecmp(opt_string, "allow_discards"))
2560 ti->num_discard_bios = 1;
2561
2562 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
2563 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
2564
2565 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
2566 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
2567 else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
2568 if (val == 0 || val > MAX_TAG_SIZE) {
2569 ti->error = "Invalid integrity arguments";
2570 return -EINVAL;
2571 }
2572 cc->on_disk_tag_size = val;
2573 sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
2574 if (!strcasecmp(sval, "aead")) {
2575 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002576 } else if (strcasecmp(sval, "none")) {
2577 ti->error = "Unknown integrity profile";
2578 return -EINVAL;
2579 }
2580
2581 cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
2582 if (!cc->cipher_auth)
2583 return -ENOMEM;
Mikulas Patockaff3af922017-03-23 10:23:14 -04002584 } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002585 if (cc->sector_size < (1 << SECTOR_SHIFT) ||
2586 cc->sector_size > 4096 ||
Mikulas Patockaff3af922017-03-23 10:23:14 -04002587 (cc->sector_size & (cc->sector_size - 1))) {
Milan Broz8f0009a2017-03-16 15:39:44 +01002588 ti->error = "Invalid feature value for sector_size";
2589 return -EINVAL;
2590 }
Mikulas Patockaff3af922017-03-23 10:23:14 -04002591 cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT;
Milan Broz8f0009a2017-03-16 15:39:44 +01002592 } else if (!strcasecmp(opt_string, "iv_large_sectors"))
2593 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
2594 else {
Milan Brozef43aa32017-01-04 20:23:54 +01002595 ti->error = "Invalid feature arguments";
2596 return -EINVAL;
2597 }
2598 }
2599
2600 return 0;
2601}
2602
Milan Broz5ebaee62010-08-12 04:14:07 +01002603/*
2604 * Construct an encryption mapping:
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002605 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
Milan Broz5ebaee62010-08-12 04:14:07 +01002606 */
2607static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2608{
2609 struct crypt_config *cc;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002610 int key_size;
Milan Brozef43aa32017-01-04 20:23:54 +01002611 unsigned int align_mask;
Milan Broz5ebaee62010-08-12 04:14:07 +01002612 unsigned long long tmpll;
2613 int ret;
Milan Brozef43aa32017-01-04 20:23:54 +01002614 size_t iv_size_padding, additional_req_size;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002615 char dummy;
Milan Broz5ebaee62010-08-12 04:14:07 +01002616
Milan Broz772ae5f2011-08-02 12:32:08 +01002617 if (argc < 5) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002618 ti->error = "Not enough arguments";
2619 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
2621
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002622 key_size = get_key_size(&argv[1]);
2623 if (key_size < 0) {
2624 ti->error = "Cannot parse key size";
2625 return -EINVAL;
2626 }
Milan Broz5ebaee62010-08-12 04:14:07 +01002627
2628 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
2629 if (!cc) {
2630 ti->error = "Cannot allocate encryption context";
2631 return -ENOMEM;
2632 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00002633 cc->key_size = key_size;
Milan Broz8f0009a2017-03-16 15:39:44 +01002634 cc->sector_size = (1 << SECTOR_SHIFT);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002635 cc->sector_shift = 0;
Milan Broz5ebaee62010-08-12 04:14:07 +01002636
2637 ti->private = cc;
Milan Brozef43aa32017-01-04 20:23:54 +01002638
2639 /* Optional parameters need to be read before cipher constructor */
2640 if (argc > 5) {
2641 ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
2642 if (ret)
2643 goto bad;
2644 }
2645
Milan Broz5ebaee62010-08-12 04:14:07 +01002646 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
2647 if (ret < 0)
2648 goto bad;
2649
Milan Broz33d2f092017-03-16 15:39:40 +01002650 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +01002651 cc->dmreq_start = sizeof(struct aead_request);
2652 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
2653 align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
2654 } else {
2655 cc->dmreq_start = sizeof(struct skcipher_request);
2656 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
2657 align_mask = crypto_skcipher_alignmask(any_tfm(cc));
2658 }
Mikulas Patockad49ec522014-08-28 11:09:31 -04002659 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
2660
Milan Brozef43aa32017-01-04 20:23:54 +01002661 if (align_mask < CRYPTO_MINALIGN) {
Mikulas Patockad49ec522014-08-28 11:09:31 -04002662 /* Allocate the padding exactly */
2663 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
Milan Brozef43aa32017-01-04 20:23:54 +01002664 & align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002665 } else {
2666 /*
2667 * If the cipher requires greater alignment than kmalloc
2668 * alignment, we don't know the exact position of the
2669 * initialization vector. We must assume worst case.
2670 */
Milan Brozef43aa32017-01-04 20:23:54 +01002671 iv_size_padding = align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002672 }
Milan Brozddd42ed2008-02-08 02:11:07 +00002673
Mikulas Patocka94f5e022015-02-13 08:25:26 -05002674 ret = -ENOMEM;
Milan Brozef43aa32017-01-04 20:23:54 +01002675
2676 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
2677 additional_req_size = sizeof(struct dm_crypt_request) +
2678 iv_size_padding + cc->iv_size +
2679 cc->iv_size +
2680 sizeof(uint64_t) +
2681 sizeof(unsigned int);
2682
2683 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start + additional_req_size);
Milan Brozddd42ed2008-02-08 02:11:07 +00002684 if (!cc->req_pool) {
2685 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01002686 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00002687 }
Milan Brozddd42ed2008-02-08 02:11:07 +00002688
Mike Snitzer30187e12016-01-31 13:28:26 -05002689 cc->per_bio_data_size = ti->per_io_data_size =
Milan Brozef43aa32017-01-04 20:23:54 +01002690 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
Mikulas Patockad49ec522014-08-28 11:09:31 -04002691 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002692
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05002693 cc->page_pool = mempool_create_page_pool(BIO_MAX_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002695 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01002696 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 }
2698
Jens Axboebb799ca2008-12-10 15:35:05 +01002699 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07002700 if (!cc->bs) {
2701 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01002702 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07002703 }
2704
Mikulas Patocka7145c242015-02-13 08:24:41 -05002705 mutex_init(&cc->bio_alloc_lock);
2706
Milan Broz28513fc2010-08-12 04:14:06 +01002707 ret = -EINVAL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002708 if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
2709 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002710 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01002711 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08002713 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Vivek Goyale80d1c82015-07-31 09:20:36 -04002715 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
2716 if (ret) {
Milan Broz28513fc2010-08-12 04:14:06 +01002717 ti->error = "Device lookup failed";
2718 goto bad;
2719 }
2720
Vivek Goyale80d1c82015-07-31 09:20:36 -04002721 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002722 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002723 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01002724 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08002726 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Milan Broz33d2f092017-03-16 15:39:40 +01002728 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
Milan Brozef43aa32017-01-04 20:23:54 +01002729 ret = crypt_integrity_ctr(cc, ti);
Milan Broz772ae5f2011-08-02 12:32:08 +01002730 if (ret)
2731 goto bad;
2732
Milan Brozef43aa32017-01-04 20:23:54 +01002733 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
2734 if (!cc->tag_pool_max_sectors)
2735 cc->tag_pool_max_sectors = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01002736
Milan Brozef43aa32017-01-04 20:23:54 +01002737 cc->tag_pool = mempool_create_kmalloc_pool(MIN_IOS,
2738 cc->tag_pool_max_sectors * cc->on_disk_tag_size);
2739 if (!cc->tag_pool) {
2740 ti->error = "Cannot allocate integrity tags mempool";
2741 goto bad;
Milan Broz772ae5f2011-08-02 12:32:08 +01002742 }
Mikulas Patocka583fe742017-04-18 16:51:54 -04002743
2744 cc->tag_pool_max_sectors <<= cc->sector_shift;
Milan Broz772ae5f2011-08-02 12:32:08 +01002745 }
2746
Milan Broz28513fc2010-08-12 04:14:06 +01002747 ret = -ENOMEM;
Tejun Heo670368a2013-07-30 08:40:21 -04002748 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_MEM_RECLAIM, 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01002749 if (!cc->io_queue) {
2750 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01002751 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01002752 }
2753
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002754 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2755 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
2756 else
2757 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
2758 num_online_cpus());
Milan Brozcabf08e2007-10-19 22:38:58 +01002759 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01002760 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01002761 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01002762 }
2763
Mikulas Patockadc267622015-02-13 08:25:59 -05002764 init_waitqueue_head(&cc->write_thread_wait);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05002765 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05002766
2767 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write");
2768 if (IS_ERR(cc->write_thread)) {
2769 ret = PTR_ERR(cc->write_thread);
2770 cc->write_thread = NULL;
2771 ti->error = "Couldn't spawn write thread";
2772 goto bad;
2773 }
2774 wake_up_process(cc->write_thread);
2775
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002776 ti->num_flush_bios = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002777 ti->discard_zeroes_data_unsupported = true;
Milan Broz983c7db2011-09-25 23:26:21 +01002778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 return 0;
2780
Milan Broz28513fc2010-08-12 04:14:06 +01002781bad:
2782 crypt_dtr(ti);
2783 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002786static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002788 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01002789 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002790
Milan Broz772ae5f2011-08-02 12:32:08 +01002791 /*
Mike Christie28a8f0d2016-06-05 14:32:25 -05002792 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
2793 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
Mike Christiee6047142016-06-05 14:32:04 -05002794 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
Milan Broz772ae5f2011-08-02 12:32:08 +01002795 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06002796 if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
Mike Christie28a8f0d2016-06-05 14:32:25 -05002797 bio_op(bio) == REQ_OP_DISCARD)) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002798 bio->bi_bdev = cc->dev->bdev;
Milan Broz772ae5f2011-08-02 12:32:08 +01002799 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07002800 bio->bi_iter.bi_sector = cc->start +
2801 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002802 return DM_MAPIO_REMAPPED;
2803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002805 /*
2806 * Check if bio is too large, split as needed.
2807 */
2808 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
Milan Brozef43aa32017-01-04 20:23:54 +01002809 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002810 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
2811
Milan Broz8f0009a2017-03-16 15:39:44 +01002812 /*
2813 * Ensure that bio is a multiple of internal sector encryption size
2814 * and is aligned to this size as defined in IO hints.
2815 */
2816 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
2817 return -EIO;
2818
2819 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
2820 return -EIO;
2821
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002822 io = dm_per_bio_data(bio, cc->per_bio_data_size);
2823 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Milan Brozef43aa32017-01-04 20:23:54 +01002824
2825 if (cc->on_disk_tag_size) {
Mikulas Patocka583fe742017-04-18 16:51:54 -04002826 unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift);
Milan Brozef43aa32017-01-04 20:23:54 +01002827
2828 if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
Mikulas Patocka583fe742017-04-18 16:51:54 -04002829 unlikely(!(io->integrity_metadata = kmalloc(tag_len,
Milan Brozef43aa32017-01-04 20:23:54 +01002830 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
2831 if (bio_sectors(bio) > cc->tag_pool_max_sectors)
2832 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
2833 io->integrity_metadata = mempool_alloc(cc->tag_pool, GFP_NOIO);
2834 io->integrity_metadata_from_pool = true;
2835 }
2836 }
2837
Milan Broz33d2f092017-03-16 15:39:40 +01002838 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002839 io->ctx.r.req_aead = (struct aead_request *)(io + 1);
2840 else
2841 io->ctx.r.req = (struct skcipher_request *)(io + 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01002842
Milan Broz20c82532011-01-13 19:59:53 +00002843 if (bio_data_dir(io->base_bio) == READ) {
2844 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05002845 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00002846 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01002847 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002849 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
2851
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002852static void crypt_status(struct dm_target *ti, status_type_t type,
2853 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Milan Broz5ebaee62010-08-12 04:14:07 +01002855 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002856 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002857 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859 switch (type) {
2860 case STATUSTYPE_INFO:
2861 result[0] = '\0';
2862 break;
2863
2864 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00002865 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002867 if (cc->key_size > 0) {
2868 if (cc->key_string)
2869 DMEMIT(":%u:%s", cc->key_size, cc->key_string);
2870 else
2871 for (i = 0; i < cc->key_size; i++)
2872 DMEMIT("%02x", cc->key[i]);
2873 } else
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002874 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Andrew Morton4ee218c2006-03-27 01:17:48 -08002876 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
2877 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01002878
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002879 num_feature_args += !!ti->num_discard_bios;
2880 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002881 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Mikulas Patockaff3af922017-03-23 10:23:14 -04002882 num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT);
Milan Broz8f0009a2017-03-16 15:39:44 +01002883 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002884 if (cc->on_disk_tag_size)
2885 num_feature_args++;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002886 if (num_feature_args) {
2887 DMEMIT(" %d", num_feature_args);
2888 if (ti->num_discard_bios)
2889 DMEMIT(" allow_discards");
2890 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2891 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002892 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
2893 DMEMIT(" submit_from_crypt_cpus");
Milan Brozef43aa32017-01-04 20:23:54 +01002894 if (cc->on_disk_tag_size)
2895 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
Milan Broz8f0009a2017-03-16 15:39:44 +01002896 if (cc->sector_size != (1 << SECTOR_SHIFT))
2897 DMEMIT(" sector_size:%d", cc->sector_size);
2898 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
2899 DMEMIT(" iv_large_sectors");
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002900 }
Milan Broz772ae5f2011-08-02 12:32:08 +01002901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 break;
2903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904}
2905
Milan Broze48d4bb2006-10-03 01:15:37 -07002906static void crypt_postsuspend(struct dm_target *ti)
2907{
2908 struct crypt_config *cc = ti->private;
2909
2910 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2911}
2912
2913static int crypt_preresume(struct dm_target *ti)
2914{
2915 struct crypt_config *cc = ti->private;
2916
2917 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
2918 DMERR("aborting resume - crypt key is not set.");
2919 return -EAGAIN;
2920 }
2921
2922 return 0;
2923}
2924
2925static void crypt_resume(struct dm_target *ti)
2926{
2927 struct crypt_config *cc = ti->private;
2928
2929 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2930}
2931
2932/* Message interface
2933 * key set <key>
2934 * key wipe
2935 */
2936static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
2937{
2938 struct crypt_config *cc = ti->private;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002939 int key_size, ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07002940
2941 if (argc < 2)
2942 goto error;
2943
Mike Snitzer498f0102011-08-02 12:32:04 +01002944 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07002945 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
2946 DMWARN("not suspended during key manipulation.");
2947 return -EINVAL;
2948 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002949 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002950 /* The key size may not be changed. */
2951 key_size = get_key_size(&argv[2]);
2952 if (key_size < 0 || cc->key_size != key_size) {
2953 memset(argv[2], '0', strlen(argv[2]));
2954 return -EINVAL;
2955 }
2956
Milan Broz542da312009-12-10 23:51:57 +00002957 ret = crypt_set_key(cc, argv[2]);
2958 if (ret)
2959 return ret;
2960 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
2961 ret = cc->iv_gen_ops->init(cc);
2962 return ret;
2963 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002964 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00002965 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
2966 ret = cc->iv_gen_ops->wipe(cc);
2967 if (ret)
2968 return ret;
2969 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002970 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00002971 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002972 }
2973
2974error:
2975 DMWARN("unrecognised message received.");
2976 return -EINVAL;
2977}
2978
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002979static int crypt_iterate_devices(struct dm_target *ti,
2980 iterate_devices_callout_fn fn, void *data)
2981{
2982 struct crypt_config *cc = ti->private;
2983
Mike Snitzer5dea2712009-07-23 20:30:42 +01002984 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002985}
2986
Mike Snitzer586b2862015-09-09 21:34:51 -04002987static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
2988{
Milan Broz8f0009a2017-03-16 15:39:44 +01002989 struct crypt_config *cc = ti->private;
2990
Mike Snitzer586b2862015-09-09 21:34:51 -04002991 /*
2992 * Unfortunate constraint that is required to avoid the potential
2993 * for exceeding underlying device's max_segments limits -- due to
2994 * crypt_alloc_buffer() possibly allocating pages for the encryption
2995 * bio that are not as physically contiguous as the original bio.
2996 */
2997 limits->max_segment_size = PAGE_SIZE;
Milan Broz8f0009a2017-03-16 15:39:44 +01002998
2999 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
3000 limits->logical_block_size = cc->sector_size;
3001 limits->physical_block_size = cc->sector_size;
3002 blk_limits_io_min(limits, cc->sector_size);
3003 }
Mike Snitzer586b2862015-09-09 21:34:51 -04003004}
3005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006static struct target_type crypt_target = {
3007 .name = "crypt",
Milan Broz8f0009a2017-03-16 15:39:44 +01003008 .version = {1, 17, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 .module = THIS_MODULE,
3010 .ctr = crypt_ctr,
3011 .dtr = crypt_dtr,
3012 .map = crypt_map,
3013 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07003014 .postsuspend = crypt_postsuspend,
3015 .preresume = crypt_preresume,
3016 .resume = crypt_resume,
3017 .message = crypt_message,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003018 .iterate_devices = crypt_iterate_devices,
Mike Snitzer586b2862015-09-09 21:34:51 -04003019 .io_hints = crypt_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020};
3021
3022static int __init dm_crypt_init(void)
3023{
3024 int r;
3025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05003027 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07003028 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return r;
3031}
3032
3033static void __exit dm_crypt_exit(void)
3034{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00003035 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036}
3037
3038module_init(dm_crypt_init);
3039module_exit(dm_crypt_exit);
3040
Jana Saoutbf142992014-06-24 14:27:04 -04003041MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
3043MODULE_LICENSE("GPL");