diff options
Diffstat (limited to 'com/BLAKE2/test')
-rw-r--r-- | com/BLAKE2/test/blake2s_kat.c | 31 | ||||
-rwxr-xr-x | com/BLAKE2/test/blake2s_kat.py | 40 |
2 files changed, 71 insertions, 0 deletions
diff --git a/com/BLAKE2/test/blake2s_kat.c b/com/BLAKE2/test/blake2s_kat.c new file mode 100644 index 0000000..0581c18 --- /dev/null +++ b/com/BLAKE2/test/blake2s_kat.c @@ -0,0 +1,31 @@ +#include <assert.h> +#include <blake2s.h> +#include <stdio.h> +#include <string.h> + +#include "blake2s_kat.h" + +int +main(void) +{ + uint8_t in[256]; + for (int i = 0; i < 256; ++i) in[i] = i; + uint8_t out[32]; + + for (unsigned i = 0; i < KATs_len; ++i) { + assert(BLAKE2s(out, 32, NULL, 0, in, i) == 0); + assert(memcmp(out, KATs[i], 32) == 0); + } + + for (unsigned i = 0; i < 1; ++i) { + _Static_assert(sizeof(KAT_secret) == 32, "sizeof KAT_secret"); + assert(BLAKE2s(out, 32, KAT_secret, 32, in, i) == 0); + + for (unsigned j = 0; j < 32; ++j) printf("%02x ", out[j]); + printf("\n"); + for (unsigned j = 0; j < 32; ++j) printf("%02x ", secret_KATs[i][j]); + printf("\n"); + + assert(memcmp(out, secret_KATs[i], 32) == 0); + } +} diff --git a/com/BLAKE2/test/blake2s_kat.py b/com/BLAKE2/test/blake2s_kat.py new file mode 100755 index 0000000..2dd5370 --- /dev/null +++ b/com/BLAKE2/test/blake2s_kat.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# Known Answer Test generator + +import json +import hashlib +import secrets + +def blake2s(w, key): + ctx = hashlib.blake2s(key=key) + ctx.update(w) + return ctx.digest().hex() + +def out(s): + o = [s[i:i+2] for i in range(0, len(s), 2)] # split into pairs + o = [f'0x{i}' for i in o] # prepend 0x and join + return ', '.join(o) + +if __name__ == '__main__': + w = b'' + for i in range(0, 256): + w += i.to_bytes(1, 'little') + k = secrets.token_bytes(32) + + print('#pragma once\n') + print(f'static const unsigned KATs_len = 256;') + print(f'static const uint8_t KAT_secret[32] = {{ {", ".join([hex(i) for i in k])} }};') + + print(f'static const uint8_t KATs[256][32] = {{') + for i in range(0, 256): + o = blake2s(w[0:i], b'') + print(f' // {i}') + print(f' {{ {out(o)} }},') + print(f'}};') + + print(f'static const uint8_t secret_KATs[256][32] = {{') + for i in range(0, 256): + o = blake2s(w[0:i], k) + print(f' // {i}') + print(f' {{ {out(o)} }},') + print(f'}};') |