aboutsummaryrefslogtreecommitdiff
path: root/src/util/test_crypto_hash.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/test_crypto_hash.c')
-rw-r--r--src/util/test_crypto_hash.c227
1 files changed, 173 insertions, 54 deletions
diff --git a/src/util/test_crypto_hash.c b/src/util/test_crypto_hash.c
index d22e1f5d3..1fddcfba8 100644
--- a/src/util/test_crypto_hash.c
+++ b/src/util/test_crypto_hash.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2002, 2003, 2004, 2006, 2009 GNUnet e.V. 3 Copyright (C) 2002, 2003, 2004, 2006, 2009, 2022 GNUnet e.V.
4 4
5 GNUnet is free software: you can redistribute it and/or modify it 5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published 6 under the terms of the GNU Affero General Public License as published
@@ -37,25 +37,29 @@ test (int number)
37 struct GNUNET_HashCode h2; 37 struct GNUNET_HashCode h2;
38 struct GNUNET_CRYPTO_HashAsciiEncoded enc; 38 struct GNUNET_CRYPTO_HashAsciiEncoded enc;
39 39
40 memset (&h1, number, sizeof(struct GNUNET_HashCode)); 40 memset (&h1,
41 GNUNET_CRYPTO_hash_to_enc (&h1, &enc); 41 number,
42 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string ((char *) &enc, &h2)) 42 sizeof(struct GNUNET_HashCode));
43 GNUNET_CRYPTO_hash_to_enc (&h1,
44 &enc);
45 if (GNUNET_OK !=
46 GNUNET_CRYPTO_hash_from_string ((char *) &enc,
47 &h2))
43 { 48 {
44 printf ("enc2hash failed!\n"); 49 printf ("enc2hash failed!\n");
45 return 1; 50 return 1;
46 } 51 }
47 if (0 != memcmp (&h1, &h2, sizeof(struct GNUNET_HashCode))) 52 if (0 != GNUNET_memcmp (&h1,
53 &h2))
48 return 1; 54 return 1;
49 return 0; 55 return 0;
50} 56}
51 57
52 58
53static int 59static int
54testEncoding () 60test_encoding (void)
55{ 61{
56 int i; 62 for (int i = 0; i < 255; i++)
57
58 for (i = 0; i < 255; i++)
59 if (0 != test (i)) 63 if (0 != test (i))
60 return 1; 64 return 1;
61 return 0; 65 return 0;
@@ -63,7 +67,7 @@ testEncoding ()
63 67
64 68
65static int 69static int
66testArithmetic () 70test_arithmetic (void)
67{ 71{
68 struct GNUNET_HashCode h1; 72 struct GNUNET_HashCode h1;
69 struct GNUNET_HashCode h2; 73 struct GNUNET_HashCode h2;
@@ -72,49 +76,150 @@ testArithmetic ()
72 struct GNUNET_CRYPTO_SymmetricSessionKey skey; 76 struct GNUNET_CRYPTO_SymmetricSessionKey skey;
73 struct GNUNET_CRYPTO_SymmetricInitializationVector iv; 77 struct GNUNET_CRYPTO_SymmetricInitializationVector iv;
74 78
75 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h1); 79 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
76 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK, &h2); 80 &h1);
77 if (GNUNET_CRYPTO_hash_distance_u32 (&h1, &h2) != 81 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
78 GNUNET_CRYPTO_hash_distance_u32 (&h2, &h1)) 82 &h2);
79 return 1; 83 if (GNUNET_CRYPTO_hash_distance_u32 (&h1,
80 GNUNET_CRYPTO_hash_difference (&h1, &h2, &d); 84 &h2) !=
81 GNUNET_CRYPTO_hash_sum (&h1, &d, &s); 85 GNUNET_CRYPTO_hash_distance_u32 (&h2,
82 if (0 != GNUNET_CRYPTO_hash_cmp (&s, &h2)) 86 &h1))
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; 87 return 1;
92 if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2)) 88 GNUNET_CRYPTO_hash_difference (&h1,
89 &h2,
90 &d);
91 GNUNET_CRYPTO_hash_sum (&h1,
92 &d,
93 &s);
94 if (0 !=
95 GNUNET_CRYPTO_hash_cmp (&s,
96 &h2))
93 return 1; 97 return 1;
94 memset (&d, 0x40, sizeof(d)); 98 GNUNET_CRYPTO_hash_xor (&h1,
95 if (0 != GNUNET_CRYPTO_hash_get_bit_rtl (&d, 3)) 99 &h2,
100 &d);
101 GNUNET_CRYPTO_hash_xor (&h1,
102 &d,
103 &s);
104 if (0 !=
105 GNUNET_CRYPTO_hash_cmp (&s,
106 &h2))
96 return 1; 107 return 1;
97 if (1 != GNUNET_CRYPTO_hash_get_bit_rtl (&d, 6)) 108 if (0 !=
109 GNUNET_CRYPTO_hash_xorcmp (&s,
110 &h2,
111 &h1))
98 return 1; 112 return 1;
99 memset (&d, 0x02, sizeof(d)); 113 if (-1 !=
100 if (0 != GNUNET_CRYPTO_hash_get_bit_ltr (&d, 3)) 114 GNUNET_CRYPTO_hash_xorcmp (&h1,
115 &h2,
116 &h1))
101 return 1; 117 return 1;
102 if (1 != GNUNET_CRYPTO_hash_get_bit_ltr (&d, 6)) 118 if (1 !=
119 GNUNET_CRYPTO_hash_xorcmp (&h1,
120 &h2,
121 &h2))
103 return 1; 122 return 1;
104 memset (&d, 0, sizeof(d)); 123 memset (&d,
105 GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv); 124 0,
125 sizeof(d));
126 GNUNET_CRYPTO_hash_to_aes_key (&d,
127 &skey,
128 &iv);
129 memset (&h1,
130 0,
131 sizeof (h1));
132 h1.bits[1] = htonl (0x00200000); /* 32 + 8 + 2 = 42 MSB bits cleared */
133 GNUNET_assert (42 ==
134 GNUNET_CRYPTO_hash_count_leading_zeros (&h1));
135 GNUNET_assert (512 - 42 - 1 ==
136 GNUNET_CRYPTO_hash_count_tailing_zeros (&h1));
137 memset (&h1,
138 0,
139 sizeof (h1));
140 memset (&h2,
141 0,
142 sizeof (h2));
143 h1.bits[3] = htonl (0x00800011); /* 3*32 + 8 Bits identical */
144 h2.bits[3] = htonl (0x00000101); /* residual delta: 0x000220.. (+1 bit)*/
145 /* Note: XOR: 0x00800110 */
146 h1.bits[4] = htonl (0x14144141);
147 h2.bits[4] = htonl (0x28288282); /* residual delta: 0x3C3CC3.. */
148 /* Note: XOR: 0x3C3CC3C3 */
149 /* Note: XOR<<1: 0x78798786 */
150 GNUNET_assert (104 ==
151 GNUNET_CRYPTO_hash_count_leading_zeros (&h1));
152 GNUNET_CRYPTO_hash_xor (&h1,
153 &h2,
154 &s);
155 GNUNET_assert (104 ==
156 GNUNET_CRYPTO_hash_count_leading_zeros (&s));
157 GNUNET_assert (0x0002207879878600LLU ==
158 GNUNET_CRYPTO_hash_bucket_distance (&s,
159 104));
160
161 memset (&h1,
162 0,
163 sizeof (h1));
164 memset (&h2,
165 0,
166 sizeof (h2));
167 h1.bits[4] = htonl (0x00000001); /* 5*32 - 1 Bits identical */
168 h2.bits[4] = htonl (0x00000000);
169 /* Note: XOR: 0x00000001 */
170 h1.bits[5] = htonl (0x14144141);
171 h2.bits[5] = htonl (0x28288282);
172 /* Note: XOR: 0x3C3CC3C3 */
173 GNUNET_assert (159 ==
174 GNUNET_CRYPTO_hash_count_leading_zeros (&h1));
175 GNUNET_CRYPTO_hash_xor (&h1,
176 &h2,
177 &s);
178 GNUNET_assert (159 ==
179 GNUNET_CRYPTO_hash_count_leading_zeros (&s));
180 GNUNET_assert (0x3C3CC3C300000000 ==
181 GNUNET_CRYPTO_hash_bucket_distance (&s,
182 159));
183
184 memset (&h1,
185 0,
186 sizeof (h1));
187 memset (&h2,
188 0,
189 sizeof (h2));
190 h1.bits[14] = htonl (0x00000001); /* 15*32 - 1 Bits identical */
191 h2.bits[14] = htonl (0x00000000);
192 /* Note: XOR: 0x00000001 */
193 h1.bits[15] = htonl (0x14144141);
194 h2.bits[15] = htonl (0x28288282);
195 /* Note: XOR: 0x3C3CC3C3 */
196 GNUNET_assert (479 ==
197 GNUNET_CRYPTO_hash_count_leading_zeros (&h1));
198 GNUNET_CRYPTO_hash_xor (&h1,
199 &h2,
200 &s);
201 GNUNET_assert (479 ==
202 GNUNET_CRYPTO_hash_count_leading_zeros (&s));
203 GNUNET_assert (0x3C3CC3C300000000 ==
204 GNUNET_CRYPTO_hash_bucket_distance (&s,
205 479));
206
106 return 0; 207 return 0;
107} 208}
108 209
109 210
110static void 211static void
111finished_task (void *cls, const struct GNUNET_HashCode *res) 212finished_task (void *cls,
213 const struct GNUNET_HashCode *res)
112{ 214{
113 int *ret = cls; 215 int *ret = cls;
114 struct GNUNET_HashCode want; 216 struct GNUNET_HashCode want;
115 217
116 GNUNET_CRYPTO_hash (block, sizeof(block), &want); 218 GNUNET_CRYPTO_hash (block,
117 if (0 != memcmp (res, &want, sizeof(want))) 219 sizeof(block),
220 &want);
221 if (0 != GNUNET_memcmp (res,
222 &want))
118 *ret = 2; 223 *ret = 2;
119 else 224 else
120 *ret = 0; 225 *ret = 0;
@@ -126,43 +231,57 @@ file_hasher (void *cls)
126{ 231{
127 GNUNET_assert (NULL != 232 GNUNET_assert (NULL !=
128 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT, 233 GNUNET_CRYPTO_hash_file (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
129 FILENAME, 1024, &finished_task, cls)); 234 FILENAME,
235 1024,
236 &finished_task,
237 cls));
130} 238}
131 239
132 240
133static int 241static int
134testFileHash () 242test_file_hash (void)
135{ 243{
136 int ret; 244 int ret;
137 FILE *f; 245 FILE *f;
138 246
139 memset (block, 42, sizeof(block) / 2); 247 memset (block,
140 memset (&block[sizeof(block) / 2], 43, sizeof(block) / 2); 248 42,
249 sizeof(block) / 2);
250 memset (&block[sizeof(block) / 2],
251 43,
252 sizeof(block) / 2);
141 GNUNET_assert (NULL != (f = fopen (FILENAME, "w+"))); 253 GNUNET_assert (NULL != (f = fopen (FILENAME, "w+")));
142 GNUNET_break (sizeof(block) == fwrite (block, 1, sizeof(block), f)); 254 GNUNET_break (sizeof(block) ==
255 fwrite (block,
256 1,
257 sizeof(block),
258 f));
143 GNUNET_break (0 == fclose (f)); 259 GNUNET_break (0 == fclose (f));
144 ret = 1; 260 ret = 1;
145 GNUNET_SCHEDULER_run (&file_hasher, &ret); 261 GNUNET_SCHEDULER_run (&file_hasher,
262 &ret);
146 GNUNET_break (0 == unlink (FILENAME)); 263 GNUNET_break (0 == unlink (FILENAME));
147 return ret; 264 return ret;
148} 265}
149 266
150 267
151int 268int
152main (int argc, char *argv[]) 269main (int argc,
270 char *argv[])
153{ 271{
154 int failureCount = 0; 272 int failureCount = 0;
155 int i; 273
156 274 GNUNET_log_setup ("test-crypto-hash",
157 GNUNET_log_setup ("test-crypto-hash", "WARNING", NULL); 275 "WARNING",
158 for (i = 0; i < 10; i++) 276 NULL);
159 failureCount += testEncoding (); 277 for (int i = 0; i < 10; i++)
160 failureCount += testArithmetic (); 278 failureCount += test_encoding ();
161 failureCount += testFileHash (); 279 failureCount += test_arithmetic ();
162 if (failureCount != 0) 280 failureCount += test_file_hash ();
281 if (0 != failureCount)
163 return 1; 282 return 1;
164 return 0; 283 return 0;
165} 284}
166 285
167 286
168/* end of hashingtest.c */ 287/* end of test_crypto_hash.c */