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
34 #include "gnunet_crypto_lib.h"
36 #define DEBUG_HKDF GNUNET_NO
39 * @brief Compute the HMAC
40 * @todo use chunked buffers
41 * @param mac gcrypt MAC handle
43 * @param key_len length of key
44 * @param buf message to be processed
45 * @param buf_len length of buf
46 * @return HMAC, freed by caller via gcry_md_close/_reset
49 doHMAC (gcry_md_hd_t mac,
50 const void *key, size_t key_len,
51 const void *buf, size_t buf_len)
53 gcry_md_setkey (mac, key, key_len);
54 gcry_md_write (mac, buf, buf_len);
56 return (const void *) gcry_md_read (mac, 0);
60 * @brief Generate pseudo-random key
61 * @param mac gcrypt HMAC handle
63 * @param xts_len length of the salt
64 * @param skm source key material
65 * @param skm_len length of skm
66 * @param prk result buffer (allocated by caller; at least gcry_md_dlen() bytes)
67 * @return GNUNET_YES on success
70 getPRK (gcry_md_hd_t mac,
71 const void *xts, size_t xts_len,
72 const void *skm, size_t skm_len,
77 ret = doHMAC (mac, xts, xts_len, skm, skm_len);
82 gcry_md_get_algo_dlen (gcry_md_get_algo (mac)));
96 printf("\n%s: ", src);
97 for (i = 0; i < l; i++)
99 printf("%2x", (int) ((const unsigned char *) p)[i]);
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_...
113 * @param xts_len length of xts
114 * @param skm source key material
115 * @param skm_len length of skm
116 * @param argp va_list of void * & size_t pairs for context chunks
117 * @return GNUNET_YES on success
120 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len,
121 int xtr_algo, int prf_algo,
122 const void *xts, size_t xts_len,
123 const void *skm, size_t skm_len,
127 unsigned long i, t, d;
128 unsigned int k = gcry_md_get_algo_dlen (prf_algo);
129 unsigned int xtr_len = gcry_md_get_algo_dlen (xtr_algo);
132 gcry_md_hd_t xtr, prf;
137 return GNUNET_SYSERR;
139 // FIXME: what is the check for?
140 if (out_len > (2 ^ 32 * k))
141 return GNUNET_SYSERR;
143 if (gcry_md_open(&xtr, xtr_algo, GCRY_MD_FLAG_HMAC) != GPG_ERR_NO_ERROR)
144 return GNUNET_SYSERR;
146 if (gcry_md_open(&prf, prf_algo, GCRY_MD_FLAG_HMAC) != GPG_ERR_NO_ERROR)
149 return GNUNET_SYSERR;
152 va_copy (args, argp);
155 while (NULL != va_arg (args, void *))
156 ctx_len += va_arg (args, size_t);
159 memset (result, 0, out_len);
160 if (getPRK (xtr, xts, xts_len, skm, skm_len, prk)
164 dump("PRK", prk, xtr_len);
172 size_t plain_len = k + ctx_len + 1;
173 char plain[plain_len];
178 va_copy (args, argp);
179 while ((ctx = va_arg (args, void *)))
183 len = va_arg (args, size_t);
184 memcpy (dst, ctx, len);
191 memset (plain + k + ctx_len, 1, 1);
193 dump("K(1)", plain, plain_len);
197 xtr_len, &plain[k], ctx_len + 1);
200 memcpy (result, hc, k);
205 for (i = 1; i < t; i++)
207 memcpy (plain, result - k, k);
208 memset (plain + k + ctx_len, i + 1, 1);
211 dump("K(i+1)", plain, plain_len);
213 hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
216 memcpy (result, hc, k);
224 memcpy (plain, result - k, k);
225 memset (plain + k + ctx_len, i + 1, 1);
228 dump("K(t):d", plain, plain_len);
231 hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
233 hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k);
236 memcpy (result, hc, d);
239 dump("result", result - k, out_len);
257 * @param result buffer for the derived key, allocated by caller
258 * @param out_len desired length of the derived key
259 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
260 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
262 * @param xts_len length of xts
263 * @param skm source key material
264 * @param skm_len length of skm
265 * @param ctx context info
266 * @param ctx_len length of ctx
267 * @return GNUNET_YES on success
270 GNUNET_CRYPTO_hkdf (void *result, size_t out_len,
271 int xtr_algo, int prf_algo,
272 const void *xts, size_t xts_len,
273 const void *skm, size_t skm_len,
279 va_start(argp, skm_len);
280 ret = GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts,
281 xts_len, skm, skm_len, argp);
287 /* end of crypto_hkdf.c */