| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <stdint.h> |
| 16 |
| 17 | #include <gtest/gtest.h> |
| 18 |
| 19 | #include <openssl/siphash.h> |
| 20 |
| 21 | #include "../test/file_test.h" |
| 22 | #include "../test/test_util.h" |
| 23 |
| 24 | TEST(SipHash, Basic) { |
| 25 | // This is the example from appendix A of the SipHash paper. |
| 26 | uint8_t key_bytes[16]; |
| 27 | for (unsigned i = 0; i < 16; i++) { |
| 28 | key_bytes[i] = i; |
| 29 | } |
| 30 | uint64_t key[2]; |
| 31 | memcpy(key, key_bytes, sizeof(key)); |
| 32 |
| 33 | uint8_t input[15]; |
| 34 | for (unsigned i = 0; i < sizeof(input); i++) { |
| 35 | input[i] = i; |
| 36 | } |
| 37 |
| 38 | EXPECT_EQ(UINT64_C(0xa129ca6149be45e5), |
| 39 | SIPHASH_24(key, input, sizeof(input))); |
| 40 | } |
| 41 |
| 42 | TEST(SipHash, Vectors) { |
| 43 | FileTestGTest("crypto/siphash/siphash_tests.txt", [](FileTest *t) { |
| 44 | std::vector<uint8_t> key, msg, hash; |
| 45 | ASSERT_TRUE(t->GetBytes(&key, "KEY")); |
| 46 | ASSERT_TRUE(t->GetBytes(&msg, "IN")); |
| 47 | ASSERT_TRUE(t->GetBytes(&hash, "HASH")); |
| 48 | ASSERT_EQ(16u, key.size()); |
| 49 | ASSERT_EQ(8u, hash.size()); |
| 50 |
| 51 | uint64_t key_words[2]; |
| 52 | memcpy(key_words, key.data(), key.size()); |
| 53 | uint64_t result = SIPHASH_24(key_words, msg.data(), msg.size()); |
| 54 | EXPECT_EQ(Bytes(reinterpret_cast<uint8_t *>(&result), sizeof(result)), |
| 55 | Bytes(hash)); |
| 56 | }); |
| 57 | } |
| 58 |