/*
This file is part of GNUnet.
- (C) 2012, 2013 Christian Grothoff (and other contributing authors)
+ Copyright (C) 2012, 2013 GNUnet e.V.
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA.
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
*/
/**
#include "gnunet_testing_lib.h"
#include <gcrypt.h>
+/**
+ * Number of characters a Base32-encoded public key requires.
+ */
+#define KEY_STR_LEN sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)*8/5+1
/**
* Flag for listing public key.
*/
static int print_examples_flag;
-/**
- * Flag for printing hash of public key.
- */
-static int print_peer_identity;
-
/**
* Option set to create a bunch of keys at once.
*/
/**
* Create a flat file with a large number of key pairs for testing.
+ *
+ * @param fn File name to store the keys.
+ * @param prefix Desired prefix for the public keys, NULL if any key is OK.
*/
static void
-create_keys (const char *fn)
+create_keys (const char *fn, const char *prefix)
{
FILE *f;
struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
+ struct GNUNET_CRYPTO_EddsaPublicKey target_pub;
+ static char vanity[KEY_STR_LEN + 1];
+ size_t len;
+ size_t n;
+ size_t rest;
+ unsigned char mask;
+ unsigned target_byte;
+ char *s;
if (NULL == (f = fopen (fn, "w+")))
{
- fprintf (stderr,
- _("Failed to open `%s': %s\n"),
- fn,
- STRERROR (errno));
+ fprintf (stderr, _("Failed to open `%s': %s\n"), fn, STRERROR (errno));
return;
}
- fprintf (stderr,
- _("Generating %u keys, please wait"),
- make_keys);
- while (0 < make_keys--)
+ if (NULL != prefix)
{
+ strncpy (vanity, prefix, KEY_STR_LEN);
+ len = GNUNET_MIN (strlen (prefix), KEY_STR_LEN);
+ n = len * 5 / 8;
+ rest = len * 5 % 8;
+
+ memset (&vanity[len], '0', KEY_STR_LEN - len);
+ vanity[KEY_STR_LEN] = '\0';
+ GNUNET_assert (GNUNET_OK ==
+ GNUNET_CRYPTO_eddsa_public_key_from_string (vanity,
+ KEY_STR_LEN,
+ &target_pub));
+ if (0 != rest)
+ {
+ /**
+ * Documentation by example:
+ * vanity = "A"
+ * len = 1
+ * n = 5/8 = 0 (bytes)
+ * rest = 5%8 = 5 (bits)
+ * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000
+ */
+ mask = ~ ((int)pow (2, 8 - rest) - 1);
+ target_byte = ((unsigned char *) &target_pub)[n] & mask;
+ }
+ else
+ {
+ /* Just so old (debian) versions of GCC calm down with the warnings. */
+ mask = target_byte = 0;
+ }
+ s = GNUNET_CRYPTO_eddsa_public_key_to_string (&target_pub);
fprintf (stderr,
- ".");
+ _("Generating %u keys like %s, please wait"),
+ make_keys,
+ s);
+ GNUNET_free (s);
+ fprintf (stderr,
+ "\nattempt %s [%u, %X]\n",
+ vanity,
+ (unsigned int) n,
+ mask);
+ }
+ else
+ {
+ fprintf (stderr,
+ _("Generating %u keys, please wait"),
+ make_keys);
+ /* Just so old (debian) versions of GCC calm down with the warnings. */
+ n = rest = target_byte = mask = 0;
+ }
+
+ while (0 < make_keys--)
+ {
+ fprintf (stderr, ".");
if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ()))
{
GNUNET_break (0);
break;
}
+ if (NULL != prefix)
+ {
+ struct GNUNET_CRYPTO_EddsaPublicKey newkey;
+
+ GNUNET_CRYPTO_eddsa_key_get_public (pk, &newkey);
+ if (0 != memcmp (&target_pub, &newkey, n))
+ {
+ make_keys++;
+ continue;
+ }
+ if (0 != rest)
+ {
+ unsigned char new_byte;
+
+ new_byte = ((unsigned char *) &newkey)[n] & mask;
+ if (target_byte != new_byte)
+ {
+ make_keys++;
+ continue;
+ }
+ }
+ }
if (GNUNET_TESTING_HOSTKEYFILESIZE !=
fwrite (pk, 1,
GNUNET_TESTING_HOSTKEYFILESIZE, f))
_("\nFinished!\n"));
else
fprintf (stderr,
- _("\nError, %u keys not generated\n"), make_keys);
+ _("\nError, %u keys not generated\n"),
+ make_keys);
fclose (f);
}
static void
-print_hex (char *msg, void *buf, size_t size)
+print_hex (const char *msg,
+ const void *buf,
+ size_t size)
{
size_t i;
+
printf ("%s: ", msg);
for (i = 0; i < size; i++)
{
- printf ("%02hhx", ((char *)buf)[i]);
+ printf ("%02hhx", ((const char *)buf)[i]);
}
printf ("\n");
}
total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
{
- memcpy (&private_key,
+ GNUNET_memcpy (&private_key,
hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE),
GNUNET_TESTING_HOSTKEYFILESIZE);
GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key);
}
if (make_keys > 0)
{
- create_keys (args[0]);
+ create_keys (args[0], args[1]);
return;
}
if (print_public_key)
- {
- struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
- struct GNUNET_CRYPTO_EddsaPublicKey pub;
- char *s;
-
- pk = GNUNET_CRYPTO_eddsa_key_create_from_file (args[0]);
- if (NULL == pk)
- return;
- GNUNET_CRYPTO_eddsa_key_get_public (pk, &pub);
- s = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub);
- FPRINTF (stdout,
- "%s\n",
- s);
- GNUNET_free (s);
- GNUNET_free (pk);
- }
- if (print_peer_identity)
{
char *str;
struct GNUNET_DISK_FileHandle *keyfile;
{ 'p', "print-public-key", NULL,
gettext_noop ("print the public key in ASCII format"),
0, &GNUNET_GETOPT_set_one, &print_public_key },
- { 'P', "print-peer-identity", NULL,
- gettext_noop ("print the hash of the public key in ASCII format"),
- 0, &GNUNET_GETOPT_set_one, &print_peer_identity },
{ 'E', "examples", NULL,
gettext_noop ("print examples of ECC operations (used for compatibility testing)"),
0, &GNUNET_GETOPT_set_one, &print_examples_flag },
return 2;
ret = (GNUNET_OK ==
- GNUNET_PROGRAM_run (argc, argv, "gnunet-ecc [OPTIONS] keyfile",
+ GNUNET_PROGRAM_run (argc, argv, "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]",
gettext_noop ("Manipulate GNUnet private ECC key files"),
options, &run, NULL)) ? 0 : 1;
GNUNET_free ((void*) argv);