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