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