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 | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2017 Red Hat, Inc. All rights reserved. |
| 5 | * Copyright (C) 2013-2017 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> |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 15 | #include <linux/key.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/bio.h> |
| 17 | #include <linux/blkdev.h> |
| 18 | #include <linux/mempool.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/crypto.h> |
| 21 | #include <linux/workqueue.h> |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 22 | #include <linux/kthread.h> |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 23 | #include <linux/backing-dev.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 24 | #include <linux/atomic.h> |
David Hardeman | 378f058 | 2005-09-17 17:55:31 +1000 | [diff] [blame] | 25 | #include <linux/scatterlist.h> |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 26 | #include <linux/rbtree.h> |
Ondrej Kozina | 027c431 | 2016-12-01 18:20:52 +0100 | [diff] [blame] | 27 | #include <linux/ctype.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <asm/page.h> |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 29 | #include <asm/unaligned.h> |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 30 | #include <crypto/hash.h> |
| 31 | #include <crypto/md5.h> |
| 32 | #include <crypto/algapi.h> |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 33 | #include <crypto/skcipher.h> |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 34 | #include <crypto/aead.h> |
| 35 | #include <crypto/authenc.h> |
| 36 | #include <linux/rtnetlink.h> /* for struct rtattr and RTA macros only */ |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 37 | #include <keys/user-type.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Mikulas Patocka | 586e80e | 2008-10-21 17:44:59 +0100 | [diff] [blame] | 39 | #include <linux/device-mapper.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 41 | #define DM_MSG_PREFIX "crypt" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * context holding the current state of a multi-part conversion |
| 45 | */ |
| 46 | struct convert_context { |
Milan Broz | 43d6903 | 2008-02-08 02:11:09 +0000 | [diff] [blame] | 47 | struct completion restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | struct bio *bio_in; |
| 49 | struct bio *bio_out; |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 50 | struct bvec_iter iter_in; |
| 51 | struct bvec_iter iter_out; |
Mikulas Patocka | c66029f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 52 | sector_t cc_sector; |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 53 | atomic_t cc_pending; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 54 | union { |
| 55 | struct skcipher_request *req; |
| 56 | struct aead_request *req_aead; |
| 57 | } r; |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 61 | /* |
| 62 | * per bio private data |
| 63 | */ |
| 64 | struct dm_crypt_io { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 65 | struct crypt_config *cc; |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 66 | struct bio *base_bio; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 67 | u8 *integrity_metadata; |
| 68 | bool integrity_metadata_from_pool; |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 69 | struct work_struct work; |
| 70 | |
| 71 | struct convert_context ctx; |
| 72 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 73 | atomic_t io_pending; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 74 | blk_status_t error; |
Milan Broz | 0c395b0 | 2008-02-08 02:10:54 +0000 | [diff] [blame] | 75 | sector_t sector; |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 76 | |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 77 | struct rb_node rb_node; |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 78 | } CRYPTO_MINALIGN_ATTR; |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 79 | |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 80 | struct dm_crypt_request { |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 81 | struct convert_context *ctx; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 82 | struct scatterlist sg_in[4]; |
| 83 | struct scatterlist sg_out[4]; |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 84 | sector_t iv_sector; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | struct crypt_config; |
| 88 | |
| 89 | struct crypt_iv_operations { |
| 90 | int (*ctr)(struct crypt_config *cc, struct dm_target *ti, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 91 | const char *opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | void (*dtr)(struct crypt_config *cc); |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 93 | int (*init)(struct crypt_config *cc); |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 94 | int (*wipe)(struct crypt_config *cc); |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 95 | int (*generator)(struct crypt_config *cc, u8 *iv, |
| 96 | struct dm_crypt_request *dmreq); |
| 97 | int (*post)(struct crypt_config *cc, u8 *iv, |
| 98 | struct dm_crypt_request *dmreq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 101 | struct iv_essiv_private { |
Kees Cook | c07c88f | 2018-07-15 20:59:12 -0700 | [diff] [blame] | 102 | struct crypto_shash *hash_tfm; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 103 | u8 *salt; |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | struct iv_benbi_private { |
| 107 | int shift; |
| 108 | }; |
| 109 | |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 110 | #define LMK_SEED_SIZE 64 /* hash + 0 */ |
| 111 | struct iv_lmk_private { |
| 112 | struct crypto_shash *hash_tfm; |
| 113 | u8 *seed; |
| 114 | }; |
| 115 | |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 116 | #define TCW_WHITENING_SIZE 16 |
| 117 | struct iv_tcw_private { |
| 118 | struct crypto_shash *crc32_tfm; |
| 119 | u8 *iv_seed; |
| 120 | u8 *whitening; |
| 121 | }; |
| 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | /* |
| 124 | * Crypt: maps a linear range of a block device |
| 125 | * and encrypts / decrypts at the same time. |
| 126 | */ |
Mikulas Patocka | 0f5d8e6 | 2015-02-13 08:27:08 -0500 | [diff] [blame] | 127 | enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID, |
Rabin Vincent | f659b10 | 2016-09-21 16:22:29 +0200 | [diff] [blame] | 128 | DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD }; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 129 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 130 | enum cipher_flags { |
| 131 | CRYPT_MODE_INTEGRITY_AEAD, /* Use authenticated mode for cihper */ |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 132 | CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */ |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 133 | }; |
| 134 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 135 | /* |
Mikulas Patocka | 610f2de | 2014-02-20 18:01:01 -0500 | [diff] [blame] | 136 | * The fields in here must be read only after initialization. |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 137 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | struct crypt_config { |
| 139 | struct dm_dev *dev; |
| 140 | sector_t start; |
| 141 | |
Mikulas Patocka | 5059353 | 2017-08-13 22:45:08 -0400 | [diff] [blame] | 142 | struct percpu_counter n_allocated_pages; |
| 143 | |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 144 | struct workqueue_struct *io_queue; |
| 145 | struct workqueue_struct *crypt_queue; |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 146 | |
Mikulas Patocka | c7329ef | 2018-07-11 12:10:51 -0400 | [diff] [blame] | 147 | spinlock_t write_thread_lock; |
Mike Snitzer | 72d711c | 2018-05-22 18:26:20 -0400 | [diff] [blame] | 148 | struct task_struct *write_thread; |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 149 | struct rb_root write_tree; |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 150 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 151 | char *cipher; |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 152 | char *cipher_string; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 153 | char *cipher_auth; |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 154 | char *key_string; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 155 | |
Julia Lawall | 1b1b58f | 2015-11-29 14:09:19 +0100 | [diff] [blame] | 156 | const struct crypt_iv_operations *iv_gen_ops; |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 157 | union { |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 158 | struct iv_essiv_private essiv; |
| 159 | struct iv_benbi_private benbi; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 160 | struct iv_lmk_private lmk; |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 161 | struct iv_tcw_private tcw; |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 162 | } iv_gen_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | sector_t iv_offset; |
| 164 | unsigned int iv_size; |
Mikulas Patocka | ff3af92 | 2017-03-23 10:23:14 -0400 | [diff] [blame] | 165 | unsigned short int sector_size; |
| 166 | unsigned char sector_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 168 | /* ESSIV: struct crypto_cipher *essiv_tfm */ |
| 169 | void *iv_private; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 170 | union { |
| 171 | struct crypto_skcipher **tfms; |
| 172 | struct crypto_aead **tfms_aead; |
| 173 | } cipher_tfm; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 174 | unsigned tfms_count; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 175 | unsigned long cipher_flags; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 176 | |
| 177 | /* |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 178 | * Layout of each crypto request: |
| 179 | * |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 180 | * struct skcipher_request |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 181 | * context |
| 182 | * padding |
| 183 | * struct dm_crypt_request |
| 184 | * padding |
| 185 | * IV |
| 186 | * |
| 187 | * The padding is added so that dm_crypt_request and the IV are |
| 188 | * correctly aligned. |
| 189 | */ |
| 190 | unsigned int dmreq_start; |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 191 | |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 192 | unsigned int per_bio_data_size; |
| 193 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 194 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | unsigned int key_size; |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 196 | unsigned int key_parts; /* independent parts in key buffer */ |
| 197 | unsigned int key_extra_size; /* additional keys length */ |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 198 | unsigned int key_mac_size; /* MAC key size for authenc(...) */ |
| 199 | |
| 200 | unsigned int integrity_tag_size; |
| 201 | unsigned int integrity_iv_size; |
| 202 | unsigned int on_disk_tag_size; |
| 203 | |
Mike Snitzer | 72d711c | 2018-05-22 18:26:20 -0400 | [diff] [blame] | 204 | /* |
| 205 | * pool for per bio private data, crypto requests, |
| 206 | * encryption requeusts/buffer pages and integrity tags |
| 207 | */ |
| 208 | unsigned tag_pool_max_sectors; |
| 209 | mempool_t tag_pool; |
| 210 | mempool_t req_pool; |
| 211 | mempool_t page_pool; |
| 212 | |
| 213 | struct bio_set bs; |
| 214 | struct mutex bio_alloc_lock; |
| 215 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 216 | u8 *authenc_key; /* space for keys in authenc() format (if used) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | u8 key[0]; |
| 218 | }; |
| 219 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 220 | #define MIN_IOS 64 |
| 221 | #define MAX_TAG_SIZE 480 |
| 222 | #define POOL_ENTRY_SIZE 512 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Mikulas Patocka | 5059353 | 2017-08-13 22:45:08 -0400 | [diff] [blame] | 224 | static DEFINE_SPINLOCK(dm_crypt_clients_lock); |
| 225 | static unsigned dm_crypt_clients_n = 0; |
| 226 | static volatile unsigned long dm_crypt_pages_per_client; |
| 227 | #define DM_CRYPT_MEMORY_PERCENT 2 |
| 228 | #define DM_CRYPT_MIN_PAGES_PER_CLIENT (BIO_MAX_PAGES * 16) |
| 229 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 230 | static void clone_init(struct dm_crypt_io *, struct bio *); |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 231 | static void kcryptd_queue_crypt(struct dm_crypt_io *io); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 232 | static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc, |
| 233 | struct scatterlist *sg); |
Olaf Kirch | 027581f | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 234 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 235 | /* |
Eric Biggers | 86f917a | 2017-03-30 22:18:48 -0700 | [diff] [blame] | 236 | * Use this to access cipher attributes that are independent of the key. |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 237 | */ |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 238 | static struct crypto_skcipher *any_tfm(struct crypt_config *cc) |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 239 | { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 240 | return cc->cipher_tfm.tfms[0]; |
| 241 | } |
| 242 | |
| 243 | static struct crypto_aead *any_tfm_aead(struct crypt_config *cc) |
| 244 | { |
| 245 | return cc->cipher_tfm.tfms_aead[0]; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | * Different IV generation algorithms: |
| 250 | * |
Rik Snel | 3c164bd | 2006-09-02 18:17:33 +1000 | [diff] [blame] | 251 | * 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] | 252 | * number, padded with zeros if necessary. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | * |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 254 | * plain64: the initial vector is the 64-bit little-endian version of the sector |
| 255 | * number, padded with zeros if necessary. |
| 256 | * |
Milan Broz | 7e3fd85 | 2017-06-06 09:07:01 +0200 | [diff] [blame] | 257 | * plain64be: the initial vector is the 64-bit big-endian version of the sector |
| 258 | * number, padded with zeros if necessary. |
| 259 | * |
Rik Snel | 3c164bd | 2006-09-02 18:17:33 +1000 | [diff] [blame] | 260 | * essiv: "encrypted sector|salt initial vector", the sector number is |
| 261 | * encrypted with the bulk cipher using a salt as key. The salt |
| 262 | * should be derived from the bulk cipher's key via hashing. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | * |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 264 | * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1 |
| 265 | * (needed for LRW-32-AES and possible other narrow block modes) |
| 266 | * |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 267 | * null: the initial vector is always zero. Provides compatibility with |
| 268 | * obsolete loop_fish2 devices. Do not use for new devices. |
| 269 | * |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 270 | * lmk: Compatible implementation of the block chaining mode used |
| 271 | * by the Loop-AES block device encryption system |
| 272 | * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/ |
| 273 | * It operates on full 512 byte sectors and uses CBC |
| 274 | * with an IV derived from the sector number, the data and |
| 275 | * optionally extra IV seed. |
| 276 | * This means that after decryption the first block |
| 277 | * of sector must be tweaked according to decrypted data. |
| 278 | * Loop-AES can use three encryption schemes: |
| 279 | * version 1: is plain aes-cbc mode |
| 280 | * version 2: uses 64 multikey scheme with lmk IV generator |
| 281 | * version 3: the same as version 2 with additional IV seed |
| 282 | * (it uses 65 keys, last key is used as IV seed) |
| 283 | * |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 284 | * tcw: Compatible implementation of the block chaining mode used |
| 285 | * by the TrueCrypt device encryption system (prior to version 4.1). |
Milan Broz | e44f23b | 2015-04-05 18:03:10 +0200 | [diff] [blame] | 286 | * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 287 | * It operates on full 512 byte sectors and uses CBC |
| 288 | * with an IV derived from initial key and the sector number. |
| 289 | * In addition, whitening value is applied on every sector, whitening |
| 290 | * is calculated from initial key, sector number and mixed using CRC32. |
| 291 | * Note that this encryption scheme is vulnerable to watermarking attacks |
| 292 | * and should be used for old compatible containers access only. |
| 293 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | * plumb: unimplemented, see: |
| 295 | * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454 |
| 296 | */ |
| 297 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 298 | static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, |
| 299 | struct dm_crypt_request *dmreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
| 301 | memset(iv, 0, cc->iv_size); |
Alasdair G Kergon | 283a832 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 302 | *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 307 | static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv, |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 308 | struct dm_crypt_request *dmreq) |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 309 | { |
| 310 | memset(iv, 0, cc->iv_size); |
Alasdair G Kergon | 283a832 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 311 | *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector); |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
Milan Broz | 7e3fd85 | 2017-06-06 09:07:01 +0200 | [diff] [blame] | 316 | static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv, |
| 317 | struct dm_crypt_request *dmreq) |
| 318 | { |
| 319 | memset(iv, 0, cc->iv_size); |
| 320 | /* iv_size is at least of size u64; usually it is 16 bytes */ |
| 321 | *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 326 | /* Initialise ESSIV - compute salt but no local memory allocations */ |
| 327 | static int crypt_iv_essiv_init(struct crypt_config *cc) |
| 328 | { |
| 329 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; |
Kees Cook | c07c88f | 2018-07-15 20:59:12 -0700 | [diff] [blame] | 330 | SHASH_DESC_ON_STACK(desc, essiv->hash_tfm); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 331 | struct crypto_cipher *essiv_tfm; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 332 | int err; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 333 | |
Kees Cook | c07c88f | 2018-07-15 20:59:12 -0700 | [diff] [blame] | 334 | desc->tfm = essiv->hash_tfm; |
Mikulas Patocka | 432061b | 2018-09-05 09:17:45 -0400 | [diff] [blame] | 335 | desc->flags = 0; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 336 | |
Kees Cook | c07c88f | 2018-07-15 20:59:12 -0700 | [diff] [blame] | 337 | err = crypto_shash_digest(desc, cc->key, cc->key_size, essiv->salt); |
| 338 | shash_desc_zero(desc); |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 339 | if (err) |
| 340 | return err; |
| 341 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 342 | essiv_tfm = cc->iv_private; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 343 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 344 | err = crypto_cipher_setkey(essiv_tfm, essiv->salt, |
Kees Cook | c07c88f | 2018-07-15 20:59:12 -0700 | [diff] [blame] | 345 | crypto_shash_digestsize(essiv->hash_tfm)); |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 346 | if (err) |
| 347 | return err; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 348 | |
| 349 | return 0; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 352 | /* Wipe salt and reset key derived from volume key */ |
| 353 | static int crypt_iv_essiv_wipe(struct crypt_config *cc) |
| 354 | { |
| 355 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; |
Kees Cook | c07c88f | 2018-07-15 20:59:12 -0700 | [diff] [blame] | 356 | unsigned salt_size = crypto_shash_digestsize(essiv->hash_tfm); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 357 | struct crypto_cipher *essiv_tfm; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 358 | int r, err = 0; |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 359 | |
| 360 | memset(essiv->salt, 0, salt_size); |
| 361 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 362 | essiv_tfm = cc->iv_private; |
| 363 | r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size); |
| 364 | if (r) |
| 365 | err = r; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 366 | |
| 367 | return err; |
| 368 | } |
| 369 | |
Eric Biggers | 86f917a | 2017-03-30 22:18:48 -0700 | [diff] [blame] | 370 | /* Allocate the cipher for ESSIV */ |
| 371 | static struct crypto_cipher *alloc_essiv_cipher(struct crypt_config *cc, |
| 372 | struct dm_target *ti, |
| 373 | const u8 *salt, |
| 374 | unsigned int saltsize) |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 375 | { |
| 376 | struct crypto_cipher *essiv_tfm; |
| 377 | int err; |
| 378 | |
| 379 | /* Setup the essiv_tfm with the given salt */ |
| 380 | essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC); |
| 381 | if (IS_ERR(essiv_tfm)) { |
| 382 | ti->error = "Error allocating crypto tfm for ESSIV"; |
| 383 | return essiv_tfm; |
| 384 | } |
| 385 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 386 | if (crypto_cipher_blocksize(essiv_tfm) != cc->iv_size) { |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 387 | ti->error = "Block size of ESSIV cipher does " |
| 388 | "not match IV size of block cipher"; |
| 389 | crypto_free_cipher(essiv_tfm); |
| 390 | return ERR_PTR(-EINVAL); |
| 391 | } |
| 392 | |
| 393 | err = crypto_cipher_setkey(essiv_tfm, salt, saltsize); |
| 394 | if (err) { |
| 395 | ti->error = "Failed to set key for ESSIV cipher"; |
| 396 | crypto_free_cipher(essiv_tfm); |
| 397 | return ERR_PTR(err); |
| 398 | } |
| 399 | |
| 400 | return essiv_tfm; |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 403 | static void crypt_iv_essiv_dtr(struct crypt_config *cc) |
| 404 | { |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 405 | struct crypto_cipher *essiv_tfm; |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 406 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; |
| 407 | |
Kees Cook | c07c88f | 2018-07-15 20:59:12 -0700 | [diff] [blame] | 408 | crypto_free_shash(essiv->hash_tfm); |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 409 | essiv->hash_tfm = NULL; |
| 410 | |
| 411 | kzfree(essiv->salt); |
| 412 | essiv->salt = NULL; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 413 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 414 | essiv_tfm = cc->iv_private; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 415 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 416 | if (essiv_tfm) |
| 417 | crypto_free_cipher(essiv_tfm); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 418 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 419 | cc->iv_private = NULL; |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | 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] | 423 | const char *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | { |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 425 | struct crypto_cipher *essiv_tfm = NULL; |
Kees Cook | c07c88f | 2018-07-15 20:59:12 -0700 | [diff] [blame] | 426 | struct crypto_shash *hash_tfm = NULL; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 427 | u8 *salt = NULL; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 428 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 430 | if (!opts) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 431 | ti->error = "Digest algorithm missing for ESSIV mode"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return -EINVAL; |
| 433 | } |
| 434 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 435 | /* Allocate hash algorithm */ |
Kees Cook | c07c88f | 2018-07-15 20:59:12 -0700 | [diff] [blame] | 436 | hash_tfm = crypto_alloc_shash(opts, 0, 0); |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 437 | if (IS_ERR(hash_tfm)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 438 | ti->error = "Error initializing ESSIV hash"; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 439 | err = PTR_ERR(hash_tfm); |
| 440 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Kees Cook | c07c88f | 2018-07-15 20:59:12 -0700 | [diff] [blame] | 443 | salt = kzalloc(crypto_shash_digestsize(hash_tfm), GFP_KERNEL); |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 444 | if (!salt) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 445 | ti->error = "Error kmallocing salt storage in ESSIV"; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 446 | err = -ENOMEM; |
| 447 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 450 | cc->iv_gen_private.essiv.salt = salt; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 451 | cc->iv_gen_private.essiv.hash_tfm = hash_tfm; |
| 452 | |
Eric Biggers | 86f917a | 2017-03-30 22:18:48 -0700 | [diff] [blame] | 453 | essiv_tfm = alloc_essiv_cipher(cc, ti, salt, |
Kees Cook | c07c88f | 2018-07-15 20:59:12 -0700 | [diff] [blame] | 454 | crypto_shash_digestsize(hash_tfm)); |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 455 | if (IS_ERR(essiv_tfm)) { |
| 456 | crypt_iv_essiv_dtr(cc); |
| 457 | return PTR_ERR(essiv_tfm); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 458 | } |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 459 | cc->iv_private = essiv_tfm; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | return 0; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 462 | |
| 463 | bad: |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 464 | if (hash_tfm && !IS_ERR(hash_tfm)) |
Kees Cook | c07c88f | 2018-07-15 20:59:12 -0700 | [diff] [blame] | 465 | crypto_free_shash(hash_tfm); |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 466 | kfree(salt); |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 467 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 470 | static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, |
| 471 | struct dm_crypt_request *dmreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 473 | struct crypto_cipher *essiv_tfm = cc->iv_private; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | memset(iv, 0, cc->iv_size); |
Alasdair G Kergon | 283a832 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 476 | *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 477 | crypto_cipher_encrypt_one(essiv_tfm, iv, iv); |
| 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | return 0; |
| 480 | } |
| 481 | |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 482 | static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, |
| 483 | const char *opts) |
| 484 | { |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 485 | unsigned bs = crypto_skcipher_blocksize(any_tfm(cc)); |
David Howells | f0d1b0b | 2006-12-08 02:37:49 -0800 | [diff] [blame] | 486 | int log = ilog2(bs); |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 487 | |
| 488 | /* we need to calculate how far we must shift the sector count |
| 489 | * to get the cipher block count, we use this shift in _gen */ |
| 490 | |
| 491 | if (1 << log != bs) { |
| 492 | ti->error = "cypher blocksize is not a power of 2"; |
| 493 | return -EINVAL; |
| 494 | } |
| 495 | |
| 496 | if (log > 9) { |
| 497 | ti->error = "cypher blocksize is > 512"; |
| 498 | return -EINVAL; |
| 499 | } |
| 500 | |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 501 | cc->iv_gen_private.benbi.shift = 9 - log; |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static void crypt_iv_benbi_dtr(struct crypt_config *cc) |
| 507 | { |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 508 | } |
| 509 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 510 | static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, |
| 511 | struct dm_crypt_request *dmreq) |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 512 | { |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 513 | __be64 val; |
| 514 | |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 515 | memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 516 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 517 | 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] | 518 | put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | return 0; |
| 521 | } |
| 522 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 523 | static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, |
| 524 | struct dm_crypt_request *dmreq) |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 525 | { |
| 526 | memset(iv, 0, cc->iv_size); |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 531 | static void crypt_iv_lmk_dtr(struct crypt_config *cc) |
| 532 | { |
| 533 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 534 | |
| 535 | if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm)) |
| 536 | crypto_free_shash(lmk->hash_tfm); |
| 537 | lmk->hash_tfm = NULL; |
| 538 | |
| 539 | kzfree(lmk->seed); |
| 540 | lmk->seed = NULL; |
| 541 | } |
| 542 | |
| 543 | static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti, |
| 544 | const char *opts) |
| 545 | { |
| 546 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 547 | |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 548 | if (cc->sector_size != (1 << SECTOR_SHIFT)) { |
| 549 | ti->error = "Unsupported sector size for LMK"; |
| 550 | return -EINVAL; |
| 551 | } |
| 552 | |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 553 | lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0); |
| 554 | if (IS_ERR(lmk->hash_tfm)) { |
| 555 | ti->error = "Error initializing LMK hash"; |
| 556 | return PTR_ERR(lmk->hash_tfm); |
| 557 | } |
| 558 | |
| 559 | /* No seed in LMK version 2 */ |
| 560 | if (cc->key_parts == cc->tfms_count) { |
| 561 | lmk->seed = NULL; |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL); |
| 566 | if (!lmk->seed) { |
| 567 | crypt_iv_lmk_dtr(cc); |
| 568 | ti->error = "Error kmallocing seed storage in LMK"; |
| 569 | return -ENOMEM; |
| 570 | } |
| 571 | |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | static int crypt_iv_lmk_init(struct crypt_config *cc) |
| 576 | { |
| 577 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 578 | int subkey_size = cc->key_size / cc->key_parts; |
| 579 | |
| 580 | /* LMK seed is on the position of LMK_KEYS + 1 key */ |
| 581 | if (lmk->seed) |
| 582 | memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size), |
| 583 | crypto_shash_digestsize(lmk->hash_tfm)); |
| 584 | |
| 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | static int crypt_iv_lmk_wipe(struct crypt_config *cc) |
| 589 | { |
| 590 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 591 | |
| 592 | if (lmk->seed) |
| 593 | memset(lmk->seed, 0, LMK_SEED_SIZE); |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
| 598 | static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv, |
| 599 | struct dm_crypt_request *dmreq, |
| 600 | u8 *data) |
| 601 | { |
| 602 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 603 | SHASH_DESC_ON_STACK(desc, lmk->hash_tfm); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 604 | struct md5_state md5state; |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 605 | __le32 buf[4]; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 606 | int i, r; |
| 607 | |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 608 | desc->tfm = lmk->hash_tfm; |
Mikulas Patocka | 432061b | 2018-09-05 09:17:45 -0400 | [diff] [blame] | 609 | desc->flags = 0; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 610 | |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 611 | r = crypto_shash_init(desc); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 612 | if (r) |
| 613 | return r; |
| 614 | |
| 615 | if (lmk->seed) { |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 616 | r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 617 | if (r) |
| 618 | return r; |
| 619 | } |
| 620 | |
| 621 | /* Sector is always 512B, block size 16, add data of blocks 1-31 */ |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 622 | r = crypto_shash_update(desc, data + 16, 16 * 31); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 623 | if (r) |
| 624 | return r; |
| 625 | |
| 626 | /* Sector is cropped to 56 bits here */ |
| 627 | buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF); |
| 628 | buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000); |
| 629 | buf[2] = cpu_to_le32(4024); |
| 630 | buf[3] = 0; |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 631 | r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf)); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 632 | if (r) |
| 633 | return r; |
| 634 | |
| 635 | /* No MD5 padding here */ |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 636 | r = crypto_shash_export(desc, &md5state); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 637 | if (r) |
| 638 | return r; |
| 639 | |
| 640 | for (i = 0; i < MD5_HASH_WORDS; i++) |
| 641 | __cpu_to_le32s(&md5state.hash[i]); |
| 642 | memcpy(iv, &md5state.hash, cc->iv_size); |
| 643 | |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv, |
| 648 | struct dm_crypt_request *dmreq) |
| 649 | { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 650 | struct scatterlist *sg; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 651 | u8 *src; |
| 652 | int r = 0; |
| 653 | |
| 654 | if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 655 | sg = crypt_get_sg_data(cc, dmreq->sg_in); |
| 656 | src = kmap_atomic(sg_page(sg)); |
| 657 | r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset); |
Cong Wang | c2e022c | 2011-11-28 13:26:02 +0800 | [diff] [blame] | 658 | kunmap_atomic(src); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 659 | } else |
| 660 | memset(iv, 0, cc->iv_size); |
| 661 | |
| 662 | return r; |
| 663 | } |
| 664 | |
| 665 | static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv, |
| 666 | struct dm_crypt_request *dmreq) |
| 667 | { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 668 | struct scatterlist *sg; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 669 | u8 *dst; |
| 670 | int r; |
| 671 | |
| 672 | if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) |
| 673 | return 0; |
| 674 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 675 | sg = crypt_get_sg_data(cc, dmreq->sg_out); |
| 676 | dst = kmap_atomic(sg_page(sg)); |
| 677 | r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 678 | |
| 679 | /* Tweak the first block of plaintext sector */ |
| 680 | if (!r) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 681 | crypto_xor(dst + sg->offset, iv, cc->iv_size); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 682 | |
Cong Wang | c2e022c | 2011-11-28 13:26:02 +0800 | [diff] [blame] | 683 | kunmap_atomic(dst); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 684 | return r; |
| 685 | } |
| 686 | |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 687 | static void crypt_iv_tcw_dtr(struct crypt_config *cc) |
| 688 | { |
| 689 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
| 690 | |
| 691 | kzfree(tcw->iv_seed); |
| 692 | tcw->iv_seed = NULL; |
| 693 | kzfree(tcw->whitening); |
| 694 | tcw->whitening = NULL; |
| 695 | |
| 696 | if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm)) |
| 697 | crypto_free_shash(tcw->crc32_tfm); |
| 698 | tcw->crc32_tfm = NULL; |
| 699 | } |
| 700 | |
| 701 | static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti, |
| 702 | const char *opts) |
| 703 | { |
| 704 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
| 705 | |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 706 | if (cc->sector_size != (1 << SECTOR_SHIFT)) { |
| 707 | ti->error = "Unsupported sector size for TCW"; |
| 708 | return -EINVAL; |
| 709 | } |
| 710 | |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 711 | if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) { |
| 712 | ti->error = "Wrong key size for TCW"; |
| 713 | return -EINVAL; |
| 714 | } |
| 715 | |
| 716 | tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0); |
| 717 | if (IS_ERR(tcw->crc32_tfm)) { |
| 718 | ti->error = "Error initializing CRC32 in TCW"; |
| 719 | return PTR_ERR(tcw->crc32_tfm); |
| 720 | } |
| 721 | |
| 722 | tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL); |
| 723 | tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL); |
| 724 | if (!tcw->iv_seed || !tcw->whitening) { |
| 725 | crypt_iv_tcw_dtr(cc); |
| 726 | ti->error = "Error allocating seed storage in TCW"; |
| 727 | return -ENOMEM; |
| 728 | } |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | static int crypt_iv_tcw_init(struct crypt_config *cc) |
| 734 | { |
| 735 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
| 736 | int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE; |
| 737 | |
| 738 | memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size); |
| 739 | memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size], |
| 740 | TCW_WHITENING_SIZE); |
| 741 | |
| 742 | return 0; |
| 743 | } |
| 744 | |
| 745 | static int crypt_iv_tcw_wipe(struct crypt_config *cc) |
| 746 | { |
| 747 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
| 748 | |
| 749 | memset(tcw->iv_seed, 0, cc->iv_size); |
| 750 | memset(tcw->whitening, 0, TCW_WHITENING_SIZE); |
| 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | static int crypt_iv_tcw_whitening(struct crypt_config *cc, |
| 756 | struct dm_crypt_request *dmreq, |
| 757 | u8 *data) |
| 758 | { |
| 759 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
Bart Van Assche | 350b539 | 2016-06-28 16:32:32 +0200 | [diff] [blame] | 760 | __le64 sector = cpu_to_le64(dmreq->iv_sector); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 761 | u8 buf[TCW_WHITENING_SIZE]; |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 762 | SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 763 | int i, r; |
| 764 | |
| 765 | /* xor whitening with sector number */ |
Ard Biesheuvel | 45fe93d | 2017-07-24 11:28:04 +0100 | [diff] [blame] | 766 | crypto_xor_cpy(buf, tcw->whitening, (u8 *)§or, 8); |
| 767 | crypto_xor_cpy(&buf[8], tcw->whitening + 8, (u8 *)§or, 8); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 768 | |
| 769 | /* calculate crc32 for every 32bit part and xor it */ |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 770 | desc->tfm = tcw->crc32_tfm; |
Mikulas Patocka | 432061b | 2018-09-05 09:17:45 -0400 | [diff] [blame] | 771 | desc->flags = 0; |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 772 | for (i = 0; i < 4; i++) { |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 773 | r = crypto_shash_init(desc); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 774 | if (r) |
| 775 | goto out; |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 776 | r = crypto_shash_update(desc, &buf[i * 4], 4); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 777 | if (r) |
| 778 | goto out; |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 779 | r = crypto_shash_final(desc, &buf[i * 4]); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 780 | if (r) |
| 781 | goto out; |
| 782 | } |
| 783 | crypto_xor(&buf[0], &buf[12], 4); |
| 784 | crypto_xor(&buf[4], &buf[8], 4); |
| 785 | |
| 786 | /* apply whitening (8 bytes) to whole sector */ |
| 787 | for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++) |
| 788 | crypto_xor(data + i * 8, buf, 8); |
| 789 | out: |
Milan Broz | 1a71d6f | 2014-11-22 09:36:04 +0100 | [diff] [blame] | 790 | memzero_explicit(buf, sizeof(buf)); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 791 | return r; |
| 792 | } |
| 793 | |
| 794 | static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv, |
| 795 | struct dm_crypt_request *dmreq) |
| 796 | { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 797 | struct scatterlist *sg; |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 798 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
Bart Van Assche | 350b539 | 2016-06-28 16:32:32 +0200 | [diff] [blame] | 799 | __le64 sector = cpu_to_le64(dmreq->iv_sector); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 800 | u8 *src; |
| 801 | int r = 0; |
| 802 | |
| 803 | /* Remove whitening from ciphertext */ |
| 804 | if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 805 | sg = crypt_get_sg_data(cc, dmreq->sg_in); |
| 806 | src = kmap_atomic(sg_page(sg)); |
| 807 | r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 808 | kunmap_atomic(src); |
| 809 | } |
| 810 | |
| 811 | /* Calculate IV */ |
Ard Biesheuvel | 45fe93d | 2017-07-24 11:28:04 +0100 | [diff] [blame] | 812 | crypto_xor_cpy(iv, tcw->iv_seed, (u8 *)§or, 8); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 813 | if (cc->iv_size > 8) |
Ard Biesheuvel | 45fe93d | 2017-07-24 11:28:04 +0100 | [diff] [blame] | 814 | crypto_xor_cpy(&iv[8], tcw->iv_seed + 8, (u8 *)§or, |
| 815 | cc->iv_size - 8); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 816 | |
| 817 | return r; |
| 818 | } |
| 819 | |
| 820 | static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv, |
| 821 | struct dm_crypt_request *dmreq) |
| 822 | { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 823 | struct scatterlist *sg; |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 824 | u8 *dst; |
| 825 | int r; |
| 826 | |
| 827 | if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) |
| 828 | return 0; |
| 829 | |
| 830 | /* Apply whitening on ciphertext */ |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 831 | sg = crypt_get_sg_data(cc, dmreq->sg_out); |
| 832 | dst = kmap_atomic(sg_page(sg)); |
| 833 | r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 834 | kunmap_atomic(dst); |
| 835 | |
| 836 | return r; |
| 837 | } |
| 838 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 839 | static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv, |
| 840 | struct dm_crypt_request *dmreq) |
| 841 | { |
| 842 | /* Used only for writes, there must be an additional space to store IV */ |
| 843 | get_random_bytes(iv, cc->iv_size); |
| 844 | return 0; |
| 845 | } |
| 846 | |
Julia Lawall | 1b1b58f | 2015-11-29 14:09:19 +0100 | [diff] [blame] | 847 | static const struct crypt_iv_operations crypt_iv_plain_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | .generator = crypt_iv_plain_gen |
| 849 | }; |
| 850 | |
Julia Lawall | 1b1b58f | 2015-11-29 14:09:19 +0100 | [diff] [blame] | 851 | static const struct crypt_iv_operations crypt_iv_plain64_ops = { |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 852 | .generator = crypt_iv_plain64_gen |
| 853 | }; |
| 854 | |
Milan Broz | 7e3fd85 | 2017-06-06 09:07:01 +0200 | [diff] [blame] | 855 | static const struct crypt_iv_operations crypt_iv_plain64be_ops = { |
| 856 | .generator = crypt_iv_plain64be_gen |
| 857 | }; |
| 858 | |
Julia Lawall | 1b1b58f | 2015-11-29 14:09:19 +0100 | [diff] [blame] | 859 | static const struct crypt_iv_operations crypt_iv_essiv_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | .ctr = crypt_iv_essiv_ctr, |
| 861 | .dtr = crypt_iv_essiv_dtr, |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 862 | .init = crypt_iv_essiv_init, |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 863 | .wipe = crypt_iv_essiv_wipe, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | .generator = crypt_iv_essiv_gen |
| 865 | }; |
| 866 | |
Julia Lawall | 1b1b58f | 2015-11-29 14:09:19 +0100 | [diff] [blame] | 867 | static const struct crypt_iv_operations crypt_iv_benbi_ops = { |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 868 | .ctr = crypt_iv_benbi_ctr, |
| 869 | .dtr = crypt_iv_benbi_dtr, |
| 870 | .generator = crypt_iv_benbi_gen |
| 871 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | |
Julia Lawall | 1b1b58f | 2015-11-29 14:09:19 +0100 | [diff] [blame] | 873 | static const struct crypt_iv_operations crypt_iv_null_ops = { |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 874 | .generator = crypt_iv_null_gen |
| 875 | }; |
| 876 | |
Julia Lawall | 1b1b58f | 2015-11-29 14:09:19 +0100 | [diff] [blame] | 877 | static const struct crypt_iv_operations crypt_iv_lmk_ops = { |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 878 | .ctr = crypt_iv_lmk_ctr, |
| 879 | .dtr = crypt_iv_lmk_dtr, |
| 880 | .init = crypt_iv_lmk_init, |
| 881 | .wipe = crypt_iv_lmk_wipe, |
| 882 | .generator = crypt_iv_lmk_gen, |
| 883 | .post = crypt_iv_lmk_post |
| 884 | }; |
| 885 | |
Julia Lawall | 1b1b58f | 2015-11-29 14:09:19 +0100 | [diff] [blame] | 886 | static const struct crypt_iv_operations crypt_iv_tcw_ops = { |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 887 | .ctr = crypt_iv_tcw_ctr, |
| 888 | .dtr = crypt_iv_tcw_dtr, |
| 889 | .init = crypt_iv_tcw_init, |
| 890 | .wipe = crypt_iv_tcw_wipe, |
| 891 | .generator = crypt_iv_tcw_gen, |
| 892 | .post = crypt_iv_tcw_post |
| 893 | }; |
| 894 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 895 | static struct crypt_iv_operations crypt_iv_random_ops = { |
| 896 | .generator = crypt_iv_random_gen |
| 897 | }; |
| 898 | |
| 899 | /* |
| 900 | * Integrity extensions |
| 901 | */ |
| 902 | static bool crypt_integrity_aead(struct crypt_config *cc) |
| 903 | { |
| 904 | return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); |
| 905 | } |
| 906 | |
| 907 | static bool crypt_integrity_hmac(struct crypt_config *cc) |
| 908 | { |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 909 | return crypt_integrity_aead(cc) && cc->key_mac_size; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | /* Get sg containing data */ |
| 913 | static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc, |
| 914 | struct scatterlist *sg) |
| 915 | { |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 916 | if (unlikely(crypt_integrity_aead(cc))) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 917 | return &sg[2]; |
| 918 | |
| 919 | return sg; |
| 920 | } |
| 921 | |
| 922 | static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio) |
| 923 | { |
| 924 | struct bio_integrity_payload *bip; |
| 925 | unsigned int tag_len; |
| 926 | int ret; |
| 927 | |
| 928 | if (!bio_sectors(bio) || !io->cc->on_disk_tag_size) |
| 929 | return 0; |
| 930 | |
| 931 | bip = bio_integrity_alloc(bio, GFP_NOIO, 1); |
| 932 | if (IS_ERR(bip)) |
| 933 | return PTR_ERR(bip); |
| 934 | |
| 935 | tag_len = io->cc->on_disk_tag_size * bio_sectors(bio); |
| 936 | |
| 937 | bip->bip_iter.bi_size = tag_len; |
| 938 | bip->bip_iter.bi_sector = io->cc->start + io->sector; |
| 939 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 940 | ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata), |
| 941 | tag_len, offset_in_page(io->integrity_metadata)); |
| 942 | if (unlikely(ret != tag_len)) |
| 943 | return -ENOMEM; |
| 944 | |
| 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti) |
| 949 | { |
| 950 | #ifdef CONFIG_BLK_DEV_INTEGRITY |
| 951 | struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk); |
| 952 | |
| 953 | /* From now we require underlying device with our integrity profile */ |
| 954 | if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) { |
| 955 | ti->error = "Integrity profile not supported."; |
| 956 | return -EINVAL; |
| 957 | } |
| 958 | |
Mikulas Patocka | 583fe74 | 2017-04-18 16:51:54 -0400 | [diff] [blame] | 959 | if (bi->tag_size != cc->on_disk_tag_size || |
| 960 | bi->tuple_size != cc->on_disk_tag_size) { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 961 | ti->error = "Integrity profile tag size mismatch."; |
| 962 | return -EINVAL; |
| 963 | } |
Mikulas Patocka | 583fe74 | 2017-04-18 16:51:54 -0400 | [diff] [blame] | 964 | if (1 << bi->interval_exp != cc->sector_size) { |
| 965 | ti->error = "Integrity profile sector size mismatch."; |
| 966 | return -EINVAL; |
| 967 | } |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 968 | |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 969 | if (crypt_integrity_aead(cc)) { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 970 | cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size; |
| 971 | DMINFO("Integrity AEAD, tag size %u, IV size %u.", |
| 972 | cc->integrity_tag_size, cc->integrity_iv_size); |
| 973 | |
| 974 | if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) { |
| 975 | ti->error = "Integrity AEAD auth tag size is not supported."; |
| 976 | return -EINVAL; |
| 977 | } |
| 978 | } else if (cc->integrity_iv_size) |
| 979 | DMINFO("Additional per-sector space %u bytes for IV.", |
| 980 | cc->integrity_iv_size); |
| 981 | |
| 982 | if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) { |
| 983 | ti->error = "Not enough space for integrity tag in the profile."; |
| 984 | return -EINVAL; |
| 985 | } |
| 986 | |
| 987 | return 0; |
| 988 | #else |
| 989 | ti->error = "Integrity profile not supported."; |
| 990 | return -EINVAL; |
| 991 | #endif |
| 992 | } |
| 993 | |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 994 | static void crypt_convert_init(struct crypt_config *cc, |
| 995 | struct convert_context *ctx, |
| 996 | struct bio *bio_out, struct bio *bio_in, |
Milan Broz | fcd369d | 2008-02-08 02:10:41 +0000 | [diff] [blame] | 997 | sector_t sector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | { |
| 999 | ctx->bio_in = bio_in; |
| 1000 | ctx->bio_out = bio_out; |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 1001 | if (bio_in) |
| 1002 | ctx->iter_in = bio_in->bi_iter; |
| 1003 | if (bio_out) |
| 1004 | ctx->iter_out = bio_out->bi_iter; |
Mikulas Patocka | c66029f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1005 | ctx->cc_sector = sector + cc->iv_offset; |
Milan Broz | 43d6903 | 2008-02-08 02:11:09 +0000 | [diff] [blame] | 1006 | init_completion(&ctx->restart); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 1009 | static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc, |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1010 | void *req) |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 1011 | { |
| 1012 | return (struct dm_crypt_request *)((char *)req + cc->dmreq_start); |
| 1013 | } |
| 1014 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1015 | static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq) |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 1016 | { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1017 | return (void *)((char *)dmreq - cc->dmreq_start); |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1020 | static u8 *iv_of_dmreq(struct crypt_config *cc, |
| 1021 | struct dm_crypt_request *dmreq) |
| 1022 | { |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 1023 | if (crypt_integrity_aead(cc)) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1024 | return (u8 *)ALIGN((unsigned long)(dmreq + 1), |
| 1025 | crypto_aead_alignmask(any_tfm_aead(cc)) + 1); |
| 1026 | else |
| 1027 | return (u8 *)ALIGN((unsigned long)(dmreq + 1), |
| 1028 | crypto_skcipher_alignmask(any_tfm(cc)) + 1); |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1031 | static u8 *org_iv_of_dmreq(struct crypt_config *cc, |
| 1032 | struct dm_crypt_request *dmreq) |
| 1033 | { |
| 1034 | return iv_of_dmreq(cc, dmreq) + cc->iv_size; |
| 1035 | } |
| 1036 | |
| 1037 | static uint64_t *org_sector_of_dmreq(struct crypt_config *cc, |
| 1038 | struct dm_crypt_request *dmreq) |
| 1039 | { |
| 1040 | u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size; |
| 1041 | return (uint64_t*) ptr; |
| 1042 | } |
| 1043 | |
| 1044 | static unsigned int *org_tag_of_dmreq(struct crypt_config *cc, |
| 1045 | struct dm_crypt_request *dmreq) |
| 1046 | { |
| 1047 | u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + |
| 1048 | cc->iv_size + sizeof(uint64_t); |
| 1049 | return (unsigned int*)ptr; |
| 1050 | } |
| 1051 | |
| 1052 | static void *tag_from_dmreq(struct crypt_config *cc, |
| 1053 | struct dm_crypt_request *dmreq) |
| 1054 | { |
| 1055 | struct convert_context *ctx = dmreq->ctx; |
| 1056 | struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx); |
| 1057 | |
| 1058 | return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) * |
| 1059 | cc->on_disk_tag_size]; |
| 1060 | } |
| 1061 | |
| 1062 | static void *iv_tag_from_dmreq(struct crypt_config *cc, |
| 1063 | struct dm_crypt_request *dmreq) |
| 1064 | { |
| 1065 | return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size; |
| 1066 | } |
| 1067 | |
| 1068 | static int crypt_convert_block_aead(struct crypt_config *cc, |
| 1069 | struct convert_context *ctx, |
| 1070 | struct aead_request *req, |
| 1071 | unsigned int tag_offset) |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 1072 | { |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 1073 | struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in); |
| 1074 | 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] | 1075 | struct dm_crypt_request *dmreq; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1076 | u8 *iv, *org_iv, *tag_iv, *tag; |
| 1077 | uint64_t *sector; |
| 1078 | int r = 0; |
| 1079 | |
| 1080 | BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size); |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 1081 | |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1082 | /* Reject unexpected unaligned bio. */ |
Mikulas Patocka | 0440d5c | 2017-11-07 10:35:57 -0500 | [diff] [blame] | 1083 | if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1084 | return -EIO; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 1085 | |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 1086 | dmreq = dmreq_of_req(cc, req); |
Mikulas Patocka | c66029f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1087 | dmreq->iv_sector = ctx->cc_sector; |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1088 | if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) |
Mikulas Patocka | ff3af92 | 2017-03-23 10:23:14 -0400 | [diff] [blame] | 1089 | dmreq->iv_sector >>= cc->sector_shift; |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 1090 | dmreq->ctx = ctx; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 1091 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1092 | *org_tag_of_dmreq(cc, dmreq) = tag_offset; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 1093 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1094 | sector = org_sector_of_dmreq(cc, dmreq); |
| 1095 | *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); |
| 1096 | |
| 1097 | iv = iv_of_dmreq(cc, dmreq); |
| 1098 | org_iv = org_iv_of_dmreq(cc, dmreq); |
| 1099 | tag = tag_from_dmreq(cc, dmreq); |
| 1100 | tag_iv = iv_tag_from_dmreq(cc, dmreq); |
| 1101 | |
| 1102 | /* AEAD request: |
| 1103 | * |----- AAD -------|------ DATA -------|-- AUTH TAG --| |
| 1104 | * | (authenticated) | (auth+encryption) | | |
| 1105 | * | sector_LE | IV | sector in/out | tag in/out | |
| 1106 | */ |
| 1107 | sg_init_table(dmreq->sg_in, 4); |
| 1108 | sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t)); |
| 1109 | sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size); |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1110 | sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1111 | sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size); |
| 1112 | |
| 1113 | sg_init_table(dmreq->sg_out, 4); |
| 1114 | sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t)); |
| 1115 | sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size); |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1116 | sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1117 | sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size); |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 1118 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1119 | if (cc->iv_gen_ops) { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1120 | /* For READs use IV stored in integrity metadata */ |
| 1121 | if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { |
| 1122 | memcpy(org_iv, tag_iv, cc->iv_size); |
| 1123 | } else { |
| 1124 | r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); |
| 1125 | if (r < 0) |
| 1126 | return r; |
| 1127 | /* Store generated IV in integrity metadata */ |
| 1128 | if (cc->integrity_iv_size) |
| 1129 | memcpy(tag_iv, org_iv, cc->iv_size); |
| 1130 | } |
| 1131 | /* Working copy of IV, to be modified in crypto API */ |
| 1132 | memcpy(iv, org_iv, cc->iv_size); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1135 | aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size); |
| 1136 | if (bio_data_dir(ctx->bio_in) == WRITE) { |
| 1137 | aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out, |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1138 | cc->sector_size, iv); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1139 | r = crypto_aead_encrypt(req); |
| 1140 | if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size) |
| 1141 | memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0, |
| 1142 | cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size)); |
| 1143 | } else { |
| 1144 | aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out, |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1145 | cc->sector_size + cc->integrity_tag_size, iv); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1146 | r = crypto_aead_decrypt(req); |
| 1147 | } |
| 1148 | |
| 1149 | if (r == -EBADMSG) |
| 1150 | DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu", |
| 1151 | (unsigned long long)le64_to_cpu(*sector)); |
| 1152 | |
| 1153 | if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) |
| 1154 | r = cc->iv_gen_ops->post(cc, org_iv, dmreq); |
| 1155 | |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1156 | bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); |
| 1157 | bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1158 | |
| 1159 | return r; |
| 1160 | } |
| 1161 | |
| 1162 | static int crypt_convert_block_skcipher(struct crypt_config *cc, |
| 1163 | struct convert_context *ctx, |
| 1164 | struct skcipher_request *req, |
| 1165 | unsigned int tag_offset) |
| 1166 | { |
| 1167 | struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in); |
| 1168 | struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out); |
| 1169 | struct scatterlist *sg_in, *sg_out; |
| 1170 | struct dm_crypt_request *dmreq; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1171 | u8 *iv, *org_iv, *tag_iv; |
| 1172 | uint64_t *sector; |
| 1173 | int r = 0; |
| 1174 | |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1175 | /* Reject unexpected unaligned bio. */ |
Mikulas Patocka | 0440d5c | 2017-11-07 10:35:57 -0500 | [diff] [blame] | 1176 | if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1177 | return -EIO; |
| 1178 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1179 | dmreq = dmreq_of_req(cc, req); |
| 1180 | dmreq->iv_sector = ctx->cc_sector; |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1181 | if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) |
Mikulas Patocka | ff3af92 | 2017-03-23 10:23:14 -0400 | [diff] [blame] | 1182 | dmreq->iv_sector >>= cc->sector_shift; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1183 | dmreq->ctx = ctx; |
| 1184 | |
| 1185 | *org_tag_of_dmreq(cc, dmreq) = tag_offset; |
| 1186 | |
| 1187 | iv = iv_of_dmreq(cc, dmreq); |
| 1188 | org_iv = org_iv_of_dmreq(cc, dmreq); |
| 1189 | tag_iv = iv_tag_from_dmreq(cc, dmreq); |
| 1190 | |
| 1191 | sector = org_sector_of_dmreq(cc, dmreq); |
| 1192 | *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); |
| 1193 | |
| 1194 | /* For skcipher we use only the first sg item */ |
| 1195 | sg_in = &dmreq->sg_in[0]; |
| 1196 | sg_out = &dmreq->sg_out[0]; |
| 1197 | |
| 1198 | sg_init_table(sg_in, 1); |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1199 | sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1200 | |
| 1201 | sg_init_table(sg_out, 1); |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1202 | sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1203 | |
| 1204 | if (cc->iv_gen_ops) { |
| 1205 | /* For READs use IV stored in integrity metadata */ |
| 1206 | if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { |
| 1207 | memcpy(org_iv, tag_iv, cc->integrity_iv_size); |
| 1208 | } else { |
| 1209 | r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); |
| 1210 | if (r < 0) |
| 1211 | return r; |
| 1212 | /* Store generated IV in integrity metadata */ |
| 1213 | if (cc->integrity_iv_size) |
| 1214 | memcpy(tag_iv, org_iv, cc->integrity_iv_size); |
| 1215 | } |
| 1216 | /* Working copy of IV, to be modified in crypto API */ |
| 1217 | memcpy(iv, org_iv, cc->iv_size); |
| 1218 | } |
| 1219 | |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1220 | skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1221 | |
| 1222 | if (bio_data_dir(ctx->bio_in) == WRITE) |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 1223 | r = crypto_skcipher_encrypt(req); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1224 | else |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 1225 | r = crypto_skcipher_decrypt(req); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1226 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1227 | if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1228 | r = cc->iv_gen_ops->post(cc, org_iv, dmreq); |
| 1229 | |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1230 | bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); |
| 1231 | bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1232 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1233 | return r; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1236 | static void kcryptd_async_done(struct crypto_async_request *async_req, |
| 1237 | int error); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1238 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1239 | static void crypt_alloc_req_skcipher(struct crypt_config *cc, |
| 1240 | struct convert_context *ctx) |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1241 | { |
Mikulas Patocka | c66029f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1242 | unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1243 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1244 | if (!ctx->r.req) |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 1245 | ctx->r.req = mempool_alloc(&cc->req_pool, GFP_NOIO); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1246 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1247 | skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]); |
Milan Broz | 54cea3f | 2015-05-15 17:00:25 +0200 | [diff] [blame] | 1248 | |
| 1249 | /* |
| 1250 | * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs |
| 1251 | * requests if driver request queue is full. |
| 1252 | */ |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1253 | skcipher_request_set_callback(ctx->r.req, |
Mikulas Patocka | 432061b | 2018-09-05 09:17:45 -0400 | [diff] [blame] | 1254 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1255 | kcryptd_async_done, dmreq_of_req(cc, ctx->r.req)); |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1258 | static void crypt_alloc_req_aead(struct crypt_config *cc, |
| 1259 | struct convert_context *ctx) |
| 1260 | { |
| 1261 | if (!ctx->r.req_aead) |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 1262 | ctx->r.req_aead = mempool_alloc(&cc->req_pool, GFP_NOIO); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1263 | |
| 1264 | aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]); |
| 1265 | |
| 1266 | /* |
| 1267 | * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs |
| 1268 | * requests if driver request queue is full. |
| 1269 | */ |
| 1270 | aead_request_set_callback(ctx->r.req_aead, |
Mikulas Patocka | 432061b | 2018-09-05 09:17:45 -0400 | [diff] [blame] | 1271 | CRYPTO_TFM_REQ_MAY_BACKLOG, |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1272 | kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead)); |
| 1273 | } |
| 1274 | |
| 1275 | static void crypt_alloc_req(struct crypt_config *cc, |
| 1276 | struct convert_context *ctx) |
| 1277 | { |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 1278 | if (crypt_integrity_aead(cc)) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1279 | crypt_alloc_req_aead(cc, ctx); |
| 1280 | else |
| 1281 | crypt_alloc_req_skcipher(cc, ctx); |
| 1282 | } |
| 1283 | |
| 1284 | static void crypt_free_req_skcipher(struct crypt_config *cc, |
| 1285 | struct skcipher_request *req, struct bio *base_bio) |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 1286 | { |
| 1287 | struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); |
| 1288 | |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 1289 | if ((struct skcipher_request *)(io + 1) != req) |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 1290 | mempool_free(req, &cc->req_pool); |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 1291 | } |
| 1292 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1293 | static void crypt_free_req_aead(struct crypt_config *cc, |
| 1294 | struct aead_request *req, struct bio *base_bio) |
| 1295 | { |
| 1296 | struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); |
| 1297 | |
| 1298 | if ((struct aead_request *)(io + 1) != req) |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 1299 | mempool_free(req, &cc->req_pool); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio) |
| 1303 | { |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 1304 | if (crypt_integrity_aead(cc)) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1305 | crypt_free_req_aead(cc, req, base_bio); |
| 1306 | else |
| 1307 | crypt_free_req_skcipher(cc, req, base_bio); |
| 1308 | } |
| 1309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | /* |
| 1311 | * Encrypt / decrypt data from one bio to another one (can be the same one) |
| 1312 | */ |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1313 | static blk_status_t crypt_convert(struct crypt_config *cc, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 1314 | struct convert_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1316 | unsigned int tag_offset = 0; |
Mikulas Patocka | ff3af92 | 2017-03-23 10:23:14 -0400 | [diff] [blame] | 1317 | unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT; |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 1318 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1320 | atomic_set(&ctx->cc_pending, 1); |
Milan Broz | c808161 | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1321 | |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 1322 | while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1324 | crypt_alloc_req(cc, ctx); |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1325 | atomic_inc(&ctx->cc_pending); |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 1326 | |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 1327 | if (crypt_integrity_aead(cc)) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1328 | r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset); |
| 1329 | else |
| 1330 | r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1331 | |
| 1332 | switch (r) { |
Milan Broz | 54cea3f | 2015-05-15 17:00:25 +0200 | [diff] [blame] | 1333 | /* |
| 1334 | * The request was queued by a crypto driver |
| 1335 | * but the driver request queue is full, let's wait. |
| 1336 | */ |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1337 | case -EBUSY: |
| 1338 | wait_for_completion(&ctx->restart); |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 1339 | reinit_completion(&ctx->restart); |
Milan Broz | 54cea3f | 2015-05-15 17:00:25 +0200 | [diff] [blame] | 1340 | /* fall through */ |
| 1341 | /* |
| 1342 | * The request is queued and processed asynchronously, |
| 1343 | * completion function kcryptd_async_done() will be called. |
| 1344 | */ |
Rabin Vincent | c0403ec | 2015-05-05 15:15:56 +0200 | [diff] [blame] | 1345 | case -EINPROGRESS: |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1346 | ctx->r.req = NULL; |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1347 | ctx->cc_sector += sector_step; |
Mikulas Patocka | 583fe74 | 2017-04-18 16:51:54 -0400 | [diff] [blame] | 1348 | tag_offset++; |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1349 | continue; |
Milan Broz | 54cea3f | 2015-05-15 17:00:25 +0200 | [diff] [blame] | 1350 | /* |
| 1351 | * The request was already processed (synchronously). |
| 1352 | */ |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 1353 | case 0: |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1354 | atomic_dec(&ctx->cc_pending); |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 1355 | ctx->cc_sector += sector_step; |
Mikulas Patocka | 583fe74 | 2017-04-18 16:51:54 -0400 | [diff] [blame] | 1356 | tag_offset++; |
Milan Broz | c7f1b20 | 2008-07-02 09:34:28 +0100 | [diff] [blame] | 1357 | cond_resched(); |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 1358 | continue; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1359 | /* |
| 1360 | * There was a data integrity error. |
| 1361 | */ |
| 1362 | case -EBADMSG: |
| 1363 | atomic_dec(&ctx->cc_pending); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1364 | return BLK_STS_PROTECTION; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1365 | /* |
| 1366 | * There was an error while processing the request. |
| 1367 | */ |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 1368 | default: |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1369 | atomic_dec(&ctx->cc_pending); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1370 | return BLK_STS_IOERR; |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 1371 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 1374 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | } |
| 1376 | |
Mikulas Patocka | cf2f1ab | 2015-02-13 08:23:52 -0500 | [diff] [blame] | 1377 | static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone); |
| 1378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | /* |
| 1380 | * Generate a new unfragmented bio with the given size |
Mike Snitzer | 586b286 | 2015-09-09 21:34:51 -0400 | [diff] [blame] | 1381 | * This should never violate the device limitations (but only because |
| 1382 | * max_segment_size is being constrained to PAGE_SIZE). |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1383 | * |
| 1384 | * This function may be called concurrently. If we allocate from the mempool |
| 1385 | * concurrently, there is a possibility of deadlock. For example, if we have |
| 1386 | * mempool of 256 pages, two processes, each wanting 256, pages allocate from |
| 1387 | * the mempool concurrently, it may deadlock in a situation where both processes |
| 1388 | * have allocated 128 pages and the mempool is exhausted. |
| 1389 | * |
| 1390 | * In order to avoid this scenario we allocate the pages under a mutex. |
| 1391 | * |
| 1392 | * In order to not degrade performance with excessive locking, we try |
| 1393 | * non-blocking allocations without a mutex first but on failure we fallback |
| 1394 | * to blocking allocations with a mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | */ |
Mikulas Patocka | cf2f1ab | 2015-02-13 08:23:52 -0500 | [diff] [blame] | 1396 | static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1398 | struct crypt_config *cc = io->cc; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1399 | struct bio *clone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1401 | gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM; |
| 1402 | unsigned i, len, remaining_size; |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 1403 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1405 | retry: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 1406 | if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM)) |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1407 | mutex_lock(&cc->bio_alloc_lock); |
| 1408 | |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 1409 | clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, &cc->bs); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1410 | if (!clone) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1411 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
Olaf Kirch | 027581f | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 1413 | clone_init(io, clone); |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 1414 | |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1415 | remaining_size = size; |
| 1416 | |
Olaf Kirch | f97380b | 2007-05-09 02:32:54 -0700 | [diff] [blame] | 1417 | for (i = 0; i < nr_iovecs; i++) { |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 1418 | page = mempool_alloc(&cc->page_pool, gfp_mask); |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1419 | if (!page) { |
| 1420 | crypt_free_buffer_pages(cc, clone); |
| 1421 | bio_put(clone); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 1422 | gfp_mask |= __GFP_DIRECT_RECLAIM; |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1423 | goto retry; |
| 1424 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1426 | len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | |
Ming Lei | 0dae7fe | 2016-10-29 16:08:06 +0800 | [diff] [blame] | 1428 | bio_add_page(clone, page, len, 0); |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 1429 | |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1430 | remaining_size -= len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1433 | /* Allocate space for integrity tags */ |
| 1434 | if (dm_crypt_integrity_io_alloc(io, clone)) { |
| 1435 | crypt_free_buffer_pages(cc, clone); |
| 1436 | bio_put(clone); |
| 1437 | clone = NULL; |
| 1438 | } |
| 1439 | out: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 1440 | if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM)) |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1441 | mutex_unlock(&cc->bio_alloc_lock); |
| 1442 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1443 | return clone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 1446 | 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] | 1447 | { |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 1448 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | struct bio_vec *bv; |
| 1450 | |
Kent Overstreet | cb34e05 | 2012-09-05 15:22:02 -0700 | [diff] [blame] | 1451 | bio_for_each_segment_all(bv, clone, i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | BUG_ON(!bv->bv_page); |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 1453 | mempool_free(bv->bv_page, &cc->page_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | } |
| 1455 | } |
| 1456 | |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 1457 | static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc, |
| 1458 | struct bio *bio, sector_t sector) |
Milan Broz | dc440d1e | 2008-10-10 13:37:03 +0100 | [diff] [blame] | 1459 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1460 | io->cc = cc; |
Milan Broz | dc440d1e | 2008-10-10 13:37:03 +0100 | [diff] [blame] | 1461 | io->base_bio = bio; |
| 1462 | io->sector = sector; |
| 1463 | io->error = 0; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1464 | io->ctx.r.req = NULL; |
| 1465 | io->integrity_metadata = NULL; |
| 1466 | io->integrity_metadata_from_pool = false; |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1467 | atomic_set(&io->io_pending, 0); |
Milan Broz | dc440d1e | 2008-10-10 13:37:03 +0100 | [diff] [blame] | 1468 | } |
| 1469 | |
Milan Broz | 3e1a8bd | 2008-10-10 13:37:02 +0100 | [diff] [blame] | 1470 | static void crypt_inc_pending(struct dm_crypt_io *io) |
| 1471 | { |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1472 | atomic_inc(&io->io_pending); |
Milan Broz | 3e1a8bd | 2008-10-10 13:37:02 +0100 | [diff] [blame] | 1473 | } |
| 1474 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | /* |
| 1476 | * One of the bios was finished. Check for completion of |
| 1477 | * the whole request and correctly clean up the buffer. |
| 1478 | */ |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1479 | static void crypt_dec_pending(struct dm_crypt_io *io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1481 | struct crypt_config *cc = io->cc; |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 1482 | struct bio *base_bio = io->base_bio; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1483 | blk_status_t error = io->error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1485 | if (!atomic_dec_and_test(&io->io_pending)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1486 | return; |
| 1487 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1488 | if (io->ctx.r.req) |
| 1489 | crypt_free_req(cc, io->ctx.r.req, base_bio); |
| 1490 | |
| 1491 | if (unlikely(io->integrity_metadata_from_pool)) |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 1492 | mempool_free(io->integrity_metadata, &io->cc->tag_pool); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1493 | else |
| 1494 | kfree(io->integrity_metadata); |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 1495 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1496 | base_bio->bi_status = error; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1497 | bio_endio(base_bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | /* |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1501 | * kcryptd/kcryptd_io: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | * |
| 1503 | * Needed because it would be very unwise to do decryption in an |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 1504 | * interrupt context. |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1505 | * |
| 1506 | * kcryptd performs the actual encryption or decryption. |
| 1507 | * |
| 1508 | * kcryptd_io performs the IO submission. |
| 1509 | * |
| 1510 | * They must be separated as otherwise the final stages could be |
| 1511 | * starved by new requests which can block in the first stages due |
| 1512 | * to memory allocation. |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1513 | * |
| 1514 | * The work is done per CPU global for all dm-crypt instances. |
| 1515 | * They should not depend on each other and do not block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1517 | static void crypt_endio(struct bio *clone) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1518 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1519 | struct dm_crypt_io *io = clone->bi_private; |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1520 | struct crypt_config *cc = io->cc; |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 1521 | unsigned rw = bio_data_dir(clone); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1522 | blk_status_t error; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1523 | |
| 1524 | /* |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1525 | * free the processed pages |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1526 | */ |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 1527 | if (rw == WRITE) |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 1528 | crypt_free_buffer_pages(cc, clone); |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 1529 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1530 | error = clone->bi_status; |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 1531 | bio_put(clone); |
| 1532 | |
Sasha Levin | 9b81c84 | 2015-08-10 19:05:18 -0400 | [diff] [blame] | 1533 | if (rw == READ && !error) { |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 1534 | kcryptd_queue_crypt(io); |
| 1535 | return; |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1536 | } |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1537 | |
Sasha Levin | 9b81c84 | 2015-08-10 19:05:18 -0400 | [diff] [blame] | 1538 | if (unlikely(error)) |
| 1539 | io->error = error; |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1540 | |
| 1541 | crypt_dec_pending(io); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1542 | } |
| 1543 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1544 | static void clone_init(struct dm_crypt_io *io, struct bio *clone) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1545 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1546 | struct crypt_config *cc = io->cc; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1547 | |
| 1548 | clone->bi_private = io; |
| 1549 | clone->bi_end_io = crypt_endio; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1550 | bio_set_dev(clone, cc->dev->bdev); |
Christoph Hellwig | ef295ec | 2016-10-28 08:48:16 -0600 | [diff] [blame] | 1551 | clone->bi_opf = io->base_bio->bi_opf; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1554 | 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] | 1555 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1556 | struct crypt_config *cc = io->cc; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1557 | struct bio *clone; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1558 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1559 | /* |
Mike Snitzer | 5977907 | 2015-04-09 16:53:24 -0400 | [diff] [blame] | 1560 | * We need the original biovec array in order to decrypt |
| 1561 | * the whole bio data *afterwards* -- thanks to immutable |
| 1562 | * biovecs we don't need to worry about the block layer |
| 1563 | * modifying the biovec array; so leverage bio_clone_fast(). |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1564 | */ |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 1565 | clone = bio_clone_fast(io->base_bio, gfp, &cc->bs); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 1566 | if (!clone) |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1567 | return 1; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1568 | |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1569 | crypt_inc_pending(io); |
| 1570 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1571 | clone_init(io, clone); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1572 | clone->bi_iter.bi_sector = cc->start + io->sector; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1573 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1574 | if (dm_crypt_integrity_io_alloc(io, clone)) { |
| 1575 | crypt_dec_pending(io); |
| 1576 | bio_put(clone); |
| 1577 | return 1; |
| 1578 | } |
| 1579 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1580 | generic_make_request(clone); |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1581 | return 0; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1582 | } |
| 1583 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1584 | static void kcryptd_io_read_work(struct work_struct *work) |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1585 | { |
| 1586 | struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); |
| 1587 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1588 | crypt_inc_pending(io); |
| 1589 | if (kcryptd_io_read(io, GFP_NOIO)) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1590 | io->error = BLK_STS_RESOURCE; |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1591 | crypt_dec_pending(io); |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1594 | static void kcryptd_queue_read(struct dm_crypt_io *io) |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1595 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1596 | struct crypt_config *cc = io->cc; |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1597 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1598 | INIT_WORK(&io->work, kcryptd_io_read_work); |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1599 | queue_work(cc->io_queue, &io->work); |
| 1600 | } |
| 1601 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1602 | static void kcryptd_io_write(struct dm_crypt_io *io) |
| 1603 | { |
| 1604 | struct bio *clone = io->ctx.bio_out; |
| 1605 | |
| 1606 | generic_make_request(clone); |
| 1607 | } |
| 1608 | |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1609 | #define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node) |
| 1610 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1611 | static int dmcrypt_write(void *data) |
| 1612 | { |
| 1613 | struct crypt_config *cc = data; |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1614 | struct dm_crypt_io *io; |
| 1615 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1616 | while (1) { |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1617 | struct rb_root write_tree; |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1618 | struct blk_plug plug; |
| 1619 | |
Mikulas Patocka | c7329ef | 2018-07-11 12:10:51 -0400 | [diff] [blame] | 1620 | spin_lock_irq(&cc->write_thread_lock); |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1621 | continue_locked: |
| 1622 | |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1623 | if (!RB_EMPTY_ROOT(&cc->write_tree)) |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1624 | goto pop_from_list; |
| 1625 | |
Rabin Vincent | f659b10 | 2016-09-21 16:22:29 +0200 | [diff] [blame] | 1626 | set_current_state(TASK_INTERRUPTIBLE); |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1627 | |
Mikulas Patocka | c7329ef | 2018-07-11 12:10:51 -0400 | [diff] [blame] | 1628 | spin_unlock_irq(&cc->write_thread_lock); |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1629 | |
Rabin Vincent | f659b10 | 2016-09-21 16:22:29 +0200 | [diff] [blame] | 1630 | if (unlikely(kthread_should_stop())) { |
Davidlohr Bueso | 642fa44 | 2017-01-03 13:43:14 -0800 | [diff] [blame] | 1631 | set_current_state(TASK_RUNNING); |
Rabin Vincent | f659b10 | 2016-09-21 16:22:29 +0200 | [diff] [blame] | 1632 | break; |
| 1633 | } |
| 1634 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1635 | schedule(); |
| 1636 | |
Davidlohr Bueso | 642fa44 | 2017-01-03 13:43:14 -0800 | [diff] [blame] | 1637 | set_current_state(TASK_RUNNING); |
Mikulas Patocka | c7329ef | 2018-07-11 12:10:51 -0400 | [diff] [blame] | 1638 | spin_lock_irq(&cc->write_thread_lock); |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1639 | goto continue_locked; |
| 1640 | |
| 1641 | pop_from_list: |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1642 | write_tree = cc->write_tree; |
| 1643 | cc->write_tree = RB_ROOT; |
Mikulas Patocka | c7329ef | 2018-07-11 12:10:51 -0400 | [diff] [blame] | 1644 | spin_unlock_irq(&cc->write_thread_lock); |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1645 | |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1646 | BUG_ON(rb_parent(write_tree.rb_node)); |
| 1647 | |
| 1648 | /* |
| 1649 | * Note: we cannot walk the tree here with rb_next because |
| 1650 | * the structures may be freed when kcryptd_io_write is called. |
| 1651 | */ |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1652 | blk_start_plug(&plug); |
| 1653 | do { |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1654 | io = crypt_io_from_node(rb_first(&write_tree)); |
| 1655 | rb_erase(&io->rb_node, &write_tree); |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1656 | kcryptd_io_write(io); |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1657 | } while (!RB_EMPTY_ROOT(&write_tree)); |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1658 | blk_finish_plug(&plug); |
| 1659 | } |
| 1660 | return 0; |
| 1661 | } |
| 1662 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1663 | 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] | 1664 | { |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1665 | struct bio *clone = io->ctx.bio_out; |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1666 | struct crypt_config *cc = io->cc; |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1667 | unsigned long flags; |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1668 | sector_t sector; |
| 1669 | struct rb_node **rbp, *parent; |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1670 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1671 | if (unlikely(io->error)) { |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1672 | crypt_free_buffer_pages(cc, clone); |
| 1673 | bio_put(clone); |
Milan Broz | 6c031f4 | 2008-10-10 13:37:06 +0100 | [diff] [blame] | 1674 | crypt_dec_pending(io); |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1675 | return; |
| 1676 | } |
| 1677 | |
| 1678 | /* crypt_convert should have filled the clone bio */ |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 1679 | BUG_ON(io->ctx.iter_out.bi_size); |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1680 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1681 | clone->bi_iter.bi_sector = cc->start + io->sector; |
Milan Broz | 899c95d | 2008-02-08 02:11:02 +0000 | [diff] [blame] | 1682 | |
Mikulas Patocka | 0f5d8e6 | 2015-02-13 08:27:08 -0500 | [diff] [blame] | 1683 | if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) { |
| 1684 | generic_make_request(clone); |
| 1685 | return; |
| 1686 | } |
| 1687 | |
Mikulas Patocka | c7329ef | 2018-07-11 12:10:51 -0400 | [diff] [blame] | 1688 | spin_lock_irqsave(&cc->write_thread_lock, flags); |
| 1689 | if (RB_EMPTY_ROOT(&cc->write_tree)) |
| 1690 | wake_up_process(cc->write_thread); |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1691 | rbp = &cc->write_tree.rb_node; |
| 1692 | parent = NULL; |
| 1693 | sector = io->sector; |
| 1694 | while (*rbp) { |
| 1695 | parent = *rbp; |
| 1696 | if (sector < crypt_io_from_node(parent)->sector) |
| 1697 | rbp = &(*rbp)->rb_left; |
| 1698 | else |
| 1699 | rbp = &(*rbp)->rb_right; |
| 1700 | } |
| 1701 | rb_link_node(&io->rb_node, parent, rbp); |
| 1702 | rb_insert_color(&io->rb_node, &cc->write_tree); |
Mikulas Patocka | c7329ef | 2018-07-11 12:10:51 -0400 | [diff] [blame] | 1703 | spin_unlock_irqrestore(&cc->write_thread_lock, flags); |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1706 | static void kcryptd_crypt_write_convert(struct dm_crypt_io *io) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1707 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1708 | struct crypt_config *cc = io->cc; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1709 | struct bio *clone; |
Milan Broz | c808161 | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1710 | int crypt_finished; |
Milan Broz | b635b00 | 2008-10-21 17:45:00 +0100 | [diff] [blame] | 1711 | sector_t sector = io->sector; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1712 | blk_status_t r; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1713 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1714 | /* |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1715 | * Prevent io from disappearing until this function completes. |
| 1716 | */ |
| 1717 | crypt_inc_pending(io); |
Milan Broz | b635b00 | 2008-10-21 17:45:00 +0100 | [diff] [blame] | 1718 | crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector); |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1719 | |
Mikulas Patocka | cf2f1ab | 2015-02-13 08:23:52 -0500 | [diff] [blame] | 1720 | clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size); |
| 1721 | if (unlikely(!clone)) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1722 | io->error = BLK_STS_IOERR; |
Mikulas Patocka | cf2f1ab | 2015-02-13 08:23:52 -0500 | [diff] [blame] | 1723 | goto dec; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1724 | } |
Milan Broz | 899c95d | 2008-02-08 02:11:02 +0000 | [diff] [blame] | 1725 | |
Mikulas Patocka | cf2f1ab | 2015-02-13 08:23:52 -0500 | [diff] [blame] | 1726 | io->ctx.bio_out = clone; |
| 1727 | io->ctx.iter_out = clone->bi_iter; |
| 1728 | |
| 1729 | sector += bio_sectors(clone); |
| 1730 | |
| 1731 | crypt_inc_pending(io); |
| 1732 | r = crypt_convert(cc, &io->ctx); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1733 | if (r) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1734 | io->error = r; |
Mikulas Patocka | cf2f1ab | 2015-02-13 08:23:52 -0500 | [diff] [blame] | 1735 | crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending); |
| 1736 | |
| 1737 | /* Encryption was already finished, submit io now */ |
| 1738 | if (crypt_finished) { |
| 1739 | kcryptd_crypt_write_io_submit(io, 0); |
| 1740 | io->sector = sector; |
| 1741 | } |
| 1742 | |
| 1743 | dec: |
Milan Broz | 899c95d | 2008-02-08 02:11:02 +0000 | [diff] [blame] | 1744 | crypt_dec_pending(io); |
Milan Broz | 84131db | 2008-02-08 02:10:59 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1747 | static void kcryptd_crypt_read_done(struct dm_crypt_io *io) |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1748 | { |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1749 | crypt_dec_pending(io); |
| 1750 | } |
| 1751 | |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1752 | static void kcryptd_crypt_read_convert(struct dm_crypt_io *io) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1753 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1754 | struct crypt_config *cc = io->cc; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1755 | blk_status_t r; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1756 | |
Milan Broz | 3e1a8bd | 2008-10-10 13:37:02 +0100 | [diff] [blame] | 1757 | crypt_inc_pending(io); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1758 | |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 1759 | crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio, |
Milan Broz | 0c395b0 | 2008-02-08 02:10:54 +0000 | [diff] [blame] | 1760 | io->sector); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1761 | |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1762 | r = crypt_convert(cc, &io->ctx); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1763 | if (r) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1764 | io->error = r; |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1765 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1766 | if (atomic_dec_and_test(&io->ctx.cc_pending)) |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1767 | kcryptd_crypt_read_done(io); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1768 | |
| 1769 | crypt_dec_pending(io); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1770 | } |
| 1771 | |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1772 | static void kcryptd_async_done(struct crypto_async_request *async_req, |
| 1773 | int error) |
| 1774 | { |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 1775 | struct dm_crypt_request *dmreq = async_req->data; |
| 1776 | struct convert_context *ctx = dmreq->ctx; |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1777 | 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] | 1778 | struct crypt_config *cc = io->cc; |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1779 | |
Milan Broz | 54cea3f | 2015-05-15 17:00:25 +0200 | [diff] [blame] | 1780 | /* |
| 1781 | * A request from crypto driver backlog is going to be processed now, |
| 1782 | * finish the completion and continue in crypt_convert(). |
| 1783 | * (Callback will be called for the second time for this request.) |
| 1784 | */ |
Rabin Vincent | c0403ec | 2015-05-05 15:15:56 +0200 | [diff] [blame] | 1785 | if (error == -EINPROGRESS) { |
| 1786 | complete(&ctx->restart); |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1787 | return; |
Rabin Vincent | c0403ec | 2015-05-05 15:15:56 +0200 | [diff] [blame] | 1788 | } |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1789 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1790 | if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1791 | error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq); |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1792 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1793 | if (error == -EBADMSG) { |
| 1794 | DMERR_LIMIT("INTEGRITY AEAD ERROR, sector %llu", |
| 1795 | (unsigned long long)le64_to_cpu(*org_sector_of_dmreq(cc, dmreq))); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1796 | io->error = BLK_STS_PROTECTION; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1797 | } else if (error < 0) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1798 | io->error = BLK_STS_IOERR; |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1799 | |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 1800 | crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio); |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1801 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1802 | if (!atomic_dec_and_test(&ctx->cc_pending)) |
Rabin Vincent | c0403ec | 2015-05-05 15:15:56 +0200 | [diff] [blame] | 1803 | return; |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1804 | |
| 1805 | if (bio_data_dir(io->base_bio) == READ) |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1806 | kcryptd_crypt_read_done(io); |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1807 | else |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1808 | kcryptd_crypt_write_io_submit(io, 1); |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1811 | static void kcryptd_crypt(struct work_struct *work) |
| 1812 | { |
| 1813 | struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); |
| 1814 | |
| 1815 | if (bio_data_dir(io->base_bio) == READ) |
| 1816 | kcryptd_crypt_read_convert(io); |
| 1817 | else |
| 1818 | kcryptd_crypt_write_convert(io); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1819 | } |
| 1820 | |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1821 | static void kcryptd_queue_crypt(struct dm_crypt_io *io) |
| 1822 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1823 | struct crypt_config *cc = io->cc; |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1824 | |
| 1825 | INIT_WORK(&io->work, kcryptd_crypt); |
| 1826 | queue_work(cc->crypt_queue, &io->work); |
| 1827 | } |
| 1828 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1829 | static void crypt_free_tfms_aead(struct crypt_config *cc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1831 | if (!cc->cipher_tfm.tfms_aead) |
| 1832 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1834 | if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) { |
| 1835 | crypto_free_aead(cc->cipher_tfm.tfms_aead[0]); |
| 1836 | cc->cipher_tfm.tfms_aead[0] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1839 | kfree(cc->cipher_tfm.tfms_aead); |
| 1840 | cc->cipher_tfm.tfms_aead = NULL; |
| 1841 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1843 | static void crypt_free_tfms_skcipher(struct crypt_config *cc) |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1844 | { |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1845 | unsigned i; |
| 1846 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1847 | if (!cc->cipher_tfm.tfms) |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1848 | return; |
| 1849 | |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1850 | for (i = 0; i < cc->tfms_count; i++) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1851 | if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) { |
| 1852 | crypto_free_skcipher(cc->cipher_tfm.tfms[i]); |
| 1853 | cc->cipher_tfm.tfms[i] = NULL; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1854 | } |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1855 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1856 | kfree(cc->cipher_tfm.tfms); |
| 1857 | cc->cipher_tfm.tfms = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | static void crypt_free_tfms(struct crypt_config *cc) |
| 1861 | { |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 1862 | if (crypt_integrity_aead(cc)) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1863 | crypt_free_tfms_aead(cc); |
| 1864 | else |
| 1865 | crypt_free_tfms_skcipher(cc); |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1868 | static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode) |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1869 | { |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1870 | unsigned i; |
| 1871 | int err; |
| 1872 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 1873 | cc->cipher_tfm.tfms = kcalloc(cc->tfms_count, |
| 1874 | sizeof(struct crypto_skcipher *), |
| 1875 | GFP_KERNEL); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1876 | if (!cc->cipher_tfm.tfms) |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1877 | return -ENOMEM; |
| 1878 | |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1879 | for (i = 0; i < cc->tfms_count; i++) { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1880 | cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0); |
| 1881 | if (IS_ERR(cc->cipher_tfm.tfms[i])) { |
| 1882 | err = PTR_ERR(cc->cipher_tfm.tfms[i]); |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1883 | crypt_free_tfms(cc); |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1884 | return err; |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | return 0; |
| 1889 | } |
| 1890 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1891 | static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode) |
| 1892 | { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1893 | int err; |
| 1894 | |
| 1895 | cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL); |
| 1896 | if (!cc->cipher_tfm.tfms) |
| 1897 | return -ENOMEM; |
| 1898 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1899 | cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 0); |
| 1900 | if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) { |
| 1901 | err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]); |
| 1902 | crypt_free_tfms(cc); |
| 1903 | return err; |
| 1904 | } |
| 1905 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1906 | return 0; |
| 1907 | } |
| 1908 | |
| 1909 | static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode) |
| 1910 | { |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 1911 | if (crypt_integrity_aead(cc)) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1912 | return crypt_alloc_tfms_aead(cc, ciphermode); |
| 1913 | else |
| 1914 | return crypt_alloc_tfms_skcipher(cc, ciphermode); |
| 1915 | } |
| 1916 | |
| 1917 | static unsigned crypt_subkey_size(struct crypt_config *cc) |
| 1918 | { |
| 1919 | return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count); |
| 1920 | } |
| 1921 | |
| 1922 | static unsigned crypt_authenckey_size(struct crypt_config *cc) |
| 1923 | { |
| 1924 | return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param)); |
| 1925 | } |
| 1926 | |
| 1927 | /* |
| 1928 | * If AEAD is composed like authenc(hmac(sha256),xts(aes)), |
| 1929 | * the key must be for some reason in special format. |
| 1930 | * This funcion converts cc->key to this special format. |
| 1931 | */ |
| 1932 | static void crypt_copy_authenckey(char *p, const void *key, |
| 1933 | unsigned enckeylen, unsigned authkeylen) |
| 1934 | { |
| 1935 | struct crypto_authenc_key_param *param; |
| 1936 | struct rtattr *rta; |
| 1937 | |
| 1938 | rta = (struct rtattr *)p; |
| 1939 | param = RTA_DATA(rta); |
| 1940 | param->enckeylen = cpu_to_be32(enckeylen); |
| 1941 | rta->rta_len = RTA_LENGTH(sizeof(*param)); |
| 1942 | rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM; |
| 1943 | p += RTA_SPACE(sizeof(*param)); |
| 1944 | memcpy(p, key + enckeylen, authkeylen); |
| 1945 | p += authkeylen; |
| 1946 | memcpy(p, key, enckeylen); |
| 1947 | } |
| 1948 | |
Mikulas Patocka | 671ea6b | 2016-08-25 07:12:54 -0400 | [diff] [blame] | 1949 | static int crypt_setkey(struct crypt_config *cc) |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1950 | { |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1951 | unsigned subkey_size; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1952 | int err = 0, i, r; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1953 | |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1954 | /* Ignore extra keys (which are used for IV etc) */ |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1955 | subkey_size = crypt_subkey_size(cc); |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1956 | |
Milan Broz | 27c7003 | 2018-01-03 22:48:59 +0100 | [diff] [blame] | 1957 | if (crypt_integrity_hmac(cc)) { |
| 1958 | if (subkey_size < cc->key_mac_size) |
| 1959 | return -EINVAL; |
| 1960 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1961 | crypt_copy_authenckey(cc->authenc_key, cc->key, |
| 1962 | subkey_size - cc->key_mac_size, |
| 1963 | cc->key_mac_size); |
Milan Broz | 27c7003 | 2018-01-03 22:48:59 +0100 | [diff] [blame] | 1964 | } |
| 1965 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1966 | for (i = 0; i < cc->tfms_count; i++) { |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 1967 | if (crypt_integrity_hmac(cc)) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1968 | r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], |
| 1969 | cc->authenc_key, crypt_authenckey_size(cc)); |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 1970 | else if (crypt_integrity_aead(cc)) |
| 1971 | r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], |
| 1972 | cc->key + (i * subkey_size), |
| 1973 | subkey_size); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1974 | else |
| 1975 | r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i], |
| 1976 | cc->key + (i * subkey_size), |
| 1977 | subkey_size); |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1978 | if (r) |
| 1979 | err = r; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1980 | } |
| 1981 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 1982 | if (crypt_integrity_hmac(cc)) |
| 1983 | memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc)); |
| 1984 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1985 | return err; |
| 1986 | } |
| 1987 | |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 1988 | #ifdef CONFIG_KEYS |
| 1989 | |
Ondrej Kozina | 027c431 | 2016-12-01 18:20:52 +0100 | [diff] [blame] | 1990 | static bool contains_whitespace(const char *str) |
| 1991 | { |
| 1992 | while (*str) |
| 1993 | if (isspace(*str++)) |
| 1994 | return true; |
| 1995 | return false; |
| 1996 | } |
| 1997 | |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 1998 | static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) |
| 1999 | { |
| 2000 | char *new_key_string, *key_desc; |
| 2001 | int ret; |
| 2002 | struct key *key; |
| 2003 | const struct user_key_payload *ukp; |
| 2004 | |
Ondrej Kozina | 027c431 | 2016-12-01 18:20:52 +0100 | [diff] [blame] | 2005 | /* |
| 2006 | * Reject key_string with whitespace. dm core currently lacks code for |
| 2007 | * proper whitespace escaping in arguments on DM_TABLE_STATUS path. |
| 2008 | */ |
| 2009 | if (contains_whitespace(key_string)) { |
| 2010 | DMERR("whitespace chars not allowed in key string"); |
| 2011 | return -EINVAL; |
| 2012 | } |
| 2013 | |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2014 | /* look for next ':' separating key_type from key_description */ |
| 2015 | key_desc = strpbrk(key_string, ":"); |
| 2016 | if (!key_desc || key_desc == key_string || !strlen(key_desc + 1)) |
| 2017 | return -EINVAL; |
| 2018 | |
| 2019 | if (strncmp(key_string, "logon:", key_desc - key_string + 1) && |
| 2020 | strncmp(key_string, "user:", key_desc - key_string + 1)) |
| 2021 | return -EINVAL; |
| 2022 | |
| 2023 | new_key_string = kstrdup(key_string, GFP_KERNEL); |
| 2024 | if (!new_key_string) |
| 2025 | return -ENOMEM; |
| 2026 | |
| 2027 | key = request_key(key_string[0] == 'l' ? &key_type_logon : &key_type_user, |
| 2028 | key_desc + 1, NULL); |
| 2029 | if (IS_ERR(key)) { |
| 2030 | kzfree(new_key_string); |
| 2031 | return PTR_ERR(key); |
| 2032 | } |
| 2033 | |
Ondrej Kozina | f5b0cba | 2017-01-31 15:47:11 +0100 | [diff] [blame] | 2034 | down_read(&key->sem); |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2035 | |
David Howells | 0837e49 | 2017-03-01 15:11:23 +0000 | [diff] [blame] | 2036 | ukp = user_key_payload_locked(key); |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2037 | if (!ukp) { |
Ondrej Kozina | f5b0cba | 2017-01-31 15:47:11 +0100 | [diff] [blame] | 2038 | up_read(&key->sem); |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2039 | key_put(key); |
| 2040 | kzfree(new_key_string); |
| 2041 | return -EKEYREVOKED; |
| 2042 | } |
| 2043 | |
| 2044 | if (cc->key_size != ukp->datalen) { |
Ondrej Kozina | f5b0cba | 2017-01-31 15:47:11 +0100 | [diff] [blame] | 2045 | up_read(&key->sem); |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2046 | key_put(key); |
| 2047 | kzfree(new_key_string); |
| 2048 | return -EINVAL; |
| 2049 | } |
| 2050 | |
| 2051 | memcpy(cc->key, ukp->data, cc->key_size); |
| 2052 | |
Ondrej Kozina | f5b0cba | 2017-01-31 15:47:11 +0100 | [diff] [blame] | 2053 | up_read(&key->sem); |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2054 | key_put(key); |
| 2055 | |
| 2056 | /* clear the flag since following operations may invalidate previously valid key */ |
| 2057 | clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
| 2058 | |
| 2059 | ret = crypt_setkey(cc); |
| 2060 | |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2061 | if (!ret) { |
| 2062 | set_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
| 2063 | kzfree(cc->key_string); |
| 2064 | cc->key_string = new_key_string; |
| 2065 | } else |
| 2066 | kzfree(new_key_string); |
| 2067 | |
| 2068 | return ret; |
| 2069 | } |
| 2070 | |
| 2071 | static int get_key_size(char **key_string) |
| 2072 | { |
| 2073 | char *colon, dummy; |
| 2074 | int ret; |
| 2075 | |
| 2076 | if (*key_string[0] != ':') |
| 2077 | return strlen(*key_string) >> 1; |
| 2078 | |
| 2079 | /* look for next ':' in key string */ |
| 2080 | colon = strpbrk(*key_string + 1, ":"); |
| 2081 | if (!colon) |
| 2082 | return -EINVAL; |
| 2083 | |
| 2084 | if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':') |
| 2085 | return -EINVAL; |
| 2086 | |
| 2087 | *key_string = colon; |
| 2088 | |
| 2089 | /* remaining key string should be :<logon|user>:<key_desc> */ |
| 2090 | |
| 2091 | return ret; |
| 2092 | } |
| 2093 | |
| 2094 | #else |
| 2095 | |
| 2096 | static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) |
| 2097 | { |
| 2098 | return -EINVAL; |
| 2099 | } |
| 2100 | |
| 2101 | static int get_key_size(char **key_string) |
| 2102 | { |
| 2103 | return (*key_string[0] == ':') ? -EINVAL : strlen(*key_string) >> 1; |
| 2104 | } |
| 2105 | |
| 2106 | #endif |
| 2107 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2108 | static int crypt_set_key(struct crypt_config *cc, char *key) |
| 2109 | { |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 2110 | int r = -EINVAL; |
| 2111 | int key_string_len = strlen(key); |
| 2112 | |
Milan Broz | 69a8cfc | 2011-01-13 19:59:49 +0000 | [diff] [blame] | 2113 | /* Hyphen (which gives a key_size of zero) means there is no key. */ |
| 2114 | if (!cc->key_size && strcmp(key, "-")) |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 2115 | goto out; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2116 | |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2117 | /* ':' means the key is in kernel keyring, short-circuit normal key processing */ |
| 2118 | if (key[0] == ':') { |
| 2119 | r = crypt_set_keyring_key(cc, key + 1); |
| 2120 | goto out; |
| 2121 | } |
| 2122 | |
Ondrej Kozina | 265e909 | 2016-11-02 15:02:08 +0100 | [diff] [blame] | 2123 | /* clear the flag since following operations may invalidate previously valid key */ |
| 2124 | clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
| 2125 | |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2126 | /* wipe references to any kernel keyring key */ |
| 2127 | kzfree(cc->key_string); |
| 2128 | cc->key_string = NULL; |
| 2129 | |
Andy Shevchenko | e944e03 | 2017-04-27 16:52:04 +0300 | [diff] [blame] | 2130 | /* Decode key from its hex representation. */ |
| 2131 | if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0) |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 2132 | goto out; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2133 | |
Mikulas Patocka | 671ea6b | 2016-08-25 07:12:54 -0400 | [diff] [blame] | 2134 | r = crypt_setkey(cc); |
Ondrej Kozina | 265e909 | 2016-11-02 15:02:08 +0100 | [diff] [blame] | 2135 | if (!r) |
| 2136 | set_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 2137 | |
| 2138 | out: |
| 2139 | /* Hex key string not needed after here, so wipe it. */ |
| 2140 | memset(key, '0', key_string_len); |
| 2141 | |
| 2142 | return r; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | static int crypt_wipe_key(struct crypt_config *cc) |
| 2146 | { |
Ondrej Kozina | c82feee | 2017-04-24 14:21:53 +0200 | [diff] [blame] | 2147 | int r; |
| 2148 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2149 | clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
Ondrej Kozina | c82feee | 2017-04-24 14:21:53 +0200 | [diff] [blame] | 2150 | get_random_bytes(&cc->key, cc->key_size); |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2151 | kzfree(cc->key_string); |
| 2152 | cc->key_string = NULL; |
Ondrej Kozina | c82feee | 2017-04-24 14:21:53 +0200 | [diff] [blame] | 2153 | r = crypt_setkey(cc); |
| 2154 | memset(&cc->key, 0, cc->key_size * sizeof(u8)); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 2155 | |
Ondrej Kozina | c82feee | 2017-04-24 14:21:53 +0200 | [diff] [blame] | 2156 | return r; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2157 | } |
| 2158 | |
Mikulas Patocka | 5059353 | 2017-08-13 22:45:08 -0400 | [diff] [blame] | 2159 | static void crypt_calculate_pages_per_client(void) |
| 2160 | { |
| 2161 | unsigned long pages = (totalram_pages - totalhigh_pages) * DM_CRYPT_MEMORY_PERCENT / 100; |
| 2162 | |
| 2163 | if (!dm_crypt_clients_n) |
| 2164 | return; |
| 2165 | |
| 2166 | pages /= dm_crypt_clients_n; |
| 2167 | if (pages < DM_CRYPT_MIN_PAGES_PER_CLIENT) |
| 2168 | pages = DM_CRYPT_MIN_PAGES_PER_CLIENT; |
| 2169 | dm_crypt_pages_per_client = pages; |
| 2170 | } |
| 2171 | |
| 2172 | static void *crypt_page_alloc(gfp_t gfp_mask, void *pool_data) |
| 2173 | { |
| 2174 | struct crypt_config *cc = pool_data; |
| 2175 | struct page *page; |
| 2176 | |
| 2177 | if (unlikely(percpu_counter_compare(&cc->n_allocated_pages, dm_crypt_pages_per_client) >= 0) && |
| 2178 | likely(gfp_mask & __GFP_NORETRY)) |
| 2179 | return NULL; |
| 2180 | |
| 2181 | page = alloc_page(gfp_mask); |
| 2182 | if (likely(page != NULL)) |
| 2183 | percpu_counter_add(&cc->n_allocated_pages, 1); |
| 2184 | |
| 2185 | return page; |
| 2186 | } |
| 2187 | |
| 2188 | static void crypt_page_free(void *page, void *pool_data) |
| 2189 | { |
| 2190 | struct crypt_config *cc = pool_data; |
| 2191 | |
| 2192 | __free_page(page); |
| 2193 | percpu_counter_sub(&cc->n_allocated_pages, 1); |
| 2194 | } |
| 2195 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 2196 | static void crypt_dtr(struct dm_target *ti) |
| 2197 | { |
| 2198 | struct crypt_config *cc = ti->private; |
| 2199 | |
| 2200 | ti->private = NULL; |
| 2201 | |
| 2202 | if (!cc) |
| 2203 | return; |
| 2204 | |
Rabin Vincent | f659b10 | 2016-09-21 16:22:29 +0200 | [diff] [blame] | 2205 | if (cc->write_thread) |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 2206 | kthread_stop(cc->write_thread); |
| 2207 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 2208 | if (cc->io_queue) |
| 2209 | destroy_workqueue(cc->io_queue); |
| 2210 | if (cc->crypt_queue) |
| 2211 | destroy_workqueue(cc->crypt_queue); |
| 2212 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 2213 | crypt_free_tfms(cc); |
| 2214 | |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 2215 | bioset_exit(&cc->bs); |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 2216 | |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 2217 | mempool_exit(&cc->page_pool); |
| 2218 | mempool_exit(&cc->req_pool); |
| 2219 | mempool_exit(&cc->tag_pool); |
| 2220 | |
Kent Overstreet | d00a11d | 2018-06-02 13:45:04 -0400 | [diff] [blame] | 2221 | WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0); |
| 2222 | percpu_counter_destroy(&cc->n_allocated_pages); |
| 2223 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 2224 | if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) |
| 2225 | cc->iv_gen_ops->dtr(cc); |
| 2226 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 2227 | if (cc->dev) |
| 2228 | dm_put_device(ti, cc->dev); |
| 2229 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 2230 | kzfree(cc->cipher); |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 2231 | kzfree(cc->cipher_string); |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2232 | kzfree(cc->key_string); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2233 | kzfree(cc->cipher_auth); |
| 2234 | kzfree(cc->authenc_key); |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 2235 | |
Mike Snitzer | d5ffebd | 2018-01-05 21:17:20 -0500 | [diff] [blame] | 2236 | mutex_destroy(&cc->bio_alloc_lock); |
| 2237 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 2238 | /* Must zero key material before freeing */ |
| 2239 | kzfree(cc); |
Mikulas Patocka | 5059353 | 2017-08-13 22:45:08 -0400 | [diff] [blame] | 2240 | |
| 2241 | spin_lock(&dm_crypt_clients_lock); |
| 2242 | WARN_ON(!dm_crypt_clients_n); |
| 2243 | dm_crypt_clients_n--; |
| 2244 | crypt_calculate_pages_per_client(); |
| 2245 | spin_unlock(&dm_crypt_clients_lock); |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 2246 | } |
| 2247 | |
Milan Broz | e889f97 | 2017-03-16 15:39:39 +0100 | [diff] [blame] | 2248 | static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 2250 | struct crypt_config *cc = ti->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2252 | if (crypt_integrity_aead(cc)) |
Milan Broz | e889f97 | 2017-03-16 15:39:39 +0100 | [diff] [blame] | 2253 | cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); |
| 2254 | else |
| 2255 | cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 2257 | if (cc->iv_size) |
| 2258 | /* at least a 64 bit sector number should fit in our buffer */ |
| 2259 | cc->iv_size = max(cc->iv_size, |
| 2260 | (unsigned int)(sizeof(u64) / sizeof(u8))); |
| 2261 | else if (ivmode) { |
| 2262 | DMWARN("Selected cipher does not support IVs"); |
| 2263 | ivmode = NULL; |
| 2264 | } |
| 2265 | |
| 2266 | /* Choose ivmode, see comments at iv code. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | if (ivmode == NULL) |
| 2268 | cc->iv_gen_ops = NULL; |
| 2269 | else if (strcmp(ivmode, "plain") == 0) |
| 2270 | cc->iv_gen_ops = &crypt_iv_plain_ops; |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 2271 | else if (strcmp(ivmode, "plain64") == 0) |
| 2272 | cc->iv_gen_ops = &crypt_iv_plain64_ops; |
Milan Broz | 7e3fd85 | 2017-06-06 09:07:01 +0200 | [diff] [blame] | 2273 | else if (strcmp(ivmode, "plain64be") == 0) |
| 2274 | cc->iv_gen_ops = &crypt_iv_plain64be_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | else if (strcmp(ivmode, "essiv") == 0) |
| 2276 | cc->iv_gen_ops = &crypt_iv_essiv_ops; |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 2277 | else if (strcmp(ivmode, "benbi") == 0) |
| 2278 | cc->iv_gen_ops = &crypt_iv_benbi_ops; |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 2279 | else if (strcmp(ivmode, "null") == 0) |
| 2280 | cc->iv_gen_ops = &crypt_iv_null_ops; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 2281 | else if (strcmp(ivmode, "lmk") == 0) { |
| 2282 | cc->iv_gen_ops = &crypt_iv_lmk_ops; |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 2283 | /* |
| 2284 | * Version 2 and 3 is recognised according |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 2285 | * to length of provided multi-key string. |
| 2286 | * If present (version 3), last key is used as IV seed. |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 2287 | * All keys (including IV seed) are always the same size. |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 2288 | */ |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 2289 | if (cc->key_size % cc->key_parts) { |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 2290 | cc->key_parts++; |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 2291 | cc->key_extra_size = cc->key_size / cc->key_parts; |
| 2292 | } |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 2293 | } else if (strcmp(ivmode, "tcw") == 0) { |
| 2294 | cc->iv_gen_ops = &crypt_iv_tcw_ops; |
| 2295 | cc->key_parts += 2; /* IV + whitening */ |
| 2296 | cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE; |
Milan Broz | e889f97 | 2017-03-16 15:39:39 +0100 | [diff] [blame] | 2297 | } else if (strcmp(ivmode, "random") == 0) { |
| 2298 | cc->iv_gen_ops = &crypt_iv_random_ops; |
| 2299 | /* Need storage space in integrity fields. */ |
| 2300 | cc->integrity_iv_size = cc->iv_size; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 2301 | } else { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 2302 | ti->error = "Invalid IV mode"; |
Milan Broz | e889f97 | 2017-03-16 15:39:39 +0100 | [diff] [blame] | 2303 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | } |
| 2305 | |
Milan Broz | e889f97 | 2017-03-16 15:39:39 +0100 | [diff] [blame] | 2306 | return 0; |
| 2307 | } |
| 2308 | |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2309 | /* |
| 2310 | * Workaround to parse cipher algorithm from crypto API spec. |
| 2311 | * The cc->cipher is currently used only in ESSIV. |
| 2312 | * This should be probably done by crypto-api calls (once available...) |
| 2313 | */ |
| 2314 | static int crypt_ctr_blkdev_cipher(struct crypt_config *cc) |
| 2315 | { |
| 2316 | const char *alg_name = NULL; |
| 2317 | char *start, *end; |
| 2318 | |
| 2319 | if (crypt_integrity_aead(cc)) { |
| 2320 | alg_name = crypto_tfm_alg_name(crypto_aead_tfm(any_tfm_aead(cc))); |
| 2321 | if (!alg_name) |
| 2322 | return -EINVAL; |
| 2323 | if (crypt_integrity_hmac(cc)) { |
| 2324 | alg_name = strchr(alg_name, ','); |
| 2325 | if (!alg_name) |
| 2326 | return -EINVAL; |
| 2327 | } |
| 2328 | alg_name++; |
| 2329 | } else { |
| 2330 | alg_name = crypto_tfm_alg_name(crypto_skcipher_tfm(any_tfm(cc))); |
| 2331 | if (!alg_name) |
| 2332 | return -EINVAL; |
| 2333 | } |
| 2334 | |
| 2335 | start = strchr(alg_name, '('); |
| 2336 | end = strchr(alg_name, ')'); |
| 2337 | |
| 2338 | if (!start && !end) { |
| 2339 | cc->cipher = kstrdup(alg_name, GFP_KERNEL); |
| 2340 | return cc->cipher ? 0 : -ENOMEM; |
| 2341 | } |
| 2342 | |
| 2343 | if (!start || !end || ++start >= end) |
| 2344 | return -EINVAL; |
| 2345 | |
| 2346 | cc->cipher = kzalloc(end - start + 1, GFP_KERNEL); |
| 2347 | if (!cc->cipher) |
| 2348 | return -ENOMEM; |
| 2349 | |
| 2350 | strncpy(cc->cipher, start, end - start); |
| 2351 | |
| 2352 | return 0; |
| 2353 | } |
| 2354 | |
| 2355 | /* |
| 2356 | * Workaround to parse HMAC algorithm from AEAD crypto API spec. |
| 2357 | * The HMAC is needed to calculate tag size (HMAC digest size). |
| 2358 | * This should be probably done by crypto-api calls (once available...) |
| 2359 | */ |
| 2360 | static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api) |
| 2361 | { |
| 2362 | char *start, *end, *mac_alg = NULL; |
| 2363 | struct crypto_ahash *mac; |
| 2364 | |
| 2365 | if (!strstarts(cipher_api, "authenc(")) |
| 2366 | return 0; |
| 2367 | |
| 2368 | start = strchr(cipher_api, '('); |
| 2369 | end = strchr(cipher_api, ','); |
| 2370 | if (!start || !end || ++start > end) |
| 2371 | return -EINVAL; |
| 2372 | |
| 2373 | mac_alg = kzalloc(end - start + 1, GFP_KERNEL); |
| 2374 | if (!mac_alg) |
| 2375 | return -ENOMEM; |
| 2376 | strncpy(mac_alg, start, end - start); |
| 2377 | |
| 2378 | mac = crypto_alloc_ahash(mac_alg, 0, 0); |
| 2379 | kfree(mac_alg); |
| 2380 | |
| 2381 | if (IS_ERR(mac)) |
| 2382 | return PTR_ERR(mac); |
| 2383 | |
| 2384 | cc->key_mac_size = crypto_ahash_digestsize(mac); |
| 2385 | crypto_free_ahash(mac); |
| 2386 | |
| 2387 | cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL); |
| 2388 | if (!cc->authenc_key) |
| 2389 | return -ENOMEM; |
| 2390 | |
| 2391 | return 0; |
| 2392 | } |
| 2393 | |
| 2394 | static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key, |
| 2395 | char **ivmode, char **ivopts) |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 2396 | { |
| 2397 | struct crypt_config *cc = ti->private; |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2398 | char *tmp, *cipher_api; |
| 2399 | int ret = -EINVAL; |
| 2400 | |
| 2401 | cc->tfms_count = 1; |
| 2402 | |
| 2403 | /* |
| 2404 | * New format (capi: prefix) |
| 2405 | * capi:cipher_api_spec-iv:ivopts |
| 2406 | */ |
| 2407 | tmp = &cipher_in[strlen("capi:")]; |
| 2408 | cipher_api = strsep(&tmp, "-"); |
| 2409 | *ivmode = strsep(&tmp, ":"); |
| 2410 | *ivopts = tmp; |
| 2411 | |
| 2412 | if (*ivmode && !strcmp(*ivmode, "lmk")) |
| 2413 | cc->tfms_count = 64; |
| 2414 | |
| 2415 | cc->key_parts = cc->tfms_count; |
| 2416 | |
| 2417 | /* Allocate cipher */ |
| 2418 | ret = crypt_alloc_tfms(cc, cipher_api); |
| 2419 | if (ret < 0) { |
| 2420 | ti->error = "Error allocating crypto tfm"; |
| 2421 | return ret; |
| 2422 | } |
| 2423 | |
| 2424 | /* Alloc AEAD, can be used only in new format. */ |
| 2425 | if (crypt_integrity_aead(cc)) { |
| 2426 | ret = crypt_ctr_auth_cipher(cc, cipher_api); |
| 2427 | if (ret < 0) { |
| 2428 | ti->error = "Invalid AEAD cipher spec"; |
| 2429 | return -ENOMEM; |
| 2430 | } |
| 2431 | cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); |
| 2432 | } else |
| 2433 | cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); |
| 2434 | |
| 2435 | ret = crypt_ctr_blkdev_cipher(cc); |
| 2436 | if (ret < 0) { |
| 2437 | ti->error = "Cannot allocate cipher string"; |
| 2438 | return -ENOMEM; |
| 2439 | } |
| 2440 | |
| 2441 | return 0; |
| 2442 | } |
| 2443 | |
| 2444 | static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key, |
| 2445 | char **ivmode, char **ivopts) |
| 2446 | { |
| 2447 | struct crypt_config *cc = ti->private; |
| 2448 | char *tmp, *cipher, *chainmode, *keycount; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 2449 | char *cipher_api = NULL; |
| 2450 | int ret = -EINVAL; |
| 2451 | char dummy; |
| 2452 | |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2453 | if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 2454 | ti->error = "Bad cipher specification"; |
| 2455 | return -EINVAL; |
| 2456 | } |
| 2457 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | /* |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 2459 | * Legacy dm-crypt cipher specification |
| 2460 | * cipher[:keycount]-mode-iv:ivopts |
| 2461 | */ |
| 2462 | tmp = cipher_in; |
| 2463 | keycount = strsep(&tmp, "-"); |
| 2464 | cipher = strsep(&keycount, ":"); |
| 2465 | |
Milan Broz | 69a8cfc | 2011-01-13 19:59:49 +0000 | [diff] [blame] | 2466 | if (!keycount) |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 2467 | cc->tfms_count = 1; |
| 2468 | else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 || |
| 2469 | !is_power_of_2(cc->tfms_count)) { |
| 2470 | ti->error = "Bad cipher key count specification"; |
| 2471 | return -EINVAL; |
| 2472 | } |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 2473 | cc->key_parts = cc->tfms_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 2475 | cc->cipher = kstrdup(cipher, GFP_KERNEL); |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 2476 | if (!cc->cipher) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | goto bad_mem; |
| 2478 | |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 2479 | chainmode = strsep(&tmp, "-"); |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2480 | *ivopts = strsep(&tmp, "-"); |
| 2481 | *ivmode = strsep(&*ivopts, ":"); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 2482 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 2483 | if (tmp) |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 2484 | DMWARN("Ignoring unexpected additional cipher options"); |
| 2485 | |
| 2486 | /* |
| 2487 | * For compatibility with the original dm-crypt mapping format, if |
| 2488 | * only the cipher name is supplied, use cbc-plain. |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 2489 | */ |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2490 | if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) { |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 2491 | chainmode = "cbc"; |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2492 | *ivmode = "plain"; |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 2493 | } |
| 2494 | |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2495 | if (strcmp(chainmode, "ecb") && !*ivmode) { |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 2496 | ti->error = "IV mechanism required"; |
| 2497 | return -EINVAL; |
| 2498 | } |
| 2499 | |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 2500 | cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL); |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 2501 | if (!cipher_api) |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 2502 | goto bad_mem; |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 2503 | |
| 2504 | ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME, |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 2505 | "%s(%s)", chainmode, cipher); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | if (ret < 0) { |
| 2507 | kfree(cipher_api); |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 2508 | goto bad_mem; |
| 2509 | } |
| 2510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | /* Allocate cipher */ |
| 2512 | ret = crypt_alloc_tfms(cc, cipher_api); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | if (ret < 0) { |
| 2514 | ti->error = "Error allocating crypto tfm"; |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2515 | kfree(cipher_api); |
| 2516 | return ret; |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 2517 | } |
Jeffy Chen | bd86e32 | 2017-09-27 20:28:57 +0800 | [diff] [blame] | 2518 | kfree(cipher_api); |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 2519 | |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2520 | return 0; |
| 2521 | bad_mem: |
| 2522 | ti->error = "Cannot allocate cipher strings"; |
| 2523 | return -ENOMEM; |
| 2524 | } |
| 2525 | |
| 2526 | static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key) |
| 2527 | { |
| 2528 | struct crypt_config *cc = ti->private; |
| 2529 | char *ivmode = NULL, *ivopts = NULL; |
| 2530 | int ret; |
| 2531 | |
| 2532 | cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL); |
| 2533 | if (!cc->cipher_string) { |
| 2534 | ti->error = "Cannot allocate cipher strings"; |
| 2535 | return -ENOMEM; |
| 2536 | } |
| 2537 | |
| 2538 | if (strstarts(cipher_in, "capi:")) |
| 2539 | ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts); |
| 2540 | else |
| 2541 | ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts); |
| 2542 | if (ret) |
| 2543 | return ret; |
| 2544 | |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 2545 | /* Initialize IV */ |
Milan Broz | e889f97 | 2017-03-16 15:39:39 +0100 | [diff] [blame] | 2546 | ret = crypt_ctr_ivmode(ti, ivmode); |
| 2547 | if (ret < 0) |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2548 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 2550 | /* Initialize and set key */ |
| 2551 | ret = crypt_set_key(cc, key); |
| 2552 | if (ret < 0) { |
| 2553 | ti->error = "Error decoding and setting key"; |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2554 | return ret; |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 2555 | } |
| 2556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | /* Allocate IV */ |
| 2558 | if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) { |
| 2559 | ret = cc->iv_gen_ops->ctr(cc, ti, ivopts); |
| 2560 | if (ret < 0) { |
| 2561 | ti->error = "Error creating IV"; |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2562 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | /* Initialize IV (set keys for ESSIV etc) */ |
| 2567 | if (cc->iv_gen_ops && cc->iv_gen_ops->init) { |
| 2568 | ret = cc->iv_gen_ops->init(cc); |
| 2569 | if (ret < 0) { |
| 2570 | ti->error = "Error initialising IV"; |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2571 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | } |
| 2573 | } |
| 2574 | |
Ondrej Kozina | dc94902 | 2018-01-12 16:30:32 +0100 | [diff] [blame] | 2575 | /* wipe the kernel key payload copy */ |
| 2576 | if (cc->key_string) |
| 2577 | memset(cc->key, 0, cc->key_size * sizeof(u8)); |
| 2578 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2582 | static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv) |
| 2583 | { |
| 2584 | struct crypt_config *cc = ti->private; |
| 2585 | struct dm_arg_set as; |
Eric Biggers | 5916a22 | 2017-06-22 11:32:45 -0700 | [diff] [blame] | 2586 | static const struct dm_arg _args[] = { |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 2587 | {0, 6, "Invalid number of feature args"}, |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2588 | }; |
| 2589 | unsigned int opt_params, val; |
| 2590 | const char *opt_string, *sval; |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 2591 | char dummy; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2592 | int ret; |
| 2593 | |
| 2594 | /* Optional parameters */ |
| 2595 | as.argc = argc; |
| 2596 | as.argv = argv; |
| 2597 | |
| 2598 | ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error); |
| 2599 | if (ret) |
| 2600 | return ret; |
| 2601 | |
| 2602 | while (opt_params--) { |
| 2603 | opt_string = dm_shift_arg(&as); |
| 2604 | if (!opt_string) { |
| 2605 | ti->error = "Not enough feature arguments"; |
| 2606 | return -EINVAL; |
| 2607 | } |
| 2608 | |
| 2609 | if (!strcasecmp(opt_string, "allow_discards")) |
| 2610 | ti->num_discard_bios = 1; |
| 2611 | |
| 2612 | else if (!strcasecmp(opt_string, "same_cpu_crypt")) |
| 2613 | set_bit(DM_CRYPT_SAME_CPU, &cc->flags); |
| 2614 | |
| 2615 | else if (!strcasecmp(opt_string, "submit_from_crypt_cpus")) |
| 2616 | set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); |
| 2617 | else if (sscanf(opt_string, "integrity:%u:", &val) == 1) { |
| 2618 | if (val == 0 || val > MAX_TAG_SIZE) { |
| 2619 | ti->error = "Invalid integrity arguments"; |
| 2620 | return -EINVAL; |
| 2621 | } |
| 2622 | cc->on_disk_tag_size = val; |
| 2623 | sval = strchr(opt_string + strlen("integrity:"), ':') + 1; |
| 2624 | if (!strcasecmp(sval, "aead")) { |
| 2625 | set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2626 | } else if (strcasecmp(sval, "none")) { |
| 2627 | ti->error = "Unknown integrity profile"; |
| 2628 | return -EINVAL; |
| 2629 | } |
| 2630 | |
| 2631 | cc->cipher_auth = kstrdup(sval, GFP_KERNEL); |
| 2632 | if (!cc->cipher_auth) |
| 2633 | return -ENOMEM; |
Mikulas Patocka | ff3af92 | 2017-03-23 10:23:14 -0400 | [diff] [blame] | 2634 | } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) { |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 2635 | if (cc->sector_size < (1 << SECTOR_SHIFT) || |
| 2636 | cc->sector_size > 4096 || |
Mikulas Patocka | ff3af92 | 2017-03-23 10:23:14 -0400 | [diff] [blame] | 2637 | (cc->sector_size & (cc->sector_size - 1))) { |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 2638 | ti->error = "Invalid feature value for sector_size"; |
| 2639 | return -EINVAL; |
| 2640 | } |
Milan Broz | 783874b | 2017-09-13 15:45:56 +0200 | [diff] [blame] | 2641 | if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) { |
| 2642 | ti->error = "Device size is not multiple of sector_size feature"; |
| 2643 | return -EINVAL; |
| 2644 | } |
Mikulas Patocka | ff3af92 | 2017-03-23 10:23:14 -0400 | [diff] [blame] | 2645 | cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT; |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 2646 | } else if (!strcasecmp(opt_string, "iv_large_sectors")) |
| 2647 | set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); |
| 2648 | else { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2649 | ti->error = "Invalid feature arguments"; |
| 2650 | return -EINVAL; |
| 2651 | } |
| 2652 | } |
| 2653 | |
| 2654 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | /* |
| 2658 | * Construct an encryption mapping: |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2659 | * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | */ |
| 2661 | static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 2662 | { |
| 2663 | struct crypt_config *cc; |
Michał Mirosław | ed0302e | 2018-10-09 22:13:43 +0200 | [diff] [blame] | 2664 | const char *devname = dm_table_device_name(ti->table); |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2665 | int key_size; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2666 | unsigned int align_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | unsigned long long tmpll; |
| 2668 | int ret; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2669 | size_t iv_size_padding, additional_req_size; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 2670 | char dummy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 2672 | if (argc < 5) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | ti->error = "Not enough arguments"; |
| 2674 | return -EINVAL; |
| 2675 | } |
| 2676 | |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2677 | key_size = get_key_size(&argv[1]); |
| 2678 | if (key_size < 0) { |
| 2679 | ti->error = "Cannot parse key size"; |
| 2680 | return -EINVAL; |
| 2681 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2682 | |
| 2683 | cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL); |
| 2684 | if (!cc) { |
| 2685 | ti->error = "Cannot allocate encryption context"; |
| 2686 | return -ENOMEM; |
| 2687 | } |
| 2688 | cc->key_size = key_size; |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 2689 | cc->sector_size = (1 << SECTOR_SHIFT); |
Mikulas Patocka | ff3af92 | 2017-03-23 10:23:14 -0400 | [diff] [blame] | 2690 | cc->sector_shift = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | |
| 2692 | ti->private = cc; |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2693 | |
Mikulas Patocka | 5059353 | 2017-08-13 22:45:08 -0400 | [diff] [blame] | 2694 | spin_lock(&dm_crypt_clients_lock); |
| 2695 | dm_crypt_clients_n++; |
| 2696 | crypt_calculate_pages_per_client(); |
| 2697 | spin_unlock(&dm_crypt_clients_lock); |
| 2698 | |
| 2699 | ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL); |
| 2700 | if (ret < 0) |
| 2701 | goto bad; |
| 2702 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2703 | /* Optional parameters need to be read before cipher constructor */ |
| 2704 | if (argc > 5) { |
| 2705 | ret = crypt_ctr_optional(ti, argc - 5, &argv[5]); |
| 2706 | if (ret) |
| 2707 | goto bad; |
| 2708 | } |
| 2709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | ret = crypt_ctr_cipher(ti, argv[0], argv[1]); |
| 2711 | if (ret < 0) |
| 2712 | goto bad; |
| 2713 | |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2714 | if (crypt_integrity_aead(cc)) { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2715 | cc->dmreq_start = sizeof(struct aead_request); |
| 2716 | cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc)); |
| 2717 | align_mask = crypto_aead_alignmask(any_tfm_aead(cc)); |
| 2718 | } else { |
| 2719 | cc->dmreq_start = sizeof(struct skcipher_request); |
| 2720 | cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc)); |
| 2721 | align_mask = crypto_skcipher_alignmask(any_tfm(cc)); |
| 2722 | } |
Mikulas Patocka | d49ec52 | 2014-08-28 11:09:31 -0400 | [diff] [blame] | 2723 | cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request)); |
| 2724 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2725 | if (align_mask < CRYPTO_MINALIGN) { |
Mikulas Patocka | d49ec52 | 2014-08-28 11:09:31 -0400 | [diff] [blame] | 2726 | /* Allocate the padding exactly */ |
| 2727 | iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request)) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2728 | & align_mask; |
Mikulas Patocka | d49ec52 | 2014-08-28 11:09:31 -0400 | [diff] [blame] | 2729 | } else { |
| 2730 | /* |
| 2731 | * If the cipher requires greater alignment than kmalloc |
| 2732 | * alignment, we don't know the exact position of the |
| 2733 | * initialization vector. We must assume worst case. |
| 2734 | */ |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2735 | iv_size_padding = align_mask; |
Mikulas Patocka | d49ec52 | 2014-08-28 11:09:31 -0400 | [diff] [blame] | 2736 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2738 | /* ...| IV + padding | original IV | original sec. number | bio tag offset | */ |
| 2739 | additional_req_size = sizeof(struct dm_crypt_request) + |
| 2740 | iv_size_padding + cc->iv_size + |
| 2741 | cc->iv_size + |
| 2742 | sizeof(uint64_t) + |
| 2743 | sizeof(unsigned int); |
| 2744 | |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 2745 | ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size); |
| 2746 | if (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | ti->error = "Cannot allocate crypt request mempool"; |
| 2748 | goto bad; |
| 2749 | } |
| 2750 | |
Mike Snitzer | 30187e1 | 2016-01-31 13:28:26 -0500 | [diff] [blame] | 2751 | cc->per_bio_data_size = ti->per_io_data_size = |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2752 | ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size, |
Mikulas Patocka | d49ec52 | 2014-08-28 11:09:31 -0400 | [diff] [blame] | 2753 | ARCH_KMALLOC_MINALIGN); |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 2754 | |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 2755 | ret = mempool_init(&cc->page_pool, BIO_MAX_PAGES, crypt_page_alloc, crypt_page_free, cc); |
| 2756 | if (ret) { |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2757 | ti->error = "Cannot allocate page mempool"; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2758 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | } |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2760 | |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 2761 | ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS); |
| 2762 | if (ret) { |
Milan Broz | 0c395b0 | 2008-02-08 02:10:54 +0000 | [diff] [blame] | 2763 | ti->error = "Cannot allocate crypt bioset"; |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 2764 | goto bad; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 2765 | } |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 2766 | |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 2767 | mutex_init(&cc->bio_alloc_lock); |
| 2768 | |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 2769 | ret = -EINVAL; |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 2770 | if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) || |
| 2771 | (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) { |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 2772 | ti->error = "Invalid iv_offset sector"; |
| 2773 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2774 | } |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 2775 | cc->iv_offset = tmpll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | |
Vivek Goyal | e80d1c8 | 2015-07-31 09:20:36 -0400 | [diff] [blame] | 2777 | ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev); |
| 2778 | if (ret) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | ti->error = "Device lookup failed"; |
| 2780 | goto bad; |
| 2781 | } |
| 2782 | |
Vivek Goyal | e80d1c8 | 2015-07-31 09:20:36 -0400 | [diff] [blame] | 2783 | ret = -EINVAL; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 2784 | if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2785 | ti->error = "Invalid device sector"; |
| 2786 | goto bad; |
| 2787 | } |
| 2788 | cc->start = tmpll; |
| 2789 | |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2790 | if (crypt_integrity_aead(cc) || cc->integrity_iv_size) { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2791 | ret = crypt_integrity_ctr(cc, ti); |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 2792 | if (ret) |
| 2793 | goto bad; |
| 2794 | |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2795 | cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size; |
| 2796 | if (!cc->tag_pool_max_sectors) |
| 2797 | cc->tag_pool_max_sectors = 1; |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 2798 | |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 2799 | ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS, |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2800 | cc->tag_pool_max_sectors * cc->on_disk_tag_size); |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 2801 | if (ret) { |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2802 | ti->error = "Cannot allocate integrity tags mempool"; |
| 2803 | goto bad; |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 2804 | } |
Mikulas Patocka | 583fe74 | 2017-04-18 16:51:54 -0400 | [diff] [blame] | 2805 | |
| 2806 | cc->tag_pool_max_sectors <<= cc->sector_shift; |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 2807 | } |
| 2808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | ret = -ENOMEM; |
Michał Mirosław | ed0302e | 2018-10-09 22:13:43 +0200 | [diff] [blame] | 2810 | cc->io_queue = alloc_workqueue("kcryptd_io/%s", |
| 2811 | WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, |
| 2812 | 1, devname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2813 | if (!cc->io_queue) { |
| 2814 | ti->error = "Couldn't create kcryptd io queue"; |
| 2815 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | } |
Christophe Saout | 37af656 | 2006-10-30 20:39:08 +0100 | [diff] [blame] | 2817 | |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 2818 | if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) |
Michał Mirosław | ed0302e | 2018-10-09 22:13:43 +0200 | [diff] [blame] | 2819 | cc->crypt_queue = alloc_workqueue("kcryptd/%s", |
| 2820 | WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, |
| 2821 | 1, devname); |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 2822 | else |
Michał Mirosław | ed0302e | 2018-10-09 22:13:43 +0200 | [diff] [blame] | 2823 | cc->crypt_queue = alloc_workqueue("kcryptd/%s", |
Tim Murray | a1b8913 | 2017-04-21 11:11:36 +0200 | [diff] [blame] | 2824 | WQ_HIGHPRI | WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, |
Michał Mirosław | ed0302e | 2018-10-09 22:13:43 +0200 | [diff] [blame] | 2825 | num_online_cpus(), devname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | if (!cc->crypt_queue) { |
| 2827 | ti->error = "Couldn't create kcryptd queue"; |
| 2828 | goto bad; |
| 2829 | } |
| 2830 | |
Mikulas Patocka | c7329ef | 2018-07-11 12:10:51 -0400 | [diff] [blame] | 2831 | spin_lock_init(&cc->write_thread_lock); |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 2832 | cc->write_tree = RB_ROOT; |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 2833 | |
Michał Mirosław | ed0302e | 2018-10-09 22:13:43 +0200 | [diff] [blame] | 2834 | cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write/%s", devname); |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 2835 | if (IS_ERR(cc->write_thread)) { |
| 2836 | ret = PTR_ERR(cc->write_thread); |
| 2837 | cc->write_thread = NULL; |
| 2838 | ti->error = "Couldn't spawn write thread"; |
| 2839 | goto bad; |
| 2840 | } |
| 2841 | wake_up_process(cc->write_thread); |
| 2842 | |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 2843 | ti->num_flush_bios = 1; |
Milan Broz | 983c7db | 2011-09-25 23:26:21 +0100 | [diff] [blame] | 2844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | return 0; |
| 2846 | |
| 2847 | bad: |
| 2848 | crypt_dtr(ti); |
| 2849 | return ret; |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 2850 | } |
| 2851 | |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 2852 | static int crypt_map(struct dm_target *ti, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | { |
| 2854 | struct dm_crypt_io *io; |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 2855 | struct crypt_config *cc = ti->private; |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 2856 | |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 2857 | /* |
Mike Christie | 28a8f0d | 2016-06-05 14:32:25 -0500 | [diff] [blame] | 2858 | * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues. |
| 2859 | * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight |
Mike Christie | e604714 | 2016-06-05 14:32:04 -0500 | [diff] [blame] | 2860 | * - for REQ_OP_DISCARD caller must use flush if IO ordering matters |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 2861 | */ |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 2862 | if (unlikely(bio->bi_opf & REQ_PREFLUSH || |
Mike Christie | 28a8f0d | 2016-06-05 14:32:25 -0500 | [diff] [blame] | 2863 | bio_op(bio) == REQ_OP_DISCARD)) { |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2864 | bio_set_dev(bio, cc->dev->bdev); |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 2865 | if (bio_sectors(bio)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2866 | bio->bi_iter.bi_sector = cc->start + |
| 2867 | dm_target_offset(ti, bio->bi_iter.bi_sector); |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 2868 | return DM_MAPIO_REMAPPED; |
| 2869 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | |
Mikulas Patocka | 4e870e9 | 2016-08-30 16:38:42 -0400 | [diff] [blame] | 2871 | /* |
| 2872 | * Check if bio is too large, split as needed. |
| 2873 | */ |
| 2874 | if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) && |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2875 | (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size)) |
Mikulas Patocka | 4e870e9 | 2016-08-30 16:38:42 -0400 | [diff] [blame] | 2876 | dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT)); |
| 2877 | |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 2878 | /* |
| 2879 | * Ensure that bio is a multiple of internal sector encryption size |
| 2880 | * and is aligned to this size as defined in IO hints. |
| 2881 | */ |
| 2882 | if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0)) |
Christoph Hellwig | 846785e | 2017-06-03 09:38:02 +0200 | [diff] [blame] | 2883 | return DM_MAPIO_KILL; |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 2884 | |
| 2885 | if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1))) |
Christoph Hellwig | 846785e | 2017-06-03 09:38:02 +0200 | [diff] [blame] | 2886 | return DM_MAPIO_KILL; |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 2887 | |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 2888 | io = dm_per_bio_data(bio, cc->per_bio_data_size); |
| 2889 | crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector)); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2890 | |
| 2891 | if (cc->on_disk_tag_size) { |
Mikulas Patocka | 583fe74 | 2017-04-18 16:51:54 -0400 | [diff] [blame] | 2892 | unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2893 | |
| 2894 | if (unlikely(tag_len > KMALLOC_MAX_SIZE) || |
Mikulas Patocka | 583fe74 | 2017-04-18 16:51:54 -0400 | [diff] [blame] | 2895 | unlikely(!(io->integrity_metadata = kmalloc(tag_len, |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2896 | GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) { |
| 2897 | if (bio_sectors(bio) > cc->tag_pool_max_sectors) |
| 2898 | dm_accept_partial_bio(bio, cc->tag_pool_max_sectors); |
Kent Overstreet | 6f1c819 | 2018-05-20 18:25:53 -0400 | [diff] [blame] | 2899 | io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2900 | io->integrity_metadata_from_pool = true; |
| 2901 | } |
| 2902 | } |
| 2903 | |
Milan Broz | 33d2f09 | 2017-03-16 15:39:40 +0100 | [diff] [blame] | 2904 | if (crypt_integrity_aead(cc)) |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2905 | io->ctx.r.req_aead = (struct aead_request *)(io + 1); |
| 2906 | else |
| 2907 | io->ctx.r.req = (struct skcipher_request *)(io + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 2909 | if (bio_data_dir(io->base_bio) == READ) { |
| 2910 | if (kcryptd_io_read(io, GFP_NOWAIT)) |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 2911 | kcryptd_queue_read(io); |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 2912 | } else |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 2913 | kcryptd_queue_crypt(io); |
| 2914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | return DM_MAPIO_SUBMITTED; |
| 2916 | } |
| 2917 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 2918 | static void crypt_status(struct dm_target *ti, status_type_t type, |
| 2919 | unsigned status_flags, char *result, unsigned maxlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 2921 | struct crypt_config *cc = ti->private; |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 2922 | unsigned i, sz = 0; |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 2923 | int num_feature_args = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | |
| 2925 | switch (type) { |
| 2926 | case STATUSTYPE_INFO: |
| 2927 | result[0] = '\0'; |
| 2928 | break; |
| 2929 | |
| 2930 | case STATUSTYPE_TABLE: |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 2931 | DMEMIT("%s ", cc->cipher_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 2933 | if (cc->key_size > 0) { |
| 2934 | if (cc->key_string) |
| 2935 | DMEMIT(":%u:%s", cc->key_size, cc->key_string); |
| 2936 | else |
| 2937 | for (i = 0; i < cc->key_size; i++) |
| 2938 | DMEMIT("%02x", cc->key[i]); |
| 2939 | } else |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 2940 | DMEMIT("-"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2941 | |
| 2942 | DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, |
| 2943 | cc->dev->name, (unsigned long long)cc->start); |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 2944 | |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 2945 | num_feature_args += !!ti->num_discard_bios; |
| 2946 | num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags); |
Mikulas Patocka | 0f5d8e6 | 2015-02-13 08:27:08 -0500 | [diff] [blame] | 2947 | num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); |
Mikulas Patocka | ff3af92 | 2017-03-23 10:23:14 -0400 | [diff] [blame] | 2948 | num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT); |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 2949 | num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2950 | if (cc->on_disk_tag_size) |
| 2951 | num_feature_args++; |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 2952 | if (num_feature_args) { |
| 2953 | DMEMIT(" %d", num_feature_args); |
| 2954 | if (ti->num_discard_bios) |
| 2955 | DMEMIT(" allow_discards"); |
| 2956 | if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) |
| 2957 | DMEMIT(" same_cpu_crypt"); |
Mikulas Patocka | 0f5d8e6 | 2015-02-13 08:27:08 -0500 | [diff] [blame] | 2958 | if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) |
| 2959 | DMEMIT(" submit_from_crypt_cpus"); |
Milan Broz | ef43aa3 | 2017-01-04 20:23:54 +0100 | [diff] [blame] | 2960 | if (cc->on_disk_tag_size) |
| 2961 | DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth); |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 2962 | if (cc->sector_size != (1 << SECTOR_SHIFT)) |
| 2963 | DMEMIT(" sector_size:%d", cc->sector_size); |
| 2964 | if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) |
| 2965 | DMEMIT(" iv_large_sectors"); |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 2966 | } |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 2967 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2968 | break; |
| 2969 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | } |
| 2971 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2972 | static void crypt_postsuspend(struct dm_target *ti) |
| 2973 | { |
| 2974 | struct crypt_config *cc = ti->private; |
| 2975 | |
| 2976 | set_bit(DM_CRYPT_SUSPENDED, &cc->flags); |
| 2977 | } |
| 2978 | |
| 2979 | static int crypt_preresume(struct dm_target *ti) |
| 2980 | { |
| 2981 | struct crypt_config *cc = ti->private; |
| 2982 | |
| 2983 | if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { |
| 2984 | DMERR("aborting resume - crypt key is not set."); |
| 2985 | return -EAGAIN; |
| 2986 | } |
| 2987 | |
| 2988 | return 0; |
| 2989 | } |
| 2990 | |
| 2991 | static void crypt_resume(struct dm_target *ti) |
| 2992 | { |
| 2993 | struct crypt_config *cc = ti->private; |
| 2994 | |
| 2995 | clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); |
| 2996 | } |
| 2997 | |
| 2998 | /* Message interface |
| 2999 | * key set <key> |
| 3000 | * key wipe |
| 3001 | */ |
Mike Snitzer | 1eb5fa8 | 2018-02-28 15:59:59 -0500 | [diff] [blame] | 3002 | static int crypt_message(struct dm_target *ti, unsigned argc, char **argv, |
| 3003 | char *result, unsigned maxlen) |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 3004 | { |
| 3005 | struct crypt_config *cc = ti->private; |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 3006 | int key_size, ret = -EINVAL; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 3007 | |
| 3008 | if (argc < 2) |
| 3009 | goto error; |
| 3010 | |
Mike Snitzer | 498f010 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 3011 | if (!strcasecmp(argv[0], "key")) { |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 3012 | if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { |
| 3013 | DMWARN("not suspended during key manipulation."); |
| 3014 | return -EINVAL; |
| 3015 | } |
Mike Snitzer | 498f010 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 3016 | if (argc == 3 && !strcasecmp(argv[1], "set")) { |
Ondrej Kozina | c538f6e | 2016-11-21 15:58:51 +0100 | [diff] [blame] | 3017 | /* The key size may not be changed. */ |
| 3018 | key_size = get_key_size(&argv[2]); |
| 3019 | if (key_size < 0 || cc->key_size != key_size) { |
| 3020 | memset(argv[2], '0', strlen(argv[2])); |
| 3021 | return -EINVAL; |
| 3022 | } |
| 3023 | |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 3024 | ret = crypt_set_key(cc, argv[2]); |
| 3025 | if (ret) |
| 3026 | return ret; |
| 3027 | if (cc->iv_gen_ops && cc->iv_gen_ops->init) |
| 3028 | ret = cc->iv_gen_ops->init(cc); |
Ondrej Kozina | dc94902 | 2018-01-12 16:30:32 +0100 | [diff] [blame] | 3029 | /* wipe the kernel key payload copy */ |
| 3030 | if (cc->key_string) |
| 3031 | memset(cc->key, 0, cc->key_size * sizeof(u8)); |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 3032 | return ret; |
| 3033 | } |
Mike Snitzer | 498f010 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 3034 | if (argc == 2 && !strcasecmp(argv[1], "wipe")) { |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 3035 | if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) { |
| 3036 | ret = cc->iv_gen_ops->wipe(cc); |
| 3037 | if (ret) |
| 3038 | return ret; |
| 3039 | } |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 3040 | return crypt_wipe_key(cc); |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 3041 | } |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | error: |
| 3045 | DMWARN("unrecognised message received."); |
| 3046 | return -EINVAL; |
| 3047 | } |
| 3048 | |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 3049 | static int crypt_iterate_devices(struct dm_target *ti, |
| 3050 | iterate_devices_callout_fn fn, void *data) |
| 3051 | { |
| 3052 | struct crypt_config *cc = ti->private; |
| 3053 | |
Mike Snitzer | 5dea271 | 2009-07-23 20:30:42 +0100 | [diff] [blame] | 3054 | return fn(ti, cc->dev, cc->start, ti->len, data); |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 3055 | } |
| 3056 | |
Mike Snitzer | 586b286 | 2015-09-09 21:34:51 -0400 | [diff] [blame] | 3057 | static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits) |
| 3058 | { |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 3059 | struct crypt_config *cc = ti->private; |
| 3060 | |
Mike Snitzer | 586b286 | 2015-09-09 21:34:51 -0400 | [diff] [blame] | 3061 | /* |
| 3062 | * Unfortunate constraint that is required to avoid the potential |
| 3063 | * for exceeding underlying device's max_segments limits -- due to |
| 3064 | * crypt_alloc_buffer() possibly allocating pages for the encryption |
| 3065 | * bio that are not as physically contiguous as the original bio. |
| 3066 | */ |
| 3067 | limits->max_segment_size = PAGE_SIZE; |
Milan Broz | 8f0009a | 2017-03-16 15:39:44 +0100 | [diff] [blame] | 3068 | |
Mikulas Patocka | bc9e9cf | 2018-08-10 11:23:56 -0400 | [diff] [blame] | 3069 | limits->logical_block_size = |
| 3070 | max_t(unsigned short, limits->logical_block_size, cc->sector_size); |
| 3071 | limits->physical_block_size = |
| 3072 | max_t(unsigned, limits->physical_block_size, cc->sector_size); |
| 3073 | limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size); |
Mike Snitzer | 586b286 | 2015-09-09 21:34:51 -0400 | [diff] [blame] | 3074 | } |
| 3075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | static struct target_type crypt_target = { |
| 3077 | .name = "crypt", |
Ondrej Kozina | dc94902 | 2018-01-12 16:30:32 +0100 | [diff] [blame] | 3078 | .version = {1, 18, 1}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | .module = THIS_MODULE, |
| 3080 | .ctr = crypt_ctr, |
| 3081 | .dtr = crypt_dtr, |
| 3082 | .map = crypt_map, |
| 3083 | .status = crypt_status, |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 3084 | .postsuspend = crypt_postsuspend, |
| 3085 | .preresume = crypt_preresume, |
| 3086 | .resume = crypt_resume, |
| 3087 | .message = crypt_message, |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 3088 | .iterate_devices = crypt_iterate_devices, |
Mike Snitzer | 586b286 | 2015-09-09 21:34:51 -0400 | [diff] [blame] | 3089 | .io_hints = crypt_io_hints, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | }; |
| 3091 | |
| 3092 | static int __init dm_crypt_init(void) |
| 3093 | { |
| 3094 | int r; |
| 3095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | r = dm_register_target(&crypt_target); |
Mikulas Patocka | 94f5e02 | 2015-02-13 08:25:26 -0500 | [diff] [blame] | 3097 | if (r < 0) |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 3098 | DMERR("register failed %d", r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3099 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | return r; |
| 3101 | } |
| 3102 | |
| 3103 | static void __exit dm_crypt_exit(void) |
| 3104 | { |
Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 3105 | dm_unregister_target(&crypt_target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | } |
| 3107 | |
| 3108 | module_init(dm_crypt_init); |
| 3109 | module_exit(dm_crypt_exit); |
| 3110 | |
Jana Saout | bf14299 | 2014-06-24 14:27:04 -0400 | [diff] [blame] | 3111 | MODULE_AUTHOR("Jana Saout <jana@saout.de>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption"); |
| 3113 | MODULE_LICENSE("GPL"); |