1 /* Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
3 * This file is provided under a dual BSD/GPLv2 license.
5 * SipHash: a fast short-input PRF
6 * https://131002.net/siphash/
8 * This implementation is specifically for SipHash2-4 for a secure PRF
9 * and HalfSipHash1-3/SipHash1-3 for an insecure PRF only suitable for
13 #include <linux/siphash.h>
14 #include <asm/unaligned.h>
16 #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
17 #include <linux/dcache.h>
18 #include <asm/word-at-a-time.h>
23 v0 += v1; v1 = rol64(v1, 13); v1 ^= v0; v0 = rol64(v0, 32); \
24 v2 += v3; v3 = rol64(v3, 16); v3 ^= v2; \
25 v0 += v3; v3 = rol64(v3, 21); v3 ^= v0; \
26 v2 += v1; v1 = rol64(v1, 17); v1 ^= v2; v2 = rol64(v2, 32); \
29 #define PREAMBLE(len) \
30 u64 v0 = 0x736f6d6570736575ULL; \
31 u64 v1 = 0x646f72616e646f6dULL; \
32 u64 v2 = 0x6c7967656e657261ULL; \
33 u64 v3 = 0x7465646279746573ULL; \
34 u64 b = ((u64)(len)) << 56; \
50 return (v0 ^ v1) ^ (v2 ^ v3);
52 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
53 u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key)
55 const u8 *end = data + len - (len % sizeof(u64));
56 const u8 left = len & (sizeof(u64) - 1);
59 for (; data != end; data += sizeof(u64)) {
60 m = le64_to_cpup(data);
66 #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
68 b |= le64_to_cpu((__force __le64)(load_unaligned_zeropad(data) &
69 bytemask_from_count(left)));
72 case 7: b |= ((u64)end[6]) << 48; fallthrough;
73 case 6: b |= ((u64)end[5]) << 40; fallthrough;
74 case 5: b |= ((u64)end[4]) << 32; fallthrough;
75 case 4: b |= le32_to_cpup(data); break;
76 case 3: b |= ((u64)end[2]) << 16; fallthrough;
77 case 2: b |= le16_to_cpup(data); break;
83 EXPORT_SYMBOL(__siphash_aligned);
86 u64 __siphash_unaligned(const void *data, size_t len, const siphash_key_t *key)
88 const u8 *end = data + len - (len % sizeof(u64));
89 const u8 left = len & (sizeof(u64) - 1);
92 for (; data != end; data += sizeof(u64)) {
93 m = get_unaligned_le64(data);
99 #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
101 b |= le64_to_cpu((__force __le64)(load_unaligned_zeropad(data) &
102 bytemask_from_count(left)));
105 case 7: b |= ((u64)end[6]) << 48; fallthrough;
106 case 6: b |= ((u64)end[5]) << 40; fallthrough;
107 case 5: b |= ((u64)end[4]) << 32; fallthrough;
108 case 4: b |= get_unaligned_le32(end); break;
109 case 3: b |= ((u64)end[2]) << 16; fallthrough;
110 case 2: b |= get_unaligned_le16(end); break;
116 EXPORT_SYMBOL(__siphash_unaligned);
119 * siphash_1u64 - compute 64-bit siphash PRF value of a u64
121 * @key: the siphash key
123 u64 siphash_1u64(const u64 first, const siphash_key_t *key)
132 EXPORT_SYMBOL(siphash_1u64);
135 * siphash_2u64 - compute 64-bit siphash PRF value of 2 u64
137 * @second: second u64
138 * @key: the siphash key
140 u64 siphash_2u64(const u64 first, const u64 second, const siphash_key_t *key)
153 EXPORT_SYMBOL(siphash_2u64);
156 * siphash_3u64 - compute 64-bit siphash PRF value of 3 u64
158 * @second: second u64
160 * @key: the siphash key
162 u64 siphash_3u64(const u64 first, const u64 second, const u64 third,
163 const siphash_key_t *key)
180 EXPORT_SYMBOL(siphash_3u64);
183 * siphash_4u64 - compute 64-bit siphash PRF value of 4 u64
185 * @second: second u64
188 * @key: the siphash key
190 u64 siphash_4u64(const u64 first, const u64 second, const u64 third,
191 const u64 forth, const siphash_key_t *key)
212 EXPORT_SYMBOL(siphash_4u64);
214 u64 siphash_1u32(const u32 first, const siphash_key_t *key)
220 EXPORT_SYMBOL(siphash_1u32);
222 u64 siphash_3u32(const u32 first, const u32 second, const u32 third,
223 const siphash_key_t *key)
225 u64 combined = (u64)second << 32 | first;
234 EXPORT_SYMBOL(siphash_3u32);
236 #if BITS_PER_LONG == 64
237 /* Note that on 64-bit, we make HalfSipHash1-3 actually be SipHash1-3, for
238 * performance reasons. On 32-bit, below, we actually implement HalfSipHash1-3.
241 #define HSIPROUND SIPROUND
242 #define HPREAMBLE(len) PREAMBLE(len)
251 return (v0 ^ v1) ^ (v2 ^ v3);
253 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
254 u32 __hsiphash_aligned(const void *data, size_t len, const hsiphash_key_t *key)
256 const u8 *end = data + len - (len % sizeof(u64));
257 const u8 left = len & (sizeof(u64) - 1);
260 for (; data != end; data += sizeof(u64)) {
261 m = le64_to_cpup(data);
266 #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
268 b |= le64_to_cpu((__force __le64)(load_unaligned_zeropad(data) &
269 bytemask_from_count(left)));
272 case 7: b |= ((u64)end[6]) << 48; fallthrough;
273 case 6: b |= ((u64)end[5]) << 40; fallthrough;
274 case 5: b |= ((u64)end[4]) << 32; fallthrough;
275 case 4: b |= le32_to_cpup(data); break;
276 case 3: b |= ((u64)end[2]) << 16; fallthrough;
277 case 2: b |= le16_to_cpup(data); break;
283 EXPORT_SYMBOL(__hsiphash_aligned);
286 u32 __hsiphash_unaligned(const void *data, size_t len,
287 const hsiphash_key_t *key)
289 const u8 *end = data + len - (len % sizeof(u64));
290 const u8 left = len & (sizeof(u64) - 1);
293 for (; data != end; data += sizeof(u64)) {
294 m = get_unaligned_le64(data);
299 #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
301 b |= le64_to_cpu((__force __le64)(load_unaligned_zeropad(data) &
302 bytemask_from_count(left)));
305 case 7: b |= ((u64)end[6]) << 48; fallthrough;
306 case 6: b |= ((u64)end[5]) << 40; fallthrough;
307 case 5: b |= ((u64)end[4]) << 32; fallthrough;
308 case 4: b |= get_unaligned_le32(end); break;
309 case 3: b |= ((u64)end[2]) << 16; fallthrough;
310 case 2: b |= get_unaligned_le16(end); break;
316 EXPORT_SYMBOL(__hsiphash_unaligned);
319 * hsiphash_1u32 - compute 64-bit hsiphash PRF value of a u32
321 * @key: the hsiphash key
323 u32 hsiphash_1u32(const u32 first, const hsiphash_key_t *key)
329 EXPORT_SYMBOL(hsiphash_1u32);
332 * hsiphash_2u32 - compute 32-bit hsiphash PRF value of 2 u32
334 * @second: second u32
335 * @key: the hsiphash key
337 u32 hsiphash_2u32(const u32 first, const u32 second, const hsiphash_key_t *key)
339 u64 combined = (u64)second << 32 | first;
346 EXPORT_SYMBOL(hsiphash_2u32);
349 * hsiphash_3u32 - compute 32-bit hsiphash PRF value of 3 u32
351 * @second: second u32
353 * @key: the hsiphash key
355 u32 hsiphash_3u32(const u32 first, const u32 second, const u32 third,
356 const hsiphash_key_t *key)
358 u64 combined = (u64)second << 32 | first;
366 EXPORT_SYMBOL(hsiphash_3u32);
369 * hsiphash_4u32 - compute 32-bit hsiphash PRF value of 4 u32
371 * @second: second u32
374 * @key: the hsiphash key
376 u32 hsiphash_4u32(const u32 first, const u32 second, const u32 third,
377 const u32 forth, const hsiphash_key_t *key)
379 u64 combined = (u64)second << 32 | first;
384 combined = (u64)forth << 32 | third;
390 EXPORT_SYMBOL(hsiphash_4u32);
394 v0 += v1; v1 = rol32(v1, 5); v1 ^= v0; v0 = rol32(v0, 16); \
395 v2 += v3; v3 = rol32(v3, 8); v3 ^= v2; \
396 v0 += v3; v3 = rol32(v3, 7); v3 ^= v0; \
397 v2 += v1; v1 = rol32(v1, 13); v1 ^= v2; v2 = rol32(v2, 16); \
400 #define HPREAMBLE(len) \
403 u32 v2 = 0x6c796765U; \
404 u32 v3 = 0x74656462U; \
405 u32 b = ((u32)(len)) << 24; \
421 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
422 u32 __hsiphash_aligned(const void *data, size_t len, const hsiphash_key_t *key)
424 const u8 *end = data + len - (len % sizeof(u32));
425 const u8 left = len & (sizeof(u32) - 1);
428 for (; data != end; data += sizeof(u32)) {
429 m = le32_to_cpup(data);
435 case 3: b |= ((u32)end[2]) << 16; fallthrough;
436 case 2: b |= le16_to_cpup(data); break;
441 EXPORT_SYMBOL(__hsiphash_aligned);
444 u32 __hsiphash_unaligned(const void *data, size_t len,
445 const hsiphash_key_t *key)
447 const u8 *end = data + len - (len % sizeof(u32));
448 const u8 left = len & (sizeof(u32) - 1);
451 for (; data != end; data += sizeof(u32)) {
452 m = get_unaligned_le32(data);
458 case 3: b |= ((u32)end[2]) << 16; fallthrough;
459 case 2: b |= get_unaligned_le16(end); break;
464 EXPORT_SYMBOL(__hsiphash_unaligned);
467 * hsiphash_1u32 - compute 32-bit hsiphash PRF value of a u32
469 * @key: the hsiphash key
471 u32 hsiphash_1u32(const u32 first, const hsiphash_key_t *key)
479 EXPORT_SYMBOL(hsiphash_1u32);
482 * hsiphash_2u32 - compute 32-bit hsiphash PRF value of 2 u32
484 * @second: second u32
485 * @key: the hsiphash key
487 u32 hsiphash_2u32(const u32 first, const u32 second, const hsiphash_key_t *key)
498 EXPORT_SYMBOL(hsiphash_2u32);
501 * hsiphash_3u32 - compute 32-bit hsiphash PRF value of 3 u32
503 * @second: second u32
505 * @key: the hsiphash key
507 u32 hsiphash_3u32(const u32 first, const u32 second, const u32 third,
508 const hsiphash_key_t *key)
522 EXPORT_SYMBOL(hsiphash_3u32);
525 * hsiphash_4u32 - compute 32-bit hsiphash PRF value of 4 u32
527 * @second: second u32
530 * @key: the hsiphash key
532 u32 hsiphash_4u32(const u32 first, const u32 second, const u32 third,
533 const u32 forth, const hsiphash_key_t *key)
550 EXPORT_SYMBOL(hsiphash_4u32);