blk-crypto.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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_MAX,
  14. };
  15. #define BLK_CRYPTO_MAX_KEY_SIZE 64
  16. #define BLK_CRYPTO_MAX_WRAPPED_KEY_SIZE 128
  17. /**
  18. * struct blk_crypto_config - an inline encryption key's crypto configuration
  19. * @crypto_mode: encryption algorithm this key is for
  20. * @data_unit_size: the data unit size for all encryption/decryptions with this
  21. * key. This is the size in bytes of each individual plaintext and
  22. * ciphertext. This is always a power of 2. It might be e.g. the
  23. * filesystem block size or the disk sector size.
  24. * @dun_bytes: the maximum number of bytes of DUN used when using this key
  25. * @is_hw_wrapped: @raw points to a wrapped key to be used by an inline
  26. * encryption hardware that accepts wrapped keys.
  27. */
  28. struct blk_crypto_config {
  29. enum blk_crypto_mode_num crypto_mode;
  30. unsigned int data_unit_size;
  31. unsigned int dun_bytes;
  32. bool is_hw_wrapped;
  33. };
  34. /**
  35. * struct blk_crypto_key - an inline encryption key
  36. * @crypto_cfg: the crypto configuration (like crypto_mode, key size) for this
  37. * key
  38. * @data_unit_size_bits: log2 of data_unit_size
  39. * @size: size of this key in bytes (determined by @crypto_cfg.crypto_mode)
  40. * @raw: the raw bytes of this key. Only the first @size bytes are used.
  41. *
  42. * A blk_crypto_key is immutable once created, and many bios can reference it at
  43. * the same time. It must not be freed until all bios using it have completed
  44. * and it has been evicted from all devices on which it may have been used.
  45. */
  46. struct blk_crypto_key {
  47. struct blk_crypto_config crypto_cfg;
  48. unsigned int data_unit_size_bits;
  49. unsigned int size;
  50. u8 raw[BLK_CRYPTO_MAX_WRAPPED_KEY_SIZE];
  51. };
  52. #define BLK_CRYPTO_MAX_IV_SIZE 32
  53. #define BLK_CRYPTO_DUN_ARRAY_SIZE (BLK_CRYPTO_MAX_IV_SIZE / sizeof(u64))
  54. /**
  55. * struct bio_crypt_ctx - an inline encryption context
  56. * @bc_key: the key, algorithm, and data unit size to use
  57. * @bc_dun: the data unit number (starting IV) to use
  58. *
  59. * A bio_crypt_ctx specifies that the contents of the bio will be encrypted (for
  60. * write requests) or decrypted (for read requests) inline by the storage device
  61. * or controller, or by the crypto API fallback.
  62. */
  63. struct bio_crypt_ctx {
  64. const struct blk_crypto_key *bc_key;
  65. u64 bc_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
  66. };
  67. #include <linux/blk_types.h>
  68. #include <linux/blkdev.h>
  69. struct request;
  70. struct request_queue;
  71. #ifdef CONFIG_BLK_INLINE_ENCRYPTION
  72. static inline bool bio_has_crypt_ctx(struct bio *bio)
  73. {
  74. return bio->bi_crypt_context;
  75. }
  76. void bio_crypt_set_ctx(struct bio *bio, const struct blk_crypto_key *key,
  77. const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
  78. gfp_t gfp_mask);
  79. bool bio_crypt_dun_is_contiguous(const struct bio_crypt_ctx *bc,
  80. unsigned int bytes,
  81. const u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE]);
  82. int blk_crypto_init_key(struct blk_crypto_key *blk_key,
  83. const u8 *raw_key, unsigned int raw_key_size,
  84. bool is_hw_wrapped,
  85. enum blk_crypto_mode_num crypto_mode,
  86. unsigned int dun_bytes,
  87. unsigned int data_unit_size);
  88. int blk_crypto_start_using_key(const struct blk_crypto_key *key,
  89. struct request_queue *q);
  90. int blk_crypto_evict_key(struct request_queue *q,
  91. const struct blk_crypto_key *key);
  92. bool blk_crypto_config_supported(struct request_queue *q,
  93. const struct blk_crypto_config *cfg);
  94. #else /* CONFIG_BLK_INLINE_ENCRYPTION */
  95. static inline bool bio_has_crypt_ctx(struct bio *bio)
  96. {
  97. return false;
  98. }
  99. #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
  100. static inline void bio_clone_skip_dm_default_key(struct bio *dst,
  101. const struct bio *src);
  102. int __bio_crypt_clone(struct bio *dst, struct bio *src, gfp_t gfp_mask);
  103. /**
  104. * bio_crypt_clone - clone bio encryption context
  105. * @dst: destination bio
  106. * @src: source bio
  107. * @gfp_mask: memory allocation flags
  108. *
  109. * If @src has an encryption context, clone it to @dst.
  110. *
  111. * Return: 0 on success, -ENOMEM if out of memory. -ENOMEM is only possible if
  112. * @gfp_mask doesn't include %__GFP_DIRECT_RECLAIM.
  113. */
  114. static inline int bio_crypt_clone(struct bio *dst, struct bio *src,
  115. gfp_t gfp_mask)
  116. {
  117. bio_clone_skip_dm_default_key(dst, src);
  118. if (bio_has_crypt_ctx(src))
  119. return __bio_crypt_clone(dst, src, gfp_mask);
  120. return 0;
  121. }
  122. #if IS_ENABLED(CONFIG_DM_DEFAULT_KEY)
  123. static inline void bio_set_skip_dm_default_key(struct bio *bio)
  124. {
  125. bio->bi_skip_dm_default_key = true;
  126. }
  127. static inline bool bio_should_skip_dm_default_key(const struct bio *bio)
  128. {
  129. return bio->bi_skip_dm_default_key;
  130. }
  131. static inline void bio_clone_skip_dm_default_key(struct bio *dst,
  132. const struct bio *src)
  133. {
  134. dst->bi_skip_dm_default_key = src->bi_skip_dm_default_key;
  135. }
  136. #else /* CONFIG_DM_DEFAULT_KEY */
  137. static inline void bio_set_skip_dm_default_key(struct bio *bio)
  138. {
  139. }
  140. static inline bool bio_should_skip_dm_default_key(const struct bio *bio)
  141. {
  142. return false;
  143. }
  144. static inline void bio_clone_skip_dm_default_key(struct bio *dst,
  145. const struct bio *src)
  146. {
  147. }
  148. #endif /* !CONFIG_DM_DEFAULT_KEY */
  149. #endif /* __LINUX_BLK_CRYPTO_H */