2 This file is part of GNUnet.
3 Copyright (C) 2010 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
17 * @file src/util/crypto_kdf.c
18 * @brief Key derivation
20 * @author Jeffrey Burdges <burdges@gnunet.org>
26 #include "gnunet_crypto_lib.h"
28 #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-kdf", __VA_ARGS__)
32 * @param result buffer for the derived key, allocated by caller
33 * @param out_len desired length of the derived key
35 * @param xts_len length of @a xts
36 * @param skm source key material
37 * @param skm_len length of @a skm
38 * @param argp va_list of void * & size_t pairs for context chunks
39 * @return #GNUNET_YES on success
42 GNUNET_CRYPTO_kdf_v (void *result,
51 * "Finally, we point out to a particularly advantageous instantiation using
52 * HMAC-SHA512 as XTR and HMAC-SHA256 in PRF* (in which case the output from SHA-512 is
53 * truncated to 256 bits). This makes sense in two ways: First, the extraction part is where we need a
54 * stronger hash function due to the unconventional demand from the hash function in the extraction
55 * setting. Second, as shown in Section 6, using HMAC with a truncated output as an extractor
56 * allows to prove the security of HKDF under considerably weaker assumptions on the underlying
59 * http://eprint.iacr.org/2010/264
62 return GNUNET_CRYPTO_hkdf_v (result,
76 * @param result buffer for the derived key, allocated by caller
77 * @param out_len desired length of the derived key
79 * @param xts_len length of @a xts
80 * @param skm source key material
81 * @param skm_len length of @a skm
82 * @param ... void * & size_t pairs for context chunks
83 * @return #GNUNET_YES on success
86 GNUNET_CRYPTO_kdf (void *result,
96 va_start (argp, skm_len);
97 ret = GNUNET_CRYPTO_kdf_v (result,
111 * Deterministically generate a pseudo-random number uniformly from the
112 * integers modulo a libgcrypt mpi.
114 * @param[out] r MPI value set to the FDH
115 * @param n MPI to work modulo
117 * @param xts_len length of @a xts
118 * @param skm source key material
119 * @param skm_len length of @a skm
120 * @param ctx context string
123 GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
125 const void *xts, size_t xts_len,
126 const void *skm, size_t skm_len,
134 nbits = gcry_mpi_get_nbits (n);
135 /* GNUNET_assert (nbits > 512); */
140 /* Ain't clear if n is always divisible by 8 */
141 uint8_t buf[ (nbits-1)/8 + 1 ];
143 rc = GNUNET_CRYPTO_kdf (buf,
150 GNUNET_assert (GNUNET_YES == rc);
152 rc = gcry_mpi_scan (r,
154 (const unsigned char *) buf,
157 GNUNET_assert (0 == rc); /* Allocation erro? */
159 gcry_mpi_clear_highbit (*r, nbits);
160 GNUNET_assert( 0 == gcry_mpi_test_bit (*r, nbits) );
162 /* We reject this FDH if either *r > n and retry with another ctr */
163 if (0 > gcry_mpi_cmp(*r, n))
165 gcry_mpi_release (*r);
169 /* end of crypto_kdf.c */