aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/siphash.txt
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2017-01-08 07:54:00 -0500
committerDavid S. Miller <davem@davemloft.net>2017-01-09 13:58:57 -0500
commit2c956a60778cbb6a27e0c7a8a52a91378c90e1d1 (patch)
treefdb29d3e887add166969e8a15c883b0c2c9ab605 /Documentation/siphash.txt
parenteafea7390e597f766927d1ba7459f3904b0b9194 (diff)
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>
Diffstat (limited to 'Documentation/siphash.txt')
-rw-r--r--Documentation/siphash.txt100
1 files changed, 100 insertions, 0 deletions
diff --git a/Documentation/siphash.txt b/Documentation/siphash.txt
new file mode 100644
index 000000000000..e8e6ddbbaab4
--- /dev/null
+++ b/Documentation/siphash.txt
@@ -0,0 +1,100 @@
1 SipHash - a short input PRF
2-----------------------------------------------
3Written by Jason A. Donenfeld <jason@zx2c4.com>
4
5SipHash is a cryptographically secure PRF -- a keyed hash function -- that
6performs very well for short inputs, hence the name. It was designed by
7cryptographers Daniel J. Bernstein and Jean-Philippe Aumasson. It is intended
8as a replacement for some uses of: `jhash`, `md5_transform`, `sha_transform`,
9and so forth.
10
11SipHash takes a secret key filled with randomly generated numbers and either
12an input buffer or several input integers. It spits out an integer that is
13indistinguishable from random. You may then use that integer as part of secure
14sequence numbers, secure cookies, or mask it off for use in a hash table.
15
161. Generating a key
17
18Keys should always be generated from a cryptographically secure source of
19random numbers, either using get_random_bytes or get_random_once:
20
21siphash_key_t key;
22get_random_bytes(&key, sizeof(key));
23
24If you're not deriving your key from here, you're doing it wrong.
25
262. Using the functions
27
28There are two variants of the function, one that takes a list of integers, and
29one that takes a buffer:
30
31u64 siphash(const void *data, size_t len, const siphash_key_t *key);
32
33And:
34
35u64 siphash_1u64(u64, const siphash_key_t *key);
36u64 siphash_2u64(u64, u64, const siphash_key_t *key);
37u64 siphash_3u64(u64, u64, u64, const siphash_key_t *key);
38u64 siphash_4u64(u64, u64, u64, u64, const siphash_key_t *key);
39u64 siphash_1u32(u32, const siphash_key_t *key);
40u64 siphash_2u32(u32, u32, const siphash_key_t *key);
41u64 siphash_3u32(u32, u32, u32, const siphash_key_t *key);
42u64 siphash_4u32(u32, u32, u32, u32, const siphash_key_t *key);
43
44If you pass the generic siphash function something of a constant length, it
45will constant fold at compile-time and automatically choose one of the
46optimized functions.
47
483. Hashtable key function usage:
49
50struct some_hashtable {
51 DECLARE_HASHTABLE(hashtable, 8);
52 siphash_key_t key;
53};
54
55void init_hashtable(struct some_hashtable *table)
56{
57 get_random_bytes(&table->key, sizeof(table->key));
58}
59
60static inline hlist_head *some_hashtable_bucket(struct some_hashtable *table, struct interesting_input *input)
61{
62 return &table->hashtable[siphash(input, sizeof(*input), &table->key) & (HASH_SIZE(table->hashtable) - 1)];
63}
64
65You may then iterate like usual over the returned hash bucket.
66
674. Security
68
69SipHash has a very high security margin, with its 128-bit key. So long as the
70key is kept secret, it is impossible for an attacker to guess the outputs of
71the function, even if being able to observe many outputs, since 2^128 outputs
72is significant.
73
74Linux implements the "2-4" variant of SipHash.
75
765. Struct-passing Pitfalls
77
78Often times the XuY functions will not be large enough, and instead you'll
79want to pass a pre-filled struct to siphash. When doing this, it's important
80to always ensure the struct has no padding holes. The easiest way to do this
81is to simply arrange the members of the struct in descending order of size,
82and to use offsetendof() instead of sizeof() for getting the size. For
83performance reasons, if possible, it's probably a good thing to align the
84struct to the right boundary. Here's an example:
85
86const struct {
87 struct in6_addr saddr;
88 u32 counter;
89 u16 dport;
90} __aligned(SIPHASH_ALIGNMENT) combined = {
91 .saddr = *(struct in6_addr *)saddr,
92 .counter = counter,
93 .dport = dport
94};
95u64 h = siphash(&combined, offsetofend(typeof(combined), dport), &secret);
96
976. Resources
98
99Read the SipHash paper if you're interested in learning more:
100https://131002.net/siphash/siphash.pdf