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