Linux 6.10-rc6
[linux-2.6-block.git] / Documentation / security / siphash.rst
CommitLineData
9135bf4d
MCC
1===========================
2SipHash - a short input PRF
3===========================
4
5:Author: Written by Jason A. Donenfeld <jason@zx2c4.com>
2c956a60
JD
6
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
6b0b0fa2 10as a replacement for some uses of: `jhash`, `md5_transform`, `sha1_transform`,
2c956a60
JD
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
9135bf4d
MCC
18Generating a key
19================
2c956a60
JD
20
21Keys should always be generated from a cryptographically secure source of
9135bf4d 22random numbers, either using get_random_bytes or get_random_once::
2c956a60 23
9135bf4d
MCC
24 siphash_key_t key;
25 get_random_bytes(&key, sizeof(key));
2c956a60
JD
26
27If you're not deriving your key from here, you're doing it wrong.
28
9135bf4d
MCC
29Using the functions
30===================
2c956a60
JD
31
32There are two variants of the function, one that takes a list of integers, and
9135bf4d 33one that takes a buffer::
2c956a60 34
9135bf4d 35 u64 siphash(const void *data, size_t len, const siphash_key_t *key);
2c956a60 36
9135bf4d 37And::
2c956a60 38
9135bf4d
MCC
39 u64 siphash_1u64(u64, const siphash_key_t *key);
40 u64 siphash_2u64(u64, u64, const siphash_key_t *key);
41 u64 siphash_3u64(u64, u64, u64, const siphash_key_t *key);
42 u64 siphash_4u64(u64, u64, u64, u64, const siphash_key_t *key);
43 u64 siphash_1u32(u32, const siphash_key_t *key);
44 u64 siphash_2u32(u32, u32, const siphash_key_t *key);
45 u64 siphash_3u32(u32, u32, u32, const siphash_key_t *key);
46 u64 siphash_4u32(u32, u32, u32, u32, const siphash_key_t *key);
2c956a60
JD
47
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
9135bf4d 52Hashtable key function usage::
2c956a60 53
9135bf4d
MCC
54 struct some_hashtable {
55 DECLARE_HASHTABLE(hashtable, 8);
56 siphash_key_t key;
57 };
2c956a60 58
9135bf4d
MCC
59 void init_hashtable(struct some_hashtable *table)
60 {
61 get_random_bytes(&table->key, sizeof(table->key));
62 }
2c956a60 63
9135bf4d
MCC
64 static inline hlist_head *some_hashtable_bucket(struct some_hashtable *table, struct interesting_input *input)
65 {
66 return &table->hashtable[siphash(input, sizeof(*input), &table->key) & (HASH_SIZE(table->hashtable) - 1)];
67 }
2c956a60
JD
68
69You may then iterate like usual over the returned hash bucket.
70
9135bf4d
MCC
71Security
72========
2c956a60
JD
73
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
9135bf4d
MCC
81Struct-passing Pitfalls
82=======================
2c956a60
JD
83
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,
12fe4343 88and to use offsetofend() instead of sizeof() for getting the size. For
2c956a60 89performance reasons, if possible, it's probably a good thing to align the
9135bf4d
MCC
90struct to the right boundary. Here's an example::
91
92 const struct {
93 struct in6_addr saddr;
94 u32 counter;
95 u16 dport;
96 } __aligned(SIPHASH_ALIGNMENT) combined = {
97 .saddr = *(struct in6_addr *)saddr,
98 .counter = counter,
99 .dport = dport
100 };
101 u64 h = siphash(&combined, offsetofend(typeof(combined), dport), &secret);
102
103Resources
104=========
2c956a60
JD
105
106Read the SipHash paper if you're interested in learning more:
107https://131002.net/siphash/siphash.pdf
1ae2324f 108
9135bf4d 109-------------------------------------------------------------------------------
1ae2324f 110
9135bf4d 111===============================================
1ae2324f 112HalfSipHash - SipHash's insecure younger cousin
9135bf4d
MCC
113===============================================
114
115:Author: Written by Jason A. Donenfeld <jason@zx2c4.com>
1ae2324f
JD
116
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
5a7e470e
EB
124HalfSipHash support is provided through the "hsiphash" family of functions.
125
ec862155 126.. warning::
5a7e470e
EB
127 Do not ever use the hsiphash functions except for as a hashtable key
128 function, and only then when you can be absolutely certain that the outputs
129 will never be transmitted out of the kernel. This is only remotely useful
130 over `jhash` as a means of mitigating hashtable flooding denial of service
ec862155 131 attacks.
1ae2324f 132
5a7e470e
EB
133On 64-bit kernels, the hsiphash functions actually implement SipHash-1-3, a
134reduced-round variant of SipHash, instead of HalfSipHash-1-3. This is because in
13564-bit code, SipHash-1-3 is no slower than HalfSipHash-1-3, and can be faster.
136Note, this does *not* mean that in 64-bit kernels the hsiphash functions are the
137same as the siphash ones, or that they are secure; the hsiphash functions still
138use a less secure reduced-round algorithm and truncate their outputs to 32
139bits.
140
141Generating a hsiphash key
142=========================
1ae2324f
JD
143
144Keys should always be generated from a cryptographically secure source of
2fbfeb4f 145random numbers, either using get_random_bytes or get_random_once::
1ae2324f 146
2fbfeb4f
BS
147 hsiphash_key_t key;
148 get_random_bytes(&key, sizeof(key));
1ae2324f
JD
149
150If you're not deriving your key from here, you're doing it wrong.
151
5a7e470e
EB
152Using the hsiphash functions
153============================
1ae2324f
JD
154
155There are two variants of the function, one that takes a list of integers, and
9135bf4d 156one that takes a buffer::
1ae2324f 157
9135bf4d 158 u32 hsiphash(const void *data, size_t len, const hsiphash_key_t *key);
1ae2324f 159
9135bf4d 160And::
1ae2324f 161
9135bf4d
MCC
162 u32 hsiphash_1u32(u32, const hsiphash_key_t *key);
163 u32 hsiphash_2u32(u32, u32, const hsiphash_key_t *key);
164 u32 hsiphash_3u32(u32, u32, u32, const hsiphash_key_t *key);
165 u32 hsiphash_4u32(u32, u32, u32, u32, const hsiphash_key_t *key);
1ae2324f
JD
166
167If you pass the generic hsiphash function something of a constant length, it
168will constant fold at compile-time and automatically choose one of the
169optimized functions.
170
9135bf4d
MCC
171Hashtable key function usage
172============================
173
174::
1ae2324f 175
9135bf4d
MCC
176 struct some_hashtable {
177 DECLARE_HASHTABLE(hashtable, 8);
178 hsiphash_key_t key;
179 };
1ae2324f 180
9135bf4d
MCC
181 void init_hashtable(struct some_hashtable *table)
182 {
183 get_random_bytes(&table->key, sizeof(table->key));
184 }
1ae2324f 185
9135bf4d
MCC
186 static inline hlist_head *some_hashtable_bucket(struct some_hashtable *table, struct interesting_input *input)
187 {
188 return &table->hashtable[hsiphash(input, sizeof(*input), &table->key) & (HASH_SIZE(table->hashtable) - 1)];
189 }
1ae2324f
JD
190
191You may then iterate like usual over the returned hash bucket.
192
9135bf4d
MCC
193Performance
194===========
1ae2324f 195
5a7e470e
EB
196hsiphash() is roughly 3 times slower than jhash(). For many replacements, this
197will not be a problem, as the hashtable lookup isn't the bottleneck. And in
198general, this is probably a good sacrifice to make for the security and DoS
199resistance of hsiphash().