1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
2 /* Copyright (C) 2016-2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4 * SipHash: a fast short-input PRF
5 * https://131002.net/siphash/
7 * This implementation is specifically for SipHash2-4 for a secure PRF
8 * and HalfSipHash1-3/SipHash1-3 for an insecure PRF only suitable for
12 #include <linux/siphash.h>
13 #include <linux/unaligned.h>
15 #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
16 #include <linux/dcache.h>
17 #include <asm/word-at-a-time.h>
20 #define SIPROUND SIPHASH_PERMUTATION(v0, v1, v2, v3)
22 #define PREAMBLE(len) \
23 u64 v0 = SIPHASH_CONST_0; \
24 u64 v1 = SIPHASH_CONST_1; \
25 u64 v2 = SIPHASH_CONST_2; \
26 u64 v3 = SIPHASH_CONST_3; \
27 u64 b = ((u64)(len)) << 56; \
43 return (v0 ^ v1) ^ (v2 ^ v3);
45 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
46 u64
__siphash_aligned(const void *data
, size_t len
, const siphash_key_t
*key
)
48 const u8
*end
= data
+ len
- (len
% sizeof(u64
));
49 const u8 left
= len
& (sizeof(u64
) - 1);
52 for (; data
!= end
; data
+= sizeof(u64
)) {
53 m
= le64_to_cpup(data
);
59 #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
61 b
|= le64_to_cpu((__force __le64
)(load_unaligned_zeropad(data
) &
62 bytemask_from_count(left
)));
65 case 7: b
|= ((u64
)end
[6]) << 48; fallthrough
;
66 case 6: b
|= ((u64
)end
[5]) << 40; fallthrough
;
67 case 5: b
|= ((u64
)end
[4]) << 32; fallthrough
;
68 case 4: b
|= le32_to_cpup(data
); break;
69 case 3: b
|= ((u64
)end
[2]) << 16; fallthrough
;
70 case 2: b
|= le16_to_cpup(data
); break;
76 EXPORT_SYMBOL(__siphash_aligned
);
79 u64
__siphash_unaligned(const void *data
, size_t len
, const siphash_key_t
*key
)
81 const u8
*end
= data
+ len
- (len
% sizeof(u64
));
82 const u8 left
= len
& (sizeof(u64
) - 1);
85 for (; data
!= end
; data
+= sizeof(u64
)) {
86 m
= get_unaligned_le64(data
);
92 #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
94 b
|= le64_to_cpu((__force __le64
)(load_unaligned_zeropad(data
) &
95 bytemask_from_count(left
)));
98 case 7: b
|= ((u64
)end
[6]) << 48; fallthrough
;
99 case 6: b
|= ((u64
)end
[5]) << 40; fallthrough
;
100 case 5: b
|= ((u64
)end
[4]) << 32; fallthrough
;
101 case 4: b
|= get_unaligned_le32(end
); break;
102 case 3: b
|= ((u64
)end
[2]) << 16; fallthrough
;
103 case 2: b
|= get_unaligned_le16(end
); break;
109 EXPORT_SYMBOL(__siphash_unaligned
);
112 * siphash_1u64 - compute 64-bit siphash PRF value of a u64
114 * @key: the siphash key
116 u64
siphash_1u64(const u64 first
, const siphash_key_t
*key
)
125 EXPORT_SYMBOL(siphash_1u64
);
128 * siphash_2u64 - compute 64-bit siphash PRF value of 2 u64
130 * @second: second u64
131 * @key: the siphash key
133 u64
siphash_2u64(const u64 first
, const u64 second
, const siphash_key_t
*key
)
146 EXPORT_SYMBOL(siphash_2u64
);
149 * siphash_3u64 - compute 64-bit siphash PRF value of 3 u64
151 * @second: second u64
153 * @key: the siphash key
155 u64
siphash_3u64(const u64 first
, const u64 second
, const u64 third
,
156 const siphash_key_t
*key
)
173 EXPORT_SYMBOL(siphash_3u64
);
176 * siphash_4u64 - compute 64-bit siphash PRF value of 4 u64
178 * @second: second u64
181 * @key: the siphash key
183 u64
siphash_4u64(const u64 first
, const u64 second
, const u64 third
,
184 const u64 forth
, const siphash_key_t
*key
)
205 EXPORT_SYMBOL(siphash_4u64
);
207 u64
siphash_1u32(const u32 first
, const siphash_key_t
*key
)
213 EXPORT_SYMBOL(siphash_1u32
);
215 u64
siphash_3u32(const u32 first
, const u32 second
, const u32 third
,
216 const siphash_key_t
*key
)
218 u64 combined
= (u64
)second
<< 32 | first
;
227 EXPORT_SYMBOL(siphash_3u32
);
229 #if BITS_PER_LONG == 64
230 /* Note that on 64-bit, we make HalfSipHash1-3 actually be SipHash1-3, for
231 * performance reasons. On 32-bit, below, we actually implement HalfSipHash1-3.
234 #define HSIPROUND SIPROUND
235 #define HPREAMBLE(len) PREAMBLE(len)
244 return (v0 ^ v1) ^ (v2 ^ v3);
246 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
247 u32
__hsiphash_aligned(const void *data
, size_t len
, const hsiphash_key_t
*key
)
249 const u8
*end
= data
+ len
- (len
% sizeof(u64
));
250 const u8 left
= len
& (sizeof(u64
) - 1);
253 for (; data
!= end
; data
+= sizeof(u64
)) {
254 m
= le64_to_cpup(data
);
259 #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
261 b
|= le64_to_cpu((__force __le64
)(load_unaligned_zeropad(data
) &
262 bytemask_from_count(left
)));
265 case 7: b
|= ((u64
)end
[6]) << 48; fallthrough
;
266 case 6: b
|= ((u64
)end
[5]) << 40; fallthrough
;
267 case 5: b
|= ((u64
)end
[4]) << 32; fallthrough
;
268 case 4: b
|= le32_to_cpup(data
); break;
269 case 3: b
|= ((u64
)end
[2]) << 16; fallthrough
;
270 case 2: b
|= le16_to_cpup(data
); break;
276 EXPORT_SYMBOL(__hsiphash_aligned
);
279 u32
__hsiphash_unaligned(const void *data
, size_t len
,
280 const hsiphash_key_t
*key
)
282 const u8
*end
= data
+ len
- (len
% sizeof(u64
));
283 const u8 left
= len
& (sizeof(u64
) - 1);
286 for (; data
!= end
; data
+= sizeof(u64
)) {
287 m
= get_unaligned_le64(data
);
292 #if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
294 b
|= le64_to_cpu((__force __le64
)(load_unaligned_zeropad(data
) &
295 bytemask_from_count(left
)));
298 case 7: b
|= ((u64
)end
[6]) << 48; fallthrough
;
299 case 6: b
|= ((u64
)end
[5]) << 40; fallthrough
;
300 case 5: b
|= ((u64
)end
[4]) << 32; fallthrough
;
301 case 4: b
|= get_unaligned_le32(end
); break;
302 case 3: b
|= ((u64
)end
[2]) << 16; fallthrough
;
303 case 2: b
|= get_unaligned_le16(end
); break;
309 EXPORT_SYMBOL(__hsiphash_unaligned
);
312 * hsiphash_1u32 - compute 64-bit hsiphash PRF value of a u32
314 * @key: the hsiphash key
316 u32
hsiphash_1u32(const u32 first
, const hsiphash_key_t
*key
)
322 EXPORT_SYMBOL(hsiphash_1u32
);
325 * hsiphash_2u32 - compute 32-bit hsiphash PRF value of 2 u32
327 * @second: second u32
328 * @key: the hsiphash key
330 u32
hsiphash_2u32(const u32 first
, const u32 second
, const hsiphash_key_t
*key
)
332 u64 combined
= (u64
)second
<< 32 | first
;
339 EXPORT_SYMBOL(hsiphash_2u32
);
342 * hsiphash_3u32 - compute 32-bit hsiphash PRF value of 3 u32
344 * @second: second u32
346 * @key: the hsiphash key
348 u32
hsiphash_3u32(const u32 first
, const u32 second
, const u32 third
,
349 const hsiphash_key_t
*key
)
351 u64 combined
= (u64
)second
<< 32 | first
;
359 EXPORT_SYMBOL(hsiphash_3u32
);
362 * hsiphash_4u32 - compute 32-bit hsiphash PRF value of 4 u32
364 * @second: second u32
367 * @key: the hsiphash key
369 u32
hsiphash_4u32(const u32 first
, const u32 second
, const u32 third
,
370 const u32 forth
, const hsiphash_key_t
*key
)
372 u64 combined
= (u64
)second
<< 32 | first
;
377 combined
= (u64
)forth
<< 32 | third
;
383 EXPORT_SYMBOL(hsiphash_4u32
);
385 #define HSIPROUND HSIPHASH_PERMUTATION(v0, v1, v2, v3)
387 #define HPREAMBLE(len) \
388 u32 v0 = HSIPHASH_CONST_0; \
389 u32 v1 = HSIPHASH_CONST_1; \
390 u32 v2 = HSIPHASH_CONST_2; \
391 u32 v3 = HSIPHASH_CONST_3; \
392 u32 b = ((u32)(len)) << 24; \
408 #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
409 u32
__hsiphash_aligned(const void *data
, size_t len
, const hsiphash_key_t
*key
)
411 const u8
*end
= data
+ len
- (len
% sizeof(u32
));
412 const u8 left
= len
& (sizeof(u32
) - 1);
415 for (; data
!= end
; data
+= sizeof(u32
)) {
416 m
= le32_to_cpup(data
);
422 case 3: b
|= ((u32
)end
[2]) << 16; fallthrough
;
423 case 2: b
|= le16_to_cpup(data
); break;
428 EXPORT_SYMBOL(__hsiphash_aligned
);
431 u32
__hsiphash_unaligned(const void *data
, size_t len
,
432 const hsiphash_key_t
*key
)
434 const u8
*end
= data
+ len
- (len
% sizeof(u32
));
435 const u8 left
= len
& (sizeof(u32
) - 1);
438 for (; data
!= end
; data
+= sizeof(u32
)) {
439 m
= get_unaligned_le32(data
);
445 case 3: b
|= ((u32
)end
[2]) << 16; fallthrough
;
446 case 2: b
|= get_unaligned_le16(end
); break;
451 EXPORT_SYMBOL(__hsiphash_unaligned
);
454 * hsiphash_1u32 - compute 32-bit hsiphash PRF value of a u32
456 * @key: the hsiphash key
458 u32
hsiphash_1u32(const u32 first
, const hsiphash_key_t
*key
)
466 EXPORT_SYMBOL(hsiphash_1u32
);
469 * hsiphash_2u32 - compute 32-bit hsiphash PRF value of 2 u32
471 * @second: second u32
472 * @key: the hsiphash key
474 u32
hsiphash_2u32(const u32 first
, const u32 second
, const hsiphash_key_t
*key
)
485 EXPORT_SYMBOL(hsiphash_2u32
);
488 * hsiphash_3u32 - compute 32-bit hsiphash PRF value of 3 u32
490 * @second: second u32
492 * @key: the hsiphash key
494 u32
hsiphash_3u32(const u32 first
, const u32 second
, const u32 third
,
495 const hsiphash_key_t
*key
)
509 EXPORT_SYMBOL(hsiphash_3u32
);
512 * hsiphash_4u32 - compute 32-bit hsiphash PRF value of 4 u32
514 * @second: second u32
517 * @key: the hsiphash key
519 u32
hsiphash_4u32(const u32 first
, const u32 second
, const u32 third
,
520 const u32 forth
, const hsiphash_key_t
*key
)
537 EXPORT_SYMBOL(hsiphash_4u32
);