working again
[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 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
40
41 /**
42  * Set this to 0 if you compile this code outside of GNUnet.
43  */
44 #define GNUNET_BUILD 1
45
46 /**
47  * Enable debugging.
48  */
49 #define DEBUG_HKDF 0
50
51
52
53 #if GNUNET_BUILD
54 #include "platform.h"
55 #include "gnunet_crypto_lib.h"
56 #else
57 #define GNUNET_NO 0
58 #define GNUNET_YES 1
59 #define GNUNET_SYSERR -1
60 #include <stdlib.h>
61 #endif
62
63 #include <gcrypt.h>
64
65
66 /**
67  * @brief Compute the HMAC
68  * @todo use chunked buffers
69  * @param mac gcrypt MAC handle
70  * @param key HMAC key
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
75  */
76 static const void *
77 doHMAC (gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf,
78         size_t buf_len)
79 {
80   gcry_md_setkey (mac, key, key_len);
81   gcry_md_write (mac, buf, buf_len);
82
83   return (const void *) gcry_md_read (mac, 0);
84 }
85
86 /**
87  * @brief Generate pseudo-random key
88  * @param mac gcrypt HMAC handle
89  * @param xts salt
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
95  */
96 static int
97 getPRK (gcry_md_hd_t mac, const void *xts, size_t xts_len, const void *skm,
98         size_t skm_len, 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, ret, gcry_md_get_algo_dlen (gcry_md_get_algo (mac)));
106
107   return GNUNET_YES;
108 }
109
110
111 #if DEBUG_HKDF
112 static void
113 dump (const char *src, const void *p, unsigned int l)
114 {
115   unsigned int i;
116
117   printf ("\n%s: ", src);
118   for (i = 0; i < l; i++)
119   {
120     printf ("%2x", (int) ((const unsigned char *) p)[i]);
121   }
122   printf ("\n");
123 }
124 #endif
125
126
127 /**
128  * @brief Derive key
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_...
133  * @param xts salt
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
139  */
140 int
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)
144 {
145   const void *hc;
146   unsigned long i, t, d;
147   unsigned int k = gcry_md_get_algo_dlen (prf_algo);
148   unsigned int xtr_len = gcry_md_get_algo_dlen (xtr_algo);
149   char prk[xtr_len];
150   int ret;
151   gcry_md_hd_t xtr, prf;
152   size_t ctx_len;
153   va_list args;
154
155   if (k == 0)
156     return GNUNET_SYSERR;
157
158   if (gcry_md_open (&xtr, xtr_algo, GCRY_MD_FLAG_HMAC) != GPG_ERR_NO_ERROR)
159     return GNUNET_SYSERR;
160
161   if (gcry_md_open (&prf, prf_algo, GCRY_MD_FLAG_HMAC) != GPG_ERR_NO_ERROR)
162   {
163     gcry_md_close (xtr);
164     return GNUNET_SYSERR;
165   }
166
167   va_copy (args, argp);
168
169   ctx_len = 0;
170   while (NULL != va_arg (args, void *))
171          ctx_len += va_arg (args, size_t);
172
173   va_end (args);
174
175   memset (result, 0, out_len);
176   if (getPRK (xtr, xts, xts_len, skm, skm_len, prk) != GNUNET_YES)
177     goto hkdf_error;
178 #if DEBUG_HKDF
179   dump ("PRK", prk, xtr_len);
180 #endif
181
182   t = out_len / k;
183   d = out_len % k;
184
185   /* K(1) */
186   {
187     size_t plain_len = k + ctx_len + 1;
188     char plain[plain_len];
189     const void *ctx;
190     char *dst;
191
192     dst = plain + k;
193     va_copy (args, argp);
194     while ((ctx = va_arg (args, void *)))
195     {
196       size_t len;
197
198       len = va_arg (args, size_t);
199       memcpy (dst, ctx, len);
200       dst += len;
201     }
202     va_end (args);
203
204     if (t > 0)
205     {
206       memset (plain + k + ctx_len, 1, 1);
207 #if DEBUG_HKDF
208       dump ("K(1)", plain, plain_len);
209 #endif
210       hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1);
211       if (hc == NULL)
212         goto hkdf_error;
213       memcpy (result, hc, k);
214       result += k;
215     }
216
217     /* K(i+1) */
218     for (i = 1; i < t; i++)
219     {
220       memcpy (plain, result - k, k);
221       memset (plain + k + ctx_len, i + 1, 1);
222       gcry_md_reset (prf);
223 #if DEBUG_HKDF
224       dump ("K(i+1)", plain, plain_len);
225 #endif
226       hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
227       if (hc == NULL)
228         goto hkdf_error;
229       memcpy (result, hc, k);
230       result += k;
231     }
232
233     /* K(t):d */
234     if (d > 0)
235     {
236       if (t > 0)
237       {
238         memcpy (plain, result - k, k);
239         i++;
240       }
241       memset (plain + k + ctx_len, i, 1);
242       gcry_md_reset (prf);
243 #if DEBUG_HKDF
244       dump ("K(t):d", plain, plain_len);
245 #endif
246       if (t > 0)
247         hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
248       else
249         hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k);
250       if (hc == NULL)
251         goto hkdf_error;
252       memcpy (result, hc, d);
253     }
254 #if DEBUG_HKDF
255     dump ("result", result - k, out_len);
256 #endif
257
258     ret = GNUNET_YES;
259     goto hkdf_ok;
260   }
261 hkdf_error:
262   ret = GNUNET_SYSERR;
263 hkdf_ok:
264   gcry_md_close (prf);
265   gcry_md_close (xtr);
266
267   return ret;
268 }
269
270
271 /**
272  * @brief Derive key
273  * @param result buffer for the derived key, allocated by caller
274  * @param out_len desired length of the derived key
275  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
276  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
277  * @param xts salt
278  * @param xts_len length of @a xts
279  * @param skm source key material
280  * @param skm_len length of @a skm
281  * @return #GNUNET_YES on success
282  */
283 int
284 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
285                     const void *xts, size_t xts_len, const void *skm,
286                     size_t skm_len, ...)
287 {
288   va_list argp;
289   int ret;
290
291   va_start (argp, skm_len);
292   ret =
293       GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts, xts_len,
294                             skm, skm_len, argp);
295   va_end (argp);
296
297   return ret;
298 }
299
300 /* end of crypto_hkdf.c */