siphash: add cryptographically secure PRF
SipHash is a 64-bit keyed hash function that is actually a
cryptographically secure PRF, like HMAC. Except SipHash is super fast,
and is meant to be used as a hashtable keyed lookup function, or as a
general PRF for short input use cases, such as sequence numbers or RNG
chaining.
For the first usage:
There are a variety of attacks known as "hashtable poisoning" in which an
attacker forms some data such that the hash of that data will be the
same, and then preceeds to fill up all entries of a hashbucket. This is
a realistic and well-known denial-of-service vector. Currently
hashtables use jhash, which is fast but not secure, and some kind of
rotating key scheme (or none at all, which isn't good). SipHash is meant
as a replacement for jhash in these cases.
There are a modicum of places in the kernel that are vulnerable to
hashtable poisoning attacks, either via userspace vectors or network
vectors, and there's not a reliable mechanism inside the kernel at the
moment to fix it. The first step toward fixing these issues is actually
getting a secure primitive into the kernel for developers to use. Then
we can, bit by bit, port things over to it as deemed appropriate.
While SipHash is extremely fast for a cryptographically secure function,
it is likely a bit slower than the insecure jhash, and so replacements
will be evaluated on a case-by-case basis based on whether or not the
difference in speed is negligible and whether or not the current jhash usage
poses a real security risk.
For the second usage:
A few places in the kernel are using MD5 or SHA1 for creating secure
sequence numbers, syn cookies, port numbers, or fast random numbers.
SipHash is a faster and more fitting, and more secure replacement for MD5
in those situations. Replacing MD5 and SHA1 with SipHash for these uses is
obvious and straight-forward, and so is submitted along with this patch
series. There shouldn't be much of a debate over its efficacy.
Dozens of languages are already using this internally for their hash
tables and PRFs. Some of the BSDs already use this in their kernels.
SipHash is a widely known high-speed solution to a widely known set of
problems, and it's time we catch-up.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Reviewed-by: Jean-Philippe Aumasson <jeanphilippe.aumasson@gmail.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Eric Biggers <ebiggers3@gmail.com>
Cc: David Laight <David.Laight@aculab.com>
Cc: Eric Dumazet <eric.dumazet@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-01-08 19:54:00 +07:00
|
|
|
SipHash - a short input PRF
|
|
|
|
-----------------------------------------------
|
|
|
|
Written by Jason A. Donenfeld <jason@zx2c4.com>
|
|
|
|
|
|
|
|
SipHash is a cryptographically secure PRF -- a keyed hash function -- that
|
|
|
|
performs very well for short inputs, hence the name. It was designed by
|
|
|
|
cryptographers Daniel J. Bernstein and Jean-Philippe Aumasson. It is intended
|
|
|
|
as a replacement for some uses of: `jhash`, `md5_transform`, `sha_transform`,
|
|
|
|
and so forth.
|
|
|
|
|
|
|
|
SipHash takes a secret key filled with randomly generated numbers and either
|
|
|
|
an input buffer or several input integers. It spits out an integer that is
|
|
|
|
indistinguishable from random. You may then use that integer as part of secure
|
|
|
|
sequence numbers, secure cookies, or mask it off for use in a hash table.
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
siphash_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:
|
|
|
|
|
|
|
|
u64 siphash(const void *data, size_t len, const siphash_key_t *key);
|
|
|
|
|
|
|
|
And:
|
|
|
|
|
|
|
|
u64 siphash_1u64(u64, const siphash_key_t *key);
|
|
|
|
u64 siphash_2u64(u64, u64, const siphash_key_t *key);
|
|
|
|
u64 siphash_3u64(u64, u64, u64, const siphash_key_t *key);
|
|
|
|
u64 siphash_4u64(u64, u64, u64, u64, const siphash_key_t *key);
|
|
|
|
u64 siphash_1u32(u32, const siphash_key_t *key);
|
|
|
|
u64 siphash_2u32(u32, u32, const siphash_key_t *key);
|
|
|
|
u64 siphash_3u32(u32, u32, u32, const siphash_key_t *key);
|
|
|
|
u64 siphash_4u32(u32, u32, u32, u32, const siphash_key_t *key);
|
|
|
|
|
|
|
|
If you pass the generic siphash 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);
|
|
|
|
siphash_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[siphash(input, sizeof(*input), &table->key) & (HASH_SIZE(table->hashtable) - 1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
You may then iterate like usual over the returned hash bucket.
|
|
|
|
|
|
|
|
4. Security
|
|
|
|
|
|
|
|
SipHash has a very high security margin, with its 128-bit key. So long as the
|
|
|
|
key is kept secret, it is impossible for an attacker to guess the outputs of
|
|
|
|
the function, even if being able to observe many outputs, since 2^128 outputs
|
|
|
|
is significant.
|
|
|
|
|
|
|
|
Linux implements the "2-4" variant of SipHash.
|
|
|
|
|
|
|
|
5. Struct-passing Pitfalls
|
|
|
|
|
|
|
|
Often times the XuY functions will not be large enough, and instead you'll
|
|
|
|
want to pass a pre-filled struct to siphash. When doing this, it's important
|
|
|
|
to always ensure the struct has no padding holes. The easiest way to do this
|
|
|
|
is to simply arrange the members of the struct in descending order of size,
|
|
|
|
and to use offsetendof() instead of sizeof() for getting the size. For
|
|
|
|
performance reasons, if possible, it's probably a good thing to align the
|
|
|
|
struct to the right boundary. Here's an example:
|
|
|
|
|
|
|
|
const struct {
|
|
|
|
struct in6_addr saddr;
|
|
|
|
u32 counter;
|
|
|
|
u16 dport;
|
|
|
|
} __aligned(SIPHASH_ALIGNMENT) combined = {
|
|
|
|
.saddr = *(struct in6_addr *)saddr,
|
|
|
|
.counter = counter,
|
|
|
|
.dport = dport
|
|
|
|
};
|
|
|
|
u64 h = siphash(&combined, offsetofend(typeof(combined), dport), &secret);
|
|
|
|
|
|
|
|
6. Resources
|
|
|
|
|
|
|
|
Read the SipHash paper if you're interested in learning more:
|
|
|
|
https://131002.net/siphash/siphash.pdf
|
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 <Jason@zx2c4.com>
Reviewed-by: Jean-Philippe Aumasson <jeanphilippe.aumasson@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2017-01-08 19:54:01 +07:00
|
|
|
|
|
|
|
|
|
|
|
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
|
|
|
|
|
|
|
|
HalfSipHash - SipHash's insecure younger cousin
|
|
|
|
-----------------------------------------------
|
|
|
|
Written by Jason A. Donenfeld <jason@zx2c4.com>
|
|
|
|
|
|
|
|
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.
|