summaryrefslogtreecommitdiff
path: root/hexnoise.py
diff options
context:
space:
mode:
authorjaseg <git@jaseg.net>2018-11-08 16:35:31 +0900
committerjaseg <git@jaseg.net>2018-11-08 16:35:31 +0900
commit333d94bf9f1b4e4c83a125342dd20fea62589057 (patch)
tree7a7b5e785e4f9282c58002ff23c44b5599b4c222 /hexnoise.py
parent050d49a56bc54c78c3fc1f4f125b09634d6c5b83 (diff)
downloadsecure-hid-333d94bf9f1b4e4c83a125342dd20fea62589057.tar.gz
secure-hid-333d94bf9f1b4e4c83a125342dd20fea62589057.tar.bz2
secure-hid-333d94bf9f1b4e4c83a125342dd20fea62589057.zip
Noise handshake working
Diffstat (limited to 'hexnoise.py')
-rwxr-xr-xhexnoise.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/hexnoise.py b/hexnoise.py
new file mode 100755
index 0000000..b092bb3
--- /dev/null
+++ b/hexnoise.py
@@ -0,0 +1,82 @@
+#!/usr/bin/env python3
+
+import time
+import string
+
+from cobs import cobs
+
+def _print_line(write, ts, line, width=16):
+ h,m,s,ms = int(ts//3600), int((ts//60)%60), int(ts%60), int((ts%1.0) * 1000)
+ timestamp = f'{h: 3d}:{m:02d}:{s:02d}:{ms:03d}'
+ line = list(line) + [None]*(width-len(line))
+ hexcol = '\033[94m'
+ col = lambda b, s: s if b != 0 else f'\033[91m{s}{hexcol}'
+ hexfmt = ' '.join(
+ ' '.join(col(b, f'{b:02x}') if b is not None else ' ' for b in line[i*8:i*8+8])
+ for i in range(1 + (len(line)-1)//8))
+ asciifmt = ''.join(chr(c) if c is not None and chr(c) in string.printable else '.' for c in line)
+ write(f'\033[38;5;244m{timestamp} {hexcol}{hexfmt} \033[38;5;244m|\033[92m{asciifmt}\033[38;5;244m|\033[0m', flush=True, end='')
+
+startup = time.time()
+
+def hexdump(write, packet, width=16):
+ ts = time.time()
+ while len(packet) > width:
+ chunk, packet = packet[:width], packet[width:]
+ _print_line(write, ts-startup, chunk, width=width)
+ write()
+ _print_line(write, ts-startup, packet, width=width)
+ write()
+
+def send_packet(ser, data, width=16):
+ encoded = cobs.encode(data) + b'\0'
+ print(f'\033[93mSending {len(data)} bytes\033[0m')
+ hexdump(print, encoded, width)
+ ser.write(encoded)
+ ser.flushOutput()
+
+def receive_packet(ser, width=16):
+ packet = ser.read_until(b'\0')
+ data = cobs.decode(packet[:-1])
+ print(f'\033[93mReceived {len(data)} bytes\033[0m')
+ hexdump(print, data, width)
+ return data
+
+if __name__ == '__main__':
+ import argparse
+ import serial
+
+ parser = argparse.ArgumentParser()
+ parser.add_argument('serial')
+ parser.add_argument('baudrate')
+ parser.add_argument('-w', '--width', type=int, default=16, help='Number of bytes to display in one line')
+ args = parser.parse_args()
+
+ ser = serial.Serial(args.serial, args.baudrate)
+
+ from noise.connection import NoiseConnection, Keypair
+
+ STATIC_LOCAL = bytes([
+ 0xbb, 0xdb, 0x4c, 0xdb, 0xd3, 0x09, 0xf1, 0xa1,
+ 0xf2, 0xe1, 0x45, 0x69, 0x67, 0xfe, 0x28, 0x8c,
+ 0xad, 0xd6, 0xf7, 0x12, 0xd6, 0x5d, 0xc7, 0xb7,
+ 0x79, 0x3d, 0x5e, 0x63, 0xda, 0x6b, 0x37, 0x5b
+ ])
+
+ proto = NoiseConnection.from_name(b'Noise_XX_25519_ChaChaPoly_BLAKE2s')
+ proto.set_as_initiator()
+ proto.set_keypair_from_private_bytes(Keypair.STATIC, STATIC_LOCAL)
+ proto.start_handshake()
+ print('\033[91mHandshake started\033[0m')
+
+ while True:
+ if proto.handshake_finished:
+ break
+ send_packet(ser, proto.write_message(), args.width)
+
+ if proto.handshake_finished:
+ break
+ proto.read_message(receive_packet(ser, args.width))
+ print('Handshake finished, handshake hash:')
+ hexdump(print, proto.get_handshake_hash(), args.width)
+