]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
siphash24: unify API
authorTom Gundersen <teg@jklm.no>
Tue, 6 Oct 2015 13:04:42 +0000 (15:04 +0200)
committerTom Gundersen <teg@jklm.no>
Tue, 6 Oct 2015 15:47:00 +0000 (17:47 +0200)
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.

src/basic/hashmap.c
src/basic/siphash24.c
src/basic/siphash24.h
src/journal/journald-rate-limit.c
src/libsystemd-network/sd-dhcp-server.c
src/libsystemd-network/test-dhcp-server.c
src/test/test-siphash24.c

index 3e17ed30df3e1c11c45d80ae0b80fb5be9fd84d0..20e7e51d9e223deebe7fe9e39a22f37c5abeba06 100644 (file)
@@ -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)
 
index 308e4230c5456257572df1821e153fa849370a66..9c56b5e9eafc8d39fd7f488fe0add435d152ad72 100644 (file)
@@ -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);
 }
index c107bdd2137aedf7794d35445460831b1cd51cc0..6c5cd98ee82a83ee12c992926580d1af26f8825c 100644 (file)
@@ -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]);
index 7e06117b31de3b5fba94b8cabc8b8ab01b2d03f8..8afd493b50f6d60917515568949ea54082fe2a58 100644 (file)
@@ -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)
index d941e6c0de76bb3e93e7166b2ce2585dd5c32a97..d27bb561cacbf01333b44f296de95d20ed72fed5 100644 (file)
@@ -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]) {
index 01205efc1877bd7f008141799f0c2444e76acde4..c3bcb9cb4b0aff39fadee1bbc0131a553d36fe9a 100644 (file)
@@ -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) {
index 65eb2b6f35eaa657ef2169ec4e006399d2d4fb88..2402da6a6f25c440e32d915ea097caf4b30eac55 100644 (file)
@@ -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));
                 }
         }
 }