blk-crypto-profile.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright 2019 Google LLC
  4. */
  5. #ifndef __LINUX_BLK_CRYPTO_PROFILE_H
  6. #define __LINUX_BLK_CRYPTO_PROFILE_H
  7. #include <linux/bio.h>
  8. #include <linux/blk-crypto.h>
  9. struct blk_crypto_profile;
  10. /**
  11. * struct blk_crypto_ll_ops - functions to control inline encryption hardware
  12. *
  13. * Low-level operations for controlling inline encryption hardware. This
  14. * interface must be implemented by storage drivers that support inline
  15. * encryption. All functions may sleep, are serialized by profile->lock, and
  16. * are never called while profile->dev (if set) is runtime-suspended.
  17. */
  18. struct blk_crypto_ll_ops {
  19. /**
  20. * @keyslot_program: Program a key into the inline encryption hardware.
  21. *
  22. * Program @key into the specified @slot in the inline encryption
  23. * hardware, overwriting any key that the keyslot may already contain.
  24. * The keyslot is guaranteed to not be in-use by any I/O.
  25. *
  26. * This is required if the device has keyslots. Otherwise (i.e. if the
  27. * device is a layered device, or if the device is real hardware that
  28. * simply doesn't have the concept of keyslots) it is never called.
  29. *
  30. * Must return 0 on success, or -errno on failure.
  31. */
  32. int (*keyslot_program)(struct blk_crypto_profile *profile,
  33. const struct blk_crypto_key *key,
  34. unsigned int slot);
  35. /**
  36. * @keyslot_evict: Evict a key from the inline encryption hardware.
  37. *
  38. * If the device has keyslots, this function must evict the key from the
  39. * specified @slot. The slot will contain @key, but there should be no
  40. * need for the @key argument to be used as @slot should be sufficient.
  41. * The keyslot is guaranteed to not be in-use by any I/O.
  42. *
  43. * If the device doesn't have keyslots itself, this function must evict
  44. * @key from any underlying devices. @slot won't be valid in this case.
  45. *
  46. * If there are no keyslots and no underlying devices, this function
  47. * isn't required.
  48. *
  49. * Must return 0 on success, or -errno on failure.
  50. */
  51. int (*keyslot_evict)(struct blk_crypto_profile *profile,
  52. const struct blk_crypto_key *key,
  53. unsigned int slot);
  54. /**
  55. * @derive_sw_secret: Derive the software secret from a hardware-wrapped
  56. * key in ephemerally-wrapped form.
  57. *
  58. * This only needs to be implemented if BLK_CRYPTO_KEY_TYPE_HW_WRAPPED
  59. * is supported.
  60. *
  61. * Must return 0 on success, -EBADMSG if the key is invalid, or another
  62. * -errno code on other errors.
  63. */
  64. int (*derive_sw_secret)(struct blk_crypto_profile *profile,
  65. const u8 *eph_key, size_t eph_key_size,
  66. u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE]);
  67. };
  68. /**
  69. * struct blk_crypto_profile - inline encryption profile for a device
  70. *
  71. * This struct contains a storage device's inline encryption capabilities (e.g.
  72. * the supported crypto algorithms), driver-provided functions to control the
  73. * inline encryption hardware (e.g. programming and evicting keys), and optional
  74. * device-independent keyslot management data.
  75. */
  76. struct blk_crypto_profile {
  77. /* public: Drivers must initialize the following fields. */
  78. /**
  79. * @ll_ops: Driver-provided functions to control the inline encryption
  80. * hardware, e.g. program and evict keys.
  81. */
  82. struct blk_crypto_ll_ops ll_ops;
  83. /**
  84. * @max_dun_bytes_supported: The maximum number of bytes supported for
  85. * specifying the data unit number (DUN). Specifically, the range of
  86. * supported DUNs is 0 through (1 << (8 * max_dun_bytes_supported)) - 1.
  87. */
  88. unsigned int max_dun_bytes_supported;
  89. /**
  90. * @key_types_supported: A bitmask of the supported key types:
  91. * BLK_CRYPTO_KEY_TYPE_STANDARD and/or BLK_CRYPTO_KEY_TYPE_HW_WRAPPED.
  92. */
  93. unsigned int key_types_supported;
  94. /**
  95. * @modes_supported: Array of bitmasks that specifies whether each
  96. * combination of crypto mode and data unit size is supported.
  97. * Specifically, the i'th bit of modes_supported[crypto_mode] is set if
  98. * crypto_mode can be used with a data unit size of (1 << i). Note that
  99. * only data unit sizes that are powers of 2 can be supported.
  100. */
  101. unsigned int modes_supported[BLK_ENCRYPTION_MODE_MAX];
  102. /**
  103. * @dev: An optional device for runtime power management. If the driver
  104. * provides this device, it will be runtime-resumed before any function
  105. * in @ll_ops is called and will remain resumed during the call.
  106. */
  107. struct device *dev;
  108. /* private: The following fields shouldn't be accessed by drivers. */
  109. /* Number of keyslots, or 0 if not applicable */
  110. unsigned int num_slots;
  111. /*
  112. * Serializes all calls to functions in @ll_ops as well as all changes
  113. * to @slot_hashtable. This can also be taken in read mode to look up
  114. * keyslots while ensuring that they can't be changed concurrently.
  115. */
  116. struct rw_semaphore lock;
  117. #ifdef CONFIG_LOCKDEP
  118. struct lock_class_key lockdep_key;
  119. #endif
  120. /* List of idle slots, with least recently used slot at front */
  121. wait_queue_head_t idle_slots_wait_queue;
  122. struct list_head idle_slots;
  123. spinlock_t idle_slots_lock;
  124. /*
  125. * Hash table which maps struct *blk_crypto_key to keyslots, so that we
  126. * can find a key's keyslot in O(1) time rather than O(num_slots).
  127. * Protected by 'lock'.
  128. */
  129. struct hlist_head *slot_hashtable;
  130. unsigned int log_slot_ht_size;
  131. /* Per-keyslot data */
  132. struct blk_crypto_keyslot *slots;
  133. };
  134. int blk_crypto_profile_init(struct blk_crypto_profile *profile,
  135. unsigned int num_slots);
  136. int devm_blk_crypto_profile_init(struct device *dev,
  137. struct blk_crypto_profile *profile,
  138. unsigned int num_slots);
  139. unsigned int blk_crypto_keyslot_index(struct blk_crypto_keyslot *slot);
  140. void blk_crypto_reprogram_all_keys(struct blk_crypto_profile *profile);
  141. void blk_crypto_profile_destroy(struct blk_crypto_profile *profile);
  142. void blk_crypto_intersect_capabilities(struct blk_crypto_profile *parent,
  143. const struct blk_crypto_profile *child);
  144. bool blk_crypto_has_capabilities(const struct blk_crypto_profile *target,
  145. const struct blk_crypto_profile *reference);
  146. void blk_crypto_update_capabilities(struct blk_crypto_profile *dst,
  147. const struct blk_crypto_profile *src);
  148. #endif /* __LINUX_BLK_CRYPTO_PROFILE_H */