summaryrefslogtreecommitdiff
path: root/src/util/crypto_hkdf.c
blob: 7270b87b653e85defd9fcfb911799a76fe3db7fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
    Copyright (c) 2010 Nils Durner

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
 */

/**
 * @file src/util/crypto_hkdf.c
 * @brief Hash-based KDF as defined in RFC 5869
 * @see http://www.rfc-editor.org/rfc/rfc5869.txt
 * @todo remove GNUNET references
 * @author Nils Durner
 *
 * The following list of people have reviewed this code and considered
 * it correct on the date given (if you reviewed it, please
 * have your name added to the list):
 *
 * - Christian Grothoff (08.10.2010)
 * - Nathan Evans (08.10.2010)
 * - Matthias Wachs (08.10.2010)
 */

#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-hkdf", __VA_ARGS__)

/**
 * Set this to 0 if you compile this code outside of GNUnet.
 */
#define GNUNET_BUILD 1

/**
 * Enable debugging.
 */
#define DEBUG_HKDF 0


#if GNUNET_BUILD
#include "platform.h"
#include "gnunet_crypto_lib.h"
#include "benchmark.h"
#else
#define GNUNET_NO 0
#define GNUNET_YES 1
#define GNUNET_SYSERR -1
#include <stdlib.h>
#endif

#include <gcrypt.h>


/**
 * @brief Compute the HMAC
 * @todo use chunked buffers
 * @param mac gcrypt MAC handle
 * @param key HMAC key
 * @param key_len length of key
 * @param buf message to be processed
 * @param buf_len length of buf
 * @return HMAC, freed by caller via gcry_md_close/_reset
 */
static const void *
doHMAC (gcry_md_hd_t mac, const void *key, size_t key_len, const void *buf,
        size_t buf_len)
{
  if (GPG_ERR_NO_ERROR != gcry_md_setkey (mac, key, key_len))
  {
    GNUNET_break (0);
    return NULL;
  }
  gcry_md_write (mac, buf, buf_len);

  return (const void *) gcry_md_read (mac, 0);
}


/**
 * @brief Generate pseudo-random key
 * @param mac gcrypt HMAC handle
 * @param xts salt
 * @param xts_len length of the @a xts salt
 * @param skm source key material
 * @param skm_len length of @a skm
 * @param prk result buffer (allocated by caller; at least gcry_md_dlen() bytes)
 * @return #GNUNET_YES on success
 */
static int
getPRK (gcry_md_hd_t mac, const void *xts, size_t xts_len, const void *skm,
        size_t skm_len, void *prk)
{
  const void *ret;

  ret = doHMAC (mac, xts, xts_len, skm, skm_len);
  if (ret == NULL)
    return GNUNET_SYSERR;
  GNUNET_memcpy (prk, ret, gcry_md_get_algo_dlen (gcry_md_get_algo (mac)));

  return GNUNET_YES;
}


#if DEBUG_HKDF
static void
dump (const char *src, const void *p, unsigned int l)
{
  unsigned int i;

  printf ("\n%s: ", src);
  for (i = 0; i < l; i++)
  {
    printf ("%2x", (int) ((const unsigned char *) p)[i]);
  }
  printf ("\n");
}


#endif


/**
 * @brief Derive key
 * @param result buffer for the derived key, allocated by caller
 * @param out_len desired length of the derived key
 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
 * @param xts salt
 * @param xts_len length of @a xts
 * @param skm source key material
 * @param skm_len length of @a skm
 * @param argp va_list of void * & size_t pairs for context chunks
 * @return #GNUNET_YES on success
 */
