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.
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)
(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 );
}
}
-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);
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);
}
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]);
char *id;
JournalRateLimitPool pools[POOLS_MAX];
- unsigned long hash;
+ uint64_t hash;
LIST_FIELDS(JournalRateLimitGroup, bucket);
LIST_FIELDS(JournalRateLimitGroup, lru);
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);
}
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;
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)
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]) {
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) {
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);
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);
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));
}
}
}