2 Copyright (c) 2010 Nils Durner
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:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
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
24 * @file src/util/crypto_hkdf.c
25 * @brief Hash-based KDF as defined in draft-krawczyk-hkdf-01
26 * @see http://tools.ietf.org/html/draft-krawczyk-hkdf-01
31 #include "gnunet_crypto_lib.h"
34 * @brief Compute the HMAC
35 * @param mac gcrypt MAC handle
37 * @param key_len length of key
38 * @param buf message to be processed
39 * @param buf_len length of buf
40 * @return HMAC, freed by caller via gcry_md_close/_reset
43 doHMAC (gcry_md_hd_t mac, const void *key, const size_t key_len,
44 const void *buf, const size_t buf_len)
46 gcry_md_setkey (mac, key, key_len);
47 gcry_md_write (mac, buf, buf_len);
49 return (void *) gcry_md_read (mac, 0);
53 * @brief Generate pseudo-random key
54 * @param mac gcrypt HMAC handle
56 * @param xts_len length of the salt
57 * @param skm source key material
58 * @param skm_len length of skm
59 * @param prk result buffer (allocated by caller; at least gcry_md_dlen() bytes)
60 * @return GNUNET_YES on success
63 getPRK (gcry_md_hd_t mac, const void *xts, const unsigned long long xts_len,
64 const void *skm, const unsigned long long skm_len, void *prk)
68 ret = doHMAC (mac, xts, xts_len, skm, skm_len);
71 memcpy (prk, ret, gcry_md_get_algo_dlen (gcry_md_get_algo (mac)));
76 static void dump(void *p, unsigned int l)
81 for (i = 0; i < l; i++)
83 printf("%2x", ((char *) p)[i]);
90 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
91 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
93 * @param xts_len length of xts
94 * @param skm source key material
95 * @param skm_len length of skm
96 * @param ctx context info
97 * @param ctx_len length of ctx
98 * @param out_len desired length of the derived key
99 * @param result buffer for the derived key, allocated by caller
100 * @return GNUNET_YES on success
103 GNUNET_CRYPTO_hkdf (int xtr_algo, int prf_algo, const void *xts,
104 const size_t xts_len, const void *skm, const size_t skm_len,
105 const void *ctx, const size_t ctx_len, const unsigned long long out_len,
108 void *prk, *hc, *plain;
109 unsigned long long plain_len;
110 unsigned long i, t, d;
111 unsigned int k, xtr_len;
113 gcry_md_hd_t xtr, prf;
116 xtr_len = gcry_md_get_algo_dlen (xtr_algo);
117 k = gcry_md_get_algo_dlen (prf_algo);
118 gcry_md_open(&xtr, xtr_algo, GCRY_MD_FLAG_HMAC);
119 gcry_md_open(&prf, prf_algo, GCRY_MD_FLAG_HMAC);
121 if (out_len > (2 ^ 32 * k) || !xtr_algo || !prf_algo)
122 return GNUNET_SYSERR;
124 prk = GNUNET_malloc (xtr_len);
126 memset (result, 0, out_len);
128 if (getPRK (xtr, xts, xts_len, skm, skm_len, prk)
134 plain_len = k + ctx_len + 4;
135 plain = GNUNET_malloc (plain_len);
136 memset (plain, 0, k);
137 memcpy (plain + k, ctx, ctx_len);
141 memset (plain + k + ctx_len, 0, 4);
143 hc = doHMAC (prf, prk, k, plain, plain_len);
146 memcpy (result, hc, k);
151 for (i = 1; i < t; i++)
153 memcpy (plain, result - k, k);
154 memcpy (plain + k + ctx_len, &i, 4);
156 hc = doHMAC (prf, prk, k, plain, plain_len);
159 memcpy (result, hc, k);
168 memcpy (plain, result - k, k);
169 memcpy (plain + k + ctx_len, &i, 4);
171 hc = doHMAC (prf, prk, k, plain, plain_len);
174 memcpy (result, hc, d);
184 GNUNET_free_non_null (plain);
192 /* end of crypto_hkdf.c */