@@ -132,47 +132,28 @@ designed for this purpose be used, such as scrypt, PBKDF2, or Argon2.
132
132
Per-file keys
133
133
-------------
134
134
135
- Master keys are not used to encrypt file contents or names directly.
136
- Instead, a unique key is derived for each encrypted file, including
137
- each regular file, directory, and symbolic link. This has several
138
- advantages:
139
-
140
- - In cryptosystems, the same key material should never be used for
141
- different purposes. Using the master key as both an XTS key for
142
- contents encryption and as a CTS-CBC key for filenames encryption
143
- would violate this rule.
144
- - Per-file keys simplify the choice of IVs (Initialization Vectors)
145
- for contents encryption. Without per-file keys, to ensure IV
146
- uniqueness both the inode and logical block number would need to be
147
- encoded in the IVs. This would make it impossible to renumber
148
- inodes, which e.g. ``resize2fs `` can do when resizing an ext4
149
- filesystem. With per-file keys, it is sufficient to encode just the
150
- logical block number in the IVs.
151
- - Per-file keys strengthen the encryption of filenames, where IVs are
152
- reused out of necessity. With a unique key per directory, IV reuse
153
- is limited to within a single directory.
154
- - Per-file keys allow individual files to be securely erased simply by
155
- securely erasing their keys. (Not yet implemented.)
156
-
157
- A KDF (Key Derivation Function) is used to derive per-file keys from
158
- the master key. This is done instead of wrapping a randomly-generated
159
- key for each file because it reduces the size of the encryption xattr,
160
- which for some filesystems makes the xattr more likely to fit in-line
161
- in the filesystem's inode table. With a KDF, only a 16-byte nonce is
162
- required --- long enough to make key reuse extremely unlikely. A
163
- wrapped key, on the other hand, would need to be up to 64 bytes ---
164
- the length of an AES-256-XTS key. Furthermore, currently there is no
165
- requirement to support unlocking a file with multiple alternative
166
- master keys or to support rotating master keys. Instead, the master
167
- keys may be wrapped in userspace, e.g. as done by the `fscrypt
168
- <https://github.com/google/fscrypt> `_ tool.
169
-
170
- The current KDF encrypts the master key using the 16-byte nonce as an
171
- AES-128-ECB key. The output is used as the derived key. If the
172
- output is longer than needed, then it is truncated to the needed
173
- length. Truncation is the norm for directories and symlinks, since
174
- those use the CTS-CBC encryption mode which requires a key half as
175
- long as that required by the XTS encryption mode.
135
+ Since each master key can protect many files, it is necessary to
136
+ "tweak" the encryption of each file so that the same plaintext in two
137
+ files doesn't map to the same ciphertext, or vice versa. In most
138
+ cases, fscrypt does this by deriving per-file keys. When a new
139
+ encrypted inode (regular file, directory, or symlink) is created,
140
+ fscrypt randomly generates a 16-byte nonce and stores it in the
141
+ inode's encryption xattr. Then, it uses a KDF (Key Derivation
142
+ Function) to derive the file's key from the master key and nonce.
143
+
144
+ The Adiantum encryption mode (see `Encryption modes and usage `_) is
145
+ special, since it accepts longer IVs and is suitable for both contents
146
+ and filenames encryption. For it, a "direct key" option is offered
147
+ where the file's nonce is included in the IVs and the master key is
148
+ used for encryption directly. This improves performance; however,
149
+ users must not use the same master key for any other encryption mode.
150
+
151
+ Below, the KDF and design considerations are described in more detail.
152
+
153
+ The current KDF works by encrypting the master key with AES-128-ECB,
154
+ using the file's nonce as the AES key. The output is used as the
155
+ derived key. If the output is longer than needed, then it is
156
+ truncated to the needed length.
176
157
177
158
Note: this KDF meets the primary security requirement, which is to
178
159
produce unique derived keys that preserve the entropy of the master
@@ -181,6 +162,20 @@ However, it is nonstandard and has some problems such as being
181
162
reversible, so it is generally considered to be a mistake! It may be
182
163
replaced with HKDF or another more standard KDF in the future.
183
164
165
+ Key derivation was chosen over key wrapping because wrapped keys would
166
+ require larger xattrs which would be less likely to fit in-line in the
167
+ filesystem's inode table, and there didn't appear to be any
168
+ significant advantages to key wrapping. In particular, currently
169
+ there is no requirement to support unlocking a file with multiple
170
+ alternative master keys or to support rotating master keys. Instead,
171
+ the master keys may be wrapped in userspace, e.g. as is done by the
172
+ `fscrypt <https://github.com/google/fscrypt >`_ tool.
173
+
174
+ Including the inode number in the IVs was considered. However, it was
175
+ rejected as it would have prevented ext4 filesystems from being
176
+ resized, and by itself still wouldn't have been sufficient to prevent
177
+ the same key from being directly reused for both XTS and CTS-CBC.
178
+
184
179
Encryption modes and usage
185
180
==========================
186
181
@@ -191,54 +186,80 @@ Currently, the following pairs of encryption modes are supported:
191
186
192
187
- AES-256-XTS for contents and AES-256-CTS-CBC for filenames
193
188
- AES-128-CBC for contents and AES-128-CTS-CBC for filenames
189
+ - Adiantum for both contents and filenames
190
+
191
+ If unsure, you should use the (AES-256-XTS, AES-256-CTS-CBC) pair.
194
192
195
- It is strongly recommended to use AES-256-XTS for contents encryption.
196
193
AES-128-CBC was added only for low-powered embedded devices with
197
194
crypto accelerators such as CAAM or CESA that do not support XTS.
198
195
196
+ Adiantum is a (primarily) stream cipher-based mode that is fast even
197
+ on CPUs without dedicated crypto instructions. It's also a true
198
+ wide-block mode, unlike XTS. It can also eliminate the need to derive
199
+ per-file keys. However, it depends on the security of two primitives,
200
+ XChaCha12 and AES-256, rather than just one. See the paper
201
+ "Adiantum: length-preserving encryption for entry-level processors"
202
+ (https://eprint.iacr.org/2018/720.pdf) for more details. To use
203
+ Adiantum, CONFIG_CRYPTO_ADIANTUM must be enabled. Also, fast
204
+ implementations of ChaCha and NHPoly1305 should be enabled, e.g.
205
+ CONFIG_CRYPTO_CHACHA20_NEON and CONFIG_CRYPTO_NHPOLY1305_NEON for ARM.
206
+
199
207
New encryption modes can be added relatively easily, without changes
200
208
to individual filesystems. However, authenticated encryption (AE)
201
209
modes are not currently supported because of the difficulty of dealing
202
210
with ciphertext expansion.
203
211
212
+ Contents encryption
213
+ -------------------
214
+
204
215
For file contents, each filesystem block is encrypted independently.
205
216
Currently, only the case where the filesystem block size is equal to
206
- the system's page size (usually 4096 bytes) is supported. With the
207
- XTS mode of operation (recommended), the logical block number within
208
- the file is used as the IV. With the CBC mode of operation (not
209
- recommended), ESSIV is used; specifically, the IV for CBC is the
210
- logical block number encrypted with AES-256, where the AES-256 key is
211
- the SHA-256 hash of the inode's data encryption key.
212
-
213
- For filenames, the full filename is encrypted at once. Because of the
214
- requirements to retain support for efficient directory lookups and
215
- filenames of up to 255 bytes, a constant initialization vector (IV) is
216
- used. However, each encrypted directory uses a unique key, which
217
- limits IV reuse to within a single directory. Note that IV reuse in
218
- the context of CTS-CBC encryption means that when the original
219
- filenames share a common prefix at least as long as the cipher block
220
- size (16 bytes for AES), the corresponding encrypted filenames will
221
- also share a common prefix. This is undesirable; it may be fixed in
222
- the future by switching to an encryption mode that is a strong
223
- pseudorandom permutation on arbitrary-length messages, e.g. the HEH
224
- (Hash-Encrypt-Hash) mode.
225
-
226
- Since filenames are encrypted with the CTS-CBC mode of operation, the
227
- plaintext and ciphertext filenames need not be multiples of the AES
228
- block size, i.e. 16 bytes. However, the minimum size that can be
229
- encrypted is 16 bytes, so shorter filenames are NUL-padded to 16 bytes
230
- before being encrypted. In addition, to reduce leakage of filename
231
- lengths via their ciphertexts, all filenames are NUL-padded to the
232
- next 4, 8, 16, or 32-byte boundary (configurable). 32 is recommended
233
- since this provides the best confidentiality, at the cost of making
234
- directory entries consume slightly more space. Note that since NUL
235
- (``\0 ``) is not otherwise a valid character in filenames, the padding
236
- will never produce duplicate plaintexts.
217
+ the system's page size (usually 4096 bytes) is supported.
218
+
219
+ Each block's IV is set to the logical block number within the file as
220
+ a little endian number, except that:
221
+
222
+ - With CBC mode encryption, ESSIV is also used. Specifically, each IV
223
+ is encrypted with AES-256 where the AES-256 key is the SHA-256 hash
224
+ of the file's data encryption key.
225
+
226
+ - In the "direct key" configuration (FS_POLICY_FLAG_DIRECT_KEY set in
227
+ the fscrypt_policy), the file's nonce is also appended to the IV.
228
+ Currently this is only allowed with the Adiantum encryption mode.
229
+
230
+ Filenames encryption
231
+ --------------------
232
+
233
+ For filenames, each full filename is encrypted at once. Because of
234
+ the requirements to retain support for efficient directory lookups and
235
+ filenames of up to 255 bytes, the same IV is used for every filename
236
+ in a directory.
237
+
238
+ However, each encrypted directory still uses a unique key; or
239
+ alternatively (for the "direct key" configuration) has the file's
240
+ nonce included in the IVs. Thus, IV reuse is limited to within a
241
+ single directory.
242
+
243
+ With CTS-CBC, the IV reuse means that when the plaintext filenames
244
+ share a common prefix at least as long as the cipher block size (16
245
+ bytes for AES), the corresponding encrypted filenames will also share
246
+ a common prefix. This is undesirable. Adiantum does not have this
247
+ weakness, as it is a wide-block encryption mode.
248
+
249
+ All supported filenames encryption modes accept any plaintext length
250
+ >= 16 bytes; cipher block alignment is not required. However,
251
+ filenames shorter than 16 bytes are NUL-padded to 16 bytes before
252
+ being encrypted. In addition, to reduce leakage of filename lengths
253
+ via their ciphertexts, all filenames are NUL-padded to the next 4, 8,
254
+ 16, or 32-byte boundary (configurable). 32 is recommended since this
255
+ provides the best confidentiality, at the cost of making directory
256
+ entries consume slightly more space. Note that since NUL (``\0 ``) is
257
+ not otherwise a valid character in filenames, the padding will never
258
+ produce duplicate plaintexts.
237
259
238
260
Symbolic link targets are considered a type of filename and are
239
- encrypted in the same way as filenames in directory entries. Each
240
- symlink also uses a unique key; hence, the hardcoded IV is not a
241
- problem for symlinks.
261
+ encrypted in the same way as filenames in directory entries, except
262
+ that IV reuse is not a problem as each symlink has its own inode.
242
263
243
264
User API
244
265
========
@@ -272,9 +293,13 @@ This structure must be initialized as follows:
272
293
and FS_ENCRYPTION_MODE_AES_256_CTS (4) for
273
294
``filenames_encryption_mode ``.
274
295
275
- - ``flags `` must be set to a value from ``<linux/fs.h> `` which
296
+ - ``flags `` must contain a value from ``<linux/fs.h> `` which
276
297
identifies the amount of NUL-padding to use when encrypting
277
298
filenames. If unsure, use FS_POLICY_FLAGS_PAD_32 (0x3).
299
+ In addition, if the chosen encryption modes are both
300
+ FS_ENCRYPTION_MODE_ADIANTUM, this can contain
301
+ FS_POLICY_FLAG_DIRECT_KEY to specify that the master key should be
302
+ used directly, without key derivation.
278
303
279
304
- ``master_key_descriptor `` specifies how to find the master key in
280
305
the keyring; see `Adding keys `_. It is up to userspace to choose a
0 commit comments