@@@ fltfmt mess
[mLib] / hash / siphash.3.in
CommitLineData
b1a20bee
MW
1.\" -*-nroff-*-
2.\"
3.\" Manual for SipHash
4.\"
5.\" (c) 2024 Straylight/Edgeware
6.\"
7.
8.\"----- Licensing notice ---------------------------------------------------
9.\"
10.\" This file is part of the mLib utilities library.
11.\"
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.
16.\"
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.
21.\"
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,
25.\" USA.
26.
27.\"--------------------------------------------------------------------------
28.so ../defs.man \" @@@PRE@@@
29.
30.\"--------------------------------------------------------------------------
31.TH siphash 3mLib "14 April 2024" "Straylight/Edgeware" "mLib utilities library"
32.\" @SIPHASH_KEYSZ
33.\" @SIPHASH_BLKSZ
34.
35.\" @siphash_setkey
36.
37.\" @siphash
38.\" @SIPHASH
39.
40.\" @siphash_init
41.\" @siphash_hash
42.\" @siphash_done
43.\" @SIPHASH_INIT
44.\" @SIPHASH_WORD
45.\" @SIPHASH_FINAL
46.
47.\"--------------------------------------------------------------------------
48.SH SYNOPSIS
49.nf
50.B "#include <mLib/siphash.h>"
51.PP
52.B "struct siphash_key { kludge64 k0, k1; };"
53.B "struct siphash { ...\& };"
54.PP
55.B "#define SIPHASH_KEYSZ 16"
56.B "#define SIPHASH_BLKSZ 8"
57.PP
58.BI "void siphash_setkey(struct siphash_key *" k ", const octet *" p );
59.PP
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 );
66.PP
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 );
70.PP
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 );
76.fi
77.
78.\"--------------------------------------------------------------------------
79.SH DESCRIPTION
80.
81SipHash is a cryptographic pseudorandom function (PRF)
82and message authentication code
83designed in 2012 by Jean-Philippe Aumasson and Daniel J.\& Bernstein
84as a keyed hash function to be used to implement data structures
85to defend against malicious input.
86It therefore provides similar benefits to mLib's
87.BR unihash (3)
88module.
89Specifically, this module implements the SipHash-2-4 variant
90described in the original paper.
91.PP
92Prior to hashing,
93a
94.I key
95must be prepared.
96To prepare a key,
97fill a buffer with
98.BR SIPHASH_KEYSZ "\ =\ 16"
99bytes (128 bits) of random data,
100and call
101.BR siphash_setkey .
102Alternatively,
103initialize the
104.B k0
105and
106.B k1
107members of the
108.B struct siphash_key
109structure directly.
110The main limit on security of the hash function
111will come from the randomness of the key;
112the C library's
113.BR rand (3)
114function is likely unacceptable.
115.PP
116Once a key is prepared, messages can be hashed.
117The simplest interface is the
118.B siphash
119function:
120pass in the prepared key and the buffer containing the message,
121and it will return the computed hash.
122Slightly more complex, but possibly faster,
123the
124.B SIPHASH
125macro will leave the computed hash value in
126.IR z_out .
127The hash has type
128.BR kludge64 :
129this can be readily converted to a more convenient type using the
130.BR GET64 (3) macro;
131see
132.BR bits (3)
133for more details about the
134.B kludge64
135type.
136.PP
137If it's not convenient to arrange that
138the message data is in a single buffer,
139then the message can be processed in pieces.
140The
141.B siphash_init
142function intializes a hashing state
143in a structure of type
144.BR "struct siphash" .
145The pieces of the message can now be presented, in order,
146to the
147.B siphash_hash
148function,
149which will update the hashing state as necessary.
150The pieces and their sizes do not have to be aligned
151in any particular way.
152Finally, the
153.B siphash_done
154function computes and returns the final result,
155which is the hash of
156the concatenation of the message pieces, in order.
157The hashing state is clobbered as a result,
158and will no longer produce useful results
159(though no undefined behaviour will result).
160.PP
161Finally, there is a low-level interface, which is harder to use.
162It is provided primarily for the benefit of the
163.B SIPHASH
164macro,
165but is made available to client programs
166in case it turns out to be useful.
167The internal state managed by the internal macros
168consists of four
169.B kludge64
170variables named
171.IR a ,
172.IR b ,
173.IR c ,
174and
175.IR d .
176The macro
177.B SIPHASH_INIT
178will initialize these variables appropriately
179to begin processing a fresh message,
180given a prepared key
181.IR k .
182The macro
183.B SIPHASH_WORD
184updates the state given the next 64-bit word of the message;
185SipHash uses a little-endian convention,
186so the word should be read using
187.BR LOAD64_L_ (3)
188or similar.
189When only seven or fewer bytes of the message remain,
190call the macro
191.B SIPHASH_FINAL
192to compute the message hash.
193This expects, in addition to the four state variables,
194a pointer
195.I p
196to the tail of the message,
197the length
198.I n
199of the tail,
200and the overall message length
201.IR msz ,
202in bytes;
203the hash is returned in the
204.B kludge64
205output variable
206.IR z_out .
207.
208.SS "Comparison with unihash(3)"
209Since
210SipHash
211and mLib's existing
212.BR unihash (3)
213perform similar functions and were designed with similar motivations,
214it's natural to compare them.
215.PP
216SipHash is newer \(en by nearly a decade.
217.B unihash
218returns a 32-bit hash;
219SipHash
220returns a 64-bit hash,
221so will certainly be preferable for huge data structures.
222.PP
223SipHash can be (and, here, has been) implemented
224without leaking message or other secret data into
225caches or other microarchitectural state.
226Alas,
227.B unihash
228does have these kinds of leaks.
229The consequences of compromising a hashing key are
230generally limited to denial of service;
231furthermore, microarchitectural leaks can only be exploited by software
232sharing hardware resources with the victim,
233and locally executing software generally has other,
234more effective means
235to deny service.
236In many cases,
237.B unihash
238is adequate;
239if security agaist local adversaries is important,
240use SipHash.
241.PP
242In terms of performace,
243.B unihash
244seems faster than SipHash on very short messages
245\(en up to about 35 bytes.
246Despite the designers' efforts,
247SipHash has significant finalization overhead,
248which are better amortized over longer messages;
249as a result, SipHash performance improves with message length.
250By contrast
251.B unihash
252has no per-message startup or finalization overhead,
253and performance
254.I decreases
255with message length, mostly as a result of poorer cache utilization.
256.PP
257For now, based on these findings,
258mLib continues to use
259.B unihash
260in its
261.BR sym (3)
262hashtable implementation.
263.
264.\"--------------------------------------------------------------------------
265.SH SEE ALSO
266.
267.BR crc32 (3),
268.BR siphash (3),
269.BR unihash (3),
270.BR mLib (3).
271.PP
272Jean-Philippe Aumasson and Daniel J.\& Bernstein,
273.IR "SipHash: a fast short-input PRF" ,
274INDOCRYPT 2012.
275.
276.\"--------------------------------------------------------------------------
277.SH AUTHOR
278.
279Mark Wooding (mdw@distorted.org.uk).
280.
281.\"----- That's all, folks --------------------------------------------------