xref: /openbsd/include/siphash.h (revision 2811b70e)
180dc78aaStedu /*-
280dc78aaStedu  * Copyright (c) 2013 Andre Oppermann <andre@FreeBSD.org>
380dc78aaStedu  * All rights reserved.
480dc78aaStedu  *
580dc78aaStedu  * Redistribution and use in source and binary forms, with or without
680dc78aaStedu  * modification, are permitted provided that the following conditions
780dc78aaStedu  * are met:
880dc78aaStedu  * 1. Redistributions of source code must retain the above copyright
980dc78aaStedu  *    notice, this list of conditions and the following disclaimer.
1080dc78aaStedu  * 2. Redistributions in binary form must reproduce the above copyright
1180dc78aaStedu  *    notice, this list of conditions and the following disclaimer in the
1280dc78aaStedu  *    documentation and/or other materials provided with the distribution.
1380dc78aaStedu  * 3. The name of the author may not be used to endorse or promote
1480dc78aaStedu  *    products derived from this software without specific prior written
1580dc78aaStedu  *    permission.
1680dc78aaStedu  *
1780dc78aaStedu  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1880dc78aaStedu  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1980dc78aaStedu  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2080dc78aaStedu  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2180dc78aaStedu  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2280dc78aaStedu  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2380dc78aaStedu  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2480dc78aaStedu  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2580dc78aaStedu  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2680dc78aaStedu  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2780dc78aaStedu  * SUCH DAMAGE.
2880dc78aaStedu  *
29*2811b70eSjmc  * $OpenBSD: siphash.h,v 1.4 2022/12/27 07:44:56 jmc Exp $
3080dc78aaStedu  */
3180dc78aaStedu 
3280dc78aaStedu /*
3380dc78aaStedu  * SipHash is a family of pseudorandom functions (a.k.a. keyed hash functions)
3480dc78aaStedu  * optimized for speed on short messages returning a 64bit hash/digest value.
3580dc78aaStedu  *
3680dc78aaStedu  * The number of rounds is defined during the initialization:
37*2811b70eSjmc  *  SipHash24_Init() for the fast and reasonable strong version
3880dc78aaStedu  *  SipHash48_Init() for the strong version (half as fast)
3980dc78aaStedu  *
4080dc78aaStedu  * struct SIPHASH_CTX ctx;
4180dc78aaStedu  * SipHash24_Init(&ctx);
4280dc78aaStedu  * SipHash_SetKey(&ctx, "16bytes long key");
4380dc78aaStedu  * SipHash_Update(&ctx, pointer_to_string, length_of_string);
4480dc78aaStedu  * SipHash_Final(output, &ctx);
4580dc78aaStedu  */
4680dc78aaStedu 
4780dc78aaStedu #ifndef _SIPHASH_H_
4880dc78aaStedu #define _SIPHASH_H_
4980dc78aaStedu 
5080dc78aaStedu #define SIPHASH_BLOCK_LENGTH	 8
5180dc78aaStedu #define SIPHASH_KEY_LENGTH	16
5280dc78aaStedu #define SIPHASH_DIGEST_LENGTH	 8
5380dc78aaStedu 
5480dc78aaStedu typedef struct _SIPHASH_CTX {
55b8a04706Stedu 	uint64_t	v[4];
56b8a04706Stedu 	uint8_t		buf[SIPHASH_BLOCK_LENGTH];
57b8a04706Stedu 	uint32_t	bytes;
5880dc78aaStedu } SIPHASH_CTX;
5980dc78aaStedu 
6080dc78aaStedu typedef struct {
61b8a04706Stedu 	uint64_t	k0;
62b8a04706Stedu 	uint64_t	k1;
6380dc78aaStedu } SIPHASH_KEY;
6480dc78aaStedu 
6580dc78aaStedu void		SipHash_Init(SIPHASH_CTX *, const SIPHASH_KEY *);
6680dc78aaStedu void		SipHash_Update(SIPHASH_CTX *, int, int, const void *, size_t);
67b8a04706Stedu uint64_t	SipHash_End(SIPHASH_CTX *, int, int);
6880dc78aaStedu void		SipHash_Final(void *, SIPHASH_CTX *, int, int);
69b8a04706Stedu uint64_t	SipHash(const SIPHASH_KEY *, int, int, const void *, size_t);
7080dc78aaStedu 
7180dc78aaStedu #define SipHash24_Init(_c, _k)		SipHash_Init((_c), (_k))
7280dc78aaStedu #define SipHash24_Update(_c, _p, _l)	SipHash_Update((_c), 2, 4, (_p), (_l))
7380dc78aaStedu #define SipHash24_End(_d)		SipHash_End((_d), 2, 4)
7480dc78aaStedu #define SipHash24_Final(_d, _c)		SipHash_Final((_d), (_c), 2, 4)
7580dc78aaStedu #define SipHash24(_k, _p, _l)		SipHash((_k), 2, 4, (_p), (_l))
7680dc78aaStedu 
7780dc78aaStedu #define SipHash48_Init(_c, _k)		SipHash_Init((_c), (_k))
7880dc78aaStedu #define SipHash48_Update(_c, _p, _l)	SipHash_Update((_c), 4, 8, (_p), (_l))
7980dc78aaStedu #define SipHash48_End(_d)		SipHash_End((_d), 4, 8)
8080dc78aaStedu #define SipHash48_Final(_d, _c)		SipHash_Final((_d), (_c), 4, 8)
8180dc78aaStedu #define SipHash48(_k, _p, _l)		SipHash((_k), 4, 8, (_p), (_l))
8280dc78aaStedu 
8380dc78aaStedu #endif /* _SIPHASH_H_ */
84