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