Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2003 Christophe Saout <christophe@saout.de> |
| 3 | * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org> |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2009 Red Hat, Inc. All rights reserved. |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 5 | * Copyright (C) 2013 Milan Broz <gmazyland@gmail.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This file is released under the GPL. |
| 8 | */ |
| 9 | |
Milan Broz | 43d6903 | 2008-02-08 02:11:09 +0000 | [diff] [blame] | 10 | #include <linux/completion.h> |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 11 | #include <linux/err.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/bio.h> |
| 16 | #include <linux/blkdev.h> |
| 17 | #include <linux/mempool.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/crypto.h> |
| 20 | #include <linux/workqueue.h> |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 21 | #include <linux/backing-dev.h> |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 22 | #include <linux/percpu.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 23 | #include <linux/atomic.h> |
David Hardeman | 378f058 | 2005-09-17 17:55:31 +1000 | [diff] [blame] | 24 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/page.h> |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 26 | #include <asm/unaligned.h> |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 27 | #include <crypto/hash.h> |
| 28 | #include <crypto/md5.h> |
| 29 | #include <crypto/algapi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Mikulas Patocka | 586e80e | 2008-10-21 17:44:59 +0100 | [diff] [blame] | 31 | #include <linux/device-mapper.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 33 | #define DM_MSG_PREFIX "crypt" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | * context holding the current state of a multi-part conversion |
| 37 | */ |
| 38 | struct convert_context { |
Milan Broz | 43d6903 | 2008-02-08 02:11:09 +0000 | [diff] [blame] | 39 | struct completion restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | struct bio *bio_in; |
| 41 | struct bio *bio_out; |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 42 | struct bvec_iter iter_in; |
| 43 | struct bvec_iter iter_out; |
Mikulas Patocka | c66029f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 44 | sector_t cc_sector; |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 45 | atomic_t cc_pending; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 48 | /* |
| 49 | * per bio private data |
| 50 | */ |
| 51 | struct dm_crypt_io { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 52 | struct crypt_config *cc; |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 53 | struct bio *base_bio; |
| 54 | struct work_struct work; |
| 55 | |
| 56 | struct convert_context ctx; |
| 57 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 58 | atomic_t io_pending; |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 59 | int error; |
Milan Broz | 0c395b0 | 2008-02-08 02:10:54 +0000 | [diff] [blame] | 60 | sector_t sector; |
Milan Broz | 393b47e | 2008-10-21 17:45:02 +0100 | [diff] [blame] | 61 | struct dm_crypt_io *base_io; |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 64 | struct dm_crypt_request { |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 65 | struct convert_context *ctx; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 66 | struct scatterlist sg_in; |
| 67 | struct scatterlist sg_out; |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 68 | sector_t iv_sector; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | struct crypt_config; |
| 72 | |
| 73 | struct crypt_iv_operations { |
| 74 | int (*ctr)(struct crypt_config *cc, struct dm_target *ti, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 75 | const char *opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | void (*dtr)(struct crypt_config *cc); |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 77 | int (*init)(struct crypt_config *cc); |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 78 | int (*wipe)(struct crypt_config *cc); |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 79 | int (*generator)(struct crypt_config *cc, u8 *iv, |
| 80 | struct dm_crypt_request *dmreq); |
| 81 | int (*post)(struct crypt_config *cc, u8 *iv, |
| 82 | struct dm_crypt_request *dmreq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 85 | struct iv_essiv_private { |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 86 | struct crypto_hash *hash_tfm; |
| 87 | u8 *salt; |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | struct iv_benbi_private { |
| 91 | int shift; |
| 92 | }; |
| 93 | |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 94 | #define LMK_SEED_SIZE 64 /* hash + 0 */ |
| 95 | struct iv_lmk_private { |
| 96 | struct crypto_shash *hash_tfm; |
| 97 | u8 *seed; |
| 98 | }; |
| 99 | |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 100 | #define TCW_WHITENING_SIZE 16 |
| 101 | struct iv_tcw_private { |
| 102 | struct crypto_shash *crc32_tfm; |
| 103 | u8 *iv_seed; |
| 104 | u8 *whitening; |
| 105 | }; |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | /* |
| 108 | * Crypt: maps a linear range of a block device |
| 109 | * and encrypts / decrypts at the same time. |
| 110 | */ |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 111 | enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID }; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 112 | |
| 113 | /* |
| 114 | * Duplicated per-CPU state for cipher. |
| 115 | */ |
| 116 | struct crypt_cpu { |
| 117 | struct ablkcipher_request *req; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /* |
| 121 | * The fields in here must be read only after initialization, |
| 122 | * changing state should be in crypt_cpu. |
| 123 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | struct crypt_config { |
| 125 | struct dm_dev *dev; |
| 126 | sector_t start; |
| 127 | |
| 128 | /* |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 129 | * pool for per bio private data, crypto requests and |
| 130 | * encryption requeusts/buffer pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | */ |
| 132 | mempool_t *io_pool; |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 133 | mempool_t *req_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | mempool_t *page_pool; |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 135 | struct bio_set *bs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 137 | struct workqueue_struct *io_queue; |
| 138 | struct workqueue_struct *crypt_queue; |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 139 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 140 | char *cipher; |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 141 | char *cipher_string; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | struct crypt_iv_operations *iv_gen_ops; |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 144 | union { |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 145 | struct iv_essiv_private essiv; |
| 146 | struct iv_benbi_private benbi; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 147 | struct iv_lmk_private lmk; |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 148 | struct iv_tcw_private tcw; |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 149 | } iv_gen_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | sector_t iv_offset; |
| 151 | unsigned int iv_size; |
| 152 | |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 153 | /* |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 154 | * Duplicated per cpu state. Access through |
| 155 | * per_cpu_ptr() only. |
| 156 | */ |
| 157 | struct crypt_cpu __percpu *cpu; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 158 | |
| 159 | /* ESSIV: struct crypto_cipher *essiv_tfm */ |
| 160 | void *iv_private; |
| 161 | struct crypto_ablkcipher **tfms; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 162 | unsigned tfms_count; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 163 | |
| 164 | /* |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 165 | * Layout of each crypto request: |
| 166 | * |
| 167 | * struct ablkcipher_request |
| 168 | * context |
| 169 | * padding |
| 170 | * struct dm_crypt_request |
| 171 | * padding |
| 172 | * IV |
| 173 | * |
| 174 | * The padding is added so that dm_crypt_request and the IV are |
| 175 | * correctly aligned. |
| 176 | */ |
| 177 | unsigned int dmreq_start; |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 178 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 179 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | unsigned int key_size; |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 181 | unsigned int key_parts; /* independent parts in key buffer */ |
| 182 | unsigned int key_extra_size; /* additional keys length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | u8 key[0]; |
| 184 | }; |
| 185 | |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 186 | #define MIN_IOS 16 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | #define MIN_POOL_PAGES 32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 189 | static struct kmem_cache *_crypt_io_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 191 | static void clone_init(struct dm_crypt_io *, struct bio *); |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 192 | static void kcryptd_queue_crypt(struct dm_crypt_io *io); |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 193 | static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq); |
Olaf Kirch | 027581f | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 194 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 195 | static struct crypt_cpu *this_crypt_config(struct crypt_config *cc) |
| 196 | { |
| 197 | return this_cpu_ptr(cc->cpu); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * Use this to access cipher attributes that are the same for each CPU. |
| 202 | */ |
| 203 | static struct crypto_ablkcipher *any_tfm(struct crypt_config *cc) |
| 204 | { |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 205 | return cc->tfms[0]; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | * Different IV generation algorithms: |
| 210 | * |
Rik Snel | 3c164bd | 2006-09-02 18:17:33 +1000 | [diff] [blame] | 211 | * plain: the initial vector is the 32-bit little-endian version of the sector |
Robert P. J. Day | 3a4fa0a | 2007-10-19 23:10:43 +0200 | [diff] [blame] | 212 | * number, padded with zeros if necessary. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | * |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 214 | * plain64: the initial vector is the 64-bit little-endian version of the sector |
| 215 | * number, padded with zeros if necessary. |
| 216 | * |
Rik Snel | 3c164bd | 2006-09-02 18:17:33 +1000 | [diff] [blame] | 217 | * essiv: "encrypted sector|salt initial vector", the sector number is |
| 218 | * encrypted with the bulk cipher using a salt as key. The salt |
| 219 | * should be derived from the bulk cipher's key via hashing. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | * |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 221 | * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1 |
| 222 | * (needed for LRW-32-AES and possible other narrow block modes) |
| 223 | * |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 224 | * null: the initial vector is always zero. Provides compatibility with |
| 225 | * obsolete loop_fish2 devices. Do not use for new devices. |
| 226 | * |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 227 | * lmk: Compatible implementation of the block chaining mode used |
| 228 | * by the Loop-AES block device encryption system |
| 229 | * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/ |
| 230 | * It operates on full 512 byte sectors and uses CBC |
| 231 | * with an IV derived from the sector number, the data and |
| 232 | * optionally extra IV seed. |
| 233 | * This means that after decryption the first block |
| 234 | * of sector must be tweaked according to decrypted data. |
| 235 | * Loop-AES can use three encryption schemes: |
| 236 | * version 1: is plain aes-cbc mode |
| 237 | * version 2: uses 64 multikey scheme with lmk IV generator |
| 238 | * version 3: the same as version 2 with additional IV seed |
| 239 | * (it uses 65 keys, last key is used as IV seed) |
| 240 | * |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 241 | * tcw: Compatible implementation of the block chaining mode used |
| 242 | * by the TrueCrypt device encryption system (prior to version 4.1). |
| 243 | * For more info see: http://www.truecrypt.org |
| 244 | * It operates on full 512 byte sectors and uses CBC |
| 245 | * with an IV derived from initial key and the sector number. |
| 246 | * In addition, whitening value is applied on every sector, whitening |
| 247 | * is calculated from initial key, sector number and mixed using CRC32. |
| 248 | * Note that this encryption scheme is vulnerable to watermarking attacks |
| 249 | * and should be used for old compatible containers access only. |
| 250 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | * plumb: unimplemented, see: |
| 252 | * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454 |
| 253 | */ |
| 254 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 255 | static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, |
| 256 | struct dm_crypt_request *dmreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
| 258 | memset(iv, 0, cc->iv_size); |
Alasdair G Kergon | 283a832 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 259 | *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 264 | static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv, |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 265 | struct dm_crypt_request *dmreq) |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 266 | { |
| 267 | memset(iv, 0, cc->iv_size); |
Alasdair G Kergon | 283a832 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 268 | *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector); |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 273 | /* Initialise ESSIV - compute salt but no local memory allocations */ |
| 274 | static int crypt_iv_essiv_init(struct crypt_config *cc) |
| 275 | { |
| 276 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; |
| 277 | struct hash_desc desc; |
| 278 | struct scatterlist sg; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 279 | struct crypto_cipher *essiv_tfm; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 280 | int err; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 281 | |
| 282 | sg_init_one(&sg, cc->key, cc->key_size); |
| 283 | desc.tfm = essiv->hash_tfm; |
| 284 | desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 285 | |
| 286 | err = crypto_hash_digest(&desc, &sg, cc->key_size, essiv->salt); |
| 287 | if (err) |
| 288 | return err; |
| 289 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 290 | essiv_tfm = cc->iv_private; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 291 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 292 | err = crypto_cipher_setkey(essiv_tfm, essiv->salt, |
| 293 | crypto_hash_digestsize(essiv->hash_tfm)); |
| 294 | if (err) |
| 295 | return err; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 296 | |
| 297 | return 0; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 300 | /* Wipe salt and reset key derived from volume key */ |
| 301 | static int crypt_iv_essiv_wipe(struct crypt_config *cc) |
| 302 | { |
| 303 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; |
| 304 | unsigned salt_size = crypto_hash_digestsize(essiv->hash_tfm); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 305 | struct crypto_cipher *essiv_tfm; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 306 | int r, err = 0; |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 307 | |
| 308 | memset(essiv->salt, 0, salt_size); |
| 309 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 310 | essiv_tfm = cc->iv_private; |
| 311 | r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size); |
| 312 | if (r) |
| 313 | err = r; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 314 | |
| 315 | return err; |
| 316 | } |
| 317 | |
| 318 | /* Set up per cpu cipher state */ |
| 319 | static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc, |
| 320 | struct dm_target *ti, |
| 321 | u8 *salt, unsigned saltsize) |
| 322 | { |
| 323 | struct crypto_cipher *essiv_tfm; |
| 324 | int err; |
| 325 | |
| 326 | /* Setup the essiv_tfm with the given salt */ |
| 327 | essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC); |
| 328 | if (IS_ERR(essiv_tfm)) { |
| 329 | ti->error = "Error allocating crypto tfm for ESSIV"; |
| 330 | return essiv_tfm; |
| 331 | } |
| 332 | |
| 333 | if (crypto_cipher_blocksize(essiv_tfm) != |
| 334 | crypto_ablkcipher_ivsize(any_tfm(cc))) { |
| 335 | ti->error = "Block size of ESSIV cipher does " |
| 336 | "not match IV size of block cipher"; |
| 337 | crypto_free_cipher(essiv_tfm); |
| 338 | return ERR_PTR(-EINVAL); |
| 339 | } |
| 340 | |
| 341 | err = crypto_cipher_setkey(essiv_tfm, salt, saltsize); |
| 342 | if (err) { |
| 343 | ti->error = "Failed to set key for ESSIV cipher"; |
| 344 | crypto_free_cipher(essiv_tfm); |
| 345 | return ERR_PTR(err); |
| 346 | } |
| 347 | |
| 348 | return essiv_tfm; |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 351 | static void crypt_iv_essiv_dtr(struct crypt_config *cc) |
| 352 | { |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 353 | struct crypto_cipher *essiv_tfm; |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 354 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; |
| 355 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 356 | crypto_free_hash(essiv->hash_tfm); |
| 357 | essiv->hash_tfm = NULL; |
| 358 | |
| 359 | kzfree(essiv->salt); |
| 360 | essiv->salt = NULL; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 361 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 362 | essiv_tfm = cc->iv_private; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 363 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 364 | if (essiv_tfm) |
| 365 | crypto_free_cipher(essiv_tfm); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 366 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 367 | cc->iv_private = NULL; |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | static int crypt_iv_essiv_ctr(struct crypt_config *cc, struct dm_target *ti, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 371 | const char *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 373 | struct crypto_cipher *essiv_tfm = NULL; |
| 374 | struct crypto_hash *hash_tfm = NULL; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 375 | u8 *salt = NULL; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 376 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 378 | if (!opts) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 379 | ti->error = "Digest algorithm missing for ESSIV mode"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | return -EINVAL; |
| 381 | } |
| 382 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 383 | /* Allocate hash algorithm */ |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 384 | hash_tfm = crypto_alloc_hash(opts, 0, CRYPTO_ALG_ASYNC); |
| 385 | if (IS_ERR(hash_tfm)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 386 | ti->error = "Error initializing ESSIV hash"; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 387 | err = PTR_ERR(hash_tfm); |
| 388 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 391 | salt = kzalloc(crypto_hash_digestsize(hash_tfm), GFP_KERNEL); |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 392 | if (!salt) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 393 | ti->error = "Error kmallocing salt storage in ESSIV"; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 394 | err = -ENOMEM; |
| 395 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | } |
| 397 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 398 | cc->iv_gen_private.essiv.salt = salt; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 399 | cc->iv_gen_private.essiv.hash_tfm = hash_tfm; |
| 400 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 401 | essiv_tfm = setup_essiv_cpu(cc, ti, salt, |
| 402 | crypto_hash_digestsize(hash_tfm)); |
| 403 | if (IS_ERR(essiv_tfm)) { |
| 404 | crypt_iv_essiv_dtr(cc); |
| 405 | return PTR_ERR(essiv_tfm); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 406 | } |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 407 | cc->iv_private = essiv_tfm; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | return 0; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 410 | |
| 411 | bad: |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 412 | if (hash_tfm && !IS_ERR(hash_tfm)) |
| 413 | crypto_free_hash(hash_tfm); |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 414 | kfree(salt); |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 415 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 418 | static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, |
| 419 | struct dm_crypt_request *dmreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | { |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 421 | struct crypto_cipher *essiv_tfm = cc->iv_private; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | memset(iv, 0, cc->iv_size); |
Alasdair G Kergon | 283a832 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 424 | *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 425 | crypto_cipher_encrypt_one(essiv_tfm, iv, iv); |
| 426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 430 | static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, |
| 431 | const char *opts) |
| 432 | { |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 433 | unsigned bs = crypto_ablkcipher_blocksize(any_tfm(cc)); |
David Howells | f0d1b0b | 2006-12-08 02:37:49 -0800 | [diff] [blame] | 434 | int log = ilog2(bs); |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 435 | |
| 436 | /* we need to calculate how far we must shift the sector count |
| 437 | * to get the cipher block count, we use this shift in _gen */ |
| 438 | |
| 439 | if (1 << log != bs) { |
| 440 | ti->error = "cypher blocksize is not a power of 2"; |
| 441 | return -EINVAL; |
| 442 | } |
| 443 | |
| 444 | if (log > 9) { |
| 445 | ti->error = "cypher blocksize is > 512"; |
| 446 | return -EINVAL; |
| 447 | } |
| 448 | |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 449 | cc->iv_gen_private.benbi.shift = 9 - log; |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 450 | |
| 451 | return 0; |
| 452 | } |
| 453 | |
| 454 | static void crypt_iv_benbi_dtr(struct crypt_config *cc) |
| 455 | { |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 456 | } |
| 457 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 458 | static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, |
| 459 | struct dm_crypt_request *dmreq) |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 460 | { |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 461 | __be64 val; |
| 462 | |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 463 | memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 464 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 465 | val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1); |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 466 | put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 471 | static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, |
| 472 | struct dm_crypt_request *dmreq) |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 473 | { |
| 474 | memset(iv, 0, cc->iv_size); |
| 475 | |
| 476 | return 0; |
| 477 | } |
| 478 | |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 479 | static void crypt_iv_lmk_dtr(struct crypt_config *cc) |
| 480 | { |
| 481 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 482 | |
| 483 | if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm)) |
| 484 | crypto_free_shash(lmk->hash_tfm); |
| 485 | lmk->hash_tfm = NULL; |
| 486 | |
| 487 | kzfree(lmk->seed); |
| 488 | lmk->seed = NULL; |
| 489 | } |
| 490 | |
| 491 | static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti, |
| 492 | const char *opts) |
| 493 | { |
| 494 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 495 | |
| 496 | lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0); |
| 497 | if (IS_ERR(lmk->hash_tfm)) { |
| 498 | ti->error = "Error initializing LMK hash"; |
| 499 | return PTR_ERR(lmk->hash_tfm); |
| 500 | } |
| 501 | |
| 502 | /* No seed in LMK version 2 */ |
| 503 | if (cc->key_parts == cc->tfms_count) { |
| 504 | lmk->seed = NULL; |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL); |
| 509 | if (!lmk->seed) { |
| 510 | crypt_iv_lmk_dtr(cc); |
| 511 | ti->error = "Error kmallocing seed storage in LMK"; |
| 512 | return -ENOMEM; |
| 513 | } |
| 514 | |
| 515 | return 0; |
| 516 | } |
| 517 | |
| 518 | static int crypt_iv_lmk_init(struct crypt_config *cc) |
| 519 | { |
| 520 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 521 | int subkey_size = cc->key_size / cc->key_parts; |
| 522 | |
| 523 | /* LMK seed is on the position of LMK_KEYS + 1 key */ |
| 524 | if (lmk->seed) |
| 525 | memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size), |
| 526 | crypto_shash_digestsize(lmk->hash_tfm)); |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | static int crypt_iv_lmk_wipe(struct crypt_config *cc) |
| 532 | { |
| 533 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 534 | |
| 535 | if (lmk->seed) |
| 536 | memset(lmk->seed, 0, LMK_SEED_SIZE); |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv, |
| 542 | struct dm_crypt_request *dmreq, |
| 543 | u8 *data) |
| 544 | { |
| 545 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 546 | struct { |
| 547 | struct shash_desc desc; |
| 548 | char ctx[crypto_shash_descsize(lmk->hash_tfm)]; |
| 549 | } sdesc; |
| 550 | struct md5_state md5state; |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 551 | __le32 buf[4]; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 552 | int i, r; |
| 553 | |
| 554 | sdesc.desc.tfm = lmk->hash_tfm; |
| 555 | sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 556 | |
| 557 | r = crypto_shash_init(&sdesc.desc); |
| 558 | if (r) |
| 559 | return r; |
| 560 | |
| 561 | if (lmk->seed) { |
| 562 | r = crypto_shash_update(&sdesc.desc, lmk->seed, LMK_SEED_SIZE); |
| 563 | if (r) |
| 564 | return r; |
| 565 | } |
| 566 | |
| 567 | /* Sector is always 512B, block size 16, add data of blocks 1-31 */ |
| 568 | r = crypto_shash_update(&sdesc.desc, data + 16, 16 * 31); |
| 569 | if (r) |
| 570 | return r; |
| 571 | |
| 572 | /* Sector is cropped to 56 bits here */ |
| 573 | buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF); |
| 574 | buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000); |
| 575 | buf[2] = cpu_to_le32(4024); |
| 576 | buf[3] = 0; |
| 577 | r = crypto_shash_update(&sdesc.desc, (u8 *)buf, sizeof(buf)); |
| 578 | if (r) |
| 579 | return r; |
| 580 | |
| 581 | /* No MD5 padding here */ |
| 582 | r = crypto_shash_export(&sdesc.desc, &md5state); |
| 583 | if (r) |
| 584 | return r; |
| 585 | |
| 586 | for (i = 0; i < MD5_HASH_WORDS; i++) |
| 587 | __cpu_to_le32s(&md5state.hash[i]); |
| 588 | memcpy(iv, &md5state.hash, cc->iv_size); |
| 589 | |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv, |
| 594 | struct dm_crypt_request *dmreq) |
| 595 | { |
| 596 | u8 *src; |
| 597 | int r = 0; |
| 598 | |
| 599 | if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) { |
Cong Wang | c2e022c | 2011-11-28 13:26:02 +0800 | [diff] [blame] | 600 | src = kmap_atomic(sg_page(&dmreq->sg_in)); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 601 | r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset); |
Cong Wang | c2e022c | 2011-11-28 13:26:02 +0800 | [diff] [blame] | 602 | kunmap_atomic(src); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 603 | } else |
| 604 | memset(iv, 0, cc->iv_size); |
| 605 | |
| 606 | return r; |
| 607 | } |
| 608 | |
| 609 | static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv, |
| 610 | struct dm_crypt_request *dmreq) |
| 611 | { |
| 612 | u8 *dst; |
| 613 | int r; |
| 614 | |
| 615 | if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) |
| 616 | return 0; |
| 617 | |
Cong Wang | c2e022c | 2011-11-28 13:26:02 +0800 | [diff] [blame] | 618 | dst = kmap_atomic(sg_page(&dmreq->sg_out)); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 619 | r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset); |
| 620 | |
| 621 | /* Tweak the first block of plaintext sector */ |
| 622 | if (!r) |
| 623 | crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size); |
| 624 | |
Cong Wang | c2e022c | 2011-11-28 13:26:02 +0800 | [diff] [blame] | 625 | kunmap_atomic(dst); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 626 | return r; |
| 627 | } |
| 628 | |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 629 | static void crypt_iv_tcw_dtr(struct crypt_config *cc) |
| 630 | { |
| 631 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
| 632 | |
| 633 | kzfree(tcw->iv_seed); |
| 634 | tcw->iv_seed = NULL; |
| 635 | kzfree(tcw->whitening); |
| 636 | tcw->whitening = NULL; |
| 637 | |
| 638 | if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm)) |
| 639 | crypto_free_shash(tcw->crc32_tfm); |
| 640 | tcw->crc32_tfm = NULL; |
| 641 | } |
| 642 | |
| 643 | static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti, |
| 644 | const char *opts) |
| 645 | { |
| 646 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
| 647 | |
| 648 | if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) { |
| 649 | ti->error = "Wrong key size for TCW"; |
| 650 | return -EINVAL; |
| 651 | } |
| 652 | |
| 653 | tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0); |
| 654 | if (IS_ERR(tcw->crc32_tfm)) { |
| 655 | ti->error = "Error initializing CRC32 in TCW"; |
| 656 | return PTR_ERR(tcw->crc32_tfm); |
| 657 | } |
| 658 | |
| 659 | tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL); |
| 660 | tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL); |
| 661 | if (!tcw->iv_seed || !tcw->whitening) { |
| 662 | crypt_iv_tcw_dtr(cc); |
| 663 | ti->error = "Error allocating seed storage in TCW"; |
| 664 | return -ENOMEM; |
| 665 | } |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | static int crypt_iv_tcw_init(struct crypt_config *cc) |
| 671 | { |
| 672 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
| 673 | int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE; |
| 674 | |
| 675 | memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size); |
| 676 | memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size], |
| 677 | TCW_WHITENING_SIZE); |
| 678 | |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | static int crypt_iv_tcw_wipe(struct crypt_config *cc) |
| 683 | { |
| 684 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
| 685 | |
| 686 | memset(tcw->iv_seed, 0, cc->iv_size); |
| 687 | memset(tcw->whitening, 0, TCW_WHITENING_SIZE); |
| 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static int crypt_iv_tcw_whitening(struct crypt_config *cc, |
| 693 | struct dm_crypt_request *dmreq, |
| 694 | u8 *data) |
| 695 | { |
| 696 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
| 697 | u64 sector = cpu_to_le64((u64)dmreq->iv_sector); |
| 698 | u8 buf[TCW_WHITENING_SIZE]; |
| 699 | struct { |
| 700 | struct shash_desc desc; |
| 701 | char ctx[crypto_shash_descsize(tcw->crc32_tfm)]; |
| 702 | } sdesc; |
| 703 | int i, r; |
| 704 | |
| 705 | /* xor whitening with sector number */ |
| 706 | memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE); |
| 707 | crypto_xor(buf, (u8 *)§or, 8); |
| 708 | crypto_xor(&buf[8], (u8 *)§or, 8); |
| 709 | |
| 710 | /* calculate crc32 for every 32bit part and xor it */ |
| 711 | sdesc.desc.tfm = tcw->crc32_tfm; |
| 712 | sdesc.desc.flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
| 713 | for (i = 0; i < 4; i++) { |
| 714 | r = crypto_shash_init(&sdesc.desc); |
| 715 | if (r) |
| 716 | goto out; |
| 717 | r = crypto_shash_update(&sdesc.desc, &buf[i * 4], 4); |
| 718 | if (r) |
| 719 | goto out; |
| 720 | r = crypto_shash_final(&sdesc.desc, &buf[i * 4]); |
| 721 | if (r) |
| 722 | goto out; |
| 723 | } |
| 724 | crypto_xor(&buf[0], &buf[12], 4); |
| 725 | crypto_xor(&buf[4], &buf[8], 4); |
| 726 | |
| 727 | /* apply whitening (8 bytes) to whole sector */ |
| 728 | for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++) |
| 729 | crypto_xor(data + i * 8, buf, 8); |
| 730 | out: |
| 731 | memset(buf, 0, sizeof(buf)); |
| 732 | return r; |
| 733 | } |
| 734 | |
| 735 | static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv, |
| 736 | struct dm_crypt_request *dmreq) |
| 737 | { |
| 738 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
| 739 | u64 sector = cpu_to_le64((u64)dmreq->iv_sector); |
| 740 | u8 *src; |
| 741 | int r = 0; |
| 742 | |
| 743 | /* Remove whitening from ciphertext */ |
| 744 | if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) { |
| 745 | src = kmap_atomic(sg_page(&dmreq->sg_in)); |
| 746 | r = crypt_iv_tcw_whitening(cc, dmreq, src + dmreq->sg_in.offset); |
| 747 | kunmap_atomic(src); |
| 748 | } |
| 749 | |
| 750 | /* Calculate IV */ |
| 751 | memcpy(iv, tcw->iv_seed, cc->iv_size); |
| 752 | crypto_xor(iv, (u8 *)§or, 8); |
| 753 | if (cc->iv_size > 8) |
| 754 | crypto_xor(&iv[8], (u8 *)§or, cc->iv_size - 8); |
| 755 | |
| 756 | return r; |
| 757 | } |
| 758 | |
| 759 | static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv, |
| 760 | struct dm_crypt_request *dmreq) |
| 761 | { |
| 762 | u8 *dst; |
| 763 | int r; |
| 764 | |
| 765 | if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) |
| 766 | return 0; |
| 767 | |
| 768 | /* Apply whitening on ciphertext */ |
| 769 | dst = kmap_atomic(sg_page(&dmreq->sg_out)); |
| 770 | r = crypt_iv_tcw_whitening(cc, dmreq, dst + dmreq->sg_out.offset); |
| 771 | kunmap_atomic(dst); |
| 772 | |
| 773 | return r; |
| 774 | } |
| 775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | static struct crypt_iv_operations crypt_iv_plain_ops = { |
| 777 | .generator = crypt_iv_plain_gen |
| 778 | }; |
| 779 | |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 780 | static struct crypt_iv_operations crypt_iv_plain64_ops = { |
| 781 | .generator = crypt_iv_plain64_gen |
| 782 | }; |
| 783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | static struct crypt_iv_operations crypt_iv_essiv_ops = { |
| 785 | .ctr = crypt_iv_essiv_ctr, |
| 786 | .dtr = crypt_iv_essiv_dtr, |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 787 | .init = crypt_iv_essiv_init, |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 788 | .wipe = crypt_iv_essiv_wipe, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | .generator = crypt_iv_essiv_gen |
| 790 | }; |
| 791 | |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 792 | static struct crypt_iv_operations crypt_iv_benbi_ops = { |
| 793 | .ctr = crypt_iv_benbi_ctr, |
| 794 | .dtr = crypt_iv_benbi_dtr, |
| 795 | .generator = crypt_iv_benbi_gen |
| 796 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 798 | static struct crypt_iv_operations crypt_iv_null_ops = { |
| 799 | .generator = crypt_iv_null_gen |
| 800 | }; |
| 801 | |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 802 | static struct crypt_iv_operations crypt_iv_lmk_ops = { |
| 803 | .ctr = crypt_iv_lmk_ctr, |
| 804 | .dtr = crypt_iv_lmk_dtr, |
| 805 | .init = crypt_iv_lmk_init, |
| 806 | .wipe = crypt_iv_lmk_wipe, |
| 807 | .generator = crypt_iv_lmk_gen, |
| 808 | .post = crypt_iv_lmk_post |
| 809 | }; |
| 810 | |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 811 | static struct crypt_iv_operations crypt_iv_tcw_ops = { |
| 812 | .ctr = crypt_iv_tcw_ctr, |
| 813 | .dtr = crypt_iv_tcw_dtr, |
| 814 | .init = crypt_iv_tcw_init, |
| 815 | .wipe = crypt_iv_tcw_wipe, |
| 816 | .generator = crypt_iv_tcw_gen, |
| 817 | .post = crypt_iv_tcw_post |
| 818 | }; |
| 819 | |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 820 | static void crypt_convert_init(struct crypt_config *cc, |
| 821 | struct convert_context *ctx, |
| 822 | struct bio *bio_out, struct bio *bio_in, |
Milan Broz | fcd369d | 2008-02-08 02:10:41 +0000 | [diff] [blame] | 823 | sector_t sector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | { |
| 825 | ctx->bio_in = bio_in; |
| 826 | ctx->bio_out = bio_out; |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 827 | if (bio_in) |
| 828 | ctx->iter_in = bio_in->bi_iter; |
| 829 | if (bio_out) |
| 830 | ctx->iter_out = bio_out->bi_iter; |
Mikulas Patocka | c66029f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 831 | ctx->cc_sector = sector + cc->iv_offset; |
Milan Broz | 43d6903 | 2008-02-08 02:11:09 +0000 | [diff] [blame] | 832 | init_completion(&ctx->restart); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | } |
| 834 | |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 835 | static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc, |
| 836 | struct ablkcipher_request *req) |
| 837 | { |
| 838 | return (struct dm_crypt_request *)((char *)req + cc->dmreq_start); |
| 839 | } |
| 840 | |
| 841 | static struct ablkcipher_request *req_of_dmreq(struct crypt_config *cc, |
| 842 | struct dm_crypt_request *dmreq) |
| 843 | { |
| 844 | return (struct ablkcipher_request *)((char *)dmreq - cc->dmreq_start); |
| 845 | } |
| 846 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 847 | static u8 *iv_of_dmreq(struct crypt_config *cc, |
| 848 | struct dm_crypt_request *dmreq) |
| 849 | { |
| 850 | return (u8 *)ALIGN((unsigned long)(dmreq + 1), |
| 851 | crypto_ablkcipher_alignmask(any_tfm(cc)) + 1); |
| 852 | } |
| 853 | |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 854 | static int crypt_convert_block(struct crypt_config *cc, |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 855 | struct convert_context *ctx, |
| 856 | struct ablkcipher_request *req) |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 857 | { |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 858 | struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in); |
| 859 | struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 860 | struct dm_crypt_request *dmreq; |
| 861 | u8 *iv; |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 862 | int r; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 863 | |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 864 | dmreq = dmreq_of_req(cc, req); |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 865 | iv = iv_of_dmreq(cc, dmreq); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 866 | |
Mikulas Patocka | c66029f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 867 | dmreq->iv_sector = ctx->cc_sector; |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 868 | dmreq->ctx = ctx; |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 869 | sg_init_table(&dmreq->sg_in, 1); |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 870 | sg_set_page(&dmreq->sg_in, bv_in.bv_page, 1 << SECTOR_SHIFT, |
| 871 | bv_in.bv_offset); |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 872 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 873 | sg_init_table(&dmreq->sg_out, 1); |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 874 | sg_set_page(&dmreq->sg_out, bv_out.bv_page, 1 << SECTOR_SHIFT, |
| 875 | bv_out.bv_offset); |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 876 | |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 877 | bio_advance_iter(ctx->bio_in, &ctx->iter_in, 1 << SECTOR_SHIFT); |
| 878 | bio_advance_iter(ctx->bio_out, &ctx->iter_out, 1 << SECTOR_SHIFT); |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 879 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 880 | if (cc->iv_gen_ops) { |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 881 | r = cc->iv_gen_ops->generator(cc, iv, dmreq); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 882 | if (r < 0) |
| 883 | return r; |
| 884 | } |
| 885 | |
| 886 | ablkcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out, |
| 887 | 1 << SECTOR_SHIFT, iv); |
| 888 | |
| 889 | if (bio_data_dir(ctx->bio_in) == WRITE) |
| 890 | r = crypto_ablkcipher_encrypt(req); |
| 891 | else |
| 892 | r = crypto_ablkcipher_decrypt(req); |
| 893 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 894 | if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) |
| 895 | r = cc->iv_gen_ops->post(cc, iv, dmreq); |
| 896 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 897 | return r; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 900 | static void kcryptd_async_done(struct crypto_async_request *async_req, |
| 901 | int error); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 902 | |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 903 | static void crypt_alloc_req(struct crypt_config *cc, |
| 904 | struct convert_context *ctx) |
| 905 | { |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 906 | struct crypt_cpu *this_cc = this_crypt_config(cc); |
Mikulas Patocka | c66029f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 907 | unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 908 | |
| 909 | if (!this_cc->req) |
| 910 | this_cc->req = mempool_alloc(cc->req_pool, GFP_NOIO); |
| 911 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 912 | ablkcipher_request_set_tfm(this_cc->req, cc->tfms[key_index]); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 913 | ablkcipher_request_set_callback(this_cc->req, |
| 914 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
| 915 | kcryptd_async_done, dmreq_of_req(cc, this_cc->req)); |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | /* |
| 919 | * Encrypt / decrypt data from one bio to another one (can be the same one) |
| 920 | */ |
| 921 | static int crypt_convert(struct crypt_config *cc, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 922 | struct convert_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | { |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 924 | struct crypt_cpu *this_cc = this_crypt_config(cc); |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 925 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 927 | atomic_set(&ctx->cc_pending, 1); |
Milan Broz | c808161 | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 928 | |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 929 | while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 931 | crypt_alloc_req(cc, ctx); |
| 932 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 933 | atomic_inc(&ctx->cc_pending); |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 934 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 935 | r = crypt_convert_block(cc, ctx, this_cc->req); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 936 | |
| 937 | switch (r) { |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 938 | /* async */ |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 939 | case -EBUSY: |
| 940 | wait_for_completion(&ctx->restart); |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 941 | reinit_completion(&ctx->restart); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 942 | /* fall through*/ |
| 943 | case -EINPROGRESS: |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 944 | this_cc->req = NULL; |
Mikulas Patocka | c66029f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 945 | ctx->cc_sector++; |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 946 | continue; |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 947 | |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 948 | /* sync */ |
| 949 | case 0: |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 950 | atomic_dec(&ctx->cc_pending); |
Mikulas Patocka | c66029f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 951 | ctx->cc_sector++; |
Milan Broz | c7f1b20 | 2008-07-02 09:34:28 +0100 | [diff] [blame] | 952 | cond_resched(); |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 953 | continue; |
| 954 | |
| 955 | /* error */ |
| 956 | default: |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 957 | atomic_dec(&ctx->cc_pending); |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 958 | return r; |
| 959 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 962 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | /* |
| 966 | * Generate a new unfragmented bio with the given size |
| 967 | * This should never violate the device limitations |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 968 | * May return a smaller bio when running out of pages, indicated by |
| 969 | * *out_of_pages set to 1. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | */ |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 971 | static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size, |
| 972 | unsigned *out_of_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 974 | struct crypt_config *cc = io->cc; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 975 | struct bio *clone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Al Viro | b4e3ca1 | 2005-10-21 03:22:34 -0400 | [diff] [blame] | 977 | gfp_t gfp_mask = GFP_NOIO | __GFP_HIGHMEM; |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 978 | unsigned i, len; |
| 979 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
Olaf Kirch | 2f9941b | 2007-05-09 02:32:53 -0700 | [diff] [blame] | 981 | clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 982 | if (!clone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | |
Olaf Kirch | 027581f | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 985 | clone_init(io, clone); |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 986 | *out_of_pages = 0; |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 987 | |
Olaf Kirch | f97380b | 2007-05-09 02:32:54 -0700 | [diff] [blame] | 988 | for (i = 0; i < nr_iovecs; i++) { |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 989 | page = mempool_alloc(cc->page_pool, gfp_mask); |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 990 | if (!page) { |
| 991 | *out_of_pages = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | break; |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 993 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
| 995 | /* |
Mikulas Patocka | aeb2dea | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 996 | * If additional pages cannot be allocated without waiting, |
| 997 | * return a partially-allocated bio. The caller will then try |
| 998 | * to allocate more bios while submitting this partial bio. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | */ |
Mikulas Patocka | aeb2dea | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1000 | gfp_mask = (gfp_mask | __GFP_NOWARN) & ~__GFP_WAIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 1002 | len = (size > PAGE_SIZE) ? PAGE_SIZE : size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 1004 | if (!bio_add_page(clone, page, len, 0)) { |
| 1005 | mempool_free(page, cc->page_pool); |
| 1006 | break; |
| 1007 | } |
| 1008 | |
| 1009 | size -= len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1012 | if (!clone->bi_iter.bi_size) { |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1013 | bio_put(clone); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | return NULL; |
| 1015 | } |
| 1016 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1017 | return clone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 1020 | static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | { |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 1022 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | struct bio_vec *bv; |
| 1024 | |
Kent Overstreet | cb34e05 | 2012-09-05 15:22:02 -0700 | [diff] [blame] | 1025 | bio_for_each_segment_all(bv, clone, i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | BUG_ON(!bv->bv_page); |
| 1027 | mempool_free(bv->bv_page, cc->page_pool); |
| 1028 | bv->bv_page = NULL; |
| 1029 | } |
| 1030 | } |
| 1031 | |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1032 | static struct dm_crypt_io *crypt_io_alloc(struct crypt_config *cc, |
Milan Broz | dc440d1e | 2008-10-10 13:37:03 +0100 | [diff] [blame] | 1033 | struct bio *bio, sector_t sector) |
| 1034 | { |
Milan Broz | dc440d1e | 2008-10-10 13:37:03 +0100 | [diff] [blame] | 1035 | struct dm_crypt_io *io; |
| 1036 | |
| 1037 | io = mempool_alloc(cc->io_pool, GFP_NOIO); |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1038 | io->cc = cc; |
Milan Broz | dc440d1e | 2008-10-10 13:37:03 +0100 | [diff] [blame] | 1039 | io->base_bio = bio; |
| 1040 | io->sector = sector; |
| 1041 | io->error = 0; |
Milan Broz | 393b47e | 2008-10-21 17:45:02 +0100 | [diff] [blame] | 1042 | io->base_io = NULL; |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1043 | atomic_set(&io->io_pending, 0); |
Milan Broz | dc440d1e | 2008-10-10 13:37:03 +0100 | [diff] [blame] | 1044 | |
| 1045 | return io; |
| 1046 | } |
| 1047 | |
Milan Broz | 3e1a8bd | 2008-10-10 13:37:02 +0100 | [diff] [blame] | 1048 | static void crypt_inc_pending(struct dm_crypt_io *io) |
| 1049 | { |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1050 | atomic_inc(&io->io_pending); |
Milan Broz | 3e1a8bd | 2008-10-10 13:37:02 +0100 | [diff] [blame] | 1051 | } |
| 1052 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | /* |
| 1054 | * One of the bios was finished. Check for completion of |
| 1055 | * the whole request and correctly clean up the buffer. |
Milan Broz | 393b47e | 2008-10-21 17:45:02 +0100 | [diff] [blame] | 1056 | * If base_io is set, wait for the last fragment to complete. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | */ |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1058 | static void crypt_dec_pending(struct dm_crypt_io *io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1060 | struct crypt_config *cc = io->cc; |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 1061 | struct bio *base_bio = io->base_bio; |
| 1062 | struct dm_crypt_io *base_io = io->base_io; |
| 1063 | int error = io->error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1065 | if (!atomic_dec_and_test(&io->io_pending)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | return; |
| 1067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | mempool_free(io, cc->io_pool); |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 1069 | |
| 1070 | if (likely(!base_io)) |
| 1071 | bio_endio(base_bio, error); |
| 1072 | else { |
| 1073 | if (error && !base_io->error) |
| 1074 | base_io->error = error; |
| 1075 | crypt_dec_pending(base_io); |
| 1076 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /* |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1080 | * kcryptd/kcryptd_io: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | * |
| 1082 | * Needed because it would be very unwise to do decryption in an |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 1083 | * interrupt context. |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1084 | * |
| 1085 | * kcryptd performs the actual encryption or decryption. |
| 1086 | * |
| 1087 | * kcryptd_io performs the IO submission. |
| 1088 | * |
| 1089 | * They must be separated as otherwise the final stages could be |
| 1090 | * starved by new requests which can block in the first stages due |
| 1091 | * to memory allocation. |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1092 | * |
| 1093 | * The work is done per CPU global for all dm-crypt instances. |
| 1094 | * They should not depend on each other and do not block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1096 | static void crypt_endio(struct bio *clone, int error) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1097 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1098 | struct dm_crypt_io *io = clone->bi_private; |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1099 | struct crypt_config *cc = io->cc; |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 1100 | unsigned rw = bio_data_dir(clone); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1101 | |
Milan Broz | adfe477 | 2007-12-13 14:15:51 +0000 | [diff] [blame] | 1102 | if (unlikely(!bio_flagged(clone, BIO_UPTODATE) && !error)) |
| 1103 | error = -EIO; |
| 1104 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1105 | /* |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1106 | * free the processed pages |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1107 | */ |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 1108 | if (rw == WRITE) |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 1109 | crypt_free_buffer_pages(cc, clone); |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 1110 | |
| 1111 | bio_put(clone); |
| 1112 | |
| 1113 | if (rw == READ && !error) { |
| 1114 | kcryptd_queue_crypt(io); |
| 1115 | return; |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1116 | } |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1117 | |
Milan Broz | adfe477 | 2007-12-13 14:15:51 +0000 | [diff] [blame] | 1118 | if (unlikely(error)) |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1119 | io->error = error; |
| 1120 | |
| 1121 | crypt_dec_pending(io); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1124 | static void clone_init(struct dm_crypt_io *io, struct bio *clone) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1125 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1126 | struct crypt_config *cc = io->cc; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1127 | |
| 1128 | clone->bi_private = io; |
| 1129 | clone->bi_end_io = crypt_endio; |
| 1130 | clone->bi_bdev = cc->dev->bdev; |
| 1131 | clone->bi_rw = io->base_bio->bi_rw; |
| 1132 | } |
| 1133 | |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1134 | static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1135 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1136 | struct crypt_config *cc = io->cc; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1137 | struct bio *base_bio = io->base_bio; |
| 1138 | struct bio *clone; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1139 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1140 | /* |
| 1141 | * The block layer might modify the bvec array, so always |
| 1142 | * copy the required bvecs because we need the original |
| 1143 | * one in order to decrypt the whole bio data *afterwards*. |
| 1144 | */ |
Kent Overstreet | bf800ef | 2012-09-06 15:35:02 -0700 | [diff] [blame] | 1145 | clone = bio_clone_bioset(base_bio, gfp, cc->bs); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 1146 | if (!clone) |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1147 | return 1; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1148 | |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1149 | crypt_inc_pending(io); |
| 1150 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1151 | clone_init(io, clone); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1152 | clone->bi_iter.bi_sector = cc->start + io->sector; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1153 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1154 | generic_make_request(clone); |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1155 | return 0; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1158 | static void kcryptd_io_write(struct dm_crypt_io *io) |
| 1159 | { |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1160 | struct bio *clone = io->ctx.bio_out; |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1161 | generic_make_request(clone); |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1164 | static void kcryptd_io(struct work_struct *work) |
| 1165 | { |
| 1166 | struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); |
| 1167 | |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1168 | if (bio_data_dir(io->base_bio) == READ) { |
| 1169 | crypt_inc_pending(io); |
| 1170 | if (kcryptd_io_read(io, GFP_NOIO)) |
| 1171 | io->error = -ENOMEM; |
| 1172 | crypt_dec_pending(io); |
| 1173 | } else |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1174 | kcryptd_io_write(io); |
| 1175 | } |
| 1176 | |
| 1177 | static void kcryptd_queue_io(struct dm_crypt_io *io) |
| 1178 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1179 | struct crypt_config *cc = io->cc; |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1180 | |
| 1181 | INIT_WORK(&io->work, kcryptd_io); |
| 1182 | queue_work(cc->io_queue, &io->work); |
| 1183 | } |
| 1184 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1185 | static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async) |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1186 | { |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1187 | struct bio *clone = io->ctx.bio_out; |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1188 | struct crypt_config *cc = io->cc; |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1189 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1190 | if (unlikely(io->error < 0)) { |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1191 | crypt_free_buffer_pages(cc, clone); |
| 1192 | bio_put(clone); |
Milan Broz | 6c031f4 | 2008-10-10 13:37:06 +0100 | [diff] [blame] | 1193 | crypt_dec_pending(io); |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1194 | return; |
| 1195 | } |
| 1196 | |
| 1197 | /* crypt_convert should have filled the clone bio */ |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 1198 | BUG_ON(io->ctx.iter_out.bi_size); |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1199 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1200 | clone->bi_iter.bi_sector = cc->start + io->sector; |
Milan Broz | 899c95d | 2008-02-08 02:11:02 +0000 | [diff] [blame] | 1201 | |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1202 | if (async) |
| 1203 | kcryptd_queue_io(io); |
Alasdair G Kergon | 1e37bb8 | 2008-10-10 13:37:05 +0100 | [diff] [blame] | 1204 | else |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1205 | generic_make_request(clone); |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1208 | static void kcryptd_crypt_write_convert(struct dm_crypt_io *io) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1209 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1210 | struct crypt_config *cc = io->cc; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1211 | struct bio *clone; |
Milan Broz | 393b47e | 2008-10-21 17:45:02 +0100 | [diff] [blame] | 1212 | struct dm_crypt_io *new_io; |
Milan Broz | c808161 | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1213 | int crypt_finished; |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1214 | unsigned out_of_pages = 0; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1215 | unsigned remaining = io->base_bio->bi_iter.bi_size; |
Milan Broz | b635b00 | 2008-10-21 17:45:00 +0100 | [diff] [blame] | 1216 | sector_t sector = io->sector; |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1217 | int r; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1218 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1219 | /* |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1220 | * Prevent io from disappearing until this function completes. |
| 1221 | */ |
| 1222 | crypt_inc_pending(io); |
Milan Broz | b635b00 | 2008-10-21 17:45:00 +0100 | [diff] [blame] | 1223 | crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector); |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1224 | |
| 1225 | /* |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1226 | * The allocated buffers can be smaller than the whole bio, |
| 1227 | * so repeat the whole process until all the data can be handled. |
| 1228 | */ |
| 1229 | while (remaining) { |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1230 | clone = crypt_alloc_buffer(io, remaining, &out_of_pages); |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 1231 | if (unlikely(!clone)) { |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1232 | io->error = -ENOMEM; |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1233 | break; |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 1234 | } |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1235 | |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 1236 | io->ctx.bio_out = clone; |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 1237 | io->ctx.iter_out = clone->bi_iter; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1238 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1239 | remaining -= clone->bi_iter.bi_size; |
Milan Broz | b635b00 | 2008-10-21 17:45:00 +0100 | [diff] [blame] | 1240 | sector += bio_sectors(clone); |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1241 | |
Milan Broz | 4e59409 | 2008-10-10 13:37:07 +0100 | [diff] [blame] | 1242 | crypt_inc_pending(io); |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1243 | |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1244 | r = crypt_convert(cc, &io->ctx); |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1245 | if (r < 0) |
| 1246 | io->error = -EIO; |
| 1247 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1248 | crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending); |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1249 | |
Milan Broz | c808161 | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1250 | /* Encryption was already finished, submit io now */ |
| 1251 | if (crypt_finished) { |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1252 | kcryptd_crypt_write_io_submit(io, 0); |
Milan Broz | c808161 | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1253 | |
| 1254 | /* |
| 1255 | * If there was an error, do not try next fragments. |
| 1256 | * For async, error is processed in async handler. |
| 1257 | */ |
Milan Broz | 6c031f4 | 2008-10-10 13:37:06 +0100 | [diff] [blame] | 1258 | if (unlikely(r < 0)) |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1259 | break; |
Milan Broz | b635b00 | 2008-10-21 17:45:00 +0100 | [diff] [blame] | 1260 | |
| 1261 | io->sector = sector; |
Milan Broz | 4e59409 | 2008-10-10 13:37:07 +0100 | [diff] [blame] | 1262 | } |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1263 | |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1264 | /* |
| 1265 | * Out of memory -> run queues |
| 1266 | * But don't wait if split was due to the io size restriction |
| 1267 | */ |
| 1268 | if (unlikely(out_of_pages)) |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 1269 | congestion_wait(BLK_RW_ASYNC, HZ/100); |
Milan Broz | 933f01d | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1270 | |
Milan Broz | 393b47e | 2008-10-21 17:45:02 +0100 | [diff] [blame] | 1271 | /* |
| 1272 | * With async crypto it is unsafe to share the crypto context |
| 1273 | * between fragments, so switch to a new dm_crypt_io structure. |
| 1274 | */ |
| 1275 | if (unlikely(!crypt_finished && remaining)) { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1276 | new_io = crypt_io_alloc(io->cc, io->base_bio, |
Milan Broz | 393b47e | 2008-10-21 17:45:02 +0100 | [diff] [blame] | 1277 | sector); |
| 1278 | crypt_inc_pending(new_io); |
| 1279 | crypt_convert_init(cc, &new_io->ctx, NULL, |
| 1280 | io->base_bio, sector); |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 1281 | new_io->ctx.iter_in = io->ctx.iter_in; |
Milan Broz | 393b47e | 2008-10-21 17:45:02 +0100 | [diff] [blame] | 1282 | |
| 1283 | /* |
| 1284 | * Fragments after the first use the base_io |
| 1285 | * pending count. |
| 1286 | */ |
| 1287 | if (!io->base_io) |
| 1288 | new_io->base_io = io; |
| 1289 | else { |
| 1290 | new_io->base_io = io->base_io; |
| 1291 | crypt_inc_pending(io->base_io); |
| 1292 | crypt_dec_pending(io); |
| 1293 | } |
| 1294 | |
| 1295 | io = new_io; |
| 1296 | } |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1297 | } |
Milan Broz | 899c95d | 2008-02-08 02:11:02 +0000 | [diff] [blame] | 1298 | |
| 1299 | crypt_dec_pending(io); |
Milan Broz | 84131db | 2008-02-08 02:10:59 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1302 | static void kcryptd_crypt_read_done(struct dm_crypt_io *io) |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1303 | { |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1304 | crypt_dec_pending(io); |
| 1305 | } |
| 1306 | |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1307 | static void kcryptd_crypt_read_convert(struct dm_crypt_io *io) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1308 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1309 | struct crypt_config *cc = io->cc; |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1310 | int r = 0; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1311 | |
Milan Broz | 3e1a8bd | 2008-10-10 13:37:02 +0100 | [diff] [blame] | 1312 | crypt_inc_pending(io); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1313 | |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 1314 | crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio, |
Milan Broz | 0c395b0 | 2008-02-08 02:10:54 +0000 | [diff] [blame] | 1315 | io->sector); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1316 | |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1317 | r = crypt_convert(cc, &io->ctx); |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1318 | if (r < 0) |
| 1319 | io->error = -EIO; |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1320 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1321 | if (atomic_dec_and_test(&io->ctx.cc_pending)) |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1322 | kcryptd_crypt_read_done(io); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1323 | |
| 1324 | crypt_dec_pending(io); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1327 | static void kcryptd_async_done(struct crypto_async_request *async_req, |
| 1328 | int error) |
| 1329 | { |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 1330 | struct dm_crypt_request *dmreq = async_req->data; |
| 1331 | struct convert_context *ctx = dmreq->ctx; |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1332 | struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx); |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1333 | struct crypt_config *cc = io->cc; |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1334 | |
| 1335 | if (error == -EINPROGRESS) { |
| 1336 | complete(&ctx->restart); |
| 1337 | return; |
| 1338 | } |
| 1339 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1340 | if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post) |
| 1341 | error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq); |
| 1342 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1343 | if (error < 0) |
| 1344 | io->error = -EIO; |
| 1345 | |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 1346 | mempool_free(req_of_dmreq(cc, dmreq), cc->req_pool); |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1347 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1348 | if (!atomic_dec_and_test(&ctx->cc_pending)) |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1349 | return; |
| 1350 | |
| 1351 | if (bio_data_dir(io->base_bio) == READ) |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1352 | kcryptd_crypt_read_done(io); |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1353 | else |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1354 | kcryptd_crypt_write_io_submit(io, 1); |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1357 | static void kcryptd_crypt(struct work_struct *work) |
| 1358 | { |
| 1359 | struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); |
| 1360 | |
| 1361 | if (bio_data_dir(io->base_bio) == READ) |
| 1362 | kcryptd_crypt_read_convert(io); |
| 1363 | else |
| 1364 | kcryptd_crypt_write_convert(io); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1367 | static void kcryptd_queue_crypt(struct dm_crypt_io *io) |
| 1368 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1369 | struct crypt_config *cc = io->cc; |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1370 | |
| 1371 | INIT_WORK(&io->work, kcryptd_crypt); |
| 1372 | queue_work(cc->crypt_queue, &io->work); |
| 1373 | } |
| 1374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | /* |
| 1376 | * Decode key from its hex representation |
| 1377 | */ |
| 1378 | static int crypt_decode_key(u8 *key, char *hex, unsigned int size) |
| 1379 | { |
| 1380 | char buffer[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | unsigned int i; |
| 1382 | |
| 1383 | buffer[2] = '\0'; |
| 1384 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1385 | for (i = 0; i < size; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | buffer[0] = *hex++; |
| 1387 | buffer[1] = *hex++; |
| 1388 | |
majianpeng | 1a66a08 | 2012-07-27 15:07:59 +0100 | [diff] [blame] | 1389 | if (kstrtou8(buffer, 16, &key[i])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | return -EINVAL; |
| 1391 | } |
| 1392 | |
| 1393 | if (*hex != '\0') |
| 1394 | return -EINVAL; |
| 1395 | |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1399 | static void crypt_free_tfms(struct crypt_config *cc) |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1400 | { |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1401 | unsigned i; |
| 1402 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1403 | if (!cc->tfms) |
| 1404 | return; |
| 1405 | |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1406 | for (i = 0; i < cc->tfms_count; i++) |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1407 | if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) { |
| 1408 | crypto_free_ablkcipher(cc->tfms[i]); |
| 1409 | cc->tfms[i] = NULL; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1410 | } |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1411 | |
| 1412 | kfree(cc->tfms); |
| 1413 | cc->tfms = NULL; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1416 | static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode) |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1417 | { |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1418 | unsigned i; |
| 1419 | int err; |
| 1420 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1421 | cc->tfms = kmalloc(cc->tfms_count * sizeof(struct crypto_ablkcipher *), |
| 1422 | GFP_KERNEL); |
| 1423 | if (!cc->tfms) |
| 1424 | return -ENOMEM; |
| 1425 | |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1426 | for (i = 0; i < cc->tfms_count; i++) { |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1427 | cc->tfms[i] = crypto_alloc_ablkcipher(ciphermode, 0, 0); |
| 1428 | if (IS_ERR(cc->tfms[i])) { |
| 1429 | err = PTR_ERR(cc->tfms[i]); |
| 1430 | crypt_free_tfms(cc); |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1431 | return err; |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | return 0; |
| 1436 | } |
| 1437 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1438 | static int crypt_setkey_allcpus(struct crypt_config *cc) |
| 1439 | { |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1440 | unsigned subkey_size; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1441 | int err = 0, i, r; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1442 | |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1443 | /* Ignore extra keys (which are used for IV etc) */ |
| 1444 | subkey_size = (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count); |
| 1445 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1446 | for (i = 0; i < cc->tfms_count; i++) { |
| 1447 | r = crypto_ablkcipher_setkey(cc->tfms[i], |
| 1448 | cc->key + (i * subkey_size), |
| 1449 | subkey_size); |
| 1450 | if (r) |
| 1451 | err = r; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | return err; |
| 1455 | } |
| 1456 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1457 | static int crypt_set_key(struct crypt_config *cc, char *key) |
| 1458 | { |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1459 | int r = -EINVAL; |
| 1460 | int key_string_len = strlen(key); |
| 1461 | |
Milan Broz | 69a8cfc | 2011-01-13 19:59:49 +0000 | [diff] [blame] | 1462 | /* The key size may not be changed. */ |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1463 | if (cc->key_size != (key_string_len >> 1)) |
| 1464 | goto out; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1465 | |
Milan Broz | 69a8cfc | 2011-01-13 19:59:49 +0000 | [diff] [blame] | 1466 | /* Hyphen (which gives a key_size of zero) means there is no key. */ |
| 1467 | if (!cc->key_size && strcmp(key, "-")) |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1468 | goto out; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1469 | |
Milan Broz | 69a8cfc | 2011-01-13 19:59:49 +0000 | [diff] [blame] | 1470 | if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0) |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1471 | goto out; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1472 | |
| 1473 | set_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
| 1474 | |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1475 | r = crypt_setkey_allcpus(cc); |
| 1476 | |
| 1477 | out: |
| 1478 | /* Hex key string not needed after here, so wipe it. */ |
| 1479 | memset(key, '0', key_string_len); |
| 1480 | |
| 1481 | return r; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | static int crypt_wipe_key(struct crypt_config *cc) |
| 1485 | { |
| 1486 | clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
| 1487 | memset(&cc->key, 0, cc->key_size * sizeof(u8)); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1488 | |
| 1489 | return crypt_setkey_allcpus(cc); |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1492 | static void crypt_dtr(struct dm_target *ti) |
| 1493 | { |
| 1494 | struct crypt_config *cc = ti->private; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1495 | struct crypt_cpu *cpu_cc; |
| 1496 | int cpu; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1497 | |
| 1498 | ti->private = NULL; |
| 1499 | |
| 1500 | if (!cc) |
| 1501 | return; |
| 1502 | |
| 1503 | if (cc->io_queue) |
| 1504 | destroy_workqueue(cc->io_queue); |
| 1505 | if (cc->crypt_queue) |
| 1506 | destroy_workqueue(cc->crypt_queue); |
| 1507 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1508 | if (cc->cpu) |
| 1509 | for_each_possible_cpu(cpu) { |
| 1510 | cpu_cc = per_cpu_ptr(cc->cpu, cpu); |
| 1511 | if (cpu_cc->req) |
| 1512 | mempool_free(cpu_cc->req, cc->req_pool); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1515 | crypt_free_tfms(cc); |
| 1516 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1517 | if (cc->bs) |
| 1518 | bioset_free(cc->bs); |
| 1519 | |
| 1520 | if (cc->page_pool) |
| 1521 | mempool_destroy(cc->page_pool); |
| 1522 | if (cc->req_pool) |
| 1523 | mempool_destroy(cc->req_pool); |
| 1524 | if (cc->io_pool) |
| 1525 | mempool_destroy(cc->io_pool); |
| 1526 | |
| 1527 | if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) |
| 1528 | cc->iv_gen_ops->dtr(cc); |
| 1529 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1530 | if (cc->dev) |
| 1531 | dm_put_device(ti, cc->dev); |
| 1532 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1533 | if (cc->cpu) |
| 1534 | free_percpu(cc->cpu); |
| 1535 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1536 | kzfree(cc->cipher); |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 1537 | kzfree(cc->cipher_string); |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1538 | |
| 1539 | /* Must zero key material before freeing */ |
| 1540 | kzfree(cc); |
| 1541 | } |
| 1542 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1543 | static int crypt_ctr_cipher(struct dm_target *ti, |
| 1544 | char *cipher_in, char *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1546 | struct crypt_config *cc = ti->private; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1547 | char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1548 | char *cipher_api = NULL; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1549 | int ret = -EINVAL; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1550 | char dummy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1552 | /* Convert to crypto api definition? */ |
| 1553 | if (strchr(cipher_in, '(')) { |
| 1554 | ti->error = "Bad cipher specification"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | return -EINVAL; |
| 1556 | } |
| 1557 | |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 1558 | cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL); |
| 1559 | if (!cc->cipher_string) |
| 1560 | goto bad_mem; |
| 1561 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1562 | /* |
| 1563 | * Legacy dm-crypt cipher specification |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1564 | * cipher[:keycount]-mode-iv:ivopts |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1565 | */ |
| 1566 | tmp = cipher_in; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1567 | keycount = strsep(&tmp, "-"); |
| 1568 | cipher = strsep(&keycount, ":"); |
| 1569 | |
| 1570 | if (!keycount) |
| 1571 | cc->tfms_count = 1; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1572 | else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 || |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1573 | !is_power_of_2(cc->tfms_count)) { |
| 1574 | ti->error = "Bad cipher key count specification"; |
| 1575 | return -EINVAL; |
| 1576 | } |
| 1577 | cc->key_parts = cc->tfms_count; |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1578 | cc->key_extra_size = 0; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1579 | |
| 1580 | cc->cipher = kstrdup(cipher, GFP_KERNEL); |
| 1581 | if (!cc->cipher) |
| 1582 | goto bad_mem; |
| 1583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | chainmode = strsep(&tmp, "-"); |
| 1585 | ivopts = strsep(&tmp, "-"); |
| 1586 | ivmode = strsep(&ivopts, ":"); |
| 1587 | |
| 1588 | if (tmp) |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1589 | DMWARN("Ignoring unexpected additional cipher options"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1591 | cc->cpu = __alloc_percpu(sizeof(*(cc->cpu)), |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1592 | __alignof__(struct crypt_cpu)); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1593 | if (!cc->cpu) { |
| 1594 | ti->error = "Cannot allocate per cpu state"; |
| 1595 | goto bad_mem; |
| 1596 | } |
| 1597 | |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 1598 | /* |
| 1599 | * For compatibility with the original dm-crypt mapping format, if |
| 1600 | * only the cipher name is supplied, use cbc-plain. |
| 1601 | */ |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1602 | if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | chainmode = "cbc"; |
| 1604 | ivmode = "plain"; |
| 1605 | } |
| 1606 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 1607 | if (strcmp(chainmode, "ecb") && !ivmode) { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1608 | ti->error = "IV mechanism required"; |
| 1609 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | } |
| 1611 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1612 | cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL); |
| 1613 | if (!cipher_api) |
| 1614 | goto bad_mem; |
| 1615 | |
| 1616 | ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME, |
| 1617 | "%s(%s)", chainmode, cipher); |
| 1618 | if (ret < 0) { |
| 1619 | kfree(cipher_api); |
| 1620 | goto bad_mem; |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 1621 | } |
| 1622 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1623 | /* Allocate cipher */ |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1624 | ret = crypt_alloc_tfms(cc, cipher_api); |
| 1625 | if (ret < 0) { |
| 1626 | ti->error = "Error allocating crypto tfm"; |
| 1627 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1630 | /* Initialize IV */ |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1631 | cc->iv_size = crypto_ablkcipher_ivsize(any_tfm(cc)); |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1632 | if (cc->iv_size) |
| 1633 | /* at least a 64 bit sector number should fit in our buffer */ |
| 1634 | cc->iv_size = max(cc->iv_size, |
| 1635 | (unsigned int)(sizeof(u64) / sizeof(u8))); |
| 1636 | else if (ivmode) { |
| 1637 | DMWARN("Selected cipher does not support IVs"); |
| 1638 | ivmode = NULL; |
| 1639 | } |
| 1640 | |
| 1641 | /* Choose ivmode, see comments at iv code. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | if (ivmode == NULL) |
| 1643 | cc->iv_gen_ops = NULL; |
| 1644 | else if (strcmp(ivmode, "plain") == 0) |
| 1645 | cc->iv_gen_ops = &crypt_iv_plain_ops; |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 1646 | else if (strcmp(ivmode, "plain64") == 0) |
| 1647 | cc->iv_gen_ops = &crypt_iv_plain64_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | else if (strcmp(ivmode, "essiv") == 0) |
| 1649 | cc->iv_gen_ops = &crypt_iv_essiv_ops; |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 1650 | else if (strcmp(ivmode, "benbi") == 0) |
| 1651 | cc->iv_gen_ops = &crypt_iv_benbi_ops; |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 1652 | else if (strcmp(ivmode, "null") == 0) |
| 1653 | cc->iv_gen_ops = &crypt_iv_null_ops; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 1654 | else if (strcmp(ivmode, "lmk") == 0) { |
| 1655 | cc->iv_gen_ops = &crypt_iv_lmk_ops; |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 1656 | /* |
| 1657 | * Version 2 and 3 is recognised according |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 1658 | * to length of provided multi-key string. |
| 1659 | * If present (version 3), last key is used as IV seed. |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 1660 | * All keys (including IV seed) are always the same size. |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 1661 | */ |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1662 | if (cc->key_size % cc->key_parts) { |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 1663 | cc->key_parts++; |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1664 | cc->key_extra_size = cc->key_size / cc->key_parts; |
| 1665 | } |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 1666 | } else if (strcmp(ivmode, "tcw") == 0) { |
| 1667 | cc->iv_gen_ops = &crypt_iv_tcw_ops; |
| 1668 | cc->key_parts += 2; /* IV + whitening */ |
| 1669 | cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 1670 | } else { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1671 | ret = -EINVAL; |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1672 | ti->error = "Invalid IV mode"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1673 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | } |
| 1675 | |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1676 | /* Initialize and set key */ |
| 1677 | ret = crypt_set_key(cc, key); |
| 1678 | if (ret < 0) { |
| 1679 | ti->error = "Error decoding and setting key"; |
| 1680 | goto bad; |
| 1681 | } |
| 1682 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1683 | /* Allocate IV */ |
| 1684 | if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) { |
| 1685 | ret = cc->iv_gen_ops->ctr(cc, ti, ivopts); |
| 1686 | if (ret < 0) { |
| 1687 | ti->error = "Error creating IV"; |
| 1688 | goto bad; |
| 1689 | } |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1692 | /* Initialize IV (set keys for ESSIV etc) */ |
| 1693 | if (cc->iv_gen_ops && cc->iv_gen_ops->init) { |
| 1694 | ret = cc->iv_gen_ops->init(cc); |
| 1695 | if (ret < 0) { |
| 1696 | ti->error = "Error initialising IV"; |
| 1697 | goto bad; |
| 1698 | } |
| 1699 | } |
| 1700 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1701 | ret = 0; |
| 1702 | bad: |
| 1703 | kfree(cipher_api); |
| 1704 | return ret; |
| 1705 | |
| 1706 | bad_mem: |
| 1707 | ti->error = "Cannot allocate cipher strings"; |
| 1708 | return -ENOMEM; |
| 1709 | } |
| 1710 | |
| 1711 | /* |
| 1712 | * Construct an encryption mapping: |
| 1713 | * <cipher> <key> <iv_offset> <dev_path> <start> |
| 1714 | */ |
| 1715 | static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 1716 | { |
| 1717 | struct crypt_config *cc; |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1718 | unsigned int key_size, opt_params; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1719 | unsigned long long tmpll; |
| 1720 | int ret; |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1721 | struct dm_arg_set as; |
| 1722 | const char *opt_string; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1723 | char dummy; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1724 | |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1725 | static struct dm_arg _args[] = { |
| 1726 | {0, 1, "Invalid number of feature args"}, |
| 1727 | }; |
| 1728 | |
| 1729 | if (argc < 5) { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1730 | ti->error = "Not enough arguments"; |
| 1731 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1732 | } |
| 1733 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1734 | key_size = strlen(argv[1]) >> 1; |
| 1735 | |
| 1736 | cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL); |
| 1737 | if (!cc) { |
| 1738 | ti->error = "Cannot allocate encryption context"; |
| 1739 | return -ENOMEM; |
| 1740 | } |
Milan Broz | 69a8cfc | 2011-01-13 19:59:49 +0000 | [diff] [blame] | 1741 | cc->key_size = key_size; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1742 | |
| 1743 | ti->private = cc; |
| 1744 | ret = crypt_ctr_cipher(ti, argv[0], argv[1]); |
| 1745 | if (ret < 0) |
| 1746 | goto bad; |
| 1747 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1748 | ret = -ENOMEM; |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1749 | cc->io_pool = mempool_create_slab_pool(MIN_IOS, _crypt_io_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | if (!cc->io_pool) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1751 | ti->error = "Cannot allocate crypt io mempool"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1752 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | } |
| 1754 | |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1755 | cc->dmreq_start = sizeof(struct ablkcipher_request); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1756 | cc->dmreq_start += crypto_ablkcipher_reqsize(any_tfm(cc)); |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1757 | cc->dmreq_start = ALIGN(cc->dmreq_start, crypto_tfm_ctx_alignment()); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1758 | cc->dmreq_start += crypto_ablkcipher_alignmask(any_tfm(cc)) & |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1759 | ~(crypto_tfm_ctx_alignment() - 1); |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1760 | |
| 1761 | cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start + |
| 1762 | sizeof(struct dm_crypt_request) + cc->iv_size); |
| 1763 | if (!cc->req_pool) { |
| 1764 | ti->error = "Cannot allocate crypt request mempool"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1765 | goto bad; |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1766 | } |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1767 | |
Matthew Dobson | a19b27c | 2006-03-26 01:37:45 -0800 | [diff] [blame] | 1768 | cc->page_pool = mempool_create_page_pool(MIN_POOL_PAGES, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | if (!cc->page_pool) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1770 | ti->error = "Cannot allocate page mempool"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1771 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | } |
| 1773 | |
Jens Axboe | bb799ca | 2008-12-10 15:35:05 +0100 | [diff] [blame] | 1774 | cc->bs = bioset_create(MIN_IOS, 0); |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 1775 | if (!cc->bs) { |
| 1776 | ti->error = "Cannot allocate crypt bioset"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1777 | goto bad; |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 1778 | } |
| 1779 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1780 | ret = -EINVAL; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1781 | if (sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1782 | ti->error = "Invalid iv_offset sector"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1783 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | } |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 1785 | cc->iv_offset = tmpll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1787 | if (dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev)) { |
| 1788 | ti->error = "Device lookup failed"; |
| 1789 | goto bad; |
| 1790 | } |
| 1791 | |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1792 | if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1793 | ti->error = "Invalid device sector"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1794 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | } |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 1796 | cc->start = tmpll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1798 | argv += 5; |
| 1799 | argc -= 5; |
| 1800 | |
| 1801 | /* Optional parameters */ |
| 1802 | if (argc) { |
| 1803 | as.argc = argc; |
| 1804 | as.argv = argv; |
| 1805 | |
| 1806 | ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error); |
| 1807 | if (ret) |
| 1808 | goto bad; |
| 1809 | |
| 1810 | opt_string = dm_shift_arg(&as); |
| 1811 | |
| 1812 | if (opt_params == 1 && opt_string && |
| 1813 | !strcasecmp(opt_string, "allow_discards")) |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1814 | ti->num_discard_bios = 1; |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1815 | else if (opt_params) { |
| 1816 | ret = -EINVAL; |
| 1817 | ti->error = "Invalid feature arguments"; |
| 1818 | goto bad; |
| 1819 | } |
| 1820 | } |
| 1821 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1822 | ret = -ENOMEM; |
Tejun Heo | 670368a | 2013-07-30 08:40:21 -0400 | [diff] [blame] | 1823 | cc->io_queue = alloc_workqueue("kcryptd_io", WQ_MEM_RECLAIM, 1); |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1824 | if (!cc->io_queue) { |
| 1825 | ti->error = "Couldn't create kcryptd io queue"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1826 | goto bad; |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1827 | } |
| 1828 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1829 | cc->crypt_queue = alloc_workqueue("kcryptd", |
Tejun Heo | 670368a | 2013-07-30 08:40:21 -0400 | [diff] [blame] | 1830 | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 1); |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1831 | if (!cc->crypt_queue) { |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 1832 | ti->error = "Couldn't create kcryptd queue"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1833 | goto bad; |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 1834 | } |
| 1835 | |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1836 | ti->num_flush_bios = 1; |
Alasdair G Kergon | 0ac5548 | 2012-07-27 15:08:08 +0100 | [diff] [blame] | 1837 | ti->discard_zeroes_data_unsupported = true; |
Milan Broz | 983c7db | 2011-09-25 23:26:21 +0100 | [diff] [blame] | 1838 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | return 0; |
| 1840 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1841 | bad: |
| 1842 | crypt_dtr(ti); |
| 1843 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | } |
| 1845 | |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 1846 | static int crypt_map(struct dm_target *ti, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1848 | struct dm_crypt_io *io; |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1849 | struct crypt_config *cc = ti->private; |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 1850 | |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1851 | /* |
| 1852 | * If bio is REQ_FLUSH or REQ_DISCARD, just bypass crypt queues. |
| 1853 | * - for REQ_FLUSH device-mapper core ensures that no IO is in-flight |
| 1854 | * - for REQ_DISCARD caller must use flush if IO ordering matters |
| 1855 | */ |
| 1856 | if (unlikely(bio->bi_rw & (REQ_FLUSH | REQ_DISCARD))) { |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 1857 | bio->bi_bdev = cc->dev->bdev; |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1858 | if (bio_sectors(bio)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1859 | bio->bi_iter.bi_sector = cc->start + |
| 1860 | dm_target_offset(ti, bio->bi_iter.bi_sector); |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 1861 | return DM_MAPIO_REMAPPED; |
| 1862 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1864 | io = crypt_io_alloc(cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector)); |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1865 | |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1866 | if (bio_data_dir(io->base_bio) == READ) { |
| 1867 | if (kcryptd_io_read(io, GFP_NOWAIT)) |
| 1868 | kcryptd_queue_io(io); |
| 1869 | } else |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1870 | kcryptd_queue_crypt(io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1872 | return DM_MAPIO_SUBMITTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | } |
| 1874 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 1875 | static void crypt_status(struct dm_target *ti, status_type_t type, |
| 1876 | unsigned status_flags, char *result, unsigned maxlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1878 | struct crypt_config *cc = ti->private; |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 1879 | unsigned i, sz = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | |
| 1881 | switch (type) { |
| 1882 | case STATUSTYPE_INFO: |
| 1883 | result[0] = '\0'; |
| 1884 | break; |
| 1885 | |
| 1886 | case STATUSTYPE_TABLE: |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 1887 | DMEMIT("%s ", cc->cipher_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 1889 | if (cc->key_size > 0) |
| 1890 | for (i = 0; i < cc->key_size; i++) |
| 1891 | DMEMIT("%02x", cc->key[i]); |
| 1892 | else |
| 1893 | DMEMIT("-"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 1895 | DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, |
| 1896 | cc->dev->name, (unsigned long long)cc->start); |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1897 | |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1898 | if (ti->num_discard_bios) |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1899 | DMEMIT(" 1 allow_discards"); |
| 1900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | break; |
| 1902 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1905 | static void crypt_postsuspend(struct dm_target *ti) |
| 1906 | { |
| 1907 | struct crypt_config *cc = ti->private; |
| 1908 | |
| 1909 | set_bit(DM_CRYPT_SUSPENDED, &cc->flags); |
| 1910 | } |
| 1911 | |
| 1912 | static int crypt_preresume(struct dm_target *ti) |
| 1913 | { |
| 1914 | struct crypt_config *cc = ti->private; |
| 1915 | |
| 1916 | if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { |
| 1917 | DMERR("aborting resume - crypt key is not set."); |
| 1918 | return -EAGAIN; |
| 1919 | } |
| 1920 | |
| 1921 | return 0; |
| 1922 | } |
| 1923 | |
| 1924 | static void crypt_resume(struct dm_target *ti) |
| 1925 | { |
| 1926 | struct crypt_config *cc = ti->private; |
| 1927 | |
| 1928 | clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); |
| 1929 | } |
| 1930 | |
| 1931 | /* Message interface |
| 1932 | * key set <key> |
| 1933 | * key wipe |
| 1934 | */ |
| 1935 | static int crypt_message(struct dm_target *ti, unsigned argc, char **argv) |
| 1936 | { |
| 1937 | struct crypt_config *cc = ti->private; |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 1938 | int ret = -EINVAL; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1939 | |
| 1940 | if (argc < 2) |
| 1941 | goto error; |
| 1942 | |
Mike Snitzer | 498f010 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1943 | if (!strcasecmp(argv[0], "key")) { |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1944 | if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { |
| 1945 | DMWARN("not suspended during key manipulation."); |
| 1946 | return -EINVAL; |
| 1947 | } |
Mike Snitzer | 498f010 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1948 | if (argc == 3 && !strcasecmp(argv[1], "set")) { |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 1949 | ret = crypt_set_key(cc, argv[2]); |
| 1950 | if (ret) |
| 1951 | return ret; |
| 1952 | if (cc->iv_gen_ops && cc->iv_gen_ops->init) |
| 1953 | ret = cc->iv_gen_ops->init(cc); |
| 1954 | return ret; |
| 1955 | } |
Mike Snitzer | 498f010 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 1956 | if (argc == 2 && !strcasecmp(argv[1], "wipe")) { |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 1957 | if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) { |
| 1958 | ret = cc->iv_gen_ops->wipe(cc); |
| 1959 | if (ret) |
| 1960 | return ret; |
| 1961 | } |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1962 | return crypt_wipe_key(cc); |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 1963 | } |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
| 1966 | error: |
| 1967 | DMWARN("unrecognised message received."); |
| 1968 | return -EINVAL; |
| 1969 | } |
| 1970 | |
Milan Broz | d41e26b | 2008-07-21 12:00:40 +0100 | [diff] [blame] | 1971 | static int crypt_merge(struct dm_target *ti, struct bvec_merge_data *bvm, |
| 1972 | struct bio_vec *biovec, int max_size) |
| 1973 | { |
| 1974 | struct crypt_config *cc = ti->private; |
| 1975 | struct request_queue *q = bdev_get_queue(cc->dev->bdev); |
| 1976 | |
| 1977 | if (!q->merge_bvec_fn) |
| 1978 | return max_size; |
| 1979 | |
| 1980 | bvm->bi_bdev = cc->dev->bdev; |
Alasdair G Kergon | b441a262 | 2010-08-12 04:14:11 +0100 | [diff] [blame] | 1981 | bvm->bi_sector = cc->start + dm_target_offset(ti, bvm->bi_sector); |
Milan Broz | d41e26b | 2008-07-21 12:00:40 +0100 | [diff] [blame] | 1982 | |
| 1983 | return min(max_size, q->merge_bvec_fn(q, bvm, biovec)); |
| 1984 | } |
| 1985 | |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1986 | static int crypt_iterate_devices(struct dm_target *ti, |
| 1987 | iterate_devices_callout_fn fn, void *data) |
| 1988 | { |
| 1989 | struct crypt_config *cc = ti->private; |
| 1990 | |
Mike Snitzer | 5dea271 | 2009-07-23 20:30:42 +0100 | [diff] [blame] | 1991 | return fn(ti, cc->dev, cc->start, ti->len, data); |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 1992 | } |
| 1993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | static struct target_type crypt_target = { |
| 1995 | .name = "crypt", |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 1996 | .version = {1, 13, 0}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | .module = THIS_MODULE, |
| 1998 | .ctr = crypt_ctr, |
| 1999 | .dtr = crypt_dtr, |
| 2000 | .map = crypt_map, |
| 2001 | .status = crypt_status, |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2002 | .postsuspend = crypt_postsuspend, |
| 2003 | .preresume = crypt_preresume, |
| 2004 | .resume = crypt_resume, |
| 2005 | .message = crypt_message, |
Milan Broz | d41e26b | 2008-07-21 12:00:40 +0100 | [diff] [blame] | 2006 | .merge = crypt_merge, |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 2007 | .iterate_devices = crypt_iterate_devices, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | }; |
| 2009 | |
| 2010 | static int __init dm_crypt_init(void) |
| 2011 | { |
| 2012 | int r; |
| 2013 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 2014 | _crypt_io_pool = KMEM_CACHE(dm_crypt_io, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | if (!_crypt_io_pool) |
| 2016 | return -ENOMEM; |
| 2017 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | r = dm_register_target(&crypt_target); |
| 2019 | if (r < 0) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 2020 | DMERR("register failed %d", r); |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 2021 | kmem_cache_destroy(_crypt_io_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | return r; |
| 2025 | } |
| 2026 | |
| 2027 | static void __exit dm_crypt_exit(void) |
| 2028 | { |
Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 2029 | dm_unregister_target(&crypt_target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | kmem_cache_destroy(_crypt_io_pool); |
| 2031 | } |
| 2032 | |
| 2033 | module_init(dm_crypt_init); |
| 2034 | module_exit(dm_crypt_exit); |
| 2035 | |
| 2036 | MODULE_AUTHOR("Christophe Saout <christophe@saout.de>"); |
| 2037 | MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption"); |
| 2038 | MODULE_LICENSE("GPL"); |