mirror of
https://github.com/systemd/systemd.git
synced 2024-12-27 07:22:31 +03:00
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.
This commit is contained in:
parent
5cd6491b71
commit
933f9caeeb
@ -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));
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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]);
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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));
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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++) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user