2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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"
32 * Number of characters a Base32-encoded public key requires.
34 #define KEY_STR_LEN sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)*8/5+1
37 * Flag for listing public key.
42 * Flag for listing public key.
44 static unsigned int list_keys_count;
47 * Flag for printing public key.
49 static int print_public_key;
52 * Flag for printing the output of random example operations.
54 static int print_examples_flag;
57 * Option set to create a bunch of keys at once.
59 static unsigned int make_keys;
63 * Create a flat file with a large number of key pairs for testing.
65 * @param fn File name to store the keys.
66 * @param prefix Desired prefix for the public keys, NULL if any key is OK.
69 create_keys (const char *fn, const char *prefix)
72 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
73 struct GNUNET_CRYPTO_EddsaPublicKey target_pub;
74 static char vanity[KEY_STR_LEN + 1];
82 if (NULL == (f = fopen (fn, "w+")))
84 fprintf (stderr, _("Failed to open `%s': %s\n"), fn, STRERROR (errno));
89 strncpy (vanity, prefix, KEY_STR_LEN);
90 len = GNUNET_MIN (strlen (prefix), KEY_STR_LEN);
94 memset (&vanity[len], '0', KEY_STR_LEN - len);
95 vanity[KEY_STR_LEN] = '\0';
96 GNUNET_assert (GNUNET_OK ==
97 GNUNET_CRYPTO_eddsa_public_key_from_string (vanity,
103 * Documentation by example:
106 * n = 5/8 = 0 (bytes)
107 * rest = 5%8 = 5 (bits)
108 * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000
110 mask = ~ ((int)pow (2, 8 - rest) - 1);
111 target_byte = ((unsigned char *) &target_pub)[n] & mask;
115 /* Just so old (debian) versions of GCC calm down with the warnings. */
116 mask = target_byte = 0;
118 s = GNUNET_CRYPTO_eddsa_public_key_to_string (&target_pub);
120 _("Generating %u keys like %s, please wait"),
125 "\nattempt %s [%u, %X]\n",
133 _("Generating %u keys, please wait"),
135 /* Just so old (debian) versions of GCC calm down with the warnings. */
136 n = rest = target_byte = mask = 0;
139 while (0 < make_keys--)
141 fprintf (stderr, ".");
142 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ()))
149 struct GNUNET_CRYPTO_EddsaPublicKey newkey;
151 GNUNET_CRYPTO_eddsa_key_get_public (pk, &newkey);
152 if (0 != memcmp (&target_pub, &newkey, n))
159 unsigned char new_byte;
161 new_byte = ((unsigned char *) &newkey)[n] & mask;
162 if (target_byte != new_byte)
169 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
171 GNUNET_TESTING_HOSTKEYFILESIZE, f))
174 _("\nFailed to write to `%s': %s\n"),
182 if (UINT_MAX == make_keys)
187 _("\nError, %u keys not generated\n"),
194 print_hex (const char *msg,
200 printf ("%s: ", msg);
201 for (i = 0; i < size; i++)
203 printf ("%02hhx", ((const char *)buf)[i]);
210 print_examples_ecdh ()
212 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv1;
213 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub1;
214 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv2;
215 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub2;
216 struct GNUNET_HashCode hash;
219 dh_pub1 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
220 dh_priv1 = GNUNET_CRYPTO_ecdhe_key_create ();
221 dh_pub2 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
222 dh_priv2 = GNUNET_CRYPTO_ecdhe_key_create ();
223 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv1, dh_pub1);
224 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv2, dh_pub2);
226 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv1, 32, buf, 128));
227 printf ("ECDHE key 1:\n");
228 printf ("private: %s\n", buf);
229 print_hex ("private(hex)", dh_priv1, sizeof *dh_priv1);
230 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub1, 32, buf, 128));
231 printf ("public: %s\n", buf);
232 print_hex ("public(hex)", dh_pub1, sizeof *dh_pub1);
234 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv2, 32, buf, 128));
235 printf ("ECDHE key 2:\n");
236 printf ("private: %s\n", buf);
237 print_hex ("private(hex)", dh_priv2, sizeof *dh_priv2);
238 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub2, 32, buf, 128));
239 printf ("public: %s\n", buf);
240 print_hex ("public(hex)", dh_pub2, sizeof *dh_pub2);
242 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecc_ecdh (dh_priv1, dh_pub2, &hash));
243 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (&hash, 64, buf, 128));
244 printf ("ECDH shared secret: %s\n", buf);
246 GNUNET_free (dh_priv1);
247 GNUNET_free (dh_priv2);
248 GNUNET_free (dh_pub1);
249 GNUNET_free (dh_pub2);
254 * Print some random example operations to stdout.
259 print_examples_ecdh ();
260 // print_examples_ecdsa ();
261 // print_examples_eddsa ();
266 print_key (const char *filename)
268 struct GNUNET_DISK_FileHandle *fd;
269 struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
270 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
274 unsigned int total_hostkeys;
277 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
280 _("Hostkeys file `%s' not found\n"),
285 /* Check hostkey file size, read entire thing into memory */
286 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
291 _("Hostkeys file `%s' is empty\n"),
293 return; /* File is empty */
295 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
298 _("Incorrect hostkey file format: %s\n"),
302 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
303 GNUNET_DISK_PERM_NONE);
306 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
309 hostkeys_data = GNUNET_malloc (fs);
310 if (fs != GNUNET_DISK_file_read (fd, hostkeys_data, fs))
313 _("Could not read hostkey file: %s\n"),
315 GNUNET_free (hostkeys_data);
316 GNUNET_DISK_file_close (fd);
319 GNUNET_DISK_file_close (fd);
321 if (NULL == hostkeys_data)
323 total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
324 for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
326 GNUNET_memcpy (&private_key,
327 hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE),
328 GNUNET_TESTING_HOSTKEYFILESIZE);
329 GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key);
330 hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key);
331 if (NULL != hostkey_str)
333 fprintf (stderr, "%4u: %s\n", c, hostkey_str);
334 GNUNET_free (hostkey_str);
337 fprintf (stderr, "%4u: %s\n", c, "invalid");
339 GNUNET_free (hostkeys_data);
344 * Main function that will be run by the scheduler.
347 * @param args remaining command-line arguments
348 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
349 * @param cfg configuration
352 run (void *cls, char *const *args, const char *cfgfile,
353 const struct GNUNET_CONFIGURATION_Handle *cfg)
355 if (print_examples_flag)
364 _("No hostkey file specified on command line\n"));
374 create_keys (args[0], args[1]);
377 if (print_public_key)
380 struct GNUNET_DISK_FileHandle *keyfile;
381 struct GNUNET_CRYPTO_EddsaPrivateKey pk;
382 struct GNUNET_CRYPTO_EddsaPublicKey pub;
384 keyfile = GNUNET_DISK_file_open (args[0], GNUNET_DISK_OPEN_READ,
385 GNUNET_DISK_PERM_NONE);
388 while (sizeof (pk) == GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk)))
390 GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub);
391 str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
392 FPRINTF (stdout, "%s\n", str);
395 GNUNET_DISK_file_close (keyfile);
402 * Program to manipulate ECC key files.
404 * @param argc number of arguments from the command line
405 * @param argv command line arguments
406 * @return 0 ok, 1 on error
412 list_keys_count = UINT32_MAX;
413 struct GNUNET_GETOPT_CommandLineOption options[] = {
414 GNUNET_GETOPT_OPTION_SET_ONE ('i',
416 gettext_noop ("list keys included in a file (for testing)"),
418 GNUNET_GETOPT_OPTION_SET_UINT ('e',
421 gettext_noop ("number of keys to list included in a file (for testing)"),
423 GNUNET_GETOPT_OPTION_SET_UINT ('g',
426 gettext_noop ("create COUNT public-private key pairs (for testing)"),
428 GNUNET_GETOPT_OPTION_SET_ONE ('p',
430 gettext_noop ("print the public key in ASCII format"),
432 GNUNET_GETOPT_OPTION_SET_ONE ('E',
434 gettext_noop ("print examples of ECC operations (used for compatibility testing)"),
435 &print_examples_flag),
436 GNUNET_GETOPT_OPTION_END
441 GNUNET_STRINGS_get_utf8_args (argc, argv,
446 GNUNET_PROGRAM_run (argc,
448 "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]",
449 gettext_noop ("Manipulate GNUnet private ECC key files"),
453 GNUNET_free ((void*) argv);
457 /* end of gnunet-ecc.c */