uncrustify as demanded.
[oweals/gnunet.git] / src / util / crypto_ecc_setup.c
index c556d805d3af28a936a5b53b2402c881f8e783a1..af42df613c3b232147651615aa2550b503c3959c 100644 (file)
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      Affero General Public License for more details.
-    
+
      You should have received a copy of the GNU Affero General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
      SPDX-License-Identifier: AGPL3.0-or-later
-*/
+ */
 
 /**
  * @file util/crypto_ecc_setup.c
 #include <gcrypt.h>
 #include "gnunet_util_lib.h"
 
-#define LOG(kind, ...) GNUNET_log_from (kind, "util-crypto-ecc", __VA_ARGS__)
+#define LOG(kind, ...) GNUNET_log_from(kind, "util-crypto-ecc", __VA_ARGS__)
 
 #define LOG_STRERROR(kind, syscall) \
-  GNUNET_log_from_strerror (kind, "util-crypto-ecc", syscall)
+  GNUNET_log_from_strerror(kind, "util-crypto-ecc", syscall)
 
 #define LOG_STRERROR_FILE(kind, syscall, filename) \
-  GNUNET_log_from_strerror_file (kind, "util-crypto-ecc", syscall, filename)
+  GNUNET_log_from_strerror_file(kind, "util-crypto-ecc", syscall, filename)
 
 /**
  * Log an error message at log-level 'level' that indicates
  */
 #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)
+    {                                                   \
+      LOG(level,                                       \
+          _("`%s' failed at %s:%d with error: %s\n"), \
+          cmd,                                         \
+          __FILE__,                                    \
+          __LINE__,                                    \
+          gcry_strerror(rc));                         \
+    } while (0)
 
 
 /**
  * some 'real' work).
  */
 static void
-short_wait ()
+short_wait()
 {
   struct GNUNET_TIME_Relative timeout;
 
-  timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
-  (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
+  timeout = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 100);
+  (void)GNUNET_NETWORK_socket_select(NULL, NULL, NULL, timeout);
 }
 
 
@@ -84,7 +84,7 @@ short_wait ()
  *   permission denied)
  */
 struct GNUNET_CRYPTO_EddsaPrivateKey *
-GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
+GNUNET_CRYPTO_eddsa_key_create_from_file(const char *filename)
 {
   struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
   struct GNUNET_DISK_FileHandle *fd;
@@ -93,159 +93,159 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
   uint64_t fs;
   ssize_t sret;
 
-  if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
+  if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file(filename))
     return NULL;
-  while (GNUNET_YES != GNUNET_DISK_file_test (filename))
-  {
-    fd =
-      GNUNET_DISK_file_open (filename,
-                             GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
-                               GNUNET_DISK_OPEN_FAILIFEXISTS,
-                             GNUNET_DISK_PERM_USER_READ |
-                               GNUNET_DISK_PERM_USER_WRITE);
-    if (NULL == fd)
+  while (GNUNET_YES != GNUNET_DISK_file_test(filename))
     {
-      if (EEXIST == errno)
-      {
-        if (GNUNET_YES != GNUNET_DISK_file_test (filename))
+      fd =
+        GNUNET_DISK_file_open(filename,
+                              GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
+                              GNUNET_DISK_OPEN_FAILIFEXISTS,
+                              GNUNET_DISK_PERM_USER_READ |
+                              GNUNET_DISK_PERM_USER_WRITE);
+      if (NULL == fd)
         {
-          /* must exist but not be accessible, fail for good! */
-          if (0 != access (filename, R_OK))
-            LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
-          else
-            GNUNET_break (0); /* what is going on!? */
+          if (EEXIST == errno)
+            {
+              if (GNUNET_YES != GNUNET_DISK_file_test(filename))
+                {
+                  /* must exist but not be accessible, fail for good! */
+                  if (0 != access(filename, R_OK))
+                    LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "access", filename);
+                  else
+                    GNUNET_break(0); /* what is going on!? */
+                  return NULL;
+                }
+              continue;
+            }
+          LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "open", filename);
           return NULL;
         }
-        continue;
-      }
-      LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
-      return NULL;
-    }
-    cnt = 0;
-    while (GNUNET_YES !=
-           GNUNET_DISK_file_lock (fd,
+      cnt = 0;
+      while (GNUNET_YES !=
+             GNUNET_DISK_file_lock(fd,
+                                   0,
+                                   sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey),
+                                   GNUNET_YES))
+        {
+          short_wait();
+          if (0 == ++cnt % 10)
+            {
+              ec = errno;
+              LOG(GNUNET_ERROR_TYPE_ERROR,
+                  _("Could not acquire lock on file `%s': %s...\n"),
+                  filename,
+                  strerror(ec));
+            }
+        }
+      LOG(GNUNET_ERROR_TYPE_INFO,
+          _("Creating a new private key.  This may take a while.\n"));
+      priv = GNUNET_CRYPTO_eddsa_key_create();
+      GNUNET_assert(NULL != priv);
+      GNUNET_assert(sizeof(*priv) ==
+                    GNUNET_DISK_file_write(fd, priv, sizeof(*priv)));
+      GNUNET_DISK_file_sync(fd);
+      if (GNUNET_YES !=
+          GNUNET_DISK_file_unlock(fd,
                                   0,
-                                  sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey),
-                                  GNUNET_YES))
-    {
-      short_wait ();
-      if (0 == ++cnt % 10)
-      {
-        ec = errno;
-        LOG (GNUNET_ERROR_TYPE_ERROR,
-             _ ("Could not acquire lock on file `%s': %s...\n"),
-             filename,
-             strerror (ec));
-      }
+                                  sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey)))
+        LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
+      GNUNET_assert(GNUNET_YES == GNUNET_DISK_file_close(fd));
+      return priv;
     }
