blob: 05acc42bdd382f369c1e0c008c2aa94ce2fcdf34 [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;
Milan Broz8f0009a2017-03-16 15:39:44 +0100175 unsigned int sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100177 /* ESSIV: struct crypto_cipher *essiv_tfm */
178 void *iv_private;
Milan Brozef43aa32017-01-04 20:23:54 +0100179 union {
180 struct crypto_skcipher **tfms;
181 struct crypto_aead **tfms_aead;
182 } cipher_tfm;
Milan Brozd1f96422011-01-13 19:59:54 +0000183 unsigned tfms_count;
Milan Brozef43aa32017-01-04 20:23:54 +0100184 unsigned long cipher_flags;
Andi Kleenc0297722011-01-13 19:59:53 +0000185
186 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000187 * Layout of each crypto request:
188 *
Herbert Xubbdb23b2016-01-24 21:16:36 +0800189 * struct skcipher_request
Milan Brozddd42ed2008-02-08 02:11:07 +0000190 * context
191 * padding
192 * struct dm_crypt_request
193 * padding
194 * IV
195 *
196 * The padding is added so that dm_crypt_request and the IV are
197 * correctly aligned.
198 */
199 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000200
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400201 unsigned int per_bio_data_size;
202
Milan Broze48d4bb2006-10-03 01:15:37 -0700203 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100205 unsigned int key_parts; /* independent parts in key buffer */
206 unsigned int key_extra_size; /* additional keys length */
Milan Brozef43aa32017-01-04 20:23:54 +0100207 unsigned int key_mac_size; /* MAC key size for authenc(...) */
208
209 unsigned int integrity_tag_size;
210 unsigned int integrity_iv_size;
211 unsigned int on_disk_tag_size;
212
213 u8 *authenc_key; /* space for keys in authenc() format (if used) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 u8 key[0];
215};
216
Milan Brozef43aa32017-01-04 20:23:54 +0100217#define MIN_IOS 64
218#define MAX_TAG_SIZE 480
219#define POOL_ENTRY_SIZE 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100221static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000222static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Brozef43aa32017-01-04 20:23:54 +0100223static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
224 struct scatterlist *sg);
Olaf Kirch027581f2007-05-09 02:32:52 -0700225
Andi Kleenc0297722011-01-13 19:59:53 +0000226/*
227 * Use this to access cipher attributes that are the same for each CPU.
228 */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800229static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +0000230{
Milan Brozef43aa32017-01-04 20:23:54 +0100231 return cc->cipher_tfm.tfms[0];
232}
233
234static struct crypto_aead *any_tfm_aead(struct crypt_config *cc)
235{
236 return cc->cipher_tfm.tfms_aead[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * Different IV generation algorithms:
241 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000242 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200243 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 *
Milan Broz61afef62009-12-10 23:52:25 +0000245 * plain64: the initial vector is the 64-bit little-endian version of the sector
246 * number, padded with zeros if necessary.
247 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000248 * essiv: "encrypted sector|salt initial vector", the sector number is
249 * encrypted with the bulk cipher using a salt as key. The salt
250 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 *
Rik Snel48527fa2006-09-03 08:56:39 +1000252 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
253 * (needed for LRW-32-AES and possible other narrow block modes)
254 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700255 * null: the initial vector is always zero. Provides compatibility with
256 * obsolete loop_fish2 devices. Do not use for new devices.
257 *
Milan Broz34745782011-01-13 19:59:55 +0000258 * lmk: Compatible implementation of the block chaining mode used
259 * by the Loop-AES block device encryption system
260 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
261 * It operates on full 512 byte sectors and uses CBC
262 * with an IV derived from the sector number, the data and
263 * optionally extra IV seed.
264 * This means that after decryption the first block
265 * of sector must be tweaked according to decrypted data.
266 * Loop-AES can use three encryption schemes:
267 * version 1: is plain aes-cbc mode
268 * version 2: uses 64 multikey scheme with lmk IV generator
269 * version 3: the same as version 2 with additional IV seed
270 * (it uses 65 keys, last key is used as IV seed)
271 *
Milan Brozed04d982013-10-28 23:21:04 +0100272 * tcw: Compatible implementation of the block chaining mode used
273 * by the TrueCrypt device encryption system (prior to version 4.1).
Milan Broze44f23b2015-04-05 18:03:10 +0200274 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
Milan Brozed04d982013-10-28 23:21:04 +0100275 * It operates on full 512 byte sectors and uses CBC
276 * with an IV derived from initial key and the sector number.
277 * In addition, whitening value is applied on every sector, whitening
278 * is calculated from initial key, sector number and mixed using CRC32.
279 * Note that this encryption scheme is vulnerable to watermarking attacks
280 * and should be used for old compatible containers access only.
281 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * plumb: unimplemented, see:
283 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
284 */
285
Milan Broz2dc5327d32011-01-13 19:59:54 +0000286static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
287 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100290 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 return 0;
293}
294
Milan Broz61afef62009-12-10 23:52:25 +0000295static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc5327d32011-01-13 19:59:54 +0000296 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000297{
298 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100299 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000300
301 return 0;
302}
303
Milan Brozb95bf2d2009-12-10 23:51:56 +0000304/* Initialise ESSIV - compute salt but no local memory allocations */
305static int crypt_iv_essiv_init(struct crypt_config *cc)
306{
307 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800308 AHASH_REQUEST_ON_STACK(req, essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000309 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000310 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100311 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000312
313 sg_init_one(&sg, cc->key, cc->key_size);
Herbert Xubbdb23b2016-01-24 21:16:36 +0800314 ahash_request_set_tfm(req, essiv->hash_tfm);
315 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
316 ahash_request_set_crypt(req, &sg, essiv->salt, cc->key_size);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000317
Herbert Xubbdb23b2016-01-24 21:16:36 +0800318 err = crypto_ahash_digest(req);
319 ahash_request_zero(req);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000320 if (err)
321 return err;
322
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100323 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000324
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100325 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800326 crypto_ahash_digestsize(essiv->hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100327 if (err)
328 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000329
330 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000331}
332
Milan Broz542da312009-12-10 23:51:57 +0000333/* Wipe salt and reset key derived from volume key */
334static int crypt_iv_essiv_wipe(struct crypt_config *cc)
335{
336 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800337 unsigned salt_size = crypto_ahash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000338 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100339 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000340
341 memset(essiv->salt, 0, salt_size);
342
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100343 essiv_tfm = cc->iv_private;
344 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
345 if (r)
346 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000347
348 return err;
349}
350
351/* Set up per cpu cipher state */
352static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
353 struct dm_target *ti,
354 u8 *salt, unsigned saltsize)
355{
356 struct crypto_cipher *essiv_tfm;
357 int err;
358
359 /* Setup the essiv_tfm with the given salt */
360 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
361 if (IS_ERR(essiv_tfm)) {
362 ti->error = "Error allocating crypto tfm for ESSIV";
363 return essiv_tfm;
364 }
365
Milan Brozef43aa32017-01-04 20:23:54 +0100366 if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) {
Andi Kleenc0297722011-01-13 19:59:53 +0000367 ti->error = "Block size of ESSIV cipher does "
368 "not match IV size of block cipher";
369 crypto_free_cipher(essiv_tfm);
370 return ERR_PTR(-EINVAL);
371 }
372
373 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
374 if (err) {
375 ti->error = "Failed to set key for ESSIV cipher";
376 crypto_free_cipher(essiv_tfm);
377 return ERR_PTR(err);
378 }
379
380 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000381}
382
Milan Broz60473592009-12-10 23:51:55 +0000383static void crypt_iv_essiv_dtr(struct crypt_config *cc)
384{
Andi Kleenc0297722011-01-13 19:59:53 +0000385 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000386 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
387
Herbert Xubbdb23b2016-01-24 21:16:36 +0800388 crypto_free_ahash(essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000389 essiv->hash_tfm = NULL;
390
391 kzfree(essiv->salt);
392 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000393
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100394 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000395
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100396 if (essiv_tfm)
397 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000398
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100399 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000400}
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100403 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Milan Broz5861f1b2009-12-10 23:51:56 +0000405 struct crypto_cipher *essiv_tfm = NULL;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800406 struct crypto_ahash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000407 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100408 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Milan Broz5861f1b2009-12-10 23:51:56 +0000410 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700411 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return -EINVAL;
413 }
414
Milan Brozb95bf2d2009-12-10 23:51:56 +0000415 /* Allocate hash algorithm */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800416 hash_tfm = crypto_alloc_ahash(opts, 0, CRYPTO_ALG_ASYNC);
Herbert Xu35058682006-08-24 19:10:20 +1000417 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700418 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000419 err = PTR_ERR(hash_tfm);
420 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
Herbert Xubbdb23b2016-01-24 21:16:36 +0800423 salt = kzalloc(crypto_ahash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000424 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700425 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000426 err = -ENOMEM;
427 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
Milan Brozb95bf2d2009-12-10 23:51:56 +0000430 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000431 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
432
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100433 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800434 crypto_ahash_digestsize(hash_tfm));
Milan Brozef43aa32017-01-04 20:23:54 +0100435
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100436 if (IS_ERR(essiv_tfm)) {
437 crypt_iv_essiv_dtr(cc);
438 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000439 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100440 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000443
444bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000445 if (hash_tfm && !IS_ERR(hash_tfm))
Herbert Xubbdb23b2016-01-24 21:16:36 +0800446 crypto_free_ahash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000447 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000448 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Milan Broz2dc5327d32011-01-13 19:59:54 +0000451static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
452 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100454 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100457 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000458 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return 0;
461}
462
Rik Snel48527fa2006-09-03 08:56:39 +1000463static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
464 const char *opts)
465{
Herbert Xubbdb23b2016-01-24 21:16:36 +0800466 unsigned bs = crypto_skcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800467 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000468
469 /* we need to calculate how far we must shift the sector count
470 * to get the cipher block count, we use this shift in _gen */
471
472 if (1 << log != bs) {
473 ti->error = "cypher blocksize is not a power of 2";
474 return -EINVAL;
475 }
476
477 if (log > 9) {
478 ti->error = "cypher blocksize is > 512";
479 return -EINVAL;
480 }
481
Milan Broz60473592009-12-10 23:51:55 +0000482 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000483
484 return 0;
485}
486
487static void crypt_iv_benbi_dtr(struct crypt_config *cc)
488{
Rik Snel48527fa2006-09-03 08:56:39 +1000489}
490
Milan Broz2dc5327d32011-01-13 19:59:54 +0000491static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
492 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000493{
Herbert Xu79066ad2006-12-05 13:41:52 -0800494 __be64 val;
495
Rik Snel48527fa2006-09-03 08:56:39 +1000496 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800497
Milan Broz2dc5327d32011-01-13 19:59:54 +0000498 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800499 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return 0;
502}
503
Milan Broz2dc5327d32011-01-13 19:59:54 +0000504static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
505 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700506{
507 memset(iv, 0, cc->iv_size);
508
509 return 0;
510}
511
Milan Broz34745782011-01-13 19:59:55 +0000512static void crypt_iv_lmk_dtr(struct crypt_config *cc)
513{
514 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
515
516 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
517 crypto_free_shash(lmk->hash_tfm);
518 lmk->hash_tfm = NULL;
519
520 kzfree(lmk->seed);
521 lmk->seed = NULL;
522}
523
524static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
525 const char *opts)
526{
527 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
528
Milan Broz8f0009a2017-03-16 15:39:44 +0100529 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
530 ti->error = "Unsupported sector size for LMK";
531 return -EINVAL;
532 }
533
Milan Broz34745782011-01-13 19:59:55 +0000534 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
535 if (IS_ERR(lmk->hash_tfm)) {
536 ti->error = "Error initializing LMK hash";
537 return PTR_ERR(lmk->hash_tfm);
538 }
539
540 /* No seed in LMK version 2 */
541 if (cc->key_parts == cc->tfms_count) {
542 lmk->seed = NULL;
543 return 0;
544 }
545
546 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
547 if (!lmk->seed) {
548 crypt_iv_lmk_dtr(cc);
549 ti->error = "Error kmallocing seed storage in LMK";
550 return -ENOMEM;
551 }
552
553 return 0;
554}
555
556static int crypt_iv_lmk_init(struct crypt_config *cc)
557{
558 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
559 int subkey_size = cc->key_size / cc->key_parts;
560
561 /* LMK seed is on the position of LMK_KEYS + 1 key */
562 if (lmk->seed)
563 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
564 crypto_shash_digestsize(lmk->hash_tfm));
565
566 return 0;
567}
568
569static int crypt_iv_lmk_wipe(struct crypt_config *cc)
570{
571 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
572
573 if (lmk->seed)
574 memset(lmk->seed, 0, LMK_SEED_SIZE);
575
576 return 0;
577}
578
579static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
580 struct dm_crypt_request *dmreq,
581 u8 *data)
582{
583 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200584 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000585 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100586 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000587 int i, r;
588
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200589 desc->tfm = lmk->hash_tfm;
590 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Broz34745782011-01-13 19:59:55 +0000591
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200592 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000593 if (r)
594 return r;
595
596 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200597 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000598 if (r)
599 return r;
600 }
601
602 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200603 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000604 if (r)
605 return r;
606
607 /* Sector is cropped to 56 bits here */
608 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
609 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
610 buf[2] = cpu_to_le32(4024);
611 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200612 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000613 if (r)
614 return r;
615
616 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200617 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000618 if (r)
619 return r;
620
621 for (i = 0; i < MD5_HASH_WORDS; i++)
622 __cpu_to_le32s(&md5state.hash[i]);
623 memcpy(iv, &md5state.hash, cc->iv_size);
624
625 return 0;
626}
627
628static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
629 struct dm_crypt_request *dmreq)
630{
Milan Brozef43aa32017-01-04 20:23:54 +0100631 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000632 u8 *src;
633 int r = 0;
634
635 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100636 sg = crypt_get_sg_data(cc, dmreq->sg_in);
637 src = kmap_atomic(sg_page(sg));
638 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800639 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000640 } else
641 memset(iv, 0, cc->iv_size);
642
643 return r;
644}
645
646static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
647 struct dm_crypt_request *dmreq)
648{
Milan Brozef43aa32017-01-04 20:23:54 +0100649 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000650 u8 *dst;
651 int r;
652
653 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
654 return 0;
655
Milan Brozef43aa32017-01-04 20:23:54 +0100656 sg = crypt_get_sg_data(cc, dmreq->sg_out);
657 dst = kmap_atomic(sg_page(sg));
658 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset);
Milan Broz34745782011-01-13 19:59:55 +0000659
660 /* Tweak the first block of plaintext sector */
661 if (!r)
Milan Brozef43aa32017-01-04 20:23:54 +0100662 crypto_xor(dst + sg->offset, iv, cc->iv_size);
Milan Broz34745782011-01-13 19:59:55 +0000663
Cong Wangc2e022c2011-11-28 13:26:02 +0800664 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000665 return r;
666}
667
Milan Brozed04d982013-10-28 23:21:04 +0100668static void crypt_iv_tcw_dtr(struct crypt_config *cc)
669{
670 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
671
672 kzfree(tcw->iv_seed);
673 tcw->iv_seed = NULL;
674 kzfree(tcw->whitening);
675 tcw->whitening = NULL;
676
677 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
678 crypto_free_shash(tcw->crc32_tfm);
679 tcw->crc32_tfm = NULL;
680}
681
682static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
683 const char *opts)
684{
685 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
686
Milan Broz8f0009a2017-03-16 15:39:44 +0100687 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
688 ti->error = "Unsupported sector size for TCW";
689 return -EINVAL;
690 }
691
Milan Brozed04d982013-10-28 23:21:04 +0100692 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
693 ti->error = "Wrong key size for TCW";
694 return -EINVAL;
695 }
696
697 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
698 if (IS_ERR(tcw->crc32_tfm)) {
699 ti->error = "Error initializing CRC32 in TCW";
700 return PTR_ERR(tcw->crc32_tfm);
701 }
702
703 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
704 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
705 if (!tcw->iv_seed || !tcw->whitening) {
706 crypt_iv_tcw_dtr(cc);
707 ti->error = "Error allocating seed storage in TCW";
708 return -ENOMEM;
709 }
710
711 return 0;
712}
713
714static int crypt_iv_tcw_init(struct crypt_config *cc)
715{
716 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
717 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
718
719 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
720 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
721 TCW_WHITENING_SIZE);
722
723 return 0;
724}
725
726static int crypt_iv_tcw_wipe(struct crypt_config *cc)
727{
728 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
729
730 memset(tcw->iv_seed, 0, cc->iv_size);
731 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
732
733 return 0;
734}
735
736static int crypt_iv_tcw_whitening(struct crypt_config *cc,
737 struct dm_crypt_request *dmreq,
738 u8 *data)
739{
740 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200741 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100742 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200743 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100744 int i, r;
745
746 /* xor whitening with sector number */
747 memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
748 crypto_xor(buf, (u8 *)&sector, 8);
749 crypto_xor(&buf[8], (u8 *)&sector, 8);
750
751 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200752 desc->tfm = tcw->crc32_tfm;
753 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Brozed04d982013-10-28 23:21:04 +0100754 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200755 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100756 if (r)
757 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200758 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100759 if (r)
760 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200761 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100762 if (r)
763 goto out;
764 }
765 crypto_xor(&buf[0], &buf[12], 4);
766 crypto_xor(&buf[4], &buf[8], 4);
767
768 /* apply whitening (8 bytes) to whole sector */
769 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
770 crypto_xor(data + i * 8, buf, 8);
771out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100772 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100773 return r;
774}
775
776static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
777 struct dm_crypt_request *dmreq)
778{
Milan Brozef43aa32017-01-04 20:23:54 +0100779 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100780 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200781 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100782 u8 *src;
783 int r = 0;
784
785 /* Remove whitening from ciphertext */
786 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100787 sg = crypt_get_sg_data(cc, dmreq->sg_in);
788 src = kmap_atomic(sg_page(sg));
789 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100790 kunmap_atomic(src);
791 }
792
793 /* Calculate IV */
794 memcpy(iv, tcw->iv_seed, cc->iv_size);
795 crypto_xor(iv, (u8 *)&sector, 8);
796 if (cc->iv_size > 8)
797 crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
798
799 return r;
800}
801
802static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
803 struct dm_crypt_request *dmreq)
804{
Milan Brozef43aa32017-01-04 20:23:54 +0100805 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100806 u8 *dst;
807 int r;
808
809 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
810 return 0;
811
812 /* Apply whitening on ciphertext */
Milan Brozef43aa32017-01-04 20:23:54 +0100813 sg = crypt_get_sg_data(cc, dmreq->sg_out);
814 dst = kmap_atomic(sg_page(sg));
815 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100816 kunmap_atomic(dst);
817
818 return r;
819}
820
Milan Brozef43aa32017-01-04 20:23:54 +0100821static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv,
822 struct dm_crypt_request *dmreq)
823{
824 /* Used only for writes, there must be an additional space to store IV */
825 get_random_bytes(iv, cc->iv_size);
826 return 0;
827}
828
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100829static const struct crypt_iv_operations crypt_iv_plain_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 .generator = crypt_iv_plain_gen
831};
832
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100833static const struct crypt_iv_operations crypt_iv_plain64_ops = {
Milan Broz61afef62009-12-10 23:52:25 +0000834 .generator = crypt_iv_plain64_gen
835};
836
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100837static const struct crypt_iv_operations crypt_iv_essiv_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 .ctr = crypt_iv_essiv_ctr,
839 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000840 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000841 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 .generator = crypt_iv_essiv_gen
843};
844
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100845static const struct crypt_iv_operations crypt_iv_benbi_ops = {
Rik Snel48527fa2006-09-03 08:56:39 +1000846 .ctr = crypt_iv_benbi_ctr,
847 .dtr = crypt_iv_benbi_dtr,
848 .generator = crypt_iv_benbi_gen
849};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100851static const struct crypt_iv_operations crypt_iv_null_ops = {
Ludwig Nussel46b47732007-05-09 02:32:55 -0700852 .generator = crypt_iv_null_gen
853};
854
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100855static const struct crypt_iv_operations crypt_iv_lmk_ops = {
Milan Broz34745782011-01-13 19:59:55 +0000856 .ctr = crypt_iv_lmk_ctr,
857 .dtr = crypt_iv_lmk_dtr,
858 .init = crypt_iv_lmk_init,
859 .wipe = crypt_iv_lmk_wipe,
860 .generator = crypt_iv_lmk_gen,
861 .post = crypt_iv_lmk_post
862};
863
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100864static const struct crypt_iv_operations crypt_iv_tcw_ops = {
Milan Brozed04d982013-10-28 23:21:04 +0100865 .ctr = crypt_iv_tcw_ctr,
866 .dtr = crypt_iv_tcw_dtr,
867 .init = crypt_iv_tcw_init,
868 .wipe = crypt_iv_tcw_wipe,
869 .generator = crypt_iv_tcw_gen,
870 .post = crypt_iv_tcw_post
871};
872
Milan Brozef43aa32017-01-04 20:23:54 +0100873static struct crypt_iv_operations crypt_iv_random_ops = {
874 .generator = crypt_iv_random_gen
875};
876
877/*
878 * Integrity extensions
879 */
880static bool crypt_integrity_aead(struct crypt_config *cc)
881{
882 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
883}
884
885static bool crypt_integrity_hmac(struct crypt_config *cc)
886{
Milan Broz33d2f092017-03-16 15:39:40 +0100887 return crypt_integrity_aead(cc) && cc->key_mac_size;
Milan Brozef43aa32017-01-04 20:23:54 +0100888}
889
890/* Get sg containing data */
891static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
892 struct scatterlist *sg)
893{
Milan Broz33d2f092017-03-16 15:39:40 +0100894 if (unlikely(crypt_integrity_aead(cc)))
Milan Brozef43aa32017-01-04 20:23:54 +0100895 return &sg[2];
896
897 return sg;
898}
899
900static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
901{
902 struct bio_integrity_payload *bip;
903 unsigned int tag_len;
904 int ret;
905
906 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size)
907 return 0;
908
909 bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
910 if (IS_ERR(bip))
911 return PTR_ERR(bip);
912
913 tag_len = io->cc->on_disk_tag_size * bio_sectors(bio);
914
915 bip->bip_iter.bi_size = tag_len;
916 bip->bip_iter.bi_sector = io->cc->start + io->sector;
917
918 /* We own the metadata, do not let bio_free to release it */
919 bip->bip_flags &= ~BIP_BLOCK_INTEGRITY;
920
921 ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
922 tag_len, offset_in_page(io->integrity_metadata));
923 if (unlikely(ret != tag_len))
924 return -ENOMEM;
925
926 return 0;
927}
928
929static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
930{
931#ifdef CONFIG_BLK_DEV_INTEGRITY
932 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk);
933
934 /* From now we require underlying device with our integrity profile */
935 if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) {
936 ti->error = "Integrity profile not supported.";
937 return -EINVAL;
938 }
939
940 if (bi->tag_size != cc->on_disk_tag_size) {
941 ti->error = "Integrity profile tag size mismatch.";
942 return -EINVAL;
943 }
944
Milan Broz33d2f092017-03-16 15:39:40 +0100945 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +0100946 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size;
947 DMINFO("Integrity AEAD, tag size %u, IV size %u.",
948 cc->integrity_tag_size, cc->integrity_iv_size);
949
950 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) {
951 ti->error = "Integrity AEAD auth tag size is not supported.";
952 return -EINVAL;
953 }
954 } else if (cc->integrity_iv_size)
955 DMINFO("Additional per-sector space %u bytes for IV.",
956 cc->integrity_iv_size);
957
958 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) {
959 ti->error = "Not enough space for integrity tag in the profile.";
960 return -EINVAL;
961 }
962
963 return 0;
964#else
965 ti->error = "Integrity profile not supported.";
966 return -EINVAL;
967#endif
968}
969
Milan Brozd469f842007-10-19 22:42:37 +0100970static void crypt_convert_init(struct crypt_config *cc,
971 struct convert_context *ctx,
972 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000973 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 ctx->bio_in = bio_in;
976 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700977 if (bio_in)
978 ctx->iter_in = bio_in->bi_iter;
979 if (bio_out)
980 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100981 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000982 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
984
Huang Yingb2174ee2009-03-16 17:44:33 +0000985static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
Milan Brozef43aa32017-01-04 20:23:54 +0100986 void *req)
Huang Yingb2174ee2009-03-16 17:44:33 +0000987{
988 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
989}
990
Milan Brozef43aa32017-01-04 20:23:54 +0100991static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq)
Huang Yingb2174ee2009-03-16 17:44:33 +0000992{
Milan Brozef43aa32017-01-04 20:23:54 +0100993 return (void *)((char *)dmreq - cc->dmreq_start);
Huang Yingb2174ee2009-03-16 17:44:33 +0000994}
995
Milan Broz2dc5327d32011-01-13 19:59:54 +0000996static u8 *iv_of_dmreq(struct crypt_config *cc,
997 struct dm_crypt_request *dmreq)
998{
Milan Broz33d2f092017-03-16 15:39:40 +0100999 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001000 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1001 crypto_aead_alignmask(any_tfm_aead(cc)) + 1);
1002 else
1003 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
1004 crypto_skcipher_alignmask(any_tfm(cc)) + 1);
Milan Broz2dc5327d32011-01-13 19:59:54 +00001005}
1006
Milan Brozef43aa32017-01-04 20:23:54 +01001007static u8 *org_iv_of_dmreq(struct crypt_config *cc,
1008 struct dm_crypt_request *dmreq)
1009{
1010 return iv_of_dmreq(cc, dmreq) + cc->iv_size;
1011}
1012
1013static uint64_t *org_sector_of_dmreq(struct crypt_config *cc,
1014 struct dm_crypt_request *dmreq)
1015{
1016 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size;
1017 return (uint64_t*) ptr;
1018}
1019
1020static unsigned int *org_tag_of_dmreq(struct crypt_config *cc,
1021 struct dm_crypt_request *dmreq)
1022{
1023 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size +
1024 cc->iv_size + sizeof(uint64_t);
1025 return (unsigned int*)ptr;
1026}
1027
1028static void *tag_from_dmreq(struct crypt_config *cc,
1029 struct dm_crypt_request *dmreq)
1030{
1031 struct convert_context *ctx = dmreq->ctx;
1032 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1033
1034 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) *
1035 cc->on_disk_tag_size];
1036}
1037
1038static void *iv_tag_from_dmreq(struct crypt_config *cc,
1039 struct dm_crypt_request *dmreq)
1040{
1041 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size;
1042}
1043
1044static int crypt_convert_block_aead(struct crypt_config *cc,
1045 struct convert_context *ctx,
1046 struct aead_request *req,
1047 unsigned int tag_offset)
Milan Broz01482b72008-02-08 02:11:04 +00001048{
Kent Overstreet003b5c52013-10-11 15:45:43 -07001049 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1050 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001051 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001052 u8 *iv, *org_iv, *tag_iv, *tag;
1053 uint64_t *sector;
1054 int r = 0;
1055
1056 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size);
Milan Broz01482b72008-02-08 02:11:04 +00001057
Milan Broz8f0009a2017-03-16 15:39:44 +01001058 /* Reject unexpected unaligned bio. */
1059 if (unlikely(bv_in.bv_offset & (cc->sector_size - 1)))
1060 return -EIO;
1061
Huang Yingb2174ee2009-03-16 17:44:33 +00001062 dmreq = dmreq_of_req(cc, req);
Mikulas Patockac66029f2012-07-27 15:08:05 +01001063 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001064 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
1065 sector_div(dmreq->iv_sector, cc->sector_size >> SECTOR_SHIFT);
Huang Yingb2174ee2009-03-16 17:44:33 +00001066 dmreq->ctx = ctx;
Milan Broz01482b72008-02-08 02:11:04 +00001067
Milan Brozef43aa32017-01-04 20:23:54 +01001068 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
Milan Broz01482b72008-02-08 02:11:04 +00001069
Milan Brozef43aa32017-01-04 20:23:54 +01001070 sector = org_sector_of_dmreq(cc, dmreq);
1071 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1072
1073 iv = iv_of_dmreq(cc, dmreq);
1074 org_iv = org_iv_of_dmreq(cc, dmreq);
1075 tag = tag_from_dmreq(cc, dmreq);
1076 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1077
1078 /* AEAD request:
1079 * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
1080 * | (authenticated) | (auth+encryption) | |
1081 * | sector_LE | IV | sector in/out | tag in/out |
1082 */
1083 sg_init_table(dmreq->sg_in, 4);
1084 sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t));
1085 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001086 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 +01001087 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size);
1088
1089 sg_init_table(dmreq->sg_out, 4);
1090 sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t));
1091 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size);
Milan Broz8f0009a2017-03-16 15:39:44 +01001092 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 +01001093 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size);
Milan Broz01482b72008-02-08 02:11:04 +00001094
Milan Broz3a7f6c92008-02-08 02:11:14 +00001095 if (cc->iv_gen_ops) {
Milan Brozef43aa32017-01-04 20:23:54 +01001096 /* For READs use IV stored in integrity metadata */
1097 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1098 memcpy(org_iv, tag_iv, cc->iv_size);
1099 } else {
1100 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1101 if (r < 0)
1102 return r;
1103 /* Store generated IV in integrity metadata */
1104 if (cc->integrity_iv_size)
1105 memcpy(tag_iv, org_iv, cc->iv_size);
1106 }
1107 /* Working copy of IV, to be modified in crypto API */
1108 memcpy(iv, org_iv, cc->iv_size);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001109 }
1110
Milan Brozef43aa32017-01-04 20:23:54 +01001111 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size);
1112 if (bio_data_dir(ctx->bio_in) == WRITE) {
1113 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001114 cc->sector_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001115 r = crypto_aead_encrypt(req);
1116 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size)
1117 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0,
1118 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size));
1119 } else {
1120 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
Milan Broz8f0009a2017-03-16 15:39:44 +01001121 cc->sector_size + cc->integrity_tag_size, iv);
Milan Brozef43aa32017-01-04 20:23:54 +01001122 r = crypto_aead_decrypt(req);
1123 }
1124
1125 if (r == -EBADMSG)
1126 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1127 (unsigned long long)le64_to_cpu(*sector));
1128
1129 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
1130 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1131
Milan Broz8f0009a2017-03-16 15:39:44 +01001132 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1133 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001134
1135 return r;
1136}
1137
1138static int crypt_convert_block_skcipher(struct crypt_config *cc,
1139 struct convert_context *ctx,
1140 struct skcipher_request *req,
1141 unsigned int tag_offset)
1142{
1143 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1144 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
1145 struct scatterlist *sg_in, *sg_out;
1146 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001147 u8 *iv, *org_iv, *tag_iv;
1148 uint64_t *sector;
1149 int r = 0;
1150
Milan Broz8f0009a2017-03-16 15:39:44 +01001151 /* Reject unexpected unaligned bio. */
1152 if (unlikely(bv_in.bv_offset & (cc->sector_size - 1)))
1153 return -EIO;
1154
Milan Brozef43aa32017-01-04 20:23:54 +01001155 dmreq = dmreq_of_req(cc, req);
1156 dmreq->iv_sector = ctx->cc_sector;
Milan Broz8f0009a2017-03-16 15:39:44 +01001157 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
1158 sector_div(dmreq->iv_sector, cc->sector_size >> SECTOR_SHIFT);
Milan Brozef43aa32017-01-04 20:23:54 +01001159 dmreq->ctx = ctx;
1160
1161 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
1162
1163 iv = iv_of_dmreq(cc, dmreq);
1164 org_iv = org_iv_of_dmreq(cc, dmreq);
1165 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1166
1167 sector = org_sector_of_dmreq(cc, dmreq);
1168 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1169
1170 /* For skcipher we use only the first sg item */
1171 sg_in = &dmreq->sg_in[0];
1172 sg_out = &dmreq->sg_out[0];
1173
1174 sg_init_table(sg_in, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001175 sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001176
1177 sg_init_table(sg_out, 1);
Milan Broz8f0009a2017-03-16 15:39:44 +01001178 sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset);
Milan Brozef43aa32017-01-04 20:23:54 +01001179
1180 if (cc->iv_gen_ops) {
1181 /* For READs use IV stored in integrity metadata */
1182 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1183 memcpy(org_iv, tag_iv, cc->integrity_iv_size);
1184 } else {
1185 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1186 if (r < 0)
1187 return r;
1188 /* Store generated IV in integrity metadata */
1189 if (cc->integrity_iv_size)
1190 memcpy(tag_iv, org_iv, cc->integrity_iv_size);
1191 }
1192 /* Working copy of IV, to be modified in crypto API */
1193 memcpy(iv, org_iv, cc->iv_size);
1194 }
1195
Milan Broz8f0009a2017-03-16 15:39:44 +01001196 skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001197
1198 if (bio_data_dir(ctx->bio_in) == WRITE)
Herbert Xubbdb23b2016-01-24 21:16:36 +08001199 r = crypto_skcipher_encrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001200 else
Herbert Xubbdb23b2016-01-24 21:16:36 +08001201 r = crypto_skcipher_decrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001202
Milan Broz2dc5327d32011-01-13 19:59:54 +00001203 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001204 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1205
Milan Broz8f0009a2017-03-16 15:39:44 +01001206 bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size);
1207 bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size);
Milan Broz2dc5327d32011-01-13 19:59:54 +00001208
Milan Broz3a7f6c92008-02-08 02:11:14 +00001209 return r;
Milan Broz01482b72008-02-08 02:11:04 +00001210}
1211
Milan Broz95497a92008-02-08 02:11:12 +00001212static void kcryptd_async_done(struct crypto_async_request *async_req,
1213 int error);
Andi Kleenc0297722011-01-13 19:59:53 +00001214
Milan Brozef43aa32017-01-04 20:23:54 +01001215static void crypt_alloc_req_skcipher(struct crypt_config *cc,
1216 struct convert_context *ctx)
Milan Brozddd42ed2008-02-08 02:11:07 +00001217{
Mikulas Patockac66029f2012-07-27 15:08:05 +01001218 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +00001219
Milan Brozef43aa32017-01-04 20:23:54 +01001220 if (!ctx->r.req)
1221 ctx->r.req = mempool_alloc(cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +00001222
Milan Brozef43aa32017-01-04 20:23:54 +01001223 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]);
Milan Broz54cea3f2015-05-15 17:00:25 +02001224
1225 /*
1226 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1227 * requests if driver request queue is full.
1228 */
Milan Brozef43aa32017-01-04 20:23:54 +01001229 skcipher_request_set_callback(ctx->r.req,
Andi Kleenc0297722011-01-13 19:59:53 +00001230 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Milan Brozef43aa32017-01-04 20:23:54 +01001231 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req));
Milan Brozddd42ed2008-02-08 02:11:07 +00001232}
1233
Milan Brozef43aa32017-01-04 20:23:54 +01001234static void crypt_alloc_req_aead(struct crypt_config *cc,
1235 struct convert_context *ctx)
1236{
1237 if (!ctx->r.req_aead)
1238 ctx->r.req_aead = mempool_alloc(cc->req_pool, GFP_NOIO);
1239
1240 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]);
1241
1242 /*
1243 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1244 * requests if driver request queue is full.
1245 */
1246 aead_request_set_callback(ctx->r.req_aead,
1247 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
1248 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead));
1249}
1250
1251static void crypt_alloc_req(struct crypt_config *cc,
1252 struct convert_context *ctx)
1253{
Milan Broz33d2f092017-03-16 15:39:40 +01001254 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001255 crypt_alloc_req_aead(cc, ctx);
1256 else
1257 crypt_alloc_req_skcipher(cc, ctx);
1258}
1259
1260static void crypt_free_req_skcipher(struct crypt_config *cc,
1261 struct skcipher_request *req, struct bio *base_bio)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001262{
1263 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1264
Herbert Xubbdb23b2016-01-24 21:16:36 +08001265 if ((struct skcipher_request *)(io + 1) != req)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001266 mempool_free(req, cc->req_pool);
1267}
1268
Milan Brozef43aa32017-01-04 20:23:54 +01001269static void crypt_free_req_aead(struct crypt_config *cc,
1270 struct aead_request *req, struct bio *base_bio)
1271{
1272 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1273
1274 if ((struct aead_request *)(io + 1) != req)
1275 mempool_free(req, cc->req_pool);
1276}
1277
1278static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio)
1279{
Milan Broz33d2f092017-03-16 15:39:40 +01001280 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001281 crypt_free_req_aead(cc, req, base_bio);
1282 else
1283 crypt_free_req_skcipher(cc, req, base_bio);
1284}
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286/*
1287 * Encrypt / decrypt data from one bio to another one (can be the same one)
1288 */
1289static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +01001290 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291{
Milan Brozef43aa32017-01-04 20:23:54 +01001292 unsigned int tag_offset = 0;
Milan Broz8f0009a2017-03-16 15:39:44 +01001293 unsigned int sector_step = cc->sector_size / (1 << SECTOR_SHIFT);
Milan Broz3f1e9072008-03-28 14:16:07 -07001294 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Mikulas Patocka40b62292012-07-27 15:08:04 +01001296 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +01001297
Kent Overstreet003b5c52013-10-11 15:45:43 -07001298 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Milan Broz3a7f6c92008-02-08 02:11:14 +00001300 crypt_alloc_req(cc, ctx);
Mikulas Patocka40b62292012-07-27 15:08:04 +01001301 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -07001302
Milan Broz33d2f092017-03-16 15:39:40 +01001303 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001304 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset);
1305 else
1306 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001307
1308 switch (r) {
Milan Broz54cea3f2015-05-15 17:00:25 +02001309 /*
1310 * The request was queued by a crypto driver
1311 * but the driver request queue is full, let's wait.
1312 */
Milan Broz3a7f6c92008-02-08 02:11:14 +00001313 case -EBUSY:
1314 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -08001315 reinit_completion(&ctx->restart);
Milan Broz54cea3f2015-05-15 17:00:25 +02001316 /* fall through */
1317 /*
1318 * The request is queued and processed asynchronously,
1319 * completion function kcryptd_async_done() will be called.
1320 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001321 case -EINPROGRESS:
Milan Brozef43aa32017-01-04 20:23:54 +01001322 ctx->r.req = NULL;
Milan Broz8f0009a2017-03-16 15:39:44 +01001323 ctx->cc_sector += sector_step;
1324 tag_offset += sector_step;
Milan Broz3a7f6c92008-02-08 02:11:14 +00001325 continue;
Milan Broz54cea3f2015-05-15 17:00:25 +02001326 /*
1327 * The request was already processed (synchronously).
1328 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001329 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001330 atomic_dec(&ctx->cc_pending);
Milan Broz8f0009a2017-03-16 15:39:44 +01001331 ctx->cc_sector += sector_step;
1332 tag_offset += sector_step;
Milan Brozc7f1b202008-07-02 09:34:28 +01001333 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -07001334 continue;
Milan Brozef43aa32017-01-04 20:23:54 +01001335 /*
1336 * There was a data integrity error.
1337 */
1338 case -EBADMSG:
1339 atomic_dec(&ctx->cc_pending);
1340 return -EILSEQ;
1341 /*
1342 * There was an error while processing the request.
1343 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001344 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001345 atomic_dec(&ctx->cc_pending);
Milan Brozef43aa32017-01-04 20:23:54 +01001346 return -EIO;
Milan Broz3f1e9072008-03-28 14:16:07 -07001347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
1349
Milan Broz3f1e9072008-03-28 14:16:07 -07001350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001353static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355/*
1356 * Generate a new unfragmented bio with the given size
Mike Snitzer586b2862015-09-09 21:34:51 -04001357 * This should never violate the device limitations (but only because
1358 * max_segment_size is being constrained to PAGE_SIZE).
Mikulas Patocka7145c242015-02-13 08:24:41 -05001359 *
1360 * This function may be called concurrently. If we allocate from the mempool
1361 * concurrently, there is a possibility of deadlock. For example, if we have
1362 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1363 * the mempool concurrently, it may deadlock in a situation where both processes
1364 * have allocated 128 pages and the mempool is exhausted.
1365 *
1366 * In order to avoid this scenario we allocate the pages under a mutex.
1367 *
1368 * In order to not degrade performance with excessive locking, we try
1369 * non-blocking allocations without a mutex first but on failure we fallback
1370 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001372static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001374 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001375 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001377 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
1378 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +00001379 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Mikulas Patocka7145c242015-02-13 08:24:41 -05001381retry:
Mel Gormand0164ad2015-11-06 16:28:21 -08001382 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001383 mutex_lock(&cc->bio_alloc_lock);
1384
Olaf Kirch2f9941b2007-05-09 02:32:53 -07001385 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -07001386 if (!clone)
Milan Brozef43aa32017-01-04 20:23:54 +01001387 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Olaf Kirch027581f2007-05-09 02:32:52 -07001389 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -07001390
Mikulas Patocka7145c242015-02-13 08:24:41 -05001391 remaining_size = size;
1392
Olaf Kirchf97380b2007-05-09 02:32:54 -07001393 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +00001394 page = mempool_alloc(cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -05001395 if (!page) {
1396 crypt_free_buffer_pages(cc, clone);
1397 bio_put(clone);
Mel Gormand0164ad2015-11-06 16:28:21 -08001398 gfp_mask |= __GFP_DIRECT_RECLAIM;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001399 goto retry;
1400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Mikulas Patocka7145c242015-02-13 08:24:41 -05001402 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Ming Lei0dae7fe2016-10-29 16:08:06 +08001404 bio_add_page(clone, page, len, 0);
Milan Broz91e10622007-12-13 14:16:10 +00001405
Mikulas Patocka7145c242015-02-13 08:24:41 -05001406 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408
Milan Brozef43aa32017-01-04 20:23:54 +01001409 /* Allocate space for integrity tags */
1410 if (dm_crypt_integrity_io_alloc(io, clone)) {
1411 crypt_free_buffer_pages(cc, clone);
1412 bio_put(clone);
1413 clone = NULL;
1414 }
1415out:
Mel Gormand0164ad2015-11-06 16:28:21 -08001416 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001417 mutex_unlock(&cc->bio_alloc_lock);
1418
Milan Broz8b004452006-10-03 01:15:37 -07001419 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420}
1421
Neil Brown644bd2f2007-10-16 13:48:46 +02001422static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Neil Brown644bd2f2007-10-16 13:48:46 +02001424 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 struct bio_vec *bv;
1426
Kent Overstreetcb34e052012-09-05 15:22:02 -07001427 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 BUG_ON(!bv->bv_page);
1429 mempool_free(bv->bv_page, cc->page_pool);
1430 bv->bv_page = NULL;
1431 }
1432}
1433
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001434static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1435 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001436{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001437 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001438 io->base_bio = bio;
1439 io->sector = sector;
1440 io->error = 0;
Milan Brozef43aa32017-01-04 20:23:54 +01001441 io->ctx.r.req = NULL;
1442 io->integrity_metadata = NULL;
1443 io->integrity_metadata_from_pool = false;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001444 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001445}
1446
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001447static void crypt_inc_pending(struct dm_crypt_io *io)
1448{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001449 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001450}
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452/*
1453 * One of the bios was finished. Check for completion of
1454 * the whole request and correctly clean up the buffer.
1455 */
Milan Broz5742fd72008-02-08 02:10:43 +00001456static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001458 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001459 struct bio *base_bio = io->base_bio;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001460 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Mikulas Patocka40b62292012-07-27 15:08:04 +01001462 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return;
1464
Milan Brozef43aa32017-01-04 20:23:54 +01001465 if (io->ctx.r.req)
1466 crypt_free_req(cc, io->ctx.r.req, base_bio);
1467
1468 if (unlikely(io->integrity_metadata_from_pool))
1469 mempool_free(io->integrity_metadata, io->cc->tag_pool);
1470 else
1471 kfree(io->integrity_metadata);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001472
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001473 base_bio->bi_error = error;
1474 bio_endio(base_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
1477/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001478 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 *
1480 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001481 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001482 *
1483 * kcryptd performs the actual encryption or decryption.
1484 *
1485 * kcryptd_io performs the IO submission.
1486 *
1487 * They must be separated as otherwise the final stages could be
1488 * starved by new requests which can block in the first stages due
1489 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001490 *
1491 * The work is done per CPU global for all dm-crypt instances.
1492 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001494static void crypt_endio(struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001495{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001496 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001497 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001498 unsigned rw = bio_data_dir(clone);
Sasha Levin9b81c842015-08-10 19:05:18 -04001499 int error;
Milan Broz8b004452006-10-03 01:15:37 -07001500
1501 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001502 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001503 */
Milan Brozee7a4912008-02-08 02:10:46 +00001504 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001505 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001506
Sasha Levin9b81c842015-08-10 19:05:18 -04001507 error = clone->bi_error;
Milan Brozee7a4912008-02-08 02:10:46 +00001508 bio_put(clone);
1509
Sasha Levin9b81c842015-08-10 19:05:18 -04001510 if (rw == READ && !error) {
Milan Brozee7a4912008-02-08 02:10:46 +00001511 kcryptd_queue_crypt(io);
1512 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001513 }
Milan Broz8b004452006-10-03 01:15:37 -07001514
Sasha Levin9b81c842015-08-10 19:05:18 -04001515 if (unlikely(error))
1516 io->error = error;
Milan Broz5742fd72008-02-08 02:10:43 +00001517
1518 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001519}
1520
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001521static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001522{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001523 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001524
1525 clone->bi_private = io;
1526 clone->bi_end_io = crypt_endio;
1527 clone->bi_bdev = cc->dev->bdev;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001528 clone->bi_opf = io->base_bio->bi_opf;
Milan Broz8b004452006-10-03 01:15:37 -07001529}
1530
Milan Broz20c82532011-01-13 19:59:53 +00001531static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001532{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001533 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001534 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001535
Milan Broz8b004452006-10-03 01:15:37 -07001536 /*
Mike Snitzer59779072015-04-09 16:53:24 -04001537 * We need the original biovec array in order to decrypt
1538 * the whole bio data *afterwards* -- thanks to immutable
1539 * biovecs we don't need to worry about the block layer
1540 * modifying the biovec array; so leverage bio_clone_fast().
Milan Broz8b004452006-10-03 01:15:37 -07001541 */
Mike Snitzer59779072015-04-09 16:53:24 -04001542 clone = bio_clone_fast(io->base_bio, gfp, cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001543 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001544 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001545
Milan Broz20c82532011-01-13 19:59:53 +00001546 crypt_inc_pending(io);
1547
Milan Broz8b004452006-10-03 01:15:37 -07001548 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001549 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001550
Milan Brozef43aa32017-01-04 20:23:54 +01001551 if (dm_crypt_integrity_io_alloc(io, clone)) {
1552 crypt_dec_pending(io);
1553 bio_put(clone);
1554 return 1;
1555 }
1556
Milan Broz93e605c2006-10-03 01:15:38 -07001557 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001558 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001559}
1560
Mikulas Patockadc267622015-02-13 08:25:59 -05001561static void kcryptd_io_read_work(struct work_struct *work)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001562{
1563 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1564
Mikulas Patockadc267622015-02-13 08:25:59 -05001565 crypt_inc_pending(io);
1566 if (kcryptd_io_read(io, GFP_NOIO))
1567 io->error = -ENOMEM;
1568 crypt_dec_pending(io);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001569}
1570
Mikulas Patockadc267622015-02-13 08:25:59 -05001571static void kcryptd_queue_read(struct dm_crypt_io *io)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001572{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001573 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001574
Mikulas Patockadc267622015-02-13 08:25:59 -05001575 INIT_WORK(&io->work, kcryptd_io_read_work);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001576 queue_work(cc->io_queue, &io->work);
1577}
1578
Mikulas Patockadc267622015-02-13 08:25:59 -05001579static void kcryptd_io_write(struct dm_crypt_io *io)
1580{
1581 struct bio *clone = io->ctx.bio_out;
1582
1583 generic_make_request(clone);
1584}
1585
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001586#define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1587
Mikulas Patockadc267622015-02-13 08:25:59 -05001588static int dmcrypt_write(void *data)
1589{
1590 struct crypt_config *cc = data;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001591 struct dm_crypt_io *io;
1592
Mikulas Patockadc267622015-02-13 08:25:59 -05001593 while (1) {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001594 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -05001595 struct blk_plug plug;
1596
1597 DECLARE_WAITQUEUE(wait, current);
1598
1599 spin_lock_irq(&cc->write_thread_wait.lock);
1600continue_locked:
1601
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001602 if (!RB_EMPTY_ROOT(&cc->write_tree))
Mikulas Patockadc267622015-02-13 08:25:59 -05001603 goto pop_from_list;
1604
Rabin Vincentf659b102016-09-21 16:22:29 +02001605 set_current_state(TASK_INTERRUPTIBLE);
Mikulas Patockadc267622015-02-13 08:25:59 -05001606 __add_wait_queue(&cc->write_thread_wait, &wait);
1607
1608 spin_unlock_irq(&cc->write_thread_wait.lock);
1609
Rabin Vincentf659b102016-09-21 16:22:29 +02001610 if (unlikely(kthread_should_stop())) {
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001611 set_current_state(TASK_RUNNING);
Rabin Vincentf659b102016-09-21 16:22:29 +02001612 remove_wait_queue(&cc->write_thread_wait, &wait);
1613 break;
1614 }
1615
Mikulas Patockadc267622015-02-13 08:25:59 -05001616 schedule();
1617
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001618 set_current_state(TASK_RUNNING);
Mikulas Patockadc267622015-02-13 08:25:59 -05001619 spin_lock_irq(&cc->write_thread_wait.lock);
1620 __remove_wait_queue(&cc->write_thread_wait, &wait);
1621 goto continue_locked;
1622
1623pop_from_list:
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001624 write_tree = cc->write_tree;
1625 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05001626 spin_unlock_irq(&cc->write_thread_wait.lock);
1627
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001628 BUG_ON(rb_parent(write_tree.rb_node));
1629
1630 /*
1631 * Note: we cannot walk the tree here with rb_next because
1632 * the structures may be freed when kcryptd_io_write is called.
1633 */
Mikulas Patockadc267622015-02-13 08:25:59 -05001634 blk_start_plug(&plug);
1635 do {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001636 io = crypt_io_from_node(rb_first(&write_tree));
1637 rb_erase(&io->rb_node, &write_tree);
Mikulas Patockadc267622015-02-13 08:25:59 -05001638 kcryptd_io_write(io);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001639 } while (!RB_EMPTY_ROOT(&write_tree));
Mikulas Patockadc267622015-02-13 08:25:59 -05001640 blk_finish_plug(&plug);
1641 }
1642 return 0;
1643}
1644
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001645static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001646{
Milan Brozdec1ced2008-02-08 02:10:57 +00001647 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001648 struct crypt_config *cc = io->cc;
Mikulas Patockadc267622015-02-13 08:25:59 -05001649 unsigned long flags;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001650 sector_t sector;
1651 struct rb_node **rbp, *parent;
Milan Brozdec1ced2008-02-08 02:10:57 +00001652
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001653 if (unlikely(io->error < 0)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001654 crypt_free_buffer_pages(cc, clone);
1655 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001656 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001657 return;
1658 }
1659
1660 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001661 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001662
Kent Overstreet4f024f32013-10-11 15:44:27 -07001663 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001664
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001665 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1666 generic_make_request(clone);
1667 return;
1668 }
1669
Mikulas Patockadc267622015-02-13 08:25:59 -05001670 spin_lock_irqsave(&cc->write_thread_wait.lock, flags);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001671 rbp = &cc->write_tree.rb_node;
1672 parent = NULL;
1673 sector = io->sector;
1674 while (*rbp) {
1675 parent = *rbp;
1676 if (sector < crypt_io_from_node(parent)->sector)
1677 rbp = &(*rbp)->rb_left;
1678 else
1679 rbp = &(*rbp)->rb_right;
1680 }
1681 rb_link_node(&io->rb_node, parent, rbp);
1682 rb_insert_color(&io->rb_node, &cc->write_tree);
1683
Mikulas Patockadc267622015-02-13 08:25:59 -05001684 wake_up_locked(&cc->write_thread_wait);
1685 spin_unlock_irqrestore(&cc->write_thread_wait.lock, flags);
Milan Broz4e4eef62008-02-08 02:10:49 +00001686}
1687
Milan Brozfc5a5e92008-10-10 13:37:04 +01001688static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001689{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001690 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001691 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001692 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001693 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +00001694 int r;
Milan Broz8b004452006-10-03 01:15:37 -07001695
Milan Broz93e605c2006-10-03 01:15:38 -07001696 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001697 * Prevent io from disappearing until this function completes.
1698 */
1699 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001700 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001701
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001702 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1703 if (unlikely(!clone)) {
1704 io->error = -EIO;
1705 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001706 }
Milan Broz899c95d2008-02-08 02:11:02 +00001707
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001708 io->ctx.bio_out = clone;
1709 io->ctx.iter_out = clone->bi_iter;
1710
1711 sector += bio_sectors(clone);
1712
1713 crypt_inc_pending(io);
1714 r = crypt_convert(cc, &io->ctx);
Milan Brozef43aa32017-01-04 20:23:54 +01001715 if (r < 0)
1716 io->error = r;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001717 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1718
1719 /* Encryption was already finished, submit io now */
1720 if (crypt_finished) {
1721 kcryptd_crypt_write_io_submit(io, 0);
1722 io->sector = sector;
1723 }
1724
1725dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001726 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001727}
1728
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001729static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001730{
Milan Broz5742fd72008-02-08 02:10:43 +00001731 crypt_dec_pending(io);
1732}
1733
Milan Broz4e4eef62008-02-08 02:10:49 +00001734static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001735{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001736 struct crypt_config *cc = io->cc;
Milan Broz5742fd72008-02-08 02:10:43 +00001737 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001738
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001739 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001740
Milan Broz53017032008-02-08 02:10:38 +00001741 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001742 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001743
Milan Broz5742fd72008-02-08 02:10:43 +00001744 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001745 if (r < 0)
Milan Brozef43aa32017-01-04 20:23:54 +01001746 io->error = r;
Milan Broz5742fd72008-02-08 02:10:43 +00001747
Mikulas Patocka40b62292012-07-27 15:08:04 +01001748 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001749 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001750
1751 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001752}
1753
Milan Broz95497a92008-02-08 02:11:12 +00001754static void kcryptd_async_done(struct crypto_async_request *async_req,
1755 int error)
1756{
Huang Yingb2174ee2009-03-16 17:44:33 +00001757 struct dm_crypt_request *dmreq = async_req->data;
1758 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001759 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001760 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001761
Milan Broz54cea3f2015-05-15 17:00:25 +02001762 /*
1763 * A request from crypto driver backlog is going to be processed now,
1764 * finish the completion and continue in crypt_convert().
1765 * (Callback will be called for the second time for this request.)
1766 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001767 if (error == -EINPROGRESS) {
1768 complete(&ctx->restart);
Milan Broz95497a92008-02-08 02:11:12 +00001769 return;
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001770 }
Milan Broz95497a92008-02-08 02:11:12 +00001771
Milan Broz2dc5327d32011-01-13 19:59:54 +00001772 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001773 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq);
Milan Broz2dc5327d32011-01-13 19:59:54 +00001774
Milan Brozef43aa32017-01-04 20:23:54 +01001775 if (error == -EBADMSG) {
1776 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1777 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
1778 io->error = -EILSEQ;
1779 } else if (error < 0)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001780 io->error = -EIO;
1781
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001782 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00001783
Mikulas Patocka40b62292012-07-27 15:08:04 +01001784 if (!atomic_dec_and_test(&ctx->cc_pending))
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001785 return;
Milan Broz95497a92008-02-08 02:11:12 +00001786
1787 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001788 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001789 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001790 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001791}
1792
Milan Broz4e4eef62008-02-08 02:10:49 +00001793static void kcryptd_crypt(struct work_struct *work)
1794{
1795 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1796
1797 if (bio_data_dir(io->base_bio) == READ)
1798 kcryptd_crypt_read_convert(io);
1799 else
1800 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001801}
1802
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001803static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1804{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001805 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001806
1807 INIT_WORK(&io->work, kcryptd_crypt);
1808 queue_work(cc->crypt_queue, &io->work);
1809}
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811/*
1812 * Decode key from its hex representation
1813 */
1814static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1815{
1816 char buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 unsigned int i;
1818
1819 buffer[2] = '\0';
1820
Milan Broz8b004452006-10-03 01:15:37 -07001821 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 buffer[0] = *hex++;
1823 buffer[1] = *hex++;
1824
majianpeng1a66a082012-07-27 15:07:59 +01001825 if (kstrtou8(buffer, 16, &key[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return -EINVAL;
1827 }
1828
1829 if (*hex != '\0')
1830 return -EINVAL;
1831
1832 return 0;
1833}
1834
Milan Brozef43aa32017-01-04 20:23:54 +01001835static void crypt_free_tfms_aead(struct crypt_config *cc)
1836{
1837 if (!cc->cipher_tfm.tfms_aead)
1838 return;
1839
1840 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1841 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]);
1842 cc->cipher_tfm.tfms_aead[0] = NULL;
1843 }
1844
1845 kfree(cc->cipher_tfm.tfms_aead);
1846 cc->cipher_tfm.tfms_aead = NULL;
1847}
1848
1849static void crypt_free_tfms_skcipher(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001850{
Milan Brozd1f96422011-01-13 19:59:54 +00001851 unsigned i;
1852
Milan Brozef43aa32017-01-04 20:23:54 +01001853 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001854 return;
1855
Milan Brozd1f96422011-01-13 19:59:54 +00001856 for (i = 0; i < cc->tfms_count; i++)
Milan Brozef43aa32017-01-04 20:23:54 +01001857 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) {
1858 crypto_free_skcipher(cc->cipher_tfm.tfms[i]);
1859 cc->cipher_tfm.tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001860 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001861
Milan Brozef43aa32017-01-04 20:23:54 +01001862 kfree(cc->cipher_tfm.tfms);
1863 cc->cipher_tfm.tfms = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001864}
1865
Milan Brozef43aa32017-01-04 20:23:54 +01001866static void crypt_free_tfms(struct crypt_config *cc)
1867{
Milan Broz33d2f092017-03-16 15:39:40 +01001868 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001869 crypt_free_tfms_aead(cc);
1870 else
1871 crypt_free_tfms_skcipher(cc);
1872}
1873
1874static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001875{
Milan Brozd1f96422011-01-13 19:59:54 +00001876 unsigned i;
1877 int err;
1878
Milan Brozef43aa32017-01-04 20:23:54 +01001879 cc->cipher_tfm.tfms = kzalloc(cc->tfms_count *
1880 sizeof(struct crypto_skcipher *), GFP_KERNEL);
1881 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001882 return -ENOMEM;
1883
Milan Brozd1f96422011-01-13 19:59:54 +00001884 for (i = 0; i < cc->tfms_count; i++) {
Milan Brozef43aa32017-01-04 20:23:54 +01001885 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
1886 if (IS_ERR(cc->cipher_tfm.tfms[i])) {
1887 err = PTR_ERR(cc->cipher_tfm.tfms[i]);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001888 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001889 return err;
1890 }
1891 }
1892
1893 return 0;
1894}
1895
Milan Brozef43aa32017-01-04 20:23:54 +01001896static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
1897{
Milan Brozef43aa32017-01-04 20:23:54 +01001898 int err;
1899
1900 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
1901 if (!cc->cipher_tfm.tfms)
1902 return -ENOMEM;
1903
Milan Brozef43aa32017-01-04 20:23:54 +01001904 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
1905 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1906 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
1907 crypt_free_tfms(cc);
1908 return err;
1909 }
1910
Milan Brozef43aa32017-01-04 20:23:54 +01001911 return 0;
1912}
1913
1914static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
1915{
Milan Broz33d2f092017-03-16 15:39:40 +01001916 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001917 return crypt_alloc_tfms_aead(cc, ciphermode);
1918 else
1919 return crypt_alloc_tfms_skcipher(cc, ciphermode);
1920}
1921
1922static unsigned crypt_subkey_size(struct crypt_config *cc)
1923{
1924 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1925}
1926
1927static unsigned crypt_authenckey_size(struct crypt_config *cc)
1928{
1929 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
1930}
1931
1932/*
1933 * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
1934 * the key must be for some reason in special format.
1935 * This funcion converts cc->key to this special format.
1936 */
1937static void crypt_copy_authenckey(char *p, const void *key,
1938 unsigned enckeylen, unsigned authkeylen)
1939{
1940 struct crypto_authenc_key_param *param;
1941 struct rtattr *rta;
1942
1943 rta = (struct rtattr *)p;
1944 param = RTA_DATA(rta);
1945 param->enckeylen = cpu_to_be32(enckeylen);
1946 rta->rta_len = RTA_LENGTH(sizeof(*param));
1947 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
1948 p += RTA_SPACE(sizeof(*param));
1949 memcpy(p, key + enckeylen, authkeylen);
1950 p += authkeylen;
1951 memcpy(p, key, enckeylen);
1952}
1953
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04001954static int crypt_setkey(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +00001955{
Milan Brozda31a072013-10-28 23:21:03 +01001956 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001957 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001958
Milan Brozda31a072013-10-28 23:21:03 +01001959 /* Ignore extra keys (which are used for IV etc) */
Milan Brozef43aa32017-01-04 20:23:54 +01001960 subkey_size = crypt_subkey_size(cc);
Milan Brozda31a072013-10-28 23:21:03 +01001961
Milan Brozef43aa32017-01-04 20:23:54 +01001962 if (crypt_integrity_hmac(cc))
1963 crypt_copy_authenckey(cc->authenc_key, cc->key,
1964 subkey_size - cc->key_mac_size,
1965 cc->key_mac_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001966 for (i = 0; i < cc->tfms_count; i++) {
Milan Broz33d2f092017-03-16 15:39:40 +01001967 if (crypt_integrity_hmac(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001968 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1969 cc->authenc_key, crypt_authenckey_size(cc));
Milan Broz33d2f092017-03-16 15:39:40 +01001970 else if (crypt_integrity_aead(cc))
1971 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1972 cc->key + (i * subkey_size),
1973 subkey_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001974 else
1975 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i],
1976 cc->key + (i * subkey_size),
1977 subkey_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001978 if (r)
1979 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001980 }
1981
Milan Brozef43aa32017-01-04 20:23:54 +01001982 if (crypt_integrity_hmac(cc))
1983 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc));
1984
Andi Kleenc0297722011-01-13 19:59:53 +00001985 return err;
1986}
1987
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001988#ifdef CONFIG_KEYS
1989
Ondrej Kozina027c4312016-12-01 18:20:52 +01001990static bool contains_whitespace(const char *str)
1991{
1992 while (*str)
1993 if (isspace(*str++))
1994 return true;
1995 return false;
1996}
1997
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001998static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
1999{
2000 char *new_key_string, *key_desc;
2001 int ret;
2002 struct key *key;
2003 const struct user_key_payload *ukp;
2004
Ondrej Kozina027c4312016-12-01 18:20:52 +01002005 /*
2006 * Reject key_string with whitespace. dm core currently lacks code for
2007 * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
2008 */
2009 if (contains_whitespace(key_string)) {
2010 DMERR("whitespace chars not allowed in key string");
2011 return -EINVAL;
2012 }
2013
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002014 /* look for next ':' separating key_type from key_description */
2015 key_desc = strpbrk(key_string, ":");
2016 if (!key_desc || key_desc == key_string || !strlen(key_desc + 1))
2017 return -EINVAL;
2018
2019 if (strncmp(key_string, "logon:", key_desc - key_string + 1) &&
2020 strncmp(key_string, "user:", key_desc - key_string + 1))
2021 return -EINVAL;
2022
2023 new_key_string = kstrdup(key_string, GFP_KERNEL);
2024 if (!new_key_string)
2025 return -ENOMEM;
2026
2027 key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user,
2028 key_desc + 1, NULL);
2029 if (IS_ERR(key)) {
2030 kzfree(new_key_string);
2031 return PTR_ERR(key);
2032 }
2033
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002034 down_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002035
David Howells0837e492017-03-01 15:11:23 +00002036 ukp = user_key_payload_locked(key);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002037 if (!ukp) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002038 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002039 key_put(key);
2040 kzfree(new_key_string);
2041 return -EKEYREVOKED;
2042 }
2043
2044 if (cc->key_size != ukp->datalen) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002045 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002046 key_put(key);
2047 kzfree(new_key_string);
2048 return -EINVAL;
2049 }
2050
2051 memcpy(cc->key, ukp->data, cc->key_size);
2052
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002053 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002054 key_put(key);
2055
2056 /* clear the flag since following operations may invalidate previously valid key */
2057 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2058
2059 ret = crypt_setkey(cc);
2060
2061 /* wipe the kernel key payload copy in each case */
2062 memset(cc->key, 0, cc->key_size * sizeof(u8));
2063
2064 if (!ret) {
2065 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2066 kzfree(cc->key_string);
2067 cc->key_string = new_key_string;
2068 } else
2069 kzfree(new_key_string);
2070
2071 return ret;
2072}
2073
2074static int get_key_size(char **key_string)
2075{
2076 char *colon, dummy;
2077 int ret;
2078
2079 if (*key_string[0] != ':')
2080 return strlen(*key_string) >> 1;
2081
2082 /* look for next ':' in key string */
2083 colon = strpbrk(*key_string + 1, ":");
2084 if (!colon)
2085 return -EINVAL;
2086
2087 if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':')
2088 return -EINVAL;
2089
2090 *key_string = colon;
2091
2092 /* remaining key string should be :<logon|user>:<key_desc> */
2093
2094 return ret;
2095}
2096
2097#else
2098
2099static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2100{
2101 return -EINVAL;
2102}
2103
2104static int get_key_size(char **key_string)
2105{
2106 return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1;
2107}
2108
2109#endif
2110
Milan Broze48d4bb2006-10-03 01:15:37 -07002111static int crypt_set_key(struct crypt_config *cc, char *key)
2112{
Milan Brozde8be5a2011-03-24 13:54:27 +00002113 int r = -EINVAL;
2114 int key_string_len = strlen(key);
2115
Milan Broz69a8cfc2011-01-13 19:59:49 +00002116 /* Hyphen (which gives a key_size of zero) means there is no key. */
2117 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00002118 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002119
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002120 /* ':' means the key is in kernel keyring, short-circuit normal key processing */
2121 if (key[0] == ':') {
2122 r = crypt_set_keyring_key(cc, key + 1);
2123 goto out;
2124 }
2125
Ondrej Kozina265e9092016-11-02 15:02:08 +01002126 /* clear the flag since following operations may invalidate previously valid key */
2127 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2128
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002129 /* wipe references to any kernel keyring key */
2130 kzfree(cc->key_string);
2131 cc->key_string = NULL;
2132
Milan Broz69a8cfc2011-01-13 19:59:49 +00002133 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00002134 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002135
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002136 r = crypt_setkey(cc);
Ondrej Kozina265e9092016-11-02 15:02:08 +01002137 if (!r)
2138 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Milan Brozde8be5a2011-03-24 13:54:27 +00002139
2140out:
2141 /* Hex key string not needed after here, so wipe it. */
2142 memset(key, '0', key_string_len);
2143
2144 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002145}
2146
2147static int crypt_wipe_key(struct crypt_config *cc)
2148{
2149 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2150 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002151 kzfree(cc->key_string);
2152 cc->key_string = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +00002153
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002154 return crypt_setkey(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07002155}
2156
Milan Broz28513fc2010-08-12 04:14:06 +01002157static void crypt_dtr(struct dm_target *ti)
2158{
2159 struct crypt_config *cc = ti->private;
2160
2161 ti->private = NULL;
2162
2163 if (!cc)
2164 return;
2165
Rabin Vincentf659b102016-09-21 16:22:29 +02002166 if (cc->write_thread)
Mikulas Patockadc267622015-02-13 08:25:59 -05002167 kthread_stop(cc->write_thread);
2168
Milan Broz28513fc2010-08-12 04:14:06 +01002169 if (cc->io_queue)
2170 destroy_workqueue(cc->io_queue);
2171 if (cc->crypt_queue)
2172 destroy_workqueue(cc->crypt_queue);
2173
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002174 crypt_free_tfms(cc);
2175
Milan Broz28513fc2010-08-12 04:14:06 +01002176 if (cc->bs)
2177 bioset_free(cc->bs);
2178
Julia Lawall6f659852015-09-13 14:15:05 +02002179 mempool_destroy(cc->page_pool);
2180 mempool_destroy(cc->req_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01002181 mempool_destroy(cc->tag_pool);
Milan Broz28513fc2010-08-12 04:14:06 +01002182
2183 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
2184 cc->iv_gen_ops->dtr(cc);
2185
Milan Broz28513fc2010-08-12 04:14:06 +01002186 if (cc->dev)
2187 dm_put_device(ti, cc->dev);
2188
Milan Broz5ebaee62010-08-12 04:14:07 +01002189 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00002190 kzfree(cc->cipher_string);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002191 kzfree(cc->key_string);
Milan Brozef43aa32017-01-04 20:23:54 +01002192 kzfree(cc->cipher_auth);
2193 kzfree(cc->authenc_key);
Milan Broz28513fc2010-08-12 04:14:06 +01002194
2195 /* Must zero key material before freeing */
2196 kzfree(cc);
2197}
2198
Milan Broze889f972017-03-16 15:39:39 +01002199static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
2200{
2201 struct crypt_config *cc = ti->private;
2202
Milan Broz33d2f092017-03-16 15:39:40 +01002203 if (crypt_integrity_aead(cc))
Milan Broze889f972017-03-16 15:39:39 +01002204 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2205 else
2206 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2207
Milan Broze889f972017-03-16 15:39:39 +01002208 if (cc->iv_size)
2209 /* at least a 64 bit sector number should fit in our buffer */
2210 cc->iv_size = max(cc->iv_size,
2211 (unsigned int)(sizeof(u64) / sizeof(u8)));
2212 else if (ivmode) {
2213 DMWARN("Selected cipher does not support IVs");
2214 ivmode = NULL;
2215 }
2216
2217 /* Choose ivmode, see comments at iv code. */
2218 if (ivmode == NULL)
2219 cc->iv_gen_ops = NULL;
2220 else if (strcmp(ivmode, "plain") == 0)
2221 cc->iv_gen_ops = &crypt_iv_plain_ops;
2222 else if (strcmp(ivmode, "plain64") == 0)
2223 cc->iv_gen_ops = &crypt_iv_plain64_ops;
2224 else if (strcmp(ivmode, "essiv") == 0)
2225 cc->iv_gen_ops = &crypt_iv_essiv_ops;
2226 else if (strcmp(ivmode, "benbi") == 0)
2227 cc->iv_gen_ops = &crypt_iv_benbi_ops;
2228 else if (strcmp(ivmode, "null") == 0)
2229 cc->iv_gen_ops = &crypt_iv_null_ops;
2230 else if (strcmp(ivmode, "lmk") == 0) {
2231 cc->iv_gen_ops = &crypt_iv_lmk_ops;
2232 /*
2233 * Version 2 and 3 is recognised according
2234 * to length of provided multi-key string.
2235 * If present (version 3), last key is used as IV seed.
2236 * All keys (including IV seed) are always the same size.
2237 */
2238 if (cc->key_size % cc->key_parts) {
2239 cc->key_parts++;
2240 cc->key_extra_size = cc->key_size / cc->key_parts;
2241 }
2242 } else if (strcmp(ivmode, "tcw") == 0) {
2243 cc->iv_gen_ops = &crypt_iv_tcw_ops;
2244 cc->key_parts += 2; /* IV + whitening */
2245 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
2246 } else if (strcmp(ivmode, "random") == 0) {
2247 cc->iv_gen_ops = &crypt_iv_random_ops;
2248 /* Need storage space in integrity fields. */
2249 cc->integrity_iv_size = cc->iv_size;
2250 } else {
2251 ti->error = "Invalid IV mode";
2252 return -EINVAL;
2253 }
2254
2255 return 0;
2256}
2257
Milan Broz33d2f092017-03-16 15:39:40 +01002258/*
2259 * Workaround to parse cipher algorithm from crypto API spec.
2260 * The cc->cipher is currently used only in ESSIV.
2261 * This should be probably done by crypto-api calls (once available...)
2262 */
2263static int crypt_ctr_blkdev_cipher(struct crypt_config *cc)
2264{
2265 const char *alg_name = NULL;
2266 char *start, *end;
2267
2268 if (crypt_integrity_aead(cc)) {
2269 alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc)));
2270 if (!alg_name)
2271 return -EINVAL;
2272 if (crypt_integrity_hmac(cc)) {
2273 alg_name = strchr(alg_name, ',');
2274 if (!alg_name)
2275 return -EINVAL;
2276 }
2277 alg_name++;
2278 } else {
2279 alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc)));
2280 if (!alg_name)
2281 return -EINVAL;
2282 }
2283
2284 start = strchr(alg_name, '(');
2285 end = strchr(alg_name, ')');
2286
2287 if (!start && !end) {
2288 cc->cipher = kstrdup(alg_name, GFP_KERNEL);
2289 return cc->cipher ? 0 : -ENOMEM;
2290 }
2291
2292 if (!start || !end || ++start >= end)
2293 return -EINVAL;
2294
2295 cc->cipher = kzalloc(end - start + 1, GFP_KERNEL);
2296 if (!cc->cipher)
2297 return -ENOMEM;
2298
2299 strncpy(cc->cipher, start, end - start);
2300
2301 return 0;
2302}
2303
2304/*
2305 * Workaround to parse HMAC algorithm from AEAD crypto API spec.
2306 * The HMAC is needed to calculate tag size (HMAC digest size).
2307 * This should be probably done by crypto-api calls (once available...)
2308 */
2309static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
2310{
2311 char *start, *end, *mac_alg = NULL;
2312 struct crypto_ahash *mac;
2313
2314 if (!strstarts(cipher_api, "authenc("))
2315 return 0;
2316
2317 start = strchr(cipher_api, '(');
2318 end = strchr(cipher_api, ',');
2319 if (!start || !end || ++start > end)
2320 return -EINVAL;
2321
2322 mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
2323 if (!mac_alg)
2324 return -ENOMEM;
2325 strncpy(mac_alg, start, end - start);
2326
2327 mac = crypto_alloc_ahash(mac_alg, 0, 0);
2328 kfree(mac_alg);
2329
2330 if (IS_ERR(mac))
2331 return PTR_ERR(mac);
2332
2333 cc->key_mac_size = crypto_ahash_digestsize(mac);
2334 crypto_free_ahash(mac);
2335
2336 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
2337 if (!cc->authenc_key)
2338 return -ENOMEM;
2339
2340 return 0;
2341}
2342
2343static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key,
2344 char **ivmode, char **ivopts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Milan Broz5ebaee62010-08-12 04:14:07 +01002346 struct crypt_config *cc = ti->private;
Milan Broz33d2f092017-03-16 15:39:40 +01002347 char *tmp, *cipher_api;
2348 int ret = -EINVAL;
2349
2350 cc->tfms_count = 1;
2351
2352 /*
2353 * New format (capi: prefix)
2354 * capi:cipher_api_spec-iv:ivopts
2355 */
2356 tmp = &cipher_in[strlen("capi:")];
2357 cipher_api = strsep(&tmp, "-");
2358 *ivmode = strsep(&tmp, ":");
2359 *ivopts = tmp;
2360
2361 if (*ivmode && !strcmp(*ivmode, "lmk"))
2362 cc->tfms_count = 64;
2363
2364 cc->key_parts = cc->tfms_count;
2365
2366 /* Allocate cipher */
2367 ret = crypt_alloc_tfms(cc, cipher_api);
2368 if (ret < 0) {
2369 ti->error = "Error allocating crypto tfm";
2370 return ret;
2371 }
2372
2373 /* Alloc AEAD, can be used only in new format. */
2374 if (crypt_integrity_aead(cc)) {
2375 ret = crypt_ctr_auth_cipher(cc, cipher_api);
2376 if (ret < 0) {
2377 ti->error = "Invalid AEAD cipher spec";
2378 return -ENOMEM;
2379 }
2380 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2381 } else
2382 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2383
2384 ret = crypt_ctr_blkdev_cipher(cc);
2385 if (ret < 0) {
2386 ti->error = "Cannot allocate cipher string";
2387 return -ENOMEM;
2388 }
2389
2390 return 0;
2391}
2392
2393static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
2394 char **ivmode, char **ivopts)
2395{
2396 struct crypt_config *cc = ti->private;
2397 char *tmp, *cipher, *chainmode, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01002398 char *cipher_api = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002399 int ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002400 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401
Milan Broz33d2f092017-03-16 15:39:40 +01002402 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002403 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 return -EINVAL;
2405 }
2406
Milan Broz5ebaee62010-08-12 04:14:07 +01002407 /*
2408 * Legacy dm-crypt cipher specification
Milan Brozd1f96422011-01-13 19:59:54 +00002409 * cipher[:keycount]-mode-iv:ivopts
Milan Broz5ebaee62010-08-12 04:14:07 +01002410 */
2411 tmp = cipher_in;
Milan Brozd1f96422011-01-13 19:59:54 +00002412 keycount = strsep(&tmp, "-");
2413 cipher = strsep(&keycount, ":");
2414
2415 if (!keycount)
2416 cc->tfms_count = 1;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002417 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
Milan Brozd1f96422011-01-13 19:59:54 +00002418 !is_power_of_2(cc->tfms_count)) {
2419 ti->error = "Bad cipher key count specification";
2420 return -EINVAL;
2421 }
2422 cc->key_parts = cc->tfms_count;
Milan Broz5ebaee62010-08-12 04:14:07 +01002423
2424 cc->cipher = kstrdup(cipher, GFP_KERNEL);
2425 if (!cc->cipher)
2426 goto bad_mem;
2427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 chainmode = strsep(&tmp, "-");
Milan Broz33d2f092017-03-16 15:39:40 +01002429 *ivopts = strsep(&tmp, "-");
2430 *ivmode = strsep(&*ivopts, ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01002433 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Milan Broz7dbcd132011-01-13 19:59:52 +00002435 /*
2436 * For compatibility with the original dm-crypt mapping format, if
2437 * only the cipher name is supplied, use cbc-plain.
2438 */
Milan Broz33d2f092017-03-16 15:39:40 +01002439 if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 chainmode = "cbc";
Milan Broz33d2f092017-03-16 15:39:40 +01002441 *ivmode = "plain";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 }
2443
Milan Broz33d2f092017-03-16 15:39:40 +01002444 if (strcmp(chainmode, "ecb") && !*ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002445 ti->error = "IV mechanism required";
2446 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 }
2448
Milan Broz5ebaee62010-08-12 04:14:07 +01002449 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
2450 if (!cipher_api)
2451 goto bad_mem;
2452
2453 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
2454 "%s(%s)", chainmode, cipher);
2455 if (ret < 0) {
2456 kfree(cipher_api);
2457 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10002458 }
2459
Milan Broz5ebaee62010-08-12 04:14:07 +01002460 /* Allocate cipher */
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002461 ret = crypt_alloc_tfms(cc, cipher_api);
2462 if (ret < 0) {
2463 ti->error = "Error allocating crypto tfm";
Milan Broz33d2f092017-03-16 15:39:40 +01002464 kfree(cipher_api);
2465 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Milan Broz33d2f092017-03-16 15:39:40 +01002468 return 0;
2469bad_mem:
2470 ti->error = "Cannot allocate cipher strings";
2471 return -ENOMEM;
2472}
2473
2474static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key)
2475{
2476 struct crypt_config *cc = ti->private;
2477 char *ivmode = NULL, *ivopts = NULL;
2478 int ret;
2479
2480 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
2481 if (!cc->cipher_string) {
2482 ti->error = "Cannot allocate cipher strings";
2483 return -ENOMEM;
2484 }
2485
2486 if (strstarts(cipher_in, "capi:"))
2487 ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts);
2488 else
2489 ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts);
2490 if (ret)
2491 return ret;
2492
Milan Broz5ebaee62010-08-12 04:14:07 +01002493 /* Initialize IV */
Milan Broze889f972017-03-16 15:39:39 +01002494 ret = crypt_ctr_ivmode(ti, ivmode);
2495 if (ret < 0)
Milan Broz33d2f092017-03-16 15:39:40 +01002496 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Milan Brozda31a072013-10-28 23:21:03 +01002498 /* Initialize and set key */
2499 ret = crypt_set_key(cc, key);
2500 if (ret < 0) {
2501 ti->error = "Error decoding and setting key";
Milan Broz33d2f092017-03-16 15:39:40 +01002502 return ret;
Milan Brozda31a072013-10-28 23:21:03 +01002503 }
2504
Milan Broz28513fc2010-08-12 04:14:06 +01002505 /* Allocate IV */
2506 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
2507 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
2508 if (ret < 0) {
2509 ti->error = "Error creating IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002510 return ret;
Milan Broz28513fc2010-08-12 04:14:06 +01002511 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00002512 }
2513
Milan Broz28513fc2010-08-12 04:14:06 +01002514 /* Initialize IV (set keys for ESSIV etc) */
2515 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
2516 ret = cc->iv_gen_ops->init(cc);
2517 if (ret < 0) {
2518 ti->error = "Error initialising IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002519 return ret;
Milan Broz28513fc2010-08-12 04:14:06 +01002520 }
2521 }
2522
Milan Broz5ebaee62010-08-12 04:14:07 +01002523 return ret;
Milan Broz5ebaee62010-08-12 04:14:07 +01002524}
2525
Milan Brozef43aa32017-01-04 20:23:54 +01002526static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
2527{
2528 struct crypt_config *cc = ti->private;
2529 struct dm_arg_set as;
2530 static struct dm_arg _args[] = {
Milan Broz8f0009a2017-03-16 15:39:44 +01002531 {0, 6, "Invalid number of feature args"},
Milan Brozef43aa32017-01-04 20:23:54 +01002532 };
2533 unsigned int opt_params, val;
2534 const char *opt_string, *sval;
Milan Broz8f0009a2017-03-16 15:39:44 +01002535 char dummy;
Milan Brozef43aa32017-01-04 20:23:54 +01002536 int ret;
2537
2538 /* Optional parameters */
2539 as.argc = argc;
2540 as.argv = argv;
2541
2542 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2543 if (ret)
2544 return ret;
2545
2546 while (opt_params--) {
2547 opt_string = dm_shift_arg(&as);
2548 if (!opt_string) {
2549 ti->error = "Not enough feature arguments";
2550 return -EINVAL;
2551 }
2552
2553 if (!strcasecmp(opt_string, "allow_discards"))
2554 ti->num_discard_bios = 1;
2555
2556 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
2557 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
2558
2559 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
2560 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
2561 else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
2562 if (val == 0 || val > MAX_TAG_SIZE) {
2563 ti->error = "Invalid integrity arguments";
2564 return -EINVAL;
2565 }
2566 cc->on_disk_tag_size = val;
2567 sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
2568 if (!strcasecmp(sval, "aead")) {
2569 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002570 } else if (strcasecmp(sval, "none")) {
2571 ti->error = "Unknown integrity profile";
2572 return -EINVAL;
2573 }
2574
2575 cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
2576 if (!cc->cipher_auth)
2577 return -ENOMEM;
Milan Broz8f0009a2017-03-16 15:39:44 +01002578 } else if (sscanf(opt_string, "sector_size:%u%c", &cc->sector_size, &dummy) == 1) {
2579 if (cc->sector_size < (1 << SECTOR_SHIFT) ||
2580 cc->sector_size > 4096 ||
2581 (1 << ilog2(cc->sector_size) != cc->sector_size)) {
2582 ti->error = "Invalid feature value for sector_size";
2583 return -EINVAL;
2584 }
2585 } else if (!strcasecmp(opt_string, "iv_large_sectors"))
2586 set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
2587 else {
Milan Brozef43aa32017-01-04 20:23:54 +01002588 ti->error = "Invalid feature arguments";
2589 return -EINVAL;
2590 }
2591 }
2592
2593 return 0;
2594}
2595
Milan Broz5ebaee62010-08-12 04:14:07 +01002596/*
2597 * Construct an encryption mapping:
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002598 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
Milan Broz5ebaee62010-08-12 04:14:07 +01002599 */
2600static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2601{
2602 struct crypt_config *cc;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002603 int key_size;
Milan Brozef43aa32017-01-04 20:23:54 +01002604 unsigned int align_mask;
Milan Broz5ebaee62010-08-12 04:14:07 +01002605 unsigned long long tmpll;
2606 int ret;
Milan Brozef43aa32017-01-04 20:23:54 +01002607 size_t iv_size_padding, additional_req_size;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002608 char dummy;
Milan Broz5ebaee62010-08-12 04:14:07 +01002609
Milan Broz772ae5f2011-08-02 12:32:08 +01002610 if (argc < 5) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002611 ti->error = "Not enough arguments";
2612 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
2614
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002615 key_size = get_key_size(&argv[1]);
2616 if (key_size < 0) {
2617 ti->error = "Cannot parse key size";
2618 return -EINVAL;
2619 }
Milan Broz5ebaee62010-08-12 04:14:07 +01002620
2621 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
2622 if (!cc) {
2623 ti->error = "Cannot allocate encryption context";
2624 return -ENOMEM;
2625 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00002626 cc->key_size = key_size;
Milan Broz8f0009a2017-03-16 15:39:44 +01002627 cc->sector_size = (1 << SECTOR_SHIFT);
Milan Broz5ebaee62010-08-12 04:14:07 +01002628
2629 ti->private = cc;
Milan Brozef43aa32017-01-04 20:23:54 +01002630
2631 /* Optional parameters need to be read before cipher constructor */
2632 if (argc > 5) {
2633 ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
2634 if (ret)
2635 goto bad;
2636 }
2637
Milan Broz5ebaee62010-08-12 04:14:07 +01002638 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
2639 if (ret < 0)
2640 goto bad;
2641
Milan Broz33d2f092017-03-16 15:39:40 +01002642 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +01002643 cc->dmreq_start = sizeof(struct aead_request);
2644 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
2645 align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
2646 } else {
2647 cc->dmreq_start = sizeof(struct skcipher_request);
2648 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
2649 align_mask = crypto_skcipher_alignmask(any_tfm(cc));
2650 }
Mikulas Patockad49ec522014-08-28 11:09:31 -04002651 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
2652
Milan Brozef43aa32017-01-04 20:23:54 +01002653 if (align_mask < CRYPTO_MINALIGN) {
Mikulas Patockad49ec522014-08-28 11:09:31 -04002654 /* Allocate the padding exactly */
2655 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
Milan Brozef43aa32017-01-04 20:23:54 +01002656 & align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002657 } else {
2658 /*
2659 * If the cipher requires greater alignment than kmalloc
2660 * alignment, we don't know the exact position of the
2661 * initialization vector. We must assume worst case.
2662 */
Milan Brozef43aa32017-01-04 20:23:54 +01002663 iv_size_padding = align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002664 }
Milan Brozddd42ed2008-02-08 02:11:07 +00002665
Mikulas Patocka94f5e022015-02-13 08:25:26 -05002666 ret = -ENOMEM;
Milan Brozef43aa32017-01-04 20:23:54 +01002667
2668 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
2669 additional_req_size = sizeof(struct dm_crypt_request) +
2670 iv_size_padding + cc->iv_size +
2671 cc->iv_size +
2672 sizeof(uint64_t) +
2673 sizeof(unsigned int);
2674
2675 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start + additional_req_size);
Milan Brozddd42ed2008-02-08 02:11:07 +00002676 if (!cc->req_pool) {
2677 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01002678 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00002679 }
Milan Brozddd42ed2008-02-08 02:11:07 +00002680
Mike Snitzer30187e12016-01-31 13:28:26 -05002681 cc->per_bio_data_size = ti->per_io_data_size =
Milan Brozef43aa32017-01-04 20:23:54 +01002682 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
Mikulas Patockad49ec522014-08-28 11:09:31 -04002683 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002684
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05002685 cc->page_pool = mempool_create_page_pool(BIO_MAX_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002687 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01002688 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
2690
Jens Axboebb799ca2008-12-10 15:35:05 +01002691 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07002692 if (!cc->bs) {
2693 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01002694 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07002695 }
2696
Mikulas Patocka7145c242015-02-13 08:24:41 -05002697 mutex_init(&cc->bio_alloc_lock);
2698
Milan Broz28513fc2010-08-12 04:14:06 +01002699 ret = -EINVAL;
Milan Broz8f0009a2017-03-16 15:39:44 +01002700 if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) ||
2701 (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002702 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01002703 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08002705 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Vivek Goyale80d1c82015-07-31 09:20:36 -04002707 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
2708 if (ret) {
Milan Broz28513fc2010-08-12 04:14:06 +01002709 ti->error = "Device lookup failed";
2710 goto bad;
2711 }
2712
Vivek Goyale80d1c82015-07-31 09:20:36 -04002713 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002714 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002715 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01002716 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08002718 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Milan Broz33d2f092017-03-16 15:39:40 +01002720 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
Milan Brozef43aa32017-01-04 20:23:54 +01002721 ret = crypt_integrity_ctr(cc, ti);
Milan Broz772ae5f2011-08-02 12:32:08 +01002722 if (ret)
2723 goto bad;
2724
Milan Brozef43aa32017-01-04 20:23:54 +01002725 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
2726 if (!cc->tag_pool_max_sectors)
2727 cc->tag_pool_max_sectors = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01002728
Milan Brozef43aa32017-01-04 20:23:54 +01002729 cc->tag_pool = mempool_create_kmalloc_pool(MIN_IOS,
2730 cc->tag_pool_max_sectors * cc->on_disk_tag_size);
2731 if (!cc->tag_pool) {
2732 ti->error = "Cannot allocate integrity tags mempool";
2733 goto bad;
Milan Broz772ae5f2011-08-02 12:32:08 +01002734 }
2735 }
2736
Milan Broz28513fc2010-08-12 04:14:06 +01002737 ret = -ENOMEM;
Tejun Heo670368a2013-07-30 08:40:21 -04002738 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_MEM_RECLAIM, 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01002739 if (!cc->io_queue) {
2740 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01002741 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01002742 }
2743
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002744 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2745 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
2746 else
2747 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
2748 num_online_cpus());
Milan Brozcabf08e2007-10-19 22:38:58 +01002749 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01002750 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01002751 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01002752 }
2753
Mikulas Patockadc267622015-02-13 08:25:59 -05002754 init_waitqueue_head(&cc->write_thread_wait);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05002755 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05002756
2757 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write");
2758 if (IS_ERR(cc->write_thread)) {
2759 ret = PTR_ERR(cc->write_thread);
2760 cc->write_thread = NULL;
2761 ti->error = "Couldn't spawn write thread";
2762 goto bad;
2763 }
2764 wake_up_process(cc->write_thread);
2765
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002766 ti->num_flush_bios = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002767 ti->discard_zeroes_data_unsupported = true;
Milan Broz983c7db2011-09-25 23:26:21 +01002768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return 0;
2770
Milan Broz28513fc2010-08-12 04:14:06 +01002771bad:
2772 crypt_dtr(ti);
2773 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774}
2775
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002776static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002778 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01002779 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002780
Milan Broz772ae5f2011-08-02 12:32:08 +01002781 /*
Mike Christie28a8f0d2016-06-05 14:32:25 -05002782 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
2783 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
Mike Christiee6047142016-06-05 14:32:04 -05002784 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
Milan Broz772ae5f2011-08-02 12:32:08 +01002785 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06002786 if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
Mike Christie28a8f0d2016-06-05 14:32:25 -05002787 bio_op(bio) == REQ_OP_DISCARD)) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002788 bio->bi_bdev = cc->dev->bdev;
Milan Broz772ae5f2011-08-02 12:32:08 +01002789 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07002790 bio->bi_iter.bi_sector = cc->start +
2791 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002792 return DM_MAPIO_REMAPPED;
2793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002795 /*
2796 * Check if bio is too large, split as needed.
2797 */
2798 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
Milan Brozef43aa32017-01-04 20:23:54 +01002799 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002800 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
2801
Milan Broz8f0009a2017-03-16 15:39:44 +01002802 /*
2803 * Ensure that bio is a multiple of internal sector encryption size
2804 * and is aligned to this size as defined in IO hints.
2805 */
2806 if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0))
2807 return -EIO;
2808
2809 if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1)))
2810 return -EIO;
2811
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002812 io = dm_per_bio_data(bio, cc->per_bio_data_size);
2813 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Milan Brozef43aa32017-01-04 20:23:54 +01002814
2815 if (cc->on_disk_tag_size) {
2816 unsigned tag_len = cc->on_disk_tag_size * bio_sectors(bio);
2817
2818 if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
Milan Broz8f0009a2017-03-16 15:39:44 +01002819 unlikely(!(io->integrity_metadata = kzalloc(tag_len,
Milan Brozef43aa32017-01-04 20:23:54 +01002820 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
2821 if (bio_sectors(bio) > cc->tag_pool_max_sectors)
2822 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
2823 io->integrity_metadata = mempool_alloc(cc->tag_pool, GFP_NOIO);
2824 io->integrity_metadata_from_pool = true;
Milan Broz8f0009a2017-03-16 15:39:44 +01002825 memset(io->integrity_metadata, 0, cc->tag_pool_max_sectors * (1 << SECTOR_SHIFT));
Milan Brozef43aa32017-01-04 20:23:54 +01002826 }
2827 }
2828
Milan Broz33d2f092017-03-16 15:39:40 +01002829 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002830 io->ctx.r.req_aead = (struct aead_request *)(io + 1);
2831 else
2832 io->ctx.r.req = (struct skcipher_request *)(io + 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01002833
Milan Broz20c82532011-01-13 19:59:53 +00002834 if (bio_data_dir(io->base_bio) == READ) {
2835 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05002836 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00002837 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01002838 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002840 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841}
2842
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002843static void crypt_status(struct dm_target *ti, status_type_t type,
2844 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
Milan Broz5ebaee62010-08-12 04:14:07 +01002846 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002847 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002848 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 switch (type) {
2851 case STATUSTYPE_INFO:
2852 result[0] = '\0';
2853 break;
2854
2855 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00002856 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002858 if (cc->key_size > 0) {
2859 if (cc->key_string)
2860 DMEMIT(":%u:%s", cc->key_size, cc->key_string);
2861 else
2862 for (i = 0; i < cc->key_size; i++)
2863 DMEMIT("%02x", cc->key[i]);
2864 } else
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002865 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Andrew Morton4ee218c2006-03-27 01:17:48 -08002867 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
2868 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01002869
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002870 num_feature_args += !!ti->num_discard_bios;
2871 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002872 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Milan Broz8f0009a2017-03-16 15:39:44 +01002873 num_feature_args += (cc->sector_size != (1 << SECTOR_SHIFT)) ? 1 : 0;
2874 num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002875 if (cc->on_disk_tag_size)
2876 num_feature_args++;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002877 if (num_feature_args) {
2878 DMEMIT(" %d", num_feature_args);
2879 if (ti->num_discard_bios)
2880 DMEMIT(" allow_discards");
2881 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2882 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002883 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
2884 DMEMIT(" submit_from_crypt_cpus");
Milan Brozef43aa32017-01-04 20:23:54 +01002885 if (cc->on_disk_tag_size)
2886 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
Milan Broz8f0009a2017-03-16 15:39:44 +01002887 if (cc->sector_size != (1 << SECTOR_SHIFT))
2888 DMEMIT(" sector_size:%d", cc->sector_size);
2889 if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags))
2890 DMEMIT(" iv_large_sectors");
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002891 }
Milan Broz772ae5f2011-08-02 12:32:08 +01002892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 break;
2894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895}
2896
Milan Broze48d4bb2006-10-03 01:15:37 -07002897static void crypt_postsuspend(struct dm_target *ti)
2898{
2899 struct crypt_config *cc = ti->private;
2900
2901 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2902}
2903
2904static int crypt_preresume(struct dm_target *ti)
2905{
2906 struct crypt_config *cc = ti->private;
2907
2908 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
2909 DMERR("aborting resume - crypt key is not set.");
2910 return -EAGAIN;
2911 }
2912
2913 return 0;
2914}
2915
2916static void crypt_resume(struct dm_target *ti)
2917{
2918 struct crypt_config *cc = ti->private;
2919
2920 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2921}
2922
2923/* Message interface
2924 * key set <key>
2925 * key wipe
2926 */
2927static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
2928{
2929 struct crypt_config *cc = ti->private;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002930 int key_size, ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07002931
2932 if (argc < 2)
2933 goto error;
2934
Mike Snitzer498f0102011-08-02 12:32:04 +01002935 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07002936 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
2937 DMWARN("not suspended during key manipulation.");
2938 return -EINVAL;
2939 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002940 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002941 /* The key size may not be changed. */
2942 key_size = get_key_size(&argv[2]);
2943 if (key_size < 0 || cc->key_size != key_size) {
2944 memset(argv[2], '0', strlen(argv[2]));
2945 return -EINVAL;
2946 }
2947
Milan Broz542da312009-12-10 23:51:57 +00002948 ret = crypt_set_key(cc, argv[2]);
2949 if (ret)
2950 return ret;
2951 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
2952 ret = cc->iv_gen_ops->init(cc);
2953 return ret;
2954 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002955 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00002956 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
2957 ret = cc->iv_gen_ops->wipe(cc);
2958 if (ret)
2959 return ret;
2960 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002961 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00002962 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002963 }
2964
2965error:
2966 DMWARN("unrecognised message received.");
2967 return -EINVAL;
2968}
2969
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002970static int crypt_iterate_devices(struct dm_target *ti,
2971 iterate_devices_callout_fn fn, void *data)
2972{
2973 struct crypt_config *cc = ti->private;
2974
Mike Snitzer5dea2712009-07-23 20:30:42 +01002975 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002976}
2977
Mike Snitzer586b2862015-09-09 21:34:51 -04002978static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
2979{
Milan Broz8f0009a2017-03-16 15:39:44 +01002980 struct crypt_config *cc = ti->private;
2981
Mike Snitzer586b2862015-09-09 21:34:51 -04002982 /*
2983 * Unfortunate constraint that is required to avoid the potential
2984 * for exceeding underlying device's max_segments limits -- due to
2985 * crypt_alloc_buffer() possibly allocating pages for the encryption
2986 * bio that are not as physically contiguous as the original bio.
2987 */
2988 limits->max_segment_size = PAGE_SIZE;
Milan Broz8f0009a2017-03-16 15:39:44 +01002989
2990 if (cc->sector_size != (1 << SECTOR_SHIFT)) {
2991 limits->logical_block_size = cc->sector_size;
2992 limits->physical_block_size = cc->sector_size;
2993 blk_limits_io_min(limits, cc->sector_size);
2994 }
Mike Snitzer586b2862015-09-09 21:34:51 -04002995}
2996
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997static struct target_type crypt_target = {
2998 .name = "crypt",
Milan Broz8f0009a2017-03-16 15:39:44 +01002999 .version = {1, 17, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 .module = THIS_MODULE,
3001 .ctr = crypt_ctr,
3002 .dtr = crypt_dtr,
3003 .map = crypt_map,
3004 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07003005 .postsuspend = crypt_postsuspend,
3006 .preresume = crypt_preresume,
3007 .resume = crypt_resume,
3008 .message = crypt_message,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01003009 .iterate_devices = crypt_iterate_devices,
Mike Snitzer586b2862015-09-09 21:34:51 -04003010 .io_hints = crypt_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011};
3012
3013static int __init dm_crypt_init(void)
3014{
3015 int r;
3016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05003018 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07003019 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return r;
3022}
3023
3024static void __exit dm_crypt_exit(void)
3025{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00003026 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027}
3028
3029module_init(dm_crypt_init);
3030module_exit(dm_crypt_exit);
3031
Jana Saoutbf142992014-06-24 14:27:04 -04003032MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
3034MODULE_LICENSE("GPL");