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 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.
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.
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/>.
20 * @file util/gnunet-ecc.c
21 * @brief tool to manipulate EDDSA key files
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_testing_lib.h"
30 * Number of characters a Base32-encoded public key requires.
32 #define KEY_STR_LEN sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)*8/5+1
35 * Flag for listing public key.
40 * Flag for listing public key.
42 static unsigned int list_keys_count;
45 * Flag for printing public key.
47 static int print_public_key;
50 * Flag for printing private key.
52 static int print_private_key;
55 * Flag for printing public key in hex.
57 static int print_public_key_hex;
60 * Flag for printing the output of random example operations.
62 static int print_examples_flag;
65 * Option set to create a bunch of keys at once.
67 static unsigned int make_keys;
71 * Create a flat file with a large number of key pairs for testing.
73 * @param fn File name to store the keys.
74 * @param prefix Desired prefix for the public keys, NULL if any key is OK.
77 create_keys (const char *fn, const char *prefix)
80 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
81 struct GNUNET_CRYPTO_EddsaPublicKey target_pub;
82 static char vanity[KEY_STR_LEN + 1];
90 if (NULL == (f = fopen (fn, "w+")))
92 fprintf (stderr, _("Failed to open `%s': %s\n"), fn, STRERROR (errno));
97 strncpy (vanity, prefix, KEY_STR_LEN);
98 len = GNUNET_MIN (strlen (prefix), KEY_STR_LEN);
102 memset (&vanity[len], '0', KEY_STR_LEN - len);
103 vanity[KEY_STR_LEN] = '\0';
104 GNUNET_assert (GNUNET_OK ==
105 GNUNET_CRYPTO_eddsa_public_key_from_string (vanity,
111 * Documentation by example:
114 * n = 5/8 = 0 (bytes)
115 * rest = 5%8 = 5 (bits)
116 * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000
118 mask = ~ ((int)pow (2, 8 - rest) - 1);
119 target_byte = ((unsigned char *) &target_pub)[n] & mask;
123 /* Just so old (debian) versions of GCC calm down with the warnings. */
124 mask = target_byte = 0;
126 s = GNUNET_CRYPTO_eddsa_public_key_to_string (&target_pub);
128 _("Generating %u keys like %s, please wait"),
133 "\nattempt %s [%u, %X]\n",
141 _("Generating %u keys, please wait"),
143 /* Just so old (debian) versions of GCC calm down with the warnings. */
144 n = rest = target_byte = mask = 0;
147 while (0 < make_keys--)
149 fprintf (stderr, ".");
150 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ()))
157 struct GNUNET_CRYPTO_EddsaPublicKey newkey;
159 GNUNET_CRYPTO_eddsa_key_get_public (pk, &newkey);
160 if (0 != memcmp (&target_pub, &newkey, n))
167 unsigned char new_byte;
169 new_byte = ((unsigned char *) &newkey)[n] & mask;
170 if (target_byte != new_byte)
177 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
179 GNUNET_TESTING_HOSTKEYFILESIZE, f))
182 _("\nFailed to write to `%s': %s\n"),
190 if (UINT_MAX == make_keys)
195 _("\nError, %u keys not generated\n"),
202 print_hex (const char *msg,
206 printf ("%s: ", msg);
207 for (size_t i = 0; i < size; i++)
209 printf ("%02hhx", ((const uint8_t *)buf)[i]);
216 print_examples_ecdh ()
218 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv1;
219 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub1;
220 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv2;
221 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub2;
222 struct GNUNET_HashCode hash;
225 dh_pub1 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
226 dh_priv1 = GNUNET_CRYPTO_ecdhe_key_create ();
227 dh_pub2 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
228 dh_priv2 = GNUNET_CRYPTO_ecdhe_key_create ();
229 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv1, dh_pub1);
230 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv2, dh_pub2);
232 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv1, 32, buf, 128));
233 printf ("ECDHE key 1:\n");
234 printf ("private: %s\n", buf);
235 print_hex ("private(hex)", dh_priv1, sizeof *dh_priv1);
236 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub1, 32, buf, 128));
237 printf ("public: %s\n", buf);
238 print_hex ("public(hex)", dh_pub1, sizeof *dh_pub1);
240 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv2, 32, buf, 128));
241 printf ("ECDHE key 2:\n");
242 printf ("private: %s\n", buf);
243 print_hex ("private(hex)", dh_priv2, sizeof *dh_priv2);
244 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub2, 32, buf, 128));
245 printf ("public: %s\n", buf);
246 print_hex ("public(hex)", dh_pub2, sizeof *dh_pub2);
248 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecc_ecdh (dh_priv1, dh_pub2, &hash));
249 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (&hash, 64, buf, 128));
250 printf ("ECDH shared secret: %s\n", buf);
252 GNUNET_free (dh_priv1);
253 GNUNET_free (dh_priv2);
254 GNUNET_free (dh_pub1);
255 GNUNET_free (dh_pub2);
260 * Print some random example operations to stdout.
265 print_examples_ecdh ();
266 // print_examples_ecdsa ();
267 // print_examples_eddsa ();
272 print_key (const char *filename)
274 struct GNUNET_DISK_FileHandle *fd;
275 struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
276 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
280 unsigned int total_hostkeys;
285 GNUNET_DISK_file_test (filename))
288 _("Hostkeys file `%s' not found\n"),
293 /* Check hostkey file size, read entire thing into memory */
295 GNUNET_DISK_file_size (filename,
303 _("Hostkeys file `%s' is empty\n"),
305 return; /* File is empty */
307 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
310 _("Incorrect hostkey file format: %s\n"),
314 fd = GNUNET_DISK_file_open (filename,
315 GNUNET_DISK_OPEN_READ,
316 GNUNET_DISK_PERM_NONE);
319 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
324 hostkeys_data = GNUNET_malloc (fs);
325 sret = GNUNET_DISK_file_read (fd,
329 (fs != (size_t) sret) )
332 _("Could not read hostkey file: %s\n"),
334 GNUNET_free (hostkeys_data);
335 GNUNET_DISK_file_close (fd);
338 GNUNET_DISK_file_close (fd);
340 if (NULL == hostkeys_data)
342 total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
343 for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
345 GNUNET_memcpy (&private_key,
346 hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE),
347 GNUNET_TESTING_HOSTKEYFILESIZE);
348 GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key);
349 hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key);
350 if (NULL != hostkey_str)
352 fprintf (stderr, "%4u: %s\n", c, hostkey_str);
353 GNUNET_free (hostkey_str);
356 fprintf (stderr, "%4u: %s\n", c, "invalid");
358 GNUNET_free (hostkeys_data);
363 * Main function that will be run by the scheduler.
365 * @param cls closure, NULL
366 * @param args remaining command-line arguments
367 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
368 * @param cfg configuration
374 const struct GNUNET_CONFIGURATION_Handle *cfg)
380 if (print_examples_flag)
389 _("No hostkey file specified on command line\n"));
399 create_keys (args[0], args[1]);
402 if (print_public_key || print_public_key_hex || print_private_key)
405 struct GNUNET_DISK_FileHandle *keyfile;
406 struct GNUNET_CRYPTO_EddsaPrivateKey pk;
407 struct GNUNET_CRYPTO_EddsaPublicKey pub;
409 keyfile = GNUNET_DISK_file_open (args[0], GNUNET_DISK_OPEN_READ,
410 GNUNET_DISK_PERM_NONE);
413 while (sizeof (pk) ==
414 GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk)))
416 GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub);
417 if (print_public_key_hex)
419 print_hex ("HEX:", &pub, sizeof (pub));
421 else if (print_public_key)
423 str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
424 FPRINTF (stdout, "%s\n", str);
427 else if (print_private_key)
429 str = GNUNET_CRYPTO_eddsa_private_key_to_string (&pk);
430 FPRINTF (stdout, "%s\n", str);
434 GNUNET_DISK_file_close (keyfile);
441 * Program to manipulate ECC key files.
443 * @param argc number of arguments from the command line
444 * @param argv command line arguments
445 * @return 0 ok, 1 on error
451 struct GNUNET_GETOPT_CommandLineOption options[] = {
452 GNUNET_GETOPT_option_flag ('i',
454 gettext_noop ("list keys included in a file (for testing)"),
456 GNUNET_GETOPT_option_uint ('e',
459 gettext_noop ("number of keys to list included in a file (for testing)"),
461 GNUNET_GETOPT_option_uint ('g',
464 gettext_noop ("create COUNT public-private key pairs (for testing)"),
466 GNUNET_GETOPT_option_flag ('p',
468 gettext_noop ("print the public key in ASCII format"),
470 GNUNET_GETOPT_option_flag ('P',
472 gettext_noop ("print the private key in ASCII format"),
474 GNUNET_GETOPT_option_flag ('x',
476 gettext_noop ("print the public key in HEX format"),
477 &print_public_key_hex),
478 GNUNET_GETOPT_option_flag ('E',
480 gettext_noop ("print examples of ECC operations (used for compatibility testing)"),
481 &print_examples_flag),
482 GNUNET_GETOPT_OPTION_END
486 list_keys_count = UINT32_MAX;
488 GNUNET_STRINGS_get_utf8_args (argc, argv,
493 GNUNET_PROGRAM_run (argc,
495 "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]",
496 gettext_noop ("Manipulate GNUnet private ECC key files"),
500 GNUNET_free ((void*) argv);
504 /* end of gnunet-ecc.c */