diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/crypto_hash.c | 27 | ||||
-rw-r--r-- | src/util/gnunet-scrypt.c | 2 | ||||
-rw-r--r-- | src/util/test_crypto_hash.c | 11 |
3 files changed, 31 insertions, 9 deletions
diff --git a/src/util/crypto_hash.c b/src/util/crypto_hash.c index 4982ba404..622953476 100644 --- a/src/util/crypto_hash.c +++ b/src/util/crypto_hash.c | |||
@@ -244,17 +244,34 @@ GNUNET_CRYPTO_hash_to_aes_key (const struct GNUNET_HashCode *hc, | |||
244 | /** | 244 | /** |
245 | * Obtain a bit from a hashcode. | 245 | * Obtain a bit from a hashcode. |
246 | * @param code the GNUNET_CRYPTO_hash to index bit-wise | 246 | * @param code the GNUNET_CRYPTO_hash to index bit-wise |
247 | * @param bit index into the hashcode, [0...511] | 247 | * @param bit index into the hashcode, [0...511] where 0 is the leftmost bit |
248 | * (bytes in code interpreted big endian) | ||
248 | * @return Bit \a bit from hashcode \a code, -1 for invalid index | 249 | * @return Bit \a bit from hashcode \a code, -1 for invalid index |
249 | */ | 250 | */ |
250 | int | 251 | int |
251 | GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code, unsigned int | 252 | GNUNET_CRYPTO_hash_get_bit_ltr (const struct GNUNET_HashCode *code, |
252 | bit) | 253 | unsigned int bit) |
253 | { | 254 | { |
254 | GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode)); | 255 | GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode)); |
255 | return (((unsigned char *) code)[bit >> 3] & (128 >> (bit & 7))) > 0; | 256 | return (((unsigned char *) code)[bit >> 3] & (128 >> (bit & 7))) > 0; |
256 | } | 257 | } |
257 | 258 | ||
259 | /** | ||
260 | * Obtain a bit from a hashcode. | ||
261 | * @param code the GNUNET_CRYPTO_hash to index bit-wise | ||
262 | * @param bit index into the hashcode, [0...511] where 0 is the rightmost bit | ||
263 | * (bytes in code interpreted little endian) | ||
264 | * @return Bit \a bit from hashcode \a code, -1 for invalid index | ||
265 | */ | ||
266 | int | ||
267 | GNUNET_CRYPTO_hash_get_bit_rtl (const struct GNUNET_HashCode *code, | ||
268 | unsigned int bit) | ||
269 | { | ||
270 | GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode)); | ||
271 | return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0; | ||
272 | } | ||
273 | |||
274 | |||
258 | 275 | ||
259 | /** | 276 | /** |
260 | * Determine how many low order bits match in two | 277 | * Determine how many low order bits match in two |
@@ -275,8 +292,8 @@ GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first, | |||
275 | unsigned int i; | 292 | unsigned int i; |
276 | 293 | ||
277 | for (i = 0; i < sizeof(struct GNUNET_HashCode) * 8; i++) | 294 | for (i = 0; i < sizeof(struct GNUNET_HashCode) * 8; i++) |
278 | if (GNUNET_CRYPTO_hash_get_bit (first, i) != | 295 | if (GNUNET_CRYPTO_hash_get_bit_rtl (first, i) != |
279 | GNUNET_CRYPTO_hash_get_bit (second, i)) | 296 | GNUNET_CRYPTO_hash_get_bit_rtl (second, i)) |
280 | return i; | 297 | return i; |
281 | return sizeof(struct GNUNET_HashCode) * 8; | 298 | return sizeof(struct GNUNET_HashCode) * 8; |
282 | } | 299 | } |
diff --git a/src/util/gnunet-scrypt.c b/src/util/gnunet-scrypt.c index 70ba48d82..9bb766595 100644 --- a/src/util/gnunet-scrypt.c +++ b/src/util/gnunet-scrypt.c | |||
@@ -79,7 +79,7 @@ count_leading_zeroes (const struct GNUNET_HashCode *hash) | |||
79 | unsigned int hash_count; | 79 | unsigned int hash_count; |
80 | 80 | ||
81 | hash_count = 0; | 81 | hash_count = 0; |
82 | while (0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count)) | 82 | while (0 == GNUNET_CRYPTO_hash_get_bit_ltr (hash, hash_count)) |
83 | hash_count++; | 83 | hash_count++; |
84 | return hash_count; | 84 | return hash_count; |
85 | } | 85 | } |
diff --git a/src/util/test_crypto_hash.c b/src/util/test_crypto_hash.c index 12e1324dd..d22e1f5d3 100644 --- a/src/util/test_crypto_hash.c +++ b/src/util/test_crypto_hash.c | |||
@@ -91,10 +91,15 @@ testArithmetic () | |||
91 | return 1; | 91 | return 1; |
92 | if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2)) | 92 | if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2)) |
93 | return 1; | 93 | return 1; |
94 | memset (&d, 0xF0, sizeof(d)); | 94 | memset (&d, 0x40, sizeof(d)); |
95 | if (0 != GNUNET_CRYPTO_hash_get_bit (&d, 3)) | 95 | if (0 != GNUNET_CRYPTO_hash_get_bit_rtl (&d, 3)) |
96 | return 1; | 96 | return 1; |
97 | if (1 != GNUNET_CRYPTO_hash_get_bit (&d, 6)) | 97 | if (1 != GNUNET_CRYPTO_hash_get_bit_rtl (&d, 6)) |
98 | return 1; | ||
99 | memset (&d, 0x02, sizeof(d)); | ||
100 | if (0 != GNUNET_CRYPTO_hash_get_bit_ltr (&d, 3)) | ||
101 | return 1; | ||
102 | if (1 != GNUNET_CRYPTO_hash_get_bit_ltr (&d, 6)) | ||
98 | return 1; | 103 | return 1; |
99 | memset (&d, 0, sizeof(d)); | 104 | memset (&d, 0, sizeof(d)); |
100 | GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv); | 105 | GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv); |