GNUnet  0.11.x
gnunet-ecc.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2012, 2013 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your 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  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
29 #include <gcrypt.h>
30 
34 #define KEY_STR_LEN sizeof(struct GNUNET_CRYPTO_EddsaPublicKey) * 8 / 5 + 1
35 
39 static int list_keys;
40 
44 static unsigned int list_keys_count;
45 
49 static int print_public_key;
50 
54 static int print_private_key;
55 
60 
65 
69 static unsigned int make_keys;
70 
71 
78 static void
79 create_keys (const char *fn, const char *prefix)
80 {
81  FILE *f;
83  struct GNUNET_CRYPTO_EddsaPublicKey target_pub;
84  static char vanity[KEY_STR_LEN + 1];
85  size_t len;
86  size_t n;
87  size_t rest;
88  unsigned char mask;
89  unsigned target_byte;
90  char *s;
91 
92  if (NULL == (f = fopen (fn, "w+")))
93  {
94  fprintf (stderr, _ ("Failed to open `%s': %s\n"), fn, strerror (errno));
95  return;
96  }
97  if (NULL != prefix)
98  {
99  len = GNUNET_strlcpy (vanity, prefix, sizeof(vanity));
100  n = len * 5 / 8;
101  rest = len * 5 % 8;
102 
103  memset (&vanity[len], '0', KEY_STR_LEN - len);
104  vanity[KEY_STR_LEN] = '\0';
107  KEY_STR_LEN,
108  &target_pub));
109  if (0 != rest)
110  {mask = ~((int) pow (2, 8 - rest) - 1);
119  target_byte = ((unsigned char *) &target_pub)[n] & mask;
120  }
121  else
122  {
123  /* Just so old (debian) versions of GCC calm down with the warnings. */
124  mask = target_byte = 0;
125  }
127  fprintf (stderr,
128  _ ("Generating %u keys like %s, please wait"),
129  make_keys,
130  s);
131  GNUNET_free (s);
132  fprintf (stderr, "\nattempt %s [%u, %X]\n", vanity, (unsigned int) n, mask);
133  }
134  else
135  {
136  fprintf (stderr, _ ("Generating %u keys, please wait"), make_keys);
137  /* Just so old (debian) versions of GCC calm down with the warnings. */
138  n = rest = target_byte = mask = 0;
139  }
140 
141  while (0 < make_keys--)
142  {
143  fprintf (stderr, ".");
145  if (NULL != prefix)
146  {
147  struct GNUNET_CRYPTO_EddsaPublicKey newkey;
148 
150  &newkey);
151  if (0 != memcmp (&target_pub,
152  &newkey,
153  n))
154  {
155  make_keys++;
156  continue;
157  }
158  if (0 != rest)
159  {
160  unsigned char new_byte;
161 
162  new_byte = ((unsigned char *) &newkey)[n] & mask;
163  if (target_byte != new_byte)
164  {
165  make_keys++;
166  continue;
167  }
168  }
169  }
171  fwrite (&pk,
172  1,
174  f))
175  {
176  fprintf (stderr,
177  _ ("\nFailed to write to `%s': %s\n"),
178  fn,
179  strerror (errno));
180  break;
181  }
182  }
183  if (UINT_MAX == make_keys)
184  fprintf (stderr, _ ("\nFinished!\n"));
185  else
186  fprintf (stderr, _ ("\nError, %u keys not generated\n"), make_keys);
187  fclose (f);
188 }
189 
190 
191 static void
192 print_hex (const char *msg, const void *buf, size_t size)
193 {
194  printf ("%s: ", msg);
195  for (size_t i = 0; i < size; i++)
196  {
197  printf ("%02hhx", ((const uint8_t *) buf)[i]);
198  }
199  printf ("\n");
200 }
201 
202 
203 static void
205 {
206  struct GNUNET_CRYPTO_EcdhePrivateKey dh_priv1;
207  struct GNUNET_CRYPTO_EcdhePublicKey dh_pub1;
208  struct GNUNET_CRYPTO_EcdhePrivateKey dh_priv2;
209  struct GNUNET_CRYPTO_EcdhePublicKey dh_pub2;
210  struct GNUNET_HashCode hash;
211  char buf[128];
212 
213  GNUNET_CRYPTO_ecdhe_key_create (&dh_priv1);
214  GNUNET_CRYPTO_ecdhe_key_create (&dh_priv2);
216  &dh_pub1);
218  &dh_pub2);
219 
220  GNUNET_assert (NULL !=
222  sizeof (dh_priv1),
223  buf,
224  sizeof (buf)));
225  printf ("ECDHE key 1:\n");
226  printf ("private: %s\n",
227  buf);
228  print_hex ("private(hex)",
229  &dh_priv1, sizeof (dh_priv1));
230  GNUNET_assert (NULL !=
232  sizeof (dh_pub1),
233  buf,
234  sizeof (buf)));
235  printf ("public: %s\n",
236  buf);
237  print_hex ("public(hex)",
238  &dh_pub1,
239  sizeof (dh_pub1));
240 
241  GNUNET_assert (NULL !=
243  sizeof (dh_priv2),
244  buf,
245  sizeof (buf)));
246  printf ("ECDHE key 2:\n");
247  printf ("private: %s\n", buf);
248  print_hex ("private(hex)",
249  &dh_priv2,
250  sizeof (dh_priv2));
251  GNUNET_assert (NULL !=
253  sizeof (dh_pub2),
254  buf,
255  sizeof (buf)));
256  printf ("public: %s\n", buf);
257  print_hex ("public(hex)",
258  &dh_pub2,
259  sizeof (dh_pub2));
260 
262  GNUNET_CRYPTO_ecc_ecdh (&dh_priv1,
263  &dh_pub2,
264  &hash));
265  GNUNET_assert (NULL !=
267  sizeof (hash),
268  buf,
269  sizeof (buf)));
270  printf ("ECDH shared secret: %s\n",
271  buf);
272 
273 }
274 
275 
279 static void
281 {
283  // print_examples_ecdsa ();
284  // print_examples_eddsa ();
285 }
286 
287 
288 static void
289 print_key (const char *filename)
290 {
291  struct GNUNET_DISK_FileHandle *fd;
292  struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
293  struct GNUNET_CRYPTO_EddsaPublicKey public_key;
294  char *hostkeys_data;
295  char *hostkey_str;
296  uint64_t fs;
297  unsigned int total_hostkeys;
298  unsigned int c;
299  ssize_t sret;
300 
301  if (GNUNET_YES != GNUNET_DISK_file_test (filename))
302  {
303  fprintf (stderr, _ ("Hostkeys file `%s' not found\n"), filename);
304  return;
305  }
306 
307  /* Check hostkey file size, read entire thing into memory */
308  if (GNUNET_OK !=
309  GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
310  fs = 0;
311  if (0 == fs)
312  {
313  fprintf (stderr, _ ("Hostkeys file `%s' is empty\n"), filename);
314  return; /* File is empty */
315  }
316  if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
317  {
318  fprintf (stderr, _ ("Incorrect hostkey file format: %s\n"), filename);
319  return;
320  }
321  fd = GNUNET_DISK_file_open (filename,
324  if (NULL == fd)
325  {
327  return;
328  }
329  hostkeys_data = GNUNET_malloc (fs);
330  sret = GNUNET_DISK_file_read (fd, hostkeys_data, fs);
331  if ((sret < 0) || (fs != (size_t) sret))
332  {
333  fprintf (stderr, _ ("Could not read hostkey file: %s\n"), filename);
334  GNUNET_free (hostkeys_data);
336  return;
337  }
339 
340  if (NULL == hostkeys_data)
341  return;
342  total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
343  for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
344  {
345  GNUNET_memcpy (&private_key,
346  hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE),
347  GNUNET_TESTING_HOSTKEYFILESIZE);
348  GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key);
349  hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key);
350  if (NULL != hostkey_str)
351  {
352  fprintf (stderr, "%4u: %s\n", c, hostkey_str);
353  GNUNET_free (hostkey_str);
354  }
355  else
356  fprintf (stderr, "%4u: %s\n", c, "invalid");
357  }
358  GNUNET_free (hostkeys_data);
359 }
360 
361 
370 static void
371 run (void *cls,
372  char *const *args,
373  const char *cfgfile,
374  const struct GNUNET_CONFIGURATION_Handle *cfg)
375 {
376  (void) cls;
377  (void) cfgfile;
378  (void) cfg;
379 
381  {
382  print_examples ();
383  return;
384  }
385  if (NULL == args[0])
386  {
387  fprintf (stderr, "%s", _ ("No hostkey file specified on command line\n"));
388  return;
389  }
390  if (list_keys)
391  {
392  print_key (args[0]);
393  return;
394  }
395  if (make_keys > 0)
396  {
397  create_keys (args[0], args[1]);
398  return;
399  }
401  {
402  char *str;
403  struct GNUNET_DISK_FileHandle *keyfile;
405  struct GNUNET_CRYPTO_EddsaPublicKey pub;
406 
407  keyfile = GNUNET_DISK_file_open (args[0],
410  if (NULL == keyfile)
411  return;
412  while (sizeof(pk) == GNUNET_DISK_file_read (keyfile, &pk, sizeof(pk)))
413  {
416  {
417  print_hex ("HEX:", &pub, sizeof(pub));
418  }
419  else if (print_public_key)
420  {
422  fprintf (stdout, "%s\n", str);
423  GNUNET_free (str);
424  }
425  else if (print_private_key)
426  {
428  fprintf (stdout, "%s\n", str);
429  GNUNET_free (str);
430  }
431  }
432  GNUNET_DISK_file_close (keyfile);
433  }
434 }
435 
436 
444 int
445 main (int argc, char *const *argv)
446 {
447  struct GNUNET_GETOPT_CommandLineOption options[] =
449  "iterate",
450  gettext_noop (
451  "list keys included in a file (for testing)"),
452  &list_keys),
454  'e',
455  "end=",
456  "COUNT",
457  gettext_noop ("number of keys to list included in a file (for testing)"),
458  &list_keys_count),
460  'g',
461  "generate-keys",
462  "COUNT",
463  gettext_noop ("create COUNT public-private key pairs (for testing)"),
464  &make_keys),
466  "print-public-key",
467  gettext_noop (
468  "print the public key in ASCII format"),
471  "print-private-key",
472  gettext_noop (
473  "print the private key in ASCII format"),
476  "print-hex",
477  gettext_noop (
478  "print the public key in HEX format"),
481  'E',
482  "examples",
483  gettext_noop (
484  "print examples of ECC operations (used for compatibility testing)"),
487  int ret;
488 
489  list_keys_count = UINT32_MAX;
490  if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
491  return 2;
492 
493  ret = (GNUNET_OK ==
494  GNUNET_PROGRAM_run (argc,
495  argv,
496  "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]",
497  gettext_noop (
498  "Manipulate GNUnet private ECC key files"),
499  options,
500  &run,
501  NULL))
502  ? 0
503  : 1;
504  GNUNET_free_nz ((void *) argv);
505  return ret;
506 }
507 
508 
509 /* end of gnunet-ecc.c */
int main(int argc, char *const *argv)
Program to manipulate ECC key files.
Definition: gnunet-ecc.c:445
int GNUNET_DISK_file_test(const char *fil)
Check that fil corresponds to a filename (of a file that exists and that is not a directory)...
Definition: disk.c:544
Open the file for reading.
static int list_keys
Flag for listing public key.
Definition: gnunet-ecc.c:39
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
int GNUNET_DISK_file_close(struct GNUNET_DISK_FileHandle *h)
Close an open file.
Definition: disk.c:1280
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
ssize_t GNUNET_DISK_file_read(const struct GNUNET_DISK_FileHandle *h, void *result, size_t len)
Read the contents of a binary file into a buffer.
Definition: disk.c:732
int GNUNET_STRINGS_get_utf8_args(int argc, char *const *argv, int *u8argc, char *const **u8argv)
Returns utf-8 encoded arguments.
Definition: strings.c:1459
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Nobody is allowed to do anything to the file.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
int GNUNET_CRYPTO_ecc_ecdh(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, const struct GNUNET_CRYPTO_EcdhePublicKey *pub, struct GNUNET_HashCode *key_material)
Derive key material from a public and a private ECC key.
Definition: crypto_ecc.c:850
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
Definition of a command line option.
#define GNUNET_TESTING_HOSTKEYFILESIZE
Size of each hostkey in the hostkey file (in BYTES).
static void print_hex(const char *msg, const void *buf, size_t size)
Definition: gnunet-ecc.c:192
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_OPTION_END
Definition: 002.c:13
#define _(String)
GNU gettext support macro.
Definition: platform.h:184
static void * hostkeys_data
The hostkeys data.
void GNUNET_CRYPTO_ecdhe_key_create(struct GNUNET_CRYPTO_EcdhePrivateKey *pk)
Create a new private key.
Definition: crypto_ecc.c:508
static void print_examples(void)
Print some random example operations to stdout.
Definition: gnunet-ecc.c:280
static unsigned int make_keys
Option set to create a bunch of keys at once.
Definition: gnunet-ecc.c:69
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level &#39;level&#39; that indicates a failure of the command &#39;cmd&#39; with the mess...
static char * fn
Filename of the unique file.
char * GNUNET_CRYPTO_eddsa_public_key_to_string(const struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Convert a public key to a string.
Definition: crypto_ecc.c:265
static char buf[2048]
static char * filename
static void print_key(const char *filename)
Definition: gnunet-ecc.c:289
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
Main function that will be run by the scheduler.
Definition: gnunet-ecc.c:371
#define KEY_STR_LEN
Number of characters a Base32-encoded public key requires.
Definition: gnunet-ecc.c:34
A 512-bit hashcode.
Private ECC key encoded for transmission.
#define GNUNET_free_nz(ptr)
Wrapper around free.
static unsigned int size
Size of the "table".
Definition: peer.c:67
void GNUNET_CRYPTO_ecdhe_key_get_public(const struct GNUNET_CRYPTO_EcdhePrivateKey *priv, struct GNUNET_CRYPTO_EcdhePublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:215
static unsigned int list_keys_count
Flag for listing public key.
Definition: gnunet-ecc.c:44
void GNUNET_CRYPTO_eddsa_key_create(struct GNUNET_CRYPTO_EddsaPrivateKey *pk)
Create a new private key.
Definition: crypto_ecc.c:544
static int print_public_key_hex
Flag for printing public key in hex.
Definition: gnunet-ecc.c:59
static struct GNUNET_FS_Handle * fs
Handle to FS service.
Definition: gnunet-fs.c:36
int GNUNET_DISK_file_size(const char *filename, uint64_t *size, int include_symbolic_links, int single_file_mode)
Get the size of the file (or directory) of the given file (in bytes).
Definition: disk.c:257
void GNUNET_CRYPTO_eddsa_key_get_public(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Extract the public key for the given private key.
Definition: crypto_ecc.c:193
static int print_public_key
Flag for printing public key.
Definition: gnunet-ecc.c:49
Private ECC key encoded for transmission.
int GNUNET_CRYPTO_eddsa_public_key_from_string(const char *enc, size_t enclen, struct GNUNET_CRYPTO_EddsaPublicKey *pub)
Convert a string representing a public key to a public key.
Definition: crypto_ecc.c:399
static void print_examples_ecdh(void)
Definition: gnunet-ecc.c:204
static int print_private_key
Flag for printing private key.
Definition: gnunet-ecc.c:54
configuration data
Definition: configuration.c:84
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and encr...
char * GNUNET_CRYPTO_eddsa_private_key_to_string(const struct GNUNET_CRYPTO_EddsaPrivateKey *priv)
Convert a private key to a string.
Definition: crypto_ecc.c:298
int GNUNET_PROGRAM_run(int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration, parse options).
Definition: program.c:372
static int print_examples_flag
Flag for printing the output of random example operations.
Definition: gnunet-ecc.c:64
int fd
File handle on Unix-like systems.
static void create_keys(const char *fn, const char *prefix)
Create a flat file with a large number of key pairs for testing.
Definition: gnunet-ecc.c:79
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint(char shortName, const char *name, const char *argumentHelp, const char *description, unsigned int *val)
Allow user to specify an unsigned int.
struct GNUNET_DISK_FileHandle * GNUNET_DISK_file_open(const char *fn, enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm)
Open a file.
Definition: disk.c:1203
size_t GNUNET_strlcpy(char *dst, const char *src, size_t n)
Like strlcpy but portable.
Definition: strings.c:221
Handle used to access files (and pipes).
#define GNUNET_malloc(size)
Wrapper around malloc.
char * GNUNET_STRINGS_data_to_string(const void *data, size_t size, char *out, size_t out_size)
Convert binary data to ASCII encoding using CrockfordBase32.
Definition: strings.c:872
#define GNUNET_free(ptr)
Wrapper around free.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
#define gettext_noop(String)
Definition: gettext.h:69
Public ECC key (always for curve Ed25519) encoded in a format suitable for network transmission and E...