2 SipHash reference C implementation
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>
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.
12 You should have received a copy of the CC0 Public Domain Dedication along
14 this software. If not, see
15 <http://creativecommons.org/publicdomain/zero/1.0/>.
18 * Edited slightly for integration in Unbound. Edits are noted with 'EDIT'.
21 * \#include <assert.h>
22 * \#include <stdint.h>
24 * \#include <string.h>
25 * Replaced the above includes with Unbound's config.h
29 /* default: SipHash-2-4 */
33 #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b))))
35 #define U32TO8_LE(p, v) \
36 (p)[0] = (uint8_t)((v)); \
37 (p)[1] = (uint8_t)((v) >> 8); \
38 (p)[2] = (uint8_t)((v) >> 16); \
39 (p)[3] = (uint8_t)((v) >> 24);
41 #define U64TO8_LE(p, v) \
42 U32TO8_LE((p), (uint32_t)((v))); \
43 U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));
45 #define U8TO64_LE(p) \
46 (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \
47 ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \
48 ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \
49 ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56))
72 printf("(%3d) v0 %08x %08x\n", (int)inlen, (uint32_t)(v0 >> 32), \
74 printf("(%3d) v1 %08x %08x\n", (int)inlen, (uint32_t)(v1 >> 32), \
76 printf("(%3d) v2 %08x %08x\n", (int)inlen, (uint32_t)(v2 >> 32), \
78 printf("(%3d) v3 %08x %08x\n", (int)inlen, (uint32_t)(v3 >> 32), \
85 int siphash(const uint8_t *in, const size_t inlen, const uint8_t *k,
86 uint8_t *out, const size_t outlen) {
88 uint64_t v0 = 0x736f6d6570736575ULL;
89 uint64_t v1 = 0x646f72616e646f6dULL;
90 uint64_t v2 = 0x6c7967656e657261ULL;
91 uint64_t v3 = 0x7465646279746573ULL;
92 uint64_t k0 = U8TO64_LE(k);
93 uint64_t k1 = U8TO64_LE(k + 8);
96 const uint8_t *end = in + inlen - (inlen % sizeof(uint64_t));
97 const int left = inlen & 7;
98 uint64_t b = ((uint64_t)inlen) << 56;
100 * The following assert moved here from the top for C90 compliance.
102 assert((outlen == 8) || (outlen == 16));
111 for (; in != end; in += 8) {
116 for (i = 0; i < cROUNDS; ++i)
124 b |= ((uint64_t)in[6]) << 48;
125 /** EDIT annotate case statement fallthrough for gcc */
128 b |= ((uint64_t)in[5]) << 40;
129 /** EDIT annotate case statement fallthrough for gcc */
132 b |= ((uint64_t)in[4]) << 32;
133 /** EDIT annotate case statement fallthrough for gcc */
136 b |= ((uint64_t)in[3]) << 24;
137 /** EDIT annotate case statement fallthrough for gcc */
140 b |= ((uint64_t)in[2]) << 16;
141 /** EDIT annotate case statement fallthrough for gcc */
144 b |= ((uint64_t)in[1]) << 8;
145 /** EDIT annotate case statement fallthrough for gcc */
148 b |= ((uint64_t)in[0]);
157 for (i = 0; i < cROUNDS; ++i)
168 for (i = 0; i < dROUNDS; ++i)
171 b = v0 ^ v1 ^ v2 ^ v3;
180 for (i = 0; i < dROUNDS; ++i)
183 b = v0 ^ v1 ^ v2 ^ v3;
184 U64TO8_LE(out + 8, b);