5 .\" (c) 2024 Straylight/Edgeware
8 .\"----- Licensing notice ---------------------------------------------------
10 .\" This file is part of the mLib utilities library.
12 .\" mLib is free software: you can redistribute it and/or modify it under
13 .\" the terms of the GNU Library General Public License as published by
14 .\" the Free Software Foundation; either version 2 of the License, or (at
15 .\" your option) any later version.
17 .\" mLib is distributed in the hope that it will be useful, but WITHOUT
18 .\" ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 .\" FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 .\" License for more details.
22 .\" You should have received a copy of the GNU Library General Public
23 .\" License along with mLib. If not, write to the Free Software
24 .\" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 .\"--------------------------------------------------------------------------
28 .so ../defs.man \" @@@PRE@@@
30 .\"--------------------------------------------------------------------------
31 .TH siphash 3mLib "14 April 2024" "Straylight/Edgeware" "mLib utilities library"
47 .\"--------------------------------------------------------------------------
50 .B "#include <mLib/siphash.h>"
52 .B "struct siphash_key { kludge64 k0, k1; };"
53 .B "struct siphash { ...\& };"
55 .B "#define SIPHASH_KEYSZ 16"
56 .B "#define SIPHASH_BLKSZ 8"
58 .BI "void siphash_setkey(struct siphash_key *" k ", const octet *" p );
60 .ta \w'\fBkludge64 siphash('u
61 .BI "kludge64 siphash(const struct siphash_key *" k ,
62 .BI " const void *" p ", size_t " sz );
63 .ta \w'\fBvoid SIPHASH('u
64 .BI "void SIPHASH(const struct siphash_key *" k ", kludge64 &" z_out ,
65 .BI " const void *" p ", size_t " sz );
67 .BI "void siphash_init(struct siphash *" s ", const struct siphash_key *" k );
68 .BI "void siphash_hash(struct siphash *" s ", const void *" p ", size_t " sz );
69 .BI "kludge64 siphash_done(struct siphash *" s );
71 .BI "void SIPHASH_INIT(struct siphash *" s ", const struct siphash_key *" k );
72 .BI "void SIPHASH_WORD(struct siphash *" s ", kludge64 " m );
73 .ta \w'\fBvoid SIPHASH_FINAL('u
74 .BI "void SIPHASH_FINAL(struct siphash *" s
75 .BI " const void *" p ", unsigned " n ", size_t " msz );
78 .\"--------------------------------------------------------------------------
81 SipHash is a cryptographic pseudorandom function (PRF)
82 and message authentication code
83 designed in 2012 by Jean-Philippe Aumasson and Daniel J.\& Bernstein
84 as a keyed hash function to be used to implement data structures
85 to defend against malicious input.
86 It therefore provides similar benefits to mLib's
89 Specifically, this module implements the SipHash-2-4 variant
90 described in the original paper.
98 .BR SIPHASH_KEYSZ "\ =\ 16"
99 bytes (128 bits) of random data,
108 .B struct siphash_key
110 The main limit on security of the hash function
111 will come from the randomness of the key;
114 function is likely unacceptable.
116 Once a key is prepared, messages can be hashed.
117 The simplest interface is the
120 pass in the prepared key and the buffer containing the message,
121 and it will return the computed hash.
122 Slightly more complex, but possibly faster,
125 macro will leave the computed hash value in
129 this can be readily converted to a more convenient type using the
133 for more details about the
137 If it's not convenient to arrange that
138 the message data is in a single buffer,
139 then the message can be processed in pieces.
142 function intializes a hashing state
143 in a structure of type
144 .BR "struct siphash" .
145 The pieces of the message can now be presented, in order,
149 which will update the hashing state as necessary.
150 The pieces and their sizes do not have to be aligned
151 in any particular way.
154 function computes and returns the final result,
156 the concatenation of the message pieces, in order.
157 The hashing state is clobbered as a result,
158 and will no longer produce useful results
159 (though no undefined behaviour will result).
161 Finally, there is a low-level interface, which is harder to use.
162 It is provided primarily for the benefit of the
165 but is made available to client programs
166 in case it turns out to be useful.
167 The internal state managed by the internal macros
178 will initialize these variables appropriately
179 to begin processing a fresh message,
184 updates the state given the next 64-bit word of the message;
185 SipHash uses a little-endian convention,
186 so the word should be read using
189 When only seven or fewer bytes of the message remain,
192 to compute the message hash.
193 This expects, in addition to the four state variables,
196 to the tail of the message,
200 and the overall message length
203 the hash is returned in the
208 .SS "Comparison with unihash(3)"
213 perform similar functions and were designed with similar motivations,
214 it's natural to compare them.
216 SipHash is newer \(en by nearly a decade.
218 returns a 32-bit hash;
220 returns a 64-bit hash,
221 so will certainly be preferable for huge data structures.
223 SipHash can be (and, here, has been) implemented
224 without leaking message or other secret data into
225 caches or other microarchitectural state.
228 does have these kinds of leaks.
229 The consequences of compromising a hashing key are
230 generally limited to denial of service;
231 furthermore, microarchitectural leaks can only be exploited by software
232 sharing hardware resources with the victim,
233 and locally executing software generally has other,
239 if security agaist local adversaries is important,
242 In terms of performace,
244 seems faster than SipHash on very short messages
245 \(en up to about 35 bytes.
246 Despite the designers' efforts,
247 SipHash has significant finalization overhead,
248 which are better amortized over longer messages;
249 as a result, SipHash performance improves with message length.
252 has no per-message startup or finalization overhead,
255 with message length, mostly as a result of poorer cache utilization.
257 For now, based on these findings,
258 mLib continues to use
262 hashtable implementation.
264 .\"--------------------------------------------------------------------------
272 Jean-Philippe Aumasson and Daniel J.\& Bernstein,
273 .IR "SipHash: a fast short-input PRF" ,
276 .\"--------------------------------------------------------------------------
279 Mark Wooding (mdw@distorted.org.uk).
281 .\"----- That's all, folks --------------------------------------------------