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