2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/gnunet-ecc.c
23 * @brief tool to manipulate EDDSA key files
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testing_lib.h"
32 * Number of characters a Base32-encoded public key requires.
34 #define KEY_STR_LEN sizeof(struct GNUNET_CRYPTO_EddsaPublicKey) * 8 / 5 + 1
37 * Flag for listing public key.
42 * Flag for listing public key.
44 static unsigned int list_keys_count;
47 * Flag for printing public key.
49 static int print_public_key;
52 * Flag for printing private key.
54 static int print_private_key;
57 * Flag for printing public key in hex.
59 static int print_public_key_hex;
62 * Flag for printing the output of random example operations.
64 static int print_examples_flag;
67 * Option set to create a bunch of keys at once.
69 static unsigned int make_keys;
73 * Create a flat file with a large number of key pairs for testing.
75 * @param fn File name to store the keys.
76 * @param prefix Desired prefix for the public keys, NULL if any key is OK.
79 create_keys(const char *fn, const char *prefix)
82 struct GNUNET_CRYPTO_EddsaPrivateKey *pk;
83 struct GNUNET_CRYPTO_EddsaPublicKey target_pub;
84 static char vanity[KEY_STR_LEN + 1];
92 if (NULL == (f = fopen(fn, "w+")))
94 fprintf(stderr, _("Failed to open `%s': %s\n"), fn, strerror(errno));
99 len = GNUNET_strlcpy(vanity, prefix, sizeof(vanity));
103 memset(&vanity[len], '0', KEY_STR_LEN - len);
104 vanity[KEY_STR_LEN] = '\0';
105 GNUNET_assert(GNUNET_OK ==
106 GNUNET_CRYPTO_eddsa_public_key_from_string(vanity,
112 * Documentation by example:
115 * n = 5/8 = 0 (bytes)
116 * rest = 5%8 = 5 (bits)
117 * mask = ~(2**(8 - 5) - 1) = ~(2**3 - 1) = ~(8 - 1) = ~b111 = b11111000
119 mask = ~((int)pow(2, 8 - rest) - 1);
120 target_byte = ((unsigned char *)&target_pub)[n] & mask;
124 /* Just so old (debian) versions of GCC calm down with the warnings. */
125 mask = target_byte = 0;
127 s = GNUNET_CRYPTO_eddsa_public_key_to_string(&target_pub);
129 _("Generating %u keys like %s, please wait"),
133 fprintf(stderr, "\nattempt %s [%u, %X]\n", vanity, (unsigned int)n, mask);
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;
142 while (0 < make_keys--)
144 fprintf(stderr, ".");
145 if (NULL == (pk = GNUNET_CRYPTO_eddsa_key_create()))
152 struct GNUNET_CRYPTO_EddsaPublicKey newkey;
154 GNUNET_CRYPTO_eddsa_key_get_public(pk, &newkey);
155 if (0 != memcmp(&target_pub, &newkey, n))
162 unsigned char new_byte;
164 new_byte = ((unsigned char *)&newkey)[n] & mask;
165 if (target_byte != new_byte)
172 if (GNUNET_TESTING_HOSTKEYFILESIZE !=
173 fwrite(pk, 1, GNUNET_TESTING_HOSTKEYFILESIZE, f))
176 _("\nFailed to write to `%s': %s\n"),
184 if (UINT_MAX == make_keys)
185 fprintf(stderr, _("\nFinished!\n"));
187 fprintf(stderr, _("\nError, %u keys not generated\n"), make_keys);
193 print_hex(const char *msg, const void *buf, size_t size)
196 for (size_t i = 0; i < size; i++)
198 printf("%02hhx", ((const uint8_t *)buf)[i]);
205 print_examples_ecdh()
207 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv1;
208 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub1;
209 struct GNUNET_CRYPTO_EcdhePrivateKey *dh_priv2;
210 struct GNUNET_CRYPTO_EcdhePublicKey *dh_pub2;
211 struct GNUNET_HashCode hash;
214 dh_pub1 = GNUNET_new(struct GNUNET_CRYPTO_EcdhePublicKey);
215 dh_priv1 = GNUNET_CRYPTO_ecdhe_key_create();
216 dh_pub2 = GNUNET_new(struct GNUNET_CRYPTO_EcdhePublicKey);
217 dh_priv2 = GNUNET_CRYPTO_ecdhe_key_create();
218 GNUNET_CRYPTO_ecdhe_key_get_public(dh_priv1, dh_pub1);
219 GNUNET_CRYPTO_ecdhe_key_get_public(dh_priv2, dh_pub2);
221 GNUNET_assert(NULL !=
222 GNUNET_STRINGS_data_to_string(dh_priv1, 32, buf, 128));
223 printf("ECDHE key 1:\n");
224 printf("private: %s\n", buf);
225 print_hex("private(hex)", dh_priv1, sizeof *dh_priv1);
226 GNUNET_assert(NULL != GNUNET_STRINGS_data_to_string(dh_pub1, 32, buf, 128));
227 printf("public: %s\n", buf);
228 print_hex("public(hex)", dh_pub1, sizeof *dh_pub1);
230 GNUNET_assert(NULL !=
231 GNUNET_STRINGS_data_to_string(dh_priv2, 32, buf, 128));
232 printf("ECDHE key 2:\n");
233 printf("private: %s\n", buf);
234 print_hex("private(hex)", dh_priv2, sizeof *dh_priv2);
235 GNUNET_assert(NULL != GNUNET_STRINGS_data_to_string(dh_pub2, 32, buf, 128));
236 printf("public: %s\n", buf);
237 print_hex("public(hex)", dh_pub2, sizeof *dh_pub2);
239 GNUNET_assert(GNUNET_OK ==
240 GNUNET_CRYPTO_ecc_ecdh(dh_priv1, dh_pub2, &hash));
241 GNUNET_assert(NULL != GNUNET_STRINGS_data_to_string(&hash, 64, buf, 128));
242 printf("ECDH shared secret: %s\n", buf);
244 GNUNET_free(dh_priv1);
245 GNUNET_free(dh_priv2);
246 GNUNET_free(dh_pub1);
247 GNUNET_free(dh_pub2);
252 * Print some random example operations to stdout.
257 print_examples_ecdh();
258 // print_examples_ecdsa ();
259 // print_examples_eddsa ();
264 print_key(const char *filename)
266 struct GNUNET_DISK_FileHandle *fd;
267 struct GNUNET_CRYPTO_EddsaPrivateKey private_key;
268 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
272 unsigned int total_hostkeys;
276 if (GNUNET_YES != GNUNET_DISK_file_test(filename))
278 fprintf(stderr, _("Hostkeys file `%s' not found\n"), filename);
282 /* Check hostkey file size, read entire thing into memory */
284 GNUNET_DISK_file_size(filename, &fs, GNUNET_YES, GNUNET_YES))
288 fprintf(stderr, _("Hostkeys file `%s' is empty\n"), filename);
289 return; /* File is empty */
291 if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
293 fprintf(stderr, _("Incorrect hostkey file format: %s\n"), filename);
296 fd = GNUNET_DISK_file_open(filename,
297 GNUNET_DISK_OPEN_READ,
298 GNUNET_DISK_PERM_NONE);
301 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR, "open", filename);
304 hostkeys_data = GNUNET_malloc(fs);
305 sret = GNUNET_DISK_file_read(fd, hostkeys_data, fs);
306 if ((sret < 0) || (fs != (size_t)sret))
308 fprintf(stderr, _("Could not read hostkey file: %s\n"), filename);
309 GNUNET_free(hostkeys_data);
310 GNUNET_DISK_file_close(fd);
313 GNUNET_DISK_file_close(fd);
315 if (NULL == hostkeys_data)
317 total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
318 for (c = 0; (c < total_hostkeys) && (c < list_keys_count); c++)
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)
327 fprintf(stderr, "%4u: %s\n", c, hostkey_str);
328 GNUNET_free(hostkey_str);
331 fprintf(stderr, "%4u: %s\n", c, "invalid");
333 GNUNET_free(hostkeys_data);
338 * Main function that will be run by the scheduler.
340 * @param cls closure, NULL
341 * @param args remaining command-line arguments
342 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
343 * @param cfg configuration
349 const struct GNUNET_CONFIGURATION_Handle *cfg)
355 if (print_examples_flag)
362 fprintf(stderr, "%s", _("No hostkey file specified on command line\n"));
372 create_keys(args[0], args[1]);
375 if (print_public_key || print_public_key_hex || print_private_key)
378 struct GNUNET_DISK_FileHandle *keyfile;
379 struct GNUNET_CRYPTO_EddsaPrivateKey pk;
380 struct GNUNET_CRYPTO_EddsaPublicKey pub;
382 keyfile = GNUNET_DISK_file_open(args[0],
383 GNUNET_DISK_OPEN_READ,
384 GNUNET_DISK_PERM_NONE);
387 while (sizeof(pk) == GNUNET_DISK_file_read(keyfile, &pk, sizeof(pk)))
389 GNUNET_CRYPTO_eddsa_key_get_public(&pk, &pub);
390 if (print_public_key_hex)
392 print_hex("HEX:", &pub, sizeof(pub));
394 else if (print_public_key)
396 str = GNUNET_CRYPTO_eddsa_public_key_to_string(&pub);
397 fprintf(stdout, "%s\n", str);
400 else if (print_private_key)
402 str = GNUNET_CRYPTO_eddsa_private_key_to_string(&pk);
403 fprintf(stdout, "%s\n", str);
407 GNUNET_DISK_file_close(keyfile);
413 * Program to manipulate ECC key files.
415 * @param argc number of arguments from the command line
416 * @param argv command line arguments
417 * @return 0 ok, 1 on error
420 main(int argc, char *const *argv)
422 struct GNUNET_GETOPT_CommandLineOption options[] =
423 { GNUNET_GETOPT_option_flag('i',
426 "list keys included in a file (for testing)"),
428 GNUNET_GETOPT_option_uint(
432 gettext_noop("number of keys to list included in a file (for testing)"),
434 GNUNET_GETOPT_option_uint(
438 gettext_noop("create COUNT public-private key pairs (for testing)"),
440 GNUNET_GETOPT_option_flag('p',
443 "print the public key in ASCII format"),
445 GNUNET_GETOPT_option_flag('P',
448 "print the private key in ASCII format"),
450 GNUNET_GETOPT_option_flag('x',
453 "print the public key in HEX format"),
454 &print_public_key_hex),
455 GNUNET_GETOPT_option_flag(
459 "print examples of ECC operations (used for compatibility testing)"),
460 &print_examples_flag),
461 GNUNET_GETOPT_OPTION_END };
464 list_keys_count = UINT32_MAX;
465 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
469 GNUNET_PROGRAM_run(argc,
471 "gnunet-ecc [OPTIONS] keyfile [VANITY_PREFIX]",
473 "Manipulate GNUnet private ECC key files"),
479 GNUNET_free((void *)argv);
483 /* end of gnunet-ecc.c */