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