blk-crypto-profile.c 20 KB

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