Refactor to standard layout.

This commit is contained in:
2023-10-19 20:41:55 -07:00
parent 0d7a91b69c
commit 59e6abff06
15 changed files with 40 additions and 39 deletions

154
test/test_emsha.cc Normal file
View File

@@ -0,0 +1,154 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 K. Isom <coder@kyleisom.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "emsha/emsha.h"
#include <chrono>
#include <iostream>
#include "test_utils.h"
using namespace std;
// how many test runs to benchmark hex strings?
static constexpr auto testIterations = 32768;
#ifndef EMSHA_NO_HEXSTRING
static void
hexStringTest()
{
uint8_t buf[32];
uint8_t out[65];
string const expected = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
out[64] = 0;
for (uint32_t i = 0; i < 32; i++) {
buf[i] = static_cast<uint8_t>(i);
}
emsha::HexString(out, buf, emsha::SHA256_HASH_SIZE);
string const outs(reinterpret_cast<const char *>(out));
if (outs != expected) {
cerr << "FAILED: HexString\n";
cerr << "\twanted: " << expected << "\n";
cerr << "\thave: " << out << "\n";
exit(1);
}
}
#endif // #ifndef EMSHA_NO_HEXSTRING
// TODO(kyle): build a test harness around this to verify times between
// runs.
static void
hashEqualTest()
{
uint8_t a[emsha::SHA256_HASH_SIZE];
uint8_t b[emsha::SHA256_HASH_SIZE];
for (uint32_t i = 0; i < emsha::SHA256_HASH_SIZE; i++) {
a[i] = static_cast<uint8_t>(i);
b[i] = static_cast<uint8_t>(i);
}
if (!(emsha::HashEqual(a, b))) {
string s;
cerr << "FAILED: HashEqual\n";
cerr << "\tHashEqual should have succeeded comparing a and b.\n";
DumpHexString(s, a, emsha::SHA256_HASH_SIZE);
cerr << "\ta <- " << s << "\n";
DumpHexString(s, b, emsha::SHA256_HASH_SIZE);
cerr << "\tb <- " << s << "\n";
exit(1);
}
for (uint32_t i = 0; i < emsha::SHA256_HASH_SIZE; i++) {
a[i] = static_cast<uint8_t>(i);
b[i] = static_cast<uint8_t>(emsha::SHA256_HASH_SIZE - i);
}
if (emsha::HashEqual(a, b)) {
string s;
cerr << "FAILED: HashEqual\n";
cerr << "\tHashEqual should not have succeeded comparing a and b.\n";
DumpHexString(s, a, emsha::SHA256_HASH_SIZE);
cerr << "\ta <- " << s << "\n";
DumpHexString(s, b, emsha::SHA256_HASH_SIZE);
cerr << "\tb <- " << s << "\n";
exit(1);
}
// This catches the bug in the initial version where the code was
// res = a[i] ^ b[i];
// instead of
// res += a[i] ^ b[i];
for (uint32_t i = 0; i < emsha::SHA256_HASH_SIZE; i++) {
a[i] = static_cast<uint8_t>(i);
b[i] = static_cast<uint8_t>(i + 1);
}
b[emsha::SHA256_HASH_SIZE - 1]--;
if (emsha::HashEqual(a, b)) {
string s;
cerr << "FAILED: HashEqual\n";
cerr << "\tREGRESSION: HashEqual should not have succeeded comparing a and b.\n";
DumpHexString(s, a, emsha::SHA256_HASH_SIZE);
cerr << "\ta <- " << s << std::endl;
DumpHexString(s, b, emsha::SHA256_HASH_SIZE);
cerr << "\tb <- " << s << std::endl;
exit(1);
}
}
int
main()
{
auto start = std::chrono::steady_clock::now();
std::string testLabel;
for (auto i = 0; i < testIterations; i++) {
#ifndef EMSHA_NO_HEXSTRING
#ifndef EMSHA_NO_HEXLUT
testLabel = "(large LUT) ";
#endif
hexStringTest();
#endif
hashEqualTest();
}
auto end = std::chrono::steady_clock::now();
auto delta = (end - start);
std::cout << "Passed HexString " << testLabel << "tests.\n";
std::cout << "Total time: "
<< std::chrono::duration<double, std::milli>(delta).count()
<< " ms\n";
std::cout << "Average over " << testIterations << " tests: "
<< std::chrono::duration<double, std::nano>(delta).count() / testIterations
<< " ns\n";
}

