Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kerukuro committed Oct 30, 2017
1 parent 0d6f26b commit 5b93174
Show file tree
Hide file tree
Showing 15 changed files with 43 additions and 28 deletions.
11 changes: 7 additions & 4 deletions algorithm/detail/blake2_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,12 @@ class blake2_provider
size_t processed = 0;
if (!squeezing)
{
if (type == blake2_type::xof)
total += pos * 8;
squeezing = true;
xoffset = 0;
memset(&m[pos], 0, N / 4 - pos);
if (N / 4 != pos)
memset(&m[pos], 0, N / 4 - pos);
transform(m.data(), 1, true);
memcpy(&m[0], H.data(), N / 8);
}
Expand All @@ -233,7 +236,7 @@ class blake2_provider
total = N;
memset(&m[N / 8], 0, m.size() - N / 8);
transform(m.data(), 1, true);
pos = std::min(hs, N / 8);
pos = std::min(hs - processed, N / 8);
memcpy(hash + processed, H.data(), pos);
processed += pos;
}
Expand All @@ -244,7 +247,8 @@ class blake2_provider
total += pos * 8;
if (type == blake2_type::hash)
{
memset(&m[pos], 0, N / 4 - pos);
if (N / 4 != pos)
memset(&m[pos], 0, N / 4 - pos);
transform(m.data(), 1, true);
memcpy(hash, H.data(), hash_size() / 8);
}
Expand All @@ -270,7 +274,6 @@ class blake2_provider
T M[16];
for (int i = 0; i < 16; i++)
M[i] = reinterpret_cast<const T*>(data)[blk * 16 + i];

