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 * $FreeBSD$ 306856398eSAndre Oppermann */ 316856398eSAndre Oppermann 326856398eSAndre Oppermann /* 336856398eSAndre Oppermann * SipHash is a family of pseudorandom functions (a.k.a. keyed hash functions) 346856398eSAndre Oppermann * optimized for speed on short messages returning a 64bit hash/digest value. 356856398eSAndre Oppermann * 366856398eSAndre Oppermann * The number of rounds is defined during the initialization: 376856398eSAndre Oppermann * SipHash24_Init() for the fast and resonable strong version 386856398eSAndre Oppermann * SipHash48_Init() for the strong version (half as fast) 396856398eSAndre Oppermann * 406856398eSAndre Oppermann * struct SIPHASH_CTX ctx; 416856398eSAndre Oppermann * SipHash24_Init(&ctx); 426856398eSAndre Oppermann * SipHash_SetKey(&ctx, "16bytes long key"); 436856398eSAndre Oppermann * SipHash_Update(&ctx, pointer_to_string, length_of_string); 446856398eSAndre Oppermann * SipHash_Final(output, &ctx); 456856398eSAndre Oppermann */ 466856398eSAndre Oppermann 476856398eSAndre Oppermann #ifndef _SIPHASH_H_ 486856398eSAndre Oppermann #define _SIPHASH_H_ 496856398eSAndre Oppermann 506856398eSAndre Oppermann #define SIPHASH_BLOCK_LENGTH 8 516856398eSAndre Oppermann #define SIPHASH_KEY_LENGTH 16 526856398eSAndre Oppermann #define SIPHASH_DIGEST_LENGTH 8 536856398eSAndre Oppermann 546856398eSAndre Oppermann typedef struct _SIPHASH_CTX { 556856398eSAndre Oppermann uint64_t v[4]; 566856398eSAndre Oppermann union { 576856398eSAndre Oppermann uint64_t b64; 586856398eSAndre Oppermann uint8_t b8[8]; 596856398eSAndre Oppermann } buf; 606856398eSAndre Oppermann uint64_t bytes; 616856398eSAndre Oppermann uint8_t buflen; 626856398eSAndre Oppermann uint8_t rounds_compr; 636856398eSAndre Oppermann uint8_t rounds_final; 646856398eSAndre Oppermann uint8_t initialized; 656856398eSAndre Oppermann } SIPHASH_CTX; 666856398eSAndre Oppermann 676856398eSAndre Oppermann 686856398eSAndre Oppermann #define SipHash24_Init(x) SipHash_InitX((x), 2, 4) 696856398eSAndre Oppermann #define SipHash48_Init(x) SipHash_InitX((x), 4, 8) 706856398eSAndre Oppermann void SipHash_InitX(SIPHASH_CTX *, int, int); 71*8254c3c5SAlan Somers void SipHash_SetKey(SIPHASH_CTX *, 72*8254c3c5SAlan Somers const uint8_t[__min_size(SIPHASH_KEY_LENGTH)]); 736856398eSAndre Oppermann void SipHash_Update(SIPHASH_CTX *, const void *, size_t); 74*8254c3c5SAlan Somers void SipHash_Final(uint8_t[__min_size(SIPHASH_DIGEST_LENGTH)], SIPHASH_CTX *); 756856398eSAndre Oppermann uint64_t SipHash_End(SIPHASH_CTX *); 766856398eSAndre Oppermann 776856398eSAndre Oppermann #define SipHash24(x, y, z, i) SipHashX((x), 2, 4, (y), (z), (i)); 786856398eSAndre Oppermann #define SipHash48(x, y, z, i) SipHashX((x), 4, 8, (y), (z), (i)); 79*8254c3c5SAlan Somers uint64_t SipHashX(SIPHASH_CTX *, int, int, 80*8254c3c5SAlan Somers const uint8_t[__min_size(SIPHASH_KEY_LENGTH)], const void *, size_t); 816856398eSAndre Oppermann 826856398eSAndre Oppermann int SipHash24_TestVectors(void); 836856398eSAndre Oppermann 846856398eSAndre Oppermann #endif /* _SIPHASH_H_ */ 85