Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / crypto_hash.c
index 8ec822434d3ce79c5e2408adb2d87390681c0860..9e5af4ab51da6715465cb563ede18925bbe0fdcb 100644 (file)
@@ -82,11 +82,6 @@ struct GNUNET_CRYPTO_FileHashContext
    */
   struct GNUNET_DISK_FileHandle *fh;
 
-  /**
-   * Our scheduler.
-   */
-  struct GNUNET_SCHEDULER_Handle *sched;
-
   /**
    * Cummulated hash.
    */
@@ -127,6 +122,7 @@ file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc,
   GNUNET_free (fhc->filename);
   if (!GNUNET_DISK_handle_invalid (fhc->fh))
     GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
+  gcry_md_close (fhc->md);
   GNUNET_free (fhc);            /* also frees fhc->buffer */
 }
 
@@ -162,12 +158,10 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     {
       res = (GNUNET_HashCode *) gcry_md_read (fhc->md, GCRY_MD_SHA512);
       file_hash_finish (fhc, res);
-      gcry_md_close (fhc->md);
       return;
     }
   fhc->task 
-    = GNUNET_SCHEDULER_add_after (tc->sched,
-                                 GNUNET_SCHEDULER_NO_TASK, 
+    = GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
                                  &file_hash_task, fhc);
 }
 
@@ -175,7 +169,6 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 /**
  * Compute the hash of an entire file.
  *
- * @param sched scheduler to use
  * @param priority scheduling priority to use
  * @param filename name of file to hash
  * @param blocksize number of bytes to process in one task
@@ -184,8 +177,7 @@ file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @return NULL on (immediate) errror
  */
 struct GNUNET_CRYPTO_FileHashContext *
-GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched,
-                         enum GNUNET_SCHEDULER_Priority priority,
+GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
                          const char *filename,
                          size_t blocksize,
                          GNUNET_CRYPTO_HashCompletedCallback callback,
@@ -197,7 +189,6 @@ GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched,
   fhc = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_FileHashContext) + blocksize);
   fhc->callback = callback;
   fhc->callback_cls = callback_cls;
-  fhc->sched = sched;
   fhc->buffer = (unsigned char *) &fhc[1];
   fhc->filename = GNUNET_strdup (filename);
   if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0))
@@ -223,7 +214,7 @@ GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched,
       return NULL;
     }
   fhc->task 
-    = GNUNET_SCHEDULER_add_with_priority (sched, priority, 
+    = GNUNET_SCHEDULER_add_with_priority (priority,
                                          &file_hash_task, fhc);
   return fhc;
 }
@@ -237,8 +228,7 @@ GNUNET_CRYPTO_hash_file (struct GNUNET_SCHEDULER_Handle *sched,
 void
 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
 {
-  GNUNET_SCHEDULER_cancel (fhc->sched,
-                          fhc->task);
+  GNUNET_SCHEDULER_cancel (fhc->task);
   GNUNET_free (fhc->filename);
   GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
   GNUNET_free (fhc);
@@ -401,9 +391,11 @@ GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
     result->bits[i] = delta->bits[i] + a->bits[i];
 }
 
+
 void
 GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a,
-                        const GNUNET_HashCode * b, GNUNET_HashCode * result)
+                        const GNUNET_HashCode * b,
+                       GNUNET_HashCode * result)
 {
   int i;
   for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0;
@@ -411,6 +403,7 @@ GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a,
     result->bits[i] = a->bits[i] ^ b->bits[i];
 }
 
+
 /**
  * Convert a hashcode into a key.
  */
@@ -430,6 +423,7 @@ GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
           sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
 }
 
+
 /**
  * Obtain a bit from a hashcode.
  * @param code the GNUNET_CRYPTO_hash to index bit-wise
@@ -437,7 +431,8 @@ GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
  * @return Bit \a bit from hashcode \a code, -1 for invalid index
  */
 int
-GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit)
+GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, 
+                           unsigned int bit)
 {
   GNUNET_assert (bit < 8 * sizeof (GNUNET_HashCode));
   return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0;
@@ -455,7 +450,9 @@ GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit)
  *
  * @return the number of bits that match
  */
-unsigned int GNUNET_CRYPTO_hash_matching_bits(const GNUNET_HashCode *first, const GNUNET_HashCode *second)
+unsigned int 
+GNUNET_CRYPTO_hash_matching_bits(const GNUNET_HashCode *first,
+                                const GNUNET_HashCode *second)
 {
   unsigned int i;
 
@@ -465,6 +462,7 @@ unsigned int GNUNET_CRYPTO_hash_matching_bits(const GNUNET_HashCode *first, cons
   return sizeof (GNUNET_HashCode) * 8;
 }
 
+
 /**
  * Compare function for HashCodes, producing a total ordering
  * of all hashcodes.
@@ -491,6 +489,7 @@ GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1,
   return 0;
 }
 
+
 /**
  * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
  * in the XOR metric (Kademlia).
@@ -530,7 +529,7 @@ void
 GNUNET_CRYPTO_hmac_derive_key(struct GNUNET_CRYPTO_AuthKey *key,
                               const struct GNUNET_CRYPTO_AesSessionKey *rkey,
                               const void *salt,
-                              const size_t salt_len,
+                              size_t salt_len,
                               ...)
 {
   va_list argp;
@@ -553,13 +552,18 @@ void
 GNUNET_CRYPTO_hmac_derive_key_v(struct GNUNET_CRYPTO_AuthKey *key,
                                 const struct GNUNET_CRYPTO_AesSessionKey *rkey,
                                 const void *salt,
-                                const size_t salt_len,
+                                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);
 }
 
+
 /**
  * Calculate HMAC of a message (RFC 2104)
  *
@@ -575,9 +579,11 @@ GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
                    GNUNET_HashCode *hmac)
 {
   gcry_md_hd_t md;
-  unsigned char *mc;
+  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);