12c956a60SJason A. Donenfeld /* Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. 22c956a60SJason A. Donenfeld * 32c956a60SJason A. Donenfeld * This file is provided under a dual BSD/GPLv2 license. 42c956a60SJason A. Donenfeld * 52c956a60SJason A. Donenfeld * SipHash: a fast short-input PRF 62c956a60SJason A. Donenfeld * https://131002.net/siphash/ 72c956a60SJason A. Donenfeld * 8*1ae2324fSJason A. Donenfeld * This implementation is specifically for SipHash2-4 for a secure PRF 9*1ae2324fSJason A. Donenfeld * and HalfSipHash1-3/SipHash1-3 for an insecure PRF only suitable for 10*1ae2324fSJason A. Donenfeld * hashtables. 112c956a60SJason A. Donenfeld */ 122c956a60SJason A. Donenfeld 132c956a60SJason A. Donenfeld #ifndef _LINUX_SIPHASH_H 142c956a60SJason A. Donenfeld #define _LINUX_SIPHASH_H 152c956a60SJason A. Donenfeld 162c956a60SJason A. Donenfeld #include <linux/types.h> 172c956a60SJason A. Donenfeld #include <linux/kernel.h> 182c956a60SJason A. Donenfeld 192c956a60SJason A. Donenfeld #define SIPHASH_ALIGNMENT __alignof__(u64) 202c956a60SJason A. Donenfeld typedef struct { 212c956a60SJason A. Donenfeld u64 key[2]; 222c956a60SJason A. Donenfeld } siphash_key_t; 232c956a60SJason A. Donenfeld 242c956a60SJason A. Donenfeld u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key); 252c956a60SJason A. Donenfeld #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 262c956a60SJason A. Donenfeld u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key); 272c956a60SJason A. Donenfeld #endif 282c956a60SJason A. Donenfeld 292c956a60SJason A. Donenfeld u64 siphash_1u64(const u64 a, const siphash_key_t *key); 302c956a60SJason A. Donenfeld u64 siphash_2u64(const u64 a, const u64 b, const siphash_key_t *key); 312c956a60SJason A. Donenfeld u64 siphash_3u64(const u64 a, const u64 b, const u64 c, 322c956a60SJason A. Donenfeld const siphash_key_t *key); 332c956a60SJason A. Donenfeld u64 siphash_4u64(const u64 a, const u64 b, const u64 c, const u64 d, 342c956a60SJason A. Donenfeld const siphash_key_t *key); 352c956a60SJason A. Donenfeld u64 siphash_1u32(const u32 a, const siphash_key_t *key); 362c956a60SJason A. Donenfeld u64 siphash_3u32(const u32 a, const u32 b, const u32 c, 372c956a60SJason A. Donenfeld const siphash_key_t *key); 382c956a60SJason A. Donenfeld 392c956a60SJason A. Donenfeld static inline u64 siphash_2u32(const u32 a, const u32 b, 402c956a60SJason A. Donenfeld const siphash_key_t *key) 412c956a60SJason A. Donenfeld { 422c956a60SJason A. Donenfeld return siphash_1u64((u64)b << 32 | a, key); 432c956a60SJason A. Donenfeld } 442c956a60SJason A. Donenfeld static inline u64 siphash_4u32(const u32 a, const u32 b, const u32 c, 452c956a60SJason A. Donenfeld const u32 d, const siphash_key_t *key) 462c956a60SJason A. Donenfeld { 472c956a60SJason A. Donenfeld return siphash_2u64((u64)b << 32 | a, (u64)d << 32 | c, key); 482c956a60SJason A. Donenfeld } 492c956a60SJason A. Donenfeld 502c956a60SJason A. Donenfeld 512c956a60SJason A. Donenfeld static inline u64 ___siphash_aligned(const __le64 *data, size_t len, 522c956a60SJason A. Donenfeld const siphash_key_t *key) 532c956a60SJason A. Donenfeld { 542c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 4) 552c956a60SJason A. Donenfeld return siphash_1u32(le32_to_cpup((const __le32 *)data), key); 562c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 8) 572c956a60SJason A. Donenfeld return siphash_1u64(le64_to_cpu(data[0]), key); 582c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 16) 592c956a60SJason A. Donenfeld return siphash_2u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), 602c956a60SJason A. Donenfeld key); 612c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 24) 622c956a60SJason A. Donenfeld return siphash_3u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), 632c956a60SJason A. Donenfeld le64_to_cpu(data[2]), key); 642c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 32) 652c956a60SJason A. Donenfeld return siphash_4u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), 662c956a60SJason A. Donenfeld le64_to_cpu(data[2]), le64_to_cpu(data[3]), 672c956a60SJason A. Donenfeld key); 682c956a60SJason A. Donenfeld return __siphash_aligned(data, len, key); 692c956a60SJason A. Donenfeld } 702c956a60SJason A. Donenfeld 712c956a60SJason A. Donenfeld /** 722c956a60SJason A. Donenfeld * siphash - compute 64-bit siphash PRF value 732c956a60SJason A. Donenfeld * @data: buffer to hash 742c956a60SJason A. Donenfeld * @size: size of @data 752c956a60SJason A. Donenfeld * @key: the siphash key 762c956a60SJason A. Donenfeld */ 772c956a60SJason A. Donenfeld static inline u64 siphash(const void *data, size_t len, 782c956a60SJason A. Donenfeld const siphash_key_t *key) 792c956a60SJason A. Donenfeld { 802c956a60SJason A. Donenfeld #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 812c956a60SJason A. Donenfeld if (!IS_ALIGNED((unsigned long)data, SIPHASH_ALIGNMENT)) 822c956a60SJason A. Donenfeld return __siphash_unaligned(data, len, key); 832c956a60SJason A. Donenfeld #endif 842c956a60SJason A. Donenfeld return ___siphash_aligned(data, len, key); 852c956a60SJason A. Donenfeld } 862c956a60SJason A. Donenfeld 87*1ae2324fSJason A. Donenfeld #define HSIPHASH_ALIGNMENT __alignof__(unsigned long) 88*1ae2324fSJason A. Donenfeld typedef struct { 89*1ae2324fSJason A. Donenfeld unsigned long key[2]; 90*1ae2324fSJason A. Donenfeld } hsiphash_key_t; 91*1ae2324fSJason A. Donenfeld 92*1ae2324fSJason A. Donenfeld u32 __hsiphash_aligned(const void *data, size_t len, 93*1ae2324fSJason A. Donenfeld const hsiphash_key_t *key); 94*1ae2324fSJason A. Donenfeld #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 95*1ae2324fSJason A. Donenfeld u32 __hsiphash_unaligned(const void *data, size_t len, 96*1ae2324fSJason A. Donenfeld const hsiphash_key_t *key); 97*1ae2324fSJason A. Donenfeld #endif 98*1ae2324fSJason A. Donenfeld 99*1ae2324fSJason A. Donenfeld u32 hsiphash_1u32(const u32 a, const hsiphash_key_t *key); 100*1ae2324fSJason A. Donenfeld u32 hsiphash_2u32(const u32 a, const u32 b, const hsiphash_key_t *key); 101*1ae2324fSJason A. Donenfeld u32 hsiphash_3u32(const u32 a, const u32 b, const u32 c, 102*1ae2324fSJason A. Donenfeld const hsiphash_key_t *key); 103*1ae2324fSJason A. Donenfeld u32 hsiphash_4u32(const u32 a, const u32 b, const u32 c, const u32 d, 104*1ae2324fSJason A. Donenfeld const hsiphash_key_t *key); 105*1ae2324fSJason A. Donenfeld 106*1ae2324fSJason A. Donenfeld static inline u32 ___hsiphash_aligned(const __le32 *data, size_t len, 107*1ae2324fSJason A. Donenfeld const hsiphash_key_t *key) 108*1ae2324fSJason A. Donenfeld { 109*1ae2324fSJason A. Donenfeld if (__builtin_constant_p(len) && len == 4) 110*1ae2324fSJason A. Donenfeld return hsiphash_1u32(le32_to_cpu(data[0]), key); 111*1ae2324fSJason A. Donenfeld if (__builtin_constant_p(len) && len == 8) 112*1ae2324fSJason A. Donenfeld return hsiphash_2u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]), 113*1ae2324fSJason A. Donenfeld key); 114*1ae2324fSJason A. Donenfeld if (__builtin_constant_p(len) && len == 12) 115*1ae2324fSJason A. Donenfeld return hsiphash_3u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]), 116*1ae2324fSJason A. Donenfeld le32_to_cpu(data[2]), key); 117*1ae2324fSJason A. Donenfeld if (__builtin_constant_p(len) && len == 16) 118*1ae2324fSJason A. Donenfeld return hsiphash_4u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]), 119*1ae2324fSJason A. Donenfeld le32_to_cpu(data[2]), le32_to_cpu(data[3]), 120*1ae2324fSJason A. Donenfeld key); 121*1ae2324fSJason A. Donenfeld return __hsiphash_aligned(data, len, key); 122*1ae2324fSJason A. Donenfeld } 123*1ae2324fSJason A. Donenfeld 124*1ae2324fSJason A. Donenfeld /** 125*1ae2324fSJason A. Donenfeld * hsiphash - compute 32-bit hsiphash PRF value 126*1ae2324fSJason A. Donenfeld * @data: buffer to hash 127*1ae2324fSJason A. Donenfeld * @size: size of @data 128*1ae2324fSJason A. Donenfeld * @key: the hsiphash key 129*1ae2324fSJason A. Donenfeld */ 130*1ae2324fSJason A. Donenfeld static inline u32 hsiphash(const void *data, size_t len, 131*1ae2324fSJason A. Donenfeld const hsiphash_key_t *key) 132*1ae2324fSJason A. Donenfeld { 133*1ae2324fSJason A. Donenfeld #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS 134*1ae2324fSJason A. Donenfeld if (!IS_ALIGNED((unsigned long)data, HSIPHASH_ALIGNMENT)) 135*1ae2324fSJason A. Donenfeld return __hsiphash_unaligned(data, len, key); 136*1ae2324fSJason A. Donenfeld #endif 137*1ae2324fSJason A. Donenfeld return ___hsiphash_aligned(data, len, key); 138*1ae2324fSJason A. Donenfeld } 139*1ae2324fSJason A. Donenfeld 1402c956a60SJason A. Donenfeld #endif /* _LINUX_SIPHASH_H */ 141