blk-crypto-internal.h 5.4 KB

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