1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright 2019 Google LLC
4 */
5
6 #ifndef __LINUX_BLK_CRYPTO_H
7 #define __LINUX_BLK_CRYPTO_H
8
9 #include <linux/types.h>
10
11 enum blk_crypto_mode_num {
12 BLK_ENCRYPTION_MODE_INVALID,
13 BLK_ENCRYPTION_MODE_AES_256_XTS,
14 BLK_ENCRYPTION_MODE_AES_128_CBC_ESSIV,
15 BLK_ENCRYPTION_MODE_ADIANTUM,
16 BLK_ENCRYPTION_MODE_SM4_XTS,
17 BLK_ENCRYPTION_MODE_MAX,
18 };
19
20 /*
21 * Supported types of keys. Must be bitflags due to their use in
22 * blk_crypto_profile::key_types_supported.
23 */
24 enum blk_crypto_key_type {
25 /*
26 * Standard keys (i.e. "software keys"). These keys are simply kept in
27 * raw, plaintext form in kernel memory.
28 */
29 BLK_CRYPTO_KEY_TYPE_STANDARD = 1 << 0,
30
31 /*
32 * Hardware-wrapped keys. These keys are only present in kernel memory
33 * in ephemerally-wrapped form, and they can only be unwrapped by
34 * dedicated hardware. For details, see the "Hardware-wrapped keys"
35 * section of Documentation/block/inline-encryption.rst.
36 */
37 BLK_CRYPTO_KEY_TYPE_HW_WRAPPED = 1 << 1,
38 };
39
40 /*
41 * Currently the maximum standard key size is 64 bytes, as that is the key size
42 * of BLK_ENCRYPTION_MODE_AES_256_XTS which takes the longest key.
43 *
44 * The maximum hardware-wrapped key size depends on the hardware's key wrapping
45 * algorithm, which is a hardware implementation detail, so it isn't precisely
46 * specified. But currently 128 bytes is plenty in practice. Implementations
47 * are recommended to wrap a 32-byte key for the hardware KDF with AES-256-GCM,
48 * which should result in a size closer to 64 bytes than 128.
49 *
50 * Both of these values can trivially be increased if ever needed.
51 */
52 #define BLK_CRYPTO_MAX_STANDARD_KEY_SIZE 64
53 #define BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE 128
54
55 /* This should use max(), but max() doesn't work in a struct definition. */
56 #define BLK_CRYPTO_MAX_ANY_KEY_SIZE \
57 (BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE > \
58 BLK_CRYPTO_MAX_STANDARD_KEY_SIZE ? \
59 BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE : BLK_CRYPTO_MAX_STANDARD_KEY_SIZE)
60
61 /*
62 * Size of the "software secret" which can be derived from a hardware-wrapped
63 * key. This is currently always 32 bytes. Note, the choice of 32 bytes
64 * assumes that the software secret is only used directly for algorithms that
65 * don't require more than a 256-bit key to get the desired security strength.
66 * If it were to be used e.g. directly as an AES-256-XTS key, then this would
67 * need to be increased (which is possible if hardware supports it, but care
68 * would need to be taken to avoid breaking users who need exactly 32 bytes).
69 */
70 #define BLK_CRYPTO_SW_SECRET_SIZE 32
71
72 /**
73 * struct blk_crypto_config - an inline encryption key's crypto configuration
74 * @crypto_mode: encryption algorithm this key is for
75 * @data_unit_size: the data unit size for all encryption/decryptions with this
76 * key. This is the size in bytes of each individual plaintext and
77 * ciphertext. This is always a power of 2. It might be e.g. the
78 * filesystem block size or the disk sector size.
79 * @dun_bytes: the maximum number of bytes of DUN used when using this key
80 * @key_type: the type of this key -- either standard or hardware-wrapped
81 */
82 struct blk_crypto_config {
83 enum blk_crypto_mode_num crypto_mode;
84 unsigned int data_unit_size;
85 unsigned int dun_bytes;
86 enum blk_crypto_key_type key_type;
87 };
88
89 /**
90 * struct blk_crypto_key - an inline encryption key
91 * @crypto_cfg: the crypto mode, data unit size, key type, and other
92 * characteristics of this key and how it will be used
93 * @data_unit_size_bits: log2 of data_unit_size
94 * @size: size of this key in bytes. The size of a standard key is fixed for a
95 * given crypto mode, but the size of a hardware-wrapped key can vary.
96 * @raw: the bytes of this key. Only the first @size bytes are significant.
97 *
98 * A blk_crypto_key is immutable once created, and many bios can reference it at
99 * the same time. It must not be freed until all bios using it have completed
100 * and it has been evicted from all devices on which it may have been used.
101 */
102 struct blk_crypto_key {
103 struct blk_crypto_config crypto_cfg;
104 unsigned int data_unit_size_bits;
105 unsigned int size;
106 u8 raw[BLK_CRYPTO_MAX_ANY_KEY_SIZE];
107 };
108
109 #define BLK_CRYPTO_MAX_IV_SIZE 32
110 #define BLK_CRYPTO_DUN_ARRAY_SIZE (BLK_CRYPTO_MAX_IV_SIZE / sizeof(u64))
111
112 /**
113 * struct bio_crypt_ctx - an inline encryption context
114 * @bc_key: the key, algorithm, and data unit size to use
115 * @bc_dun: the data unit number (starting IV) to use
116 *
117 * A bio_crypt_ctx specifies that the contents of the bio will be encrypted (for
118 * write requests) or decrypted (for read requests) inline by the storage device
119 * or controller, or by the crypto API fallback.
120 */
121 struct bio_crypt_ctx {
122 const struct blk_crypto_key *bc_key;
123 u64 bc_dun[BLK_CRYPTO_DUN_ARRAY_SIZE];
124 };
125
126 #include <linux/blk_types.h>
127 #include <linux/blkdev.h>
128
129 #ifdef CONFIG_BLK_INLINE_ENCRYPTION
130
bio_has_crypt_ctx(struct bio * bio)131 static inline bool bio_has_crypt_ctx(struct bio *bio)
132 {
133 return bio->bi_crypt_context;
134 }
135
136 void bio_crypt_set_ctx(struct bio *bio, const struct blk_crypto_key *key,
137 const u64 dun[BLK_CRYPTO_DUN_ARRAY_SIZE],
138 gfp_t gfp_mask);
139
140 bool bio_crypt_dun_is_contiguous(const struct bio_crypt_ctx *bc,
141 unsigned int bytes,
142 const u64 next_dun[BLK_CRYPTO_DUN_ARRAY_SIZE]);
143
144 int blk_crypto_init_key(struct blk_crypto_key *blk_key,
145 const u8 *raw_key, size_t raw_key_size,
146 enum blk_crypto_key_type key_type,
147 enum blk_crypto_mode_num crypto_mode,
148 unsigned int dun_bytes,
149 unsigned int data_unit_size);
150
151 int blk_crypto_start_using_key(struct block_device *bdev,
152 const struct blk_crypto_key *key);
153
154 void blk_crypto_evict_key(struct block_device *bdev,
155 const struct blk_crypto_key *key);
156
157 bool blk_crypto_config_supported_natively(struct block_device *bdev,
158 const struct blk_crypto_config *cfg);
159 bool blk_crypto_config_supported(struct block_device *bdev,
160 const struct blk_crypto_config *cfg);
161
162 int blk_crypto_derive_sw_secret(struct block_device *bdev,
163 const u8 *eph_key, size_t eph_key_size,
164 u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE]);
165
166 #else /* CONFIG_BLK_INLINE_ENCRYPTION */
167
bio_has_crypt_ctx(struct bio * bio)168 static inline bool bio_has_crypt_ctx(struct bio *bio)
169 {
170 return false;
171 }
172
173 #endif /* CONFIG_BLK_INLINE_ENCRYPTION */
174
175 static inline void bio_clone_skip_dm_default_key(struct bio *dst,
176 const struct bio *src);
177
178 int __bio_crypt_clone(struct bio *dst, struct bio *src, gfp_t gfp_mask);
179 /**
180 * bio_crypt_clone - clone bio encryption context
181 * @dst: destination bio
182 * @src: source bio
183 * @gfp_mask: memory allocation flags
184 *
185 * If @src has an encryption context, clone it to @dst.
186 *
187 * Return: 0 on success, -ENOMEM if out of memory. -ENOMEM is only possible if
188 * @gfp_mask doesn't include %__GFP_DIRECT_RECLAIM.
189 */
bio_crypt_clone(struct bio * dst,struct bio * src,gfp_t gfp_mask)190 static inline int bio_crypt_clone(struct bio *dst, struct bio *src,
191 gfp_t gfp_mask)
192 {
193 bio_clone_skip_dm_default_key(dst, src);
194 if (bio_has_crypt_ctx(src))
195 return __bio_crypt_clone(dst, src, gfp_mask);
196 return 0;
197 }
198
199 #if IS_ENABLED(CONFIG_DM_DEFAULT_KEY)
bio_set_skip_dm_default_key(struct bio * bio)200 static inline void bio_set_skip_dm_default_key(struct bio *bio)
201 {
202 bio->bi_skip_dm_default_key = true;
203 }
204
bio_should_skip_dm_default_key(const struct bio * bio)205 static inline bool bio_should_skip_dm_default_key(const struct bio *bio)
206 {
207 return bio->bi_skip_dm_default_key;
208 }
209
bio_clone_skip_dm_default_key(struct bio * dst,const struct bio * src)210 static inline void bio_clone_skip_dm_default_key(struct bio *dst,
211 const struct bio *src)
212 {
213 dst->bi_skip_dm_default_key = src->bi_skip_dm_default_key;
214 }
215 #else /* CONFIG_DM_DEFAULT_KEY */
bio_set_skip_dm_default_key(struct bio * bio)216 static inline void bio_set_skip_dm_default_key(struct bio *bio)
217 {
218 }
219
bio_should_skip_dm_default_key(const struct bio * bio)220 static inline bool bio_should_skip_dm_default_key(const struct bio *bio)
221 {
222 return false;
223 }
224
bio_clone_skip_dm_default_key(struct bio * dst,const struct bio * src)225 static inline void bio_clone_skip_dm_default_key(struct bio *dst,
226 const struct bio *src)
227 {
228 }
229 #endif /* !CONFIG_DM_DEFAULT_KEY */
230
231 #endif /* __LINUX_BLK_CRYPTO_H */
232