Skip to content

Commit d60ac92

Browse files
committed
Merge tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/linux
Pull fscrypt updates from Eric Biggers: "Make fs/crypto/ use the HMAC-SHA512 library functions instead of crypto_shash. This is simpler, faster, and more reliable" * tag 'fscrypt-for-linus' of git://git.kernel.org/pub/scm/fs/fscrypt/linux: fscrypt: use HMAC-SHA512 library for HKDF fscrypt: Remove redundant __GFP_NOWARN
2 parents a769648 + 19591f7 commit d60ac92

9 files changed

Lines changed: 83 additions & 161 deletions

File tree

fs/crypto/Kconfig

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
config FS_ENCRYPTION
33
bool "FS Encryption (Per-file encryption)"
44
select CRYPTO
5-
select CRYPTO_HASH
6-
select CRYPTO_HKDF
75
select CRYPTO_SKCIPHER
86
select CRYPTO_LIB_SHA256
7+
select CRYPTO_LIB_SHA512
98
select KEYS
109
help
1110
Enable encryption of files and directories. This
@@ -32,8 +31,6 @@ config FS_ENCRYPTION_ALGS
3231
select CRYPTO_CBC
3332
select CRYPTO_CTS
3433
select CRYPTO_ECB
35-
select CRYPTO_HMAC
36-
select CRYPTO_SHA512
3734
select CRYPTO_XTS
3835

3936
config FS_ENCRYPTION_INLINE_CRYPT

fs/crypto/bio.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
148148
*/
149149
for (i = 0; i < nr_pages; i++) {
150150
pages[i] = fscrypt_alloc_bounce_page(i == 0 ? GFP_NOFS :
151-
GFP_NOWAIT | __GFP_NOWARN);
151+
GFP_NOWAIT);
152152
if (!pages[i])
153153
break;
154154
}

fs/crypto/fname.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
* This has not yet undergone a rigorous security audit.
1212
*/
1313

14-
#include <crypto/hash.h>
1514
#include <crypto/sha2.h>
1615
#include <crypto/skcipher.h>
1716
#include <linux/export.h>

fs/crypto/fscrypt_private.h

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
#ifndef _FSCRYPT_PRIVATE_H
1212
#define _FSCRYPT_PRIVATE_H
1313

14+
#include <crypto/sha2.h>
1415
#include <linux/fscrypt.h>
1516
#include <linux/minmax.h>
1617
#include <linux/siphash.h>
17-
#include <crypto/hash.h>
1818
#include <linux/blk-crypto.h>
1919

2020
#define CONST_STRLEN(str) (sizeof(str) - 1)
@@ -381,12 +381,8 @@ bool __fscrypt_fname_encrypted_size(const union fscrypt_policy *policy,
381381
u32 *encrypted_len_ret);
382382

383383
/* hkdf.c */
384-
struct fscrypt_hkdf {
385-
struct crypto_shash *hmac_tfm;
386-
};
387-
388-
int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
389-
unsigned int master_key_size);
384+
void fscrypt_init_hkdf(struct hmac_sha512_key *hkdf, const u8 *master_key,
385+
unsigned int master_key_size);
390386

391387
/*
392388
* The list of contexts in which fscrypt uses HKDF. These values are used as
@@ -405,11 +401,9 @@ int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
405401
#define HKDF_CONTEXT_KEY_IDENTIFIER_FOR_HW_WRAPPED_KEY \
406402
8 /* info=<empty> */
407403

408-
int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,
409-
const u8 *info, unsigned int infolen,
410-
u8 *okm, unsigned int okmlen);
411-
412-
void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf);
404+
void fscrypt_hkdf_expand(const struct hmac_sha512_key *hkdf, u8 context,
405+
const u8 *info, unsigned int infolen,
406+
u8 *okm, unsigned int okmlen);
413407

