2 This file is part of GNUnet.
3 (C) 2012, 2013 Christian Grothoff (and other contributing authors)
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.
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.
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/gnunet-ecc.c
23 * @brief tool to manipulate EDDSA key files
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
31 #define KEY_STR_LEN sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)*8/5+1
34 * Flag for listing public key.
39 * Flag for listing public key.
41 static int list_keys_count;
44 * Flag for printing public key.
46 static int print_public_key;
49 * Flag for printing the output of random example operations.
51 static int print_examples_flag;
54 * Option set to create a bunch of keys at once.
56 static unsigned int make_keys;
60 * Create a flat file with a large number of key pairs for testing.
62 * @param fn File name to store the keys.
63 * @param prefix Desired prefix for the public keys, NULL if any key is OK.
66 create_keys (const char *fn, const char *prefix)
69 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
70 struct GNUNET_CRYPTO_EddsaPublicKey target_pub;
71 static char vanity[KEY_STR_LEN + 1];
78 if (NULL == (f = fopen (fn, "w+")))
81 _("Failed to open `%s': %s\n"),
88 strncpy (vanity, prefix, KEY_STR_LEN);
89 len = strlen (vanity);
93 memset (&vanity[len], '0', KEY_STR_LEN - len);
94 GNUNET_assert (GNUNET_OK ==
95 GNUNET_CRYPTO_eddsa_public_key_from_string (vanity,
101 * Documentation by example:
104 * n = 5/8 = 0 (bytes)
105 * rest = 5%8 = 5 (bits)
106 * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000
108 mask = ~ ((int)pow (2, 8 - rest) - 1);
109 target_byte = ((unsigned char *) &target_pub)[n] & mask;
112 _("Generating %u keys like %s, please wait"),
113 make_keys, GNUNET_CRYPTO_eddsa_public_key_to_string (&target_pub));
114 fprintf (stderr, "\nattempt %s [%d, %X]\n", vanity, n, mask);
118 fprintf (stderr, _("Generating %u keys, please wait"), make_keys);
121 while (0 < make_keys--)
123 fprintf (stderr, ".");
124 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ()))
131 struct GNUNET_CRYPTO_EddsaPublicKey newkey;
133 GNUNET_CRYPTO_eddsa_key_get_public (pk, &newkey);
134 if (0 != memcmp (&target_pub, &newkey, n))
141 unsigned char new_byte;
143 new_byte = ((unsigned char *) &newkey)[n] & mask;
144 if (target_byte != new_byte)
151 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
153 GNUNET_TESTING_HOSTKEYFILESIZE, f))
156 _("\nFailed to write to `%s': %s\n"),
164 if (UINT_MAX == make_keys)
169 _("\nError, %u keys not generated\n"), make_keys);
175 print_hex (char *msg, void *buf, size_t size)
178 printf ("%s: ", msg);
179 for (i = 0; i < size; i++)
181 printf ("%02hhx", ((char *)buf)[i]);
188 print_examples_ecdh ()
190 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv1;
191 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub1;
192 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv2;
193 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub2;
194 struct GNUNET_HashCode hash;
197 dh_pub1 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
198 dh_priv1 = GNUNET_CRYPTO_ecdhe_key_create ();
199 dh_pub2 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
200 dh_priv2 = GNUNET_CRYPTO_ecdhe_key_create ();
201 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv1, dh_pub1);
202 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv2, dh_pub2);
204 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv1, 32, buf, 128));
205 printf ("ECDHE key 1:\n");
206 printf ("private: %s\n", buf);
207 print_hex ("private(hex)", dh_priv1, sizeof *dh_priv1);
208 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub1, 32, buf, 128));
209 printf ("public: %s\n", buf);
210 print_hex ("public(hex)", dh_pub1, sizeof *dh_pub1);
212 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv2, 32, buf, 128));
213 printf ("ECDHE key 2:\n");
214 printf ("private: %s\n", buf);
215 print_hex ("private(hex)", dh_priv2, sizeof *dh_priv2);
216 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub2, 32, buf, 128));
217 printf ("public: %s\n", buf);
218 print_hex ("public(hex)", dh_pub2, sizeof *dh_pub2);
220 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecc_ecdh (dh_priv1, dh_pub2, &hash));
221 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (&hash, 64, buf, 128));
222 printf ("ECDH shared secret: %s\n", buf);
224 GNUNET_free (dh_priv1);
225 GNUNET_free (dh_priv2);
226 GNUNET_free (dh_pub1);
227 GNUNET_free (dh_pub2);
232 * Print some random example operations to stdout.
237 print_examples_ecdh ();
238 // print_examples_ecdsa ();
239 // print_examples_eddsa ();
244 print_key (const char *filename)
246 struct GNUNET_DISK_FileHandle *fd;
247 struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
248 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
252 unsigned int total_hostkeys;
255 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
258 _("Hostkeys file `%s' not found\n"),
263 /* Check hostkey file size, read entire thing into memory */
264 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
269 _("Hostkeys file `%s' is empty\n"),
271 return; /* File is empty */
273 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
276 _("Incorrect hostkey file format: %s\n"),
280 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
281 GNUNET_DISK_PERM_NONE);
284 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
287 hostkeys_data = GNUNET_malloc (fs);
288 if (fs != GNUNET_DISK_file_read (fd, hostkeys_data, fs))
291 _("Could not read hostkey file: %s\n"),
293 GNUNET_free (hostkeys_data);
294 GNUNET_DISK_file_close (fd);
297 GNUNET_DISK_file_close (fd);
299 if (NULL == hostkeys_data)
301 total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
302 for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
304 memcpy (&private_key,
305 hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE),
306 GNUNET_TESTING_HOSTKEYFILESIZE);
307 GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key);
308 hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key);
309 if (NULL != hostkey_str)
311 fprintf (stderr, "%4u: %s\n", c, hostkey_str);
312 GNUNET_free (hostkey_str);
315 fprintf (stderr, "%4u: %s\n", c, "invalid");
317 GNUNET_free (hostkeys_data);
322 * Main function that will be run by the scheduler.
325 * @param args remaining command-line arguments
326 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
327 * @param cfg configuration
330 run (void *cls, char *const *args, const char *cfgfile,
331 const struct GNUNET_CONFIGURATION_Handle *cfg)
333 if (print_examples_flag)
342 _("No hostkey file specified on command line\n"));
352 create_keys (args[0], args[1]);
355 if (print_public_key)
358 struct GNUNET_DISK_FileHandle *keyfile;
359 struct GNUNET_CRYPTO_EddsaPrivateKey pk;
360 struct GNUNET_CRYPTO_EddsaPublicKey pub;
362 keyfile = GNUNET_DISK_file_open (args[0], GNUNET_DISK_OPEN_READ,
363 GNUNET_DISK_PERM_NONE);
366 while (sizeof (pk) == GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk)))
368 GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub);
369 str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
370 FPRINTF (stdout, "%s\n", str);
373 GNUNET_DISK_file_close (keyfile);
380 * Program to manipulate ECC key files.
382 * @param argc number of arguments from the command line
383 * @param argv command line arguments
384 * @return 0 ok, 1 on error
387 main (int argc, char *const *argv)
389 list_keys_count = UINT32_MAX;
390 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
391 { 'i', "iterate", "FILE",
392 gettext_noop ("list keys included in a file (for testing)"),
393 0, &GNUNET_GETOPT_set_one, &list_keys },
394 { 'e', "end=", "COUNT",
395 gettext_noop ("number of keys to list included in a file (for testing)"),
396 1, &GNUNET_GETOPT_set_uint, &list_keys_count },
397 { 'g', "generate-keys", "COUNT",
398 gettext_noop ("create COUNT public-private key pairs (for testing)"),
399 1, &GNUNET_GETOPT_set_uint, &make_keys },
400 { 'p', "print-public-key", NULL,
401 gettext_noop ("print the public key in ASCII format"),
402 0, &GNUNET_GETOPT_set_one, &print_public_key },
403 { 'E', "examples", NULL,
404 gettext_noop ("print examples of ECC operations (used for compatibility testing)"),
405 0, &GNUNET_GETOPT_set_one, &print_examples_flag },
406 GNUNET_GETOPT_OPTION_END
410 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
414 GNUNET_PROGRAM_run (argc, argv, "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]",
415 gettext_noop ("Manipulate GNUnet private ECC key files"),
416 options, &run, NULL)) ? 0 : 1;
417 GNUNET_free ((void*) argv);
421 /* end of gnunet-ecc.c */