Skip to content

Commit f592efe

Browse files
committed
fscrypt: clarify what is meant by a per-file key
Now that there's sometimes a second type of per-file key (the dirhash key), clarify some function names, macros, and documentation that specifically deal with per-file *encryption* keys. Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Daniel Rosenberg <[email protected]> Signed-off-by: Eric Biggers <[email protected]>
1 parent aa408f8 commit f592efe

File tree

4 files changed

+37
-36
lines changed

4 files changed

+37
-36
lines changed

Documentation/filesystems/fscrypt.rst

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ HKDF is more flexible, is nonreversible, and evenly distributes
234234
entropy from the master key. HKDF is also standardized and widely
235235
used by other software, whereas the AES-128-ECB based KDF is ad-hoc.
236236

237-
Per-file keys
238-
-------------
237+
Per-file encryption keys
238+
------------------------
239239

240240
Since each master key can protect many files, it is necessary to
241241
"tweak" the encryption of each file so that the same plaintext in two
@@ -268,9 +268,9 @@ is greater than that of an AES-256-XTS key.
268268
Therefore, to improve performance and save memory, for Adiantum a
269269
"direct key" configuration is supported. When the user has enabled
270270
this by setting FSCRYPT_POLICY_FLAG_DIRECT_KEY in the fscrypt policy,
271-
per-file keys are not used. Instead, whenever any data (contents or
272-
filenames) is encrypted, the file's 16-byte nonce is included in the
273-
IV. Moreover:
271+
per-file encryption keys are not used. Instead, whenever any data
272+
(contents or filenames) is encrypted, the file's 16-byte nonce is
273+
included in the IV. Moreover:
274274

275275
- For v1 encryption policies, the encryption is done directly with the
276276
master key. Because of this, users **must not** use the same master
@@ -335,11 +335,11 @@ used.
335335
Adiantum is a (primarily) stream cipher-based mode that is fast even
336336
on CPUs without dedicated crypto instructions. It's also a true
337337
wide-block mode, unlike XTS. It can also eliminate the need to derive
338-
per-file keys. However, it depends on the security of two primitives,
339-
XChaCha12 and AES-256, rather than just one. See the paper
340-
"Adiantum: length-preserving encryption for entry-level processors"
341-
(https://eprint.iacr.org/2018/720.pdf) for more details. To use
342-
Adiantum, CONFIG_CRYPTO_ADIANTUM must be enabled. Also, fast
338+
per-file encryption keys. However, it depends on the security of two
339+
primitives, XChaCha12 and AES-256, rather than just one. See the
340+
paper "Adiantum: length-preserving encryption for entry-level
341+
processors" (https://eprint.iacr.org/2018/720.pdf) for more details.
342+
To use Adiantum, CONFIG_CRYPTO_ADIANTUM must be enabled. Also, fast
343343
implementations of ChaCha and NHPoly1305 should be enabled, e.g.
344344
CONFIG_CRYPTO_CHACHA20_NEON and CONFIG_CRYPTO_NHPOLY1305_NEON for ARM.
345345

@@ -1149,8 +1149,8 @@ The context structs contain the same information as the corresponding
11491149
policy structs (see `Setting an encryption policy`_), except that the
11501150
context structs also contain a nonce. The nonce is randomly generated
11511151
by the kernel and is used as KDF input or as a tweak to cause
1152-
different files to be encrypted differently; see `Per-file keys`_ and
1153-
`DIRECT_KEY policies`_.
1152+
different files to be encrypted differently; see `Per-file encryption
1153+
keys`_ and `DIRECT_KEY policies`_.
11541154

11551155
Data path changes
11561156
-----------------

fs/crypto/fscrypt_private.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ extern int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
269269
* output doesn't reveal another.
270270
*/
271271
#define HKDF_CONTEXT_KEY_IDENTIFIER 1
272-
#define HKDF_CONTEXT_PER_FILE_KEY 2
272+
#define HKDF_CONTEXT_PER_FILE_ENC_KEY 2
273273
#define HKDF_CONTEXT_DIRECT_KEY 3
274274
#define HKDF_CONTEXT_IV_INO_LBLK_64_KEY 4
275275
#define HKDF_CONTEXT_DIRHASH_KEY 5
@@ -441,8 +441,8 @@ extern struct crypto_skcipher *
441441
fscrypt_allocate_skcipher(struct fscrypt_mode *mode, const u8 *raw_key,
442442
const struct inode *inode);
443443

444-
extern int fscrypt_set_derived_key(struct fscrypt_info *ci,
445-
const u8 *derived_key);
444+
extern int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci,
445+
const u8 *raw_key);
446446

447447
extern int fscrypt_derive_dirhash_key(struct fscrypt_info *ci,
448448
const struct fscrypt_master_key *mk);

fs/crypto/keysetup.c

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ struct crypto_skcipher *fscrypt_allocate_skcipher(struct fscrypt_mode *mode,
107107
return ERR_PTR(err);
108108
}
109109

