diff options
Diffstat (limited to 'com/BLAKE2/test/blake2s_selftest.c')
-rw-r--r-- | com/BLAKE2/test/blake2s_selftest.c | 102 |
1 files changed, 102 insertions, 0 deletions
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 <assert.h> +#include <stdio.h> + +_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; +} |