~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/block/blk-crypto.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /block/blk-crypto.c (Version linux-6.12-rc7) and /block/blk-crypto.c (Version linux-5.15.171)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 /*                                                  2 /*
  3  * Copyright 2019 Google LLC                        3  * Copyright 2019 Google LLC
  4  */                                                 4  */
  5                                                     5 
  6 /*                                                  6 /*
  7  * Refer to Documentation/block/inline-encrypt      7  * Refer to Documentation/block/inline-encryption.rst for detailed explanation.
  8  */                                                 8  */
  9                                                     9 
 10 #define pr_fmt(fmt) "blk-crypto: " fmt             10 #define pr_fmt(fmt) "blk-crypto: " fmt
 11                                                    11 
 12 #include <linux/bio.h>                             12 #include <linux/bio.h>
 13 #include <linux/blkdev.h>                          13 #include <linux/blkdev.h>
 14 #include <linux/blk-crypto-profile.h>          !!  14 #include <linux/keyslot-manager.h>
 15 #include <linux/module.h>                          15 #include <linux/module.h>
 16 #include <linux/ratelimit.h>                       16 #include <linux/ratelimit.h>
 17 #include <linux/slab.h>                            17 #include <linux/slab.h>
 18                                                    18 
 19 #include "blk-crypto-internal.h"                   19 #include "blk-crypto-internal.h"
 20                                                    20 
 21 const struct blk_crypto_mode blk_crypto_modes[     21 const struct blk_crypto_mode blk_crypto_modes[] = {
 22         [BLK_ENCRYPTION_MODE_AES_256_XTS] = {      22         [BLK_ENCRYPTION_MODE_AES_256_XTS] = {
 23                 .name = "AES-256-XTS",         << 
 24                 .cipher_str = "xts(aes)",          23                 .cipher_str = "xts(aes)",
 25                 .keysize = 64,                     24                 .keysize = 64,
 26                 .ivsize = 16,                      25                 .ivsize = 16,
 27         },                                         26         },
 28         [BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV     27         [BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV] = {
 29                 .name = "AES-128-CBC-ESSIV",   << 
 30                 .cipher_str = "essiv(cbc(aes),     28                 .cipher_str = "essiv(cbc(aes),sha256)",
 31                 .keysize = 16,                     29                 .keysize = 16,
 32                 .ivsize = 16,                      30                 .ivsize = 16,
 33         },                                         31         },
 34         [BLK_ENCRYPTION_MODE_ADIANTUM] = {         32         [BLK_ENCRYPTION_MODE_ADIANTUM] = {
 35                 .name = "Adiantum",            << 
 36                 .cipher_str = "adiantum(xchach     33                 .cipher_str = "adiantum(xchacha12,aes)",
 37                 .keysize = 32,                     34                 .keysize = 32,
 38                 .ivsize = 32,                      35                 .ivsize = 32,
 39         },                                         36         },
 40         [BLK_ENCRYPTION_MODE_SM4_XTS] = {      << 
 41                 .name = "SM4-XTS",             << 
 42                 .cipher_str = "xts(sm4)",      << 
 43                 .keysize = 32,                 << 
 44                 .ivsize = 16,                  << 
 45         },                                     << 
 46 };                                                 37 };
 47                                                    38 
 48 /*                                                 39 /*
 49  * This number needs to be at least (the numbe     40  * This number needs to be at least (the number of threads doing IO
 50  * concurrently) * (maximum recursive depth of     41  * concurrently) * (maximum recursive depth of a bio), so that we don't
 51  * deadlock on crypt_ctx allocations. The defa     42  * deadlock on crypt_ctx allocations. The default is chosen to be the same
 52  * as the default number of post read contexts     43  * as the default number of post read contexts in both EXT4 and F2FS.
 53  */                                                44  */
 54 static int num_prealloc_crypt_ctxs = 128;          45 static int num_prealloc_crypt_ctxs = 128;
 55                                                    46 
 56 module_param(num_prealloc_crypt_ctxs, int, 044     47 module_param(num_prealloc_crypt_ctxs, int, 0444);
 57 MODULE_PARM_DESC(num_prealloc_crypt_ctxs,          48 MODULE_PARM_DESC(num_prealloc_crypt_ctxs,
 58                 "Number of bio crypto contexts     49                 "Number of bio crypto contexts to preallocate");
 59                                                    50 
 60 static struct kmem_cache *bio_crypt_ctx_cache;     51 static struct kmem_cache *bio_crypt_ctx_cache;
 61 static mempool_t *bio_crypt_ctx_pool;              52 static mempool_t *bio_crypt_ctx_pool;
 62                                                    53 
 63 static int __init bio_crypt_ctx_init(void)         54 static int __init bio_crypt_ctx_init(void)
 64 {                                                  55 {
 65         size_t i;                                  56         size_t i;
 66                                                    57 
 67         bio_crypt_ctx_cache = KMEM_CACHE(bio_c     58         bio_crypt_ctx_cache = KMEM_CACHE(bio_crypt_ctx, 0);
 68         if (!bio_crypt_ctx_cache)                  59         if (!bio_crypt_ctx_cache)
 69                 goto out_no_mem;                   60                 goto out_no_mem;
 70                                                    61 
 71         bio_crypt_ctx_pool = mempool_create_sl     62         bio_crypt_ctx_pool = mempool_create_slab_pool(num_prealloc_crypt_ctxs,
 72                                                    63                                                       bio_crypt_ctx_cache);
 73         if (!bio_crypt_ctx_pool)                   64         if (!bio_crypt_ctx_pool)
 74                 goto out_no_mem;                   65                 goto out_no_mem;
 75                                                    66 
 76         /* This is assumed in various places.      67         /* This is assumed in various places. */
 77         BUILD_BUG_ON(BLK_ENCRYPTION_MODE_INVAL     68         BUILD_BUG_ON(BLK_ENCRYPTION_MODE_INVALID != 0);
 78                                                    69 
 79         /* Sanity check that no algorithm exce     70         /* Sanity check that no algorithm exceeds the defined limits. */
 80         for (i = 0; i < BLK_ENCRYPTION_MODE_MA     71         for (i = 0; i < BLK_ENCRYPTION_MODE_MAX; i++) {
 81                 BUG_ON(blk_crypto_modes[i].key     72                 BUG_ON(blk_crypto_modes[i].keysize > BLK_CRYPTO_MAX_KEY_SIZE);
 82                 BUG_ON(blk_crypto_modes[i].ivs     73                 BUG_ON(blk_crypto_modes[i].ivsize > BLK_CRYPTO_MAX_IV_SIZE);
 83         }                                          74         }
 84                                                    75 
 85         return 0;                                  76         return 0;
 86 out_no_mem:                                        77 out_no_mem:
 87         panic("Failed to allocate mem for bio      78         panic("Failed to allocate mem for bio crypt ctxs\n");
 88 }                                                  79 }
 89 subsys_initcall(bio_crypt_ctx_init);               80 subsys_initcall(bio_crypt_ctx_init);
 90                                                    81 
 91 void bio_crypt_set_ctx(struct bio *bio, const      82 void bio_crypt_set_ctx(struct bio *bio, const struct blk_crypto_key *key,
 92                        const u64 dun[BLK_CRYPT     83                        const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE], gfp_t gfp_mask)
 93 {                                                  84 {
 94         struct bio_crypt_ctx *bc;                  85         struct bio_crypt_ctx *bc;
 95                                                    86 
 96         /*                                         87         /*
 97          * The caller must use a gfp_mask that     88          * The caller must use a gfp_mask that contains __GFP_DIRECT_RECLAIM so
 98          * that the mempool_alloc() can't fail     89          * that the mempool_alloc() can't fail.
 99          */                                        90          */
100         WARN_ON_ONCE(!(gfp_mask & __GFP_DIRECT     91         WARN_ON_ONCE(!(gfp_mask & __GFP_DIRECT_RECLAIM));
101                                                    92 
102         bc = mempool_alloc(bio_crypt_ctx_pool,     93         bc = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
103                                                    94 
104         bc->bc_key = key;                          95         bc->bc_key = key;
105         memcpy(bc->bc_dun, dun, sizeof(bc->bc_     96         memcpy(bc->bc_dun, dun, sizeof(bc->bc_dun));
106                                                    97 
107         bio->bi_crypt_context = bc;                98         bio->bi_crypt_context = bc;
108 }                                                  99 }
109                                                   100 
110 void __bio_crypt_free_ctx(struct bio *bio)        101 void __bio_crypt_free_ctx(struct bio *bio)
111 {                                                 102 {
112         mempool_free(bio->bi_crypt_context, bi    103         mempool_free(bio->bi_crypt_context, bio_crypt_ctx_pool);
113         bio->bi_crypt_context = NULL;             104         bio->bi_crypt_context = NULL;
114 }                                                 105 }
115                                                   106 
116 int __bio_crypt_clone(struct bio *dst, struct     107 int __bio_crypt_clone(struct bio *dst, struct bio *src, gfp_t gfp_mask)
117 {                                                 108 {
118         dst->bi_crypt_context = mempool_alloc(    109         dst->bi_crypt_context = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
119         if (!dst->bi_crypt_context)               110         if (!dst->bi_crypt_context)
120                 return -ENOMEM;                   111                 return -ENOMEM;
121         *dst->bi_crypt_context = *src->bi_cryp    112         *dst->bi_crypt_context = *src->bi_crypt_context;
122         return 0;                                 113         return 0;
123 }                                                 114 }
                                                   >> 115 EXPORT_SYMBOL_GPL(__bio_crypt_clone);
124                                                   116 
125 /* Increments @dun by @inc, treating @dun as a    117 /* Increments @dun by @inc, treating @dun as a multi-limb integer. */
126 void bio_crypt_dun_increment(u64 dun[BLK_CRYPT    118 void bio_crypt_dun_increment(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
127                              unsigned int inc)    119                              unsigned int inc)
128 {                                                 120 {
129         int i;                                    121         int i;
130                                                   122 
131         for (i = 0; inc && i < BLK_CRYPTO_DUN_    123         for (i = 0; inc && i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) {
132                 dun[i] += inc;                    124                 dun[i] += inc;
133                 /*                                125                 /*
134                  * If the addition in this lim    126                  * If the addition in this limb overflowed, then we need to
135                  * carry 1 into the next limb.    127                  * carry 1 into the next limb. Else the carry is 0.
136                  */                               128                  */
137                 if (dun[i] < inc)                 129                 if (dun[i] < inc)
138                         inc = 1;                  130                         inc = 1;
139                 else                              131                 else
140                         inc = 0;                  132                         inc = 0;
141         }                                         133         }
142 }                                                 134 }
143                                                   135 
144 void __bio_crypt_advance(struct bio *bio, unsi    136 void __bio_crypt_advance(struct bio *bio, unsigned int bytes)
145 {                                                 137 {
146         struct bio_crypt_ctx *bc = bio->bi_cry    138         struct bio_crypt_ctx *bc = bio->bi_crypt_context;
147                                                   139 
148         bio_crypt_dun_increment(bc->bc_dun,       140         bio_crypt_dun_increment(bc->bc_dun,
149                                 bytes >> bc->b    141                                 bytes >> bc->bc_key->data_unit_size_bits);
150 }                                                 142 }
151                                                   143 
152 /*                                                144 /*
153  * Returns true if @bc->bc_dun plus @bytes con    145  * Returns true if @bc->bc_dun plus @bytes converted to data units is equal to
154  * @next_dun, treating the DUNs as multi-limb     146  * @next_dun, treating the DUNs as multi-limb integers.
155  */                                               147  */
156 bool bio_crypt_dun_is_contiguous(const struct     148 bool bio_crypt_dun_is_contiguous(const struct bio_crypt_ctx *bc,
157                                  unsigned int     149                                  unsigned int bytes,
158                                  const u64 nex    150                                  const u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE])
159 {                                                 151 {
160         int i;                                    152         int i;
161         unsigned int carry = bytes >> bc->bc_k    153         unsigned int carry = bytes >> bc->bc_key->data_unit_size_bits;
162                                                   154 
163         for (i = 0; i < BLK_CRYPTO_DUN_ARRAY_S    155         for (i = 0; i < BLK_CRYPTO_DUN_ARRAY_SIZE; i++) {
164                 if (bc->bc_dun[i] + carry != n    156                 if (bc->bc_dun[i] + carry != next_dun[i])
165                         return false;             157                         return false;
166                 /*                                158                 /*
167                  * If the addition in this lim    159                  * If the addition in this limb overflowed, then we need to
168                  * carry 1 into the next limb.    160                  * carry 1 into the next limb. Else the carry is 0.
169                  */                               161                  */
170                 if ((bc->bc_dun[i] + carry) <     162                 if ((bc->bc_dun[i] + carry) < carry)
171                         carry = 1;                163                         carry = 1;
172                 else                              164                 else
173                         carry = 0;                165                         carry = 0;
174         }                                         166         }
175                                                   167 
176         /* If the DUN wrapped through 0, don't    168         /* If the DUN wrapped through 0, don't treat it as contiguous. */
177         return carry == 0;                        169         return carry == 0;
178 }                                                 170 }
179                                                   171 
180 /*                                                172 /*
181  * Checks that two bio crypt contexts are comp    173  * Checks that two bio crypt contexts are compatible - i.e. that
182  * they are mergeable except for data_unit_num    174  * they are mergeable except for data_unit_num continuity.
183  */                                               175  */
184 static bool bio_crypt_ctx_compatible(struct bi    176 static bool bio_crypt_ctx_compatible(struct bio_crypt_ctx *bc1,
185                                      struct bi    177                                      struct bio_crypt_ctx *bc2)
186 {                                                 178 {
187         if (!bc1)                                 179         if (!bc1)
188                 return !bc2;                      180                 return !bc2;
189                                                   181 
190         return bc2 && bc1->bc_key == bc2->bc_k    182         return bc2 && bc1->bc_key == bc2->bc_key;
191 }                                                 183 }
192                                                   184 
193 bool bio_crypt_rq_ctx_compatible(struct reques    185 bool bio_crypt_rq_ctx_compatible(struct request *rq, struct bio *bio)
194 {                                                 186 {
195         return bio_crypt_ctx_compatible(rq->cr    187         return bio_crypt_ctx_compatible(rq->crypt_ctx, bio->bi_crypt_context);
196 }                                                 188 }
197                                                   189 
198 /*                                                190 /*
199  * Checks that two bio crypt contexts are comp    191  * Checks that two bio crypt contexts are compatible, and also
200  * that their data_unit_nums are continuous (a    192  * that their data_unit_nums are continuous (and can hence be merged)
201  * in the order @bc1 followed by @bc2.            193  * in the order @bc1 followed by @bc2.
202  */                                               194  */
203 bool bio_crypt_ctx_mergeable(struct bio_crypt_    195 bool bio_crypt_ctx_mergeable(struct bio_crypt_ctx *bc1, unsigned int bc1_bytes,
204                              struct bio_crypt_    196                              struct bio_crypt_ctx *bc2)
205 {                                                 197 {
206         if (!bio_crypt_ctx_compatible(bc1, bc2    198         if (!bio_crypt_ctx_compatible(bc1, bc2))
207                 return false;                     199                 return false;
208                                                   200 
209         return !bc1 || bio_crypt_dun_is_contig    201         return !bc1 || bio_crypt_dun_is_contiguous(bc1, bc1_bytes, bc2->bc_dun);
210 }                                                 202 }
211                                                   203 
212 /* Check that all I/O segments are data unit a    204 /* Check that all I/O segments are data unit aligned. */
213 static bool bio_crypt_check_alignment(struct b    205 static bool bio_crypt_check_alignment(struct bio *bio)
214 {                                                 206 {
215         const unsigned int data_unit_size =       207         const unsigned int data_unit_size =
216                 bio->bi_crypt_context->bc_key-    208                 bio->bi_crypt_context->bc_key->crypto_cfg.data_unit_size;
217         struct bvec_iter iter;                    209         struct bvec_iter iter;
218         struct bio_vec bv;                        210         struct bio_vec bv;
219                                                   211 
220         bio_for_each_segment(bv, bio, iter) {     212         bio_for_each_segment(bv, bio, iter) {
221                 if (!IS_ALIGNED(bv.bv_len | bv    213                 if (!IS_ALIGNED(bv.bv_len | bv.bv_offset, data_unit_size))
222                         return false;             214                         return false;
223         }                                         215         }
224                                                   216 
225         return true;                              217         return true;
226 }                                                 218 }
227                                                   219 
228 blk_status_t __blk_crypto_rq_get_keyslot(struc    220 blk_status_t __blk_crypto_rq_get_keyslot(struct request *rq)
229 {                                                 221 {
230         return blk_crypto_get_keyslot(rq->q->c !! 222         return blk_ksm_get_slot_for_key(rq->q->ksm, rq->crypt_ctx->bc_key,
231                                       rq->cryp !! 223                                         &rq->crypt_keyslot);
232                                       &rq->cry << 
233 }                                                 224 }
234                                                   225 
235 void __blk_crypto_rq_put_keyslot(struct reques    226 void __blk_crypto_rq_put_keyslot(struct request *rq)
236 {                                                 227 {
237         blk_crypto_put_keyslot(rq->crypt_keysl !! 228         blk_ksm_put_slot(rq->crypt_keyslot);
238         rq->crypt_keyslot = NULL;                 229         rq->crypt_keyslot = NULL;
239 }                                                 230 }
240                                                   231 
241 void __blk_crypto_free_request(struct request     232 void __blk_crypto_free_request(struct request *rq)
242 {                                                 233 {
243         /* The keyslot, if one was needed, sho    234         /* The keyslot, if one was needed, should have been released earlier. */
244         if (WARN_ON_ONCE(rq->crypt_keyslot))      235         if (WARN_ON_ONCE(rq->crypt_keyslot))
245                 __blk_crypto_rq_put_keyslot(rq    236                 __blk_crypto_rq_put_keyslot(rq);
246                                                   237 
247         mempool_free(rq->crypt_ctx, bio_crypt_    238         mempool_free(rq->crypt_ctx, bio_crypt_ctx_pool);
248         rq->crypt_ctx = NULL;                     239         rq->crypt_ctx = NULL;
249 }                                                 240 }
250                                                   241 
251 /**                                               242 /**
252  * __blk_crypto_bio_prep - Prepare bio for inl    243  * __blk_crypto_bio_prep - Prepare bio for inline encryption
253  *                                                244  *
254  * @bio_ptr: pointer to original bio pointer      245  * @bio_ptr: pointer to original bio pointer
255  *                                                246  *
256  * If the bio crypt context provided for the b    247  * If the bio crypt context provided for the bio is supported by the underlying
257  * device's inline encryption hardware, do not    248  * device's inline encryption hardware, do nothing.
258  *                                                249  *
259  * Otherwise, try to perform en/decryption for    250  * Otherwise, try to perform en/decryption for this bio by falling back to the
260  * kernel crypto API. When the crypto API fall    251  * kernel crypto API. When the crypto API fallback is used for encryption,
261  * blk-crypto may choose to split the bio into    252  * blk-crypto may choose to split the bio into 2 - the first one that will
262  * continue to be processed and the second one    253  * continue to be processed and the second one that will be resubmitted via
263  * submit_bio_noacct. A bounce bio will be all    254  * submit_bio_noacct. A bounce bio will be allocated to encrypt the contents
264  * of the aforementioned "first one", and *bio    255  * of the aforementioned "first one", and *bio_ptr will be updated to this
265  * bounce bio.                                    256  * bounce bio.
266  *                                                257  *
267  * Caller must ensure bio has bio_crypt_ctx.      258  * Caller must ensure bio has bio_crypt_ctx.
268  *                                                259  *
269  * Return: true on success; false on error (an    260  * Return: true on success; false on error (and bio->bi_status will be set
270  *         appropriately, and bio_endio() will    261  *         appropriately, and bio_endio() will have been called so bio
271  *         submission should abort).              262  *         submission should abort).
272  */                                               263  */
273 bool __blk_crypto_bio_prep(struct bio **bio_pt    264 bool __blk_crypto_bio_prep(struct bio **bio_ptr)
274 {                                                 265 {
275         struct bio *bio = *bio_ptr;               266         struct bio *bio = *bio_ptr;
276         const struct blk_crypto_key *bc_key =     267         const struct blk_crypto_key *bc_key = bio->bi_crypt_context->bc_key;
277                                                   268 
278         /* Error if bio has no data. */           269         /* Error if bio has no data. */
279         if (WARN_ON_ONCE(!bio_has_data(bio)))     270         if (WARN_ON_ONCE(!bio_has_data(bio))) {
280                 bio->bi_status = BLK_STS_IOERR    271                 bio->bi_status = BLK_STS_IOERR;
281                 goto fail;                        272                 goto fail;
282         }                                         273         }
283                                                   274 
284         if (!bio_crypt_check_alignment(bio)) {    275         if (!bio_crypt_check_alignment(bio)) {
285                 bio->bi_status = BLK_STS_IOERR    276                 bio->bi_status = BLK_STS_IOERR;
286                 goto fail;                        277                 goto fail;
287         }                                         278         }
288                                                   279 
289         /*                                        280         /*
290          * Success if device supports the encr    281          * Success if device supports the encryption context, or if we succeeded
291          * in falling back to the crypto API.     282          * in falling back to the crypto API.
292          */                                       283          */
293         if (blk_crypto_config_supported_native !! 284         if (blk_ksm_crypto_cfg_supported(bio->bi_bdev->bd_disk->queue->ksm,
294                                                !! 285                                          &bc_key->crypto_cfg))
295                 return true;                      286                 return true;
                                                   >> 287 
296         if (blk_crypto_fallback_bio_prep(bio_p    288         if (blk_crypto_fallback_bio_prep(bio_ptr))
297                 return true;                      289                 return true;
298 fail:                                             290 fail:
299         bio_endio(*bio_ptr);                      291         bio_endio(*bio_ptr);
300         return false;                             292         return false;
301 }                                                 293 }
302                                                   294 
303 int __blk_crypto_rq_bio_prep(struct request *r    295 int __blk_crypto_rq_bio_prep(struct request *rq, struct bio *bio,
304                              gfp_t gfp_mask)      296                              gfp_t gfp_mask)
305 {                                                 297 {
306         if (!rq->crypt_ctx) {                     298         if (!rq->crypt_ctx) {
307                 rq->crypt_ctx = mempool_alloc(    299                 rq->crypt_ctx = mempool_alloc(bio_crypt_ctx_pool, gfp_mask);
308                 if (!rq->crypt_ctx)               300                 if (!rq->crypt_ctx)
309                         return -ENOMEM;           301                         return -ENOMEM;
310         }                                         302         }
311         *rq->crypt_ctx = *bio->bi_crypt_contex    303         *rq->crypt_ctx = *bio->bi_crypt_context;
312         return 0;                                 304         return 0;
313 }                                                 305 }
314                                                   306 
315 /**                                               307 /**
316  * blk_crypto_init_key() - Prepare a key for u    308  * blk_crypto_init_key() - Prepare a key for use with blk-crypto
317  * @blk_key: Pointer to the blk_crypto_key to     309  * @blk_key: Pointer to the blk_crypto_key to initialize.
318  * @raw_key: Pointer to the raw key. Must be t    310  * @raw_key: Pointer to the raw key. Must be the correct length for the chosen
319  *           @crypto_mode; see blk_crypto_mode    311  *           @crypto_mode; see blk_crypto_modes[].
320  * @crypto_mode: identifier for the encryption    312  * @crypto_mode: identifier for the encryption algorithm to use
321  * @dun_bytes: number of bytes that will be us    313  * @dun_bytes: number of bytes that will be used to specify the DUN when this
322  *             key is used                        314  *             key is used
323  * @data_unit_size: the data unit size to use     315  * @data_unit_size: the data unit size to use for en/decryption
324  *                                                316  *
325  * Return: 0 on success, -errno on failure.  T    317  * Return: 0 on success, -errno on failure.  The caller is responsible for
326  *         zeroizing both blk_key and raw_key     318  *         zeroizing both blk_key and raw_key when done with them.
327  */                                               319  */
328 int blk_crypto_init_key(struct blk_crypto_key     320 int blk_crypto_init_key(struct blk_crypto_key *blk_key, const u8 *raw_key,
329                         enum blk_crypto_mode_n    321                         enum blk_crypto_mode_num crypto_mode,
330                         unsigned int dun_bytes    322                         unsigned int dun_bytes,
331                         unsigned int data_unit    323                         unsigned int data_unit_size)
332 {                                                 324 {
333         const struct blk_crypto_mode *mode;       325         const struct blk_crypto_mode *mode;
334                                                   326 
335         memset(blk_key, 0, sizeof(*blk_key));     327         memset(blk_key, 0, sizeof(*blk_key));
336                                                   328 
337         if (crypto_mode >= ARRAY_SIZE(blk_cryp    329         if (crypto_mode >= ARRAY_SIZE(blk_crypto_modes))
338                 return -EINVAL;                   330                 return -EINVAL;
339                                                   331 
340         mode = &blk_crypto_modes[crypto_mode];    332         mode = &blk_crypto_modes[crypto_mode];
341         if (mode->keysize == 0)                   333         if (mode->keysize == 0)
342                 return -EINVAL;                   334                 return -EINVAL;
343                                                   335 
344         if (dun_bytes == 0 || dun_bytes > mode    336         if (dun_bytes == 0 || dun_bytes > mode->ivsize)
345                 return -EINVAL;                   337                 return -EINVAL;
346                                                   338 
347         if (!is_power_of_2(data_unit_size))       339         if (!is_power_of_2(data_unit_size))
348                 return -EINVAL;                   340                 return -EINVAL;
349                                                   341 
350         blk_key->crypto_cfg.crypto_mode = cryp    342         blk_key->crypto_cfg.crypto_mode = crypto_mode;
351         blk_key->crypto_cfg.dun_bytes = dun_by    343         blk_key->crypto_cfg.dun_bytes = dun_bytes;
352         blk_key->crypto_cfg.data_unit_size = d    344         blk_key->crypto_cfg.data_unit_size = data_unit_size;
353         blk_key->data_unit_size_bits = ilog2(d    345         blk_key->data_unit_size_bits = ilog2(data_unit_size);
354         blk_key->size = mode->keysize;            346         blk_key->size = mode->keysize;
355         memcpy(blk_key->raw, raw_key, mode->ke    347         memcpy(blk_key->raw, raw_key, mode->keysize);
356                                                   348 
357         return 0;                                 349         return 0;
358 }                                                 350 }
359                                                   351 
360 bool blk_crypto_config_supported_natively(stru << 
361                                           cons << 
362 {                                              << 
363         return __blk_crypto_cfg_supported(bdev << 
364                                           cfg) << 
365 }                                              << 
366                                                << 
367 /*                                                352 /*
368  * Check if bios with @cfg can be en/decrypted    353  * Check if bios with @cfg can be en/decrypted by blk-crypto (i.e. either the
369  * block_device it's submitted to supports inl !! 354  * request queue it's submitted to supports inline crypto, or the
370  * blk-crypto-fallback is enabled and supports    355  * blk-crypto-fallback is enabled and supports the cfg).
371  */                                               356  */
372 bool blk_crypto_config_supported(struct block_ !! 357 bool blk_crypto_config_supported(struct request_queue *q,
373                                  const struct     358                                  const struct blk_crypto_config *cfg)
374 {                                                 359 {
375         return IS_ENABLED(CONFIG_BLK_INLINE_EN    360         return IS_ENABLED(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) ||
376                blk_crypto_config_supported_nat !! 361                blk_ksm_crypto_cfg_supported(q->ksm, cfg);
377 }                                                 362 }
378                                                   363 
379 /**                                               364 /**
380  * blk_crypto_start_using_key() - Start using     365  * blk_crypto_start_using_key() - Start using a blk_crypto_key on a device
381  * @bdev: block device to operate on           << 
382  * @key: A key to use on the device               366  * @key: A key to use on the device
                                                   >> 367  * @q: the request queue for the device
383  *                                                368  *
384  * Upper layers must call this function to ens    369  * Upper layers must call this function to ensure that either the hardware
385  * supports the key's crypto settings, or the     370  * supports the key's crypto settings, or the crypto API fallback has transforms
386  * for the needed mode allocated and ready to     371  * for the needed mode allocated and ready to go. This function may allocate
387  * an skcipher, and *should not* be called fro    372  * an skcipher, and *should not* be called from the data path, since that might
388  * cause a deadlock                               373  * cause a deadlock
389  *                                                374  *
390  * Return: 0 on success; -ENOPKG if the hardwa    375  * Return: 0 on success; -ENOPKG if the hardware doesn't support the key and
391  *         blk-crypto-fallback is either disab    376  *         blk-crypto-fallback is either disabled or the needed algorithm
392  *         is disabled in the crypto API; or a    377  *         is disabled in the crypto API; or another -errno code.
393  */                                               378  */
394 int blk_crypto_start_using_key(struct block_de !! 379 int blk_crypto_start_using_key(const struct blk_crypto_key *key,
395                                const struct bl !! 380                                struct request_queue *q)
396 {                                                 381 {
397         if (blk_crypto_config_supported_native !! 382         if (blk_ksm_crypto_cfg_supported(q->ksm, &key->crypto_cfg))
398                 return 0;                         383                 return 0;
399         return blk_crypto_fallback_start_using    384         return blk_crypto_fallback_start_using_mode(key->crypto_cfg.crypto_mode);
400 }                                                 385 }
401                                                   386 
402 /**                                               387 /**
403  * blk_crypto_evict_key() - Evict a blk_crypto !! 388  * blk_crypto_evict_key() - Evict a blk_crypto_key from a request_queue
404  * @bdev: a block_device on which I/O using th !! 389  * @q: a request_queue on which I/O using the key may have been done
405  * @key: the key to evict                         390  * @key: the key to evict
406  *                                                391  *
407  * For a given block_device, this function rem !! 392  * For a given request_queue, this function removes the given blk_crypto_key
408  * the keyslot management structures and evict !! 393  * from the keyslot management structures and evicts it from any underlying
409  * keyslot(s) or blk-crypto-fallback keyslot i !! 394  * hardware keyslot(s) or blk-crypto-fallback keyslot it may have been
                                                   >> 395  * programmed into.
410  *                                                396  *
411  * Upper layers must call this before freeing     397  * Upper layers must call this before freeing the blk_crypto_key.  It must be
412  * called for every block_device the key may h !! 398  * called for every request_queue the key may have been used on.  The key must
413  * longer be in use by any I/O when this funct !! 399  * no longer be in use by any I/O when this function is called.
414  *                                                400  *
415  * Context: May sleep.                            401  * Context: May sleep.
416  */                                               402  */
417 void blk_crypto_evict_key(struct block_device  !! 403 void blk_crypto_evict_key(struct request_queue *q,
418                           const struct blk_cry    404                           const struct blk_crypto_key *key)
419 {                                                 405 {
420         struct request_queue *q = bdev_get_que << 
421         int err;                                  406         int err;
422                                                   407 
423         if (blk_crypto_config_supported_native !! 408         if (blk_ksm_crypto_cfg_supported(q->ksm, &key->crypto_cfg))
424                 err = __blk_crypto_evict_key(q !! 409                 err = blk_ksm_evict_key(q->ksm, key);
425         else                                      410         else
426                 err = blk_crypto_fallback_evic    411                 err = blk_crypto_fallback_evict_key(key);
427         /*                                        412         /*
428          * An error can only occur here if the    413          * An error can only occur here if the key failed to be evicted from a
429          * keyslot (due to a hardware or drive    414          * keyslot (due to a hardware or driver issue) or is allegedly still in
430          * use by I/O (due to a kernel bug).      415          * use by I/O (due to a kernel bug).  Even in these cases, the key is
431          * still unlinked from the keyslot man    416          * still unlinked from the keyslot management structures, and the caller
432          * is allowed and expected to free it     417          * is allowed and expected to free it right away.  There's nothing
433          * callers can do to handle errors, so    418          * callers can do to handle errors, so just log them and return void.
434          */                                       419          */
435         if (err)                                  420         if (err)
436                 pr_warn_ratelimited("%pg: erro !! 421                 pr_warn_ratelimited("error %d evicting key\n", err);
437 }                                                 422 }
438 EXPORT_SYMBOL_GPL(blk_crypto_evict_key);          423 EXPORT_SYMBOL_GPL(blk_crypto_evict_key);
439                                                   424 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php