-rename files/symbols from _aes to _symmetric
[oweals/gnunet.git] / src / util / test_crypto_hash.c
1 /*
2      This file is part of GNUnet.
3      (C) 2002, 2003, 2004, 2006, 2009 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 3, 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
21 /**
22  * @author Christian Grothoff
23  * @file util/test_crypto_hash.c
24  * @brief Test for crypto_hash.c
25  */
26 #include "platform.h"
27 #include "gnunet_common.h"
28 #include "gnunet_crypto_lib.h"
29 #include "gnunet_scheduler_lib.h"
30
31 static char block[65536];
32
33 #define FILENAME "testblock.dat"
34
35 static int
36 test (int number)
37 {
38   struct GNUNET_HashCode h1;
39   struct GNUNET_HashCode h2;
40   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
41
42   memset (&h1, number, sizeof (struct GNUNET_HashCode));
43   GNUNET_CRYPTO_hash_to_enc (&h1, &enc);
44   if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &h2))
45   {
46     printf ("enc2hash failed!\n");
47     return 1;
48   }
49   if (0 != memcmp (&h1, &h2, sizeof (struct GNUNET_HashCode)))
50     return 1;
51   return 0;
52 }
53
54 static int
55 testEncoding ()
56 {
57   int i;
58
59   for (i = 0; i < 255; i++)
60     if (0 != test (i))
61       return 1;
62   return 0;
63 }
64
65 static int
66 testArithmetic ()
67 {
68   struct GNUNET_HashCode h1;
69   struct GNUNET_HashCode h2;
70   struct GNUNET_HashCode d;
71   struct GNUNET_HashCode s;
72   struct GNUNET_CRYPTO_SymmetricSessionKey skey;
73   struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
74
75   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h1);
76   GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h2);
77   if (GNUNET_CRYPTO_hash_distance_u32 (&h1, &h2) !=
78       GNUNET_CRYPTO_hash_distance_u32 (&h2, &h1))
79     return 1;
80   GNUNET_CRYPTO_hash_difference (&h1, &h2, &d);
81   GNUNET_CRYPTO_hash_sum (&h1, &d, &s);
82   if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
83     return 1;
84   GNUNET_CRYPTO_hash_xor (&h1, &h2, &d);
85   GNUNET_CRYPTO_hash_xor (&h1, &d, &s);
86   if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2))
87     return 1;
88   if (0 != GNUNET_CRYPTO_hash_xorcmp (&s, &h2, &h1))
89     return 1;
90   if (-1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h1))
91     return 1;
92   if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2))
93     return 1;
94   memset (&d, 0xF0, sizeof (d));
95   if (0 != GNUNET_CRYPTO_hash_get_bit (&d, 3))
96     return 1;
97   if (1 != GNUNET_CRYPTO_hash_get_bit (&d, 6))
98     return 1;
99   memset (&d, 0, sizeof (d));
100   GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv);
101   return 0;
102 }
103
104 static void
105 finished_task (void *cls, const struct GNUNET_HashCode * res)
106 {
107   int *ret = cls;
108   struct GNUNET_HashCode want;
109
110   GNUNET_CRYPTO_hash (block, sizeof (block), &want);
111   if (0 != memcmp (res, &want, sizeof (want)))
112     *ret = 2;
113   else
114     *ret = 0;
115 }
116
117
118 static void
119 file_hasher (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
120 {
121   GNUNET_assert (NULL !=
122                  GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
123                                           FILENAME, 1024, &finished_task, cls));
124 }
125
126
127 static int
128 testFileHash ()
129 {
130   int ret;
131   FILE *f;
132
133   memset (block, 42, sizeof (block) / 2);
134   memset (&block[sizeof (block) / 2], 43, sizeof (block) / 2);
135   GNUNET_assert (NULL != (f = FOPEN (FILENAME, "w+")));
136   GNUNET_break (sizeof (block) == fwrite (block, 1, sizeof (block), f));
137   GNUNET_break (0 == FCLOSE (f));
138   ret = 1;
139   GNUNET_SCHEDULER_run (&file_hasher, &ret);
140   GNUNET_break (0 == UNLINK (FILENAME));
141   return ret;
142 }
143
144
145 int
146 main (int argc, char *argv[])
147 {
148   int failureCount = 0;
149   int i;
150
151   GNUNET_log_setup ("test-crypto-hash", "WARNING", NULL);
152   for (i = 0; i < 10; i++)
153     failureCount += testEncoding ();
154   failureCount += testArithmetic ();
155   failureCount += testFileHash ();
156   if (failureCount != 0)
157     return 1;
158   return 0;
159 }
160
161 /* end of hashingtest.c */