siphash24: change result argument to uint64_t

Change the "out" parameter from uint8_t[8] to uint64_t. On architectures which
enforce pointer alignment this fixes crashes when we previously cast an
unaligned array to uint64_t*, and on others this should at least improve
performance as the compiler now aligns these properly.

This also simplifies the code in most cases by getting rid of typecasts. The
only place which we can't change is struct duid's en.id, as that is _packed_
and public API, so we can't enforce alignment of the "id" field and have to
use memcpy instead.
This commit is contained in:
Martin Pitt
2015-11-16 15:20:29 +01:00
committed by Daniel Mack
parent 8dd85afe76
commit dbe81cbd2a
17 changed files with 39 additions and 37 deletions
+6 -4
View File
@@ -35,6 +35,7 @@
int dhcp_identifier_set_duid_en(struct duid *duid, size_t *len) {
sd_id128_t machine_id;
uint64_t hash;
int r;
assert(duid);
@@ -50,8 +51,9 @@ int dhcp_identifier_set_duid_en(struct duid *duid, size_t *len) {
*len = sizeof(duid->type) + sizeof(duid->en);
/* a bit of snake-oil perhaps, but no need to expose the machine-id
directly */
siphash24(duid->en.id, &machine_id, sizeof(machine_id), HASH_KEY.bytes);
directly; duid->en.id might not be aligned, so we need to copy */
siphash24(&hash, &machine_id, sizeof(machine_id), HASH_KEY.bytes);
memcpy(duid->en.id, &hash, sizeof(duid->en.id));
return 0;
}
@@ -84,10 +86,10 @@ int dhcp_identifier_set_iaid(int ifindex, uint8_t *mac, size_t mac_len, void *_i
}
if (name)
siphash24((uint8_t*)&id, name, strlen(name), HASH_KEY.bytes);
siphash24(&id, name, strlen(name), HASH_KEY.bytes);
else
/* fall back to MAC address if no predictable name available */
siphash24((uint8_t*)&id, mac, mac_len, HASH_KEY.bytes);
siphash24(&id, mac, mac_len, HASH_KEY.bytes);
/* fold into 32 bits */
unaligned_write_be32(_id, (id & 0xffffffff) ^ (id >> 32));
+1 -1
View File
@@ -56,7 +56,7 @@ const char *net_get_name(struct udev_device *device) {
#define HASH_KEY SD_ID128_MAKE(d3,1e,48,fa,90,fe,4b,4c,9d,af,d5,d7,a1,b1,2e,8a)
int net_get_unique_predictable_data(struct udev_device *device, uint8_t result[8]) {
int net_get_unique_predictable_data(struct udev_device *device, uint64_t *result) {
size_t l, sz = 0;
const char *name = NULL;
int r;
+1 -1
View File
@@ -62,7 +62,7 @@ int config_parse_ifalias(const char *unit, const char *filename, unsigned line,
const char *section, unsigned section_line, const char *lvalue,
int ltype, const char *rvalue, void *data, void *userdata);
int net_get_unique_predictable_data(struct udev_device *device, uint8_t result[8]);
int net_get_unique_predictable_data(struct udev_device *device, uint64_t *result);
const char *net_get_name(struct udev_device *device);
void serialize_in_addrs(FILE *f, const struct in_addr *addresses, size_t size);
+1 -1
View 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((uint8_t*)&hash, &state);
siphash24_finalize(&hash, &state);
next_offer = hash % server->pool_size;
for (i = 0; i < server->pool_size; i++) {
+3 -3
View File
@@ -143,15 +143,15 @@ int sd_ipv4ll_set_mac(sd_ipv4ll *ll, const struct ether_addr *addr) {
assert_return(ll, -EINVAL);
if (!ll->random_data) {
uint8_t seed[8];
uint64_t seed;
/* If no random data is set, generate some from the MAC */
siphash24(seed, &addr->ether_addr_octet,
siphash24(&seed, &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)seed);
if (r < 0)
return r;
}
+1 -1
View File
@@ -204,7 +204,7 @@ static uint64_t client_id_hash_helper(DHCPClientId *id, uint8_t key[HASH_KEY_SIZ
siphash24_init(&state, key);
client_id_hash_func(id, &state);
siphash24_finalize((uint8_t*)&hash, &state);
siphash24_finalize(&hash, &state);
return hash;
}