aboutsummaryrefslogtreecommitdiff
path: root/lib/tst/blake2s_genkat.py
blob: 2dd53700e71548795b0f7f184b0f0c72c20fabbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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'}};')