/src/unbound/util/siphash.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | SipHash reference C implementation |
3 | | |
4 | | Copyright (c) 2012-2016 Jean-Philippe Aumasson |
5 | | <jeanphilippe.aumasson@gmail.com> |
6 | | Copyright (c) 2012-2014 Daniel J. Bernstein <djb@cr.yp.to> |
7 | | |
8 | | To the extent possible under law, the author(s) have dedicated all copyright |
9 | | and related and neighboring rights to this software to the public domain |
10 | | worldwide. This software is distributed without any warranty. |
11 | | |
12 | | You should have received a copy of the CC0 Public Domain Dedication along |
13 | | with |
14 | | this software. If not, see |
15 | | <http://creativecommons.org/publicdomain/zero/1.0/>. |
16 | | */ |
17 | | /** |
18 | | * Edited slightly for integration in Unbound. Edits are noted with 'EDIT'. |
19 | | */ |
20 | | /** EDIT |
21 | | * \#include <assert.h> |
22 | | * \#include <stdint.h> |
23 | | * \#include <stdio.h> |
24 | | * \#include <string.h> |
25 | | * Replaced the above includes with Unbound's config.h |
26 | | */ |
27 | | #include "config.h" |
28 | | |
29 | | /** EDIT |
30 | | * prevent warning from -Wmissing-prototypes |
31 | | */ |
32 | | #include "util/siphash.h" |
33 | | |
34 | | /* default: SipHash-2-4 */ |
35 | 0 | #define cROUNDS 2 |
36 | 0 | #define dROUNDS 4 |
37 | | |
38 | 0 | #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) |
39 | | |
40 | | #define U32TO8_LE(p, v) \ |
41 | 0 | (p)[0] = (uint8_t)((v)); \ |
42 | 0 | (p)[1] = (uint8_t)((v) >> 8); \ |
43 | 0 | (p)[2] = (uint8_t)((v) >> 16); \ |
44 | 0 | (p)[3] = (uint8_t)((v) >> 24); |
45 | | |
46 | | #define U64TO8_LE(p, v) \ |
47 | 0 | U32TO8_LE((p), (uint32_t)((v))); \ |
48 | 0 | U32TO8_LE((p) + 4, (uint32_t)((v) >> 32)); |
49 | | |
50 | | #define U8TO64_LE(p) \ |
51 | 0 | (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \ |
52 | 0 | ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \ |
53 | 0 | ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \ |
54 | 0 | ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56)) |
55 | | |
56 | | #define SIPROUND \ |
57 | 0 | do { \ |
58 | 0 | v0 += v1; \ |
59 | 0 | v1 = ROTL(v1, 13); \ |
60 | 0 | v1 ^= v0; \ |
61 | 0 | v0 = ROTL(v0, 32); \ |
62 | 0 | v2 += v3; \ |
63 | 0 | v3 = ROTL(v3, 16); \ |
64 | 0 | v3 ^= v2; \ |
65 | 0 | v0 += v3; \ |
66 | 0 | v3 = ROTL(v3, 21); \ |
67 | 0 | v3 ^= v0; \ |
68 | 0 | v2 += v1; \ |
69 | 0 | v1 = ROTL(v1, 17); \ |
70 | 0 | v1 ^= v2; \ |
71 | 0 | v2 = ROTL(v2, 32); \ |
72 | 0 | } while (0) |
73 | | |
74 | | #ifdef DEBUG |
75 | | #define TRACE \ |
76 | | do { \ |
77 | | printf("(%3d) v0 %08x %08x\n", (int)inlen, (uint32_t)(v0 >> 32), \ |
78 | | (uint32_t)v0); \ |
79 | | printf("(%3d) v1 %08x %08x\n", (int)inlen, (uint32_t)(v1 >> 32), \ |
80 | | (uint32_t)v1); \ |
81 | | printf("(%3d) v2 %08x %08x\n", (int)inlen, (uint32_t)(v2 >> 32), \ |
82 | | (uint32_t)v2); \ |
83 | | printf("(%3d) v3 %08x %08x\n", (int)inlen, (uint32_t)(v3 >> 32), \ |
84 | | (uint32_t)v3); \ |
85 | | } while (0) |
86 | | #else |
87 | | #define TRACE |
88 | | #endif |
89 | | |
90 | | int siphash(const uint8_t *in, const size_t inlen, const uint8_t *k, |
91 | 0 | uint8_t *out, const size_t outlen) { |
92 | |
|
93 | 0 | uint64_t v0 = 0x736f6d6570736575ULL; |
94 | 0 | uint64_t v1 = 0x646f72616e646f6dULL; |
95 | 0 | uint64_t v2 = 0x6c7967656e657261ULL; |
96 | 0 | uint64_t v3 = 0x7465646279746573ULL; |
97 | 0 | uint64_t k0 = U8TO64_LE(k); |
98 | 0 | uint64_t k1 = U8TO64_LE(k + 8); |
99 | 0 | uint64_t m; |
100 | 0 | int i; |
101 | 0 | const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t)); |
102 | 0 | const int left = inlen & 7; |
103 | 0 | uint64_t b = ((uint64_t)inlen) << 56; |
104 | | /** EDIT |
105 | | * The following assert moved here from the top for C90 compliance. |
106 | | */ |
107 | 0 | assert((outlen == 8) || (outlen == 16)); |
108 | 0 | v3 ^= k1; |
109 | 0 | v2 ^= k0; |
110 | 0 | v1 ^= k1; |
111 | 0 | v0 ^= k0; |
112 | |
|
113 | 0 | if (outlen == 16) |
114 | 0 | v1 ^= 0xee; |
115 | |
|
116 | 0 | for (; in != end; in += 8) { |
117 | 0 | m = U8TO64_LE(in); |
118 | 0 | v3 ^= m; |
119 | |
|
120 | 0 | TRACE; |
121 | 0 | for (i = 0; i < cROUNDS; ++i) |
122 | 0 | SIPROUND; |
123 | |
|
124 | 0 | v0 ^= m; |
125 | 0 | } |
126 | |
|
127 | 0 | switch (left) { |
128 | 0 | case 7: |
129 | 0 | b |= ((uint64_t)in[6]) << 48; |
130 | | /** EDIT annotate case statement fallthrough for gcc */ |
131 | 0 | ATTR_FALLTHROUGH |
132 | | /* fallthrough */ |
133 | 0 | case 6: |
134 | 0 | b |= ((uint64_t)in[5]) << 40; |
135 | | /** EDIT annotate case statement fallthrough for gcc */ |
136 | 0 | ATTR_FALLTHROUGH |
137 | | /* fallthrough */ |
138 | 0 | case 5: |
139 | 0 | b |= ((uint64_t)in[4]) << 32; |
140 | | /** EDIT annotate case statement fallthrough for gcc */ |
141 | 0 | ATTR_FALLTHROUGH |
142 | | /* fallthrough */ |
143 | 0 | case 4: |
144 | 0 | b |= ((uint64_t)in[3]) << 24; |
145 | | /** EDIT annotate case statement fallthrough for gcc */ |
146 | 0 | ATTR_FALLTHROUGH |
147 | | /* fallthrough */ |
148 | 0 | case 3: |
149 | 0 | b |= ((uint64_t)in[2]) << 16; |
150 | | /** EDIT annotate case statement fallthrough for gcc */ |
151 | 0 | ATTR_FALLTHROUGH |
152 | | /* fallthrough */ |
153 | 0 | case 2: |
154 | 0 | b |= ((uint64_t)in[1]) << 8; |
155 | | /** EDIT annotate case statement fallthrough for gcc */ |
156 | 0 | ATTR_FALLTHROUGH |
157 | | /* fallthrough */ |
158 | 0 | case 1: |
159 | 0 | b |= ((uint64_t)in[0]); |
160 | 0 | break; |
161 | 0 | case 0: |
162 | 0 | break; |
163 | 0 | } |
164 | | |
165 | 0 | v3 ^= b; |
166 | |
|
167 | 0 | TRACE; |
168 | 0 | for (i = 0; i < cROUNDS; ++i) |
169 | 0 | SIPROUND; |
170 | |
|
171 | 0 | v0 ^= b; |
172 | |
|
173 | 0 | if (outlen == 16) |
174 | 0 | v2 ^= 0xee; |
175 | 0 | else |
176 | 0 | v2 ^= 0xff; |
177 | |
|
178 | 0 | TRACE; |
179 | 0 | for (i = 0; i < dROUNDS; ++i) |
180 | 0 | SIPROUND; |
181 | |
|
182 | 0 | b = v0 ^ v1 ^ v2 ^ v3; |
183 | 0 | U64TO8_LE(out, b); |
184 | |
|
185 | 0 | if (outlen == 8) |
186 | 0 | return 0; |
187 | | |
188 | 0 | v1 ^= 0xdd; |
189 | |
|
190 | 0 | TRACE; |
191 | 0 | for (i = 0; i < dROUNDS; ++i) |
192 | 0 | SIPROUND; |
193 | |
|
194 | 0 | b = v0 ^ v1 ^ v2 ^ v3; |
195 | 0 | U64TO8_LE(out + 8, b); |
196 | |
|
197 | 0 | return 0; |
198 | 0 | } |