2 Copyright (c) 2010 Nils Durner
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * @file src/util/crypto_hkdf.c
25 * @brief Hash-based KDF as defined in RFC 5869
26 * @see http://www.rfc-editor.org/rfc/rfc5869.txt
27 * @todo remove GNUNET references
30 * The following list of people have reviewed this code and considered
31 * it correct on the date given (if you reviewed it, please
32 * have your name added to the list):
34 * - Christian Grothoff (08.10.2010)
35 * - Nathan Evans (08.10.2010)
36 * - Matthias Wachs (08.10.2010)
39 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
42 * Set this to 0 if you compile this code outside of GNUnet.
44 #define GNUNET_BUILD 1
55 #include "gnunet_crypto_lib.h"
59 #define GNUNET_SYSERR -1
67 * @brief Compute the HMAC
68 * @todo use chunked buffers
69 * @param mac gcrypt MAC handle
71 * @param key_len length of key
72 * @param buf message to be processed
73 * @param buf_len length of buf
74 * @return HMAC, freed by caller via gcry_md_close/_reset
77 doHMAC (gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf,
80 gcry_md_setkey (mac, key, key_len);
81 gcry_md_write (mac, buf, buf_len);
83 return (const void *) gcry_md_read (mac, 0);
87 * @brief Generate pseudo-random key
88 * @param mac gcrypt HMAC handle
90 * @param xts_len length of the salt
91 * @param skm source key material
92 * @param skm_len length of skm
93 * @param prk result buffer (allocated by caller; at least gcry_md_dlen() bytes)
94 * @return GNUNET_YES on success
97 getPRK (gcry_md_hd_t mac, const void *xts, size_t xts_len, const void *skm,
98 size_t skm_len, void *prk)
102 ret = doHMAC (mac, xts, xts_len, skm, skm_len);
104 return GNUNET_SYSERR;
105 memcpy (prk, ret, gcry_md_get_algo_dlen (gcry_md_get_algo (mac)));
113 dump (const char *src, const void *p, unsigned int l)
117 printf ("\n%s: ", src);
118 for (i = 0; i < l; i++)
120 printf ("%2x", (int) ((const unsigned char *) p)[i]);
129 * @param result buffer for the derived key, allocated by caller
130 * @param out_len desired length of the derived key
131 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
132 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
134 * @param xts_len length of xts
135 * @param skm source key material
136 * @param skm_len length of skm
137 * @param argp va_list of void * & size_t pairs for context chunks
138 * @return #GNUNET_YES on success
141 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
142 const void *xts, size_t xts_len, const void *skm,
143 size_t skm_len, va_list argp)
146 static gcry_md_hd_t xtr;
147 static gcry_md_hd_t prf;
152 unsigned int k = gcry_md_get_algo_dlen (prf_algo);
153 unsigned int xtr_len = gcry_md_get_algo_dlen (xtr_algo);
160 return GNUNET_SYSERR;
163 if (GPG_ERR_NO_ERROR !=
164 gcry_md_open (&xtr, xtr_algo, GCRY_MD_FLAG_HMAC))
165 return GNUNET_SYSERR;
167 if (GPG_ERR_NO_ERROR !=
168 gcry_md_open (&prf, prf_algo, GCRY_MD_FLAG_HMAC))
171 return GNUNET_SYSERR;
181 va_copy (args, argp);
184 while (NULL != va_arg (args, void *))
185 ctx_len += va_arg (args, size_t);
189 memset (result, 0, out_len);
190 if (getPRK (xtr, xts, xts_len, skm, skm_len, prk) != GNUNET_YES)
193 dump ("PRK", prk, xtr_len);
201 size_t plain_len = k + ctx_len + 1;
202 char plain[plain_len];
207 va_copy (args, argp);
208 while ((ctx = va_arg (args, void *)))
212 len = va_arg (args, size_t);
213 memcpy (dst, ctx, len);
220 memset (plain + k + ctx_len, 1, 1);
222 dump ("K(1)", plain, plain_len);
224 hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1);
227 memcpy (result, hc, k);
232 for (i = 1; i < t; i++)
234 memcpy (plain, result - k, k);
235 memset (plain + k + ctx_len, i + 1, 1);
238 dump ("K(i+1)", plain, plain_len);
240 hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
243 memcpy (result, hc, k);
252 memcpy (plain, result - k, k);
255 memset (plain + k + ctx_len, i, 1);
258 dump ("K(t):d", plain, plain_len);
261 hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
263 hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k);
266 memcpy (result, hc, d);
269 dump ("result", result - k, out_len);
284 * @param result buffer for the derived key, allocated by caller
285 * @param out_len desired length of the derived key
286 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
287 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
289 * @param xts_len length of @a xts
290 * @param skm source key material
291 * @param skm_len length of @a skm
292 * @return #GNUNET_YES on success
295 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
296 const void *xts, size_t xts_len, const void *skm,
302 va_start (argp, skm_len);
304 GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts, xts_len,
311 /* end of crypto_hkdf.c */