ccx-utils

Miscellaneous utilities written in C
git clone https://ccx.te2000.cz/git/ccx-utils
Log | Files | Refs

gen-miniroon.py (1120B)


      1 #!/usr/bin/env python3
      2 import sys
      3 import hmac
      4 
      5 
      6 class NetString(bytes):
      7     @classmethod
      8     def from_any(cls, data):
      9         if isinstance(data, NetString):
     10             return cls(data)
     11         if isinstance(data, bytes):
     12             return cls(to_ns(data))
     13         if isinstance(data, str):
     14             return cls(to_ns(data.encode('ascii')))
     15         return cls(to_ns(b''.join(cls.from_any(i) for i in data)))
     16 
     17 
     18 def to_ns(b):
     19     assert isinstance(b, bytes)
     20     return NetString(b'%d:%s,' % (len(b), b))
     21 
     22 
     23 def miniroon_hmac(key, msg):
     24     print('miniroon_hmac%r' % ((key, msg),), file=sys.stderr)
     25     #return hmac.digest(key, msg, 'blake2s')
     26     return hmac.digest(key, msg, 'sha256')
     27 
     28 
     29 def make_miniroon(name, action='invoke-once', secret=b'\0'*32, caveats=(), version='capv0'):
     30     hdr = b''.join(NetString.from_any(i) for i in (version, name, action))
     31     sig = miniroon_hmac(secret, hdr)
     32     for c in caveats:
     33         sig = miniroon_hmac(sig, c)
     34     return NetString.from_any([
     35         hdr,
     36         caveats,
     37         sig,
     38     ])
     39 
     40 
     41 if __name__ == '__main__':
     42     import os
     43     os.write(1, make_miniroon(name='ccx'))