110-
/* Given the per-file key, set up the file's crypto transform object */
111-
int fscrypt_set_derived_key(struct fscrypt_info *ci, const u8 *derived_key)
110+
/* Given a per-file encryption key, set up the file's crypto transform object */
111+
int fscrypt_set_per_file_enc_key(struct fscrypt_info *ci, const u8 *raw_key)
112112
{
113113
struct crypto_skcipher *tfm;
114114

115-
tfm = fscrypt_allocate_skcipher(ci->ci_mode, derived_key, ci->ci_inode);
115+
tfm = fscrypt_allocate_skcipher(ci->ci_mode, raw_key, ci->ci_inode);
116116
if (IS_ERR(tfm))
117117
return PTR_ERR(tfm);
118118

@@ -121,10 +121,10 @@ int fscrypt_set_derived_key(struct fscrypt_info *ci, const u8 *derived_key)
121121
return 0;
122122
}
123123

124-
static int setup_per_mode_key(struct fscrypt_info *ci,
125-
struct fscrypt_master_key *mk,
126-
struct crypto_skcipher **tfms,
127-
u8 hkdf_context, bool include_fs_uuid)
124+
static int setup_per_mode_enc_key(struct fscrypt_info *ci,
125+
struct fscrypt_master_key *mk,
126+
struct crypto_skcipher **tfms,
127+
u8 hkdf_context, bool include_fs_uuid)
128128
{
129129
const struct inode *inode = ci->ci_inode;
130130
const struct super_block *sb = inode->i_sb;
@@ -196,15 +196,15 @@ static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci,
196196

197197
if (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) {
198198
/*
199-
* DIRECT_KEY: instead of deriving per-file keys, the per-file
200-
* nonce will be included in all the IVs. But unlike v1
201-
* policies, for v2 policies in this case we don't encrypt with
202-
* the master key directly but rather derive a per-mode key.
203-
* This ensures that the master key is consistently used only
204-
* for HKDF, avoiding key reuse issues.
199+
* DIRECT_KEY: instead of deriving per-file encryption keys, the
200+
* per-file nonce will be included in all the IVs. But unlike
201+
* v1 policies, for v2 policies in this case we don't encrypt
202+
* with the master key directly but rather derive a per-mode
203+
* encryption key. This ensures that the master key is
204+
* consistently used only for HKDF, avoiding key reuse issues.
205205
*/
206-
err = setup_per_mode_key(ci, mk, mk->mk_direct_tfms,
207-
HKDF_CONTEXT_DIRECT_KEY, false);
206+
err = setup_per_mode_enc_key(ci, mk, mk->mk_direct_tfms,
207+
HKDF_CONTEXT_DIRECT_KEY, false);
208208
} else if (ci->ci_policy.v2.flags &
209209
FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) {
210210
/*
@@ -213,20 +213,21 @@ static int fscrypt_setup_v2_file_key(struct fscrypt_info *ci,
213213
* the IVs. This format is optimized for use with inline
214214
* encryption hardware compliant with the UFS or eMMC standards.
215215
*/
216-
err = setup_per_mode_key(ci, mk, mk->mk_iv_ino_lblk_64_tfms,
217-
HKDF_CONTEXT_IV_INO_LBLK_64_KEY, true);
216+
err = setup_per_mode_enc_key(ci, mk, mk->mk_iv_ino_lblk_64_tfms,
217+
HKDF_CONTEXT_IV_INO_LBLK_64_KEY,
218+
true);
218219
} else {
219220
u8 derived_key[FSCRYPT_MAX_KEY_SIZE];
220221

221222
err = fscrypt_hkdf_expand(&mk->mk_secret.hkdf,
222-
HKDF_CONTEXT_PER_FILE_KEY,
223+
HKDF_CONTEXT_PER_FILE_ENC_KEY,
223224
ci->ci_nonce,
224225
FS_KEY_DERIVATION_NONCE_SIZE,
225226
derived_key, ci->ci_mode->keysize);
226227
if (err)
227228
return err;
228229

229-
err = fscrypt_set_derived_key(ci, derived_key);
230+
err = fscrypt_set_per_file_enc_key(ci, derived_key);
230231
memzero_explicit(derived_key, ci->ci_mode->keysize);
231232
}
232233
if (err)

fs/crypto/keysetup_v1.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* This file implements compatibility functions for the original encryption
1010
* policy version ("v1"), including:
1111
*
12-
* - Deriving per-file keys using the AES-128-ECB based KDF
12+
* - Deriving per-file encryption keys using the AES-128-ECB based KDF
1313
* (rather than the new method of using HKDF-SHA512)
1414
*
1515
* - Retrieving fscrypt master keys from process-subscribed keyrings
@@ -283,7 +283,7 @@ static int setup_v1_file_key_derived(struct fscrypt_info *ci,
283283
if (err)
284284
goto out;
285285

286-
err = fscrypt_set_derived_key(ci, derived_key);
286+
err = fscrypt_set_per_file_enc_key(ci, derived_key);
287287
out:
288288
kzfree(derived_key);
289289
return err;

0 commit comments

Comments
 (0)