blob: 6d8f9e7a5a3ac628d2a8a7adc44fbc0b5ba623d9 [file] [log] [blame]
Pete Bentley0c61efe2019-08-13 09:32:23 +01001/* Copyright (c) 2019, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
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
24TEST(SipHash, Basic) {
25 // This is the example from appendix A of the SipHash paper.
26 union {
27 uint8_t bytes[16];
28 uint64_t words[2];
29 } key;
30
31 for (unsigned i = 0; i < 16; i++) {
32 key.bytes[i] = i;
33 }
34
35 uint8_t input[15];
36 for (unsigned i = 0; i < sizeof(input); i++) {
37 input[i] = i;
38 }
39
40 EXPECT_EQ(UINT64_C(0xa129ca6149be45e5),
41 SIPHASH_24(key.words, input, sizeof(input)));
42}
43
44TEST(SipHash, Vectors) {
45 FileTestGTest("crypto/siphash/siphash_tests.txt", [](FileTest *t) {
46 std::vector<uint8_t> key, msg, hash;
47 ASSERT_TRUE(t->GetBytes(&key, "KEY"));
48 ASSERT_TRUE(t->GetBytes(&msg, "IN"));
49 ASSERT_TRUE(t->GetBytes(&hash, "HASH"));
50 ASSERT_EQ(16u, key.size());
51 ASSERT_EQ(8u, hash.size());
52
53 uint64_t key_words[2];
54 memcpy(key_words, key.data(), key.size());
55 uint64_t result = SIPHASH_24(key_words, msg.data(), msg.size());
56 EXPECT_EQ(Bytes(reinterpret_cast<uint8_t *>(&result), sizeof(result)),
57 Bytes(hash));
58 });
59}