Update plibc header
[oweals/gnunet.git] / src / util / gnunet-ecc.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012, 2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file util/gnunet-ecc.c
23  * @brief tool to manipulate ECC key files
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include <gcrypt.h>
30
31
32 /**
33  * Flag for printing public key.
34  */
35 static int print_public_key;
36
37 /**
38  * Flag for printing hash of public key.
39  */
40 static int print_peer_identity;
41
42 /**
43  * Option set to create a bunch of keys at once.
44  */
45 static unsigned int make_keys;
46
47
48 /**
49  * Create a flat file with a large number of key pairs for testing.
50  */
51 static void
52 create_keys (const char *fn)
53 {
54   FILE *f;
55   struct GNUNET_CRYPTO_EccPrivateKey *pk;
56
57   if (NULL == (f = fopen (fn, "w+")))
58   {
59     fprintf (stderr,
60              _("Failed to open `%s': %s\n"),
61              fn,
62              STRERROR (errno));
63     return;
64   }
65   fprintf (stderr,
66            _("Generating %u keys, please wait"),
67            make_keys);
68   while (0 < make_keys--)
69   {
70     fprintf (stderr,
71              ".");
72     if (NULL == (pk = GNUNET_CRYPTO_ecc_key_create ()))
73     {
74        GNUNET_break (0);
75        break;
76     }
77     if (GNUNET_TESTING_HOSTKEYFILESIZE !=
78         fwrite (pk, 1,
79                 GNUNET_TESTING_HOSTKEYFILESIZE, f))
80     {
81       fprintf (stderr,
82                _("\nFailed to write to `%s': %s\n"),
83                fn,
84                STRERROR (errno));
85       GNUNET_free (pk);
86       break;
87     }
88     GNUNET_free (pk);
89   }
90   if (UINT_MAX == make_keys)
91     fprintf (stderr,
92              _("\nFinished!\n"));
93   else
94     fprintf (stderr,
95              _("\nError, %u keys not generated\n"), make_keys);
96   fclose (f);
97 }
98
99
100 /**
101  * Main function that will be run by the scheduler.
102  *
103  * @param cls closure
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
107  */
108 static void
109 run (void *cls, char *const *args, const char *cfgfile,
110      const struct GNUNET_CONFIGURATION_Handle *cfg)
111 {
112   struct GNUNET_CRYPTO_EccPrivateKey *pk;
113   struct GNUNET_CRYPTO_EccPublicSignKey pub;
114
115   if (NULL == args[0])
116   {
117     fprintf (stderr, _("No hostkey file specified on command line\n"));
118     return;
119   }
120   if (make_keys > 0)
121   {
122     create_keys (args[0]);
123     return;
124   }
125   pk = GNUNET_CRYPTO_ecc_key_create_from_file (args[0]);
126   if (NULL == pk)
127     return;
128   if (print_public_key)
129   {
130     char *s;
131
132     GNUNET_CRYPTO_ecc_key_get_public_for_signature (pk, &pub);
133     s = GNUNET_CRYPTO_ecc_public_sign_key_to_string (&pub);
134     fprintf (stdout, "%s\n", s);
135     GNUNET_free (s);
136   }
137   if (print_peer_identity)
138   {
139     char *str;
140
141     GNUNET_CRYPTO_ecc_key_get_public_for_signature (pk, &pub);
142     str = GNUNET_CRYPTO_ecc_public_sign_key_to_string (&pub);
143     fprintf (stdout,
144              "%s\n",
145              str);
146     GNUNET_free (str);
147   }
148   GNUNET_free (pk);
149 }
150
151
152 /**
153  * Program to manipulate ECC key files.
154  *
155  * @param argc number of arguments from the command line
156  * @param argv command line arguments
157  * @return 0 ok, 1 on error
158  */
159 int
160 main (int argc, char *const *argv)
161 {
162   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
163     { 'g', "generate-keys", "COUNT",
164       gettext_noop ("create COUNT public-private key pairs (for testing)"),
165       1, &GNUNET_GETOPT_set_uint, &make_keys },
166     { 'p', "print-public-key", NULL,
167       gettext_noop ("print the public key in ASCII format"),
168       0, &GNUNET_GETOPT_set_one, &print_public_key },
169     { 'P', "print-peer-identity", NULL,
170       gettext_noop ("print the hash of the public key in ASCII format"),
171       0, &GNUNET_GETOPT_set_one, &print_peer_identity },
172     GNUNET_GETOPT_OPTION_END
173   };
174   int ret;
175
176   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
177     return 2;
178
179   ret = (GNUNET_OK ==
180          GNUNET_PROGRAM_run (argc, argv, "gnunet-ecc [OPTIONS] keyfile",
181                              gettext_noop ("Manipulate GNUnet private ECC key files"),
182                              options, &run, NULL)) ? 0 : 1;
183   GNUNET_free ((void*) argv);
184   return ret;
185 }
186
187 /* end of gnunet-ecc.c */