]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-siphash24.c
Merge pull request #1527 from keszybz/lz4
[thirdparty/systemd.git] / src / test / test-siphash24.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2015 Tom Gundersen
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "util.h"
23 #include "siphash24.h"
24
25 #define ITERATIONS 10000000ULL
26
27 /* see https://131002.net/siphash/siphash.pdf, Appendix A */
28 int main(int argc, char *argv[]) {
29 struct siphash state = {};
30 const uint8_t in[15] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
31 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e };
32 const uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
33 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f};
34 uint64_t out = 0;
35 unsigned i, j;
36
37 siphash24((uint8_t *)&out, in, sizeof(in), key);
38 assert_se(out == htole64(0xa129ca6149be45e5));
39
40 /* verify the internal state as given in the above paper */
41 siphash24_init(&state, key);
42 assert_se(state.v0 == 0x7469686173716475);
43 assert_se(state.v1 == 0x6b617f6d656e6665);
44 assert_se(state.v2 == 0x6b7f62616d677361);
45 assert_se(state.v3 == 0x7b6b696e727e6c7b);
46 siphash24_compress(in, sizeof(in), &state);
47 assert_se(state.v0 == 0x4a017198de0a59e0);
48 assert_se(state.v1 == 0x0d52f6f62a4f59a4);
49 assert_se(state.v2 == 0x634cb3577b01fd3d);
50 assert_se(state.v3 == 0xa5224d6f55c7d9c8);
51 siphash24_finalize((uint8_t*)&out, &state);
52 assert_se(out == htole64(0xa129ca6149be45e5));
53 assert_se(state.v0 == 0xf6bcd53893fecff1);
54 assert_se(state.v1 == 0x54b9964c7ea0d937);
55 assert_se(state.v2 == 0x1b38329c099bb55a);
56 assert_se(state.v3 == 0x1814bb89ad7be679);
57
58 /* verify that decomposing the input in three chunks gives the
59 same result */
60 for (i = 0; i < sizeof(in); i++) {
61 for (j = i; j < sizeof(in); j++) {
62 siphash24_init(&state, key);
63 siphash24_compress(in, i, &state);
64 siphash24_compress(&in[i], j - i, &state);
65 siphash24_compress(&in[j], sizeof(in) - j, &state);
66 siphash24_finalize((uint8_t*)&out, &state);
67 assert_se(out == htole64(0xa129ca6149be45e5));
68 }
69 }
70 }