diff options
Diffstat (limited to 'src/util/gnunet-ecc.c')
-rw-r--r-- | src/util/gnunet-ecc.c | 560 |
1 files changed, 280 insertions, 280 deletions
diff --git a/src/util/gnunet-ecc.c b/src/util/gnunet-ecc.c index e7a170b81..7fb8150f6 100644 --- a/src/util/gnunet-ecc.c +++ b/src/util/gnunet-ecc.c | |||
@@ -11,12 +11,12 @@ | |||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Affero General Public License for more details. | 13 | Affero General Public License for more details. |
14 | 14 | ||
15 | You should have received a copy of the GNU Affero General Public License | 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/>. | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 | ||
18 | SPDX-License-Identifier: AGPL3.0-or-later | 18 | SPDX-License-Identifier: AGPL3.0-or-later |
19 | */ | 19 | */ |
20 | 20 | ||
21 | /** | 21 | /** |
22 | * @file util/gnunet-ecc.c | 22 | * @file util/gnunet-ecc.c |
@@ -31,7 +31,7 @@ | |||
31 | /** | 31 | /** |
32 | * Number of characters a Base32-encoded public key requires. | 32 | * Number of characters a Base32-encoded public key requires. |
33 | */ | 33 | */ |
34 | #define KEY_STR_LEN sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) * 8 / 5 + 1 | 34 | #define KEY_STR_LEN sizeof(struct GNUNET_CRYPTO_EddsaPublicKey) * 8 / 5 + 1 |
35 | 35 | ||
36 | /** | 36 | /** |
37 | * Flag for listing public key. | 37 | * Flag for listing public key. |
@@ -76,7 +76,7 @@ static unsigned int make_keys; | |||
76 | * @param prefix Desired prefix for the public keys, NULL if any key is OK. | 76 | * @param prefix Desired prefix for the public keys, NULL if any key is OK. |
77 | */ | 77 | */ |
78 | static void | 78 | static void |
79 | create_keys (const char *fn, const char *prefix) | 79 | create_keys(const char *fn, const char *prefix) |
80 | { | 80 | { |
81 | FILE *f; | 81 | FILE *f; |
82 | struct GNUNET_CRYPTO_EddsaPrivateKey *pk; | 82 | struct GNUNET_CRYPTO_EddsaPrivateKey *pk; |
@@ -89,120 +89,120 @@ create_keys (const char *fn, const char *prefix) | |||
89 | unsigned target_byte; | 89 | unsigned target_byte; |
90 | char *s; | 90 | char *s; |
91 | 91 | ||
92 | if (NULL == (f = fopen (fn, "w+"))) | 92 | if (NULL == (f = fopen(fn, "w+"))) |
93 | { | 93 | { |
94 | fprintf (stderr, _ ("Failed to open `%s': %s\n"), fn, strerror (errno)); | 94 | fprintf(stderr, _("Failed to open `%s': %s\n"), fn, strerror(errno)); |
95 | return; | 95 | return; |
96 | } | 96 | } |
97 | if (NULL != prefix) | 97 | if (NULL != prefix) |
98 | { | 98 | { |
99 | len = GNUNET_strlcpy (vanity, prefix, sizeof (vanity)); | 99 | len = GNUNET_strlcpy(vanity, prefix, sizeof(vanity)); |
100 | n = len * 5 / 8; | 100 | n = len * 5 / 8; |
101 | rest = len * 5 % 8; | 101 | rest = len * 5 % 8; |
102 | 102 | ||
103 | memset (&vanity[len], '0', KEY_STR_LEN - len); | 103 | memset(&vanity[len], '0', KEY_STR_LEN - len); |
104 | vanity[KEY_STR_LEN] = '\0'; | 104 | vanity[KEY_STR_LEN] = '\0'; |
105 | GNUNET_assert (GNUNET_OK == | 105 | GNUNET_assert(GNUNET_OK == |
106 | GNUNET_CRYPTO_eddsa_public_key_from_string (vanity, | 106 | GNUNET_CRYPTO_eddsa_public_key_from_string(vanity, |
107 | KEY_STR_LEN, | 107 | KEY_STR_LEN, |
108 | &target_pub)); | 108 | &target_pub)); |
109 | if (0 != rest) | 109 | if (0 != rest) |
110 | { | 110 | { |
111 | /** | 111 | /** |
112 | * Documentation by example: | 112 | * Documentation by example: |
113 | * vanity = "A" | 113 | * vanity = "A" |
114 | * len = 1 | 114 | * len = 1 |
115 | * n = 5/8 = 0 (bytes) | 115 | * n = 5/8 = 0 (bytes) |
116 | * rest = 5%8 = 5 (bits) | 116 | * rest = 5%8 = 5 (bits) |
117 | * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000 | 117 | * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000 |
118 | */ | 118 | */ |
119 | mask = ~((int) pow (2, 8 - rest) - 1); | 119 | mask = ~((int)pow(2, 8 - rest) - 1); |
120 | target_byte = ((unsigned char *) &target_pub)[n] & mask; | 120 | target_byte = ((unsigned char *)&target_pub)[n] & mask; |
121 | } | ||
122 | else | ||
123 | { | ||
124 | /* Just so old (debian) versions of GCC calm down with the warnings. */ | ||
125 | mask = target_byte = 0; | ||
126 | } | ||
127 | s = GNUNET_CRYPTO_eddsa_public_key_to_string(&target_pub); | ||
128 | fprintf(stderr, | ||
129 | _("Generating %u keys like %s, please wait"), | ||
130 | make_keys, | ||
131 | s); | ||
132 | GNUNET_free(s); | ||
133 | fprintf(stderr, "\nattempt %s [%u, %X]\n", vanity, (unsigned int)n, mask); | ||
121 | } | 134 | } |
122 | else | 135 | else |
123 | { | 136 | { |
137 | fprintf(stderr, _("Generating %u keys, please wait"), make_keys); | ||
124 | /* Just so old (debian) versions of GCC calm down with the warnings. */ | 138 | /* Just so old (debian) versions of GCC calm down with the warnings. */ |
125 | mask = target_byte = 0; | 139 | n = rest = target_byte = mask = 0; |
126 | } | 140 | } |
127 | s = GNUNET_CRYPTO_eddsa_public_key_to_string (&target_pub); | ||
128 | fprintf (stderr, | ||
129 | _ ("Generating %u keys like %s, please wait"), | ||
130 | make_keys, | ||
131 | s); | ||
132 | GNUNET_free (s); | ||
133 | fprintf (stderr, "\nattempt %s [%u, %X]\n", vanity, (unsigned int) n, mask); | ||
134 | } | ||
135 | else | ||
136 | { | ||
137 | fprintf (stderr, _ ("Generating %u keys, please wait"), make_keys); | ||
138 | /* Just so old (debian) versions of GCC calm down with the warnings. */ | ||
139 | n = rest = target_byte = mask = 0; | ||
140 | } | ||
141 | 141 | ||
142 | while (0 < make_keys--) | 142 | while (0 < make_keys--) |
143 | { | ||
144 | fprintf (stderr, "."); | ||
145 | if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create ())) | ||
146 | { | ||
147 | GNUNET_break (0); | ||
148 | break; | ||
149 | } | ||
150 | if (NULL != prefix) | ||
151 | { | 143 | { |
152 | struct GNUNET_CRYPTO_EddsaPublicKey newkey; | 144 | fprintf(stderr, "."); |
153 | 145 | if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create())) | |
154 | GNUNET_CRYPTO_eddsa_key_get_public (pk, &newkey); | ||
155 | if (0 != memcmp (&target_pub, &newkey, n)) | ||
156 | { | ||
157 | make_keys++; | ||
158 | continue; | ||
159 | } | ||
160 | if (0 != rest) | ||
161 | { | ||
162 | unsigned char new_byte; | ||
163 | |||
164 | new_byte = ((unsigned char *) &newkey)[n] & mask; | ||
165 | if (target_byte != new_byte) | ||
166 | { | 146 | { |
167 | make_keys++; | 147 | GNUNET_break(0); |
168 | continue; | 148 | break; |
169 | } | 149 | } |
170 | } | 150 | if (NULL != prefix) |
171 | } | 151 | { |
172 | if (GNUNET_TESTING_HOSTKEYFILESIZE != | 152 | struct GNUNET_CRYPTO_EddsaPublicKey newkey; |
173 | fwrite (pk, 1, GNUNET_TESTING_HOSTKEYFILESIZE, f)) | 153 | |
174 | { | 154 | GNUNET_CRYPTO_eddsa_key_get_public(pk, &newkey); |
175 | fprintf (stderr, | 155 | if (0 != memcmp(&target_pub, &newkey, n)) |
176 | _ ("\nFailed to write to `%s': %s\n"), | 156 | { |
177 | fn, | 157 | make_keys++; |
178 | strerror (errno)); | 158 | continue; |
179 | GNUNET_free (pk); | 159 | } |
180 | break; | 160 | if (0 != rest) |
161 | { | ||
162 | unsigned char new_byte; | ||
163 | |||
164 | new_byte = ((unsigned char *)&newkey)[n] & mask; | ||
165 | if (target_byte != new_byte) | ||
166 | { | ||
167 | make_keys++; | ||
168 | continue; | ||
169 | } | ||
170 | } | ||
171 | } | ||
172 | if (GNUNET_TESTING_HOSTKEYFILESIZE != | ||
173 | fwrite(pk, 1, GNUNET_TESTING_HOSTKEYFILESIZE, f)) | ||
174 | { | ||
175 | fprintf(stderr, | ||
176 | _("\nFailed to write to `%s': %s\n"), | ||
177 | fn, | ||
178 | strerror(errno)); | ||
179 | GNUNET_free(pk); | ||
180 | break; | ||
181 | } | ||
182 | GNUNET_free(pk); | ||
181 | } | 183 | } |
182 | GNUNET_free (pk); | ||
183 | } | ||
184 | if (UINT_MAX == make_keys) | 184 | if (UINT_MAX == make_keys) |
185 | fprintf (stderr, _ ("\nFinished!\n")); | 185 | fprintf(stderr, _("\nFinished!\n")); |
186 | else | 186 | else |
187 | fprintf (stderr, _ ("\nError, %u keys not generated\n"), make_keys); | 187 | fprintf(stderr, _("\nError, %u keys not generated\n"), make_keys); |
188 | fclose (f); | 188 | fclose(f); |
189 | } | 189 | } |
190 | 190 | ||
191 | 191 | ||
192 | static void | 192 | static void |
193 | print_hex (const char *msg, const void *buf, size_t size) | 193 | print_hex(const char *msg, const void *buf, size_t size) |
194 | { | 194 | { |
195 | printf ("%s: ", msg); | 195 | printf("%s: ", msg); |
196 | for (size_t i = 0; i < size; i++) | 196 | for (size_t i = 0; i < size; i++) |
197 | { | 197 | { |
198 | printf ("%02hhx", ((const uint8_t *) buf)[i]); | 198 | printf("%02hhx", ((const uint8_t *)buf)[i]); |
199 | } | 199 | } |
200 | printf ("\n"); | 200 | printf("\n"); |
201 | } | 201 | } |
202 | 202 | ||
203 | 203 | ||
204 | static void | 204 | static void |
205 | print_examples_ecdh () | 205 | print_examples_ecdh() |
206 | { | 206 | { |
207 | struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv1; | 207 | struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv1; |
208 | struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub1; | 208 | struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub1; |
@@ -211,40 +211,40 @@ print_examples_ecdh () | |||
211 | struct GNUNET_HashCode hash; | 211 | struct GNUNET_HashCode hash; |
212 | char buf[128]; | 212 | char buf[128]; |
213 | 213 | ||
214 | dh_pub1 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey); | 214 | dh_pub1 = GNUNET_new(struct GNUNET_CRYPTO_EcdhePublicKey); |
215 | dh_priv1 = GNUNET_CRYPTO_ecdhe_key_create (); | 215 | dh_priv1 = GNUNET_CRYPTO_ecdhe_key_create(); |
216 | dh_pub2 = GNUNET_new (struct GNUNET_CRYPTO_EcdhePublicKey); | 216 | dh_pub2 = GNUNET_new(struct GNUNET_CRYPTO_EcdhePublicKey); |
217 | dh_priv2 = GNUNET_CRYPTO_ecdhe_key_create (); | 217 | dh_priv2 = GNUNET_CRYPTO_ecdhe_key_create(); |
218 | GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv1, dh_pub1); | 218 | GNUNET_CRYPTO_ecdhe_key_get_public(dh_priv1, dh_pub1); |
219 | GNUNET_CRYPTO_ecdhe_key_get_public (dh_priv2, dh_pub2); | 219 | GNUNET_CRYPTO_ecdhe_key_get_public(dh_priv2, dh_pub2); |
220 | 220 | ||
221 | GNUNET_assert (NULL != | 221 | GNUNET_assert(NULL != |
222 | GNUNET_STRINGS_data_to_string (dh_priv1, 32, buf, 128)); | 222 | GNUNET_STRINGS_data_to_string(dh_priv1, 32, buf, 128)); |
223 | printf ("ECDHE key 1:\n"); | 223 | printf("ECDHE key 1:\n"); |
224 | printf ("private: %s\n", buf); | 224 | printf("private: %s\n", buf); |
225 | print_hex ("private(hex)", dh_priv1, sizeof *dh_priv1); | 225 | print_hex("private(hex)", dh_priv1, sizeof *dh_priv1); |
226 | GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub1, 32, buf, 128)); | 226 | GNUNET_assert(NULL != GNUNET_STRINGS_data_to_string(dh_pub1, 32, buf, 128)); |
227 | printf ("public: %s\n", buf); | 227 | printf("public: %s\n", buf); |
228 | print_hex ("public(hex)", dh_pub1, sizeof *dh_pub1); | 228 | print_hex("public(hex)", dh_pub1, sizeof *dh_pub1); |
229 | 229 | ||
230 | GNUNET_assert (NULL != | 230 | GNUNET_assert(NULL != |
231 | GNUNET_STRINGS_data_to_string (dh_priv2, 32, buf, 128)); | 231 | GNUNET_STRINGS_data_to_string(dh_priv2, 32, buf, 128)); |
232 | printf ("ECDHE key 2:\n"); | 232 | printf("ECDHE key 2:\n"); |
233 | printf ("private: %s\n", buf); | 233 | printf("private: %s\n", buf); |
234 | print_hex ("private(hex)", dh_priv2, sizeof *dh_priv2); | 234 | print_hex("private(hex)", dh_priv2, sizeof *dh_priv2); |
235 | GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (dh_pub2, 32, buf, 128)); | 235 | GNUNET_assert(NULL != GNUNET_STRINGS_data_to_string(dh_pub2, 32, buf, 128)); |
236 | printf ("public: %s\n", buf); | 236 | printf("public: %s\n", buf); |
237 | print_hex ("public(hex)", dh_pub2, sizeof *dh_pub2); | 237 | print_hex("public(hex)", dh_pub2, sizeof *dh_pub2); |
238 | 238 | ||
239 | GNUNET_assert (GNUNET_OK == | 239 | GNUNET_assert(GNUNET_OK == |
240 | GNUNET_CRYPTO_ecc_ecdh (dh_priv1, dh_pub2, &hash)); | 240 | GNUNET_CRYPTO_ecc_ecdh(dh_priv1, dh_pub2, &hash)); |
241 | GNUNET_assert (NULL != GNUNET_STRINGS_data_to_string (&hash, 64, buf, 128)); | 241 | GNUNET_assert(NULL != GNUNET_STRINGS_data_to_string(&hash, 64, buf, 128)); |
242 | printf ("ECDH shared secret: %s\n", buf); | 242 | printf("ECDH shared secret: %s\n", buf); |
243 | 243 | ||
244 | GNUNET_free (dh_priv1); | 244 | GNUNET_free(dh_priv1); |
245 | GNUNET_free (dh_priv2); | 245 | GNUNET_free(dh_priv2); |
246 | GNUNET_free (dh_pub1); | 246 | GNUNET_free(dh_pub1); |
247 | GNUNET_free (dh_pub2); | 247 | GNUNET_free(dh_pub2); |
248 | } | 248 | } |
249 | 249 | ||
250 | 250 | ||
@@ -252,16 +252,16 @@ print_examples_ecdh () | |||
252 | * Print some random example operations to stdout. | 252 | * Print some random example operations to stdout. |
253 | */ | 253 | */ |
254 | static void | 254 | static void |
255 | print_examples () | 255 | print_examples() |
256 | { | 256 | { |
257 | print_examples_ecdh (); | 257 | print_examples_ecdh(); |
258 | // print_examples_ecdsa (); | 258 | // print_examples_ecdsa (); |
259 | // print_examples_eddsa (); | 259 | // print_examples_eddsa (); |
260 | } | 260 | } |
261 | 261 | ||
262 | 262 | ||
263 | static void | 263 | static void |
264 | print_key (const char *filename) | 264 | print_key(const char *filename) |
265 | { | 265 | { |
266 | struct GNUNET_DISK_FileHandle *fd; | 266 | struct GNUNET_DISK_FileHandle *fd; |
267 | struct GNUNET_CRYPTO_EddsaPrivateKey private_key; | 267 | struct GNUNET_CRYPTO_EddsaPrivateKey private_key; |
@@ -273,64 +273,64 @@ print_key (const char *filename) | |||
273 | unsigned int c; | 273 | unsigned int c; |
274 | ssize_t sret; | 274 | ssize_t sret; |
275 | 275 | ||
276 | if (GNUNET_YES != GNUNET_DISK_file_test (filename)) | 276 | if (GNUNET_YES != GNUNET_DISK_file_test(filename)) |
277 | { | 277 | { |
278 | fprintf (stderr, _ ("Hostkeys file `%s' not found\n"), filename); | 278 | fprintf(stderr, _("Hostkeys file `%s' not found\n"), filename); |
279 | return; | 279 | return; |
280 | } | 280 | } |
281 | 281 | ||
282 | /* Check hostkey file size, read entire thing into memory */ | 282 | /* Check hostkey file size, read entire thing into memory */ |
283 | if (GNUNET_OK != | 283 | if (GNUNET_OK != |
284 | GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES)) | 284 | GNUNET_DISK_file_size(filename, &fs, GNUNET_YES, GNUNET_YES)) |
285 | fs = 0; | 285 | fs = 0; |
286 | if (0 == fs) | 286 | if (0 == fs) |
287 | { | 287 | { |
288 | fprintf (stderr, _ ("Hostkeys file `%s' is empty\n"), filename); | 288 | fprintf(stderr, _("Hostkeys file `%s' is empty\n"), filename); |
289 | return; /* File is empty */ | 289 | return; /* File is empty */ |
290 | } | 290 | } |
291 | if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE)) | 291 | if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE)) |
292 | { | 292 | { |
293 | fprintf (stderr, _ ("Incorrect hostkey file format: %s\n"), filename); | 293 | fprintf(stderr, _("Incorrect hostkey file format: %s\n"), filename); |
294 | return; | 294 | return; |
295 | } | 295 | } |
296 | fd = GNUNET_DISK_file_open (filename, | 296 | fd = GNUNET_DISK_file_open(filename, |
297 | GNUNET_DISK_OPEN_READ, | 297 | GNUNET_DISK_OPEN_READ, |
298 | GNUNET_DISK_PERM_NONE); | 298 | GNUNET_DISK_PERM_NONE); |
299 | if (NULL == fd) | 299 | if (NULL == fd) |
300 | { | 300 | { |
301 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename); | 301 | GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "open", filename); |
302 | return; | 302 | return; |
303 | } | 303 | } |
304 | hostkeys_data = GNUNET_malloc (fs); | 304 | hostkeys_data = GNUNET_malloc(fs); |
305 | sret = GNUNET_DISK_file_read (fd, hostkeys_data, fs); | 305 | sret = GNUNET_DISK_file_read(fd, hostkeys_data, fs); |
306 | if ((sret < 0) || (fs != (size_t) sret)) | 306 | if ((sret < 0) || (fs != (size_t)sret)) |
307 | { | 307 | { |
308 | fprintf (stderr, _ ("Could not read hostkey file: %s\n"), filename); | 308 | fprintf(stderr, _("Could not read hostkey file: %s\n"), filename); |
309 | GNUNET_free (hostkeys_data); | 309 | GNUNET_free(hostkeys_data); |
310 | GNUNET_DISK_file_close (fd); | 310 | GNUNET_DISK_file_close(fd); |
311 | return; | 311 | return; |
312 | } | 312 | } |
313 | GNUNET_DISK_file_close (fd); | 313 | GNUNET_DISK_file_close(fd); |
314 | 314 | ||
315 | if (NULL == hostkeys_data) | 315 | if (NULL == hostkeys_data) |
316 | return; | 316 | return; |
317 | total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE; | 317 | total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE; |
318 | for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++) | 318 | for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++) |
319 | { | ||
320 | GNUNET_memcpy (&private_key, | ||
321 | hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE), | ||
322 | GNUNET_TESTING_HOSTKEYFILESIZE); | ||
323 | GNUNET_CRYPTO_eddsa_key_get_public (&private_key, &public_key); | ||
324 | hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string (&public_key); | ||
325 | if (NULL != hostkey_str) | ||
326 | { | 319 | { |
327 | fprintf (stderr, "%4u: %s\n", c, hostkey_str); | 320 | GNUNET_memcpy(&private_key, |
328 | GNUNET_free (hostkey_str); | 321 | hostkeys_data + (c * GNUNET_TESTING_HOSTKEYFILESIZE), |
322 | GNUNET_TESTING_HOSTKEYFILESIZE); | ||
323 | GNUNET_CRYPTO_eddsa_key_get_public(&private_key, &public_key); | ||
324 | hostkey_str = GNUNET_CRYPTO_eddsa_public_key_to_string(&public_key); | ||
325 | if (NULL != hostkey_str) | ||
326 | { | ||
327 | fprintf(stderr, "%4u: %s\n", c, hostkey_str); | ||
328 | GNUNET_free(hostkey_str); | ||
329 | } | ||
330 | else | ||
331 | fprintf(stderr, "%4u: %s\n", c, "invalid"); | ||
329 | } | 332 | } |
330 | else | 333 | GNUNET_free(hostkeys_data); |
331 | fprintf (stderr, "%4u: %s\n", c, "invalid"); | ||
332 | } | ||
333 | GNUNET_free (hostkeys_data); | ||
334 | } | 334 | } |
335 | 335 | ||
336 | 336 | ||
@@ -343,69 +343,69 @@ print_key (const char *filename) | |||
343 | * @param cfg configuration | 343 | * @param cfg configuration |
344 | */ | 344 | */ |
345 | static void | 345 | static void |
346 | run (void *cls, | 346 | run(void *cls, |
347 | char *const *args, | 347 | char *const *args, |
348 | const char *cfgfile, | 348 | const char *cfgfile, |
349 | const struct GNUNET_CONFIGURATION_Handle *cfg) | 349 | const struct GNUNET_CONFIGURATION_Handle *cfg) |
350 | { | 350 | { |
351 | (void) cls; | 351 | (void)cls; |
352 | (void) cfgfile; | 352 | (void)cfgfile; |
353 | (void) cfg; | 353 | (void)cfg; |
354 | 354 | ||
355 | if (print_examples_flag) | 355 | if (print_examples_flag) |
356 | { | 356 | { |
357 | print_examples (); | 357 | print_examples(); |
358 | return; | 358 | return; |
359 | } | 359 | } |
360 | if (NULL == args[0]) | 360 | if (NULL == args[0]) |
361 | { | 361 | { |
362 | fprintf (stderr, "%s", _ ("No hostkey file specified on command line\n")); | 362 | fprintf(stderr, "%s", _("No hostkey file specified on command line\n")); |
363 | return; | 363 | return; |
364 | } | 364 | } |
365 | if (list_keys) | 365 | if (list_keys) |
366 | { | 366 | { |
367 | print_key (args[0]); | 367 | print_key(args[0]); |
368 | return; | 368 | return; |
369 | } | 369 | } |
370 | if (make_keys > 0) | 370 | if (make_keys > 0) |
371 | { | 371 | { |
372 | create_keys (args[0], args[1]); | 372 | create_keys(args[0], args[1]); |
373 | return; | ||
374 | } | ||
375 | if (print_public_key || print_public_key_hex || print_private_key) | ||
376 | { | ||
377 | char *str; | ||
378 | struct GNUNET_DISK_FileHandle *keyfile; | ||
379 | struct GNUNET_CRYPTO_EddsaPrivateKey pk; | ||
380 | struct GNUNET_CRYPTO_EddsaPublicKey pub; | ||
381 | |||
382 | keyfile = GNUNET_DISK_file_open (args[0], | ||
383 | GNUNET_DISK_OPEN_READ, | ||
384 | GNUNET_DISK_PERM_NONE); | ||
385 | if (NULL == keyfile) | ||
386 | return; | 373 | return; |
387 | while (sizeof (pk) == GNUNET_DISK_file_read (keyfile, &pk, sizeof (pk))) | 374 | } |
375 | if (print_public_key || print_public_key_hex || print_private_key) | ||
388 | { | 376 | { |
389 | GNUNET_CRYPTO_eddsa_key_get_public (&pk, &pub); | 377 | char *str; |
390 | if (print_public_key_hex) | 378 | struct GNUNET_DISK_FileHandle *keyfile; |
391 | { | 379 | struct GNUNET_CRYPTO_EddsaPrivateKey pk; |
392 | print_hex ("HEX:", &pub, sizeof (pub)); | 380 | struct GNUNET_CRYPTO_EddsaPublicKey pub; |
393 | } | 381 | |
394 | else if (print_public_key) | 382 | keyfile = GNUNET_DISK_file_open(args[0], |
395 | { | 383 | GNUNET_DISK_OPEN_READ, |
396 | str = GNUNET_CRYPTO_eddsa_public_key_to_string (&pub); | 384 | GNUNET_DISK_PERM_NONE); |
397 | fprintf (stdout, "%s\n", str); | 385 | if (NULL == keyfile) |
398 | GNUNET_free (str); | 386 | return; |
399 | } | 387 | while (sizeof(pk) == GNUNET_DISK_file_read(keyfile, &pk, sizeof(pk))) |
400 | else if (print_private_key) | 388 | { |
401 | { | 389 | GNUNET_CRYPTO_eddsa_key_get_public(&pk, &pub); |
402 | str = GNUNET_CRYPTO_eddsa_private_key_to_string (&pk); | 390 | if (print_public_key_hex) |
403 | fprintf (stdout, "%s\n", str); | 391 | { |
404 | GNUNET_free (str); | 392 | print_hex("HEX:", &pub, sizeof(pub)); |
405 | } | 393 | } |
394 | else if (print_public_key) | ||
395 | { | ||
396 | str = GNUNET_CRYPTO_eddsa_public_key_to_string(&pub); | ||
397 | fprintf(stdout, "%s\n", str); | ||
398 | GNUNET_free(str); | ||
399 | } | ||
400 | else if (print_private_key) | ||
401 | { | ||
402 | str = GNUNET_CRYPTO_eddsa_private_key_to_string(&pk); | ||
403 | fprintf(stdout, "%s\n", str); | ||
404 | GNUNET_free(str); | ||
405 | } | ||
406 | } | ||
407 | GNUNET_DISK_file_close(keyfile); | ||
406 | } | 408 | } |
407 | GNUNET_DISK_file_close (keyfile); | ||
408 | } | ||
409 | } | 409 | } |
410 | 410 | ||
411 | 411 | ||
@@ -417,66 +417,66 @@ run (void *cls, | |||
417 | * @return 0 ok, 1 on error | 417 | * @return 0 ok, 1 on error |
418 | */ | 418 | */ |
419 | int | 419 | int |
420 | main (int argc, char *const *argv) | 420 | main(int argc, char *const *argv) |
421 | { | 421 | { |
422 | struct GNUNET_GETOPT_CommandLineOption options[] = | 422 | struct GNUNET_GETOPT_CommandLineOption options[] = |
423 | {GNUNET_GETOPT_option_flag ('i', | 423 | { GNUNET_GETOPT_option_flag('i', |
424 | "iterate", | 424 | "iterate", |
425 | gettext_noop ( | 425 | gettext_noop( |
426 | "list keys included in a file (for testing)"), | 426 | "list keys included in a file (for testing)"), |
427 | &list_keys), | 427 | &list_keys), |
428 | GNUNET_GETOPT_option_uint ( | 428 | GNUNET_GETOPT_option_uint( |
429 | 'e', | 429 | 'e', |
430 | "end=", | 430 | "end=", |
431 | "COUNT", | 431 | "COUNT", |
432 | gettext_noop ("number of keys to list included in a file (for testing)"), | 432 | gettext_noop("number of keys to list included in a file (for testing)"), |
433 | &list_keys_count), | 433 | &list_keys_count), |
434 | GNUNET_GETOPT_option_uint ( | 434 | GNUNET_GETOPT_option_uint( |
435 | 'g', | 435 | 'g', |
436 | "generate-keys", | 436 | "generate-keys", |
437 | "COUNT", | 437 | "COUNT", |
438 | gettext_noop ("create COUNT public-private key pairs (for testing)"), | 438 | gettext_noop("create COUNT public-private key pairs (for testing)"), |
439 | &make_keys), | 439 | &make_keys), |
440 | GNUNET_GETOPT_option_flag ('p', | 440 | GNUNET_GETOPT_option_flag('p', |
441 | "print-public-key", | 441 | "print-public-key", |
442 | gettext_noop ( | 442 | gettext_noop( |
443 | "print the public key in ASCII format"), | 443 | "print the public key in ASCII format"), |
444 | &print_public_key), | 444 | &print_public_key), |
445 | GNUNET_GETOPT_option_flag ('P', | 445 | GNUNET_GETOPT_option_flag('P', |
446 | "print-private-key", | 446 | "print-private-key", |
447 | gettext_noop ( | 447 | gettext_noop( |
448 | "print the private key in ASCII format"), | 448 | "print the private key in ASCII format"), |
449 | &print_private_key), | 449 | &print_private_key), |
450 | GNUNET_GETOPT_option_flag ('x', | 450 | GNUNET_GETOPT_option_flag('x', |
451 | "print-hex", | 451 | "print-hex", |
452 | gettext_noop ( | 452 | gettext_noop( |
453 | "print the public key in HEX format"), | 453 | "print the public key in HEX format"), |
454 | &print_public_key_hex), | 454 | &print_public_key_hex), |
455 | GNUNET_GETOPT_option_flag ( | 455 | GNUNET_GETOPT_option_flag( |
456 | 'E', | 456 | 'E', |
457 | "examples", | 457 | "examples", |
458 | gettext_noop ( | 458 | gettext_noop( |
459 | "print examples of ECC operations (used for compatibility testing)"), | 459 | "print examples of ECC operations (used for compatibility testing)"), |
460 | &print_examples_flag), | 460 | &print_examples_flag), |
461 | GNUNET_GETOPT_OPTION_END}; | 461 | GNUNET_GETOPT_OPTION_END }; |
462 | int ret; | 462 | int ret; |
463 | 463 | ||
464 | list_keys_count = UINT32_MAX; | 464 | list_keys_count = UINT32_MAX; |
465 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) | 465 | if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) |
466 | return 2; | 466 | return 2; |
467 | 467 | ||
468 | ret = (GNUNET_OK == | 468 | ret = (GNUNET_OK == |
469 | GNUNET_PROGRAM_run (argc, | 469 | GNUNET_PROGRAM_run(argc, |
470 | argv, | 470 | argv, |
471 | "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]", | 471 | "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]", |
472 | gettext_noop ( | 472 | gettext_noop( |
473 | "Manipulate GNUnet private ECC key files"), | 473 | "Manipulate GNUnet private ECC key files"), |
474 | options, | 474 | options, |
475 | &run, | 475 | &run, |
476 | NULL)) | 476 | NULL)) |
477 | ? 0 | 477 | ? 0 |
478 | : 1; | 478 | : 1; |
479 | GNUNET_free ((void *) argv); | 479 | GNUNET_free((void *)argv); |
480 | return ret; | 480 | return ret; |
481 | } | 481 | } |
482 | 482 | ||