Fix perf_crypto_rsa.c after various changes
[oweals/gnunet.git] / src / util / crypto_kdf.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010 GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20
21 /**
22  * @file src/util/crypto_kdf.c
23  * @brief Key derivation
24  * @author Nils Durner
25  * @author Jeffrey Burdges <burdges@gnunet.org>
26  */
27
28 #include <gcrypt.h>
29
30 #include "platform.h"
31 #include "gnunet_crypto_lib.h"
32
33 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
34
35 /**
36  * @brief Derive key
37  * @param result buffer for the derived key, allocated by caller
38  * @param out_len desired length of the derived key
39  * @param xts salt
40  * @param xts_len length of @a xts
41  * @param skm source key material
42  * @param skm_len length of @a skm
43  * @param argp va_list of void * & size_t pairs for context chunks
44  * @return #GNUNET_YES on success
45  */
46 int
47 GNUNET_CRYPTO_kdf_v (void *result, size_t out_len,
48                      const void *xts, size_t xts_len,
49                      const void *skm, size_t skm_len,
50                      va_list argp)
51 {
52   /*
53    * "Finally, we point out to a particularly advantageous instantiation using
54    * HMAC-SHA512 as XTR and HMAC-SHA256 in PRF* (in which case the output from SHA-512 is
55    * truncated to 256 bits). This makes sense in two ways: First, the extraction part is where we need a
56    * stronger hash function due to the unconventional demand from the hash function in the extraction
57    * setting. Second, as shown in Section 6, using HMAC with a truncated output as an extractor
58    * allows to prove the security of HKDF under considerably weaker assumptions on the underlying
59    * hash function."
60    *
61    * http://eprint.iacr.org/2010/264
62    */
63
64   return GNUNET_CRYPTO_hkdf_v (result, out_len, GCRY_MD_SHA512, GCRY_MD_SHA256,
65                                xts, xts_len, skm, skm_len, argp);
66 }
67
68
69 /**
70  * @brief Derive key
71  * @param result buffer for the derived key, allocated by caller
72  * @param out_len desired length of the derived key
73  * @param xts salt
74  * @param xts_len length of @a xts
75  * @param skm source key material
76  * @param skm_len length of @a skm
77  * @param ... void * & size_t pairs for context chunks
78  * @return #GNUNET_YES on success
79  */
80 int
81 GNUNET_CRYPTO_kdf (void *result, size_t out_len,
82                    const void *xts, size_t xts_len,
83                    const void *skm, size_t skm_len, ...)
84 {
85   va_list argp;
86   int ret;
87
88   va_start (argp, skm_len);
89   ret = GNUNET_CRYPTO_kdf_v (result, out_len, xts, xts_len, skm, skm_len, argp);
90   va_end (argp);
91
92   return ret;
93 }
94
95
96 /**
97  * Deterministically generate a pseudo-random number uniformly from the
98  * integers modulo a libgcrypt mpi.
99  *
100  * @param[out] r MPI value set to the FDH
101  * @param n MPI to work modulo
102  * @param xts salt
103  * @param xts_len length of @a xts
104  * @param skm source key material
105  * @param skm_len length of @a skm
106  * @param ctx context string
107  */
108 void
109 GNUNET_CRYPTO_kdf_mod_mpi (gcry_mpi_t *r,
110                            gcry_mpi_t n,
111                            const void *xts,  size_t xts_len,
112                            const void *skm,  size_t skm_len,
113                            const char *ctx)
114 {
115   gcry_error_t rc;
116   unsigned int nbits;
117   size_t rsize;
118   unsigned int ctr;
119
120   nbits = gcry_mpi_get_nbits (n);
121   /* GNUNET_assert (nbits > 512); */
122
123   ctr = 0;
124   while (1)
125   {
126     /* Ain't clear if n is always divisible by 8 */
127     uint8_t buf[ (nbits-1)/8 + 1 ];
128
129     rc = GNUNET_CRYPTO_kdf (buf,
130                             sizeof (buf),
131                             xts, xts_len,
132                             skm, skm_len,
133                             ctx, strlen(ctx),
134                             &ctr, sizeof(ctr),
135                             NULL, 0);
136     GNUNET_assert (GNUNET_YES == rc);
137
138     rc = gcry_mpi_scan (r,
139                         GCRYMPI_FMT_USG,
140                         (const unsigned char *) buf,
141                         sizeof (buf),
142                         &rsize);
143     GNUNET_assert (0 == rc);  /* Allocation erro? */
144
145     gcry_mpi_clear_highbit (*r, nbits);
146     GNUNET_assert( 0 == gcry_mpi_test_bit (*r, nbits) );
147     ++ctr;
148     /* We reject this FDH if either *r > n and retry with another ctr */
149     if (0 > gcry_mpi_cmp(*r, n))
150       break;
151     gcry_mpi_release (*r);
152   }
153 }