1253208Sandre/*-
2253208Sandre * Copyright (c) 2013 Andre Oppermann <andre@FreeBSD.org>
3253208Sandre * All rights reserved.
4253208Sandre *
5253208Sandre * Redistribution and use in source and binary forms, with or without
6253208Sandre * modification, are permitted provided that the following conditions
7253208Sandre * are met:
8253208Sandre * 1. Redistributions of source code must retain the above copyright
9253208Sandre *    notice, this list of conditions and the following disclaimer.
10253208Sandre * 2. Redistributions in binary form must reproduce the above copyright
11253208Sandre *    notice, this list of conditions and the following disclaimer in the
12253208Sandre *    documentation and/or other materials provided with the distribution.
13253208Sandre * 3. The name of the author may not be used to endorse or promote
14253208Sandre *    products derived from this software without specific prior written
15253208Sandre *    permission.
16253208Sandre *
17253208Sandre * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18253208Sandre * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19253208Sandre * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20253208Sandre * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21253208Sandre * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22253208Sandre * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23253208Sandre * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24253208Sandre * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25253208Sandre * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26253208Sandre * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27253208Sandre * SUCH DAMAGE.
28253208Sandre *
29253208Sandre * $FreeBSD: releng/10.2/sys/crypto/siphash/siphash.h 253208 2013-07-11 14:18:38Z andre $
30253208Sandre */
31253208Sandre
32253208Sandre/*
33253208Sandre * SipHash is a family of pseudorandom functions (a.k.a. keyed hash functions)
34253208Sandre * optimized for speed on short messages returning a 64bit hash/digest value.
35253208Sandre *
36253208Sandre * The number of rounds is defined during the initialization:
37253208Sandre *  SipHash24_Init() for the fast and resonable strong version
38253208Sandre *  SipHash48_Init() for the strong version (half as fast)
39253208Sandre *
40253208Sandre * struct SIPHASH_CTX ctx;
41253208Sandre * SipHash24_Init(&ctx);
42253208Sandre * SipHash_SetKey(&ctx, "16bytes long key");
43253208Sandre * SipHash_Update(&ctx, pointer_to_string, length_of_string);
44253208Sandre * SipHash_Final(output, &ctx);
45253208Sandre */
46253208Sandre
47253208Sandre#ifndef _SIPHASH_H_
48253208Sandre#define _SIPHASH_H_
49253208Sandre
50253208Sandre#define SIPHASH_BLOCK_LENGTH	 8
51253208Sandre#define SIPHASH_KEY_LENGTH	16
52253208Sandre#define SIPHASH_DIGEST_LENGTH	 8
53253208Sandre
54253208Sandretypedef struct _SIPHASH_CTX {
55253208Sandre	uint64_t	v[4];
56253208Sandre	union {
57253208Sandre		uint64_t	b64;
58253208Sandre		uint8_t		b8[8];
59253208Sandre	} buf;
60253208Sandre	uint64_t	bytes;
61253208Sandre	uint8_t		buflen;
62253208Sandre	uint8_t		rounds_compr;
63253208Sandre	uint8_t		rounds_final;
64253208Sandre	uint8_t		initialized;
65253208Sandre} SIPHASH_CTX;
66253208Sandre
67253208Sandre
68253208Sandre#define SipHash24_Init(x)	SipHash_InitX((x), 2, 4)
69253208Sandre#define SipHash48_Init(x)	SipHash_InitX((x), 4, 8)
70253208Sandrevoid SipHash_InitX(SIPHASH_CTX *, int, int);
71253208Sandrevoid SipHash_SetKey(SIPHASH_CTX *, const uint8_t [16]);
72253208Sandrevoid SipHash_Update(SIPHASH_CTX *, const void *, size_t);
73253208Sandrevoid SipHash_Final(void *, SIPHASH_CTX *);
74253208Sandreuint64_t SipHash_End(SIPHASH_CTX *);
75253208Sandre
76253208Sandre#define SipHash24(x, y, z, i)	SipHashX((x), 2, 4, (y), (z), (i));
77253208Sandre#define SipHash48(x, y, z, i)	SipHashX((x), 4, 8, (y), (z), (i));
78253208Sandreuint64_t SipHashX(SIPHASH_CTX *, int, int, const uint8_t [16], const void *,
79253208Sandre    size_t);
80253208Sandre
81253208Sandreint SipHash24_TestVectors(void);
82253208Sandre
83253208Sandre#endif /* _SIPHASH_H_ */
84