xref: /freebsd/sys/crypto/siphash/siphash.c (revision d0b2dbfa)
1 /*-
2  * Copyright (c) 2013 Andre Oppermann <andre@FreeBSD.org>
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 <sys/cdefs.h>
45 #include <sys/param.h>
46 #include <sys/types.h>
47 #include <sys/systm.h>
48 #include <sys/libkern.h>
49 #include <sys/endian.h>
50 
51 #include <crypto/siphash/siphash.h>
52 
53 static void	SipRounds(SIPHASH_CTX *ctx, int final);
54 
55 void
56 SipHash_InitX(SIPHASH_CTX *ctx, int rc, int rf)
57 {
58 
59 	ctx->v[0] = 0x736f6d6570736575ull;
60 	ctx->v[1] = 0x646f72616e646f6dull;
61 	ctx->v[2] = 0x6c7967656e657261ull;
62 	ctx->v[3] = 0x7465646279746573ull;
63 	ctx->buf.b64 = 0;
64 	ctx->bytes = 0;
65 	ctx->buflen = 0;
66 	ctx->rounds_compr = rc;
67 	ctx->rounds_final = rf;
68 	ctx->initialized = 1;
69 }
70 
71 void
72 SipHash_SetKey(SIPHASH_CTX *ctx, const uint8_t key[static SIPHASH_KEY_LENGTH])
73 {
74 	uint64_t k[2];
75 
76 	KASSERT(ctx->v[0] == 0x736f6d6570736575ull &&
77 	    ctx->initialized == 1,
78 	    ("%s: context %p not properly initialized", __func__, ctx));
79 
80 	k[0] = le64dec(&key[0]);
81 	k[1] = le64dec(&key[8]);
82 
83 	ctx->v[0] ^= k[0];
84 	ctx->v[1] ^= k[1];
85 	ctx->v[2] ^= k[0];
86 	ctx->v[3] ^= k[1];
87 
88 	ctx->initialized = 2;
89 }
90 
91 static size_t
92 SipBuf(SIPHASH_CTX *ctx, const uint8_t **src, size_t len, int final)
93 {
94 	size_t x = 0;
95 
96 	KASSERT((!final && len > 0) || (final && len == 0),
97 	    ("%s: invalid parameters", __func__));
98 
99 	if (!final) {
100 		x = MIN(len, sizeof(ctx->buf.b64) - ctx->buflen);
101 		bcopy(*src, &ctx->buf.b8[ctx->buflen], x);
102 		ctx->buflen += x;
103 		*src += x;
104 	} else
105 		ctx->buf.b8[7] = (uint8_t)ctx->bytes;
106 
107 	if (ctx->buflen == 8 || final) {
108 		ctx->v[3] ^= le64toh(ctx->buf.b64);
109 		SipRounds(ctx, 0);
110 		ctx->v[0] ^= le64toh(ctx->buf.b64);
111 		ctx->buf.b64 = 0;
112 		ctx->buflen = 0;
113 	}
114 	return (x);
115 }
116 
117 void
118 SipHash_Update(SIPHASH_CTX *ctx, const void *src, size_t len)
119 {
120 	uint64_t m;
121 	const uint64_t *p;
122 	const uint8_t *s;
123 	size_t rem;
124 
125 	KASSERT(ctx->initialized == 2,
126 	    ("%s: context %p not properly initialized", __func__, ctx));
127 
128 	s = src;
129 	ctx->bytes += len;
130 
131 	/*
132 	 * Push length smaller than block size into buffer or
133 	 * fill up the buffer if there is already something
134 	 * in it.
135 	 */
136 	if (ctx->buflen > 0 || len < 8)
137 		len -= SipBuf(ctx, &s, len, 0);
138 	if (len == 0)
139 		return;
140 
141 	rem = len & 0x7;
142 	len >>= 3;
143 
144 	/* Optimze for 64bit aligned/unaligned access. */
145 	if (((uintptr_t)s & 0x7) == 0) {
146 		for (p = (const uint64_t *)s; len > 0; len--, p++) {
147 			m = le64toh(*p);
148 			ctx->v[3] ^= m;
149 			SipRounds(ctx, 0);
150 			ctx->v[0] ^= m;
151 		}
152 		s = (const uint8_t *)p;
153 	} else {
154 		for (; len > 0; len--, s += 8) {
155 			m = le64dec(s);
156 			ctx->v[3] ^= m;
157 			SipRounds(ctx, 0);
158 			ctx->v[0] ^= m;
159 		}
160 	}
161 
162 	/* Push remainder into buffer. */
163 	if (rem > 0)
164 		(void)SipBuf(ctx, &s, rem, 0);
165 }
166 
167 void
168 SipHash_Final(uint8_t dst[static SIPHASH_DIGEST_LENGTH], SIPHASH_CTX *ctx)
169 {
170 	uint64_t r;
171 
172 	KASSERT(ctx->initialized == 2,
173 	    ("%s: context %p not properly initialized", __func__, ctx));
174 
175 	r = SipHash_End(ctx);
176 	le64enc(dst, r);
177 }
178 
179 uint64_t
180 SipHash_End(SIPHASH_CTX *ctx)
181 {
182 	uint64_t r;
183 
184 	KASSERT(ctx->initialized == 2,
185 	    ("%s: context %p not properly initialized", __func__, ctx));
186 
187 	SipBuf(ctx, NULL, 0, 1);
188 	ctx->v[2] ^= 0xff;
189 	SipRounds(ctx, 1);
190 	r = (ctx->v[0] ^ ctx->v[1]) ^ (ctx->v[2] ^ ctx->v[3]);
191 
192 	bzero(ctx, sizeof(*ctx));
193 	return (r);
194 }
195 
196 uint64_t
197 SipHashX(SIPHASH_CTX *ctx, int rc, int rf,
198     const uint8_t key[static SIPHASH_KEY_LENGTH], const void *src, size_t len)
199 {
200 
201 	SipHash_InitX(ctx, rc, rf);
202 	SipHash_SetKey(ctx, key);
203 	SipHash_Update(ctx, src, len);
204 
205 	return (SipHash_End(ctx));
206 }
207 
208 #define SIP_ROTL(x, b)	(uint64_t)(((x) << (b)) | ( (x) >> (64 - (b))))
209 
210 static void
211 SipRounds(SIPHASH_CTX *ctx, int final)
212 {
213 	int rounds;
214 
215 	if (!final)
216 		rounds = ctx->rounds_compr;
217 	else
218 		rounds = ctx->rounds_final;
219 
220 	while (rounds--) {
221 		ctx->v[0] += ctx->v[1];
222 		ctx->v[2] += ctx->v[3];
223 		ctx->v[1] = SIP_ROTL(ctx->v[1], 13);
224 		ctx->v[3] = SIP_ROTL(ctx->v[3], 16);
225 
226 		ctx->v[1] ^= ctx->v[0];
227 		ctx->v[3] ^= ctx->v[2];
228 		ctx->v[0] = SIP_ROTL(ctx->v[0], 32);
229 
230 		ctx->v[2] += ctx->v[1];
231 		ctx->v[0] += ctx->v[3];
232 		ctx->v[1] = SIP_ROTL(ctx->v[1], 17);
233 		ctx->v[3] = SIP_ROTL(ctx->v[3], 21);
234 
235 		ctx->v[1] ^= ctx->v[2];
236 		ctx->v[3] ^= ctx->v[0];
237 		ctx->v[2] = SIP_ROTL(ctx->v[2], 32);
238 	}
239 }
240 
241