1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright 2019 Google LLC
6 #ifndef __LINUX_BLK_CRYPTO_INTERNAL_H
7 #define __LINUX_BLK_CRYPTO_INTERNAL_H
10 #include <linux/blk-mq.h>
12 /* Represents a crypto mode supported by blk-crypto */
13 struct blk_crypto_mode {
14 const char *name; /* name of this mode, shown in sysfs */
15 const char *cipher_str; /* crypto API name (for fallback case) */
16 unsigned int keysize; /* key size in bytes */
17 unsigned int security_strength; /* security strength in bytes */
18 unsigned int ivsize; /* iv size in bytes */
21 extern const struct blk_crypto_mode blk_crypto_modes[];
23 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
25 int blk_crypto_sysfs_register(struct gendisk *disk);
27 void blk_crypto_sysfs_unregister(struct gendisk *disk);
29 void bio_crypt_dun_increment(u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
32 bool bio_crypt_rq_ctx_compatible(struct request *rq, struct bio *bio);
34 bool bio_crypt_ctx_mergeable(struct bio_crypt_ctx *bc1, unsigned int bc1_bytes,
35 struct bio_crypt_ctx *bc2);
37 static inline bool bio_crypt_ctx_back_mergeable(struct request *req,
40 return bio_crypt_ctx_mergeable(req->crypt_ctx, blk_rq_bytes(req),
41 bio->bi_crypt_context);
44 static inline bool bio_crypt_ctx_front_mergeable(struct request *req,
47 return bio_crypt_ctx_mergeable(bio->bi_crypt_context,
48 bio->bi_iter.bi_size, req->crypt_ctx);
51 static inline bool bio_crypt_ctx_merge_rq(struct request *req,
54 return bio_crypt_ctx_mergeable(req->crypt_ctx, blk_rq_bytes(req),
58 static inline void blk_crypto_rq_set_defaults(struct request *rq)
61 rq->crypt_keyslot = NULL;
64 static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
69 static inline bool blk_crypto_rq_has_keyslot(struct request *rq)
71 return rq->crypt_keyslot;
74 blk_status_t blk_crypto_get_keyslot(struct blk_crypto_profile *profile,
75 const struct blk_crypto_key *key,
76 struct blk_crypto_keyslot **slot_ptr);
78 void blk_crypto_put_keyslot(struct blk_crypto_keyslot *slot);
80 int __blk_crypto_evict_key(struct blk_crypto_profile *profile,
81 const struct blk_crypto_key *key);
83 bool __blk_crypto_cfg_supported(struct blk_crypto_profile *profile,
84 const struct blk_crypto_config *cfg);
86 int blk_crypto_ioctl(struct block_device *bdev, unsigned int cmd,
89 #else /* CONFIG_BLK_INLINE_ENCRYPTION */
91 static inline int blk_crypto_sysfs_register(struct gendisk *disk)
96 static inline void blk_crypto_sysfs_unregister(struct gendisk *disk)
100 static inline bool bio_crypt_rq_ctx_compatible(struct request *rq,
106 static inline bool bio_crypt_ctx_front_mergeable(struct request *req,
112 static inline bool bio_crypt_ctx_back_mergeable(struct request *req,
118 static inline bool bio_crypt_ctx_merge_rq(struct request *req,
119 struct request *next)
124 static inline void blk_crypto_rq_set_defaults(struct request *rq) { }
126 static inline bool blk_crypto_rq_is_encrypted(struct request *rq)
131 static inline bool blk_crypto_rq_has_keyslot(struct request *rq)
136 static inline int blk_crypto_ioctl(struct block_device *bdev, unsigned int cmd,
142 #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
144 void __bio_crypt_advance(struct bio *bio, unsigned int bytes);
145 static inline void bio_crypt_advance(struct bio *bio, unsigned int bytes)
147 if (bio_has_crypt_ctx(bio))
148 __bio_crypt_advance(bio, bytes);
151 void __bio_crypt_free_ctx(struct bio *bio);
152 static inline void bio_crypt_free_ctx(struct bio *bio)
154 if (bio_has_crypt_ctx(bio))
155 __bio_crypt_free_ctx(bio);
158 static inline void bio_crypt_do_front_merge(struct request *rq,
161 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
162 if (bio_has_crypt_ctx(bio))
163 memcpy(rq->crypt_ctx->bc_dun, bio->bi_crypt_context->bc_dun,
164 sizeof(rq->crypt_ctx->bc_dun));
168 bool __blk_crypto_bio_prep(struct bio **bio_ptr);
169 static inline bool blk_crypto_bio_prep(struct bio **bio_ptr)
171 if (bio_has_crypt_ctx(*bio_ptr))
172 return __blk_crypto_bio_prep(bio_ptr);
176 blk_status_t __blk_crypto_rq_get_keyslot(struct request *rq);
177 static inline blk_status_t blk_crypto_rq_get_keyslot(struct request *rq)
179 if (blk_crypto_rq_is_encrypted(rq))
180 return __blk_crypto_rq_get_keyslot(rq);
184 void __blk_crypto_rq_put_keyslot(struct request *rq);
185 static inline void blk_crypto_rq_put_keyslot(struct request *rq)
187 if (blk_crypto_rq_has_keyslot(rq))
188 __blk_crypto_rq_put_keyslot(rq);
191 void __blk_crypto_free_request(struct request *rq);
192 static inline void blk_crypto_free_request(struct request *rq)
194 if (blk_crypto_rq_is_encrypted(rq))
195 __blk_crypto_free_request(rq);
198 int __blk_crypto_rq_bio_prep(struct request *rq, struct bio *bio,
201 * blk_crypto_rq_bio_prep - Prepare a request's crypt_ctx when its first bio
203 * @rq: The request to prepare
204 * @bio: The first bio being inserted into the request
205 * @gfp_mask: Memory allocation flags
207 * Return: 0 on success, -ENOMEM if out of memory. -ENOMEM is only possible if
208 * @gfp_mask doesn't include %__GFP_DIRECT_RECLAIM.
210 static inline int blk_crypto_rq_bio_prep(struct request *rq, struct bio *bio,
213 if (bio_has_crypt_ctx(bio))
214 return __blk_crypto_rq_bio_prep(rq, bio, gfp_mask);
218 #ifdef CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK
220 int blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num);
222 bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr);
224 int blk_crypto_fallback_evict_key(const struct blk_crypto_key *key);
226 #else /* CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK */
229 blk_crypto_fallback_start_using_mode(enum blk_crypto_mode_num mode_num)
231 pr_warn_once("crypto API fallback is disabled\n");
235 static inline bool blk_crypto_fallback_bio_prep(struct bio **bio_ptr)
237 pr_warn_once("crypto API fallback disabled; failing request.\n");
238 (*bio_ptr)->bi_status = BLK_STS_NOTSUPP;
243 blk_crypto_fallback_evict_key(const struct blk_crypto_key *key)
248 #endif /* CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK */
250 #endif /* __LINUX_BLK_CRYPTO_INTERNAL_H */