2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2015 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file util/crypto_ecc_setup.c
23 * @brief helper function for easy EdDSA key setup
24 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)
32 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)
34 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename)
37 * Log an error message at log-level 'level' that indicates
38 * a failure of the command 'cmd' with the message given
39 * by gcry_strerror(rc).
41 #define LOG_GCRY(level, cmd, rc) do { LOG(level, _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, gcry_strerror(rc)); } while(0)
45 * Wait for a short time (we're trying to lock a file or want
46 * to give another process a shot at finishing a disk write, etc.).
47 * Sleeps for 100ms (as that should be long enough for virtually all
48 * modern systems to context switch and allow another process to do
54 struct GNUNET_TIME_Relative timeout;
56 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
57 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
62 * Create a new private key by reading it from a file. If the
63 * files does not exist, create a new key and write it to the
64 * file. Caller must free return value. Note that this function
65 * can not guarantee that another process might not be trying
66 * the same operation on the same file at the same time.
67 * If the contents of the file
68 * are invalid the old file is deleted and a fresh key is
71 * @param filename name of file to use to store the key
72 * @return new private key, NULL on error (for example,
75 struct GNUNET_CRYPTO_EddsaPrivateKey *
76 GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
78 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
79 struct GNUNET_DISK_FileHandle *fd;
85 if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
87 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
89 fd = GNUNET_DISK_file_open (filename,
90 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
91 | GNUNET_DISK_OPEN_FAILIFEXISTS,
92 GNUNET_DISK_PERM_USER_READ |
93 GNUNET_DISK_PERM_USER_WRITE);
98 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
100 /* must exist but not be accessible, fail for good! */
101 if (0 != ACCESS (filename, R_OK))
102 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
104 GNUNET_break (0); /* what is going on!? */
109 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
114 GNUNET_DISK_file_lock (fd, 0,
115 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey),
122 LOG (GNUNET_ERROR_TYPE_ERROR,
123 _("Could not acquire lock on file `%s': %s...\n"),
128 LOG (GNUNET_ERROR_TYPE_INFO,
129 _("Creating a new private key. This may take a while.\n"));
130 priv = GNUNET_CRYPTO_eddsa_key_create ();
131 GNUNET_assert (NULL != priv);
132 GNUNET_assert (sizeof (*priv) ==
133 GNUNET_DISK_file_write (fd, priv, sizeof (*priv)));
134 GNUNET_DISK_file_sync (fd);
136 GNUNET_DISK_file_unlock (fd, 0,
137 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
138 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
139 GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
142 /* key file exists already, read it! */
143 fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
144 GNUNET_DISK_PERM_NONE);
147 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
154 GNUNET_DISK_file_lock (fd, 0,
155 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey),
161 LOG (GNUNET_ERROR_TYPE_ERROR,
162 _("Could not acquire lock on file `%s': %s...\n"), filename,
164 LOG (GNUNET_ERROR_TYPE_ERROR,
166 ("This may be ok if someone is currently generating a private key.\n"));
171 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
173 /* eh, what!? File we opened is now gone!? */
174 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
176 GNUNET_DISK_file_unlock (fd, 0,
177 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
178 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
179 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
183 if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
185 if (fs < sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey))
187 /* maybe we got the read lock before the key generating
188 * process had a chance to get the write lock; give it up! */
190 GNUNET_DISK_file_unlock (fd, 0,
191 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
192 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
195 LOG (GNUNET_ERROR_TYPE_ERROR,
196 _("When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
199 (unsigned int) sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey));
200 LOG (GNUNET_ERROR_TYPE_ERROR,
201 _("This may be ok if someone is currently generating a key.\n"));
203 short_wait (); /* wait a bit longer! */
208 fs = sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey);
209 priv = GNUNET_malloc (fs);
210 sret = GNUNET_DISK_file_read (fd,
213 GNUNET_assert ( (sret >= 0) &&
214 (fs == (size_t) sret) );
216 GNUNET_DISK_file_unlock (fd,
218 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
219 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
222 GNUNET_assert (GNUNET_YES ==
223 GNUNET_DISK_file_close (fd));
229 * Create a new private key by reading it from a file. If the
230 * files does not exist, create a new key and write it to the
231 * file. Caller must free return value. Note that this function
232 * can not guarantee that another process might not be trying
233 * the same operation on the same file at the same time.
234 * If the contents of the file
235 * are invalid the old file is deleted and a fresh key is
238 * @param filename name of file to use to store the key
239 * @return new private key, NULL on error (for example,
242 struct GNUNET_CRYPTO_EcdsaPrivateKey *
243 GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
245 struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
246 struct GNUNET_DISK_FileHandle *fd;
253 GNUNET_DISK_directory_create_for_file (filename))
255 while (GNUNET_YES != GNUNET_DISK_file_test (filename))
257 fd = GNUNET_DISK_file_open (filename,
258 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE
259 | GNUNET_DISK_OPEN_FAILIFEXISTS,
260 GNUNET_DISK_PERM_USER_READ |
261 GNUNET_DISK_PERM_USER_WRITE);
266 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
268 /* must exist but not be accessible, fail for good! */
269 if (0 != ACCESS (filename, R_OK))
270 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR,
274 GNUNET_break (0); /* what is going on!? */
279 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR,
286 GNUNET_DISK_file_lock (fd,
288 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
295 LOG (GNUNET_ERROR_TYPE_ERROR,
296 _("Could not acquire lock on file `%s': %s...\n"),
301 LOG (GNUNET_ERROR_TYPE_INFO,
302 _("Creating a new private key. This may take a while.\n"));
303 priv = GNUNET_CRYPTO_ecdsa_key_create ();
304 GNUNET_assert (NULL != priv);
305 GNUNET_assert (sizeof (*priv) ==
306 GNUNET_DISK_file_write (fd,
309 GNUNET_DISK_file_sync (fd);
311 GNUNET_DISK_file_unlock (fd, 0,
312 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
313 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
316 GNUNET_assert (GNUNET_YES ==
317 GNUNET_DISK_file_close (fd));
320 /* key file exists already, read it! */
321 fd = GNUNET_DISK_file_open (filename,
322 GNUNET_DISK_OPEN_READ,
323 GNUNET_DISK_PERM_NONE);
326 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR,
335 GNUNET_DISK_file_lock (fd, 0,
336 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
342 LOG (GNUNET_ERROR_TYPE_ERROR,
343 _("Could not acquire lock on file `%s': %s...\n"),
346 LOG (GNUNET_ERROR_TYPE_ERROR,
347 _("This may be ok if someone is currently generating a private key.\n"));
353 GNUNET_DISK_file_test (filename))
355 /* eh, what!? File we opened is now gone!? */
356 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
360 GNUNET_DISK_file_unlock (fd, 0,
361 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
362 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
365 GNUNET_assert (GNUNET_OK ==
366 GNUNET_DISK_file_close (fd));
371 GNUNET_DISK_file_size (filename,
376 if (fs < sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))
378 /* maybe we got the read lock before the key generating
379 * process had a chance to get the write lock; give it up! */
381 GNUNET_DISK_file_unlock (fd, 0,
382 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
383 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
388 LOG (GNUNET_ERROR_TYPE_ERROR,
389 _("When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
390 filename, (unsigned int) fs,
391 (unsigned int) sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
392 LOG (GNUNET_ERROR_TYPE_ERROR,
393 _("This may be ok if someone is currently generating a key.\n"));
395 short_wait (); /* wait a bit longer! */
400 fs = sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
401 priv = GNUNET_malloc (fs);
402 sret = GNUNET_DISK_file_read (fd,
405 GNUNET_assert ( (sret >= 0) &&
406 (fs == (size_t) sret) );
408 GNUNET_DISK_file_unlock (fd, 0,
409 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
410 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING,
413 GNUNET_assert (GNUNET_YES ==
414 GNUNET_DISK_file_close (fd));
420 * Create a new private key by reading our peer's key from
421 * the file specified in the configuration.
423 * @param cfg the configuration to use
424 * @return new private key, NULL on error (for example,
427 struct GNUNET_CRYPTO_EddsaPrivateKey *
428 GNUNET_CRYPTO_eddsa_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
430 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
434 GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &fn))
436 priv = GNUNET_CRYPTO_eddsa_key_create_from_file (fn);
443 * Retrieve the identity of the host's peer.
445 * @param cfg configuration to use
446 * @param dst pointer to where to write the peer identity
447 * @return #GNUNET_OK on success, #GNUNET_SYSERR if the identity
448 * could not be retrieved
451 GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
452 struct GNUNET_PeerIdentity *dst)
454 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
456 if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg)))
458 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
459 _("Could not load peer's private key\n"));
460 return GNUNET_SYSERR;
462 GNUNET_CRYPTO_eddsa_key_get_public (priv, &dst->public_key);
469 * Setup a key file for a peer given the name of the
470 * configuration file (!). This function is used so that
471 * at a later point code can be certain that reading a
472 * key is fast (for example in time-dependent testcases).
474 * @param cfg_name name of the configuration file to use
477 GNUNET_CRYPTO_eddsa_setup_key (const char *cfg_name)
479 struct GNUNET_CONFIGURATION_Handle *cfg;
480 struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
482 cfg = GNUNET_CONFIGURATION_create ();
483 (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
484 priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
487 GNUNET_CONFIGURATION_destroy (cfg);
490 /* end of crypto_ecc_setup.c */