WIP FPC-III support
[linux/fpc-iii.git] / fs / crypto / hkdf.c
blobe0ec2105550534c8cc188712d8826168898fbff3
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".
7 * This is used to derive keys from the fscrypt master keys.
9 * Copyright 2019 Google LLC
12 #include <crypto/hash.h>
13 #include <crypto/sha2.h>
15 #include "fscrypt_private.h"
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.
23 #define HKDF_HMAC_ALG "hmac(sha512)"
24 #define HKDF_HASHLEN SHA512_DIGEST_SIZE
27 * HKDF consists of two steps:
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.
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.
42 /* HKDF-Extract (RFC 5869 section 2.2), unsalted */
43 static int hkdf_extract(struct crypto_shash *hmac_tfm, const u8 *ikm,
44 unsigned int ikmlen, u8 prk[HKDF_HASHLEN])
46 static const u8 default_salt[HKDF_HASHLEN];
47 int err;
49 err = crypto_shash_setkey(hmac_tfm, default_salt, HKDF_HASHLEN);
50 if (err)
51 return err;
53 return crypto_shash_tfm_digest(hmac_tfm, ikm, ikmlen, prk);
57 * Compute HKDF-Extract using the given master key as the input keying material,
58 * and prepare an HMAC transform object keyed by the resulting pseudorandom key.
60 * Afterwards, the keyed HMAC transform object can be used for HKDF-Expand many
61 * times without having to recompute HKDF-Extract each time.
63 int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
64 unsigned int master_key_size)
66 struct crypto_shash *hmac_tfm;
67 u8 prk[HKDF_HASHLEN];
68 int err;
70 hmac_tfm = crypto_alloc_shash(HKDF_HMAC_ALG, 0, 0);
71 if (IS_ERR(hmac_tfm)) {
72 fscrypt_err(NULL, "Error allocating " HKDF_HMAC_ALG ": %ld",
73 PTR_ERR(hmac_tfm));
74 return PTR_ERR(hmac_tfm);
77 if (WARN_ON(crypto_shash_digestsize(hmac_tfm) != sizeof(prk))) {
78 err = -EINVAL;
79 goto err_free_tfm;
82 err = hkdf_extract(hmac_tfm, master_key, master_key_size, prk);
83 if (err)
84 goto err_free_tfm;
86 err = crypto_shash_setkey(hmac_tfm, prk, sizeof(prk));
87 if (err)
88 goto err_free_tfm;
90 hkdf->hmac_tfm = hmac_tfm;
91 goto out;
93 err_free_tfm:
94 crypto_free_shash(hmac_tfm);
95 out:
96 memzero_explicit(prk, sizeof(prk));
97 return err;
101 * HKDF-Expand (RFC 5869 section 2.3). This expands the pseudorandom key, which
102 * was already keyed into 'hkdf->hmac_tfm' by fscrypt_init_hkdf(), into 'okmlen'
103 * bytes of output keying material parameterized by the application-specific
104 * 'info' of length 'infolen' bytes, prefixed by "fscrypt\0" and the 'context'
105 * byte. This is thread-safe and may be called by multiple threads in parallel.
107 * ('context' isn't part of the HKDF specification; it's just a prefix fscrypt
108 * adds to its application-specific info strings to guarantee that it doesn't
109 * accidentally repeat an info string when using HKDF for different purposes.)
111 int fscrypt_hkdf_expand(const struct fscrypt_hkdf *hkdf, u8 context,
112 const u8 *info, unsigned int infolen,
113 u8 *okm, unsigned int okmlen)
115 SHASH_DESC_ON_STACK(desc, hkdf->hmac_tfm);
116 u8 prefix[9];
117 unsigned int i;
118 int err;
119 const u8 *prev = NULL;
120 u8 counter = 1;
121 u8 tmp[HKDF_HASHLEN];
123 if (WARN_ON(okmlen > 255 * HKDF_HASHLEN))
124 return -EINVAL;
126 desc->tfm = hkdf->hmac_tfm;
128 memcpy(prefix, "fscrypt\0", 8);
129 prefix[8] = context;
131 for (i = 0; i < okmlen; i += HKDF_HASHLEN) {
133 err = crypto_shash_init(desc);
134 if (err)
135 goto out;
137 if (prev) {
138 err = crypto_shash_update(desc, prev, HKDF_HASHLEN);
139 if (err)
140 goto out;
143 err = crypto_shash_update(desc, prefix, sizeof(prefix));
144 if (err)
145 goto out;
147 err = crypto_shash_update(desc, info, infolen);
148 if (err)
149 goto out;
151 BUILD_BUG_ON(sizeof(counter) != 1);
152 if (okmlen - i < HKDF_HASHLEN) {
153 err = crypto_shash_finup(desc, &counter, 1, tmp);
154 if (err)
155 goto out;
156 memcpy(&okm[i], tmp, okmlen - i);
157 memzero_explicit(tmp, sizeof(tmp));
158 } else {
159 err = crypto_shash_finup(desc, &counter, 1, &okm[i]);
160 if (err)
161 goto out;
163 counter++;
164 prev = &okm[i];
166 err = 0;
167 out:
168 if (unlikely(err))
169 memzero_explicit(okm, okmlen); /* so caller doesn't need to */
170 shash_desc_zero(desc);
171 return err;
174 void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf)
176 crypto_free_shash(hkdf->hmac_tfm);