137
test/test_hmac.cc Normal file
View File

@@ -0,0 +1,137 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 K. Isom <coder@kyleisom.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <iostream>
#include "emsha/emsha.h"
#include "emsha/hmac.h"
#include "test_utils.h"
using namespace std;
const struct hmacTest rfc4231[] = {
{
{0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b,
0x0b, 0x0b, 0x0b, 0x0b}, 20,
"Hi There",
"b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7"
},
{
{0x4a, 0x65, 0x66, 0x65}, 4,
"what do ya want for nothing?",
"5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"
},
{
{0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa}, 20,
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
"\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd\xdd"
"\xdd\xdd",
"773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514ced565fe",
},
{
{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
0x19}, 25,
"\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
"\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
"\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
"\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd"
"\xcd\xcd",
"82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff46729665b"
},
{
{0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa}, 131,
"Test Using Larger Than Block-Size Key - Hash Key First",
"60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f0ee37f54"
},
{
{0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
0xaa, 0xaa, 0xaa}, 131,
"This is a test using a larger than block-size key "
"and a larger than block-size data. The key needs to "
"be hashed before being used by the HMAC algorithm.",
"9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f51535c3a35e2"
}
};
int
main()
{
int res = 0;
res = runHMACTests((struct hmacTest *) rfc4231,
sizeof rfc4231 / sizeof rfc4231[0],
"RFC 4231");
if (-1 == res) {
exit(1);
}
exit(0);
}

217
test/test_mem.cc Normal file
View File

@@ -0,0 +1,217 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 K. Isom <coder@kyleisom.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <cassert>
#include <chrono>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <iostream>
#include "emsha/emsha.h"
#include "emsha/hmac.h"
#include "emsha/sha256.h"
// Number of test iterations.
static constexpr std::uint32_t ITERS = 32768;
// The key used for HMAC.
static constexpr std::uint8_t k[] = {
0xc5, 0xb6, 0x80, 0xac, 0xdc, 0xf4, 0xff, 0xa1,
0x37, 0x05, 0xc0, 0x71, 0x11, 0x24, 0x31, 0x7c,
0xa5, 0xa2, 0xcf, 0x4d, 0x33, 0x00, 0x56, 0x4f,
0x69, 0x0f, 0x76, 0x70, 0x87, 0xd9, 0x35, 0xce,
0xa3, 0xad, 0xa3, 0x4f, 0x30, 0xe2, 0x7c, 0x58,
0x88, 0xd4, 0x89, 0x6a, 0xb5, 0xe0, 0x97, 0x1c,
0x7a, 0x69, 0x65, 0xc7, 0x61, 0x0d, 0x6d, 0xb6,
0x9b, 0x0e, 0x56, 0xd7, 0x0f, 0x5a, 0x01, 0x50,
};
static constexpr std::uint32_t kl = sizeof(k) / sizeof(k[0]);
// The message provided to both SHA-256 and HMAC-SHA-256; it is
// "The fugacity of a constituent in a mixture of gases at a given
// temperature is proportional to its mole fraction. Lewis-Randall Rule",
// chosen as one of the longer test vectors.
static const std::uint8_t m[] = {
0x54, 0x68, 0x65, 0x20, 0x66, 0x75, 0x67, 0x61,
0x63, 0x69, 0x74, 0x79, 0x20, 0x6f, 0x66, 0x20,
0x61, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x69,
0x74, 0x75, 0x65, 0x6e, 0x74, 0x20, 0x69, 0x6e,
0x20, 0x61, 0x20, 0x6d, 0x69, 0x78, 0x74, 0x75,
0x72, 0x65, 0x20, 0x6f, 0x66, 0x20, 0x67, 0x61,
0x73, 0x65, 0x73, 0x20, 0x61, 0x74, 0x20, 0x61,
0x20, 0x67, 0x69, 0x76, 0x65, 0x6e, 0x20, 0x74,
0x65, 0x6d, 0x70, 0x65, 0x72, 0x61, 0x74, 0x75,
0x72, 0x65, 0x20, 0x69, 0x73, 0x20, 0x70, 0x72,
0x6f, 0x70, 0x6f, 0x72, 0x74, 0x69, 0x6f, 0x6e,
0x61, 0x6c, 0x20, 0x74, 0x6f, 0x20, 0x69, 0x74,
0x73, 0x20, 0x6d, 0x6f, 0x6c, 0x65, 0x20, 0x66,
0x72, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x2e,
0x20, 0x20, 0x4c, 0x65, 0x77, 0x69, 0x73, 0x2d,
0x52, 0x61, 0x6e, 0x64, 0x61, 0x6c, 0x6c, 0x20,
0x52, 0x75, 0x6c, 0x65
};
// d is the expected result of SHA256(m).
static constexpr std::uint8_t d[emsha::SHA256_HASH_SIZE] = {
0x39, 0x55, 0x85, 0xce, 0x30, 0x61, 0x7b, 0x62,
0xc8, 0x0b, 0x93, 0xe8, 0x20, 0x8c, 0xe8, 0x66,
0xd4, 0xed, 0xc8, 0x11, 0xa1, 0x77, 0xfd, 0xb4,
0xb8, 0x2d, 0x39, 0x11, 0xd8, 0x69, 0x64, 0x23
};
// t is the expected result of HMAC-SHA-256(k, m).
static constexpr std::uint8_t t[emsha::SHA256_HASH_SIZE] = {
0xbb, 0xc4, 0x7c, 0x35, 0x33, 0x4b, 0x9d, 0x90,
0xee, 0x20, 0x88, 0x30, 0xe1, 0x1a, 0x0f, 0xf3,
0xf4, 0x7d, 0xcc, 0xb0, 0xc5, 0xfb, 0x83, 0xe5,
0xc2, 0xf5, 0xa7, 0x94, 0x50, 0xb6, 0xe0, 0xe0,
};
// dig is used to store the output of SHA-256 and HMAC-SHA-256.
static std::uint8_t dig[emsha::SHA256_HASH_SIZE];
static void
init()
{
std::fill(dig, dig + emsha::SHA256_HASH_SIZE, 0);
}
static void
iterateSHA()
{
emsha::SHA256 ctx;
int cmp = 0;
emsha::EMSHAResult res;
res = ctx.Update(m, sizeof(m));
assert(emsha::EMSHAResult::OK == res);
res = ctx.Result(dig);
assert(emsha::EMSHAResult::OK == res);
cmp = std::memcmp(dig, d, emsha::SHA256_HASH_SIZE);
assert(0 == cmp);
}
static void
iterateHMAC()
{
emsha::HMAC ctx(k, kl);
int cmp = 0;
emsha::EMSHAResult res;
res = ctx.Update(m, sizeof(m));
assert(emsha::EMSHAResult::OK == res);
res = ctx.Result(dig);
assert(emsha::EMSHAResult::OK == res);
cmp = std::memcmp(dig, t, emsha::SHA256_HASH_SIZE);
assert(0 == cmp);
}
static void
iterateSHASP()
{
int cmp = 0;
assert(emsha::EMSHAResult::OK == emsha::SHA256Digest(m, sizeof(m), dig));
cmp = std::memcmp(dig, d, emsha::SHA256_HASH_SIZE);
assert(0 == cmp);
}
static void
iterateHMACSP()
{
int cmp = 0;
emsha::EMSHAResult res;
res = emsha::ComputeHMAC(k, kl, m, sizeof(m), dig);
assert(emsha::EMSHAResult::OK == res);
cmp = std::memcmp(dig, t, emsha::SHA256_HASH_SIZE);
assert(0 == cmp);
}
static void
iterate(const std::string &label, void(iteration)(void))
{
std::cout << "=== " << label << " ===" << std::endl;
auto start = std::chrono::steady_clock::now();
for (std::uint32_t i = 0; i < ITERS; i++) {
iteration();
}
auto end = std::chrono::steady_clock::now();
auto delta = (end - start);
std::cout << "Total time: "
<< std::chrono::duration<double, std::milli>(delta).count()
<< " ms" << std::endl;
std::cout << "Average over " << ITERS << " tests: "
<< std::chrono::duration<double, std::nano>(delta).count() / ITERS
<< " ns" << std::endl;
}
static void
coldStart()
{
std::cout << "=== SHA-256 cold start ===\n";
auto start = std::chrono::steady_clock::now();
iterateSHA();
auto end = std::chrono::steady_clock::now();
auto delta = (end - start);
std::cout << "Total time: "
<< std::chrono::duration<double, std::nano>(delta).count()
<< " ns" << std::endl;
}
int
main()
{
init();
coldStart();
iterate("SHA-256", iterateSHA);
iterate("SHA-256 single-pass", iterateSHASP);
iterate("HMAC-SHA-256", iterateHMAC);
iterate("HMAC-SHA-256 single-pass", iterateHMACSP);
}

117
test/test_sha256.cc Normal file
View File

@@ -0,0 +1,117 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 K. Isom <coder@kyleisom.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "emsha/sha256.h"
#include <cassert>
#include <iostream>
#include "test_utils.h"
using namespace std;
// Tests taken from the Go SHA-256 package.
const struct hashTest goldenTests[] = {
{"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""},
{"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb", "a"},
{"fb8e20fc2e4c3f248c60c39bd652f3c1347298bb977b8b4d5903b85055620603", "ab"},
{"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", "abc"},
{"88d4266fd4e6338d13b845fcf289579d209c897823b9217da3e161936f031589", "abcd"},
{"36bbe50ed96841d10443bcb670d6554f0a34b761be67ec9c4a8ad2c0c44ca42c", "abcde"},
{"bef57ec7f53a6d40beb640a780a639c83bc29ac8a9816f1fc6c5c6dcd93c4721", "abcdef"},
{"7d1a54127b222502f5b79b5fb0803061152a44f92b37e23c6527baf665d4da9a", "abcdefg"},
{"9c56cc51b374c3ba189210d5b6d4bf57790d351c96c47c02190ecf1e430635ab", "abcdefgh"},
{"19cc02f26df43cc571bc9ed7b0c4d29224a3ec229529221725ef76d021c8326f", "abcdefghi"},
{"72399361da6a7754fec986dca5b7cbaf1c810a28ded4abaf56b2106d06cb78b0", "abcdefghij"},
{"a144061c271f152da4d151034508fed1c138b8c976339de229c3bb6d4bbb4fce", "Discard medicine more than two years old."},
{"6dae5caa713a10ad04b46028bf6dad68837c581616a1589a265a11288d4bb5c4", "He who has a shady past knows that nice guys finish last."},
{"ae7a702a9509039ddbf29f0765e70d0001177914b86459284dab8b348c2dce3f", "I wouldn't marry him with a ten foot pole."},
{"6748450b01c568586715291dfa3ee018da07d36bb7ea6f180c1af6270215c64f", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave"},
{"14b82014ad2b11f661b5ae6a99b75105c2ffac278cd071cd6c05832793635774", "The days of the digital watch are numbered. -Tom Stoppard"},
{"7102cfd76e2e324889eece5d6c41921b1e142a4ac5a2692be78803097f6a48d8", "Nepal premier won't resign."},
{"23b1018cd81db1d67983c5f7417c44da9deb582459e378d7a068552ea649dc9f", "For every action there is an equal and opposite government program."},
{"8001f190dfb527261c4cfcab70c98e8097a7a1922129bc4096950e57c7999a5a", "His money is twice tainted: 'taint yours and 'taint mine."},
{"8c87deb65505c3993eb24b7a150c4155e82eee6960cf0c3a8114ff736d69cad5", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977"},
{"bfb0a67a19cdec3646498b2e0f751bddc41bba4b7f30081b0b932aad214d16d7", "It's a tiny change to the code and not completely disgusting. - Bob Manchek"},
{"7f9a0b9bf56332e19f5a0ec1ad9c1425a153da1c624868fda44561d6b74daf36", "size: a.out: bad magic"},
{"b13f81b8aad9e3666879af19886140904f7f429ef083286195982a7588858cfc", "The major problem is with sendmail. -Mark Horton"},
{"b26c38d61519e894480c70c8374ea35aa0ad05b2ae3d6674eec5f52a69305ed4", "Give me a rock, paper and scissors and I will move the world. CCFestoon"},
{"049d5e26d4f10222cd841a119e38bd8d2e0d1129728688449575d4ff42b842c1", "If the enemy is within range, then so are you."},
{"0e116838e3cc1c1a14cd045397e29b4d087aa11b0853fc69ec82e90330d60949", "It's well we cannot hear the screams/That we create in others' dreams."},
{"4f7d8eb5bcf11de2a56b971021a444aa4eafd6ecd0f307b5109e4e776cd0fe46", "You remind me of a TV show, but that's all right: I watch it anyway."},
{"61c0cc4c4bd8406d5120b3fb4ebc31ce87667c162f29468b3c779675a85aebce", "C is as portable as Stonehedge!!"},
{"1fb2eb3688093c4a3f80cd87a5547e2ce940a4f923243a79a2a1e242220693ac", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley"},
{"395585ce30617b62c80b93e8208ce866d4edc811a177fdb4b82d3911d8696423", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction. Lewis-Randall Rule"},
{"4f9b189a13d030838269dce846b16a1ce9ce81fe63e65de2f636863336a98fe6", "How can you write a big system without C++? -Paul Glick"},
};
static constexpr auto numGoldenTests = sizeof goldenTests / sizeof goldenTests[0];
static const std::string labelGoldenTests = "golden tests";
int
main()
{
#ifdef EMSHA_NO_SELFTEST
cout << "[NOTICE] internal self-tests have been disabled.\n";
#else
auto selfTestStatus = emsha::SHA256SelfTest();
switch (selfTestStatus) {
case emsha::EMSHAResult::OK:
cout << "PASSED: SHA-256 self test\n";
break;
case emsha::EMSHAResult::TestFailure:
cout << "FAILED: SHA-256 self-test\n";
break;
case emsha::EMSHAResult::Unknown:
cout << "FAILED: SHA-256 self test (fault: Unknown)\n";
break;
case emsha::EMSHAResult::NullPointer:
cout << "FAILED: SHA-256 self test (fault: NullPointer)\n";
break;
case emsha::EMSHAResult::InvalidState:
cout << "FAILED: SHA-256 self test (fault: InvalidState)\n";
break;
case emsha::EMSHAResult::InputTooLong:
cout << "FAILED: SHA-256 self test (fault: InputTooLong)\n";
break;
case emsha::EMSHAResult::SelfTestDisabled:
cout << "FAILED: SHA-256 self test (fault: SelfTestDisabled)\n";
break;
default:
cout << "FAILED: SHA-256 self test (fault: internal system failure)\n";
abort();
}
assert(selfTestStatus == emsha::EMSHAResult::OK);
#endif
auto res = runHashTests(static_cast<const hashTest *>(goldenTests),
numGoldenTests, labelGoldenTests);
if (res == -1) {
exit(1);
}
exit(0);
}

299
test/test_utils.cc Normal file
View File

@@ -0,0 +1,299 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 K. Isom <coder@kyleisom.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <cstdint>
#include <cstring>
#include <iostream>
#include <string>
#include "test_utils.h"
using std::uint8_t;
using std::uint32_t;
using std::string;
using std::cout;
using std::cerr;
using std::endl;
void
DumpHexString(std::string& hs, uint8_t *s, uint32_t sl)
{
uint32_t const bl = (2 * sl) + 1;
char *buf = new char[bl];
string tmp;
memset(buf, 0, bl);
emsha::HexString(reinterpret_cast<uint8_t *>(buf), s, sl);
tmp = string(buf);
hs.swap(tmp);
delete[] buf;
}
emsha::EMSHAResult
runHMACTest(const struct hmacTest& test, const string& label)
{
emsha::HMAC h(test.key, test.keylen);
emsha::EMSHAResult res;
uint8_t dig[emsha::SHA256_HASH_SIZE];
string hs;
res = h.Update((uint8_t *)test.input.c_str(), test.input.size());
if (emsha::EMSHAResult::OK != res) {
goto exit;
}
for (uint32_t n = 0; n < RESULT_ITERATIONS; n++) {
res = h.Result(dig);
if (emsha::EMSHAResult::OK != res) {
goto exit;
}
DumpHexString(hs, dig, emsha::SHA256_HASH_SIZE);
if (hs != test.output) {
res = emsha::EMSHAResult::TestFailure;
goto exit;
}
memset(dig, 0, emsha::SHA256_HASH_SIZE);
}
// Ensure that a reset and update gives the same results.
h.Reset();
res = h.Update((uint8_t *)test.input.c_str(), test.input.size());
if (emsha::EMSHAResult::OK != res) {
goto exit;
}
for (uint32_t n = 0; n < RESULT_ITERATIONS; n++) {
res = h.Result(dig);
if (emsha::EMSHAResult::OK != res) {
goto exit;
}
DumpHexString(hs, dig, emsha::SHA256_HASH_SIZE);
if (hs != test.output) {
res = emsha::EMSHAResult::TestFailure;
goto exit;
}
memset(dig, 0, emsha::SHA256_HASH_SIZE);
}
// Test that the single-pass function works.
res = emsha::ComputeHMAC(test.key, test.keylen,
(uint8_t *)test.input.c_str(), test.input.size(),
dig);
if (emsha::EMSHAResult::OK != res) {
cerr << "(running single pass function test)\n";
goto exit;
}
DumpHexString(hs, dig, emsha::SHA256_HASH_SIZE);
if (hs != test.output) {
cerr << "(comparing single pass function output)\n";
res = emsha::EMSHAResult::TestFailure;
goto exit;
}
memset(dig, 0, emsha::SHA256_HASH_SIZE);
res = emsha::EMSHAResult::OK;
exit:
if (emsha::EMSHAResult::OK != res) {
cerr << "FAILED: " << label << endl;
cerr << "\tinput: " << test.input << endl;
cerr << "\twanted: " << test.output << endl;
cerr << "\thave: " << hs << endl;
}
return res;
}
int
runHMACTests(const struct hmacTest *tests, size_t nTests, const string& label)
{
for (uint32_t i = 0; i < nTests; i++) {
if (emsha::EMSHAResult::OK != runHMACTest(*(tests + i), label)) {
return -1;
}
}
cout << "PASSED: " << label << " (" << nTests << ")" << endl;
return 0;
}
emsha::EMSHAResult
runHashTest(const struct hashTest& test, const string& label)
{
emsha::SHA256 ctx;
emsha::EMSHAResult res;
uint8_t dig[emsha::SHA256_HASH_SIZE];
string hs;
res = ctx.Update((uint8_t *)test.input.c_str(), test.input.size());
if (emsha::EMSHAResult::OK != res) {
goto exit;
}
for (uint32_t n = 0; n < RESULT_ITERATIONS; n++) {
res = ctx.Result(dig);
if (emsha::EMSHAResult::OK != res) {
goto exit;
}
DumpHexString(hs, dig, emsha::SHA256_HASH_SIZE);
if (hs != test.output) {
res = emsha::EMSHAResult::TestFailure;
goto exit;
}
memset(dig, 0, emsha::SHA256_HASH_SIZE);
}
// Ensure that a reset and update gives the same results.
ctx.Reset();
res = ctx.Update((uint8_t *)test.input.c_str(), test.input.size());
if (emsha::EMSHAResult::OK != res) {
goto exit;
}
for (uint32_t n = 0; n < RESULT_ITERATIONS; n++) {
res = ctx.Result(dig);
if (emsha::EMSHAResult::OK != res) {
goto exit;
}
DumpHexString(hs, dig, emsha::SHA256_HASH_SIZE);
if (hs != test.output) {
res = emsha::EMSHAResult::TestFailure;
goto exit;
}
memset(dig, 0, emsha::SHA256_HASH_SIZE);
}
// Test that the single-pass function works.
res = emsha::SHA256Digest((uint8_t *) test.input.c_str(),
test.input.size(), dig);
if (emsha::EMSHAResult::OK != res) {
cerr << "(running single pass function test)\n";
goto exit;
}
DumpHexString(hs, dig, emsha::SHA256_HASH_SIZE);
if (hs != test.output) {
cerr << "(comparing single pass function output)\n";
res = emsha::EMSHAResult::TestFailure;
goto exit;
}
memset(dig, 0, emsha::SHA256_HASH_SIZE);
res = emsha::EMSHAResult::OK;
exit:
if (emsha::EMSHAResult::OK != res) {
cerr << "FAILED: " << label << endl;
cerr << "\tinput: '" << test.input << "'" << endl;
cerr << "\twanted: " << test.output << endl;
cerr << "\thave: " << hs << endl;
}
return res;
}
int
runHashTests(const struct hashTest *tests, const size_t ntests, const string& label)
{
for (uint32_t i = 0; i < ntests; i++) {
if (emsha::EMSHAResult::OK != runHashTest(*(tests + i), label)) {
return -1;
}
}
cout << "PASSED: " << label << " (" << ntests << ")" << endl;
return 0;
}
#ifdef EMSHA_NO_HEXSTRING
// If the library was built without hex string support, include it in
// the test code.
namespace emsha {
static void
write_hex_char(uint8_t *dest, uint8_t src)
{
static constexpr uint8_t lut[256][3] = {
"00", "01", "02", "03", "04", "05", "06", "07",
"08", "09", "0a", "0b", "0c", "0d", "0e", "0f",
"10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "1a", "1b", "1c", "1d", "1e", "1f",
"20", "21", "22", "23", "24", "25", "26", "27",
"28", "29", "2a", "2b", "2c", "2d", "2e", "2f",
"30", "31", "32", "33", "34", "35", "36", "37",
"38", "39", "3a", "3b", "3c", "3d", "3e", "3f",
"40", "41", "42", "43", "44", "45", "46", "47",
"48", "49", "4a", "4b", "4c", "4d", "4e", "4f",
"50", "51", "52", "53", "54", "55", "56", "57",
"58", "59", "5a", "5b", "5c", "5d", "5e", "5f",
"60", "61", "62", "63", "64", "65", "66", "67",
"68", "69", "6a", "6b", "6c", "6d", "6e", "6f",
"70", "71", "72", "73", "74", "75", "76", "77",
"78", "79", "7a", "7b", "7c", "7d", "7e", "7f",
"80", "81", "82", "83", "84", "85", "86", "87",
"88", "89", "8a", "8b", "8c", "8d", "8e", "8f",
"90", "91", "92", "93", "94", "95", "96", "97",
"98", "99", "9a", "9b", "9c", "9d", "9e", "9f",
"a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7",
"a8", "a9", "aa", "ab", "ac", "ad", "ae", "af",
"b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7",
"b8", "b9", "ba", "bb", "bc", "bd", "be", "bf",
"c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7",
"c8", "c9", "ca", "cb", "cc", "cd", "ce", "cf",
"d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
"d8", "d9", "da", "db", "dc", "dd", "de", "df",
"e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7",
"e8", "e9", "ea", "eb", "ec", "ed", "ee", "ef",
"f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
"f8", "f9", "fa", "fb", "fc", "fd", "fe", "ff"
};
memcpy(dest, lut[src], 2);
}
void
HexString(uint8_t *dest, uint8_t *src, uint32_t srclen)
{
uint8_t *dp = dest;
for (uint32_t i = 0; i < srclen; i++, dp += 2) {
write_hex_char(dp, src[i]);
}
}
}
#endif // EMSHA_NO_HEXSTRING

85
test/test_utils.h Normal file
View File

@@ -0,0 +1,85 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 K. Isom <coder@kyleisom.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef __EMSHA_TEST_UTILS_HH
#define __EMSHA_TEST_UTILS_HH
#include <cstdint>
#include <string>
#include "emsha/emsha.h"
#include "emsha/hmac.h"
#include "emsha/sha256.h"
// How many times should a test result be checked? The goal is to
// ensure that the 'result' method is idempotent. My science teachers
// always said 5 data points was the minimum you needed, so there you
// go. Thanks, Mr. Franklin.
constexpr uint32_t RESULT_ITERATIONS = 5;
// Test data structures.
struct hashTest {
std::string output;
std::string input;
};
struct hmacTest {
std::uint8_t key[256];
std::uint32_t keylen;
std::string input;
std::string output;
};
// General-purpose debuggery.
void DumpHexString(std::string&, std::uint8_t *, std::uint32_t);
void dump_pair(std::uint8_t *, std::uint8_t *);
// SHA-256 testing functions.
emsha::EMSHAResult runHashTest(const struct hashTest& test, const std::string& label);
int runHashTests(const struct hashTest *tests, const std::size_t nTests,
const std::string& label);
// HMAC-SHA-256 testery.
emsha::EMSHAResult runHMACTest(struct hmacTest& test, const std::string& label);
int runHMACTests(const struct hmacTest *tests, std::size_t nTests,
const std::string& label);
#ifdef EMSHA_NO_HEXSTRING
namespace emsha {
void hexstring(uint8_t *dest, uint8_t *src, uint32_t srclen);
}
#endif // EMSHA_NO_HEXSTRING
#endif // __EMSHA_TEST_UTILS_HH