414408
/* inline_crypt.c */
415409
#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
@@ -517,7 +511,7 @@ struct fscrypt_master_key_secret {
517511
* ->is_hw_wrapped=false, or by the "software secret" that hardware
518512
* derived from this master key if ->is_hw_wrapped=true.
519513
*/
520-
struct fscrypt_hkdf hkdf;
514+
struct hmac_sha512_key hkdf;
521515

522516
/*
523517
* True if this key is a hardware-wrapped key; false if this key is a
@@ -696,7 +690,7 @@ struct fscrypt_master_key *
696690
fscrypt_find_master_key(struct super_block *sb,
697691
const struct fscrypt_key_specifier *mk_spec);
698692

699-
int fscrypt_get_test_dummy_key_identifier(
693+
void fscrypt_get_test_dummy_key_identifier(
700694
u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE]);
701695

702696
int fscrypt_add_test_dummy_key(struct super_block *sb,
@@ -732,8 +726,8 @@ void fscrypt_destroy_prepared_key(struct super_block *sb,
732726
int fscrypt_set_per_file_enc_key(struct fscrypt_inode_info *ci,
733727
const u8 *raw_key);
734728

735-
int fscrypt_derive_dirhash_key(struct fscrypt_inode_info *ci,
736-
const struct fscrypt_master_key *mk);
729+
void fscrypt_derive_dirhash_key(struct fscrypt_inode_info *ci,
730+
const struct fscrypt_master_key *mk);
737731

738732
void fscrypt_hash_inode_number(struct fscrypt_inode_info *ci,
739733
const struct fscrypt_master_key *mk);

fs/crypto/hkdf.c

Lines changed: 40 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// SPDX-License-Identifier: GPL-2.0
22
/*
3+
* Implementation of HKDF ("HMAC-based Extract-and-Expand Key Derivation
4+
* Function"), aka RFC 5869. See also the original paper (Krawczyk 2010):
5+
* "Cryptographic Extraction and Key Derivation: The HKDF Scheme".
6+
*
37
* This is used to derive keys from the fscrypt master keys (or from the
48
* "software secrets" which hardware derives from the fscrypt master keys, in
59
* the case that the fscrypt master keys are hardware-wrapped keys).
610
*
711
* Copyright 2019 Google LLC
812
*/
913

10-
#include <crypto/hash.h>
11-
#include <crypto/hkdf.h>
12-
#include <crypto/sha2.h>
13-
1414
#include "fscrypt_private.h"
1515

1616
/*
@@ -24,7 +24,6 @@
2424
* HKDF-SHA512 being much faster than HKDF-SHA256, as the longer digest size of
2525
* SHA-512 causes HKDF-Expand to only need to do one iteration rather than two.
2626
*/
27-
#define HKDF_HMAC_ALG "hmac(sha512)"
2827
#define HKDF_HASHLEN SHA512_DIGEST_SIZE
2928

3029
/*
@@ -44,54 +43,24 @@
4443
*/
4544

4645
/*
47-
* Compute HKDF-Extract using the given master key as the input keying material,
48-
* and prepare an HMAC transform object keyed by the resulting pseudorandom key.
49-
*
50-
* Afterwards, the keyed HMAC transform object can be used for HKDF-Expand many
51-
* times without having to recompute HKDF-Extract each time.
46+
* Compute HKDF-Extract using 'master_key' as the input keying material, and
47+
* prepare the resulting HMAC key in 'hkdf'. Afterwards, 'hkdf' can be used for
48+
* HKDF-Expand many times without having to recompute HKDF-Extract each time.
5249
*/
53-
int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
54-
unsigned int master_key_size)
50+
void fscrypt_init_hkdf(struct hmac_sha512_key *hkdf, const u8 *master_key,
51+
unsigned int master_key_size)
5552
{
56-
struct crypto_shash *hmac_tfm;
5753
static const u8 default_salt[HKDF_HASHLEN];
5854
u8 prk[HKDF_HASHLEN];
59-
int err;
60-
61-
hmac_tfm = crypto_alloc_shash(HKDF_HMAC_ALG, 0, FSCRYPT_CRYPTOAPI_MASK);
62-
if (IS_ERR(hmac_tfm)) {
63-
fscrypt_err(NULL, "Error allocating " HKDF_HMAC_ALG ": %ld",
64-
PTR_ERR(hmac_tfm));
65-
return PTR_ERR(hmac_tfm);
66-
}
67-
68-
if (WARN_ON_ONCE(crypto_shash_digestsize(hmac_tfm) != sizeof(prk))) {
69-
err = -EINVAL;
70-
goto err_free_tfm;
71-
}
72-
73-
err = hkdf_extract(hmac_tfm, master_key, master_key_size,
74-
default_salt, HKDF_HASHLEN, prk);
75-
if (err)
76-
goto err_free_tfm;
77-
78-
err = crypto_shash_setkey(hmac_tfm, prk, sizeof(prk));
79-
if (err)
80-
goto err_free_tfm;
8155

82-
hkdf->hmac_tfm = hmac_tfm;
83-
goto out;
84-
85-
err_free_tfm:
86-
crypto_free_shash(hmac_tfm);
87-
out:
56+
hmac_sha512_usingrawkey(default_salt, sizeof(default_salt),
57+
master_key, master_key_size, prk);
58+
hmac_sha512_preparekey(hkdf, prk, sizeof(prk));
8859
memzero_explicit(prk, sizeof(prk));
89-
return err;
9060
}
9161

