-eliminating duplicate code
[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, 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 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   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 = (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 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                                 GNUNET_HashCode * result)
300 {
301   return GNUNET_STRINGS_string_to_data (enc, enclen,
302                                         (unsigned char*) result,
303                                         sizeof (struct GNUNET_HashCode));
304 }
305
306
307 /**
308  * Compute the distance between 2 hashcodes.  The computation must be
309  * fast, not involve bits[0] or bits[4] (they're used elsewhere), and be
310  * somewhat consistent. And of course, the result should be a positive
311  * number.
312  *
313  * @param a some hash code
314  * @param b some hash code
315  * @return a positive number which is a measure for
316  *  hashcode proximity.
317  */
318 unsigned int
319 GNUNET_CRYPTO_hash_distance_u32 (const GNUNET_HashCode * a,
320                                  const GNUNET_HashCode * b)
321 {
322   unsigned int x1 = (a->bits[1] - b->bits[1]) >> 16;
323   unsigned int x2 = (b->bits[1] - a->bits[1]) >> 16;
324
325   return (x1 * x2);
326 }
327
328
329 /**
330  * Create a random hash code.
331  *
332  * @param mode desired quality level
333  * @param result hash code that is randomized
334  */
335 void
336 GNUNET_CRYPTO_hash_create_random (enum GNUNET_CRYPTO_Quality mode,
337                                   GNUNET_HashCode * result)
338 {
339   int i;
340
341   for (i = (sizeof (GNUNET_HashCode) / sizeof (uint32_t)) - 1; i >= 0; i--)
342     result->bits[i] = GNUNET_CRYPTO_random_u32 (mode, UINT32_MAX);
343 }
344
345
346 /**
347  * compute result(delta) = b - a
348  *
349  * @param a some hash code
350  * @param b some hash code
351  * @param result set to b - a
352  */
353 void
354 GNUNET_CRYPTO_hash_difference (const GNUNET_HashCode * a,
355                                const GNUNET_HashCode * b,
356                                GNUNET_HashCode * result)
357 {
358   int i;
359
360   for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
361     result->bits[i] = b->bits[i] - a->bits[i];
362 }
363
364
365 /**
366  * compute result(b) = a + delta
367  *
368  * @param a some hash code
369  * @param delta some hash code
370  * @param result set to a + delta
371  */
372 void
373 GNUNET_CRYPTO_hash_sum (const GNUNET_HashCode * a,
374                         const GNUNET_HashCode * delta, GNUNET_HashCode * result)
375 {
376   int i;
377
378   for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
379     result->bits[i] = delta->bits[i] + a->bits[i];
380 }
381
382
383 /**
384  * compute result = a ^ b
385  *
386  * @param a some hash code
387  * @param b some hash code
388  * @param result set to a ^ b
389  */
390 void
391 GNUNET_CRYPTO_hash_xor (const GNUNET_HashCode * a, const GNUNET_HashCode * b,
392                         GNUNET_HashCode * result)
393 {
394   int i;
395
396   for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
397     result->bits[i] = a->bits[i] ^ b->bits[i];
398 }
399
400
401 /**
402  * Convert a hashcode into a key.
403  *
404  * @param hc hash code that serves to generate the key
405  * @param skey set to a valid session key
406  * @param iv set to a valid initialization vector
407  */
408 void
409 GNUNET_CRYPTO_hash_to_aes_key (const GNUNET_HashCode * hc,
410                                struct GNUNET_CRYPTO_AesSessionKey *skey,
411                                struct GNUNET_CRYPTO_AesInitializationVector *iv)
412 {
413   GNUNET_assert (sizeof (GNUNET_HashCode) >=
414                  GNUNET_CRYPTO_AES_KEY_LENGTH +
415                  sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
416   memcpy (skey, hc, GNUNET_CRYPTO_AES_KEY_LENGTH);
417   skey->crc32 =
418       htonl (GNUNET_CRYPTO_crc32_n (skey, GNUNET_CRYPTO_AES_KEY_LENGTH));
419   memcpy (iv, &((char *) hc)[GNUNET_CRYPTO_AES_KEY_LENGTH],
420           sizeof (struct GNUNET_CRYPTO_AesInitializationVector));
421 }
422
423
424 /**
425  * Obtain a bit from a hashcode.
426  * @param code the GNUNET_CRYPTO_hash to index bit-wise
427  * @param bit index into the hashcode, [0...511]
428  * @return Bit \a bit from hashcode \a code, -1 for invalid index
429  */
430 int
431 GNUNET_CRYPTO_hash_get_bit (const GNUNET_HashCode * code, unsigned int bit)
432 {
433   GNUNET_assert (bit < 8 * sizeof (GNUNET_HashCode));
434   return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0;
435 }
436
437
438 /**
439  * Determine how many low order bits match in two
440  * GNUNET_HashCodes.  i.e. - 010011 and 011111 share
441  * the first two lowest order bits, and therefore the
442  * return value is two (NOT XOR distance, nor how many
443  * bits match absolutely!).
444  *
445  * @param first the first hashcode
446  * @param second the hashcode to compare first to
447  *
448  * @return the number of bits that match
449  */
450 unsigned int
451 GNUNET_CRYPTO_hash_matching_bits (const GNUNET_HashCode * first,
452                                   const GNUNET_HashCode * second)
453 {
454   unsigned int i;
455
456   for (i = 0; i < sizeof (GNUNET_HashCode) * 8; i++)
457     if (GNUNET_CRYPTO_hash_get_bit (first, i) !=
458         GNUNET_CRYPTO_hash_get_bit (second, i))
459       return i;
460   return sizeof (GNUNET_HashCode) * 8;
461 }
462
463
464 /**
465  * Compare function for HashCodes, producing a total ordering
466  * of all hashcodes.
467  *
468  * @param h1 some hash code
469  * @param h2 some hash code
470  * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
471  */
472 int
473 GNUNET_CRYPTO_hash_cmp (const GNUNET_HashCode * h1, const GNUNET_HashCode * h2)
474 {
475   unsigned int *i1;
476   unsigned int *i2;
477   int i;
478
479   i1 = (unsigned int *) h1;
480   i2 = (unsigned int *) h2;
481   for (i = (sizeof (GNUNET_HashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
482   {
483     if (i1[i] > i2[i])
484       return 1;
485     if (i1[i] < i2[i])
486       return -1;
487   }
488   return 0;
489 }
490
491
492 /**
493  * Find out which of the two GNUNET_CRYPTO_hash codes is closer to target
494  * in the XOR metric (Kademlia).
495  *
496  * @param h1 some hash code
497  * @param h2 some hash code
498  * @param target some hash code
499  * @return -1 if h1 is closer, 1 if h2 is closer and 0 if h1==h2.
500  */
501 int
502 GNUNET_CRYPTO_hash_xorcmp (const GNUNET_HashCode * h1,
503                            const GNUNET_HashCode * h2,
504                            const GNUNET_HashCode * target)
505 {
506   int i;
507   unsigned int d1;
508   unsigned int d2;
509
510   for (i = sizeof (GNUNET_HashCode) / sizeof (unsigned int) - 1; i >= 0; i--)
511   {
512     d1 = ((unsigned int *) h1)[i] ^ ((unsigned int *) target)[i];
513     d2 = ((unsigned int *) h2)[i] ^ ((unsigned int *) target)[i];
514     if (d1 > d2)
515       return 1;
516     else if (d1 < d2)
517       return -1;
518   }
519   return 0;
520 }
521
522
523 /**
524  * @brief Derive an authentication key
525  * @param key authentication key
526  * @param rkey root key
527  * @param salt salt
528  * @param salt_len size of the salt
529  * @param ... pair of void * & size_t for context chunks, terminated by NULL
530  */
531 void
532 GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key,
533                                const struct GNUNET_CRYPTO_AesSessionKey *rkey,
534                                const void *salt, size_t salt_len, ...)
535 {
536   va_list argp;
537
538   va_start (argp, salt_len);
539   GNUNET_CRYPTO_hmac_derive_key_v (key, rkey, salt, salt_len, argp);
540   va_end (argp);
541 }
542
543
544 /**
545  * @brief Derive an authentication key
546  * @param key authentication key
547  * @param rkey root key
548  * @param salt salt
549  * @param salt_len size of the salt
550  * @param argp pair of void * & size_t for context chunks, terminated by NULL
551  */
552 void
553 GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key,
554                                  const struct GNUNET_CRYPTO_AesSessionKey *rkey,
555                                  const void *salt, size_t salt_len,
556                                  va_list argp)
557 {
558   GNUNET_CRYPTO_kdf_v (key->key, sizeof (key->key), salt, salt_len, rkey->key,
559                        sizeof (rkey->key), argp);
560 }
561
562
563 /**
564  * Calculate HMAC of a message (RFC 2104)
565  *
566  * @param key secret key
567  * @param plaintext input plaintext
568  * @param plaintext_len length of plaintext
569  * @param hmac where to store the hmac
570  */
571 void
572 GNUNET_CRYPTO_hmac (const struct GNUNET_CRYPTO_AuthKey *key,
573                     const void *plaintext, size_t plaintext_len,
574                     GNUNET_HashCode * hmac)
575 {
576   gcry_md_hd_t md;
577   const unsigned char *mc;
578
579   GNUNET_assert (GPG_ERR_NO_ERROR ==
580                  gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC));
581   gcry_md_setkey (md, key->key, sizeof (key->key));
582   gcry_md_write (md, plaintext, plaintext_len);
583   mc = gcry_md_read (md, GCRY_MD_SHA512);
584   if (mc != NULL)
585     memcpy (hmac->bits, mc, sizeof (hmac->bits));
586   gcry_md_close (md);
587 }
588
589
590
591 /**
592  * Double short (256-bit) hash to create a long hash.
593  *
594  * @param sh short hash to double
595  * @param dh where to store the (doubled) long hash (not really a hash)
596  */
597 void
598 GNUNET_CRYPTO_short_hash_double (const struct GNUNET_CRYPTO_ShortHashCode *sh,
599                                  struct GNUNET_HashCode *dh)
600 {
601   char *ptr;
602
603   ptr = (char*) dh;
604   memcpy (ptr, sh, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
605   memcpy (&ptr[sizeof (struct GNUNET_CRYPTO_ShortHashCode)], sh, sizeof (struct GNUNET_CRYPTO_ShortHashCode));
606 }
607
608
609 /**
610  * Truncate doubled short hash back to a short hash.
611  *
612  * @param dh doubled short hash to reduce again
613  * @param sh where to store the short hash
614  * @return GNUNET_OK on success, GNUNET_SYSERR if this was not a
615  *         doubled short hash
616  */
617 int
618 GNUNET_CRYPTO_short_hash_from_truncation (const struct GNUNET_HashCode *dh,
619                                           struct GNUNET_CRYPTO_ShortHashCode *sh)
620 {
621   const struct GNUNET_CRYPTO_ShortHashCode *s;
622
623   s = (const struct GNUNET_CRYPTO_ShortHashCode *) dh;
624   if (0 != memcmp (&s[0],
625                    &s[1],
626                    sizeof (struct GNUNET_CRYPTO_ShortHashCode)))
627     return GNUNET_SYSERR;
628   *sh = *s;
629   return GNUNET_OK;
630 }
631
632
633 /**
634  * Convert ASCII encoding back to a 'struct GNUNET_CRYPTO_ShortHash'
635  *
636  * @param enc the encoding
637  * @param enclen number of characters in 'enc' (without 0-terminator, which can be missing)
638  * @param result where to store the GNUNET_CRYPTO_hash code
639  * @return GNUNET_OK on success, GNUNET_SYSERR if result has the wrong encoding
640  */
641 int
642 GNUNET_CRYPTO_short_hash_from_string2 (const char *enc, size_t enclen,
643                                        struct GNUNET_CRYPTO_ShortHashCode * result)
644 {
645   return GNUNET_STRINGS_string_to_data (enc, enclen,
646                                         (unsigned char*) result,
647                                         sizeof (struct GNUNET_CRYPTO_ShortHashCode));
648 }
649
650
651 /**
652  * Convert short hash to ASCII encoding.
653  *
654  * @param block the hash code
655  * @param result where to store the encoding (struct GNUNET_CRYPTO_ShortHashAsciiEncoded can be
656  *  safely cast to char*, a '\\0' termination is set).
657  */
658 void
659 GNUNET_CRYPTO_short_hash_to_enc (const struct GNUNET_CRYPTO_ShortHashCode * block,
660                                  struct GNUNET_CRYPTO_ShortHashAsciiEncoded *result)
661 {
662   char *np;
663
664   np = GNUNET_STRINGS_data_to_string ((const unsigned char *) block,
665                                       sizeof (struct GNUNET_CRYPTO_ShortHashCode),
666                                       (char*) result,
667                                       sizeof (struct GNUNET_CRYPTO_ShortHashAsciiEncoded) - 1);
668   GNUNET_assert (NULL != np);
669   *np = '\0';
670 }
671
672 /**
673  * Compare function for ShortHashCodes, producing a total ordering
674  * of all hashcodes.
675  *
676  * @param h1 some hash code
677  * @param h2 some hash code
678  * @return 1 if h1 > h2, -1 if h1 < h2 and 0 if h1 == h2.
679  */
680 int
681 GNUNET_CRYPTO_short_hash_cmp (const struct GNUNET_CRYPTO_ShortHashCode * h1,
682                         const struct GNUNET_CRYPTO_ShortHashCode * h2)
683 {
684   unsigned int *i1;
685   unsigned int *i2;
686   int i;
687
688   i1 = (unsigned int *) h1;
689   i2 = (unsigned int *) h2;
690   for (i = (sizeof (struct GNUNET_CRYPTO_ShortHashCode) / sizeof (unsigned int)) - 1; i >= 0; i--)
691   {
692     if (i1[i] > i2[i])
693       return 1;
694     if (i1[i] < i2[i])
695       return -1;
696   }
697   return 0;
698 }
699
700 /* end of crypto_hash.c */