blob: 9965821ab333495105c397c2d9c511b42d6d668d [file] [log] [blame]
Mauro Carvalho Chehab9135bf42017-05-17 10:55:321===========================
2SipHash - a short input PRF
3===========================
4
5:Author: Written by Jason A. Donenfeld <jason@zx2c4.com>
Jason A. Donenfeld2c956a62017-01-08 12:54:006
7SipHash is a cryptographically secure PRF -- a keyed hash function -- that
8performs very well for short inputs, hence the name. It was designed by
9cryptographers Daniel J. Bernstein and Jean-Philippe Aumasson. It is intended
10as a replacement for some uses of: `jhash`, `md5_transform`, `sha_transform`,
11and so forth.
12
13SipHash takes a secret key filled with randomly generated numbers and either
14an input buffer or several input integers. It spits out an integer that is
15indistinguishable from random. You may then use that integer as part of secure
16sequence numbers, secure cookies, or mask it off for use in a hash table.
17
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3218Generating a key
19================
Jason A. Donenfeld2c956a62017-01-08 12:54:0020
21Keys should always be generated from a cryptographically secure source of
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3222random numbers, either using get_random_bytes or get_random_once::
Jason A. Donenfeld2c956a62017-01-08 12:54:0023
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3224siphash_key_t key;
25get_random_bytes(&key, sizeof(key));
Jason A. Donenfeld2c956a62017-01-08 12:54:0026
27If you're not deriving your key from here, you're doing it wrong.
28
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3229Using the functions
30===================
Jason A. Donenfeld2c956a62017-01-08 12:54:0031
32There are two variants of the function, one that takes a list of integers, and
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3233one that takes a buffer::
Jason A. Donenfeld2c956a62017-01-08 12:54:0034
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3235u64 siphash(const void *data, size_t len, const siphash_key_t *key);
Jason A. Donenfeld2c956a62017-01-08 12:54:0036
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3237And::
Jason A. Donenfeld2c956a62017-01-08 12:54:0038
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3239u64 siphash_1u64(u64, const siphash_key_t *key);
40u64 siphash_2u64(u64, u64, const siphash_key_t *key);
41u64 siphash_3u64(u64, u64, u64, const siphash_key_t *key);
42u64 siphash_4u64(u64, u64, u64, u64, const siphash_key_t *key);
43u64 siphash_1u32(u32, const siphash_key_t *key);
44u64 siphash_2u32(u32, u32, const siphash_key_t *key);
45u64 siphash_3u32(u32, u32, u32, const siphash_key_t *key);
46u64 siphash_4u32(u32, u32, u32, u32, const siphash_key_t *key);
Jason A. Donenfeld2c956a62017-01-08 12:54:0047
48If you pass the generic siphash function something of a constant length, it
49will constant fold at compile-time and automatically choose one of the
50optimized functions.
51
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3252Hashtable key function usage::
Jason A. Donenfeld2c956a62017-01-08 12:54:0053
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3254struct some_hashtable {
55DECLARE_HASHTABLE(hashtable, 8);
56siphash_key_t key;
57};
Jason A. Donenfeld2c956a62017-01-08 12:54:0058
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3259void init_hashtable(struct some_hashtable *table)
60{
61get_random_bytes(&table->key, sizeof(table->key));
62}
Jason A. Donenfeld2c956a62017-01-08 12:54:0063
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3264static inline hlist_head *some_hashtable_bucket(struct some_hashtable *table, struct interesting_input *input)
65{
66return &table->hashtable[siphash(input, sizeof(*input), &table->key) & (HASH_SIZE(table->hashtable) - 1)];
67}
Jason A. Donenfeld2c956a62017-01-08 12:54:0068
69You may then iterate like usual over the returned hash bucket.
70
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3271Security
72========
Jason A. Donenfeld2c956a62017-01-08 12:54:0073
74SipHash has a very high security margin, with its 128-bit key. So long as the
75key is kept secret, it is impossible for an attacker to guess the outputs of
76the function, even if being able to observe many outputs, since 2^128 outputs
77is significant.
78
79Linux implements the "2-4" variant of SipHash.
80
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3281Struct-passing Pitfalls
82=======================
Jason A. Donenfeld2c956a62017-01-08 12:54:0083
84Often times the XuY functions will not be large enough, and instead you'll
85want to pass a pre-filled struct to siphash. When doing this, it's important
86to always ensure the struct has no padding holes. The easiest way to do this
87is to simply arrange the members of the struct in descending order of size,
88and to use offsetendof() instead of sizeof() for getting the size. For
89performance reasons, if possible, it's probably a good thing to align the
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3290struct to the right boundary. Here's an example::
Jason A. Donenfeld2c956a62017-01-08 12:54:0091
Mauro Carvalho Chehab9135bf42017-05-17 10:55:3292const struct {
93struct in6_addr saddr;
94u32 counter;
95u16 dport;
96} __aligned(SIPHASH_ALIGNMENT) combined = {
97.saddr = *(struct in6_addr *)saddr,
98.counter = counter,
99.dport = dport
100};
101u64 h = siphash(&combined, offsetofend(typeof(combined), dport), &secret);
Jason A. Donenfeld2c956a62017-01-08 12:54:00102
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32103Resources
104=========
Jason A. Donenfeld2c956a62017-01-08 12:54:00105
106Read the SipHash paper if you're interested in learning more:
107https://131002.net/siphash/siphash.pdf
Jason A. Donenfeld1ae23242017-01-08 12:54:01108
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32109-------------------------------------------------------------------------------
Jason A. Donenfeld1ae23242017-01-08 12:54:01110
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32111===============================================
Jason A. Donenfeld1ae23242017-01-08 12:54:01112HalfSipHash - SipHash's insecure younger cousin
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32113===============================================
114
115:Author: Written by Jason A. Donenfeld <jason@zx2c4.com>
Jason A. Donenfeld1ae23242017-01-08 12:54:01116
117On the off-chance that SipHash is not fast enough for your needs, you might be
118able to justify using HalfSipHash, a terrifying but potentially useful
119possibility. HalfSipHash cuts SipHash's rounds down from "2-4" to "1-3" and,
120even scarier, uses an easily brute-forcable 64-bit key (with a 32-bit output)
121instead of SipHash's 128-bit key. However, this may appeal to some
122high-performance `jhash` users.
123
124Danger!
125
126Do not ever use HalfSipHash except for as a hashtable key function, and only
127then when you can be absolutely certain that the outputs will never be
128transmitted out of the kernel. This is only remotely useful over `jhash` as a
129means of mitigating hashtable flooding denial of service attacks.
130
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32131Generating a key
132================
Jason A. Donenfeld1ae23242017-01-08 12:54:01133
134Keys should always be generated from a cryptographically secure source of
135random numbers, either using get_random_bytes or get_random_once:
136
137hsiphash_key_t key;
138get_random_bytes(&key, sizeof(key));
139
140If you're not deriving your key from here, you're doing it wrong.
141
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32142Using the functions
143===================
Jason A. Donenfeld1ae23242017-01-08 12:54:01144
145There are two variants of the function, one that takes a list of integers, and
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32146one that takes a buffer::
Jason A. Donenfeld1ae23242017-01-08 12:54:01147
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32148u32 hsiphash(const void *data, size_t len, const hsiphash_key_t *key);
Jason A. Donenfeld1ae23242017-01-08 12:54:01149
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32150And::
Jason A. Donenfeld1ae23242017-01-08 12:54:01151
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32152u32 hsiphash_1u32(u32, const hsiphash_key_t *key);
153u32 hsiphash_2u32(u32, u32, const hsiphash_key_t *key);
154u32 hsiphash_3u32(u32, u32, u32, const hsiphash_key_t *key);
155u32 hsiphash_4u32(u32, u32, u32, u32, const hsiphash_key_t *key);
Jason A. Donenfeld1ae23242017-01-08 12:54:01156
157If you pass the generic hsiphash function something of a constant length, it
158will constant fold at compile-time and automatically choose one of the
159optimized functions.
160
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32161Hashtable key function usage
162============================
Jason A. Donenfeld1ae23242017-01-08 12:54:01163
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32164::
Jason A. Donenfeld1ae23242017-01-08 12:54:01165
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32166struct some_hashtable {
167DECLARE_HASHTABLE(hashtable, 8);
168hsiphash_key_t key;
169};
Jason A. Donenfeld1ae23242017-01-08 12:54:01170
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32171void init_hashtable(struct some_hashtable *table)
172{
173get_random_bytes(&table->key, sizeof(table->key));
174}
175
176static inline hlist_head *some_hashtable_bucket(struct some_hashtable *table, struct interesting_input *input)
177{
178return &table->hashtable[hsiphash(input, sizeof(*input), &table->key) & (HASH_SIZE(table->hashtable) - 1)];
179}
Jason A. Donenfeld1ae23242017-01-08 12:54:01180
181You may then iterate like usual over the returned hash bucket.
182
Mauro Carvalho Chehab9135bf42017-05-17 10:55:32183Performance
184===========
Jason A. Donenfeld1ae23242017-01-08 12:54:01185
186HalfSipHash is roughly 3 times slower than JenkinsHash. For many replacements,
187this will not be a problem, as the hashtable lookup isn't the bottleneck. And
188in general, this is probably a good sacrifice to make for the security and DoS
189resistance of HalfSipHash.