~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/crypto/hkdf.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /fs/crypto/hkdf.c (Version linux-6.12-rc7) and /fs/crypto/hkdf.c (Version linux-5.15.171)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php