9262
/*
93-
* HKDF-Expand (RFC 5869 section 2.3). This expands the pseudorandom key, which
94-
* was already keyed into 'hkdf->hmac_tfm' by fscrypt_init_hkdf(), into 'okmlen'
63+
* HKDF-Expand (RFC 5869 section 2.3). Expand the HMAC key 'hkdf' into 'okmlen'
9564
* bytes of output keying material parameterized by the application-specific
9665
* 'info' of length 'infolen' bytes, prefixed by "fscrypt\0" and the 'context'
9766
* byte. This is thread-safe and may be called by multiple threads in parallel.
@@ -100,30 +69,32 @@ int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
10069
* adds to its application-specific info strings to guarantee that it doesn't
10170
* accidentally repeat an info string when using HKDF for different purposes.)
10271
*/
103-
int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,
104-
const u8 *info, unsigned int infolen,
105-
u8 *okm, unsigned int okmlen)
106-
{
107-
SHASH_DESC_ON_STACK(desc, hkdf->hmac_tfm);
108-
u8 *full_info;
109-
int err;
110-
111-
full_info = kzalloc(infolen + 9, GFP_KERNEL);
112-
if (!full_info)
113-
return -ENOMEM;
114-
desc->tfm = hkdf->hmac_tfm;
115-
116-
memcpy(full_info, "fscrypt\0", 8);
117-
full_info[8] = context;
118-
memcpy(full_info + 9, info, infolen);
119-
120-
err = hkdf_expand(hkdf->hmac_tfm, full_info, infolen + 9,
121-
okm, okmlen);
122-
kfree_sensitive(full_info);
123-
return err;
124-
}
125-
126-
void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf)
72+
void fscrypt_hkdf_expand(const struct hmac_sha512_key *hkdf, u8 context,
73+
const u8 *info, unsigned int infolen,
74+
u8 *okm, unsigned int okmlen)
12775
{
128-
crypto_free_shash(hkdf->hmac_tfm);
76+
struct hmac_sha512_ctx ctx;
77+
u8 counter = 1;
78+
u8 tmp[HKDF_HASHLEN];
79+
80+
WARN_ON_ONCE(okmlen > 255 * HKDF_HASHLEN);
81+
82+
for (unsigned int i = 0; i < okmlen; i += HKDF_HASHLEN) {
83+
hmac_sha512_init(&ctx, hkdf);
84+
if (i != 0)
85+
hmac_sha512_update(&ctx, &okm[i - HKDF_HASHLEN],
86+
HKDF_HASHLEN);
87+
hmac_sha512_update(&ctx, "fscrypt\0", 8);
88+
hmac_sha512_update(&ctx, &context, 1);
89+
hmac_sha512_update(&ctx, info, infolen);
90+
hmac_sha512_update(&ctx, &counter, 1);
91+
if (okmlen - i < HKDF_HASHLEN) {
92+
hmac_sha512_final(&ctx, tmp);
93+
memcpy(&okm[i], tmp, okmlen - i);
94+
memzero_explicit(tmp, sizeof(tmp));
95+
} else {
96+
hmac_sha512_final(&ctx, &okm[i]);
97+
}
98+
counter++;
99+
}
129100
}

