forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
siphash: implement HalfSipHash1-3 for hash tables
HalfSipHash, or hsiphash, is a shortened version of SipHash, which generates 32-bit outputs using a weaker 64-bit key. It has *much* lower security margins, and shouldn't be used for anything too sensitive, but it could be used as a hashtable key function replacement, if the output is never exposed, and if the security requirement is not too high. The goal is to make this something that performance-critical jhash users would be willing to use. On 64-bit machines, HalfSipHash1-3 is slower than SipHash1-3, so we alias SipHash1-3 to HalfSipHash1-3 on those systems. 64-bit x86_64: [ 0.509409] test_siphash: SipHash2-4 cycles: 4049181 [ 0.510650] test_siphash: SipHash1-3 cycles: 2512884 [ 0.512205] test_siphash: HalfSipHash1-3 cycles: 3429920 [ 0.512904] test_siphash: JenkinsHash cycles: 978267 So, we map hsiphash() -> SipHash1-3 32-bit x86: [ 0.509868] test_siphash: SipHash2-4 cycles: 14812892 [ 0.513601] test_siphash: SipHash1-3 cycles: 9510710 [ 0.515263] test_siphash: HalfSipHash1-3 cycles: 3856157 [ 0.515952] test_siphash: JenkinsHash cycles: 1148567 So, we map hsiphash() -> HalfSipHash1-3 hsiphash() is roughly 3 times slower than jhash(), but comes with a considerable security improvement. Signed-off-by: Jason A. Donenfeld <[email protected]> Reviewed-by: Jean-Philippe Aumasson <[email protected]> Signed-off-by: David S. Miller <[email protected]>
- Loading branch information
Showing
4 changed files
with
546 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,3 +98,78 @@ u64 h = siphash(&combined, offsetofend(typeof(combined), dport), &secret); | |
|
||
Read the SipHash paper if you're interested in learning more: | ||
https://131002.net/siphash/siphash.pdf | ||
|
||
|
||
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~ | ||
|
||
HalfSipHash - SipHash's insecure younger cousin | ||
----------------------------------------------- | ||
Written by Jason A. Donenfeld <[email protected]> | ||
|
||
On the off-chance that SipHash is not fast enough for your needs, you might be | ||
able to justify using HalfSipHash, a terrifying but potentially useful | ||
possibility. HalfSipHash cuts SipHash's rounds down from "2-4" to "1-3" and, | ||
even scarier, uses an easily brute-forcable 64-bit key (with a 32-bit output) | ||
instead of SipHash's 128-bit key. However, this may appeal to some | ||
high-performance `jhash` users. | ||
|
||
Danger! | ||
|
||
Do not ever use HalfSipHash except for as a hashtable key function, and only | ||
then when you can be absolutely certain that the outputs will never be | ||
transmitted out of the kernel. This is only remotely useful over `jhash` as a | ||
means of mitigating hashtable flooding denial of service attacks. | ||
|
||
1. Generating a key | ||
|
||
Keys should always be generated from a cryptographically secure source of | ||
random numbers, either using get_random_bytes or get_random_once: | ||
|
||
hsiphash_key_t key; | ||
get_random_bytes(&key, sizeof(key)); | ||
|
||
If you're not deriving your key from here, you're doing it wrong. | ||
|
||
2. Using the functions | ||
|
||
There are two variants of the function, one that takes a list of integers, and | ||
one that takes a buffer: | ||
|
||
u32 hsiphash(const void *data, size_t len, const hsiphash_key_t *key); | ||
|
||
And: | ||
|
||
u32 hsiphash_1u32(u32, const hsiphash_key_t *key); | ||
u32 hsiphash_2u32(u32, u32, const hsiphash_key_t *key); | ||
u32 hsiphash_3u32(u32, u32, u32, const hsiphash_key_t *key); | ||
u32 hsiphash_4u32(u32, u32, u32, u32, const hsiphash_key_t *key); | ||
|
||
If you pass the generic hsiphash function something of a constant length, it | ||
will constant fold at compile-time and automatically choose one of the | ||
optimized functions. | ||
|
||
3. Hashtable key function usage: | ||
|
||
struct some_hashtable { | ||
DECLARE_HASHTABLE(hashtable, 8); | ||
hsiphash_key_t key; | ||
}; | ||
|
||
void init_hashtable(struct some_hashtable *table) | ||
{ | ||
get_random_bytes(&table->key, sizeof(table->key)); | ||
} | ||
|
||
static inline hlist_head *some_hashtable_bucket(struct some_hashtable *table, struct interesting_input *input) | ||
{ | ||
return &table->hashtable[hsiphash(input, sizeof(*input), &table->key) & (HASH_SIZE(table->hashtable) - 1)]; | ||
} | ||
|
||
You may then iterate like usual over the returned hash bucket. | ||
|
||
4. Performance | ||
|
||
HalfSipHash is roughly 3 times slower than JenkinsHash. For many replacements, | ||
this will not be a problem, as the hashtable lookup isn't the bottleneck. And | ||
in general, this is probably a good sacrifice to make for the security and DoS | ||
resistance of HalfSipHash. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.