libcruft-crypto/test/hash/blake.cpp

108 lines
3.3 KiB
C++
Raw Normal View History

///////////////////////////////////////////////////////////////////////////////
#include "hash/blake.hpp"
#include <cruft/util/ascii.hpp>
#include <cruft/util/tap.hpp>
using cruft::crypto::hash::blake;
///////////////////////////////////////////////////////////////////////////////
static blake<256>::digest_t
operator"" _digest (const char *str, size_t len)
{
blake<256>::digest_t res;
for (size_t i = 0; i < len; i += 2)
res[i/2] = util::ascii::from_hex (str[i]) << 4 | util::ascii::from_hex (str[i+1]);
return res;
}
//-----------------------------------------------------------------------------
std::vector<uint8_t>
operator"" _u8s (const char *str, size_t len)
{
std::vector<uint8_t> res;
res.resize (len);
std::copy_n (str, len, std::begin (res));
return res;
}
///////////////////////////////////////////////////////////////////////////////
static const struct {
std::vector<uint8_t> data;
std::vector<uint8_t,16> salt;
blake<256>::digest_t digest;
const char *message;
} TESTS[] = {
{
{},
{},
"716f6e863f744b9ac22c97ec7b76ea5f"
"5908bc5b2f67c61510bfc4751384ea7a"_digest,
"empty, default salt"
}, {
{ 0x00 },
{ },
"0ce8d4ef4dd7cd8d62dfded9d4edb0a774ae6a41929a74da23109e8f11139c87"_digest,
"NIST 8 bit zeros, default salt",
}, {
"0123456789abcdef"_u8s,
{},
"9dc03986e7cc2973fd21f0c0c804922ef160313d3b4e52c187a1661c5e89877c"_digest,
"16 bytes, default salt"
}, {
"0123456789abcdef0123456789abcdef0123456789abcdef0123456"_u8s,
{},
"f58834fc24e109248d33194551a28852cd3d2d6ed4aba9ab3562bb5a97ce76e1"_digest,
"exactly pre-padding size, default salt"
}, {
std::vector<uint8_t> (576/8, 0),
{},
"d419bad32d504fb7d44d460c42c5593fe544fa4c135dec31e21bd9abdcc22d41"_digest,
"NIST 576 bit zeros, default salt",
}, {
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcde"_u8s,
{ },
"c455dcfcf5c77950b92ff055e7e4f8139acac4cbd18dbed0b1383d95e4f1726c"_digest,
"almost block size, default salt"
}, {
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"_u8s,
{ },
"e25d021064a61d7af53cefaa0819bb6a351db1d760ed1f85fee671bc18ab7886"_digest,
"exactly block size, default salt",
},
};
//-----------------------------------------------------------------------------
// million 'a':
// 224: 738443f8093ae703ebe4fe991b4f00208701e2e7be1275fd1bd84ef1
// 256: 22be6de4aa4214c9403f10598f0a6b0e834570251a13bc27589437f7139a5d44
// 384: 22ccce05b0ac1ceda1b0b0dc0021e6a3957779260cd2fae8a21c3d5432ce204f1df2d62a00d8f505cae6e70e3be18701
// 512: e5d3ecf60cbf8ed8b44307ae6606f4c8eae0a602c2a393f529cf405a1384c791c3ce58f54838a76b89a6455e988d63f98222ea82bf26e11e970516fb7c62b41d
int
main ()
{
util::TAP::logger tap;
for (const auto &t: TESTS) {
blake<256> h256;
const auto d = h256 (t.data, t.salt);
tap.expect_eq (d, t.digest, "%s", t.message);
}
{
blake<256> h256;
std::vector<uint8_t> data (1'000'000, 'a');
tap.expect_eq (
h256 (data),
"22be6de4aa4214c9403f10598f0a6b0e834570251a13bc27589437f7139a5d44"_digest,
"million 'a', 256"
);
}
return tap.status ();
}