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