Jason A. Donenfeld | 53e054b | 2019-08-17 00:01:12 +0100 | [diff] [blame^] | 1 | /* Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved. |
| 2 | * |
| 3 | * This file is provided under a dual BSD/GPLv2 license. |
| 4 | * |
| 5 | * SipHash: a fast short-input PRF |
| 6 | * https://131002.net/siphash/ |
| 7 | * |
| 8 | * This implementation is specifically for SipHash2-4. |
| 9 | */ |
| 10 | |
| 11 | #ifndef _LINUX_SIPHASH_H |
| 12 | #define _LINUX_SIPHASH_H |
| 13 | |
| 14 | #include <linux/types.h> |
| 15 | #include <linux/kernel.h> |
| 16 | |
| 17 | #define SIPHASH_ALIGNMENT __alignof__(u64) |
| 18 | typedef struct { |
| 19 | u64 key[2]; |
| 20 | } siphash_key_t; |
| 21 | |
| 22 | u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key); |
| 23 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 24 | u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key); |
| 25 | #endif |
| 26 | |
| 27 | u64 siphash_1u64(const u64 a, const siphash_key_t *key); |
| 28 | u64 siphash_2u64(const u64 a, const u64 b, const siphash_key_t *key); |
| 29 | u64 siphash_3u64(const u64 a, const u64 b, const u64 c, |
| 30 | const siphash_key_t *key); |
| 31 | u64 siphash_4u64(const u64 a, const u64 b, const u64 c, const u64 d, |
| 32 | const siphash_key_t *key); |
| 33 | u64 siphash_1u32(const u32 a, const siphash_key_t *key); |
| 34 | u64 siphash_3u32(const u32 a, const u32 b, const u32 c, |
| 35 | const siphash_key_t *key); |
| 36 | |
| 37 | static inline u64 siphash_2u32(const u32 a, const u32 b, |
| 38 | const siphash_key_t *key) |
| 39 | { |
| 40 | return siphash_1u64((u64)b << 32 | a, key); |
| 41 | } |
| 42 | static inline u64 siphash_4u32(const u32 a, const u32 b, const u32 c, |
| 43 | const u32 d, const siphash_key_t *key) |
| 44 | { |
| 45 | return siphash_2u64((u64)b << 32 | a, (u64)d << 32 | c, key); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | static inline u64 ___siphash_aligned(const __le64 *data, size_t len, |
| 50 | const siphash_key_t *key) |
| 51 | { |
| 52 | if (__builtin_constant_p(len) && len == 4) |
| 53 | return siphash_1u32(le32_to_cpup((const __le32 *)data), key); |
| 54 | if (__builtin_constant_p(len) && len == 8) |
| 55 | return siphash_1u64(le64_to_cpu(data[0]), key); |
| 56 | if (__builtin_constant_p(len) && len == 16) |
| 57 | return siphash_2u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), |
| 58 | key); |
| 59 | if (__builtin_constant_p(len) && len == 24) |
| 60 | return siphash_3u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), |
| 61 | le64_to_cpu(data[2]), key); |
| 62 | if (__builtin_constant_p(len) && len == 32) |
| 63 | return siphash_4u64(le64_to_cpu(data[0]), le64_to_cpu(data[1]), |
| 64 | le64_to_cpu(data[2]), le64_to_cpu(data[3]), |
| 65 | key); |
| 66 | return __siphash_aligned(data, len, key); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * siphash - compute 64-bit siphash PRF value |
| 71 | * @data: buffer to hash |
| 72 | * @size: size of @data |
| 73 | * @key: the siphash key |
| 74 | */ |
| 75 | static inline u64 siphash(const void *data, size_t len, |
| 76 | const siphash_key_t *key) |
| 77 | { |
| 78 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 79 | if (!IS_ALIGNED((unsigned long)data, SIPHASH_ALIGNMENT)) |
| 80 | return __siphash_unaligned(data, len, key); |
| 81 | #endif |
| 82 | return ___siphash_aligned(data, len, key); |
| 83 | } |
| 84 | |
| 85 | #endif /* _LINUX_SIPHASH_H */ |