1 /* SPDX-License-Identifier: (GPL-2.0-only OR B 1 2 /* Copyright (C) 2016-2022 Jason A. Donenfeld 3 * 4 * SipHash: a fast short-input PRF 5 * https://131002.net/siphash/ 6 * 7 * This implementation is specifically for Sip 8 * and HalfSipHash1-3/SipHash1-3 for an insecu 9 * hashtables. 10 */ 11 12 #ifndef _LINUX_SIPHASH_H 13 #define _LINUX_SIPHASH_H 14 15 #include <linux/types.h> 16 #include <linux/kernel.h> 17 18 #define SIPHASH_ALIGNMENT __alignof__(u64) 19 typedef struct { 20 u64 key[2]; 21 } siphash_key_t; 22 23 #define siphash_aligned_key_t siphash_key_t __ 24 25 static inline bool siphash_key_is_zero(const s 26 { 27 return !(key->key[0] | key->key[1]); 28 } 29 30 u64 __siphash_aligned(const void *data, size_t 31 u64 __siphash_unaligned(const void *data, size 32 33 u64 siphash_1u64(const u64 a, const siphash_ke 34 u64 siphash_2u64(const u64 a, const u64 b, con 35 u64 siphash_3u64(const u64 a, const u64 b, con 36 const siphash_key_t *key); 37 u64 siphash_4u64(const u64 a, const u64 b, con 38 const siphash_key_t *key); 39 u64 siphash_1u32(const u32 a, const siphash_ke 40 u64 siphash_3u32(const u32 a, const u32 b, con 41 const siphash_key_t *key); 42 43 static inline u64 siphash_2u32(const u32 a, co 44 const siphash_k 45 { 46 return siphash_1u64((u64)b << 32 | a, 47 } 48 static inline u64 siphash_4u32(const u32 a, co 49 const u32 d, co 50 { 51 return siphash_2u64((u64)b << 32 | a, 52 } 53 54 55 static inline u64 ___siphash_aligned(const __l 56 const sip 57 { 58 if (__builtin_constant_p(len) && len = 59 return siphash_1u32(le32_to_cp 60 if (__builtin_constant_p(len) && len = 61 return siphash_1u64(le64_to_cp 62 if (__builtin_constant_p(len) && len = 63 return siphash_2u64(le64_to_cp 64 key); 65 if (__builtin_constant_p(len) && len = 66 return siphash_3u64(le64_to_cp 67 le64_to_cp 68 if (__builtin_constant_p(len) && len = 69 return siphash_4u64(le64_to_cp 70 le64_to_cp 71 key); 72 return __siphash_aligned(data, len, ke 73 } 74 75 /** 76 * siphash - compute 64-bit siphash PRF value 77 * @data: buffer to hash 78 * @size: size of @data 79 * @key: the siphash key 80 */ 81 static inline u64 siphash(const void *data, si 82 const siphash_key_t 83 { 84 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_U 85 !IS_ALIGNED((unsigned long)data, S 86 return __siphash_unaligned(dat 87 return ___siphash_aligned(data, len, k 88 } 89 90 #define HSIPHASH_ALIGNMENT __alignof__(unsigne 91 typedef struct { 92 unsigned long key[2]; 93 } hsiphash_key_t; 94 95 u32 __hsiphash_aligned(const void *data, size_ 96 const hsiphash_key_t *k 97 u32 __hsiphash_unaligned(const void *data, siz 98 const hsiphash_key_t 99 100 u32 hsiphash_1u32(const u32 a, const hsiphash_ 101 u32 hsiphash_2u32(const u32 a, const u32 b, co 102 u32 hsiphash_3u32(const u32 a, const u32 b, co 103 const hsiphash_key_t *key); 104 u32 hsiphash_4u32(const u32 a, const u32 b, co 105 const hsiphash_key_t *key); 106 107 static inline u32 ___hsiphash_aligned(const __ 108 const hs 109 { 110 if (__builtin_constant_p(len) && len = 111 return hsiphash_1u32(le32_to_c 112 if (__builtin_constant_p(len) && len = 113 return hsiphash_2u32(le32_to_c 114 key); 115 if (__builtin_constant_p(len) && len = 116 return hsiphash_3u32(le32_to_c 117 le32_to_c 118 if (__builtin_constant_p(len) && len = 119 return hsiphash_4u32(le32_to_c 120 le32_to_c 121 key); 122 return __hsiphash_aligned(data, len, k 123 } 124 125 /** 126 * hsiphash - compute 32-bit hsiphash PRF valu 127 * @data: buffer to hash 128 * @size: size of @data 129 * @key: the hsiphash key 130 */ 131 static inline u32 hsiphash(const void *data, s 132 const hsiphash_key_ 133 { 134 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_U 135 !IS_ALIGNED((unsigned long)data, H 136 return __hsiphash_unaligned(da 137 return ___hsiphash_aligned(data, len, 138 } 139 140 /* 141 * These macros expose the raw SipHash and Hal 142 * Do not use them directly! If you think you 143 * be sure to CC the maintainer of this file e 144 */ 145 146 #define SIPHASH_PERMUTATION(a, b, c, d) ( \ 147 (a) += (b), (b) = rol64((b), 13), (b) 148 (c) += (d), (d) = rol64((d), 16), (d) 149 (a) += (d), (d) = rol64((d), 21), (d) 150 (c) += (b), (b) = rol64((b), 17), (b) 151 152 #define SIPHASH_CONST_0 0x736f6d6570736575ULL 153 #define SIPHASH_CONST_1 0x646f72616e646f6dULL 154 #define SIPHASH_CONST_2 0x6c7967656e657261ULL 155 #define SIPHASH_CONST_3 0x7465646279746573ULL 156 157 #define HSIPHASH_PERMUTATION(a, b, c, d) ( \ 158 (a) += (b), (b) = rol32((b), 5), (b) ^ 159 (c) += (d), (d) = rol32((d), 8), (d) ^ 160 (a) += (d), (d) = rol32((d), 7), (d) ^ 161 (c) += (b), (b) = rol32((b), 13), (b) 162 163 #define HSIPHASH_CONST_0 0U 164 #define HSIPHASH_CONST_1 0U 165 #define HSIPHASH_CONST_2 0x6c796765U 166 #define HSIPHASH_CONST_3 0x74656462U 167 168 #endif /* _LINUX_SIPHASH_H */ 169
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.