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