1
2
3
4
5
6
7
8
9
10#define pr_fmt(fmt) "blk-crypto: " fmt
11
12#include <linux/bio.h>
13#include <linux/blkdev.h>
14#include <linux/blk-crypto-profile.h>
15#include <linux/module.h>
16#include <linux/ratelimit.h>
17#include <linux/slab.h>
18
19#include "blk-crypto-internal.h"
20
21const struct blk_crypto_mode blk_crypto_modes[] = {
22 [BLK_ENCRYPTION_MODE_AES_256_XTS] = {
23 .name = "AES-256-XTS",
24 .cipher_str = "xts(aes)",
25 .keysize = 64,
26 .ivsize = 16,
27 },
28 [BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV] = {
29 .name = "AES-128-CBC-ESSIV",
30 .cipher_str = "essiv(cbc(aes),sha256)",
31 .keysize = 16,
32 .ivsize = 16,
33 },
34 [BLK_ENCRYPTION_MODE_ADIANTUM] = {
35 .name = "Adiantum",
36 .cipher_str = "adiantum(xchacha12,aes)",
37 .keysize = 32,
38 .ivsize = 32,
39 },
40 [BLK_ENCRYPTION_MODE_SM4_XTS] = {
41 .name = "SM4-XTS",
42 .cipher_str = "xts(sm4)",
43 .keysize = 32,
44 .ivsize = 16,
45 },
46};
47
48
49
50
51
52
53
54static int num_prealloc_crypt_ctxs = 128;
55
56module_param(num_prealloc_crypt_ctxs, int, 0444);
57MODULE_PARM_DESC(num_prealloc_crypt_ctxs,
58 "Number of bio crypto contexts to preallocate");
59
60static struct kmem_cache *bio_crypt_ctx_cache;
61static mempool_t *bio_crypt_ctx_pool;
62
63static int __init bio_crypt_ctx_init(void)
64{
65 size_t i;
66
67 bio_crypt_ctx_cache = KMEM_CACHE(bio_crypt_ctx, 0);
68 if (!bio_crypt_ctx_cache)
69 goto out_no_mem;
70
71 bio_crypt_ctx_pool = mempool_create_slab_pool(num_prealloc_crypt_ctxs,
72 bio_crypt_ctx_cache);
73 if (!bio_crypt_ctx_pool)
74 goto out_no_mem;
75
76
77 BUILD_BUG_ON(BLK_ENCRYPTION_MODE_INVALID != 0);
78
79
80 for (i = 0; i < BLK_ENCRYPTION_MODE_MAX; i++) {
81 BUG_ON(blk_crypto_modes[i].keysize > BLK_CRYPTO_MAX_KEY_SIZE);
82 BUG_ON(blk_crypto_modes[i].ivsize > BLK_CRYPTO_MAX_IV_SIZE);
83 }
84
85 return 0;
86out_no_mem:
87 panic("Failed to allocate mem for bio crypt ctxs\n");
88}
89subsys_initcall(bio_crypt_ctx_init);
90
91void bio_crypt_set_ctx(struct bio *bio, const struct blk_crypto_key *key,
92 const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE], gfp_t gfp_mask)
93{
94 struct bio_crypt_ctx *bc;
95
96
97
98
99
100 WARN_ON_ONCE(!(gfp_mask & __GFP_DIRECT_RECLAIM));
101
102 bc = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
103
104 bc->bc_key = key;
105 memcpy(bc->bc_dun, dun, sizeof(bc->bc_dun));
106
107 bio->bi_crypt_context = bc;
108}
109
110void __bio_crypt_free_ctx(struct bio *bio)
111{
112 mempool_free(bio->bi_crypt_context, bio_crypt_ctx_pool);
113 bio->bi_crypt_context = NULL;
114}
115
116int __bio_crypt_clone(struct bio *dst, struct bio *src, gfp_t gfp_mask)
117{
118 dst->bi_crypt_context = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
119 if (!dst->bi_crypt_context)
120 return -ENOMEM;
121 *dst->bi_crypt_context = *src->bi_crypt_context;
122 return 0;
123}
124
125
126void bio_crypt_dun_increment(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
127 unsigned int inc)
128{
129 int i;
130
131 for (i = 0; inc && i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) {
132 dun[i] += inc;
133
134
135
136
137 if (dun[i] < inc)
138 inc = 1;
139 else
140 inc = 0;
141 }
142}
143
144void __bio_crypt_advance(struct bio *bio, unsigned int bytes)
145{
146 struct bio_crypt_ctx *bc = bio->bi_crypt_context;
147
148 bio_crypt_dun_increment(bc->bc_dun,
149 bytes >> bc->bc_key->data_unit_size_bits);
150}
151
152
153
154
155
156bool bio_crypt_dun_is_contiguous(const struct bio_crypt_ctx *bc,
157 unsigned int bytes,
158 const u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE])
159{
160 int i;
161 unsigned int carry = bytes >> bc->bc_key->data_unit_size_bits;
162
163 for (i = 0; i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) {
164 if (bc->bc_dun[i] + carry != next_dun[i])
165 return false;
166
167
168
169
170 if ((bc->bc_dun[i] + carry) < carry)
171 carry = 1;
172 else
173 carry = 0;
174 }
175
176
177 return carry == 0;
178}
179
180
181
182
183
184static bool bio_crypt_ctx_compatible(struct bio_crypt_ctx *bc1,
185 struct bio_crypt_ctx *bc2)
186{
187 if (!bc1)
188 return !bc2;
189
190 return bc2 && bc1->bc_key == bc2->bc_key;
191}
192
193bool bio_crypt_rq_ctx_compatible(struct request *rq, struct bio *bio)
194{
195 return bio_crypt_ctx_compatible(rq->crypt_ctx, bio->bi_crypt_context);
196}
197
198
199
200
201
202
203bool bio_crypt_ctx_mergeable(struct bio_crypt_ctx *bc1, unsigned int bc1_bytes,
204 struct bio_crypt_ctx *bc2)
205{
206 if (!bio_crypt_ctx_compatible(bc1, bc2))
207 return false;
208
209 return !bc1 || bio_crypt_dun_is_contiguous(bc1, bc1_bytes, bc2->bc_dun);
210}
211
212
213static bool bio_crypt_check_alignment(struct bio *bio)
214{
215 const unsigned int data_unit_size =
216 bio->bi_crypt_context->bc_key->crypto_cfg.data_unit_size;
217 struct bvec_iter iter;
218 struct bio_vec bv;
219
220 bio_for_each_segment(bv, bio, iter) {
221 if (!IS_ALIGNED(bv.bv_len | bv.bv_offset, data_unit_size))
222 return false;
223 }
224
225 return true;
226}
227
228blk_status_t __blk_crypto_rq_get_keyslot(struct request *rq)
229{
230 return blk_crypto_get_keyslot(rq->q->crypto_profile,
231 rq->crypt_ctx->bc_key,
232 &rq->crypt_keyslot);
233}
234
235void __blk_crypto_rq_put_keyslot(struct request *rq)
236{
237 blk_crypto_put_keyslot(rq->crypt_keyslot);
238 rq->crypt_keyslot = NULL;
239}
240
241void __blk_crypto_free_request(struct request *rq)
242{
243
244 if (WARN_ON_ONCE(rq->crypt_keyslot))
245 __blk_crypto_rq_put_keyslot(rq);
246
247 mempool_free(rq->crypt_ctx, bio_crypt_ctx_pool);
248 rq->crypt_ctx = NULL;
249}
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273bool __blk_crypto_bio_prep(struct bio **bio_ptr)
274{
275 struct bio *bio = *bio_ptr;
276 const struct blk_crypto_key *bc_key = bio->bi_crypt_context->bc_key;
277
278
279 if (WARN_ON_ONCE(!bio_has_data(bio))) {
280 bio->bi_status = BLK_STS_IOERR;
281 goto fail;
282 }
283
284 if (!bio_crypt_check_alignment(bio)) {
285 bio->bi_status = BLK_STS_IOERR;
286 goto fail;
287 }
288
289
290
291
292
293 if (blk_crypto_config_supported_natively(bio->bi_bdev,
294 &bc_key->crypto_cfg))
295 return true;
296 if (blk_crypto_fallback_bio_prep(bio_ptr))
297 return true;
298fail:
299 bio_endio(*bio_ptr);
300 return false;
301}
302
303int __blk_crypto_rq_bio_prep(struct request *rq, struct bio *bio,
304 gfp_t gfp_mask)
305{
306 if (!rq->crypt_ctx) {
307 rq->crypt_ctx = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
308 if (!rq->crypt_ctx)
309 return -ENOMEM;
310 }
311 *rq->crypt_ctx = *bio->bi_crypt_context;
312 return 0;
313}
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328int blk_crypto_init_key(struct blk_crypto_key *blk_key, const u8 *raw_key,
329 enum blk_crypto_mode_num crypto_mode,
330 unsigned int dun_bytes,
331 unsigned int data_unit_size)
332{
333 const struct blk_crypto_mode *mode;
334
335 memset(blk_key, 0, sizeof(*blk_key));
336
337 if (crypto_mode >= ARRAY_SIZE(blk_crypto_modes))
338 return -EINVAL;
339
340 mode = &blk_crypto_modes[crypto_mode];
341 if (mode->keysize == 0)
342 return -EINVAL;
343
344 if (dun_bytes == 0 || dun_bytes > mode->ivsize)
345 return -EINVAL;
346
347 if (!is_power_of_2(data_unit_size))
348 return -EINVAL;
349
350 blk_key->crypto_cfg.crypto_mode = crypto_mode;
351 blk_key->crypto_cfg.dun_bytes = dun_bytes;
352 blk_key->crypto_cfg.data_unit_size = data_unit_size;
353 blk_key->data_unit_size_bits = ilog2(data_unit_size);
354 blk_key->size = mode->keysize;
355 memcpy(blk_key->raw, raw_key, mode->keysize);
356
357 return 0;
358}
359
360bool blk_crypto_config_supported_natively(struct block_device *bdev,
361 const struct blk_crypto_config *cfg)
362{
363 return __blk_crypto_cfg_supported(bdev_get_queue(bdev)->crypto_profile,
364 cfg);
365}
366
367
368
369
370
371
372bool blk_crypto_config_supported(struct block_device *bdev,
373 const struct blk_crypto_config *cfg)
374{
375 return IS_ENABLED(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) ||
376 blk_crypto_config_supported_natively(bdev, cfg);
377}
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394int blk_crypto_start_using_key(struct block_device *bdev,
395 const struct blk_crypto_key *key)
396{
397 if (blk_crypto_config_supported_natively(bdev, &key->crypto_cfg))
398 return 0;
399 return blk_crypto_fallback_start_using_mode(key->crypto_cfg.crypto_mode);
400}
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417void blk_crypto_evict_key(struct block_device *bdev,
418 const struct blk_crypto_key *key)
419{
420 struct request_queue *q = bdev_get_queue(bdev);
421 int err;
422
423 if (blk_crypto_config_supported_natively(bdev, &key->crypto_cfg))
424 err = __blk_crypto_evict_key(q->crypto_profile, key);
425 else
426 err = blk_crypto_fallback_evict_key(key);
427
428
429
430
431
432
433
434
435 if (err)
436 pr_warn_ratelimited("%pg: error %d evicting key\n", bdev, err);
437}
438EXPORT_SYMBOL_GPL(blk_crypto_evict_key);
439