fixing 1753
[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, const void *key, size_t key_len, const void *buf,
75         size_t buf_len)
76 {
77   gcry_md_setkey (mac, key, key_len);
78   gcry_md_write (mac, buf, buf_len);
79
80   return (const void *) gcry_md_read (mac, 0);
81 }
82
83 /**
84  * @brief Generate pseudo-random key
85  * @param mac gcrypt HMAC handle
86  * @param xts salt
87  * @param xts_len length of the salt
88  * @param skm source key material
89  * @param skm_len length of skm
90  * @param prk result buffer (allocated by caller; at least gcry_md_dlen() bytes)
91  * @return GNUNET_YES on success
92  */
93 static int
94 getPRK (gcry_md_hd_t mac, const void *xts, size_t xts_len, const void *skm,
95         size_t skm_len, void *prk)
96 {
97   const void *ret;
98
99   ret = doHMAC (mac, xts, xts_len, skm, skm_len);
100   if (ret == NULL)
101     return GNUNET_SYSERR;
102   memcpy (prk, ret, gcry_md_get_algo_dlen (gcry_md_get_algo (mac)));
103
104   return GNUNET_YES;
105 }
106
107
108 #if DEBUG_HKDF
109 static void
110 dump (const char *src, const void *p, unsigned int l)
111 {
112   unsigned int i;
113
114   printf ("\n%s: ", src);
115   for (i = 0; i < l; i++)
116   {
117     printf ("%2x", (int) ((const unsigned char *) p)[i]);
118   }
119   printf ("\n");
120 }
121 #endif
122
123
124 /**
125  * @brief Derive key
126  * @param result buffer for the derived key, allocated by caller
127  * @param out_len desired length of the derived key
128  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
129  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
130  * @param xts salt
131  * @param xts_len length of xts
132  * @param skm source key material
133  * @param skm_len length of skm
134  * @param argp va_list of void * & size_t pairs for context chunks
135  * @return GNUNET_YES on success
136  */
137 int
138 GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
139                       const void *xts, size_t xts_len, const void *skm,
140                       size_t skm_len, va_list argp)
141 {
142   const void *hc;
143   unsigned long i, t, d;
144   unsigned int k = gcry_md_get_algo_dlen (prf_algo);
145   unsigned int xtr_len = gcry_md_get_algo_dlen (xtr_algo);
146   char prk[xtr_len];
147   int ret;
148   gcry_md_hd_t xtr, prf;
149   size_t ctx_len;
150   va_list args;
151
152   if (k == 0)
153     return GNUNET_SYSERR;
154
155   if (gcry_md_open (&xtr, xtr_algo, GCRY_MD_FLAG_HMAC) != GPG_ERR_NO_ERROR)
156     return GNUNET_SYSERR;
157
158   if (gcry_md_open (&prf, prf_algo, GCRY_MD_FLAG_HMAC) != GPG_ERR_NO_ERROR)
159   {
160     gcry_md_close (xtr);
161     return GNUNET_SYSERR;
162   }
163
164   va_copy (args, argp);
165
166   ctx_len = 0;
167   while (NULL != va_arg (args, void *))
168          ctx_len += va_arg (args, size_t);
169
170   va_end (args);
171
172   memset (result, 0, out_len);
173   if (getPRK (xtr, xts, xts_len, skm, skm_len, prk) != GNUNET_YES)
174     goto hkdf_error;
175 #if DEBUG_HKDF
176   dump ("PRK", prk, xtr_len);
177 #endif
178
179   t = out_len / k;
180   d = out_len % k;
181
182   /* K(1) */
183   {
184     size_t plain_len = k + ctx_len + 1;
185     char plain[plain_len];
186     const void *ctx;
187     char *dst;
188
189     dst = plain + k;
190     va_copy (args, argp);
191     while ((ctx = va_arg (args, void *)))
192     {
193       size_t len;
194
195       len = va_arg (args, size_t);
196       memcpy (dst, ctx, len);
197       dst += len;
198     }
199     va_end (args);
200
201     if (t > 0)
202     {
203       memset (plain + k + ctx_len, 1, 1);
204 #if DEBUG_HKDF
205       dump ("K(1)", plain, plain_len);
206 #endif
207       hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1);
208       if (hc == NULL)
209         goto hkdf_error;
210       memcpy (result, hc, k);
211       result += k;
212     }
213
214     /* K(i+1) */
215     for (i = 1; i < t; i++)
216     {
217       memcpy (plain, result - k, k);
218       memset (plain + k + ctx_len, i + 1, 1);
219       gcry_md_reset (prf);
220 #if DEBUG_HKDF
221       dump ("K(i+1)", plain, plain_len);
222 #endif
223       hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
224       if (hc == NULL)
225         goto hkdf_error;
226       memcpy (result, hc, k);
227       result += k;
228     }
229
230     /* K(t):d */
231     if (d > 0)
232     {
233       if (t > 0)
234       {
235         memcpy (plain, result - k, k);
236         i++;
237       }
238       memset (plain + k + ctx_len, i, 1);
239       gcry_md_reset (prf);
240 #if DEBUG_HKDF
241       dump ("K(t):d", plain, plain_len);
242 #endif
243       if (t > 0)
244         hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
245       else
246         hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k);
247       if (hc == NULL)
248         goto hkdf_error;
249       memcpy (result, hc, d);
250     }
251 #if DEBUG_HKDF
252     dump ("result", result - k, out_len);
253 #endif
254
255     ret = GNUNET_YES;
256     goto hkdf_ok;
257   }
258 hkdf_error:
259   ret = GNUNET_SYSERR;
260 hkdf_ok:
261   gcry_md_close (prf);
262   gcry_md_close (xtr);
263
264   return ret;
265 }
266
267
268 /**
269  * @brief Derive key
270  * @param result buffer for the derived key, allocated by caller
271  * @param out_len desired length of the derived key
272  * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
273  * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
274  * @param xts salt
275  * @param xts_len length of xts
276  * @param skm source key material
277  * @param skm_len length of skm
278  * @return GNUNET_YES on success
279  */
280 int
281 GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
282                     const void *xts, size_t xts_len, const void *skm,
283                     size_t skm_len, ...)
284 {
285   va_list argp;
286   int ret;
287
288   va_start (argp, skm_len);
289   ret =
290       GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts, xts_len,
291                             skm, skm_len, argp);
292   va_end (argp);
293
294   return ret;
295 }
296
297 /* end of crypto_hkdf.c */