]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include "memory-util.h" | |
4 | #include "siphash24.h" | |
5 | #include "tests.h" | |
6 | ||
7 | #define ITERATIONS 10000000ULL | |
8 | ||
9 | static void test_alignment_one(const uint8_t *in, size_t len, const uint8_t *key) { | |
10 | struct siphash state = {}; | |
11 | uint64_t out; | |
12 | unsigned i, j; | |
13 | ||
14 | out = siphash24(in, len, key); | |
15 | assert_se(out == 0xa129ca6149be45e5); | |
16 | ||
17 | /* verify the internal state as given in the above paper */ | |
18 | siphash24_init(&state, key); | |
19 | assert_se(state.v0 == 0x7469686173716475); | |
20 | assert_se(state.v1 == 0x6b617f6d656e6665); | |
21 | assert_se(state.v2 == 0x6b7f62616d677361); | |
22 | assert_se(state.v3 == 0x7b6b696e727e6c7b); | |
23 | siphash24_compress(in, len, &state); | |
24 | assert_se(state.v0 == 0x4a017198de0a59e0); | |
25 | assert_se(state.v1 == 0x0d52f6f62a4f59a4); | |
26 | assert_se(state.v2 == 0x634cb3577b01fd3d); | |
27 | assert_se(state.v3 == 0xa5224d6f55c7d9c8); | |
28 | out = siphash24_finalize(&state); | |
29 | assert_se(out == 0xa129ca6149be45e5); | |
30 | assert_se(state.v0 == 0xf6bcd53893fecff1); | |
31 | assert_se(state.v1 == 0x54b9964c7ea0d937); | |
32 | assert_se(state.v2 == 0x1b38329c099bb55a); | |
33 | assert_se(state.v3 == 0x1814bb89ad7be679); | |
34 | ||
35 | /* verify that decomposing the input in three chunks gives the | |
36 | same result */ | |
37 | for (i = 0; i < len; i++) { | |
38 | for (j = i; j < len; j++) { | |
39 | siphash24_init(&state, key); | |
40 | siphash24_compress(in, i, &state); | |
41 | siphash24_compress(&in[i], j - i, &state); | |
42 | siphash24_compress(&in[j], len - j, &state); | |
43 | out = siphash24_finalize(&state); | |
44 | assert_se(out == 0xa129ca6149be45e5); | |
45 | } | |
46 | } | |
47 | } | |
48 | ||
49 | TEST(alignment) { | |
50 | const uint8_t in[15] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
51 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e }; | |
52 | const uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
53 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; | |
54 | uint8_t in_buf[20]; | |
55 | ||
56 | /* Test with same input but different alignments. */ | |
57 | memcpy(in_buf, in, sizeof(in)); | |
58 | test_alignment_one(in_buf, sizeof(in), key); | |
59 | memcpy(in_buf + 1, in, sizeof(in)); | |
60 | test_alignment_one(in_buf + 1, sizeof(in), key); | |
61 | memcpy(in_buf + 2, in, sizeof(in)); | |
62 | test_alignment_one(in_buf + 2, sizeof(in), key); | |
63 | memcpy(in_buf + 4, in, sizeof(in)); | |
64 | test_alignment_one(in_buf + 4, sizeof(in), key); | |
65 | } | |
66 | ||
67 | TEST(short_hashes) { | |
68 | const uint8_t one[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, | |
69 | 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; | |
70 | const uint8_t key[16] = { 0x22, 0x24, 0x41, 0x22, 0x55, 0x77, 0x88, 0x07, | |
71 | 0x23, 0x09, 0x23, 0x14, 0x0c, 0x33, 0x0e, 0x0f}; | |
72 | uint8_t two[sizeof one] = {}; | |
73 | ||
74 | struct siphash state1 = {}, state2 = {}; | |
75 | unsigned i, j; | |
76 | ||
77 | siphash24_init(&state1, key); | |
78 | siphash24_init(&state2, key); | |
79 | ||
80 | /* hashing 1, 2, 3, 4, 5, ..., 16 bytes, with the byte after the buffer different */ | |
81 | for (i = 1; i <= sizeof one; i++) { | |
82 | siphash24_compress(one, i, &state1); | |
83 | ||
84 | two[i-1] = one[i-1]; | |
85 | siphash24_compress(two, i, &state2); | |
86 | ||
87 | assert_se(memcmp(&state1, &state2, sizeof state1) == 0); | |
88 | } | |
89 | ||
90 | /* hashing n and 1, n and 2, n and 3, ..., n-1 and 1, n-2 and 2, ... */ | |
91 | for (i = sizeof one; i > 0; i--) { | |
92 | zero(two); | |
93 | ||
94 | for (j = 1; j <= sizeof one; j++) { | |
95 | siphash24_compress(one, i, &state1); | |
96 | siphash24_compress(one, j, &state1); | |
97 | ||
98 | siphash24_compress(one, i, &state2); | |
99 | two[j-1] = one[j-1]; | |
100 | siphash24_compress(two, j, &state2); | |
101 | ||
102 | assert_se(memcmp(&state1, &state2, sizeof state1) == 0); | |
103 | } | |
104 | } | |
105 | } | |
106 | ||
107 | /* see https://131002.net/siphash/siphash.pdf, Appendix A */ | |
108 | DEFINE_TEST_MAIN(LOG_INFO); |