xref: /openbmc/linux/block/blk-crypto.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
1a892c8d5SSatya Tangirala // SPDX-License-Identifier: GPL-2.0
2a892c8d5SSatya Tangirala /*
3a892c8d5SSatya Tangirala  * Copyright 2019 Google LLC
4a892c8d5SSatya Tangirala  */
5a892c8d5SSatya Tangirala 
6a892c8d5SSatya Tangirala /*
7a892c8d5SSatya Tangirala  * Refer to Documentation/block/inline-encryption.rst for detailed explanation.
8a892c8d5SSatya Tangirala  */
9a892c8d5SSatya Tangirala 
10a892c8d5SSatya Tangirala #define pr_fmt(fmt) "blk-crypto: " fmt
11a892c8d5SSatya Tangirala 
12a892c8d5SSatya Tangirala #include <linux/bio.h>
13a892c8d5SSatya Tangirala #include <linux/blkdev.h>
141e8d44bdSEric Biggers #include <linux/blk-crypto-profile.h>
15a892c8d5SSatya Tangirala #include <linux/module.h>
1670493a63SEric Biggers #include <linux/ratelimit.h>
17a892c8d5SSatya Tangirala #include <linux/slab.h>
18a892c8d5SSatya Tangirala 
19a892c8d5SSatya Tangirala #include "blk-crypto-internal.h"
20a892c8d5SSatya Tangirala 
21a892c8d5SSatya Tangirala const struct blk_crypto_mode blk_crypto_modes[] = {
22a892c8d5SSatya Tangirala 	[BLK_ENCRYPTION_MODE_AES_256_XTS] = {
2320f01f16SEric Biggers 		.name = "AES-256-XTS",
24488f6682SSatya Tangirala 		.cipher_str = "xts(aes)",
25a892c8d5SSatya Tangirala 		.keysize = 64,
26a892c8d5SSatya Tangirala 		.ivsize = 16,
27a892c8d5SSatya Tangirala 	},
28a892c8d5SSatya Tangirala 	[BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV] = {
2920f01f16SEric Biggers 		.name = "AES-128-CBC-ESSIV",
30488f6682SSatya Tangirala 		.cipher_str = "essiv(cbc(aes),sha256)",
31a892c8d5SSatya Tangirala 		.keysize = 16,
32a892c8d5SSatya Tangirala 		.ivsize = 16,
33a892c8d5SSatya Tangirala 	},
34a892c8d5SSatya Tangirala 	[BLK_ENCRYPTION_MODE_ADIANTUM] = {
3520f01f16SEric Biggers 		.name = "Adiantum",
36488f6682SSatya Tangirala 		.cipher_str = "adiantum(xchacha12,aes)",
37a892c8d5SSatya Tangirala 		.keysize = 32,
38a892c8d5SSatya Tangirala 		.ivsize = 32,
39a892c8d5SSatya Tangirala 	},
40d209ce35STianjia Zhang 	[BLK_ENCRYPTION_MODE_SM4_XTS] = {
41d209ce35STianjia Zhang 		.name = "SM4-XTS",
42d209ce35STianjia Zhang 		.cipher_str = "xts(sm4)",
43d209ce35STianjia Zhang 		.keysize = 32,
44d209ce35STianjia Zhang 		.ivsize = 16,
45d209ce35STianjia Zhang 	},
46a892c8d5SSatya Tangirala };
47a892c8d5SSatya Tangirala 
48a892c8d5SSatya Tangirala /*
49a892c8d5SSatya Tangirala  * This number needs to be at least (the number of threads doing IO
50a892c8d5SSatya Tangirala  * concurrently) * (maximum recursive depth of a bio), so that we don't
51a892c8d5SSatya Tangirala  * deadlock on crypt_ctx allocations. The default is chosen to be the same
52a892c8d5SSatya Tangirala  * as the default number of post read contexts in both EXT4 and F2FS.
53a892c8d5SSatya Tangirala  */
54a892c8d5SSatya Tangirala static int num_prealloc_crypt_ctxs = 128;
55a892c8d5SSatya Tangirala 
56a892c8d5SSatya Tangirala module_param(num_prealloc_crypt_ctxs, int, 0444);
57a892c8d5SSatya Tangirala MODULE_PARM_DESC(num_prealloc_crypt_ctxs,
58a892c8d5SSatya Tangirala 		"Number of bio crypto contexts to preallocate");
59a892c8d5SSatya Tangirala 
60a892c8d5SSatya Tangirala static struct kmem_cache *bio_crypt_ctx_cache;
61a892c8d5SSatya Tangirala static mempool_t *bio_crypt_ctx_pool;
62a892c8d5SSatya Tangirala 
bio_crypt_ctx_init(void)63a892c8d5SSatya Tangirala static int __init bio_crypt_ctx_init(void)
64a892c8d5SSatya Tangirala {
65a892c8d5SSatya Tangirala 	size_t i;
66a892c8d5SSatya Tangirala 
67a892c8d5SSatya Tangirala 	bio_crypt_ctx_cache = KMEM_CACHE(bio_crypt_ctx, 0);
68a892c8d5SSatya Tangirala 	if (!bio_crypt_ctx_cache)
69a892c8d5SSatya Tangirala 		goto out_no_mem;
70a892c8d5SSatya Tangirala 
71a892c8d5SSatya Tangirala 	bio_crypt_ctx_pool = mempool_create_slab_pool(num_prealloc_crypt_ctxs,
72a892c8d5SSatya Tangirala 						      bio_crypt_ctx_cache);
73a892c8d5SSatya Tangirala 	if (!bio_crypt_ctx_pool)
74a892c8d5SSatya Tangirala 		goto out_no_mem;
75a892c8d5SSatya Tangirala 
76a892c8d5SSatya Tangirala 	/* This is assumed in various places. */
77a892c8d5SSatya Tangirala 	BUILD_BUG_ON(BLK_ENCRYPTION_MODE_INVALID != 0);
78a892c8d5SSatya Tangirala 
79a892c8d5SSatya Tangirala 	/* Sanity check that no algorithm exceeds the defined limits. */
80a892c8d5SSatya Tangirala 	for (i = 0; i < BLK_ENCRYPTION_MODE_MAX; i++) {
81a892c8d5SSatya Tangirala 		BUG_ON(blk_crypto_modes[i].keysize > BLK_CRYPTO_MAX_KEY_SIZE);
82a892c8d5SSatya Tangirala 		BUG_ON(blk_crypto_modes[i].ivsize > BLK_CRYPTO_MAX_IV_SIZE);
83a892c8d5SSatya Tangirala 	}
84a892c8d5SSatya Tangirala 
85a892c8d5SSatya Tangirala 	return 0;
86a892c8d5SSatya Tangirala out_no_mem:
87a892c8d5SSatya Tangirala 	panic("Failed to allocate mem for bio crypt ctxs\n");
88a892c8d5SSatya Tangirala }
89a892c8d5SSatya Tangirala subsys_initcall(bio_crypt_ctx_init);
90a892c8d5SSatya Tangirala 
bio_crypt_set_ctx(struct bio * bio,const struct blk_crypto_key * key,const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],gfp_t gfp_mask)91a892c8d5SSatya Tangirala void bio_crypt_set_ctx(struct bio *bio, const struct blk_crypto_key *key,
92a892c8d5SSatya Tangirala 		       const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE], gfp_t gfp_mask)
93a892c8d5SSatya Tangirala {
94cf785af1SEric Biggers 	struct bio_crypt_ctx *bc;
95cf785af1SEric Biggers 
96cf785af1SEric Biggers 	/*
97cf785af1SEric Biggers 	 * The caller must use a gfp_mask that contains __GFP_DIRECT_RECLAIM so
98cf785af1SEric Biggers 	 * that the mempool_alloc() can't fail.
99cf785af1SEric Biggers 	 */
100cf785af1SEric Biggers 	WARN_ON_ONCE(!(gfp_mask & __GFP_DIRECT_RECLAIM));
101cf785af1SEric Biggers 
102cf785af1SEric Biggers 	bc = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
103a892c8d5SSatya Tangirala 
104a892c8d5SSatya Tangirala 	bc->bc_key = key;
105a892c8d5SSatya Tangirala 	memcpy(bc->bc_dun, dun, sizeof(bc->bc_dun));
106a892c8d5SSatya Tangirala 
107a892c8d5SSatya Tangirala 	bio->bi_crypt_context = bc;
108a892c8d5SSatya Tangirala }
109a892c8d5SSatya Tangirala 
__bio_crypt_free_ctx(struct bio * bio)110a892c8d5SSatya Tangirala void __bio_crypt_free_ctx(struct bio *bio)
111a892c8d5SSatya Tangirala {
112a892c8d5SSatya Tangirala 	mempool_free(bio->bi_crypt_context, bio_crypt_ctx_pool);
113a892c8d5SSatya Tangirala 	bio->bi_crypt_context = NULL;
114a892c8d5SSatya Tangirala }
115a892c8d5SSatya Tangirala 
__bio_crypt_clone(struct bio * dst,struct bio * src,gfp_t gfp_mask)11607560151SEric Biggers int __bio_crypt_clone(struct bio *dst, struct bio *src, gfp_t gfp_mask)
117a892c8d5SSatya Tangirala {
118a892c8d5SSatya Tangirala 	dst->bi_crypt_context = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
11907560151SEric Biggers 	if (!dst->bi_crypt_context)
12007560151SEric Biggers 		return -ENOMEM;
121a892c8d5SSatya Tangirala 	*dst->bi_crypt_context = *src->bi_crypt_context;
12207560151SEric Biggers 	return 0;
123a892c8d5SSatya Tangirala }
124a892c8d5SSatya Tangirala 
125a892c8d5SSatya Tangirala /* Increments @dun by @inc, treating @dun as a multi-limb integer. */
bio_crypt_dun_increment(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],unsigned int inc)126a892c8d5SSatya Tangirala void bio_crypt_dun_increment(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
127a892c8d5SSatya Tangirala 			     unsigned int inc)
128a892c8d5SSatya Tangirala {
129a892c8d5SSatya Tangirala 	int i;
130a892c8d5SSatya Tangirala 
131a892c8d5SSatya Tangirala 	for (i = 0; inc && i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) {
132a892c8d5SSatya Tangirala 		dun[i] += inc;
133a892c8d5SSatya Tangirala 		/*
134a892c8d5SSatya Tangirala 		 * If the addition in this limb overflowed, then we need to
135a892c8d5SSatya Tangirala 		 * carry 1 into the next limb. Else the carry is 0.
136a892c8d5SSatya Tangirala 		 */
137a892c8d5SSatya Tangirala 		if (dun[i] < inc)
138a892c8d5SSatya Tangirala 			inc = 1;
139a892c8d5SSatya Tangirala 		else
140a892c8d5SSatya Tangirala 			inc = 0;
141a892c8d5SSatya Tangirala 	}
142a892c8d5SSatya Tangirala }
143a892c8d5SSatya Tangirala 
__bio_crypt_advance(struct bio * bio,unsigned int bytes)144a892c8d5SSatya Tangirala void __bio_crypt_advance(struct bio *bio, unsigned int bytes)
145a892c8d5SSatya Tangirala {
146a892c8d5SSatya Tangirala 	struct bio_crypt_ctx *bc = bio->bi_crypt_context;
147a892c8d5SSatya Tangirala 
148a892c8d5SSatya Tangirala 	bio_crypt_dun_increment(bc->bc_dun,
149a892c8d5SSatya Tangirala 				bytes >> bc->bc_key->data_unit_size_bits);
150a892c8d5SSatya Tangirala }
151a892c8d5SSatya Tangirala 
152a892c8d5SSatya Tangirala /*
153a892c8d5SSatya Tangirala  * Returns true if @bc->bc_dun plus @bytes converted to data units is equal to
154a892c8d5SSatya Tangirala  * @next_dun, treating the DUNs as multi-limb integers.
155a892c8d5SSatya Tangirala  */
bio_crypt_dun_is_contiguous(const struct bio_crypt_ctx * bc,unsigned int bytes,const u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE])156a892c8d5SSatya Tangirala bool bio_crypt_dun_is_contiguous(const struct bio_crypt_ctx *bc,
157a892c8d5SSatya Tangirala 				 unsigned int bytes,
158a892c8d5SSatya Tangirala 				 const u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE])
159a892c8d5SSatya Tangirala {
160a892c8d5SSatya Tangirala 	int i;
161a892c8d5SSatya Tangirala 	unsigned int carry = bytes >> bc->bc_key->data_unit_size_bits;
162a892c8d5SSatya Tangirala 
163a892c8d5SSatya Tangirala 	for (i = 0; i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) {
164a892c8d5SSatya Tangirala 		if (bc->bc_dun[i] + carry != next_dun[i])
165a892c8d5SSatya Tangirala 			return false;
166a892c8d5SSatya Tangirala 		/*
167a892c8d5SSatya Tangirala 		 * If the addition in this limb overflowed, then we need to
168a892c8d5SSatya Tangirala 		 * carry 1 into the next limb. Else the carry is 0.
169a892c8d5SSatya Tangirala 		 */
170a892c8d5SSatya Tangirala 		if ((bc->bc_dun[i] + carry) < carry)
171a892c8d5SSatya Tangirala 			carry = 1;
172a892c8d5SSatya Tangirala 		else
173a892c8d5SSatya Tangirala 			carry = 0;
174a892c8d5SSatya Tangirala 	}
175a892c8d5SSatya Tangirala 
176a892c8d5SSatya Tangirala 	/* If the DUN wrapped through 0, don't treat it as contiguous. */
177a892c8d5SSatya Tangirala 	return carry == 0;
178a892c8d5SSatya Tangirala }
179a892c8d5SSatya Tangirala 
180a892c8d5SSatya Tangirala /*
181a892c8d5SSatya Tangirala  * Checks that two bio crypt contexts are compatible - i.e. that
182a892c8d5SSatya Tangirala  * they are mergeable except for data_unit_num continuity.
183a892c8d5SSatya Tangirala  */
bio_crypt_ctx_compatible(struct bio_crypt_ctx * bc1,struct bio_crypt_ctx * bc2)184a892c8d5SSatya Tangirala static bool bio_crypt_ctx_compatible(struct bio_crypt_ctx *bc1,
185a892c8d5SSatya Tangirala 				     struct bio_crypt_ctx *bc2)
186a892c8d5SSatya Tangirala {
187a892c8d5SSatya Tangirala 	if (!bc1)
188a892c8d5SSatya Tangirala 		return !bc2;
189a892c8d5SSatya Tangirala 
190a892c8d5SSatya Tangirala 	return bc2 && bc1->bc_key == bc2->bc_key;
191a892c8d5SSatya Tangirala }
192a892c8d5SSatya Tangirala 
bio_crypt_rq_ctx_compatible(struct request * rq,struct bio * bio)193a892c8d5SSatya Tangirala bool bio_crypt_rq_ctx_compatible(struct request *rq, struct bio *bio)
194a892c8d5SSatya Tangirala {
195a892c8d5SSatya Tangirala 	return bio_crypt_ctx_compatible(rq->crypt_ctx, bio->bi_crypt_context);
196a892c8d5SSatya Tangirala }
197a892c8d5SSatya Tangirala 
198a892c8d5SSatya Tangirala /*
199a892c8d5SSatya Tangirala  * Checks that two bio crypt contexts are compatible, and also
200a892c8d5SSatya Tangirala  * that their data_unit_nums are continuous (and can hence be merged)
201a892c8d5SSatya Tangirala  * in the order @bc1 followed by @bc2.
202a892c8d5SSatya Tangirala  */
bio_crypt_ctx_mergeable(struct bio_crypt_ctx * bc1,unsigned int bc1_bytes,struct bio_crypt_ctx * bc2)203a892c8d5SSatya Tangirala bool bio_crypt_ctx_mergeable(struct bio_crypt_ctx *bc1, unsigned int bc1_bytes,
204a892c8d5SSatya Tangirala 			     struct bio_crypt_ctx *bc2)
205a892c8d5SSatya Tangirala {
206a892c8d5SSatya Tangirala 	if (!bio_crypt_ctx_compatible(bc1, bc2))
207a892c8d5SSatya Tangirala 		return false;
208a892c8d5SSatya Tangirala 
209a892c8d5SSatya Tangirala 	return !bc1 || bio_crypt_dun_is_contiguous(bc1, bc1_bytes, bc2->bc_dun);
210a892c8d5SSatya Tangirala }
211a892c8d5SSatya Tangirala 
212a892c8d5SSatya Tangirala /* Check that all I/O segments are data unit aligned. */
bio_crypt_check_alignment(struct bio * bio)213a892c8d5SSatya Tangirala static bool bio_crypt_check_alignment(struct bio *bio)
214a892c8d5SSatya Tangirala {
215a892c8d5SSatya Tangirala 	const unsigned int data_unit_size =
216a892c8d5SSatya Tangirala 		bio->bi_crypt_context->bc_key->crypto_cfg.data_unit_size;
217a892c8d5SSatya Tangirala 	struct bvec_iter iter;
218a892c8d5SSatya Tangirala 	struct bio_vec bv;
219a892c8d5SSatya Tangirala 
220a892c8d5SSatya Tangirala 	bio_for_each_segment(bv, bio, iter) {
221a892c8d5SSatya Tangirala 		if (!IS_ALIGNED(bv.bv_len | bv.bv_offset, data_unit_size))
222a892c8d5SSatya Tangirala 			return false;
223a892c8d5SSatya Tangirala 	}
224a892c8d5SSatya Tangirala 
225a892c8d5SSatya Tangirala 	return true;
226a892c8d5SSatya Tangirala }
227a892c8d5SSatya Tangirala 
__blk_crypto_rq_get_keyslot(struct request * rq)2289cd1e566SEric Biggers blk_status_t __blk_crypto_rq_get_keyslot(struct request *rq)
229a892c8d5SSatya Tangirala {
230cb77cb5aSEric Biggers 	return blk_crypto_get_keyslot(rq->q->crypto_profile,
231cb77cb5aSEric Biggers 				      rq->crypt_ctx->bc_key,
232a892c8d5SSatya Tangirala 				      &rq->crypt_keyslot);
233a892c8d5SSatya Tangirala }
234a892c8d5SSatya Tangirala 
__blk_crypto_rq_put_keyslot(struct request * rq)2359cd1e566SEric Biggers void __blk_crypto_rq_put_keyslot(struct request *rq)
236a892c8d5SSatya Tangirala {
237cb77cb5aSEric Biggers 	blk_crypto_put_keyslot(rq->crypt_keyslot);
2389cd1e566SEric Biggers 	rq->crypt_keyslot = NULL;
2399cd1e566SEric Biggers }
2409cd1e566SEric Biggers 
__blk_crypto_free_request(struct request * rq)2419cd1e566SEric Biggers void __blk_crypto_free_request(struct request *rq)
2429cd1e566SEric Biggers {
2439cd1e566SEric Biggers 	/* The keyslot, if one was needed, should have been released earlier. */
2449cd1e566SEric Biggers 	if (WARN_ON_ONCE(rq->crypt_keyslot))
2459cd1e566SEric Biggers 		__blk_crypto_rq_put_keyslot(rq);
2469cd1e566SEric Biggers 
247a892c8d5SSatya Tangirala 	mempool_free(rq->crypt_ctx, bio_crypt_ctx_pool);
2489cd1e566SEric Biggers 	rq->crypt_ctx = NULL;
249a892c8d5SSatya Tangirala }
250a892c8d5SSatya Tangirala 
251a892c8d5SSatya Tangirala /**
252a892c8d5SSatya Tangirala  * __blk_crypto_bio_prep - Prepare bio for inline encryption
253a892c8d5SSatya Tangirala  *
254a892c8d5SSatya Tangirala  * @bio_ptr: pointer to original bio pointer
255a892c8d5SSatya Tangirala  *
256488f6682SSatya Tangirala  * If the bio crypt context provided for the bio is supported by the underlying
257488f6682SSatya Tangirala  * device's inline encryption hardware, do nothing.
258488f6682SSatya Tangirala  *
259488f6682SSatya Tangirala  * Otherwise, try to perform en/decryption for this bio by falling back to the
260488f6682SSatya Tangirala  * kernel crypto API. When the crypto API fallback is used for encryption,
261488f6682SSatya Tangirala  * blk-crypto may choose to split the bio into 2 - the first one that will
262488f6682SSatya Tangirala  * continue to be processed and the second one that will be resubmitted via
263ed00aabdSChristoph Hellwig  * submit_bio_noacct. A bounce bio will be allocated to encrypt the contents
264488f6682SSatya Tangirala  * of the aforementioned "first one", and *bio_ptr will be updated to this
265488f6682SSatya Tangirala  * bounce bio.
266a892c8d5SSatya Tangirala  *
267a892c8d5SSatya Tangirala  * Caller must ensure bio has bio_crypt_ctx.
268a892c8d5SSatya Tangirala  *
269a892c8d5SSatya Tangirala  * Return: true on success; false on error (and bio->bi_status will be set
270a892c8d5SSatya Tangirala  *	   appropriately, and bio_endio() will have been called so bio
271a892c8d5SSatya Tangirala  *	   submission should abort).
272a892c8d5SSatya Tangirala  */
__blk_crypto_bio_prep(struct bio ** bio_ptr)273a892c8d5SSatya Tangirala bool __blk_crypto_bio_prep(struct bio **bio_ptr)
274a892c8d5SSatya Tangirala {
275a892c8d5SSatya Tangirala 	struct bio *bio = *bio_ptr;
276a892c8d5SSatya Tangirala 	const struct blk_crypto_key *bc_key = bio->bi_crypt_context->bc_key;
277a892c8d5SSatya Tangirala 
278a892c8d5SSatya Tangirala 	/* Error if bio has no data. */
279488f6682SSatya Tangirala 	if (WARN_ON_ONCE(!bio_has_data(bio))) {
280488f6682SSatya Tangirala 		bio->bi_status = BLK_STS_IOERR;
281a892c8d5SSatya Tangirala 		goto fail;
282a892c8d5SSatya Tangirala 	}
283a892c8d5SSatya Tangirala 
284488f6682SSatya Tangirala 	if (!bio_crypt_check_alignment(bio)) {
285488f6682SSatya Tangirala 		bio->bi_status = BLK_STS_IOERR;
286488f6682SSatya Tangirala 		goto fail;
287488f6682SSatya Tangirala 	}
288488f6682SSatya Tangirala 
289488f6682SSatya Tangirala 	/*
290488f6682SSatya Tangirala 	 * Success if device supports the encryption context, or if we succeeded
291488f6682SSatya Tangirala 	 * in falling back to the crypto API.
292488f6682SSatya Tangirala 	 */
2936715c98bSChristoph Hellwig 	if (blk_crypto_config_supported_natively(bio->bi_bdev,
2946715c98bSChristoph Hellwig 						 &bc_key->crypto_cfg))
295488f6682SSatya Tangirala 		return true;
296488f6682SSatya Tangirala 	if (blk_crypto_fallback_bio_prep(bio_ptr))
297a892c8d5SSatya Tangirala 		return true;
298a892c8d5SSatya Tangirala fail:
299a892c8d5SSatya Tangirala 	bio_endio(*bio_ptr);
300a892c8d5SSatya Tangirala 	return false;
301a892c8d5SSatya Tangirala }
302a892c8d5SSatya Tangirala 
__blk_crypto_rq_bio_prep(struct request * rq,struct bio * bio,gfp_t gfp_mask)30393f221aeSEric Biggers int __blk_crypto_rq_bio_prep(struct request *rq, struct bio *bio,
304a892c8d5SSatya Tangirala 			     gfp_t gfp_mask)
305a892c8d5SSatya Tangirala {
30693f221aeSEric Biggers 	if (!rq->crypt_ctx) {
307a892c8d5SSatya Tangirala 		rq->crypt_ctx = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
30893f221aeSEric Biggers 		if (!rq->crypt_ctx)
30993f221aeSEric Biggers 			return -ENOMEM;
31093f221aeSEric Biggers 	}
311a892c8d5SSatya Tangirala 	*rq->crypt_ctx = *bio->bi_crypt_context;
31293f221aeSEric Biggers 	return 0;
313a892c8d5SSatya Tangirala }
314a892c8d5SSatya Tangirala 
315a892c8d5SSatya Tangirala /**
316a892c8d5SSatya Tangirala  * blk_crypto_init_key() - Prepare a key for use with blk-crypto
317a892c8d5SSatya Tangirala  * @blk_key: Pointer to the blk_crypto_key to initialize.
318a892c8d5SSatya Tangirala  * @raw_key: Pointer to the raw key. Must be the correct length for the chosen
319a892c8d5SSatya Tangirala  *	     @crypto_mode; see blk_crypto_modes[].
320a892c8d5SSatya Tangirala  * @crypto_mode: identifier for the encryption algorithm to use
321a892c8d5SSatya Tangirala  * @dun_bytes: number of bytes that will be used to specify the DUN when this
322a892c8d5SSatya Tangirala  *	       key is used
323a892c8d5SSatya Tangirala  * @data_unit_size: the data unit size to use for en/decryption
324a892c8d5SSatya Tangirala  *
325a892c8d5SSatya Tangirala  * Return: 0 on success, -errno on failure.  The caller is responsible for
326a892c8d5SSatya Tangirala  *	   zeroizing both blk_key and raw_key when done with them.
327a892c8d5SSatya Tangirala  */
blk_crypto_init_key(struct blk_crypto_key * blk_key,const u8 * raw_key,enum blk_crypto_mode_num crypto_mode,unsigned int dun_bytes,unsigned int data_unit_size)328a892c8d5SSatya Tangirala int blk_crypto_init_key(struct blk_crypto_key *blk_key, const u8 *raw_key,
329a892c8d5SSatya Tangirala 			enum blk_crypto_mode_num crypto_mode,
330a892c8d5SSatya Tangirala 			unsigned int dun_bytes,
331a892c8d5SSatya Tangirala 			unsigned int data_unit_size)
332a892c8d5SSatya Tangirala {
333a892c8d5SSatya Tangirala 	const struct blk_crypto_mode *mode;
334a892c8d5SSatya Tangirala 
335a892c8d5SSatya Tangirala 	memset(blk_key, 0, sizeof(*blk_key));
336a892c8d5SSatya Tangirala 
337a892c8d5SSatya Tangirala 	if (crypto_mode >= ARRAY_SIZE(blk_crypto_modes))
338a892c8d5SSatya Tangirala 		return -EINVAL;
339a892c8d5SSatya Tangirala 
340a892c8d5SSatya Tangirala 	mode = &blk_crypto_modes[crypto_mode];
341a892c8d5SSatya Tangirala 	if (mode->keysize == 0)
342a892c8d5SSatya Tangirala 		return -EINVAL;
343a892c8d5SSatya Tangirala 
344cc40b722SEric Biggers 	if (dun_bytes == 0 || dun_bytes > mode->ivsize)
345a892c8d5SSatya Tangirala 		return -EINVAL;
346a892c8d5SSatya Tangirala 
347a892c8d5SSatya Tangirala 	if (!is_power_of_2(data_unit_size))
348a892c8d5SSatya Tangirala 		return -EINVAL;
349a892c8d5SSatya Tangirala 
350a892c8d5SSatya Tangirala 	blk_key->crypto_cfg.crypto_mode = crypto_mode;
351a892c8d5SSatya Tangirala 	blk_key->crypto_cfg.dun_bytes = dun_bytes;
352a892c8d5SSatya Tangirala 	blk_key->crypto_cfg.data_unit_size = data_unit_size;
353a892c8d5SSatya Tangirala 	blk_key->data_unit_size_bits = ilog2(data_unit_size);
354a892c8d5SSatya Tangirala 	blk_key->size = mode->keysize;
355a892c8d5SSatya Tangirala 	memcpy(blk_key->raw, raw_key, mode->keysize);
356a892c8d5SSatya Tangirala 
357a892c8d5SSatya Tangirala 	return 0;
358a892c8d5SSatya Tangirala }
359a892c8d5SSatya Tangirala 
blk_crypto_config_supported_natively(struct block_device * bdev,const struct blk_crypto_config * cfg)3606715c98bSChristoph Hellwig bool blk_crypto_config_supported_natively(struct block_device *bdev,
3616715c98bSChristoph Hellwig 					  const struct blk_crypto_config *cfg)
3626715c98bSChristoph Hellwig {
3636715c98bSChristoph Hellwig 	return __blk_crypto_cfg_supported(bdev_get_queue(bdev)->crypto_profile,
3646715c98bSChristoph Hellwig 					  cfg);
3656715c98bSChristoph Hellwig }
3666715c98bSChristoph Hellwig 
367488f6682SSatya Tangirala /*
368488f6682SSatya Tangirala  * Check if bios with @cfg can be en/decrypted by blk-crypto (i.e. either the
369fce3caeaSChristoph Hellwig  * block_device it's submitted to supports inline crypto, or the
370488f6682SSatya Tangirala  * blk-crypto-fallback is enabled and supports the cfg).
371488f6682SSatya Tangirala  */
blk_crypto_config_supported(struct block_device * bdev,const struct blk_crypto_config * cfg)372fce3caeaSChristoph Hellwig bool blk_crypto_config_supported(struct block_device *bdev,
373a892c8d5SSatya Tangirala 				 const struct blk_crypto_config *cfg)
374a892c8d5SSatya Tangirala {
375488f6682SSatya Tangirala 	return IS_ENABLED(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) ||
3766715c98bSChristoph Hellwig 	       blk_crypto_config_supported_natively(bdev, cfg);
377a892c8d5SSatya Tangirala }
378a892c8d5SSatya Tangirala 
379a892c8d5SSatya Tangirala /**
380a892c8d5SSatya Tangirala  * blk_crypto_start_using_key() - Start using a blk_crypto_key on a device
381fce3caeaSChristoph Hellwig  * @bdev: block device to operate on
382a892c8d5SSatya Tangirala  * @key: A key to use on the device
383a892c8d5SSatya Tangirala  *
384488f6682SSatya Tangirala  * Upper layers must call this function to ensure that either the hardware
385488f6682SSatya Tangirala  * supports the key's crypto settings, or the crypto API fallback has transforms
386488f6682SSatya Tangirala  * for the needed mode allocated and ready to go. This function may allocate
387488f6682SSatya Tangirala  * an skcipher, and *should not* be called from the data path, since that might
388488f6682SSatya Tangirala  * cause a deadlock
389a892c8d5SSatya Tangirala  *
390488f6682SSatya Tangirala  * Return: 0 on success; -ENOPKG if the hardware doesn't support the key and
391488f6682SSatya Tangirala  *	   blk-crypto-fallback is either disabled or the needed algorithm
392488f6682SSatya Tangirala  *	   is disabled in the crypto API; or another -errno code.
393a892c8d5SSatya Tangirala  */
blk_crypto_start_using_key(struct block_device * bdev,const struct blk_crypto_key * key)394fce3caeaSChristoph Hellwig int blk_crypto_start_using_key(struct block_device *bdev,
395fce3caeaSChristoph Hellwig 			       const struct blk_crypto_key *key)
396a892c8d5SSatya Tangirala {
3976715c98bSChristoph Hellwig 	if (blk_crypto_config_supported_natively(bdev, &key->crypto_cfg))
398a892c8d5SSatya Tangirala 		return 0;
399488f6682SSatya Tangirala 	return blk_crypto_fallback_start_using_mode(key->crypto_cfg.crypto_mode);
400a892c8d5SSatya Tangirala }
401a892c8d5SSatya Tangirala 
402a892c8d5SSatya Tangirala /**
403*5c7cb944SEric Biggers  * blk_crypto_evict_key() - Evict a blk_crypto_key from a block_device
404*5c7cb944SEric Biggers  * @bdev: a block_device on which I/O using the key may have been done
405*5c7cb944SEric Biggers  * @key: the key to evict
406a892c8d5SSatya Tangirala  *
407*5c7cb944SEric Biggers  * For a given block_device, this function removes the given blk_crypto_key from
408*5c7cb944SEric Biggers  * the keyslot management structures and evicts it from any underlying hardware
409*5c7cb944SEric Biggers  * keyslot(s) or blk-crypto-fallback keyslot it may have been programmed into.
410*5c7cb944SEric Biggers  *
411*5c7cb944SEric Biggers  * Upper layers must call this before freeing the blk_crypto_key.  It must be
412*5c7cb944SEric Biggers  * called for every block_device the key may have been used on.  The key must no
413*5c7cb944SEric Biggers  * longer be in use by any I/O when this function is called.
414*5c7cb944SEric Biggers  *
415*5c7cb944SEric Biggers  * Context: May sleep.
416a892c8d5SSatya Tangirala  */
blk_crypto_evict_key(struct block_device * bdev,const struct blk_crypto_key * key)41770493a63SEric Biggers void blk_crypto_evict_key(struct block_device *bdev,
418a892c8d5SSatya Tangirala 			  const struct blk_crypto_key *key)
419a892c8d5SSatya Tangirala {
420fce3caeaSChristoph Hellwig 	struct request_queue *q = bdev_get_queue(bdev);
42170493a63SEric Biggers 	int err;
422fce3caeaSChristoph Hellwig 
4236715c98bSChristoph Hellwig 	if (blk_crypto_config_supported_natively(bdev, &key->crypto_cfg))
42470493a63SEric Biggers 		err = __blk_crypto_evict_key(q->crypto_profile, key);
42570493a63SEric Biggers 	else
42670493a63SEric Biggers 		err = blk_crypto_fallback_evict_key(key);
427*5c7cb944SEric Biggers 	/*
428*5c7cb944SEric Biggers 	 * An error can only occur here if the key failed to be evicted from a
429*5c7cb944SEric Biggers 	 * keyslot (due to a hardware or driver issue) or is allegedly still in
430*5c7cb944SEric Biggers 	 * use by I/O (due to a kernel bug).  Even in these cases, the key is
431*5c7cb944SEric Biggers 	 * still unlinked from the keyslot management structures, and the caller
432*5c7cb944SEric Biggers 	 * is allowed and expected to free it right away.  There's nothing
433*5c7cb944SEric Biggers 	 * callers can do to handle errors, so just log them and return void.
434*5c7cb944SEric Biggers 	 */
43570493a63SEric Biggers 	if (err)
43670493a63SEric Biggers 		pr_warn_ratelimited("%pg: error %d evicting key\n", bdev, err);
437a892c8d5SSatya Tangirala }
4389355a9ebSSatya Tangirala EXPORT_SYMBOL_GPL(blk_crypto_evict_key);
439