- test for external iterator
[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  * Flag for printing short hash of public key.
44  */
45 static int print_short_identity;
46
47 /**
48  * Option set to create a bunch of keys at once.
49  */
50 static unsigned int make_keys;
51
52
53 /**
54  * Create a flat file with a large number of key pairs for testing.
55  */
56 static void
57 create_keys (const char *fn)
58 {
59   FILE *f;
60   struct GNUNET_CRYPTO_EccPrivateKey *pk;
61
62   if (NULL == (f = fopen (fn, "w+")))
63   {
64     fprintf (stderr,
65              _("Failed to open `%s': %s\n"),
66              fn,
67              STRERROR (errno));
68     return;
69   }
70   fprintf (stderr,
71            _("Generating %u keys, please wait"),
72            make_keys);
73   while (0 < make_keys--)
74   {    
75     fprintf (stderr,
76              ".");
77     if (NULL == (pk = GNUNET_CRYPTO_ecc_key_create ()))
78     {
79        GNUNET_break (0);
80        break;
81     }
82     if (GNUNET_TESTING_HOSTKEYFILESIZE != 
83         fwrite (pk, 1,
84                 GNUNET_TESTING_HOSTKEYFILESIZE, f))
85     {
86       fprintf (stderr,
87                _("\nFailed to write to `%s': %s\n"),
88                fn,
89                STRERROR (errno));
90       GNUNET_CRYPTO_ecc_key_free (pk);
91       break;
92     }
93     GNUNET_CRYPTO_ecc_key_free (pk);
94   }
95   if (UINT_MAX == make_keys)
96     fprintf (stderr,
97              _("\nFinished!\n"));
98   else
99     fprintf (stderr,
100              _("\nError, %u keys not generated\n"), make_keys);
101   fclose (f);
102 }
103
104
105 /**
106  * Main function that will be run by the scheduler.
107  *
108  * @param cls closure
109  * @param args remaining command-line arguments
110  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
111  * @param cfg configuration
112  */
113 static void
114 run (void *cls, char *const *args, const char *cfgfile,
115      const struct GNUNET_CONFIGURATION_Handle *cfg)
116 {
117   struct GNUNET_CRYPTO_EccPrivateKey *pk;
118   struct GNUNET_CRYPTO_EccPublicKey pub;
119   struct GNUNET_PeerIdentity pid;
120
121   if (NULL == args[0])
122   {
123     fprintf (stderr, _("No hostkey file specified on command line\n"));
124     return;
125   }
126   if (make_keys > 0)
127   {
128     create_keys (args[0]);
129     return;
130   }
131   pk = GNUNET_CRYPTO_ecc_key_create_from_file (args[0]);
132   if (NULL == pk)
133     return;
134   if (print_public_key)
135   {
136     char *s;
137
138     GNUNET_CRYPTO_ecc_key_get_public (pk, &pub);
139     s = GNUNET_CRYPTO_ecc_public_key_to_string (&pub);
140     fprintf (stdout, "%s\n", s);
141     GNUNET_free (s);
142   }
143   if (print_peer_identity)
144   {
145     struct GNUNET_CRYPTO_HashAsciiEncoded enc;
146
147     GNUNET_CRYPTO_ecc_key_get_public (pk, &pub);
148     GNUNET_CRYPTO_hash (&pub, sizeof (pub), &pid.hashPubKey);
149     GNUNET_CRYPTO_hash_to_enc (&pid.hashPubKey, &enc);
150     fprintf (stdout, "%s\n", enc.encoding);
151   }
152   if (print_short_identity)
153   {
154     struct GNUNET_CRYPTO_ShortHashAsciiEncoded enc;
155     struct GNUNET_CRYPTO_ShortHashCode sh;
156
157     GNUNET_CRYPTO_ecc_key_get_public (pk, &pub);
158     GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &sh);
159     GNUNET_CRYPTO_short_hash_to_enc (&sh, &enc);
160     fprintf (stdout, "%s\n", enc.short_encoding);
161   }
162   GNUNET_CRYPTO_ecc_key_free (pk);
163 }
164
165
166 /**
167  * Program to manipulate ECC key files.
168  *
169  * @param argc number of arguments from the command line
170  * @param argv command line arguments
171  * @return 0 ok, 1 on error
172  */
173 int
174 main (int argc, char *const *argv)
175 {
176   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
177     { 'g', "generate-keys", "COUNT",
178       gettext_noop ("create COUNT public-private key pairs (for testing)"),
179       1, &GNUNET_GETOPT_set_uint, &make_keys },
180     { 'p', "print-public-key", NULL,
181       gettext_noop ("print the public key in ASCII format"),
182       0, &GNUNET_GETOPT_set_one, &print_public_key },
183     { 'P', "print-peer-identity", NULL,
184       gettext_noop ("print the hash of the public key in ASCII format"),
185       0, &GNUNET_GETOPT_set_one, &print_peer_identity },
186     { 's', "print-short-identity", NULL,
187       gettext_noop ("print the short hash of the public key in ASCII format"),
188       0, &GNUNET_GETOPT_set_one, &print_short_identity },
189     GNUNET_GETOPT_OPTION_END
190   };
191   int ret;
192
193   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
194     return 2;
195
196   ret = (GNUNET_OK ==
197          GNUNET_PROGRAM_run (argc, argv, "gnunet-ecc [OPTIONS] keyfile",
198                              gettext_noop ("Manipulate GNUnet private ECC key files"),
199                              options, &run, NULL)) ? 0 : 1;
200   GNUNET_free ((void*) argv);
201   return ret;
202 }
203
204 /* end of gnunet-ecc.c */