From 4fcc3337e21089b50a1034fa05c69d4bd7b84640 Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 23 Mar 2021 20:19:24 +0100 Subject: Make mouse demo work --- fw/hexnoise.py | 34 +++++++++++++++++++++++++++++----- fw/src/demo.c | 3 +++ fw/src/noise.c | 18 +++++++++++++++++- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/fw/hexnoise.py b/fw/hexnoise.py index 2313f8f..549f583 100755 --- a/fw/hexnoise.py +++ b/fw/hexnoise.py @@ -6,6 +6,7 @@ import sys from contextlib import contextmanager, suppress, wraps import hashlib import secrets +import struct import serial from cobs import cobs @@ -336,14 +337,19 @@ class NoiseEngine: raise ProtocolError('Invalid report type') def uinput_passthrough(self): - with uinput.Device(KeyMapper.ALL_KEYS) as ui: + mouse_foo = [uinput.BTN_LEFT, uinput.BTN_RIGHT, uinput.BTN_MIDDLE, + uinput.REL_X, uinput.REL_Y, uinput.REL_WHEEL] + with uinput.Device(KeyMapper.ALL_KEYS + mouse_foo) as ui: old_kcs = set() for msg_type, payload in self.receive_loop(): report_len, *report = payload - if report_len != 8: - raise ValueError('Unsupported report length', report_len) + + report = bytes(report[:report_len]) if msg_type is ReportType.KEYBOARD: + if report_len != 8: + raise ValueError('Unsupported report length', report_len) + modbyte, _reserved, *keycodes = report import binascii keys = { *KeyMapper.map_modifiers(modbyte), *KeyMapper.map_regulars(keycodes) } @@ -358,8 +364,26 @@ class NoiseEngine: old_kcs = keys elif msg_type is ReportType.MOUSE: - # FIXME unhandled - pass + if report_len < 3 or report_len > 8: + raise ValueError('Unsupported report length', report_len) + import binascii + _report_type, buttons, a, b, c, w, _2 = report + x = ((b&0x0f)<<8) | a + y = ((b&0xf0) >> 4) | (c<<4) + if x >= 2048: + x -= 4096 + if y >= 2048: + y -= 4096 + if w >= 128: + w -= 256 + #print('got mouse report', binascii.hexlify(report), buttons, x, y, w) + ui.emit(uinput.BTN_LEFT, bool(buttons&1), syn=False) + ui.emit(uinput.BTN_MIDDLE, bool(buttons&4), syn=False) + ui.emit(uinput.BTN_RIGHT, bool(buttons&2), syn=False) + ui.emit(uinput.REL_X, x, syn=False) + ui.emit(uinput.REL_Y, y, syn=False) + ui.emit(uinput.REL_WHEEL, w, syn=False) + ui.syn() if __name__ == '__main__': import argparse diff --git a/fw/src/demo.c b/fw/src/demo.c index f20e35c..69f8e8e 100644 --- a/fw/src/demo.c +++ b/fw/src/demo.c @@ -296,6 +296,7 @@ void pairing_input(uint8_t modbyte, uint8_t keycode) { case KEY_ENTER: pairing_buf[pairing_buf_pos++] = '\0'; if (!pairing_check(&noise_state, pairing_buf)) { + LOG_PRINTF("Pairing success, persisting remote key.\n"); persist_remote_key(&noise_state); /* FIXME write key to backup memory */ @@ -303,6 +304,8 @@ void pairing_input(uint8_t modbyte, uint8_t keycode) { if (send_encrypted_message(&noise_state, &response, sizeof(response))) LOG_PRINTF("Error sending pairing response packet\n"); + noise_state.failed_handshakes = 0; + } else { /* FIXME sound alarm */ diff --git a/fw/src/noise.c b/fw/src/noise.c index 90aaf36..6405d49 100644 --- a/fw/src/noise.c +++ b/fw/src/noise.c @@ -188,7 +188,7 @@ void uninit_handshake(struct NoiseState *st, enum handshake_state new_state) { noise_handshakestate_free(st->handshake); st->handshake_state = new_state; st->handshake = NULL; - arm_key_scrubber(); + //arm_key_scrubber(); FIXME DEBUG } /*@ @@ -291,9 +291,19 @@ int handshake_phase2(struct NoiseState * const st, uint8_t *buf, size_t len) { BLAKE2s_update(&bc, st->remote_key, sizeof(st->remote_key)); BLAKE2s_finish(&bc, remote_fp); + LOG_PRINTF("Key in memory: "); + for (int i=0; iremote_key_reference[i]); + LOG_PRINTF("\n"); + //@ ghost key_checked_trace = 1; if (!fc_memcmp_uint8(remote_fp, st->remote_key_reference, sizeof(remote_fp))) { /* keys match */ //@ ghost key_match_trace = 1; + LOG_PRINTF("Keys match, accepting peer.\n"); uint8_t response = REPORT_PAIRING_SUCCESS; if (send_encrypted_message(st, &response, sizeof(response))) LOG_PRINTF("Error sending pairing response packet\n"); @@ -303,6 +313,7 @@ int handshake_phase2(struct NoiseState * const st, uint8_t *buf, size_t len) { return 1; } else { /* keys don't match */ + LOG_PRINTF("Keys don't match, requiring pairing.\n"); uint8_t response = REPORT_PAIRING_START; if (send_encrypted_message(st, &response, sizeof(response))) LOG_PRINTF("Error sending pairing response packet\n"); @@ -403,6 +414,11 @@ void persist_remote_key(struct NoiseState *st) { BLAKE2s_update(&bc, st->remote_key, sizeof(st->remote_key)); BLAKE2s_finish(&bc, st->remote_key_reference); st->handshake_state = HANDSHAKE_DONE_KNOWN_HOST; + + LOG_PRINTF("Key in memory: "); + for (int i=0; iremote_key_reference[i]); + LOG_PRINTF("\n"); } /*@ -- cgit