crypto_kdf.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file src/util/crypto_kdf.c
  18. * @brief Key derivation
  19. * @author Nils Durner
  20. * @author Jeffrey Burdges <burdges@gnunet.org>
  21. */
  22. #include <gcrypt.h>
  23. #include "platform.h"
  24. #include "gnunet_crypto_lib.h"
  25. #define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-kdf", __VA_ARGS__)
  26. /**
  27. * @brief Derive key
  28. * @param result buffer for the derived key, allocated by caller
  29. * @param out_len desired length of the derived key
  30. * @param xts salt
  31. * @param xts_len length of @a xts
  32. * @param skm source key material
  33. * @param skm_len length of @a skm
  34. * @param argp va_list of void * & size_t pairs for context chunks
  35. * @return #GNUNET_YES on success
  36. */
  37. int
  38. GNUNET_CRYPTO_kdf_v (void *result,
  39. size_t out_len,
  40. const void *xts,
  41. size_t xts_len,
  42. const void *skm,
  43. size_t skm_len,
  44. va_list argp)
  45. {
  46. /*
  47. * "Finally, we point out to a particularly advantageous instantiation using
  48. * HMAC-SHA512 as XTR and HMAC-SHA256 in PRF* (in which case the output from SHA-512 is
  49. * truncated to 256 bits). This makes sense in two ways: First, the extraction part is where we need a
  50. * stronger hash function due to the unconventional demand from the hash function in the extraction
  51. * setting. Second, as shown in Section 6, using HMAC with a truncated output as an extractor
  52. * allows to prove the security of HKDF under considerably weaker assumptions on the underlying
  53. * hash function."
  54. *
  55. * http://eprint.iacr.org/2010/264
  56. */return GNUNET_CRYPTO_hkdf_v (result,
  57. out_len,
  58. GCRY_MD_SHA512,
  59. GCRY_MD_SHA256,
  60. xts,
  61. xts_len,
  62. skm,
  63. skm_len,
  64. argp);
  65. }
  66. /**
  67. * @brief Derive key
  68. * @param result buffer for the derived key, allocated by caller
  69. * @param out_len desired length of the derived key
  70. * @param xts salt
  71. * @param xts_len length of @a xts
  72. * @param skm source key material
  73. * @param skm_len length of @a skm
  74. * @param ... void * & size_t pairs for context chunks
  75. * @return #GNUNET_YES on success
  76. */
  77. int
  78. GNUNET_CRYPTO_kdf (void *result,
  79. size_t out_len,
  80. const void *xts,
  81. size_t xts_len,
  82. const void *skm,
  83. size_t skm_len, ...)
  84. {
  85. va_list argp;
  86. int ret;
  87. va_start (argp, skm_len);
  88. ret = GNUNET_CRYPTO_kdf_v (result,
  89. out_len,
  90. xts,
  91. xts_len,
  92. skm,
  93. skm_len,
  94. argp);
  95. va_end (argp);
  96. return ret;
  97. }
  98. /**
  99. * Deterministically generate a pseudo-random number uniformly from the
  100. * integers modulo a libgcrypt mpi.
  101. *
  102. * @param[out] r MPI value set to the FDH
  103. * @param n MPI to work modulo
  104. * @param xts salt
  105. * @param xts_len length of @a xts
  106. * @param skm source key material
  107. * @param skm_len length of @a skm
  108. * @param ctx context string
  109. */
  110. void
  111. GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
  112. gcry_mpi_t n,
  113. const void *xts, size_t xts_len,
  114. const void *skm, size_t skm_len,
  115. const char *ctx)
  116. {
  117. gcry_error_t rc;
  118. unsigned int nbits;
  119. size_t rsize;
  120. uint16_t ctr;
  121. nbits = gcry_mpi_get_nbits (n);
  122. /* GNUNET_assert (nbits > 512); */
  123. ctr = 0;
  124. while (1)
  125. {
  126. /* Ain't clear if n is always divisible by 8 */
  127. uint8_t buf[ (nbits - 1) / 8 + 1 ];
  128. uint16_t ctr_nbo = htons (ctr);
  129. rc = GNUNET_CRYPTO_kdf (buf,
  130. sizeof(buf),
  131. xts, xts_len,
  132. skm, skm_len,
  133. ctx, strlen (ctx),
  134. &ctr_nbo, sizeof(ctr_nbo),
  135. NULL, 0);
  136. GNUNET_assert (GNUNET_YES == rc);
  137. rc = gcry_mpi_scan (r,
  138. GCRYMPI_FMT_USG,
  139. (const unsigned char *) buf,
  140. sizeof(buf),
  141. &rsize);
  142. GNUNET_assert (0 == rc); /* Allocation erro? */
  143. gcry_mpi_clear_highbit (*r, nbits);
  144. GNUNET_assert (0 == gcry_mpi_test_bit (*r, nbits));
  145. ++ctr;
  146. /* We reject this FDH if either *r > n and retry with another ctr */
  147. if (0 > gcry_mpi_cmp (*r, n))
  148. break;
  149. gcry_mpi_release (*r);
  150. }
  151. }
  152. /* end of crypto_kdf.c */