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