]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
siphash24: let siphash24_finalize() and siphash24() return the result directly 1923/head
authorDaniel Mack <daniel@zonque.org>
Mon, 16 Nov 2015 22:17:52 +0000 (23:17 +0100)
committerDaniel Mack <daniel@zonque.org>
Mon, 16 Nov 2015 22:17:52 +0000 (23:17 +0100)
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.

14 files changed:
src/basic/hashmap.c
src/basic/siphash24.c
src/basic/siphash24.h
src/import/pull-common.c
src/journal/journald-rate-limit.c
src/libsystemd-network/dhcp-identifier.c
src/libsystemd-network/network-internal.c
src/libsystemd-network/sd-dhcp-server.c
src/libsystemd-network/sd-ipv4ll.c
src/libsystemd-network/test-dhcp-server.c
src/libsystemd/sd-bus/bus-bloom.c
src/network/networkd-netdev.c
src/nspawn/nspawn-network.c
src/test/test-siphash24.c

index d88ceb40aa93be93b55f48aaa1ec6f45755f8947..6e501ef6ff43185145c22585f7a3ebbfd6fe6ce4 100644 (file)
@@ -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));
 }
index d7640d395ded78e0827bfdd79da180ed3a1f6fed..b6b6172f60172d45c0fe036a0673919b0c7be211 100644 (file)
@@ -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);
 }
index dc08077d535ccefca2c7c31e67921de516bf5a73..0e072eba364a5f5785da44ad086786931582f7c9 100644 (file)
@@ -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]);
index 16b0c6160fbd1f8145b6df57ea1b45c1ceb5b8b6..a83cffffa0103d4bd9f41ce60d5387dd219625d7 100644 (file)
@@ -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;
 
index ad78d0929441f95f42493f1ba02766aa51f60ea7..2d46efe2fa8dba582a1c007f71465ad03a7ec5bc 100644 (file)
@@ -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)
index 368525c40ac2de3e6dd3bdcdb11ba65ccf0c8f43..d7ae8655573f7124d179b7457945a5412c279fb4 100644 (file)
@@ -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));
index ab20b6065a3b1b40b2237028c90ff19bcaf3114e..6aec3da4d05646525f695a83457b8ef9fad774d0 100644 (file)
@@ -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;
 }
index eeb59bb3dadce790e7f70a95409b8793dab3fb2d..277c88e2b98360f91ef28b9ec31acdfece3b6073 100644 (file)
@@ -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++) {
index e69b1864d7f7c6a11e269efd2171e9f23bf491ad..006db6feeeb10bd590efc4ae01213386e7135489 100644 (file)
@@ -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;
         }
index 62fdec46dab03e6bb5e9dfc9e6028d4143185ff1..2b5f59e4d66b528cae77665c9a7038477f96f5b5 100644 (file)
@@ -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) {
index a592ff50cfd28ea7523814f7c637a6b8c4c6b8f7..5b91a7175338f21676f9eccb3cc5ad8214616854 100644 (file)
@@ -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;
                         }
 
index 081e299d8267de78af4ce2bc540c752a6d2b491c..a86a6383dad37d81a475540cd0728fa28904931e 100644 (file)
@@ -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);
index 92dfb945e00f8d45b6bc96f59663c68f450dc56f..9553192051f30a307e3a4cfc9f3dd3cf1f71448f 100644 (file)
@@ -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);
index 0200e146ad8f5451a33eb171277584febd057371..eda286da0009489cf2d88a3928652a614effb503 100644 (file)
@@ -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));
                 }
         }