int
GNUNET_CRYPTO_hkdf_v (void *result, size_t out_len, int xtr_algo, int prf_algo,
                      const void *xts, size_t xts_len, const void *skm,
                      size_t skm_len, va_list argp)
{
  gcry_md_hd_t xtr;
  gcry_md_hd_t prf;
  const void *hc;
  unsigned long i;
  unsigned long t;
  unsigned long d;
  unsigned int k = gcry_md_get_algo_dlen (prf_algo);
  unsigned int xtr_len = gcry_md_get_algo_dlen (xtr_algo);
  char prk[xtr_len];
  int ret;
  size_t ctx_len;
  va_list args;

  BENCHMARK_START (hkdf);

  if (0 == k)
    return GNUNET_SYSERR;
  if (GPG_ERR_NO_ERROR !=
      gcry_md_open (&xtr, xtr_algo, GCRY_MD_FLAG_HMAC))
    return GNUNET_SYSERR;
  if (GPG_ERR_NO_ERROR !=
      gcry_md_open (&prf, prf_algo, GCRY_MD_FLAG_HMAC))
  {
    gcry_md_close (xtr);
    return GNUNET_SYSERR;
  }
  va_copy (args, argp);

  ctx_len = 0;
  while (NULL != va_arg (args, void *))
  {
    size_t nxt = va_arg (args, size_t);
    if (nxt + ctx_len < nxt)
    {
      /* integer overflow */
      GNUNET_break (0);
      va_end (args);
      goto hkdf_error;
    }
    ctx_len += nxt;
  }

  va_end (args);

  if ( (k + ctx_len < ctx_len) ||
       (k + ctx_len + 1 < ctx_len) )
  {
    /* integer overflow */
    GNUNET_break (0);
    goto hkdf_error;
  }

  memset (result, 0, out_len);
  if (getPRK (xtr, xts, xts_len, skm, skm_len, prk) != GNUNET_YES)
    goto hkdf_error;
#if DEBUG_HKDF
  dump ("PRK", prk, xtr_len);
#endif

  t = out_len / k;
  d = out_len % k;

  /* K(1) */
  {
    size_t plain_len = k + ctx_len + 1;
    char *plain;
    const void *ctx;
    char *dst;

    plain = GNUNET_malloc (plain_len);
    dst = plain + k;
    va_copy (args, argp);
    while ((ctx = va_arg (args, void *)))
    {
      size_t len;

      len = va_arg (args, size_t);
      GNUNET_memcpy (dst, ctx, len);
      dst += len;
    }
    va_end (args);

    if (t > 0)
    {
      plain[k + ctx_len] = (char) 1;
#if DEBUG_HKDF
      dump ("K(1)", plain, plain_len);
#endif
      hc = doHMAC (prf, prk, xtr_len, &plain[k], ctx_len + 1);
      if (hc == NULL)
      {
        GNUNET_free (plain);
        goto hkdf_error;
      }
      GNUNET_memcpy (result, hc, k);
      result += k;
    }

    /* K(i+1) */
    for (i = 1; i < t; i++)
    {
      GNUNET_memcpy (plain, result - k, k);
      plain[k + ctx_len] = (char) (i + 1);
      gcry_md_reset (prf);
#if DEBUG_HKDF
      dump ("K(i+1)", plain, plain_len);
#endif
      hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
      if (hc == NULL)
      {
        GNUNET_free (plain);
        goto hkdf_error;
      }
      GNUNET_memcpy (result, hc, k);
      result += k;
    }

    /* K(t):d */
    if (d > 0)
    {
      if (t > 0)
      {
        GNUNET_memcpy (plain, result - k, k);
        i++;
      }
      plain[k + ctx_len] = (char) i;
      gcry_md_reset (prf);
#if DEBUG_HKDF
      dump ("K(t):d", plain, plain_len);
#endif
      if (t > 0)
        hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
      else
        hc = doHMAC (prf, prk, xtr_len, plain + k, plain_len - k);
      if (hc == NULL)
      {
        GNUNET_free (plain);
        goto hkdf_error;
      }
      GNUNET_memcpy (result, hc, d);
    }
#if DEBUG_HKDF
    dump ("result", result - k, out_len);
#endif

    ret = GNUNET_YES;
    GNUNET_free (plain);
    goto hkdf_ok;
  }
hkdf_error:
  ret = GNUNET_SYSERR;
hkdf_ok:
  gcry_md_close (xtr);
  gcry_md_close (prf);
  BENCHMARK_END (hkdf);
  return ret;
}


/**
 * @brief Derive key
 * @param result buffer for the derived key, allocated by caller
 * @param out_len desired length of the derived key
 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
 * @param xts salt
 * @param xts_len length of @a xts
 * @param skm source key material
 * @param skm_len length of @a skm
 * @return #GNUNET_YES on success
 */
int
GNUNET_CRYPTO_hkdf (void *result, size_t out_len, int xtr_algo, int prf_algo,
                    const void *xts, size_t xts_len, const void *skm,
                    size_t skm_len, ...)
{
  va_list argp;
  int ret;

  va_start (argp, skm_len);
  ret =
    GNUNET_CRYPTO_hkdf_v (result, out_len, xtr_algo, prf_algo, xts, xts_len,
                          skm, skm_len, argp);
  va_end (argp);

  return ret;
}


/* end of crypto_hkdf.c */