diff options
author | aqua <aqua@iserlohn-fortress.net> | 2022-07-24 18:49:43 +0300 |
---|---|---|
committer | aqua <aqua@iserlohn-fortress.net> | 2022-12-28 21:01:39 +0200 |
commit | e5d541428e910a5df98728bc46d80a612603cf13 (patch) | |
tree | a5d0a9c497ff7b67ae60815a7450253023db6c05 /com/BLAKE2/test_main.c | |
parent | Add test and valgrind targets to rules.mk (diff) | |
download | kernel-e5d541428e910a5df98728bc46d80a612603cf13.tar.xz |
Add BLAKE2s implementation
Diffstat (limited to 'com/BLAKE2/test_main.c')
-rw-r--r-- | com/BLAKE2/test_main.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/com/BLAKE2/test_main.c b/com/BLAKE2/test_main.c new file mode 100644 index 0000000..80828bb --- /dev/null +++ b/com/BLAKE2/test_main.c @@ -0,0 +1,97 @@ +// test_main.c +// Self test Modules for BLAKE2b and BLAKE2s -- and a stub main(). + +#include "blake2s.h" +#include <stdio.h> + +// 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; +} |