| 1 |
// Copyright (c) 2019, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <stdint.h> |
| 5 |
|
| 6 |
#include <gtest/gtest.h> |
| 7 |
|
| 8 |
#include <openssl/siphash.h> |
| 9 |
|
| 10 |
#include "../test/file_test.h" |
| 11 |
#include "../test/test_util.h" |
| 12 |
|
| 13 |
TEST(SipHash, Basic) { |
| 14 |
// This is the example from appendix A of the SipHash paper. |
| 15 |
uint8_t key_bytes[16]; |
| 16 |
for (unsigned i = 0; i < 16; i++) { |
| 17 |
key_bytes[i] = i; |
| 18 |
} |
| 19 |
uint64_t key[2]; |
| 20 |
memcpy(key, key_bytes, sizeof(key)); |
| 21 |
|
| 22 |
uint8_t input[15]; |
| 23 |
for (unsigned i = 0; i < sizeof(input); i++) { |
| 24 |
input[i] = i; |
| 25 |
} |
| 26 |
|
| 27 |
EXPECT_EQ(UINT64_C(0xa129ca6149be45e5), |
| 28 |
SIPHASH_24(key, input, sizeof(input))); |
| 29 |
} |
| 30 |
|
| 31 |
TEST(SipHash, Vectors) { |
| 32 |
FileTestGTest("crypto/siphash/siphash_tests.txt", [](FileTest *t) { |
| 33 |
std::vector<uint8_t> key, msg, hash_bytes; |
| 34 |
ASSERT_TRUE(t->GetBytes(&key, "KEY")); |
| 35 |
ASSERT_TRUE(t->GetBytes(&msg, "IN")); |
| 36 |
ASSERT_TRUE(t->GetBytes(&hash_bytes, "HASH")); |
| 37 |
ASSERT_EQ(16u, key.size()); |
| 38 |
ASSERT_EQ(8u, hash_bytes.size()); |
| 39 |
uint64_t hash = CRYPTO_load_u64_le(hash_bytes.data()); |
| 40 |
|
| 41 |
uint64_t key_words[2]; |
| 42 |
memcpy(key_words, key.data(), key.size()); |
| 43 |
uint64_t result = SIPHASH_24(key_words, msg.data(), msg.size()); |
| 44 |
EXPECT_EQ(result, hash); |
| 45 |
}); |
| 46 |
} |