block: Inline encryption support for blk-mq
[linux-block.git] / block / blk-crypto.c
CommitLineData
a892c8d5
ST
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2019 Google LLC
4 */
5
6/*
7 * Refer to Documentation/block/inline-encryption.rst for detailed explanation.
8 */
9
10#define pr_fmt(fmt) "blk-crypto: " fmt
11
12#include <linux/bio.h>
13#include <linux/blkdev.h>
14#include <linux/keyslot-manager.h>
15#include <linux/module.h>
16#include <linux/slab.h>
17
18#include "blk-crypto-internal.h"
19
20const struct blk_crypto_mode blk_crypto_modes[] = {
21 [BLK_ENCRYPTION_MODE_AES_256_XTS] = {
22 .keysize = 64,
23 .ivsize = 16,
24 },
25 [BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV] = {
26 .keysize = 16,
27 .ivsize = 16,
28 },
29 [BLK_ENCRYPTION_MODE_ADIANTUM] = {
30 .keysize = 32,
31 .ivsize = 32,
32 },
33};
34
35/*
36 * This number needs to be at least (the number of threads doing IO
37 * concurrently) * (maximum recursive depth of a bio), so that we don't
38 * deadlock on crypt_ctx allocations. The default is chosen to be the same
39 * as the default number of post read contexts in both EXT4 and F2FS.
40 */
41static int num_prealloc_crypt_ctxs = 128;
42
43module_param(num_prealloc_crypt_ctxs, int, 0444);
44MODULE_PARM_DESC(num_prealloc_crypt_ctxs,
45 "Number of bio crypto contexts to preallocate");
46
47static struct kmem_cache *bio_crypt_ctx_cache;
48static mempool_t *bio_crypt_ctx_pool;
49
50static int __init bio_crypt_ctx_init(void)
51{
52 size_t i;
53
54 bio_crypt_ctx_cache = KMEM_CACHE(bio_crypt_ctx, 0);
55 if (!bio_crypt_ctx_cache)
56 goto out_no_mem;
57
58 bio_crypt_ctx_pool = mempool_create_slab_pool(num_prealloc_crypt_ctxs,
59 bio_crypt_ctx_cache);
60 if (!bio_crypt_ctx_pool)
61 goto out_no_mem;
62
63 /* This is assumed in various places. */
64 BUILD_BUG_ON(BLK_ENCRYPTION_MODE_INVALID != 0);
65
66 /* Sanity check that no algorithm exceeds the defined limits. */
67 for (i = 0; i < BLK_ENCRYPTION_MODE_MAX; i++) {
68 BUG_ON(blk_crypto_modes[i].keysize > BLK_CRYPTO_MAX_KEY_SIZE);
69 BUG_ON(blk_crypto_modes[i].ivsize > BLK_CRYPTO_MAX_IV_SIZE);
70 }
71
72 return 0;
73out_no_mem:
74 panic("Failed to allocate mem for bio crypt ctxs\n");
75}
76subsys_initcall(bio_crypt_ctx_init);
77
78void bio_crypt_set_ctx(struct bio *bio, const struct blk_crypto_key *key,
79 const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE], gfp_t gfp_mask)
80{
81 struct bio_crypt_ctx *bc = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
82
83 bc->bc_key = key;
84 memcpy(bc->bc_dun, dun, sizeof(bc->bc_dun));
85
86 bio->bi_crypt_context = bc;
87}
88
89void __bio_crypt_free_ctx(struct bio *bio)
90{
91 mempool_free(bio->bi_crypt_context, bio_crypt_ctx_pool);
92 bio->bi_crypt_context = NULL;
93}
94
95void __bio_crypt_clone(struct bio *dst, struct bio *src, gfp_t gfp_mask)
96{
97 dst->bi_crypt_context = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
98 *dst->bi_crypt_context = *src->bi_crypt_context;
99}
100EXPORT_SYMBOL_GPL(__bio_crypt_clone);
101
102/* Increments @dun by @inc, treating @dun as a multi-limb integer. */
103void bio_crypt_dun_increment(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
104 unsigned int inc)
105{
106 int i;
107
108 for (i = 0; inc && i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) {
109 dun[i] += inc;
110 /*
111 * If the addition in this limb overflowed, then we need to
112 * carry 1 into the next limb. Else the carry is 0.
113 */
114 if (dun[i] < inc)
115 inc = 1;
116 else
117 inc = 0;
118 }
119}
120
121void __bio_crypt_advance(struct bio *bio, unsigned int bytes)
122{
123 struct bio_crypt_ctx *bc = bio->bi_crypt_context;
124
125 bio_crypt_dun_increment(bc->bc_dun,
126 bytes >> bc->bc_key->data_unit_size_bits);
127}
128
129/*
130 * Returns true if @bc->bc_dun plus @bytes converted to data units is equal to
131 * @next_dun, treating the DUNs as multi-limb integers.
132 */
133bool bio_crypt_dun_is_contiguous(const struct bio_crypt_ctx *bc,
134 unsigned int bytes,
135 const u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE])
136{
137 int i;
138 unsigned int carry = bytes >> bc->bc_key->data_unit_size_bits;
139
140 for (i = 0; i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) {
141 if (bc->bc_dun[i] + carry != next_dun[i])
142 return false;
143 /*
144 * If the addition in this limb overflowed, then we need to
145 * carry 1 into the next limb. Else the carry is 0.
146 */
147 if ((bc->bc_dun[i] + carry) < carry)
148 carry = 1;
149 else
150 carry = 0;
151 }
152
153 /* If the DUN wrapped through 0, don't treat it as contiguous. */
154 return carry == 0;
155}
156
157/*
158 * Checks that two bio crypt contexts are compatible - i.e. that
159 * they are mergeable except for data_unit_num continuity.
160 */
161static bool bio_crypt_ctx_compatible(struct bio_crypt_ctx *bc1,
162 struct bio_crypt_ctx *bc2)
163{
164 if (!bc1)
165 return !bc2;
166
167 return bc2 && bc1->bc_key == bc2->bc_key;
168}
169
170bool bio_crypt_rq_ctx_compatible(struct request *rq, struct bio *bio)
171{
172 return bio_crypt_ctx_compatible(rq->crypt_ctx, bio->bi_crypt_context);
173}
174
175/*
176 * Checks that two bio crypt contexts are compatible, and also
177 * that their data_unit_nums are continuous (and can hence be merged)
178 * in the order @bc1 followed by @bc2.
179 */
180bool bio_crypt_ctx_mergeable(struct bio_crypt_ctx *bc1, unsigned int bc1_bytes,
181 struct bio_crypt_ctx *bc2)
182{
183 if (!bio_crypt_ctx_compatible(bc1, bc2))
184 return false;
185
186 return !bc1 || bio_crypt_dun_is_contiguous(bc1, bc1_bytes, bc2->bc_dun);
187}
188
189/* Check that all I/O segments are data unit aligned. */
190static bool bio_crypt_check_alignment(struct bio *bio)
191{
192 const unsigned int data_unit_size =
193 bio->bi_crypt_context->bc_key->crypto_cfg.data_unit_size;
194 struct bvec_iter iter;
195 struct bio_vec bv;
196
197 bio_for_each_segment(bv, bio, iter) {
198 if (!IS_ALIGNED(bv.bv_len | bv.bv_offset, data_unit_size))
199 return false;
200 }
201
202 return true;
203}
204
205blk_status_t __blk_crypto_init_request(struct request *rq)
206{
207 return blk_ksm_get_slot_for_key(rq->q->ksm, rq->crypt_ctx->bc_key,
208 &rq->crypt_keyslot);
209}
210
211/**
212 * __blk_crypto_free_request - Uninitialize the crypto fields of a request.
213 *
214 * @rq: The request whose crypto fields to uninitialize.
215 *
216 * Completely uninitializes the crypto fields of a request. If a keyslot has
217 * been programmed into some inline encryption hardware, that keyslot is
218 * released. The rq->crypt_ctx is also freed.
219 */
220void __blk_crypto_free_request(struct request *rq)
221{
222 blk_ksm_put_slot(rq->crypt_keyslot);
223 mempool_free(rq->crypt_ctx, bio_crypt_ctx_pool);
224 blk_crypto_rq_set_defaults(rq);
225}
226
227/**
228 * __blk_crypto_bio_prep - Prepare bio for inline encryption
229 *
230 * @bio_ptr: pointer to original bio pointer
231 *
232 * Succeeds if the bio doesn't have inline encryption enabled or if the bio
233 * crypt context provided for the bio is supported by the underlying device's
234 * inline encryption hardware. Ends the bio with error otherwise.
235 *
236 * Caller must ensure bio has bio_crypt_ctx.
237 *
238 * Return: true on success; false on error (and bio->bi_status will be set
239 * appropriately, and bio_endio() will have been called so bio
240 * submission should abort).
241 */
242bool __blk_crypto_bio_prep(struct bio **bio_ptr)
243{
244 struct bio *bio = *bio_ptr;
245 const struct blk_crypto_key *bc_key = bio->bi_crypt_context->bc_key;
246 blk_status_t blk_st = BLK_STS_IOERR;
247
248 /* Error if bio has no data. */
249 if (WARN_ON_ONCE(!bio_has_data(bio)))
250 goto fail;
251
252 if (!bio_crypt_check_alignment(bio))
253 goto fail;
254
255 /*
256 * Success if device supports the encryption context.
257 */
258 if (!blk_ksm_crypto_cfg_supported(bio->bi_disk->queue->ksm,
259 &bc_key->crypto_cfg)) {
260 blk_st = BLK_STS_NOTSUPP;
261 goto fail;
262 }
263
264 return true;
265fail:
266 (*bio_ptr)->bi_status = blk_st;
267 bio_endio(*bio_ptr);
268 return false;
269}
270
271/**
272 * __blk_crypto_rq_bio_prep - Prepare a request's crypt_ctx when its first bio
273 * is inserted
274 *
275 * @rq: The request to prepare
276 * @bio: The first bio being inserted into the request
277 * @gfp_mask: gfp mask
278 */
279void __blk_crypto_rq_bio_prep(struct request *rq, struct bio *bio,
280 gfp_t gfp_mask)
281{
282 if (!rq->crypt_ctx)
283 rq->crypt_ctx = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
284 *rq->crypt_ctx = *bio->bi_crypt_context;
285}
286
287/**
288 * blk_crypto_init_key() - Prepare a key for use with blk-crypto
289 * @blk_key: Pointer to the blk_crypto_key to initialize.
290 * @raw_key: Pointer to the raw key. Must be the correct length for the chosen
291 * @crypto_mode; see blk_crypto_modes[].
292 * @crypto_mode: identifier for the encryption algorithm to use
293 * @dun_bytes: number of bytes that will be used to specify the DUN when this
294 * key is used
295 * @data_unit_size: the data unit size to use for en/decryption
296 *
297 * Return: 0 on success, -errno on failure. The caller is responsible for
298 * zeroizing both blk_key and raw_key when done with them.
299 */
300int blk_crypto_init_key(struct blk_crypto_key *blk_key, const u8 *raw_key,
301 enum blk_crypto_mode_num crypto_mode,
302 unsigned int dun_bytes,
303 unsigned int data_unit_size)
304{
305 const struct blk_crypto_mode *mode;
306
307 memset(blk_key, 0, sizeof(*blk_key));
308
309 if (crypto_mode >= ARRAY_SIZE(blk_crypto_modes))
310 return -EINVAL;
311
312 mode = &blk_crypto_modes[crypto_mode];
313 if (mode->keysize == 0)
314 return -EINVAL;
315
316 if (dun_bytes == 0 || dun_bytes > BLK_CRYPTO_MAX_IV_SIZE)
317 return -EINVAL;
318
319 if (!is_power_of_2(data_unit_size))
320 return -EINVAL;
321
322 blk_key->crypto_cfg.crypto_mode = crypto_mode;
323 blk_key->crypto_cfg.dun_bytes = dun_bytes;
324 blk_key->crypto_cfg.data_unit_size = data_unit_size;
325 blk_key->data_unit_size_bits = ilog2(data_unit_size);
326 blk_key->size = mode->keysize;
327 memcpy(blk_key->raw, raw_key, mode->keysize);
328
329 return 0;
330}
331
332bool blk_crypto_config_supported(struct request_queue *q,
333 const struct blk_crypto_config *cfg)
334{
335 return blk_ksm_crypto_cfg_supported(q->ksm, cfg);
336}
337
338/**
339 * blk_crypto_start_using_key() - Start using a blk_crypto_key on a device
340 * @key: A key to use on the device
341 * @q: the request queue for the device
342 *
343 * Upper layers must call this function to ensure that the hardware supports
344 * the key's crypto settings.
345 *
346 * Return: 0 on success; -ENOPKG if the hardware doesn't support the key
347 */
348int blk_crypto_start_using_key(const struct blk_crypto_key *key,
349 struct request_queue *q)
350{
351 if (blk_ksm_crypto_cfg_supported(q->ksm, &key->crypto_cfg))
352 return 0;
353 return -ENOPKG;
354}
355
356/**
357 * blk_crypto_evict_key() - Evict a key from any inline encryption hardware
358 * it may have been programmed into
359 * @q: The request queue who's associated inline encryption hardware this key
360 * might have been programmed into
361 * @key: The key to evict
362 *
363 * Upper layers (filesystems) must call this function to ensure that a key is
364 * evicted from any hardware that it might have been programmed into. The key
365 * must not be in use by any in-flight IO when this function is called.
366 *
367 * Return: 0 on success or if key is not present in the q's ksm, -err on error.
368 */
369int blk_crypto_evict_key(struct request_queue *q,
370 const struct blk_crypto_key *key)
371{
372 if (blk_ksm_crypto_cfg_supported(q->ksm, &key->crypto_cfg))
373 return blk_ksm_evict_key(q->ksm, key);
374
375 return 0;
376}