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