aboutsummaryrefslogtreecommitdiff
path: root/com/BLAKE2/test/blake2s_kat.py
diff options
context:
space:
mode:
authoraqua <aqua@iserlohn-fortress.net>2022-07-29 11:35:16 +0300
committeraqua <aqua@iserlohn-fortress.net>2022-12-28 21:01:45 +0200
commitbb6439d85b36d9ebbaf82a08e707428e8a7ef234 (patch)
treec8841eea0913f8fcda621870875422b1d6f28394 /com/BLAKE2/test/blake2s_kat.py
parentAdd BLAKE2s implementation (diff)
downloadkernel-bb6439d85b36d9ebbaf82a08e707428e8a7ef234.tar.xz
BLAKE2s: add Known Answer Tests
Diffstat (limited to 'com/BLAKE2/test/blake2s_kat.py')
-rwxr-xr-xcom/BLAKE2/test/blake2s_kat.py40
1 files changed, 40 insertions, 0 deletions
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'}};')