- FIX: GNUNET_SET_STATUS_HALF_DONE is never called only GNUNET_SET_STATUS_DONE
[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 EDDSA 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 listing public key.
34  */
35 static int list_keys;
36
37 /**
38  * Flag for listing public key.
39  */
40 static int list_keys_count;
41
42 /**
43  * Flag for printing public key.
44  */
45 static int print_public_key;
46
47 /**
48  * Flag for printing the output of random example operations.
49  */
50 static int print_examples_flag;
51
52 /**
53  * Flag for printing hash of public key.
54  */
55 static int print_peer_identity;
56
57 /**
58  * Option set to create a bunch of keys at once.
59  */
60 static unsigned int make_keys;
61
62
63 /**
64  * Create a flat file with a large number of key pairs for testing.
65  */
66 static void
67 create_keys (const char *fn)
68 {
69   FILE *f;
70   struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
71
72   if (NULL == (f = fopen (fn, "w+")))
73   {
74     fprintf (stderr,
75              _("Failed to open `%s': %s\n"),
76              fn,
77              STRERROR (errno));
78     return;
79   }
80   fprintf (stderr,
81            _("Generating %u keys, please wait"),
82            make_keys);
83   while (0 < make_keys--)
84   {
85     fprintf (stderr,
86              ".");
87     if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ()))
88     {
89        GNUNET_break (0);
90        break;
91     }
92     if (GNUNET_TESTING_HOSTKEYFILESIZE !=
93         fwrite (pk, 1,
94                 GNUNET_TESTING_HOSTKEYFILESIZE, f))
95     {
96       fprintf (stderr,
97                _("\nFailed to write to `%s': %s\n"),
98                fn,
99                STRERROR (errno));
100       GNUNET_free (pk);
101       break;
102     }
103     GNUNET_free (pk);
104   }
105   if (UINT_MAX == make_keys)
106     fprintf (stderr,
107              _("\nFinished!\n"));
108   else
109     fprintf (stderr,
110              _("\nError, %u keys not generated\n"), make_keys);
111   fclose (f);
112 }
113
114
115 static void
116 print_examples_ecdh (void)
117 {
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;
123   char buf[128];
124
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);
131
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);
137
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);
143
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);
147
148   GNUNET_free (dh_priv1);
149   GNUNET_free (dh_priv2);
150   GNUNET_free (dh_pub1);
151   GNUNET_free (dh_pub2);
152 }
153
154
155 /**
156  * Print some random example operations to stdout.
157  */
158 static void
159 print_examples (void)
160 {
161   print_examples_ecdh ();
162   // print_examples_ecdsa ();
163   // print_examples_eddsa ();
164 }
165
166
167 static void
168 print_key (const char *filename)
169 {
170   struct GNUNET_DISK_FileHandle *fd;
171   struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
172   struct GNUNET_CRYPTO_EddsaPublicKey public_key;
173   char *hostkeys_data;
174   char *hostkey_str;
175   uint64_t fs;
176   unsigned int total_hostkeys;
177   unsigned int c;
178
179   if (GNUNET_YES != GNUNET_DISK_file_test (filename))
180   {
181     fprintf (stderr, _("Hostkeys file not found: %s\n"), filename);
182     return;
183   }
184
185   /* Check hostkey file size, read entire thing into memory */
186   if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
187     fs = 0;
188   if (0 == fs)
189   {
190     fprintf (stderr, _("Hostkeys file is empty: %s\n"), filename);
191     return;       /* File is empty */
192   }
193   if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
194   {
195     fprintf (stderr,
196          _("Incorrect hostkey file format: %s\n"), filename);
197     return;
198   }
199   fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
200                                          GNUNET_DISK_PERM_NONE);
201   if (NULL == fd)
202   {
203     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename);
204     return;
205   }
206   hostkeys_data = GNUNET_malloc (fs);
207   if (fs != GNUNET_DISK_file_read(fd, hostkeys_data, fs))
208   {
209     fprintf (stderr,
210          _("Could not readk hostkey file: %s\n"), filename);
211     GNUNET_free (hostkeys_data);
212     return;
213   }
214   GNUNET_DISK_file_close (fd);
215
216   if (NULL == hostkeys_data)
217     return;
218   total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
219   for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
220   {
221     memcpy (&private_key,
222             hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE),
223             GNUNET_TESTING_HOSTKEYFILESIZE);
224     GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key);
225     hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key);
226     if (NULL != hostkey_str)
227     {
228       fprintf (stderr, "%4u: %s\n", c, hostkey_str);
229       GNUNET_free (hostkey_str);
230     }
231     else
232       fprintf (stderr, "%4u: %s\n", c, "invalid");
233   }
234   GNUNET_free (hostkeys_data);
235 }
236
237
238 /**
239  * Main function that will be run by the scheduler.
240  *
241  * @param cls closure
242  * @param args remaining command-line arguments
243  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
244  * @param cfg configuration
245  */
246 static void
247 run (void *cls, char *const *args, const char *cfgfile,
248      const struct GNUNET_CONFIGURATION_Handle *cfg)
249 {
250   struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
251   struct GNUNET_CRYPTO_EddsaPublicKey pub;
252
253   if (print_examples_flag)
254   {
255     print_examples ();
256     return;
257   }
258   if (NULL == args[0])
259   {
260     FPRINTF (stderr,
261              "%s",
262              _("No hostkey file specified on command line\n"));
263     return;
264   }
265   if (list_keys)
266   {
267     print_key (args[0]);
268     return;
269   }
270   if (make_keys > 0)
271   {
272     create_keys (args[0]);
273     return;
274   }
275   pk = GNUNET_CRYPTO_eddsa_key_create_from_file (args[0]);
276   if (NULL == pk)
277     return;
278   if (print_public_key)
279   {
280     char *s;
281
282     GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
283     s = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
284     FPRINTF (stdout,
285              "%s\n",
286              s);
287     GNUNET_free (s);
288   }
289   if (print_peer_identity)
290   {
291     char *str;
292
293     GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
294     str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
295     FPRINTF (stdout,
296              "%s\n",
297              str);
298     GNUNET_free (str);
299   }
300   GNUNET_free (pk);
301 }
302
303
304 /**
305  * Program to manipulate ECC key files.
306  *
307  * @param argc number of arguments from the command line
308  * @param argv command line arguments
309  * @return 0 ok, 1 on error
310  */
311 int
312 main (int argc, char *const *argv)
313 {
314   list_keys_count = UINT32_MAX;
315   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
316     { 'i', "iterate", "FILE",
317       gettext_noop ("list keys included in a file (for testing)"),
318       0, &GNUNET_GETOPT_set_one, &list_keys },
319     { 'e', "end=", "COUNT",
320       gettext_noop ("number of keys to list included in a file (for testing)"),
321       1, &GNUNET_GETOPT_set_uint, &list_keys_count },
322     { 'g', "generate-keys", "COUNT",
323       gettext_noop ("create COUNT public-private key pairs (for testing)"),
324       1, &GNUNET_GETOPT_set_uint, &make_keys },
325     { 'p', "print-public-key", NULL,
326       gettext_noop ("print the public key in ASCII format"),
327       0, &GNUNET_GETOPT_set_one, &print_public_key },
328     { 'P', "print-peer-identity", NULL,
329       gettext_noop ("print the hash of the public key in ASCII format"),
330       0, &GNUNET_GETOPT_set_one, &print_peer_identity },
331     { 'E', "examples", NULL,
332       gettext_noop ("print examples of ECC operations (used for compatibility testing)"),
333       0, &GNUNET_GETOPT_set_one, &print_examples_flag },
334     GNUNET_GETOPT_OPTION_END
335   };
336   int ret;
337
338   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
339     return 2;
340
341   ret = (GNUNET_OK ==
342          GNUNET_PROGRAM_run (argc, argv, "gnunet-ecc [OPTIONS] keyfile",
343                              gettext_noop ("Manipulate GNUnet private ECC key files"),
344                              options, &run, NULL)) ? 0 : 1;
345   GNUNET_free ((void*) argv);
346   return ret;
347 }
348
349 /* end of gnunet-ecc.c */