xref: /freebsd/sys/crypto/siphash/siphash.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
16856398eSAndre Oppermann /*-
26856398eSAndre Oppermann  * Copyright (c) 2013 Andre Oppermann <andre@FreeBSD.org>
36856398eSAndre Oppermann  * All rights reserved.
46856398eSAndre Oppermann  *
56856398eSAndre Oppermann  * Redistribution and use in source and binary forms, with or without
66856398eSAndre Oppermann  * modification, are permitted provided that the following conditions
76856398eSAndre Oppermann  * are met:
86856398eSAndre Oppermann  * 1. Redistributions of source code must retain the above copyright
96856398eSAndre Oppermann  *    notice, this list of conditions and the following disclaimer.
106856398eSAndre Oppermann  * 2. Redistributions in binary form must reproduce the above copyright
116856398eSAndre Oppermann  *    notice, this list of conditions and the following disclaimer in the
126856398eSAndre Oppermann  *    documentation and/or other materials provided with the distribution.
136856398eSAndre Oppermann  * 3. The name of the author may not be used to endorse or promote
146856398eSAndre Oppermann  *    products derived from this software without specific prior written
156856398eSAndre Oppermann  *    permission.
166856398eSAndre Oppermann  *
176856398eSAndre Oppermann  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
186856398eSAndre Oppermann  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
196856398eSAndre Oppermann  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
206856398eSAndre Oppermann  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
216856398eSAndre Oppermann  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
226856398eSAndre Oppermann  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
236856398eSAndre Oppermann  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
246856398eSAndre Oppermann  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
256856398eSAndre Oppermann  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
266856398eSAndre Oppermann  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
276856398eSAndre Oppermann  * SUCH DAMAGE.
286856398eSAndre Oppermann  */
296856398eSAndre Oppermann 
306856398eSAndre Oppermann /*
316856398eSAndre Oppermann  * SipHash is a family of pseudorandom functions (a.k.a. keyed hash functions)
326856398eSAndre Oppermann  * optimized for speed on short messages returning a 64bit hash/digest value.
336856398eSAndre Oppermann  *
346856398eSAndre Oppermann  * The number of rounds is defined during the initialization:
356856398eSAndre Oppermann  *  SipHash24_Init() for the fast and resonable strong version
366856398eSAndre Oppermann  *  SipHash48_Init() for the strong version (half as fast)
376856398eSAndre Oppermann  *
386856398eSAndre Oppermann  * struct SIPHASH_CTX ctx;
396856398eSAndre Oppermann  * SipHash24_Init(&ctx);
406856398eSAndre Oppermann  * SipHash_SetKey(&ctx, "16bytes long key");
416856398eSAndre Oppermann  * SipHash_Update(&ctx, pointer_to_string, length_of_string);
426856398eSAndre Oppermann  * SipHash_Final(output, &ctx);
436856398eSAndre Oppermann  */
446856398eSAndre Oppermann 
456856398eSAndre Oppermann #ifndef _SIPHASH_H_
466856398eSAndre Oppermann #define _SIPHASH_H_
476856398eSAndre Oppermann 
486856398eSAndre Oppermann #define SIPHASH_BLOCK_LENGTH	 8
496856398eSAndre Oppermann #define SIPHASH_KEY_LENGTH	16
506856398eSAndre Oppermann #define SIPHASH_DIGEST_LENGTH	 8
516856398eSAndre Oppermann 
526856398eSAndre Oppermann typedef struct _SIPHASH_CTX {
536856398eSAndre Oppermann 	uint64_t	v[4];
546856398eSAndre Oppermann 	union {
556856398eSAndre Oppermann 		uint64_t	b64;
566856398eSAndre Oppermann 		uint8_t		b8[8];
576856398eSAndre Oppermann 	} buf;
586856398eSAndre Oppermann 	uint64_t	bytes;
596856398eSAndre Oppermann 	uint8_t		buflen;
606856398eSAndre Oppermann 	uint8_t		rounds_compr;
616856398eSAndre Oppermann 	uint8_t		rounds_final;
626856398eSAndre Oppermann 	uint8_t		initialized;
636856398eSAndre Oppermann } SIPHASH_CTX;
646856398eSAndre Oppermann 
656856398eSAndre Oppermann 
666856398eSAndre Oppermann #define SipHash24_Init(x)	SipHash_InitX((x), 2, 4)
676856398eSAndre Oppermann #define SipHash48_Init(x)	SipHash_InitX((x), 4, 8)
686856398eSAndre Oppermann void SipHash_InitX(SIPHASH_CTX *, int, int);
69*8254c3c5SAlan Somers void SipHash_SetKey(SIPHASH_CTX *,
70*8254c3c5SAlan Somers     const uint8_t[__min_size(SIPHASH_KEY_LENGTH)]);
716856398eSAndre Oppermann void SipHash_Update(SIPHASH_CTX *, const void *, size_t);
72*8254c3c5SAlan Somers void SipHash_Final(uint8_t[__min_size(SIPHASH_DIGEST_LENGTH)], SIPHASH_CTX *);
736856398eSAndre Oppermann uint64_t SipHash_End(SIPHASH_CTX *);
746856398eSAndre Oppermann 
756856398eSAndre Oppermann #define SipHash24(x, y, z, i)	SipHashX((x), 2, 4, (y), (z), (i));
766856398eSAndre Oppermann #define SipHash48(x, y, z, i)	SipHashX((x), 4, 8, (y), (z), (i));
77*8254c3c5SAlan Somers uint64_t SipHashX(SIPHASH_CTX *, int, int,
78*8254c3c5SAlan Somers     const uint8_t[__min_size(SIPHASH_KEY_LENGTH)], const void *, size_t);
796856398eSAndre Oppermann 
806856398eSAndre Oppermann int SipHash24_TestVectors(void);
816856398eSAndre Oppermann 
826856398eSAndre Oppermann #endif /* _SIPHASH_H_ */
83