aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-12-23 06:20:51 -0500
committerJack Lloyd <[email protected]>2017-12-23 06:20:51 -0500
commit4e101df712233ba6ef972b70d705bb29c7f1b7cf (patch)
treedfd776887264efab1b1354e57e7750f6cd985757 /src
parenteb6c280b76b6f1d5909da016ee7f4c8fa52eb406 (diff)
Avoid undefined behavior in SipHash
Diffstat (limited to 'src')
-rw-r--r--src/lib/mac/siphash/siphash.cpp10
-rw-r--r--src/tests/data/mac/siphash.vec51
2 files changed, 60 insertions, 1 deletions
diff --git a/src/lib/mac/siphash/siphash.cpp b/src/lib/mac/siphash/siphash.cpp
index 80acc4d60..7d8931c07 100644
--- a/src/lib/mac/siphash/siphash.cpp
+++ b/src/lib/mac/siphash/siphash.cpp
@@ -80,7 +80,15 @@ void SipHash::final_result(uint8_t mac[])
{
verify_key_set(m_V.empty() == false);
- m_mbuf = (m_mbuf >> (64-m_mbuf_pos*8)) | (static_cast<uint64_t>(m_words) << 56);
+ if(m_mbuf_pos == 0)
+ {
+ m_mbuf = (static_cast<uint64_t>(m_words) << 56);
+ }
+ else if(m_mbuf_pos < 8)
+ {
+ m_mbuf = (m_mbuf >> (64-m_mbuf_pos*8)) | (static_cast<uint64_t>(m_words) << 56);
+ }
+
SipRounds(m_mbuf, m_V, m_C);
m_V[2] ^= 0xFF;
diff --git a/src/tests/data/mac/siphash.vec b/src/tests/data/mac/siphash.vec
index 5420a992b..7f0ec381e 100644
--- a/src/tests/data/mac/siphash.vec
+++ b/src/tests/data/mac/siphash.vec
@@ -3,8 +3,59 @@ Key = 000102030405060708090A0B0C0D0E0F
In = 000102030405060708090A0B0C0D0E
Out = E545BE4961CA29A1
+
+
+
# Randomly generated by SipHash ref impl
+Key = 000102030405060708090A0B0C0D0E0F
+In =
+Out = 310E0EDD47DB6F72
+
+Key = 000102030405060708090A0B0C0D0E0F
+In = 00
+Out = FD67DC93C539F874
+
+Key = 000102030405060708090A0B0C0D0E0F
+In = 0001
+Out = 5A4FA9D909806C0D
+
+Key = 000102030405060708090A0B0C0D0E0F
+In = 000102
+Out = 2D7EFBD796666785
+
+Key = 000102030405060708090A0B0C0D0E0F
+In = 00010203
+Out = B7877127E09427CF
+
+Key = 000102030405060708090A0B0C0D0E0F
+In = 0001020304
+Out = 8DA699CD64557618
+
+Key = 000102030405060708090A0B0C0D0E0F
+In = 000102030405
+Out = CEE3FE586E46C9CB
+
+Key = 000102030405060708090A0B0C0D0E0F
+In = 00010203040506
+Out = 37D1018BF50002AB
+
+Key = 000102030405060708090A0B0C0D0E0F
+In = 0001020304050607
+Out = 6224939A79F5F593
+
+Key = 000102030405060708090A0B0C0D0E0F
+In = 000102030405060708
+Out = B0E4A90BDF82009E
+
+Key = 000102030405060708090A0B0C0D0E0F
+In = 00010203040506070809
+Out = F3B9DD94C5BB5D7A
+
+Key = 000102030405060708090A0B0C0D0E0F
+In = 000102030405060708090A
+Out = A7AD6B22462FB3F4
+
Key = 67C6697351FF4AEC29CDBAABF2FBE346
In =
Out = 9BBA4CA0FF8CF5CA