Blame view

Documentation/siphash.txt 6.33 KB
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
1
2
3
4
5
  ===========================
  SipHash - a short input PRF
  ===========================
  
  :Author: Written by Jason A. Donenfeld <jason@zx2c4.com>
2c956a607   Jason A. Donenfeld   siphash: add cryp...
6
7
8
9
10
11
12
13
14
15
16
  
  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.
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
17
18
  Generating a key
  ================
2c956a607   Jason A. Donenfeld   siphash: add cryp...
19
20
  
  Keys should always be generated from a cryptographically secure source of
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
21
  random numbers, either using get_random_bytes or get_random_once::
2c956a607   Jason A. Donenfeld   siphash: add cryp...
22

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
23
24
  	siphash_key_t key;
  	get_random_bytes(&key, sizeof(key));
2c956a607   Jason A. Donenfeld   siphash: add cryp...
25
26
  
  If you're not deriving your key from here, you're doing it wrong.
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
27
28
  Using the functions
  ===================
2c956a607   Jason A. Donenfeld   siphash: add cryp...
29
30
  
  There are two variants of the function, one that takes a list of integers, and
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
31
  one that takes a buffer::
2c956a607   Jason A. Donenfeld   siphash: add cryp...
32

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
33
  	u64 siphash(const void *data, size_t len, const siphash_key_t *key);
2c956a607   Jason A. Donenfeld   siphash: add cryp...
34

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
35
  And::
2c956a607   Jason A. Donenfeld   siphash: add cryp...
36

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
37
38
39
40
41
42
43
44
  	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);
2c956a607   Jason A. Donenfeld   siphash: add cryp...
45
46
47
48
  
  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.
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
49
  Hashtable key function usage::
2c956a607   Jason A. Donenfeld   siphash: add cryp...
50

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
51
52
53
54
  	struct some_hashtable {
  		DECLARE_HASHTABLE(hashtable, 8);
  		siphash_key_t key;
  	};
2c956a607   Jason A. Donenfeld   siphash: add cryp...
55

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
56
57
58
59
  	void init_hashtable(struct some_hashtable *table)
  	{
  		get_random_bytes(&table->key, sizeof(table->key));
  	}
2c956a607   Jason A. Donenfeld   siphash: add cryp...
60

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
61
62
63
64
  	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)];
  	}
2c956a607   Jason A. Donenfeld   siphash: add cryp...
65
66
  
  You may then iterate like usual over the returned hash bucket.
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
67
68
  Security
  ========
2c956a607   Jason A. Donenfeld   siphash: add cryp...
69
70
71
72
73
74
75
  
  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.
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
76
77
  Struct-passing Pitfalls
  =======================
2c956a607   Jason A. Donenfeld   siphash: add cryp...
78
79
80
81
82
83
84
  
  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
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  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);
  
  Resources
  =========
2c956a607   Jason A. Donenfeld   siphash: add cryp...
100
101
102
  
  Read the SipHash paper if you're interested in learning more:
  https://131002.net/siphash/siphash.pdf
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
103

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
104
  -------------------------------------------------------------------------------
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
105

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
106
  ===============================================
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
107
  HalfSipHash - SipHash's insecure younger cousin
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
108
109
110
  ===============================================
  
  :Author: Written by Jason A. Donenfeld <jason@zx2c4.com>
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  
  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.
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
125
126
  Generating a key
  ================
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
127
128
129
130
131
132
133
134
  
  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.
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
135
136
  Using the functions
  ===================
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
137
138
  
  There are two variants of the function, one that takes a list of integers, and
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
139
  one that takes a buffer::
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
140

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
141
  	u32 hsiphash(const void *data, size_t len, const hsiphash_key_t *key);
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
142

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
143
  And::
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
144

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
145
146
147
148
  	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);
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
149
150
151
152
  
  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.
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
153
154
155
156
  Hashtable key function usage
  ============================
  
  ::
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
157

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
158
159
160
161
  	struct some_hashtable {
  		DECLARE_HASHTABLE(hashtable, 8);
  		hsiphash_key_t key;
  	};
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
162

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
163
164
165
166
  	void init_hashtable(struct some_hashtable *table)
  	{
  		get_random_bytes(&table->key, sizeof(table->key));
  	}
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
167

9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
168
169
170
171
  	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)];
  	}
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
172
173
  
  You may then iterate like usual over the returned hash bucket.
9135bf4dc   Mauro Carvalho Chehab   siphash.txt: stan...
174
175
  Performance
  ===========
1ae2324f7   Jason A. Donenfeld   siphash: implemen...
176
177
178
179
180
  
  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.