fs/crypto/hooks.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ int fscrypt_prepare_setflags(struct inode *inode,
205205
mk = ci->ci_master_key;
206206
down_read(&mk->mk_sem);
207207
if (mk->mk_present)
208-
err = fscrypt_derive_dirhash_key(ci, mk);
208+
fscrypt_derive_dirhash_key(ci, mk);
209209
else
210210
err = -ENOKEY;
211211
up_read(&mk->mk_sem);

fs/crypto/keyring.c

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ struct fscrypt_keyring {
4242

4343
static void wipe_master_key_secret(struct fscrypt_master_key_secret *secret)
4444
{
45-
fscrypt_destroy_hkdf(&secret->hkdf);
4645
memzero_explicit(secret, sizeof(*secret));
4746
}
4847

@@ -587,21 +586,17 @@ static int add_master_key(struct super_block *sb,
587586
keyid_kdf_ctx =
588587
HKDF_CONTEXT_KEY_IDENTIFIER_FOR_HW_WRAPPED_KEY;
589588
}
590-
err = fscrypt_init_hkdf(&secret->hkdf, kdf_key, kdf_key_size);
589+
fscrypt_init_hkdf(&secret->hkdf, kdf_key, kdf_key_size);
591590
/*
592591
* Now that the KDF context is initialized, the raw KDF key is
593592
* no longer needed.
594593
*/
595594
memzero_explicit(kdf_key, kdf_key_size);
596-
if (err)
597-
return err;
598595

599596
/* Calculate the key identifier */
600-
err = fscrypt_hkdf_expand(&secret->hkdf, keyid_kdf_ctx, NULL, 0,
601-
key_spec->u.identifier,
602-
FSCRYPT_KEY_IDENTIFIER_SIZE);
603-
if (err)
604-
return err;
597+
fscrypt_hkdf_expand(&secret->hkdf, keyid_kdf_ctx, NULL, 0,
598+
key_spec->u.identifier,
599+
FSCRYPT_KEY_IDENTIFIER_SIZE);
605600
}
606601
return do_add_master_key(sb, secret, key_spec);
607602
}
@@ -835,24 +830,17 @@ fscrypt_get_test_dummy_secret(struct fscrypt_master_key_secret *secret)
835830
memcpy(secret->bytes, test_key, sizeof(test_key));
836831
}
837832

838-
int fscrypt_get_test_dummy_key_identifier(
833+
void fscrypt_get_test_dummy_key_identifier(
839834
u8 key_identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
840835
{
841836
struct fscrypt_master_key_secret secret;
842-
int err;
843837

844838
fscrypt_get_test_dummy_secret(&secret);
845-
846-
err = fscrypt_init_hkdf(&secret.hkdf, secret.bytes, secret.size);
847-
if (err)
848-
goto out;
849-
err = fscrypt_hkdf_expand(&secret.hkdf,
850-
HKDF_CONTEXT_KEY_IDENTIFIER_FOR_RAW_KEY,
851-
NULL, 0, key_identifier,
852-
FSCRYPT_KEY_IDENTIFIER_SIZE);
853-
out:
839+
fscrypt_init_hkdf(&secret.hkdf, secret.bytes, secret.size);
840+
fscrypt_hkdf_expand(&secret.hkdf,
841+
HKDF_CONTEXT_KEY_IDENTIFIER_FOR_RAW_KEY, NULL, 0,
842+
key_identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
854843
wipe_master_key_secret(&secret);
855-
return err;
856844
}
857845

858846
/**

0 commit comments

Comments
 (0)