Debian Codegraph @

src/basic/siphash24.c

raw
1 /* SPDX-License-Identifier: CC0-1.0 */
2
3 /*
4 SipHash reference C implementation
5
6 Written in 2012 by
7 Jean-Philippe Aumasson <[email protected]>
8 Daniel J. Bernstein <[email protected]>
9
10 To the extent possible under law, the author(s) have dedicated all copyright
11 and related and neighboring rights to this software to the public domain
12 worldwide. This software is distributed without any warranty.
13
14 You should have received a copy of the CC0 Public Domain Dedication along with
15 this software. If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
16
17 (Minimal changes made by Lennart Poettering, to make clean for inclusion in systemd)
18 (Refactored by Tom Gundersen to split up in several functions and follow systemd
19 coding style)
20 */
21
22 #include <stdio.h>
23
24 #include "iovec-util.h"
25 #include "siphash24.h"
26 #include "string-util.h"
27 #include "unaligned.h"
28
29 static uint64_t rotate_left(uint64_t x, uint8_t b) {
30 assert(b < 64);
31
32 return (x << b) | (x >> (64 - b));
33 }
34
35 static void sipround(struct siphash *state) {
36 assert(state);
37
38 state->v0 += state->v1;
39 state->v1 = rotate_left(state->v1, 13);
40 state->v1 ^= state->v0;
41 state->v0 = rotate_left(state->v0, 32);
42 state->v2 += state->v3;
43 state->v3 = rotate_left(state->v3, 16);
44 state->v3 ^= state->v2;
45 state->v0 += state->v3;
46 state->v3 = rotate_left(state->v3, 21);
47 state->v3 ^= state->v0;
48 state->v2 += state->v1;
49 state->v1 = rotate_left(state->v1, 17);
50 state->v1 ^= state->v2;
51 state->v2 = rotate_left(state->v2, 32);
52 }
53
54 void siphash24_init(struct siphash *state, const uint8_t k[static 16]) {
55 uint64_t k0, k1;
56
57 assert(state);
58 assert(k);
59
60 k0 = unaligned_read_le64(k);
61 k1 = unaligned_read_le64(k + 8);
62
63 *state = (struct siphash) {
64 /* "somepseudorandomlygeneratedbytes" */
65 .v0 = 0x736f6d6570736575ULL ^ k0,
66 .v1 = 0x646f72616e646f6dULL ^ k1,
67 .v2 = 0x6c7967656e657261ULL ^ k0,
68 .v3 = 0x7465646279746573ULL ^ k1,
69 .padding = 0,
70 .inlen = 0,
71 };
72 }
73
74 void siphash24_compress(const void *_in, size_t inlen, struct siphash *state) {
75
76 const uint8_t *in = ASSERT_PTR(_in);
77 const uint8_t *end = in + inlen;
78 size_t left = state->inlen & 7;
79 uint64_t m;
80
81 assert(state);
82
83 /* Update total length */
84 state->inlen += inlen;
85
86 /* If padding exists, fill it out */
87 if (left > 0) {
88 for ( ; in < end && left < 8; in ++, left ++)
89 state->padding |= ((uint64_t) *in) << (left * 8);
90
91 if (in == end && left < 8)
92 /* We did not have enough input to fill out the padding completely */
93 return;
94
95 #if ENABLE_DEBUG_SIPHASH
96 printf("(%3zu) v0 %08x %08x\n", state->inlen, (uint32_t) (state->v0 >> 32), (uint32_t) state->v0);
97 printf("(%3zu) v1 %08x %08x\n", state->inlen, (uint32_t) (state->v1 >> 32), (uint32_t) state->v1);
98 printf("(%3zu) v2 %08x %08x\n", state->inlen, (uint32_t) (state->v2 >> 32), (uint32_t) state->v2);
99 printf("(%3zu) v3 %08x %08x\n", state->inlen, (uint32_t) (state->v3 >> 32), (uint32_t) state->v3);
100 printf("(%3zu) compress padding %08x %08x\n", state->inlen, (uint32_t) (state->padding >> 32), (uint32_t)state->padding);
101 #endif
102
103 state->v3 ^= state->padding;
104 sipround(state);
105 sipround(state);
106 state->v0 ^= state->padding;
107
108 state->padding = 0;
109 }
110
111 end -= (state->inlen % sizeof(uint64_t));
112
113 for ( ; in < end; in += 8) {
114 m = unaligned_read_le64(in);
115 #if ENABLE_DEBUG_SIPHASH
116 printf("(%3zu) v0 %08x %08x\n", state->inlen, (uint32_t) (state->v0 >> 32), (uint32_t) state->v0);
117 printf("(%3zu) v1 %08x %08x\n", state->inlen, (uint32_t) (state->v1 >> 32), (uint32_t) state->v1);
118 printf("(%3zu) v2 %08x %08x\n", state->inlen, (uint32_t) (state->v2 >> 32), (uint32_t) state->v2);
119 printf("(%3zu) v3 %08x %08x\n", state->inlen, (uint32_t) (state->v3 >> 32), (uint32_t) state->v3);
120 printf("(%3zu) compress %08x %08x\n", state->inlen, (uint32_t) (m >> 32), (uint32_t) m);
121 #endif
122 state->v3 ^= m;
123 sipround(state);
124 sipround(state);
125 state->v0 ^= m;
126 }
127
128 left = state->inlen & 7;
129 switch (left) {
130 case 7:
131 state->padding |= ((uint64_t) in[6]) << 48;
132 _fallthrough_;
133 case 6:
134 state->padding |= ((uint64_t) in[5]) << 40;
135 _fallthrough_;
136 case 5:
137 state->padding |= ((uint64_t) in[4]) << 32;
138 _fallthrough_;
139 case 4:
140 state->padding |= ((uint64_t) in[3]) << 24;
141 _fallthrough_;
142 case 3:
143 state->padding |= ((uint64_t) in[2]) << 16;
144 _fallthrough_;
145 case 2:
146 state->padding |= ((uint64_t) in[1]) << 8;
147 _fallthrough_;
148 case 1:
149 state->padding |= ((uint64_t) in[0]);
150 _fallthrough_;
151 case 0:
152 break;
153 }
154 }
155
156 void siphash24_compress_string(const char *in, struct siphash *state) {
157 siphash24_compress_safe(in, strlen_ptr(in), state);
158 }
159
160 void siphash24_compress_iovec(const struct iovec *iov, struct siphash *state) {
161 assert(iovec_is_valid(iov));
162 assert(state);
163
164 if (!iovec_is_set(iov))
165 return;
166
167 siphash24_compress(iov->iov_base, iov->iov_len, state);
168 }
169
170 uint64_t siphash24_finalize(struct siphash *state) {
171 uint64_t b;
172
173 assert(state);
174
175 b = state->padding | (((uint64_t) state->inlen) << 56);
176
177 #if ENABLE_DEBUG_SIPHASH
178 printf("(%3zu) v0 %08x %08x\n", state->inlen, (uint32_t) (state->v0 >> 32), (uint32_t) state->v0);
179 printf("(%3zu) v1 %08x %08x\n", state->inlen, (uint32_t) (state->v1 >> 32), (uint32_t) state->v1);
180 printf("(%3zu) v2 %08x %08x\n", state->inlen, (uint32_t) (state->v2 >> 32), (uint32_t) state->v2);
181 printf("(%3zu) v3 %08x %08x\n", state->inlen, (uint32_t) (state->v3 >> 32), (uint32_t) state->v3);
182 printf("(%3zu) padding %08x %08x\n", state->inlen, (uint32_t) (state->padding >> 32), (uint32_t) state->padding);
183 #endif
184
185 state->v3 ^= b;
186 sipround(state);
187 sipround(state);
188 state->v0 ^= b;
189
190 #if ENABLE_DEBUG_SIPHASH
191 printf("(%3zu) v0 %08x %08x\n", state->inlen, (uint32_t) (state->v0 >> 32), (uint32_t) state->v0);
192 printf("(%3zu) v1 %08x %08x\n", state->inlen, (uint32_t) (state->v1 >> 32), (uint32_t) state->v1);
193 printf("(%3zu) v2 %08x %08x\n", state->inlen, (uint32_t) (state->v2 >> 32), (uint32_t) state->v2);
194 printf("(%3zu) v3 %08x %08x\n", state->inlen, (uint32_t) (state->v3 >> 32), (uint32_t) state->v3);
195 #endif
196 state->v2 ^= 0xff;
197
198 sipround(state);
199 sipround(state);
200 sipround(state);
201 sipround(state);
202
203 return state->v0 ^ state->v1 ^ state->v2 ^ state->v3;
204 }
205
206 uint64_t siphash24(const void *in, size_t inlen, const uint8_t k[static 16]) {
207 struct siphash state;
208
209 assert(in);
210 assert(k);
211
212 siphash24_init(&state, k);
213 siphash24_compress(in, inlen, &state);
214
215 return siphash24_finalize(&state);
216 }
217
218 uint64_t siphash24_string(const char *s, const uint8_t k[static 16]) {
219 return siphash24(s, strlen(s) + 1, k);
220 }