aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorBart Polot <bart@net.in.tum.de>2014-07-03 13:25:00 +0000
committerBart Polot <bart@net.in.tum.de>2014-07-03 13:25:00 +0000
commit6d0e271bba25f92e0887b8254c8304b516fa2028 (patch)
treecd977325869e033b6293ebc799b2c2faaa1fa1ed /src/util
parenta52c9a050ee291f77d0ac7a4b5d9e2367d9f1b5c (diff)
downloadgnunet-6d0e271bba25f92e0887b8254c8304b516fa2028.tar.gz
gnunet-6d0e271bba25f92e0887b8254c8304b516fa2028.zip
Added a cleaned-up vanity key generator to gnunet-ecc
Diffstat (limited to 'src/util')
-rw-r--r--src/util/gnunet-ecc.c80
1 files changed, 72 insertions, 8 deletions
diff --git a/src/util/gnunet-ecc.c b/src/util/gnunet-ecc.c
index 66d398aed..e5804c71b 100644
--- a/src/util/gnunet-ecc.c
+++ b/src/util/gnunet-ecc.c
@@ -28,6 +28,7 @@
28#include "gnunet_testing_lib.h" 28#include "gnunet_testing_lib.h"
29#include <gcrypt.h> 29#include <gcrypt.h>
30 30
31#define KEY_STR_LEN sizeof(struct GNUNET_CRYPTO_EddsaPublicKey)*8/5+1
31 32
32/** 33/**
33 * Flag for listing public key. 34 * Flag for listing public key.
@@ -62,12 +63,22 @@ static unsigned int make_keys;
62 63
63/** 64/**
64 * Create a flat file with a large number of key pairs for testing. 65 * Create a flat file with a large number of key pairs for testing.
66 *
67 * @param fn File name to store the keys.
68 * @param prefix Desired prefix for the public keys, NULL if any key is OK.
65 */ 69 */
66static void 70static void
67create_keys (const char *fn) 71create_keys (const char *fn, const char *prefix)
68{ 72{
69 FILE *f; 73 FILE *f;
70 struct GNUNET_CRYPTO_EddsaPrivateKey *pk; 74 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
75 struct GNUNET_CRYPTO_EddsaPublicKey target_pub;
76 static char vanity[KEY_STR_LEN + 1];
77 int len;
78 int n;
79 int rest;
80 unsigned char mask;
81 unsigned target_byte;
71 82
72 if (NULL == (f = fopen (fn, "w+"))) 83 if (NULL == (f = fopen (fn, "w+")))
73 { 84 {
@@ -77,18 +88,71 @@ create_keys (const char *fn)
77 STRERROR (errno)); 88 STRERROR (errno));
78 return; 89 return;
79 } 90 }
80 fprintf (stderr, 91 if (NULL != prefix)
81 _("Generating %u keys, please wait"),
82 make_keys);
83 while (0 < make_keys--)
84 { 92 {
93 strncpy (vanity, prefix, KEY_STR_LEN);
94 len = strlen (vanity);
95 n = len * 5 / 8;
96 rest = len * 5 % 8;
97
98 memset (&vanity[len], '0', KEY_STR_LEN - len);
99 GNUNET_assert (GNUNET_OK ==
100 GNUNET_CRYPTO_eddsa_public_key_from_string (vanity,
101 KEY_STR_LEN,
102 &target_pub));
103 if (0 != rest)
104 {
105 /**
106 * Documentation by example:
107 * vanity = "A"
108 * len = 1
109 * n = 5/8 = 0 (bytes)
110 * rest = 5%8 = 5 (bits)
111 * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000
112 */
113 mask = ~ ((int)pow (2, 8 - rest) - 1);
114 target_byte = ((unsigned char *) &target_pub)[n] & mask;
115 }
85 fprintf (stderr, 116 fprintf (stderr,
86 "."); 117 _("Generating %u keys like %s, please wait"),
118 make_keys, GNUNET_CRYPTO_eddsa_public_key_to_string (&target_pub));
119 fprintf (stderr, "\nattempt %s [%d, %X]\n", vanity, n, mask);
120 }
121 else
122 {
123 fprintf (stderr, _("Generating %u keys, please wait"), make_keys);
124 }
125
126 while (0 < make_keys--)
127 {
128 fprintf (stderr, ".");
87 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ())) 129 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ()))
88 { 130 {
89 GNUNET_break (0); 131 GNUNET_break (0);
90 break; 132 break;
91 } 133 }
134 if (NULL != prefix)
135 {
136 struct GNUNET_CRYPTO_EddsaPublicKey newkey;
137
138 GNUNET_CRYPTO_eddsa_key_get_public (pk, &newkey);
139 if (0 != memcmp (&target_pub, &newkey, n))
140 {
141 make_keys++;
142 continue;
143 }
144 if (0 != rest)
145 {
146 unsigned char new_byte;
147
148 new_byte = ((unsigned char *) &newkey)[n] & mask;
149 if (target_byte != new_byte)
150 {
151 make_keys++;
152 continue;
153 }
154 }
155 }
92 if (GNUNET_TESTING_HOSTKEYFILESIZE != 156 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
93 fwrite (pk, 1, 157 fwrite (pk, 1,
94 GNUNET_TESTING_HOSTKEYFILESIZE, f)) 158 GNUNET_TESTING_HOSTKEYFILESIZE, f))
@@ -290,7 +354,7 @@ run (void *cls, char *const *args, const char *cfgfile,
290 } 354 }
291 if (make_keys > 0) 355 if (make_keys > 0)
292 { 356 {
293 create_keys (args[0]); 357 create_keys (args[0], args[1]);
294 return; 358 return;
295 } 359 }
296 if (print_public_key) 360 if (print_public_key)
@@ -372,7 +436,7 @@ main (int argc, char *const *argv)
372 return 2; 436 return 2;
373 437
374 ret = (GNUNET_OK == 438 ret = (GNUNET_OK ==
375 GNUNET_PROGRAM_run (argc, argv, "gnunet-ecc [OPTIONS] keyfile", 439 GNUNET_PROGRAM_run (argc, argv, "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]",
376 gettext_noop ("Manipulate GNUnet private ECC key files"), 440 gettext_noop ("Manipulate GNUnet private ECC key files"),
377 options, &run, NULL)) ? 0 : 1; 441 options, &run, NULL)) ? 0 : 1;
378 GNUNET_free ((void*) argv); 442 GNUNET_free ((void*) argv);