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