Skip to content

Commit 21d2fed

Browse files
committed
version 7
1 parent a142dfa commit 21d2fed

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

digest.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <iostream>
1717
#include <fstream>
1818

19-
2019
int main(int argc, char** argv)
2120
{
2221
// syntax check

keccak.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,19 @@ std::string Keccak::getHash()
254254
result += dec2hex[oneByte & 15];
255255
}
256256

257+
// Keccak224's last entry in m_hash provides only 32 bits instead of 64 bits
258+
unsigned int remainder = m_bits - hashLength * 64;
259+
unsigned int processed = 0;
260+
while (processed < remainder)
261+
{
262+
// convert a byte to hex
263+
unsigned char oneByte = (unsigned char) (m_hash[hashLength] >> processed);
264+
result += dec2hex[oneByte >> 4];
265+
result += dec2hex[oneByte & 15];
266+
267+
processed += 8;
268+
}
269+
257270
return result;
258271
}
259272

sha3.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,19 @@ std::string SHA3::getHash()
254254
result += dec2hex[oneByte & 15];
255255
}
256256

257+
// SHA3-224's last entry in m_hash provides only 32 bits instead of 64 bits
258+
unsigned int remainder = m_bits - hashLength * 64;
259+
unsigned int processed = 0;
260+
while (processed < remainder)
261+
{
262+
// convert a byte to hex
263+
unsigned char oneByte = (unsigned char) (m_hash[hashLength] >> processed);
264+
result += dec2hex[oneByte >> 4];
265+
result += dec2hex[oneByte & 15];
266+
267+
processed += 8;
268+
}
269+
257270
return result;
258271
}
259272

tests/tests.cpp

100644100755
+39-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "../sha1.h"
1313
#include "../sha256.h"
1414
#include "../sha3.h"
15+
#include "../keccak.h"
1516

1617
#include "../hmac.h"
1718

@@ -169,6 +170,22 @@ int check(const Container& input, const std::string& expectedResult)
169170
}
170171

171172

173+
// same as above for SHA3/Keccak with variable hash size
174+
template <typename HashMethod, int HashSize, typename Container>
175+
int check(const Container& input, const std::string& expectedResult)
176+
{
177+
HashMethod hasher = HashMethod(typename HashMethod::Bits(HashSize));
178+
hasher.add(&input[0], input.size());
179+
std::string hash = hasher.getHash();
180+
if (hash == expectedResult)
181+
return 0;
182+
183+
// error
184+
std::cerr << "hash/" << HashSize << " failed ! expected \"" << expectedResult << "\" but library computed \"" << hash << "\"" << std::endl;
185+
return 1;
186+
}
187+
188+
172189
// same as above but convert input from hex to raw bytes first (can contain zeros)
173190
template <typename HashMethod, typename InputContainer, typename KeyContainer>
174191
int checkHmac(const InputContainer& input, const KeyContainer& key, const std::string& expectedResult)
@@ -232,14 +249,34 @@ int main(int argc, char** argv)
232249
errors += check<SHA3>(abc896bits, "916f6061fe879741ca6469b43971dfdb28b1a32dc36cb3254e812be27aad1d18");
233250
errors += check<SHA3>(million, "5c8875ae474a3634ba4fd55ec85bffd661f32aca75c6d699d0cdcb6c115891c1");
234251

235-
// next test cases produced an error until February 2015, reported by Gary Singer
252+
// next test case produced an error until February 2015, reported by Gary Singer
236253
// note: automatic test case 71 failed, too, same bug
237254
std::cout << "test SHA3/512 ...\n";
238255
SHA3 sha3_512(SHA3::Bits512);
239256
std::vector<unsigned char> sha3bug = hex2bin("13bd2811f6ed2b6f04ff3895aceed7bef8dcd45eb121791bc194a0f806206bffc3b9281c2b308b1a729ce008119dd3066e9378acdcc50a98a82e20738800b6cddbe5fe9694ad6d");
240257
if (sha3_512(sha3bug.data(), sha3bug.size())
241258
!= "def4ab6cda8839729a03e000846604b17f03c5d5d7ec23c483670a13e11573c1e9347a63ec69a5abb21305f9382ecdaaabc6850f92840e86f88f4dabfcd93cc0")
242-
std::cerr << "SHA3 bug present" << std::endl;
259+
{
260+
std::cerr << "SHA3/512 bug present" << std::endl;
261+
errors++;
262+
}
263+
264+
// hex conversion failed for SHA3/224 (last eight hex digits [32 bits] were missing)
265+
// reported by Alexander Moch in March 2015
266+
std::cout << "test SHA3/224 ...\n";
267+
SHA3 sha3_224(SHA3::Bits224);
268+
if (check<SHA3, SHA3::Bits224>(empty, "6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7"))
269+
{
270+
std::cerr << "SHA3/224 bug present" << std::endl;
271+
errors++;
272+
}
273+
std::cout << "test Keccak/224 ...\n";
274+
Keccak keccak224(Keccak::Keccak224);
275+
if (keccak224("") != "f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd")
276+
{
277+
std::cerr << "Keccak/224 bug present" << std::endl;
278+
errors++;
279+
}
243280

244281
// check all automatically generated testsets
245282
std::cout << "generic testsets (CRC32,MD5,SHA1,SHA256,SHA3) ..." << std::endl;

0 commit comments

Comments
 (0)