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