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