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 * 81ae2324fSJason A. Donenfeld * This implementation is specifically for SipHash2-4 for a secure PRF 91ae2324fSJason A. Donenfeld * and HalfSipHash1-3/SipHash1-3 for an insecure PRF only suitable for 101ae2324fSJason 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 2449ecc2e9SEric Dumazet #define siphash_aligned_key_t siphash_key_t __aligned(16) 2549ecc2e9SEric Dumazet 26df453700SEric Dumazet static inline bool siphash_key_is_zero(const siphash_key_t *key) 27df453700SEric Dumazet { 28df453700SEric Dumazet return !(key->key[0] | key->key[1]); 29df453700SEric Dumazet } 30df453700SEric Dumazet 312c956a60SJason A. Donenfeld u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key); 322c956a60SJason A. Donenfeld u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key); 332c956a60SJason A. Donenfeld 342c956a60SJason A. Donenfeld u64 siphash_1u64(const u64 a, const siphash_key_t *key); 352c956a60SJason A. Donenfeld u64 siphash_2u64(const u64 a, const u64 b, const siphash_key_t *key); 362c956a60SJason A. Donenfeld u64 siphash_3u64(const u64 a, const u64 b, const u64 c, 372c956a60SJason A. Donenfeld const siphash_key_t *key); 382c956a60SJason A. Donenfeld u64 siphash_4u64(const u64 a, const u64 b, const u64 c, const u64 d, 392c956a60SJason A. Donenfeld const siphash_key_t *key); 402c956a60SJason A. Donenfeld u64 siphash_1u32(const u32 a, const siphash_key_t *key); 412c956a60SJason A. Donenfeld u64 siphash_3u32(const u32 a, const u32 b, const u32 c, 422c956a60SJason A. Donenfeld const siphash_key_t *key); 432c956a60SJason A. Donenfeld 442c956a60SJason A. Donenfeld static inline u64 siphash_2u32(const u32 a, const u32 b, 452c956a60SJason A. Donenfeld const siphash_key_t *key) 462c956a60SJason A. Donenfeld { 472c956a60SJason A. Donenfeld return siphash_1u64((u64)b << 32 | a, key); 482c956a60SJason A. Donenfeld } 492c956a60SJason A. Donenfeld static inline u64 siphash_4u32(const u32 a, const u32 b, const u32 c, 502c956a60SJason A. Donenfeld const u32 d, const siphash_key_t *key) 512c956a60SJason A. Donenfeld { 522c956a60SJason A. Donenfeld return siphash_2u64((u64)b << 32 | a, (u64)d << 32 | c, key); 532c956a60SJason A. Donenfeld } 542c956a60SJason A. Donenfeld 552c956a60SJason A. Donenfeld 562c956a60SJason A. Donenfeld static inline u64 ___siphash_aligned(const __le64 *data, size_t len, 572c956a60SJason A. Donenfeld const siphash_key_t *key) 582c956a60SJason A. Donenfeld { 592c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 4) 602c956a60SJason A. Donenfeld return siphash_1u32(le32_to_cpup((const __le32 *)data), key); 612c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 8) 622c956a60SJason A. Donenfeld return siphash_1u64(le64_to_cpu(data[0]), key); 632c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 16) 642c956a60SJason A. Donenfeld return siphash_2u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), 652c956a60SJason A. Donenfeld key); 662c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 24) 672c956a60SJason A. Donenfeld return siphash_3u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), 682c956a60SJason A. Donenfeld le64_to_cpu(data[2]), key); 692c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 32) 702c956a60SJason A. Donenfeld return siphash_4u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), 712c956a60SJason A. Donenfeld le64_to_cpu(data[2]), le64_to_cpu(data[3]), 722c956a60SJason A. Donenfeld key); 732c956a60SJason A. Donenfeld return __siphash_aligned(data, len, key); 742c956a60SJason A. Donenfeld } 752c956a60SJason A. Donenfeld 762c956a60SJason A. Donenfeld /** 772c956a60SJason A. Donenfeld * siphash - compute 64-bit siphash PRF value 782c956a60SJason A. Donenfeld * @data: buffer to hash 792c956a60SJason A. Donenfeld * @size: size of @data 802c956a60SJason A. Donenfeld * @key: the siphash key 812c956a60SJason A. Donenfeld */ 822c956a60SJason A. Donenfeld static inline u64 siphash(const void *data, size_t len, 832c956a60SJason A. Donenfeld const siphash_key_t *key) 842c956a60SJason A. Donenfeld { 85f7e5b9bfSArnd Bergmann if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) || 86f7e5b9bfSArnd Bergmann !IS_ALIGNED((unsigned long)data, SIPHASH_ALIGNMENT)) 872c956a60SJason A. Donenfeld return __siphash_unaligned(data, len, key); 882c956a60SJason A. Donenfeld return ___siphash_aligned(data, len, key); 892c956a60SJason A. Donenfeld } 902c956a60SJason A. Donenfeld 911ae2324fSJason A. Donenfeld #define HSIPHASH_ALIGNMENT __alignof__(unsigned long) 921ae2324fSJason A. Donenfeld typedef struct { 931ae2324fSJason A. Donenfeld unsigned long key[2]; 941ae2324fSJason A. Donenfeld } hsiphash_key_t; 951ae2324fSJason A. Donenfeld 961ae2324fSJason A. Donenfeld u32 __hsiphash_aligned(const void *data, size_t len, 971ae2324fSJason A. Donenfeld const hsiphash_key_t *key); 981ae2324fSJason A. Donenfeld u32 __hsiphash_unaligned(const void *data, size_t len, 991ae2324fSJason A. Donenfeld const hsiphash_key_t *key); 1001ae2324fSJason A. Donenfeld 1011ae2324fSJason A. Donenfeld u32 hsiphash_1u32(const u32 a, const hsiphash_key_t *key); 1021ae2324fSJason A. Donenfeld u32 hsiphash_2u32(const u32 a, const u32 b, const hsiphash_key_t *key); 1031ae2324fSJason A. Donenfeld u32 hsiphash_3u32(const u32 a, const u32 b, const u32 c, 1041ae2324fSJason A. Donenfeld const hsiphash_key_t *key); 1051ae2324fSJason A. Donenfeld u32 hsiphash_4u32(const u32 a, const u32 b, const u32 c, const u32 d, 1061ae2324fSJason A. Donenfeld const hsiphash_key_t *key); 1071ae2324fSJason A. Donenfeld 1081ae2324fSJason A. Donenfeld static inline u32 ___hsiphash_aligned(const __le32 *data, size_t len, 1091ae2324fSJason A. Donenfeld const hsiphash_key_t *key) 1101ae2324fSJason A. Donenfeld { 1111ae2324fSJason A. Donenfeld if (__builtin_constant_p(len) && len == 4) 1121ae2324fSJason A. Donenfeld return hsiphash_1u32(le32_to_cpu(data[0]), key); 1131ae2324fSJason A. Donenfeld if (__builtin_constant_p(len) && len == 8) 1141ae2324fSJason A. Donenfeld return hsiphash_2u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]), 1151ae2324fSJason A. Donenfeld key); 1161ae2324fSJason A. Donenfeld if (__builtin_constant_p(len) && len == 12) 1171ae2324fSJason A. Donenfeld return hsiphash_3u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]), 1181ae2324fSJason A. Donenfeld le32_to_cpu(data[2]), key); 1191ae2324fSJason A. Donenfeld if (__builtin_constant_p(len) && len == 16) 1201ae2324fSJason A. Donenfeld return hsiphash_4u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]), 1211ae2324fSJason A. Donenfeld le32_to_cpu(data[2]), le32_to_cpu(data[3]), 1221ae2324fSJason A. Donenfeld key); 1231ae2324fSJason A. Donenfeld return __hsiphash_aligned(data, len, key); 1241ae2324fSJason A. Donenfeld } 1251ae2324fSJason A. Donenfeld 1261ae2324fSJason A. Donenfeld /** 1271ae2324fSJason A. Donenfeld * hsiphash - compute 32-bit hsiphash PRF value 1281ae2324fSJason A. Donenfeld * @data: buffer to hash 1291ae2324fSJason A. Donenfeld * @size: size of @data 1301ae2324fSJason A. Donenfeld * @key: the hsiphash key 1311ae2324fSJason A. Donenfeld */ 1321ae2324fSJason A. Donenfeld static inline u32 hsiphash(const void *data, size_t len, 1331ae2324fSJason A. Donenfeld const hsiphash_key_t *key) 1341ae2324fSJason A. Donenfeld { 135f7e5b9bfSArnd Bergmann if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) || 136f7e5b9bfSArnd Bergmann !IS_ALIGNED((unsigned long)data, HSIPHASH_ALIGNMENT)) 1371ae2324fSJason A. Donenfeld return __hsiphash_unaligned(data, len, key); 1381ae2324fSJason A. Donenfeld return ___hsiphash_aligned(data, len, key); 1391ae2324fSJason A. Donenfeld } 1401ae2324fSJason A. Donenfeld 141*e73aaae2SJason A. Donenfeld /* 142*e73aaae2SJason A. Donenfeld * These macros expose the raw SipHash and HalfSipHash permutations. 143*e73aaae2SJason A. Donenfeld * Do not use them directly! If you think you have a use for them, 144*e73aaae2SJason A. Donenfeld * be sure to CC the maintainer of this file explaining why. 145*e73aaae2SJason A. Donenfeld */ 146*e73aaae2SJason A. Donenfeld 147*e73aaae2SJason A. Donenfeld #define SIPHASH_PERMUTATION(a, b, c, d) ( \ 148*e73aaae2SJason A. Donenfeld (a) += (b), (b) = rol64((b), 13), (b) ^= (a), (a) = rol64((a), 32), \ 149*e73aaae2SJason A. Donenfeld (c) += (d), (d) = rol64((d), 16), (d) ^= (c), \ 150*e73aaae2SJason A. Donenfeld (a) += (d), (d) = rol64((d), 21), (d) ^= (a), \ 151*e73aaae2SJason A. Donenfeld (c) += (b), (b) = rol64((b), 17), (b) ^= (c), (c) = rol64((c), 32)) 152*e73aaae2SJason A. Donenfeld 153*e73aaae2SJason A. Donenfeld #define SIPHASH_CONST_0 0x736f6d6570736575ULL 154*e73aaae2SJason A. Donenfeld #define SIPHASH_CONST_1 0x646f72616e646f6dULL 155*e73aaae2SJason A. Donenfeld #define SIPHASH_CONST_2 0x6c7967656e657261ULL 156*e73aaae2SJason A. Donenfeld #define SIPHASH_CONST_3 0x7465646279746573ULL 157*e73aaae2SJason A. Donenfeld 158*e73aaae2SJason A. Donenfeld #define HSIPHASH_PERMUTATION(a, b, c, d) ( \ 159*e73aaae2SJason A. Donenfeld (a) += (b), (b) = rol32((b), 5), (b) ^= (a), (a) = rol32((a), 16), \ 160*e73aaae2SJason A. Donenfeld (c) += (d), (d) = rol32((d), 8), (d) ^= (c), \ 161*e73aaae2SJason A. Donenfeld (a) += (d), (d) = rol32((d), 7), (d) ^= (a), \ 162*e73aaae2SJason A. Donenfeld (c) += (b), (b) = rol32((b), 13), (b) ^= (c), (c) = rol32((c), 16)) 163*e73aaae2SJason A. Donenfeld 164*e73aaae2SJason A. Donenfeld #define HSIPHASH_CONST_0 0U 165*e73aaae2SJason A. Donenfeld #define HSIPHASH_CONST_1 0U 166*e73aaae2SJason A. Donenfeld #define HSIPHASH_CONST_2 0x6c796765U 167*e73aaae2SJason A. Donenfeld #define HSIPHASH_CONST_3 0x74656462U 168*e73aaae2SJason A. Donenfeld 1692c956a60SJason A. Donenfeld #endif /* _LINUX_SIPHASH_H */ 170