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
33 #include "gnunet_crypto_lib.h"
35 #define DEBUG_HKDF GNUNET_NO
38 * @brief Compute the HMAC
39 * @param mac gcrypt MAC handle
41 * @param key_len length of key
42 * @param buf message to be processed
43 * @param buf_len length of buf
44 * @return HMAC, freed by caller via gcry_md_close/_reset
47 doHMAC (gcry_md_hd_t mac,
48 const void *key, size_t key_len,
49 const void *buf, size_t buf_len)
51 gcry_md_setkey (mac, key, key_len);
52 gcry_md_write (mac, buf, buf_len);
54 return (const void *) gcry_md_read (mac, 0);
58 * @brief Generate pseudo-random key
59 * @param mac gcrypt HMAC handle
61 * @param xts_len length of the salt
62 * @param skm source key material
63 * @param skm_len length of skm
64 * @param prk result buffer (allocated by caller; at least gcry_md_dlen() bytes)
65 * @return GNUNET_YES on success
68 getPRK (gcry_md_hd_t mac,
69 const void *xts, unsigned long long xts_len, /* FIXME: size_t? */
70 const void *skm, unsigned long long skm_len,
75 ret = doHMAC (mac, xts, xts_len, skm, skm_len);
80 gcry_md_get_algo_dlen (gcry_md_get_algo (mac)));
94 printf("\n%s: ", src);
95 for (i = 0; i < l; i++)
97 printf("%2x", (int) ((const unsigned char *) p)[i]);
106 * @param result buffer for the derived key, allocated by caller
107 * @param out_len desired length of the derived key
108 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
109 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
111 * @param xts_len length of xts
112 * @param skm source key material
113 * @param skm_len length of skm
114 * @param argp va_list of void * & size_t pairs for context chunks
115 * @return GNUNET_YES on success
118 GNUNET_CRYPTO_hkdf_v (void *result, unsigned long long out_len,
119 int xtr_algo, int prf_algo,
120 const void *xts, size_t xts_len,
121 const void *skm, size_t skm_len,
126 unsigned long long plain_len;
127 unsigned long i, t, d;
128 unsigned int k, xtr_len;
130 gcry_md_hd_t xtr, prf;
135 xtr_len = gcry_md_get_algo_dlen (xtr_algo);
136 k = gcry_md_get_algo_dlen (prf_algo);
137 gcry_md_open(&xtr, xtr_algo, GCRY_MD_FLAG_HMAC);
138 gcry_md_open(&prf, prf_algo, GCRY_MD_FLAG_HMAC);
140 if (out_len > (2 ^ 32 * k) || !xtr_algo || !prf_algo)
141 return GNUNET_SYSERR;
143 va_copy (args, argp);
144 for (ctx_len = 0; va_arg (args, void *);)
145 ctx_len += va_arg (args, size_t);
148 prk = GNUNET_malloc (xtr_len);
150 memset (result, 0, out_len);
152 if (getPRK (xtr, xts, xts_len, skm, skm_len, prk)
156 dump("PRK", prk, xtr_len);
163 plain_len = k + ctx_len + 1;
164 plain = GNUNET_malloc (plain_len);
170 va_copy (args, argp);
171 while ((ctx = va_arg (args, void *)))
175 len = va_arg (args, size_t);
176 memcpy (dst, ctx, len);
184 dump("K(1)", plain, plain_len);
188 xtr_len, plain, ctx_len + 1);
191 memcpy (result, hc, k);
200 va_copy (args, argp);
201 while ((ctx = va_arg (args, void *)))
205 len = va_arg (args, size_t);
206 memcpy (dst, ctx, len);
213 for (i = 1; i < t; i++)
215 memcpy (plain, result - k, k);
216 memset (plain + k + ctx_len, i + 1, 1);
219 dump("K(i+1)", plain, plain_len);
221 hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
224 memcpy (result, hc, k);
232 memcpy (plain, result - k, k);
233 memset (plain + k + ctx_len, i + 1, 1);
236 dump("K(t):d", plain, plain_len);
238 hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
241 memcpy (result, hc, d);
244 dump("result", result - k, out_len);
254 GNUNET_free_non_null (plain);
264 * @param result buffer for the derived key, allocated by caller
265 * @param out_len desired length of the derived key
266 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
267 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
269 * @param xts_len length of xts
270 * @param skm source key material
271 * @param skm_len length of skm
272 * @param ctx context info
273 * @param ctx_len length of ctx
274 * @return GNUNET_YES on success
277 GNUNET_CRYPTO_hkdf (void *result, unsigned long long out_len,
278 int xtr_algo, int prf_algo,
279 const void *xts, size_t xts_len,
280 const void *skm, size_t skm_len,
286 va_start(argp, skm_len);
287 ret = GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts,
288 xts_len, skm, skm_len, argp);
294 /* end of crypto_hkdf.c */