uint64_t totalbytes = total / 8 + (padding ? 0 : (blk + 1) * N) / 4;
T t0 = static_cast<T>(totalbytes);
T t1 = N == 512 ? 0 : static_cast<T>(totalbytes >> 32);
Expand Down
3 changes: 2 additions & 1 deletion algorithm/detail/blake_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ class blake_provider
m[pos] = pos == messageend && !truncated ? 0x81 : 0x80;
if (pos++ > messageend)
{
memset(&m[pos], 0, block_bytes() - pos);
if (block_bytes() != pos)
memset(&m[pos], 0, block_bytes() - pos);
transform(m.data(), 1, false, true);
pos = 0;
padding = true;
Expand Down
3 changes: 2 additions & 1 deletion algorithm/detail/groestl_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ class groestl_provider
size_t limit = block_bytes();
if (pos > limit - 8)
{
memset(&m[pos], 0, limit - pos);
if (limit != pos)
memset(&m[pos], 0, limit - pos);
transform(m.data(), 1);
total += (block_bytes() - pos) * 8;
pos = 0;
Expand Down
3 changes: 2 additions & 1 deletion algorithm/detail/jh_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ class jh_provider
m[pos++] = 0x80;
if (pos > 1)
{
memset(&m[0] + pos, 0, 64 - pos);
if (pos != 64)
memset(&m[pos], 0, 64 - pos);
transform(&m[0], 1);
pos = 0;
}
Expand Down
3 changes: 2 additions & 1 deletion algorithm/detail/kupyna_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ class kupyna_provider
size_t limit = block_bytes();
if (pos > limit - 12)
{
memset(&m[pos], 0, limit - pos);
if (limit != pos)
memset(&m[pos], 0, limit - pos);
transform(m.data(), 1);
pos = 0;
}
Expand Down
3 changes: 2 additions & 1 deletion algorithm/detail/md5_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class md5_provider
m[pos++] = 0x80;
if (pos > 56)
{
memset(&m[0] + pos, 0, 64 - pos);
if (pos != 64)
memset(&m[pos], 0, 64 - pos);
transform(&m[0], 1);
pos = 0;
}
Expand Down
3 changes: 2 additions & 1 deletion algorithm/detail/sha1_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class sha1_provider
total += pos * 8;
m[pos++] = 0x80;
if (pos > 56) {
memset(&m[0] + pos, 0, 64 - pos);
if (pos != 64)
memset(&m[pos], 0, 64 - pos);
transform(&m[0], 1);
pos = 0;
}
Expand Down
13 changes: 7 additions & 6 deletions algorithm/detail/sha2_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,16 @@ class sha2_provider
{
total += pos * 8;
m[pos++] = 0x80;
if (pos > N / 4 - 8) {
memset(&m[0] + pos, 0, N / 4 - pos);
transform(&m[0], 1);
if (pos > N / 4 - sizeof(T) * 2) {
if (pos != N / 4)
memset(&m[pos], 0, N / 4 - pos);
transform(m.data(), 1);
pos = 0;
}
memset(&m[0] + pos, 0, N / 4 - pos);
memset(&m[pos], 0, N / 4 - pos);
uint64_t mlen = byteswap(total);
memcpy(&m[0] + (N / 4 - 8), &mlen, 64 / 8);
transform(&m[0], 1);
memcpy(&m[N / 4 - 8], &mlen, 64 / 8);
transform(m.data(), 1);
for (int i = 0; i < 8; i++)
H[i] = byteswap(H[i]);
memcpy(hash, &H[0], hs/8);
Expand Down
3 changes: 2 additions & 1 deletion algorithm/detail/sha3_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ class sha3_provider
{
size_t r = rate / 8;
m[pos++] = 0x06;
memset(&m[0] + pos, 0, r - pos);
if (r != pos)
memset(&m[pos], 0, r - pos);
m[r - 1] |= 0x80;
sha3_functions::transform<24>(m.data(), 1, A.data(), rate);
memcpy(hash, A.data(), hash_size() / 8);
Expand Down
3 changes: 2 additions & 1 deletion algorithm/detail/shake_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ class shake_provider
if (!squeezing)
{
m[pos++] = suffix ? suffix : N.empty() && S.empty() ? 0x1F : 0x04;
memset(&m[0] + pos, 0, r - pos);
if (r != pos)
memset(&m[pos], 0, r - pos);
m[r - 1] |= 0x80;
sha3_functions::transform<R>(m.data(), 1, A.data(), rate);
squeezing = true;
Expand Down
2 changes: 1 addition & 1 deletion algorithm/detail/skein_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ class skein_provider
}
while (processed < hs)
{
pos = std::min(hs, N / 8);
pos = std::min(hs - processed, N / 8);
tweak[0] = 0;
tweak[1] = 255ULL << 56;
memcpy(&m[0], &total, 8);
Expand Down
7 changes: 4 additions & 3 deletions algorithm/detail/sm3_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,15 @@ class sm3_provider
m[pos++] = 0x80;
if (pos > 56)
{
memset(&m[pos], 0, 64 - pos);
if (pos != 64)
memset(&m[pos], 0, 64 - pos);
transform(m.data(), 1);
pos = 0;
}
memset(&m[pos], 0, 56 - pos);
uint64_t mlen = byteswap(total);
memcpy(&m[0] + (64 - 8), &mlen, 64 / 8);
transform(&m[0], 1);
memcpy(&m[64 - 8], &mlen, 64 / 8);
transform(m.data(), 1);
for (int i = 0; i < 8; i++)
H[i] = byteswap(H[i]);
memcpy(hash, H.data(), 32);
Expand Down
3 changes: 2 additions & 1 deletion algorithm/detail/streebog_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ class streebog_provider
inline void final(unsigned char* hash)
{
m[pos++] = 0x01;
memset(&m[pos], 0, 64 - pos);
if (pos != 64)
memset(&m[pos], 0, 64 - pos);
transform(&m[0], 1, false);
total += (pos - 1) * 8;

Expand Down
3 changes: 2 additions & 1 deletion algorithm/detail/whirlpool_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class whirlpool_provider
m[pos++] = 0x80;
if (pos > 32)
{
memset(&m[pos], 0, 64 - pos);
if (pos != 64)
memset(&m[pos], 0, 64 - pos);
transform(m.data(), 1);
pos = 0;
}
Expand Down
8 changes: 4 additions & 4 deletions test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ void basic_self_test()
"503c341341572725709b874e95f13a438677aa6f9648467fd341e0f3e5421840");

errors += !compare("BLAKE2XS-XOF/512", digestpp::blake2xs_xof().absorb(ts).hexsqueeze(64),
"4e13634574b153019520d7e22a84bebc2b344e853df9420d6819dbcb26a6610e"
"bca3e71aba24f60b0444dbbd6f1bb526a43b87c5e6ac057870d43f8ae1c4890d");
"0650cde4df888a06eada0f0fecb3c17594304b4a03fdd678182f27db1238b174"
"7e33c34ae539fe2179a7594442b5cc9a7a0f398bb15ac3095a397de6a60061d6");

errors += !compare("BLAKE2XB/512", digestpp::blake2xb().absorb(ts).hexdigest(),
"6136549d6849d7386e42a1b7c034a1ddd6527e055a8425db4f3ae3c044aa306d"
"59c0bc428787d1539c5d13c703bfef01004e22277a84f5b0b093bed8268536b7");

errors += !compare("BLAKE2XB-XOF/512", digestpp::blake2xb_xof().absorb(ts).hexsqueeze(64),
"a4cae634be0d4011ed04d3b8ca60a3616147a201603505c0dd5ade86a50e8964"
"3f4e03d0aeb3781408b12700fb407080bf907d88bdef47dda63ceefa8b9cc5c6");
"364e84ca4c103df292306c93ebba6f6633d5e9cc8a95e040498e9a012d5ca534"
"c5532e20be9705e9266ad829952104c694954be42a6f50d847f8a782910ffe4b");

std::cout << "Self-test completed with " << errors << " errors." << std::endl;
}
Expand Down

0 comments on commit 5b93174

Please sign in to comment.