1 /*-
2 * Copyright (c) 2013 Andre Oppermann <[email protected]>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote
14 * products derived from this software without specific prior written
15 * permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * SipHash is a family of PRFs SipHash-c-d where the integer parameters c and d
32 * are the number of compression rounds and the number of finalization rounds.
33 * A compression round is identical to a finalization round and this round
34 * function is called SipRound. Given a 128-bit key k and a (possibly empty)
35 * byte string m, SipHash-c-d returns a 64-bit value SipHash-c-d(k; m).
36 *
37 * Implemented from the paper "SipHash: a fast short-input PRF", 2012.09.18,
38 * by Jean-Philippe Aumasson and Daniel J. Bernstein,
39 * Permanent Document ID b9a943a805fbfc6fde808af9fc0ecdfa
40 * https://131002.net/siphash/siphash.pdf
41 * https://131002.net/siphash/
42 */
43
44 #include <libkern/libkern.h>
45 #include <net/siphash.h>
46 #include <sys/endian.h>
47 #include <sys/mcache.h>
48 #include <sys/param.h>
49 #include <sys/types.h>
50 #include <sys/systm.h>
51
52 static void SipRounds(SIPHASH_CTX *ctx, int final);
53
54 void
SipHash_InitX(SIPHASH_CTX * ctx,uint8_t rc,uint8_t rf)55 SipHash_InitX(SIPHASH_CTX *ctx, uint8_t rc, uint8_t rf)
56 {
57 ctx->v[0] = 0x736f6d6570736575ull;
58 ctx->v[1] = 0x646f72616e646f6dull;
59 ctx->v[2] = 0x6c7967656e657261ull;
60 ctx->v[3] = 0x7465646279746573ull;
61 ctx->buf.b64 = 0;
62 ctx->bytes = 0;
63 ctx->buflen = 0;
64 ctx->rounds_compr = rc;
65 ctx->rounds_final = rf;
66 ctx->initialized = 1;
67 }
68
69 void
SipHash_SetKey(SIPHASH_CTX * ctx,const uint8_t key[SIPHASH_KEY_LENGTH])70 SipHash_SetKey(SIPHASH_CTX *ctx, const uint8_t key[SIPHASH_KEY_LENGTH])
71 {
72 uint64_t k[2];
73
74 ASSERT(ctx->v[0] == 0x736f6d6570736575ull &&
75 ctx->initialized == 1);
76
77 k[0] = le64dec(&key[0]);
78 k[1] = le64dec(&key[8]);
79
80 ctx->v[0] ^= k[0];
81 ctx->v[1] ^= k[1];
82 ctx->v[2] ^= k[0];
83 ctx->v[3] ^= k[1];
84
85 ctx->initialized = 2;
86 }
87
88 static const uint8_t *__indexable
SipBuf(SIPHASH_CTX * ctx,const uint8_t * __sized_by_or_null (len)src,size_t len,size_t * delta,int final)89 SipBuf(SIPHASH_CTX *ctx, const uint8_t * __sized_by_or_null(len)src, size_t len, size_t *delta, int final)
90 {
91 size_t x = 0;
92
93 const uint8_t *buf = src;
94
95 /* handle hashing 0 length buffer - needed for test vectors */
96 if (len == 0 && final == 0) {
97 return 0;
98 }
99
100 if (final) {
101 ASSERT(len == 0);
102 ctx->buf.b8[7] = (uint8_t)ctx->bytes;
103 } else {
104 ASSERT((len > 0) && src);
105 x = MIN(len, sizeof(ctx->buf.b64) - ctx->buflen);
106 bcopy(buf, &ctx->buf.b8[ctx->buflen], x);
107 ctx->buflen += x;
108 buf += x;
109 }
110
111 if (ctx->buflen == 8 || final) {
112 ctx->v[3] ^= le64toh(ctx->buf.b64);
113 SipRounds(ctx, 0);
114 ctx->v[0] ^= le64toh(ctx->buf.b64);
115 ctx->buf.b64 = 0;
116 ctx->buflen = 0;
117 }
118
119 if (delta != NULL) {
120 *delta = x;
121 }
122
123 return buf;
124 }
125
126 void
SipHash_Update(SIPHASH_CTX * ctx,const void * src __sized_by (len0),size_t len0)127 SipHash_Update(SIPHASH_CTX *ctx, const void *src __sized_by(len0), size_t len0)
128 {
129 uint64_t m;
130 const uint64_t *p;
131 const uint8_t *s;
132 size_t rem;
133 size_t len = len0;
134 size_t len_in_bytes = 0;
135
136 ASSERT(ctx->initialized == 2);
137
138 s = src;
139 ctx->bytes += len;
140
141 /*
142 * Push length smaller than block size into buffer or
143 * fill up the buffer if there is already something
144 * in it.
145 */
146 if (ctx->buflen > 0 || len < 8) {
147 size_t delta = 0;
148 s = SipBuf(ctx, s, len, &delta, 0);
149 len -= delta;
150 }
151 if (len == 0) {
152 return;
153 }
154
155 rem = len & 0x7;
156 len_in_bytes = len;
157 len >>= 3;
158
159 /* Optimze for 64bit aligned/unaligned access. */
160 if (((uintptr_t)s & 0x7) == 0) {
161 p = __unsafe_forge_bidi_indexable(const uint64_t *,
162 __builtin_assume_aligned((const uint8_t *__unsafe_indexable)s, sizeof(uint64_t)), len_in_bytes);
163 for (; len > 0; len--, p++) {
164 m = le64toh(*p);
165 ctx->v[3] ^= m;
166 SipRounds(ctx, 0);
167 ctx->v[0] ^= m;
168 }
169 s = (const uint8_t *)p;
170 } else {
171 for (; len > 0; len--, s += 8) {
172 m = le64dec(s);
173 ctx->v[3] ^= m;
174 SipRounds(ctx, 0);
175 ctx->v[0] ^= m;
176 }
177 }
178
179 /* Push remainder into buffer. */
180 if (rem > 0) {
181 s = SipBuf(ctx, s, rem, NULL, 0);
182 }
183 }
184
185 void
SipHash_Final(uint8_t dst[SIPHASH_DIGEST_LENGTH],SIPHASH_CTX * ctx)186 SipHash_Final(uint8_t dst[SIPHASH_DIGEST_LENGTH], SIPHASH_CTX *ctx)
187 {
188 uint64_t r;
189
190 ASSERT(ctx->initialized == 2);
191
192 r = SipHash_End(ctx);
193 le64enc(dst, r);
194 }
195
196 uint64_t
SipHash_End(SIPHASH_CTX * ctx)197 SipHash_End(SIPHASH_CTX *ctx)
198 {
199 uint64_t r;
200
201 ASSERT(ctx->initialized == 2);
202
203 SipBuf(ctx, NULL, 0, NULL, 1);
204
205 ctx->v[2] ^= 0xff;
206 SipRounds(ctx, 1);
207 r = (ctx->v[0] ^ ctx->v[1]) ^ (ctx->v[2] ^ ctx->v[3]);
208
209 bzero(ctx, sizeof(*ctx));
210 return r;
211 }
212
213 uint64_t
SipHashX(SIPHASH_CTX * ctx,uint8_t rc,uint8_t rf,const uint8_t key[SIPHASH_KEY_LENGTH],const void * src __sized_by (len),size_t len)214 SipHashX(SIPHASH_CTX *ctx, uint8_t rc, uint8_t rf,
215 const uint8_t key[SIPHASH_KEY_LENGTH],
216 const void *src __sized_by(len), size_t len)
217 {
218 SipHash_InitX(ctx, rc, rf);
219 SipHash_SetKey(ctx, key);
220 SipHash_Update(ctx, src, len);
221
222 return SipHash_End(ctx);
223 }
224
225 #define SIP_ROTL(x, b) (uint64_t)(((x) << (b)) | ( (x) >> (64 - (b))))
226
227 static void
SipRounds(SIPHASH_CTX * ctx,int final)228 SipRounds(SIPHASH_CTX *ctx, int final)
229 {
230 int rounds;
231
232 if (!final) {
233 rounds = ctx->rounds_compr;
234 } else {
235 rounds = ctx->rounds_final;
236 }
237
238 while (rounds--) {
239 ctx->v[0] += ctx->v[1];
240 ctx->v[2] += ctx->v[3];
241 ctx->v[1] = SIP_ROTL(ctx->v[1], 13);
242 ctx->v[3] = SIP_ROTL(ctx->v[3], 16);
243
244 ctx->v[1] ^= ctx->v[0];
245 ctx->v[3] ^= ctx->v[2];
246 ctx->v[0] = SIP_ROTL(ctx->v[0], 32);
247
248 ctx->v[2] += ctx->v[1];
249 ctx->v[0] += ctx->v[3];
250 ctx->v[1] = SIP_ROTL(ctx->v[1], 17);
251 ctx->v[3] = SIP_ROTL(ctx->v[3], 21);
252
253 ctx->v[1] ^= ctx->v[2];
254 ctx->v[3] ^= ctx->v[0];
255 ctx->v[2] = SIP_ROTL(ctx->v[2], 32);
256 }
257 }
258