aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_hmac_calculation.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/test_transport_hmac_calculation.c')
-rw-r--r--src/transport/test_transport_hmac_calculation.c250
1 files changed, 0 insertions, 250 deletions
diff --git a/src/transport/test_transport_hmac_calculation.c b/src/transport/test_transport_hmac_calculation.c
deleted file mode 100644
index 59f7e3d90..000000000
--- a/src/transport/test_transport_hmac_calculation.c
+++ /dev/null
@@ -1,250 +0,0 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2002-2015 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 */
21/**
22 * @file util/test_crypto_ecdh_eddsa.c
23 * @brief testcase for ECC DH key exchange with EdDSA private keys.
24 * @author Christian Grothoff
25 * @author Bart Polot
26 */
27#include "platform.h"
28#include "gnunet_util_lib.h"
29#include <gcrypt.h>
30
31
32/**
33 * Structure of the key material used to encrypt backchannel messages.
34 */
35struct DVKeyState
36{
37 /**
38 * State of our block cipher.
39 */
40 gcry_cipher_hd_t cipher;
41
42 /**
43 * Actual key material.
44 */
45 struct
46 {
47 /**
48 * Key used for HMAC calculations (via #GNUNET_CRYPTO_hmac()).
49 */
50 struct GNUNET_CRYPTO_AuthKey hmac_key;
51
52 /**
53 * Symmetric key to use for encryption.
54 */
55 char aes_key[256 / 8];
56
57 /**
58 * Counter value to use during setup.
59 */
60 char aes_ctr[128 / 8];
61 } material;
62};
63
64
65/**
66 * Given the key material in @a km and the initialization vector
67 * @a iv, setup the key material for the backchannel in @a key.
68 *
69 * @param km raw master secret
70 * @param iv initialization vector
71 * @param key[out] symmetric cipher and HMAC state to generate
72 */
73static void
74dv_setup_key_state_from_km (const struct GNUNET_HashCode *km,
75 const struct GNUNET_ShortHashCode *iv,
76 struct DVKeyState *key)
77{
78 char *key_string;
79
80
81 /* must match #dh_key_derive_eph_pub */
82 GNUNET_assert (GNUNET_YES ==
83 GNUNET_CRYPTO_kdf (&key->material,
84 sizeof(key->material),
85 "transport-backchannel-key",
86 strlen ("transport-backchannel-key"),
87 &km,
88 sizeof(km),
89 iv,
90 sizeof(*iv),
91 NULL));
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
93 "Deriving backchannel key based on KM %s and IV %s\n",
94 GNUNET_h2s (km),
95 GNUNET_sh2s (iv));
96 GNUNET_assert (0 == gcry_cipher_open (&key->cipher,
97 GCRY_CIPHER_AES256 /* low level: go for speed */,
98 GCRY_CIPHER_MODE_CTR,
99 0 /* flags */));
100 GNUNET_assert (0 == gcry_cipher_setkey (key->cipher,
101 &key->material.aes_key,
102 sizeof(key->material.aes_key)));
103 gcry_cipher_setctr (key->cipher,
104 &key->material.aes_ctr,
105 sizeof(key->material.aes_ctr));
106 GNUNET_free (key_string);
107}
108
109
110/**
111 * Do HMAC calculation for backchannel messages over @a data using key
112 * material from @a key.
113 *
114 * @param key key material (from DH)
115 * @param hmac[out] set to the HMAC
116 * @param data data to perform HMAC calculation over
117 * @param data_size number of bytes in @a data
118 */
119static void
120dv_hmac (const struct DVKeyState *key,
121 struct GNUNET_HashCode *hmac,
122 const void *data,
123 size_t data_size)
124{
125 GNUNET_CRYPTO_hmac (&key->material.hmac_key, data, data_size, hmac);
126}
127
128
129/**
130 * Clean up key material in @a key.
131 *
132 * @param key key material to clean up (memory must not be free'd!)
133 */
134static void
135dv_key_clean (struct DVKeyState *key)
136{
137 gcry_cipher_close (key->cipher);
138 GNUNET_CRYPTO_zero_keys (&key->material, sizeof(key->material));
139}
140
141
142static int
143test_ecdh ()
144{
145 struct GNUNET_CRYPTO_EddsaPrivateKey priv_dsa;
146 struct GNUNET_CRYPTO_EcdhePrivateKey priv_ecdh;
147 struct GNUNET_CRYPTO_EddsaPublicKey id1;
148 struct GNUNET_CRYPTO_EcdhePublicKey id2;
149 struct GNUNET_HashCode dh[2];
150 struct DVKeyState *key[2];
151 struct GNUNET_ShortHashCode iv;
152 struct GNUNET_HashCode hmac[2];
153 char *enc = "test";
154 char *key_string_1;
155 char *key_string_2;
156
157
158 key[0] = GNUNET_new (struct DVKeyState);
159 key[1] = GNUNET_new (struct DVKeyState);
160 GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_NONCE,
161 &iv,
162 sizeof(iv));
163
164 /* Generate keys */
165 GNUNET_CRYPTO_eddsa_key_create (&priv_dsa);
166 GNUNET_CRYPTO_eddsa_key_get_public (&priv_dsa,
167 &id1);
168
169 GNUNET_CRYPTO_ecdhe_key_create (&priv_ecdh);
170 /* Extract public keys */
171 GNUNET_CRYPTO_ecdhe_key_get_public (&priv_ecdh,
172 &id2);
173 /* Do ECDH */
174 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_eddsa_ecdh (&priv_dsa,
175 &id2,
176 &dh[0]));
177 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecdh_eddsa (&priv_ecdh,
178 &id1,
179 &dh[1]));
180 /* Check that both DH results are equal. */
181 GNUNET_assert (0 == GNUNET_memcmp (&dh[0],
182 &dh[1]));
183
184 dv_setup_key_state_from_km (&dh[0],
185 (const struct GNUNET_ShortHashCode *) &iv,
186 key[0]);
187 dv_hmac ((const struct DVKeyState * ) key[0],
188 &hmac[0], enc,
189 sizeof(enc));
190
191 dv_setup_key_state_from_km (&dh[1],
192 (const struct GNUNET_ShortHashCode *) &iv,
193 key[1]);
194 dv_hmac ((const struct DVKeyState *) key[1],
195 &hmac[1],
196 enc,
197 sizeof(enc));
198
199 key_string_1 = GNUNET_STRINGS_data_to_string_alloc (&key[0]->material.hmac_key,
200 sizeof (struct
201 GNUNET_CRYPTO_AuthKey));
202 key_string_2 = GNUNET_STRINGS_data_to_string_alloc (&key[1]->material.hmac_key,
203 sizeof (struct GNUNET_CRYPTO_AuthKey));
204
205 if (0 != GNUNET_memcmp (key[0], key[1]) || 0 != GNUNET_memcmp (&hmac[0], &hmac[1]))
206 {
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
208 "first key %s\n",
209 key_string_1);
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 "second key %s\n",
212 key_string_2);
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214 "first hmac %s\n",
215 GNUNET_h2s (&hmac[0]));
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
217 "second hmac %s\n",
218 GNUNET_h2s (&hmac[1]));
219 }
220 dv_key_clean (key[0]);
221 dv_key_clean (key[1]);
222 GNUNET_free (key_string_1);
223 GNUNET_free (key_string_2);
224
225 return 0;
226}
227
228
229int
230main (int argc, char *argv[])
231{
232 if (! gcry_check_version ("1.6.0"))
233 {
234 fprintf (stderr,
235 _ (
236 "libgcrypt has not the expected version (version %s is required).\n"),
237 "1.6.0");
238 return 0;
239 }
240 if (getenv ("GNUNET_GCRYPT_DEBUG"))
241 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
242 GNUNET_log_setup ("test-transport-hmac-calculation", "DEBUG", NULL);
243 if (0 != test_ecdh ())
244 return 1;
245
246 return 0;
247}
248
249
250/* end of test_crypto_ecdh_eddsa.c */