2
2
* siphash.h - SipHash-2-4 in a single header file
3
3
* --------------------------------------------------------------------------
4
4
* Derived by William Ahern from the reference implementation[1] published[2]
5
- * by Jean-Philippe Aumasson and Daniel J. Berstein. Licensed in kind.
6
5
* by Jean-Philippe Aumasson and Daniel J. Berstein.
7
- * Minimal changes by Sebastian Pipping on top, details below.
6
+ * Minimal changes by Sebastian Pipping and Victor Stinner on top, see below.
8
7
* Licensed under the CC0 Public Domain Dedication license.
9
8
*
10
9
* 1. https://www.131002.net/siphash/siphash24.c
11
10
* 2. https://www.131002.net/siphash/
12
11
* --------------------------------------------------------------------------
13
12
* HISTORY:
14
13
*
15
- * 2017-06-18 (Sebastian Pipping)
16
- * - Address lack of stdint.h for Visual Studio 2003 to 2008
14
+ * 2017-07-25 (Vadim Zeitlin)
15
+ * - Fix use of SIPHASH_MAIN macro
16
+ *
17
+ * 2017-07-05 (Sebastian Pipping)
18
+ * - Use _SIP_ULL macro to not require a C++11 compiler if compiled as C++
19
+ * - Add const qualifiers at two places
20
+ * - Ensure <=80 characters line length (assuming tab width 4)
17
21
*
18
- * 2017-06-10 (Sebastian Pipping)
22
+ * 2017-06-23 (Victor Stinner)
23
+ * - Address Win64 compile warnings
24
+ *
25
+ * 2017-06-18 (Sebastian Pipping)
19
26
* - Clarify license note in the header
20
27
* - Address C89 issues:
21
28
* - Stop using inline keyword (and let compiler decide)
22
- * - Turn integer suffix ULL to UL
23
29
* - Replace _Bool by int
24
30
* - Turn macro siphash24 into a function
25
31
* - Address invalid conversion (void pointer) by explicit cast
32
+ * - Address lack of stdint.h for Visual Studio 2003 to 2008
26
33
* - Always expose sip24_valid (for self-tests)
27
34
*
28
35
* 2012-11-04 - Born. (William Ahern)
90
97
#endif
91
98
92
99
100
+ /*
101
+ * Workaround to not require a C++11 compiler for using ULL suffix
102
+ * if this code is included and compiled as C++; related GCC warning is:
103
+ * warning: use of C++11 long long integer constant [-Wlong-long]
104
+ */
105
+ #define _SIP_ULL (high , low ) (((uint64_t)high << 32) | low)
106
+
107
+
93
108
#define SIP_ROTL (x , b ) (uint64_t)(((x) << (b)) | ( (x) >> (64 - (b))))
94
109
95
110
#define SIP_U32TO8_LE (p , v ) \
@@ -169,11 +184,12 @@ static void sip_round(struct siphash *H, const int rounds) {
169
184
} /* sip_round() */
170
185
171
186
172
- static struct siphash * sip24_init (struct siphash * H , const struct sipkey * key ) {
173
- H -> v0 = 0x736f6d6570736575UL ^ key -> k [0 ];
174
- H -> v1 = 0x646f72616e646f6dUL ^ key -> k [1 ];
175
- H -> v2 = 0x6c7967656e657261UL ^ key -> k [0 ];
176
- H -> v3 = 0x7465646279746573UL ^ key -> k [1 ];
187
+ static struct siphash * sip24_init (struct siphash * H ,
188
+ const struct sipkey * key ) {
189
+ H -> v0 = _SIP_ULL (0x736f6d65U , 0x70736575U ) ^ key -> k [0 ];
190
+ H -> v1 = _SIP_ULL (0x646f7261U , 0x6e646f6dU ) ^ key -> k [1 ];
191
+ H -> v2 = _SIP_ULL (0x6c796765U , 0x6e657261U ) ^ key -> k [0 ];
192
+ H -> v3 = _SIP_ULL (0x74656462U , 0x79746573U ) ^ key -> k [1 ];
177
193
178
194
H -> p = H -> buf ;
179
195
H -> c = 0 ;
@@ -184,7 +200,8 @@ static struct siphash *sip24_init(struct siphash *H, const struct sipkey *key) {
184
200
185
201
#define sip_endof (a ) (&(a)[sizeof (a) / sizeof *(a)])
186
202
187
- static struct siphash * sip24_update (struct siphash * H , const void * src , size_t len ) {
203
+ static struct siphash * sip24_update (struct siphash * H , const void * src ,
204
+ size_t len ) {
188
205
const unsigned char * p = (const unsigned char * )src , * pe = p + len ;
189
206
uint64_t m ;
190
207
@@ -209,7 +226,7 @@ static struct siphash *sip24_update(struct siphash *H, const void *src, size_t l
209
226
210
227
211
228
static uint64_t sip24_final (struct siphash * H ) {
212
- char left = H -> p - H -> buf ;
229
+ const char left = ( char )( H -> p - H -> buf ) ;
213
230
uint64_t b = (H -> c + left ) << 56 ;
214
231
215
232
switch (left ) {
@@ -233,7 +250,8 @@ static uint64_t sip24_final(struct siphash *H) {
233
250
} /* sip24_final() */
234
251
235
252
236
- static uint64_t siphash24 (const void * src , size_t len , const struct sipkey * key ) {
253
+ static uint64_t siphash24 (const void * src , size_t len ,
254
+ const struct sipkey * key ) {
237
255
struct siphash state = SIPHASH_INITIALIZER ;
238
256
return sip24_final (sip24_update (sip24_init (& state , key ), src , len ));
239
257
} /* siphash24() */
@@ -321,10 +339,11 @@ static int sip24_valid(void) {
321
339
struct sipkey k ;
322
340
size_t i ;
323
341
324
- sip_tokey (& k , "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017" );
342
+ sip_tokey (& k , "\000\001\002\003\004\005\006\007\010\011"
343
+ "\012\013\014\015\016\017" );
325
344
326
345
for (i = 0 ; i < sizeof in ; ++ i ) {
327
- in [i ] = i ;
346
+ in [i ] = ( unsigned char ) i ;
328
347
329
348
if (siphash24 (in , i , & k ) != SIP_U8TO64_LE (vectors [i ]))
330
349
return 0 ;
@@ -334,12 +353,12 @@ static int sip24_valid(void) {
334
353
} /* sip24_valid() */
335
354
336
355
337
- #if SIPHASH_MAIN
356
+ #ifdef SIPHASH_MAIN
338
357
339
358
#include <stdio.h>
340
359
341
360
int main (void ) {
342
- int ok = sip24_valid ();
361
+ const int ok = sip24_valid ();
343
362
344
363
if (ok )
345
364
puts ("OK" );
0 commit comments