]>
Commit | Line | Data |
---|---|---|
a245d90a TG |
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 | ||
a245d90a | 22 | #include "siphash24.h" |
cf0fbc49 | 23 | #include "util.h" |
a245d90a TG |
24 | |
25 | #define ITERATIONS 10000000ULL | |
26 | ||
69b98e71 | 27 | static int do_test(const uint8_t *in, size_t len, const uint8_t *key) { |
1283d704 | 28 | struct siphash state = {}; |
a245d90a | 29 | uint64_t out = 0; |
0cb3c286 | 30 | unsigned i, j; |
a245d90a | 31 | |
933f9cae | 32 | out = siphash24(in, len, key); |
0cb3c286 | 33 | assert_se(out == htole64(0xa129ca6149be45e5)); |
1283d704 TG |
34 | |
35 | /* verify the internal state as given in the above paper */ | |
0cb3c286 | 36 | siphash24_init(&state, key); |
1283d704 TG |
37 | assert_se(state.v0 == 0x7469686173716475); |
38 | assert_se(state.v1 == 0x6b617f6d656e6665); | |
39 | assert_se(state.v2 == 0x6b7f62616d677361); | |
40 | assert_se(state.v3 == 0x7b6b696e727e6c7b); | |
69b98e71 | 41 | siphash24_compress(in, len, &state); |
1283d704 TG |
42 | assert_se(state.v0 == 0x4a017198de0a59e0); |
43 | assert_se(state.v1 == 0x0d52f6f62a4f59a4); | |
44 | assert_se(state.v2 == 0x634cb3577b01fd3d); | |
45 | assert_se(state.v3 == 0xa5224d6f55c7d9c8); | |
933f9cae | 46 | out = siphash24_finalize(&state); |
0cb3c286 | 47 | assert_se(out == htole64(0xa129ca6149be45e5)); |
1283d704 TG |
48 | assert_se(state.v0 == 0xf6bcd53893fecff1); |
49 | assert_se(state.v1 == 0x54b9964c7ea0d937); | |
50 | assert_se(state.v2 == 0x1b38329c099bb55a); | |
51 | assert_se(state.v3 == 0x1814bb89ad7be679); | |
52 | ||
53 | /* verify that decomposing the input in three chunks gives the | |
54 | same result */ | |
69b98e71 MP |
55 | for (i = 0; i < len; i++) { |
56 | for (j = i; j < len; j++) { | |
0cb3c286 | 57 | siphash24_init(&state, key); |
1283d704 TG |
58 | siphash24_compress(in, i, &state); |
59 | siphash24_compress(&in[i], j - i, &state); | |
69b98e71 | 60 | siphash24_compress(&in[j], len - j, &state); |
933f9cae | 61 | out = siphash24_finalize(&state); |
0cb3c286 | 62 | assert_se(out == htole64(0xa129ca6149be45e5)); |
1283d704 TG |
63 | } |
64 | } | |
69b98e71 MP |
65 | return 0; |
66 | } | |
67 | ||
68 | /* see https://131002.net/siphash/siphash.pdf, Appendix A */ | |
69 | int main(int argc, char *argv[]) { | |
70 | const uint8_t in[15] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
71 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e }; | |
72 | const uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
73 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; | |
74 | uint8_t in_buf[20]; | |
75 | ||
76 | /* Test with same input but different alignments. */ | |
77 | memcpy(in_buf, in, sizeof(in)); | |
78 | do_test(in_buf, sizeof(in), key); | |
79 | memcpy(in_buf + 1, in, sizeof(in)); | |
80 | do_test(in_buf + 1, sizeof(in), key); | |
81 | memcpy(in_buf + 2, in, sizeof(in)); | |
82 | do_test(in_buf + 2, sizeof(in), key); | |
83 | memcpy(in_buf + 4, in, sizeof(in)); | |
84 | do_test(in_buf + 4, sizeof(in), key); | |
a245d90a | 85 | } |