diff options
author | Christian Grothoff <christian@grothoff.org> | 2022-01-02 13:10:25 +0100 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2022-01-02 13:10:25 +0100 |
commit | bb686c48354853aa725e493e85edce0602ed85e2 (patch) | |
tree | 6b0aeea58a7aebe0e3108da5302eaeae2c996ceb /src/util | |
parent | 2cbb614f694701c4afdab88f58ef7626629e1bc3 (diff) | |
download | gnunet-bb686c48354853aa725e493e85edce0602ed85e2.tar.gz gnunet-bb686c48354853aa725e493e85edce0602ed85e2.zip |
revise DHT hashing functions, add test logic
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/crypto_hash.c | 143 | ||||
-rw-r--r-- | src/util/gnunet-scrypt.c | 21 | ||||
-rw-r--r-- | src/util/test_crypto_hash.c | 227 |
3 files changed, 275 insertions, 116 deletions
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c index d62ec8012..b51ecd242 100644 --- a/src/util/crypto_hash.c +++ b/src/util/crypto_hash.c | |||
@@ -135,18 +135,57 @@ GNUNET_CRYPTO_hash_xor (const struct GNUNET_HashCode *a, | |||
135 | const struct GNUNET_HashCode *b, | 135 | const struct GNUNET_HashCode *b, |
136 | struct GNUNET_HashCode *result) | 136 | struct GNUNET_HashCode *result) |
137 | { | 137 | { |
138 | for (ssize_t i = (sizeof(struct GNUNET_HashCode) / sizeof(unsigned int)) - 1; | 138 | const unsigned long long *lla = (const unsigned long long *) a; |
139 | i >= 0; | 139 | const unsigned long long *llb = (const unsigned long long *) b; |
140 | i--) | 140 | unsigned long long *llr = (unsigned long long *) result; |
141 | result->bits[i] = a->bits[i] ^ b->bits[i]; | 141 | |
142 | GNUNET_static_assert (8 == sizeof (unsigned long long)); | ||
143 | GNUNET_static_assert (0 == sizeof (*a) % sizeof (unsigned long long)); | ||
144 | for (int i = sizeof (*result) / sizeof (*llr) - 1; i>=0; i--) | ||
145 | llr[i] = lla[i] ^ llb[i]; | ||
146 | } | ||
147 | |||
148 | |||
149 | uint64_t | ||
150 | GNUNET_CRYPTO_hash_bucket_distance (const struct GNUNET_HashCode *xor, | ||
151 | unsigned int bucket) | ||
152 | { | ||
153 | const uint64_t *u = (const uint64_t *) xor; | ||
154 | unsigned int idx; | ||
155 | unsigned int bits; | ||
156 | uint64_t rval; | ||
157 | |||
158 | if (bucket == 8 * sizeof(*xor)) | ||
159 | return 0; | ||
160 | bucket++; | ||
161 | idx = bucket / 64; | ||
162 | bits = bucket % 64; | ||
163 | if (idx >= sizeof (*xor) / sizeof (*u)) | ||
164 | return 0; | ||
165 | if (0 == bits) | ||
166 | { | ||
167 | /* keeps no bits */ | ||
168 | rval = 0; | ||
169 | } | ||
170 | else | ||
171 | { | ||
172 | /* keeps lowest (64-bits) bits */ | ||
173 | rval = GNUNET_ntohll (u[idx]) << bits; | ||
174 | } | ||
175 | if (idx + 1 < sizeof (*xor) / sizeof (*u)) | ||
176 | { | ||
177 | /* discards lowest (bits) bits */ | ||
178 | rval |= GNUNET_ntohll (u[idx + 1]) >> (64 - bits); | ||
179 | } | ||
180 | return rval; | ||
142 | } | 181 | } |
143 | 182 | ||
144 | 183 | ||
145 | void | 184 | void |
146 | GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode *hc, | 185 | GNUNET_CRYPTO_hash_to_aes_key ( |
147 | struct GNUNET_CRYPTO_SymmetricSessionKey *skey, | 186 | const struct GNUNET_HashCode *hc, |
148 | struct | 187 | struct GNUNET_CRYPTO_SymmetricSessionKey *skey, |
149 | GNUNET_CRYPTO_SymmetricInitializationVector *iv) | 188 | struct GNUNET_CRYPTO_SymmetricInitializationVector *iv) |
150 | { | 189 | { |
151 | GNUNET_assert (GNUNET_YES == | 190 | GNUNET_assert (GNUNET_YES == |
152 | GNUNET_CRYPTO_kdf ( | 191 | GNUNET_CRYPTO_kdf ( |
@@ -167,33 +206,47 @@ GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode *hc, | |||
167 | } | 206 | } |
168 | 207 | ||
169 | 208 | ||
170 | int | 209 | unsigned int |
171 | GNUNET_CRYPTO_hash_get_bit_ltr (const struct GNUNET_HashCode *code, | 210 | GNUNET_CRYPTO_hash_count_leading_zeros (const struct GNUNET_HashCode *h) |
172 | unsigned int bit) | ||
173 | { | 211 | { |
174 | GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode)); | 212 | const unsigned long long *llp = (const unsigned long long *) h; |
175 | return (((unsigned char *) code)[bit >> 3] & (128 >> (bit & 7))) > 0; | 213 | unsigned int ret = 0; |
176 | } | 214 | unsigned int i; |
177 | |||
178 | 215 | ||
179 | int | 216 | GNUNET_static_assert (8 == sizeof (unsigned long long)); |
180 | GNUNET_CRYPTO_hash_get_bit_rtl (const struct GNUNET_HashCode *code, | 217 | GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long)); |
181 | unsigned int bit) | 218 | for (i = 0; i<sizeof (*h) / sizeof (*llp); i++) |
182 | { | 219 | { |
183 | GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode)); | 220 | if (0LLU != llp[i]) |
184 | return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0; | 221 | break; |
222 | ret += sizeof (*llp) * 8; | ||
223 | } | ||
224 | if (ret == 8 * sizeof (*h)) | ||
225 | return ret; | ||
226 | ret += __builtin_clzll (GNUNET_ntohll ((uint64_t) llp[i])); | ||
227 | return ret; | ||
185 | } | 228 | } |
186 | 229 | ||
187 | 230 | ||
188 | unsigned int | 231 | unsigned int |
189 | GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first, | 232 | GNUNET_CRYPTO_hash_count_tailing_zeros (const struct GNUNET_HashCode *h) |
190 | const struct GNUNET_HashCode *second) | ||
191 | { | 233 | { |
192 | for (unsigned int i = 0; i < sizeof(struct GNUNET_HashCode) * 8; i++) | 234 | const unsigned long long *llp = (const unsigned long long *) h; |
193 | if (GNUNET_CRYPTO_hash_get_bit_rtl (first, i) != | 235 | unsigned int ret = 0; |
194 | GNUNET_CRYPTO_hash_get_bit_rtl (second, i)) | 236 | int i; |
195 | return i; | 237 | |
196 | return sizeof(struct GNUNET_HashCode) * 8; | 238 | GNUNET_static_assert (8 == sizeof (unsigned long long)); |
239 | GNUNET_static_assert (0 == sizeof (*h) % sizeof (unsigned long long)); | ||
240 | for (i = sizeof (*h) / sizeof (*llp) - 1; i>=0; i--) | ||
241 | { | ||
242 | if (0LLU != llp[i]) | ||
243 | break; | ||
244 | ret += sizeof (*llp) * 8; | ||
245 | } | ||
246 | if (ret == 8 * sizeof (*h)) | ||
247 | return ret; | ||
248 | ret += __builtin_ctzll (GNUNET_ntohll ((uint64_t) llp[i])); | ||
249 | return ret; | ||
197 | } | 250 | } |
198 | 251 | ||
199 | 252 | ||
@@ -243,25 +296,30 @@ GNUNET_CRYPTO_hash_xorcmp (const struct GNUNET_HashCode *h1, | |||
243 | 296 | ||
244 | 297 | ||
245 | void | 298 | void |
246 | GNUNET_CRYPTO_hmac_derive_key (struct GNUNET_CRYPTO_AuthKey *key, | 299 | GNUNET_CRYPTO_hmac_derive_key ( |
247 | const struct | 300 | struct GNUNET_CRYPTO_AuthKey *key, |
248 | GNUNET_CRYPTO_SymmetricSessionKey *rkey, | 301 | const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, |
249 | const void *salt, size_t salt_len, ...) | 302 | const void *salt, size_t salt_len, |
303 | ...) | ||
250 | { | 304 | { |
251 | va_list argp; | 305 | va_list argp; |
252 | 306 | ||
253 | va_start (argp, salt_len); | 307 | va_start (argp, |
254 | GNUNET_CRYPTO_hmac_derive_key_v (key, rkey, salt, salt_len, argp); | 308 | salt_len); |
309 | GNUNET_CRYPTO_hmac_derive_key_v (key, | ||
310 | rkey, | ||
311 | salt, salt_len, | ||
312 | argp); | ||
255 | va_end (argp); | 313 | va_end (argp); |
256 | } | 314 | } |
257 | 315 | ||
258 | 316 | ||
259 | void | 317 | void |
260 | GNUNET_CRYPTO_hmac_derive_key_v (struct GNUNET_CRYPTO_AuthKey *key, | 318 | GNUNET_CRYPTO_hmac_derive_key_v ( |
261 | const struct | 319 | struct GNUNET_CRYPTO_AuthKey *key, |
262 | GNUNET_CRYPTO_SymmetricSessionKey *rkey, | 320 | const struct GNUNET_CRYPTO_SymmetricSessionKey *rkey, |
263 | const void *salt, size_t salt_len, | 321 | const void *salt, size_t salt_len, |
264 | va_list argp) | 322 | va_list argp) |
265 | { | 323 | { |
266 | GNUNET_CRYPTO_kdf_v (key->key, sizeof(key->key), | 324 | GNUNET_CRYPTO_kdf_v (key->key, sizeof(key->key), |
267 | salt, salt_len, | 325 | salt, salt_len, |
@@ -283,7 +341,9 @@ GNUNET_CRYPTO_hmac_raw (const void *key, size_t key_len, | |||
283 | { | 341 | { |
284 | once = 1; | 342 | once = 1; |
285 | GNUNET_assert (GPG_ERR_NO_ERROR == | 343 | GNUNET_assert (GPG_ERR_NO_ERROR == |
286 | gcry_md_open (&md, GCRY_MD_SHA512, GCRY_MD_FLAG_HMAC)); | 344 | gcry_md_open (&md, |
345 | GCRY_MD_SHA512, | ||
346 | GCRY_MD_FLAG_HMAC)); | ||
287 | } | 347 | } |
288 | else | 348 | else |
289 | { | 349 | { |
@@ -323,15 +383,12 @@ GNUNET_CRYPTO_hash_context_start () | |||
323 | struct GNUNET_HashContext *hc; | 383 | struct GNUNET_HashContext *hc; |
324 | 384 | ||
325 | BENCHMARK_START (hash_context_start); | 385 | BENCHMARK_START (hash_context_start); |
326 | |||
327 | hc = GNUNET_new (struct GNUNET_HashContext); | 386 | hc = GNUNET_new (struct GNUNET_HashContext); |
328 | GNUNET_assert (0 == | 387 | GNUNET_assert (0 == |
329 | gcry_md_open (&hc->hd, | 388 | gcry_md_open (&hc->hd, |
330 | GCRY_MD_SHA512, | 389 | GCRY_MD_SHA512, |
331 | 0)); | 390 | 0)); |
332 | |||
333 | BENCHMARK_END (hash_context_start); | 391 | BENCHMARK_END (hash_context_start); |
334 | |||
335 | return hc; | 392 | return hc; |
336 | } | 393 | } |
337 | 394 | ||
diff --git a/src/util/gnunet-scrypt.c b/src/util/gnunet-scrypt.c index fe8b6769f..ad46e3f39 100644 --- a/src/util/gnunet-scrypt.c +++ b/src/util/gnunet-scrypt.c | |||
@@ -78,24 +78,6 @@ shutdown_task (void *cls) | |||
78 | 78 | ||
79 | 79 | ||
80 | /** | 80 | /** |
81 | * Count the leading zeroes in hash. | ||
82 | * | ||
83 | * @param hash to count leading zeros in | ||
84 | * @return the number of leading zero bits. | ||
85 | */ | ||
86 | static unsigned int | ||
87 | count_leading_zeroes (const struct GNUNET_HashCode *hash) | ||
88 | { | ||
89 | unsigned int hash_count; | ||
90 | |||
91 | hash_count = 0; | ||
92 | while (0 == GNUNET_CRYPTO_hash_get_bit_ltr (hash, hash_count)) | ||
93 | hash_count++; | ||
94 | return hash_count; | ||
95 | } | ||
96 | |||
97 | |||
98 | /** | ||
99 | * Find our proof of work. | 81 | * Find our proof of work. |
100 | * | 82 | * |
101 | * @param cls closure (unused) | 83 | * @param cls closure (unused) |
@@ -131,7 +113,8 @@ find_proof (void *cls) | |||
131 | buf, | 113 | buf, |
132 | sizeof(buf), | 114 | sizeof(buf), |
133 | &result); | 115 | &result); |
134 | if (nse_work_required <= count_leading_zeroes (&result)) | 116 | if (nse_work_required <= |
117 | GNUNET_CRYPTO_hash_count_leading_zeros (&result)) | ||
135 | { | 118 | { |
136 | proof = counter; | 119 | proof = counter; |
137 | fprintf (stdout, | 120 | fprintf (stdout, |
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 | ||
53 | static int | 59 | static int |
54 | testEncoding () | 60 | test_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 | ||
65 | static int | 69 | static int |
66 | testArithmetic () | 70 | test_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 | ||
110 | static void | 211 | static void |
111 | finished_task (void *cls, const struct GNUNET_HashCode *res) | 212 | finished_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 | ||
133 | static int | 241 | static int |
134 | testFileHash () | 242 | test_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 | ||
151 | int | 268 | int |
152 | main (int argc, char *argv[]) | 269 | main (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 */ |