11da177e4SLinus Torvalds /* 2bf14299fSJana Saout * Copyright (C) 2003 Jana Saout <jana@saout.de> 31da177e4SLinus Torvalds * Copyright (C) 2004 Clemens Fruhwirth <clemens@endorphin.org> 4bbb16584SMilan Broz * Copyright (C) 2006-2020 Red Hat, Inc. All rights reserved. 5bbb16584SMilan Broz * Copyright (C) 2013-2020 Milan Broz <gmazyland@gmail.com> 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * This file is released under the GPL. 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 1043d69034SMilan Broz #include <linux/completion.h> 11d1806f6aSHerbert Xu #include <linux/err.h> 121da177e4SLinus Torvalds #include <linux/module.h> 131da177e4SLinus Torvalds #include <linux/init.h> 141da177e4SLinus Torvalds #include <linux/kernel.h> 15c538f6ecSOndrej Kozina #include <linux/key.h> 161da177e4SLinus Torvalds #include <linux/bio.h> 171da177e4SLinus Torvalds #include <linux/blkdev.h> 18fe45e630SChristoph Hellwig #include <linux/blk-integrity.h> 191da177e4SLinus Torvalds #include <linux/mempool.h> 201da177e4SLinus Torvalds #include <linux/slab.h> 211da177e4SLinus Torvalds #include <linux/crypto.h> 221da177e4SLinus Torvalds #include <linux/workqueue.h> 23dc267621SMikulas Patocka #include <linux/kthread.h> 243fcfab16SAndrew Morton #include <linux/backing-dev.h> 2560063497SArun Sharma #include <linux/atomic.h> 26378f058cSDavid Hardeman #include <linux/scatterlist.h> 27b3c5fd30SMikulas Patocka #include <linux/rbtree.h> 28027c431cSOndrej Kozina #include <linux/ctype.h> 291da177e4SLinus Torvalds #include <asm/page.h> 3048527fa7SRik Snel #include <asm/unaligned.h> 3134745785SMilan Broz #include <crypto/hash.h> 3234745785SMilan Broz #include <crypto/md5.h> 3334745785SMilan Broz #include <crypto/algapi.h> 34bbdb23b5SHerbert Xu #include <crypto/skcipher.h> 35ef43aa38SMilan Broz #include <crypto/aead.h> 36ef43aa38SMilan Broz #include <crypto/authenc.h> 37ef43aa38SMilan Broz #include <linux/rtnetlink.h> /* for struct rtattr and RTA macros only */ 3827f5411aSDmitry Baryshkov #include <linux/key-type.h> 39c538f6ecSOndrej Kozina #include <keys/user-type.h> 4027f5411aSDmitry Baryshkov #include <keys/encrypted-type.h> 41363880c4SAhmad Fatoum #include <keys/trusted-type.h> 421da177e4SLinus Torvalds 43586e80e6SMikulas Patocka #include <linux/device-mapper.h> 441da177e4SLinus Torvalds 4558d0f180SMichael Weiß #include "dm-audit.h" 4658d0f180SMichael Weiß 4772d94861SAlasdair G Kergon #define DM_MSG_PREFIX "crypt" 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds /* 501da177e4SLinus Torvalds * context holding the current state of a multi-part conversion 511da177e4SLinus Torvalds */ 521da177e4SLinus Torvalds struct convert_context { 5343d69034SMilan Broz struct completion restart; 541da177e4SLinus Torvalds struct bio *bio_in; 551da177e4SLinus Torvalds struct bio *bio_out; 56003b5c57SKent Overstreet struct bvec_iter iter_in; 57003b5c57SKent Overstreet struct bvec_iter iter_out; 588d683dcdSAliOS system security u64 cc_sector; 5940b6229bSMikulas Patocka atomic_t cc_pending; 60ef43aa38SMilan Broz union { 61bbdb23b5SHerbert Xu struct skcipher_request *req; 62ef43aa38SMilan Broz struct aead_request *req_aead; 63ef43aa38SMilan Broz } r; 64ef43aa38SMilan Broz 651da177e4SLinus Torvalds }; 661da177e4SLinus Torvalds 6753017030SMilan Broz /* 6853017030SMilan Broz * per bio private data 6953017030SMilan Broz */ 7053017030SMilan Broz struct dm_crypt_io { 7149a8a920SAlasdair G Kergon struct crypt_config *cc; 7253017030SMilan Broz struct bio *base_bio; 73ef43aa38SMilan Broz u8 *integrity_metadata; 74ef43aa38SMilan Broz bool integrity_metadata_from_pool; 7553017030SMilan Broz struct work_struct work; 7639d42fa9SIgnat Korchagin struct tasklet_struct tasklet; 7753017030SMilan Broz 7853017030SMilan Broz struct convert_context ctx; 7953017030SMilan Broz 8040b6229bSMikulas Patocka atomic_t io_pending; 814e4cbee9SChristoph Hellwig blk_status_t error; 820c395b0fSMilan Broz sector_t sector; 83dc267621SMikulas Patocka 84b3c5fd30SMikulas Patocka struct rb_node rb_node; 85298a9fa0SMikulas Patocka } CRYPTO_MINALIGN_ATTR; 8653017030SMilan Broz 8701482b76SMilan Broz struct dm_crypt_request { 88b2174eebSHuang Ying struct convert_context *ctx; 89ef43aa38SMilan Broz struct scatterlist sg_in[4]; 90ef43aa38SMilan Broz struct scatterlist sg_out[4]; 918d683dcdSAliOS system security u64 iv_sector; 9201482b76SMilan Broz }; 9301482b76SMilan Broz 941da177e4SLinus Torvalds struct crypt_config; 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds struct crypt_iv_operations { 971da177e4SLinus Torvalds int (*ctr)(struct crypt_config *cc, struct dm_target *ti, 981da177e4SLinus Torvalds const char *opts); 991da177e4SLinus Torvalds void (*dtr)(struct crypt_config *cc); 100b95bf2d3SMilan Broz int (*init)(struct crypt_config *cc); 101542da317SMilan Broz int (*wipe)(struct crypt_config *cc); 1022dc5327dSMilan Broz int (*generator)(struct crypt_config *cc, u8 *iv, 1032dc5327dSMilan Broz struct dm_crypt_request *dmreq); 1042dc5327dSMilan Broz int (*post)(struct crypt_config *cc, u8 *iv, 1052dc5327dSMilan Broz struct dm_crypt_request *dmreq); 1061da177e4SLinus Torvalds }; 1071da177e4SLinus Torvalds 10860473592SMilan Broz struct iv_benbi_private { 10960473592SMilan Broz int shift; 11060473592SMilan Broz }; 11160473592SMilan Broz 11234745785SMilan Broz #define LMK_SEED_SIZE 64 /* hash + 0 */ 11334745785SMilan Broz struct iv_lmk_private { 11434745785SMilan Broz struct crypto_shash *hash_tfm; 11534745785SMilan Broz u8 *seed; 11634745785SMilan Broz }; 11734745785SMilan Broz 118ed04d981SMilan Broz #define TCW_WHITENING_SIZE 16 119ed04d981SMilan Broz struct iv_tcw_private { 120ed04d981SMilan Broz struct crypto_shash *crc32_tfm; 121ed04d981SMilan Broz u8 *iv_seed; 122ed04d981SMilan Broz u8 *whitening; 123ed04d981SMilan Broz }; 124ed04d981SMilan Broz 125bbb16584SMilan Broz #define ELEPHANT_MAX_KEY_SIZE 32 126bbb16584SMilan Broz struct iv_elephant_private { 127bbb16584SMilan Broz struct crypto_skcipher *tfm; 128bbb16584SMilan Broz }; 129bbb16584SMilan Broz 1301da177e4SLinus Torvalds /* 1311da177e4SLinus Torvalds * Crypt: maps a linear range of a block device 1321da177e4SLinus Torvalds * and encrypts / decrypts at the same time. 1331da177e4SLinus Torvalds */ 1340f5d8e6eSMikulas Patocka enum flags { DM_CRYPT_SUSPENDED, DM_CRYPT_KEY_VALID, 13539d42fa9SIgnat Korchagin DM_CRYPT_SAME_CPU, DM_CRYPT_NO_OFFLOAD, 1368e225f04SDamien Le Moal DM_CRYPT_NO_READ_WORKQUEUE, DM_CRYPT_NO_WRITE_WORKQUEUE, 1378e225f04SDamien Le Moal DM_CRYPT_WRITE_INLINE }; 138c0297721SAndi Kleen 139ef43aa38SMilan Broz enum cipher_flags { 14074d1da39SGeert Uytterhoeven CRYPT_MODE_INTEGRITY_AEAD, /* Use authenticated mode for cipher */ 1418f0009a2SMilan Broz CRYPT_IV_LARGE_SECTORS, /* Calculate IV from sector_size, not 512B sectors */ 142bbb16584SMilan Broz CRYPT_ENCRYPT_PREPROCESS, /* Must preprocess data for encryption (elephant) */ 143ef43aa38SMilan Broz }; 144ef43aa38SMilan Broz 145c0297721SAndi Kleen /* 146610f2de3SMikulas Patocka * The fields in here must be read only after initialization. 147c0297721SAndi Kleen */ 1481da177e4SLinus Torvalds struct crypt_config { 1491da177e4SLinus Torvalds struct dm_dev *dev; 1501da177e4SLinus Torvalds sector_t start; 1511da177e4SLinus Torvalds 1525059353dSMikulas Patocka struct percpu_counter n_allocated_pages; 1535059353dSMikulas Patocka 154cabf08e4SMilan Broz struct workqueue_struct *io_queue; 155cabf08e4SMilan Broz struct workqueue_struct *crypt_queue; 1563f1e9070SMilan Broz 157c7329effSMikulas Patocka spinlock_t write_thread_lock; 15872d711c8SMike Snitzer struct task_struct *write_thread; 159b3c5fd30SMikulas Patocka struct rb_root write_tree; 160dc267621SMikulas Patocka 1617dbcd137SMilan Broz char *cipher_string; 162ef43aa38SMilan Broz char *cipher_auth; 163c538f6ecSOndrej Kozina char *key_string; 1645ebaee6dSMilan Broz 1651b1b58f5SJulia Lawall const struct crypt_iv_operations *iv_gen_ops; 16679066ad3SHerbert Xu union { 16760473592SMilan Broz struct iv_benbi_private benbi; 16834745785SMilan Broz struct iv_lmk_private lmk; 169ed04d981SMilan Broz struct iv_tcw_private tcw; 170bbb16584SMilan Broz struct iv_elephant_private elephant; 17179066ad3SHerbert Xu } iv_gen_private; 1728d683dcdSAliOS system security u64 iv_offset; 1731da177e4SLinus Torvalds unsigned int iv_size; 174ff3af92bSMikulas Patocka unsigned short int sector_size; 175ff3af92bSMikulas Patocka unsigned char sector_shift; 1761da177e4SLinus Torvalds 177ef43aa38SMilan Broz union { 178bbdb23b5SHerbert Xu struct crypto_skcipher **tfms; 179ef43aa38SMilan Broz struct crypto_aead **tfms_aead; 180ef43aa38SMilan Broz } cipher_tfm; 181d1f96423SMilan Broz unsigned tfms_count; 182ef43aa38SMilan Broz unsigned long cipher_flags; 183c0297721SAndi Kleen 184c0297721SAndi Kleen /* 185ddd42edfSMilan Broz * Layout of each crypto request: 186ddd42edfSMilan Broz * 187bbdb23b5SHerbert Xu * struct skcipher_request 188ddd42edfSMilan Broz * context 189ddd42edfSMilan Broz * padding 190ddd42edfSMilan Broz * struct dm_crypt_request 191ddd42edfSMilan Broz * padding 192ddd42edfSMilan Broz * IV 193ddd42edfSMilan Broz * 194ddd42edfSMilan Broz * The padding is added so that dm_crypt_request and the IV are 195ddd42edfSMilan Broz * correctly aligned. 196ddd42edfSMilan Broz */ 197ddd42edfSMilan Broz unsigned int dmreq_start; 198ddd42edfSMilan Broz 199298a9fa0SMikulas Patocka unsigned int per_bio_data_size; 200298a9fa0SMikulas Patocka 201e48d4bbfSMilan Broz unsigned long flags; 2021da177e4SLinus Torvalds unsigned int key_size; 203da31a078SMilan Broz unsigned int key_parts; /* independent parts in key buffer */ 204da31a078SMilan Broz unsigned int key_extra_size; /* additional keys length */ 205ef43aa38SMilan Broz unsigned int key_mac_size; /* MAC key size for authenc(...) */ 206ef43aa38SMilan Broz 207ef43aa38SMilan Broz unsigned int integrity_tag_size; 208ef43aa38SMilan Broz unsigned int integrity_iv_size; 209ef43aa38SMilan Broz unsigned int on_disk_tag_size; 210ef43aa38SMilan Broz 21172d711c8SMike Snitzer /* 21272d711c8SMike Snitzer * pool for per bio private data, crypto requests, 21372d711c8SMike Snitzer * encryption requeusts/buffer pages and integrity tags 21472d711c8SMike Snitzer */ 21572d711c8SMike Snitzer unsigned tag_pool_max_sectors; 21672d711c8SMike Snitzer mempool_t tag_pool; 21772d711c8SMike Snitzer mempool_t req_pool; 21872d711c8SMike Snitzer mempool_t page_pool; 21972d711c8SMike Snitzer 22072d711c8SMike Snitzer struct bio_set bs; 22172d711c8SMike Snitzer struct mutex bio_alloc_lock; 22272d711c8SMike Snitzer 223ef43aa38SMilan Broz u8 *authenc_key; /* space for keys in authenc() format (if used) */ 224b18ae8ddSGustavo A. R. Silva u8 key[]; 2251da177e4SLinus Torvalds }; 2261da177e4SLinus Torvalds 2270a83df6cSMikulas Patocka #define MIN_IOS 64 228ef43aa38SMilan Broz #define MAX_TAG_SIZE 480 229ef43aa38SMilan Broz #define POOL_ENTRY_SIZE 512 2301da177e4SLinus Torvalds 2315059353dSMikulas Patocka static DEFINE_SPINLOCK(dm_crypt_clients_lock); 2325059353dSMikulas Patocka static unsigned dm_crypt_clients_n = 0; 2335059353dSMikulas Patocka static volatile unsigned long dm_crypt_pages_per_client; 2345059353dSMikulas Patocka #define DM_CRYPT_MEMORY_PERCENT 2 235a8affc03SChristoph Hellwig #define DM_CRYPT_MIN_PAGES_PER_CLIENT (BIO_MAX_VECS * 16) 2365059353dSMikulas Patocka 2373f868c09SChristoph Hellwig static void crypt_endio(struct bio *clone); 238395b167cSAlasdair G Kergon static void kcryptd_queue_crypt(struct dm_crypt_io *io); 239ef43aa38SMilan Broz static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc, 240ef43aa38SMilan Broz struct scatterlist *sg); 241027581f3SOlaf Kirch 2423fd53533SYang Yingliang static bool crypt_integrity_aead(struct crypt_config *cc); 2433fd53533SYang Yingliang 244c0297721SAndi Kleen /* 24586f917adSEric Biggers * Use this to access cipher attributes that are independent of the key. 246c0297721SAndi Kleen */ 247bbdb23b5SHerbert Xu static struct crypto_skcipher *any_tfm(struct crypt_config *cc) 248c0297721SAndi Kleen { 249ef43aa38SMilan Broz return cc->cipher_tfm.tfms[0]; 250ef43aa38SMilan Broz } 251ef43aa38SMilan Broz 252ef43aa38SMilan Broz static struct crypto_aead *any_tfm_aead(struct crypt_config *cc) 253ef43aa38SMilan Broz { 254ef43aa38SMilan Broz return cc->cipher_tfm.tfms_aead[0]; 255c0297721SAndi Kleen } 256c0297721SAndi Kleen 2571da177e4SLinus Torvalds /* 2581da177e4SLinus Torvalds * Different IV generation algorithms: 2591da177e4SLinus Torvalds * 2603c164bd8SRik Snel * plain: the initial vector is the 32-bit little-endian version of the sector 2613a4fa0a2SRobert P. J. Day * number, padded with zeros if necessary. 2621da177e4SLinus Torvalds * 26361afef61SMilan Broz * plain64: the initial vector is the 64-bit little-endian version of the sector 26461afef61SMilan Broz * number, padded with zeros if necessary. 26561afef61SMilan Broz * 2667e3fd855SMilan Broz * plain64be: the initial vector is the 64-bit big-endian version of the sector 2677e3fd855SMilan Broz * number, padded with zeros if necessary. 2687e3fd855SMilan Broz * 2693c164bd8SRik Snel * essiv: "encrypted sector|salt initial vector", the sector number is 2701da177e4SLinus Torvalds * encrypted with the bulk cipher using a salt as key. The salt 2711da177e4SLinus Torvalds * should be derived from the bulk cipher's key via hashing. 2721da177e4SLinus Torvalds * 27348527fa7SRik Snel * benbi: the 64-bit "big-endian 'narrow block'-count", starting at 1 27448527fa7SRik Snel * (needed for LRW-32-AES and possible other narrow block modes) 27548527fa7SRik Snel * 27646b47730SLudwig Nussel * null: the initial vector is always zero. Provides compatibility with 27746b47730SLudwig Nussel * obsolete loop_fish2 devices. Do not use for new devices. 27846b47730SLudwig Nussel * 27934745785SMilan Broz * lmk: Compatible implementation of the block chaining mode used 28034745785SMilan Broz * by the Loop-AES block device encryption system 28134745785SMilan Broz * designed by Jari Ruusu. See http://loop-aes.sourceforge.net/ 28234745785SMilan Broz * It operates on full 512 byte sectors and uses CBC 28334745785SMilan Broz * with an IV derived from the sector number, the data and 28434745785SMilan Broz * optionally extra IV seed. 28534745785SMilan Broz * This means that after decryption the first block 28634745785SMilan Broz * of sector must be tweaked according to decrypted data. 28734745785SMilan Broz * Loop-AES can use three encryption schemes: 28834745785SMilan Broz * version 1: is plain aes-cbc mode 28934745785SMilan Broz * version 2: uses 64 multikey scheme with lmk IV generator 29034745785SMilan Broz * version 3: the same as version 2 with additional IV seed 29134745785SMilan Broz * (it uses 65 keys, last key is used as IV seed) 29234745785SMilan Broz * 293ed04d981SMilan Broz * tcw: Compatible implementation of the block chaining mode used 294ed04d981SMilan Broz * by the TrueCrypt device encryption system (prior to version 4.1). 295e44f23b3SMilan Broz * For more info see: https://gitlab.com/cryptsetup/cryptsetup/wikis/TrueCryptOnDiskFormat 296ed04d981SMilan Broz * It operates on full 512 byte sectors and uses CBC 297ed04d981SMilan Broz * with an IV derived from initial key and the sector number. 298ed04d981SMilan Broz * In addition, whitening value is applied on every sector, whitening 299ed04d981SMilan Broz * is calculated from initial key, sector number and mixed using CRC32. 300ed04d981SMilan Broz * Note that this encryption scheme is vulnerable to watermarking attacks 301ed04d981SMilan Broz * and should be used for old compatible containers access only. 302b9411d73SMilan Broz * 303b9411d73SMilan Broz * eboiv: Encrypted byte-offset IV (used in Bitlocker in CBC mode) 304b9411d73SMilan Broz * The IV is encrypted little-endian byte-offset (with the same key 305b9411d73SMilan Broz * and cipher as the volume). 306bbb16584SMilan Broz * 307bbb16584SMilan Broz * elephant: The extended version of eboiv with additional Elephant diffuser 308bbb16584SMilan Broz * used with Bitlocker CBC mode. 309bbb16584SMilan Broz * This mode was used in older Windows systems 3106f3bc22bSAlexander A. Klimov * https://download.microsoft.com/download/0/2/3/0238acaf-d3bf-4a6d-b3d6-0a0be4bbb36e/bitlockercipher200608.pdf 3111da177e4SLinus Torvalds */ 3121da177e4SLinus Torvalds 3132dc5327dSMilan Broz static int crypt_iv_plain_gen(struct crypt_config *cc, u8 *iv, 3142dc5327dSMilan Broz struct dm_crypt_request *dmreq) 3151da177e4SLinus Torvalds { 3161da177e4SLinus Torvalds memset(iv, 0, cc->iv_size); 317283a8328SAlasdair G Kergon *(__le32 *)iv = cpu_to_le32(dmreq->iv_sector & 0xffffffff); 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds return 0; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 32261afef61SMilan Broz static int crypt_iv_plain64_gen(struct crypt_config *cc, u8 *iv, 3232dc5327dSMilan Broz struct dm_crypt_request *dmreq) 32461afef61SMilan Broz { 32561afef61SMilan Broz memset(iv, 0, cc->iv_size); 326283a8328SAlasdair G Kergon *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector); 32761afef61SMilan Broz 32861afef61SMilan Broz return 0; 32961afef61SMilan Broz } 33061afef61SMilan Broz 3317e3fd855SMilan Broz static int crypt_iv_plain64be_gen(struct crypt_config *cc, u8 *iv, 3327e3fd855SMilan Broz struct dm_crypt_request *dmreq) 3337e3fd855SMilan Broz { 3347e3fd855SMilan Broz memset(iv, 0, cc->iv_size); 3357e3fd855SMilan Broz /* iv_size is at least of size u64; usually it is 16 bytes */ 3367e3fd855SMilan Broz *(__be64 *)&iv[cc->iv_size - sizeof(u64)] = cpu_to_be64(dmreq->iv_sector); 3377e3fd855SMilan Broz 3387e3fd855SMilan Broz return 0; 3397e3fd855SMilan Broz } 3407e3fd855SMilan Broz 3412dc5327dSMilan Broz static int crypt_iv_essiv_gen(struct crypt_config *cc, u8 *iv, 3422dc5327dSMilan Broz struct dm_crypt_request *dmreq) 3431da177e4SLinus Torvalds { 344a1a262b6SArd Biesheuvel /* 345a1a262b6SArd Biesheuvel * ESSIV encryption of the IV is now handled by the crypto API, 346a1a262b6SArd Biesheuvel * so just pass the plain sector number here. 347a1a262b6SArd Biesheuvel */ 3481da177e4SLinus Torvalds memset(iv, 0, cc->iv_size); 349283a8328SAlasdair G Kergon *(__le64 *)iv = cpu_to_le64(dmreq->iv_sector); 350c0297721SAndi Kleen 3511da177e4SLinus Torvalds return 0; 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 35448527fa7SRik Snel static int crypt_iv_benbi_ctr(struct crypt_config *cc, struct dm_target *ti, 35548527fa7SRik Snel const char *opts) 35648527fa7SRik Snel { 3574ea9471fSMilan Broz unsigned bs; 3584ea9471fSMilan Broz int log; 3594ea9471fSMilan Broz 3603fd53533SYang Yingliang if (crypt_integrity_aead(cc)) 3614ea9471fSMilan Broz bs = crypto_aead_blocksize(any_tfm_aead(cc)); 3624ea9471fSMilan Broz else 3634ea9471fSMilan Broz bs = crypto_skcipher_blocksize(any_tfm(cc)); 3644ea9471fSMilan Broz log = ilog2(bs); 36548527fa7SRik Snel 36648527fa7SRik Snel /* we need to calculate how far we must shift the sector count 36748527fa7SRik Snel * to get the cipher block count, we use this shift in _gen */ 36848527fa7SRik Snel 36948527fa7SRik Snel if (1 << log != bs) { 37048527fa7SRik Snel ti->error = "cypher blocksize is not a power of 2"; 37148527fa7SRik Snel return -EINVAL; 37248527fa7SRik Snel } 37348527fa7SRik Snel 37448527fa7SRik Snel if (log > 9) { 37548527fa7SRik Snel ti->error = "cypher blocksize is > 512"; 37648527fa7SRik Snel return -EINVAL; 37748527fa7SRik Snel } 37848527fa7SRik Snel 37960473592SMilan Broz cc->iv_gen_private.benbi.shift = 9 - log; 38048527fa7SRik Snel 38148527fa7SRik Snel return 0; 38248527fa7SRik Snel } 38348527fa7SRik Snel 38448527fa7SRik Snel static void crypt_iv_benbi_dtr(struct crypt_config *cc) 38548527fa7SRik Snel { 38648527fa7SRik Snel } 38748527fa7SRik Snel 3882dc5327dSMilan Broz static int crypt_iv_benbi_gen(struct crypt_config *cc, u8 *iv, 3892dc5327dSMilan Broz struct dm_crypt_request *dmreq) 39048527fa7SRik Snel { 39179066ad3SHerbert Xu __be64 val; 39279066ad3SHerbert Xu 39348527fa7SRik Snel memset(iv, 0, cc->iv_size - sizeof(u64)); /* rest is cleared below */ 39479066ad3SHerbert Xu 3952dc5327dSMilan Broz val = cpu_to_be64(((u64)dmreq->iv_sector << cc->iv_gen_private.benbi.shift) + 1); 39679066ad3SHerbert Xu put_unaligned(val, (__be64 *)(iv + cc->iv_size - sizeof(u64))); 39748527fa7SRik Snel 3981da177e4SLinus Torvalds return 0; 3991da177e4SLinus Torvalds } 4001da177e4SLinus Torvalds 4012dc5327dSMilan Broz static int crypt_iv_null_gen(struct crypt_config *cc, u8 *iv, 4022dc5327dSMilan Broz struct dm_crypt_request *dmreq) 40346b47730SLudwig Nussel { 40446b47730SLudwig Nussel memset(iv, 0, cc->iv_size); 40546b47730SLudwig Nussel 40646b47730SLudwig Nussel return 0; 40746b47730SLudwig Nussel } 40846b47730SLudwig Nussel 40934745785SMilan Broz static void crypt_iv_lmk_dtr(struct crypt_config *cc) 41034745785SMilan Broz { 41134745785SMilan Broz struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; 41234745785SMilan Broz 41334745785SMilan Broz if (lmk->hash_tfm && !IS_ERR(lmk->hash_tfm)) 41434745785SMilan Broz crypto_free_shash(lmk->hash_tfm); 41534745785SMilan Broz lmk->hash_tfm = NULL; 41634745785SMilan Broz 417453431a5SWaiman Long kfree_sensitive(lmk->seed); 41834745785SMilan Broz lmk->seed = NULL; 41934745785SMilan Broz } 42034745785SMilan Broz 42134745785SMilan Broz static int crypt_iv_lmk_ctr(struct crypt_config *cc, struct dm_target *ti, 42234745785SMilan Broz const char *opts) 42334745785SMilan Broz { 42434745785SMilan Broz struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; 42534745785SMilan Broz 4268f0009a2SMilan Broz if (cc->sector_size != (1 << SECTOR_SHIFT)) { 4278f0009a2SMilan Broz ti->error = "Unsupported sector size for LMK"; 4288f0009a2SMilan Broz return -EINVAL; 4298f0009a2SMilan Broz } 4308f0009a2SMilan Broz 431cd746938SMikulas Patocka lmk->hash_tfm = crypto_alloc_shash("md5", 0, 432cd746938SMikulas Patocka CRYPTO_ALG_ALLOCATES_MEMORY); 43334745785SMilan Broz if (IS_ERR(lmk->hash_tfm)) { 43434745785SMilan Broz ti->error = "Error initializing LMK hash"; 43534745785SMilan Broz return PTR_ERR(lmk->hash_tfm); 43634745785SMilan Broz } 43734745785SMilan Broz 43834745785SMilan Broz /* No seed in LMK version 2 */ 43934745785SMilan Broz if (cc->key_parts == cc->tfms_count) { 44034745785SMilan Broz lmk->seed = NULL; 44134745785SMilan Broz return 0; 44234745785SMilan Broz } 44334745785SMilan Broz 44434745785SMilan Broz lmk->seed = kzalloc(LMK_SEED_SIZE, GFP_KERNEL); 44534745785SMilan Broz if (!lmk->seed) { 44634745785SMilan Broz crypt_iv_lmk_dtr(cc); 44734745785SMilan Broz ti->error = "Error kmallocing seed storage in LMK"; 44834745785SMilan Broz return -ENOMEM; 44934745785SMilan Broz } 45034745785SMilan Broz 45134745785SMilan Broz return 0; 45234745785SMilan Broz } 45334745785SMilan Broz 45434745785SMilan Broz static int crypt_iv_lmk_init(struct crypt_config *cc) 45534745785SMilan Broz { 45634745785SMilan Broz struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; 45734745785SMilan Broz int subkey_size = cc->key_size / cc->key_parts; 45834745785SMilan Broz 45934745785SMilan Broz /* LMK seed is on the position of LMK_KEYS + 1 key */ 46034745785SMilan Broz if (lmk->seed) 46134745785SMilan Broz memcpy(lmk->seed, cc->key + (cc->tfms_count * subkey_size), 46234745785SMilan Broz crypto_shash_digestsize(lmk->hash_tfm)); 46334745785SMilan Broz 46434745785SMilan Broz return 0; 46534745785SMilan Broz } 46634745785SMilan Broz 46734745785SMilan Broz static int crypt_iv_lmk_wipe(struct crypt_config *cc) 46834745785SMilan Broz { 46934745785SMilan Broz struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; 47034745785SMilan Broz 47134745785SMilan Broz if (lmk->seed) 47234745785SMilan Broz memset(lmk->seed, 0, LMK_SEED_SIZE); 47334745785SMilan Broz 47434745785SMilan Broz return 0; 47534745785SMilan Broz } 47634745785SMilan Broz 47734745785SMilan Broz static int crypt_iv_lmk_one(struct crypt_config *cc, u8 *iv, 47834745785SMilan Broz struct dm_crypt_request *dmreq, 47934745785SMilan Broz u8 *data) 48034745785SMilan Broz { 48134745785SMilan Broz struct iv_lmk_private *lmk = &cc->iv_gen_private.lmk; 482b6106265SJan-Simon Möller SHASH_DESC_ON_STACK(desc, lmk->hash_tfm); 48334745785SMilan Broz struct md5_state md5state; 484da31a078SMilan Broz __le32 buf[4]; 48534745785SMilan Broz int i, r; 48634745785SMilan Broz 487b6106265SJan-Simon Möller desc->tfm = lmk->hash_tfm; 48834745785SMilan Broz 489b6106265SJan-Simon Möller r = crypto_shash_init(desc); 49034745785SMilan Broz if (r) 49134745785SMilan Broz return r; 49234745785SMilan Broz 49334745785SMilan Broz if (lmk->seed) { 494b6106265SJan-Simon Möller r = crypto_shash_update(desc, lmk->seed, LMK_SEED_SIZE); 49534745785SMilan Broz if (r) 49634745785SMilan Broz return r; 49734745785SMilan Broz } 49834745785SMilan Broz 49934745785SMilan Broz /* Sector is always 512B, block size 16, add data of blocks 1-31 */ 500b6106265SJan-Simon Möller r = crypto_shash_update(desc, data + 16, 16 * 31); 50134745785SMilan Broz if (r) 50234745785SMilan Broz return r; 50334745785SMilan Broz 50434745785SMilan Broz /* Sector is cropped to 56 bits here */ 50534745785SMilan Broz buf[0] = cpu_to_le32(dmreq->iv_sector & 0xFFFFFFFF); 50634745785SMilan Broz buf[1] = cpu_to_le32((((u64)dmreq->iv_sector >> 32) & 0x00FFFFFF) | 0x80000000); 50734745785SMilan Broz buf[2] = cpu_to_le32(4024); 50834745785SMilan Broz buf[3] = 0; 509b6106265SJan-Simon Möller r = crypto_shash_update(desc, (u8 *)buf, sizeof(buf)); 51034745785SMilan Broz if (r) 51134745785SMilan Broz return r; 51234745785SMilan Broz 51334745785SMilan Broz /* No MD5 padding here */ 514b6106265SJan-Simon Möller r = crypto_shash_export(desc, &md5state); 51534745785SMilan Broz if (r) 51634745785SMilan Broz return r; 51734745785SMilan Broz 51834745785SMilan Broz for (i = 0; i < MD5_HASH_WORDS; i++) 51934745785SMilan Broz __cpu_to_le32s(&md5state.hash[i]); 52034745785SMilan Broz memcpy(iv, &md5state.hash, cc->iv_size); 52134745785SMilan Broz 52234745785SMilan Broz return 0; 52334745785SMilan Broz } 52434745785SMilan Broz 52534745785SMilan Broz static int crypt_iv_lmk_gen(struct crypt_config *cc, u8 *iv, 52634745785SMilan Broz struct dm_crypt_request *dmreq) 52734745785SMilan Broz { 528ef43aa38SMilan Broz struct scatterlist *sg; 52934745785SMilan Broz u8 *src; 53034745785SMilan Broz int r = 0; 53134745785SMilan Broz 53234745785SMilan Broz if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) { 533ef43aa38SMilan Broz sg = crypt_get_sg_data(cc, dmreq->sg_in); 534ef43aa38SMilan Broz src = kmap_atomic(sg_page(sg)); 535ef43aa38SMilan Broz r = crypt_iv_lmk_one(cc, iv, dmreq, src + sg->offset); 536c2e022cbSCong Wang kunmap_atomic(src); 53734745785SMilan Broz } else 53834745785SMilan Broz memset(iv, 0, cc->iv_size); 53934745785SMilan Broz 54034745785SMilan Broz return r; 54134745785SMilan Broz } 54234745785SMilan Broz 54334745785SMilan Broz static int crypt_iv_lmk_post(struct crypt_config *cc, u8 *iv, 54434745785SMilan Broz struct dm_crypt_request *dmreq) 54534745785SMilan Broz { 546ef43aa38SMilan Broz struct scatterlist *sg; 54734745785SMilan Broz u8 *dst; 54834745785SMilan Broz int r; 54934745785SMilan Broz 55034745785SMilan Broz if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) 55134745785SMilan Broz return 0; 55234745785SMilan Broz 553ef43aa38SMilan Broz sg = crypt_get_sg_data(cc, dmreq->sg_out); 554ef43aa38SMilan Broz dst = kmap_atomic(sg_page(sg)); 555ef43aa38SMilan Broz r = crypt_iv_lmk_one(cc, iv, dmreq, dst + sg->offset); 55634745785SMilan Broz 55734745785SMilan Broz /* Tweak the first block of plaintext sector */ 55834745785SMilan Broz if (!r) 559ef43aa38SMilan Broz crypto_xor(dst + sg->offset, iv, cc->iv_size); 56034745785SMilan Broz 561c2e022cbSCong Wang kunmap_atomic(dst); 56234745785SMilan Broz return r; 56334745785SMilan Broz } 56434745785SMilan Broz 565ed04d981SMilan Broz static void crypt_iv_tcw_dtr(struct crypt_config *cc) 566ed04d981SMilan Broz { 567ed04d981SMilan Broz struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; 568ed04d981SMilan Broz 569453431a5SWaiman Long kfree_sensitive(tcw->iv_seed); 570ed04d981SMilan Broz tcw->iv_seed = NULL; 571453431a5SWaiman Long kfree_sensitive(tcw->whitening); 572ed04d981SMilan Broz tcw->whitening = NULL; 573ed04d981SMilan Broz 574ed04d981SMilan Broz if (tcw->crc32_tfm && !IS_ERR(tcw->crc32_tfm)) 575ed04d981SMilan Broz crypto_free_shash(tcw->crc32_tfm); 576ed04d981SMilan Broz tcw->crc32_tfm = NULL; 577ed04d981SMilan Broz } 578ed04d981SMilan Broz 579ed04d981SMilan Broz static int crypt_iv_tcw_ctr(struct crypt_config *cc, struct dm_target *ti, 580ed04d981SMilan Broz const char *opts) 581ed04d981SMilan Broz { 582ed04d981SMilan Broz struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; 583ed04d981SMilan Broz 5848f0009a2SMilan Broz if (cc->sector_size != (1 << SECTOR_SHIFT)) { 5858f0009a2SMilan Broz ti->error = "Unsupported sector size for TCW"; 5868f0009a2SMilan Broz return -EINVAL; 5878f0009a2SMilan Broz } 5888f0009a2SMilan Broz 589ed04d981SMilan Broz if (cc->key_size <= (cc->iv_size + TCW_WHITENING_SIZE)) { 590ed04d981SMilan Broz ti->error = "Wrong key size for TCW"; 591ed04d981SMilan Broz return -EINVAL; 592ed04d981SMilan Broz } 593ed04d981SMilan Broz 594cd746938SMikulas Patocka tcw->crc32_tfm = crypto_alloc_shash("crc32", 0, 595cd746938SMikulas Patocka CRYPTO_ALG_ALLOCATES_MEMORY); 596ed04d981SMilan Broz if (IS_ERR(tcw->crc32_tfm)) { 597ed04d981SMilan Broz ti->error = "Error initializing CRC32 in TCW"; 598ed04d981SMilan Broz return PTR_ERR(tcw->crc32_tfm); 599ed04d981SMilan Broz } 600ed04d981SMilan Broz 601ed04d981SMilan Broz tcw->iv_seed = kzalloc(cc->iv_size, GFP_KERNEL); 602ed04d981SMilan Broz tcw->whitening = kzalloc(TCW_WHITENING_SIZE, GFP_KERNEL); 603ed04d981SMilan Broz if (!tcw->iv_seed || !tcw->whitening) { 604ed04d981SMilan Broz crypt_iv_tcw_dtr(cc); 605ed04d981SMilan Broz ti->error = "Error allocating seed storage in TCW"; 606ed04d981SMilan Broz return -ENOMEM; 607ed04d981SMilan Broz } 608ed04d981SMilan Broz 609ed04d981SMilan Broz return 0; 610ed04d981SMilan Broz } 611ed04d981SMilan Broz 612ed04d981SMilan Broz static int crypt_iv_tcw_init(struct crypt_config *cc) 613ed04d981SMilan Broz { 614ed04d981SMilan Broz struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; 615ed04d981SMilan Broz int key_offset = cc->key_size - cc->iv_size - TCW_WHITENING_SIZE; 616ed04d981SMilan Broz 617ed04d981SMilan Broz memcpy(tcw->iv_seed, &cc->key[key_offset], cc->iv_size); 618ed04d981SMilan Broz memcpy(tcw->whitening, &cc->key[key_offset + cc->iv_size], 619ed04d981SMilan Broz TCW_WHITENING_SIZE); 620ed04d981SMilan Broz 621ed04d981SMilan Broz return 0; 622ed04d981SMilan Broz } 623ed04d981SMilan Broz 624ed04d981SMilan Broz static int crypt_iv_tcw_wipe(struct crypt_config *cc) 625ed04d981SMilan Broz { 626ed04d981SMilan Broz struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; 627ed04d981SMilan Broz 628ed04d981SMilan Broz memset(tcw->iv_seed, 0, cc->iv_size); 629ed04d981SMilan Broz memset(tcw->whitening, 0, TCW_WHITENING_SIZE); 630ed04d981SMilan Broz 631ed04d981SMilan Broz return 0; 632ed04d981SMilan Broz } 633ed04d981SMilan Broz 634ed04d981SMilan Broz static int crypt_iv_tcw_whitening(struct crypt_config *cc, 635ed04d981SMilan Broz struct dm_crypt_request *dmreq, 636ed04d981SMilan Broz u8 *data) 637ed04d981SMilan Broz { 638ed04d981SMilan Broz struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; 639350b5393SBart Van Assche __le64 sector = cpu_to_le64(dmreq->iv_sector); 640ed04d981SMilan Broz u8 buf[TCW_WHITENING_SIZE]; 641b6106265SJan-Simon Möller SHASH_DESC_ON_STACK(desc, tcw->crc32_tfm); 642ed04d981SMilan Broz int i, r; 643ed04d981SMilan Broz 644ed04d981SMilan Broz /* xor whitening with sector number */ 64545fe93dfSArd Biesheuvel crypto_xor_cpy(buf, tcw->whitening, (u8 *)§or, 8); 64645fe93dfSArd Biesheuvel crypto_xor_cpy(&buf[8], tcw->whitening + 8, (u8 *)§or, 8); 647ed04d981SMilan Broz 648ed04d981SMilan Broz /* calculate crc32 for every 32bit part and xor it */ 649b6106265SJan-Simon Möller desc->tfm = tcw->crc32_tfm; 650ed04d981SMilan Broz for (i = 0; i < 4; i++) { 651b6106265SJan-Simon Möller r = crypto_shash_init(desc); 652ed04d981SMilan Broz if (r) 653ed04d981SMilan Broz goto out; 654b6106265SJan-Simon Möller r = crypto_shash_update(desc, &buf[i * 4], 4); 655ed04d981SMilan Broz if (r) 656ed04d981SMilan Broz goto out; 657b6106265SJan-Simon Möller r = crypto_shash_final(desc, &buf[i * 4]); 658ed04d981SMilan Broz if (r) 659ed04d981SMilan Broz goto out; 660ed04d981SMilan Broz } 661ed04d981SMilan Broz crypto_xor(&buf[0], &buf[12], 4); 662ed04d981SMilan Broz crypto_xor(&buf[4], &buf[8], 4); 663ed04d981SMilan Broz 664ed04d981SMilan Broz /* apply whitening (8 bytes) to whole sector */ 665ed04d981SMilan Broz for (i = 0; i < ((1 << SECTOR_SHIFT) / 8); i++) 666ed04d981SMilan Broz crypto_xor(data + i * 8, buf, 8); 667ed04d981SMilan Broz out: 6681a71d6ffSMilan Broz memzero_explicit(buf, sizeof(buf)); 669ed04d981SMilan Broz return r; 670ed04d981SMilan Broz } 671ed04d981SMilan Broz 672ed04d981SMilan Broz static int crypt_iv_tcw_gen(struct crypt_config *cc, u8 *iv, 673ed04d981SMilan Broz struct dm_crypt_request *dmreq) 674ed04d981SMilan Broz { 675ef43aa38SMilan Broz struct scatterlist *sg; 676ed04d981SMilan Broz struct iv_tcw_private *tcw = &cc->iv_gen_private.tcw; 677350b5393SBart Van Assche __le64 sector = cpu_to_le64(dmreq->iv_sector); 678ed04d981SMilan Broz u8 *src; 679ed04d981SMilan Broz int r = 0; 680ed04d981SMilan Broz 681ed04d981SMilan Broz /* Remove whitening from ciphertext */ 682ed04d981SMilan Broz if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) { 683ef43aa38SMilan Broz sg = crypt_get_sg_data(cc, dmreq->sg_in); 684ef43aa38SMilan Broz src = kmap_atomic(sg_page(sg)); 685ef43aa38SMilan Broz r = crypt_iv_tcw_whitening(cc, dmreq, src + sg->offset); 686ed04d981SMilan Broz kunmap_atomic(src); 687ed04d981SMilan Broz } 688ed04d981SMilan Broz 689ed04d981SMilan Broz /* Calculate IV */ 69045fe93dfSArd Biesheuvel crypto_xor_cpy(iv, tcw->iv_seed, (u8 *)§or, 8); 691ed04d981SMilan Broz if (cc->iv_size > 8) 69245fe93dfSArd Biesheuvel crypto_xor_cpy(&iv[8], tcw->iv_seed + 8, (u8 *)§or, 69345fe93dfSArd Biesheuvel cc->iv_size - 8); 694ed04d981SMilan Broz 695ed04d981SMilan Broz return r; 696ed04d981SMilan Broz } 697ed04d981SMilan Broz 698ed04d981SMilan Broz static int crypt_iv_tcw_post(struct crypt_config *cc, u8 *iv, 699ed04d981SMilan Broz struct dm_crypt_request *dmreq) 700ed04d981SMilan Broz { 701ef43aa38SMilan Broz struct scatterlist *sg; 702ed04d981SMilan Broz u8 *dst; 703ed04d981SMilan Broz int r; 704ed04d981SMilan Broz 705ed04d981SMilan Broz if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) 706ed04d981SMilan Broz return 0; 707ed04d981SMilan Broz 708ed04d981SMilan Broz /* Apply whitening on ciphertext */ 709ef43aa38SMilan Broz sg = crypt_get_sg_data(cc, dmreq->sg_out); 710ef43aa38SMilan Broz dst = kmap_atomic(sg_page(sg)); 711ef43aa38SMilan Broz r = crypt_iv_tcw_whitening(cc, dmreq, dst + sg->offset); 712ed04d981SMilan Broz kunmap_atomic(dst); 713ed04d981SMilan Broz 714ed04d981SMilan Broz return r; 715ed04d981SMilan Broz } 716ed04d981SMilan Broz 717ef43aa38SMilan Broz static int crypt_iv_random_gen(struct crypt_config *cc, u8 *iv, 718ef43aa38SMilan Broz struct dm_crypt_request *dmreq) 719ef43aa38SMilan Broz { 720ef43aa38SMilan Broz /* Used only for writes, there must be an additional space to store IV */ 721ef43aa38SMilan Broz get_random_bytes(iv, cc->iv_size); 722ef43aa38SMilan Broz return 0; 723ef43aa38SMilan Broz } 724ef43aa38SMilan Broz 725b9411d73SMilan Broz static int crypt_iv_eboiv_ctr(struct crypt_config *cc, struct dm_target *ti, 726b9411d73SMilan Broz const char *opts) 727b9411d73SMilan Broz { 7283fd53533SYang Yingliang if (crypt_integrity_aead(cc)) { 72939d13a1aSArd Biesheuvel ti->error = "AEAD transforms not supported for EBOIV"; 730b9411d73SMilan Broz return -EINVAL; 731b9411d73SMilan Broz } 732b9411d73SMilan Broz 73339d13a1aSArd Biesheuvel if (crypto_skcipher_blocksize(any_tfm(cc)) != cc->iv_size) { 73439d13a1aSArd Biesheuvel ti->error = "Block size of EBOIV cipher does " 73539d13a1aSArd Biesheuvel "not match IV size of block cipher"; 73639d13a1aSArd Biesheuvel return -EINVAL; 737b9411d73SMilan Broz } 738b9411d73SMilan Broz 739b9411d73SMilan Broz return 0; 740b9411d73SMilan Broz } 741b9411d73SMilan Broz 742b9411d73SMilan Broz static int crypt_iv_eboiv_gen(struct crypt_config *cc, u8 *iv, 743b9411d73SMilan Broz struct dm_crypt_request *dmreq) 744b9411d73SMilan Broz { 74539d13a1aSArd Biesheuvel u8 buf[MAX_CIPHER_BLOCKSIZE] __aligned(__alignof__(__le64)); 74639d13a1aSArd Biesheuvel struct skcipher_request *req; 74739d13a1aSArd Biesheuvel struct scatterlist src, dst; 7487785a9e4SDamien Le Moal DECLARE_CRYPTO_WAIT(wait); 74939d13a1aSArd Biesheuvel int err; 750b9411d73SMilan Broz 7519402e959SMikulas Patocka req = skcipher_request_alloc(any_tfm(cc), GFP_NOIO); 75239d13a1aSArd Biesheuvel if (!req) 75339d13a1aSArd Biesheuvel return -ENOMEM; 754b9411d73SMilan Broz 75539d13a1aSArd Biesheuvel memset(buf, 0, cc->iv_size); 75639d13a1aSArd Biesheuvel *(__le64 *)buf = cpu_to_le64(dmreq->iv_sector * cc->sector_size); 75739d13a1aSArd Biesheuvel 75839d13a1aSArd Biesheuvel sg_init_one(&src, page_address(ZERO_PAGE(0)), cc->iv_size); 75939d13a1aSArd Biesheuvel sg_init_one(&dst, iv, cc->iv_size); 76039d13a1aSArd Biesheuvel skcipher_request_set_crypt(req, &src, &dst, cc->iv_size, buf); 76139d13a1aSArd Biesheuvel skcipher_request_set_callback(req, 0, crypto_req_done, &wait); 76239d13a1aSArd Biesheuvel err = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); 76339d13a1aSArd Biesheuvel skcipher_request_free(req); 76439d13a1aSArd Biesheuvel 76539d13a1aSArd Biesheuvel return err; 766b9411d73SMilan Broz } 767b9411d73SMilan Broz 768bbb16584SMilan Broz static void crypt_iv_elephant_dtr(struct crypt_config *cc) 769bbb16584SMilan Broz { 770bbb16584SMilan Broz struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; 771bbb16584SMilan Broz 772bbb16584SMilan Broz crypto_free_skcipher(elephant->tfm); 773bbb16584SMilan Broz elephant->tfm = NULL; 774bbb16584SMilan Broz } 775bbb16584SMilan Broz 776bbb16584SMilan Broz static int crypt_iv_elephant_ctr(struct crypt_config *cc, struct dm_target *ti, 777bbb16584SMilan Broz const char *opts) 778bbb16584SMilan Broz { 779bbb16584SMilan Broz struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; 780bbb16584SMilan Broz int r; 781bbb16584SMilan Broz 782cd746938SMikulas Patocka elephant->tfm = crypto_alloc_skcipher("ecb(aes)", 0, 783cd746938SMikulas Patocka CRYPTO_ALG_ALLOCATES_MEMORY); 784bbb16584SMilan Broz if (IS_ERR(elephant->tfm)) { 785bbb16584SMilan Broz r = PTR_ERR(elephant->tfm); 786bbb16584SMilan Broz elephant->tfm = NULL; 787bbb16584SMilan Broz return r; 788bbb16584SMilan Broz } 789bbb16584SMilan Broz 790bbb16584SMilan Broz r = crypt_iv_eboiv_ctr(cc, ti, NULL); 791bbb16584SMilan Broz if (r) 792bbb16584SMilan Broz crypt_iv_elephant_dtr(cc); 793bbb16584SMilan Broz return r; 794bbb16584SMilan Broz } 795bbb16584SMilan Broz 796bbb16584SMilan Broz static void diffuser_disk_to_cpu(u32 *d, size_t n) 797bbb16584SMilan Broz { 798bbb16584SMilan Broz #ifndef __LITTLE_ENDIAN 799bbb16584SMilan Broz int i; 800bbb16584SMilan Broz 801bbb16584SMilan Broz for (i = 0; i < n; i++) 802bbb16584SMilan Broz d[i] = le32_to_cpu((__le32)d[i]); 803bbb16584SMilan Broz #endif 804bbb16584SMilan Broz } 805bbb16584SMilan Broz 806bbb16584SMilan Broz static void diffuser_cpu_to_disk(__le32 *d, size_t n) 807bbb16584SMilan Broz { 808bbb16584SMilan Broz #ifndef __LITTLE_ENDIAN 809bbb16584SMilan Broz int i; 810bbb16584SMilan Broz 811bbb16584SMilan Broz for (i = 0; i < n; i++) 812bbb16584SMilan Broz d[i] = cpu_to_le32((u32)d[i]); 813bbb16584SMilan Broz #endif 814bbb16584SMilan Broz } 815bbb16584SMilan Broz 816bbb16584SMilan Broz static void diffuser_a_decrypt(u32 *d, size_t n) 817bbb16584SMilan Broz { 818bbb16584SMilan Broz int i, i1, i2, i3; 819bbb16584SMilan Broz 820bbb16584SMilan Broz for (i = 0; i < 5; i++) { 821bbb16584SMilan Broz i1 = 0; 822bbb16584SMilan Broz i2 = n - 2; 823bbb16584SMilan Broz i3 = n - 5; 824bbb16584SMilan Broz 825bbb16584SMilan Broz while (i1 < (n - 1)) { 826bbb16584SMilan Broz d[i1] += d[i2] ^ (d[i3] << 9 | d[i3] >> 23); 827bbb16584SMilan Broz i1++; i2++; i3++; 828bbb16584SMilan Broz 829bbb16584SMilan Broz if (i3 >= n) 830bbb16584SMilan Broz i3 -= n; 831bbb16584SMilan Broz 832bbb16584SMilan Broz d[i1] += d[i2] ^ d[i3]; 833bbb16584SMilan Broz i1++; i2++; i3++; 834bbb16584SMilan Broz 835bbb16584SMilan Broz if (i2 >= n) 836bbb16584SMilan Broz i2 -= n; 837bbb16584SMilan Broz 838bbb16584SMilan Broz d[i1] += d[i2] ^ (d[i3] << 13 | d[i3] >> 19); 839bbb16584SMilan Broz i1++; i2++; i3++; 840bbb16584SMilan Broz 841bbb16584SMilan Broz d[i1] += d[i2] ^ d[i3]; 842bbb16584SMilan Broz i1++; i2++; i3++; 843bbb16584SMilan Broz } 844bbb16584SMilan Broz } 845bbb16584SMilan Broz } 846bbb16584SMilan Broz 847bbb16584SMilan Broz static void diffuser_a_encrypt(u32 *d, size_t n) 848bbb16584SMilan Broz { 849bbb16584SMilan Broz int i, i1, i2, i3; 850bbb16584SMilan Broz 851bbb16584SMilan Broz for (i = 0; i < 5; i++) { 852bbb16584SMilan Broz i1 = n - 1; 853bbb16584SMilan Broz i2 = n - 2 - 1; 854bbb16584SMilan Broz i3 = n - 5 - 1; 855bbb16584SMilan Broz 856bbb16584SMilan Broz while (i1 > 0) { 857bbb16584SMilan Broz d[i1] -= d[i2] ^ d[i3]; 858bbb16584SMilan Broz i1--; i2--; i3--; 859bbb16584SMilan Broz 860bbb16584SMilan Broz d[i1] -= d[i2] ^ (d[i3] << 13 | d[i3] >> 19); 861bbb16584SMilan Broz i1--; i2--; i3--; 862bbb16584SMilan Broz 863bbb16584SMilan Broz if (i2 < 0) 864bbb16584SMilan Broz i2 += n; 865bbb16584SMilan Broz 866bbb16584SMilan Broz d[i1] -= d[i2] ^ d[i3]; 867bbb16584SMilan Broz i1--; i2--; i3--; 868bbb16584SMilan Broz 869bbb16584SMilan Broz if (i3 < 0) 870bbb16584SMilan Broz i3 += n; 871bbb16584SMilan Broz 872bbb16584SMilan Broz d[i1] -= d[i2] ^ (d[i3] << 9 | d[i3] >> 23); 873bbb16584SMilan Broz i1--; i2--; i3--; 874bbb16584SMilan Broz } 875bbb16584SMilan Broz } 876bbb16584SMilan Broz } 877bbb16584SMilan Broz 878bbb16584SMilan Broz static void diffuser_b_decrypt(u32 *d, size_t n) 879bbb16584SMilan Broz { 880bbb16584SMilan Broz int i, i1, i2, i3; 881bbb16584SMilan Broz 882bbb16584SMilan Broz for (i = 0; i < 3; i++) { 883bbb16584SMilan Broz i1 = 0; 884bbb16584SMilan Broz i2 = 2; 885bbb16584SMilan Broz i3 = 5; 886bbb16584SMilan Broz 887bbb16584SMilan Broz while (i1 < (n - 1)) { 888bbb16584SMilan Broz d[i1] += d[i2] ^ d[i3]; 889bbb16584SMilan Broz i1++; i2++; i3++; 890bbb16584SMilan Broz 891bbb16584SMilan Broz d[i1] += d[i2] ^ (d[i3] << 10 | d[i3] >> 22); 892bbb16584SMilan Broz i1++; i2++; i3++; 893bbb16584SMilan Broz 894bbb16584SMilan Broz if (i2 >= n) 895bbb16584SMilan Broz i2 -= n; 896bbb16584SMilan Broz 897bbb16584SMilan Broz d[i1] += d[i2] ^ d[i3]; 898bbb16584SMilan Broz i1++; i2++; i3++; 899bbb16584SMilan Broz 900bbb16584SMilan Broz if (i3 >= n) 901bbb16584SMilan Broz i3 -= n; 902bbb16584SMilan Broz 903bbb16584SMilan Broz d[i1] += d[i2] ^ (d[i3] << 25 | d[i3] >> 7); 904bbb16584SMilan Broz i1++; i2++; i3++; 905bbb16584SMilan Broz } 906bbb16584SMilan Broz } 907bbb16584SMilan Broz } 908bbb16584SMilan Broz 909bbb16584SMilan Broz static void diffuser_b_encrypt(u32 *d, size_t n) 910bbb16584SMilan Broz { 911bbb16584SMilan Broz int i, i1, i2, i3; 912bbb16584SMilan Broz 913bbb16584SMilan Broz for (i = 0; i < 3; i++) { 914bbb16584SMilan Broz i1 = n - 1; 915bbb16584SMilan Broz i2 = 2 - 1; 916bbb16584SMilan Broz i3 = 5 - 1; 917bbb16584SMilan Broz 918bbb16584SMilan Broz while (i1 > 0) { 919bbb16584SMilan Broz d[i1] -= d[i2] ^ (d[i3] << 25 | d[i3] >> 7); 920bbb16584SMilan Broz i1--; i2--; i3--; 921bbb16584SMilan Broz 922bbb16584SMilan Broz if (i3 < 0) 923bbb16584SMilan Broz i3 += n; 924bbb16584SMilan Broz 925bbb16584SMilan Broz d[i1] -= d[i2] ^ d[i3]; 926bbb16584SMilan Broz i1--; i2--; i3--; 927bbb16584SMilan Broz 928bbb16584SMilan Broz if (i2 < 0) 929bbb16584SMilan Broz i2 += n; 930bbb16584SMilan Broz 931bbb16584SMilan Broz d[i1] -= d[i2] ^ (d[i3] << 10 | d[i3] >> 22); 932bbb16584SMilan Broz i1--; i2--; i3--; 933bbb16584SMilan Broz 934bbb16584SMilan Broz d[i1] -= d[i2] ^ d[i3]; 935bbb16584SMilan Broz i1--; i2--; i3--; 936bbb16584SMilan Broz } 937bbb16584SMilan Broz } 938bbb16584SMilan Broz } 939bbb16584SMilan Broz 940bbb16584SMilan Broz static int crypt_iv_elephant(struct crypt_config *cc, struct dm_crypt_request *dmreq) 941bbb16584SMilan Broz { 942bbb16584SMilan Broz struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; 943bbb16584SMilan Broz u8 *es, *ks, *data, *data2, *data_offset; 944bbb16584SMilan Broz struct skcipher_request *req; 945bbb16584SMilan Broz struct scatterlist *sg, *sg2, src, dst; 9467785a9e4SDamien Le Moal DECLARE_CRYPTO_WAIT(wait); 947bbb16584SMilan Broz int i, r; 948bbb16584SMilan Broz 949bbb16584SMilan Broz req = skcipher_request_alloc(elephant->tfm, GFP_NOIO); 950bbb16584SMilan Broz es = kzalloc(16, GFP_NOIO); /* Key for AES */ 951bbb16584SMilan Broz ks = kzalloc(32, GFP_NOIO); /* Elephant sector key */ 952bbb16584SMilan Broz 953bbb16584SMilan Broz if (!req || !es || !ks) { 954bbb16584SMilan Broz r = -ENOMEM; 955bbb16584SMilan Broz goto out; 956bbb16584SMilan Broz } 957bbb16584SMilan Broz 958bbb16584SMilan Broz *(__le64 *)es = cpu_to_le64(dmreq->iv_sector * cc->sector_size); 959bbb16584SMilan Broz 960bbb16584SMilan Broz /* E(Ks, e(s)) */ 961bbb16584SMilan Broz sg_init_one(&src, es, 16); 962bbb16584SMilan Broz sg_init_one(&dst, ks, 16); 963bbb16584SMilan Broz skcipher_request_set_crypt(req, &src, &dst, 16, NULL); 964bbb16584SMilan Broz skcipher_request_set_callback(req, 0, crypto_req_done, &wait); 965bbb16584SMilan Broz r = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); 966bbb16584SMilan Broz if (r) 967bbb16584SMilan Broz goto out; 968bbb16584SMilan Broz 969bbb16584SMilan Broz /* E(Ks, e'(s)) */ 970bbb16584SMilan Broz es[15] = 0x80; 971bbb16584SMilan Broz sg_init_one(&dst, &ks[16], 16); 972bbb16584SMilan Broz r = crypto_wait_req(crypto_skcipher_encrypt(req), &wait); 973bbb16584SMilan Broz if (r) 974bbb16584SMilan Broz goto out; 975bbb16584SMilan Broz 976bbb16584SMilan Broz sg = crypt_get_sg_data(cc, dmreq->sg_out); 977bbb16584SMilan Broz data = kmap_atomic(sg_page(sg)); 978bbb16584SMilan Broz data_offset = data + sg->offset; 979bbb16584SMilan Broz 980bbb16584SMilan Broz /* Cannot modify original bio, copy to sg_out and apply Elephant to it */ 981bbb16584SMilan Broz if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) { 982bbb16584SMilan Broz sg2 = crypt_get_sg_data(cc, dmreq->sg_in); 983bbb16584SMilan Broz data2 = kmap_atomic(sg_page(sg2)); 984bbb16584SMilan Broz memcpy(data_offset, data2 + sg2->offset, cc->sector_size); 985bbb16584SMilan Broz kunmap_atomic(data2); 986bbb16584SMilan Broz } 987bbb16584SMilan Broz 988bbb16584SMilan Broz if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) { 989bbb16584SMilan Broz diffuser_disk_to_cpu((u32*)data_offset, cc->sector_size / sizeof(u32)); 990bbb16584SMilan Broz diffuser_b_decrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); 991bbb16584SMilan Broz diffuser_a_decrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); 992bbb16584SMilan Broz diffuser_cpu_to_disk((__le32*)data_offset, cc->sector_size / sizeof(u32)); 993bbb16584SMilan Broz } 994bbb16584SMilan Broz 995bbb16584SMilan Broz for (i = 0; i < (cc->sector_size / 32); i++) 996bbb16584SMilan Broz crypto_xor(data_offset + i * 32, ks, 32); 997bbb16584SMilan Broz 998bbb16584SMilan Broz if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) { 999bbb16584SMilan Broz diffuser_disk_to_cpu((u32*)data_offset, cc->sector_size / sizeof(u32)); 1000bbb16584SMilan Broz diffuser_a_encrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); 1001bbb16584SMilan Broz diffuser_b_encrypt((u32*)data_offset, cc->sector_size / sizeof(u32)); 1002bbb16584SMilan Broz diffuser_cpu_to_disk((__le32*)data_offset, cc->sector_size / sizeof(u32)); 1003bbb16584SMilan Broz } 1004bbb16584SMilan Broz 1005bbb16584SMilan Broz kunmap_atomic(data); 1006bbb16584SMilan Broz out: 1007453431a5SWaiman Long kfree_sensitive(ks); 1008453431a5SWaiman Long kfree_sensitive(es); 1009bbb16584SMilan Broz skcipher_request_free(req); 1010bbb16584SMilan Broz return r; 1011bbb16584SMilan Broz } 1012bbb16584SMilan Broz 1013bbb16584SMilan Broz static int crypt_iv_elephant_gen(struct crypt_config *cc, u8 *iv, 1014bbb16584SMilan Broz struct dm_crypt_request *dmreq) 1015bbb16584SMilan Broz { 1016bbb16584SMilan Broz int r; 1017bbb16584SMilan Broz 1018bbb16584SMilan Broz if (bio_data_dir(dmreq->ctx->bio_in) == WRITE) { 1019bbb16584SMilan Broz r = crypt_iv_elephant(cc, dmreq); 1020bbb16584SMilan Broz if (r) 1021bbb16584SMilan Broz return r; 1022bbb16584SMilan Broz } 1023bbb16584SMilan Broz 1024bbb16584SMilan Broz return crypt_iv_eboiv_gen(cc, iv, dmreq); 1025bbb16584SMilan Broz } 1026bbb16584SMilan Broz 1027bbb16584SMilan Broz static int crypt_iv_elephant_post(struct crypt_config *cc, u8 *iv, 1028bbb16584SMilan Broz struct dm_crypt_request *dmreq) 1029bbb16584SMilan Broz { 1030bbb16584SMilan Broz if (bio_data_dir(dmreq->ctx->bio_in) != WRITE) 1031bbb16584SMilan Broz return crypt_iv_elephant(cc, dmreq); 1032bbb16584SMilan Broz 1033bbb16584SMilan Broz return 0; 1034bbb16584SMilan Broz } 1035bbb16584SMilan Broz 1036bbb16584SMilan Broz static int crypt_iv_elephant_init(struct crypt_config *cc) 1037bbb16584SMilan Broz { 1038bbb16584SMilan Broz struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; 1039bbb16584SMilan Broz int key_offset = cc->key_size - cc->key_extra_size; 1040bbb16584SMilan Broz 1041bbb16584SMilan Broz return crypto_skcipher_setkey(elephant->tfm, &cc->key[key_offset], cc->key_extra_size); 1042bbb16584SMilan Broz } 1043bbb16584SMilan Broz 1044bbb16584SMilan Broz static int crypt_iv_elephant_wipe(struct crypt_config *cc) 1045bbb16584SMilan Broz { 1046bbb16584SMilan Broz struct iv_elephant_private *elephant = &cc->iv_gen_private.elephant; 1047bbb16584SMilan Broz u8 key[ELEPHANT_MAX_KEY_SIZE]; 1048bbb16584SMilan Broz 1049bbb16584SMilan Broz memset(key, 0, cc->key_extra_size); 1050bbb16584SMilan Broz return crypto_skcipher_setkey(elephant->tfm, key, cc->key_extra_size); 1051bbb16584SMilan Broz } 1052bbb16584SMilan Broz 10531b1b58f5SJulia Lawall static const struct crypt_iv_operations crypt_iv_plain_ops = { 10541da177e4SLinus Torvalds .generator = crypt_iv_plain_gen 10551da177e4SLinus Torvalds }; 10561da177e4SLinus Torvalds 10571b1b58f5SJulia Lawall static const struct crypt_iv_operations crypt_iv_plain64_ops = { 105861afef61SMilan Broz .generator = crypt_iv_plain64_gen 105961afef61SMilan Broz }; 106061afef61SMilan Broz 10617e3fd855SMilan Broz static const struct crypt_iv_operations crypt_iv_plain64be_ops = { 10627e3fd855SMilan Broz .generator = crypt_iv_plain64be_gen 10637e3fd855SMilan Broz }; 10647e3fd855SMilan Broz 10651b1b58f5SJulia Lawall static const struct crypt_iv_operations crypt_iv_essiv_ops = { 10661da177e4SLinus Torvalds .generator = crypt_iv_essiv_gen 10671da177e4SLinus Torvalds }; 10681da177e4SLinus Torvalds 10691b1b58f5SJulia Lawall static const struct crypt_iv_operations crypt_iv_benbi_ops = { 107048527fa7SRik Snel .ctr = crypt_iv_benbi_ctr, 107148527fa7SRik Snel .dtr = crypt_iv_benbi_dtr, 107248527fa7SRik Snel .generator = crypt_iv_benbi_gen 107348527fa7SRik Snel }; 10741da177e4SLinus Torvalds 10751b1b58f5SJulia Lawall static const struct crypt_iv_operations crypt_iv_null_ops = { 107646b47730SLudwig Nussel .generator = crypt_iv_null_gen 107746b47730SLudwig Nussel }; 107846b47730SLudwig Nussel 10791b1b58f5SJulia Lawall static const struct crypt_iv_operations crypt_iv_lmk_ops = { 108034745785SMilan Broz .ctr = crypt_iv_lmk_ctr, 108134745785SMilan Broz .dtr = crypt_iv_lmk_dtr, 108234745785SMilan Broz .init = crypt_iv_lmk_init, 108334745785SMilan Broz .wipe = crypt_iv_lmk_wipe, 108434745785SMilan Broz .generator = crypt_iv_lmk_gen, 108534745785SMilan Broz .post = crypt_iv_lmk_post 108634745785SMilan Broz }; 108734745785SMilan Broz 10881b1b58f5SJulia Lawall static const struct crypt_iv_operations crypt_iv_tcw_ops = { 1089ed04d981SMilan Broz .ctr = crypt_iv_tcw_ctr, 1090ed04d981SMilan Broz .dtr = crypt_iv_tcw_dtr, 1091ed04d981SMilan Broz .init = crypt_iv_tcw_init, 1092ed04d981SMilan Broz .wipe = crypt_iv_tcw_wipe, 1093ed04d981SMilan Broz .generator = crypt_iv_tcw_gen, 1094ed04d981SMilan Broz .post = crypt_iv_tcw_post 1095ed04d981SMilan Broz }; 1096ed04d981SMilan Broz 1097e8dc79d1SRikard Falkeborn static const struct crypt_iv_operations crypt_iv_random_ops = { 1098ef43aa38SMilan Broz .generator = crypt_iv_random_gen 1099ef43aa38SMilan Broz }; 1100ef43aa38SMilan Broz 1101e8dc79d1SRikard Falkeborn static const struct crypt_iv_operations crypt_iv_eboiv_ops = { 1102b9411d73SMilan Broz .ctr = crypt_iv_eboiv_ctr, 1103b9411d73SMilan Broz .generator = crypt_iv_eboiv_gen 1104b9411d73SMilan Broz }; 1105b9411d73SMilan Broz 1106e8dc79d1SRikard Falkeborn static const struct crypt_iv_operations crypt_iv_elephant_ops = { 1107bbb16584SMilan Broz .ctr = crypt_iv_elephant_ctr, 1108bbb16584SMilan Broz .dtr = crypt_iv_elephant_dtr, 1109bbb16584SMilan Broz .init = crypt_iv_elephant_init, 1110bbb16584SMilan Broz .wipe = crypt_iv_elephant_wipe, 1111bbb16584SMilan Broz .generator = crypt_iv_elephant_gen, 1112bbb16584SMilan Broz .post = crypt_iv_elephant_post 1113bbb16584SMilan Broz }; 1114bbb16584SMilan Broz 1115ef43aa38SMilan Broz /* 1116ef43aa38SMilan Broz * Integrity extensions 1117ef43aa38SMilan Broz */ 1118ef43aa38SMilan Broz static bool crypt_integrity_aead(struct crypt_config *cc) 1119ef43aa38SMilan Broz { 1120ef43aa38SMilan Broz return test_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); 1121ef43aa38SMilan Broz } 1122ef43aa38SMilan Broz 1123ef43aa38SMilan Broz static bool crypt_integrity_hmac(struct crypt_config *cc) 1124ef43aa38SMilan Broz { 112533d2f09fSMilan Broz return crypt_integrity_aead(cc) && cc->key_mac_size; 1126ef43aa38SMilan Broz } 1127ef43aa38SMilan Broz 1128ef43aa38SMilan Broz /* Get sg containing data */ 1129ef43aa38SMilan Broz static struct scatterlist *crypt_get_sg_data(struct crypt_config *cc, 1130ef43aa38SMilan Broz struct scatterlist *sg) 1131ef43aa38SMilan Broz { 113233d2f09fSMilan Broz if (unlikely(crypt_integrity_aead(cc))) 1133ef43aa38SMilan Broz return &sg[2]; 1134ef43aa38SMilan Broz 1135ef43aa38SMilan Broz return sg; 1136ef43aa38SMilan Broz } 1137ef43aa38SMilan Broz 1138ef43aa38SMilan Broz static int dm_crypt_integrity_io_alloc(struct dm_crypt_io *io, struct bio *bio) 1139ef43aa38SMilan Broz { 1140ef43aa38SMilan Broz struct bio_integrity_payload *bip; 1141ef43aa38SMilan Broz unsigned int tag_len; 1142ef43aa38SMilan Broz int ret; 1143ef43aa38SMilan Broz 1144ef43aa38SMilan Broz if (!bio_sectors(bio) || !io->cc->on_disk_tag_size) 1145ef43aa38SMilan Broz return 0; 1146ef43aa38SMilan Broz 1147ef43aa38SMilan Broz bip = bio_integrity_alloc(bio, GFP_NOIO, 1); 1148ef43aa38SMilan Broz if (IS_ERR(bip)) 1149ef43aa38SMilan Broz return PTR_ERR(bip); 1150ef43aa38SMilan Broz 1151ff0c129dSMikulas Patocka tag_len = io->cc->on_disk_tag_size * (bio_sectors(bio) >> io->cc->sector_shift); 1152ef43aa38SMilan Broz 1153ef43aa38SMilan Broz bip->bip_iter.bi_size = tag_len; 1154ef43aa38SMilan Broz bip->bip_iter.bi_sector = io->cc->start + io->sector; 1155ef43aa38SMilan Broz 1156ef43aa38SMilan Broz ret = bio_integrity_add_page(bio, virt_to_page(io->integrity_metadata), 1157ef43aa38SMilan Broz tag_len, offset_in_page(io->integrity_metadata)); 1158ef43aa38SMilan Broz if (unlikely(ret != tag_len)) 1159ef43aa38SMilan Broz return -ENOMEM; 1160ef43aa38SMilan Broz 1161ef43aa38SMilan Broz return 0; 1162ef43aa38SMilan Broz } 1163ef43aa38SMilan Broz 1164ef43aa38SMilan Broz static int crypt_integrity_ctr(struct crypt_config *cc, struct dm_target *ti) 1165ef43aa38SMilan Broz { 1166ef43aa38SMilan Broz #ifdef CONFIG_BLK_DEV_INTEGRITY 1167ef43aa38SMilan Broz struct blk_integrity *bi = blk_get_integrity(cc->dev->bdev->bd_disk); 11687a1cd723SMilan Broz struct mapped_device *md = dm_table_get_md(ti->table); 1169ef43aa38SMilan Broz 1170ef43aa38SMilan Broz /* From now we require underlying device with our integrity profile */ 1171ef43aa38SMilan Broz if (!bi || strcasecmp(bi->profile->name, "DM-DIF-EXT-TAG")) { 1172ef43aa38SMilan Broz ti->error = "Integrity profile not supported."; 1173ef43aa38SMilan Broz return -EINVAL; 1174ef43aa38SMilan Broz } 1175ef43aa38SMilan Broz 1176583fe747SMikulas Patocka if (bi->tag_size != cc->on_disk_tag_size || 1177583fe747SMikulas Patocka bi->tuple_size != cc->on_disk_tag_size) { 1178ef43aa38SMilan Broz ti->error = "Integrity profile tag size mismatch."; 1179ef43aa38SMilan Broz return -EINVAL; 1180ef43aa38SMilan Broz } 1181583fe747SMikulas Patocka if (1 << bi->interval_exp != cc->sector_size) { 1182583fe747SMikulas Patocka ti->error = "Integrity profile sector size mismatch."; 1183583fe747SMikulas Patocka return -EINVAL; 1184583fe747SMikulas Patocka } 1185ef43aa38SMilan Broz 118633d2f09fSMilan Broz if (crypt_integrity_aead(cc)) { 1187ef43aa38SMilan Broz cc->integrity_tag_size = cc->on_disk_tag_size - cc->integrity_iv_size; 11887a1cd723SMilan Broz DMDEBUG("%s: Integrity AEAD, tag size %u, IV size %u.", dm_device_name(md), 1189ef43aa38SMilan Broz cc->integrity_tag_size, cc->integrity_iv_size); 1190ef43aa38SMilan Broz 1191ef43aa38SMilan Broz if (crypto_aead_setauthsize(any_tfm_aead(cc), cc->integrity_tag_size)) { 1192ef43aa38SMilan Broz ti->error = "Integrity AEAD auth tag size is not supported."; 1193ef43aa38SMilan Broz return -EINVAL; 1194ef43aa38SMilan Broz } 1195ef43aa38SMilan Broz } else if (cc->integrity_iv_size) 11967a1cd723SMilan Broz DMDEBUG("%s: Additional per-sector space %u bytes for IV.", dm_device_name(md), 1197ef43aa38SMilan Broz cc->integrity_iv_size); 1198ef43aa38SMilan Broz 1199ef43aa38SMilan Broz if ((cc->integrity_tag_size + cc->integrity_iv_size) != bi->tag_size) { 1200ef43aa38SMilan Broz ti->error = "Not enough space for integrity tag in the profile."; 1201ef43aa38SMilan Broz return -EINVAL; 1202ef43aa38SMilan Broz } 1203ef43aa38SMilan Broz 1204ef43aa38SMilan Broz return 0; 1205ef43aa38SMilan Broz #else 1206ef43aa38SMilan Broz ti->error = "Integrity profile not supported."; 1207ef43aa38SMilan Broz return -EINVAL; 1208ef43aa38SMilan Broz #endif 1209ef43aa38SMilan Broz } 1210ef43aa38SMilan Broz 1211d469f841SMilan Broz static void crypt_convert_init(struct crypt_config *cc, 1212d469f841SMilan Broz struct convert_context *ctx, 12131da177e4SLinus Torvalds struct bio *bio_out, struct bio *bio_in, 1214fcd369daSMilan Broz sector_t sector) 12151da177e4SLinus Torvalds { 12161da177e4SLinus Torvalds ctx->bio_in = bio_in; 12171da177e4SLinus Torvalds ctx->bio_out = bio_out; 1218003b5c57SKent Overstreet if (bio_in) 1219003b5c57SKent Overstreet ctx->iter_in = bio_in->bi_iter; 1220003b5c57SKent Overstreet if (bio_out) 1221003b5c57SKent Overstreet ctx->iter_out = bio_out->bi_iter; 1222c66029f4SMikulas Patocka ctx->cc_sector = sector + cc->iv_offset; 122343d69034SMilan Broz init_completion(&ctx->restart); 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds 1226b2174eebSHuang Ying static struct dm_crypt_request *dmreq_of_req(struct crypt_config *cc, 1227ef43aa38SMilan Broz void *req) 1228b2174eebSHuang Ying { 1229b2174eebSHuang Ying return (struct dm_crypt_request *)((char *)req + cc->dmreq_start); 1230b2174eebSHuang Ying } 1231b2174eebSHuang Ying 1232ef43aa38SMilan Broz static void *req_of_dmreq(struct crypt_config *cc, struct dm_crypt_request *dmreq) 1233b2174eebSHuang Ying { 1234ef43aa38SMilan Broz return (void *)((char *)dmreq - cc->dmreq_start); 1235b2174eebSHuang Ying } 1236b2174eebSHuang Ying 12372dc5327dSMilan Broz static u8 *iv_of_dmreq(struct crypt_config *cc, 12382dc5327dSMilan Broz struct dm_crypt_request *dmreq) 12392dc5327dSMilan Broz { 124033d2f09fSMilan Broz if (crypt_integrity_aead(cc)) 1241ef43aa38SMilan Broz return (u8 *)ALIGN((unsigned long)(dmreq + 1), 1242ef43aa38SMilan Broz crypto_aead_alignmask(any_tfm_aead(cc)) + 1); 1243ef43aa38SMilan Broz else 12442dc5327dSMilan Broz return (u8 *)ALIGN((unsigned long)(dmreq + 1), 1245bbdb23b5SHerbert Xu crypto_skcipher_alignmask(any_tfm(cc)) + 1); 12462dc5327dSMilan Broz } 12472dc5327dSMilan Broz 1248ef43aa38SMilan Broz static u8 *org_iv_of_dmreq(struct crypt_config *cc, 1249ef43aa38SMilan Broz struct dm_crypt_request *dmreq) 1250ef43aa38SMilan Broz { 1251ef43aa38SMilan Broz return iv_of_dmreq(cc, dmreq) + cc->iv_size; 1252ef43aa38SMilan Broz } 1253ef43aa38SMilan Broz 1254c13b5487SChristoph Hellwig static __le64 *org_sector_of_dmreq(struct crypt_config *cc, 1255ef43aa38SMilan Broz struct dm_crypt_request *dmreq) 1256ef43aa38SMilan Broz { 1257ef43aa38SMilan Broz u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + cc->iv_size; 1258c13b5487SChristoph Hellwig return (__le64 *) ptr; 1259ef43aa38SMilan Broz } 1260ef43aa38SMilan Broz 1261ef43aa38SMilan Broz static unsigned int *org_tag_of_dmreq(struct crypt_config *cc, 1262ef43aa38SMilan Broz struct dm_crypt_request *dmreq) 1263ef43aa38SMilan Broz { 1264ef43aa38SMilan Broz u8 *ptr = iv_of_dmreq(cc, dmreq) + cc->iv_size + 1265ef43aa38SMilan Broz cc->iv_size + sizeof(uint64_t); 1266ef43aa38SMilan Broz return (unsigned int*)ptr; 1267ef43aa38SMilan Broz } 1268ef43aa38SMilan Broz 1269ef43aa38SMilan Broz static void *tag_from_dmreq(struct crypt_config *cc, 1270ef43aa38SMilan Broz struct dm_crypt_request *dmreq) 1271ef43aa38SMilan Broz { 1272ef43aa38SMilan Broz struct convert_context *ctx = dmreq->ctx; 1273ef43aa38SMilan Broz struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx); 1274ef43aa38SMilan Broz 1275ef43aa38SMilan Broz return &io->integrity_metadata[*org_tag_of_dmreq(cc, dmreq) * 1276ef43aa38SMilan Broz cc->on_disk_tag_size]; 1277ef43aa38SMilan Broz } 1278ef43aa38SMilan Broz 1279ef43aa38SMilan Broz static void *iv_tag_from_dmreq(struct crypt_config *cc, 1280ef43aa38SMilan Broz struct dm_crypt_request *dmreq) 1281ef43aa38SMilan Broz { 1282ef43aa38SMilan Broz return tag_from_dmreq(cc, dmreq) + cc->integrity_tag_size; 1283ef43aa38SMilan Broz } 1284ef43aa38SMilan Broz 1285ef43aa38SMilan Broz static int crypt_convert_block_aead(struct crypt_config *cc, 12863a7f6c99SMilan Broz struct convert_context *ctx, 1287ef43aa38SMilan Broz struct aead_request *req, 1288ef43aa38SMilan Broz unsigned int tag_offset) 128901482b76SMilan Broz { 1290003b5c57SKent Overstreet struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in); 1291003b5c57SKent Overstreet struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out); 12923a7f6c99SMilan Broz struct dm_crypt_request *dmreq; 1293ef43aa38SMilan Broz u8 *iv, *org_iv, *tag_iv, *tag; 1294c13b5487SChristoph Hellwig __le64 *sector; 1295ef43aa38SMilan Broz int r = 0; 1296ef43aa38SMilan Broz 1297ef43aa38SMilan Broz BUG_ON(cc->integrity_iv_size && cc->integrity_iv_size != cc->iv_size); 129801482b76SMilan Broz 12998f0009a2SMilan Broz /* Reject unexpected unaligned bio. */ 13000440d5c0SMikulas Patocka if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) 13018f0009a2SMilan Broz return -EIO; 130201482b76SMilan Broz 1303b2174eebSHuang Ying dmreq = dmreq_of_req(cc, req); 1304c66029f4SMikulas Patocka dmreq->iv_sector = ctx->cc_sector; 13058f0009a2SMilan Broz if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) 1306ff3af92bSMikulas Patocka dmreq->iv_sector >>= cc->sector_shift; 1307b2174eebSHuang Ying dmreq->ctx = ctx; 130801482b76SMilan Broz 1309ef43aa38SMilan Broz *org_tag_of_dmreq(cc, dmreq) = tag_offset; 131001482b76SMilan Broz 1311ef43aa38SMilan Broz sector = org_sector_of_dmreq(cc, dmreq); 1312ef43aa38SMilan Broz *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); 1313ef43aa38SMilan Broz 1314ef43aa38SMilan Broz iv = iv_of_dmreq(cc, dmreq); 1315ef43aa38SMilan Broz org_iv = org_iv_of_dmreq(cc, dmreq); 1316ef43aa38SMilan Broz tag = tag_from_dmreq(cc, dmreq); 1317ef43aa38SMilan Broz tag_iv = iv_tag_from_dmreq(cc, dmreq); 1318ef43aa38SMilan Broz 1319ef43aa38SMilan Broz /* AEAD request: 1320ef43aa38SMilan Broz * |----- AAD -------|------ DATA -------|-- AUTH TAG --| 1321ef43aa38SMilan Broz * | (authenticated) | (auth+encryption) | | 1322ef43aa38SMilan Broz * | sector_LE | IV | sector in/out | tag in/out | 1323ef43aa38SMilan Broz */ 1324ef43aa38SMilan Broz sg_init_table(dmreq->sg_in, 4); 1325ef43aa38SMilan Broz sg_set_buf(&dmreq->sg_in[0], sector, sizeof(uint64_t)); 1326ef43aa38SMilan Broz sg_set_buf(&dmreq->sg_in[1], org_iv, cc->iv_size); 13278f0009a2SMilan Broz sg_set_page(&dmreq->sg_in[2], bv_in.bv_page, cc->sector_size, bv_in.bv_offset); 1328ef43aa38SMilan Broz sg_set_buf(&dmreq->sg_in[3], tag, cc->integrity_tag_size); 1329ef43aa38SMilan Broz 1330ef43aa38SMilan Broz sg_init_table(dmreq->sg_out, 4); 1331ef43aa38SMilan Broz sg_set_buf(&dmreq->sg_out[0], sector, sizeof(uint64_t)); 1332ef43aa38SMilan Broz sg_set_buf(&dmreq->sg_out[1], org_iv, cc->iv_size); 13338f0009a2SMilan Broz sg_set_page(&dmreq->sg_out[2], bv_out.bv_page, cc->sector_size, bv_out.bv_offset); 1334ef43aa38SMilan Broz sg_set_buf(&dmreq->sg_out[3], tag, cc->integrity_tag_size); 133501482b76SMilan Broz 13363a7f6c99SMilan Broz if (cc->iv_gen_ops) { 1337ef43aa38SMilan Broz /* For READs use IV stored in integrity metadata */ 1338ef43aa38SMilan Broz if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { 1339ef43aa38SMilan Broz memcpy(org_iv, tag_iv, cc->iv_size); 1340ef43aa38SMilan Broz } else { 1341ef43aa38SMilan Broz r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); 13423a7f6c99SMilan Broz if (r < 0) 13433a7f6c99SMilan Broz return r; 1344ef43aa38SMilan Broz /* Store generated IV in integrity metadata */ 1345ef43aa38SMilan Broz if (cc->integrity_iv_size) 1346ef43aa38SMilan Broz memcpy(tag_iv, org_iv, cc->iv_size); 1347ef43aa38SMilan Broz } 1348ef43aa38SMilan Broz /* Working copy of IV, to be modified in crypto API */ 1349ef43aa38SMilan Broz memcpy(iv, org_iv, cc->iv_size); 1350ef43aa38SMilan Broz } 1351ef43aa38SMilan Broz 1352ef43aa38SMilan Broz aead_request_set_ad(req, sizeof(uint64_t) + cc->iv_size); 1353ef43aa38SMilan Broz if (bio_data_dir(ctx->bio_in) == WRITE) { 1354ef43aa38SMilan Broz aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out, 13558f0009a2SMilan Broz cc->sector_size, iv); 1356ef43aa38SMilan Broz r = crypto_aead_encrypt(req); 1357ef43aa38SMilan Broz if (cc->integrity_tag_size + cc->integrity_iv_size != cc->on_disk_tag_size) 1358ef43aa38SMilan Broz memset(tag + cc->integrity_tag_size + cc->integrity_iv_size, 0, 1359ef43aa38SMilan Broz cc->on_disk_tag_size - (cc->integrity_tag_size + cc->integrity_iv_size)); 1360ef43aa38SMilan Broz } else { 1361ef43aa38SMilan Broz aead_request_set_crypt(req, dmreq->sg_in, dmreq->sg_out, 13628f0009a2SMilan Broz cc->sector_size + cc->integrity_tag_size, iv); 1363ef43aa38SMilan Broz r = crypto_aead_decrypt(req); 1364ef43aa38SMilan Broz } 1365ef43aa38SMilan Broz 1366f710126cSMilan Broz if (r == -EBADMSG) { 136758d0f180SMichael Weiß sector_t s = le64_to_cpu(*sector); 136858d0f180SMichael Weiß 136966671719SChristoph Hellwig DMERR_LIMIT("%pg: INTEGRITY AEAD ERROR, sector %llu", 137066671719SChristoph Hellwig ctx->bio_in->bi_bdev, s); 137158d0f180SMichael Weiß dm_audit_log_bio(DM_MSG_PREFIX, "integrity-aead", 137258d0f180SMichael Weiß ctx->bio_in, s, 0); 1373f710126cSMilan Broz } 1374ef43aa38SMilan Broz 1375ef43aa38SMilan Broz if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) 1376ef43aa38SMilan Broz r = cc->iv_gen_ops->post(cc, org_iv, dmreq); 1377ef43aa38SMilan Broz 13788f0009a2SMilan Broz bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); 13798f0009a2SMilan Broz bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); 1380ef43aa38SMilan Broz 1381ef43aa38SMilan Broz return r; 13823a7f6c99SMilan Broz } 13833a7f6c99SMilan Broz 1384ef43aa38SMilan Broz static int crypt_convert_block_skcipher(struct crypt_config *cc, 1385ef43aa38SMilan Broz struct convert_context *ctx, 1386ef43aa38SMilan Broz struct skcipher_request *req, 1387ef43aa38SMilan Broz unsigned int tag_offset) 1388ef43aa38SMilan Broz { 1389ef43aa38SMilan Broz struct bio_vec bv_in = bio_iter_iovec(ctx->bio_in, ctx->iter_in); 1390ef43aa38SMilan Broz struct bio_vec bv_out = bio_iter_iovec(ctx->bio_out, ctx->iter_out); 1391ef43aa38SMilan Broz struct scatterlist *sg_in, *sg_out; 1392ef43aa38SMilan Broz struct dm_crypt_request *dmreq; 1393ef43aa38SMilan Broz u8 *iv, *org_iv, *tag_iv; 1394c13b5487SChristoph Hellwig __le64 *sector; 1395ef43aa38SMilan Broz int r = 0; 1396ef43aa38SMilan Broz 13978f0009a2SMilan Broz /* Reject unexpected unaligned bio. */ 13980440d5c0SMikulas Patocka if (unlikely(bv_in.bv_len & (cc->sector_size - 1))) 13998f0009a2SMilan Broz return -EIO; 14008f0009a2SMilan Broz 1401ef43aa38SMilan Broz dmreq = dmreq_of_req(cc, req); 1402ef43aa38SMilan Broz dmreq->iv_sector = ctx->cc_sector; 14038f0009a2SMilan Broz if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) 1404ff3af92bSMikulas Patocka dmreq->iv_sector >>= cc->sector_shift; 1405ef43aa38SMilan Broz dmreq->ctx = ctx; 1406ef43aa38SMilan Broz 1407ef43aa38SMilan Broz *org_tag_of_dmreq(cc, dmreq) = tag_offset; 1408ef43aa38SMilan Broz 1409ef43aa38SMilan Broz iv = iv_of_dmreq(cc, dmreq); 1410ef43aa38SMilan Broz org_iv = org_iv_of_dmreq(cc, dmreq); 1411ef43aa38SMilan Broz tag_iv = iv_tag_from_dmreq(cc, dmreq); 1412ef43aa38SMilan Broz 1413ef43aa38SMilan Broz sector = org_sector_of_dmreq(cc, dmreq); 1414ef43aa38SMilan Broz *sector = cpu_to_le64(ctx->cc_sector - cc->iv_offset); 1415ef43aa38SMilan Broz 1416ef43aa38SMilan Broz /* For skcipher we use only the first sg item */ 1417ef43aa38SMilan Broz sg_in = &dmreq->sg_in[0]; 1418ef43aa38SMilan Broz sg_out = &dmreq->sg_out[0]; 1419ef43aa38SMilan Broz 1420ef43aa38SMilan Broz sg_init_table(sg_in, 1); 14218f0009a2SMilan Broz sg_set_page(sg_in, bv_in.bv_page, cc->sector_size, bv_in.bv_offset); 1422ef43aa38SMilan Broz 1423ef43aa38SMilan Broz sg_init_table(sg_out, 1); 14248f0009a2SMilan Broz sg_set_page(sg_out, bv_out.bv_page, cc->sector_size, bv_out.bv_offset); 1425ef43aa38SMilan Broz 1426ef43aa38SMilan Broz if (cc->iv_gen_ops) { 1427ef43aa38SMilan Broz /* For READs use IV stored in integrity metadata */ 1428ef43aa38SMilan Broz if (cc->integrity_iv_size && bio_data_dir(ctx->bio_in) != WRITE) { 1429ef43aa38SMilan Broz memcpy(org_iv, tag_iv, cc->integrity_iv_size); 1430ef43aa38SMilan Broz } else { 1431ef43aa38SMilan Broz r = cc->iv_gen_ops->generator(cc, org_iv, dmreq); 1432ef43aa38SMilan Broz if (r < 0) 1433ef43aa38SMilan Broz return r; 1434bbb16584SMilan Broz /* Data can be already preprocessed in generator */ 1435bbb16584SMilan Broz if (test_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags)) 1436bbb16584SMilan Broz sg_in = sg_out; 1437ef43aa38SMilan Broz /* Store generated IV in integrity metadata */ 1438ef43aa38SMilan Broz if (cc->integrity_iv_size) 1439ef43aa38SMilan Broz memcpy(tag_iv, org_iv, cc->integrity_iv_size); 1440ef43aa38SMilan Broz } 1441ef43aa38SMilan Broz /* Working copy of IV, to be modified in crypto API */ 1442ef43aa38SMilan Broz memcpy(iv, org_iv, cc->iv_size); 1443ef43aa38SMilan Broz } 1444ef43aa38SMilan Broz 14458f0009a2SMilan Broz skcipher_request_set_crypt(req, sg_in, sg_out, cc->sector_size, iv); 14463a7f6c99SMilan Broz 14473a7f6c99SMilan Broz if (bio_data_dir(ctx->bio_in) == WRITE) 1448bbdb23b5SHerbert Xu r = crypto_skcipher_encrypt(req); 14493a7f6c99SMilan Broz else 1450bbdb23b5SHerbert Xu r = crypto_skcipher_decrypt(req); 14513a7f6c99SMilan Broz 14522dc5327dSMilan Broz if (!r && cc->iv_gen_ops && cc->iv_gen_ops->post) 1453ef43aa38SMilan Broz r = cc->iv_gen_ops->post(cc, org_iv, dmreq); 1454ef43aa38SMilan Broz 14558f0009a2SMilan Broz bio_advance_iter(ctx->bio_in, &ctx->iter_in, cc->sector_size); 14568f0009a2SMilan Broz bio_advance_iter(ctx->bio_out, &ctx->iter_out, cc->sector_size); 14572dc5327dSMilan Broz 14583a7f6c99SMilan Broz return r; 145901482b76SMilan Broz } 146001482b76SMilan Broz 146195497a96SMilan Broz static void kcryptd_async_done(struct crypto_async_request *async_req, 146295497a96SMilan Broz int error); 1463c0297721SAndi Kleen 1464d68b2958SIgnat Korchagin static int crypt_alloc_req_skcipher(struct crypt_config *cc, 1465ddd42edfSMilan Broz struct convert_context *ctx) 1466ddd42edfSMilan Broz { 1467c66029f4SMikulas Patocka unsigned key_index = ctx->cc_sector & (cc->tfms_count - 1); 1468c0297721SAndi Kleen 1469d68b2958SIgnat Korchagin if (!ctx->r.req) { 1470d68b2958SIgnat Korchagin ctx->r.req = mempool_alloc(&cc->req_pool, in_interrupt() ? GFP_ATOMIC : GFP_NOIO); 1471ef43aa38SMilan Broz if (!ctx->r.req) 1472d68b2958SIgnat Korchagin return -ENOMEM; 1473d68b2958SIgnat Korchagin } 1474c0297721SAndi Kleen 1475ef43aa38SMilan Broz skcipher_request_set_tfm(ctx->r.req, cc->cipher_tfm.tfms[key_index]); 147654cea3f6SMilan Broz 147754cea3f6SMilan Broz /* 147854cea3f6SMilan Broz * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs 147954cea3f6SMilan Broz * requests if driver request queue is full. 148054cea3f6SMilan Broz */ 1481ef43aa38SMilan Broz skcipher_request_set_callback(ctx->r.req, 1482432061b3SMikulas Patocka CRYPTO_TFM_REQ_MAY_BACKLOG, 1483ef43aa38SMilan Broz kcryptd_async_done, dmreq_of_req(cc, ctx->r.req)); 1484d68b2958SIgnat Korchagin 1485d68b2958SIgnat Korchagin return 0; 1486ddd42edfSMilan Broz } 1487ddd42edfSMilan Broz 1488d68b2958SIgnat Korchagin static int crypt_alloc_req_aead(struct crypt_config *cc, 1489ef43aa38SMilan Broz struct convert_context *ctx) 1490ef43aa38SMilan Broz { 1491004b8ae9SIgnat Korchagin if (!ctx->r.req_aead) { 1492004b8ae9SIgnat Korchagin ctx->r.req_aead = mempool_alloc(&cc->req_pool, in_interrupt() ? GFP_ATOMIC : GFP_NOIO); 1493004b8ae9SIgnat Korchagin if (!ctx->r.req_aead) 1494d68b2958SIgnat Korchagin return -ENOMEM; 1495d68b2958SIgnat Korchagin } 1496ef43aa38SMilan Broz 1497ef43aa38SMilan Broz aead_request_set_tfm(ctx->r.req_aead, cc->cipher_tfm.tfms_aead[0]); 1498ef43aa38SMilan Broz 1499ef43aa38SMilan Broz /* 1500ef43aa38SMilan Broz * Use REQ_MAY_BACKLOG so a cipher driver internally backlogs 1501ef43aa38SMilan Broz * requests if driver request queue is full. 1502ef43aa38SMilan Broz */ 1503ef43aa38SMilan Broz aead_request_set_callback(ctx->r.req_aead, 1504432061b3SMikulas Patocka CRYPTO_TFM_REQ_MAY_BACKLOG, 1505ef43aa38SMilan Broz kcryptd_async_done, dmreq_of_req(cc, ctx->r.req_aead)); 1506d68b2958SIgnat Korchagin 1507d68b2958SIgnat Korchagin return 0; 1508ef43aa38SMilan Broz } 1509ef43aa38SMilan Broz 1510d68b2958SIgnat Korchagin static int crypt_alloc_req(struct crypt_config *cc, 1511ef43aa38SMilan Broz struct convert_context *ctx) 1512ef43aa38SMilan Broz { 151333d2f09fSMilan Broz if (crypt_integrity_aead(cc)) 1514d68b2958SIgnat Korchagin return crypt_alloc_req_aead(cc, ctx); 1515ef43aa38SMilan Broz else 1516d68b2958SIgnat Korchagin return crypt_alloc_req_skcipher(cc, ctx); 1517ef43aa38SMilan Broz } 1518ef43aa38SMilan Broz 1519ef43aa38SMilan Broz static void crypt_free_req_skcipher(struct crypt_config *cc, 1520bbdb23b5SHerbert Xu struct skcipher_request *req, struct bio *base_bio) 1521298a9fa0SMikulas Patocka { 1522298a9fa0SMikulas Patocka struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); 1523298a9fa0SMikulas Patocka 1524bbdb23b5SHerbert Xu if ((struct skcipher_request *)(io + 1) != req) 15256f1c819cSKent Overstreet mempool_free(req, &cc->req_pool); 1526298a9fa0SMikulas Patocka } 1527298a9fa0SMikulas Patocka 1528ef43aa38SMilan Broz static void crypt_free_req_aead(struct crypt_config *cc, 1529ef43aa38SMilan Broz struct aead_request *req, struct bio *base_bio) 1530ef43aa38SMilan Broz { 1531ef43aa38SMilan Broz struct dm_crypt_io *io = dm_per_bio_data(base_bio, cc->per_bio_data_size); 1532ef43aa38SMilan Broz 1533ef43aa38SMilan Broz if ((struct aead_request *)(io + 1) != req) 15346f1c819cSKent Overstreet mempool_free(req, &cc->req_pool); 1535ef43aa38SMilan Broz } 1536ef43aa38SMilan Broz 1537ef43aa38SMilan Broz static void crypt_free_req(struct crypt_config *cc, void *req, struct bio *base_bio) 1538ef43aa38SMilan Broz { 153933d2f09fSMilan Broz if (crypt_integrity_aead(cc)) 1540ef43aa38SMilan Broz crypt_free_req_aead(cc, req, base_bio); 1541ef43aa38SMilan Broz else 1542ef43aa38SMilan Broz crypt_free_req_skcipher(cc, req, base_bio); 1543ef43aa38SMilan Broz } 1544ef43aa38SMilan Broz 15451da177e4SLinus Torvalds /* 15461da177e4SLinus Torvalds * Encrypt / decrypt data from one bio to another one (can be the same one) 15471da177e4SLinus Torvalds */ 15484e4cbee9SChristoph Hellwig static blk_status_t crypt_convert(struct crypt_config *cc, 15498abec36dSIgnat Korchagin struct convert_context *ctx, bool atomic, bool reset_pending) 15501da177e4SLinus Torvalds { 1551ef43aa38SMilan Broz unsigned int tag_offset = 0; 1552ff3af92bSMikulas Patocka unsigned int sector_step = cc->sector_size >> SECTOR_SHIFT; 15533f1e9070SMilan Broz int r; 15541da177e4SLinus Torvalds 15558abec36dSIgnat Korchagin /* 15568abec36dSIgnat Korchagin * if reset_pending is set we are dealing with the bio for the first time, 15578abec36dSIgnat Korchagin * else we're continuing to work on the previous bio, so don't mess with 15588abec36dSIgnat Korchagin * the cc_pending counter 15598abec36dSIgnat Korchagin */ 15608abec36dSIgnat Korchagin if (reset_pending) 156140b6229bSMikulas Patocka atomic_set(&ctx->cc_pending, 1); 1562c8081618SMilan Broz 1563003b5c57SKent Overstreet while (ctx->iter_in.bi_size && ctx->iter_out.bi_size) { 15641da177e4SLinus Torvalds 1565d68b2958SIgnat Korchagin r = crypt_alloc_req(cc, ctx); 1566d68b2958SIgnat Korchagin if (r) { 1567d68b2958SIgnat Korchagin complete(&ctx->restart); 1568d68b2958SIgnat Korchagin return BLK_STS_DEV_RESOURCE; 1569d68b2958SIgnat Korchagin } 1570d68b2958SIgnat Korchagin 157140b6229bSMikulas Patocka atomic_inc(&ctx->cc_pending); 15723f1e9070SMilan Broz 157333d2f09fSMilan Broz if (crypt_integrity_aead(cc)) 1574ef43aa38SMilan Broz r = crypt_convert_block_aead(cc, ctx, ctx->r.req_aead, tag_offset); 1575ef43aa38SMilan Broz else 1576ef43aa38SMilan Broz r = crypt_convert_block_skcipher(cc, ctx, ctx->r.req, tag_offset); 15773a7f6c99SMilan Broz 15783a7f6c99SMilan Broz switch (r) { 157954cea3f6SMilan Broz /* 158054cea3f6SMilan Broz * The request was queued by a crypto driver 158154cea3f6SMilan Broz * but the driver request queue is full, let's wait. 158254cea3f6SMilan Broz */ 15833a7f6c99SMilan Broz case -EBUSY: 15848abec36dSIgnat Korchagin if (in_interrupt()) { 15858abec36dSIgnat Korchagin if (try_wait_for_completion(&ctx->restart)) { 15868abec36dSIgnat Korchagin /* 15878abec36dSIgnat Korchagin * we don't have to block to wait for completion, 15888abec36dSIgnat Korchagin * so proceed 15898abec36dSIgnat Korchagin */ 15908abec36dSIgnat Korchagin } else { 15918abec36dSIgnat Korchagin /* 15928abec36dSIgnat Korchagin * we can't wait for completion without blocking 15938abec36dSIgnat Korchagin * exit and continue processing in a workqueue 15948abec36dSIgnat Korchagin */ 15958abec36dSIgnat Korchagin ctx->r.req = NULL; 15968abec36dSIgnat Korchagin ctx->cc_sector += sector_step; 15978abec36dSIgnat Korchagin tag_offset++; 15988abec36dSIgnat Korchagin return BLK_STS_DEV_RESOURCE; 15998abec36dSIgnat Korchagin } 16008abec36dSIgnat Korchagin } else { 16013a7f6c99SMilan Broz wait_for_completion(&ctx->restart); 16028abec36dSIgnat Korchagin } 160316735d02SWolfram Sang reinit_completion(&ctx->restart); 1604df561f66SGustavo A. R. Silva fallthrough; 160554cea3f6SMilan Broz /* 160654cea3f6SMilan Broz * The request is queued and processed asynchronously, 160754cea3f6SMilan Broz * completion function kcryptd_async_done() will be called. 160854cea3f6SMilan Broz */ 1609c0403ec0SRabin Vincent case -EINPROGRESS: 1610ef43aa38SMilan Broz ctx->r.req = NULL; 16118f0009a2SMilan Broz ctx->cc_sector += sector_step; 1612583fe747SMikulas Patocka tag_offset++; 16133a7f6c99SMilan Broz continue; 161454cea3f6SMilan Broz /* 161554cea3f6SMilan Broz * The request was already processed (synchronously). 161654cea3f6SMilan Broz */ 16173f1e9070SMilan Broz case 0: 161840b6229bSMikulas Patocka atomic_dec(&ctx->cc_pending); 16198f0009a2SMilan Broz ctx->cc_sector += sector_step; 1620583fe747SMikulas Patocka tag_offset++; 162139d42fa9SIgnat Korchagin if (!atomic) 1622c7f1b204SMilan Broz cond_resched(); 16233f1e9070SMilan Broz continue; 1624ef43aa38SMilan Broz /* 1625ef43aa38SMilan Broz * There was a data integrity error. 1626ef43aa38SMilan Broz */ 1627ef43aa38SMilan Broz case -EBADMSG: 1628ef43aa38SMilan Broz atomic_dec(&ctx->cc_pending); 16294e4cbee9SChristoph Hellwig return BLK_STS_PROTECTION; 1630ef43aa38SMilan Broz /* 1631ef43aa38SMilan Broz * There was an error while processing the request. 1632ef43aa38SMilan Broz */ 16333f1e9070SMilan Broz default: 163440b6229bSMikulas Patocka atomic_dec(&ctx->cc_pending); 16354e4cbee9SChristoph Hellwig return BLK_STS_IOERR; 16361da177e4SLinus Torvalds } 16373f1e9070SMilan Broz } 16383f1e9070SMilan Broz 16393f1e9070SMilan Broz return 0; 16403f1e9070SMilan Broz } 16411da177e4SLinus Torvalds 1642cf2f1abfSMikulas Patocka static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone); 1643cf2f1abfSMikulas Patocka 16441da177e4SLinus Torvalds /* 16451da177e4SLinus Torvalds * Generate a new unfragmented bio with the given size 1646586b286bSMike Snitzer * This should never violate the device limitations (but only because 1647586b286bSMike Snitzer * max_segment_size is being constrained to PAGE_SIZE). 16487145c241SMikulas Patocka * 16497145c241SMikulas Patocka * This function may be called concurrently. If we allocate from the mempool 16507145c241SMikulas Patocka * concurrently, there is a possibility of deadlock. For example, if we have 16517145c241SMikulas Patocka * mempool of 256 pages, two processes, each wanting 256, pages allocate from 16527145c241SMikulas Patocka * the mempool concurrently, it may deadlock in a situation where both processes 16537145c241SMikulas Patocka * have allocated 128 pages and the mempool is exhausted. 16547145c241SMikulas Patocka * 16557145c241SMikulas Patocka * In order to avoid this scenario we allocate the pages under a mutex. 16567145c241SMikulas Patocka * 16577145c241SMikulas Patocka * In order to not degrade performance with excessive locking, we try 16587145c241SMikulas Patocka * non-blocking allocations without a mutex first but on failure we fallback 16597145c241SMikulas Patocka * to blocking allocations with a mutex. 16601da177e4SLinus Torvalds */ 1661cf2f1abfSMikulas Patocka static struct bio *crypt_alloc_buffer(struct dm_crypt_io *io, unsigned size) 16621da177e4SLinus Torvalds { 166349a8a920SAlasdair G Kergon struct crypt_config *cc = io->cc; 16648b004457SMilan Broz struct bio *clone; 16651da177e4SLinus Torvalds unsigned int nr_iovecs = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 16667145c241SMikulas Patocka gfp_t gfp_mask = GFP_NOWAIT | __GFP_HIGHMEM; 16677145c241SMikulas Patocka unsigned i, len, remaining_size; 166891e10625SMilan Broz struct page *page; 16691da177e4SLinus Torvalds 16707145c241SMikulas Patocka retry: 1671d0164adcSMel Gorman if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM)) 16727145c241SMikulas Patocka mutex_lock(&cc->bio_alloc_lock); 16737145c241SMikulas Patocka 1674609be106SChristoph Hellwig clone = bio_alloc_bioset(cc->dev->bdev, nr_iovecs, io->base_bio->bi_opf, 1675609be106SChristoph Hellwig GFP_NOIO, &cc->bs); 16763f868c09SChristoph Hellwig clone->bi_private = io; 16773f868c09SChristoph Hellwig clone->bi_end_io = crypt_endio; 16786a24c718SMilan Broz 16797145c241SMikulas Patocka remaining_size = size; 16807145c241SMikulas Patocka 1681f97380bcSOlaf Kirch for (i = 0; i < nr_iovecs; i++) { 16826f1c819cSKent Overstreet page = mempool_alloc(&cc->page_pool, gfp_mask); 16837145c241SMikulas Patocka if (!page) { 16847145c241SMikulas Patocka crypt_free_buffer_pages(cc, clone); 16857145c241SMikulas Patocka bio_put(clone); 1686d0164adcSMel Gorman gfp_mask |= __GFP_DIRECT_RECLAIM; 16877145c241SMikulas Patocka goto retry; 16887145c241SMikulas Patocka } 16891da177e4SLinus Torvalds 16907145c241SMikulas Patocka len = (remaining_size > PAGE_SIZE) ? PAGE_SIZE : remaining_size; 16911da177e4SLinus Torvalds 16920dae7fe5SMing Lei bio_add_page(clone, page, len, 0); 169391e10625SMilan Broz 16947145c241SMikulas Patocka remaining_size -= len; 16951da177e4SLinus Torvalds } 16961da177e4SLinus Torvalds 1697ef43aa38SMilan Broz /* Allocate space for integrity tags */ 1698ef43aa38SMilan Broz if (dm_crypt_integrity_io_alloc(io, clone)) { 1699ef43aa38SMilan Broz crypt_free_buffer_pages(cc, clone); 1700ef43aa38SMilan Broz bio_put(clone); 1701ef43aa38SMilan Broz clone = NULL; 1702ef43aa38SMilan Broz } 170353db984eSChristoph Hellwig 1704d0164adcSMel Gorman if (unlikely(gfp_mask & __GFP_DIRECT_RECLAIM)) 17057145c241SMikulas Patocka mutex_unlock(&cc->bio_alloc_lock); 17067145c241SMikulas Patocka 17078b004457SMilan Broz return clone; 17081da177e4SLinus Torvalds } 17091da177e4SLinus Torvalds 1710644bd2f0SNeil Brown static void crypt_free_buffer_pages(struct crypt_config *cc, struct bio *clone) 17111da177e4SLinus Torvalds { 17121da177e4SLinus Torvalds struct bio_vec *bv; 17136dc4f100SMing Lei struct bvec_iter_all iter_all; 17141da177e4SLinus Torvalds 17152b070cfeSChristoph Hellwig bio_for_each_segment_all(bv, clone, iter_all) { 17161da177e4SLinus Torvalds BUG_ON(!bv->bv_page); 17176f1c819cSKent Overstreet mempool_free(bv->bv_page, &cc->page_pool); 17181da177e4SLinus Torvalds } 17191da177e4SLinus Torvalds } 17201da177e4SLinus Torvalds 1721298a9fa0SMikulas Patocka static void crypt_io_init(struct dm_crypt_io *io, struct crypt_config *cc, 1722dc440d1eSMilan Broz struct bio *bio, sector_t sector) 1723dc440d1eSMilan Broz { 172449a8a920SAlasdair G Kergon io->cc = cc; 1725dc440d1eSMilan Broz io->base_bio = bio; 1726dc440d1eSMilan Broz io->sector = sector; 1727dc440d1eSMilan Broz io->error = 0; 1728ef43aa38SMilan Broz io->ctx.r.req = NULL; 1729ef43aa38SMilan Broz io->integrity_metadata = NULL; 1730ef43aa38SMilan Broz io->integrity_metadata_from_pool = false; 173140b6229bSMikulas Patocka atomic_set(&io->io_pending, 0); 1732dc440d1eSMilan Broz } 1733dc440d1eSMilan Broz 17343e1a8bddSMilan Broz static void crypt_inc_pending(struct dm_crypt_io *io) 17353e1a8bddSMilan Broz { 173640b6229bSMikulas Patocka atomic_inc(&io->io_pending); 17373e1a8bddSMilan Broz } 17383e1a8bddSMilan Broz 17398e14f610SIgnat Korchagin static void kcryptd_io_bio_endio(struct work_struct *work) 17408e14f610SIgnat Korchagin { 17418e14f610SIgnat Korchagin struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); 17428e14f610SIgnat Korchagin bio_endio(io->base_bio); 17438e14f610SIgnat Korchagin } 17448e14f610SIgnat Korchagin 17451da177e4SLinus Torvalds /* 17461da177e4SLinus Torvalds * One of the bios was finished. Check for completion of 17471da177e4SLinus Torvalds * the whole request and correctly clean up the buffer. 17481da177e4SLinus Torvalds */ 17495742fd77SMilan Broz static void crypt_dec_pending(struct dm_crypt_io *io) 17501da177e4SLinus Torvalds { 175149a8a920SAlasdair G Kergon struct crypt_config *cc = io->cc; 1752b35f8caaSMilan Broz struct bio *base_bio = io->base_bio; 17534e4cbee9SChristoph Hellwig blk_status_t error = io->error; 17541da177e4SLinus Torvalds 175540b6229bSMikulas Patocka if (!atomic_dec_and_test(&io->io_pending)) 17561da177e4SLinus Torvalds return; 17571da177e4SLinus Torvalds 1758ef43aa38SMilan Broz if (io->ctx.r.req) 1759ef43aa38SMilan Broz crypt_free_req(cc, io->ctx.r.req, base_bio); 1760ef43aa38SMilan Broz 1761ef43aa38SMilan Broz if (unlikely(io->integrity_metadata_from_pool)) 17626f1c819cSKent Overstreet mempool_free(io->integrity_metadata, &io->cc->tag_pool); 1763ef43aa38SMilan Broz else 1764ef43aa38SMilan Broz kfree(io->integrity_metadata); 1765b35f8caaSMilan Broz 17664e4cbee9SChristoph Hellwig base_bio->bi_status = error; 17678e14f610SIgnat Korchagin 17688e14f610SIgnat Korchagin /* 17698e14f610SIgnat Korchagin * If we are running this function from our tasklet, 17708e14f610SIgnat Korchagin * we can't call bio_endio() here, because it will call 17718e14f610SIgnat Korchagin * clone_endio() from dm.c, which in turn will 17728e14f610SIgnat Korchagin * free the current struct dm_crypt_io structure with 17738e14f610SIgnat Korchagin * our tasklet. In this case we need to delay bio_endio() 17748e14f610SIgnat Korchagin * execution to after the tasklet is done and dequeued. 17758e14f610SIgnat Korchagin */ 17768e14f610SIgnat Korchagin if (tasklet_trylock(&io->tasklet)) { 17778e14f610SIgnat Korchagin tasklet_unlock(&io->tasklet); 17784246a0b6SChristoph Hellwig bio_endio(base_bio); 17798e14f610SIgnat Korchagin return; 17808e14f610SIgnat Korchagin } 17818e14f610SIgnat Korchagin 17828e14f610SIgnat Korchagin INIT_WORK(&io->work, kcryptd_io_bio_endio); 17838e14f610SIgnat Korchagin queue_work(cc->io_queue, &io->work); 17841da177e4SLinus Torvalds } 17851da177e4SLinus Torvalds 17861da177e4SLinus Torvalds /* 1787cabf08e4SMilan Broz * kcryptd/kcryptd_io: 17881da177e4SLinus Torvalds * 17891da177e4SLinus Torvalds * Needed because it would be very unwise to do decryption in an 179023541d2dSMilan Broz * interrupt context. 1791cabf08e4SMilan Broz * 1792cabf08e4SMilan Broz * kcryptd performs the actual encryption or decryption. 1793cabf08e4SMilan Broz * 1794cabf08e4SMilan Broz * kcryptd_io performs the IO submission. 1795cabf08e4SMilan Broz * 1796cabf08e4SMilan Broz * They must be separated as otherwise the final stages could be 1797cabf08e4SMilan Broz * starved by new requests which can block in the first stages due 1798cabf08e4SMilan Broz * to memory allocation. 1799c0297721SAndi Kleen * 1800c0297721SAndi Kleen * The work is done per CPU global for all dm-crypt instances. 1801c0297721SAndi Kleen * They should not depend on each other and do not block. 18021da177e4SLinus Torvalds */ 18034246a0b6SChristoph Hellwig static void crypt_endio(struct bio *clone) 18048b004457SMilan Broz { 1805028867acSAlasdair G Kergon struct dm_crypt_io *io = clone->bi_private; 180649a8a920SAlasdair G Kergon struct crypt_config *cc = io->cc; 1807ee7a491eSMilan Broz unsigned rw = bio_data_dir(clone); 18084e4cbee9SChristoph Hellwig blk_status_t error; 18098b004457SMilan Broz 18108b004457SMilan Broz /* 18116712ecf8SNeilBrown * free the processed pages 18128b004457SMilan Broz */ 1813ee7a491eSMilan Broz if (rw == WRITE) 1814644bd2f0SNeil Brown crypt_free_buffer_pages(cc, clone); 18158b004457SMilan Broz 18164e4cbee9SChristoph Hellwig error = clone->bi_status; 18178b004457SMilan Broz bio_put(clone); 1818ee7a491eSMilan Broz 18199b81c842SSasha Levin if (rw == READ && !error) { 1820cabf08e4SMilan Broz kcryptd_queue_crypt(io); 18216712ecf8SNeilBrown return; 1822ee7a491eSMilan Broz } 18235742fd77SMilan Broz 18249b81c842SSasha Levin if (unlikely(error)) 18259b81c842SSasha Levin io->error = error; 18265742fd77SMilan Broz 18275742fd77SMilan Broz crypt_dec_pending(io); 18288b004457SMilan Broz } 18298b004457SMilan Broz 1830e5524e12SMike Snitzer #define CRYPT_MAP_READ_GFP GFP_NOWAIT 1831e5524e12SMike Snitzer 183220c82538SMilan Broz static int kcryptd_io_read(struct dm_crypt_io *io, gfp_t gfp) 18338b004457SMilan Broz { 183449a8a920SAlasdair G Kergon struct crypt_config *cc = io->cc; 18358b004457SMilan Broz struct bio *clone; 183693e605c2SMilan Broz 18378b004457SMilan Broz /* 1838abfc426dSChristoph Hellwig * We need the original biovec array in order to decrypt the whole bio 1839abfc426dSChristoph Hellwig * data *afterwards* -- thanks to immutable biovecs we don't need to 1840abfc426dSChristoph Hellwig * worry about the block layer modifying the biovec array; so leverage 1841abfc426dSChristoph Hellwig * bio_alloc_clone(). 18428b004457SMilan Broz */ 1843abfc426dSChristoph Hellwig clone = bio_alloc_clone(cc->dev->bdev, io->base_bio, gfp, &cc->bs); 18447eaceaccSJens Axboe if (!clone) 184520c82538SMilan Broz return 1; 18463f868c09SChristoph Hellwig clone->bi_private = io; 18473f868c09SChristoph Hellwig clone->bi_end_io = crypt_endio; 18488b004457SMilan Broz 184920c82538SMilan Broz crypt_inc_pending(io); 185020c82538SMilan Broz 18514f024f37SKent Overstreet clone->bi_iter.bi_sector = cc->start + io->sector; 18528b004457SMilan Broz 1853ef43aa38SMilan Broz if (dm_crypt_integrity_io_alloc(io, clone)) { 1854ef43aa38SMilan Broz crypt_dec_pending(io); 1855ef43aa38SMilan Broz bio_put(clone); 1856ef43aa38SMilan Broz return 1; 1857ef43aa38SMilan Broz } 1858ef43aa38SMilan Broz 1859b7f8dff0SMike Snitzer dm_submit_bio_remap(io->base_bio, clone); 186020c82538SMilan Broz return 0; 18618b004457SMilan Broz } 18628b004457SMilan Broz 1863dc267621SMikulas Patocka static void kcryptd_io_read_work(struct work_struct *work) 1864395b167cSAlasdair G Kergon { 1865395b167cSAlasdair G Kergon struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); 1866395b167cSAlasdair G Kergon 186720c82538SMilan Broz crypt_inc_pending(io); 186820c82538SMilan Broz if (kcryptd_io_read(io, GFP_NOIO)) 18694e4cbee9SChristoph Hellwig io->error = BLK_STS_RESOURCE; 187020c82538SMilan Broz crypt_dec_pending(io); 1871395b167cSAlasdair G Kergon } 1872395b167cSAlasdair G Kergon 1873dc267621SMikulas Patocka static void kcryptd_queue_read(struct dm_crypt_io *io) 1874395b167cSAlasdair G Kergon { 187549a8a920SAlasdair G Kergon struct crypt_config *cc = io->cc; 1876395b167cSAlasdair G Kergon 1877dc267621SMikulas Patocka INIT_WORK(&io->work, kcryptd_io_read_work); 1878395b167cSAlasdair G Kergon queue_work(cc->io_queue, &io->work); 1879395b167cSAlasdair G Kergon } 1880395b167cSAlasdair G Kergon 1881dc267621SMikulas Patocka static void kcryptd_io_write(struct dm_crypt_io *io) 1882dc267621SMikulas Patocka { 1883dc267621SMikulas Patocka struct bio *clone = io->ctx.bio_out; 1884dc267621SMikulas Patocka 1885b7f8dff0SMike Snitzer dm_submit_bio_remap(io->base_bio, clone); 1886dc267621SMikulas Patocka } 1887dc267621SMikulas Patocka 1888b3c5fd30SMikulas Patocka #define crypt_io_from_node(node) rb_entry((node), struct dm_crypt_io, rb_node) 1889b3c5fd30SMikulas Patocka 1890dc267621SMikulas Patocka static int dmcrypt_write(void *data) 1891dc267621SMikulas Patocka { 1892dc267621SMikulas Patocka struct crypt_config *cc = data; 1893b3c5fd30SMikulas Patocka struct dm_crypt_io *io; 1894b3c5fd30SMikulas Patocka 1895dc267621SMikulas Patocka while (1) { 1896b3c5fd30SMikulas Patocka struct rb_root write_tree; 1897dc267621SMikulas Patocka struct blk_plug plug; 1898dc267621SMikulas Patocka 1899c7329effSMikulas Patocka spin_lock_irq(&cc->write_thread_lock); 1900dc267621SMikulas Patocka continue_locked: 1901dc267621SMikulas Patocka 1902b3c5fd30SMikulas Patocka if (!RB_EMPTY_ROOT(&cc->write_tree)) 1903dc267621SMikulas Patocka goto pop_from_list; 1904dc267621SMikulas Patocka 1905f659b100SRabin Vincent set_current_state(TASK_INTERRUPTIBLE); 1906dc267621SMikulas Patocka 1907c7329effSMikulas Patocka spin_unlock_irq(&cc->write_thread_lock); 1908dc267621SMikulas Patocka 1909f659b100SRabin Vincent if (unlikely(kthread_should_stop())) { 1910642fa448SDavidlohr Bueso set_current_state(TASK_RUNNING); 1911f659b100SRabin Vincent break; 1912f659b100SRabin Vincent } 1913f659b100SRabin Vincent 1914dc267621SMikulas Patocka schedule(); 1915dc267621SMikulas Patocka 1916642fa448SDavidlohr Bueso set_current_state(TASK_RUNNING); 1917c7329effSMikulas Patocka spin_lock_irq(&cc->write_thread_lock); 1918dc267621SMikulas Patocka goto continue_locked; 1919dc267621SMikulas Patocka 1920dc267621SMikulas Patocka pop_from_list: 1921b3c5fd30SMikulas Patocka write_tree = cc->write_tree; 1922b3c5fd30SMikulas Patocka cc->write_tree = RB_ROOT; 1923c7329effSMikulas Patocka spin_unlock_irq(&cc->write_thread_lock); 1924dc267621SMikulas Patocka 1925b3c5fd30SMikulas Patocka BUG_ON(rb_parent(write_tree.rb_node)); 1926b3c5fd30SMikulas Patocka 1927b3c5fd30SMikulas Patocka /* 1928b3c5fd30SMikulas Patocka * Note: we cannot walk the tree here with rb_next because 1929b3c5fd30SMikulas Patocka * the structures may be freed when kcryptd_io_write is called. 1930b3c5fd30SMikulas Patocka */ 1931dc267621SMikulas Patocka blk_start_plug(&plug); 1932dc267621SMikulas Patocka do { 1933b3c5fd30SMikulas Patocka io = crypt_io_from_node(rb_first(&write_tree)); 1934b3c5fd30SMikulas Patocka rb_erase(&io->rb_node, &write_tree); 1935dc267621SMikulas Patocka kcryptd_io_write(io); 1936b3c5fd30SMikulas Patocka } while (!RB_EMPTY_ROOT(&write_tree)); 1937dc267621SMikulas Patocka blk_finish_plug(&plug); 1938dc267621SMikulas Patocka } 1939dc267621SMikulas Patocka return 0; 1940dc267621SMikulas Patocka } 1941dc267621SMikulas Patocka 194272c6e7afSMikulas Patocka static void kcryptd_crypt_write_io_submit(struct dm_crypt_io *io, int async) 19434e4eef64SMilan Broz { 1944dec1cedfSMilan Broz struct bio *clone = io->ctx.bio_out; 194549a8a920SAlasdair G Kergon struct crypt_config *cc = io->cc; 1946dc267621SMikulas Patocka unsigned long flags; 1947b3c5fd30SMikulas Patocka sector_t sector; 1948b3c5fd30SMikulas Patocka struct rb_node **rbp, *parent; 1949dec1cedfSMilan Broz 19504e4cbee9SChristoph Hellwig if (unlikely(io->error)) { 1951dec1cedfSMilan Broz crypt_free_buffer_pages(cc, clone); 1952dec1cedfSMilan Broz bio_put(clone); 19536c031f41SMilan Broz crypt_dec_pending(io); 1954dec1cedfSMilan Broz return; 1955dec1cedfSMilan Broz } 1956dec1cedfSMilan Broz 1957dec1cedfSMilan Broz /* crypt_convert should have filled the clone bio */ 1958003b5c57SKent Overstreet BUG_ON(io->ctx.iter_out.bi_size); 1959dec1cedfSMilan Broz 19604f024f37SKent Overstreet clone->bi_iter.bi_sector = cc->start + io->sector; 1961899c95d3SMilan Broz 196239d42fa9SIgnat Korchagin if ((likely(!async) && test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) || 196339d42fa9SIgnat Korchagin test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) { 1964b7f8dff0SMike Snitzer dm_submit_bio_remap(io->base_bio, clone); 19650f5d8e6eSMikulas Patocka return; 19660f5d8e6eSMikulas Patocka } 19670f5d8e6eSMikulas Patocka 1968c7329effSMikulas Patocka spin_lock_irqsave(&cc->write_thread_lock, flags); 1969c7329effSMikulas Patocka if (RB_EMPTY_ROOT(&cc->write_tree)) 1970c7329effSMikulas Patocka wake_up_process(cc->write_thread); 1971b3c5fd30SMikulas Patocka rbp = &cc->write_tree.rb_node; 1972b3c5fd30SMikulas Patocka parent = NULL; 1973b3c5fd30SMikulas Patocka sector = io->sector; 1974b3c5fd30SMikulas Patocka while (*rbp) { 1975b3c5fd30SMikulas Patocka parent = *rbp; 1976b3c5fd30SMikulas Patocka if (sector < crypt_io_from_node(parent)->sector) 1977b3c5fd30SMikulas Patocka rbp = &(*rbp)->rb_left; 1978b3c5fd30SMikulas Patocka else 1979b3c5fd30SMikulas Patocka rbp = &(*rbp)->rb_right; 1980b3c5fd30SMikulas Patocka } 1981b3c5fd30SMikulas Patocka rb_link_node(&io->rb_node, parent, rbp); 1982b3c5fd30SMikulas Patocka rb_insert_color(&io->rb_node, &cc->write_tree); 1983c7329effSMikulas Patocka spin_unlock_irqrestore(&cc->write_thread_lock, flags); 19844e4eef64SMilan Broz } 19854e4eef64SMilan Broz 19868e225f04SDamien Le Moal static bool kcryptd_crypt_write_inline(struct crypt_config *cc, 19878e225f04SDamien Le Moal struct convert_context *ctx) 19888e225f04SDamien Le Moal 19898e225f04SDamien Le Moal { 19908e225f04SDamien Le Moal if (!test_bit(DM_CRYPT_WRITE_INLINE, &cc->flags)) 19918e225f04SDamien Le Moal return false; 19928e225f04SDamien Le Moal 19938e225f04SDamien Le Moal /* 19948e225f04SDamien Le Moal * Note: zone append writes (REQ_OP_ZONE_APPEND) do not have ordering 19958e225f04SDamien Le Moal * constraints so they do not need to be issued inline by 19968e225f04SDamien Le Moal * kcryptd_crypt_write_convert(). 19978e225f04SDamien Le Moal */ 19988e225f04SDamien Le Moal switch (bio_op(ctx->bio_in)) { 19998e225f04SDamien Le Moal case REQ_OP_WRITE: 20008e225f04SDamien Le Moal case REQ_OP_WRITE_ZEROES: 20018e225f04SDamien Le Moal return true; 20028e225f04SDamien Le Moal default: 20038e225f04SDamien Le Moal return false; 20048e225f04SDamien Le Moal } 20058e225f04SDamien Le Moal } 20068e225f04SDamien Le Moal 20078abec36dSIgnat Korchagin static void kcryptd_crypt_write_continue(struct work_struct *work) 20088abec36dSIgnat Korchagin { 20098abec36dSIgnat Korchagin struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); 20108abec36dSIgnat Korchagin struct crypt_config *cc = io->cc; 20118abec36dSIgnat Korchagin struct convert_context *ctx = &io->ctx; 20128abec36dSIgnat Korchagin int crypt_finished; 20138abec36dSIgnat Korchagin sector_t sector = io->sector; 20148abec36dSIgnat Korchagin blk_status_t r; 20158abec36dSIgnat Korchagin 20168abec36dSIgnat Korchagin wait_for_completion(&ctx->restart); 20178abec36dSIgnat Korchagin reinit_completion(&ctx->restart); 20188abec36dSIgnat Korchagin 20198abec36dSIgnat Korchagin r = crypt_convert(cc, &io->ctx, true, false); 20208abec36dSIgnat Korchagin if (r) 20218abec36dSIgnat Korchagin io->error = r; 20228abec36dSIgnat Korchagin crypt_finished = atomic_dec_and_test(&ctx->cc_pending); 20238abec36dSIgnat Korchagin if (!crypt_finished && kcryptd_crypt_write_inline(cc, ctx)) { 20248abec36dSIgnat Korchagin /* Wait for completion signaled by kcryptd_async_done() */ 20258abec36dSIgnat Korchagin wait_for_completion(&ctx->restart); 20268abec36dSIgnat Korchagin crypt_finished = 1; 20278abec36dSIgnat Korchagin } 20288abec36dSIgnat Korchagin 20298abec36dSIgnat Korchagin /* Encryption was already finished, submit io now */ 20308abec36dSIgnat Korchagin if (crypt_finished) { 20318abec36dSIgnat Korchagin kcryptd_crypt_write_io_submit(io, 0); 20328abec36dSIgnat Korchagin io->sector = sector; 20338abec36dSIgnat Korchagin } 20348abec36dSIgnat Korchagin 20358abec36dSIgnat Korchagin crypt_dec_pending(io); 20368abec36dSIgnat Korchagin } 20378abec36dSIgnat Korchagin 2038fc5a5e9aSMilan Broz static void kcryptd_crypt_write_convert(struct dm_crypt_io *io) 20398b004457SMilan Broz { 204049a8a920SAlasdair G Kergon struct crypt_config *cc = io->cc; 20418e225f04SDamien Le Moal struct convert_context *ctx = &io->ctx; 20428b004457SMilan Broz struct bio *clone; 2043c8081618SMilan Broz int crypt_finished; 2044b635b00eSMilan Broz sector_t sector = io->sector; 20454e4cbee9SChristoph Hellwig blk_status_t r; 20468b004457SMilan Broz 204793e605c2SMilan Broz /* 2048fc5a5e9aSMilan Broz * Prevent io from disappearing until this function completes. 2049fc5a5e9aSMilan Broz */ 2050fc5a5e9aSMilan Broz crypt_inc_pending(io); 20518e225f04SDamien Le Moal crypt_convert_init(cc, ctx, NULL, io->base_bio, sector); 2052fc5a5e9aSMilan Broz 2053cf2f1abfSMikulas Patocka clone = crypt_alloc_buffer(io, io->base_bio->bi_iter.bi_size); 205423541d2dSMilan Broz if (unlikely(!clone)) { 20554e4cbee9SChristoph Hellwig io->error = BLK_STS_IOERR; 2056cf2f1abfSMikulas Patocka goto dec; 205723541d2dSMilan Broz } 20588b004457SMilan Broz 205953017030SMilan Broz io->ctx.bio_out = clone; 2060003b5c57SKent Overstreet io->ctx.iter_out = clone->bi_iter; 20618b004457SMilan Broz 2062b635b00eSMilan Broz sector += bio_sectors(clone); 2063dec1cedfSMilan Broz 20644e594098SMilan Broz crypt_inc_pending(io); 20658e225f04SDamien Le Moal r = crypt_convert(cc, ctx, 20668abec36dSIgnat Korchagin test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags), true); 20678abec36dSIgnat Korchagin /* 20688abec36dSIgnat Korchagin * Crypto API backlogged the request, because its queue was full 20698abec36dSIgnat Korchagin * and we're in softirq context, so continue from a workqueue 20708abec36dSIgnat Korchagin * (TODO: is it actually possible to be in softirq in the write path?) 20718abec36dSIgnat Korchagin */ 20728abec36dSIgnat Korchagin if (r == BLK_STS_DEV_RESOURCE) { 20738abec36dSIgnat Korchagin INIT_WORK(&io->work, kcryptd_crypt_write_continue); 20748abec36dSIgnat Korchagin queue_work(cc->crypt_queue, &io->work); 20758abec36dSIgnat Korchagin return; 20768abec36dSIgnat Korchagin } 20774e4cbee9SChristoph Hellwig if (r) 2078ef43aa38SMilan Broz io->error = r; 20798e225f04SDamien Le Moal crypt_finished = atomic_dec_and_test(&ctx->cc_pending); 20808e225f04SDamien Le Moal if (!crypt_finished && kcryptd_crypt_write_inline(cc, ctx)) { 20818e225f04SDamien Le Moal /* Wait for completion signaled by kcryptd_async_done() */ 20828e225f04SDamien Le Moal wait_for_completion(&ctx->restart); 20838e225f04SDamien Le Moal crypt_finished = 1; 20848e225f04SDamien Le Moal } 2085dec1cedfSMilan Broz 2086c8081618SMilan Broz /* Encryption was already finished, submit io now */ 2087c8081618SMilan Broz if (crypt_finished) { 208872c6e7afSMikulas Patocka kcryptd_crypt_write_io_submit(io, 0); 2089b635b00eSMilan Broz io->sector = sector; 20904e594098SMilan Broz } 209193e605c2SMilan Broz 2092cf2f1abfSMikulas Patocka dec: 2093899c95d3SMilan Broz crypt_dec_pending(io); 209484131db6SMilan Broz } 209584131db6SMilan Broz 209672c6e7afSMikulas Patocka static void kcryptd_crypt_read_done(struct dm_crypt_io *io) 20975742fd77SMilan Broz { 20985742fd77SMilan Broz crypt_dec_pending(io); 20995742fd77SMilan Broz } 21005742fd77SMilan Broz 21018abec36dSIgnat Korchagin static void kcryptd_crypt_read_continue(struct work_struct *work) 21028abec36dSIgnat Korchagin { 21038abec36dSIgnat Korchagin struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); 21048abec36dSIgnat Korchagin struct crypt_config *cc = io->cc; 21058abec36dSIgnat Korchagin blk_status_t r; 21068abec36dSIgnat Korchagin 21078abec36dSIgnat Korchagin wait_for_completion(&io->ctx.restart); 21088abec36dSIgnat Korchagin reinit_completion(&io->ctx.restart); 21098abec36dSIgnat Korchagin 21108abec36dSIgnat Korchagin r = crypt_convert(cc, &io->ctx, true, false); 21118abec36dSIgnat Korchagin if (r) 21128abec36dSIgnat Korchagin io->error = r; 21138abec36dSIgnat Korchagin 21148abec36dSIgnat Korchagin if (atomic_dec_and_test(&io->ctx.cc_pending)) 21158abec36dSIgnat Korchagin kcryptd_crypt_read_done(io); 21168abec36dSIgnat Korchagin 21178abec36dSIgnat Korchagin crypt_dec_pending(io); 21188abec36dSIgnat Korchagin } 21198abec36dSIgnat Korchagin 21204e4eef64SMilan Broz static void kcryptd_crypt_read_convert(struct dm_crypt_io *io) 21218b004457SMilan Broz { 212249a8a920SAlasdair G Kergon struct crypt_config *cc = io->cc; 21234e4cbee9SChristoph Hellwig blk_status_t r; 21248b004457SMilan Broz 21253e1a8bddSMilan Broz crypt_inc_pending(io); 21263a7f6c99SMilan Broz 212753017030SMilan Broz crypt_convert_init(cc, &io->ctx, io->base_bio, io->base_bio, 21280c395b0fSMilan Broz io->sector); 21298b004457SMilan Broz 213039d42fa9SIgnat Korchagin r = crypt_convert(cc, &io->ctx, 21318abec36dSIgnat Korchagin test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags), true); 21328abec36dSIgnat Korchagin /* 21338abec36dSIgnat Korchagin * Crypto API backlogged the request, because its queue was full 21348abec36dSIgnat Korchagin * and we're in softirq context, so continue from a workqueue 21358abec36dSIgnat Korchagin */ 21368abec36dSIgnat Korchagin if (r == BLK_STS_DEV_RESOURCE) { 21378abec36dSIgnat Korchagin INIT_WORK(&io->work, kcryptd_crypt_read_continue); 21388abec36dSIgnat Korchagin queue_work(cc->crypt_queue, &io->work); 21398abec36dSIgnat Korchagin return; 21408abec36dSIgnat Korchagin } 21414e4cbee9SChristoph Hellwig if (r) 2142ef43aa38SMilan Broz io->error = r; 21435742fd77SMilan Broz 214440b6229bSMikulas Patocka if (atomic_dec_and_test(&io->ctx.cc_pending)) 214572c6e7afSMikulas Patocka kcryptd_crypt_read_done(io); 21463a7f6c99SMilan Broz 21473a7f6c99SMilan Broz crypt_dec_pending(io); 21488b004457SMilan Broz } 21498b004457SMilan Broz 215095497a96SMilan Broz static void kcryptd_async_done(struct crypto_async_request *async_req, 215195497a96SMilan Broz int error) 215295497a96SMilan Broz { 2153b2174eebSHuang Ying struct dm_crypt_request *dmreq = async_req->data; 2154b2174eebSHuang Ying struct convert_context *ctx = dmreq->ctx; 215595497a96SMilan Broz struct dm_crypt_io *io = container_of(ctx, struct dm_crypt_io, ctx); 215649a8a920SAlasdair G Kergon struct crypt_config *cc = io->cc; 215795497a96SMilan Broz 215854cea3f6SMilan Broz /* 215954cea3f6SMilan Broz * A request from crypto driver backlog is going to be processed now, 216054cea3f6SMilan Broz * finish the completion and continue in crypt_convert(). 216154cea3f6SMilan Broz * (Callback will be called for the second time for this request.) 216254cea3f6SMilan Broz */ 2163c0403ec0SRabin Vincent if (error == -EINPROGRESS) { 2164c0403ec0SRabin Vincent complete(&ctx->restart); 216595497a96SMilan Broz return; 2166c0403ec0SRabin Vincent } 216795497a96SMilan Broz 21682dc5327dSMilan Broz if (!error && cc->iv_gen_ops && cc->iv_gen_ops->post) 2169ef43aa38SMilan Broz error = cc->iv_gen_ops->post(cc, org_iv_of_dmreq(cc, dmreq), dmreq); 21702dc5327dSMilan Broz 2171ef43aa38SMilan Broz if (error == -EBADMSG) { 217258d0f180SMichael Weiß sector_t s = le64_to_cpu(*org_sector_of_dmreq(cc, dmreq)); 217358d0f180SMichael Weiß 217466671719SChristoph Hellwig DMERR_LIMIT("%pg: INTEGRITY AEAD ERROR, sector %llu", 217566671719SChristoph Hellwig ctx->bio_in->bi_bdev, s); 217658d0f180SMichael Weiß dm_audit_log_bio(DM_MSG_PREFIX, "integrity-aead", 217758d0f180SMichael Weiß ctx->bio_in, s, 0); 21784e4cbee9SChristoph Hellwig io->error = BLK_STS_PROTECTION; 2179ef43aa38SMilan Broz } else if (error < 0) 21804e4cbee9SChristoph Hellwig io->error = BLK_STS_IOERR; 218172c6e7afSMikulas Patocka 2182298a9fa0SMikulas Patocka crypt_free_req(cc, req_of_dmreq(cc, dmreq), io->base_bio); 218395497a96SMilan Broz 218440b6229bSMikulas Patocka if (!atomic_dec_and_test(&ctx->cc_pending)) 2185c0403ec0SRabin Vincent return; 218695497a96SMilan Broz 21878e225f04SDamien Le Moal /* 21888e225f04SDamien Le Moal * The request is fully completed: for inline writes, let 21898e225f04SDamien Le Moal * kcryptd_crypt_write_convert() do the IO submission. 21908e225f04SDamien Le Moal */ 21918e225f04SDamien Le Moal if (bio_data_dir(io->base_bio) == READ) { 219272c6e7afSMikulas Patocka kcryptd_crypt_read_done(io); 21938e225f04SDamien Le Moal return; 21948e225f04SDamien Le Moal } 21958e225f04SDamien Le Moal 21968e225f04SDamien Le Moal if (kcryptd_crypt_write_inline(cc, ctx)) { 21978e225f04SDamien Le Moal complete(&ctx->restart); 21988e225f04SDamien Le Moal return; 21998e225f04SDamien Le Moal } 22008e225f04SDamien Le Moal 220172c6e7afSMikulas Patocka kcryptd_crypt_write_io_submit(io, 1); 220295497a96SMilan Broz } 220395497a96SMilan Broz 22044e4eef64SMilan Broz static void kcryptd_crypt(struct work_struct *work) 22054e4eef64SMilan Broz { 22064e4eef64SMilan Broz struct dm_crypt_io *io = container_of(work, struct dm_crypt_io, work); 22074e4eef64SMilan Broz 22084e4eef64SMilan Broz if (bio_data_dir(io->base_bio) == READ) 22094e4eef64SMilan Broz kcryptd_crypt_read_convert(io); 22104e4eef64SMilan Broz else 22114e4eef64SMilan Broz kcryptd_crypt_write_convert(io); 22128b004457SMilan Broz } 22138b004457SMilan Broz 221439d42fa9SIgnat Korchagin static void kcryptd_crypt_tasklet(unsigned long work) 221539d42fa9SIgnat Korchagin { 221639d42fa9SIgnat Korchagin kcryptd_crypt((struct work_struct *)work); 221739d42fa9SIgnat Korchagin } 221839d42fa9SIgnat Korchagin 2219395b167cSAlasdair G Kergon static void kcryptd_queue_crypt(struct dm_crypt_io *io) 2220395b167cSAlasdair G Kergon { 222149a8a920SAlasdair G Kergon struct crypt_config *cc = io->cc; 2222395b167cSAlasdair G Kergon 222339d42fa9SIgnat Korchagin if ((bio_data_dir(io->base_bio) == READ && test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags)) || 222439d42fa9SIgnat Korchagin (bio_data_dir(io->base_bio) == WRITE && test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags))) { 2225c87a95dcSIgnat Korchagin /* 2226d3703ef3SChangbin Du * in_hardirq(): Crypto API's skcipher_walk_first() refuses to work in hard IRQ context. 2227c87a95dcSIgnat Korchagin * irqs_disabled(): the kernel may run some IO completion from the idle thread, but 2228c87a95dcSIgnat Korchagin * it is being executed with irqs disabled. 2229c87a95dcSIgnat Korchagin */ 2230d3703ef3SChangbin Du if (in_hardirq() || irqs_disabled()) { 223139d42fa9SIgnat Korchagin tasklet_init(&io->tasklet, kcryptd_crypt_tasklet, (unsigned long)&io->work); 223239d42fa9SIgnat Korchagin tasklet_schedule(&io->tasklet); 223339d42fa9SIgnat Korchagin return; 223439d42fa9SIgnat Korchagin } 223539d42fa9SIgnat Korchagin 223639d42fa9SIgnat Korchagin kcryptd_crypt(&io->work); 223739d42fa9SIgnat Korchagin return; 223839d42fa9SIgnat Korchagin } 223939d42fa9SIgnat Korchagin 2240395b167cSAlasdair G Kergon INIT_WORK(&io->work, kcryptd_crypt); 2241395b167cSAlasdair G Kergon queue_work(cc->crypt_queue, &io->work); 2242395b167cSAlasdair G Kergon } 2243395b167cSAlasdair G Kergon 2244ef43aa38SMilan Broz static void crypt_free_tfms_aead(struct crypt_config *cc) 22451da177e4SLinus Torvalds { 2246ef43aa38SMilan Broz if (!cc->cipher_tfm.tfms_aead) 2247ef43aa38SMilan Broz return; 22481da177e4SLinus Torvalds 2249ef43aa38SMilan Broz if (cc->cipher_tfm.tfms_aead[0] && !IS_ERR(cc->cipher_tfm.tfms_aead[0])) { 2250ef43aa38SMilan Broz crypto_free_aead(cc->cipher_tfm.tfms_aead[0]); 2251ef43aa38SMilan Broz cc->cipher_tfm.tfms_aead[0] = NULL; 22521da177e4SLinus Torvalds } 22531da177e4SLinus Torvalds 2254ef43aa38SMilan Broz kfree(cc->cipher_tfm.tfms_aead); 2255ef43aa38SMilan Broz cc->cipher_tfm.tfms_aead = NULL; 2256ef43aa38SMilan Broz } 22571da177e4SLinus Torvalds 2258ef43aa38SMilan Broz static void crypt_free_tfms_skcipher(struct crypt_config *cc) 2259d1f96423SMilan Broz { 2260d1f96423SMilan Broz unsigned i; 2261d1f96423SMilan Broz 2262ef43aa38SMilan Broz if (!cc->cipher_tfm.tfms) 2263fd2d231fSMikulas Patocka return; 2264fd2d231fSMikulas Patocka 2265d1f96423SMilan Broz for (i = 0; i < cc->tfms_count; i++) 2266ef43aa38SMilan Broz if (cc->cipher_tfm.tfms[i] && !IS_ERR(cc->cipher_tfm.tfms[i])) { 2267ef43aa38SMilan Broz crypto_free_skcipher(cc->cipher_tfm.tfms[i]); 2268ef43aa38SMilan Broz cc->cipher_tfm.tfms[i] = NULL; 2269d1f96423SMilan Broz } 2270d1f96423SMilan Broz 2271ef43aa38SMilan Broz kfree(cc->cipher_tfm.tfms); 2272ef43aa38SMilan Broz cc->cipher_tfm.tfms = NULL; 22731da177e4SLinus Torvalds } 22741da177e4SLinus Torvalds 22751da177e4SLinus Torvalds static void crypt_free_tfms(struct crypt_config *cc) 2276d1f96423SMilan Broz { 227733d2f09fSMilan Broz if (crypt_integrity_aead(cc)) 2278ef43aa38SMilan Broz crypt_free_tfms_aead(cc); 2279ef43aa38SMilan Broz else 2280ef43aa38SMilan Broz crypt_free_tfms_skcipher(cc); 2281d1f96423SMilan Broz } 2282d1f96423SMilan Broz 2283ef43aa38SMilan Broz static int crypt_alloc_tfms_skcipher(struct crypt_config *cc, char *ciphermode) 2284d1f96423SMilan Broz { 2285d1f96423SMilan Broz unsigned i; 2286d1f96423SMilan Broz int err; 2287d1f96423SMilan Broz 22886396bb22SKees Cook cc->cipher_tfm.tfms = kcalloc(cc->tfms_count, 22896396bb22SKees Cook sizeof(struct crypto_skcipher *), 22906396bb22SKees Cook GFP_KERNEL); 2291ef43aa38SMilan Broz if (!cc->cipher_tfm.tfms) 2292fd2d231fSMikulas Patocka return -ENOMEM; 2293fd2d231fSMikulas Patocka 2294d1f96423SMilan Broz for (i = 0; i < cc->tfms_count; i++) { 2295cd746938SMikulas Patocka cc->cipher_tfm.tfms[i] = crypto_alloc_skcipher(ciphermode, 0, 2296cd746938SMikulas Patocka CRYPTO_ALG_ALLOCATES_MEMORY); 2297ef43aa38SMilan Broz if (IS_ERR(cc->cipher_tfm.tfms[i])) { 2298ef43aa38SMilan Broz err = PTR_ERR(cc->cipher_tfm.tfms[i]); 2299fd2d231fSMikulas Patocka crypt_free_tfms(cc); 2300d1f96423SMilan Broz return err; 2301d1f96423SMilan Broz } 2302d1f96423SMilan Broz } 2303d1f96423SMilan Broz 2304af331ebaSEric Biggers /* 2305af331ebaSEric Biggers * dm-crypt performance can vary greatly depending on which crypto 2306af331ebaSEric Biggers * algorithm implementation is used. Help people debug performance 2307af331ebaSEric Biggers * problems by logging the ->cra_driver_name. 2308af331ebaSEric Biggers */ 23097a1cd723SMilan Broz DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode, 2310af331ebaSEric Biggers crypto_skcipher_alg(any_tfm(cc))->base.cra_driver_name); 2311d1f96423SMilan Broz return 0; 2312d1f96423SMilan Broz } 2313d1f96423SMilan Broz 2314ef43aa38SMilan Broz static int crypt_alloc_tfms_aead(struct crypt_config *cc, char *ciphermode) 2315ef43aa38SMilan Broz { 2316ef43aa38SMilan Broz int err; 2317ef43aa38SMilan Broz 2318ef43aa38SMilan Broz cc->cipher_tfm.tfms = kmalloc(sizeof(struct crypto_aead *), GFP_KERNEL); 2319ef43aa38SMilan Broz if (!cc->cipher_tfm.tfms) 2320ef43aa38SMilan Broz return -ENOMEM; 2321ef43aa38SMilan Broz 2322cd746938SMikulas Patocka cc->cipher_tfm.tfms_aead[0] = crypto_alloc_aead(ciphermode, 0, 2323cd746938SMikulas Patocka CRYPTO_ALG_ALLOCATES_MEMORY); 2324ef43aa38SMilan Broz if (IS_ERR(cc->cipher_tfm.tfms_aead[0])) { 2325ef43aa38SMilan Broz err = PTR_ERR(cc->cipher_tfm.tfms_aead[0]); 2326ef43aa38SMilan Broz crypt_free_tfms(cc); 2327ef43aa38SMilan Broz return err; 2328ef43aa38SMilan Broz } 2329ef43aa38SMilan Broz 23307a1cd723SMilan Broz DMDEBUG_LIMIT("%s using implementation \"%s\"", ciphermode, 2331af331ebaSEric Biggers crypto_aead_alg(any_tfm_aead(cc))->base.cra_driver_name); 2332ef43aa38SMilan Broz return 0; 2333ef43aa38SMilan Broz } 2334ef43aa38SMilan Broz 2335ef43aa38SMilan Broz static int crypt_alloc_tfms(struct crypt_config *cc, char *ciphermode) 2336ef43aa38SMilan Broz { 233733d2f09fSMilan Broz if (crypt_integrity_aead(cc)) 2338ef43aa38SMilan Broz return crypt_alloc_tfms_aead(cc, ciphermode); 2339ef43aa38SMilan Broz else 2340ef43aa38SMilan Broz return crypt_alloc_tfms_skcipher(cc, ciphermode); 2341ef43aa38SMilan Broz } 2342ef43aa38SMilan Broz 2343ef43aa38SMilan Broz static unsigned crypt_subkey_size(struct crypt_config *cc) 2344ef43aa38SMilan Broz { 2345ef43aa38SMilan Broz return (cc->key_size - cc->key_extra_size) >> ilog2(cc->tfms_count); 2346ef43aa38SMilan Broz } 2347ef43aa38SMilan Broz 2348ef43aa38SMilan Broz static unsigned crypt_authenckey_size(struct crypt_config *cc) 2349ef43aa38SMilan Broz { 2350ef43aa38SMilan Broz return crypt_subkey_size(cc) + RTA_SPACE(sizeof(struct crypto_authenc_key_param)); 2351ef43aa38SMilan Broz } 2352ef43aa38SMilan Broz 2353ef43aa38SMilan Broz /* 2354ef43aa38SMilan Broz * If AEAD is composed like authenc(hmac(sha256),xts(aes)), 2355ef43aa38SMilan Broz * the key must be for some reason in special format. 2356ef43aa38SMilan Broz * This funcion converts cc->key to this special format. 2357ef43aa38SMilan Broz */ 2358ef43aa38SMilan Broz static void crypt_copy_authenckey(char *p, const void *key, 2359ef43aa38SMilan Broz unsigned enckeylen, unsigned authkeylen) 2360ef43aa38SMilan Broz { 2361ef43aa38SMilan Broz struct crypto_authenc_key_param *param; 2362ef43aa38SMilan Broz struct rtattr *rta; 2363ef43aa38SMilan Broz 2364ef43aa38SMilan Broz rta = (struct rtattr *)p; 2365ef43aa38SMilan Broz param = RTA_DATA(rta); 2366ef43aa38SMilan Broz param->enckeylen = cpu_to_be32(enckeylen); 2367ef43aa38SMilan Broz rta->rta_len = RTA_LENGTH(sizeof(*param)); 2368ef43aa38SMilan Broz rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM; 2369ef43aa38SMilan Broz p += RTA_SPACE(sizeof(*param)); 2370ef43aa38SMilan Broz memcpy(p, key + enckeylen, authkeylen); 2371ef43aa38SMilan Broz p += authkeylen; 2372ef43aa38SMilan Broz memcpy(p, key, enckeylen); 2373ef43aa38SMilan Broz } 2374ef43aa38SMilan Broz 2375671ea6b4SMikulas Patocka static int crypt_setkey(struct crypt_config *cc) 2376c0297721SAndi Kleen { 2377da31a078SMilan Broz unsigned subkey_size; 2378fd2d231fSMikulas Patocka int err = 0, i, r; 2379c0297721SAndi Kleen 2380da31a078SMilan Broz /* Ignore extra keys (which are used for IV etc) */ 2381ef43aa38SMilan Broz subkey_size = crypt_subkey_size(cc); 2382da31a078SMilan Broz 238327c70036SMilan Broz if (crypt_integrity_hmac(cc)) { 238427c70036SMilan Broz if (subkey_size < cc->key_mac_size) 238527c70036SMilan Broz return -EINVAL; 238627c70036SMilan Broz 2387ef43aa38SMilan Broz crypt_copy_authenckey(cc->authenc_key, cc->key, 2388ef43aa38SMilan Broz subkey_size - cc->key_mac_size, 2389ef43aa38SMilan Broz cc->key_mac_size); 239027c70036SMilan Broz } 239127c70036SMilan Broz 2392d1f96423SMilan Broz for (i = 0; i < cc->tfms_count; i++) { 239333d2f09fSMilan Broz if (crypt_integrity_hmac(cc)) 239433d2f09fSMilan Broz r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], 239533d2f09fSMilan Broz cc->authenc_key, crypt_authenckey_size(cc)); 239633d2f09fSMilan Broz else if (crypt_integrity_aead(cc)) 2397ef43aa38SMilan Broz r = crypto_aead_setkey(cc->cipher_tfm.tfms_aead[i], 2398ef43aa38SMilan Broz cc->key + (i * subkey_size), 2399ef43aa38SMilan Broz subkey_size); 2400ef43aa38SMilan Broz else 2401ef43aa38SMilan Broz r = crypto_skcipher_setkey(cc->cipher_tfm.tfms[i], 2402fd2d231fSMikulas Patocka cc->key + (i * subkey_size), 2403fd2d231fSMikulas Patocka subkey_size); 2404c0297721SAndi Kleen if (r) 2405c0297721SAndi Kleen err = r; 2406c0297721SAndi Kleen } 2407c0297721SAndi Kleen 2408ef43aa38SMilan Broz if (crypt_integrity_hmac(cc)) 2409ef43aa38SMilan Broz memzero_explicit(cc->authenc_key, crypt_authenckey_size(cc)); 2410ef43aa38SMilan Broz 2411c0297721SAndi Kleen return err; 2412c0297721SAndi Kleen } 2413c0297721SAndi Kleen 2414c538f6ecSOndrej Kozina #ifdef CONFIG_KEYS 2415c538f6ecSOndrej Kozina 2416027c431cSOndrej Kozina static bool contains_whitespace(const char *str) 2417027c431cSOndrej Kozina { 2418027c431cSOndrej Kozina while (*str) 2419027c431cSOndrej Kozina if (isspace(*str++)) 2420027c431cSOndrej Kozina return true; 2421027c431cSOndrej Kozina return false; 2422027c431cSOndrej Kozina } 2423027c431cSOndrej Kozina 242427f5411aSDmitry Baryshkov static int set_key_user(struct crypt_config *cc, struct key *key) 242527f5411aSDmitry Baryshkov { 242627f5411aSDmitry Baryshkov const struct user_key_payload *ukp; 242727f5411aSDmitry Baryshkov 242827f5411aSDmitry Baryshkov ukp = user_key_payload_locked(key); 242927f5411aSDmitry Baryshkov if (!ukp) 243027f5411aSDmitry Baryshkov return -EKEYREVOKED; 243127f5411aSDmitry Baryshkov 243227f5411aSDmitry Baryshkov if (cc->key_size != ukp->datalen) 243327f5411aSDmitry Baryshkov return -EINVAL; 243427f5411aSDmitry Baryshkov 243527f5411aSDmitry Baryshkov memcpy(cc->key, ukp->data, cc->key_size); 243627f5411aSDmitry Baryshkov 243727f5411aSDmitry Baryshkov return 0; 243827f5411aSDmitry Baryshkov } 243927f5411aSDmitry Baryshkov 244027f5411aSDmitry Baryshkov static int set_key_encrypted(struct crypt_config *cc, struct key *key) 244127f5411aSDmitry Baryshkov { 244227f5411aSDmitry Baryshkov const struct encrypted_key_payload *ekp; 244327f5411aSDmitry Baryshkov 244427f5411aSDmitry Baryshkov ekp = key->payload.data[0]; 244527f5411aSDmitry Baryshkov if (!ekp) 244627f5411aSDmitry Baryshkov return -EKEYREVOKED; 244727f5411aSDmitry Baryshkov 244827f5411aSDmitry Baryshkov if (cc->key_size != ekp->decrypted_datalen) 244927f5411aSDmitry Baryshkov return -EINVAL; 245027f5411aSDmitry Baryshkov 245127f5411aSDmitry Baryshkov memcpy(cc->key, ekp->decrypted_data, cc->key_size); 245227f5411aSDmitry Baryshkov 245327f5411aSDmitry Baryshkov return 0; 245427f5411aSDmitry Baryshkov } 245527f5411aSDmitry Baryshkov 2456363880c4SAhmad Fatoum static int set_key_trusted(struct crypt_config *cc, struct key *key) 2457363880c4SAhmad Fatoum { 2458363880c4SAhmad Fatoum const struct trusted_key_payload *tkp; 2459363880c4SAhmad Fatoum 2460363880c4SAhmad Fatoum tkp = key->payload.data[0]; 2461363880c4SAhmad Fatoum if (!tkp) 2462363880c4SAhmad Fatoum return -EKEYREVOKED; 2463363880c4SAhmad Fatoum 2464363880c4SAhmad Fatoum if (cc->key_size != tkp->key_len) 2465363880c4SAhmad Fatoum return -EINVAL; 2466363880c4SAhmad Fatoum 2467363880c4SAhmad Fatoum memcpy(cc->key, tkp->key, cc->key_size); 2468363880c4SAhmad Fatoum 2469363880c4SAhmad Fatoum return 0; 2470363880c4SAhmad Fatoum } 2471363880c4SAhmad Fatoum 2472c538f6ecSOndrej Kozina static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) 2473c538f6ecSOndrej Kozina { 2474c538f6ecSOndrej Kozina char *new_key_string, *key_desc; 2475c538f6ecSOndrej Kozina int ret; 247627f5411aSDmitry Baryshkov struct key_type *type; 2477c538f6ecSOndrej Kozina struct key *key; 247827f5411aSDmitry Baryshkov int (*set_key)(struct crypt_config *cc, struct key *key); 2479c538f6ecSOndrej Kozina 2480027c431cSOndrej Kozina /* 2481027c431cSOndrej Kozina * Reject key_string with whitespace. dm core currently lacks code for 2482027c431cSOndrej Kozina * proper whitespace escaping in arguments on DM_TABLE_STATUS path. 2483027c431cSOndrej Kozina */ 2484027c431cSOndrej Kozina if (contains_whitespace(key_string)) { 2485027c431cSOndrej Kozina DMERR("whitespace chars not allowed in key string"); 2486027c431cSOndrej Kozina return -EINVAL; 2487027c431cSOndrej Kozina } 2488027c431cSOndrej Kozina 2489c538f6ecSOndrej Kozina /* look for next ':' separating key_type from key_description */ 2490c538f6ecSOndrej Kozina key_desc = strpbrk(key_string, ":"); 2491c538f6ecSOndrej Kozina if (!key_desc || key_desc == key_string || !strlen(key_desc + 1)) 2492c538f6ecSOndrej Kozina return -EINVAL; 2493c538f6ecSOndrej Kozina 249427f5411aSDmitry Baryshkov if (!strncmp(key_string, "logon:", key_desc - key_string + 1)) { 249527f5411aSDmitry Baryshkov type = &key_type_logon; 249627f5411aSDmitry Baryshkov set_key = set_key_user; 249727f5411aSDmitry Baryshkov } else if (!strncmp(key_string, "user:", key_desc - key_string + 1)) { 249827f5411aSDmitry Baryshkov type = &key_type_user; 249927f5411aSDmitry Baryshkov set_key = set_key_user; 2500831475ccSAhmad Fatoum } else if (IS_ENABLED(CONFIG_ENCRYPTED_KEYS) && 2501831475ccSAhmad Fatoum !strncmp(key_string, "encrypted:", key_desc - key_string + 1)) { 250227f5411aSDmitry Baryshkov type = &key_type_encrypted; 250327f5411aSDmitry Baryshkov set_key = set_key_encrypted; 2504363880c4SAhmad Fatoum } else if (IS_ENABLED(CONFIG_TRUSTED_KEYS) && 2505363880c4SAhmad Fatoum !strncmp(key_string, "trusted:", key_desc - key_string + 1)) { 2506363880c4SAhmad Fatoum type = &key_type_trusted; 2507363880c4SAhmad Fatoum set_key = set_key_trusted; 250827f5411aSDmitry Baryshkov } else { 2509c538f6ecSOndrej Kozina return -EINVAL; 251027f5411aSDmitry Baryshkov } 2511c538f6ecSOndrej Kozina 2512c538f6ecSOndrej Kozina new_key_string = kstrdup(key_string, GFP_KERNEL); 2513c538f6ecSOndrej Kozina if (!new_key_string) 2514c538f6ecSOndrej Kozina return -ENOMEM; 2515c538f6ecSOndrej Kozina 251627f5411aSDmitry Baryshkov key = request_key(type, key_desc + 1, NULL); 2517c538f6ecSOndrej Kozina if (IS_ERR(key)) { 2518453431a5SWaiman Long kfree_sensitive(new_key_string); 2519c538f6ecSOndrej Kozina return PTR_ERR(key); 2520c538f6ecSOndrej Kozina } 2521c538f6ecSOndrej Kozina 2522f5b0cba8SOndrej Kozina down_read(&key->sem); 2523c538f6ecSOndrej Kozina 252427f5411aSDmitry Baryshkov ret = set_key(cc, key); 252527f5411aSDmitry Baryshkov if (ret < 0) { 2526f5b0cba8SOndrej Kozina up_read(&key->sem); 2527c538f6ecSOndrej Kozina key_put(key); 2528453431a5SWaiman Long kfree_sensitive(new_key_string); 252927f5411aSDmitry Baryshkov return ret; 2530c538f6ecSOndrej Kozina } 2531c538f6ecSOndrej Kozina 2532f5b0cba8SOndrej Kozina up_read(&key->sem); 2533c538f6ecSOndrej Kozina key_put(key); 2534c538f6ecSOndrej Kozina 2535c538f6ecSOndrej Kozina /* clear the flag since following operations may invalidate previously valid key */ 2536c538f6ecSOndrej Kozina clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); 2537c538f6ecSOndrej Kozina 2538c538f6ecSOndrej Kozina ret = crypt_setkey(cc); 2539c538f6ecSOndrej Kozina 2540c538f6ecSOndrej Kozina if (!ret) { 2541c538f6ecSOndrej Kozina set_bit(DM_CRYPT_KEY_VALID, &cc->flags); 2542453431a5SWaiman Long kfree_sensitive(cc->key_string); 2543c538f6ecSOndrej Kozina cc->key_string = new_key_string; 2544c538f6ecSOndrej Kozina } else 2545453431a5SWaiman Long kfree_sensitive(new_key_string); 2546c538f6ecSOndrej Kozina 2547c538f6ecSOndrej Kozina return ret; 2548c538f6ecSOndrej Kozina } 2549c538f6ecSOndrej Kozina 2550c538f6ecSOndrej Kozina static int get_key_size(char **key_string) 2551c538f6ecSOndrej Kozina { 2552c538f6ecSOndrej Kozina char *colon, dummy; 2553c538f6ecSOndrej Kozina int ret; 2554c538f6ecSOndrej Kozina 2555c538f6ecSOndrej Kozina if (*key_string[0] != ':') 2556c538f6ecSOndrej Kozina return strlen(*key_string) >> 1; 2557c538f6ecSOndrej Kozina 2558c538f6ecSOndrej Kozina /* look for next ':' in key string */ 2559c538f6ecSOndrej Kozina colon = strpbrk(*key_string + 1, ":"); 2560c538f6ecSOndrej Kozina if (!colon) 2561c538f6ecSOndrej Kozina return -EINVAL; 2562c538f6ecSOndrej Kozina 2563c538f6ecSOndrej Kozina if (sscanf(*key_string + 1, "%u%c", &ret, &dummy) != 2 || dummy != ':') 2564c538f6ecSOndrej Kozina return -EINVAL; 2565c538f6ecSOndrej Kozina 2566c538f6ecSOndrej Kozina *key_string = colon; 2567c538f6ecSOndrej Kozina 2568c538f6ecSOndrej Kozina /* remaining key string should be :<logon|user>:<key_desc> */ 2569c538f6ecSOndrej Kozina 2570c538f6ecSOndrej Kozina return ret; 2571c538f6ecSOndrej Kozina } 2572c538f6ecSOndrej Kozina 2573c538f6ecSOndrej Kozina #else 2574c538f6ecSOndrej Kozina 2575c538f6ecSOndrej Kozina static int crypt_set_keyring_key(struct crypt_config *cc, const char *key_string) 2576c538f6ecSOndrej Kozina { 2577c538f6ecSOndrej Kozina return -EINVAL; 2578c538f6ecSOndrej Kozina } 2579c538f6ecSOndrej Kozina 2580c538f6ecSOndrej Kozina static int get_key_size(char **key_string) 2581c538f6ecSOndrej Kozina { 25826fc51504SAashish Sharma return (*key_string[0] == ':') ? -EINVAL : (int)(strlen(*key_string) >> 1); 2583c538f6ecSOndrej Kozina } 2584c538f6ecSOndrej Kozina 258527f5411aSDmitry Baryshkov #endif /* CONFIG_KEYS */ 2586c538f6ecSOndrej Kozina 2587e48d4bbfSMilan Broz static int crypt_set_key(struct crypt_config *cc, char *key) 2588e48d4bbfSMilan Broz { 2589de8be5acSMilan Broz int r = -EINVAL; 2590de8be5acSMilan Broz int key_string_len = strlen(key); 2591de8be5acSMilan Broz 259269a8cfcdSMilan Broz /* Hyphen (which gives a key_size of zero) means there is no key. */ 259369a8cfcdSMilan Broz if (!cc->key_size && strcmp(key, "-")) 2594de8be5acSMilan Broz goto out; 2595e48d4bbfSMilan Broz 2596c538f6ecSOndrej Kozina /* ':' means the key is in kernel keyring, short-circuit normal key processing */ 2597c538f6ecSOndrej Kozina if (key[0] == ':') { 2598c538f6ecSOndrej Kozina r = crypt_set_keyring_key(cc, key + 1); 2599c538f6ecSOndrej Kozina goto out; 2600c538f6ecSOndrej Kozina } 2601c538f6ecSOndrej Kozina 2602265e9098SOndrej Kozina /* clear the flag since following operations may invalidate previously valid key */ 2603265e9098SOndrej Kozina clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); 2604265e9098SOndrej Kozina 2605c538f6ecSOndrej Kozina /* wipe references to any kernel keyring key */ 2606453431a5SWaiman Long kfree_sensitive(cc->key_string); 2607c538f6ecSOndrej Kozina cc->key_string = NULL; 2608c538f6ecSOndrej Kozina 2609e944e03eSAndy Shevchenko /* Decode key from its hex representation. */ 2610e944e03eSAndy Shevchenko if (cc->key_size && hex2bin(cc->key, key, cc->key_size) < 0) 2611de8be5acSMilan Broz goto out; 2612e48d4bbfSMilan Broz 2613671ea6b4SMikulas Patocka r = crypt_setkey(cc); 2614265e9098SOndrej Kozina if (!r) 2615e48d4bbfSMilan Broz set_bit(DM_CRYPT_KEY_VALID, &cc->flags); 2616e48d4bbfSMilan Broz 2617de8be5acSMilan Broz out: 2618de8be5acSMilan Broz /* Hex key string not needed after here, so wipe it. */ 2619de8be5acSMilan Broz memset(key, '0', key_string_len); 2620de8be5acSMilan Broz 2621de8be5acSMilan Broz return r; 2622e48d4bbfSMilan Broz } 2623e48d4bbfSMilan Broz 2624e48d4bbfSMilan Broz static int crypt_wipe_key(struct crypt_config *cc) 2625e48d4bbfSMilan Broz { 2626c82feeecSOndrej Kozina int r; 2627c82feeecSOndrej Kozina 2628e48d4bbfSMilan Broz clear_bit(DM_CRYPT_KEY_VALID, &cc->flags); 2629c82feeecSOndrej Kozina get_random_bytes(&cc->key, cc->key_size); 26304a52ffc7SMilan Broz 26314a52ffc7SMilan Broz /* Wipe IV private keys */ 26324a52ffc7SMilan Broz if (cc->iv_gen_ops && cc->iv_gen_ops->wipe) { 26334a52ffc7SMilan Broz r = cc->iv_gen_ops->wipe(cc); 26344a52ffc7SMilan Broz if (r) 26354a52ffc7SMilan Broz return r; 26364a52ffc7SMilan Broz } 26374a52ffc7SMilan Broz 2638453431a5SWaiman Long kfree_sensitive(cc->key_string); 2639c538f6ecSOndrej Kozina cc->key_string = NULL; 2640c82feeecSOndrej Kozina r = crypt_setkey(cc); 2641c82feeecSOndrej Kozina memset(&cc->key, 0, cc->key_size * sizeof(u8)); 2642c0297721SAndi Kleen 2643c82feeecSOndrej Kozina return r; 2644e48d4bbfSMilan Broz } 2645e48d4bbfSMilan Broz 26465059353dSMikulas Patocka static void crypt_calculate_pages_per_client(void) 26475059353dSMikulas Patocka { 2648ca79b0c2SArun KS unsigned long pages = (totalram_pages() - totalhigh_pages()) * DM_CRYPT_MEMORY_PERCENT / 100; 26495059353dSMikulas Patocka 26505059353dSMikulas Patocka if (!dm_crypt_clients_n) 26515059353dSMikulas Patocka return; 26525059353dSMikulas Patocka 26535059353dSMikulas Patocka pages /= dm_crypt_clients_n; 26545059353dSMikulas Patocka if (pages < DM_CRYPT_MIN_PAGES_PER_CLIENT) 26555059353dSMikulas Patocka pages = DM_CRYPT_MIN_PAGES_PER_CLIENT; 26565059353dSMikulas Patocka dm_crypt_pages_per_client = pages; 26575059353dSMikulas Patocka } 26585059353dSMikulas Patocka 26595059353dSMikulas Patocka static void *crypt_page_alloc(gfp_t gfp_mask, void *pool_data) 26605059353dSMikulas Patocka { 26615059353dSMikulas Patocka struct crypt_config *cc = pool_data; 26625059353dSMikulas Patocka struct page *page; 26635059353dSMikulas Patocka 2664528b16bfSArne Welzel /* 2665528b16bfSArne Welzel * Note, percpu_counter_read_positive() may over (and under) estimate 2666528b16bfSArne Welzel * the current usage by at most (batch - 1) * num_online_cpus() pages, 2667528b16bfSArne Welzel * but avoids potential spinlock contention of an exact result. 2668528b16bfSArne Welzel */ 2669528b16bfSArne Welzel if (unlikely(percpu_counter_read_positive(&cc->n_allocated_pages) >= dm_crypt_pages_per_client) && 26705059353dSMikulas Patocka likely(gfp_mask & __GFP_NORETRY)) 26715059353dSMikulas Patocka return NULL; 26725059353dSMikulas Patocka 26735059353dSMikulas Patocka page = alloc_page(gfp_mask); 26745059353dSMikulas Patocka if (likely(page != NULL)) 26755059353dSMikulas Patocka percpu_counter_add(&cc->n_allocated_pages, 1); 26765059353dSMikulas Patocka 26775059353dSMikulas Patocka return page; 26785059353dSMikulas Patocka } 26795059353dSMikulas Patocka 26805059353dSMikulas Patocka static void crypt_page_free(void *page, void *pool_data) 26815059353dSMikulas Patocka { 26825059353dSMikulas Patocka struct crypt_config *cc = pool_data; 26835059353dSMikulas Patocka 26845059353dSMikulas Patocka __free_page(page); 26855059353dSMikulas Patocka percpu_counter_sub(&cc->n_allocated_pages, 1); 26865059353dSMikulas Patocka } 26875059353dSMikulas Patocka 268828513fccSMilan Broz static void crypt_dtr(struct dm_target *ti) 268928513fccSMilan Broz { 269028513fccSMilan Broz struct crypt_config *cc = ti->private; 269128513fccSMilan Broz 269228513fccSMilan Broz ti->private = NULL; 269328513fccSMilan Broz 269428513fccSMilan Broz if (!cc) 269528513fccSMilan Broz return; 269628513fccSMilan Broz 2697f659b100SRabin Vincent if (cc->write_thread) 2698dc267621SMikulas Patocka kthread_stop(cc->write_thread); 2699dc267621SMikulas Patocka 270028513fccSMilan Broz if (cc->io_queue) 270128513fccSMilan Broz destroy_workqueue(cc->io_queue); 270228513fccSMilan Broz if (cc->crypt_queue) 270328513fccSMilan Broz destroy_workqueue(cc->crypt_queue); 270428513fccSMilan Broz 2705fd2d231fSMikulas Patocka crypt_free_tfms(cc); 2706fd2d231fSMikulas Patocka 27076f1c819cSKent Overstreet bioset_exit(&cc->bs); 270828513fccSMilan Broz 27096f1c819cSKent Overstreet mempool_exit(&cc->page_pool); 27106f1c819cSKent Overstreet mempool_exit(&cc->req_pool); 27116f1c819cSKent Overstreet mempool_exit(&cc->tag_pool); 27126f1c819cSKent Overstreet 2713d00a11dfSKent Overstreet WARN_ON(percpu_counter_sum(&cc->n_allocated_pages) != 0); 2714d00a11dfSKent Overstreet percpu_counter_destroy(&cc->n_allocated_pages); 2715d00a11dfSKent Overstreet 271628513fccSMilan Broz if (cc->iv_gen_ops && cc->iv_gen_ops->dtr) 271728513fccSMilan Broz cc->iv_gen_ops->dtr(cc); 271828513fccSMilan Broz 271928513fccSMilan Broz if (cc->dev) 272028513fccSMilan Broz dm_put_device(ti, cc->dev); 272128513fccSMilan Broz 2722453431a5SWaiman Long kfree_sensitive(cc->cipher_string); 2723453431a5SWaiman Long kfree_sensitive(cc->key_string); 2724453431a5SWaiman Long kfree_sensitive(cc->cipher_auth); 2725453431a5SWaiman Long kfree_sensitive(cc->authenc_key); 272628513fccSMilan Broz 2727d5ffebddSMike Snitzer mutex_destroy(&cc->bio_alloc_lock); 2728d5ffebddSMike Snitzer 272928513fccSMilan Broz /* Must zero key material before freeing */ 2730453431a5SWaiman Long kfree_sensitive(cc); 27315059353dSMikulas Patocka 27325059353dSMikulas Patocka spin_lock(&dm_crypt_clients_lock); 27335059353dSMikulas Patocka WARN_ON(!dm_crypt_clients_n); 27345059353dSMikulas Patocka dm_crypt_clients_n--; 27355059353dSMikulas Patocka crypt_calculate_pages_per_client(); 27365059353dSMikulas Patocka spin_unlock(&dm_crypt_clients_lock); 273758d0f180SMichael Weiß 273858d0f180SMichael Weiß dm_audit_log_dtr(DM_MSG_PREFIX, ti, 1); 273928513fccSMilan Broz } 274028513fccSMilan Broz 2741e889f97aSMilan Broz static int crypt_ctr_ivmode(struct dm_target *ti, const char *ivmode) 27421da177e4SLinus Torvalds { 27435ebaee6dSMilan Broz struct crypt_config *cc = ti->private; 27441da177e4SLinus Torvalds 274533d2f09fSMilan Broz if (crypt_integrity_aead(cc)) 2746e889f97aSMilan Broz cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); 2747e889f97aSMilan Broz else 2748bbdb23b5SHerbert Xu cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); 2749e889f97aSMilan Broz 27505ebaee6dSMilan Broz if (cc->iv_size) 27515ebaee6dSMilan Broz /* at least a 64 bit sector number should fit in our buffer */ 27525ebaee6dSMilan Broz cc->iv_size = max(cc->iv_size, 27535ebaee6dSMilan Broz (unsigned int)(sizeof(u64) / sizeof(u8))); 27545ebaee6dSMilan Broz else if (ivmode) { 27555ebaee6dSMilan Broz DMWARN("Selected cipher does not support IVs"); 27565ebaee6dSMilan Broz ivmode = NULL; 27575ebaee6dSMilan Broz } 27585ebaee6dSMilan Broz 27595ebaee6dSMilan Broz /* Choose ivmode, see comments at iv code. */ 27601da177e4SLinus Torvalds if (ivmode == NULL) 27611da177e4SLinus Torvalds cc->iv_gen_ops = NULL; 27621da177e4SLinus Torvalds else if (strcmp(ivmode, "plain") == 0) 27631da177e4SLinus Torvalds cc->iv_gen_ops = &crypt_iv_plain_ops; 276461afef61SMilan Broz else if (strcmp(ivmode, "plain64") == 0) 276561afef61SMilan Broz cc->iv_gen_ops = &crypt_iv_plain64_ops; 27667e3fd855SMilan Broz else if (strcmp(ivmode, "plain64be") == 0) 27677e3fd855SMilan Broz cc->iv_gen_ops = &crypt_iv_plain64be_ops; 27681da177e4SLinus Torvalds else if (strcmp(ivmode, "essiv") == 0) 27691da177e4SLinus Torvalds cc->iv_gen_ops = &crypt_iv_essiv_ops; 277048527fa7SRik Snel else if (strcmp(ivmode, "benbi") == 0) 277148527fa7SRik Snel cc->iv_gen_ops = &crypt_iv_benbi_ops; 277246b47730SLudwig Nussel else if (strcmp(ivmode, "null") == 0) 277346b47730SLudwig Nussel cc->iv_gen_ops = &crypt_iv_null_ops; 2774b9411d73SMilan Broz else if (strcmp(ivmode, "eboiv") == 0) 2775b9411d73SMilan Broz cc->iv_gen_ops = &crypt_iv_eboiv_ops; 2776bbb16584SMilan Broz else if (strcmp(ivmode, "elephant") == 0) { 2777bbb16584SMilan Broz cc->iv_gen_ops = &crypt_iv_elephant_ops; 2778bbb16584SMilan Broz cc->key_parts = 2; 2779bbb16584SMilan Broz cc->key_extra_size = cc->key_size / 2; 2780bbb16584SMilan Broz if (cc->key_extra_size > ELEPHANT_MAX_KEY_SIZE) 2781bbb16584SMilan Broz return -EINVAL; 2782bbb16584SMilan Broz set_bit(CRYPT_ENCRYPT_PREPROCESS, &cc->cipher_flags); 2783bbb16584SMilan Broz } else if (strcmp(ivmode, "lmk") == 0) { 278434745785SMilan Broz cc->iv_gen_ops = &crypt_iv_lmk_ops; 2785ed04d981SMilan Broz /* 2786ed04d981SMilan Broz * Version 2 and 3 is recognised according 278734745785SMilan Broz * to length of provided multi-key string. 278834745785SMilan Broz * If present (version 3), last key is used as IV seed. 2789ed04d981SMilan Broz * All keys (including IV seed) are always the same size. 279034745785SMilan Broz */ 2791da31a078SMilan Broz if (cc->key_size % cc->key_parts) { 279234745785SMilan Broz cc->key_parts++; 2793da31a078SMilan Broz cc->key_extra_size = cc->key_size / cc->key_parts; 2794da31a078SMilan Broz } 2795ed04d981SMilan Broz } else if (strcmp(ivmode, "tcw") == 0) { 2796ed04d981SMilan Broz cc->iv_gen_ops = &crypt_iv_tcw_ops; 2797ed04d981SMilan Broz cc->key_parts += 2; /* IV + whitening */ 2798ed04d981SMilan Broz cc->key_extra_size = cc->iv_size + TCW_WHITENING_SIZE; 2799e889f97aSMilan Broz } else if (strcmp(ivmode, "random") == 0) { 2800e889f97aSMilan Broz cc->iv_gen_ops = &crypt_iv_random_ops; 2801e889f97aSMilan Broz /* Need storage space in integrity fields. */ 2802e889f97aSMilan Broz cc->integrity_iv_size = cc->iv_size; 280334745785SMilan Broz } else { 280472d94861SAlasdair G Kergon ti->error = "Invalid IV mode"; 2805e889f97aSMilan Broz return -EINVAL; 28061da177e4SLinus Torvalds } 28071da177e4SLinus Torvalds 2808e889f97aSMilan Broz return 0; 2809e889f97aSMilan Broz } 2810e889f97aSMilan Broz 281133d2f09fSMilan Broz /* 281233d2f09fSMilan Broz * Workaround to parse HMAC algorithm from AEAD crypto API spec. 281333d2f09fSMilan Broz * The HMAC is needed to calculate tag size (HMAC digest size). 281433d2f09fSMilan Broz * This should be probably done by crypto-api calls (once available...) 281533d2f09fSMilan Broz */ 281633d2f09fSMilan Broz static int crypt_ctr_auth_cipher(struct crypt_config *cc, char *cipher_api) 281733d2f09fSMilan Broz { 281833d2f09fSMilan Broz char *start, *end, *mac_alg = NULL; 281933d2f09fSMilan Broz struct crypto_ahash *mac; 282033d2f09fSMilan Broz 282133d2f09fSMilan Broz if (!strstarts(cipher_api, "authenc(")) 282233d2f09fSMilan Broz return 0; 282333d2f09fSMilan Broz 282433d2f09fSMilan Broz start = strchr(cipher_api, '('); 282533d2f09fSMilan Broz end = strchr(cipher_api, ','); 282633d2f09fSMilan Broz if (!start || !end || ++start > end) 282733d2f09fSMilan Broz return -EINVAL; 282833d2f09fSMilan Broz 282933d2f09fSMilan Broz mac_alg = kzalloc(end - start + 1, GFP_KERNEL); 283033d2f09fSMilan Broz if (!mac_alg) 283133d2f09fSMilan Broz return -ENOMEM; 283233d2f09fSMilan Broz strncpy(mac_alg, start, end - start); 283333d2f09fSMilan Broz 2834cd746938SMikulas Patocka mac = crypto_alloc_ahash(mac_alg, 0, CRYPTO_ALG_ALLOCATES_MEMORY); 283533d2f09fSMilan Broz kfree(mac_alg); 283633d2f09fSMilan Broz 283733d2f09fSMilan Broz if (IS_ERR(mac)) 283833d2f09fSMilan Broz return PTR_ERR(mac); 283933d2f09fSMilan Broz 284033d2f09fSMilan Broz cc->key_mac_size = crypto_ahash_digestsize(mac); 284133d2f09fSMilan Broz crypto_free_ahash(mac); 284233d2f09fSMilan Broz 284333d2f09fSMilan Broz cc->authenc_key = kmalloc(crypt_authenckey_size(cc), GFP_KERNEL); 284433d2f09fSMilan Broz if (!cc->authenc_key) 284533d2f09fSMilan Broz return -ENOMEM; 284633d2f09fSMilan Broz 284733d2f09fSMilan Broz return 0; 284833d2f09fSMilan Broz } 284933d2f09fSMilan Broz 285033d2f09fSMilan Broz static int crypt_ctr_cipher_new(struct dm_target *ti, char *cipher_in, char *key, 285133d2f09fSMilan Broz char **ivmode, char **ivopts) 28521da177e4SLinus Torvalds { 28535ebaee6dSMilan Broz struct crypt_config *cc = ti->private; 2854a1a262b6SArd Biesheuvel char *tmp, *cipher_api, buf[CRYPTO_MAX_ALG_NAME]; 285533d2f09fSMilan Broz int ret = -EINVAL; 285633d2f09fSMilan Broz 285733d2f09fSMilan Broz cc->tfms_count = 1; 285833d2f09fSMilan Broz 285933d2f09fSMilan Broz /* 286033d2f09fSMilan Broz * New format (capi: prefix) 286133d2f09fSMilan Broz * capi:cipher_api_spec-iv:ivopts 286233d2f09fSMilan Broz */ 286333d2f09fSMilan Broz tmp = &cipher_in[strlen("capi:")]; 28641856b9f7SMilan Broz 28651856b9f7SMilan Broz /* Separate IV options if present, it can contain another '-' in hash name */ 28661856b9f7SMilan Broz *ivopts = strrchr(tmp, ':'); 28671856b9f7SMilan Broz if (*ivopts) { 28681856b9f7SMilan Broz **ivopts = '\0'; 28691856b9f7SMilan Broz (*ivopts)++; 28701856b9f7SMilan Broz } 28711856b9f7SMilan Broz /* Parse IV mode */ 28721856b9f7SMilan Broz *ivmode = strrchr(tmp, '-'); 28731856b9f7SMilan Broz if (*ivmode) { 28741856b9f7SMilan Broz **ivmode = '\0'; 28751856b9f7SMilan Broz (*ivmode)++; 28761856b9f7SMilan Broz } 28771856b9f7SMilan Broz /* The rest is crypto API spec */ 28781856b9f7SMilan Broz cipher_api = tmp; 287933d2f09fSMilan Broz 2880a1a262b6SArd Biesheuvel /* Alloc AEAD, can be used only in new format. */ 2881a1a262b6SArd Biesheuvel if (crypt_integrity_aead(cc)) { 2882a1a262b6SArd Biesheuvel ret = crypt_ctr_auth_cipher(cc, cipher_api); 2883a1a262b6SArd Biesheuvel if (ret < 0) { 2884a1a262b6SArd Biesheuvel ti->error = "Invalid AEAD cipher spec"; 2885a1a262b6SArd Biesheuvel return -ENOMEM; 2886a1a262b6SArd Biesheuvel } 2887a1a262b6SArd Biesheuvel } 2888a1a262b6SArd Biesheuvel 288933d2f09fSMilan Broz if (*ivmode && !strcmp(*ivmode, "lmk")) 289033d2f09fSMilan Broz cc->tfms_count = 64; 289133d2f09fSMilan Broz 2892a1a262b6SArd Biesheuvel if (*ivmode && !strcmp(*ivmode, "essiv")) { 2893a1a262b6SArd Biesheuvel if (!*ivopts) { 2894a1a262b6SArd Biesheuvel ti->error = "Digest algorithm missing for ESSIV mode"; 2895a1a262b6SArd Biesheuvel return -EINVAL; 2896a1a262b6SArd Biesheuvel } 2897a1a262b6SArd Biesheuvel ret = snprintf(buf, CRYPTO_MAX_ALG_NAME, "essiv(%s,%s)", 2898a1a262b6SArd Biesheuvel cipher_api, *ivopts); 2899a1a262b6SArd Biesheuvel if (ret < 0 || ret >= CRYPTO_MAX_ALG_NAME) { 2900a1a262b6SArd Biesheuvel ti->error = "Cannot allocate cipher string"; 2901a1a262b6SArd Biesheuvel return -ENOMEM; 2902a1a262b6SArd Biesheuvel } 2903a1a262b6SArd Biesheuvel cipher_api = buf; 2904a1a262b6SArd Biesheuvel } 2905a1a262b6SArd Biesheuvel 290633d2f09fSMilan Broz cc->key_parts = cc->tfms_count; 290733d2f09fSMilan Broz 290833d2f09fSMilan Broz /* Allocate cipher */ 290933d2f09fSMilan Broz ret = crypt_alloc_tfms(cc, cipher_api); 291033d2f09fSMilan Broz if (ret < 0) { 291133d2f09fSMilan Broz ti->error = "Error allocating crypto tfm"; 291233d2f09fSMilan Broz return ret; 291333d2f09fSMilan Broz } 291433d2f09fSMilan Broz 2915a1a262b6SArd Biesheuvel if (crypt_integrity_aead(cc)) 291633d2f09fSMilan Broz cc->iv_size = crypto_aead_ivsize(any_tfm_aead(cc)); 2917a1a262b6SArd Biesheuvel else 291833d2f09fSMilan Broz cc->iv_size = crypto_skcipher_ivsize(any_tfm(cc)); 291933d2f09fSMilan Broz 292033d2f09fSMilan Broz return 0; 292133d2f09fSMilan Broz } 292233d2f09fSMilan Broz 292333d2f09fSMilan Broz static int crypt_ctr_cipher_old(struct dm_target *ti, char *cipher_in, char *key, 292433d2f09fSMilan Broz char **ivmode, char **ivopts) 292533d2f09fSMilan Broz { 292633d2f09fSMilan Broz struct crypt_config *cc = ti->private; 292733d2f09fSMilan Broz char *tmp, *cipher, *chainmode, *keycount; 29285ebaee6dSMilan Broz char *cipher_api = NULL; 29295ebaee6dSMilan Broz int ret = -EINVAL; 29305ebaee6dSMilan Broz char dummy; 29315ebaee6dSMilan Broz 293233d2f09fSMilan Broz if (strchr(cipher_in, '(') || crypt_integrity_aead(cc)) { 29335ebaee6dSMilan Broz ti->error = "Bad cipher specification"; 29345ebaee6dSMilan Broz return -EINVAL; 29355ebaee6dSMilan Broz } 29365ebaee6dSMilan Broz 29371da177e4SLinus Torvalds /* 29385ebaee6dSMilan Broz * Legacy dm-crypt cipher specification 29395ebaee6dSMilan Broz * cipher[:keycount]-mode-iv:ivopts 29405ebaee6dSMilan Broz */ 29415ebaee6dSMilan Broz tmp = cipher_in; 29425ebaee6dSMilan Broz keycount = strsep(&tmp, "-"); 29435ebaee6dSMilan Broz cipher = strsep(&keycount, ":"); 29445ebaee6dSMilan Broz 294569a8cfcdSMilan Broz if (!keycount) 29465ebaee6dSMilan Broz cc->tfms_count = 1; 29475ebaee6dSMilan Broz else if (sscanf(keycount, "%u%c", &cc->tfms_count, &dummy) != 1 || 29485ebaee6dSMilan Broz !is_power_of_2(cc->tfms_count)) { 29495ebaee6dSMilan Broz ti->error = "Bad cipher key count specification"; 29505ebaee6dSMilan Broz return -EINVAL; 29515ebaee6dSMilan Broz } 295228513fccSMilan Broz cc->key_parts = cc->tfms_count; 29531da177e4SLinus Torvalds 2954ddd42edfSMilan Broz chainmode = strsep(&tmp, "-"); 29551856b9f7SMilan Broz *ivmode = strsep(&tmp, ":"); 29561856b9f7SMilan Broz *ivopts = tmp; 2957ddd42edfSMilan Broz 2958ddd42edfSMilan Broz /* 2959ddd42edfSMilan Broz * For compatibility with the original dm-crypt mapping format, if 2960ddd42edfSMilan Broz * only the cipher name is supplied, use cbc-plain. 296128513fccSMilan Broz */ 296233d2f09fSMilan Broz if (!chainmode || (!strcmp(chainmode, "plain") && !*ivmode)) { 2963cabf08e4SMilan Broz chainmode = "cbc"; 296433d2f09fSMilan Broz *ivmode = "plain"; 2965cabf08e4SMilan Broz } 2966cabf08e4SMilan Broz 296733d2f09fSMilan Broz if (strcmp(chainmode, "ecb") && !*ivmode) { 2968c0297721SAndi Kleen ti->error = "IV mechanism required"; 2969c0297721SAndi Kleen return -EINVAL; 2970c0297721SAndi Kleen } 2971c0297721SAndi Kleen 2972cabf08e4SMilan Broz cipher_api = kmalloc(CRYPTO_MAX_ALG_NAME, GFP_KERNEL); 29739934a8beSMilan Broz if (!cipher_api) 297428513fccSMilan Broz goto bad_mem; 29759934a8beSMilan Broz 2976a1a262b6SArd Biesheuvel if (*ivmode && !strcmp(*ivmode, "essiv")) { 2977a1a262b6SArd Biesheuvel if (!*ivopts) { 2978a1a262b6SArd Biesheuvel ti->error = "Digest algorithm missing for ESSIV mode"; 2979a1a262b6SArd Biesheuvel kfree(cipher_api); 2980a1a262b6SArd Biesheuvel return -EINVAL; 2981a1a262b6SArd Biesheuvel } 2982a1a262b6SArd Biesheuvel ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME, 2983a1a262b6SArd Biesheuvel "essiv(%s(%s),%s)", chainmode, cipher, *ivopts); 2984a1a262b6SArd Biesheuvel } else { 29859934a8beSMilan Broz ret = snprintf(cipher_api, CRYPTO_MAX_ALG_NAME, 2986647c7db1SMikulas Patocka "%s(%s)", chainmode, cipher); 2987a1a262b6SArd Biesheuvel } 2988a1a262b6SArd Biesheuvel if (ret < 0 || ret >= CRYPTO_MAX_ALG_NAME) { 29891da177e4SLinus Torvalds kfree(cipher_api); 299028513fccSMilan Broz goto bad_mem; 299128513fccSMilan Broz } 299228513fccSMilan Broz 29931da177e4SLinus Torvalds /* Allocate cipher */ 29941da177e4SLinus Torvalds ret = crypt_alloc_tfms(cc, cipher_api); 29951da177e4SLinus Torvalds if (ret < 0) { 29961da177e4SLinus Torvalds ti->error = "Error allocating crypto tfm"; 299733d2f09fSMilan Broz kfree(cipher_api); 299833d2f09fSMilan Broz return ret; 2999028867acSAlasdair G Kergon } 3000bd86e320SJeffy Chen kfree(cipher_api); 3001647c7db1SMikulas Patocka 300233d2f09fSMilan Broz return 0; 300333d2f09fSMilan Broz bad_mem: 300433d2f09fSMilan Broz ti->error = "Cannot allocate cipher strings"; 300533d2f09fSMilan Broz return -ENOMEM; 300633d2f09fSMilan Broz } 300733d2f09fSMilan Broz 300833d2f09fSMilan Broz static int crypt_ctr_cipher(struct dm_target *ti, char *cipher_in, char *key) 300933d2f09fSMilan Broz { 301033d2f09fSMilan Broz struct crypt_config *cc = ti->private; 301133d2f09fSMilan Broz char *ivmode = NULL, *ivopts = NULL; 301233d2f09fSMilan Broz int ret; 301333d2f09fSMilan Broz 301433d2f09fSMilan Broz cc->cipher_string = kstrdup(cipher_in, GFP_KERNEL); 301533d2f09fSMilan Broz if (!cc->cipher_string) { 301633d2f09fSMilan Broz ti->error = "Cannot allocate cipher strings"; 301733d2f09fSMilan Broz return -ENOMEM; 301833d2f09fSMilan Broz } 301933d2f09fSMilan Broz 302033d2f09fSMilan Broz if (strstarts(cipher_in, "capi:")) 302133d2f09fSMilan Broz ret = crypt_ctr_cipher_new(ti, cipher_in, key, &ivmode, &ivopts); 302233d2f09fSMilan Broz else 302333d2f09fSMilan Broz ret = crypt_ctr_cipher_old(ti, cipher_in, key, &ivmode, &ivopts); 302433d2f09fSMilan Broz if (ret) 302533d2f09fSMilan Broz return ret; 302633d2f09fSMilan Broz 3027647c7db1SMikulas Patocka /* Initialize IV */ 3028e889f97aSMilan Broz ret = crypt_ctr_ivmode(ti, ivmode); 3029e889f97aSMilan Broz if (ret < 0) 303033d2f09fSMilan Broz return ret; 30311da177e4SLinus Torvalds 3032da31a078SMilan Broz /* Initialize and set key */ 3033da31a078SMilan Broz ret = crypt_set_key(cc, key); 3034da31a078SMilan Broz if (ret < 0) { 3035da31a078SMilan Broz ti->error = "Error decoding and setting key"; 303633d2f09fSMilan Broz return ret; 3037da31a078SMilan Broz } 3038da31a078SMilan Broz 30391da177e4SLinus Torvalds /* Allocate IV */ 30401da177e4SLinus Torvalds if (cc->iv_gen_ops && cc->iv_gen_ops->ctr) { 30411da177e4SLinus Torvalds ret = cc->iv_gen_ops->ctr(cc, ti, ivopts); 30421da177e4SLinus Torvalds if (ret < 0) { 30431da177e4SLinus Torvalds ti->error = "Error creating IV"; 304433d2f09fSMilan Broz return ret; 30451da177e4SLinus Torvalds } 30461da177e4SLinus Torvalds } 30471da177e4SLinus Torvalds 30481da177e4SLinus Torvalds /* Initialize IV (set keys for ESSIV etc) */ 30491da177e4SLinus Torvalds if (cc->iv_gen_ops && cc->iv_gen_ops->init) { 30501da177e4SLinus Torvalds ret = cc->iv_gen_ops->init(cc); 30511da177e4SLinus Torvalds if (ret < 0) { 30521da177e4SLinus Torvalds ti->error = "Error initialising IV"; 30531da177e4SLinus Torvalds return ret; 30541da177e4SLinus Torvalds } 30551da177e4SLinus Torvalds } 30561da177e4SLinus Torvalds 3057dc94902bSOndrej Kozina /* wipe the kernel key payload copy */ 3058dc94902bSOndrej Kozina if (cc->key_string) 3059dc94902bSOndrej Kozina memset(cc->key, 0, cc->key_size * sizeof(u8)); 3060dc94902bSOndrej Kozina 306133d2f09fSMilan Broz return ret; 30621da177e4SLinus Torvalds } 30631da177e4SLinus Torvalds 3064ef43aa38SMilan Broz static int crypt_ctr_optional(struct dm_target *ti, unsigned int argc, char **argv) 3065ef43aa38SMilan Broz { 3066ef43aa38SMilan Broz struct crypt_config *cc = ti->private; 3067ef43aa38SMilan Broz struct dm_arg_set as; 30685916a22bSEric Biggers static const struct dm_arg _args[] = { 306939d42fa9SIgnat Korchagin {0, 8, "Invalid number of feature args"}, 3070ef43aa38SMilan Broz }; 3071ef43aa38SMilan Broz unsigned int opt_params, val; 3072ef43aa38SMilan Broz const char *opt_string, *sval; 30738f0009a2SMilan Broz char dummy; 3074ef43aa38SMilan Broz int ret; 3075ef43aa38SMilan Broz 3076ef43aa38SMilan Broz /* Optional parameters */ 3077ef43aa38SMilan Broz as.argc = argc; 3078ef43aa38SMilan Broz as.argv = argv; 3079ef43aa38SMilan Broz 3080ef43aa38SMilan Broz ret = dm_read_arg_group(_args, &as, &opt_params, &ti->error); 3081ef43aa38SMilan Broz if (ret) 30821da177e4SLinus Torvalds return ret; 30831da177e4SLinus Torvalds 3084ef43aa38SMilan Broz while (opt_params--) { 3085ef43aa38SMilan Broz opt_string = dm_shift_arg(&as); 3086ef43aa38SMilan Broz if (!opt_string) { 3087ef43aa38SMilan Broz ti->error = "Not enough feature arguments"; 3088ef43aa38SMilan Broz return -EINVAL; 3089ef43aa38SMilan Broz } 3090ef43aa38SMilan Broz 3091ef43aa38SMilan Broz if (!strcasecmp(opt_string, "allow_discards")) 3092ef43aa38SMilan Broz ti->num_discard_bios = 1; 3093ef43aa38SMilan Broz 3094ef43aa38SMilan Broz else if (!strcasecmp(opt_string, "same_cpu_crypt")) 3095ef43aa38SMilan Broz set_bit(DM_CRYPT_SAME_CPU, &cc->flags); 3096ef43aa38SMilan Broz 3097ef43aa38SMilan Broz else if (!strcasecmp(opt_string, "submit_from_crypt_cpus")) 3098ef43aa38SMilan Broz set_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); 309939d42fa9SIgnat Korchagin else if (!strcasecmp(opt_string, "no_read_workqueue")) 310039d42fa9SIgnat Korchagin set_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags); 310139d42fa9SIgnat Korchagin else if (!strcasecmp(opt_string, "no_write_workqueue")) 310239d42fa9SIgnat Korchagin set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); 3103ef43aa38SMilan Broz else if (sscanf(opt_string, "integrity:%u:", &val) == 1) { 3104ef43aa38SMilan Broz if (val == 0 || val > MAX_TAG_SIZE) { 3105ef43aa38SMilan Broz ti->error = "Invalid integrity arguments"; 3106ef43aa38SMilan Broz return -EINVAL; 3107ef43aa38SMilan Broz } 3108ef43aa38SMilan Broz cc->on_disk_tag_size = val; 3109ef43aa38SMilan Broz sval = strchr(opt_string + strlen("integrity:"), ':') + 1; 3110ef43aa38SMilan Broz if (!strcasecmp(sval, "aead")) { 3111ef43aa38SMilan Broz set_bit(CRYPT_MODE_INTEGRITY_AEAD, &cc->cipher_flags); 3112ef43aa38SMilan Broz } else if (strcasecmp(sval, "none")) { 3113ef43aa38SMilan Broz ti->error = "Unknown integrity profile"; 3114ef43aa38SMilan Broz return -EINVAL; 3115ef43aa38SMilan Broz } 3116ef43aa38SMilan Broz 3117ef43aa38SMilan Broz cc->cipher_auth = kstrdup(sval, GFP_KERNEL); 3118ef43aa38SMilan Broz if (!cc->cipher_auth) 31191da177e4SLinus Torvalds return -ENOMEM; 3120ff3af92bSMikulas Patocka } else if (sscanf(opt_string, "sector_size:%hu%c", &cc->sector_size, &dummy) == 1) { 31218f0009a2SMilan Broz if (cc->sector_size < (1 << SECTOR_SHIFT) || 31228f0009a2SMilan Broz cc->sector_size > 4096 || 3123ff3af92bSMikulas Patocka (cc->sector_size & (cc->sector_size - 1))) { 31248f0009a2SMilan Broz ti->error = "Invalid feature value for sector_size"; 31258f0009a2SMilan Broz return -EINVAL; 31268f0009a2SMilan Broz } 3127783874b0SMilan Broz if (ti->len & ((cc->sector_size >> SECTOR_SHIFT) - 1)) { 3128783874b0SMilan Broz ti->error = "Device size is not multiple of sector_size feature"; 3129783874b0SMilan Broz return -EINVAL; 3130783874b0SMilan Broz } 3131ff3af92bSMikulas Patocka cc->sector_shift = __ffs(cc->sector_size) - SECTOR_SHIFT; 31328f0009a2SMilan Broz } else if (!strcasecmp(opt_string, "iv_large_sectors")) 31338f0009a2SMilan Broz set_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); 31348f0009a2SMilan Broz else { 3135ef43aa38SMilan Broz ti->error = "Invalid feature arguments"; 3136ef43aa38SMilan Broz return -EINVAL; 3137ef43aa38SMilan Broz } 3138ef43aa38SMilan Broz } 3139ef43aa38SMilan Broz 3140ef43aa38SMilan Broz return 0; 31411da177e4SLinus Torvalds } 31421da177e4SLinus Torvalds 31438e225f04SDamien Le Moal #ifdef CONFIG_BLK_DEV_ZONED 31448e225f04SDamien Le Moal static int crypt_report_zones(struct dm_target *ti, 31458e225f04SDamien Le Moal struct dm_report_zones_args *args, unsigned int nr_zones) 31468e225f04SDamien Le Moal { 31478e225f04SDamien Le Moal struct crypt_config *cc = ti->private; 31488e225f04SDamien Le Moal 3149912e8875SDamien Le Moal return dm_report_zones(cc->dev->bdev, cc->start, 3150912e8875SDamien Le Moal cc->start + dm_target_offset(ti, args->next_sector), 3151912e8875SDamien Le Moal args, nr_zones); 31528e225f04SDamien Le Moal } 3153e3290b94SMike Snitzer #else 3154e3290b94SMike Snitzer #define crypt_report_zones NULL 31558e225f04SDamien Le Moal #endif 31568e225f04SDamien Le Moal 31571da177e4SLinus Torvalds /* 31581da177e4SLinus Torvalds * Construct an encryption mapping: 3159c538f6ecSOndrej Kozina * <cipher> [<key>|:<key_size>:<user|logon>:<key_description>] <iv_offset> <dev_path> <start> 31601da177e4SLinus Torvalds */ 31611da177e4SLinus Torvalds static int crypt_ctr(struct dm_target *ti, unsigned int argc, char **argv) 31621da177e4SLinus Torvalds { 31631da177e4SLinus Torvalds struct crypt_config *cc; 3164ed0302e8SMichał Mirosław const char *devname = dm_table_device_name(ti->table); 3165c538f6ecSOndrej Kozina int key_size; 3166ef43aa38SMilan Broz unsigned int align_mask; 31671da177e4SLinus Torvalds unsigned long long tmpll; 31681da177e4SLinus Torvalds int ret; 3169ef43aa38SMilan Broz size_t iv_size_padding, additional_req_size; 317031998ef1SMikulas Patocka char dummy; 31711da177e4SLinus Torvalds 3172772ae5f5SMilan Broz if (argc < 5) { 31731da177e4SLinus Torvalds ti->error = "Not enough arguments"; 31741da177e4SLinus Torvalds return -EINVAL; 31751da177e4SLinus Torvalds } 31761da177e4SLinus Torvalds 3177c538f6ecSOndrej Kozina key_size = get_key_size(&argv[1]); 3178c538f6ecSOndrej Kozina if (key_size < 0) { 3179c538f6ecSOndrej Kozina ti->error = "Cannot parse key size"; 3180c538f6ecSOndrej Kozina return -EINVAL; 3181c538f6ecSOndrej Kozina } 31821da177e4SLinus Torvalds 31839c81c99bSZhengyuan Liu cc = kzalloc(struct_size(cc, key, key_size), GFP_KERNEL); 31841da177e4SLinus Torvalds if (!cc) { 31851da177e4SLinus Torvalds ti->error = "Cannot allocate encryption context"; 31861da177e4SLinus Torvalds return -ENOMEM; 31871da177e4SLinus Torvalds } 31881da177e4SLinus Torvalds cc->key_size = key_size; 31898f0009a2SMilan Broz cc->sector_size = (1 << SECTOR_SHIFT); 3190ff3af92bSMikulas Patocka cc->sector_shift = 0; 31911da177e4SLinus Torvalds 31921da177e4SLinus Torvalds ti->private = cc; 3193ef43aa38SMilan Broz 31945059353dSMikulas Patocka spin_lock(&dm_crypt_clients_lock); 31955059353dSMikulas Patocka dm_crypt_clients_n++; 31965059353dSMikulas Patocka crypt_calculate_pages_per_client(); 31975059353dSMikulas Patocka spin_unlock(&dm_crypt_clients_lock); 31985059353dSMikulas Patocka 31995059353dSMikulas Patocka ret = percpu_counter_init(&cc->n_allocated_pages, 0, GFP_KERNEL); 32005059353dSMikulas Patocka if (ret < 0) 32015059353dSMikulas Patocka goto bad; 32025059353dSMikulas Patocka 3203ef43aa38SMilan Broz /* Optional parameters need to be read before cipher constructor */ 3204ef43aa38SMilan Broz if (argc > 5) { 3205ef43aa38SMilan Broz ret = crypt_ctr_optional(ti, argc - 5, &argv[5]); 3206ef43aa38SMilan Broz if (ret) 3207ef43aa38SMilan Broz goto bad; 3208ef43aa38SMilan Broz } 3209ef43aa38SMilan Broz 32101da177e4SLinus Torvalds ret = crypt_ctr_cipher(ti, argv[0], argv[1]); 32111da177e4SLinus Torvalds if (ret < 0) 32121da177e4SLinus Torvalds goto bad; 32131da177e4SLinus Torvalds 321433d2f09fSMilan Broz if (crypt_integrity_aead(cc)) { 3215ef43aa38SMilan Broz cc->dmreq_start = sizeof(struct aead_request); 3216ef43aa38SMilan Broz cc->dmreq_start += crypto_aead_reqsize(any_tfm_aead(cc)); 3217ef43aa38SMilan Broz align_mask = crypto_aead_alignmask(any_tfm_aead(cc)); 3218ef43aa38SMilan Broz } else { 3219bbdb23b5SHerbert Xu cc->dmreq_start = sizeof(struct skcipher_request); 3220bbdb23b5SHerbert Xu cc->dmreq_start += crypto_skcipher_reqsize(any_tfm(cc)); 3221ef43aa38SMilan Broz align_mask = crypto_skcipher_alignmask(any_tfm(cc)); 3222ef43aa38SMilan Broz } 3223d49ec52fSMikulas Patocka cc->dmreq_start = ALIGN(cc->dmreq_start, __alignof__(struct dm_crypt_request)); 3224d49ec52fSMikulas Patocka 3225ef43aa38SMilan Broz if (align_mask < CRYPTO_MINALIGN) { 3226d49ec52fSMikulas Patocka /* Allocate the padding exactly */ 3227d49ec52fSMikulas Patocka iv_size_padding = -(cc->dmreq_start + sizeof(struct dm_crypt_request)) 3228ef43aa38SMilan Broz & align_mask; 3229d49ec52fSMikulas Patocka } else { 3230d49ec52fSMikulas Patocka /* 3231d49ec52fSMikulas Patocka * If the cipher requires greater alignment than kmalloc 3232d49ec52fSMikulas Patocka * alignment, we don't know the exact position of the 3233d49ec52fSMikulas Patocka * initialization vector. We must assume worst case. 3234d49ec52fSMikulas Patocka */ 3235ef43aa38SMilan Broz iv_size_padding = align_mask; 3236d49ec52fSMikulas Patocka } 32371da177e4SLinus Torvalds 3238ef43aa38SMilan Broz /* ...| IV + padding | original IV | original sec. number | bio tag offset | */ 3239ef43aa38SMilan Broz additional_req_size = sizeof(struct dm_crypt_request) + 3240ef43aa38SMilan Broz iv_size_padding + cc->iv_size + 3241ef43aa38SMilan Broz cc->iv_size + 3242ef43aa38SMilan Broz sizeof(uint64_t) + 3243ef43aa38SMilan Broz sizeof(unsigned int); 3244ef43aa38SMilan Broz 32456f1c819cSKent Overstreet ret = mempool_init_kmalloc_pool(&cc->req_pool, MIN_IOS, cc->dmreq_start + additional_req_size); 32466f1c819cSKent Overstreet if (ret) { 32471da177e4SLinus Torvalds ti->error = "Cannot allocate crypt request mempool"; 32481da177e4SLinus Torvalds goto bad; 32491da177e4SLinus Torvalds } 32501da177e4SLinus Torvalds 325130187e1dSMike Snitzer cc->per_bio_data_size = ti->per_io_data_size = 3252ef43aa38SMilan Broz ALIGN(sizeof(struct dm_crypt_io) + cc->dmreq_start + additional_req_size, 3253d49ec52fSMikulas Patocka ARCH_KMALLOC_MINALIGN); 3254298a9fa0SMikulas Patocka 3255a8affc03SChristoph Hellwig ret = mempool_init(&cc->page_pool, BIO_MAX_VECS, crypt_page_alloc, crypt_page_free, cc); 32566f1c819cSKent Overstreet if (ret) { 32578b004457SMilan Broz ti->error = "Cannot allocate page mempool"; 3258e48d4bbfSMilan Broz goto bad; 32591da177e4SLinus Torvalds } 3260e48d4bbfSMilan Broz 32616f1c819cSKent Overstreet ret = bioset_init(&cc->bs, MIN_IOS, 0, BIOSET_NEED_BVECS); 32626f1c819cSKent Overstreet if (ret) { 32630c395b0fSMilan Broz ti->error = "Cannot allocate crypt bioset"; 3264cabf08e4SMilan Broz goto bad; 326593e605c2SMilan Broz } 3266cabf08e4SMilan Broz 32677145c241SMikulas Patocka mutex_init(&cc->bio_alloc_lock); 32687145c241SMikulas Patocka 3269cabf08e4SMilan Broz ret = -EINVAL; 32708f0009a2SMilan Broz if ((sscanf(argv[2], "%llu%c", &tmpll, &dummy) != 1) || 32718f0009a2SMilan Broz (tmpll & ((cc->sector_size >> SECTOR_SHIFT) - 1))) { 3272cabf08e4SMilan Broz ti->error = "Invalid iv_offset sector"; 3273cabf08e4SMilan Broz goto bad; 32741da177e4SLinus Torvalds } 3275d2a7ad29SKiyoshi Ueda cc->iv_offset = tmpll; 32761da177e4SLinus Torvalds 3277e80d1c80SVivek Goyal ret = dm_get_device(ti, argv[3], dm_table_get_mode(ti->table), &cc->dev); 3278e80d1c80SVivek Goyal if (ret) { 32791da177e4SLinus Torvalds ti->error = "Device lookup failed"; 32801da177e4SLinus Torvalds goto bad; 32811da177e4SLinus Torvalds } 32821da177e4SLinus Torvalds 3283e80d1c80SVivek Goyal ret = -EINVAL; 3284ef87bfc2SMilan Broz if (sscanf(argv[4], "%llu%c", &tmpll, &dummy) != 1 || tmpll != (sector_t)tmpll) { 32851da177e4SLinus Torvalds ti->error = "Invalid device sector"; 32861da177e4SLinus Torvalds goto bad; 32871da177e4SLinus Torvalds } 32881da177e4SLinus Torvalds cc->start = tmpll; 32891da177e4SLinus Torvalds 3290f34ee1dcSDamien Le Moal if (bdev_is_zoned(cc->dev->bdev)) { 32918e225f04SDamien Le Moal /* 32928e225f04SDamien Le Moal * For zoned block devices, we need to preserve the issuer write 32938e225f04SDamien Le Moal * ordering. To do so, disable write workqueues and force inline 32948e225f04SDamien Le Moal * encryption completion. 32958e225f04SDamien Le Moal */ 32968e225f04SDamien Le Moal set_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); 32978e225f04SDamien Le Moal set_bit(DM_CRYPT_WRITE_INLINE, &cc->flags); 3298f34ee1dcSDamien Le Moal 3299f34ee1dcSDamien Le Moal /* 3300f34ee1dcSDamien Le Moal * All zone append writes to a zone of a zoned block device will 3301f34ee1dcSDamien Le Moal * have the same BIO sector, the start of the zone. When the 3302f34ee1dcSDamien Le Moal * cypher IV mode uses sector values, all data targeting a 3303f34ee1dcSDamien Le Moal * zone will be encrypted using the first sector numbers of the 3304f34ee1dcSDamien Le Moal * zone. This will not result in write errors but will 3305f34ee1dcSDamien Le Moal * cause most reads to fail as reads will use the sector values 3306f34ee1dcSDamien Le Moal * for the actual data locations, resulting in IV mismatch. 3307f34ee1dcSDamien Le Moal * To avoid this problem, ask DM core to emulate zone append 3308f34ee1dcSDamien Le Moal * operations with regular writes. 3309f34ee1dcSDamien Le Moal */ 3310f34ee1dcSDamien Le Moal DMDEBUG("Zone append operations will be emulated"); 3311f34ee1dcSDamien Le Moal ti->emulate_zone_append = true; 33128e225f04SDamien Le Moal } 33138e225f04SDamien Le Moal 331433d2f09fSMilan Broz if (crypt_integrity_aead(cc) || cc->integrity_iv_size) { 3315ef43aa38SMilan Broz ret = crypt_integrity_ctr(cc, ti); 3316772ae5f5SMilan Broz if (ret) 3317772ae5f5SMilan Broz goto bad; 3318772ae5f5SMilan Broz 3319ef43aa38SMilan Broz cc->tag_pool_max_sectors = POOL_ENTRY_SIZE / cc->on_disk_tag_size; 3320ef43aa38SMilan Broz if (!cc->tag_pool_max_sectors) 3321ef43aa38SMilan Broz cc->tag_pool_max_sectors = 1; 3322ef43aa38SMilan Broz 33236f1c819cSKent Overstreet ret = mempool_init_kmalloc_pool(&cc->tag_pool, MIN_IOS, 3324ef43aa38SMilan Broz cc->tag_pool_max_sectors * cc->on_disk_tag_size); 33256f1c819cSKent Overstreet if (ret) { 3326ef43aa38SMilan Broz ti->error = "Cannot allocate integrity tags mempool"; 3327f3396c58SMikulas Patocka goto bad; 3328f3396c58SMikulas Patocka } 3329772ae5f5SMilan Broz 3330583fe747SMikulas Patocka cc->tag_pool_max_sectors <<= cc->sector_shift; 3331f3396c58SMikulas Patocka } 3332772ae5f5SMilan Broz 33331da177e4SLinus Torvalds ret = -ENOMEM; 3334f612b213SMike Snitzer cc->io_queue = alloc_workqueue("kcryptd_io/%s", WQ_MEM_RECLAIM, 1, devname); 33351da177e4SLinus Torvalds if (!cc->io_queue) { 33361da177e4SLinus Torvalds ti->error = "Couldn't create kcryptd io queue"; 33371da177e4SLinus Torvalds goto bad; 33381da177e4SLinus Torvalds } 333937af6560SChristophe Saout 3340f3396c58SMikulas Patocka if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) 334148b0777cSMike Snitzer cc->crypt_queue = alloc_workqueue("kcryptd/%s", WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM, 3342ed0302e8SMichał Mirosław 1, devname); 3343f3396c58SMikulas Patocka else 334448b0777cSMike Snitzer cc->crypt_queue = alloc_workqueue("kcryptd/%s", 334548b0777cSMike Snitzer WQ_CPU_INTENSIVE | WQ_MEM_RECLAIM | WQ_UNBOUND, 3346ed0302e8SMichał Mirosław num_online_cpus(), devname); 33471da177e4SLinus Torvalds if (!cc->crypt_queue) { 33481da177e4SLinus Torvalds ti->error = "Couldn't create kcryptd queue"; 33491da177e4SLinus Torvalds goto bad; 33501da177e4SLinus Torvalds } 33511da177e4SLinus Torvalds 3352c7329effSMikulas Patocka spin_lock_init(&cc->write_thread_lock); 3353b3c5fd30SMikulas Patocka cc->write_tree = RB_ROOT; 3354dc267621SMikulas Patocka 3355a5217c11SCai Huoqing cc->write_thread = kthread_run(dmcrypt_write, cc, "dmcrypt_write/%s", devname); 3356dc267621SMikulas Patocka if (IS_ERR(cc->write_thread)) { 3357dc267621SMikulas Patocka ret = PTR_ERR(cc->write_thread); 3358dc267621SMikulas Patocka cc->write_thread = NULL; 3359dc267621SMikulas Patocka ti->error = "Couldn't spawn write thread"; 3360dc267621SMikulas Patocka goto bad; 3361dc267621SMikulas Patocka } 3362dc267621SMikulas Patocka 336355a62eefSAlasdair G Kergon ti->num_flush_bios = 1; 3364a666e5c0SMikulas Patocka ti->limit_swap_bios = true; 3365e5524e12SMike Snitzer ti->accounts_remapped_io = true; 3366983c7db3SMilan Broz 336758d0f180SMichael Weiß dm_audit_log_ctr(DM_MSG_PREFIX, ti, 1); 33681da177e4SLinus Torvalds return 0; 33691da177e4SLinus Torvalds 33701da177e4SLinus Torvalds bad: 337158d0f180SMichael Weiß dm_audit_log_ctr(DM_MSG_PREFIX, ti, 0); 33721da177e4SLinus Torvalds crypt_dtr(ti); 33731da177e4SLinus Torvalds return ret; 3374647c7db1SMikulas Patocka } 3375647c7db1SMikulas Patocka 33767de3ee57SMikulas Patocka static int crypt_map(struct dm_target *ti, struct bio *bio) 33771da177e4SLinus Torvalds { 33781da177e4SLinus Torvalds struct dm_crypt_io *io; 337949a8a920SAlasdair G Kergon struct crypt_config *cc = ti->private; 3380647c7db1SMikulas Patocka 3381772ae5f5SMilan Broz /* 338228a8f0d3SMike Christie * If bio is REQ_PREFLUSH or REQ_OP_DISCARD, just bypass crypt queues. 338328a8f0d3SMike Christie * - for REQ_PREFLUSH device-mapper core ensures that no IO is in-flight 3384e6047149SMike Christie * - for REQ_OP_DISCARD caller must use flush if IO ordering matters 3385772ae5f5SMilan Broz */ 33861eff9d32SJens Axboe if (unlikely(bio->bi_opf & REQ_PREFLUSH || 338728a8f0d3SMike Christie bio_op(bio) == REQ_OP_DISCARD)) { 338874d46992SChristoph Hellwig bio_set_dev(bio, cc->dev->bdev); 3389772ae5f5SMilan Broz if (bio_sectors(bio)) 33904f024f37SKent Overstreet bio->bi_iter.bi_sector = cc->start + 33914f024f37SKent Overstreet dm_target_offset(ti, bio->bi_iter.bi_sector); 3392647c7db1SMikulas Patocka return DM_MAPIO_REMAPPED; 3393647c7db1SMikulas Patocka } 33941da177e4SLinus Torvalds 33954e870e94SMikulas Patocka /* 33964e870e94SMikulas Patocka * Check if bio is too large, split as needed. 33974e870e94SMikulas Patocka */ 3398a8affc03SChristoph Hellwig if (unlikely(bio->bi_iter.bi_size > (BIO_MAX_VECS << PAGE_SHIFT)) && 3399ef43aa38SMilan Broz (bio_data_dir(bio) == WRITE || cc->on_disk_tag_size)) 3400a8affc03SChristoph Hellwig dm_accept_partial_bio(bio, ((BIO_MAX_VECS << PAGE_SHIFT) >> SECTOR_SHIFT)); 34014e870e94SMikulas Patocka 34028f0009a2SMilan Broz /* 34038f0009a2SMilan Broz * Ensure that bio is a multiple of internal sector encryption size 34048f0009a2SMilan Broz * and is aligned to this size as defined in IO hints. 34058f0009a2SMilan Broz */ 34068f0009a2SMilan Broz if (unlikely((bio->bi_iter.bi_sector & ((cc->sector_size >> SECTOR_SHIFT) - 1)) != 0)) 3407846785e6SChristoph Hellwig return DM_MAPIO_KILL; 34088f0009a2SMilan Broz 34098f0009a2SMilan Broz if (unlikely(bio->bi_iter.bi_size & (cc->sector_size - 1))) 3410846785e6SChristoph Hellwig return DM_MAPIO_KILL; 34118f0009a2SMilan Broz 3412298a9fa0SMikulas Patocka io = dm_per_bio_data(bio, cc->per_bio_data_size); 3413298a9fa0SMikulas Patocka crypt_io_init(io, cc, bio, dm_target_offset(ti, bio->bi_iter.bi_sector)); 3414ef43aa38SMilan Broz 3415ef43aa38SMilan Broz if (cc->on_disk_tag_size) { 3416583fe747SMikulas Patocka unsigned tag_len = cc->on_disk_tag_size * (bio_sectors(bio) >> cc->sector_shift); 3417ef43aa38SMilan Broz 3418ef43aa38SMilan Broz if (unlikely(tag_len > KMALLOC_MAX_SIZE) || 3419583fe747SMikulas Patocka unlikely(!(io->integrity_metadata = kmalloc(tag_len, 3420ef43aa38SMilan Broz GFP_NOIO | __GFP_NORETRY | __GFP_NOMEMALLOC | __GFP_NOWARN)))) { 3421ef43aa38SMilan Broz if (bio_sectors(bio) > cc->tag_pool_max_sectors) 3422ef43aa38SMilan Broz dm_accept_partial_bio(bio, cc->tag_pool_max_sectors); 34236f1c819cSKent Overstreet io->integrity_metadata = mempool_alloc(&cc->tag_pool, GFP_NOIO); 3424ef43aa38SMilan Broz io->integrity_metadata_from_pool = true; 3425ef43aa38SMilan Broz } 3426ef43aa38SMilan Broz } 3427ef43aa38SMilan Broz 342833d2f09fSMilan Broz if (crypt_integrity_aead(cc)) 3429ef43aa38SMilan Broz io->ctx.r.req_aead = (struct aead_request *)(io + 1); 3430ef43aa38SMilan Broz else 3431ef43aa38SMilan Broz io->ctx.r.req = (struct skcipher_request *)(io + 1); 34321da177e4SLinus Torvalds 343320c82538SMilan Broz if (bio_data_dir(io->base_bio) == READ) { 3434e5524e12SMike Snitzer if (kcryptd_io_read(io, CRYPT_MAP_READ_GFP)) 3435dc267621SMikulas Patocka kcryptd_queue_read(io); 343620c82538SMilan Broz } else 34374ee218cdSAndrew Morton kcryptd_queue_crypt(io); 34384ee218cdSAndrew Morton 34391da177e4SLinus Torvalds return DM_MAPIO_SUBMITTED; 34401da177e4SLinus Torvalds } 34411da177e4SLinus Torvalds 3442567dd8f3SMikulas Patocka static char hex2asc(unsigned char c) 3443567dd8f3SMikulas Patocka { 3444567dd8f3SMikulas Patocka return c + '0' + ((unsigned)(9 - c) >> 4 & 0x27); 3445567dd8f3SMikulas Patocka } 3446567dd8f3SMikulas Patocka 3447fd7c092eSMikulas Patocka static void crypt_status(struct dm_target *ti, status_type_t type, 34481f4e0ff0SAlasdair G Kergon unsigned status_flags, char *result, unsigned maxlen) 34491da177e4SLinus Torvalds { 34505ebaee6dSMilan Broz struct crypt_config *cc = ti->private; 3451fd7c092eSMikulas Patocka unsigned i, sz = 0; 3452f3396c58SMikulas Patocka int num_feature_args = 0; 34531da177e4SLinus Torvalds 34541da177e4SLinus Torvalds switch (type) { 34551da177e4SLinus Torvalds case STATUSTYPE_INFO: 34561da177e4SLinus Torvalds result[0] = '\0'; 34571da177e4SLinus Torvalds break; 34581da177e4SLinus Torvalds 34591da177e4SLinus Torvalds case STATUSTYPE_TABLE: 34607dbcd137SMilan Broz DMEMIT("%s ", cc->cipher_string); 34611da177e4SLinus Torvalds 3462c538f6ecSOndrej Kozina if (cc->key_size > 0) { 3463c538f6ecSOndrej Kozina if (cc->key_string) 3464c538f6ecSOndrej Kozina DMEMIT(":%u:%s", cc->key_size, cc->key_string); 3465567dd8f3SMikulas Patocka else { 3466567dd8f3SMikulas Patocka for (i = 0; i < cc->key_size; i++) { 3467567dd8f3SMikulas Patocka DMEMIT("%c%c", hex2asc(cc->key[i] >> 4), 3468567dd8f3SMikulas Patocka hex2asc(cc->key[i] & 0xf)); 3469567dd8f3SMikulas Patocka } 3470567dd8f3SMikulas Patocka } 3471c538f6ecSOndrej Kozina } else 3472fd7c092eSMikulas Patocka DMEMIT("-"); 34731da177e4SLinus Torvalds 34741da177e4SLinus Torvalds DMEMIT(" %llu %s %llu", (unsigned long long)cc->iv_offset, 34751da177e4SLinus Torvalds cc->dev->name, (unsigned long long)cc->start); 3476772ae5f5SMilan Broz 3477f3396c58SMikulas Patocka num_feature_args += !!ti->num_discard_bios; 3478f3396c58SMikulas Patocka num_feature_args += test_bit(DM_CRYPT_SAME_CPU, &cc->flags); 34790f5d8e6eSMikulas Patocka num_feature_args += test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags); 348039d42fa9SIgnat Korchagin num_feature_args += test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags); 348139d42fa9SIgnat Korchagin num_feature_args += test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags); 3482ff3af92bSMikulas Patocka num_feature_args += cc->sector_size != (1 << SECTOR_SHIFT); 34838f0009a2SMilan Broz num_feature_args += test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags); 3484ef43aa38SMilan Broz if (cc->on_disk_tag_size) 3485ef43aa38SMilan Broz num_feature_args++; 3486f3396c58SMikulas Patocka if (num_feature_args) { 3487f3396c58SMikulas Patocka DMEMIT(" %d", num_feature_args); 348855a62eefSAlasdair G Kergon if (ti->num_discard_bios) 3489f3396c58SMikulas Patocka DMEMIT(" allow_discards"); 3490f3396c58SMikulas Patocka if (test_bit(DM_CRYPT_SAME_CPU, &cc->flags)) 3491f3396c58SMikulas Patocka DMEMIT(" same_cpu_crypt"); 34920f5d8e6eSMikulas Patocka if (test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags)) 34930f5d8e6eSMikulas Patocka DMEMIT(" submit_from_crypt_cpus"); 349439d42fa9SIgnat Korchagin if (test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags)) 349539d42fa9SIgnat Korchagin DMEMIT(" no_read_workqueue"); 349639d42fa9SIgnat Korchagin if (test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags)) 349739d42fa9SIgnat Korchagin DMEMIT(" no_write_workqueue"); 3498ef43aa38SMilan Broz if (cc->on_disk_tag_size) 3499ef43aa38SMilan Broz DMEMIT(" integrity:%u:%s", cc->on_disk_tag_size, cc->cipher_auth); 35008f0009a2SMilan Broz if (cc->sector_size != (1 << SECTOR_SHIFT)) 35018f0009a2SMilan Broz DMEMIT(" sector_size:%d", cc->sector_size); 35028f0009a2SMilan Broz if (test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags)) 35038f0009a2SMilan Broz DMEMIT(" iv_large_sectors"); 3504f3396c58SMikulas Patocka } 35058ec45662STushar Sugandhi break; 3506772ae5f5SMilan Broz 35078ec45662STushar Sugandhi case STATUSTYPE_IMA: 35088ec45662STushar Sugandhi DMEMIT_TARGET_NAME_VERSION(ti->type); 35098ec45662STushar Sugandhi DMEMIT(",allow_discards=%c", ti->num_discard_bios ? 'y' : 'n'); 35108ec45662STushar Sugandhi DMEMIT(",same_cpu_crypt=%c", test_bit(DM_CRYPT_SAME_CPU, &cc->flags) ? 'y' : 'n'); 35118ec45662STushar Sugandhi DMEMIT(",submit_from_crypt_cpus=%c", test_bit(DM_CRYPT_NO_OFFLOAD, &cc->flags) ? 35128ec45662STushar Sugandhi 'y' : 'n'); 35138ec45662STushar Sugandhi DMEMIT(",no_read_workqueue=%c", test_bit(DM_CRYPT_NO_READ_WORKQUEUE, &cc->flags) ? 35148ec45662STushar Sugandhi 'y' : 'n'); 35158ec45662STushar Sugandhi DMEMIT(",no_write_workqueue=%c", test_bit(DM_CRYPT_NO_WRITE_WORKQUEUE, &cc->flags) ? 35168ec45662STushar Sugandhi 'y' : 'n'); 35178ec45662STushar Sugandhi DMEMIT(",iv_large_sectors=%c", test_bit(CRYPT_IV_LARGE_SECTORS, &cc->cipher_flags) ? 35188ec45662STushar Sugandhi 'y' : 'n'); 35198ec45662STushar Sugandhi 35208ec45662STushar Sugandhi if (cc->on_disk_tag_size) 35218ec45662STushar Sugandhi DMEMIT(",integrity_tag_size=%u,cipher_auth=%s", 35228ec45662STushar Sugandhi cc->on_disk_tag_size, cc->cipher_auth); 35238ec45662STushar Sugandhi if (cc->sector_size != (1 << SECTOR_SHIFT)) 35248ec45662STushar Sugandhi DMEMIT(",sector_size=%d", cc->sector_size); 35258ec45662STushar Sugandhi if (cc->cipher_string) 35268ec45662STushar Sugandhi DMEMIT(",cipher_string=%s", cc->cipher_string); 35278ec45662STushar Sugandhi 35288ec45662STushar Sugandhi DMEMIT(",key_size=%u", cc->key_size); 35298ec45662STushar Sugandhi DMEMIT(",key_parts=%u", cc->key_parts); 35308ec45662STushar Sugandhi DMEMIT(",key_extra_size=%u", cc->key_extra_size); 35318ec45662STushar Sugandhi DMEMIT(",key_mac_size=%u", cc->key_mac_size); 35328ec45662STushar Sugandhi DMEMIT(";"); 35331da177e4SLinus Torvalds break; 35341da177e4SLinus Torvalds } 35351da177e4SLinus Torvalds } 35361da177e4SLinus Torvalds 3537e48d4bbfSMilan Broz static void crypt_postsuspend(struct dm_target *ti) 3538e48d4bbfSMilan Broz { 3539e48d4bbfSMilan Broz struct crypt_config *cc = ti->private; 3540e48d4bbfSMilan Broz 3541e48d4bbfSMilan Broz set_bit(DM_CRYPT_SUSPENDED, &cc->flags); 3542e48d4bbfSMilan Broz } 3543e48d4bbfSMilan Broz 3544e48d4bbfSMilan Broz static int crypt_preresume(struct dm_target *ti) 3545e48d4bbfSMilan Broz { 3546e48d4bbfSMilan Broz struct crypt_config *cc = ti->private; 3547e48d4bbfSMilan Broz 3548e48d4bbfSMilan Broz if (!test_bit(DM_CRYPT_KEY_VALID, &cc->flags)) { 3549e48d4bbfSMilan Broz DMERR("aborting resume - crypt key is not set."); 3550e48d4bbfSMilan Broz return -EAGAIN; 3551e48d4bbfSMilan Broz } 3552e48d4bbfSMilan Broz 3553e48d4bbfSMilan Broz return 0; 3554e48d4bbfSMilan Broz } 3555e48d4bbfSMilan Broz 3556e48d4bbfSMilan Broz static void crypt_resume(struct dm_target *ti) 3557e48d4bbfSMilan Broz { 3558e48d4bbfSMilan Broz struct crypt_config *cc = ti->private; 3559e48d4bbfSMilan Broz 3560e48d4bbfSMilan Broz clear_bit(DM_CRYPT_SUSPENDED, &cc->flags); 3561e48d4bbfSMilan Broz } 3562e48d4bbfSMilan Broz 3563e48d4bbfSMilan Broz /* Message interface 3564e48d4bbfSMilan Broz * key set <key> 3565e48d4bbfSMilan Broz * key wipe 3566e48d4bbfSMilan Broz */ 35671eb5fa84SMike Snitzer static int crypt_message(struct dm_target *ti, unsigned argc, char **argv, 35681eb5fa84SMike Snitzer char *result, unsigned maxlen) 3569e48d4bbfSMilan Broz { 3570e48d4bbfSMilan Broz struct crypt_config *cc = ti->private; 3571c538f6ecSOndrej Kozina int key_size, ret = -EINVAL; 3572e48d4bbfSMilan Broz 3573e48d4bbfSMilan Broz if (argc < 2) 3574e48d4bbfSMilan Broz goto error; 3575e48d4bbfSMilan Broz 3576498f0103SMike Snitzer if (!strcasecmp(argv[0], "key")) { 3577e48d4bbfSMilan Broz if (!test_bit(DM_CRYPT_SUSPENDED, &cc->flags)) { 3578e48d4bbfSMilan Broz DMWARN("not suspended during key manipulation."); 3579e48d4bbfSMilan Broz return -EINVAL; 3580e48d4bbfSMilan Broz } 3581498f0103SMike Snitzer if (argc == 3 && !strcasecmp(argv[1], "set")) { 3582c538f6ecSOndrej Kozina /* The key size may not be changed. */ 3583c538f6ecSOndrej Kozina key_size = get_key_size(&argv[2]); 3584c538f6ecSOndrej Kozina if (key_size < 0 || cc->key_size != key_size) { 3585c538f6ecSOndrej Kozina memset(argv[2], '0', strlen(argv[2])); 3586c538f6ecSOndrej Kozina return -EINVAL; 3587c538f6ecSOndrej Kozina } 3588c538f6ecSOndrej Kozina 3589542da317SMilan Broz ret = crypt_set_key(cc, argv[2]); 3590542da317SMilan Broz if (ret) 3591542da317SMilan Broz return ret; 3592542da317SMilan Broz if (cc->iv_gen_ops && cc->iv_gen_ops->init) 3593542da317SMilan Broz ret = cc->iv_gen_ops->init(cc); 3594dc94902bSOndrej Kozina /* wipe the kernel key payload copy */ 3595dc94902bSOndrej Kozina if (cc->key_string) 3596dc94902bSOndrej Kozina memset(cc->key, 0, cc->key_size * sizeof(u8)); 3597542da317SMilan Broz return ret; 3598542da317SMilan Broz } 35994a52ffc7SMilan Broz if (argc == 2 && !strcasecmp(argv[1], "wipe")) 3600e48d4bbfSMilan Broz return crypt_wipe_key(cc); 3601e48d4bbfSMilan Broz } 3602e48d4bbfSMilan Broz 3603e48d4bbfSMilan Broz error: 3604e48d4bbfSMilan Broz DMWARN("unrecognised message received."); 3605e48d4bbfSMilan Broz return -EINVAL; 3606e48d4bbfSMilan Broz } 3607e48d4bbfSMilan Broz 3608af4874e0SMike Snitzer static int crypt_iterate_devices(struct dm_target *ti, 3609af4874e0SMike Snitzer iterate_devices_callout_fn fn, void *data) 3610af4874e0SMike Snitzer { 3611af4874e0SMike Snitzer struct crypt_config *cc = ti->private; 3612af4874e0SMike Snitzer 36135dea271bSMike Snitzer return fn(ti, cc->dev, cc->start, ti->len, data); 3614af4874e0SMike Snitzer } 3615af4874e0SMike Snitzer 3616586b286bSMike Snitzer static void crypt_io_hints(struct dm_target *ti, struct queue_limits *limits) 3617586b286bSMike Snitzer { 36188f0009a2SMilan Broz struct crypt_config *cc = ti->private; 36198f0009a2SMilan Broz 3620586b286bSMike Snitzer /* 3621586b286bSMike Snitzer * Unfortunate constraint that is required to avoid the potential 3622586b286bSMike Snitzer * for exceeding underlying device's max_segments limits -- due to 3623586b286bSMike Snitzer * crypt_alloc_buffer() possibly allocating pages for the encryption 3624586b286bSMike Snitzer * bio that are not as physically contiguous as the original bio. 3625586b286bSMike Snitzer */ 3626586b286bSMike Snitzer limits->max_segment_size = PAGE_SIZE; 36278f0009a2SMilan Broz 3628bc9e9cf0SMikulas Patocka limits->logical_block_size = 362964611a15SEric Biggers max_t(unsigned, limits->logical_block_size, cc->sector_size); 3630bc9e9cf0SMikulas Patocka limits->physical_block_size = 3631bc9e9cf0SMikulas Patocka max_t(unsigned, limits->physical_block_size, cc->sector_size); 3632bc9e9cf0SMikulas Patocka limits->io_min = max_t(unsigned, limits->io_min, cc->sector_size); 3633*86e4d3e8SKeith Busch limits->dma_alignment = limits->logical_block_size - 1; 3634586b286bSMike Snitzer } 3635586b286bSMike Snitzer 36361da177e4SLinus Torvalds static struct target_type crypt_target = { 36371da177e4SLinus Torvalds .name = "crypt", 3638e5524e12SMike Snitzer .version = {1, 24, 0}, 36391da177e4SLinus Torvalds .module = THIS_MODULE, 36401da177e4SLinus Torvalds .ctr = crypt_ctr, 36411da177e4SLinus Torvalds .dtr = crypt_dtr, 36428e225f04SDamien Le Moal .features = DM_TARGET_ZONED_HM, 36438e225f04SDamien Le Moal .report_zones = crypt_report_zones, 36441da177e4SLinus Torvalds .map = crypt_map, 36451da177e4SLinus Torvalds .status = crypt_status, 3646e48d4bbfSMilan Broz .postsuspend = crypt_postsuspend, 3647e48d4bbfSMilan Broz .preresume = crypt_preresume, 3648e48d4bbfSMilan Broz .resume = crypt_resume, 3649e48d4bbfSMilan Broz .message = crypt_message, 3650af4874e0SMike Snitzer .iterate_devices = crypt_iterate_devices, 3651586b286bSMike Snitzer .io_hints = crypt_io_hints, 36521da177e4SLinus Torvalds }; 36531da177e4SLinus Torvalds 36541da177e4SLinus Torvalds static int __init dm_crypt_init(void) 36551da177e4SLinus Torvalds { 36561da177e4SLinus Torvalds int r; 36571da177e4SLinus Torvalds 36581da177e4SLinus Torvalds r = dm_register_target(&crypt_target); 365994f5e024SMikulas Patocka if (r < 0) 366072d94861SAlasdair G Kergon DMERR("register failed %d", r); 36611da177e4SLinus Torvalds 36621da177e4SLinus Torvalds return r; 36631da177e4SLinus Torvalds } 36641da177e4SLinus Torvalds 36651da177e4SLinus Torvalds static void __exit dm_crypt_exit(void) 36661da177e4SLinus Torvalds { 366710d3bd09SMikulas Patocka dm_unregister_target(&crypt_target); 36681da177e4SLinus Torvalds } 36691da177e4SLinus Torvalds 36701da177e4SLinus Torvalds module_init(dm_crypt_init); 36711da177e4SLinus Torvalds module_exit(dm_crypt_exit); 36721da177e4SLinus Torvalds 3673bf14299fSJana Saout MODULE_AUTHOR("Jana Saout <jana@saout.de>"); 36741da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " target for transparent encryption / decryption"); 36751da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 3676