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 hash of public key.
50 static int print_peer_identity;
53 * Option set to create a bunch of keys at once.
55 static unsigned int make_keys;
59 * Create a flat file with a large number of key pairs for testing.
62 create_keys (const char *fn)
65 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
67 if (NULL == (f = fopen (fn, "w+")))
70 _("Failed to open `%s': %s\n"),
76 _("Generating %u keys, please wait"),
78 while (0 < make_keys--)
82 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ()))
87 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
89 GNUNET_TESTING_HOSTKEYFILESIZE, f))
92 _("\nFailed to write to `%s': %s\n"),
100 if (UINT_MAX == make_keys)
105 _("\nError, %u keys not generated\n"), make_keys);
111 print_key (const char *filename)
113 struct GNUNET_DISK_FileHandle *fd;
114 struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
115 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
119 unsigned int total_hostkeys;
122 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
124 fprintf (stderr, _("Hostkeys file not found: %s\n"), filename);
128 /* Check hostkey file size, read entire thing into memory */
129 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
133 fprintf (stderr, _("Hostkeys file is empty: %s\n"), filename);
134 return; /* File is empty */
136 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
139 _("Incorrect hostkey file format: %s\n"), filename);
142 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
143 GNUNET_DISK_PERM_NONE);
146 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
149 hostkeys_data = GNUNET_malloc (fs);
150 if (fs != GNUNET_DISK_file_read(fd, hostkeys_data, fs))
153 _("Could not readk hostkey file: %s\n"), filename);
154 GNUNET_free (hostkeys_data);
157 GNUNET_DISK_file_close (fd);
159 if (NULL == hostkeys_data)
161 total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
162 for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
164 memcpy (&private_key,
165 hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE),
166 GNUNET_TESTING_HOSTKEYFILESIZE);
167 GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key);
168 hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key);
169 if (NULL != hostkey_str)
171 fprintf (stderr, "%4u: %s\n", c, hostkey_str);
172 GNUNET_free (hostkey_str);
175 fprintf (stderr, "%4u: %s\n", c, "invalid");
177 GNUNET_free (hostkeys_data);
182 * Main function that will be run by the scheduler.
185 * @param args remaining command-line arguments
186 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
187 * @param cfg configuration
190 run (void *cls, char *const *args, const char *cfgfile,
191 const struct GNUNET_CONFIGURATION_Handle *cfg)
193 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
194 struct GNUNET_CRYPTO_EddsaPublicKey pub;
200 _("No hostkey file specified on command line\n"));
210 create_keys (args[0]);
213 pk = GNUNET_CRYPTO_eddsa_key_create_from_file (args[0]);
216 if (print_public_key)
220 GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
221 s = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
227 if (print_peer_identity)
231 GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
232 str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
243 * Program to manipulate ECC key files.
245 * @param argc number of arguments from the command line
246 * @param argv command line arguments
247 * @return 0 ok, 1 on error
250 main (int argc, char *const *argv)
252 list_keys_count = UINT32_MAX;
253 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
254 { 'i', "iterate", "FILE",
255 gettext_noop ("list keys included in a file (for testing)"),
256 0, &GNUNET_GETOPT_set_one, &list_keys },
257 { 'e', "end=", "COUNT",
258 gettext_noop ("number of keys to list included in a file (for testing)"),
259 1, &GNUNET_GETOPT_set_uint, &list_keys_count },
260 { 'g', "generate-keys", "COUNT",
261 gettext_noop ("create COUNT public-private key pairs (for testing)"),
262 1, &GNUNET_GETOPT_set_uint, &make_keys },
263 { 'p', "print-public-key", NULL,
264 gettext_noop ("print the public key in ASCII format"),
265 0, &GNUNET_GETOPT_set_one, &print_public_key },
266 { 'P', "print-peer-identity", NULL,
267 gettext_noop ("print the hash of the public key in ASCII format"),
268 0, &GNUNET_GETOPT_set_one, &print_peer_identity },
269 GNUNET_GETOPT_OPTION_END
273 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
277 GNUNET_PROGRAM_run (argc, argv, "gnunet-ecc [OPTIONS] keyfile",
278 gettext_noop ("Manipulate GNUnet private ECC key files"),
279 options, &run, NULL)) ? 0 : 1;
280 GNUNET_free ((void*) argv);
284 /* end of gnunet-ecc.c */