22 * siphash.h - SipHash-2-4 in a single header file
33 * --------------------------------------------------------------------------
44 * Derived by William Ahern from the reference implementation[1] published[2]
5- * by Jean-Philippe Aumasson and Daniel J. Berstein. Licensed in kind.
65 * 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.
87 * Licensed under the CC0 Public Domain Dedication license.
98 *
109 * 1. https://www.131002.net/siphash/siphash24.c
1110 * 2. https://www.131002.net/siphash/
1211 * --------------------------------------------------------------------------
1312 * HISTORY:
1413 *
15- * 2017-06-10 (Sebastian Pipping)
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)
21+ *
22+ * 2017-06-23 (Victor Stinner)
23+ * - Address Win64 compile warnings
24+ *
25+ * 2017-06-18 (Sebastian Pipping)
1626 * - Clarify license note in the header
1727 * - Address C89 issues:
1828 * - Stop using inline keyword (and let compiler decide)
19- * - Turn integer suffix ULL to UL
2029 * - Replace _Bool by int
2130 * - Turn macro siphash24 into a function
2231 * - Address invalid conversion (void pointer) by explicit cast
32+ * - Address lack of stdint.h for Visual Studio 2003 to 2008
2333 * - Always expose sip24_valid (for self-tests)
2434 *
2535 * 2012-11-04 - Born. (William Ahern)
7686#define SIPHASH_H
7787
7888#include <stddef.h> /* size_t */
79- #include <stdint.h> /* uint64_t uint32_t uint8_t */
89+
90+ #if defined(_WIN32 ) && defined(_MSC_VER ) && (_MSC_VER < 1600 )
91+ /* For vs2003/7.1 up to vs2008/9.0; _MSC_VER 1600 is vs2010/10.0 */
92+ typedef unsigned __int8 uint8_t ;
93+ typedef unsigned __int32 uint32_t ;
94+ typedef unsigned __int64 uint64_t ;
95+ #else
96+ #include <stdint.h> /* uint64_t uint32_t uint8_t */
97+ #endif
98+
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)
80106
81107
82108#define SIP_ROTL (x , b ) (uint64_t)(((x) << (b)) | ( (x) >> (64 - (b))))
@@ -158,11 +184,12 @@ static void sip_round(struct siphash *H, const int rounds) {
158184} /* sip_round() */
159185
160186
161- static struct siphash * sip24_init (struct siphash * H , const struct sipkey * key ) {
162- H -> v0 = 0x736f6d6570736575UL ^ key -> k [0 ];
163- H -> v1 = 0x646f72616e646f6dUL ^ key -> k [1 ];
164- H -> v2 = 0x6c7967656e657261UL ^ key -> k [0 ];
165- 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 ];
166193
167194 H -> p = H -> buf ;
168195 H -> c = 0 ;
@@ -173,7 +200,8 @@ static struct siphash *sip24_init(struct siphash *H, const struct sipkey *key) {
173200
174201#define sip_endof (a ) (&(a)[sizeof (a) / sizeof *(a)])
175202
176- 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 ) {
177205 const unsigned char * p = (const unsigned char * )src , * pe = p + len ;
178206 uint64_t m ;
179207
@@ -198,7 +226,7 @@ static struct siphash *sip24_update(struct siphash *H, const void *src, size_t l
198226
199227
200228static uint64_t sip24_final (struct siphash * H ) {
201- char left = H -> p - H -> buf ;
229+ const char left = ( char )( H -> p - H -> buf ) ;
202230 uint64_t b = (H -> c + left ) << 56 ;
203231
204232 switch (left ) {
@@ -222,7 +250,8 @@ static uint64_t sip24_final(struct siphash *H) {
222250} /* sip24_final() */
223251
224252
225- 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 ) {
226255 struct siphash state = SIPHASH_INITIALIZER ;
227256 return sip24_final (sip24_update (sip24_init (& state , key ), src , len ));
228257} /* siphash24() */
@@ -310,10 +339,11 @@ static int sip24_valid(void) {
310339 struct sipkey k ;
311340 size_t i ;
312341
313- 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" );
314344
315345 for (i = 0 ; i < sizeof in ; ++ i ) {
316- in [i ] = i ;
346+ in [i ] = ( unsigned char ) i ;
317347
318348 if (siphash24 (in , i , & k ) != SIP_U8TO64_LE (vectors [i ]))
319349 return 0 ;
@@ -323,12 +353,12 @@ static int sip24_valid(void) {
323353} /* sip24_valid() */
324354
325355
326- #if SIPHASH_MAIN
356+ #ifdef SIPHASH_MAIN
327357
328358#include <stdio.h>
329359
330360int main (void ) {
331- int ok = sip24_valid ();
361+ const int ok = sip24_valid ();
332362
333363 if (ok )
334364 puts ("OK" );
0 commit comments