siphash_test.cc
Go to the documentation of this file.
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 
24 TEST(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 
44 TEST(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 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
Bytes
Definition: boringssl-with-bazel/src/crypto/test/test_util.h:38
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
words
std::vector< std::string > words
Definition: bloaty/third_party/protobuf/src/google/protobuf/repeated_field_unittest.cc:1787
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
TEST
TEST(SipHash, Basic)
Definition: siphash_test.cc:24
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
hash
uint64_t hash
Definition: ring_hash.cc:284
FileTestGTest
void FileTestGTest(const char *path, std::function< void(FileTest *)> run_test)
Definition: file_test_gtest.cc:68
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
siphash.h
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
FileTest
Definition: file_test.h:90
UINT64_C
#define UINT64_C(val)
Definition: stdint-msvc2008.h:238
stdint.h
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
key
const char * key
Definition: hpack_parser_table.cc:164
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
SIPHASH_24
#define SIPHASH_24
Definition: boringssl_prefix_symbols.h:2173
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:13