blk-crypto.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright 2019 Google LLC
  4. */
  5. #ifndef __LINUX_BLK_CRYPTO_H
  6. #define __LINUX_BLK_CRYPTO_H
  7. #include <linux/types.h>
  8. enum blk_crypto_mode_num {
  9. BLK_ENCRYPTION_MODE_INVALID,
  10. BLK_ENCRYPTION_MODE_AES_256_XTS,
  11. BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV,
  12. BLK_ENCRYPTION_MODE_ADIANTUM,
  13. BLK_ENCRYPTION_MODE_SM4_XTS,
  14. BLK_ENCRYPTION_MODE_MAX,
  15. };
  16. /*
  17. * Supported types of keys. Must be bitflags due to their use in
  18. * blk_crypto_profile::key_types_supported.
  19. */
  20. enum blk_crypto_key_type {
  21. /*
  22. * Standard keys (i.e. "software keys"). These keys are simply kept in
  23. * raw, plaintext form in kernel memory.
  24. */
  25. BLK_CRYPTO_KEY_TYPE_STANDARD = 1 << 0,
  26. /*
  27. * Hardware-wrapped keys. These keys are only present in kernel memory
  28. * in ephemerally-wrapped form, and they can only be unwrapped by
  29. * dedicated hardware. For details, see the "Hardware-wrapped keys"
  30. * section of Documentation/block/inline-encryption.rst.
  31. */
  32. BLK_CRYPTO_KEY_TYPE_HW_WRAPPED = 1 << 1,
  33. };
  34. /*
  35. * Currently the maximum standard key size is 64 bytes, as that is the key size
  36. * of BLK_ENCRYPTION_MODE_AES_256_XTS which takes the longest key.
  37. *
  38. * The maximum hardware-wrapped key size depends on the hardware's key wrapping
  39. * algorithm, which is a hardware implementation detail, so it isn't precisely
  40. * specified. But currently 128 bytes is plenty in practice. Implementations
  41. * are recommended to wrap a 32-byte key for the hardware KDF with AES-256-GCM,
  42. * which should result in a size closer to 64 bytes than 128.
  43. *
  44. * Both of these values can trivially be increased if ever needed.
  45. */
  46. #define BLK_CRYPTO_MAX_STANDARD_KEY_SIZE 64
  47. #define BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE 128
  48. /* This should use max(), but max() doesn't work in a struct definition. */
  49. #define BLK_CRYPTO_MAX_ANY_KEY_SIZE \
  50. (BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE > \
  51. BLK_CRYPTO_MAX_STANDARD_KEY_SIZE ? \
  52. BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE : BLK_CRYPTO_MAX_STANDARD_KEY_SIZE)
  53. /*
  54. * Size of the "software secret" which can be derived from a hardware-wrapped
  55. * key. This is currently always 32 bytes. Note, the choice of 32 bytes
  56. * assumes that the software secret is only used directly for algorithms that
  57. * don't require more than a 256-bit key to get the desired security strength.
  58. * If it were to be used e.g. directly as an AES-256-XTS key, then this would
  59. * need to be increased (which is possible if hardware supports it, but care
  60. * would need to be taken to avoid breaking users who need exactly 32 bytes).
  61. */
  62. #define BLK_CRYPTO_SW_SECRET_SIZE 32
  63. /**
  64. * struct blk_crypto_config - an inline encryption key's crypto configuration
  65. * @crypto_mode: encryption algorithm this key is for
  66. * @data_unit_size: the data unit size for all encryption/decryptions with this
  67. * key. This is the size in bytes of each individual plaintext and
  68. * ciphertext. This is always a power of 2. It might be e.g. the
  69. * filesystem block size or the disk sector size.
  70. * @dun_bytes: the maximum number of bytes of DUN used when using this key
  71. * @key_type: the type of this key -- either standard or hardware-wrapped
  72. */
  73. struct blk_crypto_config {
  74. enum blk_crypto_mode_num crypto_mode;
  75. unsigned int data_unit_size;
  76. unsigned int dun_bytes;
  77. enum blk_crypto_key_type key_type;
  78. };
  79. /**
  80. * struct blk_crypto_key - an inline encryption key
  81. * @crypto_cfg: the crypto mode, data unit size, key type, and other
  82. * characteristics of this key and how it will be used
  83. * @data_unit_size_bits: log2 of data_unit_size
  84. * @size: size of this key in bytes. The size of a standard key is fixed for a
  85. * given crypto mode, but the size of a hardware-wrapped key can vary.
  86. * @raw: the bytes of this key. Only the first @size bytes are significant.
  87. *
  88. * A blk_crypto_key is immutable once created, and many bios can reference it at
  89. * the same time. It must not be freed until all bios using it have completed
  90. * and it has been evicted from all devices on which it may have been used.
  91. */
  92. struct blk_crypto_key {
  93. struct blk_crypto_config crypto_cfg;
  94. unsigned int data_unit_size_bits;
  95. unsigned int size;
  96. u8 raw[BLK_CRYPTO_MAX_ANY_KEY_SIZE];
  97. };
  98. #define BLK_CRYPTO_MAX_IV_SIZE 32
  99. #define BLK_CRYPTO_DUN_ARRAY_SIZE (BLK_CRYPTO_MAX_IV_SIZE / sizeof(u64))
  100. /**
  101. * struct bio_crypt_ctx - an inline encryption context
  102. * @bc_key: the key, algorithm, and data unit size to use
  103. * @bc_dun: the data unit number (starting IV) to use
  104. *
  105. * A bio_crypt_ctx specifies that the contents of the bio will be encrypted (for
  106. * write requests) or decrypted (for read requests) inline by the storage device
  107. * or controller, or by the crypto API fallback.
  108. */
  109. struct bio_crypt_ctx {
  110. const struct blk_crypto_key *bc_key;
  111. u64 bc_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
  112. };
  113. #include <linux/blk_types.h>
  114. #include <linux/blkdev.h>
  115. #ifdef CONFIG_BLK_INLINE_ENCRYPTION
  116. static inline bool bio_has_crypt_ctx(struct bio *bio)
  117. {
  118. return bio->bi_crypt_context;
  119. }
  120. void bio_crypt_set_ctx(struct bio *bio, const struct blk_crypto_key *key,
  121. const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
  122. gfp_t gfp_mask);
  123. bool bio_crypt_dun_is_contiguous(const struct bio_crypt_ctx *bc,
  124. unsigned int bytes,
  125. const u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE]);
  126. int blk_crypto_init_key(struct blk_crypto_key *blk_key,
  127. const u8 *raw_key, size_t raw_key_size,
  128. enum blk_crypto_key_type key_type,
  129. enum blk_crypto_mode_num crypto_mode,
  130. unsigned int dun_bytes,
  131. unsigned int data_unit_size);
  132. int blk_crypto_start_using_key(struct block_device *bdev,
  133. const struct blk_crypto_key *key);
  134. void blk_crypto_evict_key(struct block_device *bdev,
  135. const struct blk_crypto_key *key);
  136. bool blk_crypto_config_supported_natively(struct block_device *bdev,
  137. const struct blk_crypto_config *cfg);
  138. bool blk_crypto_config_supported(struct block_device *bdev,
  139. const struct blk_crypto_config *cfg);
  140. int blk_crypto_derive_sw_secret(struct block_device *bdev,
  141. const u8 *eph_key, size_t eph_key_size,
  142. u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE]);
  143. #else /* CONFIG_BLK_INLINE_ENCRYPTION */
  144. static inline bool bio_has_crypt_ctx(struct bio *bio)
  145. {
  146. return false;
  147. }
  148. #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
  149. static inline void bio_clone_skip_dm_default_key(struct bio *dst,
  150. const struct bio *src);
  151. int __bio_crypt_clone(struct bio *dst, struct bio *src, gfp_t gfp_mask);
  152. /**
  153. * bio_crypt_clone - clone bio encryption context
  154. * @dst: destination bio
  155. * @src: source bio
  156. * @gfp_mask: memory allocation flags
  157. *
  158. * If @src has an encryption context, clone it to @dst.
  159. *
  160. * Return: 0 on success, -ENOMEM if out of memory. -ENOMEM is only possible if
  161. * @gfp_mask doesn't include %__GFP_DIRECT_RECLAIM.
  162. */
  163. static inline int bio_crypt_clone(struct bio *dst, struct bio *src,
  164. gfp_t gfp_mask)
  165. {
  166. bio_clone_skip_dm_default_key(dst, src);
  167. if (bio_has_crypt_ctx(src))
  168. return __bio_crypt_clone(dst, src, gfp_mask);
  169. return 0;
  170. }
  171. #if IS_ENABLED(CONFIG_DM_DEFAULT_KEY)
  172. static inline void bio_set_skip_dm_default_key(struct bio *bio)
  173. {
  174. bio->bi_skip_dm_default_key = true;
  175. }
  176. static inline bool bio_should_skip_dm_default_key(const struct bio *bio)
  177. {
  178. return bio->bi_skip_dm_default_key;
  179. }
  180. static inline void bio_clone_skip_dm_default_key(struct bio *dst,
  181. const struct bio *src)
  182. {
  183. dst->bi_skip_dm_default_key = src->bi_skip_dm_default_key;
  184. }
  185. #else /* CONFIG_DM_DEFAULT_KEY */
  186. static inline void bio_set_skip_dm_default_key(struct bio *bio)
  187. {
  188. }
  189. static inline bool bio_should_skip_dm_default_key(const struct bio *bio)
  190. {
  191. return false;
  192. }
  193. static inline void bio_clone_skip_dm_default_key(struct bio *dst,
  194. const struct bio *src)
  195. {
  196. }
  197. #endif /* !CONFIG_DM_DEFAULT_KEY */
  198. #endif /* __LINUX_BLK_CRYPTO_H */