Commit 61aead95 authored by Jon Griffiths's avatar Jon Griffiths

sha256: Use hex for test results

Users should be able to verify our crypto tests by searching for the
vectors we use. Make that easier by using hex for the expected results.

A nice side effect is that the code is simpler and endian agnostic too.
parent a5472f05
......@@ -45,6 +45,11 @@ int main(int argc, char *argv[])
return 0;
}
if (strcmp(argv[1], "testdepends") == 0) {
printf("ccan/str/hex\n");
return 0;
}
if (strcmp(argv[1], "libs") == 0) {
#ifdef CCAN_CRYPTO_SHA256_USE_OPENSSL
printf("crypto\n");
......
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/str/hex/hex.h>
/* Include the C files directly. */
#include <ccan/crypto/sha256/sha256.c>
#include <ccan/tap/tap.h>
......@@ -7,47 +8,30 @@
struct test {
const char *test;
size_t repetitions;
beint32_t result[8];
const char *result;
};
static struct test tests[] = {
{ "", 1,
{ CPU_TO_BE32(0xe3b0c442), CPU_TO_BE32(0x98fc1c14),
CPU_TO_BE32(0x9afbf4c8), CPU_TO_BE32(0x996fb924),
CPU_TO_BE32(0x27ae41e4), CPU_TO_BE32(0x649b934c),
CPU_TO_BE32(0xa495991b), CPU_TO_BE32(0x7852b855) } },
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" },
{ "abc", 1,
{ CPU_TO_BE32(0xba7816bf), CPU_TO_BE32(0x8f01cfea),
CPU_TO_BE32(0x414140de), CPU_TO_BE32(0x5dae2223),
CPU_TO_BE32(0xb00361a3), CPU_TO_BE32(0x96177a9c),
CPU_TO_BE32(0xb410ff61), CPU_TO_BE32(0xf20015ad) } },
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" },
{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
{ CPU_TO_BE32(0x248d6a61), CPU_TO_BE32(0xd20638b8),
CPU_TO_BE32(0xe5c02693), CPU_TO_BE32(0x0c3e6039),
CPU_TO_BE32(0xa33ce459), CPU_TO_BE32(0x64ff2167),
CPU_TO_BE32(0xf6ecedd4), CPU_TO_BE32(0x19db06c1) } },
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" },
{ "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 1,
{ CPU_TO_BE32(0xcf5b16a7), CPU_TO_BE32(0x78af8380),
CPU_TO_BE32(0x036ce59e), CPU_TO_BE32(0x7b049237),
CPU_TO_BE32(0x0b249b11), CPU_TO_BE32(0xe8f07a51),
CPU_TO_BE32(0xafac4503), CPU_TO_BE32(0x7afee9d1) } },
"cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1" },
{ "a", 1000000,
{ CPU_TO_BE32(0xcdc76e5c), CPU_TO_BE32(0x9914fb92),
CPU_TO_BE32(0x81a1c7e2), CPU_TO_BE32(0x84d73e67),
CPU_TO_BE32(0xf1809a48), CPU_TO_BE32(0xa497200e),
CPU_TO_BE32(0x046d39cc), CPU_TO_BE32(0xc7112cd0) } }
"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0" }
#if 0 /* Good test, but takes ages! */
, { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno", 16777216,
{ CPU_TO_BE32(0x50e72a0e), CPU_TO_BE32(0x26442fe2),
CPU_TO_BE32(0x552dc393), CPU_TO_BE32(0x8ac58658),
CPU_TO_BE32(0x228c0cbf), CPU_TO_BE32(0xb1d2ca87),
CPU_TO_BE32(0x2ae43526), CPU_TO_BE32(0x6fcd055e) } }
, { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno",
16777216,
"50e72a0e26442fe2552dc3938ac58658228c0cbfb1d2ca872ae435266fcd055e" },
#endif
};
static bool do_test(const struct test *t)
{
struct sha256 h;
struct sha256 h, expected;
if (t->repetitions == 1)
sha256(&h, t->test, strlen(t->test));
......@@ -60,17 +44,19 @@ static bool do_test(const struct test *t)
sha256_done(&ctx, &h);
}
return memcmp(&h, t->result, sizeof(h)) == 0;
hex_decode(t->result, strlen(t->result), &expected, sizeof(expected));
return memcmp(&h, &expected, sizeof(h)) == 0;
}
int main(void)
{
const size_t num_tests = sizeof(tests) / sizeof(tests[0]);
size_t i;
/* This is how many tests you plan to run */
plan_tests(sizeof(tests) / sizeof(tests[0]));
plan_tests(num_tests);
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++)
for (i = 0; i < num_tests; i++)
ok1(do_test(&tests[i]));
/* This exits depending on whether all tests passed */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment