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
siphash_key_is_zero(const siphash_key_t * key)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
siphash_2u32(const u32 a,const u32 b,const siphash_key_t * key)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 }
siphash_4u32(const u32 a,const u32 b,const u32 c,const u32 d,const siphash_key_t * key)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
___siphash_aligned(const __le64 * data,size_t len,const siphash_key_t * key)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 */
siphash(const void * data,size_t len,const siphash_key_t * key)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
___hsiphash_aligned(const __le32 * data,size_t len,const hsiphash_key_t * key)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 */
hsiphash(const void * data,size_t len,const hsiphash_key_t * key)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
140e73aaae2SJason A. Donenfeld /*
141e73aaae2SJason A. Donenfeld * These macros expose the raw SipHash and HalfSipHash permutations.
142e73aaae2SJason A. Donenfeld * Do not use them directly! If you think you have a use for them,
143e73aaae2SJason A. Donenfeld * be sure to CC the maintainer of this file explaining why.
144e73aaae2SJason A. Donenfeld */
145e73aaae2SJason A. Donenfeld
146e73aaae2SJason A. Donenfeld #define SIPHASH_PERMUTATION(a, b, c, d) ( \
147e73aaae2SJason A. Donenfeld (a) += (b), (b) = rol64((b), 13), (b) ^= (a), (a) = rol64((a), 32), \
148e73aaae2SJason A. Donenfeld (c) += (d), (d) = rol64((d), 16), (d) ^= (c), \
149e73aaae2SJason A. Donenfeld (a) += (d), (d) = rol64((d), 21), (d) ^= (a), \
150e73aaae2SJason A. Donenfeld (c) += (b), (b) = rol64((b), 17), (b) ^= (c), (c) = rol64((c), 32))
151e73aaae2SJason A. Donenfeld
152e73aaae2SJason A. Donenfeld #define SIPHASH_CONST_0 0x736f6d6570736575ULL
153e73aaae2SJason A. Donenfeld #define SIPHASH_CONST_1 0x646f72616e646f6dULL
154e73aaae2SJason A. Donenfeld #define SIPHASH_CONST_2 0x6c7967656e657261ULL
155e73aaae2SJason A. Donenfeld #define SIPHASH_CONST_3 0x7465646279746573ULL
156e73aaae2SJason A. Donenfeld
157e73aaae2SJason A. Donenfeld #define HSIPHASH_PERMUTATION(a, b, c, d) ( \
158e73aaae2SJason A. Donenfeld (a) += (b), (b) = rol32((b), 5), (b) ^= (a), (a) = rol32((a), 16), \
159e73aaae2SJason A. Donenfeld (c) += (d), (d) = rol32((d), 8), (d) ^= (c), \
160e73aaae2SJason A. Donenfeld (a) += (d), (d) = rol32((d), 7), (d) ^= (a), \
161e73aaae2SJason A. Donenfeld (c) += (b), (b) = rol32((b), 13), (b) ^= (c), (c) = rol32((c), 16))
162e73aaae2SJason A. Donenfeld
163e73aaae2SJason A. Donenfeld #define HSIPHASH_CONST_0 0U
164e73aaae2SJason A. Donenfeld #define HSIPHASH_CONST_1 0U
165e73aaae2SJason A. Donenfeld #define HSIPHASH_CONST_2 0x6c796765U
166e73aaae2SJason A. Donenfeld #define HSIPHASH_CONST_3 0x74656462U
167e73aaae2SJason A. Donenfeld
1682c956a60SJason A. Donenfeld #endif /* _LINUX_SIPHASH_H */
169