1*6856398eSAndre Oppermann /*- 2*6856398eSAndre Oppermann * Copyright (c) 2013 Andre Oppermann <andre@FreeBSD.org> 3*6856398eSAndre Oppermann * All rights reserved. 4*6856398eSAndre Oppermann * 5*6856398eSAndre Oppermann * Redistribution and use in source and binary forms, with or without 6*6856398eSAndre Oppermann * modification, are permitted provided that the following conditions 7*6856398eSAndre Oppermann * are met: 8*6856398eSAndre Oppermann * 1. Redistributions of source code must retain the above copyright 9*6856398eSAndre Oppermann * notice, this list of conditions and the following disclaimer. 10*6856398eSAndre Oppermann * 2. Redistributions in binary form must reproduce the above copyright 11*6856398eSAndre Oppermann * notice, this list of conditions and the following disclaimer in the 12*6856398eSAndre Oppermann * documentation and/or other materials provided with the distribution. 13*6856398eSAndre Oppermann * 3. The name of the author may not be used to endorse or promote 14*6856398eSAndre Oppermann * products derived from this software without specific prior written 15*6856398eSAndre Oppermann * permission. 16*6856398eSAndre Oppermann * 17*6856398eSAndre Oppermann * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18*6856398eSAndre Oppermann * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19*6856398eSAndre Oppermann * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20*6856398eSAndre Oppermann * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21*6856398eSAndre Oppermann * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22*6856398eSAndre Oppermann * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23*6856398eSAndre Oppermann * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24*6856398eSAndre Oppermann * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25*6856398eSAndre Oppermann * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26*6856398eSAndre Oppermann * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27*6856398eSAndre Oppermann * SUCH DAMAGE. 28*6856398eSAndre Oppermann * 29*6856398eSAndre Oppermann * $FreeBSD$ 30*6856398eSAndre Oppermann */ 31*6856398eSAndre Oppermann 32*6856398eSAndre Oppermann /* 33*6856398eSAndre Oppermann * SipHash is a family of pseudorandom functions (a.k.a. keyed hash functions) 34*6856398eSAndre Oppermann * optimized for speed on short messages returning a 64bit hash/digest value. 35*6856398eSAndre Oppermann * 36*6856398eSAndre Oppermann * The number of rounds is defined during the initialization: 37*6856398eSAndre Oppermann * SipHash24_Init() for the fast and resonable strong version 38*6856398eSAndre Oppermann * SipHash48_Init() for the strong version (half as fast) 39*6856398eSAndre Oppermann * 40*6856398eSAndre Oppermann * struct SIPHASH_CTX ctx; 41*6856398eSAndre Oppermann * SipHash24_Init(&ctx); 42*6856398eSAndre Oppermann * SipHash_SetKey(&ctx, "16bytes long key"); 43*6856398eSAndre Oppermann * SipHash_Update(&ctx, pointer_to_string, length_of_string); 44*6856398eSAndre Oppermann * SipHash_Final(output, &ctx); 45*6856398eSAndre Oppermann */ 46*6856398eSAndre Oppermann 47*6856398eSAndre Oppermann #ifndef _SIPHASH_H_ 48*6856398eSAndre Oppermann #define _SIPHASH_H_ 49*6856398eSAndre Oppermann 50*6856398eSAndre Oppermann #define SIPHASH_BLOCK_LENGTH 8 51*6856398eSAndre Oppermann #define SIPHASH_KEY_LENGTH 16 52*6856398eSAndre Oppermann #define SIPHASH_DIGEST_LENGTH 8 53*6856398eSAndre Oppermann 54*6856398eSAndre Oppermann typedef struct _SIPHASH_CTX { 55*6856398eSAndre Oppermann uint64_t v[4]; 56*6856398eSAndre Oppermann union { 57*6856398eSAndre Oppermann uint64_t b64; 58*6856398eSAndre Oppermann uint8_t b8[8]; 59*6856398eSAndre Oppermann } buf; 60*6856398eSAndre Oppermann uint64_t bytes; 61*6856398eSAndre Oppermann uint8_t buflen; 62*6856398eSAndre Oppermann uint8_t rounds_compr; 63*6856398eSAndre Oppermann uint8_t rounds_final; 64*6856398eSAndre Oppermann uint8_t initialized; 65*6856398eSAndre Oppermann } SIPHASH_CTX; 66*6856398eSAndre Oppermann 67*6856398eSAndre Oppermann 68*6856398eSAndre Oppermann #define SipHash24_Init(x) SipHash_InitX((x), 2, 4) 69*6856398eSAndre Oppermann #define SipHash48_Init(x) SipHash_InitX((x), 4, 8) 70*6856398eSAndre Oppermann void SipHash_InitX(SIPHASH_CTX *, int, int); 71*6856398eSAndre Oppermann void SipHash_SetKey(SIPHASH_CTX *, const uint8_t [16]); 72*6856398eSAndre Oppermann void SipHash_Update(SIPHASH_CTX *, const void *, size_t); 73*6856398eSAndre Oppermann void SipHash_Final(void *, SIPHASH_CTX *); 74*6856398eSAndre Oppermann uint64_t SipHash_End(SIPHASH_CTX *); 75*6856398eSAndre Oppermann 76*6856398eSAndre Oppermann #define SipHash24(x, y, z, i) SipHashX((x), 2, 4, (y), (z), (i)); 77*6856398eSAndre Oppermann #define SipHash48(x, y, z, i) SipHashX((x), 4, 8, (y), (z), (i)); 78*6856398eSAndre Oppermann uint64_t SipHashX(SIPHASH_CTX *, int, int, const uint8_t [16], const void *, 79*6856398eSAndre Oppermann size_t); 80*6856398eSAndre Oppermann 81*6856398eSAndre Oppermann int SipHash24_TestVectors(void); 82*6856398eSAndre Oppermann 83*6856398eSAndre Oppermann #endif /* _SIPHASH_H_ */ 84