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 RFC 5869
26 * @see http://www.rfc-editor.org/rfc/rfc5869.txt
33 #include "gnunet_crypto_lib.h"
36 * @brief Compute the HMAC
37 * @param mac gcrypt MAC handle
39 * @param key_len length of key
40 * @param buf message to be processed
41 * @param buf_len length of buf
42 * @return HMAC, freed by caller via gcry_md_close/_reset
45 doHMAC (gcry_md_hd_t mac, const void *key, const size_t key_len,
46 const void *buf, const size_t buf_len)
48 gcry_md_setkey (mac, key, key_len);
49 gcry_md_write (mac, buf, buf_len);
51 return (void *) gcry_md_read (mac, 0);
55 * @brief Generate pseudo-random key
56 * @param mac gcrypt HMAC handle
58 * @param xts_len length of the salt
59 * @param skm source key material
60 * @param skm_len length of skm
61 * @param prk result buffer (allocated by caller; at least gcry_md_dlen() bytes)
62 * @return GNUNET_YES on success
65 getPRK (gcry_md_hd_t mac, const void *xts, const unsigned long long xts_len,
66 const void *skm, const unsigned long long skm_len, void *prk)
70 ret = doHMAC (mac, xts, xts_len, skm, skm_len);
73 memcpy (prk, ret, gcry_md_get_algo_dlen (gcry_md_get_algo (mac)));
80 * @param xtr_algo hash algorithm for the extraction phase, GCRY_MD_...
81 * @param prf_algo hash algorithm for the expansion phase, GCRY_MD_...
83 * @param xts_len length of xts
84 * @param skm source key material
85 * @param skm_len length of skm
86 * @param ctx context info
87 * @param ctx_len length of ctx
88 * @param out_len desired length of the derived key
89 * @param result buffer for the derived key, allocated by caller
90 * @return GNUNET_YES on success
93 GNUNET_CRYPTO_hkdf (int xtr_algo, int prf_algo, const void *xts,
94 const size_t xts_len, const void *skm, const size_t skm_len,
95 const void *ctx, const size_t ctx_len, const unsigned long long out_len,
98 void *prk, *hc, *plain;
99 unsigned long long plain_len;
100 unsigned long i, t, d;
101 unsigned int k, xtr_len;
103 gcry_md_hd_t xtr, prf;
106 xtr_len = gcry_md_get_algo_dlen (xtr_algo);
107 k = gcry_md_get_algo_dlen (prf_algo);
108 gcry_md_open(&xtr, xtr_algo, GCRY_MD_FLAG_HMAC);
109 gcry_md_open(&prf, prf_algo, GCRY_MD_FLAG_HMAC);
111 if (out_len > (2 ^ 32 * k) || !xtr_algo || !prf_algo)
112 return GNUNET_SYSERR;
114 prk = GNUNET_malloc (xtr_len);
116 memset (result, 0, out_len);
118 if (getPRK (xtr, xts, xts_len, skm, skm_len, prk)
126 plain_len = k + ctx_len + 1;
127 plain = GNUNET_malloc (plain_len);
130 memcpy (plain, ctx, ctx_len);
131 memset (plain + ctx_len, 1, 1);
133 hc = doHMAC (prf, prk, xtr_len, plain, ctx_len + 1);
136 memcpy (result, hc, k);
141 memcpy (plain + k, ctx, ctx_len);
144 for (i = 1; i < t; i++)
146 memcpy (plain, result - k, k);
147 memset (plain + k + ctx_len, i + 1, 1);
149 hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
152 memcpy (result, hc, k);
160 memcpy (plain, result - k, k);
161 memset (plain + k + ctx_len, i + 1, 1);
163 hc = doHMAC (prf, prk, xtr_len, plain, plain_len);
166 memcpy (result, hc, d);
176 GNUNET_free_non_null (plain);
184 /* end of crypto_hkdf.c */