crypto_hkdf.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. Copyright (c) 2010 Nils Durner
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. /**
  20. * @file src/util/crypto_hkdf.c
  21. * @brief Hash-based KDF as defined in RFC 5869
  22. * @see http://www.rfc-editor.org/rfc/rfc5869.txt
  23. * @todo remove GNUNET references
  24. * @author Nils Durner
  25. *
  26. * The following list of people have reviewed this code and considered
  27. * it correct on the date given (if you reviewed it, please
  28. * have your name added to the list):
  29. *
  30. * - Christian Grothoff (08.10.2010)
  31. * - Nathan Evans (08.10.2010)
  32. * - Matthias Wachs (08.10.2010)
  33. */
  34. #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
  35. /**
  36. * Set this to 0 if you compile this code outside of GNUnet.
  37. */
  38. #define GNUNET_BUILD 1
  39. /**
  40. * Enable debugging.
  41. */
  42. #define DEBUG_HKDF 0
  43. #if GNUNET_BUILD
  44. #include "platform.h"
  45. #include "gnunet_crypto_lib.h"
  46. #else
  47. #define GNUNET_NO 0
  48. #define GNUNET_YES 1
  49. #define GNUNET_SYSERR -1
  50. #include <stdlib.h>
  51. #endif
  52. #include <gcrypt.h>
  53. /**
  54. * @brief Compute the HMAC
  55. * @todo use chunked buffers
  56. * @param mac gcrypt MAC handle
  57. * @param key HMAC key
  58. * @param key_len length of key
  59. * @param buf message to be processed
  60. * @param buf_len length of buf
  61. * @return HMAC, freed by caller via gcry_md_close/_reset
  62. */
  63. static const void *
  64. doHMAC (gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf,
  65. size_t buf_len)
  66. {
  67. gcry_md_setkey (mac, key, key_len);
  68. gcry_md_write (mac, buf, buf_len);
  69. return (const void *) gcry_md_read (mac, 0);
  70. }
  71. /**
  72. * @brief Generate pseudo-random key
  73. * @param mac gcrypt HMAC handle
  74. * @param xts salt
  75. * @param xts_len length of the @a xts salt
  76. * @param skm source key material
  77. * @param skm_len length of @a skm
  78. * @param prk result buffer (allocated by caller; at least gcry_md_dlen() bytes)
  79. * @return #GNUNET_YES on success
  80. */
  81. static int
  82. getPRK (gcry_md_hd_t mac, const void *xts, size_t xts_len, const void *skm,
  83. size_t skm_len, void *prk)
  84. {
  85. const void *ret;
  86. ret = doHMAC (mac, xts, xts_len, skm, skm_len);
  87. if (ret == NULL)
  88. return GNUNET_SYSERR;
  89. memcpy (prk, ret, gcry_md_get_algo_dlen (gcry_md_get_algo (mac)));
  90. return GNUNET_YES;
  91. }
  92. #if DEBUG_HKDF
  93. static void
  94. dump (const char *src, const void *p, unsigned int l)
  95. {
  96. unsigned int i;
  97. printf ("\n%s: ", src);
  98. for (i = 0; i < l; i++)
  99. {
  100. printf ("%2x", (int) ((const unsigned char *) p)[i]);
  101. }
  102. printf ("\n");
  103. }
  104. #endif
  105. /**
  106. * @brief Derive key
  107. * @param result buffer for the derived key, allocated by caller
  108. * @param out_len desired length of the derived key
  109. * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
  110. * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
  111. * @param xts salt
  112. * @param xts_len length of @a xts
  113. * @param skm source key material
  114. * @param skm_len length of @a skm
  115. * @param argp va_list of void * & size_t pairs for context chunks
  116. * @return #GNUNET_YES on success
  117. */
  118. int
  119. GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
  120. const void *xts, size_t xts_len, const void *skm,
  121. size_t skm_len, va_list argp)
  122. {
  123. gcry_md_hd_t xtr;
  124. gcry_md_hd_t prf;
  125. const void *hc;
  126. unsigned long i;
  127. unsigned long t;
  128. unsigned long d;
  129. unsigned int k = gcry_md_get_algo_dlen (prf_algo);
  130. unsigned int xtr_len = gcry_md_get_algo_dlen (xtr_algo);
  131. char prk[xtr_len];
  132. int ret;
  133. size_t ctx_len;
  134. va_list args;
  135. if (0 == k)
  136. return GNUNET_SYSERR;
  137. if (GPG_ERR_NO_ERROR !=
  138. gcry_md_open (&xtr, xtr_algo, GCRY_MD_FLAG_HMAC))
  139. return GNUNET_SYSERR;
  140. if (GPG_ERR_NO_ERROR !=
  141. gcry_md_open (&prf, prf_algo, GCRY_MD_FLAG_HMAC))
  142. {
  143. gcry_md_close (xtr);
  144. return GNUNET_SYSERR;
  145. }
  146. va_copy (args, argp);
  147. ctx_len = 0;
  148. while (NULL != va_arg (args, void *))
  149. ctx_len += va_arg (args, size_t);
  150. va_end (args);
  151. memset (result, 0, out_len);
  152. if (getPRK (xtr, xts, xts_len, skm, skm_len, prk) != GNUNET_YES)
  153. goto hkdf_error;
  154. #if DEBUG_HKDF
  155. dump ("PRK", prk, xtr_len);
  156. #endif
  157. t = out_len / k;
  158. d = out_len % k;
  159. /* K(1) */
  160. {
  161. size_t plain_len = k + ctx_len + 1;
  162. char plain[plain_len];
  163. const void *ctx;
  164. char *dst;
  165. dst = plain + k;
  166. va_copy (args, argp);
  167. while ((ctx = va_arg (args, void *)))
  168. {
  169. size_t len;
  170. len = va_arg (args, size_t);
  171. memcpy (dst, ctx, len);
  172. dst += len;
  173. }
  174. va_end (args);
  175. if (t > 0)
  176. {
  177. memset (plain + k + ctx_len, 1, 1);
  178. #if DEBUG_HKDF
  179. dump ("K(1)", plain, plain_len);
  180. #endif
  181. hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1);
  182. if (hc == NULL)
  183. goto hkdf_error;
  184. memcpy (result, hc, k);
  185. result += k;
  186. }
  187. /* K(i+1) */
  188. for (i = 1; i < t; i++)
  189. {
  190. memcpy (plain, result - k, k);
  191. memset (plain + k + ctx_len, i + 1, 1);
  192. gcry_md_reset (prf);
  193. #if DEBUG_HKDF
  194. dump ("K(i+1)", plain, plain_len);
  195. #endif
  196. hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
  197. if (hc == NULL)
  198. goto hkdf_error;
  199. memcpy (result, hc, k);
  200. result += k;
  201. }
  202. /* K(t):d */
  203. if (d > 0)
  204. {
  205. if (t > 0)
  206. {
  207. memcpy (plain, result - k, k);
  208. i++;
  209. }
  210. memset (plain + k + ctx_len, i, 1);
  211. gcry_md_reset (prf);
  212. #if DEBUG_HKDF
  213. dump ("K(t):d", plain, plain_len);
  214. #endif
  215. if (t > 0)
  216. hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
  217. else
  218. hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k);
  219. if (hc == NULL)
  220. goto hkdf_error;
  221. memcpy (result, hc, d);
  222. }
  223. #if DEBUG_HKDF
  224. dump ("result", result - k, out_len);
  225. #endif
  226. ret = GNUNET_YES;
  227. goto hkdf_ok;
  228. }
  229. hkdf_error:
  230. ret = GNUNET_SYSERR;
  231. hkdf_ok:
  232. gcry_md_close (xtr);
  233. gcry_md_close (prf);
  234. return ret;
  235. }
  236. /**
  237. * @brief Derive key
  238. * @param result buffer for the derived key, allocated by caller
  239. * @param out_len desired length of the derived key
  240. * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
  241. * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
  242. * @param xts salt
  243. * @param xts_len length of @a xts
  244. * @param skm source key material
  245. * @param skm_len length of @a skm
  246. * @return #GNUNET_YES on success
  247. */
  248. int
  249. GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
  250. const void *xts, size_t xts_len, const void *skm,
  251. size_t skm_len, ...)
  252. {
  253. va_list argp;
  254. int ret;
  255. va_start (argp, skm_len);
  256. ret =
  257. GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts, xts_len,
  258. skm, skm_len, argp);
  259. va_end (argp);
  260. return ret;
  261. }
  262. /* end of crypto_hkdf.c */