1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * This is used to derive keys from the fscrypt master keys.
4  *
5  * Copyright 2019 Google LLC
6  */
7 
8 #include <crypto/hash.h>
9 #include <crypto/sha2.h>
10 #include <crypto/hkdf.h>
11 
12 #include "fscrypt_private.h"
13 
14 /*
15  * HKDF supports any unkeyed cryptographic hash algorithm, but fscrypt uses
16  * SHA-512 because it is well-established, secure, and reasonably efficient.
17  *
18  * HKDF-SHA256 was also considered, as its 256-bit security strength would be
19  * sufficient here.  A 512-bit security strength is "nice to have", though.
20  * Also, on 64-bit CPUs, SHA-512 is usually just as fast as SHA-256.  In the
21  * common case of deriving an AES-256-XTS key (512 bits), that can result in
22  * HKDF-SHA512 being much faster than HKDF-SHA256, as the longer digest size of
23  * SHA-512 causes HKDF-Expand to only need to do one iteration rather than two.
24  */
25 #define HKDF_HMAC_ALG		"hmac(sha512)"
26 #define HKDF_HASHLEN		SHA512_DIGEST_SIZE
27 
28 /*
29  * HKDF consists of two steps:
30  *
31  * 1. HKDF-Extract: extract a pseudorandom key of length HKDF_HASHLEN bytes from
32  *    the input keying material and optional salt.
33  * 2. HKDF-Expand: expand the pseudorandom key into output keying material of
34  *    any length, parameterized by an application-specific info string.
35  *
36  * HKDF-Extract can be skipped if the input is already a pseudorandom key of
37  * length HKDF_HASHLEN bytes.  However, cipher modes other than AES-256-XTS take
38  * shorter keys, and we don't want to force users of those modes to provide
39  * unnecessarily long master keys.  Thus fscrypt still does HKDF-Extract.  No
40  * salt is used, since fscrypt master keys should already be pseudorandom and
41  * there's no way to persist a random salt per master key from kernel mode.
42  */
43 
44 /*
45  * Compute HKDF-Extract using the given master key as the input keying material,
46  * and prepare an HMAC transform object keyed by the resulting pseudorandom key.
47  *
48  * Afterwards, the keyed HMAC transform object can be used for HKDF-Expand many
49  * times without having to recompute HKDF-Extract each time.
50  */
fscrypt_init_hkdf(struct fscrypt_hkdf * hkdf,const u8 * master_key,unsigned int master_key_size)51 int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
52 		      unsigned int master_key_size)
53 {
54 	struct crypto_shash *hmac_tfm;
55 	static const u8 default_salt[HKDF_HASHLEN];
56 	u8 prk[HKDF_HASHLEN];
57 	int err;
58 
59 	hmac_tfm = crypto_alloc_shash(HKDF_HMAC_ALG, 0, 0);
60 	if (IS_ERR(hmac_tfm)) {
61 		fscrypt_err(NULL, "Error allocating " HKDF_HMAC_ALG ": %ld",
62 			    PTR_ERR(hmac_tfm));
63 		return PTR_ERR(hmac_tfm);
64 	}
65 
66 	if (WARN_ON_ONCE(crypto_shash_digestsize(hmac_tfm) != sizeof(prk))) {
67 		err = -EINVAL;
68 		goto err_free_tfm;
69 	}
70 
71 	err = hkdf_extract(hmac_tfm, master_key, master_key_size,
72 			   default_salt, HKDF_HASHLEN, prk);
73 	if (err)
74 		goto err_free_tfm;
75 
76 	err = crypto_shash_setkey(hmac_tfm, prk, sizeof(prk));
77 	if (err)
78 		goto err_free_tfm;
79 
80 	hkdf->hmac_tfm = hmac_tfm;
81 	goto out;
82 
83 err_free_tfm:
84 	crypto_free_shash(hmac_tfm);
85 out:
86 	memzero_explicit(prk, sizeof(prk));
87 	return err;
88 }
89 
90 /*
91  * HKDF-Expand (RFC 5869 section 2.3).  This expands the pseudorandom key, which
92  * was already keyed into 'hkdf->hmac_tfm' by fscrypt_init_hkdf(), into 'okmlen'
93  * bytes of output keying material parameterized by the application-specific
94  * 'info' of length 'infolen' bytes, prefixed by "fscrypt\0" and the 'context'
95  * byte.  This is thread-safe and may be called by multiple threads in parallel.
96  *
97  * ('context' isn't part of the HKDF specification; it's just a prefix fscrypt
98  * adds to its application-specific info strings to guarantee that it doesn't
99  * accidentally repeat an info string when using HKDF for different purposes.)
100  */
fscrypt_hkdf_expand(const struct fscrypt_hkdf * hkdf,u8 context,const u8 * info,unsigned int infolen,u8 * okm,unsigned int okmlen)101 int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,
102 			const u8 *info, unsigned int infolen,
103 			u8 *okm, unsigned int okmlen)
104 {
105 	SHASH_DESC_ON_STACK(desc, hkdf->hmac_tfm);
106 	u8 *full_info;
107 	int err;
108 
109 	full_info = kzalloc(infolen + 9, GFP_KERNEL);
110 	if (!full_info)
111 		return -ENOMEM;
112 	desc->tfm = hkdf->hmac_tfm;
113 
114 	memcpy(full_info, "fscrypt\0", 8);
115 	full_info[8] = context;
116 	memcpy(full_info + 9, info, infolen);
117 
118 	err = hkdf_expand(hkdf->hmac_tfm, full_info, infolen + 9,
119 			  okm, okmlen);
120 	kfree_sensitive(full_info);
121 	return err;
122 }
123 
fscrypt_destroy_hkdf(struct fscrypt_hkdf * hkdf)124 void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf)
125 {
126 	crypto_free_shash(hkdf->hmac_tfm);
127 }
128