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