From 933f9caeeb2b3c1b951d330e04beb04226e5a890 Mon Sep 17 00:00:00 2001 From: Daniel Mack Date: Mon, 16 Nov 2015 23:17:52 +0100 Subject: [PATCH] siphash24: let siphash24_finalize() and siphash24() return the result directly Rather than passing a pointer to return the result, return it directly from the function calls. Also, return the result in native endianess, and let the callers care about the conversion. For hash tables and bloom filters, we don't care, but in order to keep MAC addresses and DHCP client IDs stable, we explicitly convert to LE. --- src/basic/hashmap.c | 2 +- src/basic/siphash24.c | 9 +++++---- src/basic/siphash24.h | 4 ++-- src/import/pull-common.c | 2 +- src/journal/journald-rate-limit.c | 4 ++-- src/libsystemd-network/dhcp-identifier.c | 8 +++++--- src/libsystemd-network/network-internal.c | 2 +- src/libsystemd-network/sd-dhcp-server.c | 2 +- src/libsystemd-network/sd-ipv4ll.c | 5 ++--- src/libsystemd-network/test-dhcp-server.c | 4 +--- src/libsystemd/sd-bus/bus-bloom.c | 2 +- src/network/networkd-netdev.c | 2 +- src/nspawn/nspawn-network.c | 2 +- src/test/test-siphash24.c | 6 +++--- 14 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/basic/hashmap.c b/src/basic/hashmap.c index d88ceb4..6e501ef 100644 --- a/src/basic/hashmap.c +++ b/src/basic/hashmap.c @@ -380,7 +380,7 @@ static unsigned base_bucket_hash(HashmapBase *h, const void *p) { h->hash_ops->hash(p, &state); - siphash24_finalize(&hash, &state); + hash = siphash24_finalize(&state); return (unsigned) (hash % n_buckets(h)); } diff --git a/src/basic/siphash24.c b/src/basic/siphash24.c index d7640d3..b6b6172 100644 --- a/src/basic/siphash24.c +++ b/src/basic/siphash24.c @@ -141,7 +141,7 @@ void siphash24_compress(const void *_in, size_t inlen, struct siphash *state) { } } -void siphash24_finalize(uint64_t *out, struct siphash *state) { +uint64_t siphash24_finalize(struct siphash *state) { uint64_t b; b = state->padding | (( ( uint64_t )state->inlen ) << 56); @@ -170,14 +170,15 @@ void siphash24_finalize(uint64_t *out, struct siphash *state) { sipround(state); sipround(state); - *(le64_t*)out = htole64(state->v0 ^ state->v1 ^ state->v2 ^ state->v3); + return state->v0 ^ state->v1 ^ state->v2 ^ state->v3; } /* SipHash-2-4 */ -void siphash24(uint64_t *out, const void *_in, size_t inlen, const uint8_t k[16]) { +uint64_t siphash24(const void *_in, size_t inlen, const uint8_t k[16]) { struct siphash state; siphash24_init(&state, k); siphash24_compress(_in, inlen, &state); - siphash24_finalize(out, &state); + + return siphash24_finalize(&state); } diff --git a/src/basic/siphash24.h b/src/basic/siphash24.h index dc08077..0e072eb 100644 --- a/src/basic/siphash24.h +++ b/src/basic/siphash24.h @@ -14,6 +14,6 @@ struct siphash { void siphash24_init(struct siphash *state, const uint8_t k[16]); void siphash24_compress(const void *in, size_t inlen, struct siphash *state); -void siphash24_finalize(uint64_t *out, struct siphash *state); +uint64_t siphash24_finalize(struct siphash *state); -void siphash24(uint64_t *out, const void *in, size_t inlen, const uint8_t k[16]); +uint64_t siphash24(const void *in, size_t inlen, const uint8_t k[16]); diff --git a/src/import/pull-common.c b/src/import/pull-common.c index 16b0c61..a83cfff 100644 --- a/src/import/pull-common.c +++ b/src/import/pull-common.c @@ -165,7 +165,7 @@ static int hash_url(const char *url, char **ret) { assert(url); - siphash24(&h, url, strlen(url), k.bytes); + h = siphash24(url, strlen(url), k.bytes); if (asprintf(ret, "%"PRIx64, h) < 0) return -ENOMEM; diff --git a/src/journal/journald-rate-limit.c b/src/journal/journald-rate-limit.c index ad78d09..2d46efe 100644 --- a/src/journal/journald-rate-limit.c +++ b/src/journal/journald-rate-limit.c @@ -162,7 +162,7 @@ static JournalRateLimitGroup* journal_rate_limit_group_new(JournalRateLimit *r, siphash24_init(&state, r->hash_key); string_hash_func(g->id, &state); - siphash24_finalize(&g->hash, &state); + g->hash = siphash24_finalize(&state); journal_rate_limit_vacuum(r, ts); @@ -230,7 +230,7 @@ int journal_rate_limit_test(JournalRateLimit *r, const char *id, int priority, u siphash24_init(&state, r->hash_key); string_hash_func(id, &state); - siphash24_finalize(&h, &state); + h = siphash24_finalize(&state); g = r->buckets[h % BUCKETS_MAX]; LIST_FOREACH(bucket, g, g) diff --git a/src/libsystemd-network/dhcp-identifier.c b/src/libsystemd-network/dhcp-identifier.c index 368525c..d7ae865 100644 --- a/src/libsystemd-network/dhcp-identifier.c +++ b/src/libsystemd-network/dhcp-identifier.c @@ -52,7 +52,7 @@ int dhcp_identifier_set_duid_en(struct duid *duid, size_t *len) { /* a bit of snake-oil perhaps, but no need to expose the machine-id directly; duid->en.id might not be aligned, so we need to copy */ - siphash24(&hash, &machine_id, sizeof(machine_id), HASH_KEY.bytes); + hash = htole64(siphash24(&machine_id, sizeof(machine_id), HASH_KEY.bytes)); memcpy(duid->en.id, &hash, sizeof(duid->en.id)); return 0; @@ -86,10 +86,12 @@ int dhcp_identifier_set_iaid(int ifindex, uint8_t *mac, size_t mac_len, void *_i } if (name) - siphash24(&id, name, strlen(name), HASH_KEY.bytes); + id = siphash24(name, strlen(name), HASH_KEY.bytes); else /* fall back to MAC address if no predictable name available */ - siphash24(&id, mac, mac_len, HASH_KEY.bytes); + id = siphash24(mac, mac_len, HASH_KEY.bytes); + + id = htole64(id); /* fold into 32 bits */ unaligned_write_be32(_id, (id & 0xffffffff) ^ (id >> 32)); diff --git a/src/libsystemd-network/network-internal.c b/src/libsystemd-network/network-internal.c index ab20b60..6aec3da 100644 --- a/src/libsystemd-network/network-internal.c +++ b/src/libsystemd-network/network-internal.c @@ -81,7 +81,7 @@ int net_get_unique_predictable_data(struct udev_device *device, uint64_t *result /* Let's hash the machine ID plus the device name. We * use a fixed, but originally randomly created hash * key here. */ - siphash24(result, v, sz, HASH_KEY.bytes); + *result = htole64(siphash24(v, sz, HASH_KEY.bytes)); return 0; } diff --git a/src/libsystemd-network/sd-dhcp-server.c b/src/libsystemd-network/sd-dhcp-server.c index eeb59bb..277c88e 100644 --- a/src/libsystemd-network/sd-dhcp-server.c +++ b/src/libsystemd-network/sd-dhcp-server.c @@ -753,7 +753,7 @@ int dhcp_server_handle_message(sd_dhcp_server *server, DHCPMessage *message, siphash24_init(&state, HASH_KEY.bytes); client_id_hash_func(&req->client_id, &state); - siphash24_finalize(&hash, &state); + hash = htole64(siphash24_finalize(&state)); next_offer = hash % server->pool_size; for (i = 0; i < server->pool_size; i++) { diff --git a/src/libsystemd-network/sd-ipv4ll.c b/src/libsystemd-network/sd-ipv4ll.c index e69b186..006db6f 100644 --- a/src/libsystemd-network/sd-ipv4ll.c +++ b/src/libsystemd-network/sd-ipv4ll.c @@ -146,12 +146,11 @@ int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr) { uint64_t seed; /* If no random data is set, generate some from the MAC */ - siphash24(&seed, &addr->ether_addr_octet, - ETH_ALEN, HASH_KEY.bytes); + seed = siphash24(&addr->ether_addr_octet, ETH_ALEN, HASH_KEY.bytes); assert_cc(sizeof(unsigned) <= 8); - r = sd_ipv4ll_set_address_seed(ll, (unsigned)seed); + r = sd_ipv4ll_set_address_seed(ll, (unsigned) htole64(seed)); if (r < 0) return r; } diff --git a/src/libsystemd-network/test-dhcp-server.c b/src/libsystemd-network/test-dhcp-server.c index 62fdec4..2b5f59e 100644 --- a/src/libsystemd-network/test-dhcp-server.c +++ b/src/libsystemd-network/test-dhcp-server.c @@ -200,13 +200,11 @@ 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; siphash24_init(&state, key); client_id_hash_func(id, &state); - siphash24_finalize(&hash, &state); - return hash; + return htole64(siphash24_finalize(&state)); } static void test_client_id_hash(void) { diff --git a/src/libsystemd/sd-bus/bus-bloom.c b/src/libsystemd/sd-bus/bus-bloom.c index a592ff5..5b91a71 100644 --- a/src/libsystemd/sd-bus/bus-bloom.c +++ b/src/libsystemd/sd-bus/bus-bloom.c @@ -72,7 +72,7 @@ static void bloom_add_data( for (d = 0; d < w; d++) { if (c <= 0) { - siphash24(&h, data, n, hash_keys[hash_index++].bytes); + h = siphash24(data, n, hash_keys[hash_index++].bytes); c += 8; } diff --git a/src/network/networkd-netdev.c b/src/network/networkd-netdev.c index 081e299..a86a638 100644 --- a/src/network/networkd-netdev.c +++ b/src/network/networkd-netdev.c @@ -438,7 +438,7 @@ int netdev_get_mac(const char *ifname, struct ether_addr **ret) { /* Let's hash the host machine ID plus the container name. We * use a fixed, but originally randomly created hash key here. */ - siphash24(&result, v, sz, HASH_KEY.bytes); + result = siphash24(v, sz, HASH_KEY.bytes); assert_cc(ETH_ALEN <= sizeof(result)); memcpy(mac->ether_addr_octet, &result, ETH_ALEN); diff --git a/src/nspawn/nspawn-network.c b/src/nspawn/nspawn-network.c index 92dfb94..9553192 100644 --- a/src/nspawn/nspawn-network.c +++ b/src/nspawn/nspawn-network.c @@ -74,7 +74,7 @@ static int generate_mac( /* Let's hash the host machine ID plus the container name. We * use a fixed, but originally randomly created hash key here. */ - siphash24(&result, v, sz, hash_key.bytes); + result = htole64(siphash24(v, sz, hash_key.bytes)); assert_cc(ETH_ALEN <= sizeof(result)); memcpy(mac->ether_addr_octet, &result, ETH_ALEN); diff --git a/src/test/test-siphash24.c b/src/test/test-siphash24.c index 0200e14..eda286d 100644 --- a/src/test/test-siphash24.c +++ b/src/test/test-siphash24.c @@ -29,7 +29,7 @@ static int do_test(const uint8_t *in, size_t len, const uint8_t *key) { uint64_t out = 0; unsigned i, j; - siphash24(&out, in, len, key); + out = siphash24(in, len, key); assert_se(out == htole64(0xa129ca6149be45e5)); /* verify the internal state as given in the above paper */ @@ -43,7 +43,7 @@ static int do_test(const uint8_t *in, size_t len, const uint8_t *key) { assert_se(state.v1 == 0x0d52f6f62a4f59a4); assert_se(state.v2 == 0x634cb3577b01fd3d); assert_se(state.v3 == 0xa5224d6f55c7d9c8); - siphash24_finalize(&out, &state); + out = siphash24_finalize(&state); assert_se(out == htole64(0xa129ca6149be45e5)); assert_se(state.v0 == 0xf6bcd53893fecff1); assert_se(state.v1 == 0x54b9964c7ea0d937); @@ -58,7 +58,7 @@ static int do_test(const uint8_t *in, size_t len, const uint8_t *key) { siphash24_compress(in, i, &state); siphash24_compress(&in[i], j - i, &state); siphash24_compress(&in[j], len - j, &state); - siphash24_finalize(&out, &state); + out = siphash24_finalize(&state); assert_se(out == htole64(0xa129ca6149be45e5)); } } -- 2.7.4