-    LOG (GNUNET_ERROR_TYPE_INFO,
-         _ ("Creating a new private key.  This may take a while.\n"));
-    priv = GNUNET_CRYPTO_eddsa_key_create ();
-    GNUNET_assert (NULL != priv);
-    GNUNET_assert (sizeof (*priv) ==
-                   GNUNET_DISK_file_write (fd, priv, sizeof (*priv)));
-    GNUNET_DISK_file_sync (fd);
-    if (GNUNET_YES !=
-        GNUNET_DISK_file_unlock (fd,
-                                 0,
-                                 sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
-      LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
-    GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
-    return priv;
-  }
   /* key file exists already, read it! */
-  fd = GNUNET_DISK_file_open (filename,
-                              GNUNET_DISK_OPEN_READ,
-                              GNUNET_DISK_PERM_NONE);
+  fd = GNUNET_DISK_file_open(filename,
+                             GNUNET_DISK_OPEN_READ,
+                             GNUNET_DISK_PERM_NONE);
   if (NULL == fd)
-  {
-    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
-    return NULL;
-  }
-  cnt = 0;
-  while (1)
-  {
-    if (GNUNET_YES !=
-        GNUNET_DISK_file_lock (fd,
-                               0,
-                               sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey),
-                               GNUNET_NO))
     {
-      if (0 == ++cnt % 60)
-      {
-        ec = errno;
-        LOG (GNUNET_ERROR_TYPE_ERROR,
-             _ ("Could not acquire lock on file `%s': %s...\n"),
-             filename,
-             strerror (ec));
-        LOG (
-          GNUNET_ERROR_TYPE_ERROR,
-          _ (
-            "This may be ok if someone is currently generating a private key.\n"));
-      }
-      short_wait ();
-      continue;
-    }
-    if (GNUNET_YES != GNUNET_DISK_file_test (filename))
-    {
-      /* eh, what!? File we opened is now gone!? */
-      LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
-      if (GNUNET_YES !=
-          GNUNET_DISK_file_unlock (fd,
-                                   0,
-                                   sizeof (
-                                     struct GNUNET_CRYPTO_EddsaPrivateKey)))
-        LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
-      GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
-
+      LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "open", filename);
       return NULL;
     }
