hkdf.c 5.4 KB

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