Skip to content

Commit

Permalink
switch SipHash from SipHash24 to SipHash13 variant
Browse files Browse the repository at this point in the history
SipHash13 is secure enough to be used in hash-tables,
and SipHash's author confirms that.
Rust already considered switch to SipHash13:
  rust-lang/rust#29754 (comment)
Jean-Philippe Aumasson confirmation:
  rust-lang/rust#29754 (comment)
Merged pull request:
  rust-lang/rust#33940
  • Loading branch information
funny-falcon committed Dec 8, 2016
1 parent 761653d commit 1a79f74
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
4 changes: 2 additions & 2 deletions random.c
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,7 @@ random_s_rand(int argc, VALUE *argv, VALUE obj)
}

#define SIP_HASH_STREAMING 0
#define sip_hash24 ruby_sip_hash24
#define sip_hash13 ruby_sip_hash13
#if !defined _WIN32 && !defined BYTE_ORDER
# ifdef WORDS_BIGENDIAN
# define BYTE_ORDER BIG_ENDIAN
Expand Down Expand Up @@ -1501,7 +1501,7 @@ rb_hash_start(st_index_t h)
st_index_t
rb_memhash(const void *ptr, long len)
{
sip_uint64_t h = sip_hash24(seed.key.sip, ptr, len);
sip_uint64_t h = sip_hash13(seed.key.sip, ptr, len);
#ifdef HAVE_UINT64_T
return (st_index_t)h;
#else
Expand Down
12 changes: 5 additions & 7 deletions siphash.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,16 +386,15 @@ sip_hash_dump(sip_hash *h)
}
#endif /* SIP_HASH_STREAMING */

#define SIP_2_ROUND(m, v0, v1, v2, v3) \
#define SIP_ROUND(m, v0, v1, v2, v3) \
do { \
XOR64_TO((v3), (m)); \
SIP_COMPRESS(v0, v1, v2, v3); \
SIP_COMPRESS(v0, v1, v2, v3); \
XOR64_TO((v0), (m)); \
} while (0)

uint64_t
sip_hash24(const uint8_t key[16], const uint8_t *data, size_t len)
sip_hash13(const uint8_t key[16], const uint8_t *data, size_t len)
{
uint64_t k0, k1;
uint64_t v0, v1, v2, v3;
Expand All @@ -415,13 +414,13 @@ sip_hash24(const uint8_t key[16], const uint8_t *data, size_t len)
uint64_t *data64 = (uint64_t *)data;
while (data64 != (uint64_t *) end) {
m = *data64++;
SIP_2_ROUND(m, v0, v1, v2, v3);
SIP_ROUND(m, v0, v1, v2, v3);
}
}
#else
for (; data != end; data += sizeof(uint64_t)) {
m = U8TO64_LE(data);
SIP_2_ROUND(m, v0, v1, v2, v3);
SIP_ROUND(m, v0, v1, v2, v3);
}
#endif

Expand Down Expand Up @@ -468,14 +467,13 @@ sip_hash24(const uint8_t key[16], const uint8_t *data, size_t len)
break;
}

SIP_2_ROUND(last, v0, v1, v2, v3);
SIP_ROUND(last, v0, v1, v2, v3);

XOR64_INT(v2, 0xff);

SIP_COMPRESS(v0, v1, v2, v3);
SIP_COMPRESS(v0, v1, v2, v3);
SIP_COMPRESS(v0, v1, v2, v3);
SIP_COMPRESS(v0, v1, v2, v3);

XOR64_TO(v0, v1);
XOR64_TO(v0, v2);
Expand Down
2 changes: 1 addition & 1 deletion siphash.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ int sip_hash_digest_integer(sip_hash *h, const uint8_t *data, size_t data_len, u
void sip_hash_free(sip_hash *h);
void sip_hash_dump(sip_hash *h);

uint64_t sip_hash24(const uint8_t key[16], const uint8_t *data, size_t len);
uint64_t sip_hash13(const uint8_t key[16], const uint8_t *data, size_t len);

#endif

0 comments on commit 1a79f74

Please sign in to comment.