|
12 | 12 | #include "../sha1.h"
|
13 | 13 | #include "../sha256.h"
|
14 | 14 | #include "../sha3.h"
|
| 15 | +#include "../keccak.h" |
15 | 16 |
|
16 | 17 | #include "../hmac.h"
|
17 | 18 |
|
@@ -169,6 +170,22 @@ int check(const Container& input, const std::string& expectedResult)
|
169 | 170 | }
|
170 | 171 |
|
171 | 172 |
|
| 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 | + |
172 | 189 | // same as above but convert input from hex to raw bytes first (can contain zeros)
|
173 | 190 | template <typename HashMethod, typename InputContainer, typename KeyContainer>
|
174 | 191 | int checkHmac(const InputContainer& input, const KeyContainer& key, const std::string& expectedResult)
|
@@ -232,14 +249,34 @@ int main(int argc, char** argv)
|
232 | 249 | errors += check<SHA3>(abc896bits, "916f6061fe879741ca6469b43971dfdb28b1a32dc36cb3254e812be27aad1d18");
|
233 | 250 | errors += check<SHA3>(million, "5c8875ae474a3634ba4fd55ec85bffd661f32aca75c6d699d0cdcb6c115891c1");
|
234 | 251 |
|
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 |
236 | 253 | // note: automatic test case 71 failed, too, same bug
|
237 | 254 | std::cout << "test SHA3/512 ...\n";
|
238 | 255 | SHA3 sha3_512(SHA3::Bits512);
|
239 | 256 | std::vector<unsigned char> sha3bug = hex2bin("13bd2811f6ed2b6f04ff3895aceed7bef8dcd45eb121791bc194a0f806206bffc3b9281c2b308b1a729ce008119dd3066e9378acdcc50a98a82e20738800b6cddbe5fe9694ad6d");
|
240 | 257 | if (sha3_512(sha3bug.data(), sha3bug.size())
|
241 | 258 | != "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 | + } |
243 | 280 |
|
244 | 281 | // check all automatically generated testsets
|
245 | 282 | std::cout << "generic testsets (CRC32,MD5,SHA1,SHA256,SHA3) ..." << std::endl;
|
|
0 commit comments