Skip to content

Commit

Permalink
Fix SipHash for big-endian (aws#1251)
Browse files Browse the repository at this point in the history
  • Loading branch information
justsmth authored Oct 20, 2023
1 parent 642e71f commit dc888d5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
17 changes: 12 additions & 5 deletions crypto/siphash/siphash.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,18 @@ uint64_t SIPHASH_24(const uint64_t key[2], const uint8_t *input,
size_t input_len) {
const size_t orig_input_len = input_len;

uint64_t v[4];
v[0] = key[0] ^ UINT64_C(0x736f6d6570736575);
v[1] = key[1] ^ UINT64_C(0x646f72616e646f6d);
v[2] = key[0] ^ UINT64_C(0x6c7967656e657261);
v[3] = key[1] ^ UINT64_C(0x7465646279746573);
uint64_t v[4], k0, k1;
#ifdef OPENSSL_BIG_ENDIAN
k0 = CRYPTO_bswap8(key[0]);
k1 = CRYPTO_bswap8(key[1]);
#else
k0 = key[0];
k1 = key[1];
#endif
v[0] = k0 ^ UINT64_C(0x736f6d6570736575);
v[1] = k1 ^ UINT64_C(0x646f72616e646f6d);
v[2] = k0 ^ UINT64_C(0x6c7967656e657261);
v[3] = k1 ^ UINT64_C(0x7465646279746573);

while (input_len >= sizeof(uint64_t)) {
uint64_t m = CRYPTO_load_u64_le(input);
Expand Down
10 changes: 5 additions & 5 deletions crypto/siphash/siphash_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ TEST(SipHash, Basic) {

TEST(SipHash, Vectors) {
FileTestGTest("crypto/siphash/siphash_tests.txt", [](FileTest *t) {
std::vector<uint8_t> key, msg, hash;
std::vector<uint8_t> key, msg, hash_bytes;
ASSERT_TRUE(t->GetBytes(&key, "KEY"));
ASSERT_TRUE(t->GetBytes(&msg, "IN"));
ASSERT_TRUE(t->GetBytes(&hash, "HASH"));
ASSERT_TRUE(t->GetBytes(&hash_bytes, "HASH"));
ASSERT_EQ(16u, key.size());
ASSERT_EQ(8u, hash.size());
ASSERT_EQ(8u, hash_bytes.size());
uint64_t hash = CRYPTO_load_u64_le(hash_bytes.data());

uint64_t key_words[2];
memcpy(key_words, key.data(), key.size());
uint64_t result = SIPHASH_24(key_words, msg.data(), msg.size());
EXPECT_EQ(Bytes(reinterpret_cast<uint8_t *>(&result), sizeof(result)),
Bytes(hash));
EXPECT_EQ(result, hash);
});
}

0 comments on commit dc888d5

Please sign in to comment.