- test for external iterator
[oweals/gnunet.git] / src / util / crypto_hash.c
1 /*
2      This file is part of GNUnet.
3      (C) 2001, 2002, 2003, 2004, 2005, 2006, 2009, 2012 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 2, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19
20      SHA-512 code by Jean-Luc Cooke <jlcooke@certainkey.com>
21
22      Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
23      Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
24      Copyright (c) 2003 Kyle McMartin <kyle@debian.org>
25 */
26
27 /**
28  * @file util/crypto_hash.c
29  * @brief SHA-512 GNUNET_CRYPTO_hash related functions
30  * @author Christian Grothoff
31  */
32
33 #include "platform.h"
34 #include "gnunet_common.h"
35 #include "gnunet_crypto_lib.h"
36 #include "gnunet_disk_lib.h"
37 #include "gnunet_strings_lib.h"
38 #include <gcrypt.h>
39
40 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
41
42 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
43
44 /**
45  * Hash block of given size.
46  *
47  * @param block the data to GNUNET_CRYPTO_hash, length is given as a second argument
48  * @param size the length of the data to GNUNET_CRYPTO_hash
49  * @param ret pointer to where to write the hashcode
50  */
51 void
52 GNUNET_CRYPTO_hash (const void *block, size_t size, struct GNUNET_HashCode * ret)
53 {
54   gcry_md_hash_buffer (GCRY_MD_SHA512, ret, block, size);
55 }
56
57
58 /**
59  * Compute short (256-bit) hash of a given block.
60  *
61  * @param block the data to GNUNET_CRYPTO_hash, length is given as a second argument
62  * @param size the length of the data to GNUNET_CRYPTO_hash
63  * @param ret pointer to where to write the hashcode
64  */
65 void
66 GNUNET_CRYPTO_short_hash (const void *block, size_t size, 
67                           struct GNUNET_CRYPTO_ShortHashCode * ret)
68 {
69   gcry_md_hash_buffer (GCRY_MD_SHA256, ret, block, size);
70 }
71
72
73 /**
74  * Context used when hashing a file.
75  */
76 struct GNUNET_CRYPTO_FileHashContext
77 {
78
79   /**
80    * Function to call upon completion.
81    */
82   GNUNET_CRYPTO_HashCompletedCallback callback;
83
84   /**
85    * Closure for callback.
86    */
87   void *callback_cls;
88
89   /**
90    * IO buffer.
91    */
92   unsigned char *buffer;
93
94   /**
95    * Name of the file we are hashing.
96    */
97   char *filename;
98
99   /**
100    * File descriptor.
101    */
102   struct GNUNET_DISK_FileHandle *fh;
103
104   /**
105    * Cummulated hash.
106    */
107   gcry_md_hd_t md;
108
109   /**
110    * Size of the file.
111    */
112   uint64_t fsize;
113
114   /**
115    * Current offset.
116    */
117   uint64_t offset;
118
119   /**
120    * Current task for hashing.
121    */
122   GNUNET_SCHEDULER_TaskIdentifier task;
123
124   /**
125    * Priority we use.
126    */
127   enum GNUNET_SCHEDULER_Priority priority;
128
129   /**
130    * Blocksize.
131    */
132   size_t bsize;
133
134 };
135
136
137 /**
138  * Report result of hash computation to callback
139  * and free associated resources.
140  */
141 static void
142 file_hash_finish (struct GNUNET_CRYPTO_FileHashContext *fhc,
143                   const struct GNUNET_HashCode * res)
144 {
145   fhc->callback (fhc->callback_cls, res);
146   GNUNET_free (fhc->filename);
147   if (!GNUNET_DISK_handle_invalid (fhc->fh))
148     GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
149   gcry_md_close (fhc->md);
150   GNUNET_free (fhc);            /* also frees fhc->buffer */
151 }
152
153
154 /**
155  * File hashing task.
156  *
157  * @param cls closure
158  * @param tc context
159  */
160 static void
161 file_hash_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
162 {
163   struct GNUNET_CRYPTO_FileHashContext *fhc = cls;
164   struct GNUNET_HashCode *res;
165   size_t delta;
166
167   fhc->task = GNUNET_SCHEDULER_NO_TASK;
168   GNUNET_assert (fhc->offset <= fhc->fsize);
169   delta = fhc->bsize;
170   if (fhc->fsize - fhc->offset < delta)
171     delta = fhc->fsize - fhc->offset;
172   if (delta != GNUNET_DISK_file_read (fhc->fh, fhc->buffer, delta))
173   {
174     LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "read", fhc->filename);
175     file_hash_finish (fhc, NULL);
176     return;
177   }
178   gcry_md_write (fhc->md, fhc->buffer, delta);
179   fhc->offset += delta;
180   if (fhc->offset == fhc->fsize)
181   {
182     res = (struct GNUNET_HashCode *) gcry_md_read (fhc->md, GCRY_MD_SHA512);
183     file_hash_finish (fhc, res);
184     return;
185   }
186   fhc->task = GNUNET_SCHEDULER_add_with_priority (fhc->priority,
187                                                   &file_hash_task, fhc);
188 }
189
190
191 /**
192  * Compute the hash of an entire file.
193  *
194  * @param priority scheduling priority to use
195  * @param filename name of file to hash
196  * @param blocksize number of bytes to process in one task
197  * @param callback function to call upon completion
198  * @param callback_cls closure for callback
199  * @return NULL on (immediate) errror
200  */
201 struct GNUNET_CRYPTO_FileHashContext *
202 GNUNET_CRYPTO_hash_file (enum GNUNET_SCHEDULER_Priority priority,
203                          const char *filename, size_t blocksize,
204                          GNUNET_CRYPTO_HashCompletedCallback callback,
205                          void *callback_cls)
206 {
207   struct GNUNET_CRYPTO_FileHashContext *fhc;
208
209   GNUNET_assert (blocksize > 0);
210   fhc =
211       GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_FileHashContext) + blocksize);
212   fhc->callback = callback;
213   fhc->callback_cls = callback_cls;
214   fhc->buffer = (unsigned char *) &fhc[1];
215   fhc->filename = GNUNET_strdup (filename);
216   if (GPG_ERR_NO_ERROR != gcry_md_open (&fhc->md, GCRY_MD_SHA512, 0))
217   {
218     GNUNET_break (0);
219     GNUNET_free (fhc);
220     return NULL;
221   }
222   fhc->bsize = blocksize;
223   if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fhc->fsize, GNUNET_NO, GNUNET_YES))
224   {
225     GNUNET_free (fhc->filename);
226     GNUNET_free (fhc);
227     return NULL;
228   }
229   fhc->fh =
230       GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ,
231                              GNUNET_DISK_PERM_NONE);
232   if (!fhc->fh)
233   {
234     GNUNET_free (fhc->filename);
235     GNUNET_free (fhc);
236     return NULL;
237   }
238   fhc->priority = priority;
239   fhc->task =
240       GNUNET_SCHEDULER_add_with_priority (priority, &file_hash_task, fhc);
241   return fhc;
242 }
243
244
245 /**
246  * Cancel a file hashing operation.
247  *
248  * @param fhc operation to cancel (callback must not yet have been invoked)
249  */
250 void
251 GNUNET_CRYPTO_hash_file_cancel (struct GNUNET_CRYPTO_FileHashContext *fhc)
252 {
253   GNUNET_SCHEDULER_cancel (fhc->task);
254   GNUNET_free (fhc->filename);
255   GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fhc->fh));
256   GNUNET_free (fhc);
257 }
258
259
260 /* ***************** binary-ASCII encoding *************** */
261
262
263 /**
264  * Convert GNUNET_CRYPTO_hash to ASCII encoding.  The ASCII encoding is rather
265  * GNUnet specific.  It was chosen such that it only uses characters
266  * in [0-9A-V], can be produced without complex arithmetics and uses a
267  * small number of characters.  The GNUnet encoding uses 103
268  * characters plus a null terminator.
269  *
270  * @param block the hash code
271  * @param result where to store the encoding (struct GNUNET_CRYPTO_HashAsciiEncoded can be
272  *  safely cast to char*, a '\\0' termination is set).
273  */
274 void
275 GNUNET_CRYPTO_hash_to_enc (const struct GNUNET_HashCode * block,
276                            struct GNUNET_CRYPTO_HashAsciiEncoded *result)
277 {
278   char *np;
279
280   np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block,
281                                       sizeof (struct GNUNET_HashCode),
282                                       (char*) result,
283                                       sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1);
284   GNUNET_assert (NULL != np);
285   *np = '\0';
286 }
287
288
289 /**
290  * Convert ASCII encoding back to hash code.
291  *
292  * @param enc the encoding
293  * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
294  * @param result where to store the hash code
295  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
296  */
297 int
298 GNUNET_CRYPTO_hash_from_string2 (const char *enc, size_t enclen,
299                                 struct GNUNET_HashCode * result)
300 {
301   char upper_enc[enclen];
302   char* up_ptr = upper_enc;
303
304   GNUNET_STRINGS_utf8_toupper(enc, &up_ptr);
305
306   return GNUNET_STRINGS_string_to_data (upper_enc, enclen,
307                                         (unsigned char*) result,
308                                         sizeof (struct GNUNET_HashCode));
309 }
310
311
312 /**
313  * Compute the distance between 2 hashcodes.  The computation must be
314  * fast, not involve bits[0] or bits[4] (they're used elsewhere), and be
315  * somewhat consistent. And of course, the result should be a positive
316  * number.
317  *
318  * @param a some hash code
319  * @param b some hash code
320  * @return a positive number which is a measure for
321  *  hashcode proximity.
322  */
323 unsigned int
324 GNUNET_CRYPTO_hash_distance_u32 (const struct GNUNET_HashCode * a,
325                                  const struct GNUNET_HashCode * b)
326 {
327   unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16;
328   unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16;
329
330   return (x1 * x2);
331 }
332
333
334 /**
335  * Create a random hash code.
336  *
337  * @param mode desired quality level
338  * @param result hash code that is randomized
339  */
340 void
341 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
342                                   struct GNUNET_HashCode *result)
343 {
344   int i;
345
346   for (i = (sizeof (struct GNUNET_HashCode) / sizeof (uint32_t)) - 1; i >= 0; i--)
347     result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX);
348 }
349
350
351 /**
352  * compute result(delta) = b - a
353  *
354  * @param a some hash code
355  * @param b some hash code
356  * @param result set to b - a
357  */
358 void
359 GNUNET_CRYPTO_hash_difference (const struct GNUNET_HashCode * a,
360                                const struct GNUNET_HashCode * b,
361                                struct GNUNET_HashCode * result)
362 {
363   int i;
364
365   for (i = (sizeof (struct GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
366     result->bits[i] = b->bits[i] - a->bits[i];
367 }
368
369
370 /**
371  * compute result(b) = a + delta
372  *
373  * @param a some hash code
374  * @param delta some hash code
375  * @param result set to a + delta
376  */
377 void
378 GNUNET_CRYPTO_hash_sum (const struct GNUNET_HashCode * a,
379                         const struct GNUNET_HashCode * delta, struct GNUNET_HashCode * result)
380 {
381   int i;
382
383   for (i = (sizeof (struct GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
384     result->bits[i] = delta->bits[i] + a->bits[i];
385 }
386
387
388 /**
389  * compute result = a ^ b
390  *
391  * @param a some hash code
392  * @param b some hash code
393  * @param result set to a ^ b
394  */
395 void
396 GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode * a, const struct GNUNET_HashCode * b,
397                         struct GNUNET_HashCode * result)
398 {
399   int i;
400
401   for (i = (sizeof (struct GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
402     result->bits[i] = a->bits[i] ^ b->bits[i];
403 }
404
405
406 /**
407  * Convert a hashcode into a key.
408  *
409  * @param hc hash code that serves to generate the key
410  * @param skey set to a valid session key
411  * @param iv set to a valid initialization vector
412  */
413 void
414 GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode * hc,
415                                struct GNUNET_CRYPTO_AesSessionKey *skey,
416                                struct GNUNET_CRYPTO_AesInitializationVector *iv)
417 {
418   GNUNET_assert (sizeof (struct GNUNET_HashCode) >=
419                  GNUNET_CRYPTO_AES_KEY_LENGTH +
420                  sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
421   memcpy (skey, hc, GNUNET_CRYPTO_AES_KEY_LENGTH);
422   memcpy (iv, &((char *) hc)[GNUNET_CRYPTO_AES_KEY_LENGTH],
423           sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
424 }
425
426
427 /**
428  * Obtain a bit from a hashcode.
429  * @param code the GNUNET_CRYPTO_hash to index bit-wise
430  * @param bit index into the hashcode, [0...511]
431  * @return Bit \a bit from hashcode \a code, -1 for invalid index
432  */
433 int
434 GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode * code, unsigned int bit)
435 {
436   GNUNET_assert (bit < 8 * sizeof (struct GNUNET_HashCode));
437   return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0;
438 }
439
440
441 /**
442  * Determine how many low order bits match in two
443  * struct GNUNET_HashCodes.  i.e. - 010011 and 011111 share
444  * the first two lowest order bits, and therefore the
445  * return value is two (NOT XOR distance, nor how many
446  * bits match absolutely!).
447  *
448  * @param first the first hashcode
449  * @param second the hashcode to compare first to
450  *
451  * @return the number of bits that match
452  */
453 unsigned int
454 GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode * first,
455                                   const struct GNUNET_HashCode * second)
456 {
457   unsigned int i;
458
459   for (i = 0; i < sizeof (struct GNUNET_HashCode) * 8; i++)
460     if (GNUNET_CRYPTO_hash_get_bit (first, i) !=
461         GNUNET_CRYPTO_hash_get_bit (second, i))
462       return i;
463   return sizeof (struct GNUNET_HashCode) * 8;
464 }
465
466
467 /**
468  * Compare function for HashCodes, producing a total ordering
469  * of all hashcodes.
470  *
471  * @param h1 some hash code
472  * @param h2 some hash code
473  * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
474  */
475 int
476 GNUNET_CRYPTO_hash_cmp (const struct GNUNET_HashCode * h1, const struct GNUNET_HashCode * h2)
477 {
478   unsigned int *i1;
479   unsigned int *i2;
480   int i;
481
482   i1 = (unsigned int *) h1;
483   i2 = (unsigned int *) h2;
484   for (i = (sizeof (struct GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
485   {
486     if (i1[i] > i2[i])
487       return 1;
488     if (i1[i] < i2[i])
489       return -1;
490   }
491   return 0;
492 }
493
494
495 /**
496  * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
497  * in the XOR metric (Kademlia).
498  *
499  * @param h1 some hash code
500  * @param h2 some hash code
501  * @param target some hash code
502  * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
503  */
504 int
505 GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode * h1,
506                            const struct GNUNET_HashCode * h2,
507                            const struct GNUNET_HashCode * target)
508 {
509   int i;
510   unsigned int d1;
511   unsigned int d2;
512
513   for (i = sizeof (struct GNUNET_HashCode) / sizeof (unsigned int) - 1; i >= 0; i--)
514   {
515     d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i];
516     d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i];
517     if (d1 > d2)
518       return 1;
519     else if (d1 < d2)
520       return -1;
521   }
522   return 0;
523 }
524
525
526 /**
527  * @brief Derive an authentication key
528  * @param key authentication key
529  * @param rkey root key
530  * @param salt salt
531  * @param salt_len size of the salt
532  * @param ... pair of void * & size_t for context chunks, terminated by NULL
533  */
534 void
535 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
536                                const struct GNUNET_CRYPTO_AesSessionKey *rkey,
537                                const void *salt, size_t salt_len, ...)
538 {
539   va_list argp;
540
541   va_start (argp, salt_len);
542   GNUNET_CRYPTO_hmac_derive_key_v (key, rkey, salt, salt_len, argp);
543   va_end (argp);
544 }
545
546
547 /**
548  * @brief Derive an authentication key
549  * @param key authentication key
550  * @param rkey root key
551  * @param salt salt
552  * @param salt_len size of the salt
553  * @param argp pair of void * & size_t for context chunks, terminated by NULL
554  */
555 void
556 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
557                                  const struct GNUNET_CRYPTO_AesSessionKey *rkey,
558                                  const void *salt, size_t salt_len,
559                                  va_list argp)
560 {
561   GNUNET_CRYPTO_kdf_v (key->key, sizeof (key->key), salt, salt_len, rkey->key,
562                        sizeof (rkey->key), argp);
563 }
564
565
566 /**
567  * Calculate HMAC of a message (RFC 2104)
568  *
569  * @param key secret key
570  * @param plaintext input plaintext
571  * @param plaintext_len length of plaintext
572  * @param hmac where to store the hmac
573  */
574 void
575 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
576                     const void *plaintext, size_t plaintext_len,
577                     struct GNUNET_HashCode * hmac)
578 {
579   gcry_md_hd_t md;
580   const unsigned char *mc;
581
582   GNUNET_assert (GPG_ERR_NO_ERROR ==
583                  gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC));
584   gcry_md_setkey (md, key->key, sizeof (key->key));
585   gcry_md_write (md, plaintext, plaintext_len);
586   mc = gcry_md_read (md, GCRY_MD_SHA512);
587   if (mc != NULL)
588     memcpy (hmac->bits, mc, sizeof (hmac->bits));
589   gcry_md_close (md);
590 }
591
592
593
594 /**
595  * Double short (256-bit) hash to create a long hash.
596  *
597  * @param sh short hash to double
598  * @param dh where to store the (doubled) long hash (not really a hash)
599  */
600 void
601 GNUNET_CRYPTO_short_hash_double (const struct GNUNET_CRYPTO_ShortHashCode *sh,
602                                  struct GNUNET_HashCode *dh)
603 {
604   char *ptr;
605
606   ptr = (char*) dh;
607   memcpy (ptr, sh, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
608   memcpy (&ptr[sizeof (struct GNUNET_CRYPTO_ShortHashCode)], sh, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
609 }
610
611
612 /**
613  * Truncate doubled short hash back to a short hash.
614  *
615  * @param dh doubled short hash to reduce again
616  * @param sh where to store the short hash
617  * @return GNUNET_OK on success, GNUNET_SYSERR if this was not a
618  *         doubled short hash
619  */
620 int
621 GNUNET_CRYPTO_short_hash_from_truncation (const struct GNUNET_HashCode *dh,
622                                           struct GNUNET_CRYPTO_ShortHashCode *sh)
623 {
624   const struct GNUNET_CRYPTO_ShortHashCode *s;
625
626   s = (const struct GNUNET_CRYPTO_ShortHashCode *) dh;
627   if (0 != memcmp (&s[0],
628                    &s[1],
629                    sizeof (struct GNUNET_CRYPTO_ShortHashCode)))
630     return GNUNET_SYSERR;
631   *sh = *s;
632   return GNUNET_OK;
633 }
634
635
636 /**
637  * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
638  *
639  * @param enc the encoding
640  * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
641  * @param result where to store the GNUNET_CRYPTO_hash code
642  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
643  */
644 int
645 GNUNET_CRYPTO_short_hash_from_string2 (const char *enc, size_t enclen,
646                                        struct GNUNET_CRYPTO_ShortHashCode * result)
647 {
648
649   char upper_enc[enclen];
650   char* up_ptr = upper_enc;
651
652   GNUNET_STRINGS_utf8_toupper(enc, &up_ptr);
653   return GNUNET_STRINGS_string_to_data (upper_enc, enclen,
654                                         (unsigned char*) result,
655                                         sizeof (struct GNUNET_CRYPTO_ShortHashCode));
656 }
657
658
659 /**
660  * Convert short hash to ASCII encoding.
661  *
662  * @param block the hash code
663  * @param result where to store the encoding (struct GNUNET_CRYPTO_ShortHashAsciiEncoded can be
664  *  safely cast to char*, a '\\0' termination is set).
665  */
666 void
667 GNUNET_CRYPTO_short_hash_to_enc (const struct GNUNET_CRYPTO_ShortHashCode * block,
668                                  struct GNUNET_CRYPTO_ShortHashAsciiEncoded *result)
669 {
670   char *np;
671
672   np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block,
673                                       sizeof (struct GNUNET_CRYPTO_ShortHashCode),
674                                       (char*) result,
675                                       sizeof (struct GNUNET_CRYPTO_ShortHashAsciiEncoded) - 1);
676   GNUNET_assert (NULL != np);
677   *np = '\0';
678 }
679
680 /**
681  * Compare function for ShortHashCodes, producing a total ordering
682  * of all hashcodes.
683  *
684  * @param h1 some hash code
685  * @param h2 some hash code
686  * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
687  */
688 int
689 GNUNET_CRYPTO_short_hash_cmp (const struct GNUNET_CRYPTO_ShortHashCode * h1,
690                         const struct GNUNET_CRYPTO_ShortHashCode * h2)
691 {
692   unsigned int *i1;
693   unsigned int *i2;
694   int i;
695
696   i1 = (unsigned int *) h1;
697   i2 = (unsigned int *) h2;
698   for (i = (sizeof (struct GNUNET_CRYPTO_ShortHashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
699   {
700     if (i1[i] > i2[i])
701       return 1;
702     if (i1[i] < i2[i])
703       return -1;
704   }
705   return 0;
706 }
707
708 /* end of crypto_hash.c */