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 ECC key files
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
33 * Flag for printing public key.
35 static int print_public_key;
38 * Flag for printing hash of public key.
40 static int print_peer_identity;
43 * Option set to create a bunch of keys at once.
45 static unsigned int make_keys;
49 * Create a flat file with a large number of key pairs for testing.
52 create_keys (const char *fn)
55 struct GNUNET_CRYPTO_EccPrivateKey *pk;
57 if (NULL == (f = fopen (fn, "w+")))
60 _("Failed to open `%s': %s\n"),
66 _("Generating %u keys, please wait"),
68 while (0 < make_keys--)
72 if (NULL == (pk = GNUNET_CRYPTO_ecc_key_create ()))
77 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
79 GNUNET_TESTING_HOSTKEYFILESIZE, f))
82 _("\nFailed to write to `%s': %s\n"),
90 if (UINT_MAX == make_keys)
95 _("\nError, %u keys not generated\n"), make_keys);
101 * Main function that will be run by the scheduler.
104 * @param args remaining command-line arguments
105 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
106 * @param cfg configuration
109 run (void *cls, char *const *args, const char *cfgfile,
110 const struct GNUNET_CONFIGURATION_Handle *cfg)
112 struct GNUNET_CRYPTO_EccPrivateKey *pk;
113 struct GNUNET_CRYPTO_EccPublicSignKey pub;
114 struct GNUNET_PeerIdentity pid;
118 fprintf (stderr, _("No hostkey file specified on command line\n"));
123 create_keys (args[0]);
126 pk = GNUNET_CRYPTO_ecc_key_create_from_file (args[0]);
129 if (print_public_key)
133 GNUNET_CRYPTO_ecc_key_get_public_for_signature (pk, &pub);
134 s = GNUNET_CRYPTO_ecc_public_sign_key_to_string (&pub);
135 fprintf (stdout, "%s\n", s);
138 if (print_peer_identity)
140 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
142 GNUNET_CRYPTO_ecc_key_get_public_for_signature (pk, &pub);
143 GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
144 GNUNET_CRYPTO_hash_to_enc (&pid.hashPubKey, &enc);
145 fprintf (stdout, "%s\n", enc.encoding);
152 * Program to manipulate ECC key files.
154 * @param argc number of arguments from the command line
155 * @param argv command line arguments
156 * @return 0 ok, 1 on error
159 main (int argc, char *const *argv)
161 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
162 { 'g', "generate-keys", "COUNT",
163 gettext_noop ("create COUNT public-private key pairs (for testing)"),
164 1, &GNUNET_GETOPT_set_uint, &make_keys },
165 { 'p', "print-public-key", NULL,
166 gettext_noop ("print the public key in ASCII format"),
167 0, &GNUNET_GETOPT_set_one, &print_public_key },
168 { 'P', "print-peer-identity", NULL,
169 gettext_noop ("print the hash of the public key in ASCII format"),
170 0, &GNUNET_GETOPT_set_one, &print_peer_identity },
171 GNUNET_GETOPT_OPTION_END
175 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
179 GNUNET_PROGRAM_run (argc, argv, "gnunet-ecc [OPTIONS] keyfile",
180 gettext_noop ("Manipulate GNUnet private ECC key files"),
181 options, &run, NULL)) ? 0 : 1;
182 GNUNET_free ((void*) argv);
186 /* end of gnunet-ecc.c */