Whamcloud - gitweb
LU-14880 libcfs: Use crypto/sha2.h if available
[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 #ifdef HAVE_CRYPTO_SHA2_HEADER
18 #include <crypto/sha2.h>
19 #else
20 #include <crypto/sha.h>
21 #endif
22 #include "llcrypt_private.h"
23
24 /*
25  * HKDF supports any unkeyed cryptographic hash algorithm, but llcrypt uses
26  * SHA-512 because it is reasonably secure and efficient; and since it produces
27  * a 64-byte digest, deriving an AES-256-XTS key preserves all 64 bytes of
28  * entropy from the master key and requires only one iteration of HKDF-Expand.
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 llcrypt still does HKDF-Extract.  No
45  * salt is used, since llcrypt 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 */
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         SHASH_DESC_ON_STACK(desc, hmac_tfm);
55         int err;
56
57         err = crypto_shash_setkey(hmac_tfm, default_salt, HKDF_HASHLEN);
58         if (err)
59                 return err;
60
61         desc->tfm = hmac_tfm;
62         err = crypto_shash_digest(desc, ikm, ikmlen, prk);
63         shash_desc_zero(desc);
64         return err;
65 }
66
67 /*
68  * Compute HKDF-Extract using the given master key as the input keying material,
69  * and prepare an HMAC transform object keyed by the resulting pseudorandom key.
70  *
71  * Afterwards, the keyed HMAC transform object can be used for HKDF-Expand many
72  * times without having to recompute HKDF-Extract each time.
73  */
74 int llcrypt_init_hkdf(struct llcrypt_hkdf *hkdf, const u8 *master_key,
75                       unsigned int master_key_size)
76 {
77         struct crypto_shash *hmac_tfm;
78         u8 prk[HKDF_HASHLEN];
79         int err;
80
81         hmac_tfm = crypto_alloc_shash(HKDF_HMAC_ALG, 0, 0);
82         if (IS_ERR(hmac_tfm)) {
83                 llcrypt_err(NULL, "Error allocating " HKDF_HMAC_ALG ": %ld",
84                             PTR_ERR(hmac_tfm));
85                 return PTR_ERR(hmac_tfm);
86         }
87
88         if (WARN_ON(crypto_shash_digestsize(hmac_tfm) != sizeof(prk))) {
89                 err = -EINVAL;
90                 goto err_free_tfm;
91         }
92
93         err = hkdf_extract(hmac_tfm, master_key, master_key_size, prk);
94         if (err)
95                 goto err_free_tfm;
96
97         err = crypto_shash_setkey(hmac_tfm, prk, sizeof(prk));
98         if (err)
99                 goto err_free_tfm;
100
101         hkdf->hmac_tfm = hmac_tfm;
102         goto out;
103
104 err_free_tfm:
105         crypto_free_shash(hmac_tfm);
106 out:
107         memzero_explicit(prk, sizeof(prk));
108         return err;
109 }
110
111 /*
112  * HKDF-Expand (RFC 5869 section 2.3).  This expands the pseudorandom key, which
113  * was already keyed into 'hkdf->hmac_tfm' by llcrypt_init_hkdf(), into 'okmlen'
114  * bytes of output keying material parameterized by the application-specific
115  * 'info' of length 'infolen' bytes, prefixed by "llcrypt\0" and the 'context'
116  * byte.  This is thread-safe and may be called by multiple threads in parallel.
117  *
118  * ('context' isn't part of the HKDF specification; it's just a prefix llcrypt
119  * adds to its application-specific info strings to guarantee that it doesn't
120  * accidentally repeat an info string when using HKDF for different purposes.)
121  */
122 int llcrypt_hkdf_expand(struct llcrypt_hkdf *hkdf, u8 context,
123                         const u8 *info, unsigned int infolen,
124                         u8 *okm, unsigned int okmlen)
125 {
126         SHASH_DESC_ON_STACK(desc, hkdf->hmac_tfm);
127         u8 prefix[9];
128         unsigned int i;
129         int err;
130         const u8 *prev = NULL;
131         u8 counter = 1;
132         u8 tmp[HKDF_HASHLEN];
133
134         if (WARN_ON(okmlen > 255 * HKDF_HASHLEN))
135                 return -EINVAL;
136
137         desc->tfm = hkdf->hmac_tfm;
138
139         memcpy(prefix, "fscrypt\0", 8);
140         prefix[8] = context;
141
142         for (i = 0; i < okmlen; i += HKDF_HASHLEN) {
143
144                 err = crypto_shash_init(desc);
145                 if (err)
146                         goto out;
147
148                 if (prev) {
149                         err = crypto_shash_update(desc, prev, HKDF_HASHLEN);
150                         if (err)
151                                 goto out;
152                 }
153
154                 err = crypto_shash_update(desc, prefix, sizeof(prefix));
155                 if (err)
156                         goto out;
157
158                 err = crypto_shash_update(desc, info, infolen);
159                 if (err)
160                         goto out;
161
162                 BUILD_BUG_ON(sizeof(counter) != 1);
163                 if (okmlen - i < HKDF_HASHLEN) {
164                         err = crypto_shash_finup(desc, &counter, 1, tmp);
165                         if (err)
166                                 goto out;
167                         memcpy(&okm[i], tmp, okmlen - i);
168                         memzero_explicit(tmp, sizeof(tmp));
169                 } else {
170                         err = crypto_shash_finup(desc, &counter, 1, &okm[i]);
171                         if (err)
172                                 goto out;
173                 }
174                 counter++;
175                 prev = &okm[i];
176         }
177         err = 0;
178 out:
179         if (unlikely(err))
180                 memzero_explicit(okm, okmlen); /* so caller doesn't need to */
181         shash_desc_zero(desc);
182         return err;
183 }
184
185 void llcrypt_destroy_hkdf(struct llcrypt_hkdf *hkdf)
186 {
187         crypto_free_shash(hkdf->hmac_tfm);
188 }