blk-crypto-internal.h 6.6 KB

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