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