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 | 54cea3f | 2015-05-15 17:00:25 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015 Red Hat, Inc. All rights reserved. |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 5 | * Copyright (C) 2013 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> |
| 15 | #include <linux/bio.h> |
| 16 | #include <linux/blkdev.h> |
| 17 | #include <linux/mempool.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/crypto.h> |
| 20 | #include <linux/workqueue.h> |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 21 | #include <linux/kthread.h> |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 22 | #include <linux/backing-dev.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 23 | #include <linux/atomic.h> |
David Hardeman | 378f058 | 2005-09-17 17:55:31 +1000 | [diff] [blame] | 24 | #include <linux/scatterlist.h> |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 25 | #include <linux/rbtree.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <asm/page.h> |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 27 | #include <asm/unaligned.h> |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 28 | #include <crypto/hash.h> |
| 29 | #include <crypto/md5.h> |
| 30 | #include <crypto/algapi.h> |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 31 | #include <crypto/skcipher.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
Mikulas Patocka | 586e80e | 2008-10-21 17:44:59 +0100 | [diff] [blame] | 33 | #include <linux/device-mapper.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 35 | #define DM_MSG_PREFIX "crypt" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
| 37 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | * context holding the current state of a multi-part conversion |
| 39 | */ |
| 40 | struct convert_context { |
Milan Broz | 43d6903 | 2008-02-08 02:11:09 +0000 | [diff] [blame] | 41 | struct completion restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | struct bio *bio_in; |
| 43 | struct bio *bio_out; |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 44 | struct bvec_iter iter_in; |
| 45 | struct bvec_iter iter_out; |
Mikulas Patocka | c66029f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 46 | sector_t cc_sector; |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 47 | atomic_t cc_pending; |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 48 | struct skcipher_request *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 51 | /* |
| 52 | * per bio private data |
| 53 | */ |
| 54 | struct dm_crypt_io { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 55 | struct crypt_config *cc; |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 56 | struct bio *base_bio; |
| 57 | struct work_struct work; |
| 58 | |
| 59 | struct convert_context ctx; |
| 60 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 61 | atomic_t io_pending; |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 62 | int error; |
Milan Broz | 0c395b0 | 2008-02-08 02:10:54 +0000 | [diff] [blame] | 63 | sector_t sector; |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 64 | |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 65 | struct rb_node rb_node; |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 66 | } CRYPTO_MINALIGN_ATTR; |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 67 | |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 68 | struct dm_crypt_request { |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 69 | struct convert_context *ctx; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 70 | struct scatterlist sg_in; |
| 71 | struct scatterlist sg_out; |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 72 | sector_t iv_sector; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | struct crypt_config; |
| 76 | |
| 77 | struct crypt_iv_operations { |
| 78 | int (*ctr)(struct crypt_config *cc, struct dm_target *ti, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 79 | const char *opts); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | void (*dtr)(struct crypt_config *cc); |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 81 | int (*init)(struct crypt_config *cc); |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 82 | int (*wipe)(struct crypt_config *cc); |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 83 | int (*generator)(struct crypt_config *cc, u8 *iv, |
| 84 | struct dm_crypt_request *dmreq); |
| 85 | int (*post)(struct crypt_config *cc, u8 *iv, |
| 86 | struct dm_crypt_request *dmreq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 89 | struct iv_essiv_private { |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 90 | struct crypto_ahash *hash_tfm; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 91 | u8 *salt; |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | struct iv_benbi_private { |
| 95 | int shift; |
| 96 | }; |
| 97 | |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 98 | #define LMK_SEED_SIZE 64 /* hash + 0 */ |
| 99 | struct iv_lmk_private { |
| 100 | struct crypto_shash *hash_tfm; |
| 101 | u8 *seed; |
| 102 | }; |
| 103 | |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 104 | #define TCW_WHITENING_SIZE 16 |
| 105 | struct iv_tcw_private { |
| 106 | struct crypto_shash *crc32_tfm; |
| 107 | u8 *iv_seed; |
| 108 | u8 *whitening; |
| 109 | }; |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | /* |
| 112 | * Crypt: maps a linear range of a block device |
| 113 | * and encrypts / decrypts at the same time. |
| 114 | */ |
Mikulas Patocka | 0f5d8e6 | 2015-02-13 08:27:08 -0500 | [diff] [blame] | 115 | enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID, |
Rabin Vincent | f659b10 | 2016-09-21 16:22:29 +0200 | [diff] [blame] | 116 | DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD }; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 117 | |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 118 | enum cipher_flags { |
| 119 | CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */ |
| 120 | }; |
| 121 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 122 | /* |
Mikulas Patocka | 610f2de | 2014-02-20 18:01:01 -0500 | [diff] [blame] | 123 | * The fields in here must be read only after initialization. |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 124 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | struct crypt_config { |
| 126 | struct dm_dev *dev; |
| 127 | sector_t start; |
| 128 | |
| 129 | /* |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 130 | * pool for per bio private data, crypto requests and |
| 131 | * encryption requeusts/buffer pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | */ |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 133 | mempool_t *req_pool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | mempool_t *page_pool; |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 135 | struct bio_set *bs; |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 136 | struct mutex bio_alloc_lock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 138 | struct workqueue_struct *io_queue; |
| 139 | struct workqueue_struct *crypt_queue; |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 140 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 141 | struct task_struct *write_thread; |
| 142 | wait_queue_head_t write_thread_wait; |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 143 | struct rb_root write_tree; |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 144 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 145 | char *cipher; |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 146 | char *cipher_string; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | struct crypt_iv_operations *iv_gen_ops; |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 149 | union { |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 150 | struct iv_essiv_private essiv; |
| 151 | struct iv_benbi_private benbi; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 152 | struct iv_lmk_private lmk; |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 153 | struct iv_tcw_private tcw; |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 154 | } iv_gen_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | sector_t iv_offset; |
| 156 | unsigned int iv_size; |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 157 | unsigned int sector_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 159 | /* ESSIV: struct crypto_cipher *essiv_tfm */ |
| 160 | void *iv_private; |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 161 | struct crypto_skcipher **tfms; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 162 | unsigned tfms_count; |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 163 | unsigned long cipher_flags; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 164 | |
| 165 | /* |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 166 | * Layout of each crypto request: |
| 167 | * |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 168 | * struct skcipher_request |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 169 | * context |
| 170 | * padding |
| 171 | * struct dm_crypt_request |
| 172 | * padding |
| 173 | * IV |
| 174 | * |
| 175 | * The padding is added so that dm_crypt_request and the IV are |
| 176 | * correctly aligned. |
| 177 | */ |
| 178 | unsigned int dmreq_start; |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 179 | |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 180 | unsigned int per_bio_data_size; |
| 181 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 182 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | unsigned int key_size; |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 184 | unsigned int key_parts; /* independent parts in key buffer */ |
| 185 | unsigned int key_extra_size; /* additional keys length */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | u8 key[0]; |
| 187 | }; |
| 188 | |
Mikulas Patocka | 0a83df6 | 2016-07-15 17:30:20 -0400 | [diff] [blame] | 189 | #define MIN_IOS 64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 191 | static void clone_init(struct dm_crypt_io *, struct bio *); |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 192 | static void kcryptd_queue_crypt(struct dm_crypt_io *io); |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 193 | static u8 *iv_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq); |
Olaf Kirch | 027581f | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 194 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 195 | /* |
| 196 | * Use this to access cipher attributes that are the same for each CPU. |
| 197 | */ |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 198 | static struct crypto_skcipher *any_tfm(struct crypt_config *cc) |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 199 | { |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 200 | return cc->tfms[0]; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | * Different IV generation algorithms: |
| 205 | * |
Rik Snel | 3c164bd | 2006-09-02 18:17:33 +1000 | [diff] [blame] | 206 | * 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] | 207 | * number, padded with zeros if necessary. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | * |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 209 | * plain64: the initial vector is the 64-bit little-endian version of the sector |
| 210 | * number, padded with zeros if necessary. |
| 211 | * |
Rik Snel | 3c164bd | 2006-09-02 18:17:33 +1000 | [diff] [blame] | 212 | * essiv: "encrypted sector|salt initial vector", the sector number is |
| 213 | * encrypted with the bulk cipher using a salt as key. The salt |
| 214 | * should be derived from the bulk cipher's key via hashing. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | * |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 216 | * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1 |
| 217 | * (needed for LRW-32-AES and possible other narrow block modes) |
| 218 | * |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 219 | * null: the initial vector is always zero. Provides compatibility with |
| 220 | * obsolete loop_fish2 devices. Do not use for new devices. |
| 221 | * |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 222 | * lmk: Compatible implementation of the block chaining mode used |
| 223 | * by the Loop-AES block device encryption system |
| 224 | * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/ |
| 225 | * It operates on full 512 byte sectors and uses CBC |
| 226 | * with an IV derived from the sector number, the data and |
| 227 | * optionally extra IV seed. |
| 228 | * This means that after decryption the first block |
| 229 | * of sector must be tweaked according to decrypted data. |
| 230 | * Loop-AES can use three encryption schemes: |
| 231 | * version 1: is plain aes-cbc mode |
| 232 | * version 2: uses 64 multikey scheme with lmk IV generator |
| 233 | * version 3: the same as version 2 with additional IV seed |
| 234 | * (it uses 65 keys, last key is used as IV seed) |
| 235 | * |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 236 | * tcw: Compatible implementation of the block chaining mode used |
| 237 | * by the TrueCrypt device encryption system (prior to version 4.1). |
Milan Broz | e44f23b | 2015-04-05 18:03:10 +0200 | [diff] [blame] | 238 | * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 239 | * It operates on full 512 byte sectors and uses CBC |
| 240 | * with an IV derived from initial key and the sector number. |
| 241 | * In addition, whitening value is applied on every sector, whitening |
| 242 | * is calculated from initial key, sector number and mixed using CRC32. |
| 243 | * Note that this encryption scheme is vulnerable to watermarking attacks |
| 244 | * and should be used for old compatible containers access only. |
| 245 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | * plumb: unimplemented, see: |
| 247 | * http://article.gmane.org/gmane.linux.kernel.device-mapper.dm-crypt/454 |
| 248 | */ |
| 249 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 250 | static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, |
| 251 | struct dm_crypt_request *dmreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | { |
| 253 | memset(iv, 0, cc->iv_size); |
Alasdair G Kergon | 283a832 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 254 | *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 259 | static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv, |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 260 | struct dm_crypt_request *dmreq) |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 261 | { |
| 262 | memset(iv, 0, cc->iv_size); |
Alasdair G Kergon | 283a832 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 263 | *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector); |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 268 | /* Initialise ESSIV - compute salt but no local memory allocations */ |
| 269 | static int crypt_iv_essiv_init(struct crypt_config *cc) |
| 270 | { |
| 271 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 272 | AHASH_REQUEST_ON_STACK(req, essiv->hash_tfm); |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 273 | struct scatterlist sg; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 274 | struct crypto_cipher *essiv_tfm; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 275 | int err; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 276 | |
| 277 | sg_init_one(&sg, cc->key, cc->key_size); |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 278 | ahash_request_set_tfm(req, essiv->hash_tfm); |
| 279 | ahash_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); |
| 280 | ahash_request_set_crypt(req, &sg, essiv->salt, cc->key_size); |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 281 | |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 282 | err = crypto_ahash_digest(req); |
| 283 | ahash_request_zero(req); |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 284 | if (err) |
| 285 | return err; |
| 286 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 287 | essiv_tfm = cc->iv_private; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 288 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 289 | err = crypto_cipher_setkey(essiv_tfm, essiv->salt, |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 290 | crypto_ahash_digestsize(essiv->hash_tfm)); |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 291 | if (err) |
| 292 | return err; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 293 | |
| 294 | return 0; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 297 | /* Wipe salt and reset key derived from volume key */ |
| 298 | static int crypt_iv_essiv_wipe(struct crypt_config *cc) |
| 299 | { |
| 300 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 301 | unsigned salt_size = crypto_ahash_digestsize(essiv->hash_tfm); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 302 | struct crypto_cipher *essiv_tfm; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 303 | int r, err = 0; |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 304 | |
| 305 | memset(essiv->salt, 0, salt_size); |
| 306 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 307 | essiv_tfm = cc->iv_private; |
| 308 | r = crypto_cipher_setkey(essiv_tfm, essiv->salt, salt_size); |
| 309 | if (r) |
| 310 | err = r; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 311 | |
| 312 | return err; |
| 313 | } |
| 314 | |
| 315 | /* Set up per cpu cipher state */ |
| 316 | static struct crypto_cipher *setup_essiv_cpu(struct crypt_config *cc, |
| 317 | struct dm_target *ti, |
| 318 | u8 *salt, unsigned saltsize) |
| 319 | { |
| 320 | struct crypto_cipher *essiv_tfm; |
| 321 | int err; |
| 322 | |
| 323 | /* Setup the essiv_tfm with the given salt */ |
| 324 | essiv_tfm = crypto_alloc_cipher(cc->cipher, 0, CRYPTO_ALG_ASYNC); |
| 325 | if (IS_ERR(essiv_tfm)) { |
| 326 | ti->error = "Error allocating crypto tfm for ESSIV"; |
| 327 | return essiv_tfm; |
| 328 | } |
| 329 | |
| 330 | if (crypto_cipher_blocksize(essiv_tfm) != |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 331 | crypto_skcipher_ivsize(any_tfm(cc))) { |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 332 | ti->error = "Block size of ESSIV cipher does " |
| 333 | "not match IV size of block cipher"; |
| 334 | crypto_free_cipher(essiv_tfm); |
| 335 | return ERR_PTR(-EINVAL); |
| 336 | } |
| 337 | |
| 338 | err = crypto_cipher_setkey(essiv_tfm, salt, saltsize); |
| 339 | if (err) { |
| 340 | ti->error = "Failed to set key for ESSIV cipher"; |
| 341 | crypto_free_cipher(essiv_tfm); |
| 342 | return ERR_PTR(err); |
| 343 | } |
| 344 | |
| 345 | return essiv_tfm; |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 348 | static void crypt_iv_essiv_dtr(struct crypt_config *cc) |
| 349 | { |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 350 | struct crypto_cipher *essiv_tfm; |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 351 | struct iv_essiv_private *essiv = &cc->iv_gen_private.essiv; |
| 352 | |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 353 | crypto_free_ahash(essiv->hash_tfm); |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 354 | essiv->hash_tfm = NULL; |
| 355 | |
| 356 | kzfree(essiv->salt); |
| 357 | essiv->salt = NULL; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 358 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 359 | essiv_tfm = cc->iv_private; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 360 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 361 | if (essiv_tfm) |
| 362 | crypto_free_cipher(essiv_tfm); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 363 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 364 | cc->iv_private = NULL; |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | 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] | 368 | const char *opts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | { |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 370 | struct crypto_cipher *essiv_tfm = NULL; |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 371 | struct crypto_ahash *hash_tfm = NULL; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 372 | u8 *salt = NULL; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 373 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 375 | if (!opts) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 376 | ti->error = "Digest algorithm missing for ESSIV mode"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | return -EINVAL; |
| 378 | } |
| 379 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 380 | /* Allocate hash algorithm */ |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 381 | hash_tfm = crypto_alloc_ahash(opts, 0, CRYPTO_ALG_ASYNC); |
Herbert Xu | 3505868 | 2006-08-24 19:10:20 +1000 | [diff] [blame] | 382 | if (IS_ERR(hash_tfm)) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 383 | ti->error = "Error initializing ESSIV hash"; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 384 | err = PTR_ERR(hash_tfm); |
| 385 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 388 | salt = kzalloc(crypto_ahash_digestsize(hash_tfm), GFP_KERNEL); |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 389 | if (!salt) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 390 | ti->error = "Error kmallocing salt storage in ESSIV"; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 391 | err = -ENOMEM; |
| 392 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 395 | cc->iv_gen_private.essiv.salt = salt; |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 396 | cc->iv_gen_private.essiv.hash_tfm = hash_tfm; |
| 397 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 398 | essiv_tfm = setup_essiv_cpu(cc, ti, salt, |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 399 | crypto_ahash_digestsize(hash_tfm)); |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 400 | if (IS_ERR(essiv_tfm)) { |
| 401 | crypt_iv_essiv_dtr(cc); |
| 402 | return PTR_ERR(essiv_tfm); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 403 | } |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 404 | cc->iv_private = essiv_tfm; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 405 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | return 0; |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 407 | |
| 408 | bad: |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 409 | if (hash_tfm && !IS_ERR(hash_tfm)) |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 410 | crypto_free_ahash(hash_tfm); |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 411 | kfree(salt); |
Milan Broz | 5861f1b | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 412 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 415 | static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, |
| 416 | struct dm_crypt_request *dmreq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | { |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 418 | struct crypto_cipher *essiv_tfm = cc->iv_private; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | memset(iv, 0, cc->iv_size); |
Alasdair G Kergon | 283a832 | 2011-08-02 12:32:01 +0100 | [diff] [blame] | 421 | *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 422 | crypto_cipher_encrypt_one(essiv_tfm, iv, iv); |
| 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | return 0; |
| 425 | } |
| 426 | |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 427 | static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, |
| 428 | const char *opts) |
| 429 | { |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 430 | unsigned bs = crypto_skcipher_blocksize(any_tfm(cc)); |
David Howells | f0d1b0b | 2006-12-08 02:37:49 -0800 | [diff] [blame] | 431 | int log = ilog2(bs); |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 432 | |
| 433 | /* we need to calculate how far we must shift the sector count |
| 434 | * to get the cipher block count, we use this shift in _gen */ |
| 435 | |
| 436 | if (1 << log != bs) { |
| 437 | ti->error = "cypher blocksize is not a power of 2"; |
| 438 | return -EINVAL; |
| 439 | } |
| 440 | |
| 441 | if (log > 9) { |
| 442 | ti->error = "cypher blocksize is > 512"; |
| 443 | return -EINVAL; |
| 444 | } |
| 445 | |
Milan Broz | 6047359 | 2009-12-10 23:51:55 +0000 | [diff] [blame] | 446 | cc->iv_gen_private.benbi.shift = 9 - log; |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | static void crypt_iv_benbi_dtr(struct crypt_config *cc) |
| 452 | { |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 453 | } |
| 454 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 455 | static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, |
| 456 | struct dm_crypt_request *dmreq) |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 457 | { |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 458 | __be64 val; |
| 459 | |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 460 | memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ |
Herbert Xu | 79066ad | 2006-12-05 13:41:52 -0800 | [diff] [blame] | 461 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 462 | 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] | 463 | put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 468 | static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, |
| 469 | struct dm_crypt_request *dmreq) |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 470 | { |
| 471 | memset(iv, 0, cc->iv_size); |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 476 | static void crypt_iv_lmk_dtr(struct crypt_config *cc) |
| 477 | { |
| 478 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 479 | |
| 480 | if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm)) |
| 481 | crypto_free_shash(lmk->hash_tfm); |
| 482 | lmk->hash_tfm = NULL; |
| 483 | |
| 484 | kzfree(lmk->seed); |
| 485 | lmk->seed = NULL; |
| 486 | } |
| 487 | |
| 488 | static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti, |
| 489 | const char *opts) |
| 490 | { |
| 491 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 492 | |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 493 | if (cc->sector_size != (1 << SECTOR_SHIFT)) { |
| 494 | ti->error = "Unsupported sector size for LMK"; |
| 495 | return -EINVAL; |
| 496 | } |
| 497 | |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 498 | lmk->hash_tfm = crypto_alloc_shash("md5", 0, 0); |
| 499 | if (IS_ERR(lmk->hash_tfm)) { |
| 500 | ti->error = "Error initializing LMK hash"; |
| 501 | return PTR_ERR(lmk->hash_tfm); |
| 502 | } |
| 503 | |
| 504 | /* No seed in LMK version 2 */ |
| 505 | if (cc->key_parts == cc->tfms_count) { |
| 506 | lmk->seed = NULL; |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL); |
| 511 | if (!lmk->seed) { |
| 512 | crypt_iv_lmk_dtr(cc); |
| 513 | ti->error = "Error kmallocing seed storage in LMK"; |
| 514 | return -ENOMEM; |
| 515 | } |
| 516 | |
| 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | static int crypt_iv_lmk_init(struct crypt_config *cc) |
| 521 | { |
| 522 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 523 | int subkey_size = cc->key_size / cc->key_parts; |
| 524 | |
| 525 | /* LMK seed is on the position of LMK_KEYS + 1 key */ |
| 526 | if (lmk->seed) |
| 527 | memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size), |
| 528 | crypto_shash_digestsize(lmk->hash_tfm)); |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static int crypt_iv_lmk_wipe(struct crypt_config *cc) |
| 534 | { |
| 535 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
| 536 | |
| 537 | if (lmk->seed) |
| 538 | memset(lmk->seed, 0, LMK_SEED_SIZE); |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv, |
| 544 | struct dm_crypt_request *dmreq, |
| 545 | u8 *data) |
| 546 | { |
| 547 | struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 548 | SHASH_DESC_ON_STACK(desc, lmk->hash_tfm); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 549 | struct md5_state md5state; |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 550 | __le32 buf[4]; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 551 | int i, r; |
| 552 | |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 553 | desc->tfm = lmk->hash_tfm; |
| 554 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 555 | |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 556 | r = crypto_shash_init(desc); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 557 | if (r) |
| 558 | return r; |
| 559 | |
| 560 | if (lmk->seed) { |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 561 | r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 562 | if (r) |
| 563 | return r; |
| 564 | } |
| 565 | |
| 566 | /* 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] | 567 | r = crypto_shash_update(desc, data + 16, 16 * 31); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 568 | if (r) |
| 569 | return r; |
| 570 | |
| 571 | /* Sector is cropped to 56 bits here */ |
| 572 | buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF); |
| 573 | buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000); |
| 574 | buf[2] = cpu_to_le32(4024); |
| 575 | buf[3] = 0; |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 576 | r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf)); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 577 | if (r) |
| 578 | return r; |
| 579 | |
| 580 | /* No MD5 padding here */ |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 581 | r = crypto_shash_export(desc, &md5state); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 582 | if (r) |
| 583 | return r; |
| 584 | |
| 585 | for (i = 0; i < MD5_HASH_WORDS; i++) |
| 586 | __cpu_to_le32s(&md5state.hash[i]); |
| 587 | memcpy(iv, &md5state.hash, cc->iv_size); |
| 588 | |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv, |
| 593 | struct dm_crypt_request *dmreq) |
| 594 | { |
| 595 | u8 *src; |
| 596 | int r = 0; |
| 597 | |
| 598 | if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) { |
Cong Wang | c2e022c | 2011-11-28 13:26:02 +0800 | [diff] [blame] | 599 | src = kmap_atomic(sg_page(&dmreq->sg_in)); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 600 | r = crypt_iv_lmk_one(cc, iv, dmreq, src + dmreq->sg_in.offset); |
Cong Wang | c2e022c | 2011-11-28 13:26:02 +0800 | [diff] [blame] | 601 | kunmap_atomic(src); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 602 | } else |
| 603 | memset(iv, 0, cc->iv_size); |
| 604 | |
| 605 | return r; |
| 606 | } |
| 607 | |
| 608 | static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv, |
| 609 | struct dm_crypt_request *dmreq) |
| 610 | { |
| 611 | u8 *dst; |
| 612 | int r; |
| 613 | |
| 614 | if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) |
| 615 | return 0; |
| 616 | |
Cong Wang | c2e022c | 2011-11-28 13:26:02 +0800 | [diff] [blame] | 617 | dst = kmap_atomic(sg_page(&dmreq->sg_out)); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 618 | r = crypt_iv_lmk_one(cc, iv, dmreq, dst + dmreq->sg_out.offset); |
| 619 | |
| 620 | /* Tweak the first block of plaintext sector */ |
| 621 | if (!r) |
| 622 | crypto_xor(dst + dmreq->sg_out.offset, iv, cc->iv_size); |
| 623 | |
Cong Wang | c2e022c | 2011-11-28 13:26:02 +0800 | [diff] [blame] | 624 | kunmap_atomic(dst); |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 625 | return r; |
| 626 | } |
| 627 | |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 628 | static void crypt_iv_tcw_dtr(struct crypt_config *cc) |
| 629 | { |
| 630 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
| 631 | |
| 632 | kzfree(tcw->iv_seed); |
| 633 | tcw->iv_seed = NULL; |
| 634 | kzfree(tcw->whitening); |
| 635 | tcw->whitening = NULL; |
| 636 | |
| 637 | if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm)) |
| 638 | crypto_free_shash(tcw->crc32_tfm); |
| 639 | tcw->crc32_tfm = NULL; |
| 640 | } |
| 641 | |
| 642 | static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti, |
| 643 | const char *opts) |
| 644 | { |
| 645 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
| 646 | |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 647 | if (cc->sector_size != (1 << SECTOR_SHIFT)) { |
| 648 | ti->error = "Unsupported sector size for TCW"; |
| 649 | return -EINVAL; |
| 650 | } |
| 651 | |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 652 | if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) { |
| 653 | ti->error = "Wrong key size for TCW"; |
| 654 | return -EINVAL; |
| 655 | } |
| 656 | |
| 657 | tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 0); |
| 658 | if (IS_ERR(tcw->crc32_tfm)) { |
| 659 | ti->error = "Error initializing CRC32 in TCW"; |
| 660 | return PTR_ERR(tcw->crc32_tfm); |
| 661 | } |
| 662 | |
| 663 | tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL); |
| 664 | tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL); |
| 665 | if (!tcw->iv_seed || !tcw->whitening) { |
| 666 | crypt_iv_tcw_dtr(cc); |
| 667 | ti->error = "Error allocating seed storage in TCW"; |
| 668 | return -ENOMEM; |
| 669 | } |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | static int crypt_iv_tcw_init(struct crypt_config *cc) |
| 675 | { |
| 676 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
| 677 | int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE; |
| 678 | |
| 679 | memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size); |
| 680 | memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size], |
| 681 | TCW_WHITENING_SIZE); |
| 682 | |
| 683 | return 0; |
| 684 | } |
| 685 | |
| 686 | static int crypt_iv_tcw_wipe(struct crypt_config *cc) |
| 687 | { |
| 688 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
| 689 | |
| 690 | memset(tcw->iv_seed, 0, cc->iv_size); |
| 691 | memset(tcw->whitening, 0, TCW_WHITENING_SIZE); |
| 692 | |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | static int crypt_iv_tcw_whitening(struct crypt_config *cc, |
| 697 | struct dm_crypt_request *dmreq, |
| 698 | u8 *data) |
| 699 | { |
| 700 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
Bart Van Assche | 350b539 | 2016-06-28 16:32:32 +0200 | [diff] [blame] | 701 | __le64 sector = cpu_to_le64(dmreq->iv_sector); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 702 | u8 buf[TCW_WHITENING_SIZE]; |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 703 | SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 704 | int i, r; |
| 705 | |
| 706 | /* xor whitening with sector number */ |
| 707 | memcpy(buf, tcw->whitening, TCW_WHITENING_SIZE); |
| 708 | crypto_xor(buf, (u8 *)§or, 8); |
| 709 | crypto_xor(&buf[8], (u8 *)§or, 8); |
| 710 | |
| 711 | /* calculate crc32 for every 32bit part and xor it */ |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 712 | desc->tfm = tcw->crc32_tfm; |
| 713 | desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 714 | for (i = 0; i < 4; i++) { |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 715 | r = crypto_shash_init(desc); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 716 | if (r) |
| 717 | goto out; |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 718 | r = crypto_shash_update(desc, &buf[i * 4], 4); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 719 | if (r) |
| 720 | goto out; |
Jan-Simon Möller | b610626 | 2012-07-02 13:50:54 +0200 | [diff] [blame] | 721 | r = crypto_shash_final(desc, &buf[i * 4]); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 722 | if (r) |
| 723 | goto out; |
| 724 | } |
| 725 | crypto_xor(&buf[0], &buf[12], 4); |
| 726 | crypto_xor(&buf[4], &buf[8], 4); |
| 727 | |
| 728 | /* apply whitening (8 bytes) to whole sector */ |
| 729 | for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++) |
| 730 | crypto_xor(data + i * 8, buf, 8); |
| 731 | out: |
Milan Broz | 1a71d6f | 2014-11-22 09:36:04 +0100 | [diff] [blame] | 732 | memzero_explicit(buf, sizeof(buf)); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 733 | return r; |
| 734 | } |
| 735 | |
| 736 | static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv, |
| 737 | struct dm_crypt_request *dmreq) |
| 738 | { |
| 739 | struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; |
Bart Van Assche | 350b539 | 2016-06-28 16:32:32 +0200 | [diff] [blame] | 740 | __le64 sector = cpu_to_le64(dmreq->iv_sector); |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 741 | u8 *src; |
| 742 | int r = 0; |
| 743 | |
| 744 | /* Remove whitening from ciphertext */ |
| 745 | if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) { |
| 746 | src = kmap_atomic(sg_page(&dmreq->sg_in)); |
| 747 | r = crypt_iv_tcw_whitening(cc, dmreq, src + dmreq->sg_in.offset); |
| 748 | kunmap_atomic(src); |
| 749 | } |
| 750 | |
| 751 | /* Calculate IV */ |
| 752 | memcpy(iv, tcw->iv_seed, cc->iv_size); |
| 753 | crypto_xor(iv, (u8 *)§or, 8); |
| 754 | if (cc->iv_size > 8) |
| 755 | crypto_xor(&iv[8], (u8 *)§or, cc->iv_size - 8); |
| 756 | |
| 757 | return r; |
| 758 | } |
| 759 | |
| 760 | static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv, |
| 761 | struct dm_crypt_request *dmreq) |
| 762 | { |
| 763 | u8 *dst; |
| 764 | int r; |
| 765 | |
| 766 | if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) |
| 767 | return 0; |
| 768 | |
| 769 | /* Apply whitening on ciphertext */ |
| 770 | dst = kmap_atomic(sg_page(&dmreq->sg_out)); |
| 771 | r = crypt_iv_tcw_whitening(cc, dmreq, dst + dmreq->sg_out.offset); |
| 772 | kunmap_atomic(dst); |
| 773 | |
| 774 | return r; |
| 775 | } |
| 776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | static struct crypt_iv_operations crypt_iv_plain_ops = { |
| 778 | .generator = crypt_iv_plain_gen |
| 779 | }; |
| 780 | |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 781 | static struct crypt_iv_operations crypt_iv_plain64_ops = { |
| 782 | .generator = crypt_iv_plain64_gen |
| 783 | }; |
| 784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | static struct crypt_iv_operations crypt_iv_essiv_ops = { |
| 786 | .ctr = crypt_iv_essiv_ctr, |
| 787 | .dtr = crypt_iv_essiv_dtr, |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 788 | .init = crypt_iv_essiv_init, |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 789 | .wipe = crypt_iv_essiv_wipe, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | .generator = crypt_iv_essiv_gen |
| 791 | }; |
| 792 | |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 793 | static struct crypt_iv_operations crypt_iv_benbi_ops = { |
| 794 | .ctr = crypt_iv_benbi_ctr, |
| 795 | .dtr = crypt_iv_benbi_dtr, |
| 796 | .generator = crypt_iv_benbi_gen |
| 797 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 799 | static struct crypt_iv_operations crypt_iv_null_ops = { |
| 800 | .generator = crypt_iv_null_gen |
| 801 | }; |
| 802 | |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 803 | static struct crypt_iv_operations crypt_iv_lmk_ops = { |
| 804 | .ctr = crypt_iv_lmk_ctr, |
| 805 | .dtr = crypt_iv_lmk_dtr, |
| 806 | .init = crypt_iv_lmk_init, |
| 807 | .wipe = crypt_iv_lmk_wipe, |
| 808 | .generator = crypt_iv_lmk_gen, |
| 809 | .post = crypt_iv_lmk_post |
| 810 | }; |
| 811 | |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 812 | static struct crypt_iv_operations crypt_iv_tcw_ops = { |
| 813 | .ctr = crypt_iv_tcw_ctr, |
| 814 | .dtr = crypt_iv_tcw_dtr, |
| 815 | .init = crypt_iv_tcw_init, |
| 816 | .wipe = crypt_iv_tcw_wipe, |
| 817 | .generator = crypt_iv_tcw_gen, |
| 818 | .post = crypt_iv_tcw_post |
| 819 | }; |
| 820 | |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 821 | static void crypt_convert_init(struct crypt_config *cc, |
| 822 | struct convert_context *ctx, |
| 823 | struct bio *bio_out, struct bio *bio_in, |
Milan Broz | fcd369d | 2008-02-08 02:10:41 +0000 | [diff] [blame] | 824 | sector_t sector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | { |
| 826 | ctx->bio_in = bio_in; |
| 827 | ctx->bio_out = bio_out; |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 828 | if (bio_in) |
| 829 | ctx->iter_in = bio_in->bi_iter; |
| 830 | if (bio_out) |
| 831 | ctx->iter_out = bio_out->bi_iter; |
Mikulas Patocka | c66029f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 832 | ctx->cc_sector = sector + cc->iv_offset; |
Milan Broz | 43d6903 | 2008-02-08 02:11:09 +0000 | [diff] [blame] | 833 | init_completion(&ctx->restart); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 836 | static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc, |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 837 | struct skcipher_request *req) |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 838 | { |
| 839 | return (struct dm_crypt_request *)((char *)req + cc->dmreq_start); |
| 840 | } |
| 841 | |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 842 | static struct skcipher_request *req_of_dmreq(struct crypt_config *cc, |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 843 | struct dm_crypt_request *dmreq) |
| 844 | { |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 845 | return (struct skcipher_request *)((char *)dmreq - cc->dmreq_start); |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 848 | static u8 *iv_of_dmreq(struct crypt_config *cc, |
| 849 | struct dm_crypt_request *dmreq) |
| 850 | { |
| 851 | return (u8 *)ALIGN((unsigned long)(dmreq + 1), |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 852 | crypto_skcipher_alignmask(any_tfm(cc)) + 1); |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 855 | static int crypt_convert_block(struct crypt_config *cc, |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 856 | struct convert_context *ctx, |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 857 | struct skcipher_request *req) |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 858 | { |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 859 | struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in); |
| 860 | 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] | 861 | struct dm_crypt_request *dmreq; |
| 862 | u8 *iv; |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 863 | int r; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 864 | |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 865 | /* Reject unexpected unaligned bio. */ |
| 866 | if (unlikely(bv_in.bv_offset & (cc->sector_size - 1))) |
| 867 | return -EIO; |
| 868 | |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 869 | dmreq = dmreq_of_req(cc, req); |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 870 | iv = iv_of_dmreq(cc, dmreq); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 871 | |
Mikulas Patocka | c66029f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 872 | dmreq->iv_sector = ctx->cc_sector; |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 873 | if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) |
| 874 | sector_div(dmreq->iv_sector, cc->sector_size >> SECTOR_SHIFT); |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 875 | dmreq->ctx = ctx; |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 876 | sg_init_table(&dmreq->sg_in, 1); |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 877 | sg_set_page(&dmreq->sg_in, bv_in.bv_page, cc->sector_size, |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 878 | bv_in.bv_offset); |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 879 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 880 | sg_init_table(&dmreq->sg_out, 1); |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 881 | sg_set_page(&dmreq->sg_out, bv_out.bv_page, cc->sector_size, |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 882 | bv_out.bv_offset); |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 883 | |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 884 | bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); |
| 885 | bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 886 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 887 | if (cc->iv_gen_ops) { |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 888 | r = cc->iv_gen_ops->generator(cc, iv, dmreq); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 889 | if (r < 0) |
| 890 | return r; |
| 891 | } |
| 892 | |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 893 | skcipher_request_set_crypt(req, &dmreq->sg_in, &dmreq->sg_out, |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 894 | cc->sector_size, iv); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 895 | |
| 896 | if (bio_data_dir(ctx->bio_in) == WRITE) |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 897 | r = crypto_skcipher_encrypt(req); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 898 | else |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 899 | r = crypto_skcipher_decrypt(req); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 900 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 901 | if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) |
| 902 | r = cc->iv_gen_ops->post(cc, iv, dmreq); |
| 903 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 904 | return r; |
Milan Broz | 01482b7 | 2008-02-08 02:11:04 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 907 | static void kcryptd_async_done(struct crypto_async_request *async_req, |
| 908 | int error); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 909 | |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 910 | static void crypt_alloc_req(struct crypt_config *cc, |
| 911 | struct convert_context *ctx) |
| 912 | { |
Mikulas Patocka | c66029f | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 913 | unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 914 | |
Mikulas Patocka | 610f2de | 2014-02-20 18:01:01 -0500 | [diff] [blame] | 915 | if (!ctx->req) |
| 916 | ctx->req = mempool_alloc(cc->req_pool, GFP_NOIO); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 917 | |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 918 | skcipher_request_set_tfm(ctx->req, cc->tfms[key_index]); |
Milan Broz | 54cea3f | 2015-05-15 17:00:25 +0200 | [diff] [blame] | 919 | |
| 920 | /* |
| 921 | * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs |
| 922 | * requests if driver request queue is full. |
| 923 | */ |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 924 | skcipher_request_set_callback(ctx->req, |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 925 | CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP, |
Mikulas Patocka | 610f2de | 2014-02-20 18:01:01 -0500 | [diff] [blame] | 926 | kcryptd_async_done, dmreq_of_req(cc, ctx->req)); |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 929 | static void crypt_free_req(struct crypt_config *cc, |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 930 | struct skcipher_request *req, struct bio *base_bio) |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 931 | { |
| 932 | struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); |
| 933 | |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 934 | if ((struct skcipher_request *)(io + 1) != req) |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 935 | mempool_free(req, cc->req_pool); |
| 936 | } |
| 937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | /* |
| 939 | * Encrypt / decrypt data from one bio to another one (can be the same one) |
| 940 | */ |
| 941 | static int crypt_convert(struct crypt_config *cc, |
Milan Broz | d469f84 | 2007-10-19 22:42:37 +0100 | [diff] [blame] | 942 | struct convert_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | { |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 944 | unsigned int sector_step = cc->sector_size / (1 << SECTOR_SHIFT); |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 945 | int r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 947 | atomic_set(&ctx->cc_pending, 1); |
Milan Broz | c808161 | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 948 | |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 949 | while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 951 | crypt_alloc_req(cc, ctx); |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 952 | atomic_inc(&ctx->cc_pending); |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 953 | |
Mikulas Patocka | 610f2de | 2014-02-20 18:01:01 -0500 | [diff] [blame] | 954 | r = crypt_convert_block(cc, ctx, ctx->req); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 955 | |
| 956 | switch (r) { |
Milan Broz | 54cea3f | 2015-05-15 17:00:25 +0200 | [diff] [blame] | 957 | /* |
| 958 | * The request was queued by a crypto driver |
| 959 | * but the driver request queue is full, let's wait. |
| 960 | */ |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 961 | case -EBUSY: |
| 962 | wait_for_completion(&ctx->restart); |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 963 | reinit_completion(&ctx->restart); |
Milan Broz | 54cea3f | 2015-05-15 17:00:25 +0200 | [diff] [blame] | 964 | /* fall through */ |
| 965 | /* |
| 966 | * The request is queued and processed asynchronously, |
| 967 | * completion function kcryptd_async_done() will be called. |
| 968 | */ |
Rabin Vincent | c0403ec | 2015-05-05 15:15:56 +0200 | [diff] [blame] | 969 | case -EINPROGRESS: |
Mikulas Patocka | 610f2de | 2014-02-20 18:01:01 -0500 | [diff] [blame] | 970 | ctx->req = NULL; |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 971 | ctx->cc_sector += sector_step; |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 972 | continue; |
Milan Broz | 54cea3f | 2015-05-15 17:00:25 +0200 | [diff] [blame] | 973 | /* |
| 974 | * The request was already processed (synchronously). |
| 975 | */ |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 976 | case 0: |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 977 | atomic_dec(&ctx->cc_pending); |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 978 | ctx->cc_sector += sector_step; |
Milan Broz | c7f1b20 | 2008-07-02 09:34:28 +0100 | [diff] [blame] | 979 | cond_resched(); |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 980 | continue; |
| 981 | |
Milan Broz | 54cea3f | 2015-05-15 17:00:25 +0200 | [diff] [blame] | 982 | /* There was an error while processing the request. */ |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 983 | default: |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 984 | atomic_dec(&ctx->cc_pending); |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 985 | return r; |
| 986 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | } |
| 988 | |
Milan Broz | 3f1e907 | 2008-03-28 14:16:07 -0700 | [diff] [blame] | 989 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Mikulas Patocka | cf2f1ab | 2015-02-13 08:23:52 -0500 | [diff] [blame] | 992 | static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone); |
| 993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | /* |
| 995 | * Generate a new unfragmented bio with the given size |
Mike Snitzer | 586b286 | 2015-09-09 21:34:51 -0400 | [diff] [blame] | 996 | * This should never violate the device limitations (but only because |
| 997 | * max_segment_size is being constrained to PAGE_SIZE). |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 998 | * |
| 999 | * This function may be called concurrently. If we allocate from the mempool |
| 1000 | * concurrently, there is a possibility of deadlock. For example, if we have |
| 1001 | * mempool of 256 pages, two processes, each wanting 256, pages allocate from |
| 1002 | * the mempool concurrently, it may deadlock in a situation where both processes |
| 1003 | * have allocated 128 pages and the mempool is exhausted. |
| 1004 | * |
| 1005 | * In order to avoid this scenario we allocate the pages under a mutex. |
| 1006 | * |
| 1007 | * In order to not degrade performance with excessive locking, we try |
| 1008 | * non-blocking allocations without a mutex first but on failure we fallback |
| 1009 | * to blocking allocations with a mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | */ |
Mikulas Patocka | cf2f1ab | 2015-02-13 08:23:52 -0500 | [diff] [blame] | 1011 | 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] | 1012 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1013 | struct crypt_config *cc = io->cc; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1014 | struct bio *clone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1016 | gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM; |
| 1017 | unsigned i, len, remaining_size; |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 1018 | struct page *page; |
Mikulas Patocka | cf2f1ab | 2015-02-13 08:23:52 -0500 | [diff] [blame] | 1019 | struct bio_vec *bvec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1021 | retry: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 1022 | if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM)) |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1023 | mutex_lock(&cc->bio_alloc_lock); |
| 1024 | |
Olaf Kirch | 2f9941b | 2007-05-09 02:32:53 -0700 | [diff] [blame] | 1025 | clone = bio_alloc_bioset(GFP_NOIO, nr_iovecs, cc->bs); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1026 | if (!clone) |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1027 | goto return_clone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | |
Olaf Kirch | 027581f | 2007-05-09 02:32:52 -0700 | [diff] [blame] | 1029 | clone_init(io, clone); |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 1030 | |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1031 | remaining_size = size; |
| 1032 | |
Olaf Kirch | f97380b | 2007-05-09 02:32:54 -0700 | [diff] [blame] | 1033 | for (i = 0; i < nr_iovecs; i++) { |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 1034 | page = mempool_alloc(cc->page_pool, gfp_mask); |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1035 | if (!page) { |
| 1036 | crypt_free_buffer_pages(cc, clone); |
| 1037 | bio_put(clone); |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 1038 | gfp_mask |= __GFP_DIRECT_RECLAIM; |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1039 | goto retry; |
| 1040 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1042 | len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | |
Mikulas Patocka | cf2f1ab | 2015-02-13 08:23:52 -0500 | [diff] [blame] | 1044 | bvec = &clone->bi_io_vec[clone->bi_vcnt++]; |
| 1045 | bvec->bv_page = page; |
| 1046 | bvec->bv_len = len; |
| 1047 | bvec->bv_offset = 0; |
| 1048 | |
| 1049 | clone->bi_iter.bi_size += len; |
Milan Broz | 91e1062 | 2007-12-13 14:16:10 +0000 | [diff] [blame] | 1050 | |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1051 | remaining_size -= len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1054 | return_clone: |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 1055 | if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM)) |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1056 | mutex_unlock(&cc->bio_alloc_lock); |
| 1057 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1058 | return clone; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 1061 | 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] | 1062 | { |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 1063 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | struct bio_vec *bv; |
| 1065 | |
Kent Overstreet | cb34e05 | 2012-09-05 15:22:02 -0700 | [diff] [blame] | 1066 | bio_for_each_segment_all(bv, clone, i) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | BUG_ON(!bv->bv_page); |
| 1068 | mempool_free(bv->bv_page, cc->page_pool); |
| 1069 | bv->bv_page = NULL; |
| 1070 | } |
| 1071 | } |
| 1072 | |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 1073 | static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc, |
| 1074 | struct bio *bio, sector_t sector) |
Milan Broz | dc440d1e | 2008-10-10 13:37:03 +0100 | [diff] [blame] | 1075 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1076 | io->cc = cc; |
Milan Broz | dc440d1e | 2008-10-10 13:37:03 +0100 | [diff] [blame] | 1077 | io->base_bio = bio; |
| 1078 | io->sector = sector; |
| 1079 | io->error = 0; |
Mikulas Patocka | 610f2de | 2014-02-20 18:01:01 -0500 | [diff] [blame] | 1080 | io->ctx.req = NULL; |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1081 | atomic_set(&io->io_pending, 0); |
Milan Broz | dc440d1e | 2008-10-10 13:37:03 +0100 | [diff] [blame] | 1082 | } |
| 1083 | |
Milan Broz | 3e1a8bd | 2008-10-10 13:37:02 +0100 | [diff] [blame] | 1084 | static void crypt_inc_pending(struct dm_crypt_io *io) |
| 1085 | { |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1086 | atomic_inc(&io->io_pending); |
Milan Broz | 3e1a8bd | 2008-10-10 13:37:02 +0100 | [diff] [blame] | 1087 | } |
| 1088 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | /* |
| 1090 | * One of the bios was finished. Check for completion of |
| 1091 | * the whole request and correctly clean up the buffer. |
| 1092 | */ |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1093 | static void crypt_dec_pending(struct dm_crypt_io *io) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1095 | struct crypt_config *cc = io->cc; |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 1096 | struct bio *base_bio = io->base_bio; |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 1097 | int error = io->error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1099 | if (!atomic_dec_and_test(&io->io_pending)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | return; |
| 1101 | |
Mikulas Patocka | 610f2de | 2014-02-20 18:01:01 -0500 | [diff] [blame] | 1102 | if (io->ctx.req) |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 1103 | crypt_free_req(cc, io->ctx.req, base_bio); |
Milan Broz | b35f8ca | 2009-03-16 17:44:36 +0000 | [diff] [blame] | 1104 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1105 | base_bio->bi_error = error; |
| 1106 | bio_endio(base_bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /* |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1110 | * kcryptd/kcryptd_io: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | * |
| 1112 | * Needed because it would be very unwise to do decryption in an |
Milan Broz | 23541d2 | 2006-10-03 01:15:39 -0700 | [diff] [blame] | 1113 | * interrupt context. |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1114 | * |
| 1115 | * kcryptd performs the actual encryption or decryption. |
| 1116 | * |
| 1117 | * kcryptd_io performs the IO submission. |
| 1118 | * |
| 1119 | * They must be separated as otherwise the final stages could be |
| 1120 | * starved by new requests which can block in the first stages due |
| 1121 | * to memory allocation. |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1122 | * |
| 1123 | * The work is done per CPU global for all dm-crypt instances. |
| 1124 | * They should not depend on each other and do not block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1126 | static void crypt_endio(struct bio *clone) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1127 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1128 | struct dm_crypt_io *io = clone->bi_private; |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1129 | struct crypt_config *cc = io->cc; |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 1130 | unsigned rw = bio_data_dir(clone); |
Sasha Levin | 9b81c84 | 2015-08-10 19:05:18 -0400 | [diff] [blame] | 1131 | int error; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1132 | |
| 1133 | /* |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1134 | * free the processed pages |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1135 | */ |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 1136 | if (rw == WRITE) |
Neil Brown | 644bd2f | 2007-10-16 13:48:46 +0200 | [diff] [blame] | 1137 | crypt_free_buffer_pages(cc, clone); |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 1138 | |
Sasha Levin | 9b81c84 | 2015-08-10 19:05:18 -0400 | [diff] [blame] | 1139 | error = clone->bi_error; |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 1140 | bio_put(clone); |
| 1141 | |
Sasha Levin | 9b81c84 | 2015-08-10 19:05:18 -0400 | [diff] [blame] | 1142 | if (rw == READ && !error) { |
Milan Broz | ee7a491 | 2008-02-08 02:10:46 +0000 | [diff] [blame] | 1143 | kcryptd_queue_crypt(io); |
| 1144 | return; |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1145 | } |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1146 | |
Sasha Levin | 9b81c84 | 2015-08-10 19:05:18 -0400 | [diff] [blame] | 1147 | if (unlikely(error)) |
| 1148 | io->error = error; |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1149 | |
| 1150 | crypt_dec_pending(io); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1153 | static void clone_init(struct dm_crypt_io *io, struct bio *clone) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1154 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1155 | struct crypt_config *cc = io->cc; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1156 | |
| 1157 | clone->bi_private = io; |
| 1158 | clone->bi_end_io = crypt_endio; |
| 1159 | clone->bi_bdev = cc->dev->bdev; |
Bart Van Assche | 4382e33 | 2016-09-14 10:45:36 +0200 | [diff] [blame] | 1160 | bio_set_op_attrs(clone, bio_op(io->base_bio), bio_flags(io->base_bio)); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1163 | 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] | 1164 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1165 | struct crypt_config *cc = io->cc; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1166 | struct bio *clone; |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1167 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1168 | /* |
Mike Snitzer | 5977907 | 2015-04-09 16:53:24 -0400 | [diff] [blame] | 1169 | * We need the original biovec array in order to decrypt |
| 1170 | * the whole bio data *afterwards* -- thanks to immutable |
| 1171 | * biovecs we don't need to worry about the block layer |
| 1172 | * modifying the biovec array; so leverage bio_clone_fast(). |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1173 | */ |
Mike Snitzer | 5977907 | 2015-04-09 16:53:24 -0400 | [diff] [blame] | 1174 | clone = bio_clone_fast(io->base_bio, gfp, cc->bs); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 1175 | if (!clone) |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1176 | return 1; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1177 | |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1178 | crypt_inc_pending(io); |
| 1179 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1180 | clone_init(io, clone); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1181 | clone->bi_iter.bi_sector = cc->start + io->sector; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1182 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1183 | generic_make_request(clone); |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1184 | return 0; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1187 | static void kcryptd_io_read_work(struct work_struct *work) |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1188 | { |
| 1189 | struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); |
| 1190 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1191 | crypt_inc_pending(io); |
| 1192 | if (kcryptd_io_read(io, GFP_NOIO)) |
| 1193 | io->error = -ENOMEM; |
| 1194 | crypt_dec_pending(io); |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1195 | } |
| 1196 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1197 | static void kcryptd_queue_read(struct dm_crypt_io *io) |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1198 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1199 | struct crypt_config *cc = io->cc; |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1200 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1201 | INIT_WORK(&io->work, kcryptd_io_read_work); |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1202 | queue_work(cc->io_queue, &io->work); |
| 1203 | } |
| 1204 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1205 | static void kcryptd_io_write(struct dm_crypt_io *io) |
| 1206 | { |
| 1207 | struct bio *clone = io->ctx.bio_out; |
| 1208 | |
| 1209 | generic_make_request(clone); |
| 1210 | } |
| 1211 | |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1212 | #define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node) |
| 1213 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1214 | static int dmcrypt_write(void *data) |
| 1215 | { |
| 1216 | struct crypt_config *cc = data; |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1217 | struct dm_crypt_io *io; |
| 1218 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1219 | while (1) { |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1220 | struct rb_root write_tree; |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1221 | struct blk_plug plug; |
| 1222 | |
| 1223 | DECLARE_WAITQUEUE(wait, current); |
| 1224 | |
| 1225 | spin_lock_irq(&cc->write_thread_wait.lock); |
| 1226 | continue_locked: |
| 1227 | |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1228 | if (!RB_EMPTY_ROOT(&cc->write_tree)) |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1229 | goto pop_from_list; |
| 1230 | |
Rabin Vincent | f659b10 | 2016-09-21 16:22:29 +0200 | [diff] [blame] | 1231 | set_current_state(TASK_INTERRUPTIBLE); |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1232 | __add_wait_queue(&cc->write_thread_wait, &wait); |
| 1233 | |
| 1234 | spin_unlock_irq(&cc->write_thread_wait.lock); |
| 1235 | |
Rabin Vincent | f659b10 | 2016-09-21 16:22:29 +0200 | [diff] [blame] | 1236 | if (unlikely(kthread_should_stop())) { |
| 1237 | set_task_state(current, TASK_RUNNING); |
| 1238 | remove_wait_queue(&cc->write_thread_wait, &wait); |
| 1239 | break; |
| 1240 | } |
| 1241 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1242 | schedule(); |
| 1243 | |
Rabin Vincent | f659b10 | 2016-09-21 16:22:29 +0200 | [diff] [blame] | 1244 | set_task_state(current, TASK_RUNNING); |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1245 | spin_lock_irq(&cc->write_thread_wait.lock); |
| 1246 | __remove_wait_queue(&cc->write_thread_wait, &wait); |
| 1247 | goto continue_locked; |
| 1248 | |
| 1249 | pop_from_list: |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1250 | write_tree = cc->write_tree; |
| 1251 | cc->write_tree = RB_ROOT; |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1252 | spin_unlock_irq(&cc->write_thread_wait.lock); |
| 1253 | |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1254 | BUG_ON(rb_parent(write_tree.rb_node)); |
| 1255 | |
| 1256 | /* |
| 1257 | * Note: we cannot walk the tree here with rb_next because |
| 1258 | * the structures may be freed when kcryptd_io_write is called. |
| 1259 | */ |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1260 | blk_start_plug(&plug); |
| 1261 | do { |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1262 | io = crypt_io_from_node(rb_first(&write_tree)); |
| 1263 | rb_erase(&io->rb_node, &write_tree); |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1264 | kcryptd_io_write(io); |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1265 | } while (!RB_EMPTY_ROOT(&write_tree)); |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1266 | blk_finish_plug(&plug); |
| 1267 | } |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1271 | 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] | 1272 | { |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1273 | struct bio *clone = io->ctx.bio_out; |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1274 | struct crypt_config *cc = io->cc; |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1275 | unsigned long flags; |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1276 | sector_t sector; |
| 1277 | struct rb_node **rbp, *parent; |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1278 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1279 | if (unlikely(io->error < 0)) { |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1280 | crypt_free_buffer_pages(cc, clone); |
| 1281 | bio_put(clone); |
Milan Broz | 6c031f4 | 2008-10-10 13:37:06 +0100 | [diff] [blame] | 1282 | crypt_dec_pending(io); |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1283 | return; |
| 1284 | } |
| 1285 | |
| 1286 | /* crypt_convert should have filled the clone bio */ |
Kent Overstreet | 003b5c5 | 2013-10-11 15:45:43 -0700 | [diff] [blame] | 1287 | BUG_ON(io->ctx.iter_out.bi_size); |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1288 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1289 | clone->bi_iter.bi_sector = cc->start + io->sector; |
Milan Broz | 899c95d | 2008-02-08 02:11:02 +0000 | [diff] [blame] | 1290 | |
Mikulas Patocka | 0f5d8e6 | 2015-02-13 08:27:08 -0500 | [diff] [blame] | 1291 | if (likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) { |
| 1292 | generic_make_request(clone); |
| 1293 | return; |
| 1294 | } |
| 1295 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1296 | spin_lock_irqsave(&cc->write_thread_wait.lock, flags); |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1297 | rbp = &cc->write_tree.rb_node; |
| 1298 | parent = NULL; |
| 1299 | sector = io->sector; |
| 1300 | while (*rbp) { |
| 1301 | parent = *rbp; |
| 1302 | if (sector < crypt_io_from_node(parent)->sector) |
| 1303 | rbp = &(*rbp)->rb_left; |
| 1304 | else |
| 1305 | rbp = &(*rbp)->rb_right; |
| 1306 | } |
| 1307 | rb_link_node(&io->rb_node, parent, rbp); |
| 1308 | rb_insert_color(&io->rb_node, &cc->write_tree); |
| 1309 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1310 | wake_up_locked(&cc->write_thread_wait); |
| 1311 | spin_unlock_irqrestore(&cc->write_thread_wait.lock, flags); |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1314 | static void kcryptd_crypt_write_convert(struct dm_crypt_io *io) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1315 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1316 | struct crypt_config *cc = io->cc; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1317 | struct bio *clone; |
Milan Broz | c808161 | 2008-10-10 13:37:08 +0100 | [diff] [blame] | 1318 | int crypt_finished; |
Milan Broz | b635b00 | 2008-10-21 17:45:00 +0100 | [diff] [blame] | 1319 | sector_t sector = io->sector; |
Milan Broz | dec1ced | 2008-02-08 02:10:57 +0000 | [diff] [blame] | 1320 | int r; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1321 | |
Milan Broz | 93e605c | 2006-10-03 01:15:38 -0700 | [diff] [blame] | 1322 | /* |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1323 | * Prevent io from disappearing until this function completes. |
| 1324 | */ |
| 1325 | crypt_inc_pending(io); |
Milan Broz | b635b00 | 2008-10-21 17:45:00 +0100 | [diff] [blame] | 1326 | crypt_convert_init(cc, &io->ctx, NULL, io->base_bio, sector); |
Milan Broz | fc5a5e9 | 2008-10-10 13:37:04 +0100 | [diff] [blame] | 1327 | |
Mikulas Patocka | cf2f1ab | 2015-02-13 08:23:52 -0500 | [diff] [blame] | 1328 | clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size); |
| 1329 | if (unlikely(!clone)) { |
| 1330 | io->error = -EIO; |
| 1331 | goto dec; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1332 | } |
Milan Broz | 899c95d | 2008-02-08 02:11:02 +0000 | [diff] [blame] | 1333 | |
Mikulas Patocka | cf2f1ab | 2015-02-13 08:23:52 -0500 | [diff] [blame] | 1334 | io->ctx.bio_out = clone; |
| 1335 | io->ctx.iter_out = clone->bi_iter; |
| 1336 | |
| 1337 | sector += bio_sectors(clone); |
| 1338 | |
| 1339 | crypt_inc_pending(io); |
| 1340 | r = crypt_convert(cc, &io->ctx); |
| 1341 | if (r) |
| 1342 | io->error = -EIO; |
| 1343 | crypt_finished = atomic_dec_and_test(&io->ctx.cc_pending); |
| 1344 | |
| 1345 | /* Encryption was already finished, submit io now */ |
| 1346 | if (crypt_finished) { |
| 1347 | kcryptd_crypt_write_io_submit(io, 0); |
| 1348 | io->sector = sector; |
| 1349 | } |
| 1350 | |
| 1351 | dec: |
Milan Broz | 899c95d | 2008-02-08 02:11:02 +0000 | [diff] [blame] | 1352 | crypt_dec_pending(io); |
Milan Broz | 84131db | 2008-02-08 02:10:59 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1355 | static void kcryptd_crypt_read_done(struct dm_crypt_io *io) |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1356 | { |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1357 | crypt_dec_pending(io); |
| 1358 | } |
| 1359 | |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1360 | static void kcryptd_crypt_read_convert(struct dm_crypt_io *io) |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1361 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1362 | struct crypt_config *cc = io->cc; |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1363 | int r = 0; |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1364 | |
Milan Broz | 3e1a8bd | 2008-10-10 13:37:02 +0100 | [diff] [blame] | 1365 | crypt_inc_pending(io); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1366 | |
Milan Broz | 5301703 | 2008-02-08 02:10:38 +0000 | [diff] [blame] | 1367 | crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio, |
Milan Broz | 0c395b0 | 2008-02-08 02:10:54 +0000 | [diff] [blame] | 1368 | io->sector); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1369 | |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1370 | r = crypt_convert(cc, &io->ctx); |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1371 | if (r < 0) |
| 1372 | io->error = -EIO; |
Milan Broz | 5742fd7 | 2008-02-08 02:10:43 +0000 | [diff] [blame] | 1373 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1374 | if (atomic_dec_and_test(&io->ctx.cc_pending)) |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1375 | kcryptd_crypt_read_done(io); |
Milan Broz | 3a7f6c9 | 2008-02-08 02:11:14 +0000 | [diff] [blame] | 1376 | |
| 1377 | crypt_dec_pending(io); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1378 | } |
| 1379 | |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1380 | static void kcryptd_async_done(struct crypto_async_request *async_req, |
| 1381 | int error) |
| 1382 | { |
Huang Ying | b2174ee | 2009-03-16 17:44:33 +0000 | [diff] [blame] | 1383 | struct dm_crypt_request *dmreq = async_req->data; |
| 1384 | struct convert_context *ctx = dmreq->ctx; |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1385 | 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] | 1386 | struct crypt_config *cc = io->cc; |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1387 | |
Milan Broz | 54cea3f | 2015-05-15 17:00:25 +0200 | [diff] [blame] | 1388 | /* |
| 1389 | * A request from crypto driver backlog is going to be processed now, |
| 1390 | * finish the completion and continue in crypt_convert(). |
| 1391 | * (Callback will be called for the second time for this request.) |
| 1392 | */ |
Rabin Vincent | c0403ec | 2015-05-05 15:15:56 +0200 | [diff] [blame] | 1393 | if (error == -EINPROGRESS) { |
| 1394 | complete(&ctx->restart); |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1395 | return; |
Rabin Vincent | c0403ec | 2015-05-05 15:15:56 +0200 | [diff] [blame] | 1396 | } |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1397 | |
Milan Broz | 2dc5327 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1398 | if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post) |
| 1399 | error = cc->iv_gen_ops->post(cc, iv_of_dmreq(cc, dmreq), dmreq); |
| 1400 | |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1401 | if (error < 0) |
| 1402 | io->error = -EIO; |
| 1403 | |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 1404 | crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio); |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1405 | |
Mikulas Patocka | 40b6229 | 2012-07-27 15:08:04 +0100 | [diff] [blame] | 1406 | if (!atomic_dec_and_test(&ctx->cc_pending)) |
Rabin Vincent | c0403ec | 2015-05-05 15:15:56 +0200 | [diff] [blame] | 1407 | return; |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1408 | |
| 1409 | if (bio_data_dir(io->base_bio) == READ) |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1410 | kcryptd_crypt_read_done(io); |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1411 | else |
Mikulas Patocka | 72c6e7a | 2012-03-28 18:41:22 +0100 | [diff] [blame] | 1412 | kcryptd_crypt_write_io_submit(io, 1); |
Milan Broz | 95497a9 | 2008-02-08 02:11:12 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Milan Broz | 4e4eef6 | 2008-02-08 02:10:49 +0000 | [diff] [blame] | 1415 | static void kcryptd_crypt(struct work_struct *work) |
| 1416 | { |
| 1417 | struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); |
| 1418 | |
| 1419 | if (bio_data_dir(io->base_bio) == READ) |
| 1420 | kcryptd_crypt_read_convert(io); |
| 1421 | else |
| 1422 | kcryptd_crypt_write_convert(io); |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1425 | static void kcryptd_queue_crypt(struct dm_crypt_io *io) |
| 1426 | { |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1427 | struct crypt_config *cc = io->cc; |
Alasdair G Kergon | 395b167 | 2008-02-08 02:10:52 +0000 | [diff] [blame] | 1428 | |
| 1429 | INIT_WORK(&io->work, kcryptd_crypt); |
| 1430 | queue_work(cc->crypt_queue, &io->work); |
| 1431 | } |
| 1432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | /* |
| 1434 | * Decode key from its hex representation |
| 1435 | */ |
| 1436 | static int crypt_decode_key(u8 *key, char *hex, unsigned int size) |
| 1437 | { |
| 1438 | char buffer[3]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | unsigned int i; |
| 1440 | |
| 1441 | buffer[2] = '\0'; |
| 1442 | |
Milan Broz | 8b00445 | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1443 | for (i = 0; i < size; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | buffer[0] = *hex++; |
| 1445 | buffer[1] = *hex++; |
| 1446 | |
majianpeng | 1a66a08 | 2012-07-27 15:07:59 +0100 | [diff] [blame] | 1447 | if (kstrtou8(buffer, 16, &key[i])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1448 | return -EINVAL; |
| 1449 | } |
| 1450 | |
| 1451 | if (*hex != '\0') |
| 1452 | return -EINVAL; |
| 1453 | |
| 1454 | return 0; |
| 1455 | } |
| 1456 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1457 | static void crypt_free_tfms(struct crypt_config *cc) |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1458 | { |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1459 | unsigned i; |
| 1460 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1461 | if (!cc->tfms) |
| 1462 | return; |
| 1463 | |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1464 | for (i = 0; i < cc->tfms_count; i++) |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1465 | if (cc->tfms[i] && !IS_ERR(cc->tfms[i])) { |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 1466 | crypto_free_skcipher(cc->tfms[i]); |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1467 | cc->tfms[i] = NULL; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1468 | } |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1469 | |
| 1470 | kfree(cc->tfms); |
| 1471 | cc->tfms = NULL; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1474 | static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode) |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1475 | { |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1476 | unsigned i; |
| 1477 | int err; |
| 1478 | |
Eric Biggers | 5d0be84 | 2016-08-30 09:51:44 -0700 | [diff] [blame] | 1479 | cc->tfms = kzalloc(cc->tfms_count * sizeof(struct crypto_skcipher *), |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1480 | GFP_KERNEL); |
| 1481 | if (!cc->tfms) |
| 1482 | return -ENOMEM; |
| 1483 | |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1484 | for (i = 0; i < cc->tfms_count; i++) { |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 1485 | cc->tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 0); |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1486 | if (IS_ERR(cc->tfms[i])) { |
| 1487 | err = PTR_ERR(cc->tfms[i]); |
| 1488 | crypt_free_tfms(cc); |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1489 | return err; |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | return 0; |
| 1494 | } |
| 1495 | |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1496 | static int crypt_setkey_allcpus(struct crypt_config *cc) |
| 1497 | { |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1498 | unsigned subkey_size; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1499 | int err = 0, i, r; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1500 | |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1501 | /* Ignore extra keys (which are used for IV etc) */ |
| 1502 | subkey_size = (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count); |
| 1503 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1504 | for (i = 0; i < cc->tfms_count; i++) { |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 1505 | r = crypto_skcipher_setkey(cc->tfms[i], |
| 1506 | cc->key + (i * subkey_size), |
| 1507 | subkey_size); |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1508 | if (r) |
| 1509 | err = r; |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | return err; |
| 1513 | } |
| 1514 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1515 | static int crypt_set_key(struct crypt_config *cc, char *key) |
| 1516 | { |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1517 | int r = -EINVAL; |
| 1518 | int key_string_len = strlen(key); |
| 1519 | |
Milan Broz | 69a8cfc | 2011-01-13 19:59:49 +0000 | [diff] [blame] | 1520 | /* The key size may not be changed. */ |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1521 | if (cc->key_size != (key_string_len >> 1)) |
| 1522 | goto out; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1523 | |
Milan Broz | 69a8cfc | 2011-01-13 19:59:49 +0000 | [diff] [blame] | 1524 | /* Hyphen (which gives a key_size of zero) means there is no key. */ |
| 1525 | if (!cc->key_size && strcmp(key, "-")) |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1526 | goto out; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1527 | |
Ondrej Kozina | 2c017f7 | 2016-11-02 15:02:08 +0100 | [diff] [blame] | 1528 | /* clear the flag since following operations may invalidate previously valid key */ |
| 1529 | clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
| 1530 | |
Milan Broz | 69a8cfc | 2011-01-13 19:59:49 +0000 | [diff] [blame] | 1531 | if (cc->key_size && crypt_decode_key(cc->key, key, cc->key_size) < 0) |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1532 | goto out; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1533 | |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1534 | r = crypt_setkey_allcpus(cc); |
Ondrej Kozina | 2c017f7 | 2016-11-02 15:02:08 +0100 | [diff] [blame] | 1535 | if (!r) |
| 1536 | set_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
Milan Broz | de8be5a | 2011-03-24 13:54:27 +0000 | [diff] [blame] | 1537 | |
| 1538 | out: |
| 1539 | /* Hex key string not needed after here, so wipe it. */ |
| 1540 | memset(key, '0', key_string_len); |
| 1541 | |
| 1542 | return r; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | static int crypt_wipe_key(struct crypt_config *cc) |
| 1546 | { |
| 1547 | clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); |
| 1548 | memset(&cc->key, 0, cc->key_size * sizeof(u8)); |
Andi Kleen | c029772 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1549 | |
| 1550 | return crypt_setkey_allcpus(cc); |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 1551 | } |
| 1552 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1553 | static void crypt_dtr(struct dm_target *ti) |
| 1554 | { |
| 1555 | struct crypt_config *cc = ti->private; |
| 1556 | |
| 1557 | ti->private = NULL; |
| 1558 | |
| 1559 | if (!cc) |
| 1560 | return; |
| 1561 | |
Rabin Vincent | f659b10 | 2016-09-21 16:22:29 +0200 | [diff] [blame] | 1562 | if (cc->write_thread) |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1563 | kthread_stop(cc->write_thread); |
| 1564 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1565 | if (cc->io_queue) |
| 1566 | destroy_workqueue(cc->io_queue); |
| 1567 | if (cc->crypt_queue) |
| 1568 | destroy_workqueue(cc->crypt_queue); |
| 1569 | |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1570 | crypt_free_tfms(cc); |
| 1571 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1572 | if (cc->bs) |
| 1573 | bioset_free(cc->bs); |
| 1574 | |
Julia Lawall | 6f65985 | 2015-09-13 14:15:05 +0200 | [diff] [blame] | 1575 | mempool_destroy(cc->page_pool); |
| 1576 | mempool_destroy(cc->req_pool); |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1577 | |
| 1578 | if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) |
| 1579 | cc->iv_gen_ops->dtr(cc); |
| 1580 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1581 | if (cc->dev) |
| 1582 | dm_put_device(ti, cc->dev); |
| 1583 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1584 | kzfree(cc->cipher); |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 1585 | kzfree(cc->cipher_string); |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1586 | |
| 1587 | /* Must zero key material before freeing */ |
| 1588 | kzfree(cc); |
| 1589 | } |
| 1590 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1591 | static int crypt_ctr_cipher(struct dm_target *ti, |
| 1592 | char *cipher_in, char *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1594 | struct crypt_config *cc = ti->private; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1595 | char *tmp, *cipher, *chainmode, *ivmode, *ivopts, *keycount; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1596 | char *cipher_api = NULL; |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1597 | int ret = -EINVAL; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1598 | char dummy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1600 | /* Convert to crypto api definition? */ |
| 1601 | if (strchr(cipher_in, '(')) { |
| 1602 | ti->error = "Bad cipher specification"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | return -EINVAL; |
| 1604 | } |
| 1605 | |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 1606 | cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL); |
| 1607 | if (!cc->cipher_string) |
| 1608 | goto bad_mem; |
| 1609 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1610 | /* |
| 1611 | * Legacy dm-crypt cipher specification |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1612 | * cipher[:keycount]-mode-iv:ivopts |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1613 | */ |
| 1614 | tmp = cipher_in; |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1615 | keycount = strsep(&tmp, "-"); |
| 1616 | cipher = strsep(&keycount, ":"); |
| 1617 | |
| 1618 | if (!keycount) |
| 1619 | cc->tfms_count = 1; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1620 | else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 || |
Milan Broz | d1f9642 | 2011-01-13 19:59:54 +0000 | [diff] [blame] | 1621 | !is_power_of_2(cc->tfms_count)) { |
| 1622 | ti->error = "Bad cipher key count specification"; |
| 1623 | return -EINVAL; |
| 1624 | } |
| 1625 | cc->key_parts = cc->tfms_count; |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1626 | cc->key_extra_size = 0; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1627 | |
| 1628 | cc->cipher = kstrdup(cipher, GFP_KERNEL); |
| 1629 | if (!cc->cipher) |
| 1630 | goto bad_mem; |
| 1631 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | chainmode = strsep(&tmp, "-"); |
| 1633 | ivopts = strsep(&tmp, "-"); |
| 1634 | ivmode = strsep(&ivopts, ":"); |
| 1635 | |
| 1636 | if (tmp) |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1637 | DMWARN("Ignoring unexpected additional cipher options"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 1639 | /* |
| 1640 | * For compatibility with the original dm-crypt mapping format, if |
| 1641 | * only the cipher name is supplied, use cbc-plain. |
| 1642 | */ |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1643 | if (!chainmode || (!strcmp(chainmode, "plain") && !ivmode)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1644 | chainmode = "cbc"; |
| 1645 | ivmode = "plain"; |
| 1646 | } |
| 1647 | |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 1648 | if (strcmp(chainmode, "ecb") && !ivmode) { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1649 | ti->error = "IV mechanism required"; |
| 1650 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1653 | cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL); |
| 1654 | if (!cipher_api) |
| 1655 | goto bad_mem; |
| 1656 | |
| 1657 | ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME, |
| 1658 | "%s(%s)", chainmode, cipher); |
| 1659 | if (ret < 0) { |
| 1660 | kfree(cipher_api); |
| 1661 | goto bad_mem; |
Herbert Xu | d1806f6 | 2006-08-22 20:29:17 +1000 | [diff] [blame] | 1662 | } |
| 1663 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1664 | /* Allocate cipher */ |
Mikulas Patocka | fd2d231 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1665 | ret = crypt_alloc_tfms(cc, cipher_api); |
| 1666 | if (ret < 0) { |
| 1667 | ti->error = "Error allocating crypto tfm"; |
| 1668 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1671 | /* Initialize IV */ |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 1672 | cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1673 | if (cc->iv_size) |
| 1674 | /* at least a 64 bit sector number should fit in our buffer */ |
| 1675 | cc->iv_size = max(cc->iv_size, |
| 1676 | (unsigned int)(sizeof(u64) / sizeof(u8))); |
| 1677 | else if (ivmode) { |
| 1678 | DMWARN("Selected cipher does not support IVs"); |
| 1679 | ivmode = NULL; |
| 1680 | } |
| 1681 | |
| 1682 | /* Choose ivmode, see comments at iv code. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1683 | if (ivmode == NULL) |
| 1684 | cc->iv_gen_ops = NULL; |
| 1685 | else if (strcmp(ivmode, "plain") == 0) |
| 1686 | cc->iv_gen_ops = &crypt_iv_plain_ops; |
Milan Broz | 61afef6 | 2009-12-10 23:52:25 +0000 | [diff] [blame] | 1687 | else if (strcmp(ivmode, "plain64") == 0) |
| 1688 | cc->iv_gen_ops = &crypt_iv_plain64_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | else if (strcmp(ivmode, "essiv") == 0) |
| 1690 | cc->iv_gen_ops = &crypt_iv_essiv_ops; |
Rik Snel | 48527fa | 2006-09-03 08:56:39 +1000 | [diff] [blame] | 1691 | else if (strcmp(ivmode, "benbi") == 0) |
| 1692 | cc->iv_gen_ops = &crypt_iv_benbi_ops; |
Ludwig Nussel | 46b4773 | 2007-05-09 02:32:55 -0700 | [diff] [blame] | 1693 | else if (strcmp(ivmode, "null") == 0) |
| 1694 | cc->iv_gen_ops = &crypt_iv_null_ops; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 1695 | else if (strcmp(ivmode, "lmk") == 0) { |
| 1696 | cc->iv_gen_ops = &crypt_iv_lmk_ops; |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 1697 | /* |
| 1698 | * Version 2 and 3 is recognised according |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 1699 | * to length of provided multi-key string. |
| 1700 | * If present (version 3), last key is used as IV seed. |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 1701 | * All keys (including IV seed) are always the same size. |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 1702 | */ |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1703 | if (cc->key_size % cc->key_parts) { |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 1704 | cc->key_parts++; |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1705 | cc->key_extra_size = cc->key_size / cc->key_parts; |
| 1706 | } |
Milan Broz | ed04d98 | 2013-10-28 23:21:04 +0100 | [diff] [blame] | 1707 | } else if (strcmp(ivmode, "tcw") == 0) { |
| 1708 | cc->iv_gen_ops = &crypt_iv_tcw_ops; |
| 1709 | cc->key_parts += 2; /* IV + whitening */ |
| 1710 | cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE; |
Milan Broz | 3474578 | 2011-01-13 19:59:55 +0000 | [diff] [blame] | 1711 | } else { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1712 | ret = -EINVAL; |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1713 | ti->error = "Invalid IV mode"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1714 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
Milan Broz | da31a07 | 2013-10-28 23:21:03 +0100 | [diff] [blame] | 1717 | /* Initialize and set key */ |
| 1718 | ret = crypt_set_key(cc, key); |
| 1719 | if (ret < 0) { |
| 1720 | ti->error = "Error decoding and setting key"; |
| 1721 | goto bad; |
| 1722 | } |
| 1723 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1724 | /* Allocate IV */ |
| 1725 | if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) { |
| 1726 | ret = cc->iv_gen_ops->ctr(cc, ti, ivopts); |
| 1727 | if (ret < 0) { |
| 1728 | ti->error = "Error creating IV"; |
| 1729 | goto bad; |
| 1730 | } |
Milan Broz | b95bf2d | 2009-12-10 23:51:56 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1733 | /* Initialize IV (set keys for ESSIV etc) */ |
| 1734 | if (cc->iv_gen_ops && cc->iv_gen_ops->init) { |
| 1735 | ret = cc->iv_gen_ops->init(cc); |
| 1736 | if (ret < 0) { |
| 1737 | ti->error = "Error initialising IV"; |
| 1738 | goto bad; |
| 1739 | } |
| 1740 | } |
| 1741 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1742 | ret = 0; |
| 1743 | bad: |
| 1744 | kfree(cipher_api); |
| 1745 | return ret; |
| 1746 | |
| 1747 | bad_mem: |
| 1748 | ti->error = "Cannot allocate cipher strings"; |
| 1749 | return -ENOMEM; |
| 1750 | } |
| 1751 | |
| 1752 | /* |
| 1753 | * Construct an encryption mapping: |
| 1754 | * <cipher> <key> <iv_offset> <dev_path> <start> |
| 1755 | */ |
| 1756 | static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) |
| 1757 | { |
| 1758 | struct crypt_config *cc; |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1759 | unsigned int key_size, opt_params; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1760 | unsigned long long tmpll; |
| 1761 | int ret; |
Mikulas Patocka | d49ec52 | 2014-08-28 11:09:31 -0400 | [diff] [blame] | 1762 | size_t iv_size_padding; |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1763 | struct dm_arg_set as; |
| 1764 | const char *opt_string; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1765 | char dummy; |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1766 | |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1767 | static struct dm_arg _args[] = { |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 1768 | {0, 5, "Invalid number of feature args"}, |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1769 | }; |
| 1770 | |
| 1771 | if (argc < 5) { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1772 | ti->error = "Not enough arguments"; |
| 1773 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1776 | key_size = strlen(argv[1]) >> 1; |
| 1777 | |
| 1778 | cc = kzalloc(sizeof(*cc) + key_size * sizeof(u8), GFP_KERNEL); |
| 1779 | if (!cc) { |
| 1780 | ti->error = "Cannot allocate encryption context"; |
| 1781 | return -ENOMEM; |
| 1782 | } |
Milan Broz | 69a8cfc | 2011-01-13 19:59:49 +0000 | [diff] [blame] | 1783 | cc->key_size = key_size; |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 1784 | cc->sector_size = (1 << SECTOR_SHIFT); |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 1785 | |
| 1786 | ti->private = cc; |
| 1787 | ret = crypt_ctr_cipher(ti, argv[0], argv[1]); |
| 1788 | if (ret < 0) |
| 1789 | goto bad; |
| 1790 | |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 1791 | cc->dmreq_start = sizeof(struct skcipher_request); |
| 1792 | cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc)); |
Mikulas Patocka | d49ec52 | 2014-08-28 11:09:31 -0400 | [diff] [blame] | 1793 | cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request)); |
| 1794 | |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 1795 | if (crypto_skcipher_alignmask(any_tfm(cc)) < CRYPTO_MINALIGN) { |
Mikulas Patocka | d49ec52 | 2014-08-28 11:09:31 -0400 | [diff] [blame] | 1796 | /* Allocate the padding exactly */ |
| 1797 | iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request)) |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 1798 | & crypto_skcipher_alignmask(any_tfm(cc)); |
Mikulas Patocka | d49ec52 | 2014-08-28 11:09:31 -0400 | [diff] [blame] | 1799 | } else { |
| 1800 | /* |
| 1801 | * If the cipher requires greater alignment than kmalloc |
| 1802 | * alignment, we don't know the exact position of the |
| 1803 | * initialization vector. We must assume worst case. |
| 1804 | */ |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 1805 | iv_size_padding = crypto_skcipher_alignmask(any_tfm(cc)); |
Mikulas Patocka | d49ec52 | 2014-08-28 11:09:31 -0400 | [diff] [blame] | 1806 | } |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1807 | |
Mikulas Patocka | 94f5e02 | 2015-02-13 08:25:26 -0500 | [diff] [blame] | 1808 | ret = -ENOMEM; |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1809 | cc->req_pool = mempool_create_kmalloc_pool(MIN_IOS, cc->dmreq_start + |
Mikulas Patocka | d49ec52 | 2014-08-28 11:09:31 -0400 | [diff] [blame] | 1810 | sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size); |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1811 | if (!cc->req_pool) { |
| 1812 | ti->error = "Cannot allocate crypt request mempool"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1813 | goto bad; |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1814 | } |
Milan Broz | ddd42ed | 2008-02-08 02:11:07 +0000 | [diff] [blame] | 1815 | |
Mike Snitzer | 30187e1 | 2016-01-31 13:28:26 -0500 | [diff] [blame] | 1816 | cc->per_bio_data_size = ti->per_io_data_size = |
Mikulas Patocka | d49ec52 | 2014-08-28 11:09:31 -0400 | [diff] [blame] | 1817 | ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + |
| 1818 | sizeof(struct dm_crypt_request) + iv_size_padding + cc->iv_size, |
| 1819 | ARCH_KMALLOC_MINALIGN); |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 1820 | |
Mikulas Patocka | cf2f1ab | 2015-02-13 08:23:52 -0500 | [diff] [blame] | 1821 | cc->page_pool = mempool_create_page_pool(BIO_MAX_PAGES, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | if (!cc->page_pool) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1823 | ti->error = "Cannot allocate page mempool"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1824 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
Jens Axboe | bb799ca | 2008-12-10 15:35:05 +0100 | [diff] [blame] | 1827 | cc->bs = bioset_create(MIN_IOS, 0); |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 1828 | if (!cc->bs) { |
| 1829 | ti->error = "Cannot allocate crypt bioset"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1830 | goto bad; |
Milan Broz | 6a24c71 | 2006-10-03 01:15:40 -0700 | [diff] [blame] | 1831 | } |
| 1832 | |
Mikulas Patocka | 7145c24 | 2015-02-13 08:24:41 -0500 | [diff] [blame] | 1833 | mutex_init(&cc->bio_alloc_lock); |
| 1834 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1835 | ret = -EINVAL; |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 1836 | if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) || |
| 1837 | (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1838 | ti->error = "Invalid iv_offset sector"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1839 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | } |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 1841 | cc->iv_offset = tmpll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | |
Vivek Goyal | e80d1c8 | 2015-07-31 09:20:36 -0400 | [diff] [blame] | 1843 | ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev); |
| 1844 | if (ret) { |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1845 | ti->error = "Device lookup failed"; |
| 1846 | goto bad; |
| 1847 | } |
| 1848 | |
Vivek Goyal | e80d1c8 | 2015-07-31 09:20:36 -0400 | [diff] [blame] | 1849 | ret = -EINVAL; |
Mikulas Patocka | 31998ef | 2012-03-28 18:41:26 +0100 | [diff] [blame] | 1850 | if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1) { |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 1851 | ti->error = "Invalid device sector"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1852 | goto bad; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1853 | } |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 1854 | cc->start = tmpll; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1856 | argv += 5; |
| 1857 | argc -= 5; |
| 1858 | |
| 1859 | /* Optional parameters */ |
| 1860 | if (argc) { |
| 1861 | as.argc = argc; |
| 1862 | as.argv = argv; |
| 1863 | |
| 1864 | ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error); |
| 1865 | if (ret) |
| 1866 | goto bad; |
| 1867 | |
Wei Yongjun | 44c144f | 2015-04-16 22:00:50 -0400 | [diff] [blame] | 1868 | ret = -EINVAL; |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 1869 | while (opt_params--) { |
| 1870 | opt_string = dm_shift_arg(&as); |
| 1871 | if (!opt_string) { |
| 1872 | ti->error = "Not enough feature arguments"; |
| 1873 | goto bad; |
| 1874 | } |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1875 | |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 1876 | if (!strcasecmp(opt_string, "allow_discards")) |
| 1877 | ti->num_discard_bios = 1; |
| 1878 | |
| 1879 | else if (!strcasecmp(opt_string, "same_cpu_crypt")) |
| 1880 | set_bit(DM_CRYPT_SAME_CPU, &cc->flags); |
| 1881 | |
Mikulas Patocka | 0f5d8e6 | 2015-02-13 08:27:08 -0500 | [diff] [blame] | 1882 | else if (!strcasecmp(opt_string, "submit_from_crypt_cpus")) |
| 1883 | set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); |
| 1884 | |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 1885 | else if (sscanf(opt_string, "sector_size:%u%c", |
| 1886 | &cc->sector_size, &dummy) == 1) { |
| 1887 | if (cc->sector_size < (1 << SECTOR_SHIFT) || |
| 1888 | cc->sector_size > 4096 || |
| 1889 | (1 << ilog2(cc->sector_size) != cc->sector_size)) { |
| 1890 | ti->error = "Invalid feature value for sector_size"; |
| 1891 | goto bad; |
| 1892 | } |
| 1893 | } else if (!strcasecmp(opt_string, "iv_large_sectors")) |
| 1894 | set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 1895 | else { |
| 1896 | ti->error = "Invalid feature arguments"; |
| 1897 | goto bad; |
| 1898 | } |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1899 | } |
| 1900 | } |
| 1901 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1902 | ret = -ENOMEM; |
Tim Murray | e97c4ed | 2016-01-19 16:33:27 -0800 | [diff] [blame] | 1903 | cc->io_queue = alloc_workqueue("kcryptd_io", |
| 1904 | WQ_HIGHPRI | |
| 1905 | WQ_MEM_RECLAIM, |
| 1906 | 1); |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1907 | if (!cc->io_queue) { |
| 1908 | ti->error = "Couldn't create kcryptd io queue"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1909 | goto bad; |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1910 | } |
| 1911 | |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 1912 | if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) |
Tim Murray | e97c4ed | 2016-01-19 16:33:27 -0800 | [diff] [blame] | 1913 | cc->crypt_queue = alloc_workqueue("kcryptd", |
| 1914 | WQ_HIGHPRI | |
| 1915 | WQ_MEM_RECLAIM, 1); |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 1916 | else |
Tim Murray | e97c4ed | 2016-01-19 16:33:27 -0800 | [diff] [blame] | 1917 | cc->crypt_queue = alloc_workqueue("kcryptd", |
| 1918 | WQ_HIGHPRI | |
| 1919 | WQ_MEM_RECLAIM | |
| 1920 | WQ_UNBOUND, |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 1921 | num_online_cpus()); |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1922 | if (!cc->crypt_queue) { |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 1923 | ti->error = "Couldn't create kcryptd queue"; |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1924 | goto bad; |
Milan Broz | 9934a8b | 2007-10-19 22:38:57 +0100 | [diff] [blame] | 1925 | } |
| 1926 | |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1927 | init_waitqueue_head(&cc->write_thread_wait); |
Mikulas Patocka | b3c5fd3 | 2015-02-13 08:27:41 -0500 | [diff] [blame] | 1928 | cc->write_tree = RB_ROOT; |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1929 | |
| 1930 | cc->write_thread = kthread_create(dmcrypt_write, cc, "dmcrypt_write"); |
| 1931 | if (IS_ERR(cc->write_thread)) { |
| 1932 | ret = PTR_ERR(cc->write_thread); |
| 1933 | cc->write_thread = NULL; |
| 1934 | ti->error = "Couldn't spawn write thread"; |
| 1935 | goto bad; |
| 1936 | } |
| 1937 | wake_up_process(cc->write_thread); |
| 1938 | |
Alasdair G Kergon | 55a62ee | 2013-03-01 22:45:47 +0000 | [diff] [blame] | 1939 | ti->num_flush_bios = 1; |
Alasdair G Kergon | 0ac5548 | 2012-07-27 15:08:08 +0100 | [diff] [blame] | 1940 | ti->discard_zeroes_data_unsupported = true; |
Milan Broz | 983c7db | 2011-09-25 23:26:21 +0100 | [diff] [blame] | 1941 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | return 0; |
| 1943 | |
Milan Broz | 28513fc | 2010-08-12 04:14:06 +0100 | [diff] [blame] | 1944 | bad: |
| 1945 | crypt_dtr(ti); |
| 1946 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | } |
| 1948 | |
Mikulas Patocka | 7de3ee5 | 2012-12-21 20:23:41 +0000 | [diff] [blame] | 1949 | static int crypt_map(struct dm_target *ti, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | { |
Alasdair G Kergon | 028867a | 2007-07-12 17:26:32 +0100 | [diff] [blame] | 1951 | struct dm_crypt_io *io; |
Alasdair G Kergon | 49a8a92 | 2012-07-27 15:08:05 +0100 | [diff] [blame] | 1952 | struct crypt_config *cc = ti->private; |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 1953 | |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1954 | /* |
Mike Christie | 28a8f0d | 2016-06-05 14:32:25 -0500 | [diff] [blame] | 1955 | * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues. |
| 1956 | * - 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] | 1957 | * - for REQ_OP_DISCARD caller must use flush if IO ordering matters |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1958 | */ |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1959 | if (unlikely(bio->bi_opf & REQ_PREFLUSH || |
Mike Christie | 28a8f0d | 2016-06-05 14:32:25 -0500 | [diff] [blame] | 1960 | bio_op(bio) == REQ_OP_DISCARD)) { |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 1961 | bio->bi_bdev = cc->dev->bdev; |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 1962 | if (bio_sectors(bio)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1963 | bio->bi_iter.bi_sector = cc->start + |
| 1964 | dm_target_offset(ti, bio->bi_iter.bi_sector); |
Mikulas Patocka | 647c7db | 2009-06-22 10:12:23 +0100 | [diff] [blame] | 1965 | return DM_MAPIO_REMAPPED; |
| 1966 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | |
Mikulas Patocka | 4e870e9 | 2016-08-30 16:38:42 -0400 | [diff] [blame] | 1968 | /* |
| 1969 | * Check if bio is too large, split as needed. |
| 1970 | */ |
| 1971 | if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_PAGES << PAGE_SHIFT)) && |
| 1972 | bio_data_dir(bio) == WRITE) |
| 1973 | dm_accept_partial_bio(bio, ((BIO_MAX_PAGES << PAGE_SHIFT) >> SECTOR_SHIFT)); |
| 1974 | |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 1975 | /* |
| 1976 | * Ensure that bio is a multiple of internal sector encryption size |
| 1977 | * and is aligned to this size as defined in IO hints. |
| 1978 | */ |
| 1979 | if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0)) |
| 1980 | return -EIO; |
| 1981 | |
| 1982 | if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1))) |
| 1983 | return -EIO; |
| 1984 | |
Mikulas Patocka | 298a9fa | 2014-03-28 15:51:55 -0400 | [diff] [blame] | 1985 | io = dm_per_bio_data(bio, cc->per_bio_data_size); |
| 1986 | crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector)); |
Herbert Xu | bbdb23b | 2016-01-24 21:16:36 +0800 | [diff] [blame] | 1987 | io->ctx.req = (struct skcipher_request *)(io + 1); |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1988 | |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1989 | if (bio_data_dir(io->base_bio) == READ) { |
| 1990 | if (kcryptd_io_read(io, GFP_NOWAIT)) |
Mikulas Patocka | dc26762 | 2015-02-13 08:25:59 -0500 | [diff] [blame] | 1991 | kcryptd_queue_read(io); |
Milan Broz | 20c8253 | 2011-01-13 19:59:53 +0000 | [diff] [blame] | 1992 | } else |
Milan Broz | cabf08e | 2007-10-19 22:38:58 +0100 | [diff] [blame] | 1993 | kcryptd_queue_crypt(io); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | |
Kiyoshi Ueda | d2a7ad2 | 2006-12-08 02:41:06 -0800 | [diff] [blame] | 1995 | return DM_MAPIO_SUBMITTED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | } |
| 1997 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 1998 | static void crypt_status(struct dm_target *ti, status_type_t type, |
| 1999 | unsigned status_flags, char *result, unsigned maxlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | { |
Milan Broz | 5ebaee6 | 2010-08-12 04:14:07 +0100 | [diff] [blame] | 2001 | struct crypt_config *cc = ti->private; |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 2002 | unsigned i, sz = 0; |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 2003 | int num_feature_args = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | |
| 2005 | switch (type) { |
| 2006 | case STATUSTYPE_INFO: |
| 2007 | result[0] = '\0'; |
| 2008 | break; |
| 2009 | |
| 2010 | case STATUSTYPE_TABLE: |
Milan Broz | 7dbcd13 | 2011-01-13 19:59:52 +0000 | [diff] [blame] | 2011 | DMEMIT("%s ", cc->cipher_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | |
Mikulas Patocka | fd7c092 | 2013-03-01 22:45:44 +0000 | [diff] [blame] | 2013 | if (cc->key_size > 0) |
| 2014 | for (i = 0; i < cc->key_size; i++) |
| 2015 | DMEMIT("%02x", cc->key[i]); |
| 2016 | else |
| 2017 | DMEMIT("-"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | |
Andrew Morton | 4ee218c | 2006-03-27 01:17:48 -0800 | [diff] [blame] | 2019 | DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, |
| 2020 | cc->dev->name, (unsigned long long)cc->start); |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 2021 | |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 2022 | num_feature_args += !!ti->num_discard_bios; |
| 2023 | num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags); |
Mikulas Patocka | 0f5d8e6 | 2015-02-13 08:27:08 -0500 | [diff] [blame] | 2024 | num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 2025 | num_feature_args += (cc->sector_size != (1 << SECTOR_SHIFT)) ? 1 : 0; |
| 2026 | num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 2027 | if (num_feature_args) { |
| 2028 | DMEMIT(" %d", num_feature_args); |
| 2029 | if (ti->num_discard_bios) |
| 2030 | DMEMIT(" allow_discards"); |
| 2031 | if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) |
| 2032 | DMEMIT(" same_cpu_crypt"); |
Mikulas Patocka | 0f5d8e6 | 2015-02-13 08:27:08 -0500 | [diff] [blame] | 2033 | if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) |
| 2034 | DMEMIT(" submit_from_crypt_cpus"); |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 2035 | if (cc->sector_size != (1 << SECTOR_SHIFT)) |
| 2036 | DMEMIT(" sector_size:%d", cc->sector_size); |
| 2037 | if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) |
| 2038 | DMEMIT(" iv_large_sectors"); |
Mikulas Patocka | f3396c58 | 2015-02-13 08:23:09 -0500 | [diff] [blame] | 2039 | } |
Milan Broz | 772ae5f | 2011-08-02 12:32:08 +0100 | [diff] [blame] | 2040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2041 | break; |
| 2042 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2043 | } |
| 2044 | |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2045 | static void crypt_postsuspend(struct dm_target *ti) |
| 2046 | { |
| 2047 | struct crypt_config *cc = ti->private; |
| 2048 | |
| 2049 | set_bit(DM_CRYPT_SUSPENDED, &cc->flags); |
| 2050 | } |
| 2051 | |
| 2052 | static int crypt_preresume(struct dm_target *ti) |
| 2053 | { |
| 2054 | struct crypt_config *cc = ti->private; |
| 2055 | |
| 2056 | if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { |
| 2057 | DMERR("aborting resume - crypt key is not set."); |
| 2058 | return -EAGAIN; |
| 2059 | } |
| 2060 | |
| 2061 | return 0; |
| 2062 | } |
| 2063 | |
| 2064 | static void crypt_resume(struct dm_target *ti) |
| 2065 | { |
| 2066 | struct crypt_config *cc = ti->private; |
| 2067 | |
| 2068 | clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); |
| 2069 | } |
| 2070 | |
| 2071 | /* Message interface |
| 2072 | * key set <key> |
| 2073 | * key wipe |
| 2074 | */ |
| 2075 | static int crypt_message(struct dm_target *ti, unsigned argc, char **argv) |
| 2076 | { |
| 2077 | struct crypt_config *cc = ti->private; |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 2078 | int ret = -EINVAL; |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2079 | |
| 2080 | if (argc < 2) |
| 2081 | goto error; |
| 2082 | |
Mike Snitzer | 498f010 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 2083 | if (!strcasecmp(argv[0], "key")) { |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2084 | if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { |
| 2085 | DMWARN("not suspended during key manipulation."); |
| 2086 | return -EINVAL; |
| 2087 | } |
Mike Snitzer | 498f010 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 2088 | if (argc == 3 && !strcasecmp(argv[1], "set")) { |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 2089 | ret = crypt_set_key(cc, argv[2]); |
| 2090 | if (ret) |
| 2091 | return ret; |
| 2092 | if (cc->iv_gen_ops && cc->iv_gen_ops->init) |
| 2093 | ret = cc->iv_gen_ops->init(cc); |
| 2094 | return ret; |
| 2095 | } |
Mike Snitzer | 498f010 | 2011-08-02 12:32:04 +0100 | [diff] [blame] | 2096 | if (argc == 2 && !strcasecmp(argv[1], "wipe")) { |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 2097 | if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) { |
| 2098 | ret = cc->iv_gen_ops->wipe(cc); |
| 2099 | if (ret) |
| 2100 | return ret; |
| 2101 | } |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2102 | return crypt_wipe_key(cc); |
Milan Broz | 542da31 | 2009-12-10 23:51:57 +0000 | [diff] [blame] | 2103 | } |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | error: |
| 2107 | DMWARN("unrecognised message received."); |
| 2108 | return -EINVAL; |
| 2109 | } |
| 2110 | |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 2111 | static int crypt_iterate_devices(struct dm_target *ti, |
| 2112 | iterate_devices_callout_fn fn, void *data) |
| 2113 | { |
| 2114 | struct crypt_config *cc = ti->private; |
| 2115 | |
Mike Snitzer | 5dea271 | 2009-07-23 20:30:42 +0100 | [diff] [blame] | 2116 | return fn(ti, cc->dev, cc->start, ti->len, data); |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 2117 | } |
| 2118 | |
Mike Snitzer | 586b286 | 2015-09-09 21:34:51 -0400 | [diff] [blame] | 2119 | static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits) |
| 2120 | { |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 2121 | struct crypt_config *cc = ti->private; |
| 2122 | |
Mike Snitzer | 586b286 | 2015-09-09 21:34:51 -0400 | [diff] [blame] | 2123 | /* |
| 2124 | * Unfortunate constraint that is required to avoid the potential |
| 2125 | * for exceeding underlying device's max_segments limits -- due to |
| 2126 | * crypt_alloc_buffer() possibly allocating pages for the encryption |
| 2127 | * bio that are not as physically contiguous as the original bio. |
| 2128 | */ |
| 2129 | limits->max_segment_size = PAGE_SIZE; |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 2130 | |
| 2131 | if (cc->sector_size != (1 << SECTOR_SHIFT)) { |
| 2132 | limits->logical_block_size = cc->sector_size; |
| 2133 | limits->physical_block_size = cc->sector_size; |
| 2134 | blk_limits_io_min(limits, cc->sector_size); |
| 2135 | } |
Mike Snitzer | 586b286 | 2015-09-09 21:34:51 -0400 | [diff] [blame] | 2136 | } |
| 2137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2138 | static struct target_type crypt_target = { |
| 2139 | .name = "crypt", |
Milan Broz | c083ebe | 2017-03-16 15:39:44 +0100 | [diff] [blame^] | 2140 | .version = {1, 17, 0}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | .module = THIS_MODULE, |
| 2142 | .ctr = crypt_ctr, |
| 2143 | .dtr = crypt_dtr, |
| 2144 | .map = crypt_map, |
| 2145 | .status = crypt_status, |
Milan Broz | e48d4bb | 2006-10-03 01:15:37 -0700 | [diff] [blame] | 2146 | .postsuspend = crypt_postsuspend, |
| 2147 | .preresume = crypt_preresume, |
| 2148 | .resume = crypt_resume, |
| 2149 | .message = crypt_message, |
Mike Snitzer | af4874e | 2009-06-22 10:12:33 +0100 | [diff] [blame] | 2150 | .iterate_devices = crypt_iterate_devices, |
Mike Snitzer | 586b286 | 2015-09-09 21:34:51 -0400 | [diff] [blame] | 2151 | .io_hints = crypt_io_hints, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | }; |
| 2153 | |
| 2154 | static int __init dm_crypt_init(void) |
| 2155 | { |
| 2156 | int r; |
| 2157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | r = dm_register_target(&crypt_target); |
Mikulas Patocka | 94f5e02 | 2015-02-13 08:25:26 -0500 | [diff] [blame] | 2159 | if (r < 0) |
Alasdair G Kergon | 72d9486 | 2006-06-26 00:27:35 -0700 | [diff] [blame] | 2160 | DMERR("register failed %d", r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | return r; |
| 2163 | } |
| 2164 | |
| 2165 | static void __exit dm_crypt_exit(void) |
| 2166 | { |
Mikulas Patocka | 10d3bd0 | 2009-01-06 03:04:58 +0000 | [diff] [blame] | 2167 | dm_unregister_target(&crypt_target); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | } |
| 2169 | |
| 2170 | module_init(dm_crypt_init); |
| 2171 | module_exit(dm_crypt_exit); |
| 2172 | |
Jana Saout | bf14299 | 2014-06-24 14:27:04 -0400 | [diff] [blame] | 2173 | MODULE_AUTHOR("Jana Saout <jana@saout.de>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption"); |
| 2175 | MODULE_LICENSE("GPL"); |