crypto_hkdf.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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-crypto-hkdf", __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. #include "benchmark.h"
  47. #else
  48. #define GNUNET_NO 0
  49. #define GNUNET_YES 1
  50. #define GNUNET_SYSERR -1
  51. #include <stdlib.h>
  52. #endif
  53. #include <gcrypt.h>
  54. /**
  55. * @brief Compute the HMAC
  56. * @todo use chunked buffers
  57. * @param mac gcrypt MAC handle
  58. * @param key HMAC key
  59. * @param key_len length of key
  60. * @param buf message to be processed
  61. * @param buf_len length of buf
  62. * @return HMAC, freed by caller via gcry_md_close/_reset
  63. */
  64. static const void *
  65. doHMAC (gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf,
  66. size_t buf_len)
  67. {
  68. gcry_md_setkey (mac, key, key_len);
  69. gcry_md_write (mac, buf, buf_len);
  70. return (const void *) gcry_md_read (mac, 0);
  71. }
  72. /**
  73. * @brief Generate pseudo-random key
  74. * @param mac gcrypt HMAC handle
  75. * @param xts salt
  76. * @param xts_len length of the @a xts salt
  77. * @param skm source key material
  78. * @param skm_len length of @a skm
  79. * @param prk result buffer (allocated by caller; at least gcry_md_dlen() bytes)
  80. * @return #GNUNET_YES on success
  81. */
  82. static int
  83. getPRK (gcry_md_hd_t mac, const void *xts, size_t xts_len, const void *skm,
  84. size_t skm_len, void *prk)
  85. {
  86. const void *ret;
  87. ret = doHMAC (mac, xts, xts_len, skm, skm_len);
  88. if (ret == NULL)
  89. return GNUNET_SYSERR;
  90. GNUNET_memcpy (prk, ret, gcry_md_get_algo_dlen (gcry_md_get_algo (mac)));
  91. return GNUNET_YES;
  92. }
  93. #if DEBUG_HKDF
  94. static void
  95. dump (const char *src, const void *p, unsigned int l)
  96. {
  97. unsigned int i;
  98. printf ("\n%s: ", src);
  99. for (i = 0; i < l; i++)
  100. {
  101. printf ("%2x", (int) ((const unsigned char *) p)[i]);
  102. }
  103. printf ("\n");
  104. }
  105. #endif
  106. /**
  107. * @brief Derive key
  108. * @param result buffer for the derived key, allocated by caller
  109. * @param out_len desired length of the derived key
  110. * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
  111. * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
  112. * @param xts salt
  113. * @param xts_len length of @a xts
  114. * @param skm source key material
  115. * @param skm_len length of @a skm
  116. * @param argp va_list of void * & size_t pairs for context chunks
  117. * @return #GNUNET_YES on success
  118. */
  119. int
  120. GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
  121. const void *xts, size_t xts_len, const void *skm,
  122. size_t skm_len, va_list argp)
  123. {
  124. gcry_md_hd_t xtr;
  125. gcry_md_hd_t prf;
  126. const void *hc;
  127. unsigned long i;
  128. unsigned long t;
  129. unsigned long d;
  130. unsigned int k = gcry_md_get_algo_dlen (prf_algo);
  131. unsigned int xtr_len = gcry_md_get_algo_dlen (xtr_algo);
  132. char prk[xtr_len];
  133. int ret;
  134. size_t ctx_len;
  135. va_list args;
  136. BENCHMARK_START (hkdf);
  137. if (0 == k)
  138. return GNUNET_SYSERR;
  139. if (GPG_ERR_NO_ERROR !=
  140. gcry_md_open (&xtr, xtr_algo, GCRY_MD_FLAG_HMAC))
  141. return GNUNET_SYSERR;
  142. if (GPG_ERR_NO_ERROR !=
  143. gcry_md_open (&prf, prf_algo, GCRY_MD_FLAG_HMAC))
  144. {
  145. gcry_md_close (xtr);
  146. return GNUNET_SYSERR;
  147. }
  148. va_copy (args, argp);
  149. ctx_len = 0;
  150. while (NULL != va_arg (args, void *))
  151. ctx_len += va_arg (args, size_t);
  152. va_end (args);
  153. memset (result, 0, out_len);
  154. if (getPRK (xtr, xts, xts_len, skm, skm_len, prk) != GNUNET_YES)
  155. goto hkdf_error;
  156. #if DEBUG_HKDF
  157. dump ("PRK", prk, xtr_len);
  158. #endif
  159. t = out_len / k;
  160. d = out_len % k;
  161. /* K(1) */
  162. {
  163. size_t plain_len = k + ctx_len + 1;
  164. char plain[plain_len];
  165. const void *ctx;
  166. char *dst;
  167. dst = plain + k;
  168. va_copy (args, argp);
  169. while ((ctx = va_arg (args, void *)))
  170. {
  171. size_t len;
  172. len = va_arg (args, size_t);
  173. GNUNET_memcpy (dst, ctx, len);
  174. dst += len;
  175. }
  176. va_end (args);
  177. if (t > 0)
  178. {
  179. memset (plain + k + ctx_len, 1, 1);
  180. #if DEBUG_HKDF
  181. dump ("K(1)", plain, plain_len);
  182. #endif
  183. hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1);
  184. if (hc == NULL)
  185. goto hkdf_error;
  186. GNUNET_memcpy (result, hc, k);
  187. result += k;
  188. }
  189. /* K(i+1) */
  190. for (i = 1; i < t; i++)
  191. {
  192. GNUNET_memcpy (plain, result - k, k);
  193. memset (plain + k + ctx_len, i + 1, 1);
  194. gcry_md_reset (prf);
  195. #if DEBUG_HKDF
  196. dump ("K(i+1)", plain, plain_len);
  197. #endif
  198. hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
  199. if (hc == NULL)
  200. goto hkdf_error;
  201. GNUNET_memcpy (result, hc, k);
  202. result += k;
  203. }
  204. /* K(t):d */
  205. if (d > 0)
  206. {
  207. if (t > 0)
  208. {
  209. GNUNET_memcpy (plain, result - k, k);
  210. i++;
  211. }
  212. memset (plain + k + ctx_len, i, 1);
  213. gcry_md_reset (prf);
  214. #if DEBUG_HKDF
  215. dump ("K(t):d", plain, plain_len);
  216. #endif
  217. if (t > 0)
  218. hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
  219. else
  220. hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k);
  221. if (hc == NULL)
  222. goto hkdf_error;
  223. GNUNET_memcpy (result, hc, d);
  224. }
  225. #if DEBUG_HKDF
  226. dump ("result", result - k, out_len);
  227. #endif
  228. ret = GNUNET_YES;
  229. goto hkdf_ok;
  230. }
  231. hkdf_error:
  232. ret = GNUNET_SYSERR;
  233. hkdf_ok:
  234. gcry_md_close (xtr);
  235. gcry_md_close (prf);
  236. BENCHMARK_END (hkdf);
  237. return ret;
  238. }
  239. /**
  240. * @brief Derive key
  241. * @param result buffer for the derived key, allocated by caller
  242. * @param out_len desired length of the derived key
  243. * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
  244. * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
  245. * @param xts salt
  246. * @param xts_len length of @a xts
  247. * @param skm source key material
  248. * @param skm_len length of @a skm
  249. * @return #GNUNET_YES on success
  250. */
  251. int
  252. GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
  253. const void *xts, size_t xts_len, const void *skm,
  254. size_t skm_len, ...)
  255. {
  256. va_list argp;
  257. int ret;
  258. va_start (argp, skm_len);
  259. ret =
  260. GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts, xts_len,
  261. skm, skm_len, argp);
  262. va_end (argp);
  263. return ret;
  264. }
  265. /* end of crypto_hkdf.c */