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 private key.
54 static int print_private_key;
57 * Flag for printing public key in hex.
59 static int print_public_key_hex;
62 * Flag for printing the output of random example operations.
64 static int print_examples_flag;
67 * Option set to create a bunch of keys at once.
69 static unsigned int make_keys;
73 * Create a flat file with a large number of key pairs for testing.
75 * @param fn File name to store the keys.
76 * @param prefix Desired prefix for the public keys, NULL if any key is OK.
79 create_keys (const char *fn, const char *prefix)
82 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
83 struct GNUNET_CRYPTO_EddsaPublicKey target_pub;
84 static char vanity[KEY_STR_LEN + 1];
92 if (NULL == (f = fopen (fn, "w+")))
94 fprintf (stderr, _("Failed to open `%s': %s\n"), fn, STRERROR (errno));
99 strncpy (vanity, prefix, KEY_STR_LEN);
100 len = GNUNET_MIN (strlen (prefix), KEY_STR_LEN);
104 memset (&vanity[len], '0', KEY_STR_LEN - len);
105 vanity[KEY_STR_LEN] = '\0';
106 GNUNET_assert (GNUNET_OK ==
107 GNUNET_CRYPTO_eddsa_public_key_from_string (vanity,
113 * Documentation by example:
116 * n = 5/8 = 0 (bytes)
117 * rest = 5%8 = 5 (bits)
118 * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000
120 mask = ~ ((int)pow (2, 8 - rest) - 1);
121 target_byte = ((unsigned char *) &target_pub)[n] & mask;
125 /* Just so old (debian) versions of GCC calm down with the warnings. */
126 mask = target_byte = 0;
128 s = GNUNET_CRYPTO_eddsa_public_key_to_string (&target_pub);
130 _("Generating %u keys like %s, please wait"),
135 "\nattempt %s [%u, %X]\n",
143 _("Generating %u keys, please wait"),
145 /* Just so old (debian) versions of GCC calm down with the warnings. */
146 n = rest = target_byte = mask = 0;
149 while (0 < make_keys--)
151 fprintf (stderr, ".");
152 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ()))
159 struct GNUNET_CRYPTO_EddsaPublicKey newkey;
161 GNUNET_CRYPTO_eddsa_key_get_public (pk, &newkey);
162 if (0 != memcmp (&target_pub, &newkey, n))
169 unsigned char new_byte;
171 new_byte = ((unsigned char *) &newkey)[n] & mask;
172 if (target_byte != new_byte)
179 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
181 GNUNET_TESTING_HOSTKEYFILESIZE, f))
184 _("\nFailed to write to `%s': %s\n"),
192 if (UINT_MAX == make_keys)
197 _("\nError, %u keys not generated\n"),
204 print_hex (const char *msg,
208 printf ("%s: ", msg);
209 for (size_t i = 0; i < size; i++)
211 printf ("%02hhx", ((const uint8_t *)buf)[i]);
218 print_examples_ecdh ()
220 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv1;
221 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub1;
222 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv2;
223 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub2;
224 struct GNUNET_HashCode hash;
227 dh_pub1 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
228 dh_priv1 = GNUNET_CRYPTO_ecdhe_key_create ();
229 dh_pub2 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
230 dh_priv2 = GNUNET_CRYPTO_ecdhe_key_create ();
231 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv1, dh_pub1);
232 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv2, dh_pub2);
234 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv1, 32, buf, 128));
235 printf ("ECDHE key 1:\n");
236 printf ("private: %s\n", buf);
237 print_hex ("private(hex)", dh_priv1, sizeof *dh_priv1);
238 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub1, 32, buf, 128));
239 printf ("public: %s\n", buf);
240 print_hex ("public(hex)", dh_pub1, sizeof *dh_pub1);
242 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv2, 32, buf, 128));
243 printf ("ECDHE key 2:\n");
244 printf ("private: %s\n", buf);
245 print_hex ("private(hex)", dh_priv2, sizeof *dh_priv2);
246 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub2, 32, buf, 128));
247 printf ("public: %s\n", buf);
248 print_hex ("public(hex)", dh_pub2, sizeof *dh_pub2);
250 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecc_ecdh (dh_priv1, dh_pub2, &hash));
251 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (&hash, 64, buf, 128));
252 printf ("ECDH shared secret: %s\n", buf);
254 GNUNET_free (dh_priv1);
255 GNUNET_free (dh_priv2);
256 GNUNET_free (dh_pub1);
257 GNUNET_free (dh_pub2);
262 * Print some random example operations to stdout.
267 print_examples_ecdh ();
268 // print_examples_ecdsa ();
269 // print_examples_eddsa ();
274 print_key (const char *filename)
276 struct GNUNET_DISK_FileHandle *fd;
277 struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
278 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
282 unsigned int total_hostkeys;
285 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
288 _("Hostkeys file `%s' not found\n"),
293 /* Check hostkey file size, read entire thing into memory */
294 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
299 _("Hostkeys file `%s' is empty\n"),
301 return; /* File is empty */
303 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
306 _("Incorrect hostkey file format: %s\n"),
310 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
311 GNUNET_DISK_PERM_NONE);
314 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
317 hostkeys_data = GNUNET_malloc (fs);
318 if (fs != GNUNET_DISK_file_read (fd, hostkeys_data, fs))
321 _("Could not read hostkey file: %s\n"),
323 GNUNET_free (hostkeys_data);
324 GNUNET_DISK_file_close (fd);
327 GNUNET_DISK_file_close (fd);
329 if (NULL == hostkeys_data)
331 total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
332 for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
334 GNUNET_memcpy (&private_key,
335 hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE),
336 GNUNET_TESTING_HOSTKEYFILESIZE);
337 GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key);
338 hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key);
339 if (NULL != hostkey_str)
341 fprintf (stderr, "%4u: %s\n", c, hostkey_str);
342 GNUNET_free (hostkey_str);
345 fprintf (stderr, "%4u: %s\n", c, "invalid");
347 GNUNET_free (hostkeys_data);
352 * Main function that will be run by the scheduler.
355 * @param args remaining command-line arguments
356 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
357 * @param cfg configuration
360 run (void *cls, char *const *args, const char *cfgfile,
361 const struct GNUNET_CONFIGURATION_Handle *cfg)
363 if (print_examples_flag)
372 _("No hostkey file specified on command line\n"));
382 create_keys (args[0], args[1]);
385 if (print_public_key || print_public_key_hex || print_private_key)
388 struct GNUNET_DISK_FileHandle *keyfile;
389 struct GNUNET_CRYPTO_EddsaPrivateKey pk;
390 struct GNUNET_CRYPTO_EddsaPublicKey pub;
392 keyfile = GNUNET_DISK_file_open (args[0], GNUNET_DISK_OPEN_READ,
393 GNUNET_DISK_PERM_NONE);
396 while (sizeof (pk) ==
397 GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk)))
399 GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub);
400 if (print_public_key_hex)
402 print_hex ("HEX:", &pub, sizeof (pub));
404 else if (print_public_key)
406 str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
407 FPRINTF (stdout, "%s\n", str);
410 else if (print_private_key)
412 str = GNUNET_CRYPTO_eddsa_private_key_to_string (&pk);
413 FPRINTF (stdout, "%s\n", str);
417 GNUNET_DISK_file_close (keyfile);
424 * Program to manipulate ECC key files.
426 * @param argc number of arguments from the command line
427 * @param argv command line arguments
428 * @return 0 ok, 1 on error
434 struct GNUNET_GETOPT_CommandLineOption options[] = {
435 GNUNET_GETOPT_option_flag ('i',
437 gettext_noop ("list keys included in a file (for testing)"),
439 GNUNET_GETOPT_option_uint ('e',
442 gettext_noop ("number of keys to list included in a file (for testing)"),
444 GNUNET_GETOPT_option_uint ('g',
447 gettext_noop ("create COUNT public-private key pairs (for testing)"),
449 GNUNET_GETOPT_option_flag ('p',
451 gettext_noop ("print the public key in ASCII format"),
453 GNUNET_GETOPT_option_flag ('P',
455 gettext_noop ("print the private key in ASCII format"),
457 GNUNET_GETOPT_option_flag ('x',
459 gettext_noop ("print the public key in HEX format"),
460 &print_public_key_hex),
461 GNUNET_GETOPT_option_flag ('E',
463 gettext_noop ("print examples of ECC operations (used for compatibility testing)"),
464 &print_examples_flag),
465 GNUNET_GETOPT_OPTION_END
469 list_keys_count = UINT32_MAX;
471 GNUNET_STRINGS_get_utf8_args (argc, argv,
476 GNUNET_PROGRAM_run (argc,
478 "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]",
479 gettext_noop ("Manipulate GNUnet private ECC key files"),
483 GNUNET_free ((void*) argv);
487 /* end of gnunet-ecc.c */