From 669aa594e6a4d06e2d77cdba5365f6992695f547 Mon Sep 17 00:00:00 2001 From: Martin Schanzenbach Date: Tue, 26 May 2020 08:52:28 +0200 Subject: [PATCH] fix bit counting mess --- src/dht/gnunet-service-dht_neighbours.c | 4 ++-- src/gnsrecord/gnunet-gnsrecord-tvg.c | 2 +- src/include/gnunet_crypto_lib.h | 19 ++++++++++++++--- src/nse/gnunet-service-nse.c | 2 +- src/revocation/revocation_api.c | 2 +- src/util/crypto_hash.c | 27 ++++++++++++++++++++----- src/util/gnunet-scrypt.c | 2 +- src/util/test_crypto_hash.c | 11 +++++++--- 8 files changed, 52 insertions(+), 17 deletions(-) diff --git a/src/dht/gnunet-service-dht_neighbours.c b/src/dht/gnunet-service-dht_neighbours.c index c251dfa12..fce69d3f6 100644 --- a/src/dht/gnunet-service-dht_neighbours.c +++ b/src/dht/gnunet-service-dht_neighbours.c @@ -927,8 +927,8 @@ get_distance (const struct GNUNET_HashCode *target, (i < sizeof(struct GNUNET_HashCode) * 8) && (i < bucket + 1 + 32 - 9); i++) { - if (GNUNET_CRYPTO_hash_get_bit (target, i) != - GNUNET_CRYPTO_hash_get_bit (have, i)) + if (GNUNET_CRYPTO_hash_get_bit_rtl (target, i) != + GNUNET_CRYPTO_hash_get_bit_rtl (have, i)) lsb |= (1 << (bucket + 32 - 9 - i)); /* first bit set will be 10, * last bit set will be 31 -- if * i does not reach 512 first... */ diff --git a/src/gnsrecord/gnunet-gnsrecord-tvg.c b/src/gnsrecord/gnunet-gnsrecord-tvg.c index cf815d629..862bd6f86 100644 --- a/src/gnsrecord/gnunet-gnsrecord-tvg.c +++ b/src/gnsrecord/gnunet-gnsrecord-tvg.c @@ -47,7 +47,7 @@ print_record(const struct GNUNET_GNSRECORD_Data *rd) fprintf (stdout, "EXPIRATION: %"PRIu64"\n", rd->expiration_time); fprintf (stdout, - "DATA_SIZE: %"PRIu64"\n", rd->data_size); + "DATA_SIZE: %zu\n", rd->data_size); fprintf (stdout, "TYPE: %d\n", rd->record_type); fprintf (stdout, diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h index a5a50e749..e880bd887 100644 --- a/src/include/gnunet_crypto_lib.h +++ b/src/include/gnunet_crypto_lib.h @@ -874,12 +874,25 @@ GNUNET_CRYPTO_hash_to_aes_key ( * Obtain a bit from a hashcode. * * @param code the `struct GNUNET_HashCode` to index bit-wise - * @param bit index into the hashcode, [0...159] + * @param bit index into the hashcode, [0...159] where 0 is the leftmost bit + * (bytes in code interpreted big endian) * @return Bit \a bit from hashcode \a code, -1 for invalid index */ int -GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code, - unsigned int bit); +GNUNET_CRYPTO_hash_get_bit_ltr (const struct GNUNET_HashCode *code, + unsigned int bit); + + +/** + * Obtain a bit from a hashcode. + * @param code the GNUNET_CRYPTO_hash to index bit-wise + * @param bit index into the hashcode, [0...511] where 0 is the rightmost bit + * (bytes in code interpreted little endian) + * @return Bit \a bit from hashcode \a code, -1 for invalid index + */ +int +GNUNET_CRYPTO_hash_get_bit_rtl (const struct GNUNET_HashCode *code, + unsigned int bit); /** diff --git a/src/nse/gnunet-service-nse.c b/src/nse/gnunet-service-nse.c index 411f533a5..461d55a7f 100644 --- a/src/nse/gnunet-service-nse.c +++ b/src/nse/gnunet-service-nse.c @@ -780,7 +780,7 @@ count_leading_zeroes (const struct GNUNET_HashCode *hash) unsigned int hash_count; hash_count = 0; - while (0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count)) + while (0 == GNUNET_CRYPTO_hash_get_bit_ltr (hash, hash_count)) hash_count++; return hash_count; } diff --git a/src/revocation/revocation_api.c b/src/revocation/revocation_api.c index 2ae8e2df9..33c67d005 100644 --- a/src/revocation/revocation_api.c +++ b/src/revocation/revocation_api.c @@ -395,7 +395,7 @@ count_leading_zeroes (const struct GNUNET_HashCode *hash) { unsigned int hash_count; hash_count = 0; - while ((0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count))) + while ((0 == GNUNET_CRYPTO_hash_get_bit_ltr (hash, hash_count))) hash_count++; return hash_count; } 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, /** * Obtain a bit from a hashcode. * @param code the GNUNET_CRYPTO_hash to index bit-wise - * @param bit index into the hashcode, [0...511] + * @param bit index into the hashcode, [0...511] where 0 is the leftmost bit + * (bytes in code interpreted big endian) * @return Bit \a bit from hashcode \a code, -1 for invalid index */ int -GNUNET_CRYPTO_hash_get_bit (const struct GNUNET_HashCode *code, unsigned int - bit) +GNUNET_CRYPTO_hash_get_bit_ltr (const struct GNUNET_HashCode *code, + unsigned int bit) { GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode)); return (((unsigned char *) code)[bit >> 3] & (128 >> (bit & 7))) > 0; } +/** + * Obtain a bit from a hashcode. + * @param code the GNUNET_CRYPTO_hash to index bit-wise + * @param bit index into the hashcode, [0...511] where 0 is the rightmost bit + * (bytes in code interpreted little endian) + * @return Bit \a bit from hashcode \a code, -1 for invalid index + */ +int +GNUNET_CRYPTO_hash_get_bit_rtl (const struct GNUNET_HashCode *code, + unsigned int bit) +{ + GNUNET_assert (bit < 8 * sizeof(struct GNUNET_HashCode)); + return (((unsigned char *) code)[bit >> 3] & (1 << (bit & 7))) > 0; +} + + /** * Determine how many low order bits match in two @@ -275,8 +292,8 @@ GNUNET_CRYPTO_hash_matching_bits (const struct GNUNET_HashCode *first, unsigned int i; for (i = 0; i < sizeof(struct GNUNET_HashCode) * 8; i++) - if (GNUNET_CRYPTO_hash_get_bit (first, i) != - GNUNET_CRYPTO_hash_get_bit (second, i)) + if (GNUNET_CRYPTO_hash_get_bit_rtl (first, i) != + GNUNET_CRYPTO_hash_get_bit_rtl (second, i)) return i; return sizeof(struct GNUNET_HashCode) * 8; } 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) unsigned int hash_count; hash_count = 0; - while (0 == GNUNET_CRYPTO_hash_get_bit (hash, hash_count)) + while (0 == GNUNET_CRYPTO_hash_get_bit_ltr (hash, hash_count)) hash_count++; return hash_count; } 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 () return 1; if (1 != GNUNET_CRYPTO_hash_xorcmp (&h1, &h2, &h2)) return 1; - memset (&d, 0xF0, sizeof(d)); - if (0 != GNUNET_CRYPTO_hash_get_bit (&d, 3)) + memset (&d, 0x40, sizeof(d)); + if (0 != GNUNET_CRYPTO_hash_get_bit_rtl (&d, 3)) return 1; - if (1 != GNUNET_CRYPTO_hash_get_bit (&d, 6)) + if (1 != GNUNET_CRYPTO_hash_get_bit_rtl (&d, 6)) + return 1; + memset (&d, 0x02, sizeof(d)); + if (0 != GNUNET_CRYPTO_hash_get_bit_ltr (&d, 3)) + return 1; + if (1 != GNUNET_CRYPTO_hash_get_bit_ltr (&d, 6)) return 1; memset (&d, 0, sizeof(d)); GNUNET_CRYPTO_hash_to_aes_key (&d, &skey, &iv); -- 2.25.1