GNUnet  0.11.x
crypto_kdf.c
Go to the documentation of this file.
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 it
6  under the terms of the GNU Affero 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.
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  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
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-crypto-kdf", __VA_ARGS__)
34 
46 int
48  size_t out_len,
49  const void *xts,
50  size_t xts_len,
51  const void *skm,
52  size_t skm_len,
53  va_list argp)
54 {
55  /*
56  * "Finally, we point out to a particularly advantageous instantiation using
57  * HMAC-SHA512 as XTR and HMAC-SHA256 in PRF* (in which case the output from SHA-512 is
58  * truncated to 256 bits). This makes sense in two ways: First, the extraction part is where we need a
59  * stronger hash function due to the unconventional demand from the hash function in the extraction
60  * setting. Second, as shown in Section 6, using HMAC with a truncated output as an extractor
61  * allows to prove the security of HKDF under considerably weaker assumptions on the underlying
62  * hash function."
63  *
64  * http://eprint.iacr.org/2010/264
65  */return GNUNET_CRYPTO_hkdf_v (result,
66  out_len,
67  GCRY_MD_SHA512,
68  GCRY_MD_SHA256,
69  xts,
70  xts_len,
71  skm,
72  skm_len,
73  argp);
74 }
75 
76 
88 int
90  size_t out_len,
91  const void *xts,
92  size_t xts_len,
93  const void *skm,
94  size_t skm_len, ...)
95 {
96  va_list argp;
97  int ret;
98 
99  va_start (argp, skm_len);
100  ret = GNUNET_CRYPTO_kdf_v (result,
101  out_len,
102  xts,
103  xts_len,
104  skm,
105  skm_len,
106  argp);
107  va_end (argp);
108 
109  return ret;
110 }
111 
112 
125 void
127  gcry_mpi_t n,
128  const void *xts, size_t xts_len,
129  const void *skm, size_t skm_len,
130  const char *ctx)
131 {
132  gcry_error_t rc;
133  unsigned int nbits;
134  size_t rsize;
135  unsigned int ctr;
136 
137  nbits = gcry_mpi_get_nbits (n);
138  /* GNUNET_assert (nbits > 512); */
139 
140  ctr = 0;
141  while (1)
142  {
143  /* Ain't clear if n is always divisible by 8 */
144  uint8_t buf[ (nbits - 1) / 8 + 1 ];
145 
146  rc = GNUNET_CRYPTO_kdf (buf,
147  sizeof(buf),
148  xts, xts_len,
149  skm, skm_len,
150  ctx, strlen (ctx),
151  &ctr, sizeof(ctr),
152  NULL, 0);
153  GNUNET_assert (GNUNET_YES == rc);
154 
155  rc = gcry_mpi_scan (r,
156  GCRYMPI_FMT_USG,
157  (const unsigned char *) buf,
158  sizeof(buf),
159  &rsize);
160  GNUNET_assert (0 == rc); /* Allocation erro? */
161 
162  gcry_mpi_clear_highbit (*r, nbits);
163  GNUNET_assert (0 == gcry_mpi_test_bit (*r, nbits));
164  ++ctr;
165  /* We reject this FDH if either *r > n and retry with another ctr */
166  if (0 > gcry_mpi_cmp (*r, n))
167  break;
168  gcry_mpi_release (*r);
169  }
170 }
171 
172 
173 /* end of crypto_kdf.c */
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)
Derive key.
Definition: crypto_hkdf.c:144
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
int GNUNET_CRYPTO_kdf_v(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len, va_list argp)
Derive key.
Definition: crypto_kdf.c:47
cryptographic primitives for GNUnet
void GNUNET_CRYPTO_kdf_mod_mpi(gcry_mpi_t *r, gcry_mpi_t n, const void *xts, size_t xts_len, const void *skm, size_t skm_len, const char *ctx)
Deterministically generate a pseudo-random number uniformly from the integers modulo a libgcrypt mpi...
Definition: crypto_kdf.c:126
static char buf[2048]
static int result
Global testing status.
#define GNUNET_YES
Definition: gnunet_common.h:77
int GNUNET_CRYPTO_kdf(void *result, size_t out_len, const void *xts, size_t xts_len, const void *skm, size_t skm_len,...)
Derive key.
Definition: crypto_kdf.c:89