xref: /aosp_15_r20/external/boringssl/src/crypto/siphash/siphash_test.cc (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* 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 
TEST(SipHash,Basic)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 
TEST(SipHash,Vectors)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