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 u64
__siphash_aligned(const void *data
, size_t len
, const siphash_key_t
*key
)
54 const u8
*end
= data
+ len
- (len
% sizeof(u64
));
55 const u8 left
= len
& (sizeof(u64
) - 1);
58 for (; data
!= end
; data
+= sizeof(u64
)) {
59 m
= le64_to_cpup(data
);
65 #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
67 b
|= le64_to_cpu((__force __le64
)(load_unaligned_zeropad(data
) &
68 bytemask_from_count(left
)));
71 case 7: b
|= ((u64
)end
[6]) << 48;
72 case 6: b
|= ((u64
)end
[5]) << 40;
73 case 5: b
|= ((u64
)end
[4]) << 32;
74 case 4: b
|= le32_to_cpup(data
); break;
75 case 3: b
|= ((u64
)end
[2]) << 16;
76 case 2: b
|= le16_to_cpup(data
); break;
82 EXPORT_SYMBOL(__siphash_aligned
);
84 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
85 u64
__siphash_unaligned(const void *data
, size_t len
, const siphash_key_t
*key
)
87 const u8
*end
= data
+ len
- (len
% sizeof(u64
));
88 const u8 left
= len
& (sizeof(u64
) - 1);
91 for (; data
!= end
; data
+= sizeof(u64
)) {
92 m
= get_unaligned_le64(data
);
98 #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
100 b
|= le64_to_cpu((__force __le64
)(load_unaligned_zeropad(data
) &
101 bytemask_from_count(left
)));
104 case 7: b
|= ((u64
)end
[6]) << 48;
105 case 6: b
|= ((u64
)end
[5]) << 40;
106 case 5: b
|= ((u64
)end
[4]) << 32;
107 case 4: b
|= get_unaligned_le32(end
); break;
108 case 3: b
|= ((u64
)end
[2]) << 16;
109 case 2: b
|= get_unaligned_le16(end
); break;
115 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 u32
__hsiphash_aligned(const void *data
, size_t len
, const hsiphash_key_t
*key
)
255 const u8
*end
= data
+ len
- (len
% sizeof(u64
));
256 const u8 left
= len
& (sizeof(u64
) - 1);
259 for (; data
!= end
; data
+= sizeof(u64
)) {
260 m
= le64_to_cpup(data
);
265 #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
267 b
|= le64_to_cpu((__force __le64
)(load_unaligned_zeropad(data
) &
268 bytemask_from_count(left
)));
271 case 7: b
|= ((u64
)end
[6]) << 48;
272 case 6: b
|= ((u64
)end
[5]) << 40;
273 case 5: b
|= ((u64
)end
[4]) << 32;
274 case 4: b
|= le32_to_cpup(data
); break;
275 case 3: b
|= ((u64
)end
[2]) << 16;
276 case 2: b
|= le16_to_cpup(data
); break;
282 EXPORT_SYMBOL(__hsiphash_aligned
);
284 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
285 u32
__hsiphash_unaligned(const void *data
, size_t len
,
286 const hsiphash_key_t
*key
)
288 const u8
*end
= data
+ len
- (len
% sizeof(u64
));
289 const u8 left
= len
& (sizeof(u64
) - 1);
292 for (; data
!= end
; data
+= sizeof(u64
)) {
293 m
= get_unaligned_le64(data
);
298 #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
300 b
|= le64_to_cpu((__force __le64
)(load_unaligned_zeropad(data
) &
301 bytemask_from_count(left
)));
304 case 7: b
|= ((u64
)end
[6]) << 48;
305 case 6: b
|= ((u64
)end
[5]) << 40;
306 case 5: b
|= ((u64
)end
[4]) << 32;
307 case 4: b
|= get_unaligned_le32(end
); break;
308 case 3: b
|= ((u64
)end
[2]) << 16;
309 case 2: b
|= get_unaligned_le16(end
); break;
315 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 u32
__hsiphash_aligned(const void *data
, size_t len
, const hsiphash_key_t
*key
)
423 const u8
*end
= data
+ len
- (len
% sizeof(u32
));
424 const u8 left
= len
& (sizeof(u32
) - 1);
427 for (; data
!= end
; data
+= sizeof(u32
)) {
428 m
= le32_to_cpup(data
);
434 case 3: b
|= ((u32
)end
[2]) << 16;
435 case 2: b
|= le16_to_cpup(data
); break;
440 EXPORT_SYMBOL(__hsiphash_aligned
);
442 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
443 u32
__hsiphash_unaligned(const void *data
, size_t len
,
444 const hsiphash_key_t
*key
)
446 const u8
*end
= data
+ len
- (len
% sizeof(u32
));
447 const u8 left
= len
& (sizeof(u32
) - 1);
450 for (; data
!= end
; data
+= sizeof(u32
)) {
451 m
= get_unaligned_le32(data
);
457 case 3: b
|= ((u32
)end
[2]) << 16;
458 case 2: b
|= get_unaligned_le16(end
); break;
463 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
);