Have tor_addr hashes return a randomized hash for AF_UNSPEC.

We don't expect this to come up very much, but we may as well make
sure that the value isn't predictable (as we do for the other
addresses) in case the issue ever comes up.

Spotted by teor.
This commit is contained in:
Nick Mathewson 2018-02-12 11:08:33 -05:00 committed by David Goulet
parent 99fbbc6c47
commit 1555946e20

View File

@ -1181,6 +1181,9 @@ tor_addr_compare_masked(const tor_addr_t *addr1, const tor_addr_t *addr2,
} }
} }
/** Input for siphash, to produce some output for an unspec value. */
static const uint32_t unspec_hash_input[] = { 0x4e4df09f, 0x92985342 };
/** Return a hash code based on the address addr. DOCDOC extra */ /** Return a hash code based on the address addr. DOCDOC extra */
uint64_t uint64_t
tor_addr_hash(const tor_addr_t *addr) tor_addr_hash(const tor_addr_t *addr)
@ -1189,7 +1192,7 @@ tor_addr_hash(const tor_addr_t *addr)
case AF_INET: case AF_INET:
return siphash24g(&addr->addr.in_addr.s_addr, 4); return siphash24g(&addr->addr.in_addr.s_addr, 4);
case AF_UNSPEC: case AF_UNSPEC:
return 0x4e4d5342; return siphash24g(unspec_hash_input, sizeof(unspec_hash_input));
case AF_INET6: case AF_INET6:
return siphash24g(&addr->addr.in6_addr.s6_addr, 16); return siphash24g(&addr->addr.in6_addr.s6_addr, 16);
default: default:
@ -1211,7 +1214,7 @@ tor_addr_keyed_hash(const struct sipkey *key, const tor_addr_t *addr)
case AF_INET: case AF_INET:
return siphash24(&addr->addr.in_addr.s_addr, 4, key); return siphash24(&addr->addr.in_addr.s_addr, 4, key);
case AF_UNSPEC: case AF_UNSPEC:
return 0x4e4d5342; return siphash24(unspec_hash_input, sizeof(unspec_hash_input), key);
case AF_INET6: case AF_INET6:
return siphash24(&addr->addr.in6_addr.s6_addr, 16, key); return siphash24(&addr->addr.in6_addr.s6_addr, 16, key);
default: default: