diff options
author | Tom Gundersen <teg@jklm.no> | 2015-10-06 15:04:42 +0200 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2015-10-06 17:47:00 +0200 |
commit | 0cb3c286883b694fc52a18a3b559ff98931641f3 (patch) | |
tree | f8c3cf9104b3ea8fc180bd1de1e72119dfe4524e /src | |
parent | Merge pull request #1473 from michich/test-hashmap (diff) | |
download | systemd-0cb3c286883b694fc52a18a3b559ff98931641f3.tar.xz systemd-0cb3c286883b694fc52a18a3b559ff98931641f3.zip |
siphash24: unify API
Make the API of the new helpers more similar to the old wrapper.
In particular we now return the hash as a byte string to avoid
any endianness problems.
Diffstat (limited to 'src')
-rw-r--r-- | src/basic/hashmap.c | 7 | ||||
-rw-r--r-- | src/basic/siphash24.c | 15 | ||||
-rw-r--r-- | src/basic/siphash24.h | 4 | ||||
-rw-r--r-- | src/journal/journald-rate-limit.c | 12 | ||||
-rw-r--r-- | src/libsystemd-network/sd-dhcp-server.c | 6 | ||||
-rw-r--r-- | src/libsystemd-network/test-dhcp-server.c | 7 | ||||
-rw-r--r-- | src/test/test-siphash24.c | 24 |
7 files changed, 37 insertions, 38 deletions
diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index 3e17ed30df..20e7e51d9e 100644 --- a/src/basic/hashmap.c +++ b/src/basic/hashmap.c @@ -372,12 +372,15 @@ static uint8_t *hash_key(HashmapBase *h) { static unsigned base_bucket_hash(HashmapBase *h, const void *p) { struct siphash state; + uint64_t hash; - siphash_init(&state, hash_key(h)); + siphash24_init(&state, hash_key(h)); h->hash_ops->hash(p, &state); - return (unsigned) (siphash24_finalize(&state) % n_buckets(h)); + siphash24_finalize((uint8_t*)&hash, &state); + + return (unsigned) (hash % n_buckets(h)); } #define bucket_hash(h, p) base_bucket_hash(HASHMAP_BASE(h), p) diff --git a/src/basic/siphash24.c b/src/basic/siphash24.c index 308e4230c5..9c56b5e9ea 100644 --- a/src/basic/siphash24.c +++ b/src/basic/siphash24.c @@ -52,7 +52,7 @@ typedef uint8_t u8; (state)->v2 += (state)->v1; (state)->v1=ROTL((state)->v1,17); (state)->v1 ^= (state)->v2; (state)->v2=ROTL((state)->v2,32); \ } while(0) -void siphash_init(struct siphash *state, const uint8_t k[16]) { +void siphash24_init(struct siphash *state, const uint8_t k[16]) { u64 k0, k1; k0 = U8TO64_LE( k ); @@ -140,7 +140,7 @@ void siphash24_compress(const void *_in, size_t inlen, struct siphash *state) { } } -uint64_t siphash24_finalize(struct siphash *state) { +void siphash24_finalize(uint8_t out[8], struct siphash *state) { u64 b; b = state->padding | (( ( u64 )state->inlen ) << 56); @@ -168,20 +168,19 @@ uint64_t siphash24_finalize(struct siphash *state) { SIPROUND(state); SIPROUND(state); - return state->v0 ^ state->v1 ^ state->v2 ^ state->v3; + b = state->v0 ^ state->v1 ^ state->v2 ^ state->v3; + + U64TO8_LE( out, b ); } /* SipHash-2-4 */ void siphash24(uint8_t out[8], const void *_in, size_t inlen, const uint8_t k[16]) { struct siphash state; - u64 b; - siphash_init(&state, k); + siphash24_init(&state, k); siphash24_compress(_in, inlen, &state); - b = siphash24_finalize(&state); - - U64TO8_LE( out, b ); + siphash24_finalize(out, &state); } diff --git a/src/basic/siphash24.h b/src/basic/siphash24.h index c107bdd213..6c5cd98ee8 100644 --- a/src/basic/siphash24.h +++ b/src/basic/siphash24.h @@ -12,8 +12,8 @@ struct siphash { size_t inlen; }; -void siphash_init(struct siphash *state, const uint8_t k[16]); +void siphash24_init(struct siphash *state, const uint8_t k[16]); void siphash24_compress(const void *in, size_t inlen, struct siphash *state); -uint64_t siphash24_finalize(struct siphash *state); +void siphash24_finalize(uint8_t out[8], struct siphash *state); void siphash24(uint8_t out[8], const void *in, size_t inlen, const uint8_t k[16]); diff --git a/src/journal/journald-rate-limit.c b/src/journal/journald-rate-limit.c index 7e06117b31..8afd493b50 100644 --- a/src/journal/journald-rate-limit.c +++ b/src/journal/journald-rate-limit.c @@ -57,7 +57,7 @@ struct JournalRateLimitGroup { char *id; JournalRateLimitPool pools[POOLS_MAX]; - unsigned long hash; + uint64_t hash; LIST_FIELDS(JournalRateLimitGroup, bucket); LIST_FIELDS(JournalRateLimitGroup, lru); @@ -158,9 +158,9 @@ static JournalRateLimitGroup* journal_rate_limit_group_new(JournalRateLimit *r, if (!g->id) goto fail; - siphash_init(&state, r->hash_key); + siphash24_init(&state, r->hash_key); string_hash_func(g->id, &state); - g->hash = siphash24_finalize(&state); + siphash24_finalize((uint8_t*)&g->hash, &state); journal_rate_limit_vacuum(r, ts); @@ -207,7 +207,7 @@ static unsigned burst_modulate(unsigned burst, uint64_t available) { } int journal_rate_limit_test(JournalRateLimit *r, const char *id, int priority, uint64_t available) { - unsigned long h; + uint64_t h; JournalRateLimitGroup *g; JournalRateLimitPool *p; struct siphash state; @@ -226,9 +226,9 @@ int journal_rate_limit_test(JournalRateLimit *r, const char *id, int priority, u ts = now(CLOCK_MONOTONIC); - siphash_init(&state, r->hash_key); + siphash24_init(&state, r->hash_key); string_hash_func(id, &state); - h = siphash24_finalize(&state); + siphash24_finalize((uint8_t*)&h, &state); g = r->buckets[h % BUCKETS_MAX]; LIST_FOREACH(bucket, g, g) diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c index d941e6c0de..d27bb561ca 100644 --- a/src/libsystemd-network/sd-dhcp-server.c +++ b/src/libsystemd-network/sd-dhcp-server.c @@ -741,15 +741,17 @@ int dhcp_server_handle_message(sd_dhcp_server *server, DHCPMessage *message, address = existing_lease->address; else { struct siphash state; + uint64_t hash; uint32_t next_offer; /* even with no persistence of leases, we try to offer the same client the same IP address. we do this by using the hash of the client id as the offset into the pool of leases when finding the next free one */ - siphash_init(&state, HASH_KEY.bytes); + siphash24_init(&state, HASH_KEY.bytes); client_id_hash_func(&req->client_id, &state); - next_offer = siphash24_finalize(&state) % server->pool_size; + siphash24_finalize((uint8_t*)&hash, &state); + next_offer = hash % server->pool_size; for (i = 0; i < server->pool_size; i++) { if (!server->bound_leases[next_offer]) { diff --git a/src/libsystemd-network/test-dhcp-server.c b/src/libsystemd-network/test-dhcp-server.c index 01205efc18..c3bcb9cb4b 100644 --- a/src/libsystemd-network/test-dhcp-server.c +++ b/src/libsystemd-network/test-dhcp-server.c @@ -200,10 +200,13 @@ static void test_message_handler(void) { static uint64_t client_id_hash_helper(DHCPClientId *id, uint8_t key[HASH_KEY_SIZE]) { struct siphash state; + uint64_t hash; - siphash_init(&state, key); + siphash24_init(&state, key); client_id_hash_func(id, &state); - return siphash24_finalize(&state); + siphash24_finalize((uint8_t*)&hash, &state); + + return hash; } static void test_client_id_hash(void) { diff --git a/src/test/test-siphash24.c b/src/test/test-siphash24.c index 65eb2b6f35..2402da6a6f 100644 --- a/src/test/test-siphash24.c +++ b/src/test/test-siphash24.c @@ -32,23 +32,13 @@ int main(int argc, char *argv[]) { const uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; uint64_t out = 0; - unsigned i, j, k; - usec_t ts; + unsigned i, j; siphash24((uint8_t *)&out, in, sizeof(in), key); - assert_se(out == 0xa129ca6149be45e5); - - assert_se(out == 0xa129ca6149be45e5ULL); - - ts = now(CLOCK_MONOTONIC); - for (k = 0; k < ITERATIONS; k++) - siphash24((uint8_t *)&out, in, sizeof(in), key); - ts = now(CLOCK_MONOTONIC) - ts; - - log_info("%llu iterations per second", (ITERATIONS * USEC_PER_SEC) / ts); + assert_se(out == htole64(0xa129ca6149be45e5)); /* verify the internal state as given in the above paper */ - siphash_init(&state, key); + siphash24_init(&state, key); assert_se(state.v0 == 0x7469686173716475); assert_se(state.v1 == 0x6b617f6d656e6665); assert_se(state.v2 == 0x6b7f62616d677361); @@ -58,7 +48,8 @@ int main(int argc, char *argv[]) { assert_se(state.v1 == 0x0d52f6f62a4f59a4); assert_se(state.v2 == 0x634cb3577b01fd3d); assert_se(state.v3 == 0xa5224d6f55c7d9c8); - assert_se(siphash24_finalize(&state) == 0xa129ca6149be45e5); + siphash24_finalize((uint8_t*)&out, &state); + assert_se(out == htole64(0xa129ca6149be45e5)); assert_se(state.v0 == 0xf6bcd53893fecff1); assert_se(state.v1 == 0x54b9964c7ea0d937); assert_se(state.v2 == 0x1b38329c099bb55a); @@ -68,11 +59,12 @@ int main(int argc, char *argv[]) { same result */ for (i = 0; i < sizeof(in); i++) { for (j = i; j < sizeof(in); j++) { - siphash_init(&state, key); + siphash24_init(&state, key); siphash24_compress(in, i, &state); siphash24_compress(&in[i], j - i, &state); siphash24_compress(&in[j], sizeof(in) - j, &state); - assert_se(siphash24_finalize(&state) == 0xa129ca6149be45e5); + siphash24_finalize((uint8_t*)&out, &state); + assert_se(out == htole64(0xa129ca6149be45e5)); } } } |