1*53c83d6dSJason A. Donenfeld /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */ 2*53c83d6dSJason A. Donenfeld /* Copyright (C) 2016-2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. 32c956a60SJason A. Donenfeld * 42c956a60SJason A. Donenfeld * SipHash: a fast short-input PRF 52c956a60SJason A. Donenfeld * https://131002.net/siphash/ 62c956a60SJason A. Donenfeld * 71ae2324fSJason A. Donenfeld * This implementation is specifically for SipHash2-4 for a secure PRF 81ae2324fSJason A. Donenfeld * and HalfSipHash1-3/SipHash1-3 for an insecure PRF only suitable for 91ae2324fSJason A. Donenfeld * hashtables. 102c956a60SJason A. Donenfeld */ 112c956a60SJason A. Donenfeld 122c956a60SJason A. Donenfeld #ifndef _LINUX_SIPHASH_H 132c956a60SJason A. Donenfeld #define _LINUX_SIPHASH_H 142c956a60SJason A. Donenfeld 152c956a60SJason A. Donenfeld #include <linux/types.h> 162c956a60SJason A. Donenfeld #include <linux/kernel.h> 172c956a60SJason A. Donenfeld 182c956a60SJason A. Donenfeld #define SIPHASH_ALIGNMENT __alignof__(u64) 192c956a60SJason A. Donenfeld typedef struct { 202c956a60SJason A. Donenfeld u64 key[2]; 212c956a60SJason A. Donenfeld } siphash_key_t; 222c956a60SJason A. Donenfeld 2349ecc2e9SEric Dumazet #define siphash_aligned_key_t siphash_key_t __aligned(16) 2449ecc2e9SEric Dumazet 25df453700SEric Dumazet static inline bool siphash_key_is_zero(const siphash_key_t *key) 26df453700SEric Dumazet { 27df453700SEric Dumazet return !(key->key[0] | key->key[1]); 28df453700SEric Dumazet } 29df453700SEric Dumazet 302c956a60SJason A. Donenfeld u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key); 312c956a60SJason A. Donenfeld u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key); 322c956a60SJason A. Donenfeld 332c956a60SJason A. Donenfeld u64 siphash_1u64(const u64 a, const siphash_key_t *key); 342c956a60SJason A. Donenfeld u64 siphash_2u64(const u64 a, const u64 b, const siphash_key_t *key); 352c956a60SJason A. Donenfeld u64 siphash_3u64(const u64 a, const u64 b, const u64 c, 362c956a60SJason A. Donenfeld const siphash_key_t *key); 372c956a60SJason A. Donenfeld u64 siphash_4u64(const u64 a, const u64 b, const u64 c, const u64 d, 382c956a60SJason A. Donenfeld const siphash_key_t *key); 392c956a60SJason A. Donenfeld u64 siphash_1u32(const u32 a, const siphash_key_t *key); 402c956a60SJason A. Donenfeld u64 siphash_3u32(const u32 a, const u32 b, const u32 c, 412c956a60SJason A. Donenfeld const siphash_key_t *key); 422c956a60SJason A. Donenfeld 432c956a60SJason A. Donenfeld static inline u64 siphash_2u32(const u32 a, const u32 b, 442c956a60SJason A. Donenfeld const siphash_key_t *key) 452c956a60SJason A. Donenfeld { 462c956a60SJason A. Donenfeld return siphash_1u64((u64)b << 32 | a, key); 472c956a60SJason A. Donenfeld } 482c956a60SJason A. Donenfeld static inline u64 siphash_4u32(const u32 a, const u32 b, const u32 c, 492c956a60SJason A. Donenfeld const u32 d, const siphash_key_t *key) 502c956a60SJason A. Donenfeld { 512c956a60SJason A. Donenfeld return siphash_2u64((u64)b << 32 | a, (u64)d << 32 | c, key); 522c956a60SJason A. Donenfeld } 532c956a60SJason A. Donenfeld 542c956a60SJason A. Donenfeld 552c956a60SJason A. Donenfeld static inline u64 ___siphash_aligned(const __le64 *data, size_t len, 562c956a60SJason A. Donenfeld const siphash_key_t *key) 572c956a60SJason A. Donenfeld { 582c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 4) 592c956a60SJason A. Donenfeld return siphash_1u32(le32_to_cpup((const __le32 *)data), key); 602c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 8) 612c956a60SJason A. Donenfeld return siphash_1u64(le64_to_cpu(data[0]), key); 622c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 16) 632c956a60SJason A. Donenfeld return siphash_2u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), 642c956a60SJason A. Donenfeld key); 652c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 24) 662c956a60SJason A. Donenfeld return siphash_3u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), 672c956a60SJason A. Donenfeld le64_to_cpu(data[2]), key); 682c956a60SJason A. Donenfeld if (__builtin_constant_p(len) && len == 32) 692c956a60SJason A. Donenfeld return siphash_4u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), 702c956a60SJason A. Donenfeld le64_to_cpu(data[2]), le64_to_cpu(data[3]), 712c956a60SJason A. Donenfeld key); 722c956a60SJason A. Donenfeld return __siphash_aligned(data, len, key); 732c956a60SJason A. Donenfeld } 742c956a60SJason A. Donenfeld 752c956a60SJason A. Donenfeld /** 762c956a60SJason A. Donenfeld * siphash - compute 64-bit siphash PRF value 772c956a60SJason A. Donenfeld * @data: buffer to hash 782c956a60SJason A. Donenfeld * @size: size of @data 792c956a60SJason A. Donenfeld * @key: the siphash key 802c956a60SJason A. Donenfeld */ 812c956a60SJason A. Donenfeld static inline u64 siphash(const void *data, size_t len, 822c956a60SJason A. Donenfeld const siphash_key_t *key) 832c956a60SJason A. Donenfeld { 84f7e5b9bfSArnd Bergmann if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) || 85f7e5b9bfSArnd Bergmann !IS_ALIGNED((unsigned long)data, SIPHASH_ALIGNMENT)) 862c956a60SJason A. Donenfeld return __siphash_unaligned(data, len, key); 872c956a60SJason A. Donenfeld return ___siphash_aligned(data, len, key); 882c956a60SJason A. Donenfeld } 892c956a60SJason A. Donenfeld 901ae2324fSJason A. Donenfeld #define HSIPHASH_ALIGNMENT __alignof__(unsigned long) 911ae2324fSJason A. Donenfeld typedef struct { 921ae2324fSJason A. Donenfeld unsigned long key[2]; 931ae2324fSJason A. Donenfeld } hsiphash_key_t; 941ae2324fSJason A. Donenfeld 951ae2324fSJason A. Donenfeld u32 __hsiphash_aligned(const void *data, size_t len, 961ae2324fSJason A. Donenfeld const hsiphash_key_t *key); 971ae2324fSJason A. Donenfeld u32 __hsiphash_unaligned(const void *data, size_t len, 981ae2324fSJason A. Donenfeld const hsiphash_key_t *key); 991ae2324fSJason A. Donenfeld 1001ae2324fSJason A. Donenfeld u32 hsiphash_1u32(const u32 a, const hsiphash_key_t *key); 1011ae2324fSJason A. Donenfeld u32 hsiphash_2u32(const u32 a, const u32 b, const hsiphash_key_t *key); 1021ae2324fSJason A. Donenfeld u32 hsiphash_3u32(const u32 a, const u32 b, const u32 c, 1031ae2324fSJason A. Donenfeld const hsiphash_key_t *key); 1041ae2324fSJason A. Donenfeld u32 hsiphash_4u32(const u32 a, const u32 b, const u32 c, const u32 d, 1051ae2324fSJason A. Donenfeld const hsiphash_key_t *key); 1061ae2324fSJason A. Donenfeld 1071ae2324fSJason A. Donenfeld static inline u32 ___hsiphash_aligned(const __le32 *data, size_t len, 1081ae2324fSJason A. Donenfeld const hsiphash_key_t *key) 1091ae2324fSJason A. Donenfeld { 1101ae2324fSJason A. Donenfeld if (__builtin_constant_p(len) && len == 4) 1111ae2324fSJason A. Donenfeld return hsiphash_1u32(le32_to_cpu(data[0]), key); 1121ae2324fSJason A. Donenfeld if (__builtin_constant_p(len) && len == 8) 1131ae2324fSJason A. Donenfeld return hsiphash_2u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]), 1141ae2324fSJason A. Donenfeld key); 1151ae2324fSJason A. Donenfeld if (__builtin_constant_p(len) && len == 12) 1161ae2324fSJason A. Donenfeld return hsiphash_3u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]), 1171ae2324fSJason A. Donenfeld le32_to_cpu(data[2]), key); 1181ae2324fSJason A. Donenfeld if (__builtin_constant_p(len) && len == 16) 1191ae2324fSJason A. Donenfeld return hsiphash_4u32(le32_to_cpu(data[0]), le32_to_cpu(data[1]), 1201ae2324fSJason A. Donenfeld le32_to_cpu(data[2]), le32_to_cpu(data[3]), 1211ae2324fSJason A. Donenfeld key); 1221ae2324fSJason A. Donenfeld return __hsiphash_aligned(data, len, key); 1231ae2324fSJason A. Donenfeld } 1241ae2324fSJason A. Donenfeld 1251ae2324fSJason A. Donenfeld /** 1261ae2324fSJason A. Donenfeld * hsiphash - compute 32-bit hsiphash PRF value 1271ae2324fSJason A. Donenfeld * @data: buffer to hash 1281ae2324fSJason A. Donenfeld * @size: size of @data 1291ae2324fSJason A. Donenfeld * @key: the hsiphash key 1301ae2324fSJason A. Donenfeld */ 1311ae2324fSJason A. Donenfeld static inline u32 hsiphash(const void *data, size_t len, 1321ae2324fSJason A. Donenfeld const hsiphash_key_t *key) 1331ae2324fSJason A. Donenfeld { 134f7e5b9bfSArnd Bergmann if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) || 135f7e5b9bfSArnd Bergmann !IS_ALIGNED((unsigned long)data, HSIPHASH_ALIGNMENT)) 1361ae2324fSJason A. Donenfeld return __hsiphash_unaligned(data, len, key); 1371ae2324fSJason A. Donenfeld return ___hsiphash_aligned(data, len, key); 1381ae2324fSJason A. Donenfeld } 1391ae2324fSJason A. Donenfeld 1402c956a60SJason A. Donenfeld #endif /* _LINUX_SIPHASH_H */ 141