6
6
//
7
7
// ===----------------------------------------------------------------------===//
8
8
9
- #include " siphash.h"
10
- #include < assert.h>
11
- #include < stddef.h>
12
- #include < stdint.h>
9
+ #include " llvm/Support/Compiler.h"
10
+ #include < cstdint>
11
+ #include < cstring>
13
12
14
13
// Lightly adapted from the SipHash reference C implementation by
15
14
// Jean-Philippe Aumasson and Daniel J. Bernstein.
16
15
17
- /* default: SipHash-2-4 */
18
- #ifndef cROUNDS
19
- #define cROUNDS 2
20
- #endif
21
- #ifndef dROUNDS
22
- #define dROUNDS 4
23
- #endif
24
-
25
16
#define ROTL (x, b ) (uint64_t )(((x) << (b)) | ((x) >> (64 - (b))))
26
17
27
- #define U32TO8_LE (p, v ) \
28
- (p)[0 ] = (uint8_t )((v)); \
29
- (p)[1 ] = (uint8_t )((v) >> 8 ); \
30
- (p)[2 ] = (uint8_t )((v) >> 16 ); \
31
- (p)[3 ] = (uint8_t )((v) >> 24 );
32
-
33
- #define U64TO8_LE (p, v ) \
34
- U32TO8_LE ((p), (uint32_t )((v))); \
35
- U32TO8_LE ((p) + 4, (uint32_t )((v) >> 32));
36
-
37
18
#define U8TO64_LE (p ) \
38
19
(((uint64_t )((p)[0 ])) | ((uint64_t )((p)[1 ]) << 8 ) | \
39
20
((uint64_t )((p)[2 ]) << 16 ) | ((uint64_t )((p)[3 ]) << 24 ) | \
58
39
v2 = ROTL (v2, 32 ); \
59
40
} while (0 )
60
41
61
- /*
62
- Computes a SipHash value
63
- *in: pointer to input data (read-only)
64
- inlen: input data length in bytes (any size_t value)
65
- *k: pointer to the key data (read-only), must be 16 bytes
66
- *out: pointer to output data (write-only), outlen bytes must be allocated
67
- outlen: length of the output in bytes, must be 8 or 16
68
- */
69
- int siphash (const void *in, const size_t inlen, const void *k, uint8_t *out,
70
- const size_t outlen) {
42
+ template <int cROUNDS, int dROUNDS, class ResultTy >
43
+ static inline ResultTy siphash (const unsigned char *in, uint64_t inlen,
44
+ const unsigned char (&k)[16]) {
71
45
72
46
const unsigned char *ni = (const unsigned char *)in;
73
47
const unsigned char *kk = (const unsigned char *)k;
74
48
75
- assert ((outlen == 8 ) || (outlen == 16 ));
49
+ static_assert (sizeof (ResultTy) == 8 || sizeof (ResultTy) == 16 ,
50
+ " result type should be uint64_t or uint128_t" );
76
51
uint64_t v0 = UINT64_C (0x736f6d6570736575 );
77
52
uint64_t v1 = UINT64_C (0x646f72616e646f6d );
78
53
uint64_t v2 = UINT64_C (0x6c7967656e657261 );
@@ -89,7 +64,7 @@ int siphash(const void *in, const size_t inlen, const void *k, uint8_t *out,
89
64
v1 ^= k1;
90
65
v0 ^= k0;
91
66
92
- if (outlen == 16 )
67
+ if (sizeof (ResultTy) == 16 )
93
68
v1 ^= 0xee ;
94
69
95
70
for (; ni != end; ni += 8 ) {
@@ -105,22 +80,22 @@ int siphash(const void *in, const size_t inlen, const void *k, uint8_t *out,
105
80
switch (left) {
106
81
case 7 :
107
82
b |= ((uint64_t )ni[6 ]) << 48 ;
108
- /* FALLTHRU */
83
+ LLVM_FALLTHROUGH;
109
84
case 6 :
110
85
b |= ((uint64_t )ni[5 ]) << 40 ;
111
- /* FALLTHRU */
86
+ LLVM_FALLTHROUGH;
112
87
case 5 :
113
88
b |= ((uint64_t )ni[4 ]) << 32 ;
114
- /* FALLTHRU */
89
+ LLVM_FALLTHROUGH;
115
90
case 4 :
116
91
b |= ((uint64_t )ni[3 ]) << 24 ;
117
- /* FALLTHRU */
92
+ LLVM_FALLTHROUGH;
118
93
case 3 :
119
94
b |= ((uint64_t )ni[2 ]) << 16 ;
120
- /* FALLTHRU */
95
+ LLVM_FALLTHROUGH;
121
96
case 2 :
122
97
b |= ((uint64_t )ni[1 ]) << 8 ;
123
- /* FALLTHRU */
98
+ LLVM_FALLTHROUGH;
124
99
case 1 :
125
100
b |= ((uint64_t )ni[0 ]);
126
101
break ;
@@ -135,7 +110,7 @@ int siphash(const void *in, const size_t inlen, const void *k, uint8_t *out,
135
110
136
111
v0 ^= b;
137
112
138
- if (outlen == 16 )
113
+ if (sizeof (ResultTy) == 16 )
139
114
v2 ^= 0xee ;
140
115
else
141
116
v2 ^= 0xff ;
@@ -144,18 +119,18 @@ int siphash(const void *in, const size_t inlen, const void *k, uint8_t *out,
144
119
SIPROUND;
145
120
146
121
b = v0 ^ v1 ^ v2 ^ v3;
147
- U64TO8_LE (out, b);
148
122
149
- if (outlen == 8 )
150
- return 0 ;
123
+ uint64_t firstHalf = b;
124
+ if (sizeof (ResultTy) == 8 )
125
+ return firstHalf;
151
126
152
127
v1 ^= 0xdd ;
153
128
154
129
for (i = 0 ; i < dROUNDS; ++i)
155
130
SIPROUND;
156
131
157
132
b = v0 ^ v1 ^ v2 ^ v3;
158
- U64TO8_LE (out + 8 , b) ;
133
+ uint64_t secondHalf = b ;
159
134
160
- return 0 ;
135
+ return firstHalf | ( ResultTy (secondHalf) << ( sizeof (ResultTy) == 8 ? 0 : 64 )) ;
161
136
}
0 commit comments