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"
33 * Flag for listing public key.
38 * Flag for listing public key.
40 static int list_keys_count;
43 * Flag for printing public key.
45 static int print_public_key;
48 * Flag for printing the output of random example operations.
50 static int print_examples_flag;
53 * Flag for printing hash of public key.
55 static int print_peer_identity;
58 * Option set to create a bunch of keys at once.
60 static unsigned int make_keys;
64 * Create a flat file with a large number of key pairs for testing.
67 create_keys (const char *fn)
70 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
72 if (NULL == (f = fopen (fn, "w+")))
75 _("Failed to open `%s': %s\n"),
81 _("Generating %u keys, please wait"),
83 while (0 < make_keys--)
87 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ()))
92 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
94 GNUNET_TESTING_HOSTKEYFILESIZE, f))
97 _("\nFailed to write to `%s': %s\n"),
105 if (UINT_MAX == make_keys)
110 _("\nError, %u keys not generated\n"), make_keys);
116 print_examples_ecdh ()
118 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv1;
119 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub1;
120 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv2;
121 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub2;
122 struct GNUNET_HashCode hash;
125 dh_pub1 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
126 dh_priv1 = GNUNET_CRYPTO_ecdhe_key_create ();
127 dh_pub2 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey);
128 dh_priv2 = GNUNET_CRYPTO_ecdhe_key_create ();
129 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv1, dh_pub1);
130 GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv2, dh_pub2);
132 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv1, 32, buf, 128));
133 printf ("ECDHE key 1:\n");
134 printf ("private: %s\n", buf);
135 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub1, 32, buf, 128));
136 printf ("public: %s\n", buf);
138 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_priv2, 32, buf, 128));
139 printf ("ECDHE key 2:\n");
140 printf ("private: %s\n", buf);
141 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub2, 32, buf, 128));
142 printf ("public: %s\n", buf);
144 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_ecc_ecdh (dh_priv1, dh_pub2, &hash));
145 GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (&hash, 64, buf, 128));
146 printf ("ECDH shared secret: %s\n", buf);
148 GNUNET_free (dh_priv1);
149 GNUNET_free (dh_priv2);
150 GNUNET_free (dh_pub1);
151 GNUNET_free (dh_pub2);
156 * Print some random example operations to stdout.
161 print_examples_ecdh ();
162 // print_examples_ecdsa ();
163 // print_examples_eddsa ();
168 print_key (const char *filename)
170 struct GNUNET_DISK_FileHandle *fd;
171 struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
172 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
176 unsigned int total_hostkeys;
179 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
182 _("Hostkeys file `%s' not found\n"),
187 /* Check hostkey file size, read entire thing into memory */
188 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
193 _("Hostkeys file `%s' is empty\n"),
195 return; /* File is empty */
197 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
200 _("Incorrect hostkey file format: %s\n"),
204 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
205 GNUNET_DISK_PERM_NONE);
208 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
211 hostkeys_data = GNUNET_malloc (fs);
212 if (fs != GNUNET_DISK_file_read (fd, hostkeys_data, fs))
215 _("Could not read hostkey file: %s\n"),
217 GNUNET_free (hostkeys_data);
218 GNUNET_DISK_file_close (fd);
221 GNUNET_DISK_file_close (fd);
223 if (NULL == hostkeys_data)
225 total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
226 for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
228 memcpy (&private_key,
229 hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE),
230 GNUNET_TESTING_HOSTKEYFILESIZE);
231 GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key);
232 hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key);
233 if (NULL != hostkey_str)
235 fprintf (stderr, "%4u: %s\n", c, hostkey_str);
236 GNUNET_free (hostkey_str);
239 fprintf (stderr, "%4u: %s\n", c, "invalid");
241 GNUNET_free (hostkeys_data);
246 * Main function that will be run by the scheduler.
249 * @param args remaining command-line arguments
250 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
251 * @param cfg configuration
254 run (void *cls, char *const *args, const char *cfgfile,
255 const struct GNUNET_CONFIGURATION_Handle *cfg)
257 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
258 struct GNUNET_CRYPTO_EddsaPublicKey pub;
260 if (print_examples_flag)
269 _("No hostkey file specified on command line\n"));
279 create_keys (args[0]);
282 pk = GNUNET_CRYPTO_eddsa_key_create_from_file (args[0]);
285 if (print_public_key)
289 GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
290 s = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
296 if (print_peer_identity)
300 GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
301 str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
312 * Program to manipulate ECC key files.
314 * @param argc number of arguments from the command line
315 * @param argv command line arguments
316 * @return 0 ok, 1 on error
319 main (int argc, char *const *argv)
321 list_keys_count = UINT32_MAX;
322 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
323 { 'i', "iterate", "FILE",
324 gettext_noop ("list keys included in a file (for testing)"),
325 0, &GNUNET_GETOPT_set_one, &list_keys },
326 { 'e', "end=", "COUNT",
327 gettext_noop ("number of keys to list included in a file (for testing)"),
328 1, &GNUNET_GETOPT_set_uint, &list_keys_count },
329 { 'g', "generate-keys", "COUNT",
330 gettext_noop ("create COUNT public-private key pairs (for testing)"),
331 1, &GNUNET_GETOPT_set_uint, &make_keys },
332 { 'p', "print-public-key", NULL,
333 gettext_noop ("print the public key in ASCII format"),
334 0, &GNUNET_GETOPT_set_one, &print_public_key },
335 { 'P', "print-peer-identity", NULL,
336 gettext_noop ("print the hash of the public key in ASCII format"),
337 0, &GNUNET_GETOPT_set_one, &print_peer_identity },
338 { 'E', "examples", NULL,
339 gettext_noop ("print examples of ECC operations (used for compatibility testing)"),
340 0, &GNUNET_GETOPT_set_one, &print_examples_flag },
341 GNUNET_GETOPT_OPTION_END
345 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
349 GNUNET_PROGRAM_run (argc, argv, "gnunet-ecc [OPTIONS] keyfile",
350 gettext_noop ("Manipulate GNUnet private ECC key files"),
351 options, &run, NULL)) ? 0 : 1;
352 GNUNET_free ((void*) argv);
356 /* end of gnunet-ecc.c */