]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-siphash24.c
Merge pull request #7530 from poettering/uid-gid-fixes
[thirdparty/systemd.git] / src / test / test-siphash24.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
a245d90a
TG
2/***
3 This file is part of systemd.
4
5 Copyright 2015 Tom Gundersen
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
a245d90a 21#include "siphash24.h"
cf0fbc49 22#include "util.h"
a245d90a
TG
23
24#define ITERATIONS 10000000ULL
25
56e3d0cf 26static void do_test(const uint8_t *in, size_t len, const uint8_t *key) {
1283d704 27 struct siphash state = {};
56e3d0cf 28 uint64_t out;
0cb3c286 29 unsigned i, j;
a245d90a 30
933f9cae 31 out = siphash24(in, len, key);
81d313a6 32 assert_se(out == 0xa129ca6149be45e5);
1283d704
TG
33
34 /* verify the internal state as given in the above paper */
0cb3c286 35 siphash24_init(&state, key);
1283d704
TG
36 assert_se(state.v0 == 0x7469686173716475);
37 assert_se(state.v1 == 0x6b617f6d656e6665);
38 assert_se(state.v2 == 0x6b7f62616d677361);
39 assert_se(state.v3 == 0x7b6b696e727e6c7b);
69b98e71 40 siphash24_compress(in, len, &state);
1283d704
TG
41 assert_se(state.v0 == 0x4a017198de0a59e0);
42 assert_se(state.v1 == 0x0d52f6f62a4f59a4);
43 assert_se(state.v2 == 0x634cb3577b01fd3d);
44 assert_se(state.v3 == 0xa5224d6f55c7d9c8);
933f9cae 45 out = siphash24_finalize(&state);
81d313a6 46 assert_se(out == 0xa129ca6149be45e5);
1283d704
TG
47 assert_se(state.v0 == 0xf6bcd53893fecff1);
48 assert_se(state.v1 == 0x54b9964c7ea0d937);
49 assert_se(state.v2 == 0x1b38329c099bb55a);
50 assert_se(state.v3 == 0x1814bb89ad7be679);
51
52 /* verify that decomposing the input in three chunks gives the
53 same result */
69b98e71
MP
54 for (i = 0; i < len; i++) {
55 for (j = i; j < len; j++) {
0cb3c286 56 siphash24_init(&state, key);
1283d704
TG
57 siphash24_compress(in, i, &state);
58 siphash24_compress(&in[i], j - i, &state);
69b98e71 59 siphash24_compress(&in[j], len - j, &state);
933f9cae 60 out = siphash24_finalize(&state);
81d313a6 61 assert_se(out == 0xa129ca6149be45e5);
1283d704
TG
62 }
63 }
56e3d0cf
ZJS
64}
65
66static void test_short_hashes(void) {
67 const uint8_t one[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
68 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
69 const uint8_t key[16] = { 0x22, 0x24, 0x41, 0x22, 0x55, 0x77, 0x88, 0x07,
70 0x23, 0x09, 0x23, 0x14, 0x0c, 0x33, 0x0e, 0x0f};
71 uint8_t two[sizeof one] = {};
72
73 struct siphash state1 = {}, state2 = {};
74 unsigned i, j;
75
76 siphash24_init(&state1, key);
77 siphash24_init(&state2, key);
78
79 /* hashing 1, 2, 3, 4, 5, ..., 16 bytes, with the byte after the buffer different */
80 for (i = 1; i <= sizeof one; i++) {
81 siphash24_compress(one, i, &state1);
82
83 two[i-1] = one[i-1];
84 siphash24_compress(two, i, &state2);
85
86 assert_se(memcmp(&state1, &state2, sizeof state1) == 0);
87 }
88
89 /* hashing n and 1, n and 2, n and 3, ..., n-1 and 1, n-2 and 2, ... */
90 for (i = sizeof one; i > 0; i--) {
91 zero(two);
92
93 for (j = 1; j <= sizeof one; j++) {
94 siphash24_compress(one, i, &state1);
95 siphash24_compress(one, j, &state1);
96
97 siphash24_compress(one, i, &state2);
98 two[j-1] = one[j-1];
99 siphash24_compress(two, j, &state2);
100
101 assert_se(memcmp(&state1, &state2, sizeof state1) == 0);
102 }
103 }
69b98e71
MP
104}
105
106/* see https://131002.net/siphash/siphash.pdf, Appendix A */
107int main(int argc, char *argv[]) {
108 const uint8_t in[15] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
109 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e };
110 const uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
111 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
112 uint8_t in_buf[20];
113
114 /* Test with same input but different alignments. */
115 memcpy(in_buf, in, sizeof(in));
116 do_test(in_buf, sizeof(in), key);
117 memcpy(in_buf + 1, in, sizeof(in));
118 do_test(in_buf + 1, sizeof(in), key);
119 memcpy(in_buf + 2, in, sizeof(in));
120 do_test(in_buf + 2, sizeof(in), key);
121 memcpy(in_buf + 4, in, sizeof(in));
122 do_test(in_buf + 4, sizeof(in), key);
56e3d0cf
ZJS
123
124 test_short_hashes();
a245d90a 125}