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