tolerate additional IPv4 address now available for gnunet.org
[oweals/gnunet.git] / src / util / crypto_hash.c
index 14044845970523f58c576bcd042af69d63e7c0cc..925d6b94dae969d8a0789a17869073231ddac5c1 100644 (file)
@@ -2,20 +2,20 @@
      This file is part of GNUnet.
      Copyright (C) 2001-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
-     by the Free Software Foundation; either version 3, or (at your
-     option) any later version.
+     GNUnet is free software: you can redistribute it and/or modify it
+     under the terms of the GNU Affero General Public License as published
+     by the Free Software Foundation, either version 3 of the License,
+     or (at your option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
      WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-     General Public License for more details.
+     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/>.
 
-     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., 51 Franklin Street, Fifth Floor,
-     Boston, MA 02110-1301, USA.
+     SPDX-License-Identifier: AGPL3.0-or-later
 
 */
 /**
 #include "platform.h"
 #include "gnunet_crypto_lib.h"
 #include "gnunet_strings_lib.h"
+#include "benchmark.h"
 #include <gcrypt.h>
 
-#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+#define LOG(kind,...) GNUNET_log_from (kind, "util-crypto-hash", __VA_ARGS__)
 
-#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
+#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util-crypto-hash", syscall, filename)
 
 /**
  * Hash block of given size.
@@ -44,7 +45,9 @@ GNUNET_CRYPTO_hash (const void *block,
                     size_t size,
                     struct GNUNET_HashCode *ret)
 {
+  BENCHMARK_START (hash);
   gcry_md_hash_buffer (GCRY_MD_SHA512, ret, block, size);
+  BENCHMARK_END (hash);
 }
 
 
@@ -69,9 +72,9 @@ GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode *block,
   char *np;
 
   np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block,
-                                     sizeof (struct GNUNET_HashCode),
-                                     (char*) result,
-                                     sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
+                                      sizeof (struct GNUNET_HashCode),
+                                      (char *) result,
+                                      sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
   GNUNET_assert (NULL != np);
   *np = '\0';
 }
@@ -367,14 +370,17 @@ GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
 
 /**
  * Calculate HMAC of a message (RFC 2104)
+ * TODO: Shouldn' this be the standard hmac function and
+ * the above be renamed?
  *
  * @param key secret key
+ * @param key_len secret key length
  * @param plaintext input plaintext
  * @param plaintext_len length of @a plaintext
  * @param hmac where to store the hmac
  */
 void
-GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
+GNUNET_CRYPTO_hmac_raw (const void *key, size_t key_len,
                     const void *plaintext, size_t plaintext_len,
                     struct GNUNET_HashCode *hmac)
 {
@@ -392,11 +398,30 @@ GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
   {
     gcry_md_reset (md);
   }
-  gcry_md_setkey (md, key->key, sizeof (key->key));
+  gcry_md_setkey (md, key, key_len);
   gcry_md_write (md, plaintext, plaintext_len);
   mc = gcry_md_read (md, GCRY_MD_SHA512);
   GNUNET_assert (NULL != mc);
-  memcpy (hmac->bits, mc, sizeof (hmac->bits));
+  GNUNET_memcpy (hmac->bits, mc, sizeof (hmac->bits));
+}
+
+
+/**
+ * Calculate HMAC of a message (RFC 2104)
+ *
+ * @param key secret key
+ * @param plaintext input plaintext
+ * @param plaintext_len length of @a plaintext
+ * @param hmac where to store the hmac
+ */
+void
+GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
+                    const void *plaintext, size_t plaintext_len,
+                    struct GNUNET_HashCode *hmac)
+{
+  GNUNET_CRYPTO_hmac_raw ((void*) key->key, sizeof (key->key),
+                          plaintext, plaintext_len,
+                          hmac);
 }
 
 
@@ -422,11 +447,16 @@ GNUNET_CRYPTO_hash_context_start ()
 {
   struct GNUNET_HashContext *hc;
 
+  BENCHMARK_START (hash_context_start);
+
   hc = GNUNET_new (struct GNUNET_HashContext);
   GNUNET_assert (0 ==
                  gcry_md_open (&hc->hd,
                                GCRY_MD_SHA512,
                                0));
+
+  BENCHMARK_END (hash_context_start);
+
   return hc;
 }
 
@@ -440,10 +470,12 @@ GNUNET_CRYPTO_hash_context_start ()
  */
 void
 GNUNET_CRYPTO_hash_context_read (struct GNUNET_HashContext *hc,
-                         const void *buf,
-                         size_t size)
+                                const void *buf,
+                                size_t size)
 {
+  BENCHMARK_START (hash_context_read);
   gcry_md_write (hc->hd, buf, size);
+  BENCHMARK_END (hash_context_read);
 }
 
 
@@ -459,12 +491,15 @@ GNUNET_CRYPTO_hash_context_finish (struct GNUNET_HashContext *hc,
 {
   const void *res = gcry_md_read (hc->hd, 0);
 
+  BENCHMARK_START (hash_context_finish);
+
   GNUNET_assert (NULL != res);
   if (NULL != r_hash)
-    memcpy (r_hash,
+    GNUNET_memcpy (r_hash,
             res,
             sizeof (struct GNUNET_HashCode));
   GNUNET_CRYPTO_hash_context_abort (hc);
+  BENCHMARK_END (hash_context_finish);
 }