1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright 2019 Google LLC
4 */
5
6 /**
7 * DOC: blk-crypto profiles
8 *
9 * 'struct blk_crypto_profile' contains all generic inline encryption-related
10 * state for a particular inline encryption device. blk_crypto_profile serves
11 * as the way that drivers for inline encryption hardware expose their crypto
12 * capabilities and certain functions (e.g., functions to program and evict
13 * keys) to upper layers. Device drivers that want to support inline encryption
14 * construct a crypto profile, then associate it with the disk's request_queue.
15 *
16 * If the device has keyslots, then its blk_crypto_profile also handles managing
17 * these keyslots in a device-independent way, using the driver-provided
18 * functions to program and evict keys as needed. This includes keeping track
19 * of which key and how many I/O requests are using each keyslot, getting
20 * keyslots for I/O requests, and handling key eviction requests.
21 *
22 * For more information, see Documentation/block/inline-encryption.rst.
23 */
24
25 #define pr_fmt(fmt) "blk-crypto: " fmt
26
27 #include <linux/blk-crypto-profile.h>
28 #include <linux/device.h>
29 #include <linux/atomic.h>
30 #include <linux/mutex.h>
31 #include <linux/pm_runtime.h>
32 #include <linux/wait.h>
33 #include <linux/blkdev.h>
34 #include <linux/blk-integrity.h>
35 #include "blk-crypto-internal.h"
36
37 struct blk_crypto_keyslot {
38 atomic_t slot_refs;
39 struct list_head idle_slot_node;
40 struct hlist_node hash_node;
41 const struct blk_crypto_key *key;
42 struct blk_crypto_profile *profile;
43 };
44
blk_crypto_hw_enter(struct blk_crypto_profile * profile)45 static inline void blk_crypto_hw_enter(struct blk_crypto_profile *profile)
46 {
47 /*
48 * Calling into the driver requires profile->lock held and the device
49 * resumed. But we must resume the device first, since that can acquire
50 * and release profile->lock via blk_crypto_reprogram_all_keys().
51 */
52 if (profile->dev)
53 pm_runtime_get_sync(profile->dev);
54 down_write(&profile->lock);
55 }
56
blk_crypto_hw_exit(struct blk_crypto_profile * profile)57 static inline void blk_crypto_hw_exit(struct blk_crypto_profile *profile)
58 {
59 up_write(&profile->lock);
60 if (profile->dev)
61 pm_runtime_put_sync(profile->dev);
62 }
63
64 /**
65 * blk_crypto_profile_init() - Initialize a blk_crypto_profile
66 * @profile: the blk_crypto_profile to initialize
67 * @num_slots: the number of keyslots
68 *
69 * Storage drivers must call this when starting to set up a blk_crypto_profile,
70 * before filling in additional fields.
71 *
72 * Return: 0 on success, or else a negative error code.
73 */
blk_crypto_profile_init(struct blk_crypto_profile * profile,unsigned int num_slots)74 int blk_crypto_profile_init(struct blk_crypto_profile *profile,
75 unsigned int num_slots)
76 {
77 unsigned int slot;
78 unsigned int i;
79 unsigned int slot_hashtable_size;
80
81 memset(profile, 0, sizeof(*profile));
82
83 /*
84 * profile->lock of an underlying device can nest inside profile->lock
85 * of a device-mapper device, so use a dynamic lock class to avoid
86 * false-positive lockdep reports.
87 */
88 lockdep_register_key(&profile->lockdep_key);
89 __init_rwsem(&profile->lock, "&profile->lock", &profile->lockdep_key);
90
91 if (num_slots == 0)
92 return 0;
93
94 /* Initialize keyslot management data. */
95
96 profile->slots = kvzalloc_objs(profile->slots[0], num_slots);
97 if (!profile->slots)
98 goto err_destroy;
99
100 profile->num_slots = num_slots;
101
102 init_waitqueue_head(&profile->idle_slots_wait_queue);
103 INIT_LIST_HEAD(&profile->idle_slots);
104
105 for (slot = 0; slot < num_slots; slot++) {
106 profile->slots[slot].profile = profile;
107 list_add_tail(&profile->slots[slot].idle_slot_node,
108 &profile->idle_slots);
109 }
110
111 spin_lock_init(&profile->idle_slots_lock);
112
113 slot_hashtable_size = roundup_pow_of_two(num_slots);
114 /*
115 * hash_ptr() assumes bits != 0, so ensure the hash table has at least 2
116 * buckets. This only makes a difference when there is only 1 keyslot.
117 */
118 if (slot_hashtable_size < 2)
119 slot_hashtable_size = 2;
120
121 profile->log_slot_ht_size = ilog2(slot_hashtable_size);
122 profile->slot_hashtable =
123 kvmalloc_objs(profile->slot_hashtable[0], slot_hashtable_size);
124 if (!profile->slot_hashtable)
125 goto err_destroy;
126 for (i = 0; i < slot_hashtable_size; i++)
127 INIT_HLIST_HEAD(&profile->slot_hashtable[i]);
128
129 return 0;
130
131 err_destroy:
132 blk_crypto_profile_destroy(profile);
133 return -ENOMEM;
134 }
135 EXPORT_SYMBOL_GPL(blk_crypto_profile_init);
136
blk_crypto_profile_destroy_callback(void * profile)137 static void blk_crypto_profile_destroy_callback(void *profile)
138 {
139 blk_crypto_profile_destroy(profile);
140 }
141
142 /**
143 * devm_blk_crypto_profile_init() - Resource-managed blk_crypto_profile_init()
144 * @dev: the device which owns the blk_crypto_profile
145 * @profile: the blk_crypto_profile to initialize
146 * @num_slots: the number of keyslots
147 *
148 * Like blk_crypto_profile_init(), but causes blk_crypto_profile_destroy() to be
149 * called automatically on driver detach.
150 *
151 * Return: 0 on success, or else a negative error code.
152 */
devm_blk_crypto_profile_init(struct device * dev,struct blk_crypto_profile * profile,unsigned int num_slots)153 int devm_blk_crypto_profile_init(struct device *dev,
154 struct blk_crypto_profile *profile,
155 unsigned int num_slots)
156 {
157 int err = blk_crypto_profile_init(profile, num_slots);
158
159 if (err)
160 return err;
161
162 return devm_add_action_or_reset(dev,
163 blk_crypto_profile_destroy_callback,
164 profile);
165 }
166 EXPORT_SYMBOL_GPL(devm_blk_crypto_profile_init);
167
168 static inline struct hlist_head *
blk_crypto_hash_bucket_for_key(struct blk_crypto_profile * profile,const struct blk_crypto_key * key)169 blk_crypto_hash_bucket_for_key(struct blk_crypto_profile *profile,
170 const struct blk_crypto_key *key)
171 {
172 return &profile->slot_hashtable[
173 hash_ptr(key, profile->log_slot_ht_size)];
174 }
175
176 static void
blk_crypto_remove_slot_from_lru_list(struct blk_crypto_keyslot * slot)177 blk_crypto_remove_slot_from_lru_list(struct blk_crypto_keyslot *slot)
178 {
179 struct blk_crypto_profile *profile = slot->profile;
180 unsigned long flags;
181
182 spin_lock_irqsave(&profile->idle_slots_lock, flags);
183 list_del(&slot->idle_slot_node);
184 spin_unlock_irqrestore(&profile->idle_slots_lock, flags);
185 }
186
187 static struct blk_crypto_keyslot *
blk_crypto_find_keyslot(struct blk_crypto_profile * profile,const struct blk_crypto_key * key)188 blk_crypto_find_keyslot(struct blk_crypto_profile *profile,
189 const struct blk_crypto_key *key)
190 {
191 const struct hlist_head *head =
192 blk_crypto_hash_bucket_for_key(profile, key);
193 struct blk_crypto_keyslot *slotp;
194
195 hlist_for_each_entry(slotp, head, hash_node) {
196 if (slotp->key == key)
197 return slotp;
198 }
199 return NULL;
200 }
201
202 static struct blk_crypto_keyslot *
blk_crypto_find_and_grab_keyslot(struct blk_crypto_profile * profile,const struct blk_crypto_key * key)203 blk_crypto_find_and_grab_keyslot(struct blk_crypto_profile *profile,
204 const struct blk_crypto_key *key)
205 {
206 struct blk_crypto_keyslot *slot;
207
208 slot = blk_crypto_find_keyslot(profile, key);
209 if (!slot)
210 return NULL;
211 if (atomic_inc_return(&slot->slot_refs) == 1) {
212 /* Took first reference to this slot; remove it from LRU list */
213 blk_crypto_remove_slot_from_lru_list(slot);
214 }
215 return slot;
216 }
217
218 /**
219 * blk_crypto_keyslot_index() - Get the index of a keyslot
220 * @slot: a keyslot that blk_crypto_get_keyslot() returned
221 *
222 * Return: the 0-based index of the keyslot within the device's keyslots.
223 */
blk_crypto_keyslot_index(struct blk_crypto_keyslot * slot)224 unsigned int blk_crypto_keyslot_index(struct blk_crypto_keyslot *slot)
225 {
226 return slot - slot->profile->slots;
227 }
228 EXPORT_SYMBOL_GPL(blk_crypto_keyslot_index);
229
230 /**
231 * blk_crypto_get_keyslot() - Get a keyslot for a key, if needed.
232 * @profile: the crypto profile of the device the key will be used on
233 * @key: the key that will be used
234 * @slot_ptr: If a keyslot is allocated, an opaque pointer to the keyslot struct
235 * will be stored here. blk_crypto_put_keyslot() must be called
236 * later to release it. Otherwise, NULL will be stored here.
237 *
238 * If the device has keyslots, this gets a keyslot that's been programmed with
239 * the specified key. If the key is already in a slot, this reuses it;
240 * otherwise this waits for a slot to become idle and programs the key into it.
241 *
242 * Context: Process context. Takes and releases profile->lock.
243 * Return: BLK_STS_OK on success, meaning that either a keyslot was allocated or
244 * one wasn't needed; or a blk_status_t error on failure.
245 */
blk_crypto_get_keyslot(struct blk_crypto_profile * profile,const struct blk_crypto_key * key,struct blk_crypto_keyslot ** slot_ptr)246 blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile,
247 const struct blk_crypto_key *key,
248 struct blk_crypto_keyslot **slot_ptr)
249 {
250 struct blk_crypto_keyslot *slot;
251 int slot_idx;
252 int err;
253
254 *slot_ptr = NULL;
255
256 /*
257 * If the device has no concept of "keyslots", then there is no need to
258 * get one.
259 */
260 if (profile->num_slots == 0)
261 return BLK_STS_OK;
262
263 down_read(&profile->lock);
264 slot = blk_crypto_find_and_grab_keyslot(profile, key);
265 up_read(&profile->lock);
266 if (slot)
267 goto success;
268
269 for (;;) {
270 blk_crypto_hw_enter(profile);
271 slot = blk_crypto_find_and_grab_keyslot(profile, key);
272 if (slot) {
273 blk_crypto_hw_exit(profile);
274 goto success;
275 }
276
277 /*
278 * If we're here, that means there wasn't a slot that was
279 * already programmed with the key. So try to program it.
280 */
281 if (!list_empty(&profile->idle_slots))
282 break;
283
284 blk_crypto_hw_exit(profile);
285 wait_event(profile->idle_slots_wait_queue,
286 !list_empty(&profile->idle_slots));
287 }
288
289 slot = list_first_entry(&profile->idle_slots, struct blk_crypto_keyslot,
290 idle_slot_node);
291 slot_idx = blk_crypto_keyslot_index(slot);
292
293 err = profile->ll_ops.keyslot_program(profile, key, slot_idx);
294 if (err) {
295 wake_up(&profile->idle_slots_wait_queue);
296 blk_crypto_hw_exit(profile);
297 return errno_to_blk_status(err);
298 }
299
300 /* Move this slot to the hash list for the new key. */
301 if (slot->key)
302 hlist_del(&slot->hash_node);
303 slot->key = key;
304 hlist_add_head(&slot->hash_node,
305 blk_crypto_hash_bucket_for_key(profile, key));
306
307 atomic_set(&slot->slot_refs, 1);
308
309 blk_crypto_remove_slot_from_lru_list(slot);
310
311 blk_crypto_hw_exit(profile);
312 success:
313 *slot_ptr = slot;
314 return BLK_STS_OK;
315 }
316
317 /**
318 * blk_crypto_put_keyslot() - Release a reference to a keyslot
319 * @slot: The keyslot to release the reference of
320 *
321 * Context: Any context.
322 */
blk_crypto_put_keyslot(struct blk_crypto_keyslot * slot)323 void blk_crypto_put_keyslot(struct blk_crypto_keyslot *slot)
324 {
325 struct blk_crypto_profile *profile = slot->profile;
326 unsigned long flags;
327
328 if (atomic_dec_and_lock_irqsave(&slot->slot_refs,
329 &profile->idle_slots_lock, flags)) {
330 list_add_tail(&slot->idle_slot_node, &profile->idle_slots);
331 spin_unlock_irqrestore(&profile->idle_slots_lock, flags);
332 wake_up(&profile->idle_slots_wait_queue);
333 }
334 }
335
336 /**
337 * __blk_crypto_cfg_supported() - Check whether the given crypto profile
338 * supports the given crypto configuration.
339 * @profile: the crypto profile to check
340 * @cfg: the crypto configuration to check for
341 *
342 * Return: %true if @profile supports the given @cfg.
343 */
__blk_crypto_cfg_supported(struct blk_crypto_profile * profile,const struct blk_crypto_config * cfg)344 bool __blk_crypto_cfg_supported(struct blk_crypto_profile *profile,
345 const struct blk_crypto_config *cfg)
346 {
347 if (!profile)
348 return false;
349 if (!(profile->modes_supported[cfg->crypto_mode] & cfg->data_unit_size))
350 return false;
351 if (profile->max_dun_bytes_supported < cfg->dun_bytes)
352 return false;
353 if (!(profile->key_types_supported & cfg->key_type))
354 return false;
355 return true;
356 }
357
358 /*
359 * This is an internal function that evicts a key from an inline encryption
360 * device that can be either a real device or the blk-crypto-fallback "device".
361 * It is used only by blk_crypto_evict_key(); see that function for details.
362 */
__blk_crypto_evict_key(struct blk_crypto_profile * profile,const struct blk_crypto_key * key)363 int __blk_crypto_evict_key(struct blk_crypto_profile *profile,
364 const struct blk_crypto_key *key)
365 {
366 struct blk_crypto_keyslot *slot;
367 int err;
368
369 if (profile->num_slots == 0) {
370 if (profile->ll_ops.keyslot_evict) {
371 blk_crypto_hw_enter(profile);
372 err = profile->ll_ops.keyslot_evict(profile, key, -1);
373 blk_crypto_hw_exit(profile);
374 return err;
375 }
376 return 0;
377 }
378
379 blk_crypto_hw_enter(profile);
380 slot = blk_crypto_find_keyslot(profile, key);
381 if (!slot) {
382 /*
383 * Not an error, since a key not in use by I/O is not guaranteed
384 * to be in a keyslot. There can be more keys than keyslots.
385 */
386 err = 0;
387 goto out;
388 }
389
390 if (WARN_ON_ONCE(atomic_read(&slot->slot_refs) != 0)) {
391 /* BUG: key is still in use by I/O */
392 err = -EBUSY;
393 goto out_remove;
394 }
395 err = profile->ll_ops.keyslot_evict(profile, key,
396 blk_crypto_keyslot_index(slot));
397 out_remove:
398 /*
399 * Callers free the key even on error, so unlink the key from the hash
400 * table and clear slot->key even on error.
401 */
402 hlist_del(&slot->hash_node);
403 slot->key = NULL;
404 out:
405 blk_crypto_hw_exit(profile);
406 return err;
407 }
408
409 /**
410 * blk_crypto_reprogram_all_keys() - Re-program all keyslots.
411 * @profile: The crypto profile
412 *
413 * Re-program all keyslots that are supposed to have a key programmed. This is
414 * intended only for use by drivers for hardware that loses its keys on reset.
415 *
416 * Context: Process context. Takes and releases profile->lock.
417 */
blk_crypto_reprogram_all_keys(struct blk_crypto_profile * profile)418 void blk_crypto_reprogram_all_keys(struct blk_crypto_profile *profile)
419 {
420 unsigned int slot;
421
422 if (profile->num_slots == 0)
423 return;
424
425 /* This is for device initialization, so don't resume the device */
426 down_write(&profile->lock);
427 for (slot = 0; slot < profile->num_slots; slot++) {
428 const struct blk_crypto_key *key = profile->slots[slot].key;
429 int err;
430
431 if (!key)
432 continue;
433
434 err = profile->ll_ops.keyslot_program(profile, key, slot);
435 WARN_ON(err);
436 }
437 up_write(&profile->lock);
438 }
439 EXPORT_SYMBOL_GPL(blk_crypto_reprogram_all_keys);
440
blk_crypto_profile_destroy(struct blk_crypto_profile * profile)441 void blk_crypto_profile_destroy(struct blk_crypto_profile *profile)
442 {
443 if (!profile)
444 return;
445 lockdep_unregister_key(&profile->lockdep_key);
446 kvfree(profile->slot_hashtable);
447 kvfree_sensitive(profile->slots,
448 sizeof(profile->slots[0]) * profile->num_slots);
449 memzero_explicit(profile, sizeof(*profile));
450 }
451 EXPORT_SYMBOL_GPL(blk_crypto_profile_destroy);
452
blk_crypto_register(struct blk_crypto_profile * profile,struct request_queue * q)453 bool blk_crypto_register(struct blk_crypto_profile *profile,
454 struct request_queue *q)
455 {
456 if (blk_integrity_queue_supports_integrity(q)) {
457 pr_warn("Integrity and hardware inline encryption are not supported together. Disabling hardware inline encryption.\n");
458 return false;
459 }
460 q->crypto_profile = profile;
461 return true;
462 }
463 EXPORT_SYMBOL_GPL(blk_crypto_register);
464
465 /**
466 * blk_crypto_derive_sw_secret() - Derive software secret from wrapped key
467 * @bdev: a block device that supports hardware-wrapped keys
468 * @eph_key: a hardware-wrapped key in ephemerally-wrapped form
469 * @eph_key_size: size of @eph_key in bytes
470 * @sw_secret: (output) the software secret
471 *
472 * Given a hardware-wrapped key in ephemerally-wrapped form (the same form that
473 * it is used for I/O), ask the hardware to derive the secret which software can
474 * use for cryptographic tasks other than inline encryption. This secret is
475 * guaranteed to be cryptographically isolated from the inline encryption key,
476 * i.e. derived with a different KDF context.
477 *
478 * Return: 0 on success, -EOPNOTSUPP if the block device doesn't support
479 * hardware-wrapped keys, -EBADMSG if the key isn't a valid
480 * ephemerally-wrapped key, or another -errno code.
481 */
blk_crypto_derive_sw_secret(struct block_device * bdev,const u8 * eph_key,size_t eph_key_size,u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE])482 int blk_crypto_derive_sw_secret(struct block_device *bdev,
483 const u8 *eph_key, size_t eph_key_size,
484 u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE])
485 {
486 struct blk_crypto_profile *profile =
487 bdev_get_queue(bdev)->crypto_profile;
488 int err;
489
490 if (!profile)
491 return -EOPNOTSUPP;
492 if (!(profile->key_types_supported & BLK_CRYPTO_KEY_TYPE_HW_WRAPPED))
493 return -EOPNOTSUPP;
494 if (!profile->ll_ops.derive_sw_secret)
495 return -EOPNOTSUPP;
496 blk_crypto_hw_enter(profile);
497 err = profile->ll_ops.derive_sw_secret(profile, eph_key, eph_key_size,
498 sw_secret);
499 blk_crypto_hw_exit(profile);
500 return err;
501 }
502 EXPORT_SYMBOL_GPL(blk_crypto_derive_sw_secret);
503
blk_crypto_import_key(struct blk_crypto_profile * profile,const u8 * raw_key,size_t raw_key_size,u8 lt_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE])504 int blk_crypto_import_key(struct blk_crypto_profile *profile,
505 const u8 *raw_key, size_t raw_key_size,
506 u8 lt_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE])
507 {
508 int ret;
509
510 if (!profile)
511 return -EOPNOTSUPP;
512 if (!(profile->key_types_supported & BLK_CRYPTO_KEY_TYPE_HW_WRAPPED))
513 return -EOPNOTSUPP;
514 if (!profile->ll_ops.import_key)
515 return -EOPNOTSUPP;
516 blk_crypto_hw_enter(profile);
517 ret = profile->ll_ops.import_key(profile, raw_key, raw_key_size,
518 lt_key);
519 blk_crypto_hw_exit(profile);
520 return ret;
521 }
522 EXPORT_SYMBOL_GPL(blk_crypto_import_key);
523
blk_crypto_generate_key(struct blk_crypto_profile * profile,u8 lt_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE])524 int blk_crypto_generate_key(struct blk_crypto_profile *profile,
525 u8 lt_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE])
526 {
527 int ret;
528
529 if (!profile)
530 return -EOPNOTSUPP;
531 if (!(profile->key_types_supported & BLK_CRYPTO_KEY_TYPE_HW_WRAPPED))
532 return -EOPNOTSUPP;
533 if (!profile->ll_ops.generate_key)
534 return -EOPNOTSUPP;
535 blk_crypto_hw_enter(profile);
536 ret = profile->ll_ops.generate_key(profile, lt_key);
537 blk_crypto_hw_exit(profile);
538 return ret;
539 }
540 EXPORT_SYMBOL_GPL(blk_crypto_generate_key);
541
blk_crypto_prepare_key(struct blk_crypto_profile * profile,const u8 * lt_key,size_t lt_key_size,u8 eph_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE])542 int blk_crypto_prepare_key(struct blk_crypto_profile *profile,
543 const u8 *lt_key, size_t lt_key_size,
544 u8 eph_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE])
545 {
546 int ret;
547
548 if (!profile)
549 return -EOPNOTSUPP;
550 if (!(profile->key_types_supported & BLK_CRYPTO_KEY_TYPE_HW_WRAPPED))
551 return -EOPNOTSUPP;
552 if (!profile->ll_ops.prepare_key)
553 return -EOPNOTSUPP;
554 blk_crypto_hw_enter(profile);
555 ret = profile->ll_ops.prepare_key(profile, lt_key, lt_key_size,
556 eph_key);
557 blk_crypto_hw_exit(profile);
558 return ret;
559 }
560 EXPORT_SYMBOL_GPL(blk_crypto_prepare_key);
561
562 /**
563 * blk_crypto_intersect_capabilities() - restrict supported crypto capabilities
564 * by child device
565 * @parent: the crypto profile for the parent device
566 * @child: the crypto profile for the child device, or NULL
567 *
568 * This clears all crypto capabilities in @parent that aren't set in @child. If
569 * @child is NULL, then this clears all parent capabilities.
570 *
571 * Only use this when setting up the crypto profile for a layered device, before
572 * it's been exposed yet.
573 */
blk_crypto_intersect_capabilities(struct blk_crypto_profile * parent,const struct blk_crypto_profile * child)574 void blk_crypto_intersect_capabilities(struct blk_crypto_profile *parent,
575 const struct blk_crypto_profile *child)
576 {
577 if (child) {
578 unsigned int i;
579
580 parent->max_dun_bytes_supported =
581 min(parent->max_dun_bytes_supported,
582 child->max_dun_bytes_supported);
583 for (i = 0; i < ARRAY_SIZE(child->modes_supported); i++)
584 parent->modes_supported[i] &= child->modes_supported[i];
585 parent->key_types_supported &= child->key_types_supported;
586 } else {
587 parent->max_dun_bytes_supported = 0;
588 memset(parent->modes_supported, 0,
589 sizeof(parent->modes_supported));
590 parent->key_types_supported = 0;
591 }
592 }
593 EXPORT_SYMBOL_GPL(blk_crypto_intersect_capabilities);
594
595 /**
596 * blk_crypto_has_capabilities() - Check whether @target supports at least all
597 * the crypto capabilities that @reference does.
598 * @target: the target profile
599 * @reference: the reference profile
600 *
601 * Return: %true if @target supports all the crypto capabilities of @reference.
602 */
blk_crypto_has_capabilities(const struct blk_crypto_profile * target,const struct blk_crypto_profile * reference)603 bool blk_crypto_has_capabilities(const struct blk_crypto_profile *target,
604 const struct blk_crypto_profile *reference)
605 {
606 int i;
607
608 if (!reference)
609 return true;
610
611 if (!target)
612 return false;
613
614 for (i = 0; i < ARRAY_SIZE(target->modes_supported); i++) {
615 if (reference->modes_supported[i] & ~target->modes_supported[i])
616 return false;
617 }
618
619 if (reference->max_dun_bytes_supported >
620 target->max_dun_bytes_supported)
621 return false;
622
623 if (reference->key_types_supported & ~target->key_types_supported)
624 return false;
625
626 return true;
627 }
628 EXPORT_SYMBOL_GPL(blk_crypto_has_capabilities);
629
630 /**
631 * blk_crypto_update_capabilities() - Update the capabilities of a crypto
632 * profile to match those of another crypto
633 * profile.
634 * @dst: The crypto profile whose capabilities to update.
635 * @src: The crypto profile whose capabilities this function will update @dst's
636 * capabilities to.
637 *
638 * Blk-crypto requires that crypto capabilities that were
639 * advertised when a bio was created continue to be supported by the
640 * device until that bio is ended. This is turn means that a device cannot
641 * shrink its advertised crypto capabilities without any explicit
642 * synchronization with upper layers. So if there's no such explicit
643 * synchronization, @src must support all the crypto capabilities that
644 * @dst does (i.e. we need blk_crypto_has_capabilities(@src, @dst)).
645 *
646 * Note also that as long as the crypto capabilities are being expanded, the
647 * order of updates becoming visible is not important because it's alright
648 * for blk-crypto to see stale values - they only cause blk-crypto to
649 * believe that a crypto capability isn't supported when it actually is (which
650 * might result in blk-crypto-fallback being used if available, or the bio being
651 * failed).
652 */
blk_crypto_update_capabilities(struct blk_crypto_profile * dst,const struct blk_crypto_profile * src)653 void blk_crypto_update_capabilities(struct blk_crypto_profile *dst,
654 const struct blk_crypto_profile *src)
655 {
656 memcpy(dst->modes_supported, src->modes_supported,
657 sizeof(dst->modes_supported));
658
659 dst->max_dun_bytes_supported = src->max_dun_bytes_supported;
660 dst->key_types_supported = src->key_types_supported;
661 }
662 EXPORT_SYMBOL_GPL(blk_crypto_update_capabilities);
663