blob: b2e48b26fd4040d3ea8546718b30e0aa0b8dce1f [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 Brozef43aa32017-01-04 20:23:54 +0100132};
133
Andi Kleenc0297722011-01-13 19:59:53 +0000134/*
Mikulas Patocka610f2de2014-02-20 18:01:01 -0500135 * The fields in here must be read only after initialization.
Andi Kleenc0297722011-01-13 19:59:53 +0000136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137struct crypt_config {
138 struct dm_dev *dev;
139 sector_t start;
140
141 /*
Milan Brozef43aa32017-01-04 20:23:54 +0100142 * pool for per bio private data, crypto requests,
143 * encryption requeusts/buffer pages and integrity tags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 */
Milan Brozddd42ed2008-02-08 02:11:07 +0000145 mempool_t *req_pool;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 mempool_t *page_pool;
Milan Brozef43aa32017-01-04 20:23:54 +0100147 mempool_t *tag_pool;
148 unsigned tag_pool_max_sectors;
149
Milan Broz6a24c712006-10-03 01:15:40 -0700150 struct bio_set *bs;
Mikulas Patocka7145c242015-02-13 08:24:41 -0500151 struct mutex bio_alloc_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Milan Brozcabf08e2007-10-19 22:38:58 +0100153 struct workqueue_struct *io_queue;
154 struct workqueue_struct *crypt_queue;
Milan Broz3f1e9072008-03-28 14:16:07 -0700155
Mikulas Patockadc267622015-02-13 08:25:59 -0500156 struct task_struct *write_thread;
157 wait_queue_head_t write_thread_wait;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -0500158 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -0500159
Milan Broz5ebaee62010-08-12 04:14:07 +0100160 char *cipher;
Milan Broz7dbcd132011-01-13 19:59:52 +0000161 char *cipher_string;
Milan Brozef43aa32017-01-04 20:23:54 +0100162 char *cipher_auth;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +0100163 char *key_string;
Milan Broz5ebaee62010-08-12 04:14:07 +0100164
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100165 const struct crypt_iv_operations *iv_gen_ops;
Herbert Xu79066ad2006-12-05 13:41:52 -0800166 union {
Milan Broz60473592009-12-10 23:51:55 +0000167 struct iv_essiv_private essiv;
168 struct iv_benbi_private benbi;
Milan Broz34745782011-01-13 19:59:55 +0000169 struct iv_lmk_private lmk;
Milan Brozed04d982013-10-28 23:21:04 +0100170 struct iv_tcw_private tcw;
Herbert Xu79066ad2006-12-05 13:41:52 -0800171 } iv_gen_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 sector_t iv_offset;
173 unsigned int iv_size;
174
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100175 /* ESSIV: struct crypto_cipher *essiv_tfm */
176 void *iv_private;
Milan Brozef43aa32017-01-04 20:23:54 +0100177 union {
178 struct crypto_skcipher **tfms;
179 struct crypto_aead **tfms_aead;
180 } cipher_tfm;
Milan Brozd1f96422011-01-13 19:59:54 +0000181 unsigned tfms_count;
Milan Brozef43aa32017-01-04 20:23:54 +0100182 unsigned long cipher_flags;
Andi Kleenc0297722011-01-13 19:59:53 +0000183
184 /*
Milan Brozddd42ed2008-02-08 02:11:07 +0000185 * Layout of each crypto request:
186 *
Herbert Xubbdb23b2016-01-24 21:16:36 +0800187 * struct skcipher_request
Milan Brozddd42ed2008-02-08 02:11:07 +0000188 * context
189 * padding
190 * struct dm_crypt_request
191 * padding
192 * IV
193 *
194 * The padding is added so that dm_crypt_request and the IV are
195 * correctly aligned.
196 */
197 unsigned int dmreq_start;
Milan Brozddd42ed2008-02-08 02:11:07 +0000198
Mikulas Patocka298a9fa2014-03-28 15:51:55 -0400199 unsigned int per_bio_data_size;
200
Milan Broze48d4bb2006-10-03 01:15:37 -0700201 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 unsigned int key_size;
Milan Brozda31a072013-10-28 23:21:03 +0100203 unsigned int key_parts; /* independent parts in key buffer */
204 unsigned int key_extra_size; /* additional keys length */
Milan Brozef43aa32017-01-04 20:23:54 +0100205 unsigned int key_mac_size; /* MAC key size for authenc(...) */
206
207 unsigned int integrity_tag_size;
208 unsigned int integrity_iv_size;
209 unsigned int on_disk_tag_size;
210
211 u8 *authenc_key; /* space for keys in authenc() format (if used) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 u8 key[0];
213};
214
Milan Brozef43aa32017-01-04 20:23:54 +0100215#define MIN_IOS 64
216#define MAX_TAG_SIZE 480
217#define POOL_ENTRY_SIZE 512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100219static void clone_init(struct dm_crypt_io *, struct bio *);
Alasdair G Kergon395b1672008-02-08 02:10:52 +0000220static void kcryptd_queue_crypt(struct dm_crypt_io *io);
Milan Brozef43aa32017-01-04 20:23:54 +0100221static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
222 struct scatterlist *sg);
Olaf Kirch027581f2007-05-09 02:32:52 -0700223
Andi Kleenc0297722011-01-13 19:59:53 +0000224/*
225 * Use this to access cipher attributes that are the same for each CPU.
226 */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800227static struct crypto_skcipher *any_tfm(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +0000228{
Milan Brozef43aa32017-01-04 20:23:54 +0100229 return cc->cipher_tfm.tfms[0];
230}
231
232static struct crypto_aead *any_tfm_aead(struct crypt_config *cc)
233{
234 return cc->cipher_tfm.tfms_aead[0];
Andi Kleenc0297722011-01-13 19:59:53 +0000235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * Different IV generation algorithms:
239 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000240 * plain: the initial vector is the 32-bit little-endian version of the sector
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +0200241 * number, padded with zeros if necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 *
Milan Broz61afef62009-12-10 23:52:25 +0000243 * plain64: the initial vector is the 64-bit little-endian version of the sector
244 * number, padded with zeros if necessary.
245 *
Rik Snel3c164bd2006-09-02 18:17:33 +1000246 * essiv: "encrypted sector|salt initial vector", the sector number is
247 * encrypted with the bulk cipher using a salt as key. The salt
248 * should be derived from the bulk cipher's key via hashing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 *
Rik Snel48527fa2006-09-03 08:56:39 +1000250 * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1
251 * (needed for LRW-32-AES and possible other narrow block modes)
252 *
Ludwig Nussel46b47732007-05-09 02:32:55 -0700253 * null: the initial vector is always zero. Provides compatibility with
254 * obsolete loop_fish2 devices. Do not use for new devices.
255 *
Milan Broz34745782011-01-13 19:59:55 +0000256 * lmk: Compatible implementation of the block chaining mode used
257 * by the Loop-AES block device encryption system
258 * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/
259 * It operates on full 512 byte sectors and uses CBC
260 * with an IV derived from the sector number, the data and
261 * optionally extra IV seed.
262 * This means that after decryption the first block
263 * of sector must be tweaked according to decrypted data.
264 * Loop-AES can use three encryption schemes:
265 * version 1: is plain aes-cbc mode
266 * version 2: uses 64 multikey scheme with lmk IV generator
267 * version 3: the same as version 2 with additional IV seed
268 * (it uses 65 keys, last key is used as IV seed)
269 *
Milan Brozed04d982013-10-28 23:21:04 +0100270 * tcw: Compatible implementation of the block chaining mode used
271 * by the TrueCrypt device encryption system (prior to version 4.1).
Milan Broze44f23b2015-04-05 18:03:10 +0200272 * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat
Milan Brozed04d982013-10-28 23:21:04 +0100273 * It operates on full 512 byte sectors and uses CBC
274 * with an IV derived from initial key and the sector number.
275 * In addition, whitening value is applied on every sector, whitening
276 * is calculated from initial key, sector number and mixed using CRC32.
277 * Note that this encryption scheme is vulnerable to watermarking attacks
278 * and should be used for old compatible containers access only.
279 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * plumb: unimplemented, see:
281 * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454
282 */
283
Milan Broz2dc5327d32011-01-13 19:59:54 +0000284static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv,
285 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100288 *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 return 0;
291}
292
Milan Broz61afef62009-12-10 23:52:25 +0000293static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv,
Milan Broz2dc5327d32011-01-13 19:59:54 +0000294 struct dm_crypt_request *dmreq)
Milan Broz61afef62009-12-10 23:52:25 +0000295{
296 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100297 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Milan Broz61afef62009-12-10 23:52:25 +0000298
299 return 0;
300}
301
Milan Brozb95bf2d2009-12-10 23:51:56 +0000302/* Initialise ESSIV - compute salt but no local memory allocations */
303static int crypt_iv_essiv_init(struct crypt_config *cc)
304{
305 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800306 AHASH_REQUEST_ON_STACK(req, essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000307 struct scatterlist sg;
Andi Kleenc0297722011-01-13 19:59:53 +0000308 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100309 int err;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000310
311 sg_init_one(&sg, cc->key, cc->key_size);
Herbert Xubbdb23b2016-01-24 21:16:36 +0800312 ahash_request_set_tfm(req, essiv->hash_tfm);
313 ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL);
314 ahash_request_set_crypt(req, &sg, essiv->salt, cc->key_size);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000315
Herbert Xubbdb23b2016-01-24 21:16:36 +0800316 err = crypto_ahash_digest(req);
317 ahash_request_zero(req);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000318 if (err)
319 return err;
320
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100321 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000322
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100323 err = crypto_cipher_setkey(essiv_tfm, essiv->salt,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800324 crypto_ahash_digestsize(essiv->hash_tfm));
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100325 if (err)
326 return err;
Andi Kleenc0297722011-01-13 19:59:53 +0000327
328 return 0;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000329}
330
Milan Broz542da312009-12-10 23:51:57 +0000331/* Wipe salt and reset key derived from volume key */
332static int crypt_iv_essiv_wipe(struct crypt_config *cc)
333{
334 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800335 unsigned salt_size = crypto_ahash_digestsize(essiv->hash_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000336 struct crypto_cipher *essiv_tfm;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100337 int r, err = 0;
Milan Broz542da312009-12-10 23:51:57 +0000338
339 memset(essiv->salt, 0, salt_size);
340
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100341 essiv_tfm = cc->iv_private;
342 r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size);
343 if (r)
344 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +0000345
346 return err;
347}
348
349/* Set up per cpu cipher state */
350static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc,
351 struct dm_target *ti,
352 u8 *salt, unsigned saltsize)
353{
354 struct crypto_cipher *essiv_tfm;
355 int err;
356
357 /* Setup the essiv_tfm with the given salt */
358 essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC);
359 if (IS_ERR(essiv_tfm)) {
360 ti->error = "Error allocating crypto tfm for ESSIV";
361 return essiv_tfm;
362 }
363
Milan Brozef43aa32017-01-04 20:23:54 +0100364 if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) {
Andi Kleenc0297722011-01-13 19:59:53 +0000365 ti->error = "Block size of ESSIV cipher does "
366 "not match IV size of block cipher";
367 crypto_free_cipher(essiv_tfm);
368 return ERR_PTR(-EINVAL);
369 }
370
371 err = crypto_cipher_setkey(essiv_tfm, salt, saltsize);
372 if (err) {
373 ti->error = "Failed to set key for ESSIV cipher";
374 crypto_free_cipher(essiv_tfm);
375 return ERR_PTR(err);
376 }
377
378 return essiv_tfm;
Milan Broz542da312009-12-10 23:51:57 +0000379}
380
Milan Broz60473592009-12-10 23:51:55 +0000381static void crypt_iv_essiv_dtr(struct crypt_config *cc)
382{
Andi Kleenc0297722011-01-13 19:59:53 +0000383 struct crypto_cipher *essiv_tfm;
Milan Broz60473592009-12-10 23:51:55 +0000384 struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv;
385
Herbert Xubbdb23b2016-01-24 21:16:36 +0800386 crypto_free_ahash(essiv->hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000387 essiv->hash_tfm = NULL;
388
389 kzfree(essiv->salt);
390 essiv->salt = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +0000391
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100392 essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000393
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100394 if (essiv_tfm)
395 crypto_free_cipher(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000396
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100397 cc->iv_private = NULL;
Milan Broz60473592009-12-10 23:51:55 +0000398}
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti,
Milan Brozd469f842007-10-19 22:42:37 +0100401 const char *opts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Milan Broz5861f1b2009-12-10 23:51:56 +0000403 struct crypto_cipher *essiv_tfm = NULL;
Herbert Xubbdb23b2016-01-24 21:16:36 +0800404 struct crypto_ahash *hash_tfm = NULL;
Milan Broz5861f1b2009-12-10 23:51:56 +0000405 u8 *salt = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100406 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Milan Broz5861f1b2009-12-10 23:51:56 +0000408 if (!opts) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700409 ti->error = "Digest algorithm missing for ESSIV mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return -EINVAL;
411 }
412
Milan Brozb95bf2d2009-12-10 23:51:56 +0000413 /* Allocate hash algorithm */
Herbert Xubbdb23b2016-01-24 21:16:36 +0800414 hash_tfm = crypto_alloc_ahash(opts, 0, CRYPTO_ALG_ASYNC);
Herbert Xu35058682006-08-24 19:10:20 +1000415 if (IS_ERR(hash_tfm)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700416 ti->error = "Error initializing ESSIV hash";
Milan Broz5861f1b2009-12-10 23:51:56 +0000417 err = PTR_ERR(hash_tfm);
418 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
Herbert Xubbdb23b2016-01-24 21:16:36 +0800421 salt = kzalloc(crypto_ahash_digestsize(hash_tfm), GFP_KERNEL);
Milan Broz5861f1b2009-12-10 23:51:56 +0000422 if (!salt) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700423 ti->error = "Error kmallocing salt storage in ESSIV";
Milan Broz5861f1b2009-12-10 23:51:56 +0000424 err = -ENOMEM;
425 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
427
Milan Brozb95bf2d2009-12-10 23:51:56 +0000428 cc->iv_gen_private.essiv.salt = salt;
Milan Brozb95bf2d2009-12-10 23:51:56 +0000429 cc->iv_gen_private.essiv.hash_tfm = hash_tfm;
430
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100431 essiv_tfm = setup_essiv_cpu(cc, ti, salt,
Herbert Xubbdb23b2016-01-24 21:16:36 +0800432 crypto_ahash_digestsize(hash_tfm));
Milan Brozef43aa32017-01-04 20:23:54 +0100433
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100434 if (IS_ERR(essiv_tfm)) {
435 crypt_iv_essiv_dtr(cc);
436 return PTR_ERR(essiv_tfm);
Andi Kleenc0297722011-01-13 19:59:53 +0000437 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100438 cc->iv_private = essiv_tfm;
Andi Kleenc0297722011-01-13 19:59:53 +0000439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return 0;
Milan Broz5861f1b2009-12-10 23:51:56 +0000441
442bad:
Milan Broz5861f1b2009-12-10 23:51:56 +0000443 if (hash_tfm && !IS_ERR(hash_tfm))
Herbert Xubbdb23b2016-01-24 21:16:36 +0800444 crypto_free_ahash(hash_tfm);
Milan Brozb95bf2d2009-12-10 23:51:56 +0000445 kfree(salt);
Milan Broz5861f1b2009-12-10 23:51:56 +0000446 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
Milan Broz2dc5327d32011-01-13 19:59:54 +0000449static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv,
450 struct dm_crypt_request *dmreq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Mikulas Patockafd2d2312012-07-27 15:08:05 +0100452 struct crypto_cipher *essiv_tfm = cc->iv_private;
Andi Kleenc0297722011-01-13 19:59:53 +0000453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 memset(iv, 0, cc->iv_size);
Alasdair G Kergon283a8322011-08-02 12:32:01 +0100455 *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector);
Andi Kleenc0297722011-01-13 19:59:53 +0000456 crypto_cipher_encrypt_one(essiv_tfm, iv, iv);
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 0;
459}
460
Rik Snel48527fa2006-09-03 08:56:39 +1000461static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti,
462 const char *opts)
463{
Herbert Xubbdb23b2016-01-24 21:16:36 +0800464 unsigned bs = crypto_skcipher_blocksize(any_tfm(cc));
David Howellsf0d1b0b2006-12-08 02:37:49 -0800465 int log = ilog2(bs);
Rik Snel48527fa2006-09-03 08:56:39 +1000466
467 /* we need to calculate how far we must shift the sector count
468 * to get the cipher block count, we use this shift in _gen */
469
470 if (1 << log != bs) {
471 ti->error = "cypher blocksize is not a power of 2";
472 return -EINVAL;
473 }
474
475 if (log > 9) {
476 ti->error = "cypher blocksize is > 512";
477 return -EINVAL;
478 }
479
Milan Broz60473592009-12-10 23:51:55 +0000480 cc->iv_gen_private.benbi.shift = 9 - log;
Rik Snel48527fa2006-09-03 08:56:39 +1000481
482 return 0;
483}
484
485static void crypt_iv_benbi_dtr(struct crypt_config *cc)
486{
Rik Snel48527fa2006-09-03 08:56:39 +1000487}
488
Milan Broz2dc5327d32011-01-13 19:59:54 +0000489static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv,
490 struct dm_crypt_request *dmreq)
Rik Snel48527fa2006-09-03 08:56:39 +1000491{
Herbert Xu79066ad2006-12-05 13:41:52 -0800492 __be64 val;
493
Rik Snel48527fa2006-09-03 08:56:39 +1000494 memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */
Herbert Xu79066ad2006-12-05 13:41:52 -0800495
Milan Broz2dc5327d32011-01-13 19:59:54 +0000496 val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1);
Herbert Xu79066ad2006-12-05 13:41:52 -0800497 put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64)));
Rik Snel48527fa2006-09-03 08:56:39 +1000498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return 0;
500}
501
Milan Broz2dc5327d32011-01-13 19:59:54 +0000502static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv,
503 struct dm_crypt_request *dmreq)
Ludwig Nussel46b47732007-05-09 02:32:55 -0700504{
505 memset(iv, 0, cc->iv_size);
506
507 return 0;
508}
509
Milan Broz34745782011-01-13 19:59:55 +0000510static void crypt_iv_lmk_dtr(struct crypt_config *cc)
511{
512 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
513
514 if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm))
515 crypto_free_shash(lmk->hash_tfm);
516 lmk->hash_tfm = NULL;
517
518 kzfree(lmk->seed);
519 lmk->seed = NULL;
520}
521
522static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti,
523 const char *opts)
524{
525 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
526
527 lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0);
528 if (IS_ERR(lmk->hash_tfm)) {
529 ti->error = "Error initializing LMK hash";
530 return PTR_ERR(lmk->hash_tfm);
531 }
532
533 /* No seed in LMK version 2 */
534 if (cc->key_parts == cc->tfms_count) {
535 lmk->seed = NULL;
536 return 0;
537 }
538
539 lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL);
540 if (!lmk->seed) {
541 crypt_iv_lmk_dtr(cc);
542 ti->error = "Error kmallocing seed storage in LMK";
543 return -ENOMEM;
544 }
545
546 return 0;
547}
548
549static int crypt_iv_lmk_init(struct crypt_config *cc)
550{
551 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
552 int subkey_size = cc->key_size / cc->key_parts;
553
554 /* LMK seed is on the position of LMK_KEYS + 1 key */
555 if (lmk->seed)
556 memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size),
557 crypto_shash_digestsize(lmk->hash_tfm));
558
559 return 0;
560}
561
562static int crypt_iv_lmk_wipe(struct crypt_config *cc)
563{
564 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
565
566 if (lmk->seed)
567 memset(lmk->seed, 0, LMK_SEED_SIZE);
568
569 return 0;
570}
571
572static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv,
573 struct dm_crypt_request *dmreq,
574 u8 *data)
575{
576 struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200577 SHASH_DESC_ON_STACK(desc, lmk->hash_tfm);
Milan Broz34745782011-01-13 19:59:55 +0000578 struct md5_state md5state;
Milan Brozda31a072013-10-28 23:21:03 +0100579 __le32 buf[4];
Milan Broz34745782011-01-13 19:59:55 +0000580 int i, r;
581
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200582 desc->tfm = lmk->hash_tfm;
583 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Broz34745782011-01-13 19:59:55 +0000584
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200585 r = crypto_shash_init(desc);
Milan Broz34745782011-01-13 19:59:55 +0000586 if (r)
587 return r;
588
589 if (lmk->seed) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200590 r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE);
Milan Broz34745782011-01-13 19:59:55 +0000591 if (r)
592 return r;
593 }
594
595 /* Sector is always 512B, block size 16, add data of blocks 1-31 */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200596 r = crypto_shash_update(desc, data + 16, 16 * 31);
Milan Broz34745782011-01-13 19:59:55 +0000597 if (r)
598 return r;
599
600 /* Sector is cropped to 56 bits here */
601 buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF);
602 buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000);
603 buf[2] = cpu_to_le32(4024);
604 buf[3] = 0;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200605 r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf));
Milan Broz34745782011-01-13 19:59:55 +0000606 if (r)
607 return r;
608
609 /* No MD5 padding here */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200610 r = crypto_shash_export(desc, &md5state);
Milan Broz34745782011-01-13 19:59:55 +0000611 if (r)
612 return r;
613
614 for (i = 0; i < MD5_HASH_WORDS; i++)
615 __cpu_to_le32s(&md5state.hash[i]);
616 memcpy(iv, &md5state.hash, cc->iv_size);
617
618 return 0;
619}
620
621static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv,
622 struct dm_crypt_request *dmreq)
623{
Milan Brozef43aa32017-01-04 20:23:54 +0100624 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000625 u8 *src;
626 int r = 0;
627
628 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100629 sg = crypt_get_sg_data(cc, dmreq->sg_in);
630 src = kmap_atomic(sg_page(sg));
631 r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset);
Cong Wangc2e022c2011-11-28 13:26:02 +0800632 kunmap_atomic(src);
Milan Broz34745782011-01-13 19:59:55 +0000633 } else
634 memset(iv, 0, cc->iv_size);
635
636 return r;
637}
638
639static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv,
640 struct dm_crypt_request *dmreq)
641{
Milan Brozef43aa32017-01-04 20:23:54 +0100642 struct scatterlist *sg;
Milan Broz34745782011-01-13 19:59:55 +0000643 u8 *dst;
644 int r;
645
646 if (bio_data_dir(dmreq->ctx->bio_in) == WRITE)
647 return 0;
648
Milan Brozef43aa32017-01-04 20:23:54 +0100649 sg = crypt_get_sg_data(cc, dmreq->sg_out);
650 dst = kmap_atomic(sg_page(sg));
651 r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset);
Milan Broz34745782011-01-13 19:59:55 +0000652
653 /* Tweak the first block of plaintext sector */
654 if (!r)
Milan Brozef43aa32017-01-04 20:23:54 +0100655 crypto_xor(dst + sg->offset, iv, cc->iv_size);
Milan Broz34745782011-01-13 19:59:55 +0000656
Cong Wangc2e022c2011-11-28 13:26:02 +0800657 kunmap_atomic(dst);
Milan Broz34745782011-01-13 19:59:55 +0000658 return r;
659}
660
Milan Brozed04d982013-10-28 23:21:04 +0100661static void crypt_iv_tcw_dtr(struct crypt_config *cc)
662{
663 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
664
665 kzfree(tcw->iv_seed);
666 tcw->iv_seed = NULL;
667 kzfree(tcw->whitening);
668 tcw->whitening = NULL;
669
670 if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm))
671 crypto_free_shash(tcw->crc32_tfm);
672 tcw->crc32_tfm = NULL;
673}
674
675static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti,
676 const char *opts)
677{
678 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
679
680 if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) {
681 ti->error = "Wrong key size for TCW";
682 return -EINVAL;
683 }
684
685 tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0);
686 if (IS_ERR(tcw->crc32_tfm)) {
687 ti->error = "Error initializing CRC32 in TCW";
688 return PTR_ERR(tcw->crc32_tfm);
689 }
690
691 tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL);
692 tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL);
693 if (!tcw->iv_seed || !tcw->whitening) {
694 crypt_iv_tcw_dtr(cc);
695 ti->error = "Error allocating seed storage in TCW";
696 return -ENOMEM;
697 }
698
699 return 0;
700}
701
702static int crypt_iv_tcw_init(struct crypt_config *cc)
703{
704 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
705 int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE;
706
707 memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size);
708 memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size],
709 TCW_WHITENING_SIZE);
710
711 return 0;
712}
713
714static int crypt_iv_tcw_wipe(struct crypt_config *cc)
715{
716 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
717
718 memset(tcw->iv_seed, 0, cc->iv_size);
719 memset(tcw->whitening, 0, TCW_WHITENING_SIZE);
720
721 return 0;
722}
723
724static int crypt_iv_tcw_whitening(struct crypt_config *cc,
725 struct dm_crypt_request *dmreq,
726 u8 *data)
727{
728 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200729 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100730 u8 buf[TCW_WHITENING_SIZE];
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200731 SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm);
Milan Brozed04d982013-10-28 23:21:04 +0100732 int i, r;
733
734 /* xor whitening with sector number */
735 memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE);
736 crypto_xor(buf, (u8 *)&sector, 8);
737 crypto_xor(&buf[8], (u8 *)&sector, 8);
738
739 /* calculate crc32 for every 32bit part and xor it */
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200740 desc->tfm = tcw->crc32_tfm;
741 desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
Milan Brozed04d982013-10-28 23:21:04 +0100742 for (i = 0; i < 4; i++) {
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200743 r = crypto_shash_init(desc);
Milan Brozed04d982013-10-28 23:21:04 +0100744 if (r)
745 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200746 r = crypto_shash_update(desc, &buf[i * 4], 4);
Milan Brozed04d982013-10-28 23:21:04 +0100747 if (r)
748 goto out;
Jan-Simon Möllerb6106262012-07-02 13:50:54 +0200749 r = crypto_shash_final(desc, &buf[i * 4]);
Milan Brozed04d982013-10-28 23:21:04 +0100750 if (r)
751 goto out;
752 }
753 crypto_xor(&buf[0], &buf[12], 4);
754 crypto_xor(&buf[4], &buf[8], 4);
755
756 /* apply whitening (8 bytes) to whole sector */
757 for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++)
758 crypto_xor(data + i * 8, buf, 8);
759out:
Milan Broz1a71d6f2014-11-22 09:36:04 +0100760 memzero_explicit(buf, sizeof(buf));
Milan Brozed04d982013-10-28 23:21:04 +0100761 return r;
762}
763
764static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv,
765 struct dm_crypt_request *dmreq)
766{
Milan Brozef43aa32017-01-04 20:23:54 +0100767 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100768 struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw;
Bart Van Assche350b5392016-06-28 16:32:32 +0200769 __le64 sector = cpu_to_le64(dmreq->iv_sector);
Milan Brozed04d982013-10-28 23:21:04 +0100770 u8 *src;
771 int r = 0;
772
773 /* Remove whitening from ciphertext */
774 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) {
Milan Brozef43aa32017-01-04 20:23:54 +0100775 sg = crypt_get_sg_data(cc, dmreq->sg_in);
776 src = kmap_atomic(sg_page(sg));
777 r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100778 kunmap_atomic(src);
779 }
780
781 /* Calculate IV */
782 memcpy(iv, tcw->iv_seed, cc->iv_size);
783 crypto_xor(iv, (u8 *)&sector, 8);
784 if (cc->iv_size > 8)
785 crypto_xor(&iv[8], (u8 *)&sector, cc->iv_size - 8);
786
787 return r;
788}
789
790static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv,
791 struct dm_crypt_request *dmreq)
792{
Milan Brozef43aa32017-01-04 20:23:54 +0100793 struct scatterlist *sg;
Milan Brozed04d982013-10-28 23:21:04 +0100794 u8 *dst;
795 int r;
796
797 if (bio_data_dir(dmreq->ctx->bio_in) != WRITE)
798 return 0;
799
800 /* Apply whitening on ciphertext */
Milan Brozef43aa32017-01-04 20:23:54 +0100801 sg = crypt_get_sg_data(cc, dmreq->sg_out);
802 dst = kmap_atomic(sg_page(sg));
803 r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset);
Milan Brozed04d982013-10-28 23:21:04 +0100804 kunmap_atomic(dst);
805
806 return r;
807}
808
Milan Brozef43aa32017-01-04 20:23:54 +0100809static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv,
810 struct dm_crypt_request *dmreq)
811{
812 /* Used only for writes, there must be an additional space to store IV */
813 get_random_bytes(iv, cc->iv_size);
814 return 0;
815}
816
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100817static const struct crypt_iv_operations crypt_iv_plain_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 .generator = crypt_iv_plain_gen
819};
820
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100821static const struct crypt_iv_operations crypt_iv_plain64_ops = {
Milan Broz61afef62009-12-10 23:52:25 +0000822 .generator = crypt_iv_plain64_gen
823};
824
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100825static const struct crypt_iv_operations crypt_iv_essiv_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 .ctr = crypt_iv_essiv_ctr,
827 .dtr = crypt_iv_essiv_dtr,
Milan Brozb95bf2d2009-12-10 23:51:56 +0000828 .init = crypt_iv_essiv_init,
Milan Broz542da312009-12-10 23:51:57 +0000829 .wipe = crypt_iv_essiv_wipe,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 .generator = crypt_iv_essiv_gen
831};
832
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100833static const struct crypt_iv_operations crypt_iv_benbi_ops = {
Rik Snel48527fa2006-09-03 08:56:39 +1000834 .ctr = crypt_iv_benbi_ctr,
835 .dtr = crypt_iv_benbi_dtr,
836 .generator = crypt_iv_benbi_gen
837};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100839static const struct crypt_iv_operations crypt_iv_null_ops = {
Ludwig Nussel46b47732007-05-09 02:32:55 -0700840 .generator = crypt_iv_null_gen
841};
842
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100843static const struct crypt_iv_operations crypt_iv_lmk_ops = {
Milan Broz34745782011-01-13 19:59:55 +0000844 .ctr = crypt_iv_lmk_ctr,
845 .dtr = crypt_iv_lmk_dtr,
846 .init = crypt_iv_lmk_init,
847 .wipe = crypt_iv_lmk_wipe,
848 .generator = crypt_iv_lmk_gen,
849 .post = crypt_iv_lmk_post
850};
851
Julia Lawall1b1b58f2015-11-29 14:09:19 +0100852static const struct crypt_iv_operations crypt_iv_tcw_ops = {
Milan Brozed04d982013-10-28 23:21:04 +0100853 .ctr = crypt_iv_tcw_ctr,
854 .dtr = crypt_iv_tcw_dtr,
855 .init = crypt_iv_tcw_init,
856 .wipe = crypt_iv_tcw_wipe,
857 .generator = crypt_iv_tcw_gen,
858 .post = crypt_iv_tcw_post
859};
860
Milan Brozef43aa32017-01-04 20:23:54 +0100861static struct crypt_iv_operations crypt_iv_random_ops = {
862 .generator = crypt_iv_random_gen
863};
864
865/*
866 * Integrity extensions
867 */
868static bool crypt_integrity_aead(struct crypt_config *cc)
869{
870 return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
871}
872
873static bool crypt_integrity_hmac(struct crypt_config *cc)
874{
Milan Broz33d2f092017-03-16 15:39:40 +0100875 return crypt_integrity_aead(cc) && cc->key_mac_size;
Milan Brozef43aa32017-01-04 20:23:54 +0100876}
877
878/* Get sg containing data */
879static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc,
880 struct scatterlist *sg)
881{
Milan Broz33d2f092017-03-16 15:39:40 +0100882 if (unlikely(crypt_integrity_aead(cc)))
Milan Brozef43aa32017-01-04 20:23:54 +0100883 return &sg[2];
884
885 return sg;
886}
887
888static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio)
889{
890 struct bio_integrity_payload *bip;
891 unsigned int tag_len;
892 int ret;
893
894 if (!bio_sectors(bio) || !io->cc->on_disk_tag_size)
895 return 0;
896
897 bip = bio_integrity_alloc(bio, GFP_NOIO, 1);
898 if (IS_ERR(bip))
899 return PTR_ERR(bip);
900
901 tag_len = io->cc->on_disk_tag_size * bio_sectors(bio);
902
903 bip->bip_iter.bi_size = tag_len;
904 bip->bip_iter.bi_sector = io->cc->start + io->sector;
905
906 /* We own the metadata, do not let bio_free to release it */
907 bip->bip_flags &= ~BIP_BLOCK_INTEGRITY;
908
909 ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata),
910 tag_len, offset_in_page(io->integrity_metadata));
911 if (unlikely(ret != tag_len))
912 return -ENOMEM;
913
914 return 0;
915}
916
917static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti)
918{
919#ifdef CONFIG_BLK_DEV_INTEGRITY
920 struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk);
921
922 /* From now we require underlying device with our integrity profile */
923 if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) {
924 ti->error = "Integrity profile not supported.";
925 return -EINVAL;
926 }
927
928 if (bi->tag_size != cc->on_disk_tag_size) {
929 ti->error = "Integrity profile tag size mismatch.";
930 return -EINVAL;
931 }
932
Milan Broz33d2f092017-03-16 15:39:40 +0100933 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +0100934 cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size;
935 DMINFO("Integrity AEAD, tag size %u, IV size %u.",
936 cc->integrity_tag_size, cc->integrity_iv_size);
937
938 if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) {
939 ti->error = "Integrity AEAD auth tag size is not supported.";
940 return -EINVAL;
941 }
942 } else if (cc->integrity_iv_size)
943 DMINFO("Additional per-sector space %u bytes for IV.",
944 cc->integrity_iv_size);
945
946 if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) {
947 ti->error = "Not enough space for integrity tag in the profile.";
948 return -EINVAL;
949 }
950
951 return 0;
952#else
953 ti->error = "Integrity profile not supported.";
954 return -EINVAL;
955#endif
956}
957
Milan Brozd469f842007-10-19 22:42:37 +0100958static void crypt_convert_init(struct crypt_config *cc,
959 struct convert_context *ctx,
960 struct bio *bio_out, struct bio *bio_in,
Milan Brozfcd369d2008-02-08 02:10:41 +0000961 sector_t sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
963 ctx->bio_in = bio_in;
964 ctx->bio_out = bio_out;
Kent Overstreet003b5c52013-10-11 15:45:43 -0700965 if (bio_in)
966 ctx->iter_in = bio_in->bi_iter;
967 if (bio_out)
968 ctx->iter_out = bio_out->bi_iter;
Mikulas Patockac66029f2012-07-27 15:08:05 +0100969 ctx->cc_sector = sector + cc->iv_offset;
Milan Broz43d69032008-02-08 02:11:09 +0000970 init_completion(&ctx->restart);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Huang Yingb2174ee2009-03-16 17:44:33 +0000973static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc,
Milan Brozef43aa32017-01-04 20:23:54 +0100974 void *req)
Huang Yingb2174ee2009-03-16 17:44:33 +0000975{
976 return (struct dm_crypt_request *)((char *)req + cc->dmreq_start);
977}
978
Milan Brozef43aa32017-01-04 20:23:54 +0100979static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq)
Huang Yingb2174ee2009-03-16 17:44:33 +0000980{
Milan Brozef43aa32017-01-04 20:23:54 +0100981 return (void *)((char *)dmreq - cc->dmreq_start);
Huang Yingb2174ee2009-03-16 17:44:33 +0000982}
983
Milan Broz2dc5327d32011-01-13 19:59:54 +0000984static u8 *iv_of_dmreq(struct crypt_config *cc,
985 struct dm_crypt_request *dmreq)
986{
Milan Broz33d2f092017-03-16 15:39:40 +0100987 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +0100988 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
989 crypto_aead_alignmask(any_tfm_aead(cc)) + 1);
990 else
991 return (u8 *)ALIGN((unsigned long)(dmreq + 1),
992 crypto_skcipher_alignmask(any_tfm(cc)) + 1);
Milan Broz2dc5327d32011-01-13 19:59:54 +0000993}
994
Milan Brozef43aa32017-01-04 20:23:54 +0100995static u8 *org_iv_of_dmreq(struct crypt_config *cc,
996 struct dm_crypt_request *dmreq)
997{
998 return iv_of_dmreq(cc, dmreq) + cc->iv_size;
999}
1000
1001static uint64_t *org_sector_of_dmreq(struct crypt_config *cc,
1002 struct dm_crypt_request *dmreq)
1003{
1004 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size;
1005 return (uint64_t*) ptr;
1006}
1007
1008static unsigned int *org_tag_of_dmreq(struct crypt_config *cc,
1009 struct dm_crypt_request *dmreq)
1010{
1011 u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size +
1012 cc->iv_size + sizeof(uint64_t);
1013 return (unsigned int*)ptr;
1014}
1015
1016static void *tag_from_dmreq(struct crypt_config *cc,
1017 struct dm_crypt_request *dmreq)
1018{
1019 struct convert_context *ctx = dmreq->ctx;
1020 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
1021
1022 return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) *
1023 cc->on_disk_tag_size];
1024}
1025
1026static void *iv_tag_from_dmreq(struct crypt_config *cc,
1027 struct dm_crypt_request *dmreq)
1028{
1029 return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size;
1030}
1031
1032static int crypt_convert_block_aead(struct crypt_config *cc,
1033 struct convert_context *ctx,
1034 struct aead_request *req,
1035 unsigned int tag_offset)
Milan Broz01482b72008-02-08 02:11:04 +00001036{
Kent Overstreet003b5c52013-10-11 15:45:43 -07001037 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1038 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001039 struct dm_crypt_request *dmreq;
Milan Brozef43aa32017-01-04 20:23:54 +01001040 unsigned int data_len = 1 << SECTOR_SHIFT;
1041 u8 *iv, *org_iv, *tag_iv, *tag;
1042 uint64_t *sector;
1043 int r = 0;
1044
1045 BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size);
Milan Broz01482b72008-02-08 02:11:04 +00001046
Huang Yingb2174ee2009-03-16 17:44:33 +00001047 dmreq = dmreq_of_req(cc, req);
Mikulas Patockac66029f2012-07-27 15:08:05 +01001048 dmreq->iv_sector = ctx->cc_sector;
Huang Yingb2174ee2009-03-16 17:44:33 +00001049 dmreq->ctx = ctx;
Milan Broz01482b72008-02-08 02:11:04 +00001050
Milan Brozef43aa32017-01-04 20:23:54 +01001051 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
Milan Broz01482b72008-02-08 02:11:04 +00001052
Milan Brozef43aa32017-01-04 20:23:54 +01001053 sector = org_sector_of_dmreq(cc, dmreq);
1054 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1055
1056 iv = iv_of_dmreq(cc, dmreq);
1057 org_iv = org_iv_of_dmreq(cc, dmreq);
1058 tag = tag_from_dmreq(cc, dmreq);
1059 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1060
1061 /* AEAD request:
1062 * |----- AAD -------|------ DATA -------|-- AUTH TAG --|
1063 * | (authenticated) | (auth+encryption) | |
1064 * | sector_LE | IV | sector in/out | tag in/out |
1065 */
1066 sg_init_table(dmreq->sg_in, 4);
1067 sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t));
1068 sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size);
1069 sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, data_len, bv_in.bv_offset);
1070 sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size);
1071
1072 sg_init_table(dmreq->sg_out, 4);
1073 sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t));
1074 sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size);
1075 sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, data_len, bv_out.bv_offset);
1076 sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size);
Milan Broz01482b72008-02-08 02:11:04 +00001077
Milan Broz3a7f6c92008-02-08 02:11:14 +00001078 if (cc->iv_gen_ops) {
Milan Brozef43aa32017-01-04 20:23:54 +01001079 /* For READs use IV stored in integrity metadata */
1080 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1081 memcpy(org_iv, tag_iv, cc->iv_size);
1082 } else {
1083 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1084 if (r < 0)
1085 return r;
1086 /* Store generated IV in integrity metadata */
1087 if (cc->integrity_iv_size)
1088 memcpy(tag_iv, org_iv, cc->iv_size);
1089 }
1090 /* Working copy of IV, to be modified in crypto API */
1091 memcpy(iv, org_iv, cc->iv_size);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001092 }
1093
Milan Brozef43aa32017-01-04 20:23:54 +01001094 aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size);
1095 if (bio_data_dir(ctx->bio_in) == WRITE) {
1096 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
1097 data_len, iv);
1098 r = crypto_aead_encrypt(req);
1099 if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size)
1100 memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0,
1101 cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size));
1102 } else {
1103 aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out,
1104 data_len + cc->integrity_tag_size, iv);
1105 r = crypto_aead_decrypt(req);
1106 }
1107
1108 if (r == -EBADMSG)
1109 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1110 (unsigned long long)le64_to_cpu(*sector));
1111
1112 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
1113 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1114
1115 bio_advance_iter(ctx->bio_in, &ctx->iter_in, data_len);
1116 bio_advance_iter(ctx->bio_out, &ctx->iter_out, data_len);
1117
1118 return r;
1119}
1120
1121static int crypt_convert_block_skcipher(struct crypt_config *cc,
1122 struct convert_context *ctx,
1123 struct skcipher_request *req,
1124 unsigned int tag_offset)
1125{
1126 struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in);
1127 struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out);
1128 struct scatterlist *sg_in, *sg_out;
1129 struct dm_crypt_request *dmreq;
1130 unsigned int data_len = 1 << SECTOR_SHIFT;
1131 u8 *iv, *org_iv, *tag_iv;
1132 uint64_t *sector;
1133 int r = 0;
1134
1135 dmreq = dmreq_of_req(cc, req);
1136 dmreq->iv_sector = ctx->cc_sector;
1137 dmreq->ctx = ctx;
1138
1139 *org_tag_of_dmreq(cc, dmreq) = tag_offset;
1140
1141 iv = iv_of_dmreq(cc, dmreq);
1142 org_iv = org_iv_of_dmreq(cc, dmreq);
1143 tag_iv = iv_tag_from_dmreq(cc, dmreq);
1144
1145 sector = org_sector_of_dmreq(cc, dmreq);
1146 *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset);
1147
1148 /* For skcipher we use only the first sg item */
1149 sg_in = &dmreq->sg_in[0];
1150 sg_out = &dmreq->sg_out[0];
1151
1152 sg_init_table(sg_in, 1);
1153 sg_set_page(sg_in, bv_in.bv_page, data_len, bv_in.bv_offset);
1154
1155 sg_init_table(sg_out, 1);
1156 sg_set_page(sg_out, bv_out.bv_page, data_len, bv_out.bv_offset);
1157
1158 if (cc->iv_gen_ops) {
1159 /* For READs use IV stored in integrity metadata */
1160 if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) {
1161 memcpy(org_iv, tag_iv, cc->integrity_iv_size);
1162 } else {
1163 r = cc->iv_gen_ops->generator(cc, org_iv, dmreq);
1164 if (r < 0)
1165 return r;
1166 /* Store generated IV in integrity metadata */
1167 if (cc->integrity_iv_size)
1168 memcpy(tag_iv, org_iv, cc->integrity_iv_size);
1169 }
1170 /* Working copy of IV, to be modified in crypto API */
1171 memcpy(iv, org_iv, cc->iv_size);
1172 }
1173
1174 skcipher_request_set_crypt(req, sg_in, sg_out, data_len, iv);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001175
1176 if (bio_data_dir(ctx->bio_in) == WRITE)
Herbert Xubbdb23b2016-01-24 21:16:36 +08001177 r = crypto_skcipher_encrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001178 else
Herbert Xubbdb23b2016-01-24 21:16:36 +08001179 r = crypto_skcipher_decrypt(req);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001180
Milan Broz2dc5327d32011-01-13 19:59:54 +00001181 if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001182 r = cc->iv_gen_ops->post(cc, org_iv, dmreq);
1183
1184 bio_advance_iter(ctx->bio_in, &ctx->iter_in, data_len);
1185 bio_advance_iter(ctx->bio_out, &ctx->iter_out, data_len);
Milan Broz2dc5327d32011-01-13 19:59:54 +00001186
Milan Broz3a7f6c92008-02-08 02:11:14 +00001187 return r;
Milan Broz01482b72008-02-08 02:11:04 +00001188}
1189
Milan Broz95497a92008-02-08 02:11:12 +00001190static void kcryptd_async_done(struct crypto_async_request *async_req,
1191 int error);
Andi Kleenc0297722011-01-13 19:59:53 +00001192
Milan Brozef43aa32017-01-04 20:23:54 +01001193static void crypt_alloc_req_skcipher(struct crypt_config *cc,
1194 struct convert_context *ctx)
Milan Brozddd42ed2008-02-08 02:11:07 +00001195{
Mikulas Patockac66029f2012-07-27 15:08:05 +01001196 unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1);
Andi Kleenc0297722011-01-13 19:59:53 +00001197
Milan Brozef43aa32017-01-04 20:23:54 +01001198 if (!ctx->r.req)
1199 ctx->r.req = mempool_alloc(cc->req_pool, GFP_NOIO);
Andi Kleenc0297722011-01-13 19:59:53 +00001200
Milan Brozef43aa32017-01-04 20:23:54 +01001201 skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]);
Milan Broz54cea3f2015-05-15 17:00:25 +02001202
1203 /*
1204 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1205 * requests if driver request queue is full.
1206 */
Milan Brozef43aa32017-01-04 20:23:54 +01001207 skcipher_request_set_callback(ctx->r.req,
Andi Kleenc0297722011-01-13 19:59:53 +00001208 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Milan Brozef43aa32017-01-04 20:23:54 +01001209 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req));
Milan Brozddd42ed2008-02-08 02:11:07 +00001210}
1211
Milan Brozef43aa32017-01-04 20:23:54 +01001212static void crypt_alloc_req_aead(struct crypt_config *cc,
1213 struct convert_context *ctx)
1214{
1215 if (!ctx->r.req_aead)
1216 ctx->r.req_aead = mempool_alloc(cc->req_pool, GFP_NOIO);
1217
1218 aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]);
1219
1220 /*
1221 * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs
1222 * requests if driver request queue is full.
1223 */
1224 aead_request_set_callback(ctx->r.req_aead,
1225 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
1226 kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead));
1227}
1228
1229static void crypt_alloc_req(struct crypt_config *cc,
1230 struct convert_context *ctx)
1231{
Milan Broz33d2f092017-03-16 15:39:40 +01001232 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001233 crypt_alloc_req_aead(cc, ctx);
1234 else
1235 crypt_alloc_req_skcipher(cc, ctx);
1236}
1237
1238static void crypt_free_req_skcipher(struct crypt_config *cc,
1239 struct skcipher_request *req, struct bio *base_bio)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001240{
1241 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1242
Herbert Xubbdb23b2016-01-24 21:16:36 +08001243 if ((struct skcipher_request *)(io + 1) != req)
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001244 mempool_free(req, cc->req_pool);
1245}
1246
Milan Brozef43aa32017-01-04 20:23:54 +01001247static void crypt_free_req_aead(struct crypt_config *cc,
1248 struct aead_request *req, struct bio *base_bio)
1249{
1250 struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size);
1251
1252 if ((struct aead_request *)(io + 1) != req)
1253 mempool_free(req, cc->req_pool);
1254}
1255
1256static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio)
1257{
Milan Broz33d2f092017-03-16 15:39:40 +01001258 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001259 crypt_free_req_aead(cc, req, base_bio);
1260 else
1261 crypt_free_req_skcipher(cc, req, base_bio);
1262}
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264/*
1265 * Encrypt / decrypt data from one bio to another one (can be the same one)
1266 */
1267static int crypt_convert(struct crypt_config *cc,
Milan Brozd469f842007-10-19 22:42:37 +01001268 struct convert_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
Milan Brozef43aa32017-01-04 20:23:54 +01001270 unsigned int tag_offset = 0;
Milan Broz3f1e9072008-03-28 14:16:07 -07001271 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Mikulas Patocka40b62292012-07-27 15:08:04 +01001273 atomic_set(&ctx->cc_pending, 1);
Milan Brozc8081612008-10-10 13:37:08 +01001274
Kent Overstreet003b5c52013-10-11 15:45:43 -07001275 while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Milan Broz3a7f6c92008-02-08 02:11:14 +00001277 crypt_alloc_req(cc, ctx);
1278
Mikulas Patocka40b62292012-07-27 15:08:04 +01001279 atomic_inc(&ctx->cc_pending);
Milan Broz3f1e9072008-03-28 14:16:07 -07001280
Milan Broz33d2f092017-03-16 15:39:40 +01001281 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001282 r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset);
1283 else
1284 r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001285
1286 switch (r) {
Milan Broz54cea3f2015-05-15 17:00:25 +02001287 /*
1288 * The request was queued by a crypto driver
1289 * but the driver request queue is full, let's wait.
1290 */
Milan Broz3a7f6c92008-02-08 02:11:14 +00001291 case -EBUSY:
1292 wait_for_completion(&ctx->restart);
Wolfram Sang16735d02013-11-14 14:32:02 -08001293 reinit_completion(&ctx->restart);
Milan Broz54cea3f2015-05-15 17:00:25 +02001294 /* fall through */
1295 /*
1296 * The request is queued and processed asynchronously,
1297 * completion function kcryptd_async_done() will be called.
1298 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001299 case -EINPROGRESS:
Milan Brozef43aa32017-01-04 20:23:54 +01001300 ctx->r.req = NULL;
Mikulas Patockac66029f2012-07-27 15:08:05 +01001301 ctx->cc_sector++;
Milan Brozef43aa32017-01-04 20:23:54 +01001302 tag_offset++;
Milan Broz3a7f6c92008-02-08 02:11:14 +00001303 continue;
Milan Broz54cea3f2015-05-15 17:00:25 +02001304 /*
1305 * The request was already processed (synchronously).
1306 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001307 case 0:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001308 atomic_dec(&ctx->cc_pending);
Mikulas Patockac66029f2012-07-27 15:08:05 +01001309 ctx->cc_sector++;
Milan Brozef43aa32017-01-04 20:23:54 +01001310 tag_offset++;
Milan Brozc7f1b202008-07-02 09:34:28 +01001311 cond_resched();
Milan Broz3f1e9072008-03-28 14:16:07 -07001312 continue;
Milan Brozef43aa32017-01-04 20:23:54 +01001313 /*
1314 * There was a data integrity error.
1315 */
1316 case -EBADMSG:
1317 atomic_dec(&ctx->cc_pending);
1318 return -EILSEQ;
1319 /*
1320 * There was an error while processing the request.
1321 */
Milan Broz3f1e9072008-03-28 14:16:07 -07001322 default:
Mikulas Patocka40b62292012-07-27 15:08:04 +01001323 atomic_dec(&ctx->cc_pending);
Milan Brozef43aa32017-01-04 20:23:54 +01001324 return -EIO;
Milan Broz3f1e9072008-03-28 14:16:07 -07001325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327
Milan Broz3f1e9072008-03-28 14:16:07 -07001328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329}
1330
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001331static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone);
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333/*
1334 * Generate a new unfragmented bio with the given size
Mike Snitzer586b2862015-09-09 21:34:51 -04001335 * This should never violate the device limitations (but only because
1336 * max_segment_size is being constrained to PAGE_SIZE).
Mikulas Patocka7145c242015-02-13 08:24:41 -05001337 *
1338 * This function may be called concurrently. If we allocate from the mempool
1339 * concurrently, there is a possibility of deadlock. For example, if we have
1340 * mempool of 256 pages, two processes, each wanting 256, pages allocate from
1341 * the mempool concurrently, it may deadlock in a situation where both processes
1342 * have allocated 128 pages and the mempool is exhausted.
1343 *
1344 * In order to avoid this scenario we allocate the pages under a mutex.
1345 *
1346 * In order to not degrade performance with excessive locking, we try
1347 * non-blocking allocations without a mutex first but on failure we fallback
1348 * to blocking allocations with a mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 */
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001350static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001352 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001353 struct bio *clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001355 gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM;
1356 unsigned i, len, remaining_size;
Milan Broz91e10622007-12-13 14:16:10 +00001357 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Mikulas Patocka7145c242015-02-13 08:24:41 -05001359retry:
Mel Gormand0164ad2015-11-06 16:28:21 -08001360 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001361 mutex_lock(&cc->bio_alloc_lock);
1362
Olaf Kirch2f9941b2007-05-09 02:32:53 -07001363 clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs);
Milan Broz8b004452006-10-03 01:15:37 -07001364 if (!clone)
Milan Brozef43aa32017-01-04 20:23:54 +01001365 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Olaf Kirch027581f2007-05-09 02:32:52 -07001367 clone_init(io, clone);
Milan Broz6a24c712006-10-03 01:15:40 -07001368
Mikulas Patocka7145c242015-02-13 08:24:41 -05001369 remaining_size = size;
1370
Olaf Kirchf97380b2007-05-09 02:32:54 -07001371 for (i = 0; i < nr_iovecs; i++) {
Milan Broz91e10622007-12-13 14:16:10 +00001372 page = mempool_alloc(cc->page_pool, gfp_mask);
Mikulas Patocka7145c242015-02-13 08:24:41 -05001373 if (!page) {
1374 crypt_free_buffer_pages(cc, clone);
1375 bio_put(clone);
Mel Gormand0164ad2015-11-06 16:28:21 -08001376 gfp_mask |= __GFP_DIRECT_RECLAIM;
Mikulas Patocka7145c242015-02-13 08:24:41 -05001377 goto retry;
1378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Mikulas Patocka7145c242015-02-13 08:24:41 -05001380 len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Ming Lei0dae7fe2016-10-29 16:08:06 +08001382 bio_add_page(clone, page, len, 0);
Milan Broz91e10622007-12-13 14:16:10 +00001383
Mikulas Patocka7145c242015-02-13 08:24:41 -05001384 remaining_size -= len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386
Milan Brozef43aa32017-01-04 20:23:54 +01001387 /* Allocate space for integrity tags */
1388 if (dm_crypt_integrity_io_alloc(io, clone)) {
1389 crypt_free_buffer_pages(cc, clone);
1390 bio_put(clone);
1391 clone = NULL;
1392 }
1393out:
Mel Gormand0164ad2015-11-06 16:28:21 -08001394 if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM))
Mikulas Patocka7145c242015-02-13 08:24:41 -05001395 mutex_unlock(&cc->bio_alloc_lock);
1396
Milan Broz8b004452006-10-03 01:15:37 -07001397 return clone;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Neil Brown644bd2f2007-10-16 13:48:46 +02001400static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Neil Brown644bd2f2007-10-16 13:48:46 +02001402 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 struct bio_vec *bv;
1404
Kent Overstreetcb34e052012-09-05 15:22:02 -07001405 bio_for_each_segment_all(bv, clone, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 BUG_ON(!bv->bv_page);
1407 mempool_free(bv->bv_page, cc->page_pool);
1408 bv->bv_page = NULL;
1409 }
1410}
1411
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001412static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc,
1413 struct bio *bio, sector_t sector)
Milan Brozdc440d1e2008-10-10 13:37:03 +01001414{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001415 io->cc = cc;
Milan Brozdc440d1e2008-10-10 13:37:03 +01001416 io->base_bio = bio;
1417 io->sector = sector;
1418 io->error = 0;
Milan Brozef43aa32017-01-04 20:23:54 +01001419 io->ctx.r.req = NULL;
1420 io->integrity_metadata = NULL;
1421 io->integrity_metadata_from_pool = false;
Mikulas Patocka40b62292012-07-27 15:08:04 +01001422 atomic_set(&io->io_pending, 0);
Milan Brozdc440d1e2008-10-10 13:37:03 +01001423}
1424
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001425static void crypt_inc_pending(struct dm_crypt_io *io)
1426{
Mikulas Patocka40b62292012-07-27 15:08:04 +01001427 atomic_inc(&io->io_pending);
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001428}
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430/*
1431 * One of the bios was finished. Check for completion of
1432 * the whole request and correctly clean up the buffer.
1433 */
Milan Broz5742fd72008-02-08 02:10:43 +00001434static void crypt_dec_pending(struct dm_crypt_io *io)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001436 struct crypt_config *cc = io->cc;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001437 struct bio *base_bio = io->base_bio;
Milan Brozb35f8ca2009-03-16 17:44:36 +00001438 int error = io->error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Mikulas Patocka40b62292012-07-27 15:08:04 +01001440 if (!atomic_dec_and_test(&io->io_pending))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return;
1442
Milan Brozef43aa32017-01-04 20:23:54 +01001443 if (io->ctx.r.req)
1444 crypt_free_req(cc, io->ctx.r.req, base_bio);
1445
1446 if (unlikely(io->integrity_metadata_from_pool))
1447 mempool_free(io->integrity_metadata, io->cc->tag_pool);
1448 else
1449 kfree(io->integrity_metadata);
Milan Brozb35f8ca2009-03-16 17:44:36 +00001450
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001451 base_bio->bi_error = error;
1452 bio_endio(base_bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
1455/*
Milan Brozcabf08e2007-10-19 22:38:58 +01001456 * kcryptd/kcryptd_io:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 *
1458 * Needed because it would be very unwise to do decryption in an
Milan Broz23541d22006-10-03 01:15:39 -07001459 * interrupt context.
Milan Brozcabf08e2007-10-19 22:38:58 +01001460 *
1461 * kcryptd performs the actual encryption or decryption.
1462 *
1463 * kcryptd_io performs the IO submission.
1464 *
1465 * They must be separated as otherwise the final stages could be
1466 * starved by new requests which can block in the first stages due
1467 * to memory allocation.
Andi Kleenc0297722011-01-13 19:59:53 +00001468 *
1469 * The work is done per CPU global for all dm-crypt instances.
1470 * They should not depend on each other and do not block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02001472static void crypt_endio(struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001473{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001474 struct dm_crypt_io *io = clone->bi_private;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001475 struct crypt_config *cc = io->cc;
Milan Brozee7a4912008-02-08 02:10:46 +00001476 unsigned rw = bio_data_dir(clone);
Sasha Levin9b81c842015-08-10 19:05:18 -04001477 int error;
Milan Broz8b004452006-10-03 01:15:37 -07001478
1479 /*
NeilBrown6712ecf2007-09-27 12:47:43 +02001480 * free the processed pages
Milan Broz8b004452006-10-03 01:15:37 -07001481 */
Milan Brozee7a4912008-02-08 02:10:46 +00001482 if (rw == WRITE)
Neil Brown644bd2f2007-10-16 13:48:46 +02001483 crypt_free_buffer_pages(cc, clone);
Milan Brozee7a4912008-02-08 02:10:46 +00001484
Sasha Levin9b81c842015-08-10 19:05:18 -04001485 error = clone->bi_error;
Milan Brozee7a4912008-02-08 02:10:46 +00001486 bio_put(clone);
1487
Sasha Levin9b81c842015-08-10 19:05:18 -04001488 if (rw == READ && !error) {
Milan Brozee7a4912008-02-08 02:10:46 +00001489 kcryptd_queue_crypt(io);
1490 return;
NeilBrown6712ecf2007-09-27 12:47:43 +02001491 }
Milan Broz8b004452006-10-03 01:15:37 -07001492
Sasha Levin9b81c842015-08-10 19:05:18 -04001493 if (unlikely(error))
1494 io->error = error;
Milan Broz5742fd72008-02-08 02:10:43 +00001495
1496 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001497}
1498
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001499static void clone_init(struct dm_crypt_io *io, struct bio *clone)
Milan Broz8b004452006-10-03 01:15:37 -07001500{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001501 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001502
1503 clone->bi_private = io;
1504 clone->bi_end_io = crypt_endio;
1505 clone->bi_bdev = cc->dev->bdev;
Christoph Hellwigef295ec2016-10-28 08:48:16 -06001506 clone->bi_opf = io->base_bio->bi_opf;
Milan Broz8b004452006-10-03 01:15:37 -07001507}
1508
Milan Broz20c82532011-01-13 19:59:53 +00001509static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp)
Milan Broz8b004452006-10-03 01:15:37 -07001510{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001511 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001512 struct bio *clone;
Milan Broz93e605c2006-10-03 01:15:38 -07001513
Milan Broz8b004452006-10-03 01:15:37 -07001514 /*
Mike Snitzer59779072015-04-09 16:53:24 -04001515 * We need the original biovec array in order to decrypt
1516 * the whole bio data *afterwards* -- thanks to immutable
1517 * biovecs we don't need to worry about the block layer
1518 * modifying the biovec array; so leverage bio_clone_fast().
Milan Broz8b004452006-10-03 01:15:37 -07001519 */
Mike Snitzer59779072015-04-09 16:53:24 -04001520 clone = bio_clone_fast(io->base_bio, gfp, cc->bs);
Jens Axboe7eaceac2011-03-10 08:52:07 +01001521 if (!clone)
Milan Broz20c82532011-01-13 19:59:53 +00001522 return 1;
Milan Broz8b004452006-10-03 01:15:37 -07001523
Milan Broz20c82532011-01-13 19:59:53 +00001524 crypt_inc_pending(io);
1525
Milan Broz8b004452006-10-03 01:15:37 -07001526 clone_init(io, clone);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001527 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz8b004452006-10-03 01:15:37 -07001528
Milan Brozef43aa32017-01-04 20:23:54 +01001529 if (dm_crypt_integrity_io_alloc(io, clone)) {
1530 crypt_dec_pending(io);
1531 bio_put(clone);
1532 return 1;
1533 }
1534
Milan Broz93e605c2006-10-03 01:15:38 -07001535 generic_make_request(clone);
Milan Broz20c82532011-01-13 19:59:53 +00001536 return 0;
Milan Broz8b004452006-10-03 01:15:37 -07001537}
1538
Mikulas Patockadc267622015-02-13 08:25:59 -05001539static void kcryptd_io_read_work(struct work_struct *work)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001540{
1541 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1542
Mikulas Patockadc267622015-02-13 08:25:59 -05001543 crypt_inc_pending(io);
1544 if (kcryptd_io_read(io, GFP_NOIO))
1545 io->error = -ENOMEM;
1546 crypt_dec_pending(io);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001547}
1548
Mikulas Patockadc267622015-02-13 08:25:59 -05001549static void kcryptd_queue_read(struct dm_crypt_io *io)
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001550{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001551 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001552
Mikulas Patockadc267622015-02-13 08:25:59 -05001553 INIT_WORK(&io->work, kcryptd_io_read_work);
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001554 queue_work(cc->io_queue, &io->work);
1555}
1556
Mikulas Patockadc267622015-02-13 08:25:59 -05001557static void kcryptd_io_write(struct dm_crypt_io *io)
1558{
1559 struct bio *clone = io->ctx.bio_out;
1560
1561 generic_make_request(clone);
1562}
1563
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001564#define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node)
1565
Mikulas Patockadc267622015-02-13 08:25:59 -05001566static int dmcrypt_write(void *data)
1567{
1568 struct crypt_config *cc = data;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001569 struct dm_crypt_io *io;
1570
Mikulas Patockadc267622015-02-13 08:25:59 -05001571 while (1) {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001572 struct rb_root write_tree;
Mikulas Patockadc267622015-02-13 08:25:59 -05001573 struct blk_plug plug;
1574
1575 DECLARE_WAITQUEUE(wait, current);
1576
1577 spin_lock_irq(&cc->write_thread_wait.lock);
1578continue_locked:
1579
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001580 if (!RB_EMPTY_ROOT(&cc->write_tree))
Mikulas Patockadc267622015-02-13 08:25:59 -05001581 goto pop_from_list;
1582
Rabin Vincentf659b102016-09-21 16:22:29 +02001583 set_current_state(TASK_INTERRUPTIBLE);
Mikulas Patockadc267622015-02-13 08:25:59 -05001584 __add_wait_queue(&cc->write_thread_wait, &wait);
1585
1586 spin_unlock_irq(&cc->write_thread_wait.lock);
1587
Rabin Vincentf659b102016-09-21 16:22:29 +02001588 if (unlikely(kthread_should_stop())) {
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001589 set_current_state(TASK_RUNNING);
Rabin Vincentf659b102016-09-21 16:22:29 +02001590 remove_wait_queue(&cc->write_thread_wait, &wait);
1591 break;
1592 }
1593
Mikulas Patockadc267622015-02-13 08:25:59 -05001594 schedule();
1595
Davidlohr Bueso642fa442017-01-03 13:43:14 -08001596 set_current_state(TASK_RUNNING);
Mikulas Patockadc267622015-02-13 08:25:59 -05001597 spin_lock_irq(&cc->write_thread_wait.lock);
1598 __remove_wait_queue(&cc->write_thread_wait, &wait);
1599 goto continue_locked;
1600
1601pop_from_list:
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001602 write_tree = cc->write_tree;
1603 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05001604 spin_unlock_irq(&cc->write_thread_wait.lock);
1605
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001606 BUG_ON(rb_parent(write_tree.rb_node));
1607
1608 /*
1609 * Note: we cannot walk the tree here with rb_next because
1610 * the structures may be freed when kcryptd_io_write is called.
1611 */
Mikulas Patockadc267622015-02-13 08:25:59 -05001612 blk_start_plug(&plug);
1613 do {
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001614 io = crypt_io_from_node(rb_first(&write_tree));
1615 rb_erase(&io->rb_node, &write_tree);
Mikulas Patockadc267622015-02-13 08:25:59 -05001616 kcryptd_io_write(io);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001617 } while (!RB_EMPTY_ROOT(&write_tree));
Mikulas Patockadc267622015-02-13 08:25:59 -05001618 blk_finish_plug(&plug);
1619 }
1620 return 0;
1621}
1622
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001623static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async)
Milan Broz4e4eef62008-02-08 02:10:49 +00001624{
Milan Brozdec1ced2008-02-08 02:10:57 +00001625 struct bio *clone = io->ctx.bio_out;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001626 struct crypt_config *cc = io->cc;
Mikulas Patockadc267622015-02-13 08:25:59 -05001627 unsigned long flags;
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001628 sector_t sector;
1629 struct rb_node **rbp, *parent;
Milan Brozdec1ced2008-02-08 02:10:57 +00001630
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001631 if (unlikely(io->error < 0)) {
Milan Brozdec1ced2008-02-08 02:10:57 +00001632 crypt_free_buffer_pages(cc, clone);
1633 bio_put(clone);
Milan Broz6c031f42008-10-10 13:37:06 +01001634 crypt_dec_pending(io);
Milan Brozdec1ced2008-02-08 02:10:57 +00001635 return;
1636 }
1637
1638 /* crypt_convert should have filled the clone bio */
Kent Overstreet003b5c52013-10-11 15:45:43 -07001639 BUG_ON(io->ctx.iter_out.bi_size);
Milan Brozdec1ced2008-02-08 02:10:57 +00001640
Kent Overstreet4f024f32013-10-11 15:44:27 -07001641 clone->bi_iter.bi_sector = cc->start + io->sector;
Milan Broz899c95d2008-02-08 02:11:02 +00001642
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05001643 if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) {
1644 generic_make_request(clone);
1645 return;
1646 }
1647
Mikulas Patockadc267622015-02-13 08:25:59 -05001648 spin_lock_irqsave(&cc->write_thread_wait.lock, flags);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05001649 rbp = &cc->write_tree.rb_node;
1650 parent = NULL;
1651 sector = io->sector;
1652 while (*rbp) {
1653 parent = *rbp;
1654 if (sector < crypt_io_from_node(parent)->sector)
1655 rbp = &(*rbp)->rb_left;
1656 else
1657 rbp = &(*rbp)->rb_right;
1658 }
1659 rb_link_node(&io->rb_node, parent, rbp);
1660 rb_insert_color(&io->rb_node, &cc->write_tree);
1661
Mikulas Patockadc267622015-02-13 08:25:59 -05001662 wake_up_locked(&cc->write_thread_wait);
1663 spin_unlock_irqrestore(&cc->write_thread_wait.lock, flags);
Milan Broz4e4eef62008-02-08 02:10:49 +00001664}
1665
Milan Brozfc5a5e92008-10-10 13:37:04 +01001666static void kcryptd_crypt_write_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001667{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001668 struct crypt_config *cc = io->cc;
Milan Broz8b004452006-10-03 01:15:37 -07001669 struct bio *clone;
Milan Brozc8081612008-10-10 13:37:08 +01001670 int crypt_finished;
Milan Brozb635b002008-10-21 17:45:00 +01001671 sector_t sector = io->sector;
Milan Brozdec1ced2008-02-08 02:10:57 +00001672 int r;
Milan Broz8b004452006-10-03 01:15:37 -07001673
Milan Broz93e605c2006-10-03 01:15:38 -07001674 /*
Milan Brozfc5a5e92008-10-10 13:37:04 +01001675 * Prevent io from disappearing until this function completes.
1676 */
1677 crypt_inc_pending(io);
Milan Brozb635b002008-10-21 17:45:00 +01001678 crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector);
Milan Brozfc5a5e92008-10-10 13:37:04 +01001679
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001680 clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size);
1681 if (unlikely(!clone)) {
1682 io->error = -EIO;
1683 goto dec;
Milan Broz8b004452006-10-03 01:15:37 -07001684 }
Milan Broz899c95d2008-02-08 02:11:02 +00001685
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001686 io->ctx.bio_out = clone;
1687 io->ctx.iter_out = clone->bi_iter;
1688
1689 sector += bio_sectors(clone);
1690
1691 crypt_inc_pending(io);
1692 r = crypt_convert(cc, &io->ctx);
Milan Brozef43aa32017-01-04 20:23:54 +01001693 if (r < 0)
1694 io->error = r;
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05001695 crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending);
1696
1697 /* Encryption was already finished, submit io now */
1698 if (crypt_finished) {
1699 kcryptd_crypt_write_io_submit(io, 0);
1700 io->sector = sector;
1701 }
1702
1703dec:
Milan Broz899c95d2008-02-08 02:11:02 +00001704 crypt_dec_pending(io);
Milan Broz84131db2008-02-08 02:10:59 +00001705}
1706
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001707static void kcryptd_crypt_read_done(struct dm_crypt_io *io)
Milan Broz5742fd72008-02-08 02:10:43 +00001708{
Milan Broz5742fd72008-02-08 02:10:43 +00001709 crypt_dec_pending(io);
1710}
1711
Milan Broz4e4eef62008-02-08 02:10:49 +00001712static void kcryptd_crypt_read_convert(struct dm_crypt_io *io)
Milan Broz8b004452006-10-03 01:15:37 -07001713{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001714 struct crypt_config *cc = io->cc;
Milan Broz5742fd72008-02-08 02:10:43 +00001715 int r = 0;
Milan Broz8b004452006-10-03 01:15:37 -07001716
Milan Broz3e1a8bd2008-10-10 13:37:02 +01001717 crypt_inc_pending(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001718
Milan Broz53017032008-02-08 02:10:38 +00001719 crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio,
Milan Broz0c395b02008-02-08 02:10:54 +00001720 io->sector);
Milan Broz8b004452006-10-03 01:15:37 -07001721
Milan Broz5742fd72008-02-08 02:10:43 +00001722 r = crypt_convert(cc, &io->ctx);
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001723 if (r < 0)
Milan Brozef43aa32017-01-04 20:23:54 +01001724 io->error = r;
Milan Broz5742fd72008-02-08 02:10:43 +00001725
Mikulas Patocka40b62292012-07-27 15:08:04 +01001726 if (atomic_dec_and_test(&io->ctx.cc_pending))
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001727 kcryptd_crypt_read_done(io);
Milan Broz3a7f6c92008-02-08 02:11:14 +00001728
1729 crypt_dec_pending(io);
Milan Broz8b004452006-10-03 01:15:37 -07001730}
1731
Milan Broz95497a92008-02-08 02:11:12 +00001732static void kcryptd_async_done(struct crypto_async_request *async_req,
1733 int error)
1734{
Huang Yingb2174ee2009-03-16 17:44:33 +00001735 struct dm_crypt_request *dmreq = async_req->data;
1736 struct convert_context *ctx = dmreq->ctx;
Milan Broz95497a92008-02-08 02:11:12 +00001737 struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx);
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001738 struct crypt_config *cc = io->cc;
Milan Broz95497a92008-02-08 02:11:12 +00001739
Milan Broz54cea3f2015-05-15 17:00:25 +02001740 /*
1741 * A request from crypto driver backlog is going to be processed now,
1742 * finish the completion and continue in crypt_convert().
1743 * (Callback will be called for the second time for this request.)
1744 */
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001745 if (error == -EINPROGRESS) {
1746 complete(&ctx->restart);
Milan Broz95497a92008-02-08 02:11:12 +00001747 return;
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001748 }
Milan Broz95497a92008-02-08 02:11:12 +00001749
Milan Broz2dc5327d32011-01-13 19:59:54 +00001750 if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post)
Milan Brozef43aa32017-01-04 20:23:54 +01001751 error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq);
Milan Broz2dc5327d32011-01-13 19:59:54 +00001752
Milan Brozef43aa32017-01-04 20:23:54 +01001753 if (error == -EBADMSG) {
1754 DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu",
1755 (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)));
1756 io->error = -EILSEQ;
1757 } else if (error < 0)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001758 io->error = -EIO;
1759
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04001760 crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio);
Milan Broz95497a92008-02-08 02:11:12 +00001761
Mikulas Patocka40b62292012-07-27 15:08:04 +01001762 if (!atomic_dec_and_test(&ctx->cc_pending))
Rabin Vincentc0403ec2015-05-05 15:15:56 +02001763 return;
Milan Broz95497a92008-02-08 02:11:12 +00001764
1765 if (bio_data_dir(io->base_bio) == READ)
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001766 kcryptd_crypt_read_done(io);
Milan Broz95497a92008-02-08 02:11:12 +00001767 else
Mikulas Patocka72c6e7a2012-03-28 18:41:22 +01001768 kcryptd_crypt_write_io_submit(io, 1);
Milan Broz95497a92008-02-08 02:11:12 +00001769}
1770
Milan Broz4e4eef62008-02-08 02:10:49 +00001771static void kcryptd_crypt(struct work_struct *work)
1772{
1773 struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work);
1774
1775 if (bio_data_dir(io->base_bio) == READ)
1776 kcryptd_crypt_read_convert(io);
1777 else
1778 kcryptd_crypt_write_convert(io);
Milan Broz8b004452006-10-03 01:15:37 -07001779}
1780
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001781static void kcryptd_queue_crypt(struct dm_crypt_io *io)
1782{
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01001783 struct crypt_config *cc = io->cc;
Alasdair G Kergon395b1672008-02-08 02:10:52 +00001784
1785 INIT_WORK(&io->work, kcryptd_crypt);
1786 queue_work(cc->crypt_queue, &io->work);
1787}
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789/*
1790 * Decode key from its hex representation
1791 */
1792static int crypt_decode_key(u8 *key, char *hex, unsigned int size)
1793{
1794 char buffer[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 unsigned int i;
1796
1797 buffer[2] = '\0';
1798
Milan Broz8b004452006-10-03 01:15:37 -07001799 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 buffer[0] = *hex++;
1801 buffer[1] = *hex++;
1802
majianpeng1a66a082012-07-27 15:07:59 +01001803 if (kstrtou8(buffer, 16, &key[i]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return -EINVAL;
1805 }
1806
1807 if (*hex != '\0')
1808 return -EINVAL;
1809
1810 return 0;
1811}
1812
Milan Brozef43aa32017-01-04 20:23:54 +01001813static void crypt_free_tfms_aead(struct crypt_config *cc)
1814{
1815 if (!cc->cipher_tfm.tfms_aead)
1816 return;
1817
1818 if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1819 crypto_free_aead(cc->cipher_tfm.tfms_aead[0]);
1820 cc->cipher_tfm.tfms_aead[0] = NULL;
1821 }
1822
1823 kfree(cc->cipher_tfm.tfms_aead);
1824 cc->cipher_tfm.tfms_aead = NULL;
1825}
1826
1827static void crypt_free_tfms_skcipher(struct crypt_config *cc)
Milan Brozd1f96422011-01-13 19:59:54 +00001828{
Milan Brozd1f96422011-01-13 19:59:54 +00001829 unsigned i;
1830
Milan Brozef43aa32017-01-04 20:23:54 +01001831 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001832 return;
1833
Milan Brozd1f96422011-01-13 19:59:54 +00001834 for (i = 0; i < cc->tfms_count; i++)
Milan Brozef43aa32017-01-04 20:23:54 +01001835 if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) {
1836 crypto_free_skcipher(cc->cipher_tfm.tfms[i]);
1837 cc->cipher_tfm.tfms[i] = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001838 }
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001839
Milan Brozef43aa32017-01-04 20:23:54 +01001840 kfree(cc->cipher_tfm.tfms);
1841 cc->cipher_tfm.tfms = NULL;
Milan Brozd1f96422011-01-13 19:59:54 +00001842}
1843
Milan Brozef43aa32017-01-04 20:23:54 +01001844static void crypt_free_tfms(struct crypt_config *cc)
1845{
Milan Broz33d2f092017-03-16 15:39:40 +01001846 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001847 crypt_free_tfms_aead(cc);
1848 else
1849 crypt_free_tfms_skcipher(cc);
1850}
1851
1852static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode)
Milan Brozd1f96422011-01-13 19:59:54 +00001853{
Milan Brozd1f96422011-01-13 19:59:54 +00001854 unsigned i;
1855 int err;
1856
Milan Brozef43aa32017-01-04 20:23:54 +01001857 cc->cipher_tfm.tfms = kzalloc(cc->tfms_count *
1858 sizeof(struct crypto_skcipher *), GFP_KERNEL);
1859 if (!cc->cipher_tfm.tfms)
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001860 return -ENOMEM;
1861
Milan Brozd1f96422011-01-13 19:59:54 +00001862 for (i = 0; i < cc->tfms_count; i++) {
Milan Brozef43aa32017-01-04 20:23:54 +01001863 cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0);
1864 if (IS_ERR(cc->cipher_tfm.tfms[i])) {
1865 err = PTR_ERR(cc->cipher_tfm.tfms[i]);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001866 crypt_free_tfms(cc);
Milan Brozd1f96422011-01-13 19:59:54 +00001867 return err;
1868 }
1869 }
1870
1871 return 0;
1872}
1873
Milan Brozef43aa32017-01-04 20:23:54 +01001874static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode)
1875{
Milan Brozef43aa32017-01-04 20:23:54 +01001876 int err;
1877
1878 cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL);
1879 if (!cc->cipher_tfm.tfms)
1880 return -ENOMEM;
1881
Milan Brozef43aa32017-01-04 20:23:54 +01001882 cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0);
1883 if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) {
1884 err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]);
1885 crypt_free_tfms(cc);
1886 return err;
1887 }
1888
Milan Brozef43aa32017-01-04 20:23:54 +01001889 return 0;
1890}
1891
1892static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode)
1893{
Milan Broz33d2f092017-03-16 15:39:40 +01001894 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001895 return crypt_alloc_tfms_aead(cc, ciphermode);
1896 else
1897 return crypt_alloc_tfms_skcipher(cc, ciphermode);
1898}
1899
1900static unsigned crypt_subkey_size(struct crypt_config *cc)
1901{
1902 return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count);
1903}
1904
1905static unsigned crypt_authenckey_size(struct crypt_config *cc)
1906{
1907 return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param));
1908}
1909
1910/*
1911 * If AEAD is composed like authenc(hmac(sha256),xts(aes)),
1912 * the key must be for some reason in special format.
1913 * This funcion converts cc->key to this special format.
1914 */
1915static void crypt_copy_authenckey(char *p, const void *key,
1916 unsigned enckeylen, unsigned authkeylen)
1917{
1918 struct crypto_authenc_key_param *param;
1919 struct rtattr *rta;
1920
1921 rta = (struct rtattr *)p;
1922 param = RTA_DATA(rta);
1923 param->enckeylen = cpu_to_be32(enckeylen);
1924 rta->rta_len = RTA_LENGTH(sizeof(*param));
1925 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
1926 p += RTA_SPACE(sizeof(*param));
1927 memcpy(p, key + enckeylen, authkeylen);
1928 p += authkeylen;
1929 memcpy(p, key, enckeylen);
1930}
1931
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04001932static int crypt_setkey(struct crypt_config *cc)
Andi Kleenc0297722011-01-13 19:59:53 +00001933{
Milan Brozda31a072013-10-28 23:21:03 +01001934 unsigned subkey_size;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001935 int err = 0, i, r;
Andi Kleenc0297722011-01-13 19:59:53 +00001936
Milan Brozda31a072013-10-28 23:21:03 +01001937 /* Ignore extra keys (which are used for IV etc) */
Milan Brozef43aa32017-01-04 20:23:54 +01001938 subkey_size = crypt_subkey_size(cc);
Milan Brozda31a072013-10-28 23:21:03 +01001939
Milan Brozef43aa32017-01-04 20:23:54 +01001940 if (crypt_integrity_hmac(cc))
1941 crypt_copy_authenckey(cc->authenc_key, cc->key,
1942 subkey_size - cc->key_mac_size,
1943 cc->key_mac_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001944 for (i = 0; i < cc->tfms_count; i++) {
Milan Broz33d2f092017-03-16 15:39:40 +01001945 if (crypt_integrity_hmac(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01001946 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1947 cc->authenc_key, crypt_authenckey_size(cc));
Milan Broz33d2f092017-03-16 15:39:40 +01001948 else if (crypt_integrity_aead(cc))
1949 r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i],
1950 cc->key + (i * subkey_size),
1951 subkey_size);
Milan Brozef43aa32017-01-04 20:23:54 +01001952 else
1953 r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i],
1954 cc->key + (i * subkey_size),
1955 subkey_size);
Mikulas Patockafd2d2312012-07-27 15:08:05 +01001956 if (r)
1957 err = r;
Andi Kleenc0297722011-01-13 19:59:53 +00001958 }
1959
Milan Brozef43aa32017-01-04 20:23:54 +01001960 if (crypt_integrity_hmac(cc))
1961 memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc));
1962
Andi Kleenc0297722011-01-13 19:59:53 +00001963 return err;
1964}
1965
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001966#ifdef CONFIG_KEYS
1967
Ondrej Kozina027c4312016-12-01 18:20:52 +01001968static bool contains_whitespace(const char *str)
1969{
1970 while (*str)
1971 if (isspace(*str++))
1972 return true;
1973 return false;
1974}
1975
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001976static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
1977{
1978 char *new_key_string, *key_desc;
1979 int ret;
1980 struct key *key;
1981 const struct user_key_payload *ukp;
1982
Ondrej Kozina027c4312016-12-01 18:20:52 +01001983 /*
1984 * Reject key_string with whitespace. dm core currently lacks code for
1985 * proper whitespace escaping in arguments on DM_TABLE_STATUS path.
1986 */
1987 if (contains_whitespace(key_string)) {
1988 DMERR("whitespace chars not allowed in key string");
1989 return -EINVAL;
1990 }
1991
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01001992 /* look for next ':' separating key_type from key_description */
1993 key_desc = strpbrk(key_string, ":");
1994 if (!key_desc || key_desc == key_string || !strlen(key_desc + 1))
1995 return -EINVAL;
1996
1997 if (strncmp(key_string, "logon:", key_desc - key_string + 1) &&
1998 strncmp(key_string, "user:", key_desc - key_string + 1))
1999 return -EINVAL;
2000
2001 new_key_string = kstrdup(key_string, GFP_KERNEL);
2002 if (!new_key_string)
2003 return -ENOMEM;
2004
2005 key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user,
2006 key_desc + 1, NULL);
2007 if (IS_ERR(key)) {
2008 kzfree(new_key_string);
2009 return PTR_ERR(key);
2010 }
2011
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002012 down_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002013
David Howells0837e492017-03-01 15:11:23 +00002014 ukp = user_key_payload_locked(key);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002015 if (!ukp) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002016 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002017 key_put(key);
2018 kzfree(new_key_string);
2019 return -EKEYREVOKED;
2020 }
2021
2022 if (cc->key_size != ukp->datalen) {
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002023 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002024 key_put(key);
2025 kzfree(new_key_string);
2026 return -EINVAL;
2027 }
2028
2029 memcpy(cc->key, ukp->data, cc->key_size);
2030
Ondrej Kozinaf5b0cba2017-01-31 15:47:11 +01002031 up_read(&key->sem);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002032 key_put(key);
2033
2034 /* clear the flag since following operations may invalidate previously valid key */
2035 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2036
2037 ret = crypt_setkey(cc);
2038
2039 /* wipe the kernel key payload copy in each case */
2040 memset(cc->key, 0, cc->key_size * sizeof(u8));
2041
2042 if (!ret) {
2043 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2044 kzfree(cc->key_string);
2045 cc->key_string = new_key_string;
2046 } else
2047 kzfree(new_key_string);
2048
2049 return ret;
2050}
2051
2052static int get_key_size(char **key_string)
2053{
2054 char *colon, dummy;
2055 int ret;
2056
2057 if (*key_string[0] != ':')
2058 return strlen(*key_string) >> 1;
2059
2060 /* look for next ':' in key string */
2061 colon = strpbrk(*key_string + 1, ":");
2062 if (!colon)
2063 return -EINVAL;
2064
2065 if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':')
2066 return -EINVAL;
2067
2068 *key_string = colon;
2069
2070 /* remaining key string should be :<logon|user>:<key_desc> */
2071
2072 return ret;
2073}
2074
2075#else
2076
2077static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string)
2078{
2079 return -EINVAL;
2080}
2081
2082static int get_key_size(char **key_string)
2083{
2084 return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1;
2085}
2086
2087#endif
2088
Milan Broze48d4bb2006-10-03 01:15:37 -07002089static int crypt_set_key(struct crypt_config *cc, char *key)
2090{
Milan Brozde8be5a2011-03-24 13:54:27 +00002091 int r = -EINVAL;
2092 int key_string_len = strlen(key);
2093
Milan Broz69a8cfc2011-01-13 19:59:49 +00002094 /* Hyphen (which gives a key_size of zero) means there is no key. */
2095 if (!cc->key_size && strcmp(key, "-"))
Milan Brozde8be5a2011-03-24 13:54:27 +00002096 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002097
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002098 /* ':' means the key is in kernel keyring, short-circuit normal key processing */
2099 if (key[0] == ':') {
2100 r = crypt_set_keyring_key(cc, key + 1);
2101 goto out;
2102 }
2103
Ondrej Kozina265e9092016-11-02 15:02:08 +01002104 /* clear the flag since following operations may invalidate previously valid key */
2105 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2106
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002107 /* wipe references to any kernel keyring key */
2108 kzfree(cc->key_string);
2109 cc->key_string = NULL;
2110
Milan Broz69a8cfc2011-01-13 19:59:49 +00002111 if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0)
Milan Brozde8be5a2011-03-24 13:54:27 +00002112 goto out;
Milan Broze48d4bb2006-10-03 01:15:37 -07002113
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002114 r = crypt_setkey(cc);
Ondrej Kozina265e9092016-11-02 15:02:08 +01002115 if (!r)
2116 set_bit(DM_CRYPT_KEY_VALID, &cc->flags);
Milan Brozde8be5a2011-03-24 13:54:27 +00002117
2118out:
2119 /* Hex key string not needed after here, so wipe it. */
2120 memset(key, '0', key_string_len);
2121
2122 return r;
Milan Broze48d4bb2006-10-03 01:15:37 -07002123}
2124
2125static int crypt_wipe_key(struct crypt_config *cc)
2126{
2127 clear_bit(DM_CRYPT_KEY_VALID, &cc->flags);
2128 memset(&cc->key, 0, cc->key_size * sizeof(u8));
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002129 kzfree(cc->key_string);
2130 cc->key_string = NULL;
Andi Kleenc0297722011-01-13 19:59:53 +00002131
Mikulas Patocka671ea6b2016-08-25 07:12:54 -04002132 return crypt_setkey(cc);
Milan Broze48d4bb2006-10-03 01:15:37 -07002133}
2134
Milan Broz28513fc2010-08-12 04:14:06 +01002135static void crypt_dtr(struct dm_target *ti)
2136{
2137 struct crypt_config *cc = ti->private;
2138
2139 ti->private = NULL;
2140
2141 if (!cc)
2142 return;
2143
Rabin Vincentf659b102016-09-21 16:22:29 +02002144 if (cc->write_thread)
Mikulas Patockadc267622015-02-13 08:25:59 -05002145 kthread_stop(cc->write_thread);
2146
Milan Broz28513fc2010-08-12 04:14:06 +01002147 if (cc->io_queue)
2148 destroy_workqueue(cc->io_queue);
2149 if (cc->crypt_queue)
2150 destroy_workqueue(cc->crypt_queue);
2151
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002152 crypt_free_tfms(cc);
2153
Milan Broz28513fc2010-08-12 04:14:06 +01002154 if (cc->bs)
2155 bioset_free(cc->bs);
2156
Julia Lawall6f659852015-09-13 14:15:05 +02002157 mempool_destroy(cc->page_pool);
2158 mempool_destroy(cc->req_pool);
Milan Brozef43aa32017-01-04 20:23:54 +01002159 mempool_destroy(cc->tag_pool);
Milan Broz28513fc2010-08-12 04:14:06 +01002160
2161 if (cc->iv_gen_ops && cc->iv_gen_ops->dtr)
2162 cc->iv_gen_ops->dtr(cc);
2163
Milan Broz28513fc2010-08-12 04:14:06 +01002164 if (cc->dev)
2165 dm_put_device(ti, cc->dev);
2166
Milan Broz5ebaee62010-08-12 04:14:07 +01002167 kzfree(cc->cipher);
Milan Broz7dbcd132011-01-13 19:59:52 +00002168 kzfree(cc->cipher_string);
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002169 kzfree(cc->key_string);
Milan Brozef43aa32017-01-04 20:23:54 +01002170 kzfree(cc->cipher_auth);
2171 kzfree(cc->authenc_key);
Milan Broz28513fc2010-08-12 04:14:06 +01002172
2173 /* Must zero key material before freeing */
2174 kzfree(cc);
2175}
2176
Milan Broze889f972017-03-16 15:39:39 +01002177static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode)
2178{
2179 struct crypt_config *cc = ti->private;
2180
Milan Broz33d2f092017-03-16 15:39:40 +01002181 if (crypt_integrity_aead(cc))
Milan Broze889f972017-03-16 15:39:39 +01002182 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2183 else
2184 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2185
Milan Broze889f972017-03-16 15:39:39 +01002186 if (cc->iv_size)
2187 /* at least a 64 bit sector number should fit in our buffer */
2188 cc->iv_size = max(cc->iv_size,
2189 (unsigned int)(sizeof(u64) / sizeof(u8)));
2190 else if (ivmode) {
2191 DMWARN("Selected cipher does not support IVs");
2192 ivmode = NULL;
2193 }
2194
2195 /* Choose ivmode, see comments at iv code. */
2196 if (ivmode == NULL)
2197 cc->iv_gen_ops = NULL;
2198 else if (strcmp(ivmode, "plain") == 0)
2199 cc->iv_gen_ops = &crypt_iv_plain_ops;
2200 else if (strcmp(ivmode, "plain64") == 0)
2201 cc->iv_gen_ops = &crypt_iv_plain64_ops;
2202 else if (strcmp(ivmode, "essiv") == 0)
2203 cc->iv_gen_ops = &crypt_iv_essiv_ops;
2204 else if (strcmp(ivmode, "benbi") == 0)
2205 cc->iv_gen_ops = &crypt_iv_benbi_ops;
2206 else if (strcmp(ivmode, "null") == 0)
2207 cc->iv_gen_ops = &crypt_iv_null_ops;
2208 else if (strcmp(ivmode, "lmk") == 0) {
2209 cc->iv_gen_ops = &crypt_iv_lmk_ops;
2210 /*
2211 * Version 2 and 3 is recognised according
2212 * to length of provided multi-key string.
2213 * If present (version 3), last key is used as IV seed.
2214 * All keys (including IV seed) are always the same size.
2215 */
2216 if (cc->key_size % cc->key_parts) {
2217 cc->key_parts++;
2218 cc->key_extra_size = cc->key_size / cc->key_parts;
2219 }
2220 } else if (strcmp(ivmode, "tcw") == 0) {
2221 cc->iv_gen_ops = &crypt_iv_tcw_ops;
2222 cc->key_parts += 2; /* IV + whitening */
2223 cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE;
2224 } else if (strcmp(ivmode, "random") == 0) {
2225 cc->iv_gen_ops = &crypt_iv_random_ops;
2226 /* Need storage space in integrity fields. */
2227 cc->integrity_iv_size = cc->iv_size;
2228 } else {
2229 ti->error = "Invalid IV mode";
2230 return -EINVAL;
2231 }
2232
2233 return 0;
2234}
2235
Milan Broz33d2f092017-03-16 15:39:40 +01002236/*
2237 * Workaround to parse cipher algorithm from crypto API spec.
2238 * The cc->cipher is currently used only in ESSIV.
2239 * This should be probably done by crypto-api calls (once available...)
2240 */
2241static int crypt_ctr_blkdev_cipher(struct crypt_config *cc)
2242{
2243 const char *alg_name = NULL;
2244 char *start, *end;
2245
2246 if (crypt_integrity_aead(cc)) {
2247 alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc)));
2248 if (!alg_name)
2249 return -EINVAL;
2250 if (crypt_integrity_hmac(cc)) {
2251 alg_name = strchr(alg_name, ',');
2252 if (!alg_name)
2253 return -EINVAL;
2254 }
2255 alg_name++;
2256 } else {
2257 alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc)));
2258 if (!alg_name)
2259 return -EINVAL;
2260 }
2261
2262 start = strchr(alg_name, '(');
2263 end = strchr(alg_name, ')');
2264
2265 if (!start && !end) {
2266 cc->cipher = kstrdup(alg_name, GFP_KERNEL);
2267 return cc->cipher ? 0 : -ENOMEM;
2268 }
2269
2270 if (!start || !end || ++start >= end)
2271 return -EINVAL;
2272
2273 cc->cipher = kzalloc(end - start + 1, GFP_KERNEL);
2274 if (!cc->cipher)
2275 return -ENOMEM;
2276
2277 strncpy(cc->cipher, start, end - start);
2278
2279 return 0;
2280}
2281
2282/*
2283 * Workaround to parse HMAC algorithm from AEAD crypto API spec.
2284 * The HMAC is needed to calculate tag size (HMAC digest size).
2285 * This should be probably done by crypto-api calls (once available...)
2286 */
2287static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api)
2288{
2289 char *start, *end, *mac_alg = NULL;
2290 struct crypto_ahash *mac;
2291
2292 if (!strstarts(cipher_api, "authenc("))
2293 return 0;
2294
2295 start = strchr(cipher_api, '(');
2296 end = strchr(cipher_api, ',');
2297 if (!start || !end || ++start > end)
2298 return -EINVAL;
2299
2300 mac_alg = kzalloc(end - start + 1, GFP_KERNEL);
2301 if (!mac_alg)
2302 return -ENOMEM;
2303 strncpy(mac_alg, start, end - start);
2304
2305 mac = crypto_alloc_ahash(mac_alg, 0, 0);
2306 kfree(mac_alg);
2307
2308 if (IS_ERR(mac))
2309 return PTR_ERR(mac);
2310
2311 cc->key_mac_size = crypto_ahash_digestsize(mac);
2312 crypto_free_ahash(mac);
2313
2314 cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL);
2315 if (!cc->authenc_key)
2316 return -ENOMEM;
2317
2318 return 0;
2319}
2320
2321static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key,
2322 char **ivmode, char **ivopts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Milan Broz5ebaee62010-08-12 04:14:07 +01002324 struct crypt_config *cc = ti->private;
Milan Broz33d2f092017-03-16 15:39:40 +01002325 char *tmp, *cipher_api;
2326 int ret = -EINVAL;
2327
2328 cc->tfms_count = 1;
2329
2330 /*
2331 * New format (capi: prefix)
2332 * capi:cipher_api_spec-iv:ivopts
2333 */
2334 tmp = &cipher_in[strlen("capi:")];
2335 cipher_api = strsep(&tmp, "-");
2336 *ivmode = strsep(&tmp, ":");
2337 *ivopts = tmp;
2338
2339 if (*ivmode && !strcmp(*ivmode, "lmk"))
2340 cc->tfms_count = 64;
2341
2342 cc->key_parts = cc->tfms_count;
2343
2344 /* Allocate cipher */
2345 ret = crypt_alloc_tfms(cc, cipher_api);
2346 if (ret < 0) {
2347 ti->error = "Error allocating crypto tfm";
2348 return ret;
2349 }
2350
2351 /* Alloc AEAD, can be used only in new format. */
2352 if (crypt_integrity_aead(cc)) {
2353 ret = crypt_ctr_auth_cipher(cc, cipher_api);
2354 if (ret < 0) {
2355 ti->error = "Invalid AEAD cipher spec";
2356 return -ENOMEM;
2357 }
2358 cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc));
2359 } else
2360 cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc));
2361
2362 ret = crypt_ctr_blkdev_cipher(cc);
2363 if (ret < 0) {
2364 ti->error = "Cannot allocate cipher string";
2365 return -ENOMEM;
2366 }
2367
2368 return 0;
2369}
2370
2371static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key,
2372 char **ivmode, char **ivopts)
2373{
2374 struct crypt_config *cc = ti->private;
2375 char *tmp, *cipher, *chainmode, *keycount;
Milan Broz5ebaee62010-08-12 04:14:07 +01002376 char *cipher_api = NULL;
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002377 int ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002378 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Milan Broz33d2f092017-03-16 15:39:40 +01002380 if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002381 ti->error = "Bad cipher specification";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 return -EINVAL;
2383 }
2384
Milan Broz5ebaee62010-08-12 04:14:07 +01002385 /*
2386 * Legacy dm-crypt cipher specification
Milan Brozd1f96422011-01-13 19:59:54 +00002387 * cipher[:keycount]-mode-iv:ivopts
Milan Broz5ebaee62010-08-12 04:14:07 +01002388 */
2389 tmp = cipher_in;
Milan Brozd1f96422011-01-13 19:59:54 +00002390 keycount = strsep(&tmp, "-");
2391 cipher = strsep(&keycount, ":");
2392
2393 if (!keycount)
2394 cc->tfms_count = 1;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002395 else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 ||
Milan Brozd1f96422011-01-13 19:59:54 +00002396 !is_power_of_2(cc->tfms_count)) {
2397 ti->error = "Bad cipher key count specification";
2398 return -EINVAL;
2399 }
2400 cc->key_parts = cc->tfms_count;
Milan Broz5ebaee62010-08-12 04:14:07 +01002401
2402 cc->cipher = kstrdup(cipher, GFP_KERNEL);
2403 if (!cc->cipher)
2404 goto bad_mem;
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 chainmode = strsep(&tmp, "-");
Milan Broz33d2f092017-03-16 15:39:40 +01002407 *ivopts = strsep(&tmp, "-");
2408 *ivmode = strsep(&*ivopts, ":");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
2410 if (tmp)
Milan Broz5ebaee62010-08-12 04:14:07 +01002411 DMWARN("Ignoring unexpected additional cipher options");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Milan Broz7dbcd132011-01-13 19:59:52 +00002413 /*
2414 * For compatibility with the original dm-crypt mapping format, if
2415 * only the cipher name is supplied, use cbc-plain.
2416 */
Milan Broz33d2f092017-03-16 15:39:40 +01002417 if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 chainmode = "cbc";
Milan Broz33d2f092017-03-16 15:39:40 +01002419 *ivmode = "plain";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
2421
Milan Broz33d2f092017-03-16 15:39:40 +01002422 if (strcmp(chainmode, "ecb") && !*ivmode) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002423 ti->error = "IV mechanism required";
2424 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 }
2426
Milan Broz5ebaee62010-08-12 04:14:07 +01002427 cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL);
2428 if (!cipher_api)
2429 goto bad_mem;
2430
2431 ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME,
2432 "%s(%s)", chainmode, cipher);
2433 if (ret < 0) {
2434 kfree(cipher_api);
2435 goto bad_mem;
Herbert Xud1806f62006-08-22 20:29:17 +10002436 }
2437
Milan Broz5ebaee62010-08-12 04:14:07 +01002438 /* Allocate cipher */
Mikulas Patockafd2d2312012-07-27 15:08:05 +01002439 ret = crypt_alloc_tfms(cc, cipher_api);
2440 if (ret < 0) {
2441 ti->error = "Error allocating crypto tfm";
Milan Broz33d2f092017-03-16 15:39:40 +01002442 kfree(cipher_api);
2443 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Milan Broz33d2f092017-03-16 15:39:40 +01002446 return 0;
2447bad_mem:
2448 ti->error = "Cannot allocate cipher strings";
2449 return -ENOMEM;
2450}
2451
2452static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key)
2453{
2454 struct crypt_config *cc = ti->private;
2455 char *ivmode = NULL, *ivopts = NULL;
2456 int ret;
2457
2458 cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL);
2459 if (!cc->cipher_string) {
2460 ti->error = "Cannot allocate cipher strings";
2461 return -ENOMEM;
2462 }
2463
2464 if (strstarts(cipher_in, "capi:"))
2465 ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts);
2466 else
2467 ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts);
2468 if (ret)
2469 return ret;
2470
Milan Broz5ebaee62010-08-12 04:14:07 +01002471 /* Initialize IV */
Milan Broze889f972017-03-16 15:39:39 +01002472 ret = crypt_ctr_ivmode(ti, ivmode);
2473 if (ret < 0)
Milan Broz33d2f092017-03-16 15:39:40 +01002474 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Milan Brozda31a072013-10-28 23:21:03 +01002476 /* Initialize and set key */
2477 ret = crypt_set_key(cc, key);
2478 if (ret < 0) {
2479 ti->error = "Error decoding and setting key";
Milan Broz33d2f092017-03-16 15:39:40 +01002480 return ret;
Milan Brozda31a072013-10-28 23:21:03 +01002481 }
2482
Milan Broz28513fc2010-08-12 04:14:06 +01002483 /* Allocate IV */
2484 if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) {
2485 ret = cc->iv_gen_ops->ctr(cc, ti, ivopts);
2486 if (ret < 0) {
2487 ti->error = "Error creating IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002488 return ret;
Milan Broz28513fc2010-08-12 04:14:06 +01002489 }
Milan Brozb95bf2d2009-12-10 23:51:56 +00002490 }
2491
Milan Broz28513fc2010-08-12 04:14:06 +01002492 /* Initialize IV (set keys for ESSIV etc) */
2493 if (cc->iv_gen_ops && cc->iv_gen_ops->init) {
2494 ret = cc->iv_gen_ops->init(cc);
2495 if (ret < 0) {
2496 ti->error = "Error initialising IV";
Milan Broz33d2f092017-03-16 15:39:40 +01002497 return ret;
Milan Broz28513fc2010-08-12 04:14:06 +01002498 }
2499 }
2500
Milan Broz5ebaee62010-08-12 04:14:07 +01002501 return ret;
Milan Broz5ebaee62010-08-12 04:14:07 +01002502}
2503
Milan Brozef43aa32017-01-04 20:23:54 +01002504static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv)
2505{
2506 struct crypt_config *cc = ti->private;
2507 struct dm_arg_set as;
2508 static struct dm_arg _args[] = {
2509 {0, 3, "Invalid number of feature args"},
2510 };
2511 unsigned int opt_params, val;
2512 const char *opt_string, *sval;
2513 int ret;
2514
2515 /* Optional parameters */
2516 as.argc = argc;
2517 as.argv = argv;
2518
2519 ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2520 if (ret)
2521 return ret;
2522
2523 while (opt_params--) {
2524 opt_string = dm_shift_arg(&as);
2525 if (!opt_string) {
2526 ti->error = "Not enough feature arguments";
2527 return -EINVAL;
2528 }
2529
2530 if (!strcasecmp(opt_string, "allow_discards"))
2531 ti->num_discard_bios = 1;
2532
2533 else if (!strcasecmp(opt_string, "same_cpu_crypt"))
2534 set_bit(DM_CRYPT_SAME_CPU, &cc->flags);
2535
2536 else if (!strcasecmp(opt_string, "submit_from_crypt_cpus"))
2537 set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
2538 else if (sscanf(opt_string, "integrity:%u:", &val) == 1) {
2539 if (val == 0 || val > MAX_TAG_SIZE) {
2540 ti->error = "Invalid integrity arguments";
2541 return -EINVAL;
2542 }
2543 cc->on_disk_tag_size = val;
2544 sval = strchr(opt_string + strlen("integrity:"), ':') + 1;
2545 if (!strcasecmp(sval, "aead")) {
2546 set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002547 } else if (strcasecmp(sval, "none")) {
2548 ti->error = "Unknown integrity profile";
2549 return -EINVAL;
2550 }
2551
2552 cc->cipher_auth = kstrdup(sval, GFP_KERNEL);
2553 if (!cc->cipher_auth)
2554 return -ENOMEM;
2555 } else {
2556 ti->error = "Invalid feature arguments";
2557 return -EINVAL;
2558 }
2559 }
2560
2561 return 0;
2562}
2563
Milan Broz5ebaee62010-08-12 04:14:07 +01002564/*
2565 * Construct an encryption mapping:
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002566 * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start>
Milan Broz5ebaee62010-08-12 04:14:07 +01002567 */
2568static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv)
2569{
2570 struct crypt_config *cc;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002571 int key_size;
Milan Brozef43aa32017-01-04 20:23:54 +01002572 unsigned int align_mask;
Milan Broz5ebaee62010-08-12 04:14:07 +01002573 unsigned long long tmpll;
2574 int ret;
Milan Brozef43aa32017-01-04 20:23:54 +01002575 size_t iv_size_padding, additional_req_size;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002576 char dummy;
Milan Broz5ebaee62010-08-12 04:14:07 +01002577
Milan Broz772ae5f2011-08-02 12:32:08 +01002578 if (argc < 5) {
Milan Broz5ebaee62010-08-12 04:14:07 +01002579 ti->error = "Not enough arguments";
2580 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
2582
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002583 key_size = get_key_size(&argv[1]);
2584 if (key_size < 0) {
2585 ti->error = "Cannot parse key size";
2586 return -EINVAL;
2587 }
Milan Broz5ebaee62010-08-12 04:14:07 +01002588
2589 cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL);
2590 if (!cc) {
2591 ti->error = "Cannot allocate encryption context";
2592 return -ENOMEM;
2593 }
Milan Broz69a8cfc2011-01-13 19:59:49 +00002594 cc->key_size = key_size;
Milan Broz5ebaee62010-08-12 04:14:07 +01002595
2596 ti->private = cc;
Milan Brozef43aa32017-01-04 20:23:54 +01002597
2598 /* Optional parameters need to be read before cipher constructor */
2599 if (argc > 5) {
2600 ret = crypt_ctr_optional(ti, argc - 5, &argv[5]);
2601 if (ret)
2602 goto bad;
2603 }
2604
Milan Broz5ebaee62010-08-12 04:14:07 +01002605 ret = crypt_ctr_cipher(ti, argv[0], argv[1]);
2606 if (ret < 0)
2607 goto bad;
2608
Milan Broz33d2f092017-03-16 15:39:40 +01002609 if (crypt_integrity_aead(cc)) {
Milan Brozef43aa32017-01-04 20:23:54 +01002610 cc->dmreq_start = sizeof(struct aead_request);
2611 cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc));
2612 align_mask = crypto_aead_alignmask(any_tfm_aead(cc));
2613 } else {
2614 cc->dmreq_start = sizeof(struct skcipher_request);
2615 cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc));
2616 align_mask = crypto_skcipher_alignmask(any_tfm(cc));
2617 }
Mikulas Patockad49ec522014-08-28 11:09:31 -04002618 cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request));
2619
Milan Brozef43aa32017-01-04 20:23:54 +01002620 if (align_mask < CRYPTO_MINALIGN) {
Mikulas Patockad49ec522014-08-28 11:09:31 -04002621 /* Allocate the padding exactly */
2622 iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request))
Milan Brozef43aa32017-01-04 20:23:54 +01002623 & align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002624 } else {
2625 /*
2626 * If the cipher requires greater alignment than kmalloc
2627 * alignment, we don't know the exact position of the
2628 * initialization vector. We must assume worst case.
2629 */
Milan Brozef43aa32017-01-04 20:23:54 +01002630 iv_size_padding = align_mask;
Mikulas Patockad49ec522014-08-28 11:09:31 -04002631 }
Milan Brozddd42ed2008-02-08 02:11:07 +00002632
Mikulas Patocka94f5e022015-02-13 08:25:26 -05002633 ret = -ENOMEM;
Milan Brozef43aa32017-01-04 20:23:54 +01002634
2635 /* ...| IV + padding | original IV | original sec. number | bio tag offset | */
2636 additional_req_size = sizeof(struct dm_crypt_request) +
2637 iv_size_padding + cc->iv_size +
2638 cc->iv_size +
2639 sizeof(uint64_t) +
2640 sizeof(unsigned int);
2641
2642 cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start + additional_req_size);
Milan Brozddd42ed2008-02-08 02:11:07 +00002643 if (!cc->req_pool) {
2644 ti->error = "Cannot allocate crypt request mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01002645 goto bad;
Milan Brozddd42ed2008-02-08 02:11:07 +00002646 }
Milan Brozddd42ed2008-02-08 02:11:07 +00002647
Mike Snitzer30187e12016-01-31 13:28:26 -05002648 cc->per_bio_data_size = ti->per_io_data_size =
Milan Brozef43aa32017-01-04 20:23:54 +01002649 ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size,
Mikulas Patockad49ec522014-08-28 11:09:31 -04002650 ARCH_KMALLOC_MINALIGN);
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002651
Mikulas Patockacf2f1ab2015-02-13 08:23:52 -05002652 cc->page_pool = mempool_create_page_pool(BIO_MAX_PAGES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (!cc->page_pool) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002654 ti->error = "Cannot allocate page mempool";
Milan Broz28513fc2010-08-12 04:14:06 +01002655 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 }
2657
Jens Axboebb799ca2008-12-10 15:35:05 +01002658 cc->bs = bioset_create(MIN_IOS, 0);
Milan Broz6a24c712006-10-03 01:15:40 -07002659 if (!cc->bs) {
2660 ti->error = "Cannot allocate crypt bioset";
Milan Broz28513fc2010-08-12 04:14:06 +01002661 goto bad;
Milan Broz6a24c712006-10-03 01:15:40 -07002662 }
2663
Mikulas Patocka7145c242015-02-13 08:24:41 -05002664 mutex_init(&cc->bio_alloc_lock);
2665
Milan Broz28513fc2010-08-12 04:14:06 +01002666 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002667 if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002668 ti->error = "Invalid iv_offset sector";
Milan Broz28513fc2010-08-12 04:14:06 +01002669 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08002671 cc->iv_offset = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Vivek Goyale80d1c82015-07-31 09:20:36 -04002673 ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev);
2674 if (ret) {
Milan Broz28513fc2010-08-12 04:14:06 +01002675 ti->error = "Device lookup failed";
2676 goto bad;
2677 }
2678
Vivek Goyale80d1c82015-07-31 09:20:36 -04002679 ret = -EINVAL;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01002680 if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002681 ti->error = "Invalid device sector";
Milan Broz28513fc2010-08-12 04:14:06 +01002682 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 }
Andrew Morton4ee218c2006-03-27 01:17:48 -08002684 cc->start = tmpll;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Milan Broz33d2f092017-03-16 15:39:40 +01002686 if (crypt_integrity_aead(cc) || cc->integrity_iv_size) {
Milan Brozef43aa32017-01-04 20:23:54 +01002687 ret = crypt_integrity_ctr(cc, ti);
Milan Broz772ae5f2011-08-02 12:32:08 +01002688 if (ret)
2689 goto bad;
2690
Milan Brozef43aa32017-01-04 20:23:54 +01002691 cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size;
2692 if (!cc->tag_pool_max_sectors)
2693 cc->tag_pool_max_sectors = 1;
Milan Broz772ae5f2011-08-02 12:32:08 +01002694
Milan Brozef43aa32017-01-04 20:23:54 +01002695 cc->tag_pool = mempool_create_kmalloc_pool(MIN_IOS,
2696 cc->tag_pool_max_sectors * cc->on_disk_tag_size);
2697 if (!cc->tag_pool) {
2698 ti->error = "Cannot allocate integrity tags mempool";
2699 goto bad;
Milan Broz772ae5f2011-08-02 12:32:08 +01002700 }
2701 }
2702
Milan Broz28513fc2010-08-12 04:14:06 +01002703 ret = -ENOMEM;
Tejun Heo670368a2013-07-30 08:40:21 -04002704 cc->io_queue = alloc_workqueue("kcryptd_io", WQ_MEM_RECLAIM, 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01002705 if (!cc->io_queue) {
2706 ti->error = "Couldn't create kcryptd io queue";
Milan Broz28513fc2010-08-12 04:14:06 +01002707 goto bad;
Milan Brozcabf08e2007-10-19 22:38:58 +01002708 }
2709
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002710 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2711 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1);
2712 else
2713 cc->crypt_queue = alloc_workqueue("kcryptd", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND,
2714 num_online_cpus());
Milan Brozcabf08e2007-10-19 22:38:58 +01002715 if (!cc->crypt_queue) {
Milan Broz9934a8b2007-10-19 22:38:57 +01002716 ti->error = "Couldn't create kcryptd queue";
Milan Broz28513fc2010-08-12 04:14:06 +01002717 goto bad;
Milan Broz9934a8b2007-10-19 22:38:57 +01002718 }
2719
Mikulas Patockadc267622015-02-13 08:25:59 -05002720 init_waitqueue_head(&cc->write_thread_wait);
Mikulas Patockab3c5fd32015-02-13 08:27:41 -05002721 cc->write_tree = RB_ROOT;
Mikulas Patockadc267622015-02-13 08:25:59 -05002722
2723 cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write");
2724 if (IS_ERR(cc->write_thread)) {
2725 ret = PTR_ERR(cc->write_thread);
2726 cc->write_thread = NULL;
2727 ti->error = "Couldn't spawn write thread";
2728 goto bad;
2729 }
2730 wake_up_process(cc->write_thread);
2731
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +00002732 ti->num_flush_bios = 1;
Alasdair G Kergon0ac55482012-07-27 15:08:08 +01002733 ti->discard_zeroes_data_unsupported = true;
Milan Broz983c7db2011-09-25 23:26:21 +01002734
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 return 0;
2736
Milan Broz28513fc2010-08-12 04:14:06 +01002737bad:
2738 crypt_dtr(ti);
2739 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740}
2741
Mikulas Patocka7de3ee52012-12-21 20:23:41 +00002742static int crypt_map(struct dm_target *ti, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01002744 struct dm_crypt_io *io;
Alasdair G Kergon49a8a922012-07-27 15:08:05 +01002745 struct crypt_config *cc = ti->private;
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002746
Milan Broz772ae5f2011-08-02 12:32:08 +01002747 /*
Mike Christie28a8f0d2016-06-05 14:32:25 -05002748 * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues.
2749 * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight
Mike Christiee6047142016-06-05 14:32:04 -05002750 * - for REQ_OP_DISCARD caller must use flush if IO ordering matters
Milan Broz772ae5f2011-08-02 12:32:08 +01002751 */
Jens Axboe1eff9d32016-08-05 15:35:16 -06002752 if (unlikely(bio->bi_opf & REQ_PREFLUSH ||
Mike Christie28a8f0d2016-06-05 14:32:25 -05002753 bio_op(bio) == REQ_OP_DISCARD)) {
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002754 bio->bi_bdev = cc->dev->bdev;
Milan Broz772ae5f2011-08-02 12:32:08 +01002755 if (bio_sectors(bio))
Kent Overstreet4f024f32013-10-11 15:44:27 -07002756 bio->bi_iter.bi_sector = cc->start +
2757 dm_target_offset(ti, bio->bi_iter.bi_sector);
Mikulas Patocka647c7db2009-06-22 10:12:23 +01002758 return DM_MAPIO_REMAPPED;
2759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002761 /*
2762 * Check if bio is too large, split as needed.
2763 */
2764 if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) &&
Milan Brozef43aa32017-01-04 20:23:54 +01002765 (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size))
Mikulas Patocka4e870e92016-08-30 16:38:42 -04002766 dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT));
2767
Mikulas Patocka298a9fa2014-03-28 15:51:55 -04002768 io = dm_per_bio_data(bio, cc->per_bio_data_size);
2769 crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector));
Milan Brozef43aa32017-01-04 20:23:54 +01002770
2771 if (cc->on_disk_tag_size) {
2772 unsigned tag_len = cc->on_disk_tag_size * bio_sectors(bio);
2773
2774 if (unlikely(tag_len > KMALLOC_MAX_SIZE) ||
2775 unlikely(!(io->integrity_metadata = kmalloc(tag_len,
2776 GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
2777 if (bio_sectors(bio) > cc->tag_pool_max_sectors)
2778 dm_accept_partial_bio(bio, cc->tag_pool_max_sectors);
2779 io->integrity_metadata = mempool_alloc(cc->tag_pool, GFP_NOIO);
2780 io->integrity_metadata_from_pool = true;
2781 }
2782 }
2783
Milan Broz33d2f092017-03-16 15:39:40 +01002784 if (crypt_integrity_aead(cc))
Milan Brozef43aa32017-01-04 20:23:54 +01002785 io->ctx.r.req_aead = (struct aead_request *)(io + 1);
2786 else
2787 io->ctx.r.req = (struct skcipher_request *)(io + 1);
Milan Brozcabf08e2007-10-19 22:38:58 +01002788
Milan Broz20c82532011-01-13 19:59:53 +00002789 if (bio_data_dir(io->base_bio) == READ) {
2790 if (kcryptd_io_read(io, GFP_NOWAIT))
Mikulas Patockadc267622015-02-13 08:25:59 -05002791 kcryptd_queue_read(io);
Milan Broz20c82532011-01-13 19:59:53 +00002792 } else
Milan Brozcabf08e2007-10-19 22:38:58 +01002793 kcryptd_queue_crypt(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08002795 return DM_MAPIO_SUBMITTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796}
2797
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002798static void crypt_status(struct dm_target *ti, status_type_t type,
2799 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
Milan Broz5ebaee62010-08-12 04:14:07 +01002801 struct crypt_config *cc = ti->private;
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002802 unsigned i, sz = 0;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002803 int num_feature_args = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 switch (type) {
2806 case STATUSTYPE_INFO:
2807 result[0] = '\0';
2808 break;
2809
2810 case STATUSTYPE_TABLE:
Milan Broz7dbcd132011-01-13 19:59:52 +00002811 DMEMIT("%s ", cc->cipher_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002813 if (cc->key_size > 0) {
2814 if (cc->key_string)
2815 DMEMIT(":%u:%s", cc->key_size, cc->key_string);
2816 else
2817 for (i = 0; i < cc->key_size; i++)
2818 DMEMIT("%02x", cc->key[i]);
2819 } else
Mikulas Patockafd7c0922013-03-01 22:45:44 +00002820 DMEMIT("-");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
Andrew Morton4ee218c2006-03-27 01:17:48 -08002822 DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset,
2823 cc->dev->name, (unsigned long long)cc->start);
Milan Broz772ae5f2011-08-02 12:32:08 +01002824
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002825 num_feature_args += !!ti->num_discard_bios;
2826 num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags);
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002827 num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags);
Milan Brozef43aa32017-01-04 20:23:54 +01002828 if (cc->on_disk_tag_size)
2829 num_feature_args++;
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002830 if (num_feature_args) {
2831 DMEMIT(" %d", num_feature_args);
2832 if (ti->num_discard_bios)
2833 DMEMIT(" allow_discards");
2834 if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags))
2835 DMEMIT(" same_cpu_crypt");
Mikulas Patocka0f5d8e62015-02-13 08:27:08 -05002836 if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags))
2837 DMEMIT(" submit_from_crypt_cpus");
Milan Brozef43aa32017-01-04 20:23:54 +01002838 if (cc->on_disk_tag_size)
2839 DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth);
Mikulas Patockaf3396c582015-02-13 08:23:09 -05002840 }
Milan Broz772ae5f2011-08-02 12:32:08 +01002841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 break;
2843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844}
2845
Milan Broze48d4bb2006-10-03 01:15:37 -07002846static void crypt_postsuspend(struct dm_target *ti)
2847{
2848 struct crypt_config *cc = ti->private;
2849
2850 set_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2851}
2852
2853static int crypt_preresume(struct dm_target *ti)
2854{
2855 struct crypt_config *cc = ti->private;
2856
2857 if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) {
2858 DMERR("aborting resume - crypt key is not set.");
2859 return -EAGAIN;
2860 }
2861
2862 return 0;
2863}
2864
2865static void crypt_resume(struct dm_target *ti)
2866{
2867 struct crypt_config *cc = ti->private;
2868
2869 clear_bit(DM_CRYPT_SUSPENDED, &cc->flags);
2870}
2871
2872/* Message interface
2873 * key set <key>
2874 * key wipe
2875 */
2876static int crypt_message(struct dm_target *ti, unsigned argc, char **argv)
2877{
2878 struct crypt_config *cc = ti->private;
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002879 int key_size, ret = -EINVAL;
Milan Broze48d4bb2006-10-03 01:15:37 -07002880
2881 if (argc < 2)
2882 goto error;
2883
Mike Snitzer498f0102011-08-02 12:32:04 +01002884 if (!strcasecmp(argv[0], "key")) {
Milan Broze48d4bb2006-10-03 01:15:37 -07002885 if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) {
2886 DMWARN("not suspended during key manipulation.");
2887 return -EINVAL;
2888 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002889 if (argc == 3 && !strcasecmp(argv[1], "set")) {
Ondrej Kozinac538f6e2016-11-21 15:58:51 +01002890 /* The key size may not be changed. */
2891 key_size = get_key_size(&argv[2]);
2892 if (key_size < 0 || cc->key_size != key_size) {
2893 memset(argv[2], '0', strlen(argv[2]));
2894 return -EINVAL;
2895 }
2896
Milan Broz542da312009-12-10 23:51:57 +00002897 ret = crypt_set_key(cc, argv[2]);
2898 if (ret)
2899 return ret;
2900 if (cc->iv_gen_ops && cc->iv_gen_ops->init)
2901 ret = cc->iv_gen_ops->init(cc);
2902 return ret;
2903 }
Mike Snitzer498f0102011-08-02 12:32:04 +01002904 if (argc == 2 && !strcasecmp(argv[1], "wipe")) {
Milan Broz542da312009-12-10 23:51:57 +00002905 if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) {
2906 ret = cc->iv_gen_ops->wipe(cc);
2907 if (ret)
2908 return ret;
2909 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002910 return crypt_wipe_key(cc);
Milan Broz542da312009-12-10 23:51:57 +00002911 }
Milan Broze48d4bb2006-10-03 01:15:37 -07002912 }
2913
2914error:
2915 DMWARN("unrecognised message received.");
2916 return -EINVAL;
2917}
2918
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002919static int crypt_iterate_devices(struct dm_target *ti,
2920 iterate_devices_callout_fn fn, void *data)
2921{
2922 struct crypt_config *cc = ti->private;
2923
Mike Snitzer5dea2712009-07-23 20:30:42 +01002924 return fn(ti, cc->dev, cc->start, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002925}
2926
Mike Snitzer586b2862015-09-09 21:34:51 -04002927static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits)
2928{
2929 /*
2930 * Unfortunate constraint that is required to avoid the potential
2931 * for exceeding underlying device's max_segments limits -- due to
2932 * crypt_alloc_buffer() possibly allocating pages for the encryption
2933 * bio that are not as physically contiguous as the original bio.
2934 */
2935 limits->max_segment_size = PAGE_SIZE;
2936}
2937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938static struct target_type crypt_target = {
2939 .name = "crypt",
Milan Brozef43aa32017-01-04 20:23:54 +01002940 .version = {1, 16, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 .module = THIS_MODULE,
2942 .ctr = crypt_ctr,
2943 .dtr = crypt_dtr,
2944 .map = crypt_map,
2945 .status = crypt_status,
Milan Broze48d4bb2006-10-03 01:15:37 -07002946 .postsuspend = crypt_postsuspend,
2947 .preresume = crypt_preresume,
2948 .resume = crypt_resume,
2949 .message = crypt_message,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01002950 .iterate_devices = crypt_iterate_devices,
Mike Snitzer586b2862015-09-09 21:34:51 -04002951 .io_hints = crypt_io_hints,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952};
2953
2954static int __init dm_crypt_init(void)
2955{
2956 int r;
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 r = dm_register_target(&crypt_target);
Mikulas Patocka94f5e022015-02-13 08:25:26 -05002959 if (r < 0)
Alasdair G Kergon72d94862006-06-26 00:27:35 -07002960 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 return r;
2963}
2964
2965static void __exit dm_crypt_exit(void)
2966{
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00002967 dm_unregister_target(&crypt_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968}
2969
2970module_init(dm_crypt_init);
2971module_exit(dm_crypt_exit);
2972
Jana Saoutbf142992014-06-24 14:27:04 -04002973MODULE_AUTHOR("Jana Saout <jana@saout.de>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption");
2975MODULE_LICENSE("GPL");