aboutsummaryrefslogtreecommitdiff
path: root/src/util/gnunet-ecc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/gnunet-ecc.c')
-rw-r--r--src/util/gnunet-ecc.c108
1 files changed, 67 insertions, 41 deletions
diff --git a/src/util/gnunet-ecc.c b/src/util/gnunet-ecc.c
index 02d2020ad..764a507b6 100644
--- a/src/util/gnunet-ecc.c
+++ b/src/util/gnunet-ecc.c
@@ -79,7 +79,7 @@ static void
79create_keys (const char *fn, const char *prefix) 79create_keys (const char *fn, const char *prefix)
80{ 80{
81 FILE *f; 81 FILE *f;
82 struct GNUNET_CRYPTO_EddsaPrivateKey *pk; 82 struct GNUNET_CRYPTO_EddsaPrivateKey pk;
83 struct GNUNET_CRYPTO_EddsaPublicKey target_pub; 83 struct GNUNET_CRYPTO_EddsaPublicKey target_pub;
84 static char vanity[KEY_STR_LEN + 1]; 84 static char vanity[KEY_STR_LEN + 1];
85 size_t len; 85 size_t len;
@@ -141,17 +141,16 @@ create_keys (const char *fn, const char *prefix)
141 while (0 < make_keys--) 141 while (0 < make_keys--)
142 { 142 {
143 fprintf (stderr, "."); 143 fprintf (stderr, ".");
144 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ())) 144 GNUNET_CRYPTO_eddsa_key_create (&pk);
145 {
146 GNUNET_break (0);
147 break;
148 }
149 if (NULL != prefix) 145 if (NULL != prefix)
150 { 146 {
151 struct GNUNET_CRYPTO_EddsaPublicKey newkey; 147 struct GNUNET_CRYPTO_EddsaPublicKey newkey;
152 148
153 GNUNET_CRYPTO_eddsa_key_get_public (pk, &newkey); 149 GNUNET_CRYPTO_eddsa_key_get_public (&pk,
154 if (0 != memcmp (&target_pub, &newkey, n)) 150 &newkey);
151 if (0 != memcmp (&target_pub,
152 &newkey,
153 n))
155 { 154 {
156 make_keys++; 155 make_keys++;
157 continue; 156 continue;
@@ -169,16 +168,17 @@ create_keys (const char *fn, const char *prefix)
169 } 168 }
170 } 169 }
171 if (GNUNET_TESTING_HOSTKEYFILESIZE != 170 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
172 fwrite (pk, 1, GNUNET_TESTING_HOSTKEYFILESIZE, f)) 171 fwrite (&pk,
172 1,
173 GNUNET_TESTING_HOSTKEYFILESIZE,
174 f))
173 { 175 {
174 fprintf (stderr, 176 fprintf (stderr,
175 _ ("\nFailed to write to `%s': %s\n"), 177 _ ("\nFailed to write to `%s': %s\n"),
176 fn, 178 fn,
177 strerror (errno)); 179 strerror (errno));
178 GNUNET_free (pk);
179 break; 180 break;
180 } 181 }
181 GNUNET_free (pk);
182 } 182 }
183 if (UINT_MAX == make_keys) 183 if (UINT_MAX == make_keys)
184 fprintf (stderr, _ ("\nFinished!\n")); 184 fprintf (stderr, _ ("\nFinished!\n"));
@@ -201,49 +201,75 @@ print_hex (const char *msg, const void *buf, size_t size)
201 201
202 202
203static void 203static void
204print_examples_ecdh () 204print_examples_ecdh (void)
205{ 205{
206 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv1; 206 struct GNUNET_CRYPTO_EcdhePrivateKey dh_priv1;
207 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub1; 207 struct GNUNET_CRYPTO_EcdhePublicKey dh_pub1;
208 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv2; 208 struct GNUNET_CRYPTO_EcdhePrivateKey dh_priv2;
209 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub2; 209 struct GNUNET_CRYPTO_EcdhePublicKey dh_pub2;
210 struct GNUNET_HashCode hash; 210 struct GNUNET_HashCode hash;
211 char buf[128]; 211 char buf[128];
212 212
213 dh_pub1 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey); 213 GNUNET_CRYPTO_ecdhe_key_create (&dh_priv1);
214 dh_priv1 = GNUNET_CRYPTO_ecdhe_key_create (); 214 GNUNET_CRYPTO_ecdhe_key_create (&dh_priv2);
215 dh_pub2 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey); 215 GNUNET_CRYPTO_ecdhe_key_get_public (&dh_priv1,
216 dh_priv2 = GNUNET_CRYPTO_ecdhe_key_create (); 216 &dh_pub1);
217 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv1, dh_pub1); 217 GNUNET_CRYPTO_ecdhe_key_get_public (&dh_priv2,
218 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv2, dh_pub2); 218 &dh_pub2);
219 219
220 GNUNET_assert (NULL != 220 GNUNET_assert (NULL !=
221 GNUNET_STRINGS_data_to_string (dh_priv1, 32, buf, 128)); 221 GNUNET_STRINGS_data_to_string (&dh_priv1,
222 sizeof (dh_priv1),
223 buf,
224 sizeof (buf)));
222 printf ("ECDHE key 1:\n"); 225 printf ("ECDHE key 1:\n");
223 printf ("private: %s\n", buf); 226 printf ("private: %s\n",
224 print_hex ("private(hex)", dh_priv1, sizeof *dh_priv1); 227 buf);
225 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub1, 32, buf, 128)); 228 print_hex ("private(hex)",
226 printf ("public: %s\n", buf); 229 &dh_priv1, sizeof (dh_priv1));
227 print_hex ("public(hex)", dh_pub1, sizeof *dh_pub1); 230 GNUNET_assert (NULL !=
231 GNUNET_STRINGS_data_to_string (&dh_pub1,
232 sizeof (dh_pub1),
233 buf,
234 sizeof (buf)));
235 printf ("public: %s\n",
236 buf);
237 print_hex ("public(hex)",
238 &dh_pub1,
239 sizeof (dh_pub1));
228 240
229 GNUNET_assert (NULL != 241 GNUNET_assert (NULL !=
230 GNUNET_STRINGS_data_to_string (dh_priv2, 32, buf, 128)); 242 GNUNET_STRINGS_data_to_string (&dh_priv2,
243 sizeof (dh_priv2),
244 buf,
245 sizeof (buf)));
231 printf ("ECDHE key 2:\n"); 246 printf ("ECDHE key 2:\n");
232 printf ("private: %s\n", buf); 247 printf ("private: %s\n", buf);
233 print_hex ("private(hex)", dh_priv2, sizeof *dh_priv2); 248 print_hex ("private(hex)",
234 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub2, 32, buf, 128)); 249 &dh_priv2,
250 sizeof (dh_priv2));
251 GNUNET_assert (NULL !=
252 GNUNET_STRINGS_data_to_string (&dh_pub2,
253 sizeof (dh_pub2),
254 buf,
255 sizeof (buf)));
235 printf ("public: %s\n", buf); 256 printf ("public: %s\n", buf);
236 print_hex ("public(hex)", dh_pub2, sizeof *dh_pub2); 257 print_hex ("public(hex)",
258 &dh_pub2,
259 sizeof (dh_pub2));
237 260
238 GNUNET_assert (GNUNET_OK == 261 GNUNET_assert (GNUNET_OK ==
239 GNUNET_CRYPTO_ecc_ecdh (dh_priv1, dh_pub2, &hash)); 262 GNUNET_CRYPTO_ecc_ecdh (&dh_priv1,
240 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (&hash, 64, buf, 128)); 263 &dh_pub2,
241 printf ("ECDH shared secret: %s\n", buf); 264 &hash));
242 265 GNUNET_assert (NULL !=
243 GNUNET_free (dh_priv1); 266 GNUNET_STRINGS_data_to_string (&hash,
244 GNUNET_free (dh_priv2); 267 sizeof (hash),
245 GNUNET_free (dh_pub1); 268 buf,
246 GNUNET_free (dh_pub2); 269 sizeof (buf)));
270 printf ("ECDH shared secret: %s\n",
271 buf);
272
247} 273}
248 274
249 275
@@ -251,7 +277,7 @@ print_examples_ecdh ()
251 * Print some random example operations to stdout. 277 * Print some random example operations to stdout.
252 */ 278 */
253static void 279static void
254print_examples () 280print_examples (void)
255{ 281{
256 print_examples_ecdh (); 282 print_examples_ecdh ();
257 // print_examples_ecdsa (); 283 // print_examples_ecdsa ();