aboutsummaryrefslogtreecommitdiff
path: root/lib/tst/blake2s_genkat.py
diff options
context:
space:
mode:
authoraqua <aqua@iserlohn-fortress.net>2022-12-31 16:03:20 +0200
committeraqua <aqua@iserlohn-fortress.net>2023-01-29 11:41:29 +0200
commitf6b7365dc8759d6df5d340e4a68fb75537c07be4 (patch)
treec15614d8dee445dd4c6ae75da2959c701e69daae /lib/tst/blake2s_genkat.py
parentMove blake2s selftest to test/ (diff)
downloadkernel-f6b7365dc8759d6df5d340e4a68fb75537c07be4.tar.xz
blake2: use makefiles instead of meson.build
Diffstat (limited to 'lib/tst/blake2s_genkat.py')
-rwxr-xr-xlib/tst/blake2s_genkat.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/lib/tst/blake2s_genkat.py b/lib/tst/blake2s_genkat.py
new file mode 100755
index 0000000..2dd5370
--- /dev/null
+++ b/lib/tst/blake2s_genkat.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'}};')