blk-crypto-internal.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright 2019 Google LLC
  4. */
  5. #ifndef __LINUX_BLK_CRYPTO_INTERNAL_H
  6. #define __LINUX_BLK_CRYPTO_INTERNAL_H
  7. #include <linux/bio.h>
  8. #include <linux/blk-mq.h>
  9. /* Represents a crypto mode supported by blk-crypto */
  10. struct blk_crypto_mode {
  11. const char *name; /* name of this mode, shown in sysfs */
  12. const char *cipher_str; /* crypto API name (for fallback case) */
  13. unsigned int keysize; /* key size in bytes */
  14. unsigned int ivsize; /* iv size in bytes */
  15. };
  16. extern const struct blk_crypto_mode blk_crypto_modes[];
  17. #ifdef CONFIG_BLK_INLINE_ENCRYPTION
  18. int blk_crypto_sysfs_register(struct gendisk *disk);
  19. void blk_crypto_sysfs_unregister(struct gendisk *disk);
  20. void bio_crypt_dun_increment(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
  21. unsigned int inc);
  22. bool bio_crypt_rq_ctx_compatible(struct request *rq, struct bio *bio);
  23. bool bio_crypt_ctx_mergeable(struct bio_crypt_ctx *bc1, unsigned int bc1_bytes,
  24. struct bio_crypt_ctx *bc2);
  25. static inline bool bio_crypt_ctx_back_mergeable(struct request *req,
  26. struct bio *bio)
  27. {
  28. return bio_crypt_ctx_mergeable(req->crypt_ctx, blk_rq_bytes(req),
  29. bio->bi_crypt_context);
  30. }
  31. static inline bool bio_crypt_ctx_front_mergeable(struct request *req,
  32. struct bio *bio)
  33. {
  34. return bio_crypt_ctx_mergeable(bio->bi_crypt_context,
  35. bio->bi_iter.bi_size, req->crypt_ctx);
  36. }
  37. static inline bool bio_crypt_ctx_merge_rq(struct request *req,
  38. struct request *next)
  39. {
  40. return bio_crypt_ctx_mergeable(req->crypt_ctx, blk_rq_bytes(req),
  41. next->crypt_ctx);
  42. }
  43. static inline void blk_crypto_rq_set_defaults(struct request *rq)
  44. {
  45. rq->crypt_ctx = NULL;
  46. rq->crypt_keyslot = NULL;
  47. }
  48. static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
  49. {
  50. return rq->crypt_ctx;
  51. }
  52. static inline bool blk_crypto_rq_has_keyslot(struct request *rq)
  53. {
  54. return rq->crypt_keyslot;
  55. }
  56. blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile,
  57. const struct blk_crypto_key *key,
  58. struct blk_crypto_keyslot **slot_ptr);
  59. void blk_crypto_put_keyslot(struct blk_crypto_keyslot *slot);
  60. int __blk_crypto_evict_key(struct blk_crypto_profile *profile,
  61. const struct blk_crypto_key *key);
  62. bool __blk_crypto_cfg_supported(struct blk_crypto_profile *profile,
  63. const struct blk_crypto_config *cfg);
  64. #else /* CONFIG_BLK_INLINE_ENCRYPTION */
  65. static inline int blk_crypto_sysfs_register(struct gendisk *disk)
  66. {
  67. return 0;
  68. }
  69. static inline void blk_crypto_sysfs_unregister(struct gendisk *disk)
  70. {
  71. }
  72. static inline bool bio_crypt_rq_ctx_compatible(struct request *rq,
  73. struct bio *bio)
  74. {
  75. return true;
  76. }
  77. static inline bool bio_crypt_ctx_front_mergeable(struct request *req,
  78. struct bio *bio)
  79. {
  80. return true;
  81. }
  82. static inline bool bio_crypt_ctx_back_mergeable(struct request *req,
  83. struct bio *bio)
  84. {
  85. return true;
  86. }
  87. static inline bool bio_crypt_ctx_merge_rq(struct request *req,
  88. struct request *next)
  89. {
  90. return true;
  91. }
  92. static inline void blk_crypto_rq_set_defaults(struct request *rq) { }
  93. static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
  94. {
  95. return false;
  96. }
  97. static inline bool blk_crypto_rq_has_keyslot(struct request *rq)
  98. {
  99. return false;
  100. }
  101. #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
  102. void __bio_crypt_advance(struct bio *bio, unsigned int bytes);
  103. static inline void bio_crypt_advance(struct bio *bio, unsigned int bytes)
  104. {
  105. if (bio_has_crypt_ctx(bio))
  106. __bio_crypt_advance(bio, bytes);
  107. }
  108. void __bio_crypt_free_ctx(struct bio *bio);
  109. static inline void bio_crypt_free_ctx(struct bio *bio)
  110. {
  111. if (bio_has_crypt_ctx(bio))
  112. __bio_crypt_free_ctx(bio);
  113. }
  114. static inline void bio_crypt_do_front_merge(struct request *rq,
  115. struct bio *bio)
  116. {
  117. #ifdef CONFIG_BLK_INLINE_ENCRYPTION
  118. if (bio_has_crypt_ctx(bio))
  119. memcpy(rq->crypt_ctx->bc_dun, bio->bi_crypt_context->bc_dun,
  120. sizeof(rq->crypt_ctx->bc_dun));
  121. #endif
  122. }
  123. bool __blk_crypto_bio_prep(struct bio **bio_ptr);
  124. static inline bool blk_crypto_bio_prep(struct bio **bio_ptr)
  125. {
  126. if (bio_has_crypt_ctx(*bio_ptr))
  127. return __blk_crypto_bio_prep(bio_ptr);
  128. return true;
  129. }
  130. blk_status_t __blk_crypto_rq_get_keyslot(struct request *rq);
  131. static inline blk_status_t blk_crypto_rq_get_keyslot(struct request *rq)
  132. {
  133. if (blk_crypto_rq_is_encrypted(rq))
  134. return __blk_crypto_rq_get_keyslot(rq);
  135. return BLK_STS_OK;
  136. }
  137. void __blk_crypto_rq_put_keyslot(struct request *rq);
  138. static inline void blk_crypto_rq_put_keyslot(struct request *rq)
  139. {
  140. if (blk_crypto_rq_has_keyslot(rq))
  141. __blk_crypto_rq_put_keyslot(rq);
  142. }
  143. void __blk_crypto_free_request(struct request *rq);
  144. static inline void blk_crypto_free_request(struct request *rq)
  145. {
  146. if (blk_crypto_rq_is_encrypted(rq))
  147. __blk_crypto_free_request(rq);
  148. }
  149. int __blk_crypto_rq_bio_prep(struct request *rq, struct bio *bio,
  150. gfp_t gfp_mask);
  151. /**
  152. * blk_crypto_rq_bio_prep - Prepare a request's crypt_ctx when its first bio
  153. * is inserted
  154. * @rq: The request to prepare
  155. * @bio: The first bio being inserted into the request
  156. * @gfp_mask: Memory allocation flags
  157. *
  158. * Return: 0 on success, -ENOMEM if out of memory. -ENOMEM is only possible if
  159. * @gfp_mask doesn't include %__GFP_DIRECT_RECLAIM.
  160. */
  161. static inline int blk_crypto_rq_bio_prep(struct request *rq, struct bio *bio,
  162. gfp_t gfp_mask)
  163. {
  164. if (bio_has_crypt_ctx(bio))
  165. return __blk_crypto_rq_bio_prep(rq, bio, gfp_mask);
  166. return 0;
  167. }
  168. #ifdef CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK
  169. int blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num);
  170. bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr);
  171. int blk_crypto_fallback_evict_key(const struct blk_crypto_key *key);
  172. #else /* CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK */
  173. static inline int
  174. blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num)
  175. {
  176. pr_warn_once("crypto API fallback is disabled\n");
  177. return -ENOPKG;
  178. }
  179. static inline bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr)
  180. {
  181. pr_warn_once("crypto API fallback disabled; failing request.\n");
  182. (*bio_ptr)->bi_status = BLK_STS_NOTSUPP;
  183. return false;
  184. }
  185. static inline int
  186. blk_crypto_fallback_evict_key(const struct blk_crypto_key *key)
  187. {
  188. return 0;
  189. }
  190. #endif /* CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK */
  191. #endif /* __LINUX_BLK_CRYPTO_INTERNAL_H */