c436e9962b50ae7e78e6e6ba6d8c34c7069edeac
[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  * @author Nils Durner
28  */
29
30 #include <gcrypt.h>
31
32 #include "platform.h"
33 #include "gnunet_crypto_lib.h"
34
35 #define DEBUG_HKDF GNUNET_NO
36
37 /**
38  * @brief Compute the HMAC
39  * @param mac gcrypt MAC handle
40  * @param key HMAC key
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
45  */
46 static const void *
47 doHMAC (gcry_md_hd_t mac, 
48         const void *key, size_t key_len,
49         const void *buf, size_t buf_len)
50 {
51   gcry_md_setkey (mac, key, key_len);
52   gcry_md_write (mac, buf, buf_len);
53
54   return (const void *) gcry_md_read (mac, 0);
55 }
56
57 /**
58  * @brief Generate pseudo-random key
59  * @param mac gcrypt HMAC handle
60  * @param xts salt
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
66  */
67 static int
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, 
71         void *prk)
72 {
73   const void *ret;
74
75   ret = doHMAC (mac, xts, xts_len, skm, skm_len);
76   if (ret == NULL)
77     return GNUNET_SYSERR;
78   memcpy (prk,
79           ret,
80           gcry_md_get_algo_dlen (gcry_md_get_algo (mac)));
81
82   return GNUNET_YES;
83 }
84
85
86 #if DEBUG_HKDF
87 static void 
88 dump(const char *src, 
89      const void *p, 
90      unsigned int l)
91 {
92   unsigned int i;
93
94   printf("\n%s: ", src);
95   for (i = 0; i < l; i++)
96     {
97       printf("%2x", (int) ((const unsigned char *) p)[i]);
98     }
99   printf("\n");
100 }
101 #endif
102
103
104 /**
105  * @brief Derive key
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_...
110  * @param xts salt
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
116  */
117 int
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,
122                       va_list argp)
123 {
124   void *prk, *plain;
125   const void *hc;
126   unsigned long long plain_len;
127   unsigned long i, t, d;
128   unsigned int k, xtr_len;
129   int ret;
130   gcry_md_hd_t xtr, prf;
131   size_t ctx_len;
132   va_list args;
133
134   prk = plain = NULL;
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);
139
140   if (out_len > (2 ^ 32 * k) || !xtr_algo || !prf_algo)
141     return GNUNET_SYSERR;
142
143   va_copy (args, argp);
144   for (ctx_len = 0; va_arg (args, void *);)
145     ctx_len += va_arg (args, size_t);
146   va_end(args);
147
148   prk = GNUNET_malloc (xtr_len);
149
150   memset (result, 0, out_len);
151   gcry_md_reset (xtr);
152   if (getPRK (xtr, xts, xts_len, skm, skm_len, prk)
153       != GNUNET_YES)
154     goto hkdf_error;
155 #if DEBUG_HKDF
156   dump("PRK", prk, xtr_len);
157 #endif
158
159   t = out_len / k;
160   d = out_len % k;
161
162   /* K(1) */
163   plain_len = k + ctx_len + 1;
164   plain = GNUNET_malloc (plain_len);
165   if (t > 0)
166     {
167       void *ctx, *dst;
168
169       dst = plain;
170       va_copy (args, argp);
171       while ((ctx = va_arg (args, void *)))
172         {
173           size_t len;
174
175           len = va_arg (args, size_t);
176           memcpy (dst, ctx, len);
177           dst += len;
178         }
179       va_end (args);
180
181       memset (dst, 1, 1);
182       gcry_md_reset (prf);
183 #if DEBUG_HKDF
184       dump("K(1)", plain, plain_len);
185 #endif
186       hc = doHMAC (prf, 
187                    prk,
188                    xtr_len, plain, ctx_len + 1);
189       if (hc == NULL)
190         goto hkdf_error;
191       memcpy (result, hc, k);
192       result += k;
193     }
194
195   if (t > 1 || d > 0)
196     {
197       void *ctx, *dst;
198
199       dst = plain + k;
200       va_copy (args, argp);
201       while ((ctx = va_arg (args, void *)))
202         {
203           size_t len;
204
205           len = va_arg (args, size_t);
206           memcpy (dst, ctx, len);
207           dst += len;
208         }
209       va_end (args);
210     }
211
212   /* K(i+1) */
213   for (i = 1; i < t; i++)
214     {
215       memcpy (plain, result - k, k);
216       memset (plain + k + ctx_len, i + 1, 1);
217       gcry_md_reset (prf);
218 #if DEBUG_HKDF
219       dump("K(i+1)", plain, plain_len);
220 #endif
221       hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
222       if (hc == NULL)
223         goto hkdf_error;
224       memcpy (result, hc, k);
225       result += k;
226     }
227
228   /* K(t):d */
229   if (d > 0)
230     {
231       if (t > 0)
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(t):d", 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, d);
242     }
243 #if DEBUG_HKDF
244   dump("result", result - k, out_len);
245 #endif
246
247   ret = GNUNET_YES;
248   goto hkdf_ok;
249
250 hkdf_error:
251   ret = GNUNET_SYSERR;
252 hkdf_ok:
253   GNUNET_free (prk);
254   GNUNET_free_non_null (plain);
255   gcry_md_close (prf);
256   gcry_md_close (xtr);
257
258   return ret;
259 }
260
261
262 /**
263  * @brief Derive key
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_...
268  * @param xts salt
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
275  */
276 int
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, 
281                     ...)
282 {
283   va_list argp;
284   int ret;
285
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);
289   va_end(argp);
290
291   return ret;
292 }
293
294 /* end of crypto_hkdf.c */