1488f6682SSatya Tangirala // SPDX-License-Identifier: GPL-2.0
2488f6682SSatya Tangirala /*
3488f6682SSatya Tangirala * Copyright 2019 Google LLC
4488f6682SSatya Tangirala */
5488f6682SSatya Tangirala
6488f6682SSatya Tangirala /*
7488f6682SSatya Tangirala * Refer to Documentation/block/inline-encryption.rst for detailed explanation.
8488f6682SSatya Tangirala */
9488f6682SSatya Tangirala
10488f6682SSatya Tangirala #define pr_fmt(fmt) "blk-crypto-fallback: " fmt
11488f6682SSatya Tangirala
12488f6682SSatya Tangirala #include <crypto/skcipher.h>
13488f6682SSatya Tangirala #include <linux/blk-crypto.h>
141e8d44bdSEric Biggers #include <linux/blk-crypto-profile.h>
15488f6682SSatya Tangirala #include <linux/blkdev.h>
16488f6682SSatya Tangirala #include <linux/crypto.h>
17488f6682SSatya Tangirala #include <linux/mempool.h>
18488f6682SSatya Tangirala #include <linux/module.h>
19488f6682SSatya Tangirala #include <linux/random.h>
2024b83debSChristoph Hellwig #include <linux/scatterlist.h>
21488f6682SSatya Tangirala
22672fdcf0SMing Lei #include "blk-cgroup.h"
23488f6682SSatya Tangirala #include "blk-crypto-internal.h"
24488f6682SSatya Tangirala
25488f6682SSatya Tangirala static unsigned int num_prealloc_bounce_pg = 32;
26488f6682SSatya Tangirala module_param(num_prealloc_bounce_pg, uint, 0);
27488f6682SSatya Tangirala MODULE_PARM_DESC(num_prealloc_bounce_pg,
28488f6682SSatya Tangirala "Number of preallocated bounce pages for the blk-crypto crypto API fallback");
29488f6682SSatya Tangirala
30488f6682SSatya Tangirala static unsigned int blk_crypto_num_keyslots = 100;
31488f6682SSatya Tangirala module_param_named(num_keyslots, blk_crypto_num_keyslots, uint, 0);
32488f6682SSatya Tangirala MODULE_PARM_DESC(num_keyslots,
33488f6682SSatya Tangirala "Number of keyslots for the blk-crypto crypto API fallback");
34488f6682SSatya Tangirala
35488f6682SSatya Tangirala static unsigned int num_prealloc_fallback_crypt_ctxs = 128;
36488f6682SSatya Tangirala module_param(num_prealloc_fallback_crypt_ctxs, uint, 0);
37488f6682SSatya Tangirala MODULE_PARM_DESC(num_prealloc_crypt_fallback_ctxs,
38488f6682SSatya Tangirala "Number of preallocated bio fallback crypto contexts for blk-crypto to use during crypto API fallback");
39488f6682SSatya Tangirala
40488f6682SSatya Tangirala struct bio_fallback_crypt_ctx {
41488f6682SSatya Tangirala struct bio_crypt_ctx crypt_ctx;
42488f6682SSatya Tangirala /*
43488f6682SSatya Tangirala * Copy of the bvec_iter when this bio was submitted.
44488f6682SSatya Tangirala * We only want to en/decrypt the part of the bio as described by the
45488f6682SSatya Tangirala * bvec_iter upon submission because bio might be split before being
46488f6682SSatya Tangirala * resubmitted
47488f6682SSatya Tangirala */
48488f6682SSatya Tangirala struct bvec_iter crypt_iter;
49488f6682SSatya Tangirala union {
50488f6682SSatya Tangirala struct {
51488f6682SSatya Tangirala struct work_struct work;
52488f6682SSatya Tangirala struct bio *bio;
53488f6682SSatya Tangirala };
54488f6682SSatya Tangirala struct {
55488f6682SSatya Tangirala void *bi_private_orig;
56488f6682SSatya Tangirala bio_end_io_t *bi_end_io_orig;
57488f6682SSatya Tangirala };
58488f6682SSatya Tangirala };
59488f6682SSatya Tangirala };
60488f6682SSatya Tangirala
61488f6682SSatya Tangirala static struct kmem_cache *bio_fallback_crypt_ctx_cache;
62488f6682SSatya Tangirala static mempool_t *bio_fallback_crypt_ctx_pool;
63488f6682SSatya Tangirala
64488f6682SSatya Tangirala /*
65488f6682SSatya Tangirala * Allocating a crypto tfm during I/O can deadlock, so we have to preallocate
66488f6682SSatya Tangirala * all of a mode's tfms when that mode starts being used. Since each mode may
67488f6682SSatya Tangirala * need all the keyslots at some point, each mode needs its own tfm for each
68488f6682SSatya Tangirala * keyslot; thus, a keyslot may contain tfms for multiple modes. However, to
69488f6682SSatya Tangirala * match the behavior of real inline encryption hardware (which only supports a
70488f6682SSatya Tangirala * single encryption context per keyslot), we only allow one tfm per keyslot to
71488f6682SSatya Tangirala * be used at a time - the rest of the unused tfms have their keys cleared.
72488f6682SSatya Tangirala */
73488f6682SSatya Tangirala static DEFINE_MUTEX(tfms_init_lock);
74488f6682SSatya Tangirala static bool tfms_inited[BLK_ENCRYPTION_MODE_MAX];
75488f6682SSatya Tangirala
76eebcafaeSEric Biggers static struct blk_crypto_fallback_keyslot {
77488f6682SSatya Tangirala enum blk_crypto_mode_num crypto_mode;
78488f6682SSatya Tangirala struct crypto_skcipher *tfms[BLK_ENCRYPTION_MODE_MAX];
79488f6682SSatya Tangirala } *blk_crypto_keyslots;
80488f6682SSatya Tangirala
81c984ff14SSweet Tea Dorminy static struct blk_crypto_profile *blk_crypto_fallback_profile;
82488f6682SSatya Tangirala static struct workqueue_struct *blk_crypto_wq;
83488f6682SSatya Tangirala static mempool_t *blk_crypto_bounce_page_pool;
845407334cSChristoph Hellwig static struct bio_set crypto_bio_split;
85488f6682SSatya Tangirala
86488f6682SSatya Tangirala /*
87488f6682SSatya Tangirala * This is the key we set when evicting a keyslot. This *should* be the all 0's
88488f6682SSatya Tangirala * key, but AES-XTS rejects that key, so we use some random bytes instead.
89488f6682SSatya Tangirala */
90488f6682SSatya Tangirala static u8 blank_key[BLK_CRYPTO_MAX_KEY_SIZE];
91488f6682SSatya Tangirala
blk_crypto_fallback_evict_keyslot(unsigned int slot)92eebcafaeSEric Biggers static void blk_crypto_fallback_evict_keyslot(unsigned int slot)
93488f6682SSatya Tangirala {
94eebcafaeSEric Biggers struct blk_crypto_fallback_keyslot *slotp = &blk_crypto_keyslots[slot];
95488f6682SSatya Tangirala enum blk_crypto_mode_num crypto_mode = slotp->crypto_mode;
96488f6682SSatya Tangirala int err;
97488f6682SSatya Tangirala
98488f6682SSatya Tangirala WARN_ON(slotp->crypto_mode == BLK_ENCRYPTION_MODE_INVALID);
99488f6682SSatya Tangirala
100488f6682SSatya Tangirala /* Clear the key in the skcipher */
101488f6682SSatya Tangirala err = crypto_skcipher_setkey(slotp->tfms[crypto_mode], blank_key,
102488f6682SSatya Tangirala blk_crypto_modes[crypto_mode].keysize);
103488f6682SSatya Tangirala WARN_ON(err);
104488f6682SSatya Tangirala slotp->crypto_mode = BLK_ENCRYPTION_MODE_INVALID;
105488f6682SSatya Tangirala }
106488f6682SSatya Tangirala
107cb77cb5aSEric Biggers static int
blk_crypto_fallback_keyslot_program(struct blk_crypto_profile * profile,const struct blk_crypto_key * key,unsigned int slot)108cb77cb5aSEric Biggers blk_crypto_fallback_keyslot_program(struct blk_crypto_profile *profile,
109488f6682SSatya Tangirala const struct blk_crypto_key *key,
110488f6682SSatya Tangirala unsigned int slot)
111488f6682SSatya Tangirala {
112eebcafaeSEric Biggers struct blk_crypto_fallback_keyslot *slotp = &blk_crypto_keyslots[slot];
113488f6682SSatya Tangirala const enum blk_crypto_mode_num crypto_mode =
114488f6682SSatya Tangirala key->crypto_cfg.crypto_mode;
115488f6682SSatya Tangirala int err;
116488f6682SSatya Tangirala
117488f6682SSatya Tangirala if (crypto_mode != slotp->crypto_mode &&
118488f6682SSatya Tangirala slotp->crypto_mode != BLK_ENCRYPTION_MODE_INVALID)
119eebcafaeSEric Biggers blk_crypto_fallback_evict_keyslot(slot);
120488f6682SSatya Tangirala
121488f6682SSatya Tangirala slotp->crypto_mode = crypto_mode;
122488f6682SSatya Tangirala err = crypto_skcipher_setkey(slotp->tfms[crypto_mode], key->raw,
123488f6682SSatya Tangirala key->size);
124488f6682SSatya Tangirala if (err) {
125eebcafaeSEric Biggers blk_crypto_fallback_evict_keyslot(slot);
126488f6682SSatya Tangirala return err;
127488f6682SSatya Tangirala }
128488f6682SSatya Tangirala return 0;
129488f6682SSatya Tangirala }
130488f6682SSatya Tangirala
blk_crypto_fallback_keyslot_evict(struct blk_crypto_profile * profile,const struct blk_crypto_key * key,unsigned int slot)131cb77cb5aSEric Biggers static int blk_crypto_fallback_keyslot_evict(struct blk_crypto_profile *profile,
132488f6682SSatya Tangirala const struct blk_crypto_key *key,
133488f6682SSatya Tangirala unsigned int slot)
134488f6682SSatya Tangirala {
135eebcafaeSEric Biggers blk_crypto_fallback_evict_keyslot(slot);
136488f6682SSatya Tangirala return 0;
137488f6682SSatya Tangirala }
138488f6682SSatya Tangirala
139cb77cb5aSEric Biggers static const struct blk_crypto_ll_ops blk_crypto_fallback_ll_ops = {
140eebcafaeSEric Biggers .keyslot_program = blk_crypto_fallback_keyslot_program,
141eebcafaeSEric Biggers .keyslot_evict = blk_crypto_fallback_keyslot_evict,
142488f6682SSatya Tangirala };
143488f6682SSatya Tangirala
blk_crypto_fallback_encrypt_endio(struct bio * enc_bio)144488f6682SSatya Tangirala static void blk_crypto_fallback_encrypt_endio(struct bio *enc_bio)
145488f6682SSatya Tangirala {
146488f6682SSatya Tangirala struct bio *src_bio = enc_bio->bi_private;
147488f6682SSatya Tangirala int i;
148488f6682SSatya Tangirala
149488f6682SSatya Tangirala for (i = 0; i < enc_bio->bi_vcnt; i++)
150488f6682SSatya Tangirala mempool_free(enc_bio->bi_io_vec[i].bv_page,
151488f6682SSatya Tangirala blk_crypto_bounce_page_pool);
152488f6682SSatya Tangirala
153488f6682SSatya Tangirala src_bio->bi_status = enc_bio->bi_status;
154488f6682SSatya Tangirala
155066ff571SChristoph Hellwig bio_uninit(enc_bio);
156066ff571SChristoph Hellwig kfree(enc_bio);
157488f6682SSatya Tangirala bio_endio(src_bio);
158488f6682SSatya Tangirala }
159488f6682SSatya Tangirala
blk_crypto_fallback_clone_bio(struct bio * bio_src)160eebcafaeSEric Biggers static struct bio *blk_crypto_fallback_clone_bio(struct bio *bio_src)
161488f6682SSatya Tangirala {
162066ff571SChristoph Hellwig unsigned int nr_segs = bio_segments(bio_src);
163488f6682SSatya Tangirala struct bvec_iter iter;
164488f6682SSatya Tangirala struct bio_vec bv;
165488f6682SSatya Tangirala struct bio *bio;
166488f6682SSatya Tangirala
167066ff571SChristoph Hellwig bio = bio_kmalloc(nr_segs, GFP_NOIO);
168488f6682SSatya Tangirala if (!bio)
169488f6682SSatya Tangirala return NULL;
170066ff571SChristoph Hellwig bio_init(bio, bio_src->bi_bdev, bio->bi_inline_vecs, nr_segs,
171066ff571SChristoph Hellwig bio_src->bi_opf);
17246bbf653SChristoph Hellwig if (bio_flagged(bio_src, BIO_REMAPPED))
17346bbf653SChristoph Hellwig bio_set_flag(bio, BIO_REMAPPED);
174488f6682SSatya Tangirala bio->bi_ioprio = bio_src->bi_ioprio;
175*44981351SBart Van Assche bio->bi_write_hint = bio_src->bi_write_hint;
176488f6682SSatya Tangirala bio->bi_iter.bi_sector = bio_src->bi_iter.bi_sector;
177488f6682SSatya Tangirala bio->bi_iter.bi_size = bio_src->bi_iter.bi_size;
178488f6682SSatya Tangirala
179488f6682SSatya Tangirala bio_for_each_segment(bv, bio_src, iter)
180488f6682SSatya Tangirala bio->bi_io_vec[bio->bi_vcnt++] = bv;
181488f6682SSatya Tangirala
182488f6682SSatya Tangirala bio_clone_blkg_association(bio, bio_src);
183488f6682SSatya Tangirala
184488f6682SSatya Tangirala return bio;
185488f6682SSatya Tangirala }
186488f6682SSatya Tangirala
187eebcafaeSEric Biggers static bool
blk_crypto_fallback_alloc_cipher_req(struct blk_crypto_keyslot * slot,struct skcipher_request ** ciph_req_ret,struct crypto_wait * wait)188cb77cb5aSEric Biggers blk_crypto_fallback_alloc_cipher_req(struct blk_crypto_keyslot *slot,
189488f6682SSatya Tangirala struct skcipher_request **ciph_req_ret,
190488f6682SSatya Tangirala struct crypto_wait *wait)
191488f6682SSatya Tangirala {
192488f6682SSatya Tangirala struct skcipher_request *ciph_req;
193eebcafaeSEric Biggers const struct blk_crypto_fallback_keyslot *slotp;
194cb77cb5aSEric Biggers int keyslot_idx = blk_crypto_keyslot_index(slot);
195488f6682SSatya Tangirala
196488f6682SSatya Tangirala slotp = &blk_crypto_keyslots[keyslot_idx];
197488f6682SSatya Tangirala ciph_req = skcipher_request_alloc(slotp->tfms[slotp->crypto_mode],
198488f6682SSatya Tangirala GFP_NOIO);
199488f6682SSatya Tangirala if (!ciph_req)
200488f6682SSatya Tangirala return false;
201488f6682SSatya Tangirala
202488f6682SSatya Tangirala skcipher_request_set_callback(ciph_req,
203488f6682SSatya Tangirala CRYPTO_TFM_REQ_MAY_BACKLOG |
204488f6682SSatya Tangirala CRYPTO_TFM_REQ_MAY_SLEEP,
205488f6682SSatya Tangirala crypto_req_done, wait);
206488f6682SSatya Tangirala *ciph_req_ret = ciph_req;
207488f6682SSatya Tangirala
208488f6682SSatya Tangirala return true;
209488f6682SSatya Tangirala }
210488f6682SSatya Tangirala
blk_crypto_fallback_split_bio_if_needed(struct bio ** bio_ptr)211eebcafaeSEric Biggers static bool blk_crypto_fallback_split_bio_if_needed(struct bio **bio_ptr)
212488f6682SSatya Tangirala {
213488f6682SSatya Tangirala struct bio *bio = *bio_ptr;
214488f6682SSatya Tangirala unsigned int i = 0;
215488f6682SSatya Tangirala unsigned int num_sectors = 0;
216488f6682SSatya Tangirala struct bio_vec bv;
217488f6682SSatya Tangirala struct bvec_iter iter;
218488f6682SSatya Tangirala
219488f6682SSatya Tangirala bio_for_each_segment(bv, bio, iter) {
220488f6682SSatya Tangirala num_sectors += bv.bv_len >> SECTOR_SHIFT;
221a8affc03SChristoph Hellwig if (++i == BIO_MAX_VECS)
222488f6682SSatya Tangirala break;
223488f6682SSatya Tangirala }
224488f6682SSatya Tangirala if (num_sectors < bio_sectors(bio)) {
225488f6682SSatya Tangirala struct bio *split_bio;
226488f6682SSatya Tangirala
2275407334cSChristoph Hellwig split_bio = bio_split(bio, num_sectors, GFP_NOIO,
2285407334cSChristoph Hellwig &crypto_bio_split);
229488f6682SSatya Tangirala if (!split_bio) {
230488f6682SSatya Tangirala bio->bi_status = BLK_STS_RESOURCE;
231488f6682SSatya Tangirala return false;
232488f6682SSatya Tangirala }
233488f6682SSatya Tangirala bio_chain(split_bio, bio);
234ed00aabdSChristoph Hellwig submit_bio_noacct(bio);
235488f6682SSatya Tangirala *bio_ptr = split_bio;
236488f6682SSatya Tangirala }
237488f6682SSatya Tangirala
238488f6682SSatya Tangirala return true;
239488f6682SSatya Tangirala }
240488f6682SSatya Tangirala
241488f6682SSatya Tangirala union blk_crypto_iv {
242488f6682SSatya Tangirala __le64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
243488f6682SSatya Tangirala u8 bytes[BLK_CRYPTO_MAX_IV_SIZE];
244488f6682SSatya Tangirala };
245488f6682SSatya Tangirala
blk_crypto_dun_to_iv(const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],union blk_crypto_iv * iv)246488f6682SSatya Tangirala static void blk_crypto_dun_to_iv(const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
247488f6682SSatya Tangirala union blk_crypto_iv *iv)
248488f6682SSatya Tangirala {
249488f6682SSatya Tangirala int i;
250488f6682SSatya Tangirala
251488f6682SSatya Tangirala for (i = 0; i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++)
252488f6682SSatya Tangirala iv->dun[i] = cpu_to_le64(dun[i]);
253488f6682SSatya Tangirala }
254488f6682SSatya Tangirala
255488f6682SSatya Tangirala /*
256488f6682SSatya Tangirala * The crypto API fallback's encryption routine.
257488f6682SSatya Tangirala * Allocate a bounce bio for encryption, encrypt the input bio using crypto API,
258488f6682SSatya Tangirala * and replace *bio_ptr with the bounce bio. May split input bio if it's too
259488f6682SSatya Tangirala * large. Returns true on success. Returns false and sets bio->bi_status on
260488f6682SSatya Tangirala * error.
261488f6682SSatya Tangirala */
blk_crypto_fallback_encrypt_bio(struct bio ** bio_ptr)262488f6682SSatya Tangirala static bool blk_crypto_fallback_encrypt_bio(struct bio **bio_ptr)
263488f6682SSatya Tangirala {
264488f6682SSatya Tangirala struct bio *src_bio, *enc_bio;
265488f6682SSatya Tangirala struct bio_crypt_ctx *bc;
266cb77cb5aSEric Biggers struct blk_crypto_keyslot *slot;
267488f6682SSatya Tangirala int data_unit_size;
268488f6682SSatya Tangirala struct skcipher_request *ciph_req = NULL;
269488f6682SSatya Tangirala DECLARE_CRYPTO_WAIT(wait);
270488f6682SSatya Tangirala u64 curr_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
271488f6682SSatya Tangirala struct scatterlist src, dst;
272488f6682SSatya Tangirala union blk_crypto_iv iv;
273488f6682SSatya Tangirala unsigned int i, j;
274488f6682SSatya Tangirala bool ret = false;
275488f6682SSatya Tangirala blk_status_t blk_st;
276488f6682SSatya Tangirala
277488f6682SSatya Tangirala /* Split the bio if it's too big for single page bvec */
278eebcafaeSEric Biggers if (!blk_crypto_fallback_split_bio_if_needed(bio_ptr))
279488f6682SSatya Tangirala return false;
280488f6682SSatya Tangirala
281488f6682SSatya Tangirala src_bio = *bio_ptr;
282488f6682SSatya Tangirala bc = src_bio->bi_crypt_context;
283488f6682SSatya Tangirala data_unit_size = bc->bc_key->crypto_cfg.data_unit_size;
284488f6682SSatya Tangirala
285488f6682SSatya Tangirala /* Allocate bounce bio for encryption */
286eebcafaeSEric Biggers enc_bio = blk_crypto_fallback_clone_bio(src_bio);
287488f6682SSatya Tangirala if (!enc_bio) {
288488f6682SSatya Tangirala src_bio->bi_status = BLK_STS_RESOURCE;
289488f6682SSatya Tangirala return false;
290488f6682SSatya Tangirala }
291488f6682SSatya Tangirala
292488f6682SSatya Tangirala /*
293cb77cb5aSEric Biggers * Get a blk-crypto-fallback keyslot that contains a crypto_skcipher for
294cb77cb5aSEric Biggers * this bio's algorithm and key.
295488f6682SSatya Tangirala */
296c984ff14SSweet Tea Dorminy blk_st = blk_crypto_get_keyslot(blk_crypto_fallback_profile,
297cb77cb5aSEric Biggers bc->bc_key, &slot);
298488f6682SSatya Tangirala if (blk_st != BLK_STS_OK) {
299488f6682SSatya Tangirala src_bio->bi_status = blk_st;
300488f6682SSatya Tangirala goto out_put_enc_bio;
301488f6682SSatya Tangirala }
302488f6682SSatya Tangirala
303488f6682SSatya Tangirala /* and then allocate an skcipher_request for it */
304eebcafaeSEric Biggers if (!blk_crypto_fallback_alloc_cipher_req(slot, &ciph_req, &wait)) {
305488f6682SSatya Tangirala src_bio->bi_status = BLK_STS_RESOURCE;
306488f6682SSatya Tangirala goto out_release_keyslot;
307488f6682SSatya Tangirala }
308488f6682SSatya Tangirala
309488f6682SSatya Tangirala memcpy(curr_dun, bc->bc_dun, sizeof(curr_dun));
310488f6682SSatya Tangirala sg_init_table(&src, 1);
311488f6682SSatya Tangirala sg_init_table(&dst, 1);
312488f6682SSatya Tangirala
313488f6682SSatya Tangirala skcipher_request_set_crypt(ciph_req, &src, &dst, data_unit_size,
314488f6682SSatya Tangirala iv.bytes);
315488f6682SSatya Tangirala
316488f6682SSatya Tangirala /* Encrypt each page in the bounce bio */
317488f6682SSatya Tangirala for (i = 0; i < enc_bio->bi_vcnt; i++) {
318488f6682SSatya Tangirala struct bio_vec *enc_bvec = &enc_bio->bi_io_vec[i];
319488f6682SSatya Tangirala struct page *plaintext_page = enc_bvec->bv_page;
320488f6682SSatya Tangirala struct page *ciphertext_page =
321488f6682SSatya Tangirala mempool_alloc(blk_crypto_bounce_page_pool, GFP_NOIO);
322488f6682SSatya Tangirala
323488f6682SSatya Tangirala enc_bvec->bv_page = ciphertext_page;
324488f6682SSatya Tangirala
325488f6682SSatya Tangirala if (!ciphertext_page) {
326488f6682SSatya Tangirala src_bio->bi_status = BLK_STS_RESOURCE;
327488f6682SSatya Tangirala goto out_free_bounce_pages;
328488f6682SSatya Tangirala }
329488f6682SSatya Tangirala
330488f6682SSatya Tangirala sg_set_page(&src, plaintext_page, data_unit_size,
331488f6682SSatya Tangirala enc_bvec->bv_offset);
332488f6682SSatya Tangirala sg_set_page(&dst, ciphertext_page, data_unit_size,
333488f6682SSatya Tangirala enc_bvec->bv_offset);
334488f6682SSatya Tangirala
335488f6682SSatya Tangirala /* Encrypt each data unit in this page */
336488f6682SSatya Tangirala for (j = 0; j < enc_bvec->bv_len; j += data_unit_size) {
337488f6682SSatya Tangirala blk_crypto_dun_to_iv(curr_dun, &iv);
338488f6682SSatya Tangirala if (crypto_wait_req(crypto_skcipher_encrypt(ciph_req),
339488f6682SSatya Tangirala &wait)) {
340488f6682SSatya Tangirala i++;
341488f6682SSatya Tangirala src_bio->bi_status = BLK_STS_IOERR;
342488f6682SSatya Tangirala goto out_free_bounce_pages;
343488f6682SSatya Tangirala }
344488f6682SSatya Tangirala bio_crypt_dun_increment(curr_dun, 1);
345488f6682SSatya Tangirala src.offset += data_unit_size;
346488f6682SSatya Tangirala dst.offset += data_unit_size;
347488f6682SSatya Tangirala }
348488f6682SSatya Tangirala }
349488f6682SSatya Tangirala
350488f6682SSatya Tangirala enc_bio->bi_private = src_bio;
351488f6682SSatya Tangirala enc_bio->bi_end_io = blk_crypto_fallback_encrypt_endio;
352488f6682SSatya Tangirala *bio_ptr = enc_bio;
353488f6682SSatya Tangirala ret = true;
354488f6682SSatya Tangirala
355488f6682SSatya Tangirala enc_bio = NULL;
356488f6682SSatya Tangirala goto out_free_ciph_req;
357488f6682SSatya Tangirala
358488f6682SSatya Tangirala out_free_bounce_pages:
359488f6682SSatya Tangirala while (i > 0)
360488f6682SSatya Tangirala mempool_free(enc_bio->bi_io_vec[--i].bv_page,
361488f6682SSatya Tangirala blk_crypto_bounce_page_pool);
362488f6682SSatya Tangirala out_free_ciph_req:
363488f6682SSatya Tangirala skcipher_request_free(ciph_req);
364488f6682SSatya Tangirala out_release_keyslot:
365cb77cb5aSEric Biggers blk_crypto_put_keyslot(slot);
366488f6682SSatya Tangirala out_put_enc_bio:
367488f6682SSatya Tangirala if (enc_bio)
368066ff571SChristoph Hellwig bio_uninit(enc_bio);
369066ff571SChristoph Hellwig kfree(enc_bio);
370488f6682SSatya Tangirala return ret;
371488f6682SSatya Tangirala }
372488f6682SSatya Tangirala
373488f6682SSatya Tangirala /*
374488f6682SSatya Tangirala * The crypto API fallback's main decryption routine.
375488f6682SSatya Tangirala * Decrypts input bio in place, and calls bio_endio on the bio.
376488f6682SSatya Tangirala */
blk_crypto_fallback_decrypt_bio(struct work_struct * work)377488f6682SSatya Tangirala static void blk_crypto_fallback_decrypt_bio(struct work_struct *work)
378488f6682SSatya Tangirala {
379488f6682SSatya Tangirala struct bio_fallback_crypt_ctx *f_ctx =
380488f6682SSatya Tangirala container_of(work, struct bio_fallback_crypt_ctx, work);
381488f6682SSatya Tangirala struct bio *bio = f_ctx->bio;
382488f6682SSatya Tangirala struct bio_crypt_ctx *bc = &f_ctx->crypt_ctx;
383cb77cb5aSEric Biggers struct blk_crypto_keyslot *slot;
384488f6682SSatya Tangirala struct skcipher_request *ciph_req = NULL;
385488f6682SSatya Tangirala DECLARE_CRYPTO_WAIT(wait);
386488f6682SSatya Tangirala u64 curr_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
387488f6682SSatya Tangirala union blk_crypto_iv iv;
388488f6682SSatya Tangirala struct scatterlist sg;
389488f6682SSatya Tangirala struct bio_vec bv;
390488f6682SSatya Tangirala struct bvec_iter iter;
391488f6682SSatya Tangirala const int data_unit_size = bc->bc_key->crypto_cfg.data_unit_size;
392488f6682SSatya Tangirala unsigned int i;
393488f6682SSatya Tangirala blk_status_t blk_st;
394488f6682SSatya Tangirala
395488f6682SSatya Tangirala /*
396cb77cb5aSEric Biggers * Get a blk-crypto-fallback keyslot that contains a crypto_skcipher for
397cb77cb5aSEric Biggers * this bio's algorithm and key.
398488f6682SSatya Tangirala */
399c984ff14SSweet Tea Dorminy blk_st = blk_crypto_get_keyslot(blk_crypto_fallback_profile,
400cb77cb5aSEric Biggers bc->bc_key, &slot);
401488f6682SSatya Tangirala if (blk_st != BLK_STS_OK) {
402488f6682SSatya Tangirala bio->bi_status = blk_st;
403488f6682SSatya Tangirala goto out_no_keyslot;
404488f6682SSatya Tangirala }
405488f6682SSatya Tangirala
406488f6682SSatya Tangirala /* and then allocate an skcipher_request for it */
407eebcafaeSEric Biggers if (!blk_crypto_fallback_alloc_cipher_req(slot, &ciph_req, &wait)) {
408488f6682SSatya Tangirala bio->bi_status = BLK_STS_RESOURCE;
409488f6682SSatya Tangirala goto out;
410488f6682SSatya Tangirala }
411488f6682SSatya Tangirala
412488f6682SSatya Tangirala memcpy(curr_dun, bc->bc_dun, sizeof(curr_dun));
413488f6682SSatya Tangirala sg_init_table(&sg, 1);
414488f6682SSatya Tangirala skcipher_request_set_crypt(ciph_req, &sg, &sg, data_unit_size,
415488f6682SSatya Tangirala iv.bytes);
416488f6682SSatya Tangirala
417488f6682SSatya Tangirala /* Decrypt each segment in the bio */
418488f6682SSatya Tangirala __bio_for_each_segment(bv, bio, iter, f_ctx->crypt_iter) {
419488f6682SSatya Tangirala struct page *page = bv.bv_page;
420488f6682SSatya Tangirala
421488f6682SSatya Tangirala sg_set_page(&sg, page, data_unit_size, bv.bv_offset);
422488f6682SSatya Tangirala
423488f6682SSatya Tangirala /* Decrypt each data unit in the segment */
424488f6682SSatya Tangirala for (i = 0; i < bv.bv_len; i += data_unit_size) {
425488f6682SSatya Tangirala blk_crypto_dun_to_iv(curr_dun, &iv);
426488f6682SSatya Tangirala if (crypto_wait_req(crypto_skcipher_decrypt(ciph_req),
427488f6682SSatya Tangirala &wait)) {
428488f6682SSatya Tangirala bio->bi_status = BLK_STS_IOERR;
429488f6682SSatya Tangirala goto out;
430488f6682SSatya Tangirala }
431488f6682SSatya Tangirala bio_crypt_dun_increment(curr_dun, 1);
432488f6682SSatya Tangirala sg.offset += data_unit_size;
433488f6682SSatya Tangirala }
434488f6682SSatya Tangirala }
435488f6682SSatya Tangirala
436488f6682SSatya Tangirala out:
437488f6682SSatya Tangirala skcipher_request_free(ciph_req);
438cb77cb5aSEric Biggers blk_crypto_put_keyslot(slot);
439488f6682SSatya Tangirala out_no_keyslot:
440488f6682SSatya Tangirala mempool_free(f_ctx, bio_fallback_crypt_ctx_pool);
441488f6682SSatya Tangirala bio_endio(bio);
442488f6682SSatya Tangirala }
443488f6682SSatya Tangirala
444488f6682SSatya Tangirala /**
445488f6682SSatya Tangirala * blk_crypto_fallback_decrypt_endio - queue bio for fallback decryption
446488f6682SSatya Tangirala *
447488f6682SSatya Tangirala * @bio: the bio to queue
448488f6682SSatya Tangirala *
449488f6682SSatya Tangirala * Restore bi_private and bi_end_io, and queue the bio for decryption into a
450488f6682SSatya Tangirala * workqueue, since this function will be called from an atomic context.
451488f6682SSatya Tangirala */
blk_crypto_fallback_decrypt_endio(struct bio * bio)452488f6682SSatya Tangirala static void blk_crypto_fallback_decrypt_endio(struct bio *bio)
453488f6682SSatya Tangirala {
454488f6682SSatya Tangirala struct bio_fallback_crypt_ctx *f_ctx = bio->bi_private;
455488f6682SSatya Tangirala
456488f6682SSatya Tangirala bio->bi_private = f_ctx->bi_private_orig;
457488f6682SSatya Tangirala bio->bi_end_io = f_ctx->bi_end_io_orig;
458488f6682SSatya Tangirala
459488f6682SSatya Tangirala /* If there was an IO error, don't queue for decrypt. */
460488f6682SSatya Tangirala if (bio->bi_status) {
461488f6682SSatya Tangirala mempool_free(f_ctx, bio_fallback_crypt_ctx_pool);
462488f6682SSatya Tangirala bio_endio(bio);
463488f6682SSatya Tangirala return;
464488f6682SSatya Tangirala }
465488f6682SSatya Tangirala
466488f6682SSatya Tangirala INIT_WORK(&f_ctx->work, blk_crypto_fallback_decrypt_bio);
467488f6682SSatya Tangirala f_ctx->bio = bio;
468488f6682SSatya Tangirala queue_work(blk_crypto_wq, &f_ctx->work);
469488f6682SSatya Tangirala }
470488f6682SSatya Tangirala
471488f6682SSatya Tangirala /**
472488f6682SSatya Tangirala * blk_crypto_fallback_bio_prep - Prepare a bio to use fallback en/decryption
473488f6682SSatya Tangirala *
474488f6682SSatya Tangirala * @bio_ptr: pointer to the bio to prepare
475488f6682SSatya Tangirala *
476488f6682SSatya Tangirala * If bio is doing a WRITE operation, this splits the bio into two parts if it's
477eebcafaeSEric Biggers * too big (see blk_crypto_fallback_split_bio_if_needed()). It then allocates a
478eebcafaeSEric Biggers * bounce bio for the first part, encrypts it, and updates bio_ptr to point to
479eebcafaeSEric Biggers * the bounce bio.
480488f6682SSatya Tangirala *
481488f6682SSatya Tangirala * For a READ operation, we mark the bio for decryption by using bi_private and
482488f6682SSatya Tangirala * bi_end_io.
483488f6682SSatya Tangirala *
484488f6682SSatya Tangirala * In either case, this function will make the bio look like a regular bio (i.e.
485488f6682SSatya Tangirala * as if no encryption context was ever specified) for the purposes of the rest
486488f6682SSatya Tangirala * of the stack except for blk-integrity (blk-integrity and blk-crypto are not
487488f6682SSatya Tangirala * currently supported together).
488488f6682SSatya Tangirala *
489488f6682SSatya Tangirala * Return: true on success. Sets bio->bi_status and returns false on error.
490488f6682SSatya Tangirala */
blk_crypto_fallback_bio_prep(struct bio ** bio_ptr)491488f6682SSatya Tangirala bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr)
492488f6682SSatya Tangirala {
493488f6682SSatya Tangirala struct bio *bio = *bio_ptr;
494488f6682SSatya Tangirala struct bio_crypt_ctx *bc = bio->bi_crypt_context;
495488f6682SSatya Tangirala struct bio_fallback_crypt_ctx *f_ctx;
496488f6682SSatya Tangirala
497488f6682SSatya Tangirala if (WARN_ON_ONCE(!tfms_inited[bc->bc_key->crypto_cfg.crypto_mode])) {
498488f6682SSatya Tangirala /* User didn't call blk_crypto_start_using_key() first */
499488f6682SSatya Tangirala bio->bi_status = BLK_STS_IOERR;
500488f6682SSatya Tangirala return false;
501488f6682SSatya Tangirala }
502488f6682SSatya Tangirala
503c984ff14SSweet Tea Dorminy if (!__blk_crypto_cfg_supported(blk_crypto_fallback_profile,
504488f6682SSatya Tangirala &bc->bc_key->crypto_cfg)) {
505488f6682SSatya Tangirala bio->bi_status = BLK_STS_NOTSUPP;
506488f6682SSatya Tangirala return false;
507488f6682SSatya Tangirala }
508488f6682SSatya Tangirala
509488f6682SSatya Tangirala if (bio_data_dir(bio) == WRITE)
510488f6682SSatya Tangirala return blk_crypto_fallback_encrypt_bio(bio_ptr);
511488f6682SSatya Tangirala
512488f6682SSatya Tangirala /*
513488f6682SSatya Tangirala * bio READ case: Set up a f_ctx in the bio's bi_private and set the
514488f6682SSatya Tangirala * bi_end_io appropriately to trigger decryption when the bio is ended.
515488f6682SSatya Tangirala */
516488f6682SSatya Tangirala f_ctx = mempool_alloc(bio_fallback_crypt_ctx_pool, GFP_NOIO);
517488f6682SSatya Tangirala f_ctx->crypt_ctx = *bc;
518488f6682SSatya Tangirala f_ctx->crypt_iter = bio->bi_iter;
519488f6682SSatya Tangirala f_ctx->bi_private_orig = bio->bi_private;
520488f6682SSatya Tangirala f_ctx->bi_end_io_orig = bio->bi_end_io;
521488f6682SSatya Tangirala bio->bi_private = (void *)f_ctx;
522488f6682SSatya Tangirala bio->bi_end_io = blk_crypto_fallback_decrypt_endio;
523488f6682SSatya Tangirala bio_crypt_free_ctx(bio);
524488f6682SSatya Tangirala
525488f6682SSatya Tangirala return true;
526488f6682SSatya Tangirala }
527488f6682SSatya Tangirala
blk_crypto_fallback_evict_key(const struct blk_crypto_key * key)528488f6682SSatya Tangirala int blk_crypto_fallback_evict_key(const struct blk_crypto_key *key)
529488f6682SSatya Tangirala {
530c984ff14SSweet Tea Dorminy return __blk_crypto_evict_key(blk_crypto_fallback_profile, key);
531488f6682SSatya Tangirala }
532488f6682SSatya Tangirala
533488f6682SSatya Tangirala static bool blk_crypto_fallback_inited;
blk_crypto_fallback_init(void)534488f6682SSatya Tangirala static int blk_crypto_fallback_init(void)
535488f6682SSatya Tangirala {
536488f6682SSatya Tangirala int i;
537e7ecc142SColin Ian King int err;
538488f6682SSatya Tangirala
539488f6682SSatya Tangirala if (blk_crypto_fallback_inited)
540488f6682SSatya Tangirala return 0;
541488f6682SSatya Tangirala
542197173dbSJason A. Donenfeld get_random_bytes(blank_key, BLK_CRYPTO_MAX_KEY_SIZE);
543488f6682SSatya Tangirala
5445407334cSChristoph Hellwig err = bioset_init(&crypto_bio_split, 64, 0, 0);
545488f6682SSatya Tangirala if (err)
546488f6682SSatya Tangirala goto out;
5475407334cSChristoph Hellwig
548c984ff14SSweet Tea Dorminy /* Dynamic allocation is needed because of lockdep_register_key(). */
549c984ff14SSweet Tea Dorminy blk_crypto_fallback_profile =
550c984ff14SSweet Tea Dorminy kzalloc(sizeof(*blk_crypto_fallback_profile), GFP_KERNEL);
551c984ff14SSweet Tea Dorminy if (!blk_crypto_fallback_profile) {
552c984ff14SSweet Tea Dorminy err = -ENOMEM;
5535407334cSChristoph Hellwig goto fail_free_bioset;
554c984ff14SSweet Tea Dorminy }
555c984ff14SSweet Tea Dorminy
556c984ff14SSweet Tea Dorminy err = blk_crypto_profile_init(blk_crypto_fallback_profile,
557c984ff14SSweet Tea Dorminy blk_crypto_num_keyslots);
558c984ff14SSweet Tea Dorminy if (err)
559c984ff14SSweet Tea Dorminy goto fail_free_profile;
560488f6682SSatya Tangirala err = -ENOMEM;
561488f6682SSatya Tangirala
562c984ff14SSweet Tea Dorminy blk_crypto_fallback_profile->ll_ops = blk_crypto_fallback_ll_ops;
563c984ff14SSweet Tea Dorminy blk_crypto_fallback_profile->max_dun_bytes_supported = BLK_CRYPTO_MAX_IV_SIZE;
564488f6682SSatya Tangirala
565488f6682SSatya Tangirala /* All blk-crypto modes have a crypto API fallback. */
566488f6682SSatya Tangirala for (i = 0; i < BLK_ENCRYPTION_MODE_MAX; i++)
567c984ff14SSweet Tea Dorminy blk_crypto_fallback_profile->modes_supported[i] = 0xFFFFFFFF;
568c984ff14SSweet Tea Dorminy blk_crypto_fallback_profile->modes_supported[BLK_ENCRYPTION_MODE_INVALID] = 0;
569488f6682SSatya Tangirala
570488f6682SSatya Tangirala blk_crypto_wq = alloc_workqueue("blk_crypto_wq",
571488f6682SSatya Tangirala WQ_UNBOUND | WQ_HIGHPRI |
572488f6682SSatya Tangirala WQ_MEM_RECLAIM, num_online_cpus());
573488f6682SSatya Tangirala if (!blk_crypto_wq)
574cb77cb5aSEric Biggers goto fail_destroy_profile;
575488f6682SSatya Tangirala
576488f6682SSatya Tangirala blk_crypto_keyslots = kcalloc(blk_crypto_num_keyslots,
577488f6682SSatya Tangirala sizeof(blk_crypto_keyslots[0]),
578488f6682SSatya Tangirala GFP_KERNEL);
579488f6682SSatya Tangirala if (!blk_crypto_keyslots)
580488f6682SSatya Tangirala goto fail_free_wq;
581488f6682SSatya Tangirala
582488f6682SSatya Tangirala blk_crypto_bounce_page_pool =
583488f6682SSatya Tangirala mempool_create_page_pool(num_prealloc_bounce_pg, 0);
584488f6682SSatya Tangirala if (!blk_crypto_bounce_page_pool)
585488f6682SSatya Tangirala goto fail_free_keyslots;
586488f6682SSatya Tangirala
587488f6682SSatya Tangirala bio_fallback_crypt_ctx_cache = KMEM_CACHE(bio_fallback_crypt_ctx, 0);
588488f6682SSatya Tangirala if (!bio_fallback_crypt_ctx_cache)
589488f6682SSatya Tangirala goto fail_free_bounce_page_pool;
590488f6682SSatya Tangirala
591488f6682SSatya Tangirala bio_fallback_crypt_ctx_pool =
592488f6682SSatya Tangirala mempool_create_slab_pool(num_prealloc_fallback_crypt_ctxs,
593488f6682SSatya Tangirala bio_fallback_crypt_ctx_cache);
594488f6682SSatya Tangirala if (!bio_fallback_crypt_ctx_pool)
595488f6682SSatya Tangirala goto fail_free_crypt_ctx_cache;
596488f6682SSatya Tangirala
597488f6682SSatya Tangirala blk_crypto_fallback_inited = true;
598488f6682SSatya Tangirala
599488f6682SSatya Tangirala return 0;
600488f6682SSatya Tangirala fail_free_crypt_ctx_cache:
601488f6682SSatya Tangirala kmem_cache_destroy(bio_fallback_crypt_ctx_cache);
602488f6682SSatya Tangirala fail_free_bounce_page_pool:
603488f6682SSatya Tangirala mempool_destroy(blk_crypto_bounce_page_pool);
604488f6682SSatya Tangirala fail_free_keyslots:
605488f6682SSatya Tangirala kfree(blk_crypto_keyslots);
606488f6682SSatya Tangirala fail_free_wq:
607488f6682SSatya Tangirala destroy_workqueue(blk_crypto_wq);
608cb77cb5aSEric Biggers fail_destroy_profile:
609c984ff14SSweet Tea Dorminy blk_crypto_profile_destroy(blk_crypto_fallback_profile);
610c984ff14SSweet Tea Dorminy fail_free_profile:
611c984ff14SSweet Tea Dorminy kfree(blk_crypto_fallback_profile);
6125407334cSChristoph Hellwig fail_free_bioset:
6135407334cSChristoph Hellwig bioset_exit(&crypto_bio_split);
614488f6682SSatya Tangirala out:
615488f6682SSatya Tangirala return err;
616488f6682SSatya Tangirala }
617488f6682SSatya Tangirala
618488f6682SSatya Tangirala /*
619488f6682SSatya Tangirala * Prepare blk-crypto-fallback for the specified crypto mode.
620488f6682SSatya Tangirala * Returns -ENOPKG if the needed crypto API support is missing.
621488f6682SSatya Tangirala */
blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num)622488f6682SSatya Tangirala int blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num)
623488f6682SSatya Tangirala {
624488f6682SSatya Tangirala const char *cipher_str = blk_crypto_modes[mode_num].cipher_str;
625eebcafaeSEric Biggers struct blk_crypto_fallback_keyslot *slotp;
626488f6682SSatya Tangirala unsigned int i;
627488f6682SSatya Tangirala int err = 0;
628488f6682SSatya Tangirala
629488f6682SSatya Tangirala /*
630488f6682SSatya Tangirala * Fast path
631488f6682SSatya Tangirala * Ensure that updates to blk_crypto_keyslots[i].tfms[mode_num]
632488f6682SSatya Tangirala * for each i are visible before we try to access them.
633488f6682SSatya Tangirala */
634488f6682SSatya Tangirala if (likely(smp_load_acquire(&tfms_inited[mode_num])))
635488f6682SSatya Tangirala return 0;
636488f6682SSatya Tangirala
637488f6682SSatya Tangirala mutex_lock(&tfms_init_lock);
638488f6682SSatya Tangirala if (tfms_inited[mode_num])
639488f6682SSatya Tangirala goto out;
640488f6682SSatya Tangirala
641488f6682SSatya Tangirala err = blk_crypto_fallback_init();
642488f6682SSatya Tangirala if (err)
643488f6682SSatya Tangirala goto out;
644488f6682SSatya Tangirala
645488f6682SSatya Tangirala for (i = 0; i < blk_crypto_num_keyslots; i++) {
646488f6682SSatya Tangirala slotp = &blk_crypto_keyslots[i];
647488f6682SSatya Tangirala slotp->tfms[mode_num] = crypto_alloc_skcipher(cipher_str, 0, 0);
648488f6682SSatya Tangirala if (IS_ERR(slotp->tfms[mode_num])) {
649488f6682SSatya Tangirala err = PTR_ERR(slotp->tfms[mode_num]);
650488f6682SSatya Tangirala if (err == -ENOENT) {
651488f6682SSatya Tangirala pr_warn_once("Missing crypto API support for \"%s\"\n",
652488f6682SSatya Tangirala cipher_str);
653488f6682SSatya Tangirala err = -ENOPKG;
654488f6682SSatya Tangirala }
655488f6682SSatya Tangirala slotp->tfms[mode_num] = NULL;
656488f6682SSatya Tangirala goto out_free_tfms;
657488f6682SSatya Tangirala }
658488f6682SSatya Tangirala
659488f6682SSatya Tangirala crypto_skcipher_set_flags(slotp->tfms[mode_num],
660488f6682SSatya Tangirala CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
661488f6682SSatya Tangirala }
662488f6682SSatya Tangirala
663488f6682SSatya Tangirala /*
664488f6682SSatya Tangirala * Ensure that updates to blk_crypto_keyslots[i].tfms[mode_num]
665488f6682SSatya Tangirala * for each i are visible before we set tfms_inited[mode_num].
666488f6682SSatya Tangirala */
667488f6682SSatya Tangirala smp_store_release(&tfms_inited[mode_num], true);
668488f6682SSatya Tangirala goto out;
669488f6682SSatya Tangirala
670488f6682SSatya Tangirala out_free_tfms:
671488f6682SSatya Tangirala for (i = 0; i < blk_crypto_num_keyslots; i++) {
672488f6682SSatya Tangirala slotp = &blk_crypto_keyslots[i];
673488f6682SSatya Tangirala crypto_free_skcipher(slotp->tfms[mode_num]);
674488f6682SSatya Tangirala slotp->tfms[mode_num] = NULL;
675488f6682SSatya Tangirala }
676488f6682SSatya Tangirala out:
677488f6682SSatya Tangirala mutex_unlock(&tfms_init_lock);
678488f6682SSatya Tangirala return err;
679488f6682SSatya Tangirala }
680