2 * Copyright (c) 2013 Andre Oppermann <andre@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
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).
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/
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/types.h>
49 #include <sys/systm.h>
50 #include <sys/libkern.h>
51 #include <sys/endian.h>
53 #include <crypto/siphash/siphash.h>
55 static void SipRounds(SIPHASH_CTX *ctx, int final);
58 SipHash_InitX(SIPHASH_CTX *ctx, int rc, int rf)
61 ctx->v[0] = 0x736f6d6570736575ull;
62 ctx->v[1] = 0x646f72616e646f6dull;
63 ctx->v[2] = 0x6c7967656e657261ull;
64 ctx->v[3] = 0x7465646279746573ull;
68 ctx->rounds_compr = rc;
69 ctx->rounds_final = rf;
74 SipHash_SetKey(SIPHASH_CTX *ctx, const uint8_t key[static SIPHASH_KEY_LENGTH])
78 KASSERT(ctx->v[0] == 0x736f6d6570736575ull &&
79 ctx->initialized == 1,
80 ("%s: context %p not properly initialized", __func__, ctx));
82 k[0] = le64dec(&key[0]);
83 k[1] = le64dec(&key[8]);
94 SipBuf(SIPHASH_CTX *ctx, const uint8_t **src, size_t len, int final)
98 KASSERT((!final && len > 0) || (final && len == 0),
99 ("%s: invalid parameters", __func__));
102 x = MIN(len, sizeof(ctx->buf.b64) - ctx->buflen);
103 bcopy(*src, &ctx->buf.b8[ctx->buflen], x);
107 ctx->buf.b8[7] = (uint8_t)ctx->bytes;
109 if (ctx->buflen == 8 || final) {
110 ctx->v[3] ^= le64toh(ctx->buf.b64);
112 ctx->v[0] ^= le64toh(ctx->buf.b64);
120 SipHash_Update(SIPHASH_CTX *ctx, const void *src, size_t len)
127 KASSERT(ctx->initialized == 2,
128 ("%s: context %p not properly initialized", __func__, ctx));
134 * Push length smaller than block size into buffer or
135 * fill up the buffer if there is already something
138 if (ctx->buflen > 0 || len < 8)
139 len -= SipBuf(ctx, &s, len, 0);
146 /* Optimze for 64bit aligned/unaligned access. */
147 if (((uintptr_t)s & 0x7) == 0) {
148 for (p = (const uint64_t *)s; len > 0; len--, p++) {
154 s = (const uint8_t *)p;
156 for (; len > 0; len--, s += 8) {
164 /* Push remainder into buffer. */
166 (void)SipBuf(ctx, &s, rem, 0);
170 SipHash_Final(uint8_t dst[static SIPHASH_DIGEST_LENGTH], SIPHASH_CTX *ctx)
174 KASSERT(ctx->initialized == 2,
175 ("%s: context %p not properly initialized", __func__, ctx));
177 r = SipHash_End(ctx);
182 SipHash_End(SIPHASH_CTX *ctx)
186 KASSERT(ctx->initialized == 2,
187 ("%s: context %p not properly initialized", __func__, ctx));
189 SipBuf(ctx, NULL, 0, 1);
192 r = (ctx->v[0] ^ ctx->v[1]) ^ (ctx->v[2] ^ ctx->v[3]);
194 bzero(ctx, sizeof(*ctx));
199 SipHashX(SIPHASH_CTX *ctx, int rc, int rf,
200 const uint8_t key[static SIPHASH_KEY_LENGTH], const void *src, size_t len)
203 SipHash_InitX(ctx, rc, rf);
204 SipHash_SetKey(ctx, key);
205 SipHash_Update(ctx, src, len);
207 return (SipHash_End(ctx));
210 #define SIP_ROTL(x, b) (uint64_t)(((x) << (b)) | ( (x) >> (64 - (b))))
213 SipRounds(SIPHASH_CTX *ctx, int final)
218 rounds = ctx->rounds_compr;
220 rounds = ctx->rounds_final;
223 ctx->v[0] += ctx->v[1];
224 ctx->v[2] += ctx->v[3];
225 ctx->v[1] = SIP_ROTL(ctx->v[1], 13);
226 ctx->v[3] = SIP_ROTL(ctx->v[3], 16);
228 ctx->v[1] ^= ctx->v[0];
229 ctx->v[3] ^= ctx->v[2];
230 ctx->v[0] = SIP_ROTL(ctx->v[0], 32);
232 ctx->v[2] += ctx->v[1];
233 ctx->v[0] += ctx->v[3];
234 ctx->v[1] = SIP_ROTL(ctx->v[1], 17);
235 ctx->v[3] = SIP_ROTL(ctx->v[3], 21);
237 ctx->v[1] ^= ctx->v[2];
238 ctx->v[3] ^= ctx->v[0];
239 ctx->v[2] = SIP_ROTL(ctx->v[2], 32);