fix
[oweals/gnunet.git] / src / util / crypto_hkdf.c
1 /*
2     Copyright (c) 2010 Nils Durner
3
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:
10
11     The above copyright notice and this permission notice shall be included in
12     all copies or substantial portions of the Software.
13
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
20     THE SOFTWARE.
21 */
22
23 /**
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
28  * @author Nils Durner
29  *
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):
33  *
34  * - Christian Grothoff (08.10.2010)
35  * - Nathan Evans (08.10.2010)
36  * - Matthias Wachs (08.10.2010)
37  */
38
39 /**
40  * Set this to 0 if you compile this code outside of GNUnet.
41  */
42 #define GNUNET_BUILD 1
43
44 /**
45  * Enable debugging.
46  */
47 #define DEBUG_HKDF 0
48
49
50 #if GNUNET_BUILD
51 #include "platform.h"
52 #include "gnunet_crypto_lib.h"
53 #else
54 #define GNUNET_NO 0
55 #define GNUNET_YES 1
56 #define GNUNET_SYSERR -1
57 #include <stdlib.h>
58 #endif
59
60 #include <gcrypt.h>
61
62
63 /**
64  * @brief Compute the HMAC
65  * @todo use chunked buffers
66  * @param mac gcrypt MAC handle
67  * @param key HMAC key
68  * @param key_len length of key
69  * @param buf message to be processed
70  * @param buf_len length of buf
71  * @return HMAC, freed by caller via gcry_md_close/_reset
72  */
73 static const void *
74 doHMAC (gcry_md_hd_t mac, 
75         const void *key, size_t key_len,
76         const void *buf, size_t buf_len)
77 {
78   gcry_md_setkey (mac, key, key_len);
79   gcry_md_write (mac, buf, buf_len);
80
81   return (const void *) gcry_md_read (mac, 0);
82 }
83
84 /**
85  * @brief Generate pseudo-random key
86  * @param mac gcrypt HMAC handle
87  * @param xts salt
88  * @param xts_len length of the salt
89  * @param skm source key material
90  * @param skm_len length of skm
91  * @param prk result buffer (allocated by caller; at least gcry_md_dlen() bytes)
92  * @return GNUNET_YES on success
93  */
94 static int
95 getPRK (gcry_md_hd_t mac, 
96         const void *xts, size_t xts_len,
97         const void *skm, size_t skm_len,
98         void *prk)
99 {
100   const void *ret;
101
102   ret = doHMAC (mac, xts, xts_len, skm, skm_len);
103   if (ret == NULL)
104     return GNUNET_SYSERR;
105   memcpy (prk,
106           ret,
107           gcry_md_get_algo_dlen (gcry_md_get_algo (mac)));
108
109   return GNUNET_YES;
110 }
111
112
113 #if DEBUG_HKDF
114 static void 
115 dump(const char *src, 
116      const void *p, 
117      unsigned int l)
118 {
119   unsigned int i;
120
121   printf("\n%s: ", src);
122   for (i = 0; i < l; i++)
123     {
124       printf("%2x", (int) ((const unsigned char *) p)[i]);
125     }
126   printf("\n");
127 }
128 #endif
129
130
131 /**
132  * @brief Derive key
133  * @param result buffer for the derived key, allocated by caller
134  * @param out_len desired length of the derived key
135  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
136  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
137  * @param xts salt
138  * @param xts_len length of xts
139  * @param skm source key material
140  * @param skm_len length of skm
141  * @param argp va_list of void * & size_t pairs for context chunks
142  * @return GNUNET_YES on success
143  */
144 int
145 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len,
146                       int xtr_algo, int prf_algo, 
147                       const void *xts, size_t xts_len,
148                       const void *skm, size_t skm_len,
149                       va_list argp)
150 {
151   const void *hc;
152   unsigned long i, t, d;
153   unsigned int k = gcry_md_get_algo_dlen (prf_algo);
154   unsigned int xtr_len = gcry_md_get_algo_dlen (xtr_algo);
155   char prk[xtr_len];
156   int ret;
157   gcry_md_hd_t xtr, prf;
158   size_t ctx_len;
159   va_list args;
160
161   if (k == 0)
162     return GNUNET_SYSERR;
163
164   // FIXME: what is the check for?
165   if (out_len > (2 ^ 32 * k))
166     return GNUNET_SYSERR;
167
168   if (gcry_md_open(&xtr, xtr_algo, GCRY_MD_FLAG_HMAC) != GPG_ERR_NO_ERROR)
169     return GNUNET_SYSERR;
170
171   if (gcry_md_open(&prf, prf_algo, GCRY_MD_FLAG_HMAC) != GPG_ERR_NO_ERROR)
172   {
173     gcry_md_close (xtr);
174     return GNUNET_SYSERR;
175   }
176
177   va_copy (args, argp);
178
179   ctx_len = 0;
180   while (NULL != va_arg (args, void *))
181     ctx_len += va_arg (args, size_t);
182   va_end(args);
183
184   memset (result, 0, out_len);
185   if (getPRK (xtr, xts, xts_len, skm, skm_len, prk)
186       != GNUNET_YES)
187     goto hkdf_error;
188 #if DEBUG_HKDF
189   dump("PRK", prk, xtr_len);
190 #endif
191
192   t = out_len / k;
193   d = out_len % k;
194
195   /* K(1) */
196   {
197   size_t plain_len = k + ctx_len + 1;
198   char plain[plain_len];
199       const void *ctx;
200       char *dst;
201
202       dst = plain + k;
203       va_copy (args, argp);
204       while ((ctx = va_arg (args, void *)))
205         {
206           size_t len;
207
208           len = va_arg (args, size_t);
209           memcpy (dst, ctx, len);
210           dst += len;
211         }
212       va_end (args);
213
214   if (t > 0)
215     {
216       memset (plain + k + ctx_len, 1, 1);
217 #if DEBUG_HKDF
218       dump("K(1)", plain, plain_len);
219 #endif
220       hc = doHMAC (prf, 
221                    prk,
222                    xtr_len, &plain[k], ctx_len + 1);
223       if (hc == NULL)
224         goto hkdf_error;
225       memcpy (result, hc, k);
226       result += k;
227     }
228
229   /* K(i+1) */
230   for (i = 1; i < t; i++)
231     {
232       memcpy (plain, result - k, k);
233       memset (plain + k + ctx_len, i + 1, 1);
234       gcry_md_reset (prf);
235 #if DEBUG_HKDF
236       dump("K(i+1)", plain, plain_len);
237 #endif
238       hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
239       if (hc == NULL)
240         goto hkdf_error;
241       memcpy (result, hc, k);
242       result += k;
243     }
244
245   /* K(t):d */
246   if (d > 0)
247     {
248       if (t > 0)
249         memcpy (plain, result - k, k);
250       memset (plain + k + ctx_len, i + 1, 1);
251       gcry_md_reset (prf);
252 #if DEBUG_HKDF
253       dump("K(t):d", plain, plain_len);
254 #endif
255       if (t > 0)
256         hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
257       else
258         hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k);
259       if (hc == NULL)
260         goto hkdf_error;
261       memcpy (result, hc, d);
262     }
263 #if DEBUG_HKDF
264   dump("result", result - k, out_len);
265 #endif
266
267   ret = GNUNET_YES;
268   goto hkdf_ok;
269   }
270 hkdf_error:
271   ret = GNUNET_SYSERR;
272 hkdf_ok:
273   gcry_md_close (prf);
274   gcry_md_close (xtr);
275
276   return ret;
277 }
278
279
280 /**
281  * @brief Derive key
282  * @param result buffer for the derived key, allocated by caller
283  * @param out_len desired length of the derived key
284  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
285  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
286  * @param xts salt
287  * @param xts_len length of xts
288  * @param skm source key material
289  * @param skm_len length of skm
290  * @param ctx context info
291  * @param ctx_len length of ctx
292  * @return GNUNET_YES on success
293  */
294 int
295 GNUNET_CRYPTO_hkdf (void *result, size_t out_len,
296                     int xtr_algo, int prf_algo, 
297                     const void *xts, size_t xts_len,
298                     const void *skm, size_t skm_len, 
299                     ...)
300 {
301   va_list argp;
302   int ret;
303
304   va_start(argp, skm_len);
305   ret = GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts,
306       xts_len, skm, skm_len, argp);
307   va_end(argp);
308
309   return ret;
310 }
311
312 /* end of crypto_hkdf.c */