forked from catid/libcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SipHash.cpp
146 lines (125 loc) · 4.24 KB
/
SipHash.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Copyright (c) 2013 Christopher A. Taylor. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of LibCat nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "SipHash.hpp"
#include "EndianNeutral.hpp"
using namespace cat;
#define SIP_HALF_ROUND(a, b, c, d, s, t) \
a += b; \
c += d; \
b = CAT_ROL64(b, s) ^ a; \
d = CAT_ROL64(d, t) ^ c; \
a = CAT_ROL64(a, 32);
#define SIP_DOUBLE_ROUND(v0, v1, v2, v3) \
SIP_HALF_ROUND(v0, v1, v2, v3, 13, 16); \
SIP_HALF_ROUND(v2, v1, v0, v3, 17, 21); \
SIP_HALF_ROUND(v0, v1, v2, v3, 13, 16); \
SIP_HALF_ROUND(v2, v1, v0, v3, 17, 21);
u64 cat::siphash24(const char key[16], const void *msg, const u64 msg_len,
const u64 nonce, const void *ad, const u64 ad_len)
{
// Convert key into two 64-bit integers
u64 k0 = getLE(*(const u64 *)key);
u64 k1 = getLE(*(const u64 *)(key + 8));
// Mix the key across initial state
u64 v0 = k0 ^ 0x736f6d6570736575ULL;
u64 v1 = k1 ^ 0x646f72616e646f6dULL;
u64 v2 = k0 ^ 0x6c7967656e657261ULL;
u64 v3 = k1 ^ 0x7465646279746573ULL;
// Mix in the nonce
{
v3 ^= nonce;
SIP_DOUBLE_ROUND(v0, v1, v2, v3);
v0 ^= nonce;
}
// Hash the message
{
// Perform SIP rounds on 8 bytes of input at a time
const u64 *m64 = (const u64 *)msg;
u64 words = msg_len >> 3;
while (words > 0) {
u64 mi = getLE(*m64++);
v3 ^= mi;
SIP_DOUBLE_ROUND(v0, v1, v2, v3);
v0 ^= mi;
--words;
}
// Mix the last 1..7 bytes with the length
const u8 *m = reinterpret_cast<const u8 *>( m64 );
u64 last7 = msg_len << 56;
switch (msg_len & 7) {
case 7: last7 |= (u64)m[6] << 48;
case 6: last7 |= (u64)m[5] << 40;
case 5: last7 |= (u64)m[4] << 32;
case 4: last7 |= getLE(*(const u32 *)m); // low -> low
break;
case 3: last7 |= (u64)m[2] << 16;
case 2: last7 |= (u64)m[1] << 8;
case 1: last7 |= (u64)m[0];
break;
};
// Final mix
v3 ^= last7;
SIP_DOUBLE_ROUND(v0, v1, v2, v3);
v0 ^= last7;
v2 ^= 0xff;
SIP_DOUBLE_ROUND(v0, v1, v2, v3);
SIP_DOUBLE_ROUND(v0, v1, v2, v3);
}
// Hash the AD, if needed
if (ad && ad_len > 0) {
// Perform SIP rounds on 8 bytes of input at a time
const u64 *m64 = (const u64 *)ad;
u64 words = ad_len >> 3;
while (words > 0) {
u64 mi = getLE(*m64++);
v3 ^= mi;
SIP_DOUBLE_ROUND(v0, v1, v2, v3);
v0 ^= mi;
--words;
}
// Mix the last 1..7 bytes with the length
const u8 *m = reinterpret_cast<const u8 *>( m64 );
u64 last7 = ad_len << 56;
switch (ad_len & 7) {
case 7: last7 |= (u64)m[6] << 48;
case 6: last7 |= (u64)m[5] << 40;
case 5: last7 |= (u64)m[4] << 32;
case 4: last7 |= getLE(*(const u32 *)m); // low -> low
break;
case 3: last7 |= (u64)m[2] << 16;
case 2: last7 |= (u64)m[1] << 8;
case 1: last7 |= (u64)m[0];
break;
};
// Final mix
v3 ^= last7;
SIP_DOUBLE_ROUND(v0, v1, v2, v3);
v0 ^= last7;
v2 ^= 0xff;
SIP_DOUBLE_ROUND(v0, v1, v2, v3);
SIP_DOUBLE_ROUND(v0, v1, v2, v3);
}
return (v0 ^ v1) ^ (v2 ^ v3);
}