-fixes
[oweals/gnunet.git] / src / util / crypto_hash.c
index c693ed0810a5448dca23e54ba483f764a3e917d0..f896390455bfd5321c0fa89fcdd79ff1524a0e6b 100644 (file)
@@ -36,6 +36,9 @@
 #include "gnunet_disk_lib.h"
 #include <gcrypt.h>
 
+#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
+
+#define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
 
 /**
  * Hash block of given size.
@@ -102,6 +105,11 @@ struct GNUNET_CRYPTO_FileHashContext
    */
   GNUNET_SCHEDULER_TaskIdentifier task;
 
+  /**
+   * Priority we use.
+   */
+  enum GNUNET_SCHEDULER_Priority priority;
+
   /**
    * Blocksize.
    */
@@ -147,7 +155,7 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     delta = fhc->fsize - fhc->offset;
   if (delta != GNUNET_DISK_file_read (fhc->fh, fhc->buffer, delta))
   {
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "read", fhc->filename);
+    LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", fhc->filename);
     file_hash_finish (fhc, NULL);
     return;
   }
@@ -159,7 +167,8 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     file_hash_finish (fhc, res);
     return;
   }
-  fhc->task = GNUNET_SCHEDULER_add_now (&file_hash_task, fhc);
+  fhc->task = GNUNET_SCHEDULER_add_with_priority (fhc->priority,
+                                                 &file_hash_task, fhc);
 }
 
 
@@ -175,8 +184,7 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  */
 struct GNUNET_CRYPTO_FileHashContext *
 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
-                         const char *filename,
-                         size_t blocksize,
+                         const char *filename, size_t blocksize,
                          GNUNET_CRYPTO_HashCompletedCallback callback,
                          void *callback_cls)
 {
@@ -202,17 +210,18 @@ GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
     GNUNET_free (fhc);
     return NULL;
   }
-  fhc->fh = GNUNET_DISK_file_open (filename,
-                                   GNUNET_DISK_OPEN_READ,
-                                   GNUNET_DISK_PERM_NONE);
+  fhc->fh =
+      GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
+                             GNUNET_DISK_PERM_NONE);
   if (!fhc->fh)
   {
     GNUNET_free (fhc->filename);
     GNUNET_free (fhc);
     return NULL;
   }
-  fhc->task
-      = GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc);
+  fhc->priority = priority;
+  fhc->task =
+      GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc);
   return fhc;
 }
 
@@ -232,9 +241,16 @@ GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
 }
 
 
-
 /* ***************** binary-ASCII encoding *************** */
 
+/* FIXME: should use GNUNET_STRINGS_data_to_string and strings_to_data below!!! */
+
+/**
+ * Get the numeric value corresponding to a character.
+ *
+ * @param a a character
+ * @return corresponding numeric value
+ */
 static unsigned int
 getValue__ (unsigned char a)
 {
@@ -245,6 +261,7 @@ getValue__ (unsigned char a)
   return -1;
 }
 
+
 /**
  * Convert GNUNET_CRYPTO_hash to ASCII encoding.  The ASCII encoding is rather
  * GNUnet specific.  It was chosen such that it only uses characters
@@ -297,32 +314,40 @@ GNUNET_CRYPTO_hash_to_enc (const GNUNET_HashCode * block,
   result->encoding[wpos] = '\0';
 }
 
+
 /**
  * Convert ASCII encoding back to GNUNET_CRYPTO_hash
  *
  * @param enc the encoding
+ * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
  * @param result where to store the GNUNET_CRYPTO_hash code
  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
  */
 int
-GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result)
+GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
+                                GNUNET_HashCode * result)
 {
   unsigned int rpos;
   unsigned int wpos;
   unsigned int bits;
   unsigned int vbit;
+  int ret;
 
-  if (strlen (enc) != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1)
+  if (enclen != sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1)
     return GNUNET_SYSERR;
 
   vbit = 2;                     /* padding! */
   wpos = sizeof (GNUNET_HashCode);
   rpos = sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1;
-  bits = getValue__ (enc[--rpos]) >> 3;
+  bits = (ret = getValue__ (enc[--rpos])) >> 3;
+  if (-1 == ret)
+    return GNUNET_SYSERR;
   while (wpos > 0)
   {
     GNUNET_assert (rpos > 0);
-    bits = (getValue__ (enc[--rpos]) << vbit) | bits;
+    bits = ((ret = getValue__ (enc[--rpos])) << vbit) | bits;
+    if (-1 == ret)
+      return GNUNET_SYSERR;
     vbit += 5;
     if (vbit >= 8)
     {
@@ -336,12 +361,13 @@ GNUNET_CRYPTO_hash_from_string (const char *enc, GNUNET_HashCode * result)
   return GNUNET_OK;
 }
 
+
 /**
  * Compute the distance between 2 hashcodes.  The computation must be
  * fast, not involve bits[0] or bits[4] (they're used elsewhere), and be
  * somewhat consistent. And of course, the result should be a positive
  * number.
- * 
+ *
  * @param a some hash code
  * @param b some hash code
  * @return a positive number which is a measure for
@@ -357,6 +383,13 @@ GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
   return (x1 * x2);
 }
 
+
+/**
+ * Create a random hash code.
+ *
+ * @param mode desired quality level
+ * @param result hash code that is randomized
+ */
 void
 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
                                   GNUNET_HashCode * result)
@@ -367,6 +400,14 @@ GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
     result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX);
 }
 
+
+/**
+ * compute result(delta) = b - a
+ *
+ * @param a some hash code
+ * @param b some hash code
+ * @param result set to b - a
+ */
 void
 GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
                                const GNUNET_HashCode * b,
@@ -378,6 +419,14 @@ GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
     result->bits[i] = b->bits[i] - a->bits[i];
 }
 
+
+/**
+ * compute result(b) = a + delta
+ *
+ * @param a some hash code
+ * @param delta some hash code
+ * @param result set to a + delta
+ */
 void
 GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
                         const GNUNET_HashCode * delta, GNUNET_HashCode * result)
@@ -389,9 +438,16 @@ GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
 }
 
 
+/**
+ * compute result = a ^ b
+ *
+ * @param a some hash code
+ * @param b some hash code
+ * @param result set to a ^ b
+ */
 void
-GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a,
-                        const GNUNET_HashCode * b, GNUNET_HashCode * result)
+GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b,
+                        GNUNET_HashCode * result)
 {
   int i;
 
@@ -402,6 +458,10 @@ GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a,
 
 /**
  * Convert a hashcode into a key.
+ *
+ * @param hc hash code that serves to generate the key
+ * @param skey set to a valid session key
+ * @param iv set to a valid initialization vector
  */
 void
 GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
@@ -432,6 +492,7 @@ GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit)
   return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0;
 }
 
+
 /**
  * Determine how many low order bits match in two
  * GNUNET_HashCodes.  i.e. - 010011 and 011111 share
@@ -461,6 +522,9 @@ GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first,
 /**
  * Compare function for HashCodes, producing a total ordering
  * of all hashcodes.
+ *
+ * @param h1 some hash code
+ * @param h2 some hash code
  * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
  */
 int
@@ -486,6 +550,10 @@ GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2)
 /**
  * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
  * in the XOR metric (Kademlia).
+ *
+ * @param h1 some hash code
+ * @param h2 some hash code
+ * @param target some hash code
  * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
  */
 int
@@ -542,12 +610,11 @@ GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
 void
 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
                                  const struct GNUNET_CRYPTO_AesSessionKey *rkey,
-                                 const void *salt,
-                                 size_t salt_len, va_list argp)
+                                 const void *salt, size_t salt_len,
+                                 va_list argp)
 {
-  GNUNET_CRYPTO_kdf_v (key->key,
-                       sizeof (key->key),
-                       salt, salt_len, rkey->key, sizeof (rkey->key), argp);
+  GNUNET_CRYPTO_kdf_v (key->key, sizeof (key->key), salt, salt_len, rkey->key,
+                       sizeof (rkey->key), argp);
 }
 
 
@@ -561,15 +628,14 @@ GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
  */
 void
 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
-                    const void *plaintext,
-                    size_t plaintext_len, GNUNET_HashCode * hmac)
+                    const void *plaintext, size_t plaintext_len,
+                    GNUNET_HashCode * hmac)
 {
   gcry_md_hd_t md;
   const unsigned char *mc;
 
-  GNUNET_assert (GPG_ERR_NO_ERROR == gcry_md_open (&md,
-                                                   GCRY_MD_SHA512,
-                                                   GCRY_MD_FLAG_HMAC));
+  GNUNET_assert (GPG_ERR_NO_ERROR ==
+                 gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC));
   gcry_md_setkey (md, key->key, sizeof (key->key));
   gcry_md_write (md, plaintext, plaintext_len);
   mc = gcry_md_read (md, GCRY_MD_SHA512);