Skip to content

Commit 1a79f74

Browse files
committed
switch SipHash from SipHash24 to SipHash13 variant
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
1 parent 761653d commit 1a79f74

File tree

3 files changed

+8
-10
lines changed

3 files changed

+8
-10
lines changed

random.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,7 +1457,7 @@ random_s_rand(int argc, VALUE *argv, VALUE obj)
14571457
}
14581458

14591459
#define SIP_HASH_STREAMING 0
1460-
#define sip_hash24 ruby_sip_hash24
1460+
#define sip_hash13 ruby_sip_hash13
14611461
#if !defined _WIN32 && !defined BYTE_ORDER
14621462
# ifdef WORDS_BIGENDIAN
14631463
# define BYTE_ORDER BIG_ENDIAN
@@ -1501,7 +1501,7 @@ rb_hash_start(st_index_t h)
15011501
st_index_t
15021502
rb_memhash(const void *ptr, long len)
15031503
{
1504-
sip_uint64_t h = sip_hash24(seed.key.sip, ptr, len);
1504+
sip_uint64_t h = sip_hash13(seed.key.sip, ptr, len);
15051505
#ifdef HAVE_UINT64_T
15061506
return (st_index_t)h;
15071507
#else

siphash.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -386,16 +386,15 @@ sip_hash_dump(sip_hash *h)
386386
}
387387
#endif /* SIP_HASH_STREAMING */
388388

389-
#define SIP_2_ROUND(m, v0, v1, v2, v3) \
389+
#define SIP_ROUND(m, v0, v1, v2, v3) \
390390
do { \
391391
XOR64_TO((v3), (m)); \
392392
SIP_COMPRESS(v0, v1, v2, v3); \
393-
SIP_COMPRESS(v0, v1, v2, v3); \
394393
XOR64_TO((v0), (m)); \
395394
} while (0)
396395

397396
uint64_t
398-
sip_hash24(const uint8_t key[16], const uint8_t *data, size_t len)
397+
sip_hash13(const uint8_t key[16], const uint8_t *data, size_t len)
399398
{
400399
uint64_t k0, k1;
401400
uint64_t v0, v1, v2, v3;
@@ -415,13 +414,13 @@ sip_hash24(const uint8_t key[16], const uint8_t *data, size_t len)
415414
uint64_t *data64 = (uint64_t *)data;
416415
while (data64 != (uint64_t *) end) {
417416
m = *data64++;
418-
SIP_2_ROUND(m, v0, v1, v2, v3);
417+
SIP_ROUND(m, v0, v1, v2, v3);
419418
}
420419
}
421420
#else
422421
for (; data != end; data += sizeof(uint64_t)) {
423422
m = U8TO64_LE(data);
424-
SIP_2_ROUND(m, v0, v1, v2, v3);
423+
SIP_ROUND(m, v0, v1, v2, v3);
425424
}
426425
#endif
427426

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

471-
SIP_2_ROUND(last, v0, v1, v2, v3);
470+
SIP_ROUND(last, v0, v1, v2, v3);
472471

473472
XOR64_INT(v2, 0xff);
474473

475474
SIP_COMPRESS(v0, v1, v2, v3);
476475
SIP_COMPRESS(v0, v1, v2, v3);
477476
SIP_COMPRESS(v0, v1, v2, v3);
478-
SIP_COMPRESS(v0, v1, v2, v3);
479477

480478
XOR64_TO(v0, v1);
481479
XOR64_TO(v0, v2);

siphash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ int sip_hash_digest_integer(sip_hash *h, const uint8_t *data, size_t data_len, u
4343
void sip_hash_free(sip_hash *h);
4444
void sip_hash_dump(sip_hash *h);
4545

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

4848
#endif

0 commit comments

Comments
 (0)