From 21be46a0b5364c5f00f4d081ad9524ae9a36d022 Mon Sep 17 00:00:00 2001 From: jaseg Date: Thu, 8 Nov 2018 19:53:11 +0900 Subject: HID report transmission partially works now partially i.e. if you attach the keyboard before the noise handshake. I suspect some memory corruption somewhere. --- src/demo.c | 45 +++++++++++++++++++++++++------- src/noise.c | 71 ++++++++++++++++++++++++++++++-------------------- src/noise.h | 1 + src/packet_interface.c | 6 +++++ src/packet_interface.h | 2 ++ src/usart_helpers.c | 6 ----- src/usart_helpers.h | 1 - 7 files changed, 87 insertions(+), 45 deletions(-) (limited to 'src') diff --git a/src/demo.c b/src/demo.c index 60f3ddc..d080844 100644 --- a/src/demo.c +++ b/src/demo.c @@ -113,22 +113,47 @@ static void gpio_setup(void) gpio_mode_setup(GPIOE, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO3 | GPIO4); } +enum packet_types { + _RESERVED = 0, + HID_KEYBOARD_REPORT = 1, + HID_MOUSE_REPORT = 2 +}; + +struct hid_report_packet { + uint8_t type; + uint8_t len; + uint8_t report[8]; +}; + static void hid_in_message_handler(uint8_t device_id, const uint8_t *data, uint32_t length) { - UNUSED(device_id); - UNUSED(data); if (length < 4) { - LOG_PRINTF("data too short, type=%d\n", hid_get_type(device_id)); + LOG_PRINTF("HID report too short\n"); return; } - - // print only first 4 bytes, since every mouse should have at least these four set. - // Report descriptors are not read by driver for now, so we do not know what each byte means - LOG_PRINTF("HID EVENT %02X %02X %02X %02X \n", data[0], data[1], data[2], data[3]); - /* - if (hid_get_type(device_id) == HID_TYPE_KEYBOARD) { + if (length > 8) { + LOG_PRINTF("HID report too long\n"); + return; } - */ + + int type = hid_get_type(device_id); + if (type != HID_TYPE_KEYBOARD && type != HID_TYPE_MOUSE) { + LOG_PRINTF("Unsupported HID report type %x\n", type); + return; + } + + LOG_PRINTF("Sending event %02X %02X %02X %02X\n", data[0], data[1], data[2], data[3]); + struct hid_report_packet pkt = { + .type = type == HID_TYPE_KEYBOARD ? HID_KEYBOARD_REPORT : HID_MOUSE_REPORT, + .len = length, + .report = {0} + }; + memcpy(pkt.report, data, length); + + if (send_encrypted_message((uint8_t *)&pkt, sizeof(pkt))) { + LOG_PRINTF("Error sending HID report packet\n"); + return; + } } volatile struct { diff --git a/src/noise.c b/src/noise.c index a2cbb10..83f2e4c 100644 --- a/src/noise.c +++ b/src/noise.c @@ -16,11 +16,12 @@ } while(0); -static uint8_t local_key[CURVE25519_KEY_LEN]; -NoiseCipherState *tx_cipher, *rx_cipher; volatile uint8_t host_packet_buf[MAX_HOST_PACKET_SIZE]; volatile uint8_t host_packet_length = 0; +static uint8_t local_key[CURVE25519_KEY_LEN]; +static NoiseCipherState *tx_cipher = NULL, *rx_cipher = NULL; + NoiseHandshakeState *start_protocol_handshake() { /* TODO Noise-C is nice for prototyping, but we should really get rid of it for mostly three reasons: @@ -71,19 +72,15 @@ errout: } NoiseHandshakeState *try_continue_noise_handshake(NoiseHandshakeState *handshake) { -#define MAX_MESSAGE_LEN 256 - uint8_t message[MAX_MESSAGE_LEN]; + int err; + uint8_t message[MAX_HOST_PACKET_SIZE]; NoiseBuffer noise_msg; /* Run the protocol handshake */ switch (noise_handshakestate_get_action(handshake)) { case NOISE_ACTION_WRITE_MESSAGE: /* Write the next handshake message with a zero-length payload */ noise_buffer_set_output(noise_msg, message, sizeof(message)); - if (noise_handshakestate_write_message(handshake, &noise_msg, NULL) != NOISE_ERROR_NONE) { - LOG_PRINTF("Error writing handshake message\n"); - noise_handshakestate_free(handshake); - handshake = NULL; - } + HANDLE_NOISE_ERROR(noise_handshakestate_write_message(handshake, &noise_msg, NULL), "writing handshake message"); send_packet(usart2_out, message, noise_msg.size); break; @@ -91,29 +88,23 @@ NoiseHandshakeState *try_continue_noise_handshake(NoiseHandshakeState *handshake if (host_packet_length > 0) { /* Read the next handshake message and discard the payload */ noise_buffer_set_input(noise_msg, (uint8_t *)host_packet_buf, host_packet_length); - if (noise_handshakestate_read_message(handshake, &noise_msg, NULL) != NOISE_ERROR_NONE) { - LOG_PRINTF("Error reading handshake message\n"); - noise_handshakestate_free(handshake); - handshake = NULL; - } + HANDLE_NOISE_ERROR(noise_handshakestate_read_message(handshake, &noise_msg, NULL), "reading handshake message"); host_packet_length = 0; /* Acknowledge to USART ISR the buffer has been handled */ } break; case NOISE_ACTION_SPLIT: - if (noise_handshakestate_split(handshake, &tx_cipher, &rx_cipher) != NOISE_ERROR_NONE) { - LOG_PRINTF("Error splitting handshake state\n"); + HANDLE_NOISE_ERROR(noise_handshakestate_split(handshake, &tx_cipher, &rx_cipher), "splitting handshake state"); + LOG_PRINTF("Noise protocol handshake completed successfully, handshake hash:\n"); + + uint8_t buf[BLAKE2S_HASH_SIZE]; + if (noise_handshakestate_get_handshake_hash(handshake, buf, sizeof(buf)) != NOISE_ERROR_NONE) { + LOG_PRINTF("Error fetching noise handshake state\n"); } else { - LOG_PRINTF("Noise protocol handshake completed successfully, handshake hash:\n"); - uint8_t buf[BLAKE2S_HASH_SIZE]; - if (noise_handshakestate_get_handshake_hash(handshake, buf, sizeof(buf)) != NOISE_ERROR_NONE) { - LOG_PRINTF("Error fetching noise handshake state\n"); - } else { - LOG_PRINTF(" "); - for (size_t i=0; iirqn); } -void send_packet(struct dma_usart_file *f, const uint8_t *data, size_t len) { - /* ignore return value as putf is blocking and always succeeds */ - (void)cobs_encode_incremental(f, putf, (char *)data, len); - flush(f); -} - diff --git a/src/usart_helpers.h b/src/usart_helpers.h index bd50d00..531652a 100644 --- a/src/usart_helpers.h +++ b/src/usart_helpers.h @@ -63,7 +63,6 @@ int dma_fifo_push(volatile struct dma_buf *buf, char c); int putf(void *file, char c); int putb(void *file, const uint8_t *buf, size_t len); void flush(void *file); -void send_packet(struct dma_usart_file *f, const uint8_t *data, size_t len); /* This macro abomination templates a bunch of dma-specific register/constant names from preprocessor macros passed in * from cmake. */ -- cgit