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