-    if (GNUNET_OK !=
-        GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
-      fs = 0;
-    if (fs < sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey))
+  cnt = 0;
+  while (1)
     {
-      /* maybe we got the read lock before the key generating
-       * process had a chance to get the write lock; give it up! */
       if (GNUNET_YES !=
-          GNUNET_DISK_file_unlock (fd,
-                                   0,
-                                   sizeof (
-                                     struct GNUNET_CRYPTO_EddsaPrivateKey)))
-        LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
-      if (0 == ++cnt % 10)
-      {
-        LOG (GNUNET_ERROR_TYPE_ERROR,
-             _ (
-               "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
-             filename,
-             (unsigned int) fs,
-             (unsigned int) sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey));
-        LOG (GNUNET_ERROR_TYPE_ERROR,
-             _ ("This may be ok if someone is currently generating a key.\n"));
-      }
-      short_wait (); /* wait a bit longer! */
-      continue;
+          GNUNET_DISK_file_lock(fd,
+                                0,
+                                sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey),
+                                GNUNET_NO))
+        {
+          if (0 == ++cnt % 60)
+            {
+              ec = errno;
+              LOG(GNUNET_ERROR_TYPE_ERROR,
+                  _("Could not acquire lock on file `%s': %s...\n"),
+                  filename,
+                  strerror(ec));
+              LOG(
+                GNUNET_ERROR_TYPE_ERROR,
+                _(
+                  "This may be ok if someone is currently generating a private key.\n"));
+            }
+          short_wait();
+          continue;
+        }
+      if (GNUNET_YES != GNUNET_DISK_file_test(filename))
+        {
+          /* eh, what!? File we opened is now gone!? */
+          LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", filename);
+          if (GNUNET_YES !=
+              GNUNET_DISK_file_unlock(fd,
+                                      0,
+                                      sizeof(
+                                        struct GNUNET_CRYPTO_EddsaPrivateKey)))
+            LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
+          GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
+
+          return NULL;
+        }
+      if (GNUNET_OK !=
+          GNUNET_DISK_file_size(filename, &fs, GNUNET_YES, GNUNET_YES))
+        fs = 0;
+      if (fs < sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey))
+        {
+          /* maybe we got the read lock before the key generating
+           * process had a chance to get the write lock; give it up! */
+          if (GNUNET_YES !=
+              GNUNET_DISK_file_unlock(fd,
+                                      0,
+                                      sizeof(
+                                        struct GNUNET_CRYPTO_EddsaPrivateKey)))
+            LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
+          if (0 == ++cnt % 10)
+            {
+              LOG(GNUNET_ERROR_TYPE_ERROR,
+                  _(
+                    "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
+                  filename,
+                  (unsigned int)fs,
+                  (unsigned int)sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey));
+              LOG(GNUNET_ERROR_TYPE_ERROR,
+                  _("This may be ok if someone is currently generating a key.\n"));
+            }
+          short_wait(); /* wait a bit longer! */
+          continue;
+        }
+      break;
     }
-    break;
-  }
-  fs = sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey);
-  priv = GNUNET_malloc (fs);
-  sret = GNUNET_DISK_file_read (fd, priv, fs);
-  GNUNET_assert ((sret >= 0) && (fs == (size_t) sret));
+  fs = sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey);
+  priv = GNUNET_malloc(fs);
+  sret = GNUNET_DISK_file_read(fd, priv, fs);
+  GNUNET_assert((sret >= 0) && (fs == (size_t)sret));
   if (GNUNET_YES !=
-      GNUNET_DISK_file_unlock (fd,
-                               0,
-                               sizeof (struct GNUNET_CRYPTO_EddsaPrivateKey)))
-    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
-  GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
+      GNUNET_DISK_file_unlock(fd,
+                              0,
+                              sizeof(struct GNUNET_CRYPTO_EddsaPrivateKey)))
+    LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
+  GNUNET_assert(GNUNET_YES == GNUNET_DISK_file_close(fd));
 #if CRYPTO_BUG
-  if (GNUNET_OK != check_eddsa_key (priv))
-  {
-    GNUNET_break (0);
-    GNUNET_free (priv);
-    return NULL;
-  }
+  if (GNUNET_OK != check_eddsa_key(priv))
+    {
+      GNUNET_break(0);
+      GNUNET_free(priv);
+      return NULL;
+    }
 #endif
   return priv;
 }
@@ -266,7 +266,7 @@ GNUNET_CRYPTO_eddsa_key_create_from_file (const char *filename)
  *   permission denied)
  */
 struct GNUNET_CRYPTO_EcdsaPrivateKey *
-GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
+GNUNET_CRYPTO_ecdsa_key_create_from_file(const char *filename)
 {
   struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;
   struct GNUNET_DISK_FileHandle *fd;
@@ -275,152 +275,152 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
   uint64_t fs;
   ssize_t sret;
 
-  if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file (filename))
+  if (GNUNET_SYSERR == GNUNET_DISK_directory_create_for_file(filename))
     return NULL;
