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