struct
[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
31 #include <gcrypt.h>
32
33 #include "platform.h"
34 #include "gnunet_crypto_lib.h"
35
36 #define DEBUG_HKDF GNUNET_NO
37
38 /**
39  * @brief Compute the HMAC
40  * @todo use chunked buffers
41  * @param mac gcrypt MAC handle
42  * @param key HMAC key
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
47  */
48 static const void *
49 doHMAC (gcry_md_hd_t mac, 
50         const void *key, size_t key_len,
51         const void *buf, size_t buf_len)
52 {
53   gcry_md_setkey (mac, key, key_len);
54   gcry_md_write (mac, buf, buf_len);
55
56   return (const void *) gcry_md_read (mac, 0);
57 }
58
59 /**
60  * @brief Generate pseudo-random key
61  * @param mac gcrypt HMAC handle
62  * @param xts salt
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
68  */
69 static int
70 getPRK (gcry_md_hd_t mac, 
71         const void *xts, size_t xts_len,
72         const void *skm, size_t skm_len,
73         void *prk)
74 {
75   const void *ret;
76
77   ret = doHMAC (mac, xts, xts_len, skm, skm_len);
78   if (ret == NULL)
79     return GNUNET_SYSERR;
80   memcpy (prk,
81           ret,
82           gcry_md_get_algo_dlen (gcry_md_get_algo (mac)));
83
84   return GNUNET_YES;
85 }
86
87
88 #if DEBUG_HKDF
89 static void 
90 dump(const char *src, 
91      const void *p, 
92      unsigned int l)
93 {
94   unsigned int i;
95
96   printf("\n%s: ", src);
97   for (i = 0; i < l; i++)
98     {
99       printf("%2x", (int) ((const unsigned char *) p)[i]);
100     }
101   printf("\n");
102 }
103 #endif
104
105
106 /**
107  * @brief Derive key
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_...
112  * @param xts salt
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
118  */
119 int
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,
124                       va_list argp)
125 {
126   const void *hc;
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);
130   char prk[xtr_len];
131   int ret;
132   gcry_md_hd_t xtr, prf;
133   size_t ctx_len;
134   va_list args;
135
136   if (k == 0)
137     return GNUNET_SYSERR;
138
139   // FIXME: what is the check for?
140   if (out_len > (2 ^ 32 * k))
141     return GNUNET_SYSERR;
142
143   if (gcry_md_open(&xtr, xtr_algo, GCRY_MD_FLAG_HMAC) != GPG_ERR_NO_ERROR)
144     return GNUNET_SYSERR;
145
146   if (gcry_md_open(&prf, prf_algo, GCRY_MD_FLAG_HMAC) != GPG_ERR_NO_ERROR)
147   {
148     gcry_md_close (xtr);
149     return GNUNET_SYSERR;
150   }
151
152   va_copy (args, argp);
153
154   ctx_len = 0;
155   while (NULL != va_arg (args, void *))
156     ctx_len += va_arg (args, size_t);
157   va_end(args);
158
159   memset (result, 0, out_len);
160   if (getPRK (xtr, xts, xts_len, skm, skm_len, prk)
161       != GNUNET_YES)
162     goto hkdf_error;
163 #if DEBUG_HKDF
164   dump("PRK", prk, xtr_len);
165 #endif
166
167   t = out_len / k;
168   d = out_len % k;
169
170   /* K(1) */
171   {
172   size_t plain_len = k + ctx_len + 1;
173   char plain[plain_len];
174       const void *ctx;
175       char *dst;
176
177       dst = plain + k;
178       va_copy (args, argp);
179       while ((ctx = va_arg (args, void *)))
180         {
181           size_t len;
182
183           len = va_arg (args, size_t);
184           memcpy (dst, ctx, len);
185           dst += len;
186         }
187       va_end (args);
188
189   if (t > 0)
190     {
191       memset (plain + k + ctx_len, 1, 1);
192 #if DEBUG_HKDF
193       dump("K(1)", plain, plain_len);
194 #endif
195       hc = doHMAC (prf, 
196                    prk,
197                    xtr_len, &plain[k], ctx_len + 1);
198       if (hc == NULL)
199         goto hkdf_error;
200       memcpy (result, hc, k);
201       result += k;
202     }
203
204   /* K(i+1) */
205   for (i = 1; i < t; i++)
206     {
207       memcpy (plain, result - k, k);
208       memset (plain + k + ctx_len, i + 1, 1);
209       gcry_md_reset (prf);
210 #if DEBUG_HKDF
211       dump("K(i+1)", plain, plain_len);
212 #endif
213       hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
214       if (hc == NULL)
215         goto hkdf_error;
216       memcpy (result, hc, k);
217       result += k;
218     }
219
220   /* K(t):d */
221   if (d > 0)
222     {
223       if (t > 0)
224         memcpy (plain, result - k, k);
225       memset (plain + k + ctx_len, i + 1, 1);
226       gcry_md_reset (prf);
227 #if DEBUG_HKDF
228       dump("K(t):d", plain, plain_len);
229 #endif
230       if (t > 0)
231         hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
232       else
233         hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k);
234       if (hc == NULL)
235         goto hkdf_error;
236       memcpy (result, hc, d);
237     }
238 #if DEBUG_HKDF
239   dump("result", result - k, out_len);
240 #endif
241
242   ret = GNUNET_YES;
243   goto hkdf_ok;
244   }
245 hkdf_error:
246   ret = GNUNET_SYSERR;
247 hkdf_ok:
248   gcry_md_close (prf);
249   gcry_md_close (xtr);
250
251   return ret;
252 }
253
254
255 /**
256  * @brief Derive key
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_...
261  * @param xts salt
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
268  */
269 int
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, 
274                     ...)
275 {
276   va_list argp;
277   int ret;
278
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);
282   va_end(argp);
283
284   return ret;
285 }
286
287 /* end of crypto_hkdf.c */