-  while (GNUNET_YES != GNUNET_DISK_file_test (filename))
-  {
-    fd =
-      GNUNET_DISK_file_open (filename,
-                             GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
-                               GNUNET_DISK_OPEN_FAILIFEXISTS,
-                             GNUNET_DISK_PERM_USER_READ |
-                               GNUNET_DISK_PERM_USER_WRITE);
-    if (NULL == fd)
+  while (GNUNET_YES != GNUNET_DISK_file_test(filename))
     {
-      if (EEXIST == errno)
-      {
-        if (GNUNET_YES != GNUNET_DISK_file_test (filename))
+      fd =
+        GNUNET_DISK_file_open(filename,
+                              GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE |
+                              GNUNET_DISK_OPEN_FAILIFEXISTS,
+                              GNUNET_DISK_PERM_USER_READ |
+                              GNUNET_DISK_PERM_USER_WRITE);
+      if (NULL == fd)
         {
-          /* must exist but not be accessible, fail for good! */
-          if (0 != access (filename, R_OK))
-            LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "access", filename);
-          else
-            GNUNET_break (0); /* what is going on!? */
+          if (EEXIST == errno)
+            {
+              if (GNUNET_YES != GNUNET_DISK_file_test(filename))
+                {
+                  /* must exist but not be accessible, fail for good! */
+                  if (0 != access(filename, R_OK))
+                    LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "access", filename);
+                  else
+                    GNUNET_break(0); /* what is going on!? */
+                  return NULL;
+                }
+              continue;
+            }
+          LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "open", filename);
           return NULL;
         }
-        continue;
-      }
-      LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
-      return NULL;
-    }
-    cnt = 0;
-    while (GNUNET_YES !=
-           GNUNET_DISK_file_lock (fd,
+      cnt = 0;
+      while (GNUNET_YES !=
+             GNUNET_DISK_file_lock(fd,
+                                   0,
+                                   sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey),
+                                   GNUNET_YES))
+        {
+          short_wait();
+          if (0 == ++cnt % 10)
+            {
+              ec = errno;
+              LOG(GNUNET_ERROR_TYPE_ERROR,
+                  _("Could not acquire lock on file `%s': %s...\n"),
+                  filename,
+                  strerror(ec));
+            }
+        }
+      LOG(GNUNET_ERROR_TYPE_INFO,
+          _("Creating a new private key.  This may take a while.\n"));
+      priv = GNUNET_CRYPTO_ecdsa_key_create();
+      GNUNET_assert(NULL != priv);
+      GNUNET_assert(sizeof(*priv) ==
+                    GNUNET_DISK_file_write(fd, priv, sizeof(*priv)));
+      GNUNET_DISK_file_sync(fd);
+      if (GNUNET_YES !=
+          GNUNET_DISK_file_unlock(fd,
                                   0,
-                                  sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
-                                  GNUNET_YES))
-    {
-      short_wait ();
-      if (0 == ++cnt % 10)
-      {
-        ec = errno;
-        LOG (GNUNET_ERROR_TYPE_ERROR,
-             _ ("Could not acquire lock on file `%s': %s...\n"),
-             filename,
-             strerror (ec));
-      }
+                                  sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey)))
+        LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
+      GNUNET_assert(GNUNET_YES == GNUNET_DISK_file_close(fd));
+      return priv;
     }
-    LOG (GNUNET_ERROR_TYPE_INFO,
-         _ ("Creating a new private key.  This may take a while.\n"));
-    priv = GNUNET_CRYPTO_ecdsa_key_create ();
-    GNUNET_assert (NULL != priv);
-    GNUNET_assert (sizeof (*priv) ==
-                   GNUNET_DISK_file_write (fd, priv, sizeof (*priv)));
-    GNUNET_DISK_file_sync (fd);
-    if (GNUNET_YES !=
-        GNUNET_DISK_file_unlock (fd,
-                                 0,
-                                 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
-      LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
-    GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
-    return priv;
-  }
   /* key file exists already, read it! */
-  fd = GNUNET_DISK_file_open (filename,
-                              GNUNET_DISK_OPEN_READ,
-                              GNUNET_DISK_PERM_NONE);
+  fd = GNUNET_DISK_file_open(filename,
+                             GNUNET_DISK_OPEN_READ,
+                             GNUNET_DISK_PERM_NONE);
   if (NULL == fd)
-  {
-    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_ERROR, "open", filename);
-    return NULL;
-  }
-  cnt = 0;
-  while (1)
-  {
-    if (GNUNET_YES !=
-        GNUNET_DISK_file_lock (fd,
-                               0,
-                               sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
-                               GNUNET_NO))
     {
-      if (0 == ++cnt % 60)
-      {
-        ec = errno;
-        LOG (GNUNET_ERROR_TYPE_ERROR,
-             _ ("Could not acquire lock on file `%s': %s...\n"),
-             filename,
-             strerror (ec));
-        LOG (
-          GNUNET_ERROR_TYPE_ERROR,
-          _ (
-            "This may be ok if someone is currently generating a private key.\n"));
-      }
-      short_wait ();
-      continue;
-    }
-    if (GNUNET_YES != GNUNET_DISK_file_test (filename))
-    {
-      /* eh, what!? File we opened is now gone!? */
-      LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "stat", filename);
-      if (GNUNET_YES !=
-          GNUNET_DISK_file_unlock (fd,
-                                   0,
-                                   sizeof (
-                                     struct GNUNET_CRYPTO_EcdsaPrivateKey)))
-        LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
-      GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd));
-
+      LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_ERROR, "open", filename);
       return NULL;
     }
-    if (GNUNET_OK !=
-        GNUNET_DISK_file_size (filename, &fs, GNUNET_YES, GNUNET_YES))
-      fs = 0;
-    if (fs < sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))
+  cnt = 0;
+  while (1)
     {
-      /* maybe we got the read lock before the key generating
-       * process had a chance to get the write lock; give it up! */
       if (GNUNET_YES !=
-          GNUNET_DISK_file_unlock (fd,
-                                   0,
-                                   sizeof (
-                                     struct GNUNET_CRYPTO_EcdsaPrivateKey)))
-        LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
-      if (0 == ++cnt % 10)
-      {
-        LOG (GNUNET_ERROR_TYPE_ERROR,
-             _ (
-               "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
-             filename,
-             (unsigned int) fs,
-             (unsigned int) sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
-        LOG (GNUNET_ERROR_TYPE_ERROR,
-             _ ("This may be ok if someone is currently generating a key.\n"));
-      }
-      short_wait (); /* wait a bit longer! */
-      continue;
+          GNUNET_DISK_file_lock(fd,
+                                0,
+                                sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey),
+                                GNUNET_NO))
+        {
+          if (0 == ++cnt % 60)
+            {
+              ec = errno;
+              LOG(GNUNET_ERROR_TYPE_ERROR,
+                  _("Could not acquire lock on file `%s': %s...\n"),
+                  filename,
+                  strerror(ec));
+              LOG(
+                GNUNET_ERROR_TYPE_ERROR,
+                _(
+                  "This may be ok if someone is currently generating a private key.\n"));
+            }
+          short_wait();
+          continue;
+        }
+      if (GNUNET_YES != GNUNET_DISK_file_test(filename))
+        {
+          /* eh, what!? File we opened is now gone!? */
+          LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "stat", filename);
+          if (GNUNET_YES !=
+              GNUNET_DISK_file_unlock(fd,
+                                      0,
+                                      sizeof(
+                                        struct GNUNET_CRYPTO_EcdsaPrivateKey)))
+            LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
+          GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fd));
+
+          return NULL;
+        }
+      if (GNUNET_OK !=
+          GNUNET_DISK_file_size(filename, &fs, GNUNET_YES, GNUNET_YES))
+        fs = 0;
+      if (fs < sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey))
+        {
+          /* maybe we got the read lock before the key generating
+           * process had a chance to get the write lock; give it up! */
+          if (GNUNET_YES !=
+              GNUNET_DISK_file_unlock(fd,
+                                      0,
+                                      sizeof(
+                                        struct GNUNET_CRYPTO_EcdsaPrivateKey)))
+            LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
+          if (0 == ++cnt % 10)
+            {
+              LOG(GNUNET_ERROR_TYPE_ERROR,
+                  _(
+                    "When trying to read key file `%s' I found %u bytes but I need at least %u.\n"),
+                  filename,
+                  (unsigned int)fs,
+                  (unsigned int)sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey));
+              LOG(GNUNET_ERROR_TYPE_ERROR,
+                  _("This may be ok if someone is currently generating a key.\n"));
+            }
+          short_wait(); /* wait a bit longer! */
+          continue;
+        }
+      break;
     }
-    break;
-  }
-  fs = sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
-  priv = GNUNET_malloc (fs);
-  sret = GNUNET_DISK_file_read (fd, priv, fs);
-  GNUNET_assert ((sret >= 0) && (fs == (size_t) sret));
+  fs = sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey);
+  priv = GNUNET_malloc(fs);
+  sret = GNUNET_DISK_file_read(fd, priv, fs);
+  GNUNET_assert((sret >= 0) && (fs == (size_t)sret));
   if (GNUNET_YES !=
-      GNUNET_DISK_file_unlock (fd,
-                               0,
-                               sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
-    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
-  GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd));
+      GNUNET_DISK_file_unlock(fd,
+                              0,
+                              sizeof(struct GNUNET_CRYPTO_EcdsaPrivateKey)))
+    LOG_STRERROR_FILE(GNUNET_ERROR_TYPE_WARNING, "fcntl", filename);
+  GNUNET_assert(GNUNET_YES == GNUNET_DISK_file_close(fd));
   return priv;
 }
 
@@ -434,17 +434,17 @@ GNUNET_CRYPTO_ecdsa_key_create_from_file (const char *filename)
  *   permission denied)
  */
 struct GNUNET_CRYPTO_EddsaPrivateKey *
-GNUNET_CRYPTO_eddsa_key_create_from_configuration (
+GNUNET_CRYPTO_eddsa_key_create_from_configuration(
   const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
   char *fn;
 
   if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &fn))
+      GNUNET_CONFIGURATION_get_value_filename(cfg, "PEER", "PRIVATE_KEY", &fn))
     return NULL;
-  priv = GNUNET_CRYPTO_eddsa_key_create_from_file (fn);
-  GNUNET_free (fn);
+  priv = GNUNET_CRYPTO_eddsa_key_create_from_file(fn);
+  GNUNET_free(fn);
   return priv;
 }
 
@@ -458,19 +458,19 @@ GNUNET_CRYPTO_eddsa_key_create_from_configuration (
  *         could not be retrieved
  */
 int
-GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
-                                 struct GNUNET_PeerIdentity *dst)
+GNUNET_CRYPTO_get_peer_identity(const struct GNUNET_CONFIGURATION_Handle *cfg,
+                                struct GNUNET_PeerIdentity *dst)
 {
   struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
 
-  if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg)))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                ("Could not load peer's private key\n"));
-    return GNUNET_SYSERR;
-  }
-  GNUNET_CRYPTO_eddsa_key_get_public (priv, &dst->public_key);
-  GNUNET_free (priv);
+  if (NULL == (priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration(cfg)))
+    {
+      GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
+                 _("Could not load peer's private key\n"));
+      return GNUNET_SYSERR;
+    }
+  GNUNET_CRYPTO_eddsa_key_get_public(priv, &dst->public_key);
+  GNUNET_free(priv);
   return GNUNET_OK;
 }
 
@@ -484,17 +484,17 @@ GNUNET_CRYPTO_get_peer_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
  * @param cfg_name name of the configuration file to use
  */
 void
-GNUNET_CRYPTO_eddsa_setup_key (const char *cfg_name)
+GNUNET_CRYPTO_eddsa_setup_key(const char *cfg_name)
 {
   struct GNUNET_CONFIGURATION_Handle *cfg;
   struct GNUNET_CRYPTO_EddsaPrivateKey *priv;
 
-  cfg = GNUNET_CONFIGURATION_create ();
-  (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
-  priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
+  cfg = GNUNET_CONFIGURATION_create();
+  (void)GNUNET_CONFIGURATION_load(cfg, cfg_name);
+  priv = GNUNET_CRYPTO_eddsa_key_create_from_configuration(cfg);
   if (NULL != priv)
-    GNUNET_free (priv);
-  GNUNET_CONFIGURATION_destroy (cfg);
+    GNUNET_free(priv);
+  GNUNET_CONFIGURATION_destroy(cfg);
 }
 
 /* end of crypto_ecc_setup.c */