Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / util / crypto_mpi.c
index 8e52424cf808d1634b4620c8a094c21536ff155f..ff3e9a8a72aaab5c6c2e4e805554c30adc48d3ef 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2012, 2013 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2012, 2013 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
  */
 #include "platform.h"
 #include <gcrypt.h>
-#include "gnunet_util_lib.h"
+#include "gnunet_crypto_lib.h"
 
 
-#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+#define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-mpi", __VA_ARGS__)
 
 /**
  * Log an error message at log-level 'level' that indicates
@@ -52,9 +52,11 @@ adjust (void *buf,
        size_t size,
        size_t target)
 {
+  char *p = buf;
+
   if (size < target)
   {
-    memmove (&buf[target - size], buf, size);
+    memmove (&p[target - size], buf, size);
     memset (buf, 0, target - size);
   }
 }
@@ -75,6 +77,7 @@ GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
                                   gcry_mpi_t val)
 {
   size_t rsize;
+  int rc;
 
   if (gcry_mpi_get_flag (val, GCRYMPI_FLAG_OPAQUE))
   {
@@ -87,7 +90,7 @@ GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
     rsize = (nbits+7)/8;
     if (rsize > size)
       rsize = size;
-    memcpy (buf, p, rsize);
+    GNUNET_memcpy (buf, p, rsize);
     if (rsize < size)
       memset (buf+rsize, 0, size - rsize);
   }
@@ -96,9 +99,17 @@ GNUNET_CRYPTO_mpi_print_unsigned (void *buf,
     /* Store regular MPIs as unsigned integers right aligned into
        the buffer.  */
     rsize = size;
-    GNUNET_assert (0 ==
-                   gcry_mpi_print (GCRYMPI_FMT_USG, buf, rsize, &rsize,
-                                   val));
+    if (0 !=
+        (rc = gcry_mpi_print (GCRYMPI_FMT_USG,
+                              buf,
+                              rsize, &rsize,
+                              val)))
+    {
+      LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
+                "gcry_mpi_print",
+                rc);
+      GNUNET_assert (0);
+    }
     adjust (buf, rsize, size);
   }
 }
@@ -124,7 +135,9 @@ GNUNET_CRYPTO_mpi_scan_unsigned (gcry_mpi_t *result,
                                GCRYMPI_FMT_USG,
                                data, size, &size)))
   {
-    LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
+    LOG_GCRY (GNUNET_ERROR_TYPE_ERROR,
+              "gcry_mpi_scan",
+              rc);
     GNUNET_assert (0);
   }
 }