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];
79 if (NULL == (f = fopen (fn, "w+")))
81 fprintf (stderr, _("Failed to open `%s': %s\n"), fn, STRERROR (errno));
86 strncpy (vanity, prefix, KEY_STR_LEN);
87 len = GNUNET_MIN (strlen (prefix), KEY_STR_LEN);
91 memset (&vanity[len], '0', KEY_STR_LEN - len);
92 vanity[KEY_STR_LEN] = '\0';
93 GNUNET_assert (GNUNET_OK ==
94 GNUNET_CRYPTO_eddsa_public_key_from_string (vanity,
100 * Documentation by example:
103 * n = 5/8 = 0 (bytes)
104 * rest = 5%8 = 5 (bits)
105 * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000
107 mask = ~ ((int)pow (2, 8 - rest) - 1);
108 target_byte = ((unsigned char *) &target_pub)[n] & mask;
112 /* Just so old (debian) versions of GCC calm down with the warnings. */
113 mask = target_byte = 0;
115 s = GNUNET_CRYPTO_eddsa_public_key_to_string (&target_pub);
117 _("Generating %u keys like %s, please wait"),
122 "\nattempt %s [%d, %X]\n",
130 _("Generating %u keys, please wait"),
132 /* Just so old (debian) versions of GCC calm down with the warnings. */
133 n = rest = target_byte = mask = 0;
136 while (0 < make_keys--)
138 fprintf (stderr, ".");
139 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ()))
146 struct GNUNET_CRYPTO_EddsaPublicKey newkey;
148 GNUNET_CRYPTO_eddsa_key_get_public (pk, &newkey);
149 if (0 != memcmp (&target_pub, &newkey, n))
156 unsigned char new_byte;
158 new_byte = ((unsigned char *) &newkey)[n] & mask;
159 if (target_byte != new_byte)
166 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
168 GNUNET_TESTING_HOSTKEYFILESIZE, f))
171 _("\nFailed to write to `%s': %s\n"),
179 if (UINT_MAX == make_keys)
184 _("\nError, %u keys not generated\n"),
191 print_hex (const char *msg,
197 printf ("%s: ", msg);
198 for (i = 0; i < size; i++)
200 printf ("%02hhx", ((const char *)buf)[i]);
207 print_examples_ecdh ()
209 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv1;
210 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub1;
211 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv2;
212 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub2;
213 struct GNUNET_HashCode hash;
216 dh_pub1 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
217 dh_priv1 = GNUNET_CRYPTO_ecdhe_key_create ();
218 dh_pub2 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
219 dh_priv2 = GNUNET_CRYPTO_ecdhe_key_create ();
220 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv1, dh_pub1);
221 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv2, dh_pub2);
223 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv1, 32, buf, 128));
224 printf ("ECDHE key 1:\n");
225 printf ("private: %s\n", buf);
226 print_hex ("private(hex)", dh_priv1, sizeof *dh_priv1);
227 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub1, 32, buf, 128));
228 printf ("public: %s\n", buf);
229 print_hex ("public(hex)", dh_pub1, sizeof *dh_pub1);
231 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv2, 32, buf, 128));
232 printf ("ECDHE key 2:\n");
233 printf ("private: %s\n", buf);
234 print_hex ("private(hex)", dh_priv2, sizeof *dh_priv2);
235 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub2, 32, buf, 128));
236 printf ("public: %s\n", buf);
237 print_hex ("public(hex)", dh_pub2, sizeof *dh_pub2);
239 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecc_ecdh (dh_priv1, dh_pub2, &hash));
240 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (&hash, 64, buf, 128));
241 printf ("ECDH shared secret: %s\n", buf);
243 GNUNET_free (dh_priv1);
244 GNUNET_free (dh_priv2);
245 GNUNET_free (dh_pub1);
246 GNUNET_free (dh_pub2);
251 * Print some random example operations to stdout.
256 print_examples_ecdh ();
257 // print_examples_ecdsa ();
258 // print_examples_eddsa ();
263 print_key (const char *filename)
265 struct GNUNET_DISK_FileHandle *fd;
266 struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
267 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
271 unsigned int total_hostkeys;
274 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
277 _("Hostkeys file `%s' not found\n"),
282 /* Check hostkey file size, read entire thing into memory */
283 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
288 _("Hostkeys file `%s' is empty\n"),
290 return; /* File is empty */
292 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
295 _("Incorrect hostkey file format: %s\n"),
299 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
300 GNUNET_DISK_PERM_NONE);
303 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
306 hostkeys_data = GNUNET_malloc (fs);
307 if (fs != GNUNET_DISK_file_read (fd, hostkeys_data, fs))
310 _("Could not read hostkey file: %s\n"),
312 GNUNET_free (hostkeys_data);
313 GNUNET_DISK_file_close (fd);
316 GNUNET_DISK_file_close (fd);
318 if (NULL == hostkeys_data)
320 total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
321 for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
323 memcpy (&private_key,
324 hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE),
325 GNUNET_TESTING_HOSTKEYFILESIZE);
326 GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key);
327 hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key);
328 if (NULL != hostkey_str)
330 fprintf (stderr, "%4u: %s\n", c, hostkey_str);
331 GNUNET_free (hostkey_str);
334 fprintf (stderr, "%4u: %s\n", c, "invalid");
336 GNUNET_free (hostkeys_data);
341 * Main function that will be run by the scheduler.
344 * @param args remaining command-line arguments
345 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
346 * @param cfg configuration
349 run (void *cls, char *const *args, const char *cfgfile,
350 const struct GNUNET_CONFIGURATION_Handle *cfg)
352 if (print_examples_flag)
361 _("No hostkey file specified on command line\n"));
371 create_keys (args[0], args[1]);
374 if (print_public_key)
377 struct GNUNET_DISK_FileHandle *keyfile;
378 struct GNUNET_CRYPTO_EddsaPrivateKey pk;
379 struct GNUNET_CRYPTO_EddsaPublicKey pub;
381 keyfile = GNUNET_DISK_file_open (args[0], GNUNET_DISK_OPEN_READ,
382 GNUNET_DISK_PERM_NONE);
385 while (sizeof (pk) == GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk)))
387 GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub);
388 str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
389 FPRINTF (stdout, "%s\n", str);
392 GNUNET_DISK_file_close (keyfile);
399 * Program to manipulate ECC key files.
401 * @param argc number of arguments from the command line
402 * @param argv command line arguments
403 * @return 0 ok, 1 on error
406 main (int argc, char *const *argv)
408 list_keys_count = UINT32_MAX;
409 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
410 { 'i', "iterate", "FILE",
411 gettext_noop ("list keys included in a file (for testing)"),
412 0, &GNUNET_GETOPT_set_one, &list_keys },
413 { 'e', "end=", "COUNT",
414 gettext_noop ("number of keys to list included in a file (for testing)"),
415 1, &GNUNET_GETOPT_set_uint, &list_keys_count },
416 { 'g', "generate-keys", "COUNT",
417 gettext_noop ("create COUNT public-private key pairs (for testing)"),
418 1, &GNUNET_GETOPT_set_uint, &make_keys },
419 { 'p', "print-public-key", NULL,
420 gettext_noop ("print the public key in ASCII format"),
421 0, &GNUNET_GETOPT_set_one, &print_public_key },
422 { 'E', "examples", NULL,
423 gettext_noop ("print examples of ECC operations (used for compatibility testing)"),
424 0, &GNUNET_GETOPT_set_one, &print_examples_flag },
425 GNUNET_GETOPT_OPTION_END
429 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
433 GNUNET_PROGRAM_run (argc, argv, "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]",
434 gettext_noop ("Manipulate GNUnet private ECC key files"),
435 options, &run, NULL)) ? 0 : 1;
436 GNUNET_free ((void*) argv);
440 /* end of gnunet-ecc.c */