From dbd16766dae9210cb93e3408abc0c53db322e31a Mon Sep 17 00:00:00 2001 From: aqua Date: Mon, 1 Aug 2022 11:22:54 +0300 Subject: Move blake2s selftest to test/ --- com/BLAKE2/meson.build | 7 +-- com/BLAKE2/test/blake2s_selftest.c | 102 +++++++++++++++++++++++++++++++++++++ com/BLAKE2/test_fns.c | 34 ------------- com/BLAKE2/test_main.c | 97 ----------------------------------- 4 files changed, 106 insertions(+), 134 deletions(-) create mode 100644 com/BLAKE2/test/blake2s_selftest.c delete mode 100644 com/BLAKE2/test_fns.c delete mode 100644 com/BLAKE2/test_main.c (limited to 'com') diff --git a/com/BLAKE2/meson.build b/com/BLAKE2/meson.build index 1592dd7..7d180e3 100644 --- a/com/BLAKE2/meson.build +++ b/com/BLAKE2/meson.build @@ -1,14 +1,15 @@ #BLAKE2s = static_library('BLAKE2s', 'blake2s.c') BLAKE2s_native = shared_library('BLAKE2s_native', 'blake2s.c', native: true) -test('BLAKE2s functions', executable('b2s_fns', 'test_fns.c', link_with: BLAKE2s_native, native: true), suite: 'BLAKE2') -test('BLAKE2s selftest', executable('b2s_selftest', 'test_main.c', link_with: BLAKE2s_native, native: true), suite: 'BLAKE2') - kat = generator(python3, arguments: '@INPUT@', capture: true, output: '@BASENAME@.h' ) +test('BLAKE2s selftest', + executable('b2s_selftest', 'test/blake2s_selftest.c', link_with: BLAKE2s_native, native: true), + suite: 'BLAKE2' +) test('BLAKE2s KAT', executable('b2s_kat', ['test/blake2s_kat.c', kat.process('test/blake2s_kat.py')], link_with: BLAKE2s_native, native: true), diff --git a/com/BLAKE2/test/blake2s_selftest.c b/com/BLAKE2/test/blake2s_selftest.c new file mode 100644 index 0000000..3ada150 --- /dev/null +++ b/com/BLAKE2/test/blake2s_selftest.c @@ -0,0 +1,102 @@ +// test_main.c +// Self test Modules for BLAKE2b and BLAKE2s -- and a stub main(). + +#include "blake2s.h" +#include +#include + +_Static_assert(sizeof(struct BLAKE2s_param) == (8 * sizeof(uint32_t)), "sizeof struct BLAKE2s_param"); + +// Deterministic sequences (Fibonacci generator). +static void +selftest_seq(uint8_t *out, size_t len, uint32_t seed) +{ + size_t i; + uint32_t t, a, b; + + a = 0xDEAD4BAD * seed; // prime + b = 1; + + for (i = 0; i < len; i++) { // fill the buf + t = a + b; + a = b; + b = t; + out[i] = (t >> 24) & 0xFF; + } +} + +// BLAKE2s self-test validation. Return 0 when OK. +int +blake2s_selftest() +{ + // Grand hash of hash results. + const uint8_t blake2s_res[32] = {0x6A, 0x41, 0x1F, 0x08, 0xCE, 0x25, 0xAD, 0xCD, 0xFB, 0x02, 0xAB, + 0xA6, 0x41, 0x45, 0x1C, 0xEC, 0x53, 0xC5, 0x98, 0xB2, 0x4F, 0x4F, + 0xC7, 0x87, 0xFB, 0xDC, 0x88, 0x79, 0x7F, 0x4C, 0x1D, 0xFE}; + // Parameter sets. + const size_t b2s_md_len[4] = {16, 20, 28, 32}; + const size_t b2s_in_len[6] = {0, 3, 64, 65, 255, 1024}; + + size_t i, j, outlen, inlen; + uint8_t in[1024], md[32], key[32]; + struct BLAKE2s_ctx ctx; + + // 256-bit hash for testing. + if (BLAKE2s_init(&ctx, 32, NULL, 0)) return -1; + + for (i = 0; i < 4; i++) { + outlen = b2s_md_len[i]; + for (j = 0; j < 6; j++) { + inlen = b2s_in_len[j]; + + selftest_seq(in, inlen, inlen); // unkeyed hash + BLAKE2s(md, outlen, NULL, 0, in, inlen); + BLAKE2s_update(&ctx, md, outlen); // hash the hash + + selftest_seq(key, outlen, outlen); // keyed hash + BLAKE2s(md, outlen, key, outlen, in, inlen); + BLAKE2s_update(&ctx, md, outlen); // hash the hash + } + } + + // Compute and compare the hash of hashes. + BLAKE2s_final(&ctx, md); + for (i = 0; i < 32; i++) { + if (md[i] != blake2s_res[i]) return -1; + } + + return 0; +} + +int +main(void) +{ + // functions + assert(rotr_u32(0xdecafade, 16) == 0xfadedeca); + assert(rotr_u32(0xdecafade, 8) == 0xdedecafa); + + const int good = blake2s_selftest(); + printf("blake2s_selftest() = %s\n", good ? "FAIL" : "OK"); + + char *in = "abc"; + size_t inlen = 3; + + uint8_t out[32]; + BLAKE2s(out, 32, NULL, 0, in, inlen); + + const uint8_t blake2s_res[32] = {0x50, 0x8C, 0x5E, 0x8C, 0x32, 0x7C, 0x14, 0xE2, 0xE1, 0xA7, 0x2B, + 0xA3, 0x4E, 0xEB, 0x45, 0x2F, 0x37, 0x45, 0x8B, 0x20, 0x9E, 0xD6, + 0x3A, 0x29, 0x4D, 0x99, 0x9B, 0x4C, 0x86, 0x67, 0x59, 0x82}; + + for (unsigned i = 0; i < 32; i++) { + if (out[i] != blake2s_res[i]) { + printf("digest failed"); + printf("\nout: "); + for (unsigned n = 0; n < 32; ++n) printf(" %02x", out[n]); + printf("\nres: "); + for (unsigned n = 0; n < 32; ++n) printf(" %02x", blake2s_res[n]); + return -1; + } + } + return good; +} diff --git a/com/BLAKE2/test_fns.c b/com/BLAKE2/test_fns.c deleted file mode 100644 index ca3551a..0000000 --- a/com/BLAKE2/test_fns.c +++ /dev/null @@ -1,34 +0,0 @@ -#include "blake2s.h" -#include -#include - -_Static_assert(sizeof(struct BLAKE2s_param) == (8 * sizeof(uint32_t)), "sizeof struct BLAKE2s_param"); - -int -main(void) -{ - assert(rotr_u32(0xdecafade, 16) == 0xfadedeca); - assert(rotr_u32(0xdecafade, 8) == 0xdedecafa); - - union { - struct BLAKE2s_param block; - uint32_t data[8]; - } p; - p.block.outlen = 32; - p.block.keylen = 0; - p.block.fanout = 1; - p.block.depth = 1; - p.block.leaf_length = 0; - p.block.node_offset = 0; - p.block.node_offset_ex = 0; - p.block.node_depth = 0; - p.block.inner_length = 0; - p.block.salt = 0; - p.block.personalization = 0; - - for (int i = 0; i < 8; ++i) printf("%08x ", p.data[i]); - printf("\n"); - - assert(p.data[0] == (0x01010000 ^ 32)); - -} diff --git a/com/BLAKE2/test_main.c b/com/BLAKE2/test_main.c deleted file mode 100644 index 80828bb..0000000 --- a/com/BLAKE2/test_main.c +++ /dev/null @@ -1,97 +0,0 @@ -// test_main.c -// Self test Modules for BLAKE2b and BLAKE2s -- and a stub main(). - -#include "blake2s.h" -#include - -// Deterministic sequences (Fibonacci generator). - -static void -selftest_seq(uint8_t *out, size_t len, uint32_t seed) -{ - size_t i; - uint32_t t, a, b; - - a = 0xDEAD4BAD * seed; // prime - b = 1; - - for (i = 0; i < len; i++) { // fill the buf - t = a + b; - a = b; - b = t; - out[i] = (t >> 24) & 0xFF; - } -} - -// BLAKE2s self-test validation. Return 0 when OK. -int -blake2s_selftest() -{ - // Grand hash of hash results. - const uint8_t blake2s_res[32] = {0x6A, 0x41, 0x1F, 0x08, 0xCE, 0x25, 0xAD, 0xCD, 0xFB, 0x02, 0xAB, - 0xA6, 0x41, 0x45, 0x1C, 0xEC, 0x53, 0xC5, 0x98, 0xB2, 0x4F, 0x4F, - 0xC7, 0x87, 0xFB, 0xDC, 0x88, 0x79, 0x7F, 0x4C, 0x1D, 0xFE}; - // Parameter sets. - const size_t b2s_md_len[4] = {16, 20, 28, 32}; - const size_t b2s_in_len[6] = {0, 3, 64, 65, 255, 1024}; - - size_t i, j, outlen, inlen; - uint8_t in[1024], md[32], key[32]; - struct BLAKE2s_ctx ctx; - - // 256-bit hash for testing. - if (BLAKE2s_init(&ctx, 32, NULL, 0)) return -1; - - for (i = 0; i < 4; i++) { - outlen = b2s_md_len[i]; - for (j = 0; j < 6; j++) { - inlen = b2s_in_len[j]; - - selftest_seq(in, inlen, inlen); // unkeyed hash - BLAKE2s(md, outlen, NULL, 0, in, inlen); - BLAKE2s_update(&ctx, md, outlen); // hash the hash - - selftest_seq(key, outlen, outlen); // keyed hash - BLAKE2s(md, outlen, key, outlen, in, inlen); - BLAKE2s_update(&ctx, md, outlen); // hash the hash - } - } - - // Compute and compare the hash of hashes. - BLAKE2s_final(&ctx, md); - for (i = 0; i < 32; i++) { - if (md[i] != blake2s_res[i]) return -1; - } - - return 0; -} - -// Test driver. -int -main(void) -{ - const int good = blake2s_selftest(); - printf("blake2s_selftest() = %s\n", good ? "FAIL" : "OK"); - - char *in = "abc"; - size_t inlen = 3; - - uint8_t out[32]; - BLAKE2s(out, 32, NULL, 0, in, inlen); - - const uint8_t blake2s_res[32] = {0x50, 0x8C, 0x5E, 0x8C, 0x32, 0x7C, 0x14, 0xE2, 0xE1, 0xA7, 0x2B, - 0xA3, 0x4E, 0xEB, 0x45, 0x2F, 0x37, 0x45, 0x8B, 0x20, 0x9E, 0xD6, - 0x3A, 0x29, 0x4D, 0x99, 0x9B, 0x4C, 0x86, 0x67, 0x59, 0x82}; - - for (unsigned i = 0; i < 32; i++) { - if (out[i] != blake2s_res[i]) { - printf("digest failed"); - printf("\nout: "); - for (unsigned n = 0; n < 32; ++n) printf(" %02x", out[n]); - printf("\nres: "); - for (unsigned n = 0; n < 32; ++n) printf(" %02x", blake2s_res[n]); - return -1; - } - } - return good; -} -